utils.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/common.h"
  21. #include "libavutil/dict.h"
  22. // #include "libavutil/error.h"
  23. #include "libavutil/log.h"
  24. #include "libavutil/mem.h"
  25. #include "libavutil/opt.h"
  26. #include "avresample.h"
  27. #include "audio_data.h"
  28. #include "internal.h"
  29. int avresample_open(AVAudioResampleContext *avr)
  30. {
  31. int ret;
  32. /* set channel mixing parameters */
  33. avr->in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
  34. if (avr->in_channels <= 0 || avr->in_channels > AVRESAMPLE_MAX_CHANNELS) {
  35. av_log(avr, AV_LOG_ERROR, "Invalid input channel layout: %"PRIu64"\n",
  36. avr->in_channel_layout);
  37. return AVERROR(EINVAL);
  38. }
  39. avr->out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
  40. if (avr->out_channels <= 0 || avr->out_channels > AVRESAMPLE_MAX_CHANNELS) {
  41. av_log(avr, AV_LOG_ERROR, "Invalid output channel layout: %"PRIu64"\n",
  42. avr->out_channel_layout);
  43. return AVERROR(EINVAL);
  44. }
  45. avr->resample_channels = FFMIN(avr->in_channels, avr->out_channels);
  46. avr->downmix_needed = avr->in_channels > avr->out_channels;
  47. avr->upmix_needed = avr->out_channels > avr->in_channels ||
  48. (!avr->downmix_needed && (avr->mix_matrix ||
  49. avr->in_channel_layout != avr->out_channel_layout));
  50. avr->mixing_needed = avr->downmix_needed || avr->upmix_needed;
  51. /* set resampling parameters */
  52. avr->resample_needed = avr->in_sample_rate != avr->out_sample_rate ||
  53. avr->force_resampling;
  54. /* select internal sample format if not specified by the user */
  55. if (avr->internal_sample_fmt == AV_SAMPLE_FMT_NONE &&
  56. (avr->mixing_needed || avr->resample_needed)) {
  57. enum AVSampleFormat in_fmt = av_get_planar_sample_fmt(avr->in_sample_fmt);
  58. enum AVSampleFormat out_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
  59. int max_bps = FFMAX(av_get_bytes_per_sample(in_fmt),
  60. av_get_bytes_per_sample(out_fmt));
  61. if (max_bps <= 2) {
  62. avr->internal_sample_fmt = AV_SAMPLE_FMT_S16P;
  63. } else if (avr->mixing_needed) {
  64. avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
  65. } else {
  66. if (max_bps <= 4) {
  67. if (in_fmt == AV_SAMPLE_FMT_S32P ||
  68. out_fmt == AV_SAMPLE_FMT_S32P) {
  69. if (in_fmt == AV_SAMPLE_FMT_FLTP ||
  70. out_fmt == AV_SAMPLE_FMT_FLTP) {
  71. /* if one is s32 and the other is flt, use dbl */
  72. avr->internal_sample_fmt = AV_SAMPLE_FMT_DBLP;
  73. } else {
  74. /* if one is s32 and the other is s32, s16, or u8, use s32 */
  75. avr->internal_sample_fmt = AV_SAMPLE_FMT_S32P;
  76. }
  77. } else {
  78. /* if one is flt and the other is flt, s16 or u8, use flt */
  79. avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
  80. }
  81. } else {
  82. /* if either is dbl, use dbl */
  83. avr->internal_sample_fmt = AV_SAMPLE_FMT_DBLP;
  84. }
  85. }
  86. av_log(avr, AV_LOG_DEBUG, "Using %s as internal sample format\n",
  87. av_get_sample_fmt_name(avr->internal_sample_fmt));
  88. }
  89. /* set sample format conversion parameters */
  90. if (avr->in_channels == 1)
  91. avr->in_sample_fmt = av_get_planar_sample_fmt(avr->in_sample_fmt);
  92. if (avr->out_channels == 1)
  93. avr->out_sample_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
  94. avr->in_convert_needed = (avr->resample_needed || avr->mixing_needed) &&
  95. avr->in_sample_fmt != avr->internal_sample_fmt;
  96. if (avr->resample_needed || avr->mixing_needed)
  97. avr->out_convert_needed = avr->internal_sample_fmt != avr->out_sample_fmt;
  98. else
  99. avr->out_convert_needed = avr->in_sample_fmt != avr->out_sample_fmt;
  100. /* allocate buffers */
  101. if (avr->mixing_needed || avr->in_convert_needed) {
  102. avr->in_buffer = ff_audio_data_alloc(FFMAX(avr->in_channels, avr->out_channels),
  103. 0, avr->internal_sample_fmt,
  104. "in_buffer");
  105. if (!avr->in_buffer) {
  106. ret = AVERROR(EINVAL);
  107. goto error;
  108. }
  109. }
  110. if (avr->resample_needed) {
  111. avr->resample_out_buffer = ff_audio_data_alloc(avr->out_channels,
  112. 1024, avr->internal_sample_fmt,
  113. "resample_out_buffer");
  114. if (!avr->resample_out_buffer) {
  115. ret = AVERROR(EINVAL);
  116. goto error;
  117. }
  118. }
  119. if (avr->out_convert_needed) {
  120. avr->out_buffer = ff_audio_data_alloc(avr->out_channels, 0,
  121. avr->out_sample_fmt, "out_buffer");
  122. if (!avr->out_buffer) {
  123. ret = AVERROR(EINVAL);
  124. goto error;
  125. }
  126. }
  127. avr->out_fifo = av_audio_fifo_alloc(avr->out_sample_fmt, avr->out_channels,
  128. 1024);
  129. if (!avr->out_fifo) {
  130. ret = AVERROR(ENOMEM);
  131. goto error;
  132. }
  133. /* setup contexts */
  134. if (avr->in_convert_needed) {
  135. avr->ac_in = ff_audio_convert_alloc(avr, avr->internal_sample_fmt,
  136. avr->in_sample_fmt, avr->in_channels,
  137. avr->in_sample_rate);
  138. if (!avr->ac_in) {
  139. ret = AVERROR(ENOMEM);
  140. goto error;
  141. }
  142. }
  143. if (avr->out_convert_needed) {
  144. enum AVSampleFormat src_fmt;
  145. if (avr->in_convert_needed)
  146. src_fmt = avr->internal_sample_fmt;
  147. else
  148. src_fmt = avr->in_sample_fmt;
  149. avr->ac_out = ff_audio_convert_alloc(avr, avr->out_sample_fmt, src_fmt,
  150. avr->out_channels,
  151. avr->out_sample_rate);
  152. if (!avr->ac_out) {
  153. ret = AVERROR(ENOMEM);
  154. goto error;
  155. }
  156. }
  157. if (avr->resample_needed) {
  158. avr->resample = ff_audio_resample_init(avr);
  159. if (!avr->resample) {
  160. ret = AVERROR(ENOMEM);
  161. goto error;
  162. }
  163. }
  164. if (avr->mixing_needed) {
  165. avr->am = ff_audio_mix_alloc(avr);
  166. if (!avr->am) {
  167. ret = AVERROR(ENOMEM);
  168. goto error;
  169. }
  170. }
  171. return 0;
  172. error:
  173. avresample_close(avr);
  174. return ret;
  175. }
  176. void avresample_close(AVAudioResampleContext *avr)
  177. {
  178. ff_audio_data_free(&avr->in_buffer);
  179. ff_audio_data_free(&avr->resample_out_buffer);
  180. ff_audio_data_free(&avr->out_buffer);
  181. av_audio_fifo_free(avr->out_fifo);
  182. avr->out_fifo = NULL;
  183. ff_audio_convert_free(&avr->ac_in);
  184. ff_audio_convert_free(&avr->ac_out);
  185. ff_audio_resample_free(&avr->resample);
  186. ff_audio_mix_free(&avr->am);
  187. av_freep(&avr->mix_matrix);
  188. }
  189. void avresample_free(AVAudioResampleContext **avr)
  190. {
  191. if (!*avr)
  192. return;
  193. avresample_close(*avr);
  194. av_opt_free(*avr);
  195. av_freep(avr);
  196. }
  197. static int handle_buffered_output(AVAudioResampleContext *avr,
  198. AudioData *output, AudioData *converted)
  199. {
  200. int ret;
  201. if (!output || av_audio_fifo_size(avr->out_fifo) > 0 ||
  202. (converted && output->allocated_samples < converted->nb_samples)) {
  203. if (converted) {
  204. /* if there are any samples in the output FIFO or if the
  205. user-supplied output buffer is not large enough for all samples,
  206. we add to the output FIFO */
  207. av_dlog(avr, "[FIFO] add %s to out_fifo\n", converted->name);
  208. ret = ff_audio_data_add_to_fifo(avr->out_fifo, converted, 0,
  209. converted->nb_samples);
  210. if (ret < 0)
  211. return ret;
  212. }
  213. /* if the user specified an output buffer, read samples from the output
  214. FIFO to the user output */
  215. if (output && output->allocated_samples > 0) {
  216. av_dlog(avr, "[FIFO] read from out_fifo to output\n");
  217. av_dlog(avr, "[end conversion]\n");
  218. return ff_audio_data_read_from_fifo(avr->out_fifo, output,
  219. output->allocated_samples);
  220. }
  221. } else if (converted) {
  222. /* copy directly to output if it is large enough or there is not any
  223. data in the output FIFO */
  224. av_dlog(avr, "[copy] %s to output\n", converted->name);
  225. output->nb_samples = 0;
  226. ret = ff_audio_data_copy(output, converted);
  227. if (ret < 0)
  228. return ret;
  229. av_dlog(avr, "[end conversion]\n");
  230. return output->nb_samples;
  231. }
  232. av_dlog(avr, "[end conversion]\n");
  233. return 0;
  234. }
  235. int attribute_align_arg avresample_convert(AVAudioResampleContext *avr,
  236. uint8_t **output, int out_plane_size,
  237. int out_samples, uint8_t **input,
  238. int in_plane_size, int in_samples)
  239. {
  240. AudioData input_buffer;
  241. AudioData output_buffer;
  242. AudioData *current_buffer;
  243. int ret, direct_output;
  244. /* reset internal buffers */
  245. if (avr->in_buffer) {
  246. avr->in_buffer->nb_samples = 0;
  247. ff_audio_data_set_channels(avr->in_buffer,
  248. avr->in_buffer->allocated_channels);
  249. }
  250. if (avr->resample_out_buffer) {
  251. avr->resample_out_buffer->nb_samples = 0;
  252. ff_audio_data_set_channels(avr->resample_out_buffer,
  253. avr->resample_out_buffer->allocated_channels);
  254. }
  255. if (avr->out_buffer) {
  256. avr->out_buffer->nb_samples = 0;
  257. ff_audio_data_set_channels(avr->out_buffer,
  258. avr->out_buffer->allocated_channels);
  259. }
  260. av_dlog(avr, "[start conversion]\n");
  261. /* initialize output_buffer with output data */
  262. direct_output = output && av_audio_fifo_size(avr->out_fifo) == 0;
  263. if (output) {
  264. ret = ff_audio_data_init(&output_buffer, output, out_plane_size,
  265. avr->out_channels, out_samples,
  266. avr->out_sample_fmt, 0, "output");
  267. if (ret < 0)
  268. return ret;
  269. output_buffer.nb_samples = 0;
  270. }
  271. if (input) {
  272. /* initialize input_buffer with input data */
  273. ret = ff_audio_data_init(&input_buffer, input, in_plane_size,
  274. avr->in_channels, in_samples,
  275. avr->in_sample_fmt, 1, "input");
  276. if (ret < 0)
  277. return ret;
  278. current_buffer = &input_buffer;
  279. if (avr->upmix_needed && !avr->in_convert_needed && !avr->resample_needed &&
  280. !avr->out_convert_needed && direct_output && out_samples >= in_samples) {
  281. /* in some rare cases we can copy input to output and upmix
  282. directly in the output buffer */
  283. av_dlog(avr, "[copy] %s to output\n", current_buffer->name);
  284. ret = ff_audio_data_copy(&output_buffer, current_buffer);
  285. if (ret < 0)
  286. return ret;
  287. current_buffer = &output_buffer;
  288. } else if (avr->mixing_needed || avr->in_convert_needed) {
  289. /* if needed, copy or convert input to in_buffer, and downmix if
  290. applicable */
  291. if (avr->in_convert_needed) {
  292. ret = ff_audio_data_realloc(avr->in_buffer,
  293. current_buffer->nb_samples);
  294. if (ret < 0)
  295. return ret;
  296. av_dlog(avr, "[convert] %s to in_buffer\n", current_buffer->name);
  297. ret = ff_audio_convert(avr->ac_in, avr->in_buffer,
  298. current_buffer);
  299. if (ret < 0)
  300. return ret;
  301. } else {
  302. av_dlog(avr, "[copy] %s to in_buffer\n", current_buffer->name);
  303. ret = ff_audio_data_copy(avr->in_buffer, current_buffer);
  304. if (ret < 0)
  305. return ret;
  306. }
  307. ff_audio_data_set_channels(avr->in_buffer, avr->in_channels);
  308. if (avr->downmix_needed) {
  309. av_dlog(avr, "[downmix] in_buffer\n");
  310. ret = ff_audio_mix(avr->am, avr->in_buffer);
  311. if (ret < 0)
  312. return ret;
  313. }
  314. current_buffer = avr->in_buffer;
  315. }
  316. } else {
  317. /* flush resampling buffer and/or output FIFO if input is NULL */
  318. if (!avr->resample_needed)
  319. return handle_buffered_output(avr, output ? &output_buffer : NULL,
  320. NULL);
  321. current_buffer = NULL;
  322. }
  323. if (avr->resample_needed) {
  324. AudioData *resample_out;
  325. if (!avr->out_convert_needed && direct_output && out_samples > 0)
  326. resample_out = &output_buffer;
  327. else
  328. resample_out = avr->resample_out_buffer;
  329. av_dlog(avr, "[resample] %s to %s\n",
  330. current_buffer ? current_buffer->name : "null",
  331. resample_out->name);
  332. ret = ff_audio_resample(avr->resample, resample_out,
  333. current_buffer);
  334. if (ret < 0)
  335. return ret;
  336. /* if resampling did not produce any samples, just return 0 */
  337. if (resample_out->nb_samples == 0) {
  338. av_dlog(avr, "[end conversion]\n");
  339. return 0;
  340. }
  341. current_buffer = resample_out;
  342. }
  343. if (avr->upmix_needed) {
  344. av_dlog(avr, "[upmix] %s\n", current_buffer->name);
  345. ret = ff_audio_mix(avr->am, current_buffer);
  346. if (ret < 0)
  347. return ret;
  348. }
  349. /* if we resampled or upmixed directly to output, return here */
  350. if (current_buffer == &output_buffer) {
  351. av_dlog(avr, "[end conversion]\n");
  352. return current_buffer->nb_samples;
  353. }
  354. if (avr->out_convert_needed) {
  355. if (direct_output && out_samples >= current_buffer->nb_samples) {
  356. /* convert directly to output */
  357. av_dlog(avr, "[convert] %s to output\n", current_buffer->name);
  358. ret = ff_audio_convert(avr->ac_out, &output_buffer, current_buffer);
  359. if (ret < 0)
  360. return ret;
  361. av_dlog(avr, "[end conversion]\n");
  362. return output_buffer.nb_samples;
  363. } else {
  364. ret = ff_audio_data_realloc(avr->out_buffer,
  365. current_buffer->nb_samples);
  366. if (ret < 0)
  367. return ret;
  368. av_dlog(avr, "[convert] %s to out_buffer\n", current_buffer->name);
  369. ret = ff_audio_convert(avr->ac_out, avr->out_buffer,
  370. current_buffer);
  371. if (ret < 0)
  372. return ret;
  373. current_buffer = avr->out_buffer;
  374. }
  375. }
  376. return handle_buffered_output(avr, output ? &output_buffer : NULL,
  377. current_buffer);
  378. }
  379. int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
  380. int stride)
  381. {
  382. int in_channels, out_channels, i, o;
  383. if (avr->am)
  384. return ff_audio_mix_get_matrix(avr->am, matrix, stride);
  385. in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
  386. out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
  387. if ( in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS ||
  388. out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
  389. av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
  390. return AVERROR(EINVAL);
  391. }
  392. if (!avr->mix_matrix) {
  393. av_log(avr, AV_LOG_ERROR, "matrix is not set\n");
  394. return AVERROR(EINVAL);
  395. }
  396. for (o = 0; o < out_channels; o++)
  397. for (i = 0; i < in_channels; i++)
  398. matrix[o * stride + i] = avr->mix_matrix[o * in_channels + i];
  399. return 0;
  400. }
  401. int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
  402. int stride)
  403. {
  404. int in_channels, out_channels, i, o;
  405. if (avr->am)
  406. return ff_audio_mix_set_matrix(avr->am, matrix, stride);
  407. in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
  408. out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
  409. if ( in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS ||
  410. out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
  411. av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
  412. return AVERROR(EINVAL);
  413. }
  414. if (avr->mix_matrix)
  415. av_freep(&avr->mix_matrix);
  416. avr->mix_matrix = av_malloc(in_channels * out_channels *
  417. sizeof(*avr->mix_matrix));
  418. if (!avr->mix_matrix)
  419. return AVERROR(ENOMEM);
  420. for (o = 0; o < out_channels; o++)
  421. for (i = 0; i < in_channels; i++)
  422. avr->mix_matrix[o * in_channels + i] = matrix[o * stride + i];
  423. return 0;
  424. }
  425. int avresample_available(AVAudioResampleContext *avr)
  426. {
  427. return av_audio_fifo_size(avr->out_fifo);
  428. }
  429. int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples)
  430. {
  431. if (!output)
  432. return av_audio_fifo_drain(avr->out_fifo, nb_samples);
  433. return av_audio_fifo_read(avr->out_fifo, (void**)output, nb_samples);
  434. }
  435. unsigned avresample_version(void)
  436. {
  437. return LIBAVRESAMPLE_VERSION_INT;
  438. }
  439. const char *avresample_license(void)
  440. {
  441. #define LICENSE_PREFIX "libavresample license: "
  442. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  443. }
  444. const char *avresample_configuration(void)
  445. {
  446. return FFMPEG_CONFIGURATION;
  447. }