[Libav-user] Encoding multiples images with H264

avpicture aykut_avci at hotmail.com
Mon Nov 26 17:22:05 CET 2012


Hello all,

The code below is to encode couple of pictures using H264. 
Everything seems fine but I don't see any encoded data in "encoded_buf".
Could you please comment on it?

So the ret variables are to check the return values. 
ret1 = 5760000
ret2 = 2160000
ret3 = 900
ret4 = 0

PS: mfSetContextParameters function is to set the context parameters. I will
place the code at the end of the page.

Thank you.

---------------------------
    AVCodec *codec;
    int encoded_buf_size;
    uint8_t *encoded_buf, *yuvoutbuffer;
    AVFrame* YUVframe;
    AVFrame *RGBframe;
    int ret1=0, ret2=0, ret3=0, ret4=0, ret5=0;

    YUVframe = avcodec_alloc_frame();
    RGBframe = avcodec_alloc_frame();

    c = mfSetContextParameters(c, aWidth, aHeight, mvGOPsize);

    codec = avcodec_find_encoder(CODEC_ID_H264);
    if (!codec) {gpLogDebug("Video codec not found."); return;}
    if (avcodec_open(c, codec) < 0) {gpLogDebug("Could not open codec.");
return;}

    int nbytes = avpicture_get_size(PIX_FMT_YUV420P, c->width, c->height);
    yuvoutbuffer=(uint8_t *) av_malloc(nbytes);
    YUVframe->pts = 0;

    ret1=avpicture_fill((AVPicture*)RGBframe, (uint8_t *) bits[0],
PIX_FMT_RGB32, c->width, c->height);
    ret2=avpicture_fill((AVPicture*)YUVframe, yuvoutbuffer, PIX_FMT_YUV420P,
c->width, c->height);

    struct SwsContext* fooContext = sws_getContext(c->width, c->height,
                                      PIX_FMT_RGB32,
                                      c->width, c->height,
                                      PIX_FMT_YUV420P,
                                      SWS_FAST_BILINEAR, NULL, NULL, NULL);


    //perform the conversion
    ret3=sws_scale(fooContext, RGBframe->data, RGBframe->linesize, 0,
c->height, YUVframe->data, YUVframe->linesize);
    // Here is where I try to convert to YUV
    encoded_buf_size = 200000;
    encoded_buf = (uint8_t *) malloc(encoded_buf_size);
    ret4 = avcodec_encode_video(c, encoded_buf, encoded_buf_size, YUVframe);

    av_free(encoded_buf);
    av_free(RGBframe);
    av_free(YUVframe);
    av_free(yuvoutbuffer);

-----------------------------------------

    c= avcodec_alloc_context();
    c->bit_rate = 512000;
    c->width = aWidth;
    c->height = aHeight;
    c->time_base.num = 1;
    c->time_base.den = 30;
    c->gop_size = mvGOPsize;
    c->max_b_frames=0;
    c->bit_rate_tolerance= 40000;
    c->pix_fmt = PIX_FMT_YUV420P;
    c->me_range = 16;
    c->max_qdiff = 1;
    c->qmin = 1;
    c->qmax = 30;
    c->qcompress = 0.6;
    c->keyint_min = 120;
    c->refs = 3;
    c->trellis =1;
    c->rtp_payload_size = 500;
    c->level=13; //Level 1.3
    c->me_method=7;
    c->qblur=0.5;
    c->profile=66;//Baseline
    c->thread_count=6;




--
View this message in context: http://libav-users.943685.n4.nabble.com/Encoding-multiples-images-with-H264-tp4656150.html
Sent from the libav-users mailing list archive at Nabble.com.


More information about the Libav-user mailing list