Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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