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.

756 lines
27KB

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