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

                     int64_t pos, uint64_t cluster_time,

                     int is_keyframe, int is_bframe,

                     int *ptrack, AVPacket **ppkt)

{

    int res = 0;

    int track;

    AVPacket *pkt;

    uint8_t *origdata = data;

    int16_t block_time;

    uint32_t *lace_size = NULL;

    int n, flags, laces = 0;

    uint64_t num;



    /* 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 (ptrack)  *ptrack = track;

    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;

    }

    if(matroska->ctx->streams[ matroska->tracks[track]->stream_index ]->discard >= AVDISCARD_ALL){

        av_free(origdata);

        return res;

    }



    /* block_time (relative to cluster time) */

    block_time = (data[0] << 8) | data[1];

    data += 2;

    size -= 2;

    flags = *data;

    data += 1;

    size -= 1;

    if (is_keyframe == -1)

        is_keyframe = flags & 1 ? PKT_FLAG_KEY : 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 */

            if (size == 0) {

                res = -1;

                break;

            }

            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) {

        int real_v = matroska->tracks[track]->flags & MATROSKA_TRACK_REAL_V;

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

            uint64_t timecode = AV_NOPTS_VALUE;

            int slice, slices = 1;



            if (real_v) {

                slices = *data++ + 1;

                lace_size[n]--;

            }

            if (cluster_time != (uint64_t)-1 && n == 0) {

                if (cluster_time + block_time >= 0)

                    timecode = cluster_time + block_time;

            }

            /* FIXME: duration */



            for (slice=0; slice<slices; slice++) {

                int slice_size, slice_offset = 0;

                if (real_v)

                    slice_offset = rv_offset(data, slice, slices);

                if (slice+1 == slices)

                    slice_size = lace_size[n] - slice_offset;

                else

                    slice_size = rv_offset(data, slice+1, slices) - slice_offset;

                pkt = av_mallocz(sizeof(AVPacket));

                if (ppkt)  *ppkt = pkt;

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

                if (av_new_packet(pkt, slice_size) < 0) {

                    res = AVERROR_NOMEM;

                    n = laces-1;

                    break;

                }

                memcpy (pkt->data, data+slice_offset, slice_size);



                if (n == 0)

                    pkt->flags = is_keyframe;

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



                pkt->pts = timecode;

                pkt->pos = pos;



                if (matroska->tracks[track]->flags & MATROSKA_TRACK_REORDER)

                    matroska_queue_packet_reordered(matroska, pkt, is_bframe);

                else

                    matroska_queue_packet(matroska, pkt);

            }

            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,

                     int is_keyframe, int is_bframe,

                     int *ptrack, AVPacket **ppkt)

{

    int res = 0;

    int track;

    AVPacket *pkt;

    uint8_t *origdata = data;

    int16_t block_time;

    uint32_t *lace_size = NULL;

    int n, flags, laces = 0;

    uint64_t num;



    

    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 (ptrack)  *ptrack = track;

    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;

    }

    if(matroska->ctx->streams[ matroska->tracks[track]->stream_index ]->discard >= AVDISCARD_ALL){

        av_free(origdata);

        return res;

    }



    

    block_time = (data[0] << 8) | data[1];

    data += 2;

    size -= 2;

    flags = *data;

    data += 1;

    size -= 1;

    if (is_keyframe == -1)

        is_keyframe = flags & 1 ? PKT_FLAG_KEY : 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: 

            if (size == 0) {

                res = -1;

                break;

            }

            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) {

        int real_v = matroska->tracks[track]->flags & MATROSKA_TRACK_REAL_V;

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

            uint64_t timecode = AV_NOPTS_VALUE;

            int slice, slices = 1;



            if (real_v) {

                slices = *data++ + 1;

                lace_size[n]--;

            }

            if (cluster_time != (uint64_t)-1 && n == 0) {

                if (cluster_time + block_time >= 0)

                    timecode = cluster_time + block_time;

            }

            



            for (slice=0; slice<slices; slice++) {

                int slice_size, slice_offset = 0;

                if (real_v)

                    slice_offset = rv_offset(data, slice, slices);

                if (slice+1 == slices)

                    slice_size = lace_size[n] - slice_offset;

                else

                    slice_size = rv_offset(data, slice+1, slices) - slice_offset;

                pkt = av_mallocz(sizeof(AVPacket));

                if (ppkt)  *ppkt = pkt;

                

                if (av_new_packet(pkt, slice_size) < 0) {

                    res = AVERROR_NOMEM;

                    n = laces-1;

                    break;

                }

                memcpy (pkt->data, data+slice_offset, slice_size);



                if (n == 0)

                    pkt->flags = is_keyframe;

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



                pkt->pts = timecode;

                pkt->pos = pos;



                if (matroska->tracks[track]->flags & MATROSKA_TRACK_REORDER)

                    matroska_queue_packet_reordered(matroska, pkt, is_bframe);

                else

                    matroska_queue_packet(matroska, pkt);

            }

            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,

                     int VAR_5, int VAR_6,

                     int *VAR_7, AVPacket **VAR_8)

{

    int VAR_9 = 0;

    int VAR_10;

    AVPacket *pkt;

    uint8_t *origdata = VAR_1;

    int16_t block_time;

    uint32_t *lace_size = NULL;

    int VAR_11, VAR_12, VAR_13 = 0;

    uint64_t num;



    

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

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

        av_free(origdata);

        return VAR_9;

    }

    VAR_1 += VAR_11;

    VAR_2 -= VAR_11;



    

    VAR_10 = matroska_find_track_by_num(VAR_0, num);

    if (VAR_7)  *VAR_7 = VAR_10;

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

        av_log(VAR_0->ctx, AV_LOG_INFO,

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

        av_free(origdata);

        return VAR_9;

    }

    if(VAR_0->ctx->streams[ VAR_0->tracks[VAR_10]->stream_index ]->discard >= AVDISCARD_ALL){

        av_free(origdata);

        return VAR_9;

    }



    

    block_time = (VAR_1[0] << 8) | VAR_1[1];

    VAR_1 += 2;

    VAR_2 -= 2;

    VAR_12 = *VAR_1;

    VAR_1 += 1;

    VAR_2 -= 1;

    if (VAR_5 == -1)

        VAR_5 = VAR_12 & 1 ? PKT_FLAG_KEY : 0;

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

        case 0x0: 

            VAR_13 = 1;

            lace_size = av_mallocz(sizeof(int));

            lace_size[0] = VAR_2;

            break;



        case 0x1: 

        case 0x2: 

        case 0x3: 

            if (VAR_2 == 0) {

                VAR_9 = -1;

                break;

            }

            VAR_13 = (*VAR_1) + 1;

            VAR_1 += 1;

            VAR_2 -= 1;

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



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

                case 0x1:  {

                    uint8_t temp;

                    uint32_t total = 0;

                    for (VAR_11 = 0; VAR_9 == 0 && VAR_11 < VAR_13 - 1; VAR_11++) {

                        while (1) {

                            if (VAR_2 == 0) {

                                VAR_9 = -1;

                                break;

                            }

                            temp = *VAR_1;

                            lace_size[VAR_11] += temp;

                            VAR_1 += 1;

                            VAR_2 -= 1;

                            if (temp != 0xff)

                                break;

                        }

                        total += lace_size[VAR_11];

                    }

                    lace_size[VAR_11] = VAR_2 - total;

                    break;

                }



                case 0x2: 

                    for (VAR_11 = 0; VAR_11 < VAR_13; VAR_11++)

                        lace_size[VAR_11] = VAR_2 / VAR_13;

                    break;



                case 0x3:  {

                    uint32_t total;

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

                    if (VAR_11 < 0) {

                        av_log(VAR_0->ctx, AV_LOG_INFO,

                               "EBML block VAR_1 error\VAR_11");

                        break;

                    }

                    VAR_1 += VAR_11;

                    VAR_2 -= VAR_11;

                    total = lace_size[0] = num;

                    for (VAR_11 = 1; VAR_9 == 0 && VAR_11 < VAR_13 - 1; VAR_11++) {

                        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_11");

                            break;

                        }

                        VAR_1 += VAR_14;

                        VAR_2 -= VAR_14;

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

                        total += lace_size[VAR_11];

                    }

                    lace_size[VAR_11] = VAR_2 - total;

                    break;

                }

            }

            break;

    }



    if (VAR_9 == 0) {

        int VAR_15 = VAR_0->tracks[VAR_10]->VAR_12 & MATROSKA_TRACK_REAL_V;

        for (VAR_11 = 0; VAR_11 < VAR_13; VAR_11++) {

            uint64_t timecode = AV_NOPTS_VALUE;

            int VAR_16, VAR_17 = 1;



            if (VAR_15) {

                VAR_17 = *VAR_1++ + 1;

                lace_size[VAR_11]--;

            }

            if (VAR_4 != (uint64_t)-1 && VAR_11 == 0) {

                if (VAR_4 + block_time >= 0)

                    timecode = VAR_4 + block_time;

            }

            



            for (VAR_16=0; VAR_16<VAR_17; VAR_16++) {

                int VAR_18, VAR_19 = 0;

                if (VAR_15)

                    VAR_19 = rv_offset(VAR_1, VAR_16, VAR_17);

                if (VAR_16+1 == VAR_17)

                    VAR_18 = lace_size[VAR_11] - VAR_19;

                else

                    VAR_18 = rv_offset(VAR_1, VAR_16+1, VAR_17) - VAR_19;

                pkt = av_mallocz(sizeof(AVPacket));

                if (VAR_8)  *VAR_8 = pkt;

                

                if (av_new_packet(pkt, VAR_18) < 0) {

                    VAR_9 = AVERROR_NOMEM;

                    VAR_11 = VAR_13-1;

                    break;

                }

                memcpy (pkt->VAR_1, VAR_1+VAR_19, VAR_18);



                if (VAR_11 == 0)

                    pkt->VAR_12 = VAR_5;

                pkt->stream_index = VAR_0->tracks[VAR_10]->stream_index;



                pkt->pts = timecode;

                pkt->VAR_3 = VAR_3;



                if (VAR_0->tracks[VAR_10]->VAR_12 & MATROSKA_TRACK_REORDER)

                    matroska_queue_packet_reordered(VAR_0, pkt, VAR_6);

                else

                    matroska_queue_packet(VAR_0, pkt);

            }

            VAR_1 += lace_size[VAR_11];

        }

    }



    av_free(lace_size);

    av_free(origdata);

    return VAR_9;

}

FUNC_0(MatroskaDemuxContext *VAR_0, uint8_t *VAR_1, int VAR_2,
int64_t VAR_3, uint64_t VAR_4,
int VAR_5, int VAR_6,
int *VAR_7, AVPacket **VAR_8)
{ int VAR_9 = 0; int VAR_10; AVPacket *pkt; uint8_t *origdata = VAR_1; int16_t block_time; uint32_t *lace_size = NULL; int VAR_11, VAR_12, VAR_13 = 0; uint64_t num; if ((VAR_11 = matroska_ebmlnum_uint(VAR_1, VAR_2, &num)) < 0) { av_log(VAR_0->ctx, AV_LOG_ERROR, "EBML block VAR_1 error\VAR_11"); av_free(origdata); return VAR_9; } VAR_1 += VAR_11; VAR_2 -= VAR_11; VAR_10 = matroska_find_track_by_num(VAR_0, num); if (VAR_7)  *VAR_7 = VAR_10; if (VAR_2 <= 3 || VAR_10 < 0 || VAR_10 >= VAR_0->num_tracks) { av_log(VAR_0->ctx, AV_LOG_INFO,
"Invalid stream %d or VAR_2 %u\VAR_11", VAR_10, VAR_2); av_free(origdata); return VAR_9; } if(VAR_0->ctx->streams[ VAR_0->tracks[VAR_10]->stream_index ]->discard >= AVDISCARD_ALL){ av_free(origdata); return VAR_9; } block_time = (VAR_1[0] << 8) | VAR_1[1]; VAR_1 += 2; VAR_2 -= 2; VAR_12 = *VAR_1; VAR_1 += 1; VAR_2 -= 1; if (VAR_5 == -1)
VAR_5 = VAR_12 & 1 ? PKT_FLAG_KEY : 0; switch ((VAR_12 & 0x06) >> 1) { case 0x0:
VAR_13 = 1; lace_size = av_mallocz(sizeof(int)); lace_size[0] = VAR_2; break; case 0x1:
case 0x2:
case 0x3:
if (VAR_2 == 0) { VAR_9 = -1; break; } VAR_13 = (*VAR_1) + 1; VAR_1 += 1; VAR_2 -= 1; lace_size = av_mallocz(VAR_13 * sizeof(int)); switch ((VAR_12 & 0x06) >> 1) { case 0x1:  { uint8_t temp; uint32_t total = 0; for (VAR_11 = 0; VAR_9 == 0 && VAR_11 < VAR_13 - 1; VAR_11++) { while (1) { if (VAR_2 == 0) { VAR_9 = -1; break; } temp = *VAR_1; lace_size[VAR_11] += temp; VAR_1 += 1; VAR_2 -= 1; if (temp != 0xff)
break; } total += lace_size[VAR_11]; } lace_size[VAR_11] = VAR_2 - total; break; } case 0x2:
for (VAR_11 = 0; VAR_11 < VAR_13; VAR_11++) lace_size[VAR_11] = VAR_2 / VAR_13; break; case 0x3:  { uint32_t total; VAR_11 = matroska_ebmlnum_uint(VAR_1, VAR_2, &num); if (VAR_11 < 0) { av_log(VAR_0->ctx, AV_LOG_INFO,
"EBML block VAR_1 error\VAR_11"); break; } VAR_1 += VAR_11; VAR_2 -= VAR_11; total = lace_size[0] = num; for (VAR_11 = 1; VAR_9 == 0 && VAR_11 < VAR_13 - 1; VAR_11++) { 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_11"); break; } VAR_1 += VAR_14; VAR_2 -= VAR_14; lace_size[VAR_11] = lace_size[VAR_11 - 1] + snum; total += lace_size[VAR_11]; } lace_size[VAR_11] = VAR_2 - total; break; } } break; } if (VAR_9 == 0) { int VAR_15 = VAR_0->tracks[VAR_10]->VAR_12 & MATROSKA_TRACK_REAL_V; for (VAR_11 = 0; VAR_11 < VAR_13; VAR_11++) { uint64_t timecode = AV_NOPTS_VALUE; int VAR_16, VAR_17 = 1; if (VAR_15) { VAR_17 = *VAR_1++ + 1; lace_size[VAR_11]--; } if (VAR_4 != (uint64_t)-1 && VAR_11 == 0) { if (VAR_4 + block_time >= 0)
timecode = VAR_4 + block_time; } for (VAR_16=0; VAR_16<VAR_17; VAR_16++) { int VAR_18, VAR_19 = 0; if (VAR_15)
VAR_19 = rv_offset(VAR_1, VAR_16, VAR_17); if (VAR_16+1 == VAR_17)
VAR_18 = lace_size[VAR_11] - VAR_19; else
VAR_18 = rv_offset(VAR_1, VAR_16+1, VAR_17) - VAR_19; pkt = av_mallocz(sizeof(AVPacket)); if (VAR_8)  *VAR_8 = pkt; if (av_new_packet(pkt, VAR_18) < 0) { VAR_9 = AVERROR_NOMEM; VAR_11 = VAR_13-1; break; } memcpy (pkt->VAR_1, VAR_1+VAR_19, VAR_18); if (VAR_11 == 0)
pkt->VAR_12 = VAR_5; pkt->stream_index = VAR_0->tracks[VAR_10]->stream_index; pkt->pts = timecode; pkt->VAR_3 = VAR_3; if (VAR_0->tracks[VAR_10]->VAR_12 & MATROSKA_TRACK_REORDER)
matroska_queue_packet_reordered(VAR_0, pkt, VAR_6); else
matroska_queue_packet(VAR_0, pkt); } VAR_1 += lace_size[VAR_11]; } } av_free(lace_size); av_free(origdata); return VAR_9; }