utils.c 15 KB

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