[FFmpeg-cvslog] Merge commit '5f102a9559099429826e84758b8b5182244c52db'

Clément Bœsch git at videolan.org
Tue Apr 4 12:44:21 EEST 2017


ffmpeg | branch: master | Clément Bœsch <cboesch at gopro.com> | Tue Apr  4 11:44:45 2017 +0200| [4ea942f2ceaafbfed43933895bd0e8aad043ca44] | committer: Clément Bœsch

Merge commit '5f102a9559099429826e84758b8b5182244c52db'

* commit '5f102a9559099429826e84758b8b5182244c52db':
  examples/encode_video: switch to the new encoding API

Merged-by: Clément Bœsch <cboesch at gopro.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=4ea942f2ceaafbfed43933895bd0e8aad043ca44
---

 doc/examples/encode_video.c | 59 ++++++++++++++++++++++++---------------------
 1 file changed, 31 insertions(+), 28 deletions(-)

diff --git a/doc/examples/encode_video.c b/doc/examples/encode_video.c
index fc576e0..f29e9fb 100644
--- a/doc/examples/encode_video.c
+++ b/doc/examples/encode_video.c
@@ -36,12 +36,39 @@
 #include <libavutil/opt.h>
 #include <libavutil/imgutils.h>
 
+static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
+                   FILE *outfile)
+{
+    int ret;
+
+    /* send the frame to the encoder */
+    ret = avcodec_send_frame(enc_ctx, frame);
+    if (ret < 0) {
+        fprintf(stderr, "Error sending a frame for encoding\n");
+        exit(1);
+    }
+
+    while (ret >= 0) {
+        ret = avcodec_receive_packet(enc_ctx, pkt);
+        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
+            return;
+        else if (ret < 0) {
+            fprintf(stderr, "Error during encoding\n");
+            exit(1);
+        }
+
+        printf("Write frame %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
+        fwrite(pkt->data, 1, pkt->size, outfile);
+        av_packet_unref(pkt);
+    }
+}
+
 int main(int argc, char **argv)
 {
     const char *filename, *codec_name;
     const AVCodec *codec;
     AVCodecContext *c= NULL;
-    int i, ret, x, y, got_output;
+    int i, ret, x, y;
     FILE *f;
     AVFrame *frame;
     AVPacket pkt;
@@ -150,35 +177,11 @@ int main(int argc, char **argv)
         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_packet_unref(&pkt);
-        }
+        encode(c, frame, &pkt, f);
     }
 
-    /* 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_packet_unref(&pkt);
-        }
-    }
+    /* flush the encoder */
+    encode(c, NULL, &pkt, f);
 
     /* add sequence end code to have a real MPEG file */
     fwrite(endcode, 1, sizeof(endcode), f);


======================================================================

diff --cc doc/examples/encode_video.c
index fc576e0,2ff6354..f29e9fb
--- a/doc/examples/encode_video.c
+++ b/doc/examples/encode_video.c
@@@ -31,19 -29,46 +31,46 @@@
  #include <stdlib.h>
  #include <string.h>
  
 -#include "libavcodec/avcodec.h"
 +#include <libavcodec/avcodec.h>
  
 -#include "libavutil/frame.h"
 -#include "libavutil/imgutils.h"
 +#include <libavutil/opt.h>
 +#include <libavutil/imgutils.h>
  
+ static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
+                    FILE *outfile)
+ {
+     int ret;
+ 
+     /* send the frame to the encoder */
+     ret = avcodec_send_frame(enc_ctx, frame);
+     if (ret < 0) {
 -        fprintf(stderr, "error sending a frame for encoding\n");
++        fprintf(stderr, "Error sending a frame for encoding\n");
+         exit(1);
+     }
+ 
+     while (ret >= 0) {
+         ret = avcodec_receive_packet(enc_ctx, pkt);
+         if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
+             return;
+         else if (ret < 0) {
 -            fprintf(stderr, "error during encoding\n");
++            fprintf(stderr, "Error during encoding\n");
+             exit(1);
+         }
+ 
 -        printf("encoded frame %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
++        printf("Write frame %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
+         fwrite(pkt->data, 1, pkt->size, outfile);
+         av_packet_unref(pkt);
+     }
+ }
+ 
  int main(int argc, char **argv)
  {
 -    const char *filename;
 +    const char *filename, *codec_name;
      const AVCodec *codec;
      AVCodecContext *c= NULL;
-     int i, ret, x, y, got_output;
+     int i, ret, x, y;
      FILE *f;
 -    AVFrame *picture;
 +    AVFrame *frame;
      AVPacket pkt;
      uint8_t endcode[] = { 0, 0, 1, 0xb7 };
  
@@@ -147,38 -154,14 +174,14 @@@
              }
          }
  
 -        picture->pts = i;
 +        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_packet_unref(&pkt);
-         }
 -        encode(c, picture, &pkt, f);
++        encode(c, frame, &pkt, f);
      }
  
-     /* 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_packet_unref(&pkt);
-         }
-     }
+     /* flush the encoder */
+     encode(c, NULL, &pkt, f);
  
      /* add sequence end code to have a real MPEG file */
      fwrite(endcode, 1, sizeof(endcode), f);



More information about the ffmpeg-cvslog mailing list