0
我正在创建视频会议应用程序。我发现摄像头(至少有3个)为mJPEG输出格式提供更高的分辨率和帧速率。到目前为止,我使用YUY2,在I420中转换为X264进行压缩。要将mJPEG转码为I420,我需要先解码它。我正在尝试使用libavcodec从网络摄像头解码图像。这是我的代码。用libavcodec解码mJPEG
初始化:
// mJPEG to I420 conversion
AVCodecContext * _transcoder = nullptr;
AVFrame * _outputFrame;
AVPacket _inputPacket;
avcodec_register_all();
_outputFrame = av_frame_alloc();
av_frame_unref(_outputFrame);
av_init_packet(&_inputPacket);
AVCodec * codecDecode = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
_transcoder = avcodec_alloc_context3(codecDecode);
avcodec_get_context_defaults3(_transcoder, codecDecode);
_transcoder->flags2 |= CODEC_FLAG2_FAST;
_transcoder->pix_fmt = AVPixelFormat::AV_PIX_FMT_YUV420P;
_transcoder->width = width;
_transcoder->height = height;
avcodec_open2(_transcoder, codecDecode, nullptr);
解码:
_inputPacket.size = size;
_inputPacket.data = data;
int got_picture;
int decompressed_size = avcodec_decode_video2(_transcoder, _outputFrame, &got_picture, &_inputPacket);
但到目前为止,我所得到的是一个绿色的屏幕。我错在哪里?
UPD: 我已启用libavcodec日志记录,但没有警告或错误。 此外我还发现_outputframe具有AV_PIX_FMT_YUVJ422P作为格式和色彩空间,它不适合libavcodec枚举中的任何值(实际值为156448160)。
1)这是太小的代码片断肯定地说。 2)由于解码格式取决于源码流,并且会被解码器覆盖,因此您无法真正将解码编码为yuv420p(宽度/高度相同)。因此,在查看解码结果时,请确保使用正确的色彩空间。 3)你是否启用了ffmpeg/libav logging(av_log_set_callback),看看他们是否输出错误? – nobody555
@ nobody555,感谢您的见解。我用新的信息更新了答案。请让我知道你需要哪些代码来更好地理解问题。关于2) - 所以,如果我需要I420作为输出,我应该如何设置它?是否有可能通过libavcodec从mJPEG获得I420,或者我应该寻找其他解决方案? –
如果你需要的I420比你解码mjpeg的任何libavcodec决定(取决于你的来源),并使用swscale将其转换为I420。 – nobody555