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: [email protected] [mailto:[email protected]] On 
Behalf Of Ashwin Chandra - SISA
Sent: Thursday, February 07, 2013 3:08 PM
To: [email protected]
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?



_______________________________________________
Libav-user mailing list
[email protected]
http://ffmpeg.org/mailman/listinfo/libav-user

Reply via email to