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.

414 lines
15KB

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