[Libav-user] avcodec_decode_video2 problem

Dolevo Jay barikondunar at hotmail.com
Wed Dec 5 11:51:12 CET 2012


Hi experts,


I am having a problem when I am decoding my encoded data which 
is done by x264_encoder_encode. Basically, I get a frame and encode it 
with h264_encoder_encode. Then I send the NAL to the decoder and there I
 try to decode it with avcodec_decode_video2.


The problem is,  


lenDecoded = avcodec_decode_video2(c1, av_pic, &got_picture, &pkt);



lenDecoded returns always -1 and got_picture returns always 0
 while "no frame!" error appears on the screen. I waited some time, I 
thought that it needs some time to decode but no joy.


I will provide you all the code here and I am expecting your priceless guidance and help. 


Thank you.



//ENCODING PARAMETERS, It is currently set for zero latency and ultra fast encoding.

void SetParameters(EncoderType et, int aWidth, int aHeight, int quantizationFactor, int gopSize )

{

    ////////

    convertCtx = sws_getContext(aWidth, aHeight, PIX_FMT_RGB24, 
aWidth, aHeight, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);

    x264_picture_alloc(&pic_in, X264_CSP_I420, aWidth, aHeight);    //memory allocation for being encoded data.

    srcstride = aWidth*3;                               //RGB stride is just 3*width

    ////////


    Framebuffersize=0;


    x264_param_default( ¶m );


    x264_param_default_preset(¶m, "veryfast", "zerolatency");       //Default preset.


    Framebuffer = (uint8_t*) malloc(MAX_FRAME_SIZE);


    mux_buffer_size = 0;

    mux_buffer = NULL;


    //Set video resolution

    param.i_width = aWidth;

    param.i_height = aHeight;


    //We don't need bframe

    param.i_bframe = 0;

    //param.i_bframe_adaptive = 0;


    //Maximum dimension of the gop

    param.i_keyint_min = 2;

    param.i_keyint_max = 5;


    param.rc.i_lookahead = 0;

    param.i_threads = 1;

    //param.i_sync_lookahead = 0;


    //More high, more compressed but loss quality

    quantization_factor = 30;


    //Setting CRF

    param.rc.i_rc_method = X264_RC_CRF;

    //Specify quality of encoding

    param.rc.f_rf_constant = quantization_factor;


    //only pass1 mode for low latency

    param.rc.b_stat_read = 0;

    param.rc.b_stat_write = 1;


    param.i_threads = 1;                                    //The number of threads that x264 will use

    param.i_width = aWidth;

    param.i_height = aHeight;

    param.i_fps_num = 5;                                    //Apparently GOP size

    param.i_fps_den = 1;

    // Intra refres:

    param.b_intra_refresh = 1;

    //Rate control:

    param.rc.i_rc_method = X264_RC_CRF;

    param.rc.f_rf_constant = 25;

    param.rc.f_rf_constant_max = 35;

    //For streaming:

    param.b_annexb = 1;

    param.b_repeat_headers = 1;

    //my params:

    param.i_log_level = X264_LOG_DEBUG;

    //AA added

    param.i_bframe_adaptive = 0;

    param.rc.i_lookahead = 0;

    param.i_bframe = 0;

    param->i_frame_reference = 1;

    param->i_scenecut_threshold = 0;

    param->b_deblocking_filter = 0;

    param->b_cabac = 0;

    param->i_bframe = 0;

    param->analyse.intra = 0;

    param->analyse.inter = 0;

    param->analyse.b_transform_8x8 = 0;

    param->analyse.i_me_method = X264_ME_DIA;

    param->analyse.i_subpel_refine = 0;

    param->rc.i_aq_mode = 0;

    param->analyse.b_mixed_references = 0;

    param->analyse.i_trellis = 0;


    x264_param_apply_profile(¶m, "baseline");           //baseline profile is selected.


    //If ultrafast flag is enabled the transcoding latency is reduced,

    //the bandwith of the encoder is also duplicated, but the dimension

    //of the encoded frame increase, also by a factor of 2

    if(et == X264_ULTRAFAST)

    {

        setParamForUltrafastEncoding( ¶m );

    }


    //Open the encoder

    if( ( h = x264_encoder_open( ¶m ) ) == NULL )

    {

        fprintf( stderr, "x264 [error]: x264_encoder_open failed\n" );

        exit(1);

    }


    this->quantization_factor = quantizationFactor;

    //param.i_keyint_max = gopSize;

}




//ENCODING STARTS HERE

void mpStartEncodingFrame(const char ** bits, int aWidth, int aHeight, int mvGOPsize, qint32 tstamp)

{

    sws_scale(convertCtx, (uint8_t**)bits, &srcstride, 0, aHeight, pic_in.img.plane, pic_in.img.i_stride);

    pic_in.i_dts=tstamp;

    pic_in.i_type = X264_TYPE_I;

    pic_in.i_qpplus1 = 0;


    int a = Encode_frame( h , &pic_in );


    //The encoded data (Framebuffer) is here sent to the decoder side,


    free(pBits);

}



//Actual encoding,

int  Encode_frame( x264_t *h, x264_picture_t *pic )

{


    int i_nal, i_nalu_size;

    i_nalu_size = 0;


    int frame_size=x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out );

    if(i_nal>1)

        int yy=1;


    if( frame_size < 0 )

    {

        fprintf( stderr, "x264 [error]: x264_encoder_encode failed\n" );

        return -1;

    }


    memcpy(Framebuffer, nal[0].p_payload, frame_size);

    Framebuffersize = frame_size;


    return Framebuffersize;

}



//DECODING

int decoderDecodeFrame(uint8_t* buffer, int buffersize)

{

    AVPacket pkt;

    int got_picture = 0, lenDecoded;

    av_init_packet(&pkt);

    AVFormatContext *a;

    AVCodecContext *c1;

    avcodec_register_all();


    av_init_packet(&pkt);


    codec = avcodec_find_decoder(CODEC_ID_H264);

    if ( !codec ) {

        fprintf(stderr, "[avcodec][Err] codec not found!\n");

         exit(1);

    }

    c1 = avcodec_alloc_context3(codec);

    c1->width = 1600;

    c1->height = 900;

    c1->codec_id = CODEC_ID_H264;


    c1->codec_type = AVMEDIA_TYPE_VIDEO;


    av_pic = avcodec_alloc_frame();


    if(codec->capabilities&CODEC_CAP_TRUNCATED)

               c1->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete

    frames */


        /* open it */

        if (avcodec_open2(c1, codec, NULL) < 0) {

            fprintf(stderr, "[avcodec][Err] could not open codec\n");

            exit(1);

        }


    pkt.data=buffer;

    pkt.size=buffersize;


    lenDecoded = avcodec_decode_video2(c1, av_pic, &got_picture, &pkt);


    if (lenDecoded < 0)

    {

           fprintf(stderr, "Error while decoding frame %d\n %d", av_pic, lenDecoded);

           exit(1);

    }


    if (got_picture)

        printf("OK");

        return 1;


    return 0;

}
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20121205/4fbf690b/attachment.html>


More information about the Libav-user mailing list