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.

660 lines
24KB

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