utils.c 28 KB

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