]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/qdm2.c
lzf: update pointer p after realloc
[ffmpeg] / libavcodec / qdm2.c
index 6343ce3d5e20e2e407543641b863bdf66848ba84..7a7c149c2b64ca7b5fd3ff41b730a88e6d74f52c 100644 (file)
 #include <stddef.h>
 #include <stdio.h>
 
-#define BITSTREAM_READER_LE
 #include "libavutil/channel_layout.h"
+
+#define BITSTREAM_READER_LE
 #include "avcodec.h"
 #include "get_bits.h"
 #include "internal.h"
-#include "rdft.h"
-#include "mpegaudiodsp.h"
 #include "mpegaudio.h"
+#include "mpegaudiodsp.h"
+#include "rdft.h"
 
 #include "qdm2data.h"
 #include "qdm2_tablegen.h"
 
-#undef NDEBUG
-#include <assert.h>
-
 
 #define QDM2_LIST_ADD(list, size, packet) \
 do { \
@@ -83,7 +81,7 @@ typedef int8_t sb_int8_array[2][30][64];
 /**
  * Subpacket
  */
-typedef struct {
+typedef struct QDM2SubPacket {
     int type;            ///< subpacket type
     unsigned int size;   ///< subpacket size
     const uint8_t *data; ///< pointer to subpacket data (points to input data buffer, it's not a private copy)
@@ -97,12 +95,12 @@ typedef struct QDM2SubPNode {
     struct QDM2SubPNode *next; ///< pointer to next packet in the list, NULL if leaf node
 } QDM2SubPNode;
 
-typedef struct {
+typedef struct QDM2Complex {
     float re;
     float im;
 } QDM2Complex;
 
-typedef struct {
+typedef struct FFTTone {
     float level;
     QDM2Complex *complex;
     const float *table;
@@ -113,7 +111,7 @@ typedef struct {
     short cutoff;
 } FFTTone;
 
-typedef struct {
+typedef struct FFTCoefficient {
     int16_t sub_packet;
     uint8_t channel;
     int16_t offset;
@@ -121,14 +119,14 @@ typedef struct {
     uint8_t phase;
 } FFTCoefficient;
 
-typedef struct {
+typedef struct QDM2FFT {
     DECLARE_ALIGNED(32, QDM2Complex, complex)[MPA_MAX_CHANNELS][256];
 } QDM2FFT;
 
 /**
  * QDM2 decoder context
  */
-typedef struct {
+typedef struct QDM2Context {
     /// Parameters from codec header, do not change during playback
     int nb_channels;         ///< number of channels
     int channels;            ///< number of channels
@@ -395,7 +393,7 @@ static int qdm2_get_se_vlc(VLC *vlc, GetBitContext *gb, int depth)
 /**
  * QDM2 checksum
  *
- * @param data      pointer to data to be checksum'ed
+ * @param data      pointer to data to be checksummed
  * @param length    data length
  * @param value     checksum value
  *
@@ -455,7 +453,7 @@ static void qdm2_decode_sub_packet_header(GetBitContext *gb,
 static QDM2SubPNode *qdm2_search_subpacket_type_in_list(QDM2SubPNode *list,
                                                         int type)
 {
-    while (list != NULL && list->packet != NULL) {
+    while (list && list->packet) {
         if (list->packet->type == type)
             return list;
         list = list->next;
@@ -525,8 +523,8 @@ static void build_sb_samples_from_noise(QDM2Context *q, int sb)
  * @param channels         number of channels
  * @param coding_method    q->coding_method[0][0][0]
  */
-static void fix_coding_method_array(int sb, int channels,
-                                    sb_int8_array coding_method)
+static int fix_coding_method_array(int sb, int channels,
+                                   sb_int8_array coding_method)
 {
     int j, k;
     int ch;
@@ -534,6 +532,8 @@ static void fix_coding_method_array(int sb, int channels,
 
     for (ch = 0; ch < channels; ch++) {
         for (j = 0; j < 64; ) {
+            if (coding_method[ch][sb][j] < 8)
+                return -1;
             if ((coding_method[ch][sb][j] - 8) > 22) {
                 run      = 1;
                 case_val = 8;
@@ -579,6 +579,7 @@ static void fix_coding_method_array(int sb, int channels,
             j += run;
         }
     }
+    return 0;
 }
 
 /**
@@ -721,67 +722,68 @@ static void fill_coding_method_array(sb_int8_array tone_level_idx,
                 }
                 tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1];
             }
-            acc = 0;
-            for (ch = 0; ch < nb_channels; ch++)
-                for (sb = 0; sb < 30; sb++)
-                    for (j = 0; j < 64; j++)
-                        acc += tone_level_idx_temp[ch][sb][j];
-
-            multres = 0x66666667 * (acc * 10);
-            esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31);
-            for (ch = 0;  ch < nb_channels; ch++)
-                for (sb = 0; sb < 30; sb++)
-                    for (j = 0; j < 64; j++) {
-                        comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10;
-                        if (comp < 0)
-                            comp += 0xff;
-                        comp /= 256; // signed shift
-                        switch(sb) {
-                            case 0:
-                                if (comp < 30)
-                                    comp = 30;
-                                comp += 15;
-                                break;
-                            case 1:
-                                if (comp < 24)
-                                    comp = 24;
-                                comp += 10;
-                                break;
-                            case 2:
-                            case 3:
-                            case 4:
-                                if (comp < 16)
-                                    comp = 16;
-                        }
-                        if (comp <= 5)
-                            tmp = 0;
-                        else if (comp <= 10)
-                            tmp = 10;
-                        else if (comp <= 16)
-                            tmp = 16;
-                        else if (comp <= 24)
-                            tmp = -1;
-                        else
-                            tmp = 0;
-                        coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff;
+
+        acc = 0;
+        for (ch = 0; ch < nb_channels; ch++)
+            for (sb = 0; sb < 30; sb++)
+                for (j = 0; j < 64; j++)
+                    acc += tone_level_idx_temp[ch][sb][j];
+
+        multres = 0x66666667LL * (acc * 10);
+        esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31);
+        for (ch = 0;  ch < nb_channels; ch++)
+            for (sb = 0; sb < 30; sb++)
+                for (j = 0; j < 64; j++) {
+                    comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10;
+                    if (comp < 0)
+                        comp += 0xff;
+                    comp /= 256; // signed shift
+                    switch(sb) {
+                        case 0:
+                            if (comp < 30)
+                                comp = 30;
+                            comp += 15;
+                            break;
+                        case 1:
+                            if (comp < 24)
+                                comp = 24;
+                            comp += 10;
+                            break;
+                        case 2:
+                        case 3:
+                        case 4:
+                            if (comp < 16)
+                                comp = 16;
                     }
+                    if (comp <= 5)
+                        tmp = 0;
+                    else if (comp <= 10)
+                        tmp = 10;
+                    else if (comp <= 16)
+                        tmp = 16;
+                    else if (comp <= 24)
+                        tmp = -1;
+                    else
+                        tmp = 0;
+                    coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff;
+                }
+        for (sb = 0; sb < 30; sb++)
+            fix_coding_method_array(sb, nb_channels, coding_method);
+        for (ch = 0; ch < nb_channels; ch++)
             for (sb = 0; sb < 30; sb++)
-                fix_coding_method_array(sb, nb_channels, coding_method);
-            for (ch = 0; ch < nb_channels; ch++)
-                for (sb = 0; sb < 30; sb++)
-                    for (j = 0; j < 64; j++)
-                        if (sb >= 10) {
-                            if (coding_method[ch][sb][j] < 10)
-                                coding_method[ch][sb][j] = 10;
+                for (j = 0; j < 64; j++)
+                    if (sb >= 10) {
+                        if (coding_method[ch][sb][j] < 10)
+                            coding_method[ch][sb][j] = 10;
+                    } else {
+                        if (sb >= 2) {
+                            if (coding_method[ch][sb][j] < 16)
+                                coding_method[ch][sb][j] = 16;
                         } else {
-                            if (sb >= 2) {
-                                if (coding_method[ch][sb][j] < 16)
-                                    coding_method[ch][sb][j] = 16;
-                            } else {
-                                if (coding_method[ch][sb][j] < 30)
-                                    coding_method[ch][sb][j] = 30;
-                            }
+                            if (coding_method[ch][sb][j] < 30)
+                                coding_method[ch][sb][j] = 30;
                         }
+                    }
     } else { // superblocktype_2_3 != 0
         for (ch = 0; ch < nb_channels; ch++)
             for (sb = 0; sb < 30; sb++)
@@ -791,7 +793,6 @@ static void fill_coding_method_array(sb_int8_array tone_level_idx,
 }
 
 /**
- *
  * Called by process_subpacket_11 to process more data from subpacket 11
  * with sb 0-8.
  * Called by process_subpacket_12 to process data from subpacket 12 with
@@ -807,7 +808,7 @@ static void synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb,
                                        int length, int sb_min, int sb_max)
 {
     int sb, j, k, n, ch, run, channels;
-    int joined_stereo, zero_encoding, chs;
+    int joined_stereo, zero_encoding;
     int type34_first;
     float type34_div = 0;
     float type34_predictor;
@@ -816,7 +817,7 @@ static void synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb,
     if (length == 0) {
         // If no data use noise
         for (sb=sb_min; sb < sb_max; sb++)
-            build_sb_samples_from_noise (q, sb);
+            build_sb_samples_from_noise(q, sb);
 
         return;
     }
@@ -829,18 +830,22 @@ static void synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb,
         else if (sb >= 24)
             joined_stereo = 1;
         else
-            joined_stereo = (get_bits_left(gb) >= 1) ? get_bits1 (gb) : 0;
+            joined_stereo = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0;
 
         if (joined_stereo) {
             if (get_bits_left(gb) >= 16)
                 for (j = 0; j < 16; j++)
-                    sign_bits[j] = get_bits1 (gb);
+                    sign_bits[j] = get_bits1(gb);
 
             for (j = 0; j < 64; j++)
                 if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j])
                     q->coding_method[0][sb][j] = q->coding_method[1][sb][j];
 
-            fix_coding_method_array(sb, q->nb_channels, q->coding_method);
+            if (fix_coding_method_array(sb, q->nb_channels,
+                                            q->coding_method)) {
+                build_sb_samples_from_noise(q, sb);
+                continue;
+            }
             channels = 1;
         }
 
@@ -961,16 +966,18 @@ static void synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb,
                 }
 
                 if (joined_stereo) {
-                    float tmp[10][MPA_MAX_CHANNELS];
-
-                    for (k = 0; k < run; k++) {
-                        tmp[k][0] = samples[k];
-                        tmp[k][1] = (sign_bits[(j + k) / 8]) ? -samples[k] : samples[k];
+                    for (k = 0; k < run && j + k < 128; k++) {
+                        q->sb_samples[0][j + k][sb] =
+                            q->tone_level[0][sb][(j + k) / 2] * samples[k];
+                        if (q->nb_channels == 2) {
+                            if (sign_bits[(j + k) / 8])
+                                q->sb_samples[1][j + k][sb] =
+                                    q->tone_level[1][sb][(j + k) / 2] * -samples[k];
+                            else
+                                q->sb_samples[1][j + k][sb] =
+                                    q->tone_level[1][sb][(j + k) / 2] * samples[k];
+                        }
                     }
-                    for (chs = 0; chs < q->nb_channels; chs++)
-                        for (k = 0; k < run; k++)
-                            if ((j + k) < 128)
-                                q->sb_samples[chs][j + k][sb] = q->tone_level[chs][sb][((j + k)/2)] * tmp[k][chs];
                 } else {
                     for (k = 0; k < run; k++)
                         if ((j + k) < 128)
@@ -1030,7 +1037,7 @@ static void init_quantized_coeffs_elem0(int8_t *quantized_coeffs,
  * @param q         context
  * @param gb        bitreader context
  */
-static void init_tone_level_dequantization (QDM2Context *q, GetBitContext *gb)
+static void init_tone_level_dequantization(QDM2Context *q, GetBitContext *gb)
 {
     int sb, j, k, n, ch;
 
@@ -1202,23 +1209,23 @@ static void process_synthesis_subpackets(QDM2Context *q, QDM2SubPNode *list)
     QDM2SubPNode *nodes[4];
 
     nodes[0] = qdm2_search_subpacket_type_in_list(list, 9);
-    if (nodes[0] != NULL)
+    if (nodes[0])
         process_subpacket_9(q, nodes[0]);
 
     nodes[1] = qdm2_search_subpacket_type_in_list(list, 10);
-    if (nodes[1] != NULL)
+    if (nodes[1])
         process_subpacket_10(q, nodes[1]);
     else
         process_subpacket_10(q, NULL);
 
     nodes[2] = qdm2_search_subpacket_type_in_list(list, 11);
-    if (nodes[0] != NULL && nodes[1] != NULL && nodes[2] != NULL)
+    if (nodes[0] && nodes[1] && nodes[2])
         process_subpacket_11(q, nodes[2]);
     else
         process_subpacket_11(q, NULL);
 
     nodes[3] = qdm2_search_subpacket_type_in_list(list, 12);
-    if (nodes[0] != NULL && nodes[1] != NULL && nodes[3] != NULL)
+    if (nodes[0] && nodes[1] && nodes[3])
         process_subpacket_12(q, nodes[3]);
     else
         process_subpacket_12(q, NULL);
@@ -1343,7 +1350,7 @@ static void qdm2_decode_super_block(QDM2Context *q)
         }
     } // Packet bytes loop
 
-    if (q->sub_packet_list_D[0].packet != NULL) {
+    if (q->sub_packet_list_D[0].packet) {
         process_synthesis_subpackets(q, q->sub_packet_list_D);
         q->do_synth_filter = 1;
     } else if (q->do_synth_filter) {
@@ -1455,7 +1462,7 @@ static void qdm2_decode_fft_packets(QDM2Context *q)
     int i, j, min, max, value, type, unknown_flag;
     GetBitContext gb;
 
-    if (q->sub_packet_list_B[0].packet == NULL)
+    if (!q->sub_packet_list_B[0].packet)
         return;
 
     /* reset minimum indexes for FFT coefficients */
@@ -1753,7 +1760,7 @@ static av_cold int qdm2_decode_init(AVCodecContext *avctx)
 
     if (!avctx->extradata || (avctx->extradata_size < 48)) {
         av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     extradata      = avctx->extradata;
@@ -1769,18 +1776,18 @@ static av_cold int qdm2_decode_init(AVCodecContext *avctx)
     if (extradata_size < 12) {
         av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
                extradata_size);
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     if (memcmp(extradata, "frmaQDM", 7)) {
         av_log(avctx, AV_LOG_ERROR, "invalid headers, QDM? not found\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     if (extradata[7] == 'C') {
 //        s->is_qdmc = 1;
-        av_log(avctx, AV_LOG_ERROR, "stream is QDMC version 1, which is not supported\n");
-        return -1;
+        avpriv_report_missing_feature(avctx, "QDMC version 1");
+        return AVERROR_PATCHWELCOME;
     }
 
     extradata += 8;
@@ -1791,14 +1798,14 @@ static av_cold int qdm2_decode_init(AVCodecContext *avctx)
     if(size > extradata_size){
         av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
                extradata_size, size);
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     extradata += 4;
     av_log(avctx, AV_LOG_DEBUG, "size: %d\n", size);
     if (AV_RB32(extradata) != MKBETAG('Q','D','C','A')) {
         av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     extradata += 8;
@@ -1873,8 +1880,8 @@ static av_cold int qdm2_decode_init(AVCodecContext *avctx)
 
     // Fail on unknown fft order
     if ((s->fft_order < 7) || (s->fft_order > 9)) {
-        av_log(avctx, AV_LOG_ERROR, "Unknown FFT order (%d), contact the developers!\n", s->fft_order);
-        return -1;
+        avpriv_request_sample(avctx, "Unknown FFT order %d", s->fft_order);
+        return AVERROR_PATCHWELCOME;
     }
     if (s->fft_size != (1 << (s->fft_order - 1))) {
         av_log(avctx, AV_LOG_ERROR, "FFT size %d not power of 2.\n", s->fft_size);
@@ -1930,7 +1937,7 @@ static int qdm2_decode(QDM2Context *q, const uint8_t *in, int16_t *out)
     for (ch = 0; ch < q->channels; ch++) {
         qdm2_calculate_fft(q, ch, q->sub_packet);
 
-        if (!q->has_errors && q->sub_packet_list_C[0].packet != NULL) {
+        if (!q->has_errors && q->sub_packet_list_C[0].packet) {
             SAMPLES_NEEDED_2("has errors, and C list is not empty")
             return -1;
         }
@@ -1942,7 +1949,7 @@ static int qdm2_decode(QDM2Context *q, const uint8_t *in, int16_t *out)
 
     q->sub_packet = (q->sub_packet + 1) % 16;
 
-    /* clip and convert output float[] to 16bit signed samples */
+    /* clip and convert output float[] to 16-bit signed samples */
     for (i = 0; i < frame_size; i++) {
         int value = (int)q->output_buffer[i];
 
@@ -1981,8 +1988,8 @@ static int qdm2_decode_frame(AVCodecContext *avctx, void *data,
     out = (int16_t *)frame->data[0];
 
     for (i = 0; i < 16; i++) {
-        if (qdm2_decode(s, buf, out) < 0)
-            return -1;
+        if ((ret = qdm2_decode(s, buf, out)) < 0)
+            return ret;
         out += s->channels * s->frame_size;
     }
 
@@ -1993,6 +2000,7 @@ static int qdm2_decode_frame(AVCodecContext *avctx, void *data,
 
 AVCodec ff_qdm2_decoder = {
     .name             = "qdm2",
+    .long_name        = NULL_IF_CONFIG_SMALL("QDesign Music Codec 2"),
     .type             = AVMEDIA_TYPE_AUDIO,
     .id               = AV_CODEC_ID_QDM2,
     .priv_data_size   = sizeof(QDM2Context),
@@ -2000,6 +2008,5 @@ AVCodec ff_qdm2_decoder = {
     .init_static_data = qdm2_init_static_data,
     .close            = qdm2_decode_close,
     .decode           = qdm2_decode_frame,
-    .capabilities     = CODEC_CAP_DR1,
-    .long_name        = NULL_IF_CONFIG_SMALL("QDesign Music Codec 2"),
+    .capabilities     = AV_CODEC_CAP_DR1,
 };