Revision 209472b4 libavformat/matroskadec.c

View differences:

libavformat/matroskadec.c
216 216
    AVStream *skip_to_stream;
217 217
} MatroskaDemuxContext;
218 218

  
219
typedef struct {
220
    uint64_t duration;
221
    int64_t  reference;
222
    EbmlBin  bin;
223
} MatroskaBlock;
224

  
225
typedef struct {
226
    uint64_t timecode;
227
    EbmlList blocks;
228
} MatroskaCluster;
229

  
219 230
#define ARRAY_SIZE(x)  (sizeof(x)/sizeof(*x))
220 231

  
221 232
static EbmlSyntax ebml_header[] = {
......
426 437
    { 0 }
427 438
};
428 439

  
440
static EbmlSyntax matroska_blockgroup[] = {
441
    { MATROSKA_ID_BLOCK,          EBML_BIN,  0, offsetof(MatroskaBlock,bin) },
442
    { MATROSKA_ID_SIMPLEBLOCK,    EBML_BIN,  0, offsetof(MatroskaBlock,bin) },
443
    { MATROSKA_ID_BLOCKDURATION,  EBML_UINT, 0, offsetof(MatroskaBlock,duration), {.u=AV_NOPTS_VALUE} },
444
    { MATROSKA_ID_BLOCKREFERENCE, EBML_UINT, 0, offsetof(MatroskaBlock,reference) },
445
    { EBML_ID_VOID,               EBML_NONE },
446
    { 0 }
447
};
448

  
449
static EbmlSyntax matroska_cluster[] = {
450
    { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) },
451
    { MATROSKA_ID_BLOCKGROUP,     EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
452
    { MATROSKA_ID_SIMPLEBLOCK,    EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
453
    { EBML_ID_VOID,               EBML_NONE },
454
    { 0 }
455
};
456

  
457
static EbmlSyntax matroska_clusters[] = {
458
    { MATROSKA_ID_CLUSTER,        EBML_NEST, 0, 0, {.n=matroska_cluster} },
459
    { 0 }
460
};
461

  
429 462
/*
430 463
 * The first few functions handle EBML file parsing. The rest
431 464
 * is the document interpretation. Matroska really just is a
......
1675 1708
    int res = 0;
1676 1709
    AVStream *st;
1677 1710
    AVPacket *pkt;
1678
    uint8_t *origdata = data;
1679 1711
    int16_t block_time;
1680 1712
    uint32_t *lace_size = NULL;
1681 1713
    int n, flags, laces = 0;
......
1684 1716
    /* first byte(s): tracknum */
1685 1717
    if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
1686 1718
        av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
1687
        av_free(origdata);
1688 1719
        return res;
1689 1720
    }
1690 1721
    data += n;
......
1695 1726
    if (size <= 3 || !track || !track->stream) {
1696 1727
        av_log(matroska->ctx, AV_LOG_INFO,
1697 1728
               "Invalid stream %"PRIu64" or size %u\n", num, size);
1698
        av_free(origdata);
1699 1729
        return res;
1700 1730
    }
1701 1731
    st = track->stream;
1702 1732
    if (st->discard >= AVDISCARD_ALL) {
1703
        av_free(origdata);
1704 1733
        return res;
1705 1734
    }
1706 1735
    if (duration == AV_NOPTS_VALUE)
......
1716 1745

  
1717 1746
    if (matroska->skip_to_keyframe) {
1718 1747
        if (!is_keyframe || st != matroska->skip_to_stream) {
1719
            av_free(origdata);
1720 1748
            return res;
1721 1749
        }
1722 1750
        matroska->skip_to_keyframe = 0;
......
1885 1913
    }
1886 1914

  
1887 1915
    av_free(lace_size);
1888
    av_free(origdata);
1889
    return res;
1890
}
1891

  
1892
static int
1893
matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
1894
                           uint64_t              cluster_time)
1895
{
1896
    int res = 0;
1897
    uint32_t id;
1898
    int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
1899
    uint64_t duration = AV_NOPTS_VALUE;
1900
    uint8_t *data;
1901
    int size = 0;
1902
    int64_t pos = 0;
1903

  
1904
    av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
1905

  
1906
    while (res == 0) {
1907
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1908
            res = AVERROR(EIO);
1909
            break;
1910
        } else if (matroska->level_up) {
1911
            matroska->level_up--;
1912
            break;
1913
        }
1914

  
1915
        switch (id) {
1916
            /* one block inside the group. Note, block parsing is one
1917
             * of the harder things, so this code is a bit complicated.
1918
             * See http://www.matroska.org/ for documentation. */
1919
            case MATROSKA_ID_BLOCK: {
1920
                pos = url_ftell(matroska->ctx->pb);
1921
                res = ebml_read_binary(matroska, &id, &data, &size);
1922
                break;
1923
            }
1924

  
1925
            case MATROSKA_ID_BLOCKDURATION: {
1926
                if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
1927
                    break;
1928
                break;
1929
            }
1930

  
1931
            case MATROSKA_ID_BLOCKREFERENCE: {
1932
                int64_t num;
1933
                /* We've found a reference, so not even the first frame in
1934
                 * the lace is a key frame. */
1935
                is_keyframe = 0;
1936
                if (last_num_packets != matroska->num_packets)
1937
                    matroska->packets[last_num_packets]->flags = 0;
1938
                if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
1939
                    break;
1940
                break;
1941
            }
1942

  
1943
            default:
1944
                av_log(matroska->ctx, AV_LOG_INFO,
1945
                       "Unknown entry 0x%x in blockgroup data\n", id);
1946
                /* fall-through */
1947

  
1948
            case EBML_ID_VOID:
1949
                res = ebml_read_skip(matroska);
1950
                break;
1951
        }
1952

  
1953
        if (matroska->level_up) {
1954
            matroska->level_up--;
1955
            break;
1956
        }
1957
    }
1958

  
1959
    if (res)
1960
        return res;
1961

  
1962
    if (size > 0)
1963
        res = matroska_parse_block(matroska, data, size, pos, cluster_time,
1964
                                   duration, is_keyframe);
1965

  
1966 1916
    return res;
1967 1917
}
1968 1918

  
1969 1919
static int
1970 1920
matroska_parse_cluster (MatroskaDemuxContext *matroska)
1971 1921
{
1972
    int res = 0;
1973
    uint32_t id;
1974
    uint64_t cluster_time = 0;
1975
    uint8_t *data;
1976
    int64_t pos;
1977
    int size;
1978

  
1979
    av_log(matroska->ctx, AV_LOG_DEBUG,
1980
           "parsing cluster at %"PRId64"\n", url_ftell(matroska->ctx->pb));
1981

  
1982
    while (res == 0) {
1983
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1984
            res = AVERROR(EIO);
1985
            break;
1986
        } else if (matroska->level_up) {
1987
            matroska->level_up--;
1988
            break;
1989
        }
1990

  
1991
        switch (id) {
1992
            /* cluster timecode */
1993
            case MATROSKA_ID_CLUSTERTIMECODE: {
1994
                uint64_t num;
1995
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1996
                    break;
1997
                cluster_time = num;
1998
                break;
1999
            }
2000

  
2001
                /* a group of blocks inside a cluster */
2002
            case MATROSKA_ID_BLOCKGROUP:
2003
                if ((res = ebml_read_master(matroska, &id)) < 0)
2004
                    break;
2005
                res = matroska_parse_blockgroup(matroska, cluster_time);
2006
                break;
2007

  
2008
            case MATROSKA_ID_SIMPLEBLOCK:
2009
                pos = url_ftell(matroska->ctx->pb);
2010
                res = ebml_read_binary(matroska, &id, &data, &size);
2011
                if (res == 0)
2012
                    res = matroska_parse_block(matroska, data, size, pos,
2013
                                               cluster_time, AV_NOPTS_VALUE,
2014
                                               -1);
2015
                break;
2016

  
2017
            default:
2018
                av_log(matroska->ctx, AV_LOG_INFO,
2019
                       "Unknown entry 0x%x in cluster data\n", id);
2020
                /* fall-through */
2021

  
2022
            case EBML_ID_VOID:
2023
                res = ebml_read_skip(matroska);
2024
                break;
2025
        }
2026

  
2027
        if (matroska->level_up) {
2028
            matroska->level_up--;
2029
            break;
2030
        }
2031
    }
2032

  
1922
    MatroskaCluster cluster = { 0 };
1923
    EbmlList *blocks_list;
1924
    MatroskaBlock *blocks;
1925
    int i, res = ebml_parse(matroska, matroska_clusters, &cluster, 0, 1);
1926
    blocks_list = &cluster.blocks;
1927
    blocks = blocks_list->elem;
1928
    for (i=0; !res && i<blocks_list->nb_elem; i++)
1929
        if (blocks[i].bin.size > 0)
1930
            res=matroska_parse_block(matroska,
1931
                                     blocks[i].bin.data, blocks[i].bin.size,
1932
                                     blocks[i].bin.pos,  cluster.timecode,
1933
                                     blocks[i].duration, !blocks[i].reference);
1934
    ebml_free(matroska_cluster, &cluster);
2033 1935
    return res;
2034 1936
}
2035 1937

  
......
2038 1940
                      AVPacket        *pkt)
2039 1941
{
2040 1942
    MatroskaDemuxContext *matroska = s->priv_data;
2041
    int res;
2042
    uint32_t id;
2043 1943

  
2044 1944
    /* Read stream until we have a packet queued. */
2045 1945
    while (matroska_deliver_packet(matroska, pkt)) {
......
2048 1948
        if (matroska->done)
2049 1949
            return AVERROR(EIO);
2050 1950

  
2051
        res = 0;
2052
        while (res == 0) {
2053
            if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2054
                return AVERROR(EIO);
2055
            } else if (matroska->level_up) {
2056
                matroska->level_up--;
2057
                break;
2058
            }
2059

  
2060
            switch (id) {
2061
                case MATROSKA_ID_CLUSTER:
2062
                    if ((res = ebml_read_master(matroska, &id)) < 0)
2063
                        break;
2064
                    if ((res = matroska_parse_cluster(matroska)) == 0)
2065
                        res = 1; /* Parsed one cluster, let's get out. */
2066
                    break;
2067

  
2068
                default:
2069
                case EBML_ID_VOID:
2070
                    res = ebml_read_skip(matroska);
2071
                    break;
2072
            }
2073

  
2074
            if (matroska->level_up) {
2075
                matroska->level_up--;
2076
                break;
2077
            }
2078
        }
2079

  
2080
        if (res == -1)
1951
        if (matroska_parse_cluster(matroska) < 0)
2081 1952
            matroska->done = 1;
2082 1953
    }
2083 1954

  

Also available in: Unified diff