transcode_aac.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * simple audio converter
  21. *
  22. * @example transcode_aac.c
  23. * Convert an input audio file to AAC in an MP4 container using FFmpeg.
  24. * @author Andreas Unterweger (dustsigns@gmail.com)
  25. */
  26. #include <stdio.h>
  27. #include "libavformat/avformat.h"
  28. #include "libavformat/avio.h"
  29. #include "libavcodec/avcodec.h"
  30. #include "libavutil/audio_fifo.h"
  31. #include "libavutil/avassert.h"
  32. #include "libavutil/avstring.h"
  33. #include "libavutil/frame.h"
  34. #include "libavutil/opt.h"
  35. #include "libswresample/swresample.h"
  36. /** The output bit rate in kbit/s */
  37. #define OUTPUT_BIT_RATE 96000
  38. /** The number of output channels */
  39. #define OUTPUT_CHANNELS 2
  40. /**
  41. * Convert an error code into a text message.
  42. * @param error Error code to be converted
  43. * @return Corresponding error text (not thread-safe)
  44. */
  45. static const char *get_error_text(const int error)
  46. {
  47. static char error_buffer[255];
  48. av_strerror(error, error_buffer, sizeof(error_buffer));
  49. return error_buffer;
  50. }
  51. /** Open an input file and the required decoder. */
  52. static int open_input_file(const char *filename,
  53. AVFormatContext **input_format_context,
  54. AVCodecContext **input_codec_context)
  55. {
  56. AVCodecContext *avctx;
  57. AVCodec *input_codec;
  58. int error;
  59. /** Open the input file to read from it. */
  60. if ((error = avformat_open_input(input_format_context, filename, NULL,
  61. NULL)) < 0) {
  62. fprintf(stderr, "Could not open input file '%s' (error '%s')\n",
  63. filename, get_error_text(error));
  64. *input_format_context = NULL;
  65. return error;
  66. }
  67. /** Get information on the input file (number of streams etc.). */
  68. if ((error = avformat_find_stream_info(*input_format_context, NULL)) < 0) {
  69. fprintf(stderr, "Could not open find stream info (error '%s')\n",
  70. get_error_text(error));
  71. avformat_close_input(input_format_context);
  72. return error;
  73. }
  74. /** Make sure that there is only one stream in the input file. */
  75. if ((*input_format_context)->nb_streams != 1) {
  76. fprintf(stderr, "Expected one audio input stream, but found %d\n",
  77. (*input_format_context)->nb_streams);
  78. avformat_close_input(input_format_context);
  79. return AVERROR_EXIT;
  80. }
  81. /** Find a decoder for the audio stream. */
  82. if (!(input_codec = avcodec_find_decoder((*input_format_context)->streams[0]->codecpar->codec_id))) {
  83. fprintf(stderr, "Could not find input codec\n");
  84. avformat_close_input(input_format_context);
  85. return AVERROR_EXIT;
  86. }
  87. /** allocate a new decoding context */
  88. avctx = avcodec_alloc_context3(input_codec);
  89. if (!avctx) {
  90. fprintf(stderr, "Could not allocate a decoding context\n");
  91. avformat_close_input(input_format_context);
  92. return AVERROR(ENOMEM);
  93. }
  94. /** initialize the stream parameters with demuxer information */
  95. error = avcodec_parameters_to_context(avctx, (*input_format_context)->streams[0]->codecpar);
  96. if (error < 0) {
  97. avformat_close_input(input_format_context);
  98. avcodec_free_context(&avctx);
  99. return error;
  100. }
  101. /** Open the decoder for the audio stream to use it later. */
  102. if ((error = avcodec_open2(avctx, input_codec, NULL)) < 0) {
  103. fprintf(stderr, "Could not open input codec (error '%s')\n",
  104. get_error_text(error));
  105. avcodec_free_context(&avctx);
  106. avformat_close_input(input_format_context);
  107. return error;
  108. }
  109. /** Save the decoder context for easier access later. */
  110. *input_codec_context = avctx;
  111. return 0;
  112. }
  113. /**
  114. * Open an output file and the required encoder.
  115. * Also set some basic encoder parameters.
  116. * Some of these parameters are based on the input file's parameters.
  117. */
  118. static int open_output_file(const char *filename,
  119. AVCodecContext *input_codec_context,
  120. AVFormatContext **output_format_context,
  121. AVCodecContext **output_codec_context)
  122. {
  123. AVCodecContext *avctx = NULL;
  124. AVIOContext *output_io_context = NULL;
  125. AVStream *stream = NULL;
  126. AVCodec *output_codec = NULL;
  127. int error;
  128. /** Open the output file to write to it. */
  129. if ((error = avio_open(&output_io_context, filename,
  130. AVIO_FLAG_WRITE)) < 0) {
  131. fprintf(stderr, "Could not open output file '%s' (error '%s')\n",
  132. filename, get_error_text(error));
  133. return error;
  134. }
  135. /** Create a new format context for the output container format. */
  136. if (!(*output_format_context = avformat_alloc_context())) {
  137. fprintf(stderr, "Could not allocate output format context\n");
  138. return AVERROR(ENOMEM);
  139. }
  140. /** Associate the output file (pointer) with the container format context. */
  141. (*output_format_context)->pb = output_io_context;
  142. /** Guess the desired container format based on the file extension. */
  143. if (!((*output_format_context)->oformat = av_guess_format(NULL, filename,
  144. NULL))) {
  145. fprintf(stderr, "Could not find output file format\n");
  146. goto cleanup;
  147. }
  148. av_strlcpy((*output_format_context)->filename, filename,
  149. sizeof((*output_format_context)->filename));
  150. /** Find the encoder to be used by its name. */
  151. if (!(output_codec = avcodec_find_encoder(AV_CODEC_ID_AAC))) {
  152. fprintf(stderr, "Could not find an AAC encoder.\n");
  153. goto cleanup;
  154. }
  155. /** Create a new audio stream in the output file container. */
  156. if (!(stream = avformat_new_stream(*output_format_context, NULL))) {
  157. fprintf(stderr, "Could not create new stream\n");
  158. error = AVERROR(ENOMEM);
  159. goto cleanup;
  160. }
  161. avctx = avcodec_alloc_context3(output_codec);
  162. if (!avctx) {
  163. fprintf(stderr, "Could not allocate an encoding context\n");
  164. error = AVERROR(ENOMEM);
  165. goto cleanup;
  166. }
  167. /**
  168. * Set the basic encoder parameters.
  169. * The input file's sample rate is used to avoid a sample rate conversion.
  170. */
  171. avctx->channels = OUTPUT_CHANNELS;
  172. avctx->channel_layout = av_get_default_channel_layout(OUTPUT_CHANNELS);
  173. avctx->sample_rate = input_codec_context->sample_rate;
  174. avctx->sample_fmt = output_codec->sample_fmts[0];
  175. avctx->bit_rate = OUTPUT_BIT_RATE;
  176. /** Allow the use of the experimental AAC encoder */
  177. avctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
  178. /** Set the sample rate for the container. */
  179. stream->time_base.den = input_codec_context->sample_rate;
  180. stream->time_base.num = 1;
  181. /**
  182. * Some container formats (like MP4) require global headers to be present
  183. * Mark the encoder so that it behaves accordingly.
  184. */
  185. if ((*output_format_context)->oformat->flags & AVFMT_GLOBALHEADER)
  186. avctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  187. /** Open the encoder for the audio stream to use it later. */
  188. if ((error = avcodec_open2(avctx, output_codec, NULL)) < 0) {
  189. fprintf(stderr, "Could not open output codec (error '%s')\n",
  190. get_error_text(error));
  191. goto cleanup;
  192. }
  193. error = avcodec_parameters_from_context(stream->codecpar, avctx);
  194. if (error < 0) {
  195. fprintf(stderr, "Could not initialize stream parameters\n");
  196. goto cleanup;
  197. }
  198. /** Save the encoder context for easier access later. */
  199. *output_codec_context = avctx;
  200. return 0;
  201. cleanup:
  202. avcodec_free_context(&avctx);
  203. avio_closep(&(*output_format_context)->pb);
  204. avformat_free_context(*output_format_context);
  205. *output_format_context = NULL;
  206. return error < 0 ? error : AVERROR_EXIT;
  207. }
  208. /** Initialize one data packet for reading or writing. */
  209. static void init_packet(AVPacket *packet)
  210. {
  211. av_init_packet(packet);
  212. /** Set the packet data and size so that it is recognized as being empty. */
  213. packet->data = NULL;
  214. packet->size = 0;
  215. }
  216. /** Initialize one audio frame for reading from the input file */
  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. /**
  226. * Initialize the audio resampler based on the input and output codec settings.
  227. * If the input and output sample formats differ, a conversion is required
  228. * libswresample takes care of this, but requires initialization.
  229. */
  230. static int init_resampler(AVCodecContext *input_codec_context,
  231. AVCodecContext *output_codec_context,
  232. SwrContext **resample_context)
  233. {
  234. int error;
  235. /**
  236. * Create a resampler context for the conversion.
  237. * Set the conversion parameters.
  238. * Default channel layouts based on the number of channels
  239. * are assumed for simplicity (they are sometimes not detected
  240. * properly by the demuxer and/or decoder).
  241. */
  242. *resample_context = swr_alloc_set_opts(NULL,
  243. av_get_default_channel_layout(output_codec_context->channels),
  244. output_codec_context->sample_fmt,
  245. output_codec_context->sample_rate,
  246. av_get_default_channel_layout(input_codec_context->channels),
  247. input_codec_context->sample_fmt,
  248. input_codec_context->sample_rate,
  249. 0, NULL);
  250. if (!*resample_context) {
  251. fprintf(stderr, "Could not allocate resample context\n");
  252. return AVERROR(ENOMEM);
  253. }
  254. /**
  255. * Perform a sanity check so that the number of converted samples is
  256. * not greater than the number of samples to be converted.
  257. * If the sample rates differ, this case has to be handled differently
  258. */
  259. av_assert0(output_codec_context->sample_rate == input_codec_context->sample_rate);
  260. /** Open the resampler with the specified parameters. */
  261. if ((error = swr_init(*resample_context)) < 0) {
  262. fprintf(stderr, "Could not open resample context\n");
  263. swr_free(resample_context);
  264. return error;
  265. }
  266. return 0;
  267. }
  268. /** Initialize a FIFO buffer for the audio samples to be encoded. */
  269. static int init_fifo(AVAudioFifo **fifo, AVCodecContext *output_codec_context)
  270. {
  271. /** Create the FIFO buffer based on the specified output sample format. */
  272. if (!(*fifo = av_audio_fifo_alloc(output_codec_context->sample_fmt,
  273. output_codec_context->channels, 1))) {
  274. fprintf(stderr, "Could not allocate FIFO\n");
  275. return AVERROR(ENOMEM);
  276. }
  277. return 0;
  278. }
  279. /** Write the header of the output file container. */
  280. static int write_output_file_header(AVFormatContext *output_format_context)
  281. {
  282. int error;
  283. if ((error = avformat_write_header(output_format_context, NULL)) < 0) {
  284. fprintf(stderr, "Could not write output file header (error '%s')\n",
  285. get_error_text(error));
  286. return error;
  287. }
  288. return 0;
  289. }
  290. /** Decode one audio frame from the input file. */
  291. static int decode_audio_frame(AVFrame *frame,
  292. AVFormatContext *input_format_context,
  293. AVCodecContext *input_codec_context,
  294. int *data_present, int *finished)
  295. {
  296. /** Packet used for temporary storage. */
  297. AVPacket input_packet;
  298. int error;
  299. init_packet(&input_packet);
  300. /** Read one audio frame from the input file into a temporary packet. */
  301. if ((error = av_read_frame(input_format_context, &input_packet)) < 0) {
  302. /** If we are at the end of the file, flush the decoder below. */
  303. if (error == AVERROR_EOF)
  304. *finished = 1;
  305. else {
  306. fprintf(stderr, "Could not read frame (error '%s')\n",
  307. get_error_text(error));
  308. return error;
  309. }
  310. }
  311. /**
  312. * Decode the audio frame stored in the temporary packet.
  313. * The input audio stream decoder is used to do this.
  314. * If we are at the end of the file, pass an empty packet to the decoder
  315. * to flush it.
  316. */
  317. if ((error = avcodec_decode_audio4(input_codec_context, frame,
  318. data_present, &input_packet)) < 0) {
  319. fprintf(stderr, "Could not decode frame (error '%s')\n",
  320. get_error_text(error));
  321. av_packet_unref(&input_packet);
  322. return error;
  323. }
  324. /**
  325. * If the decoder has not been flushed completely, we are not finished,
  326. * so that this function has to be called again.
  327. */
  328. if (*finished && *data_present)
  329. *finished = 0;
  330. av_packet_unref(&input_packet);
  331. return 0;
  332. }
  333. /**
  334. * Initialize a temporary storage for the specified number of audio samples.
  335. * The conversion requires temporary storage due to the different format.
  336. * The number of audio samples to be allocated is specified in frame_size.
  337. */
  338. static int init_converted_samples(uint8_t ***converted_input_samples,
  339. AVCodecContext *output_codec_context,
  340. int frame_size)
  341. {
  342. int error;
  343. /**
  344. * Allocate as many pointers as there are audio channels.
  345. * Each pointer will later point to the audio samples of the corresponding
  346. * channels (although it may be NULL for interleaved formats).
  347. */
  348. if (!(*converted_input_samples = calloc(output_codec_context->channels,
  349. sizeof(**converted_input_samples)))) {
  350. fprintf(stderr, "Could not allocate converted input sample pointers\n");
  351. return AVERROR(ENOMEM);
  352. }
  353. /**
  354. * Allocate memory for the samples of all channels in one consecutive
  355. * block for convenience.
  356. */
  357. if ((error = av_samples_alloc(*converted_input_samples, NULL,
  358. output_codec_context->channels,
  359. frame_size,
  360. output_codec_context->sample_fmt, 0)) < 0) {
  361. fprintf(stderr,
  362. "Could not allocate converted input samples (error '%s')\n",
  363. get_error_text(error));
  364. av_freep(&(*converted_input_samples)[0]);
  365. free(*converted_input_samples);
  366. return error;
  367. }
  368. return 0;
  369. }
  370. /**
  371. * Convert the input audio samples into the output sample format.
  372. * The conversion happens on a per-frame basis, the size of which is specified
  373. * by frame_size.
  374. */
  375. static int convert_samples(const uint8_t **input_data,
  376. uint8_t **converted_data, const int frame_size,
  377. SwrContext *resample_context)
  378. {
  379. int error;
  380. /** Convert the samples using the resampler. */
  381. if ((error = swr_convert(resample_context,
  382. converted_data, frame_size,
  383. input_data , frame_size)) < 0) {
  384. fprintf(stderr, "Could not convert input samples (error '%s')\n",
  385. get_error_text(error));
  386. return error;
  387. }
  388. return 0;
  389. }
  390. /** Add converted input audio samples to the FIFO buffer for later processing. */
  391. static int add_samples_to_fifo(AVAudioFifo *fifo,
  392. uint8_t **converted_input_samples,
  393. const int frame_size)
  394. {
  395. int error;
  396. /**
  397. * Make the FIFO as large as it needs to be to hold both,
  398. * the old and the new samples.
  399. */
  400. if ((error = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + frame_size)) < 0) {
  401. fprintf(stderr, "Could not reallocate FIFO\n");
  402. return error;
  403. }
  404. /** Store the new samples in the FIFO buffer. */
  405. if (av_audio_fifo_write(fifo, (void **)converted_input_samples,
  406. frame_size) < frame_size) {
  407. fprintf(stderr, "Could not write data to FIFO\n");
  408. return AVERROR_EXIT;
  409. }
  410. return 0;
  411. }
  412. /**
  413. * Read one audio frame from the input file, decodes, converts and stores
  414. * it in the FIFO buffer.
  415. */
  416. static int read_decode_convert_and_store(AVAudioFifo *fifo,
  417. AVFormatContext *input_format_context,
  418. AVCodecContext *input_codec_context,
  419. AVCodecContext *output_codec_context,
  420. SwrContext *resampler_context,
  421. int *finished)
  422. {
  423. /** Temporary storage of the input samples of the frame read from the file. */
  424. AVFrame *input_frame = NULL;
  425. /** Temporary storage for the converted input samples. */
  426. uint8_t **converted_input_samples = NULL;
  427. int data_present;
  428. int ret = AVERROR_EXIT;
  429. /** Initialize temporary storage for one input frame. */
  430. if (init_input_frame(&input_frame))
  431. goto cleanup;
  432. /** Decode one frame worth of audio samples. */
  433. if (decode_audio_frame(input_frame, input_format_context,
  434. input_codec_context, &data_present, finished))
  435. goto cleanup;
  436. /**
  437. * If we are at the end of the file and there are no more samples
  438. * in the decoder which are delayed, we are actually finished.
  439. * This must not be treated as an error.
  440. */
  441. if (*finished && !data_present) {
  442. ret = 0;
  443. goto cleanup;
  444. }
  445. /** If there is decoded data, convert and store it */
  446. if (data_present) {
  447. /** Initialize the temporary storage for the converted input samples. */
  448. if (init_converted_samples(&converted_input_samples, output_codec_context,
  449. input_frame->nb_samples))
  450. goto cleanup;
  451. /**
  452. * Convert the input samples to the desired output sample format.
  453. * This requires a temporary storage provided by converted_input_samples.
  454. */
  455. if (convert_samples((const uint8_t**)input_frame->extended_data, converted_input_samples,
  456. input_frame->nb_samples, resampler_context))
  457. goto cleanup;
  458. /** Add the converted input samples to the FIFO buffer for later processing. */
  459. if (add_samples_to_fifo(fifo, converted_input_samples,
  460. input_frame->nb_samples))
  461. goto cleanup;
  462. ret = 0;
  463. }
  464. ret = 0;
  465. cleanup:
  466. if (converted_input_samples) {
  467. av_freep(&converted_input_samples[0]);
  468. free(converted_input_samples);
  469. }
  470. av_frame_free(&input_frame);
  471. return ret;
  472. }
  473. /**
  474. * Initialize one input frame for writing to the output file.
  475. * The frame will be exactly frame_size samples large.
  476. */
  477. static int init_output_frame(AVFrame **frame,
  478. AVCodecContext *output_codec_context,
  479. int frame_size)
  480. {
  481. int error;
  482. /** Create a new frame to store the audio samples. */
  483. if (!(*frame = av_frame_alloc())) {
  484. fprintf(stderr, "Could not allocate output frame\n");
  485. return AVERROR_EXIT;
  486. }
  487. /**
  488. * Set the frame's parameters, especially its size and format.
  489. * av_frame_get_buffer needs this to allocate memory for the
  490. * audio samples of the frame.
  491. * Default channel layouts based on the number of channels
  492. * are assumed for simplicity.
  493. */
  494. (*frame)->nb_samples = frame_size;
  495. (*frame)->channel_layout = output_codec_context->channel_layout;
  496. (*frame)->format = output_codec_context->sample_fmt;
  497. (*frame)->sample_rate = output_codec_context->sample_rate;
  498. /**
  499. * Allocate the samples of the created frame. This call will make
  500. * sure that the audio frame can hold as many samples as specified.
  501. */
  502. if ((error = av_frame_get_buffer(*frame, 0)) < 0) {
  503. fprintf(stderr, "Could allocate output frame samples (error '%s')\n",
  504. get_error_text(error));
  505. av_frame_free(frame);
  506. return error;
  507. }
  508. return 0;
  509. }
  510. /** Global timestamp for the audio frames */
  511. static int64_t pts = 0;
  512. /** Encode one frame worth of audio to the output file. */
  513. static int encode_audio_frame(AVFrame *frame,
  514. AVFormatContext *output_format_context,
  515. AVCodecContext *output_codec_context,
  516. int *data_present)
  517. {
  518. /** Packet used for temporary storage. */
  519. AVPacket output_packet;
  520. int error;
  521. init_packet(&output_packet);
  522. /** Set a timestamp based on the sample rate for the container. */
  523. if (frame) {
  524. frame->pts = pts;
  525. pts += frame->nb_samples;
  526. }
  527. /**
  528. * Encode the audio frame and store it in the temporary packet.
  529. * The output audio stream encoder is used to do this.
  530. */
  531. if ((error = avcodec_encode_audio2(output_codec_context, &output_packet,
  532. frame, data_present)) < 0) {
  533. fprintf(stderr, "Could not encode frame (error '%s')\n",
  534. get_error_text(error));
  535. av_packet_unref(&output_packet);
  536. return error;
  537. }
  538. /** Write one audio frame from the temporary packet to the output file. */
  539. if (*data_present) {
  540. if ((error = av_write_frame(output_format_context, &output_packet)) < 0) {
  541. fprintf(stderr, "Could not write frame (error '%s')\n",
  542. get_error_text(error));
  543. av_packet_unref(&output_packet);
  544. return error;
  545. }
  546. av_packet_unref(&output_packet);
  547. }
  548. return 0;
  549. }
  550. /**
  551. * Load one audio frame from the FIFO buffer, encode and write it to the
  552. * output file.
  553. */
  554. static int load_encode_and_write(AVAudioFifo *fifo,
  555. AVFormatContext *output_format_context,
  556. AVCodecContext *output_codec_context)
  557. {
  558. /** Temporary storage of the output samples of the frame written to the file. */
  559. AVFrame *output_frame;
  560. /**
  561. * Use the maximum number of possible samples per frame.
  562. * If there is less than the maximum possible frame size in the FIFO
  563. * buffer use this number. Otherwise, use the maximum possible frame size
  564. */
  565. const int frame_size = FFMIN(av_audio_fifo_size(fifo),
  566. output_codec_context->frame_size);
  567. int data_written;
  568. /** Initialize temporary storage for one output frame. */
  569. if (init_output_frame(&output_frame, output_codec_context, frame_size))
  570. return AVERROR_EXIT;
  571. /**
  572. * Read as many samples from the FIFO buffer as required to fill the frame.
  573. * The samples are stored in the frame temporarily.
  574. */
  575. if (av_audio_fifo_read(fifo, (void **)output_frame->data, frame_size) < frame_size) {
  576. fprintf(stderr, "Could not read data from FIFO\n");
  577. av_frame_free(&output_frame);
  578. return AVERROR_EXIT;
  579. }
  580. /** Encode one frame worth of audio samples. */
  581. if (encode_audio_frame(output_frame, output_format_context,
  582. output_codec_context, &data_written)) {
  583. av_frame_free(&output_frame);
  584. return AVERROR_EXIT;
  585. }
  586. av_frame_free(&output_frame);
  587. return 0;
  588. }
  589. /** Write the trailer of the output file container. */
  590. static int write_output_file_trailer(AVFormatContext *output_format_context)
  591. {
  592. int error;
  593. if ((error = av_write_trailer(output_format_context)) < 0) {
  594. fprintf(stderr, "Could not write output file trailer (error '%s')\n",
  595. get_error_text(error));
  596. return error;
  597. }
  598. return 0;
  599. }
  600. /** Convert an audio file to an AAC file in an MP4 container. */
  601. int main(int argc, char **argv)
  602. {
  603. AVFormatContext *input_format_context = NULL, *output_format_context = NULL;
  604. AVCodecContext *input_codec_context = NULL, *output_codec_context = NULL;
  605. SwrContext *resample_context = NULL;
  606. AVAudioFifo *fifo = NULL;
  607. int ret = AVERROR_EXIT;
  608. if (argc < 3) {
  609. fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
  610. exit(1);
  611. }
  612. /** Register all codecs and formats so that they can be used. */
  613. av_register_all();
  614. /** Open the input file for reading. */
  615. if (open_input_file(argv[1], &input_format_context,
  616. &input_codec_context))
  617. goto cleanup;
  618. /** Open the output file for writing. */
  619. if (open_output_file(argv[2], input_codec_context,
  620. &output_format_context, &output_codec_context))
  621. goto cleanup;
  622. /** Initialize the resampler to be able to convert audio sample formats. */
  623. if (init_resampler(input_codec_context, output_codec_context,
  624. &resample_context))
  625. goto cleanup;
  626. /** Initialize the FIFO buffer to store audio samples to be encoded. */
  627. if (init_fifo(&fifo, output_codec_context))
  628. goto cleanup;
  629. /** Write the header of the output file container. */
  630. if (write_output_file_header(output_format_context))
  631. goto cleanup;
  632. /**
  633. * Loop as long as we have input samples to read or output samples
  634. * to write; abort as soon as we have neither.
  635. */
  636. while (1) {
  637. /** Use the encoder's desired frame size for processing. */
  638. const int output_frame_size = output_codec_context->frame_size;
  639. int finished = 0;
  640. /**
  641. * Make sure that there is one frame worth of samples in the FIFO
  642. * buffer so that the encoder can do its work.
  643. * Since the decoder's and the encoder's frame size may differ, we
  644. * need to FIFO buffer to store as many frames worth of input samples
  645. * that they make up at least one frame worth of output samples.
  646. */
  647. while (av_audio_fifo_size(fifo) < output_frame_size) {
  648. /**
  649. * Decode one frame worth of audio samples, convert it to the
  650. * output sample format and put it into the FIFO buffer.
  651. */
  652. if (read_decode_convert_and_store(fifo, input_format_context,
  653. input_codec_context,
  654. output_codec_context,
  655. resample_context, &finished))
  656. goto cleanup;
  657. /**
  658. * If we are at the end of the input file, we continue
  659. * encoding the remaining audio samples to the output file.
  660. */
  661. if (finished)
  662. break;
  663. }
  664. /**
  665. * If we have enough samples for the encoder, we encode them.
  666. * At the end of the file, we pass the remaining samples to
  667. * the encoder.
  668. */
  669. while (av_audio_fifo_size(fifo) >= output_frame_size ||
  670. (finished && av_audio_fifo_size(fifo) > 0))
  671. /**
  672. * Take one frame worth of audio samples from the FIFO buffer,
  673. * encode it and write it to the output file.
  674. */
  675. if (load_encode_and_write(fifo, output_format_context,
  676. output_codec_context))
  677. goto cleanup;
  678. /**
  679. * If we are at the end of the input file and have encoded
  680. * all remaining samples, we can exit this loop and finish.
  681. */
  682. if (finished) {
  683. int data_written;
  684. /** Flush the encoder as it may have delayed frames. */
  685. do {
  686. if (encode_audio_frame(NULL, output_format_context,
  687. output_codec_context, &data_written))
  688. goto cleanup;
  689. } while (data_written);
  690. break;
  691. }
  692. }
  693. /** Write the trailer of the output file container. */
  694. if (write_output_file_trailer(output_format_context))
  695. goto cleanup;
  696. ret = 0;
  697. cleanup:
  698. if (fifo)
  699. av_audio_fifo_free(fifo);
  700. swr_free(&resample_context);
  701. if (output_codec_context)
  702. avcodec_free_context(&output_codec_context);
  703. if (output_format_context) {
  704. avio_closep(&output_format_context->pb);
  705. avformat_free_context(output_format_context);
  706. }
  707. if (input_codec_context)
  708. avcodec_free_context(&input_codec_context);
  709. if (input_format_context)
  710. avformat_close_input(&input_format_context);
  711. return ret;
  712. }