shithub: libvpx

Download patch

ref: 374eaf20e76e2a9413329ed11f16613ed2687edb
parent: c00d88cdae3cd326f29108587062fd7afbe184c4
author: Dmitry Kovalev <[email protected]>
date: Wed Feb 12 11:26:58 EST 2014

Flushing the encoder in simple_decoder example.

Change-Id: I39d78facc79a364cdc529b8cc4930d74f766d413

--- a/examples/simple_encoder.c
+++ b/examples/simple_encoder.c
@@ -99,6 +99,33 @@
   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 @@
   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;
+  while (vpx_img_read(&raw, infile))
+    encode_frame(&codec, &raw, frame_count++, writer);
+  encode_frame(&codec, NULL, -1, writer);  // flush the encoder
 
-    ++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);
-      }
-    }
-  }
   printf("\n");
   fclose(infile);
   printf("Processed %d frames.\n", frame_count);