---
 libavcodec/api-example.c |   67 ++++++++++++++++++++++++++++++++++++----------
 1 file changed, 53 insertions(+), 14 deletions(-)

diff --git a/libavcodec/api-example.c b/libavcodec/api-example.c
index 93d6c22..9f633d9 100644
--- a/libavcodec/api-example.c
+++ b/libavcodec/api-example.c
@@ -37,6 +37,7 @@
 #endif
 
 #include "libavcodec/avcodec.h"
+#include "libavutil/audioconvert.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/samplefmt.h"
 
@@ -51,11 +52,13 @@ static void audio_encode_example(const char *filename)
 {
     AVCodec *codec;
     AVCodecContext *c= NULL;
-    int frame_size, i, j, out_size, outbuf_size;
+    AVFrame *frame;
+    AVPacket pkt;
+    int i, j, ret, got_output;
+    int buffer_size;
     FILE *f;
-    short *samples;
+    uint16_t *samples;
     float t, tincr;
-    uint8_t *outbuf;
 
     printf("Audio encoding\n");
 
@@ -72,6 +75,8 @@ static void audio_encode_example(const char *filename)
     c->bit_rate = 64000;
     c->sample_rate = 44100;
     c->channels = 2;
+    c->channel_layout = AV_CH_LAYOUT_STEREO;
+    c->sample_fmt = AV_SAMPLE_FMT_S16;
 
     /* open it */
     if (avcodec_open2(c, codec, NULL) < 0) {
@@ -79,35 +84,69 @@ static void audio_encode_example(const char *filename)
         exit(1);
     }
 
-    /* the codec gives us the frame size, in samples */
-    frame_size = c->frame_size;
-    samples = malloc(frame_size * 2 * c->channels);
-    outbuf_size = 10000;
-    outbuf = malloc(outbuf_size);
-
     f = fopen(filename, "wb");
     if (!f) {
         fprintf(stderr, "could not open %s\n", filename);
         exit(1);
     }
 
+    /* frame containing input raw audio */
+    frame = avcodec_alloc_frame();
+    if (!frame) {
+        fprintf(stderr, "could not allocate audio frame\n");
+        exit(1);
+    }
+
+    frame->nb_samples     = c->frame_size;
+    frame->format         = c->sample_fmt;
+    frame->channel_layout = c->channel_layout;
+
+    /* the codec gives us the frame size, in samples,
+     * we calculate the size of the samples buffer in bytes */
+    buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size,
+                                             c->sample_fmt, 0);
+    samples = av_malloc(buffer_size);
+    if (!samples) {
+        fprintf(stderr, "could not allocate %d bytes for samples buffer\n",
+                buffer_size);
+        exit(1);
+    }
+    /* setup the data pointers in the AVFrame */
+    ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
+                                   (const uint8_t*)samples, buffer_size, 0);
+    if (ret < 0) {
+        fprintf(stderr, "could not setup audio frame\n");
+        exit(1);
+    }
+
     /* encode a single tone sound */
     t = 0;
     tincr = 2 * M_PI * 440.0 / c->sample_rate;
     for(i=0;i<200;i++) {
-        for(j=0;j<frame_size;j++) {
+        av_init_packet(&pkt);
+        pkt.data = NULL; // packet data will be allocated by the encoder
+        pkt.size = 0;
+
+        for (j = 0; j < c->frame_size; j++) {
             samples[2*j] = (int)(sin(t) * 10000);
             samples[2*j+1] = samples[2*j];
             t += tincr;
         }
         /* encode the samples */
-        out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
-        fwrite(outbuf, 1, out_size, f);
+        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_free_packet(&pkt);
+        }
     }
     fclose(f);
-    free(outbuf);
-    free(samples);
 
+    av_freep(&samples);
+    av_freep(&frame);
     avcodec_close(c);
     av_free(c);
 }
-- 
1.7.10.4

_______________________________________________
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to