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.

793 lines
28KB

  1. /*
  2. * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/frame.h"
  24. #include "libavutil/log.h"
  25. #include "libavutil/mem.h"
  26. #include "libavutil/opt.h"
  27. #include "avresample.h"
  28. #include "internal.h"
  29. #include "audio_data.h"
  30. #include "audio_convert.h"
  31. #include "audio_mix.h"
  32. #include "resample.h"
  33. int avresample_open(AVAudioResampleContext *avr)
  34. {
  35. int ret;
  36. if (avresample_is_open(avr)) {
  37. av_log(avr, AV_LOG_ERROR, "The resampling context is already open.\n");
  38. return AVERROR(EINVAL);
  39. }
  40. /* set channel mixing parameters */
  41. avr->in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
  42. if (avr->in_channels <= 0 || avr->in_channels > AVRESAMPLE_MAX_CHANNELS) {
  43. av_log(avr, AV_LOG_ERROR, "Invalid input channel layout: %"PRIu64"\n",
  44. avr->in_channel_layout);
  45. return AVERROR(EINVAL);
  46. }
  47. avr->out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
  48. if (avr->out_channels <= 0 || avr->out_channels > AVRESAMPLE_MAX_CHANNELS) {
  49. av_log(avr, AV_LOG_ERROR, "Invalid output channel layout: %"PRIu64"\n",
  50. avr->out_channel_layout);
  51. return AVERROR(EINVAL);
  52. }
  53. avr->resample_channels = FFMIN(avr->in_channels, avr->out_channels);
  54. avr->downmix_needed = avr->in_channels > avr->out_channels;
  55. avr->upmix_needed = avr->out_channels > avr->in_channels ||
  56. (!avr->downmix_needed && (avr->mix_matrix ||
  57. avr->in_channel_layout != avr->out_channel_layout));
  58. avr->mixing_needed = avr->downmix_needed || avr->upmix_needed;
  59. /* set resampling parameters */
  60. avr->resample_needed = avr->in_sample_rate != avr->out_sample_rate ||
  61. avr->force_resampling;
  62. /* select internal sample format if not specified by the user */
  63. if (avr->internal_sample_fmt == AV_SAMPLE_FMT_NONE &&
  64. (avr->mixing_needed || avr->resample_needed)) {
  65. enum AVSampleFormat in_fmt = av_get_planar_sample_fmt(avr->in_sample_fmt);
  66. enum AVSampleFormat out_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
  67. int max_bps = FFMAX(av_get_bytes_per_sample(in_fmt),
  68. av_get_bytes_per_sample(out_fmt));
  69. if (max_bps <= 2) {
  70. avr->internal_sample_fmt = AV_SAMPLE_FMT_S16P;
  71. } else if (avr->mixing_needed) {
  72. avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
  73. } else {
  74. if (max_bps <= 4) {
  75. if (in_fmt == AV_SAMPLE_FMT_S32P ||
  76. out_fmt == AV_SAMPLE_FMT_S32P) {
  77. if (in_fmt == AV_SAMPLE_FMT_FLTP ||
  78. out_fmt == AV_SAMPLE_FMT_FLTP) {
  79. /* if one is s32 and the other is flt, use dbl */
  80. avr->internal_sample_fmt = AV_SAMPLE_FMT_DBLP;
  81. } else {
  82. /* if one is s32 and the other is s32, s16, or u8, use s32 */
  83. avr->internal_sample_fmt = AV_SAMPLE_FMT_S32P;
  84. }
  85. } else {
  86. /* if one is flt and the other is flt, s16 or u8, use flt */
  87. avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
  88. }
  89. } else {
  90. /* if either is dbl, use dbl */
  91. avr->internal_sample_fmt = AV_SAMPLE_FMT_DBLP;
  92. }
  93. }
  94. av_log(avr, AV_LOG_DEBUG, "Using %s as internal sample format\n",
  95. av_get_sample_fmt_name(avr->internal_sample_fmt));
  96. }
  97. /* we may need to add an extra conversion in order to remap channels if
  98. the output format is not planar */
  99. if (avr->use_channel_map && !avr->mixing_needed && !avr->resample_needed &&
  100. !ff_sample_fmt_is_planar(avr->out_sample_fmt, avr->out_channels)) {
  101. avr->internal_sample_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
  102. }
  103. /* set sample format conversion parameters */
  104. if (avr->resample_needed || avr->mixing_needed)
  105. avr->in_convert_needed = avr->in_sample_fmt != avr->internal_sample_fmt;
  106. else
  107. avr->in_convert_needed = avr->use_channel_map &&
  108. !ff_sample_fmt_is_planar(avr->out_sample_fmt, avr->out_channels);
  109. if (avr->resample_needed || avr->mixing_needed || avr->in_convert_needed)
  110. avr->out_convert_needed = avr->internal_sample_fmt != avr->out_sample_fmt;
  111. else
  112. avr->out_convert_needed = avr->in_sample_fmt != avr->out_sample_fmt;
  113. avr->in_copy_needed = !avr->in_convert_needed && (avr->mixing_needed ||
  114. (avr->use_channel_map && avr->resample_needed));
  115. if (avr->use_channel_map) {
  116. if (avr->in_copy_needed) {
  117. avr->remap_point = REMAP_IN_COPY;
  118. av_log(avr, AV_LOG_TRACE, "remap channels during in_copy\n");
  119. } else if (avr->in_convert_needed) {
  120. avr->remap_point = REMAP_IN_CONVERT;
  121. av_log(avr, AV_LOG_TRACE, "remap channels during in_convert\n");
  122. } else if (avr->out_convert_needed) {
  123. avr->remap_point = REMAP_OUT_CONVERT;
  124. av_log(avr, AV_LOG_TRACE, "remap channels during out_convert\n");
  125. } else {
  126. avr->remap_point = REMAP_OUT_COPY;
  127. av_log(avr, AV_LOG_TRACE, "remap channels during out_copy\n");
  128. }
  129. #ifdef DEBUG
  130. {
  131. int ch;
  132. av_log(avr, AV_LOG_TRACE, "output map: ");
  133. if (avr->ch_map_info.do_remap)
  134. for (ch = 0; ch < avr->in_channels; ch++)
  135. av_log(avr, AV_LOG_TRACE, " % 2d", avr->ch_map_info.channel_map[ch]);
  136. else
  137. av_log(avr, AV_LOG_TRACE, "n/a");
  138. av_log(avr, AV_LOG_TRACE, "\n");
  139. av_log(avr, AV_LOG_TRACE, "copy map: ");
  140. if (avr->ch_map_info.do_copy)
  141. for (ch = 0; ch < avr->in_channels; ch++)
  142. av_log(avr, AV_LOG_TRACE, " % 2d", avr->ch_map_info.channel_copy[ch]);
  143. else
  144. av_log(avr, AV_LOG_TRACE, "n/a");
  145. av_log(avr, AV_LOG_TRACE, "\n");
  146. av_log(avr, AV_LOG_TRACE, "zero map: ");
  147. if (avr->ch_map_info.do_zero)
  148. for (ch = 0; ch < avr->in_channels; ch++)
  149. av_log(avr, AV_LOG_TRACE, " % 2d", avr->ch_map_info.channel_zero[ch]);
  150. else
  151. av_log(avr, AV_LOG_TRACE, "n/a");
  152. av_log(avr, AV_LOG_TRACE, "\n");
  153. av_log(avr, AV_LOG_TRACE, "input map: ");
  154. for (ch = 0; ch < avr->in_channels; ch++)
  155. av_log(avr, AV_LOG_TRACE, " % 2d", avr->ch_map_info.input_map[ch]);
  156. av_log(avr, AV_LOG_TRACE, "\n");
  157. }
  158. #endif
  159. } else
  160. avr->remap_point = REMAP_NONE;
  161. /* allocate buffers */
  162. if (avr->in_copy_needed || avr->in_convert_needed) {
  163. avr->in_buffer = ff_audio_data_alloc(FFMAX(avr->in_channels, avr->out_channels),
  164. 0, avr->internal_sample_fmt,
  165. "in_buffer");
  166. if (!avr->in_buffer) {
  167. ret = AVERROR(EINVAL);
  168. goto error;
  169. }
  170. }
  171. if (avr->resample_needed) {
  172. avr->resample_out_buffer = ff_audio_data_alloc(avr->out_channels,
  173. 1024, avr->internal_sample_fmt,
  174. "resample_out_buffer");
  175. if (!avr->resample_out_buffer) {
  176. ret = AVERROR(EINVAL);
  177. goto error;
  178. }
  179. }
  180. if (avr->out_convert_needed) {
  181. avr->out_buffer = ff_audio_data_alloc(avr->out_channels, 0,
  182. avr->out_sample_fmt, "out_buffer");
  183. if (!avr->out_buffer) {
  184. ret = AVERROR(EINVAL);
  185. goto error;
  186. }
  187. }
  188. avr->out_fifo = av_audio_fifo_alloc(avr->out_sample_fmt, avr->out_channels,
  189. 1024);
  190. if (!avr->out_fifo) {
  191. ret = AVERROR(ENOMEM);
  192. goto error;
  193. }
  194. /* setup contexts */
  195. if (avr->in_convert_needed) {
  196. avr->ac_in = ff_audio_convert_alloc(avr, avr->internal_sample_fmt,
  197. avr->in_sample_fmt, avr->in_channels,
  198. avr->in_sample_rate,
  199. avr->remap_point == REMAP_IN_CONVERT);
  200. if (!avr->ac_in) {
  201. ret = AVERROR(ENOMEM);
  202. goto error;
  203. }
  204. }
  205. if (avr->out_convert_needed) {
  206. enum AVSampleFormat src_fmt;
  207. if (avr->in_convert_needed)
  208. src_fmt = avr->internal_sample_fmt;
  209. else
  210. src_fmt = avr->in_sample_fmt;
  211. avr->ac_out = ff_audio_convert_alloc(avr, avr->out_sample_fmt, src_fmt,
  212. avr->out_channels,
  213. avr->out_sample_rate,
  214. avr->remap_point == REMAP_OUT_CONVERT);
  215. if (!avr->ac_out) {
  216. ret = AVERROR(ENOMEM);
  217. goto error;
  218. }
  219. }
  220. if (avr->resample_needed) {
  221. avr->resample = ff_audio_resample_init(avr);
  222. if (!avr->resample) {
  223. ret = AVERROR(ENOMEM);
  224. goto error;
  225. }
  226. }
  227. if (avr->mixing_needed) {
  228. avr->am = ff_audio_mix_alloc(avr);
  229. if (!avr->am) {
  230. ret = AVERROR(ENOMEM);
  231. goto error;
  232. }
  233. }
  234. return 0;
  235. error:
  236. avresample_close(avr);
  237. return ret;
  238. }
  239. int avresample_is_open(AVAudioResampleContext *avr)
  240. {
  241. return !!avr->out_fifo;
  242. }
  243. void avresample_close(AVAudioResampleContext *avr)
  244. {
  245. ff_audio_data_free(&avr->in_buffer);
  246. ff_audio_data_free(&avr->resample_out_buffer);
  247. ff_audio_data_free(&avr->out_buffer);
  248. av_audio_fifo_free(avr->out_fifo);
  249. avr->out_fifo = NULL;
  250. ff_audio_convert_free(&avr->ac_in);
  251. ff_audio_convert_free(&avr->ac_out);
  252. ff_audio_resample_free(&avr->resample);
  253. ff_audio_mix_free(&avr->am);
  254. av_freep(&avr->mix_matrix);
  255. avr->use_channel_map = 0;
  256. }
  257. void avresample_free(AVAudioResampleContext **avr)
  258. {
  259. if (!*avr)
  260. return;
  261. avresample_close(*avr);
  262. av_opt_free(*avr);
  263. av_freep(avr);
  264. }
  265. static int handle_buffered_output(AVAudioResampleContext *avr,
  266. AudioData *output, AudioData *converted)
  267. {
  268. int ret;
  269. if (!output || av_audio_fifo_size(avr->out_fifo) > 0 ||
  270. (converted && output->allocated_samples < converted->nb_samples)) {
  271. if (converted) {
  272. /* if there are any samples in the output FIFO or if the
  273. user-supplied output buffer is not large enough for all samples,
  274. we add to the output FIFO */
  275. av_log(avr, AV_LOG_TRACE, "[FIFO] add %s to out_fifo\n", converted->name);
  276. ret = ff_audio_data_add_to_fifo(avr->out_fifo, converted, 0,
  277. converted->nb_samples);
  278. if (ret < 0)
  279. return ret;
  280. }
  281. /* if the user specified an output buffer, read samples from the output
  282. FIFO to the user output */
  283. if (output && output->allocated_samples > 0) {
  284. av_log(avr, AV_LOG_TRACE, "[FIFO] read from out_fifo to output\n");
  285. av_log(avr, AV_LOG_TRACE, "[end conversion]\n");
  286. return ff_audio_data_read_from_fifo(avr->out_fifo, output,
  287. output->allocated_samples);
  288. }
  289. } else if (converted) {
  290. /* copy directly to output if it is large enough or there is not any
  291. data in the output FIFO */
  292. av_log(avr, AV_LOG_TRACE, "[copy] %s to output\n", converted->name);
  293. output->nb_samples = 0;
  294. ret = ff_audio_data_copy(output, converted,
  295. avr->remap_point == REMAP_OUT_COPY ?
  296. &avr->ch_map_info : NULL);
  297. if (ret < 0)
  298. return ret;
  299. av_log(avr, AV_LOG_TRACE, "[end conversion]\n");
  300. return output->nb_samples;
  301. }
  302. av_log(avr, AV_LOG_TRACE, "[end conversion]\n");
  303. return 0;
  304. }
  305. int attribute_align_arg avresample_convert(AVAudioResampleContext *avr,
  306. uint8_t **output, int out_plane_size,
  307. int out_samples, uint8_t **input,
  308. int in_plane_size, int in_samples)
  309. {
  310. AudioData input_buffer;
  311. AudioData output_buffer;
  312. AudioData *current_buffer;
  313. int ret, direct_output;
  314. /* reset internal buffers */
  315. if (avr->in_buffer) {
  316. avr->in_buffer->nb_samples = 0;
  317. ff_audio_data_set_channels(avr->in_buffer,
  318. avr->in_buffer->allocated_channels);
  319. }
  320. if (avr->resample_out_buffer) {
  321. avr->resample_out_buffer->nb_samples = 0;
  322. ff_audio_data_set_channels(avr->resample_out_buffer,
  323. avr->resample_out_buffer->allocated_channels);
  324. }
  325. if (avr->out_buffer) {
  326. avr->out_buffer->nb_samples = 0;
  327. ff_audio_data_set_channels(avr->out_buffer,
  328. avr->out_buffer->allocated_channels);
  329. }
  330. av_log(avr, AV_LOG_TRACE, "[start conversion]\n");
  331. /* initialize output_buffer with output data */
  332. direct_output = output && av_audio_fifo_size(avr->out_fifo) == 0;
  333. if (output) {
  334. ret = ff_audio_data_init(&output_buffer, output, out_plane_size,
  335. avr->out_channels, out_samples,
  336. avr->out_sample_fmt, 0, "output");
  337. if (ret < 0)
  338. return ret;
  339. output_buffer.nb_samples = 0;
  340. }
  341. if (input) {
  342. /* initialize input_buffer with input data */
  343. ret = ff_audio_data_init(&input_buffer, input, in_plane_size,
  344. avr->in_channels, in_samples,
  345. avr->in_sample_fmt, 1, "input");
  346. if (ret < 0)
  347. return ret;
  348. current_buffer = &input_buffer;
  349. if (avr->upmix_needed && !avr->in_convert_needed && !avr->resample_needed &&
  350. !avr->out_convert_needed && direct_output && out_samples >= in_samples) {
  351. /* in some rare cases we can copy input to output and upmix
  352. directly in the output buffer */
  353. av_log(avr, AV_LOG_TRACE, "[copy] %s to output\n", current_buffer->name);
  354. ret = ff_audio_data_copy(&output_buffer, current_buffer,
  355. avr->remap_point == REMAP_OUT_COPY ?
  356. &avr->ch_map_info : NULL);
  357. if (ret < 0)
  358. return ret;
  359. current_buffer = &output_buffer;
  360. } else if (avr->remap_point == REMAP_OUT_COPY &&
  361. (!direct_output || out_samples < in_samples)) {
  362. /* if remapping channels during output copy, we may need to
  363. * use an intermediate buffer in order to remap before adding
  364. * samples to the output fifo */
  365. av_log(avr, AV_LOG_TRACE, "[copy] %s to out_buffer\n", current_buffer->name);
  366. ret = ff_audio_data_copy(avr->out_buffer, current_buffer,
  367. &avr->ch_map_info);
  368. if (ret < 0)
  369. return ret;
  370. current_buffer = avr->out_buffer;
  371. } else if (avr->in_copy_needed || avr->in_convert_needed) {
  372. /* if needed, copy or convert input to in_buffer, and downmix if
  373. applicable */
  374. if (avr->in_convert_needed) {
  375. ret = ff_audio_data_realloc(avr->in_buffer,
  376. current_buffer->nb_samples);
  377. if (ret < 0)
  378. return ret;
  379. av_log(avr, AV_LOG_TRACE, "[convert] %s to in_buffer\n", current_buffer->name);
  380. ret = ff_audio_convert(avr->ac_in, avr->in_buffer,
  381. current_buffer);
  382. if (ret < 0)
  383. return ret;
  384. } else {
  385. av_log(avr, AV_LOG_TRACE, "[copy] %s to in_buffer\n", current_buffer->name);
  386. ret = ff_audio_data_copy(avr->in_buffer, current_buffer,
  387. avr->remap_point == REMAP_IN_COPY ?
  388. &avr->ch_map_info : NULL);
  389. if (ret < 0)
  390. return ret;
  391. }
  392. ff_audio_data_set_channels(avr->in_buffer, avr->in_channels);
  393. if (avr->downmix_needed) {
  394. av_log(avr, AV_LOG_TRACE, "[downmix] in_buffer\n");
  395. ret = ff_audio_mix(avr->am, avr->in_buffer);
  396. if (ret < 0)
  397. return ret;
  398. }
  399. current_buffer = avr->in_buffer;
  400. }
  401. } else {
  402. /* flush resampling buffer and/or output FIFO if input is NULL */
  403. if (!avr->resample_needed)
  404. return handle_buffered_output(avr, output ? &output_buffer : NULL,
  405. NULL);
  406. current_buffer = NULL;
  407. }
  408. if (avr->resample_needed) {
  409. AudioData *resample_out;
  410. if (!avr->out_convert_needed && direct_output && out_samples > 0)
  411. resample_out = &output_buffer;
  412. else
  413. resample_out = avr->resample_out_buffer;
  414. av_log(avr, AV_LOG_TRACE, "[resample] %s to %s\n",
  415. current_buffer ? current_buffer->name : "null",
  416. resample_out->name);
  417. ret = ff_audio_resample(avr->resample, resample_out,
  418. current_buffer);
  419. if (ret < 0)
  420. return ret;
  421. /* if resampling did not produce any samples, just return 0 */
  422. if (resample_out->nb_samples == 0) {
  423. av_log(avr, AV_LOG_TRACE, "[end conversion]\n");
  424. return 0;
  425. }
  426. current_buffer = resample_out;
  427. }
  428. if (avr->upmix_needed) {
  429. av_log(avr, AV_LOG_TRACE, "[upmix] %s\n", current_buffer->name);
  430. ret = ff_audio_mix(avr->am, current_buffer);
  431. if (ret < 0)
  432. return ret;
  433. }
  434. /* if we resampled or upmixed directly to output, return here */
  435. if (current_buffer == &output_buffer) {
  436. av_log(avr, AV_LOG_TRACE, "[end conversion]\n");
  437. return current_buffer->nb_samples;
  438. }
  439. if (avr->out_convert_needed) {
  440. if (direct_output && out_samples >= current_buffer->nb_samples) {
  441. /* convert directly to output */
  442. av_log(avr, AV_LOG_TRACE, "[convert] %s to output\n", current_buffer->name);
  443. ret = ff_audio_convert(avr->ac_out, &output_buffer, current_buffer);
  444. if (ret < 0)
  445. return ret;
  446. av_log(avr, AV_LOG_TRACE, "[end conversion]\n");
  447. return output_buffer.nb_samples;
  448. } else {
  449. ret = ff_audio_data_realloc(avr->out_buffer,
  450. current_buffer->nb_samples);
  451. if (ret < 0)
  452. return ret;
  453. av_log(avr, AV_LOG_TRACE, "[convert] %s to out_buffer\n", current_buffer->name);
  454. ret = ff_audio_convert(avr->ac_out, avr->out_buffer,
  455. current_buffer);
  456. if (ret < 0)
  457. return ret;
  458. current_buffer = avr->out_buffer;
  459. }
  460. }
  461. return handle_buffered_output(avr, output ? &output_buffer : NULL,
  462. current_buffer);
  463. }
  464. int avresample_config(AVAudioResampleContext *avr, AVFrame *out, AVFrame *in)
  465. {
  466. if (avresample_is_open(avr)) {
  467. avresample_close(avr);
  468. }
  469. if (in) {
  470. avr->in_channel_layout = in->channel_layout;
  471. avr->in_sample_rate = in->sample_rate;
  472. avr->in_sample_fmt = in->format;
  473. }
  474. if (out) {
  475. avr->out_channel_layout = out->channel_layout;
  476. avr->out_sample_rate = out->sample_rate;
  477. avr->out_sample_fmt = out->format;
  478. }
  479. return 0;
  480. }
  481. static int config_changed(AVAudioResampleContext *avr,
  482. AVFrame *out, AVFrame *in)
  483. {
  484. int ret = 0;
  485. if (in) {
  486. if (avr->in_channel_layout != in->channel_layout ||
  487. avr->in_sample_rate != in->sample_rate ||
  488. avr->in_sample_fmt != in->format) {
  489. ret |= AVERROR_INPUT_CHANGED;
  490. }
  491. }
  492. if (out) {
  493. if (avr->out_channel_layout != out->channel_layout ||
  494. avr->out_sample_rate != out->sample_rate ||
  495. avr->out_sample_fmt != out->format) {
  496. ret |= AVERROR_OUTPUT_CHANGED;
  497. }
  498. }
  499. return ret;
  500. }
  501. static inline int convert_frame(AVAudioResampleContext *avr,
  502. AVFrame *out, AVFrame *in)
  503. {
  504. int ret;
  505. uint8_t **out_data = NULL, **in_data = NULL;
  506. int out_linesize = 0, in_linesize = 0;
  507. int out_nb_samples = 0, in_nb_samples = 0;
  508. if (out) {
  509. out_data = out->extended_data;
  510. out_linesize = out->linesize[0];
  511. out_nb_samples = out->nb_samples;
  512. }
  513. if (in) {
  514. in_data = in->extended_data;
  515. in_linesize = in->linesize[0];
  516. in_nb_samples = in->nb_samples;
  517. }
  518. ret = avresample_convert(avr, out_data, out_linesize,
  519. out_nb_samples,
  520. in_data, in_linesize,
  521. in_nb_samples);
  522. if (ret < 0) {
  523. if (out)
  524. out->nb_samples = 0;
  525. return ret;
  526. }
  527. if (out)
  528. out->nb_samples = ret;
  529. return 0;
  530. }
  531. static inline int available_samples(AVFrame *out)
  532. {
  533. int samples;
  534. int bytes_per_sample = av_get_bytes_per_sample(out->format);
  535. if (!bytes_per_sample)
  536. return AVERROR(EINVAL);
  537. samples = out->linesize[0] / bytes_per_sample;
  538. if (av_sample_fmt_is_planar(out->format)) {
  539. return samples;
  540. } else {
  541. int channels = av_get_channel_layout_nb_channels(out->channel_layout);
  542. return samples / channels;
  543. }
  544. }
  545. int avresample_convert_frame(AVAudioResampleContext *avr,
  546. AVFrame *out, AVFrame *in)
  547. {
  548. int ret, setup = 0;
  549. if (!avresample_is_open(avr)) {
  550. if ((ret = avresample_config(avr, out, in)) < 0)
  551. return ret;
  552. if ((ret = avresample_open(avr)) < 0)
  553. return ret;
  554. setup = 1;
  555. } else {
  556. // return as is or reconfigure for input changes?
  557. if ((ret = config_changed(avr, out, in)))
  558. return ret;
  559. }
  560. if (out) {
  561. if (!out->linesize[0]) {
  562. out->nb_samples = avresample_get_out_samples(avr, in->nb_samples);
  563. if ((ret = av_frame_get_buffer(out, 0)) < 0) {
  564. if (setup)
  565. avresample_close(avr);
  566. return ret;
  567. }
  568. } else {
  569. if (!out->nb_samples)
  570. out->nb_samples = available_samples(out);
  571. }
  572. }
  573. return convert_frame(avr, out, in);
  574. }
  575. int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
  576. int stride)
  577. {
  578. int in_channels, out_channels, i, o;
  579. if (avr->am)
  580. return ff_audio_mix_get_matrix(avr->am, matrix, stride);
  581. in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
  582. out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
  583. if ( in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS ||
  584. out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
  585. av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
  586. return AVERROR(EINVAL);
  587. }
  588. if (!avr->mix_matrix) {
  589. av_log(avr, AV_LOG_ERROR, "matrix is not set\n");
  590. return AVERROR(EINVAL);
  591. }
  592. for (o = 0; o < out_channels; o++)
  593. for (i = 0; i < in_channels; i++)
  594. matrix[o * stride + i] = avr->mix_matrix[o * in_channels + i];
  595. return 0;
  596. }
  597. int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
  598. int stride)
  599. {
  600. int in_channels, out_channels, i, o;
  601. if (avr->am)
  602. return ff_audio_mix_set_matrix(avr->am, matrix, stride);
  603. in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
  604. out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
  605. if ( in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS ||
  606. out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
  607. av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
  608. return AVERROR(EINVAL);
  609. }
  610. if (avr->mix_matrix)
  611. av_freep(&avr->mix_matrix);
  612. avr->mix_matrix = av_malloc(in_channels * out_channels *
  613. sizeof(*avr->mix_matrix));
  614. if (!avr->mix_matrix)
  615. return AVERROR(ENOMEM);
  616. for (o = 0; o < out_channels; o++)
  617. for (i = 0; i < in_channels; i++)
  618. avr->mix_matrix[o * in_channels + i] = matrix[o * stride + i];
  619. return 0;
  620. }
  621. int avresample_set_channel_mapping(AVAudioResampleContext *avr,
  622. const int *channel_map)
  623. {
  624. ChannelMapInfo *info = &avr->ch_map_info;
  625. int in_channels, ch, i;
  626. in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
  627. if (in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS) {
  628. av_log(avr, AV_LOG_ERROR, "Invalid input channel layout\n");
  629. return AVERROR(EINVAL);
  630. }
  631. memset(info, 0, sizeof(*info));
  632. memset(info->input_map, -1, sizeof(info->input_map));
  633. for (ch = 0; ch < in_channels; ch++) {
  634. if (channel_map[ch] >= in_channels) {
  635. av_log(avr, AV_LOG_ERROR, "Invalid channel map\n");
  636. return AVERROR(EINVAL);
  637. }
  638. if (channel_map[ch] < 0) {
  639. info->channel_zero[ch] = 1;
  640. info->channel_map[ch] = -1;
  641. info->do_zero = 1;
  642. } else if (info->input_map[channel_map[ch]] >= 0) {
  643. info->channel_copy[ch] = info->input_map[channel_map[ch]];
  644. info->channel_map[ch] = -1;
  645. info->do_copy = 1;
  646. } else {
  647. info->channel_map[ch] = channel_map[ch];
  648. info->input_map[channel_map[ch]] = ch;
  649. info->do_remap = 1;
  650. }
  651. }
  652. /* Fill-in unmapped input channels with unmapped output channels.
  653. This is used when remapping during conversion from interleaved to
  654. planar format. */
  655. for (ch = 0, i = 0; ch < in_channels && i < in_channels; ch++, i++) {
  656. while (ch < in_channels && info->input_map[ch] >= 0)
  657. ch++;
  658. while (i < in_channels && info->channel_map[i] >= 0)
  659. i++;
  660. if (ch >= in_channels || i >= in_channels)
  661. break;
  662. info->input_map[ch] = i;
  663. }
  664. avr->use_channel_map = 1;
  665. return 0;
  666. }
  667. int avresample_available(AVAudioResampleContext *avr)
  668. {
  669. return av_audio_fifo_size(avr->out_fifo);
  670. }
  671. int avresample_get_out_samples(AVAudioResampleContext *avr, int in_nb_samples)
  672. {
  673. int64_t samples = avresample_get_delay(avr) + (int64_t)in_nb_samples;
  674. if (avr->resample_needed) {
  675. samples = av_rescale_rnd(samples,
  676. avr->out_sample_rate,
  677. avr->in_sample_rate,
  678. AV_ROUND_UP);
  679. }
  680. samples += avresample_available(avr);
  681. if (samples > INT_MAX)
  682. return AVERROR(EINVAL);
  683. return samples;
  684. }
  685. int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples)
  686. {
  687. if (!output)
  688. return av_audio_fifo_drain(avr->out_fifo, nb_samples);
  689. return av_audio_fifo_read(avr->out_fifo, (void**)output, nb_samples);
  690. }
  691. unsigned avresample_version(void)
  692. {
  693. return LIBAVRESAMPLE_VERSION_INT;
  694. }
  695. const char *avresample_license(void)
  696. {
  697. #define LICENSE_PREFIX "libavresample license: "
  698. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  699. }
  700. const char *avresample_configuration(void)
  701. {
  702. return FFMPEG_CONFIGURATION;
  703. }