LCOV - code coverage report
Current view: top level - libavcodec - qdm2.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 471 841 56.0 %
Date: 2017-12-17 04:34:43 Functions: 27 31 87.1 %

          Line data    Source code
       1             : /*
       2             :  * QDM2 compatible decoder
       3             :  * Copyright (c) 2003 Ewald Snel
       4             :  * Copyright (c) 2005 Benjamin Larsson
       5             :  * Copyright (c) 2005 Alex Beregszaszi
       6             :  * Copyright (c) 2005 Roberto Togni
       7             :  *
       8             :  * This file is part of FFmpeg.
       9             :  *
      10             :  * FFmpeg is free software; you can redistribute it and/or
      11             :  * modify it under the terms of the GNU Lesser General Public
      12             :  * License as published by the Free Software Foundation; either
      13             :  * version 2.1 of the License, or (at your option) any later version.
      14             :  *
      15             :  * FFmpeg is distributed in the hope that it will be useful,
      16             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      17             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      18             :  * Lesser General Public License for more details.
      19             :  *
      20             :  * You should have received a copy of the GNU Lesser General Public
      21             :  * License along with FFmpeg; if not, write to the Free Software
      22             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      23             :  */
      24             : 
      25             : /**
      26             :  * @file
      27             :  * QDM2 decoder
      28             :  * @author Ewald Snel, Benjamin Larsson, Alex Beregszaszi, Roberto Togni
      29             :  *
      30             :  * The decoder is not perfect yet, there are still some distortions
      31             :  * especially on files encoded with 16 or 8 subbands.
      32             :  */
      33             : 
      34             : #include <math.h>
      35             : #include <stddef.h>
      36             : #include <stdio.h>
      37             : 
      38             : #include "libavutil/channel_layout.h"
      39             : 
      40             : #define BITSTREAM_READER_LE
      41             : #include "avcodec.h"
      42             : #include "get_bits.h"
      43             : #include "bytestream.h"
      44             : #include "internal.h"
      45             : #include "mpegaudio.h"
      46             : #include "mpegaudiodsp.h"
      47             : #include "rdft.h"
      48             : 
      49             : #include "qdm2_tablegen.h"
      50             : 
      51             : #define QDM2_LIST_ADD(list, size, packet) \
      52             : do { \
      53             :       if (size > 0) { \
      54             :     list[size - 1].next = &list[size]; \
      55             :       } \
      56             :       list[size].packet = packet; \
      57             :       list[size].next = NULL; \
      58             :       size++; \
      59             : } while(0)
      60             : 
      61             : // Result is 8, 16 or 30
      62             : #define QDM2_SB_USED(sub_sampling) (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))
      63             : 
      64             : #define FIX_NOISE_IDX(noise_idx) \
      65             :   if ((noise_idx) >= 3840) \
      66             :     (noise_idx) -= 3840; \
      67             : 
      68             : #define SB_DITHERING_NOISE(sb,noise_idx) (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])
      69             : 
      70             : #define SAMPLES_NEEDED \
      71             :      av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n");
      72             : 
      73             : #define SAMPLES_NEEDED_2(why) \
      74             :      av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why);
      75             : 
      76             : #define QDM2_MAX_FRAME_SIZE 512
      77             : 
      78             : typedef int8_t sb_int8_array[2][30][64];
      79             : 
      80             : /**
      81             :  * Subpacket
      82             :  */
      83             : typedef struct QDM2SubPacket {
      84             :     int type;            ///< subpacket type
      85             :     unsigned int size;   ///< subpacket size
      86             :     const uint8_t *data; ///< pointer to subpacket data (points to input data buffer, it's not a private copy)
      87             : } QDM2SubPacket;
      88             : 
      89             : /**
      90             :  * A node in the subpacket list
      91             :  */
      92             : typedef struct QDM2SubPNode {
      93             :     QDM2SubPacket *packet;      ///< packet
      94             :     struct QDM2SubPNode *next; ///< pointer to next packet in the list, NULL if leaf node
      95             : } QDM2SubPNode;
      96             : 
      97             : typedef struct QDM2Complex {
      98             :     float re;
      99             :     float im;
     100             : } QDM2Complex;
     101             : 
     102             : typedef struct FFTTone {
     103             :     float level;
     104             :     QDM2Complex *complex;
     105             :     const float *table;
     106             :     int   phase;
     107             :     int   phase_shift;
     108             :     int   duration;
     109             :     short time_index;
     110             :     short cutoff;
     111             : } FFTTone;
     112             : 
     113             : typedef struct FFTCoefficient {
     114             :     int16_t sub_packet;
     115             :     uint8_t channel;
     116             :     int16_t offset;
     117             :     int16_t exp;
     118             :     uint8_t phase;
     119             : } FFTCoefficient;
     120             : 
     121             : typedef struct QDM2FFT {
     122             :     DECLARE_ALIGNED(32, QDM2Complex, complex)[MPA_MAX_CHANNELS][256];
     123             : } QDM2FFT;
     124             : 
     125             : /**
     126             :  * QDM2 decoder context
     127             :  */
     128             : typedef struct QDM2Context {
     129             :     /// Parameters from codec header, do not change during playback
     130             :     int nb_channels;         ///< number of channels
     131             :     int channels;            ///< number of channels
     132             :     int group_size;          ///< size of frame group (16 frames per group)
     133             :     int fft_size;            ///< size of FFT, in complex numbers
     134             :     int checksum_size;       ///< size of data block, used also for checksum
     135             : 
     136             :     /// Parameters built from header parameters, do not change during playback
     137             :     int group_order;         ///< order of frame group
     138             :     int fft_order;           ///< order of FFT (actually fftorder+1)
     139             :     int frame_size;          ///< size of data frame
     140             :     int frequency_range;
     141             :     int sub_sampling;        ///< subsampling: 0=25%, 1=50%, 2=100% */
     142             :     int coeff_per_sb_select; ///< selector for "num. of coeffs. per subband" tables. Can be 0, 1, 2
     143             :     int cm_table_select;     ///< selector for "coding method" tables. Can be 0, 1 (from init: 0-4)
     144             : 
     145             :     /// Packets and packet lists
     146             :     QDM2SubPacket sub_packets[16];      ///< the packets themselves
     147             :     QDM2SubPNode sub_packet_list_A[16]; ///< list of all packets
     148             :     QDM2SubPNode sub_packet_list_B[16]; ///< FFT packets B are on list
     149             :     int sub_packets_B;                  ///< number of packets on 'B' list
     150             :     QDM2SubPNode sub_packet_list_C[16]; ///< packets with errors?
     151             :     QDM2SubPNode sub_packet_list_D[16]; ///< DCT packets
     152             : 
     153             :     /// FFT and tones
     154             :     FFTTone fft_tones[1000];
     155             :     int fft_tone_start;
     156             :     int fft_tone_end;
     157             :     FFTCoefficient fft_coefs[1000];
     158             :     int fft_coefs_index;
     159             :     int fft_coefs_min_index[5];
     160             :     int fft_coefs_max_index[5];
     161             :     int fft_level_exp[6];
     162             :     RDFTContext rdft_ctx;
     163             :     QDM2FFT fft;
     164             : 
     165             :     /// I/O data
     166             :     const uint8_t *compressed_data;
     167             :     int compressed_size;
     168             :     float output_buffer[QDM2_MAX_FRAME_SIZE * MPA_MAX_CHANNELS * 2];
     169             : 
     170             :     /// Synthesis filter
     171             :     MPADSPContext mpadsp;
     172             :     DECLARE_ALIGNED(32, float, synth_buf)[MPA_MAX_CHANNELS][512*2];
     173             :     int synth_buf_offset[MPA_MAX_CHANNELS];
     174             :     DECLARE_ALIGNED(32, float, sb_samples)[MPA_MAX_CHANNELS][128][SBLIMIT];
     175             :     DECLARE_ALIGNED(32, float, samples)[MPA_MAX_CHANNELS * MPA_FRAME_SIZE];
     176             : 
     177             :     /// Mixed temporary data used in decoding
     178             :     float tone_level[MPA_MAX_CHANNELS][30][64];
     179             :     int8_t coding_method[MPA_MAX_CHANNELS][30][64];
     180             :     int8_t quantized_coeffs[MPA_MAX_CHANNELS][10][8];
     181             :     int8_t tone_level_idx_base[MPA_MAX_CHANNELS][30][8];
     182             :     int8_t tone_level_idx_hi1[MPA_MAX_CHANNELS][3][8][8];
     183             :     int8_t tone_level_idx_mid[MPA_MAX_CHANNELS][26][8];
     184             :     int8_t tone_level_idx_hi2[MPA_MAX_CHANNELS][26];
     185             :     int8_t tone_level_idx[MPA_MAX_CHANNELS][30][64];
     186             :     int8_t tone_level_idx_temp[MPA_MAX_CHANNELS][30][64];
     187             : 
     188             :     // Flags
     189             :     int has_errors;         ///< packet has errors
     190             :     int superblocktype_2_3; ///< select fft tables and some algorithm based on superblock type
     191             :     int do_synth_filter;    ///< used to perform or skip synthesis filter
     192             : 
     193             :     int sub_packet;
     194             :     int noise_idx; ///< index for dithering noise table
     195             : } QDM2Context;
     196             : 
     197             : static const int switchtable[23] = {
     198             :     0, 5, 1, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 4
     199             : };
     200             : 
     201       66888 : static int qdm2_get_vlc(GetBitContext *gb, const VLC *vlc, int flag, int depth)
     202             : {
     203             :     int value;
     204             : 
     205       66888 :     value = get_vlc2(gb, vlc->table, vlc->bits, depth);
     206             : 
     207             :     /* stage-2, 3 bits exponent escape sequence */
     208       66888 :     if (value-- == 0)
     209         555 :         value = get_bits(gb, get_bits(gb, 3) + 1);
     210             : 
     211             :     /* stage-3, optional */
     212       66888 :     if (flag) {
     213             :         int tmp;
     214             : 
     215       18134 :         if (value >= 60) {
     216           0 :             av_log(NULL, AV_LOG_ERROR, "value %d in qdm2_get_vlc too large\n", value);
     217           0 :             return 0;
     218             :         }
     219             : 
     220       18134 :         tmp= vlc_stage3_values[value];
     221             : 
     222       18134 :         if ((value & ~3) > 0)
     223       14090 :             tmp += get_bits(gb, (value >> 2));
     224       18134 :         value = tmp;
     225             :     }
     226             : 
     227       66888 :     return value;
     228             : }
     229             : 
     230       10896 : static int qdm2_get_se_vlc(const VLC *vlc, GetBitContext *gb, int depth)
     231             : {
     232       10896 :     int value = qdm2_get_vlc(gb, vlc, 0, depth);
     233             : 
     234       10896 :     return (value & 1) ? ((value + 1) >> 1) : -(value >> 1);
     235             : }
     236             : 
     237             : /**
     238             :  * QDM2 checksum
     239             :  *
     240             :  * @param data      pointer to data to be checksummed
     241             :  * @param length    data length
     242             :  * @param value     checksum value
     243             :  *
     244             :  * @return          0 if checksum is OK
     245             :  */
     246         138 : static uint16_t qdm2_packet_checksum(const uint8_t *data, int length, int value)
     247             : {
     248             :     int i;
     249             : 
     250       51198 :     for (i = 0; i < length; i++)
     251       51060 :         value -= data[i];
     252             : 
     253         138 :     return (uint16_t)(value & 0xffff);
     254             : }
     255             : 
     256             : /**
     257             :  * Fill a QDM2SubPacket structure with packet type, size, and data pointer.
     258             :  *
     259             :  * @param gb            bitreader context
     260             :  * @param sub_packet    packet under analysis
     261             :  */
     262        1104 : static void qdm2_decode_sub_packet_header(GetBitContext *gb,
     263             :                                           QDM2SubPacket *sub_packet)
     264             : {
     265        1104 :     sub_packet->type = get_bits(gb, 8);
     266             : 
     267        1104 :     if (sub_packet->type == 0) {
     268         138 :         sub_packet->size = 0;
     269         138 :         sub_packet->data = NULL;
     270             :     } else {
     271         966 :         sub_packet->size = get_bits(gb, 8);
     272             : 
     273         966 :         if (sub_packet->type & 0x80) {
     274         138 :             sub_packet->size <<= 8;
     275         138 :             sub_packet->size  |= get_bits(gb, 8);
     276         138 :             sub_packet->type  &= 0x7f;
     277             :         }
     278             : 
     279         966 :         if (sub_packet->type == 0x7f)
     280           0 :             sub_packet->type |= (get_bits(gb, 8) << 8);
     281             : 
     282             :         // FIXME: this depends on bitreader-internal data
     283         966 :         sub_packet->data = &gb->buffer[get_bits_count(gb) / 8];
     284             :     }
     285             : 
     286        1104 :     av_log(NULL, AV_LOG_DEBUG, "Subpacket: type=%d size=%d start_offs=%x\n",
     287        1104 :            sub_packet->type, sub_packet->size, get_bits_count(gb) / 8);
     288        1104 : }
     289             : 
     290             : /**
     291             :  * Return node pointer to first packet of requested type in list.
     292             :  *
     293             :  * @param list    list of subpackets to be scanned
     294             :  * @param type    type of searched subpacket
     295             :  * @return        node pointer for subpacket if found, else NULL
     296             :  */
     297         552 : static QDM2SubPNode *qdm2_search_subpacket_type_in_list(QDM2SubPNode *list,
     298             :                                                         int type)
     299             : {
     300        1518 :     while (list && list->packet) {
     301         552 :         if (list->packet->type == type)
     302         138 :             return list;
     303         414 :         list = list->next;
     304             :     }
     305         414 :     return NULL;
     306             : }
     307             : 
     308             : /**
     309             :  * Replace 8 elements with their average value.
     310             :  * Called by qdm2_decode_superblock before starting subblock decoding.
     311             :  *
     312             :  * @param q       context
     313             :  */
     314         138 : static void average_quantized_coeffs(QDM2Context *q)
     315             : {
     316             :     int i, j, n, ch, sum;
     317             : 
     318         138 :     n = coeff_per_sb_for_avg[q->coeff_per_sb_select][QDM2_SB_USED(q->sub_sampling) - 1] + 1;
     319             : 
     320         414 :     for (ch = 0; ch < q->nb_channels; ch++)
     321        3036 :         for (i = 0; i < n; i++) {
     322        2760 :             sum = 0;
     323             : 
     324       24840 :             for (j = 0; j < 8; j++)
     325       22080 :                 sum += q->quantized_coeffs[ch][i][j];
     326             : 
     327        2760 :             sum /= 8;
     328        2760 :             if (sum > 0)
     329        2466 :                 sum--;
     330             : 
     331       24840 :             for (j = 0; j < 8; j++)
     332       22080 :                 q->quantized_coeffs[ch][i][j] = sum;
     333             :         }
     334         138 : }
     335             : 
     336             : /**
     337             :  * Build subband samples with noise weighted by q->tone_level.
     338             :  * Called by synthfilt_build_sb_samples.
     339             :  *
     340             :  * @param q     context
     341             :  * @param sb    subband index
     342             :  */
     343        4140 : static void build_sb_samples_from_noise(QDM2Context *q, int sb)
     344             : {
     345             :     int ch, j;
     346             : 
     347        4140 :     FIX_NOISE_IDX(q->noise_idx);
     348             : 
     349        4140 :     if (!q->nb_channels)
     350           0 :         return;
     351             : 
     352       12420 :     for (ch = 0; ch < q->nb_channels; ch++) {
     353      538200 :         for (j = 0; j < 64; j++) {
     354     1059840 :             q->sb_samples[ch][j * 2][sb] =
     355     1059840 :                 SB_DITHERING_NOISE(sb, q->noise_idx) * q->tone_level[ch][sb][j];
     356     1059840 :             q->sb_samples[ch][j * 2 + 1][sb] =
     357     1059840 :                 SB_DITHERING_NOISE(sb, q->noise_idx) * q->tone_level[ch][sb][j];
     358             :         }
     359             :     }
     360             : }
     361             : 
     362             : /**
     363             :  * Called while processing data from subpackets 11 and 12.
     364             :  * Used after making changes to coding_method array.
     365             :  *
     366             :  * @param sb               subband index
     367             :  * @param channels         number of channels
     368             :  * @param coding_method    q->coding_method[0][0][0]
     369             :  */
     370           0 : static int fix_coding_method_array(int sb, int channels,
     371             :                                    sb_int8_array coding_method)
     372             : {
     373             :     int j, k;
     374             :     int ch;
     375             :     int run, case_val;
     376             : 
     377           0 :     for (ch = 0; ch < channels; ch++) {
     378           0 :         for (j = 0; j < 64; ) {
     379           0 :             if (coding_method[ch][sb][j] < 8)
     380           0 :                 return -1;
     381           0 :             if ((coding_method[ch][sb][j] - 8) > 22) {
     382           0 :                 run      = 1;
     383           0 :                 case_val = 8;
     384             :             } else {
     385           0 :                 switch (switchtable[coding_method[ch][sb][j] - 8]) {
     386           0 :                 case 0: run  = 10;
     387           0 :                     case_val = 10;
     388           0 :                     break;
     389           0 :                 case 1: run  = 1;
     390           0 :                     case_val = 16;
     391           0 :                     break;
     392           0 :                 case 2: run  = 5;
     393           0 :                     case_val = 24;
     394           0 :                     break;
     395           0 :                 case 3: run  = 3;
     396           0 :                     case_val = 30;
     397           0 :                     break;
     398           0 :                 case 4: run  = 1;
     399           0 :                     case_val = 30;
     400           0 :                     break;
     401           0 :                 case 5: run  = 1;
     402           0 :                     case_val = 8;
     403           0 :                     break;
     404           0 :                 default: run = 1;
     405           0 :                     case_val = 8;
     406           0 :                     break;
     407             :                 }
     408             :             }
     409           0 :             for (k = 0; k < run; k++) {
     410           0 :                 if (j + k < 128) {
     411           0 :                     if (coding_method[ch][sb + (j + k) / 64][(j + k) % 64] > coding_method[ch][sb][j]) {
     412           0 :                         if (k > 0) {
     413           0 :                             SAMPLES_NEEDED
     414             :                             //not debugged, almost never used
     415           0 :                             memset(&coding_method[ch][sb][j + k], case_val,
     416             :                                    k *sizeof(int8_t));
     417           0 :                             memset(&coding_method[ch][sb][j + k], case_val,
     418             :                                    3 * sizeof(int8_t));
     419             :                         }
     420             :                     }
     421             :                 }
     422             :             }
     423           0 :             j += run;
     424             :         }
     425             :     }
     426           0 :     return 0;
     427             : }
     428             : 
     429             : /**
     430             :  * Related to synthesis filter
     431             :  * Called by process_subpacket_10
     432             :  *
     433             :  * @param q       context
     434             :  * @param flag    1 if called after getting data from subpacket 10, 0 if no subpacket 10
     435             :  */
     436         138 : static void fill_tone_level_array(QDM2Context *q, int flag)
     437             : {
     438             :     int i, sb, ch, sb_used;
     439             :     int tmp, tab;
     440             : 
     441         414 :     for (ch = 0; ch < q->nb_channels; ch++)
     442        8556 :         for (sb = 0; sb < 30; sb++)
     443       74520 :             for (i = 0; i < 8; i++) {
     444       66240 :                 if ((tab=coeff_per_sb_for_dequant[q->coeff_per_sb_select][sb]) < (last_coeff[q->coeff_per_sb_select] - 1))
     445      105984 :                     tmp = q->quantized_coeffs[ch][tab + 1][i] * dequant_table[q->coeff_per_sb_select][tab + 1][sb]+
     446       52992 :                           q->quantized_coeffs[ch][tab][i] * dequant_table[q->coeff_per_sb_select][tab][sb];
     447             :                 else
     448       13248 :                     tmp = q->quantized_coeffs[ch][tab][i] * dequant_table[q->coeff_per_sb_select][tab][sb];
     449       66240 :                 if(tmp < 0)
     450           0 :                     tmp += 0xff;
     451       66240 :                 q->tone_level_idx_base[ch][sb][i] = (tmp / 256) & 0xff;
     452             :             }
     453             : 
     454         138 :     sb_used = QDM2_SB_USED(q->sub_sampling);
     455             : 
     456         276 :     if ((q->superblocktype_2_3 != 0) && !flag) {
     457        4278 :         for (sb = 0; sb < sb_used; sb++)
     458       12420 :             for (ch = 0; ch < q->nb_channels; ch++)
     459      538200 :                 for (i = 0; i < 64; i++) {
     460      529920 :                     q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
     461      529920 :                     if (q->tone_level_idx[ch][sb][i] < 0)
     462           0 :                         q->tone_level[ch][sb][i] = 0;
     463             :                     else
     464      529920 :                         q->tone_level[ch][sb][i] = fft_tone_level_table[0][q->tone_level_idx[ch][sb][i] & 0x3f];
     465             :                 }
     466             :     } else {
     467           0 :         tab = q->superblocktype_2_3 ? 0 : 1;
     468           0 :         for (sb = 0; sb < sb_used; sb++) {
     469           0 :             if ((sb >= 4) && (sb <= 23)) {
     470           0 :                 for (ch = 0; ch < q->nb_channels; ch++)
     471           0 :                     for (i = 0; i < 64; i++) {
     472           0 :                         tmp = q->tone_level_idx_base[ch][sb][i / 8] -
     473           0 :                               q->tone_level_idx_hi1[ch][sb / 8][i / 8][i % 8] -
     474           0 :                               q->tone_level_idx_mid[ch][sb - 4][i / 8] -
     475           0 :                               q->tone_level_idx_hi2[ch][sb - 4];
     476           0 :                         q->tone_level_idx[ch][sb][i] = tmp & 0xff;
     477           0 :                         if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
     478           0 :                             q->tone_level[ch][sb][i] = 0;
     479             :                         else
     480           0 :                             q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
     481             :                 }
     482             :             } else {
     483           0 :                 if (sb > 4) {
     484           0 :                     for (ch = 0; ch < q->nb_channels; ch++)
     485           0 :                         for (i = 0; i < 64; i++) {
     486           0 :                             tmp = q->tone_level_idx_base[ch][sb][i / 8] -
     487           0 :                                   q->tone_level_idx_hi1[ch][2][i / 8][i % 8] -
     488           0 :                                   q->tone_level_idx_hi2[ch][sb - 4];
     489           0 :                             q->tone_level_idx[ch][sb][i] = tmp & 0xff;
     490           0 :                             if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
     491           0 :                                 q->tone_level[ch][sb][i] = 0;
     492             :                             else
     493           0 :                                 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
     494             :                     }
     495             :                 } else {
     496           0 :                     for (ch = 0; ch < q->nb_channels; ch++)
     497           0 :                         for (i = 0; i < 64; i++) {
     498           0 :                             tmp = q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
     499           0 :                             if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
     500           0 :                                 q->tone_level[ch][sb][i] = 0;
     501             :                             else
     502           0 :                                 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
     503             :                         }
     504             :                 }
     505             :             }
     506             :         }
     507             :     }
     508         138 : }
     509             : 
     510             : /**
     511             :  * Related to synthesis filter
     512             :  * Called by process_subpacket_11
     513             :  * c is built with data from subpacket 11
     514             :  * Most of this function is used only if superblock_type_2_3 == 0,
     515             :  * never seen it in samples.
     516             :  *
     517             :  * @param tone_level_idx
     518             :  * @param tone_level_idx_temp
     519             :  * @param coding_method        q->coding_method[0][0][0]
     520             :  * @param nb_channels          number of channels
     521             :  * @param c                    coming from subpacket 11, passed as 8*c
     522             :  * @param superblocktype_2_3   flag based on superblock packet type
     523             :  * @param cm_table_select      q->cm_table_select
     524             :  */
     525           0 : static void fill_coding_method_array(sb_int8_array tone_level_idx,
     526             :                                      sb_int8_array tone_level_idx_temp,
     527             :                                      sb_int8_array coding_method,
     528             :                                      int nb_channels,
     529             :                                      int c, int superblocktype_2_3,
     530             :                                      int cm_table_select)
     531             : {
     532             :     int ch, sb, j;
     533             :     int tmp, acc, esp_40, comp;
     534             :     int add1, add2, add3, add4;
     535             :     int64_t multres;
     536             : 
     537           0 :     if (!superblocktype_2_3) {
     538             :         /* This case is untested, no samples available */
     539           0 :         avpriv_request_sample(NULL, "!superblocktype_2_3");
     540           0 :         return;
     541             :         for (ch = 0; ch < nb_channels; ch++) {
     542             :             for (sb = 0; sb < 30; sb++) {
     543             :                 for (j = 1; j < 63; j++) {  // The loop only iterates to 63 so the code doesn't overflow the buffer
     544             :                     add1 = tone_level_idx[ch][sb][j] - 10;
     545             :                     if (add1 < 0)
     546             :                         add1 = 0;
     547             :                     add2 = add3 = add4 = 0;
     548             :                     if (sb > 1) {
     549             :                         add2 = tone_level_idx[ch][sb - 2][j] + tone_level_idx_offset_table[sb][0] - 6;
     550             :                         if (add2 < 0)
     551             :                             add2 = 0;
     552             :                     }
     553             :                     if (sb > 0) {
     554             :                         add3 = tone_level_idx[ch][sb - 1][j] + tone_level_idx_offset_table[sb][1] - 6;
     555             :                         if (add3 < 0)
     556             :                             add3 = 0;
     557             :                     }
     558             :                     if (sb < 29) {
     559             :                         add4 = tone_level_idx[ch][sb + 1][j] + tone_level_idx_offset_table[sb][3] - 6;
     560             :                         if (add4 < 0)
     561             :                             add4 = 0;
     562             :                     }
     563             :                     tmp = tone_level_idx[ch][sb][j + 1] * 2 - add4 - add3 - add2 - add1;
     564             :                     if (tmp < 0)
     565             :                         tmp = 0;
     566             :                     tone_level_idx_temp[ch][sb][j + 1] = tmp & 0xff;
     567             :                 }
     568             :                 tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1];
     569             :             }
     570             :         }
     571             :         acc = 0;
     572             :         for (ch = 0; ch < nb_channels; ch++)
     573             :             for (sb = 0; sb < 30; sb++)
     574             :                 for (j = 0; j < 64; j++)
     575             :                     acc += tone_level_idx_temp[ch][sb][j];
     576             : 
     577             :         multres = 0x66666667LL * (acc * 10);
     578             :         esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31);
     579             :         for (ch = 0;  ch < nb_channels; ch++)
     580             :             for (sb = 0; sb < 30; sb++)
     581             :                 for (j = 0; j < 64; j++) {
     582             :                     comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10;
     583             :                     if (comp < 0)
     584             :                         comp += 0xff;
     585             :                     comp /= 256; // signed shift
     586             :                     switch(sb) {
     587             :                         case 0:
     588             :                             if (comp < 30)
     589             :                                 comp = 30;
     590             :                             comp += 15;
     591             :                             break;
     592             :                         case 1:
     593             :                             if (comp < 24)
     594             :                                 comp = 24;
     595             :                             comp += 10;
     596             :                             break;
     597             :                         case 2:
     598             :                         case 3:
     599             :                         case 4:
     600             :                             if (comp < 16)
     601             :                                 comp = 16;
     602             :                     }
     603             :                     if (comp <= 5)
     604             :                         tmp = 0;
     605             :                     else if (comp <= 10)
     606             :                         tmp = 10;
     607             :                     else if (comp <= 16)
     608             :                         tmp = 16;
     609             :                     else if (comp <= 24)
     610             :                         tmp = -1;
     611             :                     else
     612             :                         tmp = 0;
     613             :                     coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff;
     614             :                 }
     615             :         for (sb = 0; sb < 30; sb++)
     616             :             fix_coding_method_array(sb, nb_channels, coding_method);
     617             :         for (ch = 0; ch < nb_channels; ch++)
     618             :             for (sb = 0; sb < 30; sb++)
     619             :                 for (j = 0; j < 64; j++)
     620             :                     if (sb >= 10) {
     621             :                         if (coding_method[ch][sb][j] < 10)
     622             :                             coding_method[ch][sb][j] = 10;
     623             :                     } else {
     624             :                         if (sb >= 2) {
     625             :                             if (coding_method[ch][sb][j] < 16)
     626             :                                 coding_method[ch][sb][j] = 16;
     627             :                         } else {
     628             :                             if (coding_method[ch][sb][j] < 30)
     629             :                                 coding_method[ch][sb][j] = 30;
     630             :                         }
     631             :                     }
     632             :     } else { // superblocktype_2_3 != 0
     633           0 :         for (ch = 0; ch < nb_channels; ch++)
     634           0 :             for (sb = 0; sb < 30; sb++)
     635           0 :                 for (j = 0; j < 64; j++)
     636           0 :                     coding_method[ch][sb][j] = coding_method_table[cm_table_select][sb];
     637             :     }
     638             : }
     639             : 
     640             : /**
     641             :  * Called by process_subpacket_11 to process more data from subpacket 11
     642             :  * with sb 0-8.
     643             :  * Called by process_subpacket_12 to process data from subpacket 12 with
     644             :  * sb 8-sb_used.
     645             :  *
     646             :  * @param q         context
     647             :  * @param gb        bitreader context
     648             :  * @param length    packet length in bits
     649             :  * @param sb_min    lower subband processed (sb_min included)
     650             :  * @param sb_max    higher subband processed (sb_max excluded)
     651             :  */
     652         276 : static int synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb,
     653             :                                        int length, int sb_min, int sb_max)
     654             : {
     655             :     int sb, j, k, n, ch, run, channels;
     656             :     int joined_stereo, zero_encoding;
     657             :     int type34_first;
     658         276 :     float type34_div = 0;
     659             :     float type34_predictor;
     660             :     float samples[10];
     661         276 :     int sign_bits[16] = {0};
     662             : 
     663         276 :     if (length == 0) {
     664             :         // If no data use noise
     665        4416 :         for (sb=sb_min; sb < sb_max; sb++)
     666        4140 :             build_sb_samples_from_noise(q, sb);
     667             : 
     668         276 :         return 0;
     669             :     }
     670             : 
     671           0 :     for (sb = sb_min; sb < sb_max; sb++) {
     672           0 :         channels = q->nb_channels;
     673             : 
     674           0 :         if (q->nb_channels <= 1 || sb < 12)
     675           0 :             joined_stereo = 0;
     676           0 :         else if (sb >= 24)
     677           0 :             joined_stereo = 1;
     678             :         else
     679           0 :             joined_stereo = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0;
     680             : 
     681           0 :         if (joined_stereo) {
     682           0 :             if (get_bits_left(gb) >= 16)
     683           0 :                 for (j = 0; j < 16; j++)
     684           0 :                     sign_bits[j] = get_bits1(gb);
     685             : 
     686           0 :             for (j = 0; j < 64; j++)
     687           0 :                 if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j])
     688           0 :                     q->coding_method[0][sb][j] = q->coding_method[1][sb][j];
     689             : 
     690           0 :             if (fix_coding_method_array(sb, q->nb_channels,
     691           0 :                                             q->coding_method)) {
     692           0 :                 av_log(NULL, AV_LOG_ERROR, "coding method invalid\n");
     693           0 :                 build_sb_samples_from_noise(q, sb);
     694           0 :                 continue;
     695             :             }
     696           0 :             channels = 1;
     697             :         }
     698             : 
     699           0 :         for (ch = 0; ch < channels; ch++) {
     700           0 :             FIX_NOISE_IDX(q->noise_idx);
     701           0 :             zero_encoding = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0;
     702           0 :             type34_predictor = 0.0;
     703           0 :             type34_first = 1;
     704             : 
     705           0 :             for (j = 0; j < 128; ) {
     706           0 :                 switch (q->coding_method[ch][sb][j / 2]) {
     707           0 :                     case 8:
     708           0 :                         if (get_bits_left(gb) >= 10) {
     709           0 :                             if (zero_encoding) {
     710           0 :                                 for (k = 0; k < 5; k++) {
     711           0 :                                     if ((j + 2 * k) >= 128)
     712           0 :                                         break;
     713           0 :                                     samples[2 * k] = get_bits1(gb) ? dequant_1bit[joined_stereo][2 * get_bits1(gb)] : 0;
     714             :                                 }
     715             :                             } else {
     716           0 :                                 n = get_bits(gb, 8);
     717           0 :                                 if (n >= 243) {
     718           0 :                                     av_log(NULL, AV_LOG_ERROR, "Invalid 8bit codeword\n");
     719           0 :                                     return AVERROR_INVALIDDATA;
     720             :                                 }
     721             : 
     722           0 :                                 for (k = 0; k < 5; k++)
     723           0 :                                     samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
     724             :                             }
     725           0 :                             for (k = 0; k < 5; k++)
     726           0 :                                 samples[2 * k + 1] = SB_DITHERING_NOISE(sb,q->noise_idx);
     727             :                         } else {
     728           0 :                             for (k = 0; k < 10; k++)
     729           0 :                                 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
     730             :                         }
     731           0 :                         run = 10;
     732           0 :                         break;
     733             : 
     734           0 :                     case 10:
     735           0 :                         if (get_bits_left(gb) >= 1) {
     736           0 :                             float f = 0.81;
     737             : 
     738           0 :                             if (get_bits1(gb))
     739           0 :                                 f = -f;
     740           0 :                             f -= noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;
     741           0 :                             samples[0] = f;
     742             :                         } else {
     743           0 :                             samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
     744             :                         }
     745           0 :                         run = 1;
     746           0 :                         break;
     747             : 
     748           0 :                     case 16:
     749           0 :                         if (get_bits_left(gb) >= 10) {
     750           0 :                             if (zero_encoding) {
     751           0 :                                 for (k = 0; k < 5; k++) {
     752           0 :                                     if ((j + k) >= 128)
     753           0 :                                         break;
     754           0 :                                     samples[k] = (get_bits1(gb) == 0) ? 0 : dequant_1bit[joined_stereo][2 * get_bits1(gb)];
     755             :                                 }
     756             :                             } else {
     757           0 :                                 n = get_bits (gb, 8);
     758           0 :                                 if (n >= 243) {
     759           0 :                                     av_log(NULL, AV_LOG_ERROR, "Invalid 8bit codeword\n");
     760           0 :                                     return AVERROR_INVALIDDATA;
     761             :                                 }
     762             : 
     763           0 :                                 for (k = 0; k < 5; k++)
     764           0 :                                     samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
     765             :                             }
     766             :                         } else {
     767           0 :                             for (k = 0; k < 5; k++)
     768           0 :                                 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
     769             :                         }
     770           0 :                         run = 5;
     771           0 :                         break;
     772             : 
     773           0 :                     case 24:
     774           0 :                         if (get_bits_left(gb) >= 7) {
     775           0 :                             n = get_bits(gb, 7);
     776           0 :                             if (n >= 125) {
     777           0 :                                 av_log(NULL, AV_LOG_ERROR, "Invalid 7bit codeword\n");
     778           0 :                                 return AVERROR_INVALIDDATA;
     779             :                             }
     780             : 
     781           0 :                             for (k = 0; k < 3; k++)
     782           0 :                                 samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5;
     783             :                         } else {
     784           0 :                             for (k = 0; k < 3; k++)
     785           0 :                                 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
     786             :                         }
     787           0 :                         run = 3;
     788           0 :                         break;
     789             : 
     790           0 :                     case 30:
     791           0 :                         if (get_bits_left(gb) >= 4) {
     792           0 :                             unsigned index = qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1);
     793           0 :                             if (index >= FF_ARRAY_ELEMS(type30_dequant)) {
     794           0 :                                 av_log(NULL, AV_LOG_ERROR, "index %d out of type30_dequant array\n", index);
     795           0 :                                 return AVERROR_INVALIDDATA;
     796             :                             }
     797           0 :                             samples[0] = type30_dequant[index];
     798             :                         } else
     799           0 :                             samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
     800             : 
     801           0 :                         run = 1;
     802           0 :                         break;
     803             : 
     804           0 :                     case 34:
     805           0 :                         if (get_bits_left(gb) >= 7) {
     806           0 :                             if (type34_first) {
     807           0 :                                 type34_div = (float)(1 << get_bits(gb, 2));
     808           0 :                                 samples[0] = ((float)get_bits(gb, 5) - 16.0) / 15.0;
     809           0 :                                 type34_predictor = samples[0];
     810           0 :                                 type34_first = 0;
     811             :                             } else {
     812           0 :                                 unsigned index = qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1);
     813           0 :                                 if (index >= FF_ARRAY_ELEMS(type34_delta)) {
     814           0 :                                     av_log(NULL, AV_LOG_ERROR, "index %d out of type34_delta array\n", index);
     815           0 :                                     return AVERROR_INVALIDDATA;
     816             :                                 }
     817           0 :                                 samples[0] = type34_delta[index] / type34_div + type34_predictor;
     818           0 :                                 type34_predictor = samples[0];
     819             :                             }
     820             :                         } else {
     821           0 :                             samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
     822             :                         }
     823           0 :                         run = 1;
     824           0 :                         break;
     825             : 
     826           0 :                     default:
     827           0 :                         samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
     828           0 :                         run = 1;
     829           0 :                         break;
     830             :                 }
     831             : 
     832           0 :                 if (joined_stereo) {
     833           0 :                     for (k = 0; k < run && j + k < 128; k++) {
     834           0 :                         q->sb_samples[0][j + k][sb] =
     835           0 :                             q->tone_level[0][sb][(j + k) / 2] * samples[k];
     836           0 :                         if (q->nb_channels == 2) {
     837           0 :                             if (sign_bits[(j + k) / 8])
     838           0 :                                 q->sb_samples[1][j + k][sb] =
     839           0 :                                     q->tone_level[1][sb][(j + k) / 2] * -samples[k];
     840             :                             else
     841           0 :                                 q->sb_samples[1][j + k][sb] =
     842           0 :                                     q->tone_level[1][sb][(j + k) / 2] * samples[k];
     843             :                         }
     844             :                     }
     845             :                 } else {
     846           0 :                     for (k = 0; k < run; k++)
     847           0 :                         if ((j + k) < 128)
     848           0 :                             q->sb_samples[ch][j + k][sb] = q->tone_level[ch][sb][(j + k)/2] * samples[k];
     849             :                 }
     850             : 
     851           0 :                 j += run;
     852             :             } // j loop
     853             :         } // channel loop
     854             :     } // subband loop
     855           0 :     return 0;
     856             : }
     857             : 
     858             : /**
     859             :  * Init the first element of a channel in quantized_coeffs with data
     860             :  * from packet 10 (quantized_coeffs[ch][0]).
     861             :  * This is similar to process_subpacket_9, but for a single channel
     862             :  * and for element [0]
     863             :  * same VLC tables as process_subpacket_9 are used.
     864             :  *
     865             :  * @param quantized_coeffs    pointer to quantized_coeffs[ch][0]
     866             :  * @param gb        bitreader context
     867             :  */
     868           0 : static int init_quantized_coeffs_elem0(int8_t *quantized_coeffs,
     869             :                                         GetBitContext *gb)
     870             : {
     871             :     int i, k, run, level, diff;
     872             : 
     873           0 :     if (get_bits_left(gb) < 16)
     874           0 :         return -1;
     875           0 :     level = qdm2_get_vlc(gb, &vlc_tab_level, 0, 2);
     876             : 
     877           0 :     quantized_coeffs[0] = level;
     878             : 
     879           0 :     for (i = 0; i < 7; ) {
     880           0 :         if (get_bits_left(gb) < 16)
     881           0 :             return -1;
     882           0 :         run = qdm2_get_vlc(gb, &vlc_tab_run, 0, 1) + 1;
     883             : 
     884           0 :         if (i + run >= 8)
     885           0 :             return -1;
     886             : 
     887           0 :         if (get_bits_left(gb) < 16)
     888           0 :             return -1;
     889           0 :         diff = qdm2_get_se_vlc(&vlc_tab_diff, gb, 2);
     890             : 
     891           0 :         for (k = 1; k <= run; k++)
     892           0 :             quantized_coeffs[i + k] = (level + ((k * diff) / run));
     893             : 
     894           0 :         level += diff;
     895           0 :         i += run;
     896             :     }
     897           0 :     return 0;
     898             : }
     899             : 
     900             : /**
     901             :  * Related to synthesis filter, process data from packet 10
     902             :  * Init part of quantized_coeffs via function init_quantized_coeffs_elem0
     903             :  * Init tone_level_idx_hi1, tone_level_idx_hi2, tone_level_idx_mid with
     904             :  * data from packet 10
     905             :  *
     906             :  * @param q         context
     907             :  * @param gb        bitreader context
     908             :  */
     909           0 : static void init_tone_level_dequantization(QDM2Context *q, GetBitContext *gb)
     910             : {
     911             :     int sb, j, k, n, ch;
     912             : 
     913           0 :     for (ch = 0; ch < q->nb_channels; ch++) {
     914           0 :         init_quantized_coeffs_elem0(q->quantized_coeffs[ch][0], gb);
     915             : 
     916           0 :         if (get_bits_left(gb) < 16) {
     917           0 :             memset(q->quantized_coeffs[ch][0], 0, 8);
     918           0 :             break;
     919             :         }
     920             :     }
     921             : 
     922           0 :     n = q->sub_sampling + 1;
     923             : 
     924           0 :     for (sb = 0; sb < n; sb++)
     925           0 :         for (ch = 0; ch < q->nb_channels; ch++)
     926           0 :             for (j = 0; j < 8; j++) {
     927           0 :                 if (get_bits_left(gb) < 1)
     928           0 :                     break;
     929           0 :                 if (get_bits1(gb)) {
     930           0 :                     for (k=0; k < 8; k++) {
     931           0 :                         if (get_bits_left(gb) < 16)
     932           0 :                             break;
     933           0 :                         q->tone_level_idx_hi1[ch][sb][j][k] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi1, 0, 2);
     934             :                     }
     935             :                 } else {
     936           0 :                     for (k=0; k < 8; k++)
     937           0 :                         q->tone_level_idx_hi1[ch][sb][j][k] = 0;
     938             :                 }
     939             :             }
     940             : 
     941           0 :     n = QDM2_SB_USED(q->sub_sampling) - 4;
     942             : 
     943           0 :     for (sb = 0; sb < n; sb++)
     944           0 :         for (ch = 0; ch < q->nb_channels; ch++) {
     945           0 :             if (get_bits_left(gb) < 16)
     946           0 :                 break;
     947           0 :             q->tone_level_idx_hi2[ch][sb] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi2, 0, 2);
     948           0 :             if (sb > 19)
     949           0 :                 q->tone_level_idx_hi2[ch][sb] -= 16;
     950             :             else
     951           0 :                 for (j = 0; j < 8; j++)
     952           0 :                     q->tone_level_idx_mid[ch][sb][j] = -16;
     953             :         }
     954             : 
     955           0 :     n = QDM2_SB_USED(q->sub_sampling) - 5;
     956             : 
     957           0 :     for (sb = 0; sb < n; sb++)
     958           0 :         for (ch = 0; ch < q->nb_channels; ch++)
     959           0 :             for (j = 0; j < 8; j++) {
     960           0 :                 if (get_bits_left(gb) < 16)
     961           0 :                     break;
     962           0 :                 q->tone_level_idx_mid[ch][sb][j] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_mid, 0, 2) - 32;
     963             :             }
     964           0 : }
     965             : 
     966             : /**
     967             :  * Process subpacket 9, init quantized_coeffs with data from it
     968             :  *
     969             :  * @param q       context
     970             :  * @param node    pointer to node with packet
     971             :  */
     972         138 : static int process_subpacket_9(QDM2Context *q, QDM2SubPNode *node)
     973             : {
     974             :     GetBitContext gb;
     975             :     int i, j, k, n, ch, run, level, diff;
     976             : 
     977         138 :     init_get_bits(&gb, node->packet->data, node->packet->size * 8);
     978             : 
     979         138 :     n = coeff_per_sb_for_avg[q->coeff_per_sb_select][QDM2_SB_USED(q->sub_sampling) - 1] + 1;
     980             : 
     981        1380 :     for (i = 1; i < n; i++)
     982        3726 :         for (ch = 0; ch < q->nb_channels; ch++) {
     983        2484 :             level = qdm2_get_vlc(&gb, &vlc_tab_level, 0, 2);
     984        2484 :             q->quantized_coeffs[ch][i][0] = level;
     985             : 
     986       15864 :             for (j = 0; j < (8 - 1); ) {
     987       10896 :                 run  = qdm2_get_vlc(&gb, &vlc_tab_run, 0, 1) + 1;
     988       10896 :                 diff = qdm2_get_se_vlc(&vlc_tab_diff, &gb, 2);
     989             : 
     990       10896 :                 if (j + run >= 8)
     991           0 :                     return -1;
     992             : 
     993       28284 :                 for (k = 1; k <= run; k++)
     994       17388 :                     q->quantized_coeffs[ch][i][j + k] = (level + ((k * diff) / run));
     995             : 
     996       10896 :                 level += diff;
     997       10896 :                 j     += run;
     998             :             }
     999             :         }
    1000             : 
    1001         414 :     for (ch = 0; ch < q->nb_channels; ch++)
    1002        2484 :         for (i = 0; i < 8; i++)
    1003        2208 :             q->quantized_coeffs[ch][0][i] = 0;
    1004             : 
    1005         138 :     return 0;
    1006             : }
    1007             : 
    1008             : /**
    1009             :  * Process subpacket 10 if not null, else
    1010             :  *
    1011             :  * @param q         context
    1012             :  * @param node      pointer to node with packet
    1013             :  */
    1014         138 : static void process_subpacket_10(QDM2Context *q, QDM2SubPNode *node)
    1015             : {
    1016             :     GetBitContext gb;
    1017             : 
    1018         138 :     if (node) {
    1019           0 :         init_get_bits(&gb, node->packet->data, node->packet->size * 8);
    1020           0 :         init_tone_level_dequantization(q, &gb);
    1021           0 :         fill_tone_level_array(q, 1);
    1022             :     } else {
    1023         138 :         fill_tone_level_array(q, 0);
    1024             :     }
    1025         138 : }
    1026             : 
    1027             : /**
    1028             :  * Process subpacket 11
    1029             :  *
    1030             :  * @param q         context
    1031             :  * @param node      pointer to node with packet
    1032             :  */
    1033         138 : static void process_subpacket_11(QDM2Context *q, QDM2SubPNode *node)
    1034             : {
    1035             :     GetBitContext gb;
    1036         138 :     int length = 0;
    1037             : 
    1038         138 :     if (node) {
    1039           0 :         length = node->packet->size * 8;
    1040           0 :         init_get_bits(&gb, node->packet->data, length);
    1041             :     }
    1042             : 
    1043         138 :     if (length >= 32) {
    1044           0 :         int c = get_bits(&gb, 13);
    1045             : 
    1046           0 :         if (c > 3)
    1047           0 :             fill_coding_method_array(q->tone_level_idx,
    1048           0 :                                      q->tone_level_idx_temp, q->coding_method,
    1049             :                                      q->nb_channels, 8 * c,
    1050             :                                      q->superblocktype_2_3, q->cm_table_select);
    1051             :     }
    1052             : 
    1053         138 :     synthfilt_build_sb_samples(q, &gb, length, 0, 8);
    1054         138 : }
    1055             : 
    1056             : /**
    1057             :  * Process subpacket 12
    1058             :  *
    1059             :  * @param q         context
    1060             :  * @param node      pointer to node with packet
    1061             :  */
    1062         138 : static void process_subpacket_12(QDM2Context *q, QDM2SubPNode *node)
    1063             : {
    1064             :     GetBitContext gb;
    1065         138 :     int length = 0;
    1066             : 
    1067         138 :     if (node) {
    1068           0 :         length = node->packet->size * 8;
    1069           0 :         init_get_bits(&gb, node->packet->data, length);
    1070             :     }
    1071             : 
    1072         138 :     synthfilt_build_sb_samples(q, &gb, length, 8, QDM2_SB_USED(q->sub_sampling));
    1073         138 : }
    1074             : 
    1075             : /**
    1076             :  * Process new subpackets for synthesis filter
    1077             :  *
    1078             :  * @param q       context
    1079             :  * @param list    list with synthesis filter packets (list D)
    1080             :  */
    1081         138 : static void process_synthesis_subpackets(QDM2Context *q, QDM2SubPNode *list)
    1082             : {
    1083             :     QDM2SubPNode *nodes[4];
    1084             : 
    1085         138 :     nodes[0] = qdm2_search_subpacket_type_in_list(list, 9);
    1086         138 :     if (nodes[0])
    1087         138 :         process_subpacket_9(q, nodes[0]);
    1088             : 
    1089         138 :     nodes[1] = qdm2_search_subpacket_type_in_list(list, 10);
    1090         138 :     if (nodes[1])
    1091           0 :         process_subpacket_10(q, nodes[1]);
    1092             :     else
    1093         138 :         process_subpacket_10(q, NULL);
    1094             : 
    1095         138 :     nodes[2] = qdm2_search_subpacket_type_in_list(list, 11);
    1096         138 :     if (nodes[0] && nodes[1] && nodes[2])
    1097           0 :         process_subpacket_11(q, nodes[2]);
    1098             :     else
    1099         138 :         process_subpacket_11(q, NULL);
    1100             : 
    1101         138 :     nodes[3] = qdm2_search_subpacket_type_in_list(list, 12);
    1102         138 :     if (nodes[0] && nodes[1] && nodes[3])
    1103           0 :         process_subpacket_12(q, nodes[3]);
    1104             :     else
    1105         138 :         process_subpacket_12(q, NULL);
    1106         138 : }
    1107             : 
    1108             : /**
    1109             :  * Decode superblock, fill packet lists.
    1110             :  *
    1111             :  * @param q    context
    1112             :  */
    1113         138 : static void qdm2_decode_super_block(QDM2Context *q)
    1114             : {
    1115             :     GetBitContext gb;
    1116             :     QDM2SubPacket header, *packet;
    1117             :     int i, packet_bytes, sub_packet_size, sub_packets_D;
    1118         138 :     unsigned int next_index = 0;
    1119             : 
    1120         138 :     memset(q->tone_level_idx_hi1, 0, sizeof(q->tone_level_idx_hi1));
    1121         138 :     memset(q->tone_level_idx_mid, 0, sizeof(q->tone_level_idx_mid));
    1122         138 :     memset(q->tone_level_idx_hi2, 0, sizeof(q->tone_level_idx_hi2));
    1123             : 
    1124         138 :     q->sub_packets_B = 0;
    1125         138 :     sub_packets_D    = 0;
    1126             : 
    1127         138 :     average_quantized_coeffs(q); // average elements in quantized_coeffs[max_ch][10][8]
    1128             : 
    1129         138 :     init_get_bits(&gb, q->compressed_data, q->compressed_size * 8);
    1130         138 :     qdm2_decode_sub_packet_header(&gb, &header);
    1131             : 
    1132         138 :     if (header.type < 2 || header.type >= 8) {
    1133           0 :         q->has_errors = 1;
    1134           0 :         av_log(NULL, AV_LOG_ERROR, "bad superblock type\n");
    1135           0 :         return;
    1136             :     }
    1137             : 
    1138         138 :     q->superblocktype_2_3 = (header.type == 2 || header.type == 3);
    1139         138 :     packet_bytes          = (q->compressed_size - get_bits_count(&gb) / 8);
    1140             : 
    1141         138 :     init_get_bits(&gb, header.data, header.size * 8);
    1142             : 
    1143         138 :     if (header.type == 2 || header.type == 4 || header.type == 5) {
    1144         138 :         int csum = 257 * get_bits(&gb, 8);
    1145         138 :         csum += 2 * get_bits(&gb, 8);
    1146             : 
    1147         138 :         csum = qdm2_packet_checksum(q->compressed_data, q->checksum_size, csum);
    1148             : 
    1149         138 :         if (csum != 0) {
    1150           0 :             q->has_errors = 1;
    1151           0 :             av_log(NULL, AV_LOG_ERROR, "bad packet checksum\n");
    1152           0 :             return;
    1153             :         }
    1154             :     }
    1155             : 
    1156         138 :     q->sub_packet_list_B[0].packet = NULL;
    1157         138 :     q->sub_packet_list_D[0].packet = NULL;
    1158             : 
    1159         966 :     for (i = 0; i < 6; i++)
    1160         828 :         if (--q->fft_level_exp[i] < 0)
    1161         828 :             q->fft_level_exp[i] = 0;
    1162             : 
    1163         966 :     for (i = 0; packet_bytes > 0; i++) {
    1164             :         int j;
    1165             : 
    1166         966 :         if (i >= FF_ARRAY_ELEMS(q->sub_packet_list_A)) {
    1167           0 :             SAMPLES_NEEDED_2("too many packet bytes");
    1168           0 :             return;
    1169             :         }
    1170             : 
    1171         966 :         q->sub_packet_list_A[i].next = NULL;
    1172             : 
    1173         966 :         if (i > 0) {
    1174         828 :             q->sub_packet_list_A[i - 1].next = &q->sub_packet_list_A[i];
    1175             : 
    1176             :             /* seek to next block */
    1177         828 :             init_get_bits(&gb, header.data, header.size * 8);
    1178         828 :             skip_bits(&gb, next_index * 8);
    1179             : 
    1180         828 :             if (next_index >= header.size)
    1181           0 :                 break;
    1182             :         }
    1183             : 
    1184             :         /* decode subpacket */
    1185         966 :         packet = &q->sub_packets[i];
    1186         966 :         qdm2_decode_sub_packet_header(&gb, packet);
    1187         966 :         next_index      = packet->size + get_bits_count(&gb) / 8;
    1188         966 :         sub_packet_size = ((packet->size > 0xff) ? 1 : 0) + packet->size + 2;
    1189             : 
    1190         966 :         if (packet->type == 0)
    1191         138 :             break;
    1192             : 
    1193         828 :         if (sub_packet_size > packet_bytes) {
    1194           0 :             if (packet->type != 10 && packet->type != 11 && packet->type != 12)
    1195           0 :                 break;
    1196           0 :             packet->size += packet_bytes - sub_packet_size;
    1197             :         }
    1198             : 
    1199         828 :         packet_bytes -= sub_packet_size;
    1200             : 
    1201             :         /* add subpacket to 'all subpackets' list */
    1202         828 :         q->sub_packet_list_A[i].packet = packet;
    1203             : 
    1204             :         /* add subpacket to related list */
    1205         828 :         if (packet->type == 8) {
    1206           0 :             SAMPLES_NEEDED_2("packet type 8");
    1207           0 :             return;
    1208         828 :         } else if (packet->type >= 9 && packet->type <= 12) {
    1209             :             /* packets for MPEG Audio like Synthesis Filter */
    1210         138 :             QDM2_LIST_ADD(q->sub_packet_list_D, sub_packets_D, packet);
    1211         690 :         } else if (packet->type == 13) {
    1212           0 :             for (j = 0; j < 6; j++)
    1213           0 :                 q->fft_level_exp[j] = get_bits(&gb, 6);
    1214         690 :         } else if (packet->type == 14) {
    1215           0 :             for (j = 0; j < 6; j++)
    1216           0 :                 q->fft_level_exp[j] = qdm2_get_vlc(&gb, &fft_level_exp_vlc, 0, 2);
    1217         690 :         } else if (packet->type == 15) {
    1218           0 :             SAMPLES_NEEDED_2("packet type 15")
    1219           0 :             return;
    1220        1380 :         } else if (packet->type >= 16 && packet->type < 48 &&
    1221         690 :                    !fft_subpackets[packet->type - 16]) {
    1222             :             /* packets for FFT */
    1223         690 :             QDM2_LIST_ADD(q->sub_packet_list_B, q->sub_packets_B, packet);
    1224             :         }
    1225             :     } // Packet bytes loop
    1226             : 
    1227         138 :     if (q->sub_packet_list_D[0].packet) {
    1228         138 :         process_synthesis_subpackets(q, q->sub_packet_list_D);
    1229         138 :         q->do_synth_filter = 1;
    1230           0 :     } else if (q->do_synth_filter) {
    1231           0 :         process_subpacket_10(q, NULL);
    1232           0 :         process_subpacket_11(q, NULL);
    1233           0 :         process_subpacket_12(q, NULL);
    1234             :     }
    1235             : }
    1236             : 
    1237       20037 : static void qdm2_fft_init_coefficient(QDM2Context *q, int sub_packet,
    1238             :                                       int offset, int duration, int channel,
    1239             :                                       int exp, int phase)
    1240             : {
    1241       20037 :     if (q->fft_coefs_min_index[duration] < 0)
    1242         542 :         q->fft_coefs_min_index[duration] = q->fft_coefs_index;
    1243             : 
    1244       20321 :     q->fft_coefs[q->fft_coefs_index].sub_packet =
    1245         284 :         ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet);
    1246       20037 :     q->fft_coefs[q->fft_coefs_index].channel = channel;
    1247       20037 :     q->fft_coefs[q->fft_coefs_index].offset  = offset;
    1248       20037 :     q->fft_coefs[q->fft_coefs_index].exp     = exp;
    1249       20037 :     q->fft_coefs[q->fft_coefs_index].phase   = phase;
    1250       20037 :     q->fft_coefs_index++;
    1251       20037 : }
    1252             : 
    1253         552 : static void qdm2_fft_decode_tones(QDM2Context *q, int duration,
    1254             :                                   GetBitContext *gb, int b)
    1255             : {
    1256             :     int channel, stereo, phase, exp;
    1257             :     int local_int_4, local_int_8, stereo_phase, local_int_10;
    1258             :     int local_int_14, stereo_exp, local_int_20, local_int_28;
    1259             :     int n, offset;
    1260             : 
    1261         552 :     local_int_4  = 0;
    1262         552 :     local_int_28 = 0;
    1263         552 :     local_int_20 = 2;
    1264         552 :     local_int_8  = (4 - duration);
    1265         552 :     local_int_10 = 1 << (q->group_order - duration - 1);
    1266         552 :     offset       = 1;
    1267             : 
    1268       16700 :     while (get_bits_left(gb)>0) {
    1269       16148 :         if (q->superblocktype_2_3) {
    1270       34282 :             while ((n = qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) {
    1271        2053 :                 if (get_bits_left(gb)<0) {
    1272          67 :                     if(local_int_4 < q->group_size)
    1273           0 :                         av_log(NULL, AV_LOG_ERROR, "overread in qdm2_fft_decode_tones()\n");
    1274          67 :                     return;
    1275             :                 }
    1276        1986 :                 offset = 1;
    1277        1986 :                 if (n == 0) {
    1278        1974 :                     local_int_4  += local_int_10;
    1279        1974 :                     local_int_28 += (1 << local_int_8);
    1280             :                 } else {
    1281          12 :                     local_int_4  += 8 * local_int_10;
    1282          12 :                     local_int_28 += (8 << local_int_8);
    1283             :                 }
    1284             :             }
    1285       16081 :             offset += (n - 2);
    1286             :         } else {
    1287           0 :             offset += qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2);
    1288           0 :             while (offset >= (local_int_10 - 1)) {
    1289           0 :                 offset       += (1 - (local_int_10 - 1));
    1290           0 :                 local_int_4  += local_int_10;
    1291           0 :                 local_int_28 += (1 << local_int_8);
    1292             :             }
    1293             :         }
    1294             : 
    1295       16081 :         if (local_int_4 >= q->group_size)
    1296         485 :             return;
    1297             : 
    1298       15596 :         local_int_14 = (offset >> local_int_8);
    1299       15596 :         if (local_int_14 >= FF_ARRAY_ELEMS(fft_level_index_table))
    1300           0 :             return;
    1301             : 
    1302       15596 :         if (q->nb_channels > 1) {
    1303       15596 :             channel = get_bits1(gb);
    1304       15596 :             stereo  = get_bits1(gb);
    1305             :         } else {
    1306           0 :             channel = 0;
    1307           0 :             stereo  = 0;
    1308             :         }
    1309             : 
    1310       15596 :         exp  = qdm2_get_vlc(gb, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2);
    1311       15596 :         exp += q->fft_level_exp[fft_level_index_table[local_int_14]];
    1312       15596 :         exp  = (exp < 0) ? 0 : exp;
    1313             : 
    1314       15596 :         phase        = get_bits(gb, 3);
    1315       15596 :         stereo_exp   = 0;
    1316       15596 :         stereo_phase = 0;
    1317             : 
    1318       15596 :         if (stereo) {
    1319        4441 :             stereo_exp   = (exp - qdm2_get_vlc(gb, &fft_stereo_exp_vlc, 0, 1));
    1320        4441 :             stereo_phase = (phase - qdm2_get_vlc(gb, &fft_stereo_phase_vlc, 0, 1));
    1321        4441 :             if (stereo_phase < 0)
    1322         497 :                 stereo_phase += 8;
    1323             :         }
    1324             : 
    1325       15596 :         if (q->frequency_range > (local_int_14 + 1)) {
    1326       15596 :             int sub_packet = (local_int_20 + local_int_28);
    1327             : 
    1328       15596 :             qdm2_fft_init_coefficient(q, sub_packet, offset, duration,
    1329             :                                       channel, exp, phase);
    1330       15596 :             if (stereo)
    1331        4441 :                 qdm2_fft_init_coefficient(q, sub_packet, offset, duration,
    1332             :                                           1 - channel,
    1333             :                                           stereo_exp, stereo_phase);
    1334             :         }
    1335       15596 :         offset++;
    1336             :     }
    1337             : }
    1338             : 
    1339         138 : static void qdm2_decode_fft_packets(QDM2Context *q)
    1340             : {
    1341             :     int i, j, min, max, value, type, unknown_flag;
    1342             :     GetBitContext gb;
    1343             : 
    1344         138 :     if (!q->sub_packet_list_B[0].packet)
    1345           0 :         return;
    1346             : 
    1347             :     /* reset minimum indexes for FFT coefficients */
    1348         138 :     q->fft_coefs_index = 0;
    1349         828 :     for (i = 0; i < 5; i++)
    1350         690 :         q->fft_coefs_min_index[i] = -1;
    1351             : 
    1352             :     /* process subpackets ordered by type, largest type first */
    1353         828 :     for (i = 0, max = 256; i < q->sub_packets_B; i++) {
    1354         690 :         QDM2SubPacket *packet = NULL;
    1355             : 
    1356             :         /* find subpacket with largest type less than max */
    1357        4140 :         for (j = 0, min = 0; j < q->sub_packets_B; j++) {
    1358        3450 :             value = q->sub_packet_list_B[j].packet->type;
    1359        3450 :             if (value > min && value < max) {
    1360         690 :                 min    = value;
    1361         690 :                 packet = q->sub_packet_list_B[j].packet;
    1362             :             }
    1363             :         }
    1364             : 
    1365         690 :         max = min;
    1366             : 
    1367             :         /* check for errors (?) */
    1368         690 :         if (!packet)
    1369           0 :             return;
    1370             : 
    1371         828 :         if (i == 0 &&
    1372         414 :             (packet->type < 16 || packet->type >= 48 ||
    1373         138 :              fft_subpackets[packet->type - 16]))
    1374           0 :             return;
    1375             : 
    1376             :         /* decode FFT tones */
    1377         690 :         init_get_bits(&gb, packet->data, packet->size * 8);
    1378             : 
    1379         690 :         if (packet->type >= 32 && packet->type < 48 && !fft_subpackets[packet->type - 16])
    1380           0 :             unknown_flag = 1;
    1381             :         else
    1382         690 :             unknown_flag = 0;
    1383             : 
    1384         690 :         type = packet->type;
    1385             : 
    1386        1380 :         if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) {
    1387         690 :             int duration = q->sub_sampling + 5 - (type & 15);
    1388             : 
    1389         690 :             if (duration >= 0 && duration < 4)
    1390         552 :                 qdm2_fft_decode_tones(q, duration, &gb, unknown_flag);
    1391           0 :         } else if (type == 31) {
    1392           0 :             for (j = 0; j < 4; j++)
    1393           0 :                 qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
    1394           0 :         } else if (type == 46) {
    1395           0 :             for (j = 0; j < 6; j++)
    1396           0 :                 q->fft_level_exp[j] = get_bits(&gb, 6);
    1397           0 :             for (j = 0; j < 4; j++)
    1398           0 :                 qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
    1399             :         }
    1400             :     } // Loop on B packets
    1401             : 
    1402             :     /* calculate maximum indexes for FFT coefficients */
    1403         828 :     for (i = 0, j = -1; i < 5; i++)
    1404         690 :         if (q->fft_coefs_min_index[i] >= 0) {
    1405         542 :             if (j >= 0)
    1406         404 :                 q->fft_coefs_max_index[j] = q->fft_coefs_min_index[i];
    1407         542 :             j = i;
    1408             :         }
    1409         138 :     if (j >= 0)
    1410         138 :         q->fft_coefs_max_index[j] = q->fft_coefs_index;
    1411             : }
    1412             : 
    1413      340988 : static void qdm2_fft_generate_tone(QDM2Context *q, FFTTone *tone)
    1414             : {
    1415             :     float level, f[6];
    1416             :     int i;
    1417             :     QDM2Complex c;
    1418      340988 :     const double iscale = 2.0 * M_PI / 512.0;
    1419             : 
    1420      340988 :     tone->phase += tone->phase_shift;
    1421             : 
    1422             :     /* calculate current level (maximum amplitude) of tone */
    1423      340988 :     level = fft_tone_envelope_table[tone->duration][tone->time_index] * tone->level;
    1424      340988 :     c.im  = level * sin(tone->phase * iscale);
    1425      340988 :     c.re  = level * cos(tone->phase * iscale);
    1426             : 
    1427             :     /* generate FFT coefficients for tone */
    1428      340988 :     if (tone->duration >= 3 || tone->cutoff >= 3) {
    1429       46448 :         tone->complex[0].im += c.im;
    1430       46448 :         tone->complex[0].re += c.re;
    1431       46448 :         tone->complex[1].im -= c.im;
    1432       46448 :         tone->complex[1].re -= c.re;
    1433             :     } else {
    1434      294540 :         f[1] = -tone->table[4];
    1435      294540 :         f[0] = tone->table[3] - tone->table[0];
    1436      294540 :         f[2] = 1.0 - tone->table[2] - tone->table[3];
    1437      294540 :         f[3] = tone->table[1] + tone->table[4] - 1.0;
    1438      294540 :         f[4] = tone->table[0] - tone->table[1];
    1439      294540 :         f[5] = tone->table[2];
    1440      883620 :         for (i = 0; i < 2; i++) {
    1441     1178160 :             tone->complex[fft_cutoff_index_table[tone->cutoff][i]].re +=
    1442      589080 :                 c.re * f[i];
    1443     1178160 :             tone->complex[fft_cutoff_index_table[tone->cutoff][i]].im +=
    1444      589080 :                 c.im * ((tone->cutoff <= i) ? -f[i] : f[i]);
    1445             :         }
    1446     1472700 :         for (i = 0; i < 4; i++) {
    1447     1178160 :             tone->complex[i].re += c.re * f[i + 2];
    1448     1178160 :             tone->complex[i].im += c.im * f[i + 2];
    1449             :         }
    1450             :     }
    1451             : 
    1452             :     /* copy the tone if it has not yet died out */
    1453      340988 :     if (++tone->time_index < ((1 << (5 - tone->duration)) - 1)) {
    1454      321230 :         memcpy(&q->fft_tones[q->fft_tone_end], tone, sizeof(FFTTone));
    1455      321230 :         q->fft_tone_end = (q->fft_tone_end + 1) % 1000;
    1456             :     }
    1457      340988 : }
    1458             : 
    1459        2208 : static void qdm2_fft_tone_synthesizer(QDM2Context *q, int sub_packet)
    1460             : {
    1461             :     int i, j, ch;
    1462        2208 :     const double iscale = 0.25 * M_PI;
    1463             : 
    1464        6624 :     for (ch = 0; ch < q->channels; ch++) {
    1465        4416 :         memset(q->fft.complex[ch], 0, q->fft_size * sizeof(QDM2Complex));
    1466             :     }
    1467             : 
    1468             : 
    1469             :     /* apply FFT tones with duration 4 (1 FFT period) */
    1470        2208 :     if (q->fft_coefs_min_index[4] >= 0)
    1471           2 :         for (i = q->fft_coefs_min_index[4]; i < q->fft_coefs_max_index[4]; i++) {
    1472             :             float level;
    1473             :             QDM2Complex c;
    1474             : 
    1475           0 :             if (q->fft_coefs[i].sub_packet != sub_packet)
    1476           0 :                 break;
    1477             : 
    1478           0 :             ch = (q->channels == 1) ? 0 : q->fft_coefs[i].channel;
    1479           0 :             level = (q->fft_coefs[i].exp < 0) ? 0.0 : fft_tone_level_table[q->superblocktype_2_3 ? 0 : 1][q->fft_coefs[i].exp & 63];
    1480             : 
    1481           0 :             c.re = level * cos(q->fft_coefs[i].phase * iscale);
    1482           0 :             c.im = level * sin(q->fft_coefs[i].phase * iscale);
    1483           0 :             q->fft.complex[ch][q->fft_coefs[i].offset + 0].re += c.re;
    1484           0 :             q->fft.complex[ch][q->fft_coefs[i].offset + 0].im += c.im;
    1485           0 :             q->fft.complex[ch][q->fft_coefs[i].offset + 1].re -= c.re;
    1486           0 :             q->fft.complex[ch][q->fft_coefs[i].offset + 1].im -= c.im;
    1487             :         }
    1488             : 
    1489             :     /* generate existing FFT tones */
    1490      325367 :     for (i = q->fft_tone_end; i != q->fft_tone_start; ) {
    1491      320951 :         qdm2_fft_generate_tone(q, &q->fft_tones[q->fft_tone_start]);
    1492      320951 :         q->fft_tone_start = (q->fft_tone_start + 1) % 1000;
    1493             :     }
    1494             : 
    1495             :     /* create and generate new FFT tones with duration 0 (long) to 3 (short) */
    1496       11040 :     for (i = 0; i < 4; i++)
    1497        8832 :         if (q->fft_coefs_min_index[i] >= 0) {
    1498       28711 :             for (j = q->fft_coefs_min_index[i]; j < q->fft_coefs_max_index[i]; j++) {
    1499             :                 int offset, four_i;
    1500             :                 FFTTone tone;
    1501             : 
    1502       24279 :                 if (q->fft_coefs[j].sub_packet != sub_packet)
    1503        4242 :                     break;
    1504             : 
    1505       20037 :                 four_i = (4 - i);
    1506       20037 :                 offset = q->fft_coefs[j].offset >> four_i;
    1507       20037 :                 ch = (q->channels == 1) ? 0 : q->fft_coefs[j].channel;
    1508             : 
    1509       20037 :                 if (offset < q->frequency_range) {
    1510       20037 :                     if (offset < 2)
    1511        2780 :                         tone.cutoff = offset;
    1512             :                     else
    1513       17257 :                         tone.cutoff = (offset >= 60) ? 3 : 2;
    1514             : 
    1515       20037 :                     tone.level = (q->fft_coefs[j].exp < 0) ? 0.0 : fft_tone_level_table[q->superblocktype_2_3 ? 0 : 1][q->fft_coefs[j].exp & 63];
    1516       20037 :                     tone.complex = &q->fft.complex[ch][offset];
    1517       20037 :                     tone.table = fft_tone_sample_table[i][q->fft_coefs[j].offset - (offset << four_i)];
    1518       20037 :                     tone.phase = 64 * q->fft_coefs[j].phase - (offset << 8) - 128;
    1519       20037 :                     tone.phase_shift = (2 * q->fft_coefs[j].offset + 1) << (7 - four_i);
    1520       20037 :                     tone.duration = i;
    1521       20037 :                     tone.time_index = 0;
    1522             : 
    1523       20037 :                     qdm2_fft_generate_tone(q, &tone);
    1524             :                 }
    1525             :             }
    1526        8674 :             q->fft_coefs_min_index[i] = j;
    1527             :         }
    1528        2208 : }
    1529             : 
    1530        4416 : static void qdm2_calculate_fft(QDM2Context *q, int channel, int sub_packet)
    1531             : {
    1532        4416 :     const float gain = (q->channels == 1 && q->nb_channels == 2) ? 0.5f : 1.0f;
    1533        4416 :     float *out       = q->output_buffer + channel;
    1534             :     int i;
    1535        4416 :     q->fft.complex[channel][0].re *= 2.0f;
    1536        4416 :     q->fft.complex[channel][0].im  = 0.0f;
    1537        4416 :     q->rdft_ctx.rdft_calc(&q->rdft_ctx, (FFTSample *)q->fft.complex[channel]);
    1538             :     /* add samples to output buffer */
    1539     1134912 :     for (i = 0; i < FFALIGN(q->fft_size, 8); i++) {
    1540     1130496 :         out[0]           += q->fft.complex[channel][i].re * gain;
    1541     1130496 :         out[q->channels] += q->fft.complex[channel][i].im * gain;
    1542     1130496 :         out              += 2 * q->channels;
    1543             :     }
    1544        4416 : }
    1545             : 
    1546             : /**
    1547             :  * @param q        context
    1548             :  * @param index    subpacket number
    1549             :  */
    1550        2208 : static void qdm2_synthesis_filter(QDM2Context *q, int index)
    1551             : {
    1552        2208 :     int i, k, ch, sb_used, sub_sampling, dither_state = 0;
    1553             : 
    1554             :     /* copy sb_samples */
    1555        2208 :     sb_used = QDM2_SB_USED(q->sub_sampling);
    1556             : 
    1557        6624 :     for (ch = 0; ch < q->channels; ch++)
    1558       39744 :         for (i = 0; i < 8; i++)
    1559      105984 :             for (k = sb_used; k < SBLIMIT; k++)
    1560       70656 :                 q->sb_samples[ch][(8 * index) + i][k] = 0;
    1561             : 
    1562        6624 :     for (ch = 0; ch < q->nb_channels; ch++) {
    1563        4416 :         float *samples_ptr = q->samples + ch;
    1564             : 
    1565       39744 :         for (i = 0; i < 8; i++) {
    1566       70656 :             ff_mpa_synth_filter_float(&q->mpadsp,
    1567       35328 :                                       q->synth_buf[ch], &(q->synth_buf_offset[ch]),
    1568             :                                       ff_mpa_synth_window_float, &dither_state,
    1569       35328 :                                       samples_ptr, q->nb_channels,
    1570       35328 :                                       q->sb_samples[ch][(8 * index) + i]);
    1571       35328 :             samples_ptr += 32 * q->nb_channels;
    1572             :         }
    1573             :     }
    1574             : 
    1575             :     /* add samples to output buffer */
    1576        2208 :     sub_sampling = (4 >> q->sub_sampling);
    1577             : 
    1578        6624 :     for (ch = 0; ch < q->channels; ch++)
    1579     1134912 :         for (i = 0; i < q->frame_size; i++)
    1580     1130496 :             q->output_buffer[q->channels * i + ch] += (1 << 23) * q->samples[q->nb_channels * sub_sampling * i + ch];
    1581        2208 : }
    1582             : 
    1583             : /**
    1584             :  * Init static data (does not depend on specific file)
    1585             :  *
    1586             :  * @param q    context
    1587             :  */
    1588           2 : static av_cold void qdm2_init_static_data(void) {
    1589             :     static int done;
    1590             : 
    1591           2 :     if(done)
    1592           1 :         return;
    1593             : 
    1594           1 :     qdm2_init_vlc();
    1595           1 :     ff_mpa_synth_init_float(ff_mpa_synth_window_float);
    1596           1 :     softclip_table_init();
    1597           1 :     rnd_table_init();
    1598           1 :     init_noise_samples();
    1599             : 
    1600           1 :     done = 1;
    1601             : }
    1602             : 
    1603             : /**
    1604             :  * Init parameters from codec extradata
    1605             :  */
    1606           2 : static av_cold int qdm2_decode_init(AVCodecContext *avctx)
    1607             : {
    1608           2 :     QDM2Context *s = avctx->priv_data;
    1609             :     int tmp_val, tmp, size;
    1610             :     GetByteContext gb;
    1611             : 
    1612           2 :     qdm2_init_static_data();
    1613             : 
    1614             :     /* extradata parsing
    1615             : 
    1616             :     Structure:
    1617             :     wave {
    1618             :         frma (QDM2)
    1619             :         QDCA
    1620             :         QDCP
    1621             :     }
    1622             : 
    1623             :     32  size (including this field)
    1624             :     32  tag (=frma)
    1625             :     32  type (=QDM2 or QDMC)
    1626             : 
    1627             :     32  size (including this field, in bytes)
    1628             :     32  tag (=QDCA) // maybe mandatory parameters
    1629             :     32  unknown (=1)
    1630             :     32  channels (=2)
    1631             :     32  samplerate (=44100)
    1632             :     32  bitrate (=96000)
    1633             :     32  block size (=4096)
    1634             :     32  frame size (=256) (for one channel)
    1635             :     32  packet size (=1300)
    1636             : 
    1637             :     32  size (including this field, in bytes)
    1638             :     32  tag (=QDCP) // maybe some tuneable parameters
    1639             :     32  float1 (=1.0)
    1640             :     32  zero ?
    1641             :     32  float2 (=1.0)
    1642             :     32  float3 (=1.0)
    1643             :     32  unknown (27)
    1644             :     32  unknown (8)
    1645             :     32  zero ?
    1646             :     */
    1647             : 
    1648           2 :     if (!avctx->extradata || (avctx->extradata_size < 48)) {
    1649           0 :         av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
    1650           0 :         return AVERROR_INVALIDDATA;
    1651             :     }
    1652             : 
    1653           2 :     bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
    1654             : 
    1655          12 :     while (bytestream2_get_bytes_left(&gb) > 8) {
    1656          10 :         if (bytestream2_peek_be64(&gb) == (((uint64_t)MKBETAG('f','r','m','a') << 32) |
    1657             :                                             (uint64_t)MKBETAG('Q','D','M','2')))
    1658           2 :             break;
    1659           8 :         bytestream2_skip(&gb, 1);
    1660             :     }
    1661             : 
    1662           2 :     if (bytestream2_get_bytes_left(&gb) < 12) {
    1663           0 :         av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
    1664             :                bytestream2_get_bytes_left(&gb));
    1665           0 :         return AVERROR_INVALIDDATA;
    1666             :     }
    1667             : 
    1668           2 :     bytestream2_skip(&gb, 8);
    1669           2 :     size = bytestream2_get_be32(&gb);
    1670             : 
    1671           2 :     if (size > bytestream2_get_bytes_left(&gb)) {
    1672           0 :         av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
    1673             :                bytestream2_get_bytes_left(&gb), size);
    1674           0 :         return AVERROR_INVALIDDATA;
    1675             :     }
    1676             : 
    1677           2 :     av_log(avctx, AV_LOG_DEBUG, "size: %d\n", size);
    1678           2 :     if (bytestream2_get_be32(&gb) != MKBETAG('Q','D','C','A')) {
    1679           0 :         av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
    1680           0 :         return AVERROR_INVALIDDATA;
    1681             :     }
    1682             : 
    1683           2 :     bytestream2_skip(&gb, 4);
    1684             : 
    1685           2 :     avctx->channels = s->nb_channels = s->channels = bytestream2_get_be32(&gb);
    1686           2 :     if (s->channels <= 0 || s->channels > MPA_MAX_CHANNELS) {
    1687           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
    1688           0 :         return AVERROR_INVALIDDATA;
    1689             :     }
    1690           2 :     avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
    1691             :                                                    AV_CH_LAYOUT_MONO;
    1692             : 
    1693           2 :     avctx->sample_rate = bytestream2_get_be32(&gb);
    1694           2 :     avctx->bit_rate = bytestream2_get_be32(&gb);
    1695           2 :     s->group_size = bytestream2_get_be32(&gb);
    1696           2 :     s->fft_size = bytestream2_get_be32(&gb);
    1697           2 :     s->checksum_size = bytestream2_get_be32(&gb);
    1698           2 :     if (s->checksum_size >= 1U << 28) {
    1699           0 :         av_log(avctx, AV_LOG_ERROR, "data block size too large (%u)\n", s->checksum_size);
    1700           0 :         return AVERROR_INVALIDDATA;
    1701             :     }
    1702             : 
    1703           2 :     s->fft_order = av_log2(s->fft_size) + 1;
    1704             : 
    1705             :     // something like max decodable tones
    1706           2 :     s->group_order = av_log2(s->group_size) + 1;
    1707           2 :     s->frame_size = s->group_size / 16; // 16 iterations per super block
    1708             : 
    1709           2 :     if (s->frame_size > QDM2_MAX_FRAME_SIZE)
    1710           0 :         return AVERROR_INVALIDDATA;
    1711             : 
    1712           2 :     s->sub_sampling = s->fft_order - 7;
    1713           2 :     s->frequency_range = 255 / (1 << (2 - s->sub_sampling));
    1714             : 
    1715           2 :     switch ((s->sub_sampling * 2 + s->channels - 1)) {
    1716           0 :         case 0: tmp = 40; break;
    1717           0 :         case 1: tmp = 48; break;
    1718           0 :         case 2: tmp = 56; break;
    1719           0 :         case 3: tmp = 72; break;
    1720           0 :         case 4: tmp = 80; break;
    1721           2 :         case 5: tmp = 100;break;
    1722           0 :         default: tmp=s->sub_sampling; break;
    1723             :     }
    1724           2 :     tmp_val = 0;
    1725           2 :     if ((tmp * 1000) < avctx->bit_rate)  tmp_val = 1;
    1726           2 :     if ((tmp * 1440) < avctx->bit_rate)  tmp_val = 2;
    1727           2 :     if ((tmp * 1760) < avctx->bit_rate)  tmp_val = 3;
    1728           2 :     if ((tmp * 2240) < avctx->bit_rate)  tmp_val = 4;
    1729           2 :     s->cm_table_select = tmp_val;
    1730             : 
    1731           2 :     if (avctx->bit_rate <= 8000)
    1732           0 :         s->coeff_per_sb_select = 0;
    1733           2 :     else if (avctx->bit_rate < 16000)
    1734           0 :         s->coeff_per_sb_select = 1;
    1735             :     else
    1736           2 :         s->coeff_per_sb_select = 2;
    1737             : 
    1738             :     // Fail on unknown fft order
    1739           2 :     if ((s->fft_order < 7) || (s->fft_order > 9)) {
    1740           0 :         avpriv_request_sample(avctx, "Unknown FFT order %d", s->fft_order);
    1741           0 :         return AVERROR_PATCHWELCOME;
    1742             :     }
    1743           2 :     if (s->fft_size != (1 << (s->fft_order - 1))) {
    1744           0 :         av_log(avctx, AV_LOG_ERROR, "FFT size %d not power of 2.\n", s->fft_size);
    1745           0 :         return AVERROR_INVALIDDATA;
    1746             :     }
    1747             : 
    1748           2 :     ff_rdft_init(&s->rdft_ctx, s->fft_order, IDFT_C2R);
    1749           2 :     ff_mpadsp_init(&s->mpadsp);
    1750             : 
    1751           2 :     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
    1752             : 
    1753           2 :     return 0;
    1754             : }
    1755             : 
    1756           2 : static av_cold int qdm2_decode_close(AVCodecContext *avctx)
    1757             : {
    1758           2 :     QDM2Context *s = avctx->priv_data;
    1759             : 
    1760           2 :     ff_rdft_end(&s->rdft_ctx);
    1761             : 
    1762           2 :     return 0;
    1763             : }
    1764             : 
    1765        2208 : static int qdm2_decode(QDM2Context *q, const uint8_t *in, int16_t *out)
    1766             : {
    1767             :     int ch, i;
    1768        2208 :     const int frame_size = (q->frame_size * q->channels);
    1769             : 
    1770        2208 :     if((unsigned)frame_size > FF_ARRAY_ELEMS(q->output_buffer)/2)
    1771           0 :         return -1;
    1772             : 
    1773             :     /* select input buffer */
    1774        2208 :     q->compressed_data = in;
    1775        2208 :     q->compressed_size = q->checksum_size;
    1776             : 
    1777             :     /* copy old block, clear new block of output samples */
    1778        2208 :     memmove(q->output_buffer, &q->output_buffer[frame_size], frame_size * sizeof(float));
    1779        2208 :     memset(&q->output_buffer[frame_size], 0, frame_size * sizeof(float));
    1780             : 
    1781             :     /* decode block of QDM2 compressed data */
    1782        2208 :     if (q->sub_packet == 0) {
    1783         138 :         q->has_errors = 0; // zero it for a new super block
    1784         138 :         av_log(NULL,AV_LOG_DEBUG,"Superblock follows\n");
    1785         138 :         qdm2_decode_super_block(q);
    1786             :     }
    1787             : 
    1788             :     /* parse subpackets */
    1789        2208 :     if (!q->has_errors) {
    1790        2208 :         if (q->sub_packet == 2)
    1791         138 :             qdm2_decode_fft_packets(q);
    1792             : 
    1793        2208 :         qdm2_fft_tone_synthesizer(q, q->sub_packet);
    1794             :     }
    1795             : 
    1796             :     /* sound synthesis stage 1 (FFT) */
    1797        6624 :     for (ch = 0; ch < q->channels; ch++) {
    1798        4416 :         qdm2_calculate_fft(q, ch, q->sub_packet);
    1799             : 
    1800        4416 :         if (!q->has_errors && q->sub_packet_list_C[0].packet) {
    1801           0 :             SAMPLES_NEEDED_2("has errors, and C list is not empty")
    1802           0 :             return -1;
    1803             :         }
    1804             :     }
    1805             : 
    1806             :     /* sound synthesis stage 2 (MPEG audio like synthesis filter) */
    1807        2208 :     if (!q->has_errors && q->do_synth_filter)
    1808        2208 :         qdm2_synthesis_filter(q, q->sub_packet);
    1809             : 
    1810        2208 :     q->sub_packet = (q->sub_packet + 1) % 16;
    1811             : 
    1812             :     /* clip and convert output float[] to 16-bit signed samples */
    1813     1132704 :     for (i = 0; i < frame_size; i++) {
    1814     1130496 :         int value = (int)q->output_buffer[i];
    1815             : 
    1816     1130496 :         if (value > SOFTCLIP_THRESHOLD)
    1817         245 :             value = (value >  HARDCLIP_THRESHOLD) ?  32767 :  softclip_table[ value - SOFTCLIP_THRESHOLD];
    1818     1130251 :         else if (value < -SOFTCLIP_THRESHOLD)
    1819         839 :             value = (value < -HARDCLIP_THRESHOLD) ? -32767 : -softclip_table[-value - SOFTCLIP_THRESHOLD];
    1820             : 
    1821     1130496 :         out[i] = value;
    1822             :     }
    1823             : 
    1824        2208 :     return 0;
    1825             : }
    1826             : 
    1827         138 : static int qdm2_decode_frame(AVCodecContext *avctx, void *data,
    1828             :                              int *got_frame_ptr, AVPacket *avpkt)
    1829             : {
    1830         138 :     AVFrame *frame     = data;
    1831         138 :     const uint8_t *buf = avpkt->data;
    1832         138 :     int buf_size = avpkt->size;
    1833         138 :     QDM2Context *s = avctx->priv_data;
    1834             :     int16_t *out;
    1835             :     int i, ret;
    1836             : 
    1837         138 :     if(!buf)
    1838           0 :         return 0;
    1839         138 :     if(buf_size < s->checksum_size)
    1840           0 :         return -1;
    1841             : 
    1842             :     /* get output buffer */
    1843         138 :     frame->nb_samples = 16 * s->frame_size;
    1844         138 :     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
    1845           0 :         return ret;
    1846         138 :     out = (int16_t *)frame->data[0];
    1847             : 
    1848        2346 :     for (i = 0; i < 16; i++) {
    1849        2208 :         if ((ret = qdm2_decode(s, buf, out)) < 0)
    1850           0 :             return ret;
    1851        2208 :         out += s->channels * s->frame_size;
    1852             :     }
    1853             : 
    1854         138 :     *got_frame_ptr = 1;
    1855             : 
    1856         138 :     return s->checksum_size;
    1857             : }
    1858             : 
    1859             : AVCodec ff_qdm2_decoder = {
    1860             :     .name             = "qdm2",
    1861             :     .long_name        = NULL_IF_CONFIG_SMALL("QDesign Music Codec 2"),
    1862             :     .type             = AVMEDIA_TYPE_AUDIO,
    1863             :     .id               = AV_CODEC_ID_QDM2,
    1864             :     .priv_data_size   = sizeof(QDM2Context),
    1865             :     .init             = qdm2_decode_init,
    1866             :     .close            = qdm2_decode_close,
    1867             :     .decode           = qdm2_decode_frame,
    1868             :     .capabilities     = AV_CODEC_CAP_DR1,
    1869             : };

Generated by: LCOV version 1.13