2014-05-03 104 views
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)。

+0

1)这是太小的代码片断肯定地说。 2)由于解码格式取决于源码流,并且会被解码器覆盖,因此您无法真正将解码编码为yuv420p(宽度/高度相同)。因此,在查看解码结果时,请确保使用正确的色彩空间。 3)你是否启用了ffmpeg/libav logging(av_log_set_callback),看看他们是否输出错误? – nobody555

+0

@ nobody555,感谢您的见解。我用新的信息更新了答案。请让我知道你需要哪些代码来更好地理解问题。关于2) - 所以,如果我需要I420作为输出,我应该如何设置它?是否有可能通过libavcodec从mJPEG获得I420,或者我应该寻找其他解决方案? –

+0

如果你需要的I420比你解码mjpeg的任何libavcodec决定(取决于你的来源),并使用swscale将其转换为I420。 – nobody555

回答

0

经过评论建议后,我想出了工作解决方案。

初始化:

av_init_packet(&_inputPacket); 

AVCodec * codecDecode = avcodec_find_decoder(AV_CODEC_ID_MJPEG); 

_transcoder = avcodec_alloc_context3(codecDecode); 
avcodec_get_context_defaults3(_transcoder, codecDecode); 
avcodec_open2(_transcoder, codecDecode, nullptr); 

// swscale contex init 
mJPEGconvertCtx = sws_getContext(width, height, AV_PIX_FMT_YUVJ422P, 
     width, height, AV_PIX_FMT_YUV420P, SWS_FAST_BILINEAR, nullptr, nullptr, nullptr); 

// x264 pic init 
x264_picture_t _pic_in; 
x264_picture_alloc(&_pic_in, X264_CSP_I420, width, height); 
_pic_in.img.i_csp = X264_CSP_I420 ; 
_pic_in.img.i_plane = 3; 
_pic_in.img.i_stride[0] = width; 
_pic_in.img.i_stride[1] = width/2; 
_pic_in.img.i_stride[2] = width/2; 

_pic_in.i_type = X264_TYPE_AUTO ; 
_pic_in.i_qpplus1 = 0; 

转码:

_inputPacket.size = size; 
_inputPacket.data = data; 

int got_picture; 

// decode 
avcodec_decode_video2(_transcoder, _outputFrame, &got_picture, &_inputPacket); 

// transform 
sws_scale(_mJPEGconvertCtx, _outputFrame->data, _outputFrame->linesize, 0, height, 
     _pic_in.img.plane, _pic_in.img.i_stride); 

然后,_pic_in由X264直接使用。图像很好,但转码时间对于更高分辨率来说是可怕的。