ff_audio_resample.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //
  2. // Created by xu fulong on 2022/7/12.
  3. //
  4. #include "ff_audio_resample.h"
  5. #define ALOGE(Format, ...) LOGE("audio_resample", Format, ##__VA_ARGS__)
  6. FFAudioResample::FFAudioResample() {
  7. resample = new AudioResample();
  8. }
  9. FFAudioResample::~FFAudioResample() {
  10. delete resample;
  11. }
  12. static int initOutputFrame(AudioResample **pResample) {
  13. AudioResample *ar = *pResample;
  14. AVFrame *frame = av_frame_alloc();
  15. frame->format = ar->outCodecCtx->sample_fmt;
  16. frame->nb_samples = ar->outCodecCtx->frame_size;
  17. frame->sample_rate = ar->outCodecCtx->sample_rate;
  18. frame->channel_layout = ar->outCodecCtx->channel_layout;
  19. int ret = av_frame_get_buffer(frame, 0);
  20. ar->outFrame = frame;
  21. *pResample = ar;
  22. return ret;
  23. }
  24. static int initResample(AudioResample **pResample) {
  25. AudioResample *ar = *pResample;
  26. SwrContext *context = swr_alloc_set_opts(nullptr,
  27. av_get_default_channel_layout(ar->outCodecCtx->channels),
  28. ar->outCodecCtx->sample_fmt,
  29. ar->outCodecCtx->sample_rate,
  30. av_get_default_channel_layout(ar->inCodecCtx->channels),
  31. ar->inCodecCtx->sample_fmt,
  32. ar->inCodecCtx->sample_rate,
  33. 0, nullptr);
  34. int ret = swr_init(context);
  35. ar->resampleCtx = context;
  36. *pResample = ar;
  37. return ret;
  38. }
  39. static int initConvertedSamples(AudioResample **pResample, uint8_t ***converted_input_samples, int frame_size) {
  40. int ret;
  41. AudioResample *ar = *pResample;
  42. *converted_input_samples = (uint8_t **) calloc(ar->outCodecCtx->channels, sizeof(**converted_input_samples));
  43. if ((ret = av_samples_alloc(*converted_input_samples, nullptr,
  44. ar->outCodecCtx->channels,
  45. frame_size,
  46. ar->outCodecCtx->sample_fmt, 0)) < 0) {
  47. ALOGE("av_samples_alloc error:%s", av_err2str(ret));
  48. av_freep(&(*converted_input_samples)[0]);
  49. free(*converted_input_samples);
  50. return ret;
  51. }
  52. return 0;
  53. }
  54. int FFAudioResample::openInputFile(const char *filename) {
  55. int ret;
  56. const AVCodec *input_codec;
  57. AVStream *audio_stream = nullptr;
  58. if ((ret = avformat_open_input(&resample->inFormatCtx, filename, nullptr,nullptr)) < 0) {
  59. ALOGE("Could not open input file:%s\n", av_err2str(ret));
  60. return ret;
  61. }
  62. avformat_find_stream_info(resample->inFormatCtx, nullptr);
  63. for (int i = 0; i < resample->inFormatCtx->nb_streams; ++i) {
  64. if (resample->inFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  65. audio_stream = resample->inFormatCtx->streams[i];
  66. }
  67. }
  68. if (!(input_codec = avcodec_find_decoder(audio_stream->codecpar->codec_id))) {
  69. ALOGE("Could not find input codec:%s\n", avcodec_get_name(audio_stream->codecpar->codec_id));
  70. return -1;
  71. }
  72. resample->inCodecCtx = avcodec_alloc_context3(input_codec);
  73. avcodec_parameters_to_context(resample->inCodecCtx, audio_stream->codecpar);
  74. if ((ret = avcodec_open2(resample->inCodecCtx, input_codec, nullptr)) < 0) {
  75. ALOGE("Could not open input codec (error:%s)\n", av_err2str(ret));
  76. }
  77. resample->inFrame = av_frame_alloc();
  78. return 0;
  79. }
  80. int FFAudioResample::openOutputFile(const char *filename, int sample_rate) {
  81. AVIOContext *output_io_context = nullptr;
  82. const AVCodec *output_codec;
  83. int ret;
  84. if ((ret = avio_open(&output_io_context, filename, AVIO_FLAG_WRITE)) < 0) {
  85. ALOGE("Could not open output file:%s\n", av_err2str(ret));
  86. return ret;
  87. }
  88. resample->outFormatCtx = avformat_alloc_context();
  89. resample->outFormatCtx->pb = output_io_context;
  90. resample->outFormatCtx->url = av_strdup(filename);
  91. resample->outFormatCtx->oformat = av_guess_format(nullptr, filename,nullptr);
  92. if (!(resample->outFormatCtx->oformat)) {
  93. ALOGE("Could not find output file format\n");
  94. return -1;
  95. }
  96. /* Find the encoder to be used by its name. */
  97. if (!(output_codec = avcodec_find_encoder(resample->inCodecCtx->codec_id))) {
  98. ALOGE( "Could not find encoder=%s\n", resample->inCodecCtx->codec->name);
  99. return -1;
  100. }
  101. /* Create a new audio stream in the output file container. */
  102. AVStream *stream = avformat_new_stream(resample->outFormatCtx, nullptr);
  103. resample->outCodecCtx = avcodec_alloc_context3(output_codec);
  104. /* Set the basic encoder parameters.*/
  105. resample->outCodecCtx->channels = resample->inCodecCtx->channels;
  106. resample->outCodecCtx->channel_layout = av_get_default_channel_layout(resample->inCodecCtx->channels);
  107. resample->outCodecCtx->sample_rate = sample_rate;
  108. resample->outCodecCtx->sample_fmt = output_codec->sample_fmts[0];
  109. /* Allow the use of the experimental AAC encoder. */
  110. resample->outCodecCtx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
  111. /* Set the sample rate for the container. */
  112. stream->time_base.den = sample_rate;
  113. stream->time_base.num = 1;
  114. /* Some container formats (like MP4) require global headers to be present.
  115. * Mark the encoder so that it behaves accordingly. */
  116. if (resample->outFormatCtx->oformat->flags & AVFMT_GLOBALHEADER)
  117. resample->outCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  118. /* Open the encoder for the audio stream to use it later. */
  119. if ((ret = avcodec_open2(resample->outCodecCtx, output_codec, nullptr)) < 0) {
  120. ALOGE("Could not open output codec (error:%s)\n", av_err2str(ret));
  121. return ret;
  122. }
  123. avcodec_parameters_from_context(stream->codecpar, resample->outCodecCtx);
  124. return 0;
  125. }
  126. int FFAudioResample::decodeAudioFrame(AVFrame *frame, int *data_present, int *finished) {
  127. int ret;
  128. if ((ret = av_read_frame(resample->inFormatCtx, &resample->inPacket)) < 0) {
  129. if (ret == AVERROR_EOF)
  130. *finished = 1;
  131. else {
  132. ALOGE("Could not read frame (error:%s)\n", av_err2str(ret));
  133. return ret;
  134. }
  135. }
  136. if (resample->inFormatCtx->streams[resample->inPacket.stream_index]->codecpar->codec_type
  137. != AVMEDIA_TYPE_AUDIO) {
  138. ret = 0;
  139. ALOGE("isn't audio packet, skip it...");
  140. goto cleanup;
  141. }
  142. /* Send the audio frame stored in the temporary packet to the decoder.*/
  143. if ((ret = avcodec_send_packet(resample->inCodecCtx, &resample->inPacket)) < 0) {
  144. ALOGE("Could not send packet for decoding (error:%s)\n", av_err2str(ret));
  145. return ret;
  146. }
  147. /* Receive one frame from the decoder. */
  148. ret = avcodec_receive_frame(resample->inCodecCtx, frame);
  149. if (ret == AVERROR(EAGAIN)) {
  150. ret = 0;
  151. goto cleanup;
  152. } else if (ret == AVERROR_EOF) {
  153. *finished = 1;
  154. ret = 0;
  155. goto cleanup;
  156. } else if (ret < 0) {
  157. ALOGE("Could not decode frame (error:%s)\n", av_err2str(ret));
  158. goto cleanup;
  159. } else {
  160. *data_present = 1;
  161. goto cleanup;
  162. }
  163. cleanup:
  164. av_packet_unref(&resample->inPacket);
  165. return ret;
  166. }
  167. /**
  168. * Read one audio frame from the input file, decode, convert and store
  169. * it in the FIFO buffer.
  170. *
  171. */
  172. int FFAudioResample::decodeAndConvert(int *finished) {
  173. uint8_t **converted_dst_samples = nullptr;
  174. int data_present = 0;
  175. int ret = AVERROR_EXIT;
  176. /* Decode one frame worth of audio samples. */
  177. if (decodeAudioFrame(resample->inFrame, &data_present, finished))
  178. goto cleanup;
  179. if (*finished) {
  180. ret = 0;
  181. goto cleanup;
  182. }
  183. /* If there is decoded data, convert and store it. */
  184. if (data_present) {
  185. int dst_nb_samples = (int) av_rescale_rnd(resample->inFrame->nb_samples, resample->outCodecCtx->sample_rate,
  186. resample->inCodecCtx->sample_rate, AV_ROUND_UP);
  187. if (initConvertedSamples(&resample, &converted_dst_samples, dst_nb_samples))
  188. goto cleanup;
  189. ret = swr_convert(resample->resampleCtx, converted_dst_samples, dst_nb_samples,
  190. (const uint8_t**)resample->inFrame->extended_data, resample->inFrame->nb_samples);
  191. if (ret < 0) {
  192. ALOGE("Could not convert input samples (error:%s)\n", av_err2str(ret));
  193. goto cleanup;
  194. }
  195. av_audio_fifo_write(resample->fifo, (void **)converted_dst_samples, ret);
  196. }
  197. ret = 0;
  198. cleanup:
  199. if (converted_dst_samples) {
  200. av_freep(&converted_dst_samples[0]);
  201. free(converted_dst_samples);
  202. }
  203. return ret;
  204. }
  205. int FFAudioResample::encodeAudioFrame(AVFrame *frame, int *data_present) {
  206. int ret;
  207. /* Set a timestamp based on the sample rate for the container. */
  208. if (frame) {
  209. frame->pts = resample->pts;
  210. resample->pts += frame->nb_samples;
  211. }
  212. ret = avcodec_send_frame(resample->outCodecCtx, frame);
  213. if (ret == AVERROR_EOF) {
  214. ret = 0;
  215. goto cleanup;
  216. } else if (ret < 0) {
  217. ALOGE("Could not send packet for encoding (error:%s)\n", av_err2str(ret));
  218. return ret;
  219. }
  220. ret = avcodec_receive_packet(resample->outCodecCtx, &resample->outPacket);
  221. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  222. ret = 0;
  223. goto cleanup;
  224. } else if (ret < 0) {
  225. ALOGE("Could not encode frame (error:%s)\n", av_err2str(ret));
  226. goto cleanup;
  227. } else {
  228. *data_present = 1;
  229. }
  230. /* Write one audio frame from the temporary packet to the output file. */
  231. if (*data_present &&
  232. (ret = av_write_frame(resample->outFormatCtx, &resample->outPacket)) < 0) {
  233. ALOGE("Could not write frame (error:%s)\n", av_err2str(ret));
  234. }
  235. cleanup:
  236. av_packet_unref(&resample->outPacket);
  237. return ret;
  238. }
  239. /**
  240. * Load one audio frame from the FIFO buffer, encode and write it to the
  241. * output file.
  242. *
  243. */
  244. int FFAudioResample::encodeAndWrite() {
  245. int data_written;
  246. const int frame_size = FFMIN(av_audio_fifo_size(resample->fifo),
  247. resample->outCodecCtx->frame_size);
  248. resample->outFrame->nb_samples = frame_size;
  249. if (av_audio_fifo_read(resample->fifo, (void **)resample->outFrame->data, frame_size) < frame_size) {
  250. ALOGE("Could not read data from FIFO\n");
  251. return AVERROR_EXIT;
  252. }
  253. if (encodeAudioFrame(resample->outFrame, &data_written)) {
  254. return AVERROR_EXIT;
  255. }
  256. return 0;
  257. }
  258. int FFAudioResample::resampling(const char *src_file, const char *dst_file, int sampleRate) {
  259. int ret = AVERROR_EXIT;
  260. /* Open the input file for reading. */
  261. if (openInputFile(src_file))
  262. goto cleanup;
  263. /* Open the output file for writing. */
  264. if (openOutputFile(dst_file, sampleRate))
  265. goto cleanup;
  266. /* Initialize the re-sampler to be able to convert audio sample formats. */
  267. if (initResample(&resample))
  268. goto cleanup;
  269. /* Initialize the FIFO buffer to store audio samples to be encoded. */
  270. resample->fifo = av_audio_fifo_alloc(resample->outCodecCtx->sample_fmt,
  271. resample->outCodecCtx->channels, 1024 * 10);
  272. if (initOutputFrame(&resample))
  273. goto cleanup;
  274. /* Write the header of the output file container. */
  275. if ((ret = avformat_write_header(resample->outFormatCtx, nullptr)) < 0) {
  276. ALOGE("write header error=%s", av_err2str(ret));
  277. }
  278. while (true) {
  279. int finished = 0;
  280. const int output_frame_size = resample->outCodecCtx->frame_size;
  281. while (av_audio_fifo_size(resample->fifo) < output_frame_size) {
  282. /* Decode one frame, convert sample format and put it into the FIFO buffer. */
  283. if (decodeAndConvert(&finished))
  284. goto cleanup;
  285. if (finished)
  286. break;
  287. }
  288. /* If we have enough samples for the encoder, we encode them.*/
  289. while (av_audio_fifo_size(resample->fifo) >= output_frame_size ||
  290. (finished && av_audio_fifo_size(resample->fifo) > 0))
  291. if (encodeAndWrite())
  292. goto cleanup;
  293. /* encode all the remaining samples. */
  294. if (finished) {
  295. int data_written;
  296. do {
  297. data_written = 0;
  298. if (encodeAudioFrame(nullptr, &data_written))
  299. goto cleanup;
  300. } while (data_written);
  301. break;
  302. }
  303. }
  304. /* Write the trailer of the output file container. */
  305. if (av_write_trailer(resample->outFormatCtx)) {
  306. ALOGE("write trailer error...");
  307. }
  308. ret = 0;
  309. cleanup:
  310. if (resample->fifo)
  311. av_audio_fifo_free(resample->fifo);
  312. swr_free(&(resample->resampleCtx));
  313. if (resample->outCodecCtx)
  314. avcodec_free_context(&(resample->outCodecCtx));
  315. if (resample->outFormatCtx) {
  316. avio_closep(&(resample->outFormatCtx->pb));
  317. avformat_free_context(resample->outFormatCtx);
  318. }
  319. if (resample->inCodecCtx)
  320. avcodec_free_context(&(resample->inCodecCtx));
  321. if (resample->inFormatCtx)
  322. avformat_close_input(&(resample->inFormatCtx));
  323. if (resample->inFrame)
  324. av_frame_free(&(resample->inFrame));
  325. if (resample->outFrame)
  326. av_frame_free(&(resample->outFrame));
  327. return ret;
  328. }