[FFmpeg-cvslog] Merge commit 'f27e262dbdea1991b22e08b639ac03e642a3482c'

Clément Bœsch git at videolan.org
Mon Apr 3 22:14:31 EEST 2017


ffmpeg | branch: master | Clément Bœsch <u at pkh.me> | Mon Apr  3 21:12:15 2017 +0200| [a434657de9dc1084b47cd484bc69cdb1b4057844] | committer: Clément Bœsch

Merge commit 'f27e262dbdea1991b22e08b639ac03e642a3482c'

* commit 'f27e262dbdea1991b22e08b639ac03e642a3482c':
  examples/encode_audio: switch to the new audio encoding API

Merged-by: Clément Bœsch <u at pkh.me>

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

 doc/examples/encode_audio.c | 69 +++++++++++++++++++++++++++------------------
 1 file changed, 41 insertions(+), 28 deletions(-)

diff --git a/doc/examples/encode_audio.c b/doc/examples/encode_audio.c
index ba9ef6d..d1ef105 100644
--- a/doc/examples/encode_audio.c
+++ b/doc/examples/encode_audio.c
@@ -92,14 +92,42 @@ static int select_channel_layout(const AVCodec *codec)
     return best_ch_layout;
 }
 
+static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt,
+                   FILE *output)
+{
+    int ret;
+
+    /* send the frame for encoding */
+    ret = avcodec_send_frame(ctx, frame);
+    if (ret < 0) {
+        fprintf(stderr, "Error sending the frame to the encoder\n");
+        exit(1);
+    }
+
+    /* read all the available output packets (in general there may be any
+     * number of them */
+    while (ret >= 0) {
+        ret = avcodec_receive_packet(ctx, pkt);
+        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
+            return;
+        else if (ret < 0) {
+            fprintf(stderr, "Error encoding audio frame\n");
+            exit(1);
+        }
+
+        fwrite(pkt->data, 1, pkt->size, output);
+        av_packet_unref(pkt);
+    }
+}
+
 int main(int argc, char **argv)
 {
     const char *filename;
     const AVCodec *codec;
     AVCodecContext *c= NULL;
     AVFrame *frame;
-    AVPacket pkt;
-    int i, j, k, ret, got_output;
+    AVPacket *pkt;
+    int i, j, k, ret;
     FILE *f;
     uint16_t *samples;
     float t, tincr;
@@ -154,6 +182,13 @@ int main(int argc, char **argv)
         exit(1);
     }
 
+    /* packet for holding encoded output */
+    pkt = av_packet_alloc();
+    if (!pkt) {
+        fprintf(stderr, "could not allocate the packet\n");
+        exit(1);
+    }
+
     /* frame containing input raw audio */
     frame = av_frame_alloc();
     if (!frame) {
@@ -176,10 +211,6 @@ int main(int argc, char **argv)
     t = 0;
     tincr = 2 * M_PI * 440.0 / c->sample_rate;
     for (i = 0; i < 200; i++) {
-        av_init_packet(&pkt);
-        pkt.data = NULL; // packet data will be allocated by the encoder
-        pkt.size = 0;
-
         /* make sure the frame is writable -- makes a copy if the encoder
          * kept a reference internally */
         ret = av_frame_make_writable(frame);
@@ -194,34 +225,16 @@ int main(int argc, char **argv)
                 samples[2*j + k] = samples[2*j];
             t += tincr;
         }
-        /* encode the samples */
-        ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);
-        if (ret < 0) {
-            fprintf(stderr, "Error encoding audio frame\n");
-            exit(1);
-        }
-        if (got_output) {
-            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++) {
-        ret = avcodec_encode_audio2(c, &pkt, NULL, &got_output);
-        if (ret < 0) {
-            fprintf(stderr, "Error encoding frame\n");
-            exit(1);
-        }
+    /* flush the encoder */
+    encode(c, NULL, pkt, f);
 
-        if (got_output) {
-            fwrite(pkt.data, 1, pkt.size, f);
-            av_packet_unref(&pkt);
-        }
-    }
     fclose(f);
 
     av_frame_free(&frame);
+    av_packet_free(&pkt);
     avcodec_free_context(&c);
 
     return 0;


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

diff --cc doc/examples/encode_audio.c
index ba9ef6d,a32fcc9..d1ef105
--- a/doc/examples/encode_audio.c
+++ b/doc/examples/encode_audio.c
@@@ -92,6 -89,34 +92,34 @@@ static int select_channel_layout(const 
      return best_ch_layout;
  }
  
+ static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt,
+                    FILE *output)
+ {
+     int ret;
+ 
+     /* send the frame for encoding */
+     ret = avcodec_send_frame(ctx, frame);
+     if (ret < 0) {
 -        fprintf(stderr, "error sending the frame to the encoder\n");
++        fprintf(stderr, "Error sending the frame to the encoder\n");
+         exit(1);
+     }
+ 
+     /* read all the available output packets (in general there may be any
+      * number of them */
+     while (ret >= 0) {
+         ret = avcodec_receive_packet(ctx, pkt);
+         if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
+             return;
+         else if (ret < 0) {
 -            fprintf(stderr, "error encoding audio frame\n");
++            fprintf(stderr, "Error encoding audio frame\n");
+             exit(1);
+         }
+ 
+         fwrite(pkt->data, 1, pkt->size, output);
+         av_packet_unref(pkt);
+     }
+ }
+ 
  int main(int argc, char **argv)
  {
      const char *filename;
@@@ -175,11 -203,7 +210,7 @@@
      /* encode a single tone sound */
      t = 0;
      tincr = 2 * M_PI * 440.0 / c->sample_rate;
 -    for(i=0;i<200;i++) {
 +    for (i = 0; i < 200; i++) {
-         av_init_packet(&pkt);
-         pkt.data = NULL; // packet data will be allocated by the encoder
-         pkt.size = 0;
- 
          /* make sure the frame is writable -- makes a copy if the encoder
           * kept a reference internally */
          ret = av_frame_make_writable(frame);
@@@ -217,12 -227,6 +234,8 @@@
      fclose(f);
  
      av_frame_free(&frame);
+     av_packet_free(&pkt);
      avcodec_free_context(&c);
 +
 +    return 0;
  }



More information about the ffmpeg-cvslog mailing list