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.

756 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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 "libavresample/avresample.h"
  40. #include "avcodec.h"
  41. #include "bitstream.h"
  42. #include "celp_filters.h"
  43. #include "fft.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 = bitstream_init8(&rc->bc, data, size);
  74. if (ret < 0)
  75. return ret;
  76. rc->range = 128;
  77. rc->value = 127 - bitstream_read(&rc->bc, 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 = avresample_convert(s->avr, (uint8_t**)s->out, s->out_size, nb_samples,
  103. NULL, 0, 0);
  104. if (ret < 0)
  105. return ret;
  106. else if (ret != nb_samples) {
  107. av_log(s->avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n",
  108. ret);
  109. return AVERROR_BUG;
  110. }
  111. if (celt_size) {
  112. if (celt_size != nb_samples) {
  113. av_log(s->avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n");
  114. return AVERROR_BUG;
  115. }
  116. av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, nb_samples);
  117. for (i = 0; i < s->output_channels; i++) {
  118. s->fdsp->vector_fmac_scalar(s->out[i],
  119. s->celt_output[i], 1.0,
  120. nb_samples);
  121. }
  122. }
  123. if (s->redundancy_idx) {
  124. for (i = 0; i < s->output_channels; i++)
  125. opus_fade(s->out[i], s->out[i],
  126. s->redundancy_output[i] + 120 + s->redundancy_idx,
  127. ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
  128. s->redundancy_idx = 0;
  129. }
  130. s->out[0] += nb_samples;
  131. s->out[1] += nb_samples;
  132. s->out_size -= nb_samples * sizeof(float);
  133. return 0;
  134. }
  135. static int opus_init_resample(OpusStreamContext *s)
  136. {
  137. float delay[16] = { 0.0 };
  138. uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
  139. int ret;
  140. av_opt_set_int(s->avr, "in_sample_rate", s->silk_samplerate, 0);
  141. ret = avresample_open(s->avr);
  142. if (ret < 0) {
  143. av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n");
  144. return ret;
  145. }
  146. ret = avresample_convert(s->avr, NULL, 0, 0, delayptr, sizeof(delay),
  147. silk_resample_delay[s->packet.bandwidth]);
  148. if (ret < 0) {
  149. av_log(s->avctx, AV_LOG_ERROR,
  150. "Error feeding initial silence to the resampler.\n");
  151. return ret;
  152. }
  153. return 0;
  154. }
  155. static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
  156. {
  157. int ret;
  158. enum OpusBandwidth bw = s->packet.bandwidth;
  159. if (s->packet.mode == OPUS_MODE_SILK &&
  160. bw == OPUS_BANDWIDTH_MEDIUMBAND)
  161. bw = OPUS_BANDWIDTH_WIDEBAND;
  162. ret = opus_rc_init(&s->redundancy_rc, data, size);
  163. if (ret < 0)
  164. goto fail;
  165. opus_raw_init(&s->redundancy_rc, data + size, size);
  166. ret = ff_celt_decode_frame(s->celt, &s->redundancy_rc,
  167. s->redundancy_output,
  168. s->packet.stereo + 1, 240,
  169. 0, celt_band_end[s->packet.bandwidth]);
  170. if (ret < 0)
  171. goto fail;
  172. return 0;
  173. fail:
  174. av_log(s->avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n");
  175. return ret;
  176. }
  177. static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
  178. {
  179. int samples = s->packet.frame_duration;
  180. int redundancy = 0;
  181. int redundancy_size, redundancy_pos;
  182. int ret, i, consumed;
  183. int delayed_samples = s->delayed_samples;
  184. ret = opus_rc_init(&s->rc, data, size);
  185. if (ret < 0)
  186. return ret;
  187. /* decode the silk frame */
  188. if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) {
  189. if (!avresample_is_open(s->avr)) {
  190. ret = opus_init_resample(s);
  191. if (ret < 0)
  192. return ret;
  193. }
  194. samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output,
  195. FFMIN(s->packet.bandwidth, OPUS_BANDWIDTH_WIDEBAND),
  196. s->packet.stereo + 1,
  197. silk_frame_duration_ms[s->packet.config]);
  198. if (samples < 0) {
  199. av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n");
  200. return samples;
  201. }
  202. samples = avresample_convert(s->avr, (uint8_t**)s->out, s->out_size,
  203. s->packet.frame_duration,
  204. (uint8_t**)s->silk_output,
  205. sizeof(s->silk_buf[0]),
  206. samples);
  207. if (samples < 0) {
  208. av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n");
  209. return samples;
  210. }
  211. s->delayed_samples += s->packet.frame_duration - samples;
  212. } else
  213. ff_silk_flush(s->silk);
  214. // decode redundancy information
  215. consumed = opus_rc_tell(&s->rc);
  216. if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8)
  217. redundancy = opus_rc_p2model(&s->rc, 12);
  218. else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8)
  219. redundancy = 1;
  220. if (redundancy) {
  221. redundancy_pos = opus_rc_p2model(&s->rc, 1);
  222. if (s->packet.mode == OPUS_MODE_HYBRID)
  223. redundancy_size = opus_rc_unimodel(&s->rc, 256) + 2;
  224. else
  225. redundancy_size = size - (consumed + 7) / 8;
  226. size -= redundancy_size;
  227. if (size < 0) {
  228. av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n");
  229. return AVERROR_INVALIDDATA;
  230. }
  231. if (redundancy_pos) {
  232. ret = opus_decode_redundancy(s, data + size, redundancy_size);
  233. if (ret < 0)
  234. return ret;
  235. ff_celt_flush(s->celt);
  236. }
  237. }
  238. /* decode the CELT frame */
  239. if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
  240. float *out_tmp[2] = { s->out[0], s->out[1] };
  241. float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
  242. out_tmp : s->celt_output;
  243. int celt_output_samples = samples;
  244. int delay_samples = av_audio_fifo_size(s->celt_delay);
  245. if (delay_samples) {
  246. if (s->packet.mode == OPUS_MODE_HYBRID) {
  247. av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples);
  248. for (i = 0; i < s->output_channels; i++) {
  249. s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0,
  250. delay_samples);
  251. out_tmp[i] += delay_samples;
  252. }
  253. celt_output_samples -= delay_samples;
  254. } else {
  255. av_log(s->avctx, AV_LOG_WARNING,
  256. "Spurious CELT delay samples present.\n");
  257. av_audio_fifo_drain(s->celt_delay, delay_samples);
  258. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  259. return AVERROR_BUG;
  260. }
  261. }
  262. opus_raw_init(&s->rc, data + size, size);
  263. ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
  264. s->packet.stereo + 1,
  265. s->packet.frame_duration,
  266. (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
  267. celt_band_end[s->packet.bandwidth]);
  268. if (ret < 0)
  269. return ret;
  270. if (s->packet.mode == OPUS_MODE_HYBRID) {
  271. int celt_delay = s->packet.frame_duration - celt_output_samples;
  272. void *delaybuf[2] = { s->celt_output[0] + celt_output_samples,
  273. s->celt_output[1] + celt_output_samples };
  274. for (i = 0; i < s->output_channels; i++) {
  275. s->fdsp->vector_fmac_scalar(out_tmp[i],
  276. s->celt_output[i], 1.0,
  277. celt_output_samples);
  278. }
  279. ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay);
  280. if (ret < 0)
  281. return ret;
  282. }
  283. } else
  284. ff_celt_flush(s->celt);
  285. if (s->redundancy_idx) {
  286. for (i = 0; i < s->output_channels; i++)
  287. opus_fade(s->out[i], s->out[i],
  288. s->redundancy_output[i] + 120 + s->redundancy_idx,
  289. ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
  290. s->redundancy_idx = 0;
  291. }
  292. if (redundancy) {
  293. if (!redundancy_pos) {
  294. ff_celt_flush(s->celt);
  295. ret = opus_decode_redundancy(s, data + size, redundancy_size);
  296. if (ret < 0)
  297. return ret;
  298. for (i = 0; i < s->output_channels; i++) {
  299. opus_fade(s->out[i] + samples - 120 + delayed_samples,
  300. s->out[i] + samples - 120 + delayed_samples,
  301. s->redundancy_output[i] + 120,
  302. ff_celt_window2, 120 - delayed_samples);
  303. if (delayed_samples)
  304. s->redundancy_idx = 120 - delayed_samples;
  305. }
  306. } else {
  307. for (i = 0; i < s->output_channels; i++) {
  308. memcpy(s->out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float));
  309. opus_fade(s->out[i] + 120 + delayed_samples,
  310. s->redundancy_output[i] + 120,
  311. s->out[i] + 120 + delayed_samples,
  312. ff_celt_window2, 120);
  313. }
  314. }
  315. }
  316. return samples;
  317. }
  318. static int opus_decode_subpacket(OpusStreamContext *s,
  319. const uint8_t *buf, int buf_size,
  320. float **out, int out_size,
  321. int nb_samples)
  322. {
  323. int output_samples = 0;
  324. int flush_needed = 0;
  325. int i, j, ret;
  326. s->out[0] = out[0];
  327. s->out[1] = out[1];
  328. s->out_size = out_size;
  329. /* check if we need to flush the resampler */
  330. if (avresample_is_open(s->avr)) {
  331. if (buf) {
  332. int64_t cur_samplerate;
  333. av_opt_get_int(s->avr, "in_sample_rate", 0, &cur_samplerate);
  334. flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
  335. } else {
  336. flush_needed = !!s->delayed_samples;
  337. }
  338. }
  339. if (!buf && !flush_needed)
  340. return 0;
  341. /* use dummy output buffers if the channel is not mapped to anything */
  342. if (!s->out[0] ||
  343. (s->output_channels == 2 && !s->out[1])) {
  344. av_fast_malloc(&s->out_dummy, &s->out_dummy_allocated_size, s->out_size);
  345. if (!s->out_dummy)
  346. return AVERROR(ENOMEM);
  347. if (!s->out[0])
  348. s->out[0] = s->out_dummy;
  349. if (!s->out[1])
  350. s->out[1] = s->out_dummy;
  351. }
  352. /* flush the resampler if necessary */
  353. if (flush_needed) {
  354. ret = opus_flush_resample(s, s->delayed_samples);
  355. if (ret < 0) {
  356. av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
  357. return ret;
  358. }
  359. avresample_close(s->avr);
  360. output_samples += s->delayed_samples;
  361. s->delayed_samples = 0;
  362. if (!buf)
  363. goto finish;
  364. }
  365. /* decode all the frames in the packet */
  366. for (i = 0; i < s->packet.frame_count; i++) {
  367. int size = s->packet.frame_size[i];
  368. int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
  369. if (samples < 0) {
  370. av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
  371. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  372. return samples;
  373. for (j = 0; j < s->output_channels; j++)
  374. memset(s->out[j], 0, s->packet.frame_duration * sizeof(float));
  375. samples = s->packet.frame_duration;
  376. }
  377. output_samples += samples;
  378. for (j = 0; j < s->output_channels; j++)
  379. s->out[j] += samples;
  380. s->out_size -= samples * sizeof(float);
  381. }
  382. finish:
  383. s->out[0] = s->out[1] = NULL;
  384. s->out_size = 0;
  385. return output_samples;
  386. }
  387. static int opus_decode_packet(AVCodecContext *avctx, void *data,
  388. int *got_frame_ptr, AVPacket *avpkt)
  389. {
  390. OpusContext *c = avctx->priv_data;
  391. AVFrame *frame = data;
  392. const uint8_t *buf = avpkt->data;
  393. int buf_size = avpkt->size;
  394. int coded_samples = 0;
  395. int decoded_samples = INT_MAX;
  396. int delayed_samples = 0;
  397. int i, ret;
  398. /* calculate the number of delayed samples */
  399. for (i = 0; i < c->nb_streams; i++) {
  400. delayed_samples = FFMAX(delayed_samples,
  401. c->streams[i].delayed_samples + av_audio_fifo_size(c->sync_buffers[i]));
  402. }
  403. /* decode the header of the first sub-packet to find out the sample count */
  404. if (buf) {
  405. OpusPacket *pkt = &c->streams[0].packet;
  406. ret = ff_opus_parse_packet(pkt, buf, buf_size, c->nb_streams > 1);
  407. if (ret < 0) {
  408. av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
  409. return ret;
  410. }
  411. coded_samples += pkt->frame_count * pkt->frame_duration;
  412. c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config);
  413. }
  414. frame->nb_samples = coded_samples + delayed_samples;
  415. /* no input or buffered data => nothing to do */
  416. if (!frame->nb_samples) {
  417. *got_frame_ptr = 0;
  418. return 0;
  419. }
  420. /* setup the data buffers */
  421. ret = ff_get_buffer(avctx, frame, 0);
  422. if (ret < 0) {
  423. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  424. return ret;
  425. }
  426. frame->nb_samples = 0;
  427. memset(c->out, 0, c->nb_streams * 2 * sizeof(*c->out));
  428. for (i = 0; i < avctx->channels; i++) {
  429. ChannelMap *map = &c->channel_maps[i];
  430. if (!map->copy)
  431. c->out[2 * map->stream_idx + map->channel_idx] = (float*)frame->extended_data[i];
  432. }
  433. /* read the data from the sync buffers */
  434. for (i = 0; i < c->nb_streams; i++) {
  435. float **out = c->out + 2 * i;
  436. int sync_size = av_audio_fifo_size(c->sync_buffers[i]);
  437. float sync_dummy[32];
  438. int out_dummy = (!out[0]) | ((!out[1]) << 1);
  439. if (!out[0])
  440. out[0] = sync_dummy;
  441. if (!out[1])
  442. out[1] = sync_dummy;
  443. if (out_dummy && sync_size > FF_ARRAY_ELEMS(sync_dummy))
  444. return AVERROR_BUG;
  445. ret = av_audio_fifo_read(c->sync_buffers[i], (void**)out, sync_size);
  446. if (ret < 0)
  447. return ret;
  448. if (out_dummy & 1)
  449. out[0] = NULL;
  450. else
  451. out[0] += ret;
  452. if (out_dummy & 2)
  453. out[1] = NULL;
  454. else
  455. out[1] += ret;
  456. c->out_size[i] = frame->linesize[0] - ret * sizeof(float);
  457. }
  458. /* decode each sub-packet */
  459. for (i = 0; i < c->nb_streams; i++) {
  460. OpusStreamContext *s = &c->streams[i];
  461. if (i && buf) {
  462. ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->nb_streams - 1);
  463. if (ret < 0) {
  464. av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
  465. return ret;
  466. }
  467. if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
  468. av_log(avctx, AV_LOG_ERROR,
  469. "Mismatching coded sample count in substream %d.\n", i);
  470. return AVERROR_INVALIDDATA;
  471. }
  472. s->silk_samplerate = get_silk_samplerate(s->packet.config);
  473. }
  474. ret = opus_decode_subpacket(&c->streams[i], buf, s->packet.data_size,
  475. c->out + 2 * i, c->out_size[i], coded_samples);
  476. if (ret < 0)
  477. return ret;
  478. c->decoded_samples[i] = ret;
  479. decoded_samples = FFMIN(decoded_samples, ret);
  480. buf += s->packet.packet_size;
  481. buf_size -= s->packet.packet_size;
  482. }
  483. /* buffer the extra samples */
  484. for (i = 0; i < c->nb_streams; i++) {
  485. int buffer_samples = c->decoded_samples[i] - decoded_samples;
  486. if (buffer_samples) {
  487. float *buf[2] = { c->out[2 * i + 0] ? c->out[2 * i + 0] : (float*)frame->extended_data[0],
  488. c->out[2 * i + 1] ? c->out[2 * i + 1] : (float*)frame->extended_data[0] };
  489. buf[0] += decoded_samples;
  490. buf[1] += decoded_samples;
  491. ret = av_audio_fifo_write(c->sync_buffers[i], (void**)buf, buffer_samples);
  492. if (ret < 0)
  493. return ret;
  494. }
  495. }
  496. for (i = 0; i < avctx->channels; i++) {
  497. ChannelMap *map = &c->channel_maps[i];
  498. /* handle copied channels */
  499. if (map->copy) {
  500. memcpy(frame->extended_data[i],
  501. frame->extended_data[map->copy_idx],
  502. frame->linesize[0]);
  503. } else if (map->silence) {
  504. memset(frame->extended_data[i], 0, frame->linesize[0]);
  505. }
  506. if (c->gain_i && decoded_samples > 0) {
  507. c->fdsp.vector_fmul_scalar((float*)frame->extended_data[i],
  508. (float*)frame->extended_data[i],
  509. c->gain, FFALIGN(decoded_samples, 8));
  510. }
  511. }
  512. frame->nb_samples = decoded_samples;
  513. *got_frame_ptr = !!decoded_samples;
  514. return avpkt->size;
  515. }
  516. static av_cold void opus_decode_flush(AVCodecContext *ctx)
  517. {
  518. OpusContext *c = ctx->priv_data;
  519. int i;
  520. for (i = 0; i < c->nb_streams; i++) {
  521. OpusStreamContext *s = &c->streams[i];
  522. memset(&s->packet, 0, sizeof(s->packet));
  523. s->delayed_samples = 0;
  524. if (s->celt_delay)
  525. av_audio_fifo_drain(s->celt_delay, av_audio_fifo_size(s->celt_delay));
  526. avresample_close(s->avr);
  527. av_audio_fifo_drain(c->sync_buffers[i], av_audio_fifo_size(c->sync_buffers[i]));
  528. ff_silk_flush(s->silk);
  529. ff_celt_flush(s->celt);
  530. }
  531. }
  532. static av_cold int opus_decode_close(AVCodecContext *avctx)
  533. {
  534. OpusContext *c = avctx->priv_data;
  535. int i;
  536. for (i = 0; i < c->nb_streams; i++) {
  537. OpusStreamContext *s = &c->streams[i];
  538. ff_silk_free(&s->silk);
  539. ff_celt_free(&s->celt);
  540. av_freep(&s->out_dummy);
  541. s->out_dummy_allocated_size = 0;
  542. av_audio_fifo_free(s->celt_delay);
  543. avresample_free(&s->avr);
  544. }
  545. av_freep(&c->streams);
  546. if (c->sync_buffers) {
  547. for (i = 0; i < c->nb_streams; i++)
  548. av_audio_fifo_free(c->sync_buffers[i]);
  549. }
  550. av_freep(&c->sync_buffers);
  551. av_freep(&c->decoded_samples);
  552. av_freep(&c->out);
  553. av_freep(&c->out_size);
  554. c->nb_streams = 0;
  555. av_freep(&c->channel_maps);
  556. return 0;
  557. }
  558. static av_cold int opus_decode_init(AVCodecContext *avctx)
  559. {
  560. OpusContext *c = avctx->priv_data;
  561. int ret, i, j;
  562. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  563. avctx->sample_rate = 48000;
  564. avpriv_float_dsp_init(&c->fdsp, 0);
  565. /* find out the channel configuration */
  566. ret = ff_opus_parse_extradata(avctx, c);
  567. if (ret < 0)
  568. return ret;
  569. /* allocate and init each independent decoder */
  570. c->streams = av_mallocz_array(c->nb_streams, sizeof(*c->streams));
  571. c->out = av_mallocz_array(c->nb_streams, 2 * sizeof(*c->out));
  572. c->out_size = av_mallocz_array(c->nb_streams, sizeof(*c->out_size));
  573. c->sync_buffers = av_mallocz_array(c->nb_streams, sizeof(*c->sync_buffers));
  574. c->decoded_samples = av_mallocz_array(c->nb_streams, sizeof(*c->decoded_samples));
  575. if (!c->streams || !c->sync_buffers || !c->decoded_samples || !c->out || !c->out_size) {
  576. c->nb_streams = 0;
  577. ret = AVERROR(ENOMEM);
  578. goto fail;
  579. }
  580. for (i = 0; i < c->nb_streams; i++) {
  581. OpusStreamContext *s = &c->streams[i];
  582. uint64_t layout;
  583. s->output_channels = (i < c->nb_stereo_streams) ? 2 : 1;
  584. s->avctx = avctx;
  585. for (j = 0; j < s->output_channels; j++) {
  586. s->silk_output[j] = s->silk_buf[j];
  587. s->celt_output[j] = s->celt_buf[j];
  588. s->redundancy_output[j] = s->redundancy_buf[j];
  589. }
  590. s->fdsp = &c->fdsp;
  591. s->avr = avresample_alloc_context();
  592. if (!s->avr)
  593. goto fail;
  594. layout = (s->output_channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
  595. av_opt_set_int(s->avr, "in_sample_fmt", avctx->sample_fmt, 0);
  596. av_opt_set_int(s->avr, "out_sample_fmt", avctx->sample_fmt, 0);
  597. av_opt_set_int(s->avr, "in_channel_layout", layout, 0);
  598. av_opt_set_int(s->avr, "out_channel_layout", layout, 0);
  599. av_opt_set_int(s->avr, "out_sample_rate", avctx->sample_rate, 0);
  600. ret = ff_silk_init(avctx, &s->silk, s->output_channels);
  601. if (ret < 0)
  602. goto fail;
  603. ret = ff_celt_init(avctx, &s->celt, s->output_channels);
  604. if (ret < 0)
  605. goto fail;
  606. s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt,
  607. s->output_channels, 1024);
  608. if (!s->celt_delay) {
  609. ret = AVERROR(ENOMEM);
  610. goto fail;
  611. }
  612. c->sync_buffers[i] = av_audio_fifo_alloc(avctx->sample_fmt,
  613. s->output_channels, 32);
  614. if (!c->sync_buffers[i]) {
  615. ret = AVERROR(ENOMEM);
  616. goto fail;
  617. }
  618. }
  619. return 0;
  620. fail:
  621. opus_decode_close(avctx);
  622. return ret;
  623. }
  624. AVCodec ff_opus_decoder = {
  625. .name = "opus",
  626. .long_name = NULL_IF_CONFIG_SMALL("Opus"),
  627. .type = AVMEDIA_TYPE_AUDIO,
  628. .id = AV_CODEC_ID_OPUS,
  629. .priv_data_size = sizeof(OpusContext),
  630. .init = opus_decode_init,
  631. .close = opus_decode_close,
  632. .decode = opus_decode_packet,
  633. .flush = opus_decode_flush,
  634. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
  635. };