utils.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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_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. 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_dlog(avr, "[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_dlog(avr, "[FIFO] read from out_fifo to output\n");
  285. av_dlog(avr, "[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_dlog(avr, "[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_dlog(avr, "[end conversion]\n");
  300. return output->nb_samples;
  301. }
  302. av_dlog(avr, "[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_dlog(avr, "[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_dlog(avr, "[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_dlog(avr, "[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_dlog(avr, "[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_dlog(avr, "[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_dlog(avr, "[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_dlog(avr, "[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_dlog(avr, "[end conversion]\n");
  424. return 0;
  425. }
  426. current_buffer = resample_out;
  427. }
  428. if (avr->upmix_needed) {
  429. av_dlog(avr, "[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_dlog(avr, "[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_dlog(avr, "[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_dlog(avr, "[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_dlog(avr, "[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 bytes_per_sample = av_get_bytes_per_sample(out->format);
  534. int samples = out->linesize[0] / bytes_per_sample;
  535. if (av_sample_fmt_is_planar(out->format)) {
  536. return samples;
  537. } else {
  538. int channels = av_get_channel_layout_nb_channels(out->channel_layout);
  539. return samples / channels;
  540. }
  541. }
  542. int avresample_convert_frame(AVAudioResampleContext *avr,
  543. AVFrame *out, AVFrame *in)
  544. {
  545. int ret, setup = 0;
  546. if (!avresample_is_open(avr)) {
  547. if ((ret = avresample_config(avr, out, in)) < 0)
  548. return ret;
  549. if ((ret = avresample_open(avr)) < 0)
  550. return ret;
  551. setup = 1;
  552. } else {
  553. // return as is or reconfigure for input changes?
  554. if ((ret = config_changed(avr, out, in)))
  555. return ret;
  556. }
  557. if (out) {
  558. if (!out->linesize[0]) {
  559. out->nb_samples = avresample_get_out_samples(avr, in->nb_samples);
  560. if ((ret = av_frame_get_buffer(out, 0)) < 0) {
  561. if (setup)
  562. avresample_close(avr);
  563. return ret;
  564. }
  565. } else {
  566. if (!out->nb_samples)
  567. out->nb_samples = available_samples(out);
  568. }
  569. }
  570. return convert_frame(avr, out, in);
  571. }
  572. int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
  573. int stride)
  574. {
  575. int in_channels, out_channels, i, o;
  576. if (avr->am)
  577. return ff_audio_mix_get_matrix(avr->am, matrix, stride);
  578. in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
  579. out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
  580. if ( in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS ||
  581. out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
  582. av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
  583. return AVERROR(EINVAL);
  584. }
  585. if (!avr->mix_matrix) {
  586. av_log(avr, AV_LOG_ERROR, "matrix is not set\n");
  587. return AVERROR(EINVAL);
  588. }
  589. for (o = 0; o < out_channels; o++)
  590. for (i = 0; i < in_channels; i++)
  591. matrix[o * stride + i] = avr->mix_matrix[o * in_channels + i];
  592. return 0;
  593. }
  594. int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
  595. int stride)
  596. {
  597. int in_channels, out_channels, i, o;
  598. if (avr->am)
  599. return ff_audio_mix_set_matrix(avr->am, matrix, stride);
  600. in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
  601. out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
  602. if ( in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS ||
  603. out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
  604. av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
  605. return AVERROR(EINVAL);
  606. }
  607. if (avr->mix_matrix)
  608. av_freep(&avr->mix_matrix);
  609. avr->mix_matrix = av_malloc(in_channels * out_channels *
  610. sizeof(*avr->mix_matrix));
  611. if (!avr->mix_matrix)
  612. return AVERROR(ENOMEM);
  613. for (o = 0; o < out_channels; o++)
  614. for (i = 0; i < in_channels; i++)
  615. avr->mix_matrix[o * in_channels + i] = matrix[o * stride + i];
  616. return 0;
  617. }
  618. int avresample_set_channel_mapping(AVAudioResampleContext *avr,
  619. const int *channel_map)
  620. {
  621. ChannelMapInfo *info = &avr->ch_map_info;
  622. int in_channels, ch, i;
  623. in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
  624. if (in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS) {
  625. av_log(avr, AV_LOG_ERROR, "Invalid input channel layout\n");
  626. return AVERROR(EINVAL);
  627. }
  628. memset(info, 0, sizeof(*info));
  629. memset(info->input_map, -1, sizeof(info->input_map));
  630. for (ch = 0; ch < in_channels; ch++) {
  631. if (channel_map[ch] >= in_channels) {
  632. av_log(avr, AV_LOG_ERROR, "Invalid channel map\n");
  633. return AVERROR(EINVAL);
  634. }
  635. if (channel_map[ch] < 0) {
  636. info->channel_zero[ch] = 1;
  637. info->channel_map[ch] = -1;
  638. info->do_zero = 1;
  639. } else if (info->input_map[channel_map[ch]] >= 0) {
  640. info->channel_copy[ch] = info->input_map[channel_map[ch]];
  641. info->channel_map[ch] = -1;
  642. info->do_copy = 1;
  643. } else {
  644. info->channel_map[ch] = channel_map[ch];
  645. info->input_map[channel_map[ch]] = ch;
  646. info->do_remap = 1;
  647. }
  648. }
  649. /* Fill-in unmapped input channels with unmapped output channels.
  650. This is used when remapping during conversion from interleaved to
  651. planar format. */
  652. for (ch = 0, i = 0; ch < in_channels && i < in_channels; ch++, i++) {
  653. while (ch < in_channels && info->input_map[ch] >= 0)
  654. ch++;
  655. while (i < in_channels && info->channel_map[i] >= 0)
  656. i++;
  657. if (ch >= in_channels || i >= in_channels)
  658. break;
  659. info->input_map[ch] = i;
  660. }
  661. avr->use_channel_map = 1;
  662. return 0;
  663. }
  664. int avresample_available(AVAudioResampleContext *avr)
  665. {
  666. return av_audio_fifo_size(avr->out_fifo);
  667. }
  668. int avresample_get_out_samples(AVAudioResampleContext *avr, int in_nb_samples)
  669. {
  670. int64_t samples = avresample_get_delay(avr) + (int64_t)in_nb_samples;
  671. if (avr->resample_needed) {
  672. samples = av_rescale_rnd(samples,
  673. avr->out_sample_rate,
  674. avr->in_sample_rate,
  675. AV_ROUND_UP);
  676. }
  677. samples += avresample_available(avr);
  678. if (samples > INT_MAX)
  679. return AVERROR(EINVAL);
  680. return samples;
  681. }
  682. int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples)
  683. {
  684. if (!output)
  685. return av_audio_fifo_drain(avr->out_fifo, nb_samples);
  686. return av_audio_fifo_read(avr->out_fifo, (void**)output, nb_samples);
  687. }
  688. unsigned avresample_version(void)
  689. {
  690. return LIBAVRESAMPLE_VERSION_INT;
  691. }
  692. const char *avresample_license(void)
  693. {
  694. #define LICENSE_PREFIX "libavresample license: "
  695. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  696. }
  697. const char *avresample_configuration(void)
  698. {
  699. return FFMPEG_CONFIGURATION;
  700. }