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.

434 lines
16KB

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