You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

758 lines
28KB

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