matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,

                     int64_t pos, uint64_t cluster_time, uint64_t duration,

                     int is_keyframe, int is_bframe)

{

    int res = 0;

    int track;

    AVStream *st;

    AVPacket *pkt;

    uint8_t *origdata = data;

    int16_t block_time;

    uint32_t *lace_size = NULL;

    int n, flags, laces = 0;

    uint64_t num;

    int stream_index;



    /* first byte(s): tracknum */

    if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {

        av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");

        av_free(origdata);

        return res;

    }

    data += n;

    size -= n;



    /* fetch track from num */

    track = matroska_find_track_by_num(matroska, num);

    if (size <= 3 || track < 0 || track >= matroska->num_tracks) {

        av_log(matroska->ctx, AV_LOG_INFO,

               "Invalid stream %d or size %u\n", track, size);

        av_free(origdata);

        return res;

    }

    stream_index = matroska->tracks[track]->stream_index;

    if (stream_index < 0 || stream_index >= matroska->ctx->nb_streams) {

        av_free(origdata);

        return res;

    }

    st = matroska->ctx->streams[stream_index];

    if (st->discard >= AVDISCARD_ALL) {

        av_free(origdata);

        return res;

    }

    if (duration == AV_NOPTS_VALUE)

        duration = matroska->tracks[track]->default_duration / matroska->time_scale;



    /* block_time (relative to cluster time) */

    block_time = AV_RB16(data);

    data += 2;

    flags = *data++;

    size -= 3;

    if (is_keyframe == -1)

        is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;



    if (matroska->skip_to_keyframe) {

        if (!is_keyframe || st != matroska->skip_to_stream) {

            av_free(origdata);

            return res;

        }

        matroska->skip_to_keyframe = 0;

    }



    switch ((flags & 0x06) >> 1) {

        case 0x0: /* no lacing */

            laces = 1;

            lace_size = av_mallocz(sizeof(int));

            lace_size[0] = size;

            break;



        case 0x1: /* xiph lacing */

        case 0x2: /* fixed-size lacing */

        case 0x3: /* EBML lacing */

            assert(size>0); // size <=3 is checked before size-=3 above

            laces = (*data) + 1;

            data += 1;

            size -= 1;

            lace_size = av_mallocz(laces * sizeof(int));



            switch ((flags & 0x06) >> 1) {

                case 0x1: /* xiph lacing */ {

                    uint8_t temp;

                    uint32_t total = 0;

                    for (n = 0; res == 0 && n < laces - 1; n++) {

                        while (1) {

                            if (size == 0) {

                                res = -1;

                                break;

                            }

                            temp = *data;

                            lace_size[n] += temp;

                            data += 1;

                            size -= 1;

                            if (temp != 0xff)

                                break;

                        }

                        total += lace_size[n];

                    }

                    lace_size[n] = size - total;

                    break;

                }



                case 0x2: /* fixed-size lacing */

                    for (n = 0; n < laces; n++)

                        lace_size[n] = size / laces;

                    break;



                case 0x3: /* EBML lacing */ {

                    uint32_t total;

                    n = matroska_ebmlnum_uint(data, size, &num);

                    if (n < 0) {

                        av_log(matroska->ctx, AV_LOG_INFO,

                               "EBML block data error\n");

                        break;

                    }

                    data += n;

                    size -= n;

                    total = lace_size[0] = num;

                    for (n = 1; res == 0 && n < laces - 1; n++) {

                        int64_t snum;

                        int r;

                        r = matroska_ebmlnum_sint (data, size, &snum);

                        if (r < 0) {

                            av_log(matroska->ctx, AV_LOG_INFO,

                                   "EBML block data error\n");

                            break;

                        }

                        data += r;

                        size -= r;

                        lace_size[n] = lace_size[n - 1] + snum;

                        total += lace_size[n];

                    }

                    lace_size[n] = size - total;

                    break;

                }

            }

            break;

    }



    if (res == 0) {

        uint64_t timecode = AV_NOPTS_VALUE;



        if (cluster_time != (uint64_t)-1

            && (block_time >= 0 || cluster_time >= -block_time))

            timecode = cluster_time + block_time;



        for (n = 0; n < laces; n++) {

            if (st->codec->codec_id == CODEC_ID_RA_288 ||

                st->codec->codec_id == CODEC_ID_COOK ||

                st->codec->codec_id == CODEC_ID_ATRAC3) {

                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];

                int a = st->codec->block_align;

                int sps = audiotrack->sub_packet_size;

                int cfs = audiotrack->coded_framesize;

                int h = audiotrack->sub_packet_h;

                int y = audiotrack->sub_packet_cnt;

                int w = audiotrack->frame_size;

                int x;



                if (!audiotrack->pkt_cnt) {

                    if (st->codec->codec_id == CODEC_ID_RA_288)

                        for (x=0; x<h/2; x++)

                            memcpy(audiotrack->buf+x*2*w+y*cfs,

                                   data+x*cfs, cfs);

                    else

                        for (x=0; x<w/sps; x++)

                            memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);



                    if (++audiotrack->sub_packet_cnt >= h) {

                        audiotrack->sub_packet_cnt = 0;

                        audiotrack->pkt_cnt = h*w / a;

                    }

                }

                while (audiotrack->pkt_cnt) {

                    pkt = av_mallocz(sizeof(AVPacket));

                    av_new_packet(pkt, a);

                    memcpy(pkt->data, audiotrack->buf

                           + a * (h*w / a - audiotrack->pkt_cnt--), a);

                    pkt->pos = pos;

                    pkt->stream_index = stream_index;

                    matroska_queue_packet(matroska, pkt);

                }

            } else {

                int result, offset = 0, ilen, olen, pkt_size = lace_size[n];

                uint8_t *pkt_data = data;



                if (matroska->tracks[track]->encoding_scope & 1) {

                    switch (matroska->tracks[track]->encoding_algo) {

                    case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:

                        offset = matroska->tracks[track]->encoding_settings_len;

                        break;

                    case MATROSKA_TRACK_ENCODING_COMP_LZO:

                        pkt_data = NULL;

                        do {

                            ilen = lace_size[n];

                            olen = pkt_size *= 3;

                            pkt_data = av_realloc(pkt_data,

                                                  pkt_size+LZO_OUTPUT_PADDING);

                            result = lzo1x_decode(pkt_data, &olen, data, &ilen);

                        } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);

                        if (result) {

                            av_free(pkt_data);

                            continue;

                        }

                        pkt_size -= olen;

                        break;

#ifdef CONFIG_ZLIB

                    case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {

                        z_stream zstream = {0};

                        pkt_data = NULL;

                        if (inflateInit(&zstream) != Z_OK)

                            continue;

                        zstream.next_in = data;

                        zstream.avail_in = lace_size[n];

                        do {

                            pkt_size *= 3;

                            pkt_data = av_realloc(pkt_data, pkt_size);

                            zstream.avail_out = pkt_size - zstream.total_out;

                            zstream.next_out = pkt_data + zstream.total_out;

                            result = inflate(&zstream, Z_NO_FLUSH);

                        } while (result==Z_OK && pkt_size<10000000);

                        pkt_size = zstream.total_out;

                        inflateEnd(&zstream);

                        if (result != Z_STREAM_END) {

                            av_free(pkt_data);

                            continue;

                        }

                        break;

                    }

#endif

#ifdef CONFIG_BZLIB

                    case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {

                        bz_stream bzstream = {0};

                        pkt_data = NULL;

                        if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)

                            continue;

                        bzstream.next_in = data;

                        bzstream.avail_in = lace_size[n];

                        do {

                            pkt_size *= 3;

                            pkt_data = av_realloc(pkt_data, pkt_size);

                            bzstream.avail_out = pkt_size - bzstream.total_out_lo32;

                            bzstream.next_out = pkt_data + bzstream.total_out_lo32;

                            result = BZ2_bzDecompress(&bzstream);

                        } while (result==BZ_OK && pkt_size<10000000);

                        pkt_size = bzstream.total_out_lo32;

                        BZ2_bzDecompressEnd(&bzstream);

                        if (result != BZ_STREAM_END) {

                            av_free(pkt_data);

                            continue;

                        }

                        break;

                    }

#endif

                    }

                }



                pkt = av_mallocz(sizeof(AVPacket));

                /* XXX: prevent data copy... */

                if (av_new_packet(pkt, pkt_size+offset) < 0) {


                    res = AVERROR(ENOMEM);

                    n = laces-1;

                    break;

                }

                if (offset)

                    memcpy (pkt->data, matroska->tracks[track]->encoding_settings, offset);

                memcpy (pkt->data+offset, pkt_data, pkt_size);



                if (n == 0)

                    pkt->flags = is_keyframe;

                pkt->stream_index = stream_index;



                pkt->pts = timecode;

                pkt->pos = pos;

                pkt->duration = duration;



                matroska_queue_packet(matroska, pkt);

            }



            if (timecode != AV_NOPTS_VALUE)

                timecode = duration ? timecode + duration : AV_NOPTS_VALUE;

            data += lace_size[n];

        }

    }



    av_free(lace_size);

    av_free(origdata);

    return res;

}
matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,

                     int64_t pos, uint64_t cluster_time, uint64_t duration,

                     int is_keyframe, int is_bframe)

{

    int res = 0;

    int track;

    AVStream *st;

    AVPacket *pkt;

    uint8_t *origdata = data;

    int16_t block_time;

    uint32_t *lace_size = NULL;

    int n, flags, laces = 0;

    uint64_t num;

    int stream_index;



    

    if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {

        av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");

        av_free(origdata);

        return res;

    }

    data += n;

    size -= n;



    

    track = matroska_find_track_by_num(matroska, num);

    if (size <= 3 || track < 0 || track >= matroska->num_tracks) {

        av_log(matroska->ctx, AV_LOG_INFO,

               "Invalid stream %d or size %u\n", track, size);

        av_free(origdata);

        return res;

    }

    stream_index = matroska->tracks[track]->stream_index;

    if (stream_index < 0 || stream_index >= matroska->ctx->nb_streams) {

        av_free(origdata);

        return res;

    }

    st = matroska->ctx->streams[stream_index];

    if (st->discard >= AVDISCARD_ALL) {

        av_free(origdata);

        return res;

    }

    if (duration == AV_NOPTS_VALUE)

        duration = matroska->tracks[track]->default_duration / matroska->time_scale;



    

    block_time = AV_RB16(data);

    data += 2;

    flags = *data++;

    size -= 3;

    if (is_keyframe == -1)

        is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;



    if (matroska->skip_to_keyframe) {

        if (!is_keyframe || st != matroska->skip_to_stream) {

            av_free(origdata);

            return res;

        }

        matroska->skip_to_keyframe = 0;

    }



    switch ((flags & 0x06) >> 1) {

        case 0x0: 

            laces = 1;

            lace_size = av_mallocz(sizeof(int));

            lace_size[0] = size;

            break;



        case 0x1: 

        case 0x2: 

        case 0x3: 

            assert(size>0); 

            laces = (*data) + 1;

            data += 1;

            size -= 1;

            lace_size = av_mallocz(laces * sizeof(int));



            switch ((flags & 0x06) >> 1) {

                case 0x1:  {

                    uint8_t temp;

                    uint32_t total = 0;

                    for (n = 0; res == 0 && n < laces - 1; n++) {

                        while (1) {

                            if (size == 0) {

                                res = -1;

                                break;

                            }

                            temp = *data;

                            lace_size[n] += temp;

                            data += 1;

                            size -= 1;

                            if (temp != 0xff)

                                break;

                        }

                        total += lace_size[n];

                    }

                    lace_size[n] = size - total;

                    break;

                }



                case 0x2: 

                    for (n = 0; n < laces; n++)

                        lace_size[n] = size / laces;

                    break;



                case 0x3:  {

                    uint32_t total;

                    n = matroska_ebmlnum_uint(data, size, &num);

                    if (n < 0) {

                        av_log(matroska->ctx, AV_LOG_INFO,

                               "EBML block data error\n");

                        break;

                    }

                    data += n;

                    size -= n;

                    total = lace_size[0] = num;

                    for (n = 1; res == 0 && n < laces - 1; n++) {

                        int64_t snum;

                        int r;

                        r = matroska_ebmlnum_sint (data, size, &snum);

                        if (r < 0) {

                            av_log(matroska->ctx, AV_LOG_INFO,

                                   "EBML block data error\n");

                            break;

                        }

                        data += r;

                        size -= r;

                        lace_size[n] = lace_size[n - 1] + snum;

                        total += lace_size[n];

                    }

                    lace_size[n] = size - total;

                    break;

                }

            }

            break;

    }



    if (res == 0) {

        uint64_t timecode = AV_NOPTS_VALUE;



        if (cluster_time != (uint64_t)-1

            && (block_time >= 0 || cluster_time >= -block_time))

            timecode = cluster_time + block_time;



        for (n = 0; n < laces; n++) {

            if (st->codec->codec_id == CODEC_ID_RA_288 ||

                st->codec->codec_id == CODEC_ID_COOK ||

                st->codec->codec_id == CODEC_ID_ATRAC3) {

                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];

                int a = st->codec->block_align;

                int sps = audiotrack->sub_packet_size;

                int cfs = audiotrack->coded_framesize;

                int h = audiotrack->sub_packet_h;

                int y = audiotrack->sub_packet_cnt;

                int w = audiotrack->frame_size;

                int x;



                if (!audiotrack->pkt_cnt) {

                    if (st->codec->codec_id == CODEC_ID_RA_288)

                        for (x=0; x<h/2; x++)

                            memcpy(audiotrack->buf+x*2*w+y*cfs,

                                   data+x*cfs, cfs);

                    else

                        for (x=0; x<w/sps; x++)

                            memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);



                    if (++audiotrack->sub_packet_cnt >= h) {

                        audiotrack->sub_packet_cnt = 0;

                        audiotrack->pkt_cnt = h*w / a;

                    }

                }

                while (audiotrack->pkt_cnt) {

                    pkt = av_mallocz(sizeof(AVPacket));

                    av_new_packet(pkt, a);

                    memcpy(pkt->data, audiotrack->buf

                           + a * (h*w / a - audiotrack->pkt_cnt--), a);

                    pkt->pos = pos;

                    pkt->stream_index = stream_index;

                    matroska_queue_packet(matroska, pkt);

                }

            } else {

                int result, offset = 0, ilen, olen, pkt_size = lace_size[n];

                uint8_t *pkt_data = data;



                if (matroska->tracks[track]->encoding_scope & 1) {

                    switch (matroska->tracks[track]->encoding_algo) {

                    case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:

                        offset = matroska->tracks[track]->encoding_settings_len;

                        break;

                    case MATROSKA_TRACK_ENCODING_COMP_LZO:

                        pkt_data = NULL;

                        do {

                            ilen = lace_size[n];

                            olen = pkt_size *= 3;

                            pkt_data = av_realloc(pkt_data,

                                                  pkt_size+LZO_OUTPUT_PADDING);

                            result = lzo1x_decode(pkt_data, &olen, data, &ilen);

                        } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);

                        if (result) {

                            av_free(pkt_data);

                            continue;

                        }

                        pkt_size -= olen;

                        break;

#ifdef CONFIG_ZLIB

                    case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {

                        z_stream zstream = {0};

                        pkt_data = NULL;

                        if (inflateInit(&zstream) != Z_OK)

                            continue;

                        zstream.next_in = data;

                        zstream.avail_in = lace_size[n];

                        do {

                            pkt_size *= 3;

                            pkt_data = av_realloc(pkt_data, pkt_size);

                            zstream.avail_out = pkt_size - zstream.total_out;

                            zstream.next_out = pkt_data + zstream.total_out;

                            result = inflate(&zstream, Z_NO_FLUSH);

                        } while (result==Z_OK && pkt_size<10000000);

                        pkt_size = zstream.total_out;

                        inflateEnd(&zstream);

                        if (result != Z_STREAM_END) {

                            av_free(pkt_data);

                            continue;

                        }

                        break;

                    }

#endif

#ifdef CONFIG_BZLIB

                    case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {

                        bz_stream bzstream = {0};

                        pkt_data = NULL;

                        if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)

                            continue;

                        bzstream.next_in = data;

                        bzstream.avail_in = lace_size[n];

                        do {

                            pkt_size *= 3;

                            pkt_data = av_realloc(pkt_data, pkt_size);

                            bzstream.avail_out = pkt_size - bzstream.total_out_lo32;

                            bzstream.next_out = pkt_data + bzstream.total_out_lo32;

                            result = BZ2_bzDecompress(&bzstream);

                        } while (result==BZ_OK && pkt_size<10000000);

                        pkt_size = bzstream.total_out_lo32;

                        BZ2_bzDecompressEnd(&bzstream);

                        if (result != BZ_STREAM_END) {

                            av_free(pkt_data);

                            continue;

                        }

                        break;

                    }

#endif

                    }

                }



                pkt = av_mallocz(sizeof(AVPacket));

                

                if (av_new_packet(pkt, pkt_size+offset) < 0) {


                    res = AVERROR(ENOMEM);

                    n = laces-1;

                    break;

                }

                if (offset)

                    memcpy (pkt->data, matroska->tracks[track]->encoding_settings, offset);

                memcpy (pkt->data+offset, pkt_data, pkt_size);



                if (n == 0)

                    pkt->flags = is_keyframe;

                pkt->stream_index = stream_index;



                pkt->pts = timecode;

                pkt->pos = pos;

                pkt->duration = duration;



                matroska_queue_packet(matroska, pkt);

            }



            if (timecode != AV_NOPTS_VALUE)

                timecode = duration ? timecode + duration : AV_NOPTS_VALUE;

            data += lace_size[n];

        }

    }



    av_free(lace_size);

    av_free(origdata);

    return res;

}
FUNC_0(MatroskaDemuxContext *VAR_0, uint8_t *VAR_1, int VAR_2,

                     int64_t VAR_3, uint64_t VAR_4, uint64_t VAR_5,

                     int VAR_6, int VAR_7)

{

    int VAR_8 = 0;

    int VAR_9;

    AVStream *st;

    AVPacket *pkt;

    uint8_t *origdata = VAR_1;

    int16_t block_time;

    uint32_t *lace_size = NULL;

    int VAR_10, VAR_11, VAR_12 = 0;

    uint64_t num;

    int VAR_13;



    

    if ((VAR_10 = matroska_ebmlnum_uint(VAR_1, VAR_2, &num)) < 0) {

        av_log(VAR_0->ctx, AV_LOG_ERROR, "EBML block VAR_1 error\VAR_10");

        av_free(origdata);

        return VAR_8;

    }

    VAR_1 += VAR_10;

    VAR_2 -= VAR_10;



    

    VAR_9 = matroska_find_track_by_num(VAR_0, num);

    if (VAR_2 <= 3 || VAR_9 < 0 || VAR_9 >= VAR_0->num_tracks) {

        av_log(VAR_0->ctx, AV_LOG_INFO,

               "Invalid stream %d or VAR_2 %u\VAR_10", VAR_9, VAR_2);

        av_free(origdata);

        return VAR_8;

    }

    VAR_13 = VAR_0->tracks[VAR_9]->VAR_13;

    if (VAR_13 < 0 || VAR_13 >= VAR_0->ctx->nb_streams) {

        av_free(origdata);

        return VAR_8;

    }

    st = VAR_0->ctx->streams[VAR_13];

    if (st->discard >= AVDISCARD_ALL) {

        av_free(origdata);

        return VAR_8;

    }

    if (VAR_5 == AV_NOPTS_VALUE)

        VAR_5 = VAR_0->tracks[VAR_9]->default_duration / VAR_0->time_scale;



    

    block_time = AV_RB16(VAR_1);

    VAR_1 += 2;

    VAR_11 = *VAR_1++;

    VAR_2 -= 3;

    if (VAR_6 == -1)

        VAR_6 = VAR_11 & 0x80 ? PKT_FLAG_KEY : 0;



    if (VAR_0->skip_to_keyframe) {

        if (!VAR_6 || st != VAR_0->skip_to_stream) {

            av_free(origdata);

            return VAR_8;

        }

        VAR_0->skip_to_keyframe = 0;

    }



    switch ((VAR_11 & 0x06) >> 1) {

        case 0x0: 

            VAR_12 = 1;

            lace_size = av_mallocz(sizeof(int));

            lace_size[0] = VAR_2;

            break;



        case 0x1: 

        case 0x2: 

        case 0x3: 

            assert(VAR_2>0); 

            VAR_12 = (*VAR_1) + 1;

            VAR_1 += 1;

            VAR_2 -= 1;

            lace_size = av_mallocz(VAR_12 * sizeof(int));



            switch ((VAR_11 & 0x06) >> 1) {

                case 0x1:  {

                    uint8_t temp;

                    uint32_t total = 0;

                    for (VAR_10 = 0; VAR_8 == 0 && VAR_10 < VAR_12 - 1; VAR_10++) {

                        while (1) {

                            if (VAR_2 == 0) {

                                VAR_8 = -1;

                                break;

                            }

                            temp = *VAR_1;

                            lace_size[VAR_10] += temp;

                            VAR_1 += 1;

                            VAR_2 -= 1;

                            if (temp != 0xff)

                                break;

                        }

                        total += lace_size[VAR_10];

                    }

                    lace_size[VAR_10] = VAR_2 - total;

                    break;

                }



                case 0x2: 

                    for (VAR_10 = 0; VAR_10 < VAR_12; VAR_10++)

                        lace_size[VAR_10] = VAR_2 / VAR_12;

                    break;



                case 0x3:  {

                    uint32_t total;

                    VAR_10 = matroska_ebmlnum_uint(VAR_1, VAR_2, &num);

                    if (VAR_10 < 0) {

                        av_log(VAR_0->ctx, AV_LOG_INFO,

                               "EBML block VAR_1 error\VAR_10");

                        break;

                    }

                    VAR_1 += VAR_10;

                    VAR_2 -= VAR_10;

                    total = lace_size[0] = num;

                    for (VAR_10 = 1; VAR_8 == 0 && VAR_10 < VAR_12 - 1; VAR_10++) {

                        int64_t snum;

                        int VAR_14;

                        VAR_14 = matroska_ebmlnum_sint (VAR_1, VAR_2, &snum);

                        if (VAR_14 < 0) {

                            av_log(VAR_0->ctx, AV_LOG_INFO,

                                   "EBML block VAR_1 error\VAR_10");

                            break;

                        }

                        VAR_1 += VAR_14;

                        VAR_2 -= VAR_14;

                        lace_size[VAR_10] = lace_size[VAR_10 - 1] + snum;

                        total += lace_size[VAR_10];

                    }

                    lace_size[VAR_10] = VAR_2 - total;

                    break;

                }

            }

            break;

    }



    if (VAR_8 == 0) {

        uint64_t timecode = AV_NOPTS_VALUE;



        if (VAR_4 != (uint64_t)-1

            && (block_time >= 0 || VAR_4 >= -block_time))

            timecode = VAR_4 + block_time;



        for (VAR_10 = 0; VAR_10 < VAR_12; VAR_10++) {

            if (st->codec->codec_id == CODEC_ID_RA_288 ||

                st->codec->codec_id == CODEC_ID_COOK ||

                st->codec->codec_id == CODEC_ID_ATRAC3) {

                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)VAR_0->tracks[VAR_9];

                int VAR_15 = st->codec->block_align;

                int VAR_16 = audiotrack->sub_packet_size;

                int VAR_17 = audiotrack->coded_framesize;

                int VAR_18 = audiotrack->sub_packet_h;

                int VAR_19 = audiotrack->sub_packet_cnt;

                int VAR_20 = audiotrack->frame_size;

                int VAR_21;



                if (!audiotrack->pkt_cnt) {

                    if (st->codec->codec_id == CODEC_ID_RA_288)

                        for (VAR_21=0; VAR_21<VAR_18/2; VAR_21++)

                            memcpy(audiotrack->buf+VAR_21*2*VAR_20+VAR_19*VAR_17,

                                   VAR_1+VAR_21*VAR_17, VAR_17);

                    else

                        for (VAR_21=0; VAR_21<VAR_20/VAR_16; VAR_21++)

                            memcpy(audiotrack->buf+VAR_16*(VAR_18*VAR_21+((VAR_18+1)/2)*(VAR_19&1)+(VAR_19>>1)), VAR_1+VAR_21*VAR_16, VAR_16);



                    if (++audiotrack->sub_packet_cnt >= VAR_18) {

                        audiotrack->sub_packet_cnt = 0;

                        audiotrack->pkt_cnt = VAR_18*VAR_20 / VAR_15;

                    }

                }

                while (audiotrack->pkt_cnt) {

                    pkt = av_mallocz(sizeof(AVPacket));

                    av_new_packet(pkt, VAR_15);

                    memcpy(pkt->VAR_1, audiotrack->buf

                           + VAR_15 * (VAR_18*VAR_20 / VAR_15 - audiotrack->pkt_cnt--), VAR_15);

                    pkt->VAR_3 = VAR_3;

                    pkt->VAR_13 = VAR_13;

                    matroska_queue_packet(VAR_0, pkt);

                }

            } else {

                int VAR_22, VAR_23 = 0, VAR_24, VAR_25, VAR_26 = lace_size[VAR_10];

                uint8_t *pkt_data = VAR_1;



                if (VAR_0->tracks[VAR_9]->encoding_scope & 1) {

                    switch (VAR_0->tracks[VAR_9]->encoding_algo) {

                    case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:

                        VAR_23 = VAR_0->tracks[VAR_9]->encoding_settings_len;

                        break;

                    case MATROSKA_TRACK_ENCODING_COMP_LZO:

                        pkt_data = NULL;

                        do {

                            VAR_24 = lace_size[VAR_10];

                            VAR_25 = VAR_26 *= 3;

                            pkt_data = av_realloc(pkt_data,

                                                  VAR_26+LZO_OUTPUT_PADDING);

                            VAR_22 = lzo1x_decode(pkt_data, &VAR_25, VAR_1, &VAR_24);

                        } while (VAR_22==LZO_OUTPUT_FULL && VAR_26<10000000);

                        if (VAR_22) {

                            av_free(pkt_data);

                            continue;

                        }

                        VAR_26 -= VAR_25;

                        break;

#ifdef CONFIG_ZLIB

                    case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {

                        z_stream zstream = {0};

                        pkt_data = NULL;

                        if (inflateInit(&zstream) != Z_OK)

                            continue;

                        zstream.next_in = VAR_1;

                        zstream.avail_in = lace_size[VAR_10];

                        do {

                            VAR_26 *= 3;

                            pkt_data = av_realloc(pkt_data, VAR_26);

                            zstream.avail_out = VAR_26 - zstream.total_out;

                            zstream.next_out = pkt_data + zstream.total_out;

                            VAR_22 = inflate(&zstream, Z_NO_FLUSH);

                        } while (VAR_22==Z_OK && VAR_26<10000000);

                        VAR_26 = zstream.total_out;

                        inflateEnd(&zstream);

                        if (VAR_22 != Z_STREAM_END) {

                            av_free(pkt_data);

                            continue;

                        }

                        break;

                    }

#endif

#ifdef CONFIG_BZLIB

                    case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {

                        bz_stream bzstream = {0};

                        pkt_data = NULL;

                        if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)

                            continue;

                        bzstream.next_in = VAR_1;

                        bzstream.avail_in = lace_size[VAR_10];

                        do {

                            VAR_26 *= 3;

                            pkt_data = av_realloc(pkt_data, VAR_26);

                            bzstream.avail_out = VAR_26 - bzstream.total_out_lo32;

                            bzstream.next_out = pkt_data + bzstream.total_out_lo32;

                            VAR_22 = BZ2_bzDecompress(&bzstream);

                        } while (VAR_22==BZ_OK && VAR_26<10000000);

                        VAR_26 = bzstream.total_out_lo32;

                        BZ2_bzDecompressEnd(&bzstream);

                        if (VAR_22 != BZ_STREAM_END) {

                            av_free(pkt_data);

                            continue;

                        }

                        break;

                    }

#endif

                    }

                }



                pkt = av_mallocz(sizeof(AVPacket));

                

                if (av_new_packet(pkt, VAR_26+VAR_23) < 0) {


                    VAR_8 = AVERROR(ENOMEM);

                    VAR_10 = VAR_12-1;

                    break;

                }

                if (VAR_23)

                    memcpy (pkt->VAR_1, VAR_0->tracks[VAR_9]->encoding_settings, VAR_23);

                memcpy (pkt->VAR_1+VAR_23, pkt_data, VAR_26);



                if (VAR_10 == 0)

                    pkt->VAR_11 = VAR_6;

                pkt->VAR_13 = VAR_13;



                pkt->pts = timecode;

                pkt->VAR_3 = VAR_3;

                pkt->VAR_5 = VAR_5;



                matroska_queue_packet(VAR_0, pkt);

            }



            if (timecode != AV_NOPTS_VALUE)

                timecode = VAR_5 ? timecode + VAR_5 : AV_NOPTS_VALUE;

            VAR_1 += lace_size[VAR_10];

        }

    }



    av_free(lace_size);

    av_free(origdata);

    return VAR_8;

}
FUNC_0(MatroskaDemuxContext *VAR_0, uint8_t *VAR_1, int VAR_2,
int64_t VAR_3, uint64_t VAR_4, uint64_t VAR_5,
int VAR_6, int VAR_7)
{ int VAR_8 = 0; int VAR_9; AVStream *st; AVPacket *pkt; uint8_t *origdata = VAR_1; int16_t block_time; uint32_t *lace_size = NULL; int VAR_10, VAR_11, VAR_12 = 0; uint64_t num; int VAR_13; if ((VAR_10 = matroska_ebmlnum_uint(VAR_1, VAR_2, &num)) < 0) { av_log(VAR_0->ctx, AV_LOG_ERROR, "EBML block VAR_1 error\VAR_10"); av_free(origdata); return VAR_8; } VAR_1 += VAR_10; VAR_2 -= VAR_10; VAR_9 = matroska_find_track_by_num(VAR_0, num); if (VAR_2 <= 3 || VAR_9 < 0 || VAR_9 >= VAR_0->num_tracks) { av_log(VAR_0->ctx, AV_LOG_INFO,
"Invalid stream %d or VAR_2 %u\VAR_10", VAR_9, VAR_2); av_free(origdata); return VAR_8; } VAR_13 = VAR_0->tracks[VAR_9]->VAR_13; if (VAR_13 < 0 || VAR_13 >= VAR_0->ctx->nb_streams) { av_free(origdata); return VAR_8; } st = VAR_0->ctx->streams[VAR_13]; if (st->discard >= AVDISCARD_ALL) { av_free(origdata); return VAR_8; } if (VAR_5 == AV_NOPTS_VALUE)
VAR_5 = VAR_0->tracks[VAR_9]->default_duration / VAR_0->time_scale; block_time = AV_RB16(VAR_1); VAR_1 += 2; VAR_11 = *VAR_1++; VAR_2 -= 3; if (VAR_6 == -1)
VAR_6 = VAR_11 & 0x80 ? PKT_FLAG_KEY : 0; if (VAR_0->skip_to_keyframe) { if (!VAR_6 || st != VAR_0->skip_to_stream) { av_free(origdata); return VAR_8; } VAR_0->skip_to_keyframe = 0; } switch ((VAR_11 & 0x06) >> 1) { case 0x0:
VAR_12 = 1; lace_size = av_mallocz(sizeof(int)); lace_size[0] = VAR_2; break; case 0x1:
case 0x2:
case 0x3:
assert(VAR_2>0); VAR_12 = (*VAR_1) + 1; VAR_1 += 1; VAR_2 -= 1; lace_size = av_mallocz(VAR_12 * sizeof(int)); switch ((VAR_11 & 0x06) >> 1) { case 0x1:  { uint8_t temp; uint32_t total = 0; for (VAR_10 = 0; VAR_8 == 0 && VAR_10 < VAR_12 - 1; VAR_10++) { while (1) { if (VAR_2 == 0) { VAR_8 = -1; break; } temp = *VAR_1; lace_size[VAR_10] += temp; VAR_1 += 1; VAR_2 -= 1; if (temp != 0xff)
break; } total += lace_size[VAR_10]; } lace_size[VAR_10] = VAR_2 - total; break; } case 0x2:
for (VAR_10 = 0; VAR_10 < VAR_12; VAR_10++) lace_size[VAR_10] = VAR_2 / VAR_12; break; case 0x3:  { uint32_t total; VAR_10 = matroska_ebmlnum_uint(VAR_1, VAR_2, &num); if (VAR_10 < 0) { av_log(VAR_0->ctx, AV_LOG_INFO,
"EBML block VAR_1 error\VAR_10"); break; } VAR_1 += VAR_10; VAR_2 -= VAR_10; total = lace_size[0] = num; for (VAR_10 = 1; VAR_8 == 0 && VAR_10 < VAR_12 - 1; VAR_10++) { int64_t snum; int VAR_14; VAR_14 = matroska_ebmlnum_sint (VAR_1, VAR_2, &snum); if (VAR_14 < 0) { av_log(VAR_0->ctx, AV_LOG_INFO,
"EBML block VAR_1 error\VAR_10"); break; } VAR_1 += VAR_14; VAR_2 -= VAR_14; lace_size[VAR_10] = lace_size[VAR_10 - 1] + snum; total += lace_size[VAR_10]; } lace_size[VAR_10] = VAR_2 - total; break; } } break; } if (VAR_8 == 0) { uint64_t timecode = AV_NOPTS_VALUE; if (VAR_4 != (uint64_t)-1
&& (block_time >= 0 || VAR_4 >= -block_time))
timecode = VAR_4 + block_time; for (VAR_10 = 0; VAR_10 < VAR_12; VAR_10++) { if (st->codec->codec_id == CODEC_ID_RA_288 ||
st->codec->codec_id == CODEC_ID_COOK ||
st->codec->codec_id == CODEC_ID_ATRAC3) { MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)VAR_0->tracks[VAR_9]; int VAR_15 = st->codec->block_align; int VAR_16 = audiotrack->sub_packet_size; int VAR_17 = audiotrack->coded_framesize; int VAR_18 = audiotrack->sub_packet_h; int VAR_19 = audiotrack->sub_packet_cnt; int VAR_20 = audiotrack->frame_size; int VAR_21; if (!audiotrack->pkt_cnt) { if (st->codec->codec_id == CODEC_ID_RA_288)
for (VAR_21=0; VAR_21<VAR_18/2; VAR_21++) memcpy(audiotrack->buf+VAR_21*2*VAR_20+VAR_19*VAR_17,
VAR_1+VAR_21*VAR_17, VAR_17); else
for (VAR_21=0; VAR_21<VAR_20/VAR_16; VAR_21++) memcpy(audiotrack->buf+VAR_16*(VAR_18*VAR_21+((VAR_18+1)/2)*(VAR_19&1)+(VAR_19>>1)), VAR_1+VAR_21*VAR_16, VAR_16); if (++audiotrack->sub_packet_cnt >= VAR_18) { audiotrack->sub_packet_cnt = 0; audiotrack->pkt_cnt = VAR_18*VAR_20 / VAR_15; } } while (audiotrack->pkt_cnt) { pkt = av_mallocz(sizeof(AVPacket)); av_new_packet(pkt, VAR_15); memcpy(pkt->VAR_1, audiotrack->buf
+ VAR_15 * (VAR_18*VAR_20 / VAR_15 - audiotrack->pkt_cnt--), VAR_15); pkt->VAR_3 = VAR_3; pkt->VAR_13 = VAR_13; matroska_queue_packet(VAR_0, pkt); } } else { int VAR_22, VAR_23 = 0, VAR_24, VAR_25, VAR_26 = lace_size[VAR_10]; uint8_t *pkt_data = VAR_1; if (VAR_0->tracks[VAR_9]->encoding_scope & 1) { switch (VAR_0->tracks[VAR_9]->encoding_algo) { case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
VAR_23 = VAR_0->tracks[VAR_9]->encoding_settings_len; break; case MATROSKA_TRACK_ENCODING_COMP_LZO:
pkt_data = NULL; do { VAR_24 = lace_size[VAR_10]; VAR_25 = VAR_26 *= 3; pkt_data = av_realloc(pkt_data,
VAR_26+LZO_OUTPUT_PADDING); VAR_22 = lzo1x_decode(pkt_data, &VAR_25, VAR_1, &VAR_24); } while (VAR_22==LZO_OUTPUT_FULL && VAR_26<10000000); if (VAR_22) { av_free(pkt_data); continue; } VAR_26 -= VAR_25; break; #ifdef CONFIG_ZLIB
case MATROSKA_TRACK_ENCODING_COMP_ZLIB: { z_stream zstream = {0}; pkt_data = NULL; if (inflateInit(&zstream) != Z_OK)
continue; zstream.next_in = VAR_1; zstream.avail_in = lace_size[VAR_10]; do { VAR_26 *= 3; pkt_data = av_realloc(pkt_data, VAR_26); zstream.avail_out = VAR_26 - zstream.total_out; zstream.next_out = pkt_data + zstream.total_out; VAR_22 = inflate(&zstream, Z_NO_FLUSH); } while (VAR_22==Z_OK && VAR_26<10000000); VAR_26 = zstream.total_out; inflateEnd(&zstream); if (VAR_22 != Z_STREAM_END) { av_free(pkt_data); continue; } break; } #endif
#ifdef CONFIG_BZLIB
case MATROSKA_TRACK_ENCODING_COMP_BZLIB: { bz_stream bzstream = {0}; pkt_data = NULL; if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
continue; bzstream.next_in = VAR_1; bzstream.avail_in = lace_size[VAR_10]; do { VAR_26 *= 3; pkt_data = av_realloc(pkt_data, VAR_26); bzstream.avail_out = VAR_26 - bzstream.total_out_lo32; bzstream.next_out = pkt_data + bzstream.total_out_lo32; VAR_22 = BZ2_bzDecompress(&bzstream); } while (VAR_22==BZ_OK && VAR_26<10000000); VAR_26 = bzstream.total_out_lo32; BZ2_bzDecompressEnd(&bzstream); if (VAR_22 != BZ_STREAM_END) { av_free(pkt_data); continue; } break; } #endif
} } pkt = av_mallocz(sizeof(AVPacket)); if (av_new_packet(pkt, VAR_26+VAR_23) < 0) { VAR_8 = AVERROR(ENOMEM); VAR_10 = VAR_12-1; break; } if (VAR_23)
memcpy (pkt->VAR_1, VAR_0->tracks[VAR_9]->encoding_settings, VAR_23); memcpy (pkt->VAR_1+VAR_23, pkt_data, VAR_26); if (VAR_10 == 0)
pkt->VAR_11 = VAR_6; pkt->VAR_13 = VAR_13; pkt->pts = timecode; pkt->VAR_3 = VAR_3; pkt->VAR_5 = VAR_5; matroska_queue_packet(VAR_0, pkt); } if (timecode != AV_NOPTS_VALUE)
timecode = VAR_5 ? timecode + VAR_5 : AV_NOPTS_VALUE; VAR_1 += lace_size[VAR_10]; } } av_free(lace_size); av_free(origdata); return VAR_8; }