Libav
transcode_aac.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
28 #include <stdio.h>
29 
30 #include "libavformat/avformat.h"
31 #include "libavformat/avio.h"
32 
33 #include "libavcodec/avcodec.h"
34 
35 #include "libavutil/audio_fifo.h"
36 #include "libavutil/avstring.h"
37 #include "libavutil/frame.h"
38 #include "libavutil/opt.h"
39 
41 
43 #define OUTPUT_BIT_RATE 48000
44 
45 #define OUTPUT_CHANNELS 2
46 
47 #define OUTPUT_SAMPLE_FORMAT AV_SAMPLE_FMT_S16
48 
54 static char *const get_error_text(const int error)
55 {
56  static char error_buffer[255];
57  av_strerror(error, error_buffer, sizeof(error_buffer));
58  return error_buffer;
59 }
60 
62 static int open_input_file(const char *filename,
63  AVFormatContext **input_format_context,
64  AVCodecContext **input_codec_context)
65 {
66  AVCodec *input_codec;
67  int error;
68 
70  if ((error = avformat_open_input(input_format_context, filename, NULL,
71  NULL)) < 0) {
72  fprintf(stderr, "Could not open input file '%s' (error '%s')\n",
73  filename, get_error_text(error));
74  *input_format_context = NULL;
75  return error;
76  }
77 
79  if ((error = avformat_find_stream_info(*input_format_context, NULL)) < 0) {
80  fprintf(stderr, "Could not open find stream info (error '%s')\n",
81  get_error_text(error));
82  avformat_close_input(input_format_context);
83  return error;
84  }
85 
87  if ((*input_format_context)->nb_streams != 1) {
88  fprintf(stderr, "Expected one audio input stream, but found %d\n",
89  (*input_format_context)->nb_streams);
90  avformat_close_input(input_format_context);
91  return AVERROR_EXIT;
92  }
93 
95  if (!(input_codec = avcodec_find_decoder((*input_format_context)->streams[0]->codec->codec_id))) {
96  fprintf(stderr, "Could not find input codec\n");
97  avformat_close_input(input_format_context);
98  return AVERROR_EXIT;
99  }
100 
102  if ((error = avcodec_open2((*input_format_context)->streams[0]->codec,
103  input_codec, NULL)) < 0) {
104  fprintf(stderr, "Could not open input codec (error '%s')\n",
105  get_error_text(error));
106  avformat_close_input(input_format_context);
107  return error;
108  }
109 
111  *input_codec_context = (*input_format_context)->streams[0]->codec;
112 
113  return 0;
114 }
115 
121 static int open_output_file(const char *filename,
122  AVCodecContext *input_codec_context,
123  AVFormatContext **output_format_context,
124  AVCodecContext **output_codec_context)
125 {
126  AVIOContext *output_io_context = NULL;
127  AVStream *stream = NULL;
128  AVCodec *output_codec = NULL;
129  int error;
130 
132  if ((error = avio_open(&output_io_context, filename,
133  AVIO_FLAG_WRITE)) < 0) {
134  fprintf(stderr, "Could not open output file '%s' (error '%s')\n",
135  filename, get_error_text(error));
136  return error;
137  }
138 
140  if (!(*output_format_context = avformat_alloc_context())) {
141  fprintf(stderr, "Could not allocate output format context\n");
142  return AVERROR(ENOMEM);
143  }
144 
146  (*output_format_context)->pb = output_io_context;
147 
149  if (!((*output_format_context)->oformat = av_guess_format(NULL, filename,
150  NULL))) {
151  fprintf(stderr, "Could not find output file format\n");
152  goto cleanup;
153  }
154 
155  av_strlcpy((*output_format_context)->filename, filename,
156  sizeof((*output_format_context)->filename));
157 
159  if (!(output_codec = avcodec_find_encoder(AV_CODEC_ID_AAC))) {
160  fprintf(stderr, "Could not find an AAC encoder.\n");
161  goto cleanup;
162  }
163 
165  if (!(stream = avformat_new_stream(*output_format_context, output_codec))) {
166  fprintf(stderr, "Could not create new stream\n");
167  error = AVERROR(ENOMEM);
168  goto cleanup;
169  }
170 
172  *output_codec_context = stream->codec;
173 
178  (*output_codec_context)->channels = OUTPUT_CHANNELS;
179  (*output_codec_context)->channel_layout = av_get_default_channel_layout(OUTPUT_CHANNELS);
180  (*output_codec_context)->sample_rate = input_codec_context->sample_rate;
181  (*output_codec_context)->sample_fmt = AV_SAMPLE_FMT_S16;
182  (*output_codec_context)->bit_rate = OUTPUT_BIT_RATE;
183 
188  if ((*output_format_context)->oformat->flags & AVFMT_GLOBALHEADER)
189  (*output_codec_context)->flags |= CODEC_FLAG_GLOBAL_HEADER;
190 
192  if ((error = avcodec_open2(*output_codec_context, output_codec, NULL)) < 0) {
193  fprintf(stderr, "Could not open output codec (error '%s')\n",
194  get_error_text(error));
195  goto cleanup;
196  }
197 
198  return 0;
199 
200 cleanup:
201  avio_close((*output_format_context)->pb);
202  avformat_free_context(*output_format_context);
203  *output_format_context = NULL;
204  return error < 0 ? error : AVERROR_EXIT;
205 }
206 
208 static void init_packet(AVPacket *packet)
209 {
210  av_init_packet(packet);
212  packet->data = NULL;
213  packet->size = 0;
214 }
215 
217 static int init_input_frame(AVFrame **frame)
218 {
219  if (!(*frame = av_frame_alloc())) {
220  fprintf(stderr, "Could not allocate input frame\n");
221  return AVERROR(ENOMEM);
222  }
223  return 0;
224 }
225 
231 static int init_resampler(AVCodecContext *input_codec_context,
232  AVCodecContext *output_codec_context,
233  AVAudioResampleContext **resample_context)
234 {
239  if (input_codec_context->sample_fmt != output_codec_context->sample_fmt ||
240  input_codec_context->channels != output_codec_context->channels) {
241  int error;
242 
244  if (!(*resample_context = avresample_alloc_context())) {
245  fprintf(stderr, "Could not allocate resample context\n");
246  return AVERROR(ENOMEM);
247  }
248 
255  av_opt_set_int(*resample_context, "in_channel_layout",
256  av_get_default_channel_layout(input_codec_context->channels), 0);
257  av_opt_set_int(*resample_context, "out_channel_layout",
258  av_get_default_channel_layout(output_codec_context->channels), 0);
259  av_opt_set_int(*resample_context, "in_sample_rate",
260  input_codec_context->sample_rate, 0);
261  av_opt_set_int(*resample_context, "out_sample_rate",
262  output_codec_context->sample_rate, 0);
263  av_opt_set_int(*resample_context, "in_sample_fmt",
264  input_codec_context->sample_fmt, 0);
265  av_opt_set_int(*resample_context, "out_sample_fmt",
266  output_codec_context->sample_fmt, 0);
267 
269  if ((error = avresample_open(*resample_context)) < 0) {
270  fprintf(stderr, "Could not open resample context\n");
271  avresample_free(resample_context);
272  return error;
273  }
274  }
275  return 0;
276 }
277 
279 static int init_fifo(AVAudioFifo **fifo)
280 {
283  fprintf(stderr, "Could not allocate FIFO\n");
284  return AVERROR(ENOMEM);
285  }
286  return 0;
287 }
288 
290 static int write_output_file_header(AVFormatContext *output_format_context)
291 {
292  int error;
293  if ((error = avformat_write_header(output_format_context, NULL)) < 0) {
294  fprintf(stderr, "Could not write output file header (error '%s')\n",
295  get_error_text(error));
296  return error;
297  }
298  return 0;
299 }
300 
302 static int decode_audio_frame(AVFrame *frame,
303  AVFormatContext *input_format_context,
304  AVCodecContext *input_codec_context,
305  int *data_present, int *finished)
306 {
308  AVPacket input_packet;
309  int error;
310  init_packet(&input_packet);
311 
313  if ((error = av_read_frame(input_format_context, &input_packet)) < 0) {
315  if (error == AVERROR_EOF)
316  *finished = 1;
317  else {
318  fprintf(stderr, "Could not read frame (error '%s')\n",
319  get_error_text(error));
320  return error;
321  }
322  }
323 
330  if ((error = avcodec_decode_audio4(input_codec_context, frame,
331  data_present, &input_packet)) < 0) {
332  fprintf(stderr, "Could not decode frame (error '%s')\n",
333  get_error_text(error));
334  av_free_packet(&input_packet);
335  return error;
336  }
337 
342  if (*finished && *data_present)
343  *finished = 0;
344  av_free_packet(&input_packet);
345  return 0;
346 }
347 
353 static int init_converted_samples(uint8_t ***converted_input_samples,
354  AVCodecContext *output_codec_context,
355  int frame_size)
356 {
357  int error;
358 
364  if (!(*converted_input_samples = calloc(output_codec_context->channels,
365  sizeof(**converted_input_samples)))) {
366  fprintf(stderr, "Could not allocate converted input sample pointers\n");
367  return AVERROR(ENOMEM);
368  }
369 
374  if ((error = av_samples_alloc(*converted_input_samples, NULL,
375  output_codec_context->channels,
376  frame_size,
377  output_codec_context->sample_fmt, 0)) < 0) {
378  fprintf(stderr,
379  "Could not allocate converted input samples (error '%s')\n",
380  get_error_text(error));
381  av_freep(&(*converted_input_samples)[0]);
382  free(*converted_input_samples);
383  return error;
384  }
385  return 0;
386 }
387 
393 static int convert_samples(uint8_t **input_data,
394  uint8_t **converted_data, const int frame_size,
395  AVAudioResampleContext *resample_context)
396 {
397  int error;
398 
400  if ((error = avresample_convert(resample_context, converted_data, 0,
401  frame_size, input_data, 0, frame_size)) < 0) {
402  fprintf(stderr, "Could not convert input samples (error '%s')\n",
403  get_error_text(error));
404  return error;
405  }
406 
412  if (avresample_available(resample_context)) {
413  fprintf(stderr, "Converted samples left over\n");
414  return AVERROR_EXIT;
415  }
416 
417  return 0;
418 }
419 
422  uint8_t **converted_input_samples,
423  const int frame_size)
424 {
425  int error;
426 
431  if ((error = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + frame_size)) < 0) {
432  fprintf(stderr, "Could not reallocate FIFO\n");
433  return error;
434  }
435 
437  if (av_audio_fifo_write(fifo, (void **)converted_input_samples,
438  frame_size) < frame_size) {
439  fprintf(stderr, "Could not write data to FIFO\n");
440  return AVERROR_EXIT;
441  }
442  return 0;
443 }
444 
450  AVFormatContext *input_format_context,
451  AVCodecContext *input_codec_context,
452  AVCodecContext *output_codec_context,
453  AVAudioResampleContext *resampler_context,
454  int *finished)
455 {
457  AVFrame *input_frame = NULL;
459  uint8_t **converted_input_samples = NULL;
460  int data_present;
461  int ret = AVERROR_EXIT;
462 
464  if (init_input_frame(&input_frame))
465  goto cleanup;
467  if (decode_audio_frame(input_frame, input_format_context,
468  input_codec_context, &data_present, finished))
469  goto cleanup;
475  if (*finished && !data_present) {
476  ret = 0;
477  goto cleanup;
478  }
480  if (data_present) {
482  if (init_converted_samples(&converted_input_samples, output_codec_context,
483  input_frame->nb_samples))
484  goto cleanup;
485 
490  if (convert_samples(input_frame->extended_data, converted_input_samples,
491  input_frame->nb_samples, resampler_context))
492  goto cleanup;
493 
495  if (add_samples_to_fifo(fifo, converted_input_samples,
496  input_frame->nb_samples))
497  goto cleanup;
498  ret = 0;
499  }
500  ret = 0;
501 
502 cleanup:
503  if (converted_input_samples) {
504  av_freep(&converted_input_samples[0]);
505  free(converted_input_samples);
506  }
507  av_frame_free(&input_frame);
508 
509  return ret;
510 }
511 
516 static int init_output_frame(AVFrame **frame,
517  AVCodecContext *output_codec_context,
518  int frame_size)
519 {
520  int error;
521 
523  if (!(*frame = av_frame_alloc())) {
524  fprintf(stderr, "Could not allocate output frame\n");
525  return AVERROR_EXIT;
526  }
527 
535  (*frame)->nb_samples = frame_size;
536  (*frame)->channel_layout = output_codec_context->channel_layout;
537  (*frame)->format = output_codec_context->sample_fmt;
538  (*frame)->sample_rate = output_codec_context->sample_rate;
539 
544  if ((error = av_frame_get_buffer(*frame, 0)) < 0) {
545  fprintf(stderr, "Could allocate output frame samples (error '%s')\n",
546  get_error_text(error));
547  av_frame_free(frame);
548  return error;
549  }
550 
551  return 0;
552 }
553 
555 static int encode_audio_frame(AVFrame *frame,
556  AVFormatContext *output_format_context,
557  AVCodecContext *output_codec_context,
558  int *data_present)
559 {
562  int error;
563  init_packet(&output_packet);
564 
569  if ((error = avcodec_encode_audio2(output_codec_context, &output_packet,
570  frame, data_present)) < 0) {
571  fprintf(stderr, "Could not encode frame (error '%s')\n",
572  get_error_text(error));
573  av_free_packet(&output_packet);
574  return error;
575  }
576 
578  if (*data_present) {
579  if ((error = av_write_frame(output_format_context, &output_packet)) < 0) {
580  fprintf(stderr, "Could not write frame (error '%s')\n",
581  get_error_text(error));
582  av_free_packet(&output_packet);
583  return error;
584  }
585 
586  av_free_packet(&output_packet);
587  }
588 
589  return 0;
590 }
591 
597  AVFormatContext *output_format_context,
598  AVCodecContext *output_codec_context)
599 {
607  const int frame_size = FFMIN(av_audio_fifo_size(fifo),
608  output_codec_context->frame_size);
609  int data_written;
610 
612  if (init_output_frame(&output_frame, output_codec_context, frame_size))
613  return AVERROR_EXIT;
614 
619  if (av_audio_fifo_read(fifo, (void **)output_frame->data, frame_size) < frame_size) {
620  fprintf(stderr, "Could not read data from FIFO\n");
621  av_frame_free(&output_frame);
622  return AVERROR_EXIT;
623  }
624 
626  if (encode_audio_frame(output_frame, output_format_context,
627  output_codec_context, &data_written)) {
628  av_frame_free(&output_frame);
629  return AVERROR_EXIT;
630  }
631  av_frame_free(&output_frame);
632  return 0;
633 }
634 
636 static int write_output_file_trailer(AVFormatContext *output_format_context)
637 {
638  int error;
639  if ((error = av_write_trailer(output_format_context)) < 0) {
640  fprintf(stderr, "Could not write output file trailer (error '%s')\n",
641  get_error_text(error));
642  return error;
643  }
644  return 0;
645 }
646 
648 int main(int argc, char **argv)
649 {
650  AVFormatContext *input_format_context = NULL, *output_format_context = NULL;
651  AVCodecContext *input_codec_context = NULL, *output_codec_context = NULL;
652  AVAudioResampleContext *resample_context = NULL;
653  AVAudioFifo *fifo = NULL;
654  int ret = AVERROR_EXIT;
655 
656  if (argc < 3) {
657  fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
658  exit(1);
659  }
660 
662  av_register_all();
664  if (open_input_file(argv[1], &input_format_context,
665  &input_codec_context))
666  goto cleanup;
668  if (open_output_file(argv[2], input_codec_context,
669  &output_format_context, &output_codec_context))
670  goto cleanup;
672  if (init_resampler(input_codec_context, output_codec_context,
673  &resample_context))
674  goto cleanup;
676  if (init_fifo(&fifo))
677  goto cleanup;
679  if (write_output_file_header(output_format_context))
680  goto cleanup;
681 
686  while (1) {
688  const int output_frame_size = output_codec_context->frame_size;
689  int finished = 0;
690 
698  while (av_audio_fifo_size(fifo) < output_frame_size) {
703  if (read_decode_convert_and_store(fifo, input_format_context,
704  input_codec_context,
705  output_codec_context,
706  resample_context, &finished))
707  goto cleanup;
708 
713  if (finished)
714  break;
715  }
716 
722  while (av_audio_fifo_size(fifo) >= output_frame_size ||
723  (finished && av_audio_fifo_size(fifo) > 0))
728  if (load_encode_and_write(fifo, output_format_context,
729  output_codec_context))
730  goto cleanup;
731 
736  if (finished) {
737  int data_written;
739  do {
740  if (encode_audio_frame(NULL, output_format_context,
741  output_codec_context, &data_written))
742  goto cleanup;
743  } while (data_written);
744  break;
745  }
746  }
747 
749  if (write_output_file_trailer(output_format_context))
750  goto cleanup;
751  ret = 0;
752 
753 cleanup:
754  if (fifo)
755  av_audio_fifo_free(fifo);
756  if (resample_context) {
757  avresample_close(resample_context);
758  avresample_free(&resample_context);
759  }
760  if (output_codec_context)
761  avcodec_close(output_codec_context);
762  if (output_format_context) {
763  avio_close(output_format_context->pb);
764  avformat_free_context(output_format_context);
765  }
766  if (input_codec_context)
767  avcodec_close(input_codec_context);
768  if (input_format_context)
769  avformat_close_input(&input_format_context);
770 
771  return ret;
772 }
int avio_open(AVIOContext **s, const char *url, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:778
uint64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
const struct AVCodec * codec
Definition: avcodec.h:1059
Bytestream IO Context.
Definition: avio.h:68
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:60
Buffered I/O operations.
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
Definition: audio_fifo.c:139
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:243
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: utils.c:1761
int main(int argc, char **argv)
Convert an audio file to an AAC file in an MP4 container.
int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition: mux.c:238
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:361
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:247
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
int size
Definition: avcodec.h:974
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:293
int avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of audio.
Definition: utils.c:1314
AVCodec.
Definition: avcodec.h:2796
static void init_packet(AVPacket *packet)
Initialize one data packet for reading or writing.
void avresample_free(AVAudioResampleContext **avr)
Free AVAudioResampleContext and associated AVOption values.
Definition: utils.c:278
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
static int open_input_file(const char *filename, AVFormatContext **input_format_context, AVCodecContext **input_codec_context)
Open an input file and the required decoder.
Definition: transcode_aac.c:62
Format I/O context.
Definition: avformat.h:922
static int encode_audio_frame(AVFrame *frame, AVFormatContext *output_format_context, AVCodecContext *output_codec_context, int *data_present)
Encode one frame worth of audio to the output file.
static int output_packet(AVFormatContext *ctx, int flush)
Definition: mpegenc.c:932
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1799
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:57
AVOptions.
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:657
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2518
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:98
#define OUTPUT_SAMPLE_FORMAT
The audio sample output format.
Definition: transcode_aac.c:47
uint8_t * data
Definition: avcodec.h:973
#define AVERROR_EOF
End of file.
Definition: error.h:51
static int init_input_frame(AVFrame **frame)
Initialize one audio frame for reading from the input file.
void avresample_close(AVAudioResampleContext *avr)
Close AVAudioResampleContext.
Definition: utils.c:262
static const uint8_t frame_size[4]
Definition: g723_1_data.h:47
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
Definition: utils.c:1710
static int output_frame(H264Context *h, AVFrame *dst, AVFrame *src)
Definition: h264.c:1705
static int write_output_file_header(AVFormatContext *output_format_context)
Write the header of the output file container.
#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.
Definition: frame.c:69
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:800
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:266
static int load_encode_and_write(AVAudioFifo *fifo, AVFormatContext *output_format_context, AVCodecContext *output_codec_context)
Load one audio frame from the FIFO buffer, encode and write it to the output file.
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:81
reference-counted frame API
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1852
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:186
external API header
#define FFMIN(a, b)
Definition: common.h:57
static int open_output_file(const char *filename, AVCodecContext *input_codec_context, AVFormatContext **output_format_context, AVCodecContext **output_codec_context)
Open an output file and the required encoder.
static int decode_audio_frame(AVFrame *frame, AVFormatContext *input_format_context, AVCodecContext *input_codec_context, int *data_present, int *finished)
Decode one audio frame from the input file.
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:415
static char *const get_error_text(const int error)
Convert an error code into a text message.
Definition: transcode_aac.c:54
AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
Definition: format.c:104
static int add_samples_to_fifo(AVAudioFifo *fifo, uint8_t **converted_input_samples, const int frame_size)
Add converted input audio samples to the FIFO buffer for later processing.
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:52
Stream structure.
Definition: avformat.h:699
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1811
NULL
Definition: eval.c:55
int avresample_available(AVAudioResampleContext *avr)
Return the number of available samples in the output FIFO.
Definition: utils.c:744
int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Allocate a samples buffer for nb_samples samples, and fill data pointers and linesize accordingly...
Definition: samplefmt.c:162
Libavcodec external API header.
#define OUTPUT_BIT_RATE
The output bit rate in kbit/s.
Definition: transcode_aac.c:43
int sample_rate
samples per second
Definition: avcodec.h:1791
static int init_fifo(AVAudioFifo **fifo)
Initialize a FIFO buffer for the audio samples to be encoded.
main external API structure.
Definition: avcodec.h:1050
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: utils.c:1780
static int read_decode_convert_and_store(AVAudioFifo *fifo, AVFormatContext *input_format_context, AVCodecContext *input_codec_context, AVCodecContext *output_codec_context, AVAudioResampleContext *resampler_context, int *finished)
Read one audio frame from the input file, decodes, converts and stores it in the FIFO buffer...
static int convert_samples(uint8_t **input_data, uint8_t **converted_data, const int frame_size, AVAudioResampleContext *resample_context)
Convert the input audio samples into the output sample format.
int avresample_convert(AVAudioResampleContext *avr, uint8_t **output, int out_plane_size, int out_samples, uint8_t **input, int in_plane_size, int in_samples)
Convert input samples and write them to the output FIFO.
Definition: utils.c:330
static int init_resampler(AVCodecContext *input_codec_context, AVCodecContext *output_codec_context, AVAudioResampleContext **resample_context)
Initialize the audio resampler based on the input and output codec settings.
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:982
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:2445
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:989
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:175
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:113
#define OUTPUT_CHANNELS
The number of output channels.
Definition: transcode_aac.c:45
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
Definition: error.c:23
int av_audio_fifo_realloc(AVAudioFifo *af, int nb_samples)
Reallocate an AVAudioFifo.
Definition: audio_fifo.c:97
AVAudioResampleContext * avresample_alloc_context(void)
Allocate AVAudioResampleContext and set options.
Definition: options.c:96
Main libavformat public API header.
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:2052
signed 16 bits
Definition: samplefmt.h:64
static int write_output_file_trailer(AVFormatContext *output_format_context)
Write the trailer of the output file container.
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:47
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:2496
Audio FIFO Buffer.
int channels
number of audio channels
Definition: avcodec.h:1792
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:583
static int init_converted_samples(uint8_t ***converted_input_samples, AVCodecContext *output_codec_context, int frame_size)
Initialize a temporary storage for the specified number of audio samples.
static int init_output_frame(AVFrame **frame, AVCodecContext *output_codec_context, int frame_size)
Initialize one input frame for writing to the output file.
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:169
This structure stores compressed data.
Definition: avcodec.h:950
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:51
int avresample_open(AVAudioResampleContext *avr)
Initialize AVAudioResampleContext.
Definition: utils.c:36
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:179
int avcodec_decode_audio4(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Decode the audio frame of size avpkt->size from avpkt->data into frame.
Definition: utils.c:1630