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 96000
44 
45 #define OUTPUT_CHANNELS 2
46 
52 static char *const get_error_text(const int error)
53 {
54  static char error_buffer[255];
55  av_strerror(error, error_buffer, sizeof(error_buffer));
56  return error_buffer;
57 }
58 
60 static int open_input_file(const char *filename,
61  AVFormatContext **input_format_context,
62  AVCodecContext **input_codec_context)
63 {
64  AVCodecContext *avctx;
65  AVCodec *input_codec;
66  int error;
67 
69  if ((error = avformat_open_input(input_format_context, filename, NULL,
70  NULL)) < 0) {
71  fprintf(stderr, "Could not open input file '%s' (error '%s')\n",
72  filename, get_error_text(error));
73  *input_format_context = NULL;
74  return error;
75  }
76 
78  if ((error = avformat_find_stream_info(*input_format_context, NULL)) < 0) {
79  fprintf(stderr, "Could not open find stream info (error '%s')\n",
80  get_error_text(error));
81  avformat_close_input(input_format_context);
82  return error;
83  }
84 
86  if ((*input_format_context)->nb_streams != 1) {
87  fprintf(stderr, "Expected one audio input stream, but found %d\n",
88  (*input_format_context)->nb_streams);
89  avformat_close_input(input_format_context);
90  return AVERROR_EXIT;
91  }
92 
94  if (!(input_codec = avcodec_find_decoder((*input_format_context)->streams[0]->codecpar->codec_id))) {
95  fprintf(stderr, "Could not find input codec\n");
96  avformat_close_input(input_format_context);
97  return AVERROR_EXIT;
98  }
99 
101  avctx = avcodec_alloc_context3(input_codec);
102  if (!avctx) {
103  fprintf(stderr, "Could not allocate a decoding context\n");
104  avformat_close_input(input_format_context);
105  return AVERROR(ENOMEM);
106  }
107 
109  error = avcodec_parameters_to_context(avctx, (*input_format_context)->streams[0]->codecpar);
110  if (error < 0) {
111  avformat_close_input(input_format_context);
112  avcodec_free_context(&avctx);
113  return error;
114  }
115 
117  if ((error = avcodec_open2(avctx, input_codec, NULL)) < 0) {
118  fprintf(stderr, "Could not open input codec (error '%s')\n",
119  get_error_text(error));
120  avcodec_free_context(&avctx);
121  avformat_close_input(input_format_context);
122  return error;
123  }
124 
126  *input_codec_context = avctx;
127 
128  return 0;
129 }
130 
136 static int open_output_file(const char *filename,
137  AVCodecContext *input_codec_context,
138  AVFormatContext **output_format_context,
139  AVCodecContext **output_codec_context)
140 {
141  AVCodecContext *avctx = NULL;
142  AVIOContext *output_io_context = NULL;
143  AVStream *stream = NULL;
144  AVCodec *output_codec = NULL;
145  int error;
146 
148  if ((error = avio_open(&output_io_context, filename,
149  AVIO_FLAG_WRITE)) < 0) {
150  fprintf(stderr, "Could not open output file '%s' (error '%s')\n",
151  filename, get_error_text(error));
152  return error;
153  }
154 
156  if (!(*output_format_context = avformat_alloc_context())) {
157  fprintf(stderr, "Could not allocate output format context\n");
158  return AVERROR(ENOMEM);
159  }
160 
162  (*output_format_context)->pb = output_io_context;
163 
165  if (!((*output_format_context)->oformat = av_guess_format(NULL, filename,
166  NULL))) {
167  fprintf(stderr, "Could not find output file format\n");
168  goto cleanup;
169  }
170 
171  av_strlcpy((*output_format_context)->filename, filename,
172  sizeof((*output_format_context)->filename));
173 
175  if (!(output_codec = avcodec_find_encoder(AV_CODEC_ID_AAC))) {
176  fprintf(stderr, "Could not find an AAC encoder.\n");
177  goto cleanup;
178  }
179 
181  if (!(stream = avformat_new_stream(*output_format_context, NULL))) {
182  fprintf(stderr, "Could not create new stream\n");
183  error = AVERROR(ENOMEM);
184  goto cleanup;
185  }
186 
187  avctx = avcodec_alloc_context3(output_codec);
188  if (!avctx) {
189  fprintf(stderr, "Could not allocate an encoding context\n");
190  error = AVERROR(ENOMEM);
191  goto cleanup;
192  }
193 
198  avctx->channels = OUTPUT_CHANNELS;
200  avctx->sample_rate = input_codec_context->sample_rate;
201  avctx->sample_fmt = output_codec->sample_fmts[0];
202  avctx->bit_rate = OUTPUT_BIT_RATE;
203 
206 
208  stream->time_base.den = input_codec_context->sample_rate;
209  stream->time_base.num = 1;
210 
215  if ((*output_format_context)->oformat->flags & AVFMT_GLOBALHEADER)
217 
219  if ((error = avcodec_open2(avctx, output_codec, NULL)) < 0) {
220  fprintf(stderr, "Could not open output codec (error '%s')\n",
221  get_error_text(error));
222  goto cleanup;
223  }
224 
225  error = avcodec_parameters_from_context(stream->codecpar, avctx);
226  if (error < 0) {
227  fprintf(stderr, "Could not initialize stream parameters\n");
228  goto cleanup;
229  }
230 
232  *output_codec_context = avctx;
233 
234  return 0;
235 
236 cleanup:
237  avcodec_free_context(&avctx);
238  avio_close((*output_format_context)->pb);
239  avformat_free_context(*output_format_context);
240  *output_format_context = NULL;
241  return error < 0 ? error : AVERROR_EXIT;
242 }
243 
245 static void init_packet(AVPacket *packet)
246 {
247  av_init_packet(packet);
249  packet->data = NULL;
250  packet->size = 0;
251 }
252 
254 static int init_input_frame(AVFrame **frame)
255 {
256  if (!(*frame = av_frame_alloc())) {
257  fprintf(stderr, "Could not allocate input frame\n");
258  return AVERROR(ENOMEM);
259  }
260  return 0;
261 }
262 
268 static int init_resampler(AVCodecContext *input_codec_context,
269  AVCodecContext *output_codec_context,
270  AVAudioResampleContext **resample_context)
271 {
276  if (input_codec_context->sample_fmt != output_codec_context->sample_fmt ||
277  input_codec_context->channels != output_codec_context->channels) {
278  int error;
279 
281  if (!(*resample_context = avresample_alloc_context())) {
282  fprintf(stderr, "Could not allocate resample context\n");
283  return AVERROR(ENOMEM);
284  }
285 
292  av_opt_set_int(*resample_context, "in_channel_layout",
293  av_get_default_channel_layout(input_codec_context->channels), 0);
294  av_opt_set_int(*resample_context, "out_channel_layout",
295  av_get_default_channel_layout(output_codec_context->channels), 0);
296  av_opt_set_int(*resample_context, "in_sample_rate",
297  input_codec_context->sample_rate, 0);
298  av_opt_set_int(*resample_context, "out_sample_rate",
299  output_codec_context->sample_rate, 0);
300  av_opt_set_int(*resample_context, "in_sample_fmt",
301  input_codec_context->sample_fmt, 0);
302  av_opt_set_int(*resample_context, "out_sample_fmt",
303  output_codec_context->sample_fmt, 0);
304 
306  if ((error = avresample_open(*resample_context)) < 0) {
307  fprintf(stderr, "Could not open resample context\n");
308  avresample_free(resample_context);
309  return error;
310  }
311  }
312  return 0;
313 }
314 
316 static int init_fifo(AVAudioFifo **fifo, AVCodecContext *output_codec_context)
317 {
319  if (!(*fifo = av_audio_fifo_alloc(output_codec_context->sample_fmt,
320  output_codec_context->channels, 1))) {
321  fprintf(stderr, "Could not allocate FIFO\n");
322  return AVERROR(ENOMEM);
323  }
324  return 0;
325 }
326 
328 static int write_output_file_header(AVFormatContext *output_format_context)
329 {
330  int error;
331  if ((error = avformat_write_header(output_format_context, NULL)) < 0) {
332  fprintf(stderr, "Could not write output file header (error '%s')\n",
333  get_error_text(error));
334  return error;
335  }
336  return 0;
337 }
338 
340 static int decode_audio_frame(AVFrame *frame,
341  AVFormatContext *input_format_context,
342  AVCodecContext *input_codec_context,
343  int *data_present, int *finished)
344 {
346  AVPacket input_packet;
347  int error;
348  init_packet(&input_packet);
349 
351  if ((error = av_read_frame(input_format_context, &input_packet)) < 0) {
353  if (error == AVERROR_EOF)
354  *finished = 1;
355  else {
356  fprintf(stderr, "Could not read frame (error '%s')\n",
357  get_error_text(error));
358  return error;
359  }
360  }
361 
368  if ((error = avcodec_decode_audio4(input_codec_context, frame,
369  data_present, &input_packet)) < 0) {
370  fprintf(stderr, "Could not decode frame (error '%s')\n",
371  get_error_text(error));
372  av_packet_unref(&input_packet);
373  return error;
374  }
375 
380  if (*finished && *data_present)
381  *finished = 0;
382  av_packet_unref(&input_packet);
383  return 0;
384 }
385 
391 static int init_converted_samples(uint8_t ***converted_input_samples,
392  AVCodecContext *output_codec_context,
393  int frame_size)
394 {
395  int error;
396 
402  if (!(*converted_input_samples = calloc(output_codec_context->channels,
403  sizeof(**converted_input_samples)))) {
404  fprintf(stderr, "Could not allocate converted input sample pointers\n");
405  return AVERROR(ENOMEM);
406  }
407 
412  if ((error = av_samples_alloc(*converted_input_samples, NULL,
413  output_codec_context->channels,
414  frame_size,
415  output_codec_context->sample_fmt, 0)) < 0) {
416  fprintf(stderr,
417  "Could not allocate converted input samples (error '%s')\n",
418  get_error_text(error));
419  av_freep(&(*converted_input_samples)[0]);
420  free(*converted_input_samples);
421  return error;
422  }
423  return 0;
424 }
425 
431 static int convert_samples(uint8_t **input_data,
432  uint8_t **converted_data, const int frame_size,
433  AVAudioResampleContext *resample_context)
434 {
435  int error;
436 
438  if ((error = avresample_convert(resample_context, converted_data, 0,
439  frame_size, input_data, 0, frame_size)) < 0) {
440  fprintf(stderr, "Could not convert input samples (error '%s')\n",
441  get_error_text(error));
442  return error;
443  }
444 
450  if (avresample_available(resample_context)) {
451  fprintf(stderr, "Converted samples left over\n");
452  return AVERROR_EXIT;
453  }
454 
455  return 0;
456 }
457 
460  uint8_t **converted_input_samples,
461  const int frame_size)
462 {
463  int error;
464 
469  if ((error = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + frame_size)) < 0) {
470  fprintf(stderr, "Could not reallocate FIFO\n");
471  return error;
472  }
473 
475  if (av_audio_fifo_write(fifo, (void **)converted_input_samples,
476  frame_size) < frame_size) {
477  fprintf(stderr, "Could not write data to FIFO\n");
478  return AVERROR_EXIT;
479  }
480  return 0;
481 }
482 
488  AVFormatContext *input_format_context,
489  AVCodecContext *input_codec_context,
490  AVCodecContext *output_codec_context,
491  AVAudioResampleContext *resampler_context,
492  int *finished)
493 {
495  AVFrame *input_frame = NULL;
497  uint8_t **converted_input_samples = NULL;
498  int data_present;
499  int ret = AVERROR_EXIT;
500 
502  if (init_input_frame(&input_frame))
503  goto cleanup;
505  if (decode_audio_frame(input_frame, input_format_context,
506  input_codec_context, &data_present, finished))
507  goto cleanup;
513  if (*finished && !data_present) {
514  ret = 0;
515  goto cleanup;
516  }
518  if (data_present) {
520  if (init_converted_samples(&converted_input_samples, output_codec_context,
521  input_frame->nb_samples))
522  goto cleanup;
523 
528  if (convert_samples(input_frame->extended_data, converted_input_samples,
529  input_frame->nb_samples, resampler_context))
530  goto cleanup;
531 
533  if (add_samples_to_fifo(fifo, converted_input_samples,
534  input_frame->nb_samples))
535  goto cleanup;
536  ret = 0;
537  }
538  ret = 0;
539 
540 cleanup:
541  if (converted_input_samples) {
542  av_freep(&converted_input_samples[0]);
543  free(converted_input_samples);
544  }
545  av_frame_free(&input_frame);
546 
547  return ret;
548 }
549 
554 static int init_output_frame(AVFrame **frame,
555  AVCodecContext *output_codec_context,
556  int frame_size)
557 {
558  int error;
559 
561  if (!(*frame = av_frame_alloc())) {
562  fprintf(stderr, "Could not allocate output frame\n");
563  return AVERROR_EXIT;
564  }
565 
573  (*frame)->nb_samples = frame_size;
574  (*frame)->channel_layout = output_codec_context->channel_layout;
575  (*frame)->format = output_codec_context->sample_fmt;
576  (*frame)->sample_rate = output_codec_context->sample_rate;
577 
582  if ((error = av_frame_get_buffer(*frame, 0)) < 0) {
583  fprintf(stderr, "Could allocate output frame samples (error '%s')\n",
584  get_error_text(error));
585  av_frame_free(frame);
586  return error;
587  }
588 
589  return 0;
590 }
591 
593 static int64_t pts = 0;
594 
596 static int encode_audio_frame(AVFrame *frame,
597  AVFormatContext *output_format_context,
598  AVCodecContext *output_codec_context,
599  int *data_present)
600 {
603  int error;
604  init_packet(&output_packet);
605 
607  if (frame) {
608  frame->pts = pts;
609  pts += frame->nb_samples;
610  }
611 
616  if ((error = avcodec_encode_audio2(output_codec_context, &output_packet,
617  frame, data_present)) < 0) {
618  fprintf(stderr, "Could not encode frame (error '%s')\n",
619  get_error_text(error));
620  av_packet_unref(&output_packet);
621  return error;
622  }
623 
625  if (*data_present) {
626  if ((error = av_write_frame(output_format_context, &output_packet)) < 0) {
627  fprintf(stderr, "Could not write frame (error '%s')\n",
628  get_error_text(error));
629  av_packet_unref(&output_packet);
630  return error;
631  }
632 
633  av_packet_unref(&output_packet);
634  }
635 
636  return 0;
637 }
638 
644  AVFormatContext *output_format_context,
645  AVCodecContext *output_codec_context)
646 {
654  const int frame_size = FFMIN(av_audio_fifo_size(fifo),
655  output_codec_context->frame_size);
656  int data_written;
657 
659  if (init_output_frame(&output_frame, output_codec_context, frame_size))
660  return AVERROR_EXIT;
661 
666  if (av_audio_fifo_read(fifo, (void **)output_frame->data, frame_size) < frame_size) {
667  fprintf(stderr, "Could not read data from FIFO\n");
668  av_frame_free(&output_frame);
669  return AVERROR_EXIT;
670  }
671 
673  if (encode_audio_frame(output_frame, output_format_context,
674  output_codec_context, &data_written)) {
675  av_frame_free(&output_frame);
676  return AVERROR_EXIT;
677  }
678  av_frame_free(&output_frame);
679  return 0;
680 }
681 
683 static int write_output_file_trailer(AVFormatContext *output_format_context)
684 {
685  int error;
686  if ((error = av_write_trailer(output_format_context)) < 0) {
687  fprintf(stderr, "Could not write output file trailer (error '%s')\n",
688  get_error_text(error));
689  return error;
690  }
691  return 0;
692 }
693 
695 int main(int argc, char **argv)
696 {
697  AVFormatContext *input_format_context = NULL, *output_format_context = NULL;
698  AVCodecContext *input_codec_context = NULL, *output_codec_context = NULL;
699  AVAudioResampleContext *resample_context = NULL;
700  AVAudioFifo *fifo = NULL;
701  int ret = AVERROR_EXIT;
702 
703  if (argc < 3) {
704  fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
705  exit(1);
706  }
707 
709  av_register_all();
711  if (open_input_file(argv[1], &input_format_context,
712  &input_codec_context))
713  goto cleanup;
715  if (open_output_file(argv[2], input_codec_context,
716  &output_format_context, &output_codec_context))
717  goto cleanup;
719  if (init_resampler(input_codec_context, output_codec_context,
720  &resample_context))
721  goto cleanup;
723  if (init_fifo(&fifo, output_codec_context))
724  goto cleanup;
726  if (write_output_file_header(output_format_context))
727  goto cleanup;
728 
733  while (1) {
735  const int output_frame_size = output_codec_context->frame_size;
736  int finished = 0;
737 
745  while (av_audio_fifo_size(fifo) < output_frame_size) {
750  if (read_decode_convert_and_store(fifo, input_format_context,
751  input_codec_context,
752  output_codec_context,
753  resample_context, &finished))
754  goto cleanup;
755 
760  if (finished)
761  break;
762  }
763 
769  while (av_audio_fifo_size(fifo) >= output_frame_size ||
770  (finished && av_audio_fifo_size(fifo) > 0))
775  if (load_encode_and_write(fifo, output_format_context,
776  output_codec_context))
777  goto cleanup;
778 
783  if (finished) {
784  int data_written;
786  do {
787  if (encode_audio_frame(NULL, output_format_context,
788  output_codec_context, &data_written))
789  goto cleanup;
790  } while (data_written);
791  break;
792  }
793  }
794 
796  if (write_output_file_trailer(output_format_context))
797  goto cleanup;
798  ret = 0;
799 
800 cleanup:
801  if (fifo)
802  av_audio_fifo_free(fifo);
803  if (resample_context) {
804  avresample_close(resample_context);
805  avresample_free(&resample_context);
806  }
807  if (output_codec_context)
808  avcodec_free_context(&output_codec_context);
809  if (output_format_context) {
810  avio_close(output_format_context->pb);
811  avformat_free_context(output_format_context);
812  }
813  if (input_codec_context)
814  avcodec_free_context(&input_codec_context);
815  if (input_format_context)
816  avformat_close_input(&input_format_context);
817 
818  return ret;
819 }
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:928
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:2610
uint64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
Bytestream IO Context.
Definition: avio.h:104
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
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: utils.c:2010
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:252
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:470
int avresample_convert(AVAudioResampleContext *avr, uint8_t **output, int out_plane_size, int out_samples, uint8_t *const *input, int in_plane_size, int in_samples)
Convert input samples and write them to the output FIFO.
Definition: utils.c:330
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:284
static const uint8_t frame_size[4]
Definition: g723_1.h:219
attribute_deprecated 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:1624
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:1347
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:369
attribute_deprecated int avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of audio.
Definition: utils.c:1270
AVCodec.
Definition: avcodec.h:3120
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:202
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:60
Format I/O context.
Definition: avformat.h:940
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.
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2160
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:68
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:211
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
Definition: utils.c:2885
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:136
uint8_t * data
Definition: avcodec.h:1346
#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 int output_frame(H264Context *h, AVFrame *dst, AVFrame *src)
Definition: h264dec.c:663
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:80
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:983
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1503
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:307
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:2203
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
int bit_rate
the average bitrate
Definition: avcodec.h:1473
external API header
#define FFMIN(a, b)
Definition: common.h:66
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.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:136
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:419
static char *const get_error_text(const int error)
Convert an error code into a text message.
Definition: transcode_aac.c:52
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:705
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2172
NULL
Definition: eval.c:55
int avresample_available(AVAudioResampleContext *avr)
Return the number of available samples in the output FIFO.
Definition: utils.c:748
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
static int init_fifo(AVAudioFifo **fifo, AVCodecContext *output_codec_context)
Initialize a FIFO buffer for the audio samples to be encoded.
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...
Definition: options.c:151
#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:2152
main external API structure.
Definition: avcodec.h:1409
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: utils.c:2029
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:347
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 avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
Definition: utils.c:2837
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:850
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:2594
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1021
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:186
static int64_t pts
Global timestamp for the audio frames.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
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
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:784
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:2092
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:31
int den
denominator
Definition: rational.h:45
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:2626
Audio FIFO Buffer.
int channels
number of audio channels
Definition: avcodec.h:2153
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:714
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.
AVCodecParameters * codecpar
Definition: avformat.h:831
enum AVSampleFormat * sample_fmts
array of supported sample formats, or NULL if unknown, array is terminated by -1
Definition: avcodec.h:3143
static int init_output_frame(AVFrame **frame, AVCodecContext *output_codec_context, int frame_size)
Initialize one input frame for writing to the output file.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:742
static void output_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
Definition: avconv.c:362
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:174
static av_cold void cleanup(OMXCodecContext *s)
Definition: omx.c:576
This structure stores compressed data.
Definition: avcodec.h:1323
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:44
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:184
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition: avcodec.h:2605