From 374eaf20e76e2a9413329ed11f16613ed2687edb Mon Sep 17 00:00:00 2001 From: Dmitry Kovalev <dkovalev@google.com> Date: Wed, 12 Feb 2014 16:26:58 -0800 Subject: [PATCH] Flushing the encoder in simple_decoder example. Change-Id: I39d78facc79a364cdc529b8cc4930d74f766d413 --- examples/simple_encoder.c | 55 ++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/examples/simple_encoder.c b/examples/simple_encoder.c index 2e7c7a6678..e419e81e10 100644 --- a/examples/simple_encoder.c +++ b/examples/simple_encoder.c @@ -99,6 +99,33 @@ void usage_exit() { exit(EXIT_FAILURE); } +static void encode_frame(vpx_codec_ctx_t *codec, + vpx_image_t *img, + int frame_index, + VpxVideoWriter *writer) { + vpx_codec_iter_t iter = NULL; + const vpx_codec_cx_pkt_t *pkt = NULL; + const vpx_codec_err_t res = vpx_codec_encode(codec, img, frame_index, 1, 0, + VPX_DL_GOOD_QUALITY); + if (res != VPX_CODEC_OK) + die_codec(codec, "Failed to encode frame"); + + while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL) { + if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) { + const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0; + if (!vpx_video_writer_write_frame(writer, + pkt->data.frame.buf, + pkt->data.frame.sz, + pkt->data.frame.pts)) { + die_codec(codec, "Failed to write compressed frame"); + } + + printf(keyframe ? "K" : "."); + fflush(stdout); + } + } +} + int main(int argc, char **argv) { FILE *infile = NULL; vpx_codec_ctx_t codec; @@ -166,30 +193,10 @@ int main(int argc, char **argv) { if (vpx_codec_enc_init(&codec, encoder->interface(), &cfg, 0)) die_codec(&codec, "Failed to initialize encoder"); - while (vpx_img_read(&raw, infile)) { - vpx_codec_iter_t iter = NULL; - const vpx_codec_cx_pkt_t *pkt = NULL; - - ++frame_count; - - res = vpx_codec_encode(&codec, &raw, frame_count, 1, 0, - VPX_DL_GOOD_QUALITY); - if (res != VPX_CODEC_OK) - die_codec(&codec, "Failed to encode frame"); - - while ((pkt = vpx_codec_get_cx_data(&codec, &iter)) != NULL) { - if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) { - const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0; - if (!vpx_video_writer_write_frame(writer, - pkt->data.frame.buf, - pkt->data.frame.sz, - pkt->data.frame.pts)) - die_codec(&codec, "Failed to write compressed frame."); - printf(keyframe ? "K" : "."); - fflush(stdout); - } - } - } + while (vpx_img_read(&raw, infile)) + encode_frame(&codec, &raw, frame_count++, writer); + encode_frame(&codec, NULL, -1, writer); // flush the encoder + printf("\n"); fclose(infile); printf("Processed %d frames.\n", frame_count); -- GitLab