LCOV - code coverage report
Current view: top level - libavcodec - opusdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 293 410 71.5 %
Date: 2017-12-14 08:27:08 Functions: 10 11 90.9 %

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

Generated by: LCOV version 1.13