static av_cold int qdm2_decode_init(AVCodecContext *avctx)

{

    QDM2Context *s = avctx->priv_data;

    uint8_t *extradata;

    int extradata_size;

    int tmp_val, tmp, size;



    /* extradata parsing



    Structure:

    wave {

        frma (QDM2)

        QDCA

        QDCP

    }



    32  size (including this field)

    32  tag (=frma)

    32  type (=QDM2 or QDMC)



    32  size (including this field, in bytes)

    32  tag (=QDCA) // maybe mandatory parameters

    32  unknown (=1)

    32  channels (=2)

    32  samplerate (=44100)

    32  bitrate (=96000)

    32  block size (=4096)

    32  frame size (=256) (for one channel)

    32  packet size (=1300)



    32  size (including this field, in bytes)

    32  tag (=QDCP) // maybe some tuneable parameters

    32  float1 (=1.0)

    32  zero ?

    32  float2 (=1.0)

    32  float3 (=1.0)

    32  unknown (27)

    32  unknown (8)

    32  zero ?

    */



    if (!avctx->extradata || (avctx->extradata_size < 48)) {

        av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");

        return -1;

    }



    extradata = avctx->extradata;

    extradata_size = avctx->extradata_size;



    while (extradata_size > 7) {

        if (!memcmp(extradata, "frmaQDM", 7))

            break;

        extradata++;

        extradata_size--;

    }



    if (extradata_size < 12) {

        av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",

               extradata_size);

        return -1;

    }



    if (memcmp(extradata, "frmaQDM", 7)) {

        av_log(avctx, AV_LOG_ERROR, "invalid headers, QDM? not found\n");

        return -1;

    }



    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;

    }



    extradata += 8;

    extradata_size -= 8;



    size = AV_RB32(extradata);



    if(size > extradata_size){

        av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",

               extradata_size, size);

        return -1;

    }



    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;

    }



    extradata += 8;



    avctx->channels = s->nb_channels = s->channels = AV_RB32(extradata);

    extradata += 4;

    if (s->channels > MPA_MAX_CHANNELS)




    avctx->sample_rate = AV_RB32(extradata);

    extradata += 4;



    avctx->bit_rate = AV_RB32(extradata);

    extradata += 4;



    s->group_size = AV_RB32(extradata);

    extradata += 4;



    s->fft_size = AV_RB32(extradata);

    extradata += 4;



    s->checksum_size = AV_RB32(extradata);



    s->fft_order = av_log2(s->fft_size) + 1;

    s->fft_frame_size = 2 * s->fft_size; // complex has two floats



    // something like max decodable tones

    s->group_order = av_log2(s->group_size) + 1;

    s->frame_size = s->group_size / 16; // 16 iterations per super block





    s->sub_sampling = s->fft_order - 7;

    s->frequency_range = 255 / (1 << (2 - s->sub_sampling));



    switch ((s->sub_sampling * 2 + s->channels - 1)) {

        case 0: tmp = 40; break;

        case 1: tmp = 48; break;

        case 2: tmp = 56; break;

        case 3: tmp = 72; break;

        case 4: tmp = 80; break;

        case 5: tmp = 100;break;

        default: tmp=s->sub_sampling; break;

    }

    tmp_val = 0;

    if ((tmp * 1000) < avctx->bit_rate)  tmp_val = 1;

    if ((tmp * 1440) < avctx->bit_rate)  tmp_val = 2;

    if ((tmp * 1760) < avctx->bit_rate)  tmp_val = 3;

    if ((tmp * 2240) < avctx->bit_rate)  tmp_val = 4;

    s->cm_table_select = tmp_val;



    if (s->sub_sampling == 0)

        tmp = 7999;

    else

        tmp = ((-(s->sub_sampling -1)) & 8000) + 20000;

    /*

    0: 7999 -> 0

    1: 20000 -> 2

    2: 28000 -> 2

    */

    if (tmp < 8000)

        s->coeff_per_sb_select = 0;

    else if (tmp <= 16000)

        s->coeff_per_sb_select = 1;

    else

        s->coeff_per_sb_select = 2;



    // 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;

    }



    ff_rdft_init(&s->rdft_ctx, s->fft_order, IDFT_C2R);

    ff_mpadsp_init(&s->mpadsp);



    qdm2_init(s);



    avctx->sample_fmt = AV_SAMPLE_FMT_S16;



//    dump_context(s);

    return 0;

}
static av_cold int qdm2_decode_init(AVCodecContext *avctx)

{

    QDM2Context *s = avctx->priv_data;

    uint8_t *extradata;

    int extradata_size;

    int tmp_val, tmp, size;



    



































































    if (!avctx->extradata || (avctx->extradata_size < 48)) {

        av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");

        return -1;

    }



    extradata = avctx->extradata;

    extradata_size = avctx->extradata_size;



    while (extradata_size > 7) {

        if (!memcmp(extradata, "frmaQDM", 7))

            break;

        extradata++;

        extradata_size--;

    }



    if (extradata_size < 12) {

        av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",

               extradata_size);

        return -1;

    }



    if (memcmp(extradata, "frmaQDM", 7)) {

        av_log(avctx, AV_LOG_ERROR, "invalid headers, QDM? not found\n");

        return -1;

    }



    if (extradata[7] == 'C') {



        av_log(avctx, AV_LOG_ERROR, "stream is QDMC version 1, which is not supported\n");

        return -1;

    }



    extradata += 8;

    extradata_size -= 8;



    size = AV_RB32(extradata);



    if(size > extradata_size){

        av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",

               extradata_size, size);

        return -1;

    }



    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;

    }



    extradata += 8;



    avctx->channels = s->nb_channels = s->channels = AV_RB32(extradata);

    extradata += 4;

    if (s->channels > MPA_MAX_CHANNELS)




    avctx->sample_rate = AV_RB32(extradata);

    extradata += 4;



    avctx->bit_rate = AV_RB32(extradata);

    extradata += 4;



    s->group_size = AV_RB32(extradata);

    extradata += 4;



    s->fft_size = AV_RB32(extradata);

    extradata += 4;



    s->checksum_size = AV_RB32(extradata);



    s->fft_order = av_log2(s->fft_size) + 1;

    s->fft_frame_size = 2 * s->fft_size; 



    

    s->group_order = av_log2(s->group_size) + 1;

    s->frame_size = s->group_size / 16; 





    s->sub_sampling = s->fft_order - 7;

    s->frequency_range = 255 / (1 << (2 - s->sub_sampling));



    switch ((s->sub_sampling * 2 + s->channels - 1)) {

        case 0: tmp = 40; break;

        case 1: tmp = 48; break;

        case 2: tmp = 56; break;

        case 3: tmp = 72; break;

        case 4: tmp = 80; break;

        case 5: tmp = 100;break;

        default: tmp=s->sub_sampling; break;

    }

    tmp_val = 0;

    if ((tmp * 1000) < avctx->bit_rate)  tmp_val = 1;

    if ((tmp * 1440) < avctx->bit_rate)  tmp_val = 2;

    if ((tmp * 1760) < avctx->bit_rate)  tmp_val = 3;

    if ((tmp * 2240) < avctx->bit_rate)  tmp_val = 4;

    s->cm_table_select = tmp_val;



    if (s->sub_sampling == 0)

        tmp = 7999;

    else

        tmp = ((-(s->sub_sampling -1)) & 8000) + 20000;

    









    if (tmp < 8000)

        s->coeff_per_sb_select = 0;

    else if (tmp <= 16000)

        s->coeff_per_sb_select = 1;

    else

        s->coeff_per_sb_select = 2;



    

    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;

    }



    ff_rdft_init(&s->rdft_ctx, s->fft_order, IDFT_C2R);

    ff_mpadsp_init(&s->mpadsp);



    qdm2_init(s);



    avctx->sample_fmt = AV_SAMPLE_FMT_S16;





    return 0;

}
static av_cold int FUNC_0(AVCodecContext *avctx)

{

    QDM2Context *s = avctx->priv_data;

    uint8_t *extradata;

    int VAR_0;

    int VAR_1, VAR_2, VAR_3;



    



































































    if (!avctx->extradata || (avctx->VAR_0 < 48)) {

        av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");

        return -1;

    }



    extradata = avctx->extradata;

    VAR_0 = avctx->VAR_0;



    while (VAR_0 > 7) {

        if (!memcmp(extradata, "frmaQDM", 7))

            break;

        extradata++;

        VAR_0--;

    }



    if (VAR_0 < 12) {

        av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",

               VAR_0);

        return -1;

    }



    if (memcmp(extradata, "frmaQDM", 7)) {

        av_log(avctx, AV_LOG_ERROR, "invalid headers, QDM? not found\n");

        return -1;

    }



    if (extradata[7] == 'C') {



        av_log(avctx, AV_LOG_ERROR, "stream is QDMC version 1, which is not supported\n");

        return -1;

    }



    extradata += 8;

    VAR_0 -= 8;



    VAR_3 = AV_RB32(extradata);



    if(VAR_3 > VAR_0){

        av_log(avctx, AV_LOG_ERROR, "extradata VAR_3 too small, %i < %i\n",

               VAR_0, VAR_3);

        return -1;

    }



    extradata += 4;

    av_log(avctx, AV_LOG_DEBUG, "VAR_3: %d\n", VAR_3);

    if (AV_RB32(extradata) != MKBETAG('Q','D','C','A')) {

        av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");

        return -1;

    }



    extradata += 8;



    avctx->channels = s->nb_channels = s->channels = AV_RB32(extradata);

    extradata += 4;

    if (s->channels > MPA_MAX_CHANNELS)




    avctx->sample_rate = AV_RB32(extradata);

    extradata += 4;



    avctx->bit_rate = AV_RB32(extradata);

    extradata += 4;



    s->group_size = AV_RB32(extradata);

    extradata += 4;



    s->fft_size = AV_RB32(extradata);

    extradata += 4;



    s->checksum_size = AV_RB32(extradata);



    s->fft_order = av_log2(s->fft_size) + 1;

    s->fft_frame_size = 2 * s->fft_size; 



    

    s->group_order = av_log2(s->group_size) + 1;

    s->frame_size = s->group_size / 16; 





    s->sub_sampling = s->fft_order - 7;

    s->frequency_range = 255 / (1 << (2 - s->sub_sampling));



    switch ((s->sub_sampling * 2 + s->channels - 1)) {

        case 0: VAR_2 = 40; break;

        case 1: VAR_2 = 48; break;

        case 2: VAR_2 = 56; break;

        case 3: VAR_2 = 72; break;

        case 4: VAR_2 = 80; break;

        case 5: VAR_2 = 100;break;

        default: VAR_2=s->sub_sampling; break;

    }

    VAR_1 = 0;

    if ((VAR_2 * 1000) < avctx->bit_rate)  VAR_1 = 1;

    if ((VAR_2 * 1440) < avctx->bit_rate)  VAR_1 = 2;

    if ((VAR_2 * 1760) < avctx->bit_rate)  VAR_1 = 3;

    if ((VAR_2 * 2240) < avctx->bit_rate)  VAR_1 = 4;

    s->cm_table_select = VAR_1;



    if (s->sub_sampling == 0)

        VAR_2 = 7999;

    else

        VAR_2 = ((-(s->sub_sampling -1)) & 8000) + 20000;

    









    if (VAR_2 < 8000)

        s->coeff_per_sb_select = 0;

    else if (VAR_2 <= 16000)

        s->coeff_per_sb_select = 1;

    else

        s->coeff_per_sb_select = 2;



    

    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;

    }



    ff_rdft_init(&s->rdft_ctx, s->fft_order, IDFT_C2R);

    ff_mpadsp_init(&s->mpadsp);



    qdm2_init(s);



    avctx->sample_fmt = AV_SAMPLE_FMT_S16;





    return 0;

}
static av_cold int FUNC_0(AVCodecContext *avctx)
{ QDM2Context *s = avctx->priv_data; uint8_t *extradata; int VAR_0; int VAR_1, VAR_2, VAR_3; if (!avctx->extradata || (avctx->VAR_0 < 48)) { av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n"); return -1; } extradata = avctx->extradata; VAR_0 = avctx->VAR_0; while (VAR_0 > 7) { if (!memcmp(extradata, "frmaQDM", 7))
break; extradata++; VAR_0--; } if (VAR_0 < 12) { av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
VAR_0); return -1; } if (memcmp(extradata, "frmaQDM", 7)) { av_log(avctx, AV_LOG_ERROR, "invalid headers, QDM? not found\n"); return -1; } if (extradata[7] == 'C') { av_log(avctx, AV_LOG_ERROR, "stream is QDMC version 1, which is not supported\n"); return -1; } extradata += 8; VAR_0 -= 8; VAR_3 = AV_RB32(extradata); if(VAR_3 > VAR_0){ av_log(avctx, AV_LOG_ERROR, "extradata VAR_3 too small, %i < %i\n",
VAR_0, VAR_3); return -1; } extradata += 4; av_log(avctx, AV_LOG_DEBUG, "VAR_3: %d\n", VAR_3); if (AV_RB32(extradata) != MKBETAG('Q','D','C','A')) { av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n"); return -1; } extradata += 8; avctx->channels = s->nb_channels = s->channels = AV_RB32(extradata); extradata += 4; if (s->channels > MPA_MAX_CHANNELS)
avctx->sample_rate = AV_RB32(extradata); extradata += 4; avctx->bit_rate = AV_RB32(extradata); extradata += 4; s->group_size = AV_RB32(extradata); extradata += 4; s->fft_size = AV_RB32(extradata); extradata += 4; s->checksum_size = AV_RB32(extradata); s->fft_order = av_log2(s->fft_size) + 1; s->fft_frame_size = 2 * s->fft_size; s->group_order = av_log2(s->group_size) + 1; s->frame_size = s->group_size / 16; s->sub_sampling = s->fft_order - 7; s->frequency_range = 255 / (1 << (2 - s->sub_sampling)); switch ((s->sub_sampling * 2 + s->channels - 1)) { case 0: VAR_2 = 40; break; case 1: VAR_2 = 48; break; case 2: VAR_2 = 56; break; case 3: VAR_2 = 72; break; case 4: VAR_2 = 80; break; case 5: VAR_2 = 100;break; default: VAR_2=s->sub_sampling; break; } VAR_1 = 0; if ((VAR_2 * 1000) < avctx->bit_rate)  VAR_1 = 1; if ((VAR_2 * 1440) < avctx->bit_rate)  VAR_1 = 2; if ((VAR_2 * 1760) < avctx->bit_rate)  VAR_1 = 3; if ((VAR_2 * 2240) < avctx->bit_rate)  VAR_1 = 4; s->cm_table_select = VAR_1; if (s->sub_sampling == 0)
VAR_2 = 7999; else
VAR_2 = ((-(s->sub_sampling -1)) & 8000) + 20000; if (VAR_2 < 8000)
s->coeff_per_sb_select = 0; else if (VAR_2 <= 16000)
s->coeff_per_sb_select = 1; else
s->coeff_per_sb_select = 2; 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; } ff_rdft_init(&s->rdft_ctx, s->fft_order, IDFT_C2R); ff_mpadsp_init(&s->mpadsp); qdm2_init(s); avctx->sample_fmt = AV_SAMPLE_FMT_S16; return 0; }