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.

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