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.

681 lines
21KB

  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 "celp_filters.h"
  42. #include "fft.h"
  43. #include "get_bits.h"
  44. #include "internal.h"
  45. #include "mathops.h"
  46. #include "opus.h"
  47. static const uint16_t silk_frame_duration_ms[16] = {
  48. 10, 20, 40, 60,
  49. 10, 20, 40, 60,
  50. 10, 20, 40, 60,
  51. 10, 20,
  52. 10, 20,
  53. };
  54. /* number of samples of silence to feed to the resampler
  55. * at the beginning */
  56. static const int silk_resample_delay[] = {
  57. 4, 8, 11, 11, 11
  58. };
  59. static const uint8_t celt_band_end[] = { 13, 17, 17, 19, 21 };
  60. static int get_silk_samplerate(int config)
  61. {
  62. if (config < 4)
  63. return 8000;
  64. else if (config < 8)
  65. return 12000;
  66. return 16000;
  67. }
  68. /**
  69. * Range decoder
  70. */
  71. static int opus_rc_init(OpusRangeCoder *rc, const uint8_t *data, int size)
  72. {
  73. int ret = init_get_bits8(&rc->gb, data, size);
  74. if (ret < 0)
  75. return ret;
  76. rc->range = 128;
  77. rc->value = 127 - get_bits(&rc->gb, 7);
  78. rc->total_read_bits = 9;
  79. opus_rc_normalize(rc);
  80. return 0;
  81. }
  82. static void opus_raw_init(OpusRangeCoder *rc, const uint8_t *rightend,
  83. unsigned int bytes)
  84. {
  85. rc->rb.position = rightend;
  86. rc->rb.bytes = bytes;
  87. rc->rb.cachelen = 0;
  88. rc->rb.cacheval = 0;
  89. }
  90. static void opus_fade(float *out,
  91. const float *in1, const float *in2,
  92. const float *window, int len)
  93. {
  94. int i;
  95. for (i = 0; i < len; i++)
  96. out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]);
  97. }
  98. static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
  99. {
  100. int celt_size = av_audio_fifo_size(s->celt_delay);
  101. int ret, i;
  102. ret = swr_convert(s->swr,
  103. (uint8_t**)s->out, nb_samples,
  104. NULL, 0);
  105. if (ret < 0)
  106. return ret;
  107. else if (ret != nb_samples) {
  108. av_log(s->avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n",
  109. ret);
  110. return AVERROR_BUG;
  111. }
  112. if (celt_size) {
  113. if (celt_size != nb_samples) {
  114. av_log(s->avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n");
  115. return AVERROR_BUG;
  116. }
  117. av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, nb_samples);
  118. for (i = 0; i < s->output_channels; i++) {
  119. s->fdsp->vector_fmac_scalar(s->out[i],
  120. s->celt_output[i], 1.0,
  121. nb_samples);
  122. }
  123. }
  124. if (s->redundancy_idx) {
  125. for (i = 0; i < s->output_channels; i++)
  126. opus_fade(s->out[i], s->out[i],
  127. s->redundancy_output[i] + 120 + s->redundancy_idx,
  128. ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
  129. s->redundancy_idx = 0;
  130. }
  131. s->out[0] += nb_samples;
  132. s->out[1] += nb_samples;
  133. s->out_size -= nb_samples * sizeof(float);
  134. return 0;
  135. }
  136. static int opus_init_resample(OpusStreamContext *s)
  137. {
  138. static const float delay[16] = { 0.0 };
  139. const uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
  140. int ret;
  141. av_opt_set_int(s->swr, "in_sample_rate", s->silk_samplerate, 0);
  142. ret = swr_init(s->swr);
  143. if (ret < 0) {
  144. av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n");
  145. return ret;
  146. }
  147. ret = swr_convert(s->swr,
  148. NULL, 0,
  149. delayptr, silk_resample_delay[s->packet.bandwidth]);
  150. if (ret < 0) {
  151. av_log(s->avctx, AV_LOG_ERROR,
  152. "Error feeding initial silence to the resampler.\n");
  153. return ret;
  154. }
  155. return 0;
  156. }
  157. static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
  158. {
  159. int ret;
  160. enum OpusBandwidth bw = s->packet.bandwidth;
  161. if (s->packet.mode == OPUS_MODE_SILK &&
  162. bw == OPUS_BANDWIDTH_MEDIUMBAND)
  163. bw = OPUS_BANDWIDTH_WIDEBAND;
  164. ret = opus_rc_init(&s->redundancy_rc, data, size);
  165. if (ret < 0)
  166. goto fail;
  167. opus_raw_init(&s->redundancy_rc, data + size, size);
  168. ret = ff_celt_decode_frame(s->celt, &s->redundancy_rc,
  169. s->redundancy_output,
  170. s->packet.stereo + 1, 240,
  171. 0, celt_band_end[s->packet.bandwidth]);
  172. if (ret < 0)
  173. goto fail;
  174. return 0;
  175. fail:
  176. av_log(s->avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n");
  177. return ret;
  178. }
  179. static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
  180. {
  181. int samples = s->packet.frame_duration;
  182. int redundancy = 0;
  183. int redundancy_size, redundancy_pos;
  184. int ret, i, consumed;
  185. int delayed_samples = s->delayed_samples;
  186. ret = opus_rc_init(&s->rc, data, size);
  187. if (ret < 0)
  188. return ret;
  189. /* decode the silk frame */
  190. if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) {
  191. if (!swr_is_initialized(s->swr)) {
  192. ret = opus_init_resample(s);
  193. if (ret < 0)
  194. return ret;
  195. }
  196. samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output,
  197. FFMIN(s->packet.bandwidth, OPUS_BANDWIDTH_WIDEBAND),
  198. s->packet.stereo + 1,
  199. silk_frame_duration_ms[s->packet.config]);
  200. if (samples < 0) {
  201. av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n");
  202. return samples;
  203. }
  204. samples = swr_convert(s->swr,
  205. (uint8_t**)s->out, s->packet.frame_duration,
  206. (const uint8_t**)s->silk_output, samples);
  207. if (samples < 0) {
  208. av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n");
  209. return samples;
  210. }
  211. av_assert2((samples & 7) == 0);
  212. s->delayed_samples += s->packet.frame_duration - samples;
  213. } else
  214. ff_silk_flush(s->silk);
  215. // decode redundancy information
  216. consumed = opus_rc_tell(&s->rc);
  217. if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8)
  218. redundancy = opus_rc_p2model(&s->rc, 12);
  219. else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8)
  220. redundancy = 1;
  221. if (redundancy) {
  222. redundancy_pos = opus_rc_p2model(&s->rc, 1);
  223. if (s->packet.mode == OPUS_MODE_HYBRID)
  224. redundancy_size = opus_rc_unimodel(&s->rc, 256) + 2;
  225. else
  226. redundancy_size = size - (consumed + 7) / 8;
  227. size -= redundancy_size;
  228. if (size < 0) {
  229. av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n");
  230. return AVERROR_INVALIDDATA;
  231. }
  232. if (redundancy_pos) {
  233. ret = opus_decode_redundancy(s, data + size, redundancy_size);
  234. if (ret < 0)
  235. return ret;
  236. ff_celt_flush(s->celt);
  237. }
  238. }
  239. /* decode the CELT frame */
  240. if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
  241. float *out_tmp[2] = { s->out[0], s->out[1] };
  242. float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
  243. out_tmp : s->celt_output;
  244. int celt_output_samples = samples;
  245. int delay_samples = av_audio_fifo_size(s->celt_delay);
  246. if (delay_samples) {
  247. if (s->packet.mode == OPUS_MODE_HYBRID) {
  248. av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples);
  249. for (i = 0; i < s->output_channels; i++) {
  250. s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0,
  251. delay_samples);
  252. out_tmp[i] += delay_samples;
  253. }
  254. celt_output_samples -= delay_samples;
  255. } else {
  256. av_log(s->avctx, AV_LOG_WARNING,
  257. "Spurious CELT delay samples present.\n");
  258. av_audio_fifo_drain(s->celt_delay, delay_samples);
  259. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  260. return AVERROR_BUG;
  261. }
  262. }
  263. opus_raw_init(&s->rc, data + size, size);
  264. ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
  265. s->packet.stereo + 1,
  266. s->packet.frame_duration,
  267. (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
  268. celt_band_end[s->packet.bandwidth]);
  269. if (ret < 0)
  270. return ret;
  271. if (s->packet.mode == OPUS_MODE_HYBRID) {
  272. int celt_delay = s->packet.frame_duration - celt_output_samples;
  273. void *delaybuf[2] = { s->celt_output[0] + celt_output_samples,
  274. s->celt_output[1] + celt_output_samples };
  275. for (i = 0; i < s->output_channels; i++) {
  276. s->fdsp->vector_fmac_scalar(out_tmp[i],
  277. s->celt_output[i], 1.0,
  278. celt_output_samples);
  279. }
  280. ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay);
  281. if (ret < 0)
  282. return ret;
  283. }
  284. } else
  285. ff_celt_flush(s->celt);
  286. if (s->redundancy_idx) {
  287. for (i = 0; i < s->output_channels; i++)
  288. opus_fade(s->out[i], s->out[i],
  289. s->redundancy_output[i] + 120 + s->redundancy_idx,
  290. ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
  291. s->redundancy_idx = 0;
  292. }
  293. if (redundancy) {
  294. if (!redundancy_pos) {
  295. ff_celt_flush(s->celt);
  296. ret = opus_decode_redundancy(s, data + size, redundancy_size);
  297. if (ret < 0)
  298. return ret;
  299. for (i = 0; i < s->output_channels; i++) {
  300. opus_fade(s->out[i] + samples - 120 + delayed_samples,
  301. s->out[i] + samples - 120 + delayed_samples,
  302. s->redundancy_output[i] + 120,
  303. ff_celt_window2, 120 - delayed_samples);
  304. if (delayed_samples)
  305. s->redundancy_idx = 120 - delayed_samples;
  306. }
  307. } else {
  308. for (i = 0; i < s->output_channels; i++) {
  309. memcpy(s->out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float));
  310. opus_fade(s->out[i] + 120 + delayed_samples,
  311. s->redundancy_output[i] + 120,
  312. s->out[i] + 120 + delayed_samples,
  313. ff_celt_window2, 120);
  314. }
  315. }
  316. }
  317. return samples;
  318. }
  319. static int opus_decode_subpacket(OpusStreamContext *s,
  320. const uint8_t *buf, int buf_size,
  321. int nb_samples)
  322. {
  323. int output_samples = 0;
  324. int flush_needed = 0;
  325. int i, j, ret;
  326. /* check if we need to flush the resampler */
  327. if (swr_is_initialized(s->swr)) {
  328. if (buf) {
  329. int64_t cur_samplerate;
  330. av_opt_get_int(s->swr, "in_sample_rate", 0, &cur_samplerate);
  331. flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
  332. } else {
  333. flush_needed = !!s->delayed_samples;
  334. }
  335. }
  336. if (!buf && !flush_needed)
  337. return 0;
  338. /* use dummy output buffers if the channel is not mapped to anything */
  339. if (!s->out[0] ||
  340. (s->output_channels == 2 && !s->out[1])) {
  341. av_fast_malloc(&s->out_dummy, &s->out_dummy_allocated_size, s->out_size);
  342. if (!s->out_dummy)
  343. return AVERROR(ENOMEM);
  344. if (!s->out[0])
  345. s->out[0] = s->out_dummy;
  346. if (!s->out[1])
  347. s->out[1] = s->out_dummy;
  348. }
  349. /* flush the resampler if necessary */
  350. if (flush_needed) {
  351. ret = opus_flush_resample(s, s->delayed_samples);
  352. if (ret < 0) {
  353. av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
  354. return ret;
  355. }
  356. swr_close(s->swr);
  357. output_samples += s->delayed_samples;
  358. s->delayed_samples = 0;
  359. if (!buf)
  360. goto finish;
  361. }
  362. /* decode all the frames in the packet */
  363. for (i = 0; i < s->packet.frame_count; i++) {
  364. int size = s->packet.frame_size[i];
  365. int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
  366. if (samples < 0) {
  367. av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
  368. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  369. return samples;
  370. for (j = 0; j < s->output_channels; j++)
  371. memset(s->out[j], 0, s->packet.frame_duration * sizeof(float));
  372. samples = s->packet.frame_duration;
  373. }
  374. output_samples += samples;
  375. for (j = 0; j < s->output_channels; j++)
  376. s->out[j] += samples;
  377. s->out_size -= samples * sizeof(float);
  378. }
  379. finish:
  380. s->out[0] = s->out[1] = NULL;
  381. s->out_size = 0;
  382. return output_samples;
  383. }
  384. static int opus_decode_packet(AVCodecContext *avctx, void *data,
  385. int *got_frame_ptr, AVPacket *avpkt)
  386. {
  387. OpusContext *c = avctx->priv_data;
  388. AVFrame *frame = data;
  389. const uint8_t *buf = avpkt->data;
  390. int buf_size = avpkt->size;
  391. int coded_samples = 0;
  392. int decoded_samples = 0;
  393. int i, ret;
  394. /* decode the header of the first sub-packet to find out the sample count */
  395. if (buf) {
  396. OpusPacket *pkt = &c->streams[0].packet;
  397. ret = ff_opus_parse_packet(pkt, buf, buf_size, c->nb_streams > 1);
  398. if (ret < 0) {
  399. av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
  400. return ret;
  401. }
  402. coded_samples += pkt->frame_count * pkt->frame_duration;
  403. c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config);
  404. }
  405. frame->nb_samples = coded_samples + c->streams[0].delayed_samples;
  406. /* no input or buffered data => nothing to do */
  407. if (!frame->nb_samples) {
  408. *got_frame_ptr = 0;
  409. return 0;
  410. }
  411. /* setup the data buffers */
  412. ret = ff_get_buffer(avctx, frame, 0);
  413. if (ret < 0) {
  414. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  415. return ret;
  416. }
  417. frame->nb_samples = 0;
  418. for (i = 0; i < avctx->channels; i++) {
  419. ChannelMap *map = &c->channel_maps[i];
  420. if (!map->copy)
  421. c->streams[map->stream_idx].out[map->channel_idx] = (float*)frame->extended_data[i];
  422. }
  423. for (i = 0; i < c->nb_streams; i++)
  424. c->streams[i].out_size = frame->linesize[0];
  425. /* decode each sub-packet */
  426. for (i = 0; i < c->nb_streams; i++) {
  427. OpusStreamContext *s = &c->streams[i];
  428. if (i && buf) {
  429. ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->nb_streams - 1);
  430. if (ret < 0) {
  431. av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
  432. return ret;
  433. }
  434. if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
  435. av_log(avctx, AV_LOG_ERROR,
  436. "Mismatching coded sample count in substream %d.\n", i);
  437. return AVERROR_INVALIDDATA;
  438. }
  439. s->silk_samplerate = get_silk_samplerate(s->packet.config);
  440. }
  441. ret = opus_decode_subpacket(&c->streams[i], buf,
  442. s->packet.data_size, coded_samples);
  443. if (ret < 0)
  444. return ret;
  445. if (decoded_samples && ret != decoded_samples) {
  446. av_log(avctx, AV_LOG_ERROR, "Different numbers of decoded samples "
  447. "in a multi-channel stream\n");
  448. return AVERROR_INVALIDDATA;
  449. }
  450. decoded_samples = ret;
  451. buf += s->packet.packet_size;
  452. buf_size -= s->packet.packet_size;
  453. }
  454. for (i = 0; i < avctx->channels; i++) {
  455. ChannelMap *map = &c->channel_maps[i];
  456. /* handle copied channels */
  457. if (map->copy) {
  458. memcpy(frame->extended_data[i],
  459. frame->extended_data[map->copy_idx],
  460. frame->linesize[0]);
  461. } else if (map->silence) {
  462. memset(frame->extended_data[i], 0, frame->linesize[0]);
  463. }
  464. if (c->gain_i) {
  465. c->fdsp.vector_fmul_scalar((float*)frame->extended_data[i],
  466. (float*)frame->extended_data[i],
  467. c->gain, FFALIGN(decoded_samples, 8));
  468. }
  469. }
  470. frame->nb_samples = decoded_samples;
  471. *got_frame_ptr = !!decoded_samples;
  472. return avpkt->size;
  473. }
  474. static av_cold void opus_decode_flush(AVCodecContext *ctx)
  475. {
  476. OpusContext *c = ctx->priv_data;
  477. int i;
  478. for (i = 0; i < c->nb_streams; i++) {
  479. OpusStreamContext *s = &c->streams[i];
  480. memset(&s->packet, 0, sizeof(s->packet));
  481. s->delayed_samples = 0;
  482. if (s->celt_delay)
  483. av_audio_fifo_drain(s->celt_delay, av_audio_fifo_size(s->celt_delay));
  484. swr_close(s->swr);
  485. ff_silk_flush(s->silk);
  486. ff_celt_flush(s->celt);
  487. }
  488. }
  489. static av_cold int opus_decode_close(AVCodecContext *avctx)
  490. {
  491. OpusContext *c = avctx->priv_data;
  492. int i;
  493. for (i = 0; i < c->nb_streams; i++) {
  494. OpusStreamContext *s = &c->streams[i];
  495. ff_silk_free(&s->silk);
  496. ff_celt_free(&s->celt);
  497. av_freep(&s->out_dummy);
  498. s->out_dummy_allocated_size = 0;
  499. av_audio_fifo_free(s->celt_delay);
  500. swr_free(&s->swr);
  501. }
  502. av_freep(&c->streams);
  503. c->nb_streams = 0;
  504. av_freep(&c->channel_maps);
  505. return 0;
  506. }
  507. static av_cold int opus_decode_init(AVCodecContext *avctx)
  508. {
  509. OpusContext *c = avctx->priv_data;
  510. int ret, i, j;
  511. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  512. avctx->sample_rate = 48000;
  513. avpriv_float_dsp_init(&c->fdsp, 0);
  514. /* find out the channel configuration */
  515. ret = ff_opus_parse_extradata(avctx, c);
  516. if (ret < 0)
  517. return ret;
  518. /* allocate and init each independent decoder */
  519. c->streams = av_mallocz_array(c->nb_streams, sizeof(*c->streams));
  520. if (!c->streams) {
  521. c->nb_streams = 0;
  522. ret = AVERROR(ENOMEM);
  523. goto fail;
  524. }
  525. for (i = 0; i < c->nb_streams; i++) {
  526. OpusStreamContext *s = &c->streams[i];
  527. uint64_t layout;
  528. s->output_channels = (i < c->nb_stereo_streams) ? 2 : 1;
  529. s->avctx = avctx;
  530. for (j = 0; j < s->output_channels; j++) {
  531. s->silk_output[j] = s->silk_buf[j];
  532. s->celt_output[j] = s->celt_buf[j];
  533. s->redundancy_output[j] = s->redundancy_buf[j];
  534. }
  535. s->fdsp = &c->fdsp;
  536. s->swr =swr_alloc();
  537. if (!s->swr)
  538. goto fail;
  539. layout = (s->output_channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
  540. av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
  541. av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
  542. av_opt_set_int(s->swr, "in_channel_layout", layout, 0);
  543. av_opt_set_int(s->swr, "out_channel_layout", layout, 0);
  544. av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
  545. av_opt_set_int(s->swr, "filter_size", 16, 0);
  546. ret = ff_silk_init(avctx, &s->silk, s->output_channels);
  547. if (ret < 0)
  548. goto fail;
  549. ret = ff_celt_init(avctx, &s->celt, s->output_channels);
  550. if (ret < 0)
  551. goto fail;
  552. s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt,
  553. s->output_channels, 1024);
  554. if (!s->celt_delay) {
  555. ret = AVERROR(ENOMEM);
  556. goto fail;
  557. }
  558. }
  559. return 0;
  560. fail:
  561. opus_decode_close(avctx);
  562. return ret;
  563. }
  564. AVCodec ff_opus_decoder = {
  565. .name = "opus",
  566. .long_name = NULL_IF_CONFIG_SMALL("Opus"),
  567. .type = AVMEDIA_TYPE_AUDIO,
  568. .id = AV_CODEC_ID_OPUS,
  569. .priv_data_size = sizeof(OpusContext),
  570. .init = opus_decode_init,
  571. .close = opus_decode_close,
  572. .decode = opus_decode_packet,
  573. .flush = opus_decode_flush,
  574. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  575. };