Decoding mJPEG with libavcodec

前端 未结 1 1426
失恋的感觉
失恋的感觉 2021-02-11 08:25

I am creating video conference application. I have discovered that webcams (at least 3 I have) provide higher resolutions and framerates for mJPEG output format. So far I was us

相关标签:
1条回答
  • 2021-02-11 09:12

    After suggestions from comments, I came up with working solution.

    Initialization:

    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;
    

    Transcoding:

    _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);
    

    Afterwards, _pic_in is used directly by x264. Image is fine, but the transcoding times are horrible for higher resolutions.

    0 讨论(0)
提交回复
热议问题