Revision 737c40da

View differences:

libavformat/matroskadec.c
595 595
}
596 596

  
597 597
/*
598
 * Read the next element as binary data.
599
 * 0 is success, < 0 is failure.
600
 */
601
static int ebml_read_binary(ByteIOContext *pb, int length, EbmlBin *bin)
602
{
603
    av_free(bin->data);
604
    if (!(bin->data = av_malloc(length)))
605
        return AVERROR(ENOMEM);
606

  
607
    bin->size = length;
608
    bin->pos  = url_ftell(pb);
609
    if (get_buffer(pb, bin->data, length) != length)
610
        return AVERROR(EIO);
611

  
612
    return 0;
613
}
614

  
615
/*
598 616
 * Read the next element, but only the header. The contents
599 617
 * are supposed to be sub-elements which can be read separately.
600 618
 * 0 is success, < 0 is failure.
......
618 636
}
619 637

  
620 638
/*
621
 * Read the next element as binary data.
622
 * 0 is success, < 0 is failure.
623
 */
624
static int ebml_read_binary(ByteIOContext *pb, int length, EbmlBin *bin)
625
{
626
    av_free(bin->data);
627
    if (!(bin->data = av_malloc(length)))
628
        return AVERROR(ENOMEM);
629

  
630
    bin->size = length;
631
    bin->pos  = url_ftell(pb);
632
    if (get_buffer(pb, bin->data, length) != length)
633
        return AVERROR(EIO);
634

  
635
    return 0;
636
}
637

  
638
/*
639 639
 * Read signed/unsigned "EBML" numbers.
640 640
 * Return: number of bytes processed, < 0 on error.
641 641
 * XXX: use ebml_read_num().
......
696 696
    return res;
697 697
}
698 698

  
699
static int ebml_parse_elem(MatroskaDemuxContext *matroska,
700
                           EbmlSyntax *syntax, void *data);
699 701

  
700
static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska,
701
                                                 int num)
702
static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
703
                         uint32_t id, void *data)
702 704
{
703
    MatroskaTrack *tracks = matroska->tracks.elem;
704 705
    int i;
705

  
706
    for (i=0; i < matroska->tracks.nb_elem; i++)
707
        if (tracks[i].num == num)
708
            return &tracks[i];
709

  
710
    av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num);
711
    return NULL;
706
    for (i=0; syntax[i].id; i++)
707
        if (id == syntax[i].id)
708
            break;
709
    if (!syntax[i].id)
710
        av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id);
711
    return ebml_parse_elem(matroska, &syntax[i], data);
712 712
}
713 713

  
714

  
715
/*
716
 * Put one packet in an application-supplied AVPacket struct.
717
 * Returns 0 on success or -1 on failure.
718
 */
719
static int matroska_deliver_packet(MatroskaDemuxContext *matroska,
720
                                   AVPacket *pkt)
714
static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
715
                      void *data)
721 716
{
722
    if (matroska->num_packets > 0) {
723
        memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
724
        av_free(matroska->packets[0]);
725
        if (matroska->num_packets > 1) {
726
            memmove(&matroska->packets[0], &matroska->packets[1],
727
                    (matroska->num_packets - 1) * sizeof(AVPacket *));
728
            matroska->packets =
729
                av_realloc(matroska->packets, (matroska->num_packets - 1) *
730
                           sizeof(AVPacket *));
731
        } else {
732
            av_freep(&matroska->packets);
733
        }
734
        matroska->num_packets--;
735
        return 0;
736
    }
737

  
738
    return -1;
717
    uint32_t id;
718
    int res = ebml_read_element_id(matroska, &id);
719
    return res < 0 ? res : ebml_parse_id(matroska, syntax, id, data);
739 720
}
740 721

  
741
/*
742
 * Put a packet into our internal queue. Will be delivered to the
743
 * user/application during the next get_packet() call.
744
 */
745
static void matroska_queue_packet(MatroskaDemuxContext *matroska, AVPacket *pkt)
722
static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
723
                           void *data)
746 724
{
747
    matroska->packets =
748
        av_realloc(matroska->packets, (matroska->num_packets + 1) *
749
                   sizeof(AVPacket *));
750
    matroska->packets[matroska->num_packets] = pkt;
751
    matroska->num_packets++;
752
}
725
    int i, res = 0;
753 726

  
754
/*
755
 * Free all packets in our internal queue.
756
 */
757
static void matroska_clear_queue(MatroskaDemuxContext *matroska)
758
{
759
    if (matroska->packets) {
760
        int n;
761
        for (n = 0; n < matroska->num_packets; n++) {
762
            av_free_packet(matroska->packets[n]);
763
            av_free(matroska->packets[n]);
727
    for (i=0; syntax[i].id; i++)
728
        switch (syntax[i].type) {
729
        case EBML_UINT:
730
            *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u;
731
            break;
732
        case EBML_FLOAT:
733
            *(double   *)((char *)data+syntax[i].data_offset) = syntax[i].def.f;
734
            break;
735
        case EBML_STR:
736
        case EBML_UTF8:
737
            *(char    **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s);
738
            break;
764 739
        }
765
        av_free(matroska->packets);
766
        matroska->packets = NULL;
767
        matroska->num_packets = 0;
768
    }
769
}
770

  
771

  
772
/*
773
 * Autodetecting...
774
 */
775
static int matroska_probe(AVProbeData *p)
776
{
777
    uint64_t total = 0;
778
    int len_mask = 0x80, size = 1, n = 1;
779
    char probe_data[] = "matroska";
780

  
781
    /* ebml header? */
782
    if (AV_RB32(p->buf) != EBML_ID_HEADER)
783
        return 0;
784

  
785
    /* length of header */
786
    total = p->buf[4];
787
    while (size <= 8 && !(total & len_mask)) {
788
        size++;
789
        len_mask >>= 1;
790
    }
791
    if (size > 8)
792
      return 0;
793
    total &= (len_mask - 1);
794
    while (n < size)
795
        total = (total << 8) | p->buf[4 + n++];
796

  
797
    /* does the probe data contain the whole header? */
798
    if (p->buf_size < 4 + size + total)
799
      return 0;
800 740

  
801
    /* the header must contain the document type 'matroska'. For now,
802
     * we don't parse the whole header but simply check for the
803
     * availability of that array of characters inside the header.
804
     * Not fully fool-proof, but good enough. */
805
    for (n = 4+size; n <= 4+size+total-(sizeof(probe_data)-1); n++)
806
        if (!memcmp(p->buf+n, probe_data, sizeof(probe_data)-1))
807
            return AVPROBE_SCORE_MAX;
741
    while (!res && !ebml_level_end(matroska))
742
        res = ebml_parse(matroska, syntax, data);
808 743

  
809
    return 0;
744
    return res;
810 745
}
811 746

  
812
static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
813
                         uint32_t id, void *data);
814
static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
815
                           void *data);
816

  
817 747
static int ebml_parse_elem(MatroskaDemuxContext *matroska,
818 748
                           EbmlSyntax *syntax, void *data)
819 749
{
......
857 787
    return res;
858 788
}
859 789

  
860
static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
861
                         uint32_t id, void *data)
862
{
863
    int i;
864
    for (i=0; syntax[i].id; i++)
865
        if (id == syntax[i].id)
866
            break;
867
    if (!syntax[i].id)
868
        av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id);
869
    return ebml_parse_elem(matroska, &syntax[i], data);
870
}
871

  
872
static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
873
                      void *data)
874
{
875
    uint32_t id;
876
    int res = ebml_read_element_id(matroska, &id);
877
    return res < 0 ? res : ebml_parse_id(matroska, syntax, id, data);
878
}
879

  
880
static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
881
                           void *data)
882
{
883
    int i, res = 0;
884

  
885
    for (i=0; syntax[i].id; i++)
886
        switch (syntax[i].type) {
887
        case EBML_UINT:
888
            *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u;
889
            break;
890
        case EBML_FLOAT:
891
            *(double   *)((char *)data+syntax[i].data_offset) = syntax[i].def.f;
892
            break;
893
        case EBML_STR:
894
        case EBML_UTF8:
895
            *(char    **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s);
896
            break;
897
        }
898

  
899
    while (!res && !ebml_level_end(matroska))
900
        res = ebml_parse(matroska, syntax, data);
901

  
902
    return res;
903
}
904

  
905 790
static void ebml_free(EbmlSyntax *syntax, void *data)
906 791
{
907 792
    int i, j;
......
925 810
    }
926 811
}
927 812

  
813

  
814
/*
815
 * Autodetecting...
816
 */
817
static int matroska_probe(AVProbeData *p)
818
{
819
    uint64_t total = 0;
820
    int len_mask = 0x80, size = 1, n = 1;
821
    char probe_data[] = "matroska";
822

  
823
    /* ebml header? */
824
    if (AV_RB32(p->buf) != EBML_ID_HEADER)
825
        return 0;
826

  
827
    /* length of header */
828
    total = p->buf[4];
829
    while (size <= 8 && !(total & len_mask)) {
830
        size++;
831
        len_mask >>= 1;
832
    }
833
    if (size > 8)
834
      return 0;
835
    total &= (len_mask - 1);
836
    while (n < size)
837
        total = (total << 8) | p->buf[4 + n++];
838

  
839
    /* does the probe data contain the whole header? */
840
    if (p->buf_size < 4 + size + total)
841
      return 0;
842

  
843
    /* the header must contain the document type 'matroska'. For now,
844
     * we don't parse the whole header but simply check for the
845
     * availability of that array of characters inside the header.
846
     * Not fully fool-proof, but good enough. */
847
    for (n = 4+size; n <= 4+size+total-(sizeof(probe_data)-1); n++)
848
        if (!memcmp(p->buf+n, probe_data, sizeof(probe_data)-1))
849
            return AVPROBE_SCORE_MAX;
850

  
851
    return 0;
852
}
853

  
854
static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska,
855
                                                 int num)
856
{
857
    MatroskaTrack *tracks = matroska->tracks.elem;
858
    int i;
859

  
860
    for (i=0; i < matroska->tracks.nb_elem; i++)
861
        if (tracks[i].num == num)
862
            return &tracks[i];
863

  
864
    av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num);
865
    return NULL;
866
}
867

  
928 868
static int matroska_decode_buffer(uint8_t** buf, int* buf_size,
929 869
                                  MatroskaTrack *track)
930 870
{
......
1378 1318
    return 0;
1379 1319
}
1380 1320

  
1321
/*
1322
 * Put a packet into our internal queue. Will be delivered to the
1323
 * user/application during the next get_packet() call.
1324
 */
1325
static void matroska_queue_packet(MatroskaDemuxContext *matroska, AVPacket *pkt)
1326
{
1327
    matroska->packets =
1328
        av_realloc(matroska->packets, (matroska->num_packets + 1) *
1329
                   sizeof(AVPacket *));
1330
    matroska->packets[matroska->num_packets] = pkt;
1331
    matroska->num_packets++;
1332
}
1333

  
1334
/*
1335
 * Put one packet in an application-supplied AVPacket struct.
1336
 * Returns 0 on success or -1 on failure.
1337
 */
1338
static int matroska_deliver_packet(MatroskaDemuxContext *matroska,
1339
                                   AVPacket *pkt)
1340
{
1341
    if (matroska->num_packets > 0) {
1342
        memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
1343
        av_free(matroska->packets[0]);
1344
        if (matroska->num_packets > 1) {
1345
            memmove(&matroska->packets[0], &matroska->packets[1],
1346
                    (matroska->num_packets - 1) * sizeof(AVPacket *));
1347
            matroska->packets =
1348
                av_realloc(matroska->packets, (matroska->num_packets - 1) *
1349
                           sizeof(AVPacket *));
1350
        } else {
1351
            av_freep(&matroska->packets);
1352
        }
1353
        matroska->num_packets--;
1354
        return 0;
1355
    }
1356

  
1357
    return -1;
1358
}
1359

  
1360
/*
1361
 * Free all packets in our internal queue.
1362
 */
1363
static void matroska_clear_queue(MatroskaDemuxContext *matroska)
1364
{
1365
    if (matroska->packets) {
1366
        int n;
1367
        for (n = 0; n < matroska->num_packets; n++) {
1368
            av_free_packet(matroska->packets[n]);
1369
            av_free(matroska->packets[n]);
1370
        }
1371
        av_free(matroska->packets);
1372
        matroska->packets = NULL;
1373
        matroska->num_packets = 0;
1374
    }
1375
}
1376

  
1381 1377
static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data,
1382 1378
                                int size, int64_t pos, uint64_t cluster_time,
1383 1379
                                uint64_t duration, int is_keyframe)

Also available in: Unified diff