Statistics
| Branch: | Revision:

ffmpeg / libavformat / asfdec.c @ e356fc57

History | View | Annotate | Download (46.3 KB)

1 de6d9b64 Fabrice Bellard
/*
2 7fbde343 Aurelien Jacobs
 * ASF compatible demuxer
3 406792e7 Diego Biurrun
 * Copyright (c) 2000, 2001 Fabrice Bellard
4 de6d9b64 Fabrice Bellard
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 19720f15 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21 245976da Diego Biurrun
22 c9d7cd6d Baptiste Coudurier
//#define DEBUG
23
24 245976da Diego Biurrun
#include "libavutil/common.h"
25 5f1ac36d Cyril Comparon
#include "libavutil/avstring.h"
26 245976da Diego Biurrun
#include "libavcodec/mpegaudio.h"
27 de6d9b64 Fabrice Bellard
#include "avformat.h"
28 9d9f4119 Måns Rullgård
#include "riff.h"
29 542993b0 Konstantin Andreyev
#include "asf.h"
30 72be7db4 Reimar Döffinger
#include "asfcrypt.h"
31 5f1ac36d Cyril Comparon
#include "avlanguage.h"
32 de6d9b64 Fabrice Bellard
33 9686df2b Diego Biurrun
void ff_mms_set_stream_selection(URLContext *h, AVFormatContext *format);
34 49ce700a Björn Axelsson
35 4bc328a2 Anton Khirnov
typedef struct {
36
    int asfid2avid[128];                 ///< conversion table from asf ID 2 AVStream ID
37
    ASFStream streams[128];              ///< it's max number and it's not that big
38
    uint32_t stream_bitrates[128];       ///< max number of streams, bitrate for each (for streaming)
39 d42b0972 Anton Khirnov
    AVRational dar[128];
40 4bc328a2 Anton Khirnov
    char stream_languages[128][6];       ///< max number of streams, language for each (RFC1766, e.g. en-US)
41
    /* non streamed additonnal info */
42
    /* packet filling */
43
    int packet_size_left;
44
    /* only for reading */
45
    uint64_t data_offset;                ///< beginning of the first data packet
46
    uint64_t data_object_offset;         ///< data object offset (excl. GUID & size)
47
    uint64_t data_object_size;           ///< size of the data object
48
    int index_read;
49
50
    ASFMainHeader hdr;
51
52
    int packet_flags;
53
    int packet_property;
54
    int packet_timestamp;
55
    int packet_segsizetype;
56
    int packet_segments;
57
    int packet_seq;
58
    int packet_replic_size;
59
    int packet_key_frame;
60
    int packet_padsize;
61
    unsigned int packet_frag_offset;
62
    unsigned int packet_frag_size;
63
    int64_t packet_frag_timestamp;
64
    int packet_multi_size;
65
    int packet_obj_size;
66
    int packet_time_delta;
67
    int packet_time_start;
68
    int64_t packet_pos;
69
70
    int stream_index;
71
72
    ASFStream* asf_st;                   ///< currently decoded stream
73
} ASFContext;
74
75 580fb5e7 Michael Niedermayer
#undef NDEBUG
76
#include <assert.h>
77
78 30cdfb49 Måns Rullgård
#define ASF_MAX_STREAMS 127
79 615b92fd Konstantin Andreyev
#define FRAME_HEADER_SIZE 17
80 115329f1 Diego Biurrun
// Fix Me! FRAME_HEADER_SIZE may be different.
81 615b92fd Konstantin Andreyev
82 b8b00d94 Reimar Döffinger
static const ff_asf_guid index_guid = {
83 24c14d6d Michael Niedermayer
    0x90, 0x08, 0x00, 0x33, 0xb1, 0xe5, 0xcf, 0x11, 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb
84 de6d9b64 Fabrice Bellard
};
85
86 b8b00d94 Reimar Döffinger
static const ff_asf_guid stream_bitrate_guid = { /* (http://get.to/sdp) */
87 0e3b6a6f Ryan Martell
    0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
88
};
89 de6d9b64 Fabrice Bellard
/**********************************/
90
/* decoding */
91
92
#ifdef DEBUG
93 5082f759 François Revol
#define PRINT_IF_GUID(g,cmp) \
94 a750050f Peter Ross
if (!ff_guidcmp(g, &cmp)) \
95 dfd2a005 Luca Barbato
    av_dlog(NULL, "(GUID: %s) ", #cmp)
96 5082f759 François Revol
97 b8b00d94 Reimar Döffinger
static void print_guid(const ff_asf_guid *g)
98 de6d9b64 Fabrice Bellard
{
99
    int i;
100 17af0525 Aurelien Jacobs
    PRINT_IF_GUID(g, ff_asf_header);
101
    else PRINT_IF_GUID(g, ff_asf_file_header);
102
    else PRINT_IF_GUID(g, ff_asf_stream_header);
103
    else PRINT_IF_GUID(g, ff_asf_audio_stream);
104
    else PRINT_IF_GUID(g, ff_asf_audio_conceal_none);
105
    else PRINT_IF_GUID(g, ff_asf_video_stream);
106
    else PRINT_IF_GUID(g, ff_asf_video_conceal_none);
107
    else PRINT_IF_GUID(g, ff_asf_command_stream);
108
    else PRINT_IF_GUID(g, ff_asf_comment_header);
109
    else PRINT_IF_GUID(g, ff_asf_codec_comment_header);
110
    else PRINT_IF_GUID(g, ff_asf_codec_comment1_header);
111
    else PRINT_IF_GUID(g, ff_asf_data_header);
112 5082f759 François Revol
    else PRINT_IF_GUID(g, index_guid);
113 17af0525 Aurelien Jacobs
    else PRINT_IF_GUID(g, ff_asf_head1_guid);
114
    else PRINT_IF_GUID(g, ff_asf_head2_guid);
115
    else PRINT_IF_GUID(g, ff_asf_my_guid);
116
    else PRINT_IF_GUID(g, ff_asf_ext_stream_header);
117
    else PRINT_IF_GUID(g, ff_asf_extended_content_header);
118
    else PRINT_IF_GUID(g, ff_asf_ext_stream_embed_stream_header);
119
    else PRINT_IF_GUID(g, ff_asf_ext_stream_audio_stream);
120
    else PRINT_IF_GUID(g, ff_asf_metadata_header);
121 88b51ea9 Vladimir Pantelic
    else PRINT_IF_GUID(g, ff_asf_marker_header);
122 0e3b6a6f Ryan Martell
    else PRINT_IF_GUID(g, stream_bitrate_guid);
123 5f1ac36d Cyril Comparon
    else PRINT_IF_GUID(g, ff_asf_language_guid);
124 5082f759 François Revol
    else
125 dfd2a005 Luca Barbato
        av_dlog(NULL, "(GUID: unknown) ");
126 24c14d6d Michael Niedermayer
    for(i=0;i<16;i++)
127 dfd2a005 Luca Barbato
        av_dlog(NULL, " 0x%02x,", (*g)[i]);
128
    av_dlog(NULL, "}\n");
129 de6d9b64 Fabrice Bellard
}
130 6df84c3a Alex Beregszaszi
#undef PRINT_IF_GUID
131 f94981ea Måns Rullgård
#else
132
#define print_guid(g)
133 de6d9b64 Fabrice Bellard
#endif
134
135 ae628ec1 Anton Khirnov
void ff_get_guid(AVIOContext *s, ff_asf_guid *g)
136 de6d9b64 Fabrice Bellard
{
137 24c14d6d Michael Niedermayer
    assert(sizeof(*g) == 16);
138 b7effd4e Anton Khirnov
    avio_read(s, *g, sizeof(*g));
139 de6d9b64 Fabrice Bellard
}
140
141 c9a65ca8 Fabrice Bellard
static int asf_probe(AVProbeData *pd)
142
{
143
    /* check file header */
144 a750050f Peter Ross
    if (!ff_guidcmp(pd->buf, &ff_asf_header))
145 c9a65ca8 Fabrice Bellard
        return AVPROBE_SCORE_MAX;
146
    else
147
        return 0;
148
}
149
150 ae628ec1 Anton Khirnov
static int get_value(AVIOContext *pb, int type){
151 30e88789 Michael Niedermayer
    switch(type){
152 b7effd4e Anton Khirnov
        case 2: return avio_rl32(pb);
153
        case 3: return avio_rl32(pb);
154
        case 4: return avio_rl64(pb);
155
        case 5: return avio_rl16(pb);
156 30e88789 Michael Niedermayer
        default:return INT_MIN;
157
    }
158
}
159
160 20e2a7ae Aurelien Jacobs
static void get_tag(AVFormatContext *s, const char *key, int type, int len)
161
{
162 12ad6671 Michael Niedermayer
    char *value;
163 04295054 Anton Khirnov
    int64_t off = url_ftell(s->pb);
164 12ad6671 Michael Niedermayer
165 fce44489 Anton Khirnov
    if ((unsigned)len >= (UINT_MAX - 1)/2)
166 12ad6671 Michael Niedermayer
        return;
167
168 fce44489 Anton Khirnov
    value = av_malloc(2*len+1);
169 12ad6671 Michael Niedermayer
    if (!value)
170 04295054 Anton Khirnov
        goto finish;
171 12ad6671 Michael Niedermayer
172 6538c0f1 Benoit Fouet
    if (type == 0) {         // UTF16-LE
173 93b78d12 Anton Khirnov
        avio_get_str16le(s->pb, len, value, 2*len + 1);
174 6538c0f1 Benoit Fouet
    } else if (type > 1 && type <= 5) {  // boolean or DWORD or QWORD or WORD
175 20e2a7ae Aurelien Jacobs
        uint64_t num = get_value(s->pb, type);
176 12ad6671 Michael Niedermayer
        snprintf(value, len, "%"PRIu64, num);
177 20e2a7ae Aurelien Jacobs
    } else {
178 27e74927 Benoit Fouet
        av_log(s, AV_LOG_DEBUG, "Unsupported value type %d in tag %s.\n", type, key);
179 04295054 Anton Khirnov
        goto finish;
180 20e2a7ae Aurelien Jacobs
    }
181 5cbd3b5d Anton Khirnov
    if (*value)
182
        av_metadata_set2(&s->metadata, key, value, 0);
183 04295054 Anton Khirnov
finish:
184 fce44489 Anton Khirnov
    av_freep(&value);
185 6b4aa5da Anton Khirnov
    avio_seek(s->pb, off + len, SEEK_SET);
186 20e2a7ae Aurelien Jacobs
}
187
188 c1fea230 Anton Khirnov
static int asf_read_file_properties(AVFormatContext *s, int64_t size)
189
{
190
    ASFContext *asf = s->priv_data;
191 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
192 c1fea230 Anton Khirnov
193
    ff_get_guid(pb, &asf->hdr.guid);
194 b7effd4e Anton Khirnov
    asf->hdr.file_size          = avio_rl64(pb);
195
    asf->hdr.create_time        = avio_rl64(pb);
196
    avio_rl64(pb);                               /* number of packets */
197
    asf->hdr.play_time          = avio_rl64(pb);
198
    asf->hdr.send_time          = avio_rl64(pb);
199
    asf->hdr.preroll            = avio_rl32(pb);
200
    asf->hdr.ignore             = avio_rl32(pb);
201
    asf->hdr.flags              = avio_rl32(pb);
202
    asf->hdr.min_pktsize        = avio_rl32(pb);
203
    asf->hdr.max_pktsize        = avio_rl32(pb);
204
    asf->hdr.max_bitrate        = avio_rl32(pb);
205 c1fea230 Anton Khirnov
    s->packet_size = asf->hdr.max_pktsize;
206
207
    return 0;
208
}
209
210 8bf6db1b Anton Khirnov
static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
211
{
212
    ASFContext *asf = s->priv_data;
213 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
214 8bf6db1b Anton Khirnov
    AVStream *st;
215
    ASFStream *asf_st;
216
    ff_asf_guid g;
217
    enum AVMediaType type;
218
    int type_specific_size, sizeX;
219
    uint64_t total_size;
220
    unsigned int tag1;
221
    int64_t pos1, pos2, start_time;
222
    int test_for_ext_stream_audio, is_dvr_ms_audio=0;
223
224
    if (s->nb_streams == ASF_MAX_STREAMS) {
225
        av_log(s, AV_LOG_ERROR, "too many streams\n");
226
        return AVERROR(EINVAL);
227
    }
228
229
    pos1 = url_ftell(pb);
230
231
    st = av_new_stream(s, 0);
232
    if (!st)
233
        return AVERROR(ENOMEM);
234
    av_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
235
    asf_st = av_mallocz(sizeof(ASFStream));
236
    if (!asf_st)
237
        return AVERROR(ENOMEM);
238
    st->priv_data = asf_st;
239
    start_time = asf->hdr.preroll;
240
241
    asf_st->stream_language_index = 128; // invalid stream index means no language info
242
243
    if(!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
244
        st->duration = asf->hdr.play_time /
245
            (10000000 / 1000) - start_time;
246
    }
247
    ff_get_guid(pb, &g);
248
249
    test_for_ext_stream_audio = 0;
250
    if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
251
        type = AVMEDIA_TYPE_AUDIO;
252
    } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
253
        type = AVMEDIA_TYPE_VIDEO;
254
    } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
255
        type = AVMEDIA_TYPE_VIDEO;
256
        st->codec->codec_id = CODEC_ID_MJPEG;
257
    } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
258
        type = AVMEDIA_TYPE_DATA;
259
    } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_embed_stream_header)) {
260
        test_for_ext_stream_audio = 1;
261
        type = AVMEDIA_TYPE_UNKNOWN;
262
    } else {
263
        return -1;
264
    }
265
    ff_get_guid(pb, &g);
266 b7effd4e Anton Khirnov
    total_size = avio_rl64(pb);
267
    type_specific_size = avio_rl32(pb);
268
    avio_rl32(pb);
269
    st->id = avio_rl16(pb) & 0x7f; /* stream id */
270 8bf6db1b Anton Khirnov
    // mapping of asf ID to AV stream ID;
271
    asf->asfid2avid[st->id] = s->nb_streams - 1;
272
273 b7effd4e Anton Khirnov
    avio_rl32(pb);
274 8bf6db1b Anton Khirnov
275
    if (test_for_ext_stream_audio) {
276
        ff_get_guid(pb, &g);
277
        if (!ff_guidcmp(&g, &ff_asf_ext_stream_audio_stream)) {
278
            type = AVMEDIA_TYPE_AUDIO;
279
            is_dvr_ms_audio=1;
280
            ff_get_guid(pb, &g);
281 b7effd4e Anton Khirnov
            avio_rl32(pb);
282
            avio_rl32(pb);
283
            avio_rl32(pb);
284 8bf6db1b Anton Khirnov
            ff_get_guid(pb, &g);
285 b7effd4e Anton Khirnov
            avio_rl32(pb);
286 8bf6db1b Anton Khirnov
        }
287
    }
288
289
    st->codec->codec_type = type;
290
    if (type == AVMEDIA_TYPE_AUDIO) {
291
        ff_get_wav_header(pb, st->codec, type_specific_size);
292
        if (is_dvr_ms_audio) {
293
            // codec_id and codec_tag are unreliable in dvr_ms
294
            // files. Set them later by probing stream.
295
            st->codec->codec_id = CODEC_ID_PROBE;
296
            st->codec->codec_tag = 0;
297
        }
298
        if (st->codec->codec_id == CODEC_ID_AAC) {
299
            st->need_parsing = AVSTREAM_PARSE_NONE;
300
        } else {
301
            st->need_parsing = AVSTREAM_PARSE_FULL;
302
        }
303
        /* We have to init the frame size at some point .... */
304
        pos2 = url_ftell(pb);
305
        if (size >= (pos2 + 8 - pos1 + 24)) {
306 b7effd4e Anton Khirnov
            asf_st->ds_span = avio_r8(pb);
307
            asf_st->ds_packet_size = avio_rl16(pb);
308
            asf_st->ds_chunk_size = avio_rl16(pb);
309
            avio_rl16(pb); //ds_data_size
310
            avio_r8(pb);   //ds_silence_data
311 8bf6db1b Anton Khirnov
        }
312
        //printf("Descrambling: ps:%d cs:%d ds:%d s:%d  sd:%d\n",
313
        //       asf_st->ds_packet_size, asf_st->ds_chunk_size,
314
        //       asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data);
315
        if (asf_st->ds_span > 1) {
316
            if (!asf_st->ds_chunk_size
317
                    || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1)
318
                    || asf_st->ds_packet_size % asf_st->ds_chunk_size)
319
                asf_st->ds_span = 0; // disable descrambling
320
        }
321
        switch (st->codec->codec_id) {
322
            case CODEC_ID_MP3:
323
                st->codec->frame_size = MPA_FRAME_SIZE;
324
                break;
325
            case CODEC_ID_PCM_S16LE:
326
            case CODEC_ID_PCM_S16BE:
327
            case CODEC_ID_PCM_U16LE:
328
            case CODEC_ID_PCM_U16BE:
329
            case CODEC_ID_PCM_S8:
330
            case CODEC_ID_PCM_U8:
331
            case CODEC_ID_PCM_ALAW:
332
            case CODEC_ID_PCM_MULAW:
333
                st->codec->frame_size = 1;
334
                break;
335
            default:
336
                /* This is probably wrong, but it prevents a crash later */
337
                st->codec->frame_size = 1;
338
                break;
339
        }
340
    } else if (type == AVMEDIA_TYPE_VIDEO &&
341
            size - (url_ftell(pb) - pos1 + 24) >= 51) {
342 b7effd4e Anton Khirnov
        avio_rl32(pb);
343
        avio_rl32(pb);
344
        avio_r8(pb);
345
        avio_rl16(pb);        /* size */
346
        sizeX= avio_rl32(pb); /* size */
347
        st->codec->width = avio_rl32(pb);
348
        st->codec->height = avio_rl32(pb);
349 8bf6db1b Anton Khirnov
        /* not available for asf */
350 b7effd4e Anton Khirnov
        avio_rl16(pb); /* panes */
351
        st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
352
        tag1 = avio_rl32(pb);
353 e356fc57 Anton Khirnov
        avio_seek(pb, 20, SEEK_CUR);
354 8bf6db1b Anton Khirnov
        //                av_log(s, AV_LOG_DEBUG, "size:%d tsize:%d sizeX:%d\n", size, total_size, sizeX);
355
        if (sizeX > 40) {
356
            st->codec->extradata_size = sizeX - 40;
357
            st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
358 b7effd4e Anton Khirnov
            avio_read(pb, st->codec->extradata, st->codec->extradata_size);
359 8bf6db1b Anton Khirnov
        }
360
361
        /* Extract palette from extradata if bpp <= 8 */
362
        /* This code assumes that extradata contains only palette */
363
        /* This is true for all paletted codecs implemented in ffmpeg */
364
        if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
365 aabc2682 Mans Rullgard
            int av_unused i;
366 8bf6db1b Anton Khirnov
            st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
367
#if HAVE_BIGENDIAN
368
            for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
369
                st->codec->palctrl->palette[i] = av_bswap32(((uint32_t*)st->codec->extradata)[i]);
370
#else
371
            memcpy(st->codec->palctrl->palette, st->codec->extradata,
372
                    FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
373
#endif
374
            st->codec->palctrl->palette_changed = 1;
375
        }
376
377
        st->codec->codec_tag = tag1;
378
        st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
379
        if(tag1 == MKTAG('D', 'V', 'R', ' ')){
380
            st->need_parsing = AVSTREAM_PARSE_FULL;
381
            // issue658 containse wrong w/h and MS even puts a fake seq header with wrong w/h in extradata while a correct one is in te stream. maximum lameness
382
            st->codec->width  =
383
                st->codec->height = 0;
384
            av_freep(&st->codec->extradata);
385
            st->codec->extradata_size=0;
386
        }
387
        if(st->codec->codec_id == CODEC_ID_H264)
388
            st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
389
    }
390
    pos2 = url_ftell(pb);
391 e356fc57 Anton Khirnov
    avio_seek(pb, size - (pos2 - pos1 + 24), SEEK_CUR);
392 8bf6db1b Anton Khirnov
393
    return 0;
394
}
395
396 c1fea230 Anton Khirnov
static int asf_read_ext_stream_properties(AVFormatContext *s, int64_t size)
397
{
398
    ASFContext *asf = s->priv_data;
399 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
400 c1fea230 Anton Khirnov
    ff_asf_guid g;
401
    int ext_len, payload_ext_ct, stream_ct, i;
402
    uint32_t ext_d, leak_rate, stream_num;
403
    unsigned int stream_languageid_index;
404
405 b7effd4e Anton Khirnov
    avio_rl64(pb); // starttime
406
    avio_rl64(pb); // endtime
407
    leak_rate = avio_rl32(pb); // leak-datarate
408
    avio_rl32(pb); // bucket-datasize
409
    avio_rl32(pb); // init-bucket-fullness
410
    avio_rl32(pb); // alt-leak-datarate
411
    avio_rl32(pb); // alt-bucket-datasize
412
    avio_rl32(pb); // alt-init-bucket-fullness
413
    avio_rl32(pb); // max-object-size
414
    avio_rl32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
415
    stream_num = avio_rl16(pb); // stream-num
416
417
    stream_languageid_index = avio_rl16(pb); // stream-language-id-index
418 c1fea230 Anton Khirnov
    if (stream_num < 128)
419
        asf->streams[stream_num].stream_language_index = stream_languageid_index;
420
421 b7effd4e Anton Khirnov
    avio_rl64(pb); // avg frametime in 100ns units
422
    stream_ct = avio_rl16(pb); //stream-name-count
423
    payload_ext_ct = avio_rl16(pb); //payload-extension-system-count
424 c1fea230 Anton Khirnov
425
    if (stream_num < 128)
426
        asf->stream_bitrates[stream_num] = leak_rate;
427
428
    for (i=0; i<stream_ct; i++){
429 b7effd4e Anton Khirnov
        avio_rl16(pb);
430
        ext_len = avio_rl16(pb);
431 6b4aa5da Anton Khirnov
        avio_seek(pb, ext_len, SEEK_CUR);
432 c1fea230 Anton Khirnov
    }
433
434
    for (i=0; i<payload_ext_ct; i++){
435
        ff_get_guid(pb, &g);
436 b7effd4e Anton Khirnov
        ext_d=avio_rl16(pb);
437
        ext_len=avio_rl32(pb);
438 6b4aa5da Anton Khirnov
        avio_seek(pb, ext_len, SEEK_CUR);
439 c1fea230 Anton Khirnov
    }
440
441
    return 0;
442
}
443
444
static int asf_read_content_desc(AVFormatContext *s, int64_t size)
445
{
446 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
447 c1fea230 Anton Khirnov
    int len1, len2, len3, len4, len5;
448
449 b7effd4e Anton Khirnov
    len1 = avio_rl16(pb);
450
    len2 = avio_rl16(pb);
451
    len3 = avio_rl16(pb);
452
    len4 = avio_rl16(pb);
453
    len5 = avio_rl16(pb);
454 c1fea230 Anton Khirnov
    get_tag(s, "title"    , 0, len1);
455
    get_tag(s, "author"   , 0, len2);
456
    get_tag(s, "copyright", 0, len3);
457
    get_tag(s, "comment"  , 0, len4);
458 e356fc57 Anton Khirnov
    avio_seek(pb, len5, SEEK_CUR);
459 c1fea230 Anton Khirnov
460
    return 0;
461
}
462
463
static int asf_read_ext_content_desc(AVFormatContext *s, int64_t size)
464
{
465 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
466 c1fea230 Anton Khirnov
    ASFContext *asf = s->priv_data;
467
    int desc_count, i, ret;
468
469 b7effd4e Anton Khirnov
    desc_count = avio_rl16(pb);
470 c1fea230 Anton Khirnov
    for(i=0;i<desc_count;i++) {
471
        int name_len,value_type,value_len;
472
        char name[1024];
473
474 b7effd4e Anton Khirnov
        name_len = avio_rl16(pb);
475 c1fea230 Anton Khirnov
        if (name_len%2)     // must be even, broken lavf versions wrote len-1
476
            name_len += 1;
477
        if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
478 e356fc57 Anton Khirnov
            avio_seek(pb, name_len - ret, SEEK_CUR);
479 b7effd4e Anton Khirnov
        value_type = avio_rl16(pb);
480
        value_len  = avio_rl16(pb);
481 c1fea230 Anton Khirnov
        if (!value_type && value_len%2)
482
            value_len += 1;
483
        /**
484
         * My sample has that stream set to 0 maybe that mean the container.
485
         * Asf stream count start at 1. I am using 0 to the container value since it's unused
486
         */
487
        if (!strcmp(name, "AspectRatioX")){
488
            asf->dar[0].num= get_value(s->pb, value_type);
489
        } else if(!strcmp(name, "AspectRatioY")){
490
            asf->dar[0].den= get_value(s->pb, value_type);
491
        } else
492
            get_tag(s, name, value_type, value_len);
493
    }
494
495
    return 0;
496
}
497
498
static int asf_read_language_list(AVFormatContext *s, int64_t size)
499
{
500 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
501 c1fea230 Anton Khirnov
    ASFContext *asf = s->priv_data;
502
    int j, ret;
503 b7effd4e Anton Khirnov
    int stream_count = avio_rl16(pb);
504 c1fea230 Anton Khirnov
    for(j = 0; j < stream_count; j++) {
505
        char lang[6];
506 b7effd4e Anton Khirnov
        unsigned int lang_len = avio_r8(pb);
507 c1fea230 Anton Khirnov
        if ((ret = avio_get_str16le(pb, lang_len, lang, sizeof(lang))) < lang_len)
508 e356fc57 Anton Khirnov
            avio_seek(pb, lang_len - ret, SEEK_CUR);
509 c1fea230 Anton Khirnov
        if (j < 128)
510
            av_strlcpy(asf->stream_languages[j], lang, sizeof(*asf->stream_languages));
511
    }
512
513
    return 0;
514
}
515
516
static int asf_read_metadata(AVFormatContext *s, int64_t size)
517
{
518 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
519 c1fea230 Anton Khirnov
    ASFContext *asf = s->priv_data;
520
    int n, stream_num, name_len, value_len, value_type, value_num;
521
    int ret, i;
522 b7effd4e Anton Khirnov
    n = avio_rl16(pb);
523 c1fea230 Anton Khirnov
524
    for(i=0;i<n;i++) {
525
        char name[1024];
526
527 b7effd4e Anton Khirnov
        avio_rl16(pb); //lang_list_index
528
        stream_num= avio_rl16(pb);
529
        name_len=   avio_rl16(pb);
530
        value_type= avio_rl16(pb);
531
        value_len=  avio_rl32(pb);
532 c1fea230 Anton Khirnov
533
        if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
534 e356fc57 Anton Khirnov
            avio_seek(pb, name_len - ret, SEEK_CUR);
535 c1fea230 Anton Khirnov
        //av_log(s, AV_LOG_ERROR, "%d %d %d %d %d <%s>\n", i, stream_num, name_len, value_type, value_len, name);
536 b7effd4e Anton Khirnov
        value_num= avio_rl16(pb);//we should use get_value() here but it does not work 2 is le16 here but le32 elsewhere
537 e356fc57 Anton Khirnov
        avio_seek(pb, value_len - 2, SEEK_CUR);
538 c1fea230 Anton Khirnov
539
        if(stream_num<128){
540
            if     (!strcmp(name, "AspectRatioX")) asf->dar[stream_num].num= value_num;
541
            else if(!strcmp(name, "AspectRatioY")) asf->dar[stream_num].den= value_num;
542
        }
543
    }
544
545
    return 0;
546
}
547
548
static int asf_read_marker(AVFormatContext *s, int64_t size)
549
{
550 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
551 c1fea230 Anton Khirnov
    int i, count, name_len, ret;
552
    char name[1024];
553
554 b7effd4e Anton Khirnov
    avio_rl64(pb);            // reserved 16 bytes
555
    avio_rl64(pb);            // ...
556
    count = avio_rl32(pb);    // markers count
557
    avio_rl16(pb);            // reserved 2 bytes
558
    name_len = avio_rl16(pb); // name length
559 c1fea230 Anton Khirnov
    for(i=0;i<name_len;i++){
560 b7effd4e Anton Khirnov
        avio_r8(pb); // skip the name
561 c1fea230 Anton Khirnov
    }
562
563
    for(i=0;i<count;i++){
564
        int64_t pres_time;
565
        int name_len;
566
567 b7effd4e Anton Khirnov
        avio_rl64(pb);             // offset, 8 bytes
568
        pres_time = avio_rl64(pb); // presentation time
569
        avio_rl16(pb);             // entry length
570
        avio_rl32(pb);             // send time
571
        avio_rl32(pb);             // flags
572
        name_len = avio_rl32(pb);  // name length
573 c1fea230 Anton Khirnov
        if ((ret = avio_get_str16le(pb, name_len * 2, name, sizeof(name))) < name_len)
574 e356fc57 Anton Khirnov
            avio_seek(pb, name_len - ret, SEEK_CUR);
575 c1fea230 Anton Khirnov
        ff_new_chapter(s, i, (AVRational){1, 10000000}, pres_time, AV_NOPTS_VALUE, name );
576
    }
577
578
    return 0;
579
}
580
581 de6d9b64 Fabrice Bellard
static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
582
{
583 c9a65ca8 Fabrice Bellard
    ASFContext *asf = s->priv_data;
584 b8b00d94 Reimar Döffinger
    ff_asf_guid g;
585 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
586 0b1d291a Anton Khirnov
    int i;
587 0c1a9eda Zdenek Kabelac
    int64_t gsize;
588 de6d9b64 Fabrice Bellard
589 a750050f Peter Ross
    ff_get_guid(pb, &g);
590
    if (ff_guidcmp(&g, &ff_asf_header))
591 ccafd472 Baptiste Coudurier
        return -1;
592 b7effd4e Anton Khirnov
    avio_rl64(pb);
593
    avio_rl32(pb);
594
    avio_r8(pb);
595
    avio_r8(pb);
596 2141dc37 Zdenek Kabelac
    memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
597 de6d9b64 Fabrice Bellard
    for(;;) {
598 cb0061d4 Michael Niedermayer
        uint64_t gpos= url_ftell(pb);
599 a750050f Peter Ross
        ff_get_guid(pb, &g);
600 b7effd4e Anton Khirnov
        gsize = avio_rl64(pb);
601 dfd2a005 Luca Barbato
        av_dlog(s, "%08"PRIx64": ", gpos);
602 de6d9b64 Fabrice Bellard
        print_guid(&g);
603 dfd2a005 Luca Barbato
        av_dlog(s, "  size=0x%"PRIx64"\n", gsize);
604 a750050f Peter Ross
        if (!ff_guidcmp(&g, &ff_asf_data_header)) {
605 09d9672a Michael Niedermayer
            asf->data_object_offset = url_ftell(pb);
606
            // if not streaming, gsize is not unlimited (how?), and there is enough space in the file..
607
            if (!(asf->hdr.flags & 0x01) && gsize >= 100) {
608
                asf->data_object_size = gsize - 24;
609
            } else {
610
                asf->data_object_size = (uint64_t)-1;
611
            }
612
            break;
613
        }
614 de6d9b64 Fabrice Bellard
        if (gsize < 24)
615 ccafd472 Baptiste Coudurier
            return -1;
616 a750050f Peter Ross
        if (!ff_guidcmp(&g, &ff_asf_file_header)) {
617 c1fea230 Anton Khirnov
            asf_read_file_properties(s, gsize);
618 a750050f Peter Ross
        } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
619 8bf6db1b Anton Khirnov
            asf_read_stream_properties(s, gsize);
620 a750050f Peter Ross
        } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
621 c1fea230 Anton Khirnov
            asf_read_content_desc(s, gsize);
622 a750050f Peter Ross
        } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
623 c1fea230 Anton Khirnov
            asf_read_language_list(s, gsize);
624 a750050f Peter Ross
        } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
625 c1fea230 Anton Khirnov
            asf_read_ext_content_desc(s, gsize);
626 a750050f Peter Ross
        } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
627 c1fea230 Anton Khirnov
            asf_read_metadata(s, gsize);
628 a750050f Peter Ross
        } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
629 c1fea230 Anton Khirnov
            asf_read_ext_stream_properties(s, gsize);
630 88141c91 Michael Niedermayer
631
            // there could be a optional stream properties object to follow
632
            // if so the next iteration will pick it up
633 1d4c1c3b Michael Niedermayer
            continue;
634 a750050f Peter Ross
        } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
635 de6d9b64 Fabrice Bellard
            int v1, v2;
636 a750050f Peter Ross
            ff_get_guid(pb, &g);
637 b7effd4e Anton Khirnov
            v1 = avio_rl32(pb);
638
            v2 = avio_rl16(pb);
639 1d4c1c3b Michael Niedermayer
            continue;
640 a750050f Peter Ross
        } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
641 c1fea230 Anton Khirnov
            asf_read_marker(s, gsize);
642 de6d9b64 Fabrice Bellard
        } else if (url_feof(pb)) {
643 ccafd472 Baptiste Coudurier
            return -1;
644 de6d9b64 Fabrice Bellard
        } else {
645 febd1c90 Daniel G. Taylor
            if (!s->keylen) {
646 a750050f Peter Ross
                if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
647 febd1c90 Daniel G. Taylor
                    av_log(s, AV_LOG_WARNING, "DRM protected stream detected, decoding will likely fail!\n");
648 a750050f Peter Ross
                } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
649 febd1c90 Daniel G. Taylor
                    av_log(s, AV_LOG_WARNING, "Ext DRM protected stream detected, decoding will likely fail!\n");
650 a750050f Peter Ross
                } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
651 febd1c90 Daniel G. Taylor
                    av_log(s, AV_LOG_WARNING, "Digital signature detected, decoding will likely fail!\n");
652
                }
653
            }
654 de6d9b64 Fabrice Bellard
        }
655 cb0061d4 Michael Niedermayer
        if(url_ftell(pb) != gpos + gsize)
656
            av_log(s, AV_LOG_DEBUG, "gpos mismatch our pos=%"PRIu64", end=%"PRIu64"\n", url_ftell(pb)-gpos, gsize);
657 6b4aa5da Anton Khirnov
        avio_seek(pb, gpos + gsize, SEEK_SET);
658 de6d9b64 Fabrice Bellard
    }
659 a750050f Peter Ross
    ff_get_guid(pb, &g);
660 b7effd4e Anton Khirnov
    avio_rl64(pb);
661
    avio_r8(pb);
662
    avio_r8(pb);
663 2141dc37 Zdenek Kabelac
    if (url_feof(pb))
664 ccafd472 Baptiste Coudurier
        return -1;
665 2141dc37 Zdenek Kabelac
    asf->data_offset = url_ftell(pb);
666 de6d9b64 Fabrice Bellard
    asf->packet_size_left = 0;
667
668 30e88789 Michael Niedermayer
669
    for(i=0; i<128; i++){
670
        int stream_num= asf->asfid2avid[i];
671 d8470737 Zuxy Meng
        if(stream_num>=0){
672 59729451 Aurelien Jacobs
            AVStream *st = s->streams[stream_num];
673
            if (!st->codec->bit_rate)
674 7c725380 Anton Khirnov
                st->codec->bit_rate = asf->stream_bitrates[i];
675 d42b0972 Anton Khirnov
            if (asf->dar[i].num > 0 && asf->dar[i].den > 0){
676 59729451 Aurelien Jacobs
                av_reduce(&st->sample_aspect_ratio.num,
677
                          &st->sample_aspect_ratio.den,
678 d42b0972 Anton Khirnov
                          asf->dar[i].num, asf->dar[i].den, INT_MAX);
679
            } else if ((asf->dar[0].num > 0) && (asf->dar[0].den > 0) && (st->codec->codec_type==AVMEDIA_TYPE_VIDEO)) // Use ASF container value if the stream doesn't AR set.
680 6ac6e3d1 Richard Buteau
                av_reduce(&st->sample_aspect_ratio.num,
681
                          &st->sample_aspect_ratio.den,
682 d42b0972 Anton Khirnov
                          asf->dar[0].num, asf->dar[0].den, INT_MAX);
683 6ac6e3d1 Richard Buteau
684
//av_log(s, AV_LOG_INFO, "i=%d, st->codec->codec_type:%d, dar %d:%d sar=%d:%d\n", i, st->codec->codec_type, dar[i].num, dar[i].den, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
685 5f1ac36d Cyril Comparon
686
            // copy and convert language codes to the frontend
687
            if (asf->streams[i].stream_language_index < 128) {
688
                const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
689
                if (rfc1766 && strlen(rfc1766) > 1) {
690
                    const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
691
                    const char *iso6392 = av_convert_lang_to(primary_tag, AV_LANG_ISO639_2_BIBL);
692
                    if (iso6392)
693 2ef6c124 Stefano Sabatini
                        av_metadata_set2(&st->metadata, "language", iso6392, 0);
694 5f1ac36d Cyril Comparon
                }
695
            }
696 30e88789 Michael Niedermayer
        }
697
    }
698
699 ad7768f4 Anton Khirnov
    ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
700 03700d39 Anton Khirnov
701 de6d9b64 Fabrice Bellard
    return 0;
702
}
703
704 2a10020b Zdenek Kabelac
#define DO_2BITS(bits, var, defval) \
705
    switch (bits & 3) \
706
    { \
707 b7effd4e Anton Khirnov
    case 3: var = avio_rl32(pb); rsize += 4; break; \
708
    case 2: var = avio_rl16(pb); rsize += 2; break; \
709
    case 1: var = avio_r8(pb);   rsize++; break; \
710 2a10020b Zdenek Kabelac
    default: var = defval; break; \
711
    }
712
713 277a28e8 Ronald S. Bultje
/**
714
 * Load a single ASF packet into the demuxer.
715
 * @param s demux context
716
 * @param pb context to read data from
717 32e543f8 Benoit Fouet
 * @return 0 on success, <0 on error
718 277a28e8 Ronald S. Bultje
 */
719 ae628ec1 Anton Khirnov
static int ff_asf_get_packet(AVFormatContext *s, AVIOContext *pb)
720 de6d9b64 Fabrice Bellard
{
721
    ASFContext *asf = s->priv_data;
722 2a10020b Zdenek Kabelac
    uint32_t packet_length, padsize;
723 0c0d17f3 Michael Niedermayer
    int rsize = 8;
724 0fd9e6f3 Michael Niedermayer
    int c, d, e, off;
725
726 68a10b40 Reimar Döffinger
    // if we do not know packet size, allow skipping up to 32 kB
727
    off= 32768;
728
    if (s->packet_size > 0)
729 b098b309 Reimar Döffinger
        off= (url_ftell(pb) - s->data_offset) % s->packet_size + 3;
730 0fd9e6f3 Michael Niedermayer
731
    c=d=e=-1;
732
    while(off-- > 0){
733
        c=d; d=e;
734 b7effd4e Anton Khirnov
        e= avio_r8(pb);
735 0fd9e6f3 Michael Niedermayer
        if(c == 0x82 && !d && !e)
736
            break;
737
    }
738 115329f1 Diego Biurrun
739 1359c2d4 Zdenek Kabelac
    if (c != 0x82) {
740 2078ca16 Ronald S. Bultje
        /**
741
         * This code allows handling of -EAGAIN at packet boundaries (i.e.
742
         * if the packet sync code above triggers -EAGAIN). This does not
743
         * imply complete -EAGAIN handling support at random positions in
744
         * the stream.
745
         */
746
        if (url_ferror(pb) == AVERROR(EAGAIN))
747
            return AVERROR(EAGAIN);
748 5acdd6e6 Zdenek Kabelac
        if (!url_feof(pb))
749 bb270c08 Diego Biurrun
            av_log(s, AV_LOG_ERROR, "ff asf bad header %x  at:%"PRId64"\n", c, url_ftell(pb));
750 2a10020b Zdenek Kabelac
    }
751 0c0d17f3 Michael Niedermayer
    if ((c & 0x8f) == 0x82) {
752 0fd9e6f3 Michael Niedermayer
        if (d || e) {
753 5acdd6e6 Zdenek Kabelac
            if (!url_feof(pb))
754 bb270c08 Diego Biurrun
                av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
755 f7835507 Michael Niedermayer
            return -1;
756 bb270c08 Diego Biurrun
        }
757 b7effd4e Anton Khirnov
        c= avio_r8(pb);
758
        d= avio_r8(pb);
759 0c0d17f3 Michael Niedermayer
        rsize+=3;
760
    }else{
761 6b4aa5da Anton Khirnov
        avio_seek(pb, -1, SEEK_CUR); //FIXME
762 de6d9b64 Fabrice Bellard
    }
763 2a10020b Zdenek Kabelac
764 0c0d17f3 Michael Niedermayer
    asf->packet_flags    = c;
765
    asf->packet_property = d;
766 2a10020b Zdenek Kabelac
767 91d19d47 Ronald S. Bultje
    DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
768 2a10020b Zdenek Kabelac
    DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
769
    DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
770
771 5b11e081 Michael Niedermayer
    //the following checks prevent overflows and infinite loops
772 96f0731a Reimar Döffinger
    if(!packet_length || packet_length >= (1U<<29)){
773 5b11e081 Michael Niedermayer
        av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, url_ftell(pb));
774 ceba96cb Michael Niedermayer
        return -1;
775 5b11e081 Michael Niedermayer
    }
776 92216453 Michael Niedermayer
    if(padsize >= packet_length){
777 5b11e081 Michael Niedermayer
        av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, url_ftell(pb));
778 ceba96cb Michael Niedermayer
        return -1;
779 5b11e081 Michael Niedermayer
    }
780
781 b7effd4e Anton Khirnov
    asf->packet_timestamp = avio_rl32(pb);
782
    avio_rl16(pb); /* duration */
783 2141dc37 Zdenek Kabelac
    // rsize has at least 11 bytes which have to be present
784 2a10020b Zdenek Kabelac
785
    if (asf->packet_flags & 0x01) {
786 b7effd4e Anton Khirnov
        asf->packet_segsizetype = avio_r8(pb); rsize++;
787 2a10020b Zdenek Kabelac
        asf->packet_segments = asf->packet_segsizetype & 0x3f;
788
    } else {
789 bb270c08 Diego Biurrun
        asf->packet_segments = 1;
790 2a10020b Zdenek Kabelac
        asf->packet_segsizetype = 0x80;
791
    }
792
    asf->packet_size_left = packet_length - padsize - rsize;
793 acbe6cfa Zdenek Kabelac
    if (packet_length < asf->hdr.min_pktsize)
794
        padsize += asf->hdr.min_pktsize - packet_length;
795 2a10020b Zdenek Kabelac
    asf->packet_padsize = padsize;
796 dfd2a005 Luca Barbato
    av_dlog(s, "packet: size=%d padsize=%d  left=%d\n", s->packet_size, asf->packet_padsize, asf->packet_size_left);
797 de6d9b64 Fabrice Bellard
    return 0;
798
}
799
800 ae60a857 Michael Niedermayer
/**
801
 *
802
 * @return <0 if error
803
 */
804 ae628ec1 Anton Khirnov
static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb){
805 ae60a857 Michael Niedermayer
    ASFContext *asf = s->priv_data;
806
    int rsize = 1;
807 b7effd4e Anton Khirnov
    int num = avio_r8(pb);
808 559a55ad Michael Niedermayer
    int64_t ts0, ts1;
809 ae60a857 Michael Niedermayer
810
    asf->packet_segments--;
811
    asf->packet_key_frame = num >> 7;
812
    asf->stream_index = asf->asfid2avid[num & 0x7f];
813
    // sequence should be ignored!
814
    DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
815
    DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
816
    DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
817
//printf("key:%d stream:%d seq:%d offset:%d replic_size:%d\n", asf->packet_key_frame, asf->stream_index, asf->packet_seq, //asf->packet_frag_offset, asf->packet_replic_size);
818
    if (asf->packet_replic_size >= 8) {
819 b7effd4e Anton Khirnov
        asf->packet_obj_size = avio_rl32(pb);
820 ae60a857 Michael Niedermayer
        if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
821
            av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
822
            return -1;
823
        }
824 b7effd4e Anton Khirnov
        asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
825 559a55ad Michael Niedermayer
        if(asf->packet_replic_size >= 8+38+4){
826
//            for(i=0; i<asf->packet_replic_size-8; i++)
827 b7effd4e Anton Khirnov
//                av_log(s, AV_LOG_DEBUG, "%02X ",avio_r8(pb));
828 559a55ad Michael Niedermayer
//            av_log(s, AV_LOG_DEBUG, "\n");
829 e356fc57 Anton Khirnov
            avio_seek(pb, 10, SEEK_CUR);
830 b7effd4e Anton Khirnov
            ts0= avio_rl64(pb);
831
            ts1= avio_rl64(pb);
832 e356fc57 Anton Khirnov
            avio_seek(pb, 12, SEEK_CUR);
833 b7effd4e Anton Khirnov
            avio_rl32(pb);
834 e356fc57 Anton Khirnov
            avio_seek(pb, asf->packet_replic_size - 8 - 38 - 4, SEEK_CUR);
835 559a55ad Michael Niedermayer
            if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
836
            else         asf->packet_frag_timestamp= AV_NOPTS_VALUE;
837
        }else
838 e356fc57 Anton Khirnov
            avio_seek(pb, asf->packet_replic_size - 8, SEEK_CUR);
839 ae60a857 Michael Niedermayer
        rsize += asf->packet_replic_size; // FIXME - check validity
840
    } else if (asf->packet_replic_size==1){
841 efc90ebf Reimar Döffinger
        // multipacket - frag_offset is beginning timestamp
842 ae60a857 Michael Niedermayer
        asf->packet_time_start = asf->packet_frag_offset;
843
        asf->packet_frag_offset = 0;
844
        asf->packet_frag_timestamp = asf->packet_timestamp;
845
846 b7effd4e Anton Khirnov
        asf->packet_time_delta = avio_r8(pb);
847 ae60a857 Michael Niedermayer
        rsize++;
848
    }else if(asf->packet_replic_size!=0){
849
        av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
850
        return -1;
851
    }
852
    if (asf->packet_flags & 0x01) {
853
        DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
854 13f7e4b9 Michael Niedermayer
        if(asf->packet_frag_size > asf->packet_size_left - rsize){
855
            av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid\n");
856
            return -1;
857
        }
858 ae60a857 Michael Niedermayer
        //printf("Fragsize %d\n", asf->packet_frag_size);
859
    } else {
860
        asf->packet_frag_size = asf->packet_size_left - rsize;
861
        //printf("Using rest  %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
862
    }
863
    if (asf->packet_replic_size == 1) {
864
        asf->packet_multi_size = asf->packet_frag_size;
865
        if (asf->packet_multi_size > asf->packet_size_left)
866
            return -1;
867
    }
868
    asf->packet_size_left -= rsize;
869
    //printf("___objsize____  %d   %d    rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
870
871
    return 0;
872
}
873
874 277a28e8 Ronald S. Bultje
/**
875
 * Parse data from individual ASF packets (which were previously loaded
876
 * with asf_get_packet()).
877
 * @param s demux context
878
 * @param pb context to read data from
879
 * @param pkt pointer to store packet data into
880 32e543f8 Benoit Fouet
 * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
881 277a28e8 Ronald S. Bultje
 *          packets need to be loaded (through asf_get_packet())
882
 */
883 ae628ec1 Anton Khirnov
static int ff_asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
884 de6d9b64 Fabrice Bellard
{
885
    ASFContext *asf = s->priv_data;
886 2a10020b Zdenek Kabelac
    ASFStream *asf_st = 0;
887
    for (;;) {
888 4172951b Reimar Döffinger
        int ret;
889 ceba96cb Michael Niedermayer
        if(url_feof(pb))
890 799a0722 Ronald S. Bultje
            return AVERROR_EOF;
891 bb270c08 Diego Biurrun
        if (asf->packet_size_left < FRAME_HEADER_SIZE
892
            || asf->packet_segments < 1) {
893
            //asf->packet_size_left <= asf->packet_padsize) {
894
            int ret = asf->packet_size_left + asf->packet_padsize;
895 949b1a13 Steve L'Homme
            //printf("PacketLeftSize:%d  Pad:%d Pos:%"PRId64"\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb));
896 5b11e081 Michael Niedermayer
            assert(ret>=0);
897 bb270c08 Diego Biurrun
            /* fail safe */
898 e356fc57 Anton Khirnov
            avio_seek(pb, ret, SEEK_CUR);
899 b04d5d31 Michael Niedermayer
900 022f0b7c Ronald S. Bultje
            asf->packet_pos= url_ftell(pb);
901 b6eaae39 Kohn Emil Dan
            if (asf->data_object_size != (uint64_t)-1 &&
902
                (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
903 1d0036b0 Ronald S. Bultje
                return AVERROR_EOF; /* Do not exceed the size of the data object */
904 f27544fd Ronald S. Bultje
            return 1;
905 bb270c08 Diego Biurrun
        }
906
        if (asf->packet_time_start == 0) {
907 b0d75d60 Ronald S. Bultje
            if(asf_read_frame_header(s, pb) < 0){
908 ae60a857 Michael Niedermayer
                asf->packet_segments= 0;
909 236116cb Michael Niedermayer
                continue;
910 1305a9d5 Michael Niedermayer
            }
911 bb270c08 Diego Biurrun
            if (asf->stream_index < 0
912 f3356e9c Michael Niedermayer
                || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
913
                || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
914
                ) {
915 1359c2d4 Zdenek Kabelac
                asf->packet_time_start = 0;
916 bb270c08 Diego Biurrun
                /* unhandled packet (should not happen) */
917 e356fc57 Anton Khirnov
                avio_seek(pb, asf->packet_frag_size, SEEK_CUR);
918 bb270c08 Diego Biurrun
                asf->packet_size_left -= asf->packet_frag_size;
919 b9866ebc Michael Niedermayer
                if(asf->stream_index < 0)
920 ae60a857 Michael Niedermayer
                    av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
921 1359c2d4 Zdenek Kabelac
                continue;
922 bb270c08 Diego Biurrun
            }
923
            asf->asf_st = s->streams[asf->stream_index]->priv_data;
924
        }
925
        asf_st = asf->asf_st;
926
927
        if (asf->packet_replic_size == 1) {
928 efc90ebf Reimar Döffinger
            // frag_offset is here used as the beginning timestamp
929 bb270c08 Diego Biurrun
            asf->packet_frag_timestamp = asf->packet_time_start;
930
            asf->packet_time_start += asf->packet_time_delta;
931 b7effd4e Anton Khirnov
            asf->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
932 bb270c08 Diego Biurrun
            asf->packet_size_left--;
933 2141dc37 Zdenek Kabelac
            asf->packet_multi_size--;
934 bb270c08 Diego Biurrun
            if (asf->packet_multi_size < asf->packet_obj_size)
935
            {
936
                asf->packet_time_start = 0;
937 e356fc57 Anton Khirnov
                avio_seek(pb, asf->packet_multi_size, SEEK_CUR);
938 bb270c08 Diego Biurrun
                asf->packet_size_left -= asf->packet_multi_size;
939 2141dc37 Zdenek Kabelac
                continue;
940 bb270c08 Diego Biurrun
            }
941
            asf->packet_multi_size -= asf->packet_obj_size;
942
            //printf("COMPRESS size  %d  %d  %d   ms:%d\n", asf->packet_obj_size, asf->packet_frag_timestamp, asf->packet_size_left, asf->packet_multi_size);
943
        }
944 60ff1c44 Michael Niedermayer
        if(   /*asf->packet_frag_size == asf->packet_obj_size*/
945
              asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size
946
           && asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size){
947
            av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
948
                asf_st->frag_offset, asf->packet_frag_size,
949
                asf->packet_obj_size, asf_st->pkt.size);
950
            asf->packet_obj_size= asf_st->pkt.size;
951
        }
952
953 f8f0a0bd Michael Niedermayer
        if (   asf_st->pkt.size != asf->packet_obj_size
954
            || asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) { //FIXME is this condition sufficient?
955 200026aa Michael Niedermayer
            if(asf_st->pkt.data){
956
                av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, new %d\n", asf_st->pkt.size, asf->packet_obj_size);
957
                asf_st->frag_offset = 0;
958
                av_free_packet(&asf_st->pkt);
959
            }
960 bb270c08 Diego Biurrun
            /* new packet */
961
            av_new_packet(&asf_st->pkt, asf->packet_obj_size);
962
            asf_st->seq = asf->packet_seq;
963 eb63b051 Baptiste Coudurier
            asf_st->pkt.dts = asf->packet_frag_timestamp;
964 bb270c08 Diego Biurrun
            asf_st->pkt.stream_index = asf->stream_index;
965 115329f1 Diego Biurrun
            asf_st->pkt.pos =
966
            asf_st->packet_pos= asf->packet_pos;
967
//printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
968 cc947f04 Jean-Daniel Dupas
//asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & AV_PKT_FLAG_KEY,
969 72415b2a Stefano Sabatini
//s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO, asf->packet_obj_size);
970
            if (s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
971 bb270c08 Diego Biurrun
                asf->packet_key_frame = 1;
972
            if (asf->packet_key_frame)
973 cc947f04 Jean-Daniel Dupas
                asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
974 bb270c08 Diego Biurrun
        }
975
976
        /* read data */
977
        //printf("READ PACKET s:%d  os:%d  o:%d,%d  l:%d   DATA:%p\n",
978 91d19d47 Ronald S. Bultje
        //       s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
979 bb270c08 Diego Biurrun
        //       asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
980
        asf->packet_size_left -= asf->packet_frag_size;
981
        if (asf->packet_size_left < 0)
982 2141dc37 Zdenek Kabelac
            continue;
983 4c71d727 Michael Niedermayer
984
        if(   asf->packet_frag_offset >= asf_st->pkt.size
985
           || asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset){
986
            av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
987
                asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
988
            continue;
989
        }
990
991 b7effd4e Anton Khirnov
        ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
992 4172951b Reimar Döffinger
                         asf->packet_frag_size);
993 bf09a019 Reimar Döffinger
        if (ret != asf->packet_frag_size) {
994
            if (ret < 0 || asf->packet_frag_offset + ret == 0)
995
                return ret < 0 ? ret : AVERROR_EOF;
996
            if (asf_st->ds_span > 1) {
997
                // scrambling, we can either drop it completely or fill the remainder
998
                // TODO: should we fill the whole packet instead of just the current
999
                // fragment?
1000
                memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
1001
                       asf->packet_frag_size - ret);
1002
                ret = asf->packet_frag_size;
1003
            } else
1004
                // no scrambling, so we can return partial packets
1005
                av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
1006
        }
1007 72be7db4 Reimar Döffinger
        if (s->key && s->keylen == 20)
1008
            ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
1009 bf09a019 Reimar Döffinger
                            ret);
1010
        asf_st->frag_offset += ret;
1011 bb270c08 Diego Biurrun
        /* test if whole packet is read */
1012
        if (asf_st->frag_offset == asf_st->pkt.size) {
1013 da0b94f5 Michael Niedermayer
            //workaround for macroshit radio DVR-MS files
1014
            if(   s->streams[asf->stream_index]->codec->codec_id == CODEC_ID_MPEG2VIDEO
1015
               && asf_st->pkt.size > 100){
1016
                int i;
1017
                for(i=0; i<asf_st->pkt.size && !asf_st->pkt.data[i]; i++);
1018
                if(i == asf_st->pkt.size){
1019
                    av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
1020
                    asf_st->frag_offset = 0;
1021
                    av_free_packet(&asf_st->pkt);
1022
                    continue;
1023
                }
1024
            }
1025
1026 bb270c08 Diego Biurrun
            /* return packet */
1027
            if (asf_st->ds_span > 1) {
1028 05219463 Michael Niedermayer
              if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span){
1029 b43387d4 Michael Niedermayer
                    av_log(s, AV_LOG_ERROR, "pkt.size != ds_packet_size * ds_span (%d %d %d)\n", asf_st->pkt.size, asf_st->ds_packet_size, asf_st->ds_span);
1030 05219463 Michael Niedermayer
              }else{
1031 bb270c08 Diego Biurrun
                /* packet descrambling */
1032 3c3ef81b Reimar Döffinger
                uint8_t *newdata = av_malloc(asf_st->pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
1033 bb270c08 Diego Biurrun
                if (newdata) {
1034
                    int offset = 0;
1035 3c3ef81b Reimar Döffinger
                    memset(newdata + asf_st->pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1036 bb270c08 Diego Biurrun
                    while (offset < asf_st->pkt.size) {
1037
                        int off = offset / asf_st->ds_chunk_size;
1038
                        int row = off / asf_st->ds_span;
1039
                        int col = off % asf_st->ds_span;
1040
                        int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1041
                        //printf("off:%d  row:%d  col:%d  idx:%d\n", off, row, col, idx);
1042 05219463 Michael Niedermayer
1043
                        assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
1044
                        assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
1045 bb270c08 Diego Biurrun
                        memcpy(newdata + offset,
1046
                               asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1047
                               asf_st->ds_chunk_size);
1048
                        offset += asf_st->ds_chunk_size;
1049
                    }
1050
                    av_free(asf_st->pkt.data);
1051
                    asf_st->pkt.data = newdata;
1052
                }
1053 05219463 Michael Niedermayer
              }
1054 bb270c08 Diego Biurrun
            }
1055
            asf_st->frag_offset = 0;
1056 b83e83b9 Michael Niedermayer
            *pkt= asf_st->pkt;
1057 bb270c08 Diego Biurrun
            //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
1058
            asf_st->pkt.size = 0;
1059
            asf_st->pkt.data = 0;
1060
            break; // packet completed
1061
        }
1062 de6d9b64 Fabrice Bellard
    }
1063
    return 0;
1064
}
1065
1066 f27544fd Ronald S. Bultje
static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1067
{
1068
    ASFContext *asf = s->priv_data;
1069
1070
    for (;;) {
1071
        int ret;
1072
1073
        /* parse cached packets, if any */
1074 1d217cdb Ronald S. Bultje
        if ((ret = ff_asf_parse_packet(s, s->pb, pkt)) <= 0)
1075 f27544fd Ronald S. Bultje
            return ret;
1076 1d217cdb Ronald S. Bultje
        if ((ret = ff_asf_get_packet(s, s->pb)) < 0)
1077 f27544fd Ronald S. Bultje
            assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
1078
        asf->packet_time_start = 0;
1079
    }
1080
1081
    return 0;
1082
}
1083
1084 38376f00 Kevin Kuphal
// Added to support seeking after packets have been read
1085
// If information is not reset, read_packet fails due to
1086
// leftover information from previous reads
1087
static void asf_reset_header(AVFormatContext *s)
1088
{
1089
    ASFContext *asf = s->priv_data;
1090 82b9e4a2 Michael Niedermayer
    ASFStream *asf_st;
1091
    int i;
1092 38376f00 Kevin Kuphal
1093
    asf->packet_size_left = 0;
1094
    asf->packet_segments = 0;
1095
    asf->packet_flags = 0;
1096
    asf->packet_property = 0;
1097
    asf->packet_timestamp = 0;
1098
    asf->packet_segsizetype = 0;
1099
    asf->packet_segments = 0;
1100
    asf->packet_seq = 0;
1101
    asf->packet_replic_size = 0;
1102
    asf->packet_key_frame = 0;
1103
    asf->packet_padsize = 0;
1104
    asf->packet_frag_offset = 0;
1105
    asf->packet_frag_size = 0;
1106
    asf->packet_frag_timestamp = 0;
1107
    asf->packet_multi_size = 0;
1108
    asf->packet_obj_size = 0;
1109
    asf->packet_time_delta = 0;
1110
    asf->packet_time_start = 0;
1111 115329f1 Diego Biurrun
1112 82b9e4a2 Michael Niedermayer
    for(i=0; i<s->nb_streams; i++){
1113
        asf_st= s->streams[i]->priv_data;
1114
        av_free_packet(&asf_st->pkt);
1115
        asf_st->frag_offset=0;
1116
        asf_st->seq=0;
1117
    }
1118
    asf->asf_st= NULL;
1119 38376f00 Kevin Kuphal
}
1120
1121 5d25d2a9 Diego Biurrun
static int asf_read_close(AVFormatContext *s)
1122
{
1123
    int i;
1124
1125
    asf_reset_header(s);
1126
    for(i=0;i<s->nb_streams;i++) {
1127
        AVStream *st = s->streams[i];
1128
        av_free(st->codec->palctrl);
1129
    }
1130
    return 0;
1131
}
1132
1133 8d14a25c Michael Niedermayer
static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
1134 38376f00 Kevin Kuphal
{
1135
    AVPacket pkt1, *pkt = &pkt1;
1136 82b9e4a2 Michael Niedermayer
    ASFStream *asf_st;
1137 580fb5e7 Michael Niedermayer
    int64_t pts;
1138 82b9e4a2 Michael Niedermayer
    int64_t pos= *ppos;
1139 3e9245a9 Michael Niedermayer
    int i;
1140 30cdfb49 Måns Rullgård
    int64_t start_pos[ASF_MAX_STREAMS];
1141 115329f1 Diego Biurrun
1142 3e9245a9 Michael Niedermayer
    for(i=0; i<s->nb_streams; i++){
1143
        start_pos[i]= pos;
1144
    }
1145 115329f1 Diego Biurrun
1146 68a10b40 Reimar Döffinger
    if (s->packet_size > 0)
1147 b098b309 Reimar Döffinger
        pos= (pos+s->packet_size-1-s->data_offset)/s->packet_size*s->packet_size+ s->data_offset;
1148 8d14a25c Michael Niedermayer
    *ppos= pos;
1149 6b4aa5da Anton Khirnov
    avio_seek(s->pb, pos, SEEK_SET);
1150 115329f1 Diego Biurrun
1151 82b9e4a2 Michael Niedermayer
//printf("asf_read_pts\n");
1152
    asf_reset_header(s);
1153 3e9245a9 Michael Niedermayer
    for(;;){
1154 82b9e4a2 Michael Niedermayer
        if (av_read_frame(s, pkt) < 0){
1155 0b93103a Michael Niedermayer
            av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
1156 bb270c08 Diego Biurrun
            return AV_NOPTS_VALUE;
1157 82b9e4a2 Michael Niedermayer
        }
1158 115329f1 Diego Biurrun
1159 38cf2a72 Michael Niedermayer
        pts= pkt->pts;
1160 580fb5e7 Michael Niedermayer
1161
        av_free_packet(pkt);
1162 cc947f04 Jean-Daniel Dupas
        if(pkt->flags&AV_PKT_FLAG_KEY){
1163 3e9245a9 Michael Niedermayer
            i= pkt->stream_index;
1164
1165
            asf_st= s->streams[i]->priv_data;
1166
1167 91d19d47 Ronald S. Bultje
//            assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
1168 8d14a25c Michael Niedermayer
            pos= asf_st->packet_pos;
1169 09646bab Michael Niedermayer
1170 52e54612 Michael Niedermayer
            av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
1171 8d14a25c Michael Niedermayer
            start_pos[i]= asf_st->packet_pos + 1;
1172 115329f1 Diego Biurrun
1173 3e9245a9 Michael Niedermayer
            if(pkt->stream_index == stream_index)
1174
               break;
1175
        }
1176
    }
1177
1178
    *ppos= pos;
1179 949b1a13 Steve L'Homme
//printf("found keyframe at %"PRId64" stream %d stamp:%"PRId64"\n", *ppos, stream_index, pts);
1180 580fb5e7 Michael Niedermayer
1181
    return pts;
1182 38376f00 Kevin Kuphal
}
1183
1184 56c96a22 Michael Niedermayer
static void asf_build_simple_index(AVFormatContext *s, int stream_index)
1185
{
1186 b8b00d94 Reimar Döffinger
    ff_asf_guid g;
1187 56c96a22 Michael Niedermayer
    ASFContext *asf = s->priv_data;
1188 d728e3cd Michael Niedermayer
    int64_t current_pos= url_ftell(s->pb);
1189 56c96a22 Michael Niedermayer
    int i;
1190
1191 6b4aa5da Anton Khirnov
    avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET);
1192 a750050f Peter Ross
    ff_get_guid(s->pb, &g);
1193 77b7c334 Stefan Gehrer
1194
    /* the data object can be followed by other top-level objects,
1195
       skip them until the simple index object is reached */
1196 a750050f Peter Ross
    while (ff_guidcmp(&g, &index_guid)) {
1197 b7effd4e Anton Khirnov
        int64_t gsize= avio_rl64(s->pb);
1198 77b7c334 Stefan Gehrer
        if (gsize < 24 || url_feof(s->pb)) {
1199 6b4aa5da Anton Khirnov
            avio_seek(s->pb, current_pos, SEEK_SET);
1200 77b7c334 Stefan Gehrer
            return;
1201
        }
1202 6b4aa5da Anton Khirnov
        avio_seek(s->pb, gsize-24, SEEK_CUR);
1203 a750050f Peter Ross
        ff_get_guid(s->pb, &g);
1204 77b7c334 Stefan Gehrer
    }
1205
1206
    {
1207 134271c2 Michael Niedermayer
        int64_t itime, last_pos=-1;
1208 d728e3cd Michael Niedermayer
        int pct, ict;
1209 b7effd4e Anton Khirnov
        int64_t av_unused gsize= avio_rl64(s->pb);
1210 a750050f Peter Ross
        ff_get_guid(s->pb, &g);
1211 b7effd4e Anton Khirnov
        itime=avio_rl64(s->pb);
1212
        pct=avio_rl32(s->pb);
1213
        ict=avio_rl32(s->pb);
1214 871e62e5 Benoit Fouet
        av_log(s, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
1215 56c96a22 Michael Niedermayer
1216
        for (i=0;i<ict;i++){
1217 b7effd4e Anton Khirnov
            int pktnum=avio_rl32(s->pb);
1218
            int pktct =avio_rl16(s->pb);
1219 91d19d47 Ronald S. Bultje
            int64_t pos      = s->data_offset + s->packet_size*(int64_t)pktnum;
1220 d728e3cd Michael Niedermayer
            int64_t index_pts= av_rescale(itime, i, 10000);
1221 56c96a22 Michael Niedermayer
1222 134271c2 Michael Niedermayer
            if(pos != last_pos){
1223 d728e3cd Michael Niedermayer
            av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d\n", pktnum, pktct);
1224 91d19d47 Ronald S. Bultje
            av_add_index_entry(s->streams[stream_index], pos, index_pts, s->packet_size, 0, AVINDEX_KEYFRAME);
1225 134271c2 Michael Niedermayer
            last_pos=pos;
1226
            }
1227 56c96a22 Michael Niedermayer
        }
1228 52e54612 Michael Niedermayer
        asf->index_read= 1;
1229 56c96a22 Michael Niedermayer
    }
1230 6b4aa5da Anton Khirnov
    avio_seek(s->pb, current_pos, SEEK_SET);
1231 56c96a22 Michael Niedermayer
}
1232
1233 3ba1438d Michael Niedermayer
static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
1234 2a10020b Zdenek Kabelac
{
1235 afda223c Fabrice Bellard
    ASFContext *asf = s->priv_data;
1236 56c96a22 Michael Niedermayer
    AVStream *st = s->streams[stream_index];
1237
    int64_t pos;
1238
    int index;
1239 115329f1 Diego Biurrun
1240 91d19d47 Ronald S. Bultje
    if (s->packet_size <= 0)
1241 38376f00 Kevin Kuphal
        return -1;
1242
1243 fa13095a Björn Axelsson
    /* Try using the protocol's read_seek if available */
1244 fd2982a0 Aurelien Jacobs
    if(s->pb) {
1245 fa13095a Björn Axelsson
        int ret = av_url_read_fseek(s->pb, stream_index, pts, flags);
1246
        if(ret >= 0)
1247
            asf_reset_header(s);
1248 fd2982a0 Aurelien Jacobs
        if (ret != AVERROR(ENOSYS))
1249 cd31f8dc Aurelien Jacobs
            return ret;
1250 fa13095a Björn Axelsson
    }
1251
1252 52e54612 Michael Niedermayer
    if (!asf->index_read)
1253 56c96a22 Michael Niedermayer
        asf_build_simple_index(s, stream_index);
1254
1255 52e54612 Michael Niedermayer
    if(!(asf->index_read && st->index_entries)){
1256 56c96a22 Michael Niedermayer
        if(av_seek_frame_binary(s, stream_index, pts, flags)<0)
1257
            return -1;
1258
    }else{
1259
        index= av_index_search_timestamp(st, pts, flags);
1260
        if(index<0)
1261
            return -1;
1262
1263
        /* find the position */
1264
        pos = st->index_entries[index].pos;
1265
1266
    // various attempts to find key frame have failed so far
1267
    //    asf_reset_header(s);
1268 6b4aa5da Anton Khirnov
    //    avio_seek(s->pb, pos, SEEK_SET);
1269 56c96a22 Michael Niedermayer
    //    key_pos = pos;
1270
    //     for(i=0;i<16;i++){
1271 899681cd Björn Axelsson
    //         pos = url_ftell(s->pb);
1272 56c96a22 Michael Niedermayer
    //         if (av_read_frame(s, &pkt) < 0){
1273
    //             av_log(s, AV_LOG_INFO, "seek failed\n");
1274
    //             return -1;
1275
    //         }
1276
    //         asf_st = s->streams[stream_index]->priv_data;
1277
    //         pos += st->parser->frame_offset;
1278
    //
1279
    //         if (pkt.size > b) {
1280
    //             b = pkt.size;
1281
    //             key_pos = pos;
1282
    //         }
1283
    //
1284
    //         av_free_packet(&pkt);
1285
    //     }
1286
1287
        /* do the seek */
1288 871e62e5 Benoit Fouet
        av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
1289 6b4aa5da Anton Khirnov
        avio_seek(s->pb, pos, SEEK_SET);
1290 56c96a22 Michael Niedermayer
    }
1291 38376f00 Kevin Kuphal
    asf_reset_header(s);
1292
    return 0;
1293 2a10020b Zdenek Kabelac
}
1294
1295 c6610a21 Diego Elio Pettenò
AVInputFormat ff_asf_demuxer = {
1296 c9a65ca8 Fabrice Bellard
    "asf",
1297 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("ASF format"),
1298 c9a65ca8 Fabrice Bellard
    sizeof(ASFContext),
1299
    asf_probe,
1300
    asf_read_header,
1301
    asf_read_packet,
1302
    asf_read_close,
1303 2a10020b Zdenek Kabelac
    asf_read_seek,
1304 8d14a25c Michael Niedermayer
    asf_read_pts,
1305 c9a65ca8 Fabrice Bellard
};