Revision 935ec5a1 libavformat/matroskadec.c

View differences:

libavformat/matroskadec.c
973 973
}
974 974

  
975 975
static int
976
matroska_decode_buffer(uint8_t** buf, int* buf_size, MatroskaTrack *track)
977
{
978
    uint8_t* data = *buf;
979
    int isize = *buf_size;
980
    uint8_t* pkt_data = NULL;
981
    int pkt_size = isize;
982
    int result = 0;
983
    int olen;
984

  
985
    switch (track->encoding_algo) {
986
    case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
987
        return track->encoding_settings_len;
988
    case MATROSKA_TRACK_ENCODING_COMP_LZO:
989
        do {
990
            olen = pkt_size *= 3;
991
            pkt_data = av_realloc(pkt_data,
992
                                  pkt_size+LZO_OUTPUT_PADDING);
993
            result = lzo1x_decode(pkt_data, &olen, data, &isize);
994
        } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
995
        if (result)
996
            goto failed;
997
        pkt_size -= olen;
998
        break;
999
#ifdef CONFIG_ZLIB
1000
    case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
1001
        z_stream zstream = {0};
1002
        if (inflateInit(&zstream) != Z_OK)
1003
            return -1;
1004
        zstream.next_in = data;
1005
        zstream.avail_in = isize;
1006
        do {
1007
            pkt_size *= 3;
1008
            pkt_data = av_realloc(pkt_data, pkt_size);
1009
            zstream.avail_out = pkt_size - zstream.total_out;
1010
            zstream.next_out = pkt_data + zstream.total_out;
1011
            result = inflate(&zstream, Z_NO_FLUSH);
1012
        } while (result==Z_OK && pkt_size<10000000);
1013
        pkt_size = zstream.total_out;
1014
        inflateEnd(&zstream);
1015
        if (result != Z_STREAM_END)
1016
            goto failed;
1017
        break;
1018
    }
1019
#endif
1020
#ifdef CONFIG_BZLIB
1021
    case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
1022
        bz_stream bzstream = {0};
1023
        if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1024
            return -1;
1025
        bzstream.next_in = data;
1026
        bzstream.avail_in = isize;
1027
        do {
1028
            pkt_size *= 3;
1029
            pkt_data = av_realloc(pkt_data, pkt_size);
1030
            bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1031
            bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1032
            result = BZ2_bzDecompress(&bzstream);
1033
        } while (result==BZ_OK && pkt_size<10000000);
1034
        pkt_size = bzstream.total_out_lo32;
1035
        BZ2_bzDecompressEnd(&bzstream);
1036
        if (result != BZ_STREAM_END)
1037
            goto failed;
1038
        break;
1039
    }
1040
#endif
1041
    }
1042

  
1043
    *buf = pkt_data;
1044
    *buf_size = pkt_size;
1045
    return 0;
1046
 failed:
1047
    av_free(pkt_data);
1048
    return -1;
1049
}
1050

  
1051
static int
976 1052
matroska_add_stream (MatroskaDemuxContext *matroska)
977 1053
{
978 1054
    int res = 0;
......
2801 2877
                    matroska_queue_packet(matroska, pkt);
2802 2878
                }
2803 2879
            } else {
2804
                int result, offset = 0, ilen, olen, pkt_size = lace_size[n];
2880
                int offset = 0, pkt_size = lace_size[n];
2805 2881
                uint8_t *pkt_data = data;
2806 2882

  
2807 2883
                if (matroska->tracks[track]->encoding_scope & 1) {
2808
                    switch (matroska->tracks[track]->encoding_algo) {
2809
                    case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
2810
                        offset = matroska->tracks[track]->encoding_settings_len;
2811
                        break;
2812
                    case MATROSKA_TRACK_ENCODING_COMP_LZO:
2813
                        pkt_data = NULL;
2814
                        do {
2815
                            ilen = lace_size[n];
2816
                            olen = pkt_size *= 3;
2817
                            pkt_data = av_realloc(pkt_data,
2818
                                                  pkt_size+LZO_OUTPUT_PADDING);
2819
                            result = lzo1x_decode(pkt_data, &olen, data, &ilen);
2820
                        } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
2821
                        if (result) {
2822
                            av_free(pkt_data);
2823
                            continue;
2824
                        }
2825
                        pkt_size -= olen;
2826
                        break;
2827
#ifdef CONFIG_ZLIB
2828
                    case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
2829
                        z_stream zstream = {0};
2830
                        pkt_data = NULL;
2831
                        if (inflateInit(&zstream) != Z_OK)
2832
                            continue;
2833
                        zstream.next_in = data;
2834
                        zstream.avail_in = lace_size[n];
2835
                        do {
2836
                            pkt_size *= 3;
2837
                            pkt_data = av_realloc(pkt_data, pkt_size);
2838
                            zstream.avail_out = pkt_size - zstream.total_out;
2839
                            zstream.next_out = pkt_data + zstream.total_out;
2840
                            result = inflate(&zstream, Z_NO_FLUSH);
2841
                        } while (result==Z_OK && pkt_size<10000000);
2842
                        pkt_size = zstream.total_out;
2843
                        inflateEnd(&zstream);
2844
                        if (result != Z_STREAM_END) {
2845
                            av_free(pkt_data);
2846
                            continue;
2847
                        }
2848
                        break;
2849
                    }
2850
#endif
2851
#ifdef CONFIG_BZLIB
2852
                    case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
2853
                        bz_stream bzstream = {0};
2854
                        pkt_data = NULL;
2855
                        if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
2856
                            continue;
2857
                        bzstream.next_in = data;
2858
                        bzstream.avail_in = lace_size[n];
2859
                        do {
2860
                            pkt_size *= 3;
2861
                            pkt_data = av_realloc(pkt_data, pkt_size);
2862
                            bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
2863
                            bzstream.next_out = pkt_data + bzstream.total_out_lo32;
2864
                            result = BZ2_bzDecompress(&bzstream);
2865
                        } while (result==BZ_OK && pkt_size<10000000);
2866
                        pkt_size = bzstream.total_out_lo32;
2867
                        BZ2_bzDecompressEnd(&bzstream);
2868
                        if (result != BZ_STREAM_END) {
2869
                            av_free(pkt_data);
2870
                            continue;
2871
                        }
2872
                        break;
2873
                    }
2874
#endif
2875
                    }
2884
                    offset = matroska_decode_buffer(&pkt_data, &pkt_size,
2885
                                                    matroska->tracks[track]);
2886
                    if (offset < 0)
2887
                        continue;
2876 2888
                }
2877 2889

  
2878 2890
                pkt = av_mallocz(sizeof(AVPacket));

Also available in: Unified diff