summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorClément Bœsch <u@pkh.me>2017-03-29 13:38:29 +0200
committerClément Bœsch <u@pkh.me>2017-03-29 13:43:47 +0200
commit878070cc56065a6e0bc4aa2a73c6ac512bfa4d17 (patch)
treecc3f2ba88f0914785b77a1be2679aea2a786ad97 /doc
parent780cc080d85656429ba97426f35c7bc1f7f201bb (diff)
parentf5df897c4b61985e3afc89ba1290649712ff438e (diff)
Merge commit 'f5df897c4b61985e3afc89ba1290649712ff438e'
* commit 'f5df897c4b61985e3afc89ba1290649712ff438e': examples/avcodec: split audio decoding into a separate example Merged-by: Clément Bœsch <u@pkh.me>
Diffstat (limited to 'doc')
-rw-r--r--doc/Makefile1
-rw-r--r--doc/examples/.gitignore1
-rw-r--r--doc/examples/Makefile1
-rw-r--r--doc/examples/decode_audio.c151
-rw-r--r--doc/examples/decoding_encoding.c108
5 files changed, 154 insertions, 108 deletions
diff --git a/doc/Makefile b/doc/Makefile
index 396d9e08cf..d76139a60b 100644
--- a/doc/Makefile
+++ b/doc/Makefile
@@ -38,6 +38,7 @@ DOCS = $(DOCS-yes)
DOC_EXAMPLES-$(CONFIG_AVIO_DIR_CMD_EXAMPLE) += avio_dir_cmd
DOC_EXAMPLES-$(CONFIG_AVIO_READING_EXAMPLE) += avio_reading
+DOC_EXAMPLES-$(CONFIG_DECODE_AUDIO_EXAMPLE) += decode_audio
DOC_EXAMPLES-$(CONFIG_DECODING_ENCODING_EXAMPLE) += decoding_encoding
DOC_EXAMPLES-$(CONFIG_DEMUXING_DECODING_EXAMPLE) += demuxing_decoding
DOC_EXAMPLES-$(CONFIG_ENCODE_AUDIO_EXAMPLE) += encode_audio
diff --git a/doc/examples/.gitignore b/doc/examples/.gitignore
index 1a6209ba36..ee10e07768 100644
--- a/doc/examples/.gitignore
+++ b/doc/examples/.gitignore
@@ -1,5 +1,6 @@
/avio_dir_cmd
/avio_reading
+/decode_audio
/decoding_encoding
/demuxing_decoding
/encode_audio
diff --git a/doc/examples/Makefile b/doc/examples/Makefile
index f76e5a293c..3f33c22c16 100644
--- a/doc/examples/Makefile
+++ b/doc/examples/Makefile
@@ -13,6 +13,7 @@ LDLIBS := $(shell pkg-config --libs $(FFMPEG_LIBS)) $(LDLIBS)
EXAMPLES= avio_dir_cmd \
avio_reading \
+ decode_audio \
decoding_encoding \
demuxing_decoding \
encode_audio \
diff --git a/doc/examples/decode_audio.c b/doc/examples/decode_audio.c
new file mode 100644
index 0000000000..738a188aef
--- /dev/null
+++ b/doc/examples/decode_audio.c
@@ -0,0 +1,151 @@
+/*
+ * Copyright (c) 2001 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+/**
+ * @file
+ * audio decoding with libavcodec API example
+ *
+ * @example decode_audio.c
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "libavcodec/avcodec.h"
+
+#include "libavutil/frame.h"
+
+#define AUDIO_INBUF_SIZE 20480
+#define AUDIO_REFILL_THRESH 4096
+
+int main(int argc, char **argv)
+{
+ const char *outfilename, *filename;
+ AVCodec *codec;
+ AVCodecContext *c= NULL;
+ int len;
+ FILE *f, *outfile;
+ uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
+ AVPacket avpkt;
+ AVFrame *decoded_frame = NULL;
+
+ if (argc <= 2) {
+ fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
+ exit(0);
+ }
+ filename = argv[1];
+ outfilename = argv[2];
+
+ /* register all the codecs */
+ avcodec_register_all();
+
+ av_init_packet(&avpkt);
+
+ /* find the MPEG audio decoder */
+ codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
+ if (!codec) {
+ fprintf(stderr, "Codec not found\n");
+ exit(1);
+ }
+
+ c = avcodec_alloc_context3(codec);
+ if (!c) {
+ fprintf(stderr, "Could not allocate audio codec context\n");
+ exit(1);
+ }
+
+ /* open it */
+ if (avcodec_open2(c, codec, NULL) < 0) {
+ fprintf(stderr, "Could not open codec\n");
+ exit(1);
+ }
+
+ f = fopen(filename, "rb");
+ if (!f) {
+ fprintf(stderr, "Could not open %s\n", filename);
+ exit(1);
+ }
+ outfile = fopen(outfilename, "wb");
+ if (!outfile) {
+ av_free(c);
+ exit(1);
+ }
+
+ /* decode until eof */
+ avpkt.data = inbuf;
+ avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
+
+ while (avpkt.size > 0) {
+ int i, ch;
+ int got_frame = 0;
+
+ if (!decoded_frame) {
+ if (!(decoded_frame = av_frame_alloc())) {
+ fprintf(stderr, "Could not allocate audio frame\n");
+ exit(1);
+ }
+ }
+
+ 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_get_bytes_per_sample(c->sample_fmt);
+ if (data_size < 0) {
+ /* This should not occur, checking just for paranoia */
+ fprintf(stderr, "Failed to calculate data size\n");
+ exit(1);
+ }
+ for (i=0; i<decoded_frame->nb_samples; i++)
+ for (ch=0; ch<c->channels; ch++)
+ fwrite(decoded_frame->data[ch] + data_size*i, 1, data_size, outfile);
+ }
+ avpkt.size -= len;
+ avpkt.data += len;
+ avpkt.dts =
+ avpkt.pts = AV_NOPTS_VALUE;
+ 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);
+
+ avcodec_free_context(&c);
+ av_frame_free(&decoded_frame);
+
+ return 0;
+}
diff --git a/doc/examples/decoding_encoding.c b/doc/examples/decoding_encoding.c
index f863bdf4a2..7ee4f7743f 100644
--- a/doc/examples/decoding_encoding.c
+++ b/doc/examples/decoding_encoding.c
@@ -41,112 +41,6 @@
#include <libavutil/samplefmt.h>
#define INBUF_SIZE 4096
-#define AUDIO_INBUF_SIZE 20480
-#define AUDIO_REFILL_THRESH 4096
-
-/*
- * Audio decoding.
- */
-static void audio_decode_example(const char *outfilename, const char *filename)
-{
- AVCodec *codec;
- AVCodecContext *c= NULL;
- int len;
- FILE *f, *outfile;
- uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
- AVPacket avpkt;
- AVFrame *decoded_frame = NULL;
-
- av_init_packet(&avpkt);
-
- printf("Decode audio file %s to %s\n", filename, outfilename);
-
- /* find the MPEG audio decoder */
- codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
- if (!codec) {
- fprintf(stderr, "Codec not found\n");
- exit(1);
- }
-
- c = avcodec_alloc_context3(codec);
- if (!c) {
- fprintf(stderr, "Could not allocate audio codec context\n");
- exit(1);
- }
-
- /* open it */
- if (avcodec_open2(c, codec, NULL) < 0) {
- fprintf(stderr, "Could not open codec\n");
- exit(1);
- }
-
- f = fopen(filename, "rb");
- if (!f) {
- fprintf(stderr, "Could not open %s\n", filename);
- exit(1);
- }
- outfile = fopen(outfilename, "wb");
- if (!outfile) {
- av_free(c);
- exit(1);
- }
-
- /* decode until eof */
- avpkt.data = inbuf;
- avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
-
- while (avpkt.size > 0) {
- int i, ch;
- int got_frame = 0;
-
- if (!decoded_frame) {
- if (!(decoded_frame = av_frame_alloc())) {
- fprintf(stderr, "Could not allocate audio frame\n");
- exit(1);
- }
- }
-
- 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_get_bytes_per_sample(c->sample_fmt);
- if (data_size < 0) {
- /* This should not occur, checking just for paranoia */
- fprintf(stderr, "Failed to calculate data size\n");
- exit(1);
- }
- for (i=0; i<decoded_frame->nb_samples; i++)
- for (ch=0; ch<c->channels; ch++)
- fwrite(decoded_frame->data[ch] + data_size*i, 1, data_size, outfile);
- }
- avpkt.size -= len;
- avpkt.data += len;
- avpkt.dts =
- avpkt.pts = AV_NOPTS_VALUE;
- 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);
-
- avcodec_free_context(&c);
- av_frame_free(&decoded_frame);
-}
/*
* Video encoding example
@@ -454,8 +348,6 @@ int main(int argc, char **argv)
if (!strcmp(output_type, "h264")) {
video_encode_example("test.h264", AV_CODEC_ID_H264);
- } else if (!strcmp(output_type, "mp2")) {
- audio_decode_example("test.pcm", "test.mp2");
} else if (!strcmp(output_type, "mpg")) {
video_encode_example("test.mpg", AV_CODEC_ID_MPEG1VIDEO);
video_decode_example("test%02d.pgm", "test.mpg");