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 }