<div dir="ltr">Hi Robert,<div><br></div><div>i have your same goal, also <span style="font-family:arial,sans-serif;font-size:13px">I am writing a an application that encodes incoming video in H.264 using libavcodec and libx264. I don't know the solution for yuor problem, because i do not know how to encode.</span></div>
<div><span style="font-family:arial,sans-serif;font-size:13px">i have a avi or mp4 file as input. The <b>avcodec_encode_video2</b> function takes input raw video data from frame, so i demux my file and i obtain a raw video file; now i want encode it, so how i can open the raw file, read the raw frames and encode them into h264?? Is this the right wat yo encode in H.264? There are other solutions?? How do you encode?? Can you help me, please??</span><span style="font-family:arial,sans-serif;font-size:13px"><br>
</span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">Thanks in advance. </span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br>
</span></div><div><span style="font-family:arial,sans-serif;font-size:13px">Regards</span></div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/10/21 Robert Schmidt <span dir="ltr"><<a href="mailto:rob.schmidt86@outlook.com" target="_blank">rob.schmidt86@outlook.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hello all,<br>
<br>
I am writing a an application that encodes incoming video in H.264 using libavcodec and libx264.  However, I noticed that the output data was much larger than I expected.  When I examined the output, I discovered that the encoder was only producing I- and P-frames and never producing B-frames.<br>

<br>
I created a standalone utility in an attempt to isolate my encoding logic and determine what I am doing wrong.  The utility reads in H.264 video from a file stored using the ITU H.264 Annex B format, decodes it, re-encodes it, and writes the resulting packets to another file in the Annex B format.  Like my application, the test utility fails to generate any B-frames.<br>

<br>
I ran the input file through ffmpeg using the same settings that I was providing in my utility and found that ffmpeg does produce B-frames.  I have been trying to determine what I am doing differently than ffmpeg, but I have not been able to figure it out yet.  I'm hoping that perhaps someone on this list can spot my oversight.<br>

<br>
I use the following command line with ffmpeg:<br>
<br>
    $ ffmpeg -v debug -i ~/annexb.264 -codec:v libx264 -preset superfast -g 30 -f h264 ./out.264<br>
<br>
As far as I can tell, ffmpeg should simply be using the "superfast" libx264 preset and a group of pictures setting of 30 frames.  Beyond that, it should be using the defaults.<br>
<br>
My code to set up the encoder looks like the following:<br>
<br>
static AVStream *add_video_stream(AVFormatContext *output_ctx, AVCodec **output_codec, enum AVCodecID codec_id)<br>
{<br>
    *output_codec = avcodec_find_encoder(codec_id);<br>
    if (*output_codec == NULL) {<br>
        printf("Could not find encoder for '%s' (%d)\n", avcodec_get_name(codec_id), codec_id);<br>
        return NULL;<br>
    }<br>
<br>
    AVStream *output_stream = avformat_new_stream(output_ctx, *output_codec);<br>
    if (output_stream == NULL) {<br>
        printf("Could not create video stream.\n");<br>
        return NULL;<br>
    }<br>
    output_stream->id = output_ctx->nb_streams - 1;<br>
    AVCodecContext *codec_ctx = output_stream->codec;<br>
<br>
    avcodec_get_context_defaults3(codec_ctx, *output_codec);<br>
<br>
    codec_ctx->width = 1280;<br>
    codec_ctx->height = 720;<br>
<br>
    codec_ctx->time_base.den = 15000;<br>
    codec_ctx->time_base.num = 1001;<br>
<br>
/*    codec_ctx->gop_size = 30;*/<br>
    codec_ctx->pix_fmt = AV_PIX_FMT_YUVJ420P;<br>
<br>
    // try to force B-frame output<br>
/*    codec_ctx->max_b_frames = 3;*/<br>
/*    codec_ctx->b_frame_strategy = 2;*/<br>
<br>
    output_stream->sample_aspect_ratio.num = 1;<br>
    output_stream->sample_aspect_ratio.den = 1;<br>
<br>
    codec_ctx->sample_aspect_ratio.num = 1;<br>
    codec_ctx->sample_aspect_ratio.den = 1;<br>
<br>
    codec_ctx->chroma_sample_location = AVCHROMA_LOC_LEFT;<br>
<br>
    codec_ctx->bits_per_raw_sample = 8;<br>
<br>
    if ((output_ctx->oformat->flags & AVFMT_GLOBALHEADER) != 0) {<br>
        codec_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;<br>
    }<br>
<br>
    return output_stream;<br>
}<br>
<br>
<br>
int main(int argc, char **argv)<br>
{<br>
    // ... open input file<br>
<br>
    avformat_alloc_output_context2(&output_ctx, NULL, "h264", output_path);<br>
    if (output_ctx == NULL) {<br>
        fprintf(stderr, "Unable to allocate output context.\n");<br>
        return 1;<br>
    }<br>
<br>
    AVCodec *output_codec = NULL;<br>
    output_stream = add_video_stream(output_ctx, &output_codec, output_ctx->oformat->video_codec);<br>
    if (output_stream == NULL) {<br>
        fprintf(stderr, "Error adding video stream to output context.\n");<br>
        return 1;<br>
    }<br>
    encode_ctx = output_stream->codec;<br>
<br>
    // seems to have no effect<br>
#if 0<br>
    if (decode_ctx->extradata_size != 0) {<br>
        size_t extradata_size = decode_ctx->extradata_size;<br>
        printf("extradata_size: %zu\n", extradata_size);<br>
        encode_ctx->extradata = av_mallocz(extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);<br>
        memcpy(encode_ctx->extradata, decode_ctx->extradata, extradata_size);<br>
        encode_ctx->extradata_size = extradata_size;<br>
    }<br>
#endif // 0<br>
<br>
    AVDictionary *opts = NULL;<br>
    av_dict_set(&opts, "preset", "superfast", 0);<br>
    // av_dict_set(&opts, "threads", "auto", 0); // seems to have no effect<br>
<br>
    ret = avcodec_open2(encode_ctx, output_codec, &opts);<br>
    if (ret < 0) {<br>
        fprintf(stderr, "Unable to open output video cocec: %s\n", av_err2str(ret));<br>
        return 1;<br>
    }<br>
<br>
    // ... decoding/encoding loop, clean up, etc.<br>
<br>
    return 0;<br>
}<br>
<br>
I have tried manually specifying the B-frame parameters in the AVCodecContext structure to no avail.<br>
<br>
I've also tried debugging both my utility and ffmpeg under gdb so that I can compare the values of the AVCodecContext, X264Context, and AVStream structures.   I have tried to make sure they are identical, but I still am not getting any B-frames.<br>

<br>
For a while I thought perhaps the issue was that I was mishandling timestamps, so I replicated ffmpeg's processing chain and output timestamp debugging information similar to what ffmpeg produces.  My debugging output appears identical to ffmpeg's.<br>

<br>
Does anyone have any ideas as to what I may be doing wrong?  I asked the same question on StackOverflow last week (<a href="http://stackoverflow.com/q/19456745/2895838" target="_blank">http://stackoverflow.com/q/19456745/2895838</a>) where I also included some of the logging output from both ffmpeg and my test utility.  I've omitted that here in the interest of brevity.  I also thought that the entire source code for the test utility would be too long for an e-mail, but I'm happy to provide more if that is helpful.<br>

<br>
For what it's worth, I'm currently using ffmpeg 1.2 and its libraries.<br>
<br>
Any assistance is greatly appreciated.  I've been banging my head against this one for a while now.<br>
<br>
Thanks in advance.<br>
<br>
Rob<br>
_______________________________________________<br>
Libav-user mailing list<br>
<a href="mailto:Libav-user@ffmpeg.org">Libav-user@ffmpeg.org</a><br>
<a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Francesco Damato
</div>