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.

761 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. static const uint16_t silk_frame_duration_ms[16] = {
  46. 10, 20, 40, 60,
  47. 10, 20, 40, 60,
  48. 10, 20, 40, 60,
  49. 10, 20,
  50. 10, 20,
  51. };
  52. /* number of samples of silence to feed to the resampler
  53. * at the beginning */
  54. static const int silk_resample_delay[] = {
  55. 4, 8, 11, 11, 11
  56. };
  57. static const uint8_t celt_band_end[] = { 13, 17, 17, 19, 21 };
  58. static int get_silk_samplerate(int config)
  59. {
  60. if (config < 4)
  61. return 8000;
  62. else if (config < 8)
  63. return 12000;
  64. return 16000;
  65. }
  66. /**
  67. * Range decoder
  68. */
  69. static int opus_rc_init(OpusRangeCoder *rc, const uint8_t *data, int size)
  70. {
  71. int ret = init_get_bits8(&rc->gb, data, size);
  72. if (ret < 0)
  73. return ret;
  74. rc->range = 128;
  75. rc->value = 127 - get_bits(&rc->gb, 7);
  76. rc->total_read_bits = 9;
  77. opus_rc_normalize(rc);
  78. return 0;
  79. }
  80. static void opus_raw_init(OpusRangeCoder *rc, const uint8_t *rightend,
  81. unsigned int bytes)
  82. {
  83. rc->rb.position = rightend;
  84. rc->rb.bytes = bytes;
  85. rc->rb.cachelen = 0;
  86. rc->rb.cacheval = 0;
  87. }
  88. static void opus_fade(float *out,
  89. const float *in1, const float *in2,
  90. const float *window, int len)
  91. {
  92. int i;
  93. for (i = 0; i < len; i++)
  94. out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]);
  95. }
  96. static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
  97. {
  98. int celt_size = av_audio_fifo_size(s->celt_delay);
  99. int ret, i;
  100. ret = swr_convert(s->swr,
  101. (uint8_t**)s->out, nb_samples,
  102. NULL, 0);
  103. if (ret < 0)
  104. return ret;
  105. else if (ret != nb_samples) {
  106. av_log(s->avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n",
  107. ret);
  108. return AVERROR_BUG;
  109. }
  110. if (celt_size) {
  111. if (celt_size != nb_samples) {
  112. av_log(s->avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n");
  113. return AVERROR_BUG;
  114. }
  115. av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, nb_samples);
  116. for (i = 0; i < s->output_channels; i++) {
  117. s->fdsp->vector_fmac_scalar(s->out[i],
  118. s->celt_output[i], 1.0,
  119. nb_samples);
  120. }
  121. }
  122. if (s->redundancy_idx) {
  123. for (i = 0; i < s->output_channels; i++)
  124. opus_fade(s->out[i], s->out[i],
  125. s->redundancy_output[i] + 120 + s->redundancy_idx,
  126. ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
  127. s->redundancy_idx = 0;
  128. }
  129. s->out[0] += nb_samples;
  130. s->out[1] += nb_samples;
  131. s->out_size -= nb_samples * sizeof(float);
  132. return 0;
  133. }
  134. static int opus_init_resample(OpusStreamContext *s)
  135. {
  136. static const float delay[16] = { 0.0 };
  137. const uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
  138. int ret;
  139. av_opt_set_int(s->swr, "in_sample_rate", s->silk_samplerate, 0);
  140. ret = swr_init(s->swr);
  141. if (ret < 0) {
  142. av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n");
  143. return ret;
  144. }
  145. ret = swr_convert(s->swr,
  146. NULL, 0,
  147. delayptr, 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 (!swr_is_initialized(s->swr)) {
  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 = swr_convert(s->swr,
  203. (uint8_t**)s->out, s->packet.frame_duration,
  204. (const uint8_t**)s->silk_output, samples);
  205. if (samples < 0) {
  206. av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n");
  207. return samples;
  208. }
  209. av_assert2((samples & 7) == 0);
  210. s->delayed_samples += s->packet.frame_duration - samples;
  211. } else
  212. ff_silk_flush(s->silk);
  213. // decode redundancy information
  214. consumed = opus_rc_tell(&s->rc);
  215. if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8)
  216. redundancy = opus_rc_p2model(&s->rc, 12);
  217. else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8)
  218. redundancy = 1;
  219. if (redundancy) {
  220. redundancy_pos = opus_rc_p2model(&s->rc, 1);
  221. if (s->packet.mode == OPUS_MODE_HYBRID)
  222. redundancy_size = opus_rc_unimodel(&s->rc, 256) + 2;
  223. else
  224. redundancy_size = size - (consumed + 7) / 8;
  225. size -= redundancy_size;
  226. if (size < 0) {
  227. av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n");
  228. return AVERROR_INVALIDDATA;
  229. }
  230. if (redundancy_pos) {
  231. ret = opus_decode_redundancy(s, data + size, redundancy_size);
  232. if (ret < 0)
  233. return ret;
  234. ff_celt_flush(s->celt);
  235. }
  236. }
  237. /* decode the CELT frame */
  238. if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
  239. float *out_tmp[2] = { s->out[0], s->out[1] };
  240. float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
  241. out_tmp : s->celt_output;
  242. int celt_output_samples = samples;
  243. int delay_samples = av_audio_fifo_size(s->celt_delay);
  244. if (delay_samples) {
  245. if (s->packet.mode == OPUS_MODE_HYBRID) {
  246. av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples);
  247. for (i = 0; i < s->output_channels; i++) {
  248. s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0,
  249. delay_samples);
  250. out_tmp[i] += delay_samples;
  251. }
  252. celt_output_samples -= delay_samples;
  253. } else {
  254. av_log(s->avctx, AV_LOG_WARNING,
  255. "Spurious CELT delay samples present.\n");
  256. av_audio_fifo_drain(s->celt_delay, delay_samples);
  257. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  258. return AVERROR_BUG;
  259. }
  260. }
  261. opus_raw_init(&s->rc, data + size, size);
  262. ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
  263. s->packet.stereo + 1,
  264. s->packet.frame_duration,
  265. (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
  266. celt_band_end[s->packet.bandwidth]);
  267. if (ret < 0)
  268. return ret;
  269. if (s->packet.mode == OPUS_MODE_HYBRID) {
  270. int celt_delay = s->packet.frame_duration - celt_output_samples;
  271. void *delaybuf[2] = { s->celt_output[0] + celt_output_samples,
  272. s->celt_output[1] + celt_output_samples };
  273. for (i = 0; i < s->output_channels; i++) {
  274. s->fdsp->vector_fmac_scalar(out_tmp[i],
  275. s->celt_output[i], 1.0,
  276. celt_output_samples);
  277. }
  278. ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay);
  279. if (ret < 0)
  280. return ret;
  281. }
  282. } else
  283. ff_celt_flush(s->celt);
  284. if (s->redundancy_idx) {
  285. for (i = 0; i < s->output_channels; i++)
  286. opus_fade(s->out[i], s->out[i],
  287. s->redundancy_output[i] + 120 + s->redundancy_idx,
  288. ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
  289. s->redundancy_idx = 0;
  290. }
  291. if (redundancy) {
  292. if (!redundancy_pos) {
  293. ff_celt_flush(s->celt);
  294. ret = opus_decode_redundancy(s, data + size, redundancy_size);
  295. if (ret < 0)
  296. return ret;
  297. for (i = 0; i < s->output_channels; i++) {
  298. opus_fade(s->out[i] + samples - 120 + delayed_samples,
  299. s->out[i] + samples - 120 + delayed_samples,
  300. s->redundancy_output[i] + 120,
  301. ff_celt_window2, 120 - delayed_samples);
  302. if (delayed_samples)
  303. s->redundancy_idx = 120 - delayed_samples;
  304. }
  305. } else {
  306. for (i = 0; i < s->output_channels; i++) {
  307. memcpy(s->out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float));
  308. opus_fade(s->out[i] + 120 + delayed_samples,
  309. s->redundancy_output[i] + 120,
  310. s->out[i] + 120 + delayed_samples,
  311. ff_celt_window2, 120);
  312. }
  313. }
  314. }
  315. return samples;
  316. }
  317. static int opus_decode_subpacket(OpusStreamContext *s,
  318. const uint8_t *buf, int buf_size,
  319. float **out, int out_size,
  320. int nb_samples)
  321. {
  322. int output_samples = 0;
  323. int flush_needed = 0;
  324. int i, j, ret;
  325. s->out[0] = out[0];
  326. s->out[1] = out[1];
  327. s->out_size = out_size;
  328. /* check if we need to flush the resampler */
  329. if (swr_is_initialized(s->swr)) {
  330. if (buf) {
  331. int64_t cur_samplerate;
  332. av_opt_get_int(s->swr, "in_sample_rate", 0, &cur_samplerate);
  333. flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
  334. } else {
  335. flush_needed = !!s->delayed_samples;
  336. }
  337. }
  338. if (!buf && !flush_needed)
  339. return 0;
  340. /* use dummy output buffers if the channel is not mapped to anything */
  341. if (!s->out[0] ||
  342. (s->output_channels == 2 && !s->out[1])) {
  343. av_fast_malloc(&s->out_dummy, &s->out_dummy_allocated_size, s->out_size);
  344. if (!s->out_dummy)
  345. return AVERROR(ENOMEM);
  346. if (!s->out[0])
  347. s->out[0] = s->out_dummy;
  348. if (!s->out[1])
  349. s->out[1] = s->out_dummy;
  350. }
  351. /* flush the resampler if necessary */
  352. if (flush_needed) {
  353. ret = opus_flush_resample(s, s->delayed_samples);
  354. if (ret < 0) {
  355. av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
  356. return ret;
  357. }
  358. swr_close(s->swr);
  359. output_samples += s->delayed_samples;
  360. s->delayed_samples = 0;
  361. if (!buf)
  362. goto finish;
  363. }
  364. /* decode all the frames in the packet */
  365. for (i = 0; i < s->packet.frame_count; i++) {
  366. int size = s->packet.frame_size[i];
  367. int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
  368. if (samples < 0) {
  369. av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
  370. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  371. return samples;
  372. for (j = 0; j < s->output_channels; j++)
  373. memset(s->out[j], 0, s->packet.frame_duration * sizeof(float));
  374. samples = s->packet.frame_duration;
  375. }
  376. output_samples += samples;
  377. for (j = 0; j < s->output_channels; j++)
  378. s->out[j] += samples;
  379. s->out_size -= samples * sizeof(float);
  380. }
  381. finish:
  382. s->out[0] = s->out[1] = NULL;
  383. s->out_size = 0;
  384. return output_samples;
  385. }
  386. static int opus_decode_packet(AVCodecContext *avctx, void *data,
  387. int *got_frame_ptr, AVPacket *avpkt)
  388. {
  389. OpusContext *c = avctx->priv_data;
  390. AVFrame *frame = data;
  391. const uint8_t *buf = avpkt->data;
  392. int buf_size = avpkt->size;
  393. int coded_samples = 0;
  394. int decoded_samples = INT_MAX;
  395. int delayed_samples = 0;
  396. int i, ret;
  397. /* calculate the number of delayed samples */
  398. for (i = 0; i < c->nb_streams; i++) {
  399. OpusStreamContext *s = &c->streams[i];
  400. s->out[0] =
  401. s->out[1] = NULL;
  402. delayed_samples = FFMAX(delayed_samples,
  403. s->delayed_samples + av_audio_fifo_size(c->sync_buffers[i]));
  404. }
  405. /* decode the header of the first sub-packet to find out the sample count */
  406. if (buf) {
  407. OpusPacket *pkt = &c->streams[0].packet;
  408. ret = ff_opus_parse_packet(pkt, buf, buf_size, c->nb_streams > 1);
  409. if (ret < 0) {
  410. av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
  411. return ret;
  412. }
  413. coded_samples += pkt->frame_count * pkt->frame_duration;
  414. c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config);
  415. }
  416. frame->nb_samples = coded_samples + delayed_samples;
  417. /* no input or buffered data => nothing to do */
  418. if (!frame->nb_samples) {
  419. *got_frame_ptr = 0;
  420. return 0;
  421. }
  422. /* setup the data buffers */
  423. ret = ff_get_buffer(avctx, frame, 0);
  424. if (ret < 0)
  425. return ret;
  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. swr_close(s->swr);
  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. swr_free(&s->swr);
  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. av_freep(&c->fdsp);
  557. return 0;
  558. }
  559. static av_cold int opus_decode_init(AVCodecContext *avctx)
  560. {
  561. OpusContext *c = avctx->priv_data;
  562. int ret, i, j;
  563. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  564. avctx->sample_rate = 48000;
  565. c->fdsp = avpriv_float_dsp_alloc(0);
  566. if (!c->fdsp)
  567. return AVERROR(ENOMEM);
  568. /* find out the channel configuration */
  569. ret = ff_opus_parse_extradata(avctx, c);
  570. if (ret < 0) {
  571. av_freep(&c->channel_maps);
  572. av_freep(&c->fdsp);
  573. return ret;
  574. }
  575. /* allocate and init each independent decoder */
  576. c->streams = av_mallocz_array(c->nb_streams, sizeof(*c->streams));
  577. c->out = av_mallocz_array(c->nb_streams, 2 * sizeof(*c->out));
  578. c->out_size = av_mallocz_array(c->nb_streams, sizeof(*c->out_size));
  579. c->sync_buffers = av_mallocz_array(c->nb_streams, sizeof(*c->sync_buffers));
  580. c->decoded_samples = av_mallocz_array(c->nb_streams, sizeof(*c->decoded_samples));
  581. if (!c->streams || !c->sync_buffers || !c->decoded_samples || !c->out || !c->out_size) {
  582. c->nb_streams = 0;
  583. ret = AVERROR(ENOMEM);
  584. goto fail;
  585. }
  586. for (i = 0; i < c->nb_streams; i++) {
  587. OpusStreamContext *s = &c->streams[i];
  588. uint64_t layout;
  589. s->output_channels = (i < c->nb_stereo_streams) ? 2 : 1;
  590. s->avctx = avctx;
  591. for (j = 0; j < s->output_channels; j++) {
  592. s->silk_output[j] = s->silk_buf[j];
  593. s->celt_output[j] = s->celt_buf[j];
  594. s->redundancy_output[j] = s->redundancy_buf[j];
  595. }
  596. s->fdsp = c->fdsp;
  597. s->swr =swr_alloc();
  598. if (!s->swr)
  599. goto fail;
  600. layout = (s->output_channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
  601. av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
  602. av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
  603. av_opt_set_int(s->swr, "in_channel_layout", layout, 0);
  604. av_opt_set_int(s->swr, "out_channel_layout", layout, 0);
  605. av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
  606. av_opt_set_int(s->swr, "filter_size", 16, 0);
  607. ret = ff_silk_init(avctx, &s->silk, s->output_channels);
  608. if (ret < 0)
  609. goto fail;
  610. ret = ff_celt_init(avctx, &s->celt, s->output_channels);
  611. if (ret < 0)
  612. goto fail;
  613. s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt,
  614. s->output_channels, 1024);
  615. if (!s->celt_delay) {
  616. ret = AVERROR(ENOMEM);
  617. goto fail;
  618. }
  619. c->sync_buffers[i] = av_audio_fifo_alloc(avctx->sample_fmt,
  620. s->output_channels, 32);
  621. if (!c->sync_buffers[i]) {
  622. ret = AVERROR(ENOMEM);
  623. goto fail;
  624. }
  625. }
  626. return 0;
  627. fail:
  628. opus_decode_close(avctx);
  629. return ret;
  630. }
  631. AVCodec ff_opus_decoder = {
  632. .name = "opus",
  633. .long_name = NULL_IF_CONFIG_SMALL("Opus"),
  634. .type = AVMEDIA_TYPE_AUDIO,
  635. .id = AV_CODEC_ID_OPUS,
  636. .priv_data_size = sizeof(OpusContext),
  637. .init = opus_decode_init,
  638. .close = opus_decode_close,
  639. .decode = opus_decode_packet,
  640. .flush = opus_decode_flush,
  641. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
  642. };