首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用libavcodec解码mJPEG

用libavcodec解码mJPEG
EN

Stack Overflow用户
提问于 2014-05-03 10:40:15
回答 1查看 5.1K关注 0票数 1

我正在创建视频会议应用程序。我发现网络摄像头(至少有3台)为mJPEG输出格式提供了更高的分辨率和框架。到目前为止,我使用的是YUY2,在I420中转换为使用X264进行压缩。要将mJPEG代码转换为I420,首先需要对其进行解码。我试图用libavcodec从网络摄像头解码图像。这是我的密码。

初始化:

代码语言:javascript
复制
// 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);

解码:

代码语言:javascript
复制
_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)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-05 16:32:26

在听取意见后,我想出了解决办法。

初始化:

代码语言:javascript
复制
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;

转码:

代码语言:javascript
复制
_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直接使用。图像是好的,但转码时间是可怕的更高的分辨率。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23443322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档