decode_audio.c
Go to the documentation of this file.
1 /*
2  * copyright (c) 2001 Fabrice Bellard
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
33 #include "libavutil/frame.h"
34 #include "libavutil/mem.h"
35 
36 #include "libavcodec/avcodec.h"
37 
38 #define AUDIO_INBUF_SIZE 20480
39 #define AUDIO_REFILL_THRESH 4096
40 
41 static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame,
42  FILE *outfile)
43 {
44  int16_t *interleave_buf;
45  int ret, data_size, i;
46 
47  /* send the packet with the compressed data to the decoder */
48  ret = avcodec_send_packet(dec_ctx, pkt);
49  if (ret < 0) {
50  fprintf(stderr, "Error submitting the packet to the decoder\n");
51  exit(1);
52  }
53 
54  /* read all the output frames (in general there may be any number of them */
55  while (ret >= 0) {
56  ret = avcodec_receive_frame(dec_ctx, frame);
57  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
58  return;
59  else if (ret < 0) {
60  fprintf(stderr, "Error during decoding\n");
61  exit(1);
62  }
63 
64  /* the stream parameters may change at any time, check that they are
65  * what we expect */
67  frame->format != AV_SAMPLE_FMT_S16P) {
68  fprintf(stderr, "Unsupported frame parameters\n");
69  exit(1);
70  }
71 
72  /* The decoded data is signed 16-bit planar -- each channel in its own
73  * buffer. We interleave the two channels manually here, but using
74  * libavresample is recommended instead. */
75  data_size = sizeof(*interleave_buf) * 2 * frame->nb_samples;
76  interleave_buf = av_malloc(data_size);
77  if (!interleave_buf)
78  exit(1);
79 
80  for (i = 0; i < frame->nb_samples; i++) {
81  interleave_buf[2 * i] = ((int16_t*)frame->data[0])[i];
82  interleave_buf[2 * i + 1] = ((int16_t*)frame->data[1])[i];
83  }
84  fwrite(interleave_buf, 1, data_size, outfile);
85  av_freep(&interleave_buf);
86  }
87 }
88 
89 int main(int argc, char **argv)
90 {
91  const char *outfilename, *filename;
92  const AVCodec *codec;
93  AVCodecContext *c= NULL;
94  AVCodecParserContext *parser = NULL;
95  int len, ret;
96  FILE *f, *outfile;
98  uint8_t *data;
99  size_t data_size;
100  AVPacket *pkt;
101  AVFrame *decoded_frame = NULL;
102 
103  if (argc <= 2) {
104  fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
105  exit(0);
106  }
107  filename = argv[1];
108  outfilename = argv[2];
109 
110  /* register all the codecs */
112 
113  pkt = av_packet_alloc();
114 
115  /* find the MPEG audio decoder */
117  if (!codec) {
118  fprintf(stderr, "codec not found\n");
119  exit(1);
120  }
121 
122  parser = av_parser_init(codec->id);
123  if (!parser) {
124  fprintf(stderr, "parser not found\n");
125  exit(1);
126  }
127 
128  c = avcodec_alloc_context3(codec);
129 
130  /* open it */
131  if (avcodec_open2(c, codec, NULL) < 0) {
132  fprintf(stderr, "could not open codec\n");
133  exit(1);
134  }
135 
136  f = fopen(filename, "rb");
137  if (!f) {
138  fprintf(stderr, "could not open %s\n", filename);
139  exit(1);
140  }
141  outfile = fopen(outfilename, "wb");
142  if (!outfile) {
143  av_free(c);
144  exit(1);
145  }
146 
147  /* decode until eof */
148  data = inbuf;
149  data_size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
150 
151  while (data_size > 0) {
152  if (!decoded_frame) {
153  if (!(decoded_frame = av_frame_alloc())) {
154  fprintf(stderr, "out of memory\n");
155  exit(1);
156  }
157  }
158 
159  ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
160  data, data_size,
162  if (ret < 0) {
163  fprintf(stderr, "Error while parsing\n");
164  exit(1);
165  }
166  data += ret;
167  data_size -= ret;
168 
169  if (pkt->size)
170  decode(c, pkt, decoded_frame, outfile);
171 
172  if (data_size < AUDIO_REFILL_THRESH) {
173  memmove(inbuf, data, data_size);
174  data = inbuf;
175  len = fread(data + data_size, 1,
176  AUDIO_INBUF_SIZE - data_size, f);
177  if (len > 0)
178  data_size += len;
179  }
180  }
181 
182  /* flush the decoder */
183  pkt->data = NULL;
184  pkt->size = 0;
185  decode(c, pkt, decoded_frame, outfile);
186 
187  fclose(outfile);
188  fclose(f);
189 
191  av_parser_close(parser);
192  av_frame_free(&decoded_frame);
193  av_packet_free(&pkt);
194 
195  return 0;
196 }
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
This structure describes decoded (raw) audio or video data.
Definition: frame.h:147
memory handling functions
int size
Definition: avcodec.h:1154
void avcodec_register_all(void)
Register all the codecs, parsers and bitstream filters which were enabled at configuration time...
AVCodec.
Definition: avcodec.h:2842
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:41
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AUDIO_REFILL_THRESH
Definition: decode_audio.c:39
uint8_t * data
Definition: avcodec.h:1153
#define AVERROR_EOF
End of file.
Definition: error.h:51
enum AVCodecID id
Definition: avcodec.h:2856
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder.
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
reference-counted frame API
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:309
audio channel layout utility functions
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int64_t pts, int64_t dts, int64_t pos)
Parse a packet.
void av_parser_close(AVCodecParserContext *s)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:207
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
AVCodecParserContext * av_parser_init(int codec_id)
Libavcodec external API header.
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer...
main external API structure.
Definition: avcodec.h:1216
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
#define AUDIO_INBUF_SIZE
Definition: decode_audio.c:38
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:153
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:654
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
int main(int argc, char **argv)
Definition: decode_audio.c:89
signed 16 bits, planar
Definition: samplefmt.h:69
This structure stores compressed data.
Definition: avcodec.h:1130
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:200
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:241