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.

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