31 avcodec_register_all();
32 AVCodec* codec_h265 = avcodec_find_decoder(AV_CODEC_ID_H265);
33 if (codec_h265 ==
nullptr) {
34 AERROR <<
"error: codec not found";
37 codec_ctx_h265_ = avcodec_alloc_context3(codec_h265);
38 if (codec_ctx_h265_ ==
nullptr) {
39 AERROR <<
"error: codec context alloc fail";
42 if (avcodec_open2(codec_ctx_h265_, codec_h265,
nullptr) < 0) {
43 AERROR <<
"error: could not open codec";
46 yuv_frame_ = av_frame_alloc();
47 if (yuv_frame_ ==
nullptr) {
48 AERROR <<
"error: could not alloc yuv frame";
52 if (codec_jpeg ==
nullptr) {
53 AERROR <<
"error: jpeg Codec not found";
56 codec_ctx_jpeg_ = avcodec_alloc_context3(codec_jpeg);
57 if (codec_ctx_jpeg_ ==
nullptr) {
58 AERROR <<
"error: jpeg ctx allco fail";
62 codec_ctx_jpeg_->bit_rate = 400000;
63 codec_ctx_jpeg_->codec_type = AVMEDIA_TYPE_VIDEO;
65 codec_ctx_jpeg_->width = 1920;
66 codec_ctx_jpeg_->height = 1080;
67 codec_ctx_jpeg_->time_base = (AVRational){1, 15};
68 codec_ctx_jpeg_->pix_fmt = AV_PIX_FMT_YUVJ422P;
69 if (avcodec_open2(codec_ctx_jpeg_, codec_jpeg,
nullptr) < 0) {
70 AERROR <<
"error: could not open jpeg context";
92 const uint8_t* indata,
const int32_t insize,
93 std::vector<uint8_t>* outdata)
const {
98 apt.data =
const_cast<uint8_t*
>(indata);
104 avcodec_decode_video2(codec_ctx_h265_, yuv_frame_, &got_picture, &apt);
106 AERROR <<
"error: decode failed: input_framesize = " << apt.size
107 <<
". error code = " << ret;
112 AWARN <<
"warn: failed to get yuv picture";
115 av_packet_unref(&apt);
117 ret = avcodec_encode_video2(codec_ctx_jpeg_, &apt, yuv_frame_, &got_picture);
119 AERROR <<
"error: jpeg encode failed, error code = " << ret;
123 AERROR <<
"error: failed to get jpeg picture from yuyv";
126 outdata->resize(apt.size);
127 std::copy(apt.data, apt.data + apt.size, outdata->begin());