<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>
Hi experts,
<br><br>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.
<br><br>The problem is,
<br><br>lenDecoded = avcodec_decode_video2(c1, av_pic, &got_picture, &pkt);
<br><br><br>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.
<br><br>I will provide you all the code here and I am expecting your priceless guidance and help.
<br><br>Thank you.
<br><br><br>//ENCODING PARAMETERS, It is currently set for zero latency and ultra fast encoding.
<br>void SetParameters(EncoderType et, int aWidth, int aHeight, int quantizationFactor, int gopSize )
<br>{
<br> ////////
<br> convertCtx = sws_getContext(aWidth, aHeight, PIX_FMT_RGB24,
aWidth, aHeight, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);
<br> x264_picture_alloc(&pic_in, X264_CSP_I420, aWidth, aHeight); //memory allocation for being encoded data.
<br> srcstride = aWidth*3; //RGB stride is just 3*width
<br> ////////
<br><br> Framebuffersize=0;
<br><br> x264_param_default( śm );
<br><br> x264_param_default_preset(śm, "veryfast", "zerolatency"); //Default preset.
<br><br> Framebuffer = (uint8_t*) malloc(MAX_FRAME_SIZE);
<br><br> mux_buffer_size = 0;
<br> mux_buffer = NULL;
<br><br> //Set video resolution
<br> param.i_width = aWidth;
<br> param.i_height = aHeight;
<br><br> //We don't need bframe
<br> param.i_bframe = 0;
<br> //param.i_bframe_adaptive = 0;
<br><br> //Maximum dimension of the gop
<br> param.i_keyint_min = 2;
<br> param.i_keyint_max = 5;
<br><br> param.rc.i_lookahead = 0;
<br> param.i_threads = 1;
<br> //param.i_sync_lookahead = 0;
<br><br> //More high, more compressed but loss quality
<br> quantization_factor = 30;
<br><br> //Setting CRF
<br> param.rc.i_rc_method = X264_RC_CRF;
<br> //Specify quality of encoding
<br> param.rc.f_rf_constant = quantization_factor;
<br><br> //only pass1 mode for low latency
<br> param.rc.b_stat_read = 0;
<br> param.rc.b_stat_write = 1;
<br><br> param.i_threads = 1; //The number of threads that x264 will use
<br> param.i_width = aWidth;
<br> param.i_height = aHeight;
<br> param.i_fps_num = 5; //Apparently GOP size
<br> param.i_fps_den = 1;
<br> // Intra refres:
<br> param.b_intra_refresh = 1;
<br> //Rate control:
<br> param.rc.i_rc_method = X264_RC_CRF;
<br> param.rc.f_rf_constant = 25;
<br> param.rc.f_rf_constant_max = 35;
<br> //For streaming:
<br> param.b_annexb = 1;
<br> param.b_repeat_headers = 1;
<br> //my params:
<br> param.i_log_level = X264_LOG_DEBUG;
<br> //AA added
<br> param.i_bframe_adaptive = 0;
<br> param.rc.i_lookahead = 0;
<br> param.i_bframe = 0;
<br> param->i_frame_reference = 1;
<br> param->i_scenecut_threshold = 0;
<br> param->b_deblocking_filter = 0;
<br> param->b_cabac = 0;
<br> param->i_bframe = 0;
<br> param->analyse.intra = 0;
<br> param->analyse.inter = 0;
<br> param->analyse.b_transform_8x8 = 0;
<br> param->analyse.i_me_method = X264_ME_DIA;
<br> param->analyse.i_subpel_refine = 0;
<br> param->rc.i_aq_mode = 0;
<br> param->analyse.b_mixed_references = 0;
<br> param->analyse.i_trellis = 0;
<br><br> x264_param_apply_profile(śm, "baseline"); //baseline profile is selected.
<br><br> //If ultrafast flag is enabled the transcoding latency is reduced,
<br> //the bandwith of the encoder is also duplicated, but the dimension
<br> //of the encoded frame increase, also by a factor of 2
<br> if(et == X264_ULTRAFAST)
<br> {
<br> setParamForUltrafastEncoding( śm );
<br> }
<br><br> //Open the encoder
<br> if( ( h = x264_encoder_open( śm ) ) == NULL )
<br> {
<br> fprintf( stderr, "x264 [error]: x264_encoder_open failed\n" );
<br> exit(1);
<br> }
<br><br> this->quantization_factor = quantizationFactor;
<br> //param.i_keyint_max = gopSize;
<br>}
<br><br><br><br>//ENCODING STARTS HERE
<br>void mpStartEncodingFrame(const char ** bits, int aWidth, int aHeight, int mvGOPsize, qint32 tstamp)
<br>{
<br> sws_scale(convertCtx, (uint8_t**)bits, &srcstride, 0, aHeight, pic_in.img.plane, pic_in.img.i_stride);
<br> pic_in.i_dts=tstamp;
<br> pic_in.i_type = X264_TYPE_I;
<br> pic_in.i_qpplus1 = 0;
<br><br> int a = Encode_frame( h , &pic_in );
<br><br> //The encoded data (Framebuffer) is here sent to the decoder side,
<br><br> free(pBits);
<br>}
<br><br><br>//Actual encoding,
<br>int Encode_frame( x264_t *h, x264_picture_t *pic )
<br>{
<br><br> int i_nal, i_nalu_size;
<br> i_nalu_size = 0;
<br><br> int frame_size=x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out );
<br> if(i_nal>1)
<br> int yy=1;
<br><br> if( frame_size < 0 )
<br> {
<br> fprintf( stderr, "x264 [error]: x264_encoder_encode failed\n" );
<br> return -1;
<br> }
<br><br> memcpy(Framebuffer, nal[0].p_payload, frame_size);
<br> Framebuffersize = frame_size;
<br><br> return Framebuffersize;
<br>}
<br><br><br>//DECODING
<br>int decoderDecodeFrame(uint8_t* buffer, int buffersize)
<br>{
<br> AVPacket pkt;
<br> int got_picture = 0, lenDecoded;
<br> av_init_packet(&pkt);
<br> AVFormatContext *a;
<br> AVCodecContext *c1;
<br> avcodec_register_all();
<br><br> av_init_packet(&pkt);
<br><br> codec = avcodec_find_decoder(CODEC_ID_H264);
<br> if ( !codec ) {
<br> fprintf(stderr, "[avcodec][Err] codec not found!\n");
<br> exit(1);
<br> }
<br> c1 = avcodec_alloc_context3(codec);
<br> c1->width = 1600;
<br> c1->height = 900;
<br> c1->codec_id = CODEC_ID_H264;
<br><br> c1->codec_type = AVMEDIA_TYPE_VIDEO;
<br><br> av_pic = avcodec_alloc_frame();
<br><br> if(codec->capabilities&CODEC_CAP_TRUNCATED)
<br> c1->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete
<br> frames */
<br><br> /* open it */
<br> if (avcodec_open2(c1, codec, NULL) < 0) {
<br> fprintf(stderr, "[avcodec][Err] could not open codec\n");
<br> exit(1);
<br> }
<br><br> pkt.data=buffer;
<br> pkt.size=buffersize;
<br><br> lenDecoded = avcodec_decode_video2(c1, av_pic, &got_picture, &pkt);
<br><br> if (lenDecoded < 0)
<br> {
<br> fprintf(stderr, "Error while decoding frame %d\n %d", av_pic, lenDecoded);
<br> exit(1);
<br> }
<br><br> if (got_picture)
<br> printf("OK");
<br> return 1;
<br><br> return 0;
<br>}
</div></body>
</html>