Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi There

I am trying to get MJPG encoding working, I have AV_CODEC_ID_MPEG1VIDEO working, but when I switched to AV_CODEC_ID_MJPEG I get an error -22

The code fails on the last part with "could not open codec".

Any help would be greatly appreciated!

This is the code:

C++
AVCodecID codec_id = AV_CODEC_ID_MJPEG; //AV_CODEC_ID_MPEG1VIDEO;
    AVCodec* codec = avcodec_find_encoder(codec_id);
    if (!codec) 
    { 
        printf("Codec not found\n"); 
        return -1;
    }

    // Initialize codec. 
    AVCodecContext* codecContext = avcodec_alloc_context3(codec); 

    codecContext->codec_id = codec_id;
    codecContext->codec_type = AVMEDIA_TYPE_VIDEO;
    codecContext->pix_fmt = PIX_FMT_YUV422P; 
    codecContext->bit_rate = 400000;
    codecContext->width = 320; 
    codecContext->height = 240;
    codecContext->time_base.num = 1; 
    codecContext->time_base.den = 25; 

   int retval;
    // Open the codec. 
    if ((retval = avcodec_open2(codecContext, codec, NULL)) < 0) 
    { 
        printf("could not open codec\n"); 
        return -2;
    }
Posted
Comments
KarstenK 15-Mar-14 4:25am    
it isnt a video codec .-O

1 solution

Try using PIX_FMT_YUVJ422P instead of PIX_FMT_YUV422P.
I found that in this[^] forum question. That question and answer is from Oct. 2012, so things could have changed.

If that does not solve your problem, I noticed the following comment in pixfmt.h:
C++
AV_PIX_FMT_YUVJ422P,  ///< planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_range

So you may just have to specify the color range to make it work. Unfortunately, I don't know the right way to do that off-hand (I have only used the MJPEG decoder, not the encoder).

As a side-note, I believe you should change your naming convention from PIX_FMT_xxxx to the newer AV_PIX_FMT_xxxx. However, they do have the same value in their respective enums, so that is not your problem.

Soren Madsen
 
Share this answer
 
Comments
godspeed123 17-Mar-14 1:51am    
Thanks for the help! That allows me to intialize the encoder!
SoMad 17-Mar-14 2:08am    
It worked? That is awesome!

Soren Madsen

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900