You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

500 lines
18KB

  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. /* set sample format conversion parameters */
  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. avr->in_convert_needed = (avr->resample_needed || avr->mixing_needed) &&
  98. avr->in_sample_fmt != avr->internal_sample_fmt;
  99. if (avr->resample_needed || avr->mixing_needed)
  100. avr->out_convert_needed = avr->internal_sample_fmt != avr->out_sample_fmt;
  101. else
  102. avr->out_convert_needed = avr->in_sample_fmt != avr->out_sample_fmt;
  103. /* allocate buffers */
  104. if (avr->mixing_needed || avr->in_convert_needed) {
  105. avr->in_buffer = ff_audio_data_alloc(FFMAX(avr->in_channels, avr->out_channels),
  106. 0, avr->internal_sample_fmt,
  107. "in_buffer");
  108. if (!avr->in_buffer) {
  109. ret = AVERROR(EINVAL);
  110. goto error;
  111. }
  112. }
  113. if (avr->resample_needed) {
  114. avr->resample_out_buffer = ff_audio_data_alloc(avr->out_channels,
  115. 0, avr->internal_sample_fmt,
  116. "resample_out_buffer");
  117. if (!avr->resample_out_buffer) {
  118. ret = AVERROR(EINVAL);
  119. goto error;
  120. }
  121. }
  122. if (avr->out_convert_needed) {
  123. avr->out_buffer = ff_audio_data_alloc(avr->out_channels, 0,
  124. avr->out_sample_fmt, "out_buffer");
  125. if (!avr->out_buffer) {
  126. ret = AVERROR(EINVAL);
  127. goto error;
  128. }
  129. }
  130. avr->out_fifo = av_audio_fifo_alloc(avr->out_sample_fmt, avr->out_channels,
  131. 1024);
  132. if (!avr->out_fifo) {
  133. ret = AVERROR(ENOMEM);
  134. goto error;
  135. }
  136. /* setup contexts */
  137. if (avr->in_convert_needed) {
  138. avr->ac_in = ff_audio_convert_alloc(avr, avr->internal_sample_fmt,
  139. avr->in_sample_fmt, avr->in_channels,
  140. avr->in_sample_rate);
  141. if (!avr->ac_in) {
  142. ret = AVERROR(ENOMEM);
  143. goto error;
  144. }
  145. }
  146. if (avr->out_convert_needed) {
  147. enum AVSampleFormat src_fmt;
  148. if (avr->in_convert_needed)
  149. src_fmt = avr->internal_sample_fmt;
  150. else
  151. src_fmt = avr->in_sample_fmt;
  152. avr->ac_out = ff_audio_convert_alloc(avr, avr->out_sample_fmt, src_fmt,
  153. avr->out_channels,
  154. avr->out_sample_rate);
  155. if (!avr->ac_out) {
  156. ret = AVERROR(ENOMEM);
  157. goto error;
  158. }
  159. }
  160. if (avr->resample_needed) {
  161. avr->resample = ff_audio_resample_init(avr);
  162. if (!avr->resample) {
  163. ret = AVERROR(ENOMEM);
  164. goto error;
  165. }
  166. }
  167. if (avr->mixing_needed) {
  168. avr->am = ff_audio_mix_alloc(avr);
  169. if (!avr->am) {
  170. ret = AVERROR(ENOMEM);
  171. goto error;
  172. }
  173. }
  174. return 0;
  175. error:
  176. avresample_close(avr);
  177. return ret;
  178. }
  179. void avresample_close(AVAudioResampleContext *avr)
  180. {
  181. ff_audio_data_free(&avr->in_buffer);
  182. ff_audio_data_free(&avr->resample_out_buffer);
  183. ff_audio_data_free(&avr->out_buffer);
  184. av_audio_fifo_free(avr->out_fifo);
  185. avr->out_fifo = NULL;
  186. ff_audio_convert_free(&avr->ac_in);
  187. ff_audio_convert_free(&avr->ac_out);
  188. ff_audio_resample_free(&avr->resample);
  189. ff_audio_mix_free(&avr->am);
  190. av_freep(&avr->mix_matrix);
  191. }
  192. void avresample_free(AVAudioResampleContext **avr)
  193. {
  194. if (!*avr)
  195. return;
  196. avresample_close(*avr);
  197. av_opt_free(*avr);
  198. av_freep(avr);
  199. }
  200. static int handle_buffered_output(AVAudioResampleContext *avr,
  201. AudioData *output, AudioData *converted)
  202. {
  203. int ret;
  204. if (!output || av_audio_fifo_size(avr->out_fifo) > 0 ||
  205. (converted && output->allocated_samples < converted->nb_samples)) {
  206. if (converted) {
  207. /* if there are any samples in the output FIFO or if the
  208. user-supplied output buffer is not large enough for all samples,
  209. we add to the output FIFO */
  210. av_dlog(avr, "[FIFO] add %s to out_fifo\n", converted->name);
  211. ret = ff_audio_data_add_to_fifo(avr->out_fifo, converted, 0,
  212. converted->nb_samples);
  213. if (ret < 0)
  214. return ret;
  215. }
  216. /* if the user specified an output buffer, read samples from the output
  217. FIFO to the user output */
  218. if (output && output->allocated_samples > 0) {
  219. av_dlog(avr, "[FIFO] read from out_fifo to output\n");
  220. av_dlog(avr, "[end conversion]\n");
  221. return ff_audio_data_read_from_fifo(avr->out_fifo, output,
  222. output->allocated_samples);
  223. }
  224. } else if (converted) {
  225. /* copy directly to output if it is large enough or there is not any
  226. data in the output FIFO */
  227. av_dlog(avr, "[copy] %s to output\n", converted->name);
  228. output->nb_samples = 0;
  229. ret = ff_audio_data_copy(output, converted);
  230. if (ret < 0)
  231. return ret;
  232. av_dlog(avr, "[end conversion]\n");
  233. return output->nb_samples;
  234. }
  235. av_dlog(avr, "[end conversion]\n");
  236. return 0;
  237. }
  238. int attribute_align_arg avresample_convert(AVAudioResampleContext *avr,
  239. uint8_t **output, int out_plane_size,
  240. int out_samples, uint8_t **input,
  241. int in_plane_size, int in_samples)
  242. {
  243. AudioData input_buffer;
  244. AudioData output_buffer;
  245. AudioData *current_buffer;
  246. int ret, direct_output;
  247. /* reset internal buffers */
  248. if (avr->in_buffer) {
  249. avr->in_buffer->nb_samples = 0;
  250. ff_audio_data_set_channels(avr->in_buffer,
  251. avr->in_buffer->allocated_channels);
  252. }
  253. if (avr->resample_out_buffer) {
  254. avr->resample_out_buffer->nb_samples = 0;
  255. ff_audio_data_set_channels(avr->resample_out_buffer,
  256. avr->resample_out_buffer->allocated_channels);
  257. }
  258. if (avr->out_buffer) {
  259. avr->out_buffer->nb_samples = 0;
  260. ff_audio_data_set_channels(avr->out_buffer,
  261. avr->out_buffer->allocated_channels);
  262. }
  263. av_dlog(avr, "[start conversion]\n");
  264. /* initialize output_buffer with output data */
  265. direct_output = output && av_audio_fifo_size(avr->out_fifo) == 0;
  266. if (output) {
  267. ret = ff_audio_data_init(&output_buffer, output, out_plane_size,
  268. avr->out_channels, out_samples,
  269. avr->out_sample_fmt, 0, "output");
  270. if (ret < 0)
  271. return ret;
  272. output_buffer.nb_samples = 0;
  273. }
  274. if (input) {
  275. /* initialize input_buffer with input data */
  276. ret = ff_audio_data_init(&input_buffer, input, in_plane_size,
  277. avr->in_channels, in_samples,
  278. avr->in_sample_fmt, 1, "input");
  279. if (ret < 0)
  280. return ret;
  281. current_buffer = &input_buffer;
  282. if (avr->upmix_needed && !avr->in_convert_needed && !avr->resample_needed &&
  283. !avr->out_convert_needed && direct_output && out_samples >= in_samples) {
  284. /* in some rare cases we can copy input to output and upmix
  285. directly in the output buffer */
  286. av_dlog(avr, "[copy] %s to output\n", current_buffer->name);
  287. ret = ff_audio_data_copy(&output_buffer, current_buffer);
  288. if (ret < 0)
  289. return ret;
  290. current_buffer = &output_buffer;
  291. } else if (avr->mixing_needed || avr->in_convert_needed) {
  292. /* if needed, copy or convert input to in_buffer, and downmix if
  293. applicable */
  294. if (avr->in_convert_needed) {
  295. ret = ff_audio_data_realloc(avr->in_buffer,
  296. current_buffer->nb_samples);
  297. if (ret < 0)
  298. return ret;
  299. av_dlog(avr, "[convert] %s to in_buffer\n", current_buffer->name);
  300. ret = ff_audio_convert(avr->ac_in, avr->in_buffer,
  301. current_buffer);
  302. if (ret < 0)
  303. return ret;
  304. } else {
  305. av_dlog(avr, "[copy] %s to in_buffer\n", current_buffer->name);
  306. ret = ff_audio_data_copy(avr->in_buffer, current_buffer);
  307. if (ret < 0)
  308. return ret;
  309. }
  310. ff_audio_data_set_channels(avr->in_buffer, avr->in_channels);
  311. if (avr->downmix_needed) {
  312. av_dlog(avr, "[downmix] in_buffer\n");
  313. ret = ff_audio_mix(avr->am, avr->in_buffer);
  314. if (ret < 0)
  315. return ret;
  316. }
  317. current_buffer = avr->in_buffer;
  318. }
  319. } else {
  320. /* flush resampling buffer and/or output FIFO if input is NULL */
  321. if (!avr->resample_needed)
  322. return handle_buffered_output(avr, output ? &output_buffer : NULL,
  323. NULL);
  324. current_buffer = NULL;
  325. }
  326. if (avr->resample_needed) {
  327. AudioData *resample_out;
  328. if (!avr->out_convert_needed && direct_output && out_samples > 0)
  329. resample_out = &output_buffer;
  330. else
  331. resample_out = avr->resample_out_buffer;
  332. av_dlog(avr, "[resample] %s to %s\n", current_buffer->name,
  333. resample_out->name);
  334. ret = ff_audio_resample(avr->resample, resample_out,
  335. current_buffer);
  336. if (ret < 0)
  337. return ret;
  338. /* if resampling did not produce any samples, just return 0 */
  339. if (resample_out->nb_samples == 0) {
  340. av_dlog(avr, "[end conversion]\n");
  341. return 0;
  342. }
  343. current_buffer = resample_out;
  344. }
  345. if (avr->upmix_needed) {
  346. av_dlog(avr, "[upmix] %s\n", current_buffer->name);
  347. ret = ff_audio_mix(avr->am, current_buffer);
  348. if (ret < 0)
  349. return ret;
  350. }
  351. /* if we resampled or upmixed directly to output, return here */
  352. if (current_buffer == &output_buffer) {
  353. av_dlog(avr, "[end conversion]\n");
  354. return current_buffer->nb_samples;
  355. }
  356. if (avr->out_convert_needed) {
  357. if (direct_output && out_samples >= current_buffer->nb_samples) {
  358. /* convert directly to output */
  359. av_dlog(avr, "[convert] %s to output\n", current_buffer->name);
  360. ret = ff_audio_convert(avr->ac_out, &output_buffer, current_buffer);
  361. if (ret < 0)
  362. return ret;
  363. av_dlog(avr, "[end conversion]\n");
  364. return output_buffer.nb_samples;
  365. } else {
  366. ret = ff_audio_data_realloc(avr->out_buffer,
  367. current_buffer->nb_samples);
  368. if (ret < 0)
  369. return ret;
  370. av_dlog(avr, "[convert] %s to out_buffer\n", current_buffer->name);
  371. ret = ff_audio_convert(avr->ac_out, avr->out_buffer,
  372. current_buffer);
  373. if (ret < 0)
  374. return ret;
  375. current_buffer = avr->out_buffer;
  376. }
  377. }
  378. return handle_buffered_output(avr, output ? &output_buffer : NULL,
  379. current_buffer);
  380. }
  381. int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
  382. int stride)
  383. {
  384. int in_channels, out_channels, i, o;
  385. if (avr->am)
  386. return ff_audio_mix_get_matrix(avr->am, matrix, stride);
  387. in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
  388. out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
  389. if ( in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS ||
  390. out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
  391. av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
  392. return AVERROR(EINVAL);
  393. }
  394. if (!avr->mix_matrix) {
  395. av_log(avr, AV_LOG_ERROR, "matrix is not set\n");
  396. return AVERROR(EINVAL);
  397. }
  398. for (o = 0; o < out_channels; o++)
  399. for (i = 0; i < in_channels; i++)
  400. matrix[o * stride + i] = avr->mix_matrix[o * in_channels + i];
  401. return 0;
  402. }
  403. int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
  404. int stride)
  405. {
  406. int in_channels, out_channels, i, o;
  407. if (avr->am)
  408. return ff_audio_mix_set_matrix(avr->am, matrix, stride);
  409. in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
  410. out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
  411. if ( in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS ||
  412. out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
  413. av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
  414. return AVERROR(EINVAL);
  415. }
  416. if (avr->mix_matrix)
  417. av_freep(&avr->mix_matrix);
  418. avr->mix_matrix = av_malloc(in_channels * out_channels *
  419. sizeof(*avr->mix_matrix));
  420. if (!avr->mix_matrix)
  421. return AVERROR(ENOMEM);
  422. for (o = 0; o < out_channels; o++)
  423. for (i = 0; i < in_channels; i++)
  424. avr->mix_matrix[o * in_channels + i] = matrix[o * stride + i];
  425. return 0;
  426. }
  427. int avresample_available(AVAudioResampleContext *avr)
  428. {
  429. return av_audio_fifo_size(avr->out_fifo);
  430. }
  431. int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples)
  432. {
  433. if (!output)
  434. return av_audio_fifo_drain(avr->out_fifo, nb_samples);
  435. return av_audio_fifo_read(avr->out_fifo, (void**)output, nb_samples);
  436. }
  437. unsigned avresample_version(void)
  438. {
  439. return LIBAVRESAMPLE_VERSION_INT;
  440. }
  441. const char *avresample_license(void)
  442. {
  443. #define LICENSE_PREFIX "libavresample license: "
  444. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  445. }
  446. const char *avresample_configuration(void)
  447. {
  448. return LIBAV_CONFIGURATION;
  449. }