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.

735 lines
24KB

  1. /*
  2. * Opus decoder
  3. * Copyright (c) 2012 Andrew D'Addesio
  4. * Copyright (c) 2013-2014 Mozilla Corporation
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Opus decoder
  25. * @author Andrew D'Addesio, Anton Khirnov
  26. *
  27. * Codec homepage: http://opus-codec.org/
  28. * Specification: http://tools.ietf.org/html/rfc6716
  29. * Ogg Opus specification: https://tools.ietf.org/html/draft-ietf-codec-oggopus-03
  30. *
  31. * Ogg-contained .opus files can be produced with opus-tools:
  32. * http://git.xiph.org/?p=opus-tools.git
  33. */
  34. #include <stdint.h>
  35. #include "libavutil/attributes.h"
  36. #include "libavutil/audio_fifo.h"
  37. #include "libavutil/channel_layout.h"
  38. #include "libavutil/opt.h"
  39. #include "libswresample/swresample.h"
  40. #include "avcodec.h"
  41. #include "get_bits.h"
  42. #include "internal.h"
  43. #include "mathops.h"
  44. #include "opus.h"
  45. #include "opustab.h"
  46. static const uint16_t silk_frame_duration_ms[16] = {
  47. 10, 20, 40, 60,
  48. 10, 20, 40, 60,
  49. 10, 20, 40, 60,
  50. 10, 20,
  51. 10, 20,
  52. };
  53. /* number of samples of silence to feed to the resampler
  54. * at the beginning */
  55. static const int silk_resample_delay[] = {
  56. 4, 8, 11, 11, 11
  57. };
  58. static const uint8_t celt_band_end[] = { 13, 17, 17, 19, 21 };
  59. static int get_silk_samplerate(int config)
  60. {
  61. if (config < 4)
  62. return 8000;
  63. else if (config < 8)
  64. return 12000;
  65. return 16000;
  66. }
  67. static void opus_fade(float *out,
  68. const float *in1, const float *in2,
  69. const float *window, int len)
  70. {
  71. int i;
  72. for (i = 0; i < len; i++)
  73. out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]);
  74. }
  75. static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
  76. {
  77. int celt_size = av_audio_fifo_size(s->celt_delay);
  78. int ret, i;
  79. ret = swr_convert(s->swr,
  80. (uint8_t**)s->out, nb_samples,
  81. NULL, 0);
  82. if (ret < 0)
  83. return ret;
  84. else if (ret != nb_samples) {
  85. av_log(s->avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n",
  86. ret);
  87. return AVERROR_BUG;
  88. }
  89. if (celt_size) {
  90. if (celt_size != nb_samples) {
  91. av_log(s->avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n");
  92. return AVERROR_BUG;
  93. }
  94. av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, nb_samples);
  95. for (i = 0; i < s->output_channels; i++) {
  96. s->fdsp->vector_fmac_scalar(s->out[i],
  97. s->celt_output[i], 1.0,
  98. nb_samples);
  99. }
  100. }
  101. if (s->redundancy_idx) {
  102. for (i = 0; i < s->output_channels; i++)
  103. opus_fade(s->out[i], s->out[i],
  104. s->redundancy_output[i] + 120 + s->redundancy_idx,
  105. ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
  106. s->redundancy_idx = 0;
  107. }
  108. s->out[0] += nb_samples;
  109. s->out[1] += nb_samples;
  110. s->out_size -= nb_samples * sizeof(float);
  111. return 0;
  112. }
  113. static int opus_init_resample(OpusStreamContext *s)
  114. {
  115. static const float delay[16] = { 0.0 };
  116. const uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
  117. int ret;
  118. av_opt_set_int(s->swr, "in_sample_rate", s->silk_samplerate, 0);
  119. ret = swr_init(s->swr);
  120. if (ret < 0) {
  121. av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n");
  122. return ret;
  123. }
  124. ret = swr_convert(s->swr,
  125. NULL, 0,
  126. delayptr, silk_resample_delay[s->packet.bandwidth]);
  127. if (ret < 0) {
  128. av_log(s->avctx, AV_LOG_ERROR,
  129. "Error feeding initial silence to the resampler.\n");
  130. return ret;
  131. }
  132. return 0;
  133. }
  134. static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
  135. {
  136. int ret;
  137. enum OpusBandwidth bw = s->packet.bandwidth;
  138. if (s->packet.mode == OPUS_MODE_SILK &&
  139. bw == OPUS_BANDWIDTH_MEDIUMBAND)
  140. bw = OPUS_BANDWIDTH_WIDEBAND;
  141. ret = ff_opus_rc_dec_init(&s->redundancy_rc, data, size);
  142. if (ret < 0)
  143. goto fail;
  144. ff_opus_rc_dec_raw_init(&s->redundancy_rc, data + size, size);
  145. ret = ff_celt_decode_frame(s->celt, &s->redundancy_rc,
  146. s->redundancy_output,
  147. s->packet.stereo + 1, 240,
  148. 0, celt_band_end[s->packet.bandwidth]);
  149. if (ret < 0)
  150. goto fail;
  151. return 0;
  152. fail:
  153. av_log(s->avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n");
  154. return ret;
  155. }
  156. static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
  157. {
  158. int samples = s->packet.frame_duration;
  159. int redundancy = 0;
  160. int redundancy_size, redundancy_pos;
  161. int ret, i, consumed;
  162. int delayed_samples = s->delayed_samples;
  163. ret = ff_opus_rc_dec_init(&s->rc, data, size);
  164. if (ret < 0)
  165. return ret;
  166. /* decode the silk frame */
  167. if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) {
  168. if (!swr_is_initialized(s->swr)) {
  169. ret = opus_init_resample(s);
  170. if (ret < 0)
  171. return ret;
  172. }
  173. samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output,
  174. FFMIN(s->packet.bandwidth, OPUS_BANDWIDTH_WIDEBAND),
  175. s->packet.stereo + 1,
  176. silk_frame_duration_ms[s->packet.config]);
  177. if (samples < 0) {
  178. av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n");
  179. return samples;
  180. }
  181. samples = swr_convert(s->swr,
  182. (uint8_t**)s->out, s->packet.frame_duration,
  183. (const uint8_t**)s->silk_output, samples);
  184. if (samples < 0) {
  185. av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n");
  186. return samples;
  187. }
  188. av_assert2((samples & 7) == 0);
  189. s->delayed_samples += s->packet.frame_duration - samples;
  190. } else
  191. ff_silk_flush(s->silk);
  192. // decode redundancy information
  193. consumed = opus_rc_tell(&s->rc);
  194. if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8)
  195. redundancy = ff_opus_rc_dec_log(&s->rc, 12);
  196. else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8)
  197. redundancy = 1;
  198. if (redundancy) {
  199. redundancy_pos = ff_opus_rc_dec_log(&s->rc, 1);
  200. if (s->packet.mode == OPUS_MODE_HYBRID)
  201. redundancy_size = ff_opus_rc_dec_uint(&s->rc, 256) + 2;
  202. else
  203. redundancy_size = size - (consumed + 7) / 8;
  204. size -= redundancy_size;
  205. if (size < 0) {
  206. av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n");
  207. return AVERROR_INVALIDDATA;
  208. }
  209. if (redundancy_pos) {
  210. ret = opus_decode_redundancy(s, data + size, redundancy_size);
  211. if (ret < 0)
  212. return ret;
  213. ff_celt_flush(s->celt);
  214. }
  215. }
  216. /* decode the CELT frame */
  217. if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
  218. float *out_tmp[2] = { s->out[0], s->out[1] };
  219. float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
  220. out_tmp : s->celt_output;
  221. int celt_output_samples = samples;
  222. int delay_samples = av_audio_fifo_size(s->celt_delay);
  223. if (delay_samples) {
  224. if (s->packet.mode == OPUS_MODE_HYBRID) {
  225. av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples);
  226. for (i = 0; i < s->output_channels; i++) {
  227. s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0,
  228. delay_samples);
  229. out_tmp[i] += delay_samples;
  230. }
  231. celt_output_samples -= delay_samples;
  232. } else {
  233. av_log(s->avctx, AV_LOG_WARNING,
  234. "Spurious CELT delay samples present.\n");
  235. av_audio_fifo_drain(s->celt_delay, delay_samples);
  236. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  237. return AVERROR_BUG;
  238. }
  239. }
  240. ff_opus_rc_dec_raw_init(&s->rc, data + size, size);
  241. ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
  242. s->packet.stereo + 1,
  243. s->packet.frame_duration,
  244. (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
  245. celt_band_end[s->packet.bandwidth]);
  246. if (ret < 0)
  247. return ret;
  248. if (s->packet.mode == OPUS_MODE_HYBRID) {
  249. int celt_delay = s->packet.frame_duration - celt_output_samples;
  250. void *delaybuf[2] = { s->celt_output[0] + celt_output_samples,
  251. s->celt_output[1] + celt_output_samples };
  252. for (i = 0; i < s->output_channels; i++) {
  253. s->fdsp->vector_fmac_scalar(out_tmp[i],
  254. s->celt_output[i], 1.0,
  255. celt_output_samples);
  256. }
  257. ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay);
  258. if (ret < 0)
  259. return ret;
  260. }
  261. } else
  262. ff_celt_flush(s->celt);
  263. if (s->redundancy_idx) {
  264. for (i = 0; i < s->output_channels; i++)
  265. opus_fade(s->out[i], s->out[i],
  266. s->redundancy_output[i] + 120 + s->redundancy_idx,
  267. ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
  268. s->redundancy_idx = 0;
  269. }
  270. if (redundancy) {
  271. if (!redundancy_pos) {
  272. ff_celt_flush(s->celt);
  273. ret = opus_decode_redundancy(s, data + size, redundancy_size);
  274. if (ret < 0)
  275. return ret;
  276. for (i = 0; i < s->output_channels; i++) {
  277. opus_fade(s->out[i] + samples - 120 + delayed_samples,
  278. s->out[i] + samples - 120 + delayed_samples,
  279. s->redundancy_output[i] + 120,
  280. ff_celt_window2, 120 - delayed_samples);
  281. if (delayed_samples)
  282. s->redundancy_idx = 120 - delayed_samples;
  283. }
  284. } else {
  285. for (i = 0; i < s->output_channels; i++) {
  286. memcpy(s->out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float));
  287. opus_fade(s->out[i] + 120 + delayed_samples,
  288. s->redundancy_output[i] + 120,
  289. s->out[i] + 120 + delayed_samples,
  290. ff_celt_window2, 120);
  291. }
  292. }
  293. }
  294. return samples;
  295. }
  296. static int opus_decode_subpacket(OpusStreamContext *s,
  297. const uint8_t *buf, int buf_size,
  298. float **out, int out_size,
  299. int nb_samples)
  300. {
  301. int output_samples = 0;
  302. int flush_needed = 0;
  303. int i, j, ret;
  304. s->out[0] = out[0];
  305. s->out[1] = out[1];
  306. s->out_size = out_size;
  307. /* check if we need to flush the resampler */
  308. if (swr_is_initialized(s->swr)) {
  309. if (buf) {
  310. int64_t cur_samplerate;
  311. av_opt_get_int(s->swr, "in_sample_rate", 0, &cur_samplerate);
  312. flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
  313. } else {
  314. flush_needed = !!s->delayed_samples;
  315. }
  316. }
  317. if (!buf && !flush_needed)
  318. return 0;
  319. /* use dummy output buffers if the channel is not mapped to anything */
  320. if (!s->out[0] ||
  321. (s->output_channels == 2 && !s->out[1])) {
  322. av_fast_malloc(&s->out_dummy, &s->out_dummy_allocated_size, s->out_size);
  323. if (!s->out_dummy)
  324. return AVERROR(ENOMEM);
  325. if (!s->out[0])
  326. s->out[0] = s->out_dummy;
  327. if (!s->out[1])
  328. s->out[1] = s->out_dummy;
  329. }
  330. /* flush the resampler if necessary */
  331. if (flush_needed) {
  332. ret = opus_flush_resample(s, s->delayed_samples);
  333. if (ret < 0) {
  334. av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
  335. return ret;
  336. }
  337. swr_close(s->swr);
  338. output_samples += s->delayed_samples;
  339. s->delayed_samples = 0;
  340. if (!buf)
  341. goto finish;
  342. }
  343. /* decode all the frames in the packet */
  344. for (i = 0; i < s->packet.frame_count; i++) {
  345. int size = s->packet.frame_size[i];
  346. int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
  347. if (samples < 0) {
  348. av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
  349. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  350. return samples;
  351. for (j = 0; j < s->output_channels; j++)
  352. memset(s->out[j], 0, s->packet.frame_duration * sizeof(float));
  353. samples = s->packet.frame_duration;
  354. }
  355. output_samples += samples;
  356. for (j = 0; j < s->output_channels; j++)
  357. s->out[j] += samples;
  358. s->out_size -= samples * sizeof(float);
  359. }
  360. finish:
  361. s->out[0] = s->out[1] = NULL;
  362. s->out_size = 0;
  363. return output_samples;
  364. }
  365. static int opus_decode_packet(AVCodecContext *avctx, void *data,
  366. int *got_frame_ptr, AVPacket *avpkt)
  367. {
  368. OpusContext *c = avctx->priv_data;
  369. AVFrame *frame = data;
  370. const uint8_t *buf = avpkt->data;
  371. int buf_size = avpkt->size;
  372. int coded_samples = 0;
  373. int decoded_samples = INT_MAX;
  374. int delayed_samples = 0;
  375. int i, ret;
  376. /* calculate the number of delayed samples */
  377. for (i = 0; i < c->nb_streams; i++) {
  378. OpusStreamContext *s = &c->streams[i];
  379. s->out[0] =
  380. s->out[1] = NULL;
  381. delayed_samples = FFMAX(delayed_samples,
  382. s->delayed_samples + av_audio_fifo_size(c->sync_buffers[i]));
  383. }
  384. /* decode the header of the first sub-packet to find out the sample count */
  385. if (buf) {
  386. OpusPacket *pkt = &c->streams[0].packet;
  387. ret = ff_opus_parse_packet(pkt, buf, buf_size, c->nb_streams > 1);
  388. if (ret < 0) {
  389. av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
  390. return ret;
  391. }
  392. coded_samples += pkt->frame_count * pkt->frame_duration;
  393. c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config);
  394. }
  395. frame->nb_samples = coded_samples + delayed_samples;
  396. /* no input or buffered data => nothing to do */
  397. if (!frame->nb_samples) {
  398. *got_frame_ptr = 0;
  399. return 0;
  400. }
  401. /* setup the data buffers */
  402. ret = ff_get_buffer(avctx, frame, 0);
  403. if (ret < 0)
  404. return ret;
  405. frame->nb_samples = 0;
  406. memset(c->out, 0, c->nb_streams * 2 * sizeof(*c->out));
  407. for (i = 0; i < avctx->channels; i++) {
  408. ChannelMap *map = &c->channel_maps[i];
  409. if (!map->copy)
  410. c->out[2 * map->stream_idx + map->channel_idx] = (float*)frame->extended_data[i];
  411. }
  412. /* read the data from the sync buffers */
  413. for (i = 0; i < c->nb_streams; i++) {
  414. float **out = c->out + 2 * i;
  415. int sync_size = av_audio_fifo_size(c->sync_buffers[i]);
  416. float sync_dummy[32];
  417. int out_dummy = (!out[0]) | ((!out[1]) << 1);
  418. if (!out[0])
  419. out[0] = sync_dummy;
  420. if (!out[1])
  421. out[1] = sync_dummy;
  422. if (out_dummy && sync_size > FF_ARRAY_ELEMS(sync_dummy))
  423. return AVERROR_BUG;
  424. ret = av_audio_fifo_read(c->sync_buffers[i], (void**)out, sync_size);
  425. if (ret < 0)
  426. return ret;
  427. if (out_dummy & 1)
  428. out[0] = NULL;
  429. else
  430. out[0] += ret;
  431. if (out_dummy & 2)
  432. out[1] = NULL;
  433. else
  434. out[1] += ret;
  435. c->out_size[i] = frame->linesize[0] - ret * sizeof(float);
  436. }
  437. /* decode each sub-packet */
  438. for (i = 0; i < c->nb_streams; i++) {
  439. OpusStreamContext *s = &c->streams[i];
  440. if (i && buf) {
  441. ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->nb_streams - 1);
  442. if (ret < 0) {
  443. av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
  444. return ret;
  445. }
  446. if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
  447. av_log(avctx, AV_LOG_ERROR,
  448. "Mismatching coded sample count in substream %d.\n", i);
  449. return AVERROR_INVALIDDATA;
  450. }
  451. s->silk_samplerate = get_silk_samplerate(s->packet.config);
  452. }
  453. ret = opus_decode_subpacket(&c->streams[i], buf, s->packet.data_size,
  454. c->out + 2 * i, c->out_size[i], coded_samples);
  455. if (ret < 0)
  456. return ret;
  457. c->decoded_samples[i] = ret;
  458. decoded_samples = FFMIN(decoded_samples, ret);
  459. buf += s->packet.packet_size;
  460. buf_size -= s->packet.packet_size;
  461. }
  462. /* buffer the extra samples */
  463. for (i = 0; i < c->nb_streams; i++) {
  464. int buffer_samples = c->decoded_samples[i] - decoded_samples;
  465. if (buffer_samples) {
  466. float *buf[2] = { c->out[2 * i + 0] ? c->out[2 * i + 0] : (float*)frame->extended_data[0],
  467. c->out[2 * i + 1] ? c->out[2 * i + 1] : (float*)frame->extended_data[0] };
  468. buf[0] += decoded_samples;
  469. buf[1] += decoded_samples;
  470. ret = av_audio_fifo_write(c->sync_buffers[i], (void**)buf, buffer_samples);
  471. if (ret < 0)
  472. return ret;
  473. }
  474. }
  475. for (i = 0; i < avctx->channels; i++) {
  476. ChannelMap *map = &c->channel_maps[i];
  477. /* handle copied channels */
  478. if (map->copy) {
  479. memcpy(frame->extended_data[i],
  480. frame->extended_data[map->copy_idx],
  481. frame->linesize[0]);
  482. } else if (map->silence) {
  483. memset(frame->extended_data[i], 0, frame->linesize[0]);
  484. }
  485. if (c->gain_i && decoded_samples > 0) {
  486. c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
  487. (float*)frame->extended_data[i],
  488. c->gain, FFALIGN(decoded_samples, 8));
  489. }
  490. }
  491. frame->nb_samples = decoded_samples;
  492. *got_frame_ptr = !!decoded_samples;
  493. return avpkt->size;
  494. }
  495. static av_cold void opus_decode_flush(AVCodecContext *ctx)
  496. {
  497. OpusContext *c = ctx->priv_data;
  498. int i;
  499. for (i = 0; i < c->nb_streams; i++) {
  500. OpusStreamContext *s = &c->streams[i];
  501. memset(&s->packet, 0, sizeof(s->packet));
  502. s->delayed_samples = 0;
  503. if (s->celt_delay)
  504. av_audio_fifo_drain(s->celt_delay, av_audio_fifo_size(s->celt_delay));
  505. swr_close(s->swr);
  506. av_audio_fifo_drain(c->sync_buffers[i], av_audio_fifo_size(c->sync_buffers[i]));
  507. ff_silk_flush(s->silk);
  508. ff_celt_flush(s->celt);
  509. }
  510. }
  511. static av_cold int opus_decode_close(AVCodecContext *avctx)
  512. {
  513. OpusContext *c = avctx->priv_data;
  514. int i;
  515. for (i = 0; i < c->nb_streams; i++) {
  516. OpusStreamContext *s = &c->streams[i];
  517. ff_silk_free(&s->silk);
  518. ff_celt_free(&s->celt);
  519. av_freep(&s->out_dummy);
  520. s->out_dummy_allocated_size = 0;
  521. av_audio_fifo_free(s->celt_delay);
  522. swr_free(&s->swr);
  523. }
  524. av_freep(&c->streams);
  525. if (c->sync_buffers) {
  526. for (i = 0; i < c->nb_streams; i++)
  527. av_audio_fifo_free(c->sync_buffers[i]);
  528. }
  529. av_freep(&c->sync_buffers);
  530. av_freep(&c->decoded_samples);
  531. av_freep(&c->out);
  532. av_freep(&c->out_size);
  533. c->nb_streams = 0;
  534. av_freep(&c->channel_maps);
  535. av_freep(&c->fdsp);
  536. return 0;
  537. }
  538. static av_cold int opus_decode_init(AVCodecContext *avctx)
  539. {
  540. OpusContext *c = avctx->priv_data;
  541. int ret, i, j;
  542. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  543. avctx->sample_rate = 48000;
  544. c->fdsp = avpriv_float_dsp_alloc(0);
  545. if (!c->fdsp)
  546. return AVERROR(ENOMEM);
  547. /* find out the channel configuration */
  548. ret = ff_opus_parse_extradata(avctx, c);
  549. if (ret < 0) {
  550. av_freep(&c->fdsp);
  551. return ret;
  552. }
  553. /* allocate and init each independent decoder */
  554. c->streams = av_mallocz_array(c->nb_streams, sizeof(*c->streams));
  555. c->out = av_mallocz_array(c->nb_streams, 2 * sizeof(*c->out));
  556. c->out_size = av_mallocz_array(c->nb_streams, sizeof(*c->out_size));
  557. c->sync_buffers = av_mallocz_array(c->nb_streams, sizeof(*c->sync_buffers));
  558. c->decoded_samples = av_mallocz_array(c->nb_streams, sizeof(*c->decoded_samples));
  559. if (!c->streams || !c->sync_buffers || !c->decoded_samples || !c->out || !c->out_size) {
  560. c->nb_streams = 0;
  561. ret = AVERROR(ENOMEM);
  562. goto fail;
  563. }
  564. for (i = 0; i < c->nb_streams; i++) {
  565. OpusStreamContext *s = &c->streams[i];
  566. uint64_t layout;
  567. s->output_channels = (i < c->nb_stereo_streams) ? 2 : 1;
  568. s->avctx = avctx;
  569. for (j = 0; j < s->output_channels; j++) {
  570. s->silk_output[j] = s->silk_buf[j];
  571. s->celt_output[j] = s->celt_buf[j];
  572. s->redundancy_output[j] = s->redundancy_buf[j];
  573. }
  574. s->fdsp = c->fdsp;
  575. s->swr =swr_alloc();
  576. if (!s->swr)
  577. goto fail;
  578. layout = (s->output_channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
  579. av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
  580. av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
  581. av_opt_set_int(s->swr, "in_channel_layout", layout, 0);
  582. av_opt_set_int(s->swr, "out_channel_layout", layout, 0);
  583. av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
  584. av_opt_set_int(s->swr, "filter_size", 16, 0);
  585. ret = ff_silk_init(avctx, &s->silk, s->output_channels);
  586. if (ret < 0)
  587. goto fail;
  588. ret = ff_celt_init(avctx, &s->celt, s->output_channels);
  589. if (ret < 0)
  590. goto fail;
  591. s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt,
  592. s->output_channels, 1024);
  593. if (!s->celt_delay) {
  594. ret = AVERROR(ENOMEM);
  595. goto fail;
  596. }
  597. c->sync_buffers[i] = av_audio_fifo_alloc(avctx->sample_fmt,
  598. s->output_channels, 32);
  599. if (!c->sync_buffers[i]) {
  600. ret = AVERROR(ENOMEM);
  601. goto fail;
  602. }
  603. }
  604. return 0;
  605. fail:
  606. opus_decode_close(avctx);
  607. return ret;
  608. }
  609. AVCodec ff_opus_decoder = {
  610. .name = "opus",
  611. .long_name = NULL_IF_CONFIG_SMALL("Opus"),
  612. .type = AVMEDIA_TYPE_AUDIO,
  613. .id = AV_CODEC_ID_OPUS,
  614. .priv_data_size = sizeof(OpusContext),
  615. .init = opus_decode_init,
  616. .close = opus_decode_close,
  617. .decode = opus_decode_packet,
  618. .flush = opus_decode_flush,
  619. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
  620. };