[Libav-user] Problem with using libavcodec with AV_CODEC_ID_H264

Ashwin Chandra - SISA ashwin.c at sisa.samsung.com
Fri Feb 8 17:38:45 CET 2013


But the decoding_encoding example works for me without any changes. The problem is if you just want to encode two frames and flush them all out. With 25 there doesn't seem to be any issues.


From: libav-user-bounces at ffmpeg.org [mailto:libav-user-bounces at ffmpeg.org] On Behalf Of Harald Jordan
Sent: Friday, February 08, 2013 12:53 AM
To: 'This list is about using libavcodec, libavformat, libavutil, libavdevice and libavfilter.'
Subject: Re: [Libav-user] Problem with using libavcodec with AV_CODEC_ID_H264

If you did not make any changes to the open_video and add_video functions of the decoding_encoding example, it cannot work. For H264 you need to manually define a set a of H264 private settings before opening the codec.
http://stackoverflow.com/questions/3553003/encoding-h-264-with-libavcodec-x264

Harry

Von: libav-user-bounces at ffmpeg.org<mailto:libav-user-bounces at ffmpeg.org> [mailto:libav-user-bounces at ffmpeg.org] Im Auftrag von Ashwin Chandra - SISA
Gesendet: Freitag, 08. Februar 2013 00:29
An: This list is about using libavcodec, libavformat, libavutil, libavdevice and libavfilter.
Betreff: Re: [Libav-user] Problem with using libavcodec with AV_CODEC_ID_H264

To be clearer, I modified the decoding_encoding.c example in ffmpeg to cause the problem.
I basically changed the video_encode_example() to encode 1 second worth of video and then flush the delayed frame. The code below is exactly the same except 25 frames was replaced with 1 frame.
Now if I take this code block and duplicate it again, so that it basically runs twice, the second avcodec_encode_video2() will hang.


/* encode 1 second of video */
    for(i=0;i<1;i++) {
        av_init_packet(&pkt);
        pkt.data = NULL;    // packet data will be allocated by the encoder
        pkt.size = 0;

        fflush(stdout);
        /* prepare a dummy image */
        /* Y */
        for(y=0;y<c->height;y++) {
            for(x=0;x<c->width;x++) {
                frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
           }
        }

        /* Cb and Cr */
        for(y=0;y<c->height/2;y++) {
            for(x=0;x<c->width/2;x++) {
                frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
                frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
            }
        }

        frame->pts = i;

        /* encode the image */
        ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
        if (ret < 0) {
            fprintf(stderr, "Error encoding frame\n");
            exit(1);
        }

        if (got_output) {
            printf("Write frame %3d (size=%5d)\n", i, pkt.size);
            fwrite(pkt.data, 1, pkt.size, f);
            av_free_packet(&pkt);
        }
    }

    /* get the delayed frames */
    for (got_output = 1; got_output; i++) {
        fflush(stdout);

        ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
        if (ret < 0) {
            fprintf(stderr, "Error encoding frame\n");
            exit(1);
        }

        if (got_output) {
            printf("Write frame %3d (size=%5d)\n", i, pkt.size);
            fwrite(pkt.data, 1, pkt.size, f);
            av_free_packet(&pkt);
        }
    }



From: libav-user-bounces at ffmpeg.org<mailto:libav-user-bounces at ffmpeg.org> [mailto:libav-user-bounces at ffmpeg.org] On Behalf Of Ashwin Chandra - SISA
Sent: Thursday, February 07, 2013 3:08 PM
To: libav-user at ffmpeg.org<mailto:libav-user at ffmpeg.org>
Subject: [Libav-user] Problem with using libavcodec with AV_CODEC_ID_H264

I have some code that takes a running stream of uncompressed video data and encodes it using AV_CODEC_ID_H264.

The sequence I follow is

1.  Call avcodec_encode_video2 on the AVFrame which contains my uncompressed frame.

2.  Call avcodec_encode_video2 again passing NULL in the AVFrame parameter.

3.  Repeat 2. Until a frame arrives from the encoder.

This seems to work fine if the codec is MPEG2, but with H264, it hangs inside avcodec_encode_video2 at step 2) on the second frame. I don't have debug symbols and can't figure out why. Does there need to be a minimum set of uncompressed data in the encoder before trying to flush out an encoded frame for H264? If so, how do I know when it is safe to flush a frame?



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20130208/6063378c/attachment.html>


More information about the Libav-user mailing list