2
0

opusdec.c 24 KB

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