utils.c 24 KB

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