[FFmpeg-devel] [PATCH 2/3] examples/decoding_encoding: upgrade to encode_video2.
Stefano Sabatini
stefasab at gmail.com
Tue Apr 24 18:26:14 CEST 2012
On date Thursday 2012-04-12 11:32:58 +0200, Nicolas George encoded:
>
> Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
> ---
> doc/examples/decoding_encoding.c | 52 +++++++++++++++++++++++---------------
> 1 file changed, 31 insertions(+), 21 deletions(-)
>
> diff --git a/doc/examples/decoding_encoding.c b/doc/examples/decoding_encoding.c
> index 1262611..5a4a1ec 100644
> --- a/doc/examples/decoding_encoding.c
> +++ b/doc/examples/decoding_encoding.c
> @@ -228,10 +228,11 @@ static void video_encode_example(const char *filename, int codec_id)
> {
> AVCodec *codec;
> AVCodecContext *c= NULL;
> - int i, out_size, size, x, y, outbuf_size;
> + int i, ret, got_packet, x, y;
> FILE *f;
> AVFrame *picture;
> - uint8_t *outbuf;
> + AVPacket packet;
> + uint8_t sequence_end_code[] = { 0x00, 0x00, 0x01, 0xb7 };
this change may go to a separate patch
>
> printf("Video encoding\n");
>
> @@ -271,10 +272,6 @@ static void video_encode_example(const char *filename, int codec_id)
> exit(1);
> }
>
> - /* alloc image and output buffer */
> - outbuf_size = 100000 + 12*c->width*c->height;
> - outbuf = malloc(outbuf_size);
> -
> /* the image can be allocated by any means and av_image_alloc() is
> * just the most convenient way if av_malloc() is to be used */
> av_image_alloc(picture->data, picture->linesize,
> @@ -299,29 +296,42 @@ static void video_encode_example(const char *filename, int codec_id)
> }
> }
>
> + picture->pts = i;
> +
> /* encode the image */
> - out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
> - printf("encoding frame %3d (size=%5d)\n", i, out_size);
> - fwrite(outbuf, 1, out_size, f);
> + av_init_packet(&packet);
> + packet.data = NULL;
> + packet.size = 0;
> + got_packet = 0;
> + ret = avcodec_encode_video2(c, &packet, picture, &got_packet);
> + if (ret < 0) {
> + fprintf(stderr, "Video encoding failed.\n");
> + exit(1);
> + }
> + if (got_packet) {
> + printf("got packet after frame %3d (size=%5d)\n", i, packet.size);
> + fwrite(packet.data, 1, packet.size, f);
> + av_destruct_packet(&packet);
> + }
> }
>
> /* get the delayed frames */
> - for(; out_size; i++) {
> - fflush(stdout);
> -
> - out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
> - printf("write frame %3d (size=%5d)\n", i, out_size);
> - fwrite(outbuf, 1, out_size, f);
> + while (1) {
> + av_init_packet(&packet);
> + packet.data = NULL;
> + packet.size = 0;
> + got_packet = 0;
> + ret = avcodec_encode_video2(c, &packet, NULL, &got_packet);
> + if (ret < 0 || !got_packet)
> + break;
> + printf("got extra packet (size=%5d)\n", packet.size);
> + fwrite(packet.data, 1, packet.size, f);
> + av_destruct_packet(&packet);
> }
>
> /* add sequence end code to have a real mpeg file */
> - outbuf[0] = 0x00;
> - outbuf[1] = 0x00;
> - outbuf[2] = 0x01;
> - outbuf[3] = 0xb7;
> - fwrite(outbuf, 1, 4, f);
> + fwrite(sequence_end_code, 1, sizeof(sequence_end_code), f);
> fclose(f);
> - free(outbuf);
Looks fine otherwise, thanks.
--
FFmpeg = Fast & Fantastic Magnificient Philosofic Enlightened Guru
More information about the ffmpeg-devel
mailing list