]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/api-example.c
aacenc: Write correct length for long identification strings.
[ffmpeg] / libavcodec / api-example.c
index 2a47fea7f88366917234906319179e7adf93a79a..970a90eaba2ccfbb748c26d63b42e6ee676c7b02 100644 (file)
@@ -1,27 +1,28 @@
 /*
  * copyright (c) 2001 Fabrice Bellard
  *
- * This file is part of FFmpeg.
+ * This file is part of Libav.
  *
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * FFmpeg is distributed in the hope that it will be useful,
+ * Libav is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
- * @file libavcodec/apiexample.c
- * avcodec API use example.
+ * @file
+ * libavcodec API use example.
  *
+ * @example libavcodec/api-example.c
  * Note that this library only handles codecs (mpeg, mpeg4, etc...),
  * not file formats (avi, vob, etc...). See library 'libavformat' for the
  * format handling
 #undef HAVE_AV_CONFIG_H
 #endif
 
-#include "avcodec.h"
+#include "libavcodec/avcodec.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/samplefmt.h"
 
 #define INBUF_SIZE 4096
+#define AUDIO_INBUF_SIZE 20480
+#define AUDIO_REFILL_THRESH 4096
 
 /*
  * Audio encoding example
@@ -62,7 +66,7 @@ static void audio_encode_example(const char *filename)
         exit(1);
     }
 
-    c= avcodec_alloc_context();
+    c = avcodec_alloc_context3(codec);
 
     /* put sample parameters */
     c->bit_rate = 64000;
@@ -115,10 +119,13 @@ static void audio_decode_example(const char *outfilename, const char *filename)
 {
     AVCodec *codec;
     AVCodecContext *c= NULL;
-    int out_size, size, len;
+    int len;
     FILE *f, *outfile;
-    uint8_t *outbuf;
-    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
+    uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
+    AVPacket avpkt;
+    AVFrame *decoded_frame = NULL;
+
+    av_init_packet(&avpkt);
 
     printf("Audio decoding\n");
 
@@ -129,7 +136,7 @@ static void audio_decode_example(const char *outfilename, const char *filename)
         exit(1);
     }
 
-    c= avcodec_alloc_context();
+    c = avcodec_alloc_context3(codec);
 
     /* open it */
     if (avcodec_open(c, codec) < 0) {
@@ -137,8 +144,6 @@ static void audio_decode_example(const char *outfilename, const char *filename)
         exit(1);
     }
 
-    outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
-
     f = fopen(filename, "rb");
     if (!f) {
         fprintf(stderr, "could not open %s\n", filename);
@@ -151,36 +156,54 @@ static void audio_decode_example(const char *outfilename, const char *filename)
     }
 
     /* decode until eof */
-    inbuf_ptr = inbuf;
-    for(;;) {
-        size = fread(inbuf, 1, INBUF_SIZE, f);
-        if (size == 0)
-            break;
+    avpkt.data = inbuf;
+    avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
 
-        inbuf_ptr = inbuf;
-        while (size > 0) {
-            out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
-            len = avcodec_decode_audio2(c, (short *)outbuf, &out_size,
-                                       inbuf_ptr, size);
-            if (len < 0) {
-                fprintf(stderr, "Error while decoding\n");
+    while (avpkt.size > 0) {
+        int got_frame = 0;
+
+        if (!decoded_frame) {
+            if (!(decoded_frame = avcodec_alloc_frame())) {
+                fprintf(stderr, "out of memory\n");
                 exit(1);
             }
-            if (out_size > 0) {
-                /* if a frame has been decoded, output it */
-                fwrite(outbuf, 1, out_size, outfile);
-            }
-            size -= len;
-            inbuf_ptr += len;
+        } else
+            avcodec_get_frame_defaults(decoded_frame);
+
+        len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
+        if (len < 0) {
+            fprintf(stderr, "Error while decoding\n");
+            exit(1);
+        }
+        if (got_frame) {
+            /* if a frame has been decoded, output it */
+            int data_size = av_samples_get_buffer_size(NULL, c->channels,
+                                                       decoded_frame->nb_samples,
+                                                       c->sample_fmt, 1);
+            fwrite(decoded_frame->data[0], 1, data_size, outfile);
+        }
+        avpkt.size -= len;
+        avpkt.data += len;
+        if (avpkt.size < AUDIO_REFILL_THRESH) {
+            /* Refill the input buffer, to avoid trying to decode
+             * incomplete frames. Instead of this, one could also use
+             * a parser, or use a proper container format through
+             * libavformat. */
+            memmove(inbuf, avpkt.data, avpkt.size);
+            avpkt.data = inbuf;
+            len = fread(avpkt.data + avpkt.size, 1,
+                        AUDIO_INBUF_SIZE - avpkt.size, f);
+            if (len > 0)
+                avpkt.size += len;
         }
     }
 
     fclose(outfile);
     fclose(f);
-    free(outbuf);
 
     avcodec_close(c);
     av_free(c);
+    av_free(decoded_frame);
 }
 
 /*
@@ -204,7 +227,7 @@ static void video_encode_example(const char *filename)
         exit(1);
     }
 
-    c= avcodec_alloc_context();
+    c = avcodec_alloc_context3(codec);
     picture= avcodec_alloc_frame();
 
     /* put sample parameters */
@@ -314,11 +337,14 @@ static void video_decode_example(const char *outfilename, const char *filename)
 {
     AVCodec *codec;
     AVCodecContext *c= NULL;
-    int frame, size, got_picture, len;
+    int frame, got_picture, len;
     FILE *f;
     AVFrame *picture;
-    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
+    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
     char buf[1024];
+    AVPacket avpkt;
+
+    av_init_packet(&avpkt);
 
     /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
     memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
@@ -332,7 +358,7 @@ static void video_decode_example(const char *outfilename, const char *filename)
         exit(1);
     }
 
-    c= avcodec_alloc_context();
+    c = avcodec_alloc_context3(codec);
     picture= avcodec_alloc_frame();
 
     if(codec->capabilities&CODEC_CAP_TRUNCATED)
@@ -358,8 +384,8 @@ static void video_decode_example(const char *outfilename, const char *filename)
 
     frame = 0;
     for(;;) {
-        size = fread(inbuf, 1, INBUF_SIZE, f);
-        if (size == 0)
+        avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
+        if (avpkt.size == 0)
             break;
 
         /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
@@ -377,10 +403,9 @@ static void video_decode_example(const char *outfilename, const char *filename)
 
         /* here, we use a stream based decoder (mpeg1video), so we
            feed decoder and see if it could decode a frame */
-        inbuf_ptr = inbuf;
-        while (size > 0) {
-            len = avcodec_decode_video(c, picture, &got_picture,
-                                       inbuf_ptr, size);
+        avpkt.data = inbuf;
+        while (avpkt.size > 0) {
+            len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);
             if (len < 0) {
                 fprintf(stderr, "Error while decoding frame %d\n", frame);
                 exit(1);
@@ -396,16 +421,17 @@ static void video_decode_example(const char *outfilename, const char *filename)
                          c->width, c->height, buf);
                 frame++;
             }
-            size -= len;
-            inbuf_ptr += len;
+            avpkt.size -= len;
+            avpkt.data += len;
         }
     }
 
     /* some codecs, such as MPEG, transmit the I and P frame with a
        latency of one frame. You must do the following to have a
        chance to get the last frame of the video */
-    len = avcodec_decode_video(c, picture, &got_picture,
-                               NULL, 0);
+    avpkt.data = NULL;
+    avpkt.size = 0;
+    len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);
     if (got_picture) {
         printf("saving last frame %3d\n", frame);
         fflush(stdout);