LCOV - code coverage report
Current view: top level - libavcodec - qdmc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 10 350 2.9 %
Date: 2017-12-13 02:34:56 Functions: 1 17 5.9 %

          Line data    Source code
       1             : /*
       2             :  * QDMC compatible decoder
       3             :  * Copyright (c) 2017 Paul B Mahol
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include <math.h>
      23             : #include <stddef.h>
      24             : #include <stdio.h>
      25             : 
      26             : #define BITSTREAM_READER_LE
      27             : 
      28             : #include "libavutil/channel_layout.h"
      29             : 
      30             : #include "avcodec.h"
      31             : #include "bytestream.h"
      32             : #include "get_bits.h"
      33             : #include "internal.h"
      34             : #include "fft.h"
      35             : 
      36             : typedef struct QDMCTone {
      37             :     uint8_t mode;
      38             :     uint8_t phase;
      39             :     uint8_t offset;
      40             :     int16_t freq;
      41             :     int16_t amplitude;
      42             : } QDMCTone;
      43             : 
      44             : typedef struct QDMCContext {
      45             :     AVCodecContext *avctx;
      46             : 
      47             :     uint8_t frame_bits;
      48             :     int band_index;
      49             :     int frame_size;
      50             :     int subframe_size;
      51             :     int fft_offset;
      52             :     int buffer_offset;
      53             :     int nb_channels;
      54             :     int checksum_size;
      55             : 
      56             :     uint8_t noise[2][19][17];
      57             :     QDMCTone tones[5][8192];
      58             :     int nb_tones[5];
      59             :     int cur_tone[5];
      60             :     float alt_sin[5][31];
      61             :     float fft_buffer[4][8192 * 2];
      62             :     float noise2_buffer[4096 * 2];
      63             :     float noise_buffer[4096 * 2];
      64             :     float buffer[2 * 32768];
      65             :     float *buffer_ptr;
      66             :     int rndval;
      67             : 
      68             :     DECLARE_ALIGNED(32, FFTComplex, cmplx)[2][512];
      69             :     FFTContext fft_ctx;
      70             : } QDMCContext;
      71             : 
      72             : static float sin_table[512];
      73             : static VLC vtable[6];
      74             : 
      75             : static const unsigned code_prefix[] = {
      76             :     0x0, 0x1, 0x2, 0x3, 0x4, 0x6, 0x8, 0xA,
      77             :     0xC, 0x10, 0x14, 0x18, 0x1C, 0x24, 0x2C, 0x34,
      78             :     0x3C, 0x4C, 0x5C, 0x6C, 0x7C, 0x9C, 0xBC, 0xDC,
      79             :     0xFC, 0x13C, 0x17C, 0x1BC, 0x1FC, 0x27C, 0x2FC, 0x37C,
      80             :     0x3FC, 0x4FC, 0x5FC, 0x6FC, 0x7FC, 0x9FC, 0xBFC, 0xDFC,
      81             :     0xFFC, 0x13FC, 0x17FC, 0x1BFC, 0x1FFC, 0x27FC, 0x2FFC, 0x37FC,
      82             :     0x3FFC, 0x4FFC, 0x5FFC, 0x6FFC, 0x7FFC, 0x9FFC, 0xBFFC, 0xDFFC,
      83             :     0xFFFC, 0x13FFC, 0x17FFC, 0x1BFFC, 0x1FFFC, 0x27FFC, 0x2FFFC, 0x37FFC,
      84             :     0x3FFFC
      85             : };
      86             : 
      87             : static const float amplitude_tab[64] = {
      88             :     1.18750000f, 1.68359380f, 2.37500000f, 3.36718750f, 4.75000000f,
      89             :     6.73437500f, 9.50000000f, 13.4687500f, 19.0000000f, 26.9375000f,
      90             :     38.0000000f, 53.8750000f, 76.0000000f, 107.750000f, 152.000000f,
      91             :     215.500000f, 304.000000f, 431.000000f, 608.000000f, 862.000000f,
      92             :     1216.00000f, 1724.00000f, 2432.00000f, 3448.00000f, 4864.00000f,
      93             :     6896.00000f, 9728.00000f, 13792.0000f, 19456.0000f, 27584.0000f,
      94             :     38912.0000f, 55168.0000f, 77824.0000f, 110336.000f, 155648.000f,
      95             :     220672.000f, 311296.000f, 441344.000f, 622592.000f, 882688.000f,
      96             :     1245184.00f, 1765376.00f, 2490368.00f, 3530752.00f, 4980736.00f,
      97             :     7061504.00f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      98             : };
      99             : 
     100             : static const uint16_t qdmc_nodes[112] = {
     101             :     0, 1, 2, 4, 6, 8, 12, 16, 24, 32, 48, 56, 64,
     102             :     80, 96, 120, 144, 176, 208, 240, 256,
     103             :     0, 2, 4, 8, 16, 24, 32, 48, 56, 64, 80, 104,
     104             :     128, 160, 208, 256, 0, 0, 0, 0, 0,
     105             :     0, 2, 4, 8, 16, 32, 48, 64, 80, 112, 160, 208,
     106             :     256, 0, 0, 0, 0, 0, 0, 0, 0,
     107             :     0, 4, 8, 16, 32, 48, 64, 96, 144, 208, 256,
     108             :     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     109             :     0, 4, 16, 32, 64, 256, 0, 0, 0, 0, 0, 0, 0, 0,
     110             :     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
     111             : };
     112             : 
     113             : static const uint8_t noise_bands_size[] = {
     114             :     19, 14, 11, 9, 4, 2, 0
     115             : };
     116             : 
     117             : static const uint8_t noise_bands_selector[] = {
     118             :     4, 3, 2, 1, 0, 0, 0,
     119             : };
     120             : 
     121             : static const uint8_t noise_value_bits[] = {
     122             :     12, 7, 9, 7, 10, 9, 11, 9, 9, 2, 9, 9, 9, 9,
     123             :     9, 3, 9, 10, 10, 12, 2, 3, 3, 5, 5, 6, 7,
     124             : };
     125             : 
     126             : static const uint8_t noise_value_symbols[] = {
     127             :     0, 10, 11, 12, 13, 14, 15, 16, 18, 1, 20, 22, 24,
     128             :     26, 28, 2, 30, 32, 34, 36, 3, 4, 5, 6, 7, 8, 9,
     129             : };
     130             : 
     131             : static const uint16_t noise_value_codes[] = {
     132             :     0xC7A, 0x002, 0x0FA, 0x03A, 0x35A, 0x1C2, 0x07A, 0x1FA,
     133             :     0x17A, 0x000, 0x0DA, 0x142, 0x0C2, 0x042, 0x1DA, 0x001,
     134             :     0x05A, 0x15A, 0x27A, 0x47A, 0x003, 0x005, 0x006, 0x012,
     135             :     0x00A, 0x022, 0x01A,
     136             : };
     137             : 
     138             : static const uint8_t noise_segment_length_bits[] = {
     139             :     10, 8, 5, 1, 2, 4, 4, 4, 6, 7, 9, 10,
     140             : };
     141             : 
     142             : static const uint8_t noise_segment_length_symbols[] = {
     143             :     0, 13, 17, 1, 2, 3, 4, 5, 6, 7, 8, 9,
     144             : };
     145             : 
     146             : static const uint16_t noise_segment_length_codes[] = {
     147             :     0x30B, 0x8B, 0x1B, 0x0, 0x1, 0x3, 0x7, 0xF, 0x2b, 0x4B, 0xB, 0x10B,
     148             : };
     149             : 
     150             : static const uint8_t freq_diff_bits[] = {
     151             :     18, 2, 4, 4, 5, 4, 4, 5, 5, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 6,
     152             :     7, 6, 6, 6, 7, 7, 7, 7, 7, 8, 9, 9, 8, 9, 11, 11, 12, 12, 13, 12,
     153             :     14, 15, 18, 16, 17,
     154             : };
     155             : 
     156             : static const uint32_t freq_diff_codes[] = {
     157             :     0x2AD46, 0x1, 0x0, 0x3, 0xC, 0xA, 0x7, 0x18, 0x12, 0xE, 0x4, 0x16,
     158             :     0xF, 0x1C, 0x8, 0x22, 0x26, 0x2, 0x3B, 0x34, 0x74, 0x1F, 0x14, 0x2B,
     159             :     0x1B, 0x3F, 0x28, 0x54, 0x6, 0x4B, 0xB, 0x68, 0xE8, 0x46, 0xC6, 0x1E8,
     160             :     0x146, 0x346, 0x546, 0x746, 0x1D46, 0xF46, 0xD46, 0x6D46, 0xAD46, 0x2D46,
     161             :     0x1AD46,
     162             : };
     163             : 
     164             : static const uint8_t amplitude_bits[] = {
     165             :     13, 7, 8, 9, 10, 10, 10, 10, 10, 9, 8, 7, 6,
     166             :     5, 4, 3, 3, 2, 3, 3, 4, 5, 7, 8, 9, 11, 12, 13,
     167             : };
     168             : 
     169             : static const uint16_t amplitude_codes[] = {
     170             :     0x1EC6, 0x6, 0xC2, 0x142, 0x242, 0x246, 0xC6, 0x46, 0x42, 0x146, 0xA2,
     171             :     0x62, 0x26, 0x16, 0xE, 0x5, 0x4, 0x3, 0x0, 0x1, 0xA, 0x12, 0x2, 0x22,
     172             :     0x1C6, 0x2C6, 0x6C6, 0xEC6,
     173             : };
     174             : 
     175             : static const uint8_t amplitude_diff_bits[] = {
     176             :     8, 2, 1, 3, 4, 5, 6, 7, 8,
     177             : };
     178             : 
     179             : static const uint8_t amplitude_diff_codes[] = {
     180             :     0xFE, 0x0, 0x1, 0x2, 0x6, 0xE, 0x1E, 0x3E, 0x7E,
     181             : };
     182             : 
     183             : static const uint8_t phase_diff_bits[] = {
     184             :     6, 2, 2, 4, 4, 6, 5, 4, 2,
     185             : };
     186             : 
     187             : static const uint8_t phase_diff_codes[] = {
     188             :     0x35, 0x2, 0x0, 0x1, 0xD, 0x15, 0x5, 0x9, 0x3,
     189             : };
     190             : 
     191             : #define INIT_VLC_STATIC_LE(vlc, nb_bits, nb_codes,                 \
     192             :                            bits, bits_wrap, bits_size,             \
     193             :                            codes, codes_wrap, codes_size,          \
     194             :                            symbols, symbols_wrap, symbols_size,    \
     195             :                            static_size)                            \
     196             :     do {                                                           \
     197             :         static VLC_TYPE table[static_size][2];                     \
     198             :         (vlc)->table           = table;                            \
     199             :         (vlc)->table_allocated = static_size;                      \
     200             :         ff_init_vlc_sparse(vlc, nb_bits, nb_codes,                 \
     201             :                            bits, bits_wrap, bits_size,             \
     202             :                            codes, codes_wrap, codes_size,          \
     203             :                            symbols, symbols_wrap, symbols_size,    \
     204             :                            INIT_VLC_LE | INIT_VLC_USE_NEW_STATIC); \
     205             :     } while (0)
     206             : 
     207        5596 : static av_cold void qdmc_init_static_data(AVCodec *codec)
     208             : {
     209             :     int i;
     210             : 
     211        5596 :     INIT_VLC_STATIC_LE(&vtable[0], 12, FF_ARRAY_ELEMS(noise_value_bits),
     212             :                        noise_value_bits, 1, 1, noise_value_codes, 2, 2, noise_value_symbols, 1, 1, 4096);
     213        5596 :     INIT_VLC_STATIC_LE(&vtable[1], 10, FF_ARRAY_ELEMS(noise_segment_length_bits),
     214             :                        noise_segment_length_bits, 1, 1, noise_segment_length_codes, 2, 2,
     215             :                        noise_segment_length_symbols, 1, 1, 1024);
     216        5596 :     INIT_VLC_STATIC_LE(&vtable[2], 13, FF_ARRAY_ELEMS(amplitude_bits),
     217             :                        amplitude_bits, 1, 1, amplitude_codes, 2, 2, NULL, 0, 0, 8192);
     218        5596 :     INIT_VLC_STATIC_LE(&vtable[3], 18, FF_ARRAY_ELEMS(freq_diff_bits),
     219             :                        freq_diff_bits, 1, 1, freq_diff_codes, 4, 4, NULL, 0, 0, 262144);
     220        5596 :     INIT_VLC_STATIC_LE(&vtable[4], 8, FF_ARRAY_ELEMS(amplitude_diff_bits),
     221             :                        amplitude_diff_bits, 1, 1, amplitude_diff_codes, 1, 1, NULL, 0, 0, 256);
     222        5596 :     INIT_VLC_STATIC_LE(&vtable[5], 6, FF_ARRAY_ELEMS(phase_diff_bits),
     223             :                        phase_diff_bits, 1, 1, phase_diff_codes, 1, 1, NULL, 0, 0, 64);
     224             : 
     225     2870748 :     for (i = 0; i < 512; i++)
     226     2865152 :         sin_table[i] = sin(2.0f * i * M_PI * 0.001953125f);
     227        5596 : }
     228             : 
     229           0 : static void make_noises(QDMCContext *s)
     230             : {
     231             :     int i, j, n0, n1, n2, diff;
     232             :     float *nptr;
     233             : 
     234           0 :     for (j = 0; j < noise_bands_size[s->band_index]; j++) {
     235           0 :         n0 = qdmc_nodes[j + 21 * s->band_index    ];
     236           0 :         n1 = qdmc_nodes[j + 21 * s->band_index + 1];
     237           0 :         n2 = qdmc_nodes[j + 21 * s->band_index + 2];
     238           0 :         nptr = s->noise_buffer + 256 * j;
     239             : 
     240           0 :         for (i = 0; i + n0 < n1; i++, nptr++)
     241           0 :             nptr[0] = i / (float)(n1 - n0);
     242             : 
     243           0 :         diff = n2 - n1;
     244           0 :         nptr = s->noise_buffer + (j << 8) + n1 - n0;
     245             : 
     246           0 :         for (i = n1; i < n2; i++, nptr++, diff--)
     247           0 :             nptr[0] = diff / (float)(n2 - n1);
     248             :     }
     249           0 : }
     250             : 
     251           0 : static av_cold int qdmc_decode_init(AVCodecContext *avctx)
     252             : {
     253           0 :     QDMCContext *s = avctx->priv_data;
     254             :     int fft_size, fft_order, size, g, j, x;
     255             :     GetByteContext b;
     256             : 
     257           0 :     if (!avctx->extradata || (avctx->extradata_size < 48)) {
     258           0 :         av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
     259           0 :         return AVERROR_INVALIDDATA;
     260             :     }
     261             : 
     262           0 :     bytestream2_init(&b, avctx->extradata, avctx->extradata_size);
     263             : 
     264           0 :     while (bytestream2_get_bytes_left(&b) > 8) {
     265           0 :         if (bytestream2_peek_be64(&b) == (((uint64_t)MKBETAG('f','r','m','a') << 32) |
     266             :                                            (uint64_t)MKBETAG('Q','D','M','C')))
     267           0 :             break;
     268           0 :         bytestream2_skipu(&b, 1);
     269             :     }
     270           0 :     bytestream2_skipu(&b, 8);
     271             : 
     272           0 :     if (bytestream2_get_bytes_left(&b) < 36) {
     273           0 :         av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
     274             :                bytestream2_get_bytes_left(&b));
     275           0 :         return AVERROR_INVALIDDATA;
     276             :     }
     277             : 
     278           0 :     size = bytestream2_get_be32u(&b);
     279           0 :     if (size > bytestream2_get_bytes_left(&b)) {
     280           0 :         av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
     281             :                bytestream2_get_bytes_left(&b), size);
     282           0 :         return AVERROR_INVALIDDATA;
     283             :     }
     284             : 
     285           0 :     if (bytestream2_get_be32u(&b) != MKBETAG('Q','D','C','A')) {
     286           0 :         av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
     287           0 :         return AVERROR_INVALIDDATA;
     288             :     }
     289           0 :     bytestream2_skipu(&b, 4);
     290             : 
     291           0 :     avctx->channels = s->nb_channels = bytestream2_get_be32u(&b);
     292           0 :     if (s->nb_channels <= 0 || s->nb_channels > 2) {
     293           0 :         av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
     294           0 :         return AVERROR_INVALIDDATA;
     295             :     }
     296           0 :     avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
     297             :                                                    AV_CH_LAYOUT_MONO;
     298             : 
     299           0 :     avctx->sample_rate = bytestream2_get_be32u(&b);
     300           0 :     avctx->bit_rate = bytestream2_get_be32u(&b);
     301           0 :     bytestream2_skipu(&b, 4);
     302           0 :     fft_size = bytestream2_get_be32u(&b);
     303           0 :     fft_order = av_log2(fft_size) + 1;
     304           0 :     s->checksum_size = bytestream2_get_be32u(&b);
     305           0 :     if (s->checksum_size >= 1U << 28) {
     306           0 :         av_log(avctx, AV_LOG_ERROR, "data block size too large (%u)\n", s->checksum_size);
     307           0 :         return AVERROR_INVALIDDATA;
     308             :     }
     309             : 
     310           0 :     if (avctx->sample_rate >= 32000) {
     311           0 :         x = 28000;
     312           0 :         s->frame_bits = 13;
     313           0 :     } else if (avctx->sample_rate >= 16000) {
     314           0 :         x = 20000;
     315           0 :         s->frame_bits = 12;
     316             :     } else {
     317           0 :         x = 16000;
     318           0 :         s->frame_bits = 11;
     319             :     }
     320           0 :     s->frame_size = 1 << s->frame_bits;
     321           0 :     s->subframe_size = s->frame_size >> 5;
     322             : 
     323           0 :     if (avctx->channels == 2)
     324           0 :         x = 3 * x / 2;
     325           0 :     s->band_index = noise_bands_selector[FFMIN(6, llrint(floor(avctx->bit_rate * 3.0 / (double)x + 0.5)))];
     326             : 
     327           0 :     if ((fft_order < 7) || (fft_order > 9)) {
     328           0 :         avpriv_request_sample(avctx, "Unknown FFT order %d", fft_order);
     329           0 :         return AVERROR_PATCHWELCOME;
     330             :     }
     331             : 
     332           0 :     if (fft_size != (1 << (fft_order - 1))) {
     333           0 :         av_log(avctx, AV_LOG_ERROR, "FFT size %d not power of 2.\n", fft_size);
     334           0 :         return AVERROR_INVALIDDATA;
     335             :     }
     336             : 
     337           0 :     ff_fft_init(&s->fft_ctx, fft_order, 1);
     338             : 
     339           0 :     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
     340             : 
     341           0 :     for (g = 5; g > 0; g--) {
     342           0 :         for (j = 0; j < (1 << g) - 1; j++)
     343           0 :             s->alt_sin[5-g][j] = sin_table[(((j+1) << (8 - g)) & 0x1FF)];
     344             :     }
     345             : 
     346           0 :     make_noises(s);
     347             : 
     348           0 :     return 0;
     349             : }
     350             : 
     351           0 : static av_cold int qdmc_decode_close(AVCodecContext *avctx)
     352             : {
     353           0 :     QDMCContext *s = avctx->priv_data;
     354             : 
     355           0 :     ff_fft_end(&s->fft_ctx);
     356             : 
     357           0 :     return 0;
     358             : }
     359             : 
     360           0 : static int qdmc_get_vlc(GetBitContext *gb, VLC *table, int flag)
     361             : {
     362             :     int v;
     363             : 
     364           0 :     v = get_vlc2(gb, table->table, table->bits, 1);
     365           0 :     if (v < 0)
     366           0 :         return AVERROR_INVALIDDATA;
     367           0 :     if (v)
     368           0 :         v = v - 1;
     369             :     else
     370           0 :         v = get_bits(gb, get_bits(gb, 3) + 1);
     371             : 
     372           0 :     if (flag) {
     373           0 :         if (v >= FF_ARRAY_ELEMS(code_prefix))
     374           0 :             return AVERROR_INVALIDDATA;
     375             : 
     376           0 :         v = code_prefix[v] + get_bitsz(gb, v >> 2);
     377             :     }
     378             : 
     379           0 :     return v;
     380             : }
     381             : 
     382           0 : static int skip_label(QDMCContext *s, GetBitContext *gb)
     383             : {
     384           0 :     uint32_t label = get_bits_long(gb, 32);
     385           0 :     uint16_t sum = 226, checksum = get_bits(gb, 16);
     386           0 :     const uint8_t *ptr = gb->buffer + 6;
     387             :     int i;
     388             : 
     389           0 :     if (label != MKTAG('Q', 'M', 'C', 1))
     390           0 :         return AVERROR_INVALIDDATA;
     391             : 
     392           0 :     for (i = 0; i < s->checksum_size - 6; i++)
     393           0 :         sum += ptr[i];
     394             : 
     395           0 :     return sum != checksum;
     396             : }
     397             : 
     398           0 : static int read_noise_data(QDMCContext *s, GetBitContext *gb)
     399             : {
     400             :     int ch, j, k, v, idx, band, lastval, newval, len;
     401             : 
     402           0 :     for (ch = 0; ch < s->nb_channels; ch++) {
     403           0 :         for (band = 0; band < noise_bands_size[s->band_index]; band++) {
     404           0 :             v = qdmc_get_vlc(gb, &vtable[0], 0);
     405           0 :             if (v < 0)
     406           0 :                 return AVERROR_INVALIDDATA;
     407             : 
     408           0 :             if (v & 1)
     409           0 :                 v = v + 1;
     410             :             else
     411           0 :                 v = -v;
     412             : 
     413           0 :             lastval = v / 2;
     414           0 :             s->noise[ch][band][0] = lastval - 1;
     415           0 :             for (j = 0; j < 15;) {
     416           0 :                 len = qdmc_get_vlc(gb, &vtable[1], 1);
     417           0 :                 if (len < 0)
     418           0 :                     return AVERROR_INVALIDDATA;
     419           0 :                 len += 1;
     420             : 
     421           0 :                 v = qdmc_get_vlc(gb, &vtable[0], 0);
     422           0 :                 if (v < 0)
     423           0 :                     return AVERROR_INVALIDDATA;
     424             : 
     425           0 :                 if (v & 1)
     426           0 :                     newval = lastval + (v + 1) / 2;
     427             :                 else
     428           0 :                     newval = lastval - v / 2;
     429             : 
     430           0 :                 idx = j + 1;
     431           0 :                 if (len + idx > 16)
     432           0 :                     return AVERROR_INVALIDDATA;
     433             : 
     434           0 :                 for (k = 1; idx <= j + len; k++, idx++)
     435           0 :                     s->noise[ch][band][idx] = lastval + k * (newval - lastval) / len - 1;
     436             : 
     437           0 :                 lastval = newval;
     438           0 :                 j += len;
     439             :             }
     440             :         }
     441             :     }
     442             : 
     443           0 :     return 0;
     444             : }
     445             : 
     446           0 : static void add_tone(QDMCContext *s, int group, int offset, int freq, int stereo_mode, int amplitude, int phase)
     447             : {
     448           0 :     const int index = s->nb_tones[group];
     449             : 
     450           0 :     if (index >= FF_ARRAY_ELEMS(s->tones[group])) {
     451           0 :         av_log(s->avctx, AV_LOG_WARNING, "Too many tones already in buffer, ignoring tone!\n");
     452           0 :         return;
     453             :     }
     454             : 
     455           0 :     s->tones[group][index].offset    = offset;
     456           0 :     s->tones[group][index].freq      = freq;
     457           0 :     s->tones[group][index].mode      = stereo_mode;
     458           0 :     s->tones[group][index].amplitude = amplitude;
     459           0 :     s->tones[group][index].phase     = phase;
     460           0 :     s->nb_tones[group]++;
     461             : }
     462             : 
     463           0 : static int read_wave_data(QDMCContext *s, GetBitContext *gb)
     464             : {
     465           0 :     int amp, phase, stereo_mode = 0, i, group, freq, group_size, group_bits;
     466             :     int amp2, phase2, pos2, off;
     467             : 
     468           0 :     for (group = 0; group < 5; group++) {
     469           0 :         group_size = 1 << (s->frame_bits - group - 1);
     470           0 :         group_bits = 4 - group;
     471           0 :         pos2 = 0;
     472           0 :         off  = 0;
     473             : 
     474           0 :         for (i = 1; ; i = freq + 1) {
     475             :             int v;
     476             : 
     477           0 :             v = qdmc_get_vlc(gb, &vtable[3], 1);
     478           0 :             if (v < 0)
     479           0 :                 return AVERROR_INVALIDDATA;
     480             : 
     481           0 :             freq = i + v;
     482           0 :             while (freq >= group_size - 1) {
     483           0 :                 freq += 2 - group_size;
     484           0 :                 pos2 += group_size;
     485           0 :                 off  += 1 << group_bits;
     486             :             }
     487             : 
     488           0 :             if (pos2 >= s->frame_size)
     489           0 :                 break;
     490             : 
     491           0 :             if (s->nb_channels > 1)
     492           0 :                 stereo_mode = get_bits(gb, 2);
     493             : 
     494           0 :             amp   = qdmc_get_vlc(gb, &vtable[2], 0);
     495           0 :             if (amp < 0)
     496           0 :                 return AVERROR_INVALIDDATA;
     497           0 :             phase = get_bits(gb, 3);
     498             : 
     499           0 :             if (stereo_mode > 1) {
     500           0 :                 amp2   = qdmc_get_vlc(gb, &vtable[4], 0);
     501           0 :                 if (amp2 < 0)
     502           0 :                     return AVERROR_INVALIDDATA;
     503           0 :                 amp2   = amp - amp2;
     504             : 
     505           0 :                 phase2 = qdmc_get_vlc(gb, &vtable[5], 0);
     506           0 :                 if (phase2 < 0)
     507           0 :                     return AVERROR_INVALIDDATA;
     508           0 :                 phase2 = phase - phase2;
     509             : 
     510           0 :                 if (phase2 < 0)
     511           0 :                     phase2 += 8;
     512             :             }
     513             : 
     514           0 :             if ((freq >> group_bits) + 1 < s->subframe_size) {
     515           0 :                 add_tone(s, group, off, freq, stereo_mode & 1, amp, phase);
     516           0 :                 if (stereo_mode > 1)
     517           0 :                     add_tone(s, group, off, freq, ~stereo_mode & 1, amp2, phase2);
     518             :             }
     519             :         }
     520             :     }
     521             : 
     522           0 :     return 0;
     523             : }
     524             : 
     525           0 : static void lin_calc(QDMCContext *s, float amplitude, int node1, int node2, int index)
     526             : {
     527             :     int subframe_size, i, j, k, length;
     528             :     float scale, *noise_ptr;
     529             : 
     530           0 :     scale = 0.5 * amplitude;
     531           0 :     subframe_size = s->subframe_size;
     532           0 :     if (subframe_size >= node2)
     533           0 :         subframe_size = node2;
     534           0 :     length = (subframe_size - node1) & 0xFFFC;
     535           0 :     j = node1;
     536           0 :     noise_ptr = &s->noise_buffer[256 * index];
     537             : 
     538           0 :     for (i = 0; i < length; i += 4, j+= 4, noise_ptr += 4) {
     539           0 :         s->noise2_buffer[j    ] += scale * noise_ptr[0];
     540           0 :         s->noise2_buffer[j + 1] += scale * noise_ptr[1];
     541           0 :         s->noise2_buffer[j + 2] += scale * noise_ptr[2];
     542           0 :         s->noise2_buffer[j + 3] += scale * noise_ptr[3];
     543             :     }
     544             : 
     545           0 :     k = length + node1;
     546           0 :     noise_ptr = s->noise_buffer + length + (index << 8);
     547           0 :     for (i = length; i < subframe_size - node1; i++, k++, noise_ptr++)
     548           0 :         s->noise2_buffer[k] += scale * noise_ptr[0];
     549           0 : }
     550             : 
     551           0 : static void add_noise(QDMCContext *s, int ch, int current_subframe)
     552             : {
     553             :     int i, j, aindex;
     554             :     float amplitude;
     555           0 :     float *im = &s->fft_buffer[0 + ch][s->fft_offset + s->subframe_size * current_subframe];
     556           0 :     float *re = &s->fft_buffer[2 + ch][s->fft_offset + s->subframe_size * current_subframe];
     557             : 
     558           0 :     memset(s->noise2_buffer, 0, 4 * s->subframe_size);
     559             : 
     560           0 :     for (i = 0; i < noise_bands_size[s->band_index]; i++) {
     561           0 :         if (qdmc_nodes[i + 21 * s->band_index] > s->subframe_size - 1)
     562           0 :             break;
     563             : 
     564           0 :         aindex = s->noise[ch][i][current_subframe / 2];
     565           0 :         amplitude = aindex > 0 ? amplitude_tab[aindex & 0x3F] : 0.0f;
     566             : 
     567           0 :         lin_calc(s, amplitude, qdmc_nodes[21 * s->band_index + i],
     568           0 :                  qdmc_nodes[21 * s->band_index + i + 2], i);
     569             :     }
     570             : 
     571           0 :     for (j = 2; j < s->subframe_size - 1; j++) {
     572             :         float rnd_re, rnd_im;
     573             : 
     574           0 :         s->rndval = 214013 * s->rndval + 2531011;
     575           0 :         rnd_im = ((s->rndval & 0x7FFF) - 16384.0f) * 0.000030517578f * s->noise2_buffer[j];
     576           0 :         s->rndval = 214013 * s->rndval + 2531011;
     577           0 :         rnd_re = ((s->rndval & 0x7FFF) - 16384.0f) * 0.000030517578f * s->noise2_buffer[j];
     578           0 :         im[j  ] += rnd_im;
     579           0 :         re[j  ] += rnd_re;
     580           0 :         im[j+1] -= rnd_im;
     581           0 :         re[j+1] -= rnd_re;
     582             :     }
     583           0 : }
     584             : 
     585           0 : static void add_wave(QDMCContext *s, int offset, int freqs, int group, int stereo_mode, int amp, int phase)
     586             : {
     587             :     int j, group_bits, pos, pindex;
     588             :     float im, re, amplitude, level, *imptr, *reptr;
     589             : 
     590           0 :     if (s->nb_channels == 1)
     591           0 :         stereo_mode = 0;
     592             : 
     593           0 :     group_bits = 4 - group;
     594           0 :     pos = freqs >> (4 - group);
     595           0 :     amplitude = amplitude_tab[amp & 0x3F];
     596           0 :     imptr = &s->fft_buffer[    stereo_mode][s->fft_offset + s->subframe_size * offset + pos];
     597           0 :     reptr = &s->fft_buffer[2 + stereo_mode][s->fft_offset + s->subframe_size * offset + pos];
     598           0 :     pindex = (phase << 6) - ((2 * (freqs >> (4 - group)) + 1) << 7);
     599           0 :     for (j = 0; j < (1 << (group_bits + 1)) - 1; j++) {
     600           0 :         pindex += (2 * freqs + 1) << (7 - group_bits);
     601           0 :         level = amplitude * s->alt_sin[group][j];
     602           0 :         im = level * sin_table[ pindex        & 0x1FF];
     603           0 :         re = level * sin_table[(pindex + 128) & 0x1FF];
     604           0 :         imptr[0] += im;
     605           0 :         imptr[1] -= im;
     606           0 :         reptr[0] += re;
     607           0 :         reptr[1] -= re;
     608           0 :         imptr += s->subframe_size;
     609           0 :         reptr += s->subframe_size;
     610           0 :         if (imptr >= &s->fft_buffer[stereo_mode][2 * s->frame_size]) {
     611           0 :             imptr = &s->fft_buffer[0 + stereo_mode][pos];
     612           0 :             reptr = &s->fft_buffer[2 + stereo_mode][pos];
     613             :         }
     614             :     }
     615           0 : }
     616             : 
     617           0 : static void add_wave0(QDMCContext *s, int offset, int freqs, int stereo_mode, int amp, int phase)
     618             : {
     619             :     float level, im, re;
     620             :     int pos;
     621             : 
     622           0 :     if (s->nb_channels == 1)
     623           0 :         stereo_mode = 0;
     624             : 
     625           0 :     level = amplitude_tab[amp & 0x3F];
     626           0 :     im = level * sin_table[ (phase << 6)        & 0x1FF];
     627           0 :     re = level * sin_table[((phase << 6) + 128) & 0x1FF];
     628           0 :     pos = s->fft_offset + freqs + s->subframe_size * offset;
     629           0 :     s->fft_buffer[    stereo_mode][pos    ] += im;
     630           0 :     s->fft_buffer[2 + stereo_mode][pos    ] += re;
     631           0 :     s->fft_buffer[    stereo_mode][pos + 1] -= im;
     632           0 :     s->fft_buffer[2 + stereo_mode][pos + 1] -= re;
     633           0 : }
     634             : 
     635           0 : static void add_waves(QDMCContext *s, int current_subframe)
     636             : {
     637             :     int w, g;
     638             : 
     639           0 :     for (g = 0; g < 4; g++) {
     640           0 :         for (w = s->cur_tone[g]; w < s->nb_tones[g]; w++) {
     641           0 :             QDMCTone *t = &s->tones[g][w];
     642             : 
     643           0 :             if (current_subframe < t->offset)
     644           0 :                 break;
     645           0 :             add_wave(s, t->offset, t->freq, g, t->mode, t->amplitude, t->phase);
     646             :         }
     647           0 :         s->cur_tone[g] = w;
     648             :     }
     649           0 :     for (w = s->cur_tone[4]; w < s->nb_tones[4]; w++) {
     650           0 :         QDMCTone *t = &s->tones[4][w];
     651             : 
     652           0 :         if (current_subframe < t->offset)
     653           0 :             break;
     654           0 :         add_wave0(s, t->offset, t->freq, t->mode, t->amplitude, t->phase);
     655             :     }
     656           0 :     s->cur_tone[4] = w;
     657           0 : }
     658             : 
     659           0 : static int decode_frame(QDMCContext *s, GetBitContext *gb, int16_t *out)
     660             : {
     661             :     int ret, ch, i, n;
     662             : 
     663           0 :     if (skip_label(s, gb))
     664           0 :         return AVERROR_INVALIDDATA;
     665             : 
     666           0 :     s->fft_offset = s->frame_size - s->fft_offset;
     667           0 :     s->buffer_ptr = &s->buffer[s->nb_channels * s->buffer_offset];
     668             : 
     669           0 :     ret = read_noise_data(s, gb);
     670           0 :     if (ret < 0)
     671           0 :         return ret;
     672             : 
     673           0 :     ret = read_wave_data(s, gb);
     674           0 :     if (ret < 0)
     675           0 :         return ret;
     676             : 
     677           0 :     for (n = 0; n < 32; n++) {
     678             :         float *r;
     679             : 
     680           0 :         for (ch = 0; ch < s->nb_channels; ch++)
     681           0 :             add_noise(s, ch, n);
     682             : 
     683           0 :         add_waves(s, n);
     684             : 
     685           0 :         for (ch = 0; ch < s->nb_channels; ch++) {
     686           0 :             for (i = 0; i < s->subframe_size; i++) {
     687           0 :                 s->cmplx[ch][i].re = s->fft_buffer[ch + 2][s->fft_offset + n * s->subframe_size + i];
     688           0 :                 s->cmplx[ch][i].im = s->fft_buffer[ch + 0][s->fft_offset + n * s->subframe_size + i];
     689           0 :                 s->cmplx[ch][s->subframe_size + i].re = 0;
     690           0 :                 s->cmplx[ch][s->subframe_size + i].im = 0;
     691             :             }
     692             :         }
     693             : 
     694           0 :         for (ch = 0; ch < s->nb_channels; ch++) {
     695           0 :             s->fft_ctx.fft_permute(&s->fft_ctx, s->cmplx[ch]);
     696           0 :             s->fft_ctx.fft_calc(&s->fft_ctx, s->cmplx[ch]);
     697             :         }
     698             : 
     699           0 :         r = &s->buffer_ptr[s->nb_channels * n * s->subframe_size];
     700           0 :         for (i = 0; i < 2 * s->subframe_size; i++) {
     701           0 :             for (ch = 0; ch < s->nb_channels; ch++) {
     702           0 :                 *r++ += s->cmplx[ch][i].re;
     703             :             }
     704             :         }
     705             : 
     706           0 :         r = &s->buffer_ptr[n * s->subframe_size * s->nb_channels];
     707           0 :         for (i = 0; i < s->nb_channels * s->subframe_size; i++) {
     708           0 :             out[i] = av_clipf(r[i], INT16_MIN, INT16_MAX);
     709             :         }
     710           0 :         out += s->subframe_size * s->nb_channels;
     711             : 
     712           0 :         for (ch = 0; ch < s->nb_channels; ch++) {
     713           0 :             memset(s->fft_buffer[ch+0] + s->fft_offset + n * s->subframe_size, 0, 4 * s->subframe_size);
     714           0 :             memset(s->fft_buffer[ch+2] + s->fft_offset + n * s->subframe_size, 0, 4 * s->subframe_size);
     715             :         }
     716           0 :         memset(s->buffer + s->nb_channels * (n * s->subframe_size + s->frame_size + s->buffer_offset), 0, 4 * s->subframe_size * s->nb_channels);
     717             :     }
     718             : 
     719           0 :     s->buffer_offset += s->frame_size;
     720           0 :     if (s->buffer_offset >= 32768 - s->frame_size) {
     721           0 :         memcpy(s->buffer, &s->buffer[s->nb_channels * s->buffer_offset], 4 * s->frame_size * s->nb_channels);
     722           0 :         s->buffer_offset = 0;
     723             :     }
     724             : 
     725           0 :     return 0;
     726             : }
     727             : 
     728           0 : static av_cold void qdmc_flush(AVCodecContext *avctx)
     729             : {
     730           0 :     QDMCContext *s = avctx->priv_data;
     731             : 
     732           0 :     memset(s->buffer, 0, sizeof(s->buffer));
     733           0 :     memset(s->fft_buffer, 0, sizeof(s->fft_buffer));
     734           0 :     s->fft_offset = 0;
     735           0 :     s->buffer_offset = 0;
     736           0 : }
     737             : 
     738           0 : static int qdmc_decode_frame(AVCodecContext *avctx, void *data,
     739             :                              int *got_frame_ptr, AVPacket *avpkt)
     740             : {
     741           0 :     QDMCContext *s = avctx->priv_data;
     742           0 :     AVFrame *frame = data;
     743             :     GetBitContext gb;
     744             :     int ret;
     745             : 
     746           0 :     if (!avpkt->data)
     747           0 :         return 0;
     748           0 :     if (avpkt->size < s->checksum_size)
     749           0 :         return AVERROR_INVALIDDATA;
     750             : 
     751           0 :     s->avctx = avctx;
     752           0 :     frame->nb_samples = s->frame_size;
     753           0 :     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
     754           0 :         return ret;
     755             : 
     756           0 :     if ((ret = init_get_bits8(&gb, avpkt->data, s->checksum_size)) < 0)
     757           0 :         return ret;
     758             : 
     759           0 :     memset(s->nb_tones, 0, sizeof(s->nb_tones));
     760           0 :     memset(s->cur_tone, 0, sizeof(s->cur_tone));
     761             : 
     762           0 :     ret = decode_frame(s, &gb, (int16_t *)frame->data[0]);
     763           0 :     if (ret >= 0) {
     764           0 :         *got_frame_ptr = 1;
     765           0 :         return s->checksum_size;
     766             :     }
     767           0 :     qdmc_flush(avctx);
     768           0 :     return ret;
     769             : }
     770             : 
     771             : AVCodec ff_qdmc_decoder = {
     772             :     .name             = "qdmc",
     773             :     .long_name        = NULL_IF_CONFIG_SMALL("QDesign Music Codec 1"),
     774             :     .type             = AVMEDIA_TYPE_AUDIO,
     775             :     .id               = AV_CODEC_ID_QDMC,
     776             :     .priv_data_size   = sizeof(QDMCContext),
     777             :     .init             = qdmc_decode_init,
     778             :     .init_static_data = qdmc_init_static_data,
     779             :     .close            = qdmc_decode_close,
     780             :     .decode           = qdmc_decode_frame,
     781             :     .flush            = qdmc_flush,
     782             :     .capabilities     = AV_CODEC_CAP_DR1,
     783             : };

Generated by: LCOV version 1.13