~ubuntu-branches/ubuntu/oneiric/libav/oneiric

« back to all changes in this revision

Viewing changes to libavformat/matroskadec.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2011-04-30 14:27:42 UTC
  • mfrom: (1.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110430142742-quvblxk1tj6adlh5
Tags: 4:0.7~b1-1ubuntu1
* Merge from debian. Remaining changes:
  - don't build against libfaad, libdirac, librtmp and libopenjpeg
    (all in universe)
  - explicitly --enable-pic on powerpc, cf. LP #654666
  - different arm configure bits that should probably better be
    merged into debian
* Cherry-picked from git: 
  - install doc/APIChanges and refer to them in NEWS.Debian (Closes: #623682)
  - don't try to install non-existing documentation, fixes FTBFS on powerpc

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Matroska file demuxer
3
 
 * Copyright (c) 2003-2008 The FFmpeg Project
4
 
 *
5
 
 * This file is part of FFmpeg.
6
 
 *
7
 
 * FFmpeg is free software; you can redistribute it and/or
 
3
 * Copyright (c) 2003-2008 The Libav Project
 
4
 *
 
5
 * This file is part of Libav.
 
6
 *
 
7
 * Libav is free software; you can redistribute it and/or
8
8
 * modify it under the terms of the GNU Lesser General Public
9
9
 * License as published by the Free Software Foundation; either
10
10
 * version 2.1 of the License, or (at your option) any later version.
11
11
 *
12
 
 * FFmpeg is distributed in the hope that it will be useful,
 
12
 * Libav is distributed in the hope that it will be useful,
13
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
15
 * Lesser General Public License for more details.
16
16
 *
17
17
 * You should have received a copy of the GNU Lesser General Public
18
 
 * License along with FFmpeg; if not, write to the Free Software
 
18
 * License along with Libav; if not, write to the Free Software
19
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
20
 */
21
21
 
31
31
#include <stdio.h>
32
32
#include "avformat.h"
33
33
#include "internal.h"
 
34
#include "avio_internal.h"
34
35
/* For ff_codec_get_id(). */
35
36
#include "riff.h"
36
37
#include "isom.h"
58
59
    EBML_NEST,
59
60
    EBML_PASS,
60
61
    EBML_STOP,
 
62
    EBML_TYPE_COUNT
61
63
} EbmlType;
62
64
 
63
65
typedef const struct EbmlSyntax {
139
141
    double time_scale;
140
142
    uint64_t default_duration;
141
143
    uint64_t flag_default;
 
144
    uint64_t flag_forced;
142
145
    MatroskaTrackVideo video;
143
146
    MatroskaTrackAudio audio;
144
147
    EbmlList encodings;
214
217
    int num_levels;
215
218
    MatroskaLevel levels[EBML_MAX_DEPTH];
216
219
    int level_up;
 
220
    uint32_t current_id;
217
221
 
218
222
    uint64_t time_scale;
219
223
    double   duration;
234
238
    AVPacket *prev_pkt;
235
239
 
236
240
    int done;
237
 
    int has_cluster_id;
238
241
 
239
242
    /* What to skip before effectively reading a packet. */
240
243
    int skip_to_keyframe;
336
339
    { MATROSKA_ID_TRACKDEFAULTDURATION, EBML_UINT, 0, offsetof(MatroskaTrack,default_duration) },
337
340
    { MATROSKA_ID_TRACKTIMECODESCALE,   EBML_FLOAT,0, offsetof(MatroskaTrack,time_scale), {.f=1.0} },
338
341
    { MATROSKA_ID_TRACKFLAGDEFAULT,     EBML_UINT, 0, offsetof(MatroskaTrack,flag_default), {.u=1} },
 
342
    { MATROSKA_ID_TRACKFLAGFORCED,      EBML_UINT, 0, offsetof(MatroskaTrack,flag_forced), {.u=0} },
339
343
    { MATROSKA_ID_TRACKVIDEO,           EBML_NEST, 0, offsetof(MatroskaTrack,video), {.n=matroska_track_video} },
340
344
    { MATROSKA_ID_TRACKAUDIO,           EBML_NEST, 0, offsetof(MatroskaTrack,audio), {.n=matroska_track_audio} },
341
345
    { MATROSKA_ID_TRACKCONTENTENCODINGS,EBML_NEST, 0, 0, {.n=matroska_track_encodings} },
342
346
    { MATROSKA_ID_TRACKFLAGENABLED,     EBML_NONE },
343
 
    { MATROSKA_ID_TRACKFLAGFORCED,      EBML_NONE },
344
347
    { MATROSKA_ID_TRACKFLAGLACING,      EBML_NONE },
345
348
    { MATROSKA_ID_CODECNAME,            EBML_NONE },
346
349
    { MATROSKA_ID_CODECDECODEALL,       EBML_NONE },
426
429
    { MATROSKA_ID_TAGSTRING,          EBML_UTF8, 0, offsetof(MatroskaTag,string) },
427
430
    { MATROSKA_ID_TAGLANG,            EBML_STR,  0, offsetof(MatroskaTag,lang), {.s="und"} },
428
431
    { MATROSKA_ID_TAGDEFAULT,         EBML_UINT, 0, offsetof(MatroskaTag,def) },
 
432
    { MATROSKA_ID_TAGDEFAULT_BUG,     EBML_UINT, 0, offsetof(MatroskaTag,def) },
429
433
    { MATROSKA_ID_SIMPLETAG,          EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTag,sub), {.n=matroska_simpletag} },
430
434
    { 0 }
431
435
};
469
473
    { MATROSKA_ID_CUES,           EBML_NEST, 0, 0, {.n=matroska_index      } },
470
474
    { MATROSKA_ID_TAGS,           EBML_NEST, 0, 0, {.n=matroska_tags       } },
471
475
    { MATROSKA_ID_SEEKHEAD,       EBML_NEST, 0, 0, {.n=matroska_seekhead   } },
472
 
    { MATROSKA_ID_CLUSTER,        EBML_STOP, 0, offsetof(MatroskaDemuxContext,has_cluster_id) },
 
476
    { MATROSKA_ID_CLUSTER,        EBML_STOP },
473
477
    { 0 }
474
478
};
475
479
 
512
516
 */
513
517
static int ebml_level_end(MatroskaDemuxContext *matroska)
514
518
{
515
 
    ByteIOContext *pb = matroska->ctx->pb;
516
 
    int64_t pos = url_ftell(pb);
 
519
    AVIOContext *pb = matroska->ctx->pb;
 
520
    int64_t pos = avio_tell(pb);
517
521
 
518
522
    if (matroska->num_levels > 0) {
519
523
        MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
520
 
        if (pos - level->start >= level->length) {
 
524
        if (pos - level->start >= level->length || matroska->current_id) {
521
525
            matroska->num_levels--;
522
526
            return 1;
523
527
        }
533
537
 * number.
534
538
 * Returns: number of bytes read, < 0 on error
535
539
 */
536
 
static int ebml_read_num(MatroskaDemuxContext *matroska, ByteIOContext *pb,
 
540
static int ebml_read_num(MatroskaDemuxContext *matroska, AVIOContext *pb,
537
541
                         int max_size, uint64_t *number)
538
542
{
539
 
    int len_mask = 0x80, read = 1, n = 1;
540
 
    int64_t total = 0;
 
543
    int read = 1, n = 1;
 
544
    uint64_t total = 0;
541
545
 
542
 
    /* The first byte tells us the length in bytes - get_byte() can normally
 
546
    /* The first byte tells us the length in bytes - avio_r8() can normally
543
547
     * return 0, but since that's not a valid first ebmlID byte, we can
544
548
     * use it safely here to catch EOS. */
545
 
    if (!(total = get_byte(pb))) {
 
549
    if (!(total = avio_r8(pb))) {
546
550
        /* we might encounter EOS here */
547
 
        if (!url_feof(pb)) {
548
 
            int64_t pos = url_ftell(pb);
 
551
        if (!pb->eof_reached) {
 
552
            int64_t pos = avio_tell(pb);
549
553
            av_log(matroska->ctx, AV_LOG_ERROR,
550
554
                   "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
551
555
                   pos, pos);
554
558
    }
555
559
 
556
560
    /* get the length of the EBML number */
557
 
    while (read <= max_size && !(total & len_mask)) {
558
 
        read++;
559
 
        len_mask >>= 1;
560
 
    }
 
561
    read = 8 - ff_log2_tab[total];
561
562
    if (read > max_size) {
562
 
        int64_t pos = url_ftell(pb) - 1;
 
563
        int64_t pos = avio_tell(pb) - 1;
563
564
        av_log(matroska->ctx, AV_LOG_ERROR,
564
565
               "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
565
566
               (uint8_t) total, pos, pos);
567
568
    }
568
569
 
569
570
    /* read out length */
570
 
    total &= ~len_mask;
 
571
    total ^= 1 << ff_log2_tab[total];
571
572
    while (n++ < read)
572
 
        total = (total << 8) | get_byte(pb);
 
573
        total = (total << 8) | avio_r8(pb);
573
574
 
574
575
    *number = total;
575
576
 
576
577
    return read;
577
578
}
578
579
 
 
580
/**
 
581
 * Read a EBML length value.
 
582
 * This needs special handling for the "unknown length" case which has multiple
 
583
 * encodings.
 
584
 */
 
585
static int ebml_read_length(MatroskaDemuxContext *matroska, AVIOContext *pb,
 
586
                            uint64_t *number)
 
587
{
 
588
    int res = ebml_read_num(matroska, pb, 8, number);
 
589
    if (res > 0 && *number + 1 == 1ULL << (7 * res))
 
590
        *number = 0xffffffffffffffULL;
 
591
    return res;
 
592
}
 
593
 
579
594
/*
580
595
 * Read the next element as an unsigned int.
581
596
 * 0 is success, < 0 is failure.
582
597
 */
583
 
static int ebml_read_uint(ByteIOContext *pb, int size, uint64_t *num)
 
598
static int ebml_read_uint(AVIOContext *pb, int size, uint64_t *num)
584
599
{
585
600
    int n = 0;
586
601
 
587
 
    if (size < 1 || size > 8)
 
602
    if (size > 8)
588
603
        return AVERROR_INVALIDDATA;
589
604
 
590
605
    /* big-endian ordering; build up number */
591
606
    *num = 0;
592
607
    while (n++ < size)
593
 
        *num = (*num << 8) | get_byte(pb);
 
608
        *num = (*num << 8) | avio_r8(pb);
594
609
 
595
610
    return 0;
596
611
}
599
614
 * Read the next element as a float.
600
615
 * 0 is success, < 0 is failure.
601
616
 */
602
 
static int ebml_read_float(ByteIOContext *pb, int size, double *num)
 
617
static int ebml_read_float(AVIOContext *pb, int size, double *num)
603
618
{
604
 
    if (size == 4) {
605
 
        *num= av_int2flt(get_be32(pb));
 
619
    if (size == 0) {
 
620
        *num = 0;
 
621
    } else if (size == 4) {
 
622
        *num= av_int2flt(avio_rb32(pb));
606
623
    } else if(size==8){
607
 
        *num= av_int2dbl(get_be64(pb));
 
624
        *num= av_int2dbl(avio_rb64(pb));
608
625
    } else
609
626
        return AVERROR_INVALIDDATA;
610
627
 
615
632
 * Read the next element as an ASCII string.
616
633
 * 0 is success, < 0 is failure.
617
634
 */
618
 
static int ebml_read_ascii(ByteIOContext *pb, int size, char **str)
 
635
static int ebml_read_ascii(AVIOContext *pb, int size, char **str)
619
636
{
620
637
    av_free(*str);
621
638
    /* EBML strings are usually not 0-terminated, so we allocate one
622
639
     * byte more, read the string and NULL-terminate it ourselves. */
623
640
    if (!(*str = av_malloc(size + 1)))
624
641
        return AVERROR(ENOMEM);
625
 
    if (get_buffer(pb, (uint8_t *) *str, size) != size) {
626
 
        av_free(*str);
 
642
    if (avio_read(pb, (uint8_t *) *str, size) != size) {
 
643
        av_freep(str);
627
644
        return AVERROR(EIO);
628
645
    }
629
646
    (*str)[size] = '\0';
635
652
 * Read the next element as binary data.
636
653
 * 0 is success, < 0 is failure.
637
654
 */
638
 
static int ebml_read_binary(ByteIOContext *pb, int length, EbmlBin *bin)
 
655
static int ebml_read_binary(AVIOContext *pb, int length, EbmlBin *bin)
639
656
{
640
657
    av_free(bin->data);
641
658
    if (!(bin->data = av_malloc(length)))
642
659
        return AVERROR(ENOMEM);
643
660
 
644
661
    bin->size = length;
645
 
    bin->pos  = url_ftell(pb);
646
 
    if (get_buffer(pb, bin->data, length) != length)
 
662
    bin->pos  = avio_tell(pb);
 
663
    if (avio_read(pb, bin->data, length) != length) {
 
664
        av_freep(&bin->data);
647
665
        return AVERROR(EIO);
 
666
    }
648
667
 
649
668
    return 0;
650
669
}
654
673
 * are supposed to be sub-elements which can be read separately.
655
674
 * 0 is success, < 0 is failure.
656
675
 */
657
 
static int ebml_read_master(MatroskaDemuxContext *matroska, int length)
 
676
static int ebml_read_master(MatroskaDemuxContext *matroska, uint64_t length)
658
677
{
659
 
    ByteIOContext *pb = matroska->ctx->pb;
 
678
    AVIOContext *pb = matroska->ctx->pb;
660
679
    MatroskaLevel *level;
661
680
 
662
681
    if (matroska->num_levels >= EBML_MAX_DEPTH) {
666
685
    }
667
686
 
668
687
    level = &matroska->levels[matroska->num_levels++];
669
 
    level->start = url_ftell(pb);
 
688
    level->start = avio_tell(pb);
670
689
    level->length = length;
671
690
 
672
691
    return 0;
679
698
static int matroska_ebmlnum_uint(MatroskaDemuxContext *matroska,
680
699
                                 uint8_t *data, uint32_t size, uint64_t *num)
681
700
{
682
 
    ByteIOContext pb;
683
 
    init_put_byte(&pb, data, size, 0, NULL, NULL, NULL, NULL);
684
 
    return ebml_read_num(matroska, &pb, 8, num);
 
701
    AVIOContext pb;
 
702
    ffio_init_context(&pb, data, size, 0, NULL, NULL, NULL, NULL);
 
703
    return ebml_read_num(matroska, &pb, FFMIN(size, 8), num);
685
704
}
686
705
 
687
706
/*
713
732
    for (i=0; syntax[i].id; i++)
714
733
        if (id == syntax[i].id)
715
734
            break;
 
735
    if (!syntax[i].id && id == MATROSKA_ID_CLUSTER &&
 
736
        matroska->num_levels > 0 &&
 
737
        matroska->levels[matroska->num_levels-1].length == 0xffffffffffffff)
 
738
        return 0;  // we reached the end of an unknown size cluster
716
739
    if (!syntax[i].id && id != EBML_ID_VOID && id != EBML_ID_CRC32)
717
740
        av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id);
718
741
    return ebml_parse_elem(matroska, &syntax[i], data);
721
744
static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
722
745
                      void *data)
723
746
{
724
 
    uint64_t id;
725
 
    int res = ebml_read_num(matroska, matroska->ctx->pb, 4, &id);
726
 
    id |= 1 << 7*res;
727
 
    return res < 0 ? res : ebml_parse_id(matroska, syntax, id, data);
 
747
    if (!matroska->current_id) {
 
748
        uint64_t id;
 
749
        int res = ebml_read_num(matroska, matroska->ctx->pb, 4, &id);
 
750
        if (res < 0)
 
751
            return res;
 
752
        matroska->current_id = id | 1 << 7*res;
 
753
    }
 
754
    return ebml_parse_id(matroska, syntax, matroska->current_id, data);
728
755
}
729
756
 
730
757
static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
755
782
static int ebml_parse_elem(MatroskaDemuxContext *matroska,
756
783
                           EbmlSyntax *syntax, void *data)
757
784
{
758
 
    ByteIOContext *pb = matroska->ctx->pb;
 
785
    static const uint64_t max_lengths[EBML_TYPE_COUNT] = {
 
786
        [EBML_UINT]  = 8,
 
787
        [EBML_FLOAT] = 8,
 
788
        // max. 16 MB for strings
 
789
        [EBML_STR]   = 0x1000000,
 
790
        [EBML_UTF8]  = 0x1000000,
 
791
        // max. 256 MB for binary data
 
792
        [EBML_BIN]   = 0x10000000,
 
793
        // no limits for anything else
 
794
    };
 
795
    AVIOContext *pb = matroska->ctx->pb;
759
796
    uint32_t id = syntax->id;
760
797
    uint64_t length;
761
798
    int res;
769
806
        list->nb_elem++;
770
807
    }
771
808
 
772
 
    if (syntax->type != EBML_PASS && syntax->type != EBML_STOP)
773
 
        if ((res = ebml_read_num(matroska, pb, 8, &length)) < 0)
 
809
    if (syntax->type != EBML_PASS && syntax->type != EBML_STOP) {
 
810
        matroska->current_id = 0;
 
811
        if ((res = ebml_read_length(matroska, pb, &length)) < 0)
774
812
            return res;
 
813
        if (max_lengths[syntax->type] && length > max_lengths[syntax->type]) {
 
814
            av_log(matroska->ctx, AV_LOG_ERROR,
 
815
                   "Invalid length 0x%"PRIx64" > 0x%"PRIx64" for syntax element %i\n",
 
816
                   length, max_lengths[syntax->type], syntax->type);
 
817
            return AVERROR_INVALIDDATA;
 
818
        }
 
819
    }
775
820
 
776
821
    switch (syntax->type) {
777
822
    case EBML_UINT:  res = ebml_read_uint  (pb, length, data);  break;
782
827
    case EBML_NEST:  if ((res=ebml_read_master(matroska, length)) < 0)
783
828
                         return res;
784
829
                     if (id == MATROSKA_ID_SEGMENT)
785
 
                         matroska->segment_start = url_ftell(matroska->ctx->pb);
 
830
                         matroska->segment_start = avio_tell(matroska->ctx->pb);
786
831
                     return ebml_parse_nest(matroska, syntax->def.n, data);
787
832
    case EBML_PASS:  return ebml_parse_id(matroska, syntax->def.n, id, data);
788
 
    case EBML_STOP:  *(int *)data = 1;      return 1;
789
 
    default:         return url_fseek(pb,length,SEEK_CUR)<0 ? AVERROR(EIO) : 0;
 
833
    case EBML_STOP:  return 1;
 
834
    default:         return avio_skip(pb,length)<0 ? AVERROR(EIO) : 0;
790
835
    }
791
836
    if (res == AVERROR_INVALIDDATA)
792
837
        av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n");
887
932
    int result = 0;
888
933
    int olen;
889
934
 
 
935
    if (pkt_size >= 10000000)
 
936
        return -1;
 
937
 
890
938
    switch (encodings[0].compression.algo) {
891
939
    case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
892
940
        return encodings[0].compression.settings.size;
1003
1051
 
1004
1052
    for (i=0; i < list->nb_elem; i++) {
1005
1053
        const char *lang = strcmp(tags[i].lang, "und") ? tags[i].lang : NULL;
 
1054
 
 
1055
        if (!tags[i].name) {
 
1056
            av_log(s, AV_LOG_WARNING, "Skipping invalid tag with no TagName.\n");
 
1057
            continue;
 
1058
        }
1006
1059
        if (prefix)  snprintf(key, sizeof(key), "%s/%s", prefix, tags[i].name);
1007
1060
        else         av_strlcpy(key, tags[i].name, sizeof(key));
1008
1061
        if (tags[i].def || !lang) {
1018
1071
                matroska_convert_tag(s, &tags[i].sub, metadata, key);
1019
1072
        }
1020
1073
    }
 
1074
    ff_metadata_conv(metadata, NULL, ff_mkv_metadata_conv);
1021
1075
}
1022
1076
 
1023
1077
static void matroska_convert_tags(AVFormatContext *s)
1057
1111
    EbmlList *seekhead_list = &matroska->seekhead;
1058
1112
    MatroskaSeekhead *seekhead = seekhead_list->elem;
1059
1113
    uint32_t level_up = matroska->level_up;
1060
 
    int64_t before_pos = url_ftell(matroska->ctx->pb);
 
1114
    int64_t before_pos = avio_tell(matroska->ctx->pb);
 
1115
    uint32_t saved_id = matroska->current_id;
1061
1116
    MatroskaLevel level;
1062
1117
    int i;
1063
1118
 
 
1119
    // we should not do any seeking in the streaming case
 
1120
    if (!matroska->ctx->pb->seekable ||
 
1121
        (matroska->ctx->flags & AVFMT_FLAG_IGNIDX))
 
1122
        return;
 
1123
 
1064
1124
    for (i=0; i<seekhead_list->nb_elem; i++) {
1065
1125
        int64_t offset = seekhead[i].pos + matroska->segment_start;
1066
1126
 
1070
1130
            continue;
1071
1131
 
1072
1132
        /* seek */
1073
 
        if (url_fseek(matroska->ctx->pb, offset, SEEK_SET) != offset)
 
1133
        if (avio_seek(matroska->ctx->pb, offset, SEEK_SET) != offset)
1074
1134
            continue;
1075
1135
 
1076
1136
        /* We don't want to lose our seekhead level, so we add
1086
1146
        level.length = (uint64_t)-1;
1087
1147
        matroska->levels[matroska->num_levels] = level;
1088
1148
        matroska->num_levels++;
 
1149
        matroska->current_id = 0;
1089
1150
 
1090
1151
        ebml_parse(matroska, matroska_segment, matroska);
1091
1152
 
1098
1159
    }
1099
1160
 
1100
1161
    /* seek back */
1101
 
    url_fseek(matroska->ctx->pb, before_pos, SEEK_SET);
 
1162
    avio_seek(matroska->ctx->pb, before_pos, SEEK_SET);
1102
1163
    matroska->level_up = level_up;
 
1164
    matroska->current_id = saved_id;
1103
1165
}
1104
1166
 
1105
1167
static int matroska_aac_profile(char *codec_id)
1137
1199
    uint64_t max_start = 0;
1138
1200
    Ebml ebml = { 0 };
1139
1201
    AVStream *st;
1140
 
    int i, j;
 
1202
    int i, j, res;
1141
1203
 
1142
1204
    matroska->ctx = s;
1143
1205
 
1158
1220
    if (i >= FF_ARRAY_ELEMS(matroska_doctypes)) {
1159
1221
        av_log(s, AV_LOG_WARNING, "Unknown EBML doctype '%s'\n", ebml.doctype);
1160
1222
    }
1161
 
    av_metadata_set2(&s->metadata, "doctype", ebml.doctype, 0);
1162
1223
    ebml_free(ebml_syntax, &ebml);
1163
1224
 
1164
1225
    /* The next thing is a segment. */
1165
 
    if (ebml_parse(matroska, matroska_segments, matroska) < 0)
1166
 
        return -1;
 
1226
    if ((res = ebml_parse(matroska, matroska_segments, matroska)) < 0)
 
1227
        return res;
1167
1228
    matroska_execute_seekhead(matroska);
1168
1229
 
 
1230
    if (!matroska->time_scale)
 
1231
        matroska->time_scale = 1000000;
1169
1232
    if (matroska->duration)
1170
1233
        matroska->ctx->duration = matroska->duration * matroska->time_scale
1171
1234
                                  * 1000 / AV_TIME_BASE;
1180
1243
        uint8_t *extradata = NULL;
1181
1244
        int extradata_size = 0;
1182
1245
        int extradata_offset = 0;
1183
 
        ByteIOContext b;
 
1246
        AVIOContext b;
1184
1247
 
1185
1248
        /* Apply some sanity checks. */
1186
1249
        if (track->type != MATROSKA_TRACK_TYPE_VIDEO &&
1266
1329
        } else if (!strcmp(track->codec_id, "A_MS/ACM")
1267
1330
                   && track->codec_priv.size >= 14
1268
1331
                   && track->codec_priv.data != NULL) {
1269
 
            init_put_byte(&b, track->codec_priv.data, track->codec_priv.size,
1270
 
                          URL_RDONLY, NULL, NULL, NULL, NULL);
1271
 
            ff_get_wav_header(&b, st->codec, track->codec_priv.size);
 
1332
            int ret;
 
1333
            ffio_init_context(&b, track->codec_priv.data, track->codec_priv.size,
 
1334
                          AVIO_RDONLY, NULL, NULL, NULL, NULL);
 
1335
            ret = ff_get_wav_header(&b, st->codec, track->codec_priv.size);
 
1336
            if (ret < 0)
 
1337
                return ret;
1272
1338
            codec_id = st->codec->codec_id;
1273
1339
            extradata_offset = FFMIN(track->codec_priv.size, 18);
1274
1340
        } else if (!strcmp(track->codec_id, "V_QUICKTIME")
1311
1377
            extradata = av_mallocz(extradata_size);
1312
1378
            if (extradata == NULL)
1313
1379
                return AVERROR(ENOMEM);
1314
 
            init_put_byte(&b, extradata, extradata_size, 1,
 
1380
            ffio_init_context(&b, extradata, extradata_size, 1,
1315
1381
                          NULL, NULL, NULL, NULL);
1316
 
            put_buffer(&b, "TTA1", 4);
1317
 
            put_le16(&b, 1);
1318
 
            put_le16(&b, track->audio.channels);
1319
 
            put_le16(&b, track->audio.bitdepth);
1320
 
            put_le32(&b, track->audio.out_samplerate);
1321
 
            put_le32(&b, matroska->ctx->duration * track->audio.out_samplerate);
 
1382
            avio_write(&b, "TTA1", 4);
 
1383
            avio_wl16(&b, 1);
 
1384
            avio_wl16(&b, track->audio.channels);
 
1385
            avio_wl16(&b, track->audio.bitdepth);
 
1386
            avio_wl32(&b, track->audio.out_samplerate);
 
1387
            avio_wl32(&b, matroska->ctx->duration * track->audio.out_samplerate);
1322
1388
        } else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
1323
1389
                   codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
1324
1390
            extradata_offset = 26;
1328
1394
        } else if (codec_id == CODEC_ID_RA_288 || codec_id == CODEC_ID_COOK ||
1329
1395
                   codec_id == CODEC_ID_ATRAC3 || codec_id == CODEC_ID_SIPR) {
1330
1396
            int flavor;
1331
 
            init_put_byte(&b, track->codec_priv.data,track->codec_priv.size,
 
1397
            ffio_init_context(&b, track->codec_priv.data,track->codec_priv.size,
1332
1398
                          0, NULL, NULL, NULL, NULL);
1333
 
            url_fskip(&b, 22);
1334
 
            flavor                       = get_be16(&b);
1335
 
            track->audio.coded_framesize = get_be32(&b);
1336
 
            url_fskip(&b, 12);
1337
 
            track->audio.sub_packet_h    = get_be16(&b);
1338
 
            track->audio.frame_size      = get_be16(&b);
1339
 
            track->audio.sub_packet_size = get_be16(&b);
 
1399
            avio_skip(&b, 22);
 
1400
            flavor                       = avio_rb16(&b);
 
1401
            track->audio.coded_framesize = avio_rb32(&b);
 
1402
            avio_skip(&b, 12);
 
1403
            track->audio.sub_packet_h    = avio_rb16(&b);
 
1404
            track->audio.frame_size      = avio_rb16(&b);
 
1405
            track->audio.sub_packet_size = avio_rb16(&b);
1340
1406
            track->audio.buf = av_malloc(track->audio.frame_size * track->audio.sub_packet_h);
1341
1407
            if (codec_id == CODEC_ID_RA_288) {
1342
1408
                st->codec->block_align = track->audio.coded_framesize;
1369
1435
 
1370
1436
        if (track->flag_default)
1371
1437
            st->disposition |= AV_DISPOSITION_DEFAULT;
 
1438
        if (track->flag_forced)
 
1439
            st->disposition |= AV_DISPOSITION_FORCED;
1372
1440
 
1373
1441
        if (track->default_duration)
1374
1442
            av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
1706
1774
                int offset = 0, pkt_size = lace_size[n];
1707
1775
                uint8_t *pkt_data = data;
1708
1776
 
1709
 
                if (lace_size[n] > size) {
 
1777
                if (pkt_size > size) {
1710
1778
                    av_log(matroska->ctx, AV_LOG_ERROR, "Invalid packet size\n");
1711
1779
                    break;
1712
1780
                }
1751
1819
                if (matroska->prev_pkt &&
1752
1820
                    timecode != AV_NOPTS_VALUE &&
1753
1821
                    matroska->prev_pkt->pts == timecode &&
1754
 
                    matroska->prev_pkt->stream_index == st->index)
 
1822
                    matroska->prev_pkt->stream_index == st->index &&
 
1823
                    st->codec->codec_id == CODEC_ID_SSA)
1755
1824
                    matroska_merge_packets(matroska->prev_pkt, pkt);
1756
1825
                else {
1757
1826
                    dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
1776
1845
    EbmlList *blocks_list;
1777
1846
    MatroskaBlock *blocks;
1778
1847
    int i, res;
1779
 
    int64_t pos = url_ftell(matroska->ctx->pb);
 
1848
    int64_t pos = avio_tell(matroska->ctx->pb);
1780
1849
    matroska->prev_pkt = NULL;
1781
 
    if (matroska->has_cluster_id){
1782
 
        /* For the first cluster we parse, its ID was already read as
1783
 
           part of matroska_read_header(), so don't read it again */
1784
 
        res = ebml_parse_id(matroska, matroska_clusters,
1785
 
                            MATROSKA_ID_CLUSTER, &cluster);
 
1850
    if (matroska->current_id)
1786
1851
        pos -= 4;  /* sizeof the ID which was already read */
1787
 
        matroska->has_cluster_id = 0;
1788
 
    } else
1789
 
        res = ebml_parse(matroska, matroska_clusters, &cluster);
 
1852
    res = ebml_parse(matroska, matroska_clusters, &cluster);
1790
1853
    blocks_list = &cluster.blocks;
1791
1854
    blocks = blocks_list->elem;
1792
1855
    for (i=0; i<blocks_list->nb_elem; i++)
1793
 
        if (blocks[i].bin.size > 0) {
 
1856
        if (blocks[i].bin.size > 0 && blocks[i].bin.data) {
1794
1857
            int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1;
 
1858
            if (!blocks[i].non_simple)
 
1859
                blocks[i].duration = AV_NOPTS_VALUE;
1795
1860
            res=matroska_parse_block(matroska,
1796
1861
                                     blocks[i].bin.data, blocks[i].bin.size,
1797
1862
                                     blocks[i].bin.pos,  cluster.timecode,
1829
1894
    timestamp = FFMAX(timestamp, st->index_entries[0].timestamp);
1830
1895
 
1831
1896
    if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
1832
 
        url_fseek(s->pb, st->index_entries[st->nb_index_entries-1].pos, SEEK_SET);
 
1897
        avio_seek(s->pb, st->index_entries[st->nb_index_entries-1].pos, SEEK_SET);
1833
1898
        while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
1834
1899
            matroska_clear_queue(matroska);
1835
1900
            if (matroska_parse_cluster(matroska) < 0)
1854
1919
        }
1855
1920
    }
1856
1921
 
1857
 
    url_fseek(s->pb, st->index_entries[index_min].pos, SEEK_SET);
 
1922
    avio_seek(s->pb, st->index_entries[index_min].pos, SEEK_SET);
1858
1923
    matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
1859
1924
    matroska->skip_to_timecode = st->index_entries[index].timestamp;
1860
1925
    matroska->done = 0;
1878
1943
    return 0;
1879
1944
}
1880
1945
 
1881
 
AVInputFormat matroska_demuxer = {
1882
 
    "matroska",
1883
 
    NULL_IF_CONFIG_SMALL("Matroska file format"),
 
1946
AVInputFormat ff_matroska_demuxer = {
 
1947
    "matroska,webm",
 
1948
    NULL_IF_CONFIG_SMALL("Matroska/WebM file format"),
1884
1949
    sizeof(MatroskaDemuxContext),
1885
1950
    matroska_probe,
1886
1951
    matroska_read_header,
1887
1952
    matroska_read_packet,
1888
1953
    matroska_read_close,
1889
1954
    matroska_read_seek,
1890
 
    .metadata_conv = ff_mkv_metadata_conv,
1891
1955
};