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.

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