[FFmpeg-devel] [PATCH 1/3] examples/decoding_encoding: upgrade to encode_audio2.

Nicolas George nicolas.george at normalesup.org
Thu Apr 12 11:32:57 CEST 2012


Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
 doc/examples/decoding_encoding.c |   31 ++++++++++++++++++++++++-------
 1 file changed, 24 insertions(+), 7 deletions(-)

diff --git a/doc/examples/decoding_encoding.c b/doc/examples/decoding_encoding.c
index 4b87e2d..1262611 100644
--- a/doc/examples/decoding_encoding.c
+++ b/doc/examples/decoding_encoding.c
@@ -46,11 +46,12 @@ static void audio_encode_example(const char *filename)
 {
     AVCodec *codec;
     AVCodecContext *c= NULL;
-    int frame_size, i, j, out_size, outbuf_size;
+    int frame_size, i, j, ret, got_packet;
     FILE *f;
     short *samples;
+    AVFrame *frame;
     float t, tincr;
-    uint8_t *outbuf;
+    AVPacket packet;
 
     printf("Audio encoding\n");
 
@@ -77,9 +78,14 @@ static void audio_encode_example(const char *filename)
 
     /* the codec gives us the frame size, in samples */
     frame_size = c->frame_size;
+    frame = avcodec_alloc_frame();
+    if (!frame) {
+        fprintf(stderr, "Could not allocate frame.\n");
+        exit(1);
+    }
     samples = malloc(frame_size * 2 * c->channels);
-    outbuf_size = 10000;
-    outbuf = malloc(outbuf_size);
+    frame->data[0] = (void *)samples;
+    frame->nb_samples = frame_size;
 
     f = fopen(filename, "wb");
     if (!f) {
@@ -97,11 +103,22 @@ static void audio_encode_example(const char *filename)
             t += tincr;
         }
         /* encode the samples */
-        out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
-        fwrite(outbuf, 1, out_size, f);
+        av_init_packet(&packet);
+        packet.data = NULL;
+        packet.size = 0;
+        got_packet = 0;
+        ret = avcodec_encode_audio2(c, &packet, frame, &got_packet);
+        if (ret < 0) {
+            fprintf(stderr, "Audio encoding failed.\n");
+            exit(1);
+        }
+        if (got_packet) {
+            fwrite(packet.data, 1, packet.size, f);
+            av_destruct_packet(&packet);
+        }
     }
     fclose(f);
-    free(outbuf);
+    av_free(frame);
     free(samples);
 
     avcodec_close(c);
-- 
1.7.9.5



More information about the ffmpeg-devel mailing list