utils.c 24 KB

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