Statistics
| Branch: | Revision:

ffmpeg / libavformat / asfdec.c @ 7c725380

History | View | Annotate | Download (47.4 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 a750050f Peter Ross
void ff_get_guid(ByteIOContext *s, ff_asf_guid *g)
136 de6d9b64 Fabrice Bellard
{
137 24c14d6d Michael Niedermayer
    assert(sizeof(*g) == 16);
138 3dc761a4 Baptiste Coudurier
    get_buffer(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 30e88789 Michael Niedermayer
static int get_value(ByteIOContext *pb, int type){
151
    switch(type){
152
        case 2: return get_le32(pb);
153
        case 3: return get_le32(pb);
154
        case 4: return get_le64(pb);
155
        case 5: return get_le16(pb);
156
        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 fce44489 Anton Khirnov
    av_metadata_set2(&s->metadata, key, value, 0);
182 04295054 Anton Khirnov
finish:
183 fce44489 Anton Khirnov
    av_freep(&value);
184 04295054 Anton Khirnov
    url_fseek(s->pb, off + len, SEEK_SET);
185 20e2a7ae Aurelien Jacobs
}
186
187 de6d9b64 Fabrice Bellard
static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
188
{
189 c9a65ca8 Fabrice Bellard
    ASFContext *asf = s->priv_data;
190 b8b00d94 Reimar Döffinger
    ff_asf_guid g;
191 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
192 de6d9b64 Fabrice Bellard
    AVStream *st;
193
    ASFStream *asf_st;
194 e095026a Fabrice Bellard
    int size, i;
195 0c1a9eda Zdenek Kabelac
    int64_t gsize;
196 de6d9b64 Fabrice Bellard
197 a750050f Peter Ross
    ff_get_guid(pb, &g);
198
    if (ff_guidcmp(&g, &ff_asf_header))
199 ccafd472 Baptiste Coudurier
        return -1;
200 de6d9b64 Fabrice Bellard
    get_le64(pb);
201
    get_le32(pb);
202
    get_byte(pb);
203
    get_byte(pb);
204 2141dc37 Zdenek Kabelac
    memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
205 de6d9b64 Fabrice Bellard
    for(;;) {
206 cb0061d4 Michael Niedermayer
        uint64_t gpos= url_ftell(pb);
207 93b78d12 Anton Khirnov
        int ret;
208 a750050f Peter Ross
        ff_get_guid(pb, &g);
209 de6d9b64 Fabrice Bellard
        gsize = get_le64(pb);
210 dfd2a005 Luca Barbato
        av_dlog(s, "%08"PRIx64": ", gpos);
211 de6d9b64 Fabrice Bellard
        print_guid(&g);
212 dfd2a005 Luca Barbato
        av_dlog(s, "  size=0x%"PRIx64"\n", gsize);
213 a750050f Peter Ross
        if (!ff_guidcmp(&g, &ff_asf_data_header)) {
214 09d9672a Michael Niedermayer
            asf->data_object_offset = url_ftell(pb);
215
            // if not streaming, gsize is not unlimited (how?), and there is enough space in the file..
216
            if (!(asf->hdr.flags & 0x01) && gsize >= 100) {
217
                asf->data_object_size = gsize - 24;
218
            } else {
219
                asf->data_object_size = (uint64_t)-1;
220
            }
221
            break;
222
        }
223 de6d9b64 Fabrice Bellard
        if (gsize < 24)
224 ccafd472 Baptiste Coudurier
            return -1;
225 a750050f Peter Ross
        if (!ff_guidcmp(&g, &ff_asf_file_header)) {
226
            ff_get_guid(pb, &asf->hdr.guid);
227 bb270c08 Diego Biurrun
            asf->hdr.file_size          = get_le64(pb);
228
            asf->hdr.create_time        = get_le64(pb);
229 569ff021 Anton Khirnov
            get_le64(pb);                               /* number of packets */
230 bb270c08 Diego Biurrun
            asf->hdr.play_time          = get_le64(pb);
231 b4bbcd63 Rafaël Carré
            asf->hdr.send_time          = get_le64(pb);
232 bb270c08 Diego Biurrun
            asf->hdr.preroll            = get_le32(pb);
233
            asf->hdr.ignore             = get_le32(pb);
234
            asf->hdr.flags              = get_le32(pb);
235
            asf->hdr.min_pktsize        = get_le32(pb);
236
            asf->hdr.max_pktsize        = get_le32(pb);
237
            asf->hdr.max_bitrate        = get_le32(pb);
238 91d19d47 Ronald S. Bultje
            s->packet_size = asf->hdr.max_pktsize;
239 a750050f Peter Ross
        } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
240 72415b2a Stefano Sabatini
            enum AVMediaType type;
241 fb65d2ca Diego Pettenò
            int type_specific_size, sizeX;
242 7ad5455c Michael Niedermayer
            uint64_t total_size;
243 de6d9b64 Fabrice Bellard
            unsigned int tag1;
244 371bf806 Michael Niedermayer
            int64_t pos1, pos2, start_time;
245 6fea687e John Donaghy
            int test_for_ext_stream_audio, is_dvr_ms_audio=0;
246 2a10020b Zdenek Kabelac
247 30cdfb49 Måns Rullgård
            if (s->nb_streams == ASF_MAX_STREAMS) {
248
                av_log(s, AV_LOG_ERROR, "too many streams\n");
249
                return AVERROR(EINVAL);
250
            }
251
252 de6d9b64 Fabrice Bellard
            pos1 = url_ftell(pb);
253
254 247eadca Fabrice Bellard
            st = av_new_stream(s, 0);
255 de6d9b64 Fabrice Bellard
            if (!st)
256 ccafd472 Baptiste Coudurier
                return AVERROR(ENOMEM);
257 9ee91c2f Michael Niedermayer
            av_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
258 de6d9b64 Fabrice Bellard
            asf_st = av_mallocz(sizeof(ASFStream));
259
            if (!asf_st)
260 ccafd472 Baptiste Coudurier
                return AVERROR(ENOMEM);
261 de6d9b64 Fabrice Bellard
            st->priv_data = asf_st;
262 371bf806 Michael Niedermayer
            start_time = asf->hdr.preroll;
263
264 5f1ac36d Cyril Comparon
            asf_st->stream_language_index = 128; // invalid stream index means no language info
265
266 73fe1052 Ryan Martell
            if(!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
267 f2086fb5 Josh Harris
                st->duration = asf->hdr.play_time /
268 371bf806 Michael Niedermayer
                    (10000000 / 1000) - start_time;
269 73fe1052 Ryan Martell
            }
270 a750050f Peter Ross
            ff_get_guid(pb, &g);
271 88141c91 Michael Niedermayer
272
            test_for_ext_stream_audio = 0;
273 a750050f Peter Ross
            if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
274 72415b2a Stefano Sabatini
                type = AVMEDIA_TYPE_AUDIO;
275 a750050f Peter Ross
            } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
276 72415b2a Stefano Sabatini
                type = AVMEDIA_TYPE_VIDEO;
277 a750050f Peter Ross
            } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
278 a3ab7c32 Martin Storsjö
                type = AVMEDIA_TYPE_VIDEO;
279
                st->codec->codec_id = CODEC_ID_MJPEG;
280 a750050f Peter Ross
            } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
281 72415b2a Stefano Sabatini
                type = AVMEDIA_TYPE_DATA;
282 a750050f Peter Ross
            } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_embed_stream_header)) {
283 88141c91 Michael Niedermayer
                test_for_ext_stream_audio = 1;
284 72415b2a Stefano Sabatini
                type = AVMEDIA_TYPE_UNKNOWN;
285 de6d9b64 Fabrice Bellard
            } else {
286 ccafd472 Baptiste Coudurier
                return -1;
287 de6d9b64 Fabrice Bellard
            }
288 a750050f Peter Ross
            ff_get_guid(pb, &g);
289 de6d9b64 Fabrice Bellard
            total_size = get_le64(pb);
290 2e7973bb Roman Shaposhnik
            type_specific_size = get_le32(pb);
291 de6d9b64 Fabrice Bellard
            get_le32(pb);
292 bb270c08 Diego Biurrun
            st->id = get_le16(pb) & 0x7f; /* stream id */
293 2a10020b Zdenek Kabelac
            // mapping of asf ID to AV stream ID;
294 247eadca Fabrice Bellard
            asf->asfid2avid[st->id] = s->nb_streams - 1;
295 2a10020b Zdenek Kabelac
296 de6d9b64 Fabrice Bellard
            get_le32(pb);
297 88141c91 Michael Niedermayer
298
            if (test_for_ext_stream_audio) {
299 a750050f Peter Ross
                ff_get_guid(pb, &g);
300
                if (!ff_guidcmp(&g, &ff_asf_ext_stream_audio_stream)) {
301 72415b2a Stefano Sabatini
                    type = AVMEDIA_TYPE_AUDIO;
302 6fea687e John Donaghy
                    is_dvr_ms_audio=1;
303 a750050f Peter Ross
                    ff_get_guid(pb, &g);
304 88141c91 Michael Niedermayer
                    get_le32(pb);
305
                    get_le32(pb);
306
                    get_le32(pb);
307 a750050f Peter Ross
                    ff_get_guid(pb, &g);
308 88141c91 Michael Niedermayer
                    get_le32(pb);
309
                }
310
            }
311
312 bb270c08 Diego Biurrun
            st->codec->codec_type = type;
313 72415b2a Stefano Sabatini
            if (type == AVMEDIA_TYPE_AUDIO) {
314 1a40491e Daniel Verkamp
                ff_get_wav_header(pb, st->codec, type_specific_size);
315 6fea687e John Donaghy
                if (is_dvr_ms_audio) {
316
                    // codec_id and codec_tag are unreliable in dvr_ms
317
                    // files. Set them later by probing stream.
318 0bef08e5 Michael Niedermayer
                    st->codec->codec_id = CODEC_ID_PROBE;
319 6fea687e John Donaghy
                    st->codec->codec_tag = 0;
320
                }
321 e7f73adb Alex Converse
                if (st->codec->codec_id == CODEC_ID_AAC) {
322
                    st->need_parsing = AVSTREAM_PARSE_NONE;
323
                } else {
324 e9259f8d Alex Converse
                    st->need_parsing = AVSTREAM_PARSE_FULL;
325 e7f73adb Alex Converse
                }
326 bb270c08 Diego Biurrun
                /* We have to init the frame size at some point .... */
327
                pos2 = url_ftell(pb);
328 b9c6d01b Michael Niedermayer
                if (gsize >= (pos2 + 8 - pos1 + 24)) {
329 bb270c08 Diego Biurrun
                    asf_st->ds_span = get_byte(pb);
330
                    asf_st->ds_packet_size = get_le16(pb);
331
                    asf_st->ds_chunk_size = get_le16(pb);
332 6bb2a2df Michael Niedermayer
                    get_le16(pb); //ds_data_size
333
                    get_byte(pb); //ds_silence_data
334 bb270c08 Diego Biurrun
                }
335
                //printf("Descrambling: ps:%d cs:%d ds:%d s:%d  sd:%d\n",
336
                //       asf_st->ds_packet_size, asf_st->ds_chunk_size,
337
                //       asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data);
338
                if (asf_st->ds_span > 1) {
339
                    if (!asf_st->ds_chunk_size
340 05219463 Michael Niedermayer
                        || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1)
341
                        || asf_st->ds_packet_size % asf_st->ds_chunk_size)
342 bb270c08 Diego Biurrun
                        asf_st->ds_span = 0; // disable descrambling
343
                }
344 01f4895c Michael Niedermayer
                switch (st->codec->codec_id) {
345 80783dc2 Fabrice Bellard
                case CODEC_ID_MP3:
346 01f4895c Michael Niedermayer
                    st->codec->frame_size = MPA_FRAME_SIZE;
347 f80c1ac0 Philip Gladstone
                    break;
348
                case CODEC_ID_PCM_S16LE:
349
                case CODEC_ID_PCM_S16BE:
350
                case CODEC_ID_PCM_U16LE:
351
                case CODEC_ID_PCM_U16BE:
352
                case CODEC_ID_PCM_S8:
353
                case CODEC_ID_PCM_U8:
354
                case CODEC_ID_PCM_ALAW:
355
                case CODEC_ID_PCM_MULAW:
356 01f4895c Michael Niedermayer
                    st->codec->frame_size = 1;
357 f80c1ac0 Philip Gladstone
                    break;
358
                default:
359
                    /* This is probably wrong, but it prevents a crash later */
360 01f4895c Michael Niedermayer
                    st->codec->frame_size = 1;
361 f80c1ac0 Philip Gladstone
                    break;
362
                }
363 fed79592 Martin Storsjö
            } else if (type == AVMEDIA_TYPE_VIDEO &&
364 d970ec75 Martin Storsjö
                       gsize - (url_ftell(pb) - pos1 + 24) >= 51) {
365 bb270c08 Diego Biurrun
                get_le32(pb);
366 de6d9b64 Fabrice Bellard
                get_le32(pb);
367
                get_byte(pb);
368
                size = get_le16(pb); /* size */
369 0ffd77b9 Michael Niedermayer
                sizeX= get_le32(pb); /* size */
370 01f4895c Michael Niedermayer
                st->codec->width = get_le32(pb);
371 bb270c08 Diego Biurrun
                st->codec->height = get_le32(pb);
372 2a10020b Zdenek Kabelac
                /* not available for asf */
373 de6d9b64 Fabrice Bellard
                get_le16(pb); /* panes */
374 dd1c8f3e Luca Abeni
                st->codec->bits_per_coded_sample = get_le16(pb); /* depth */
375 de6d9b64 Fabrice Bellard
                tag1 = get_le32(pb);
376 bb270c08 Diego Biurrun
                url_fskip(pb, 20);
377 871e62e5 Benoit Fouet
//                av_log(s, AV_LOG_DEBUG, "size:%d tsize:%d sizeX:%d\n", size, total_size, sizeX);
378 0ffd77b9 Michael Niedermayer
                size= sizeX;
379 bb270c08 Diego Biurrun
                if (size > 40) {
380
                    st->codec->extradata_size = size - 40;
381
                    st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
382
                    get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
383
                }
384 5e29abf8 Roberto Togni
385 6d050dd6 avcoder
                /* Extract palette from extradata if bpp <= 8 */
386
                /* This code assumes that extradata contains only palette */
387
                /* This is true for all paletted codecs implemented in ffmpeg */
388
                if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
389
                    st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
390 63613fe6 Måns Rullgård
#if HAVE_BIGENDIAN
391 6d050dd6 avcoder
                    for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
392 8fc0162a Måns Rullgård
                        st->codec->palctrl->palette[i] = av_bswap32(((uint32_t*)st->codec->extradata)[i]);
393 5e29abf8 Roberto Togni
#else
394 6d050dd6 avcoder
                    memcpy(st->codec->palctrl->palette, st->codec->extradata,
395
                           FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
396 5e29abf8 Roberto Togni
#endif
397 6d050dd6 avcoder
                    st->codec->palctrl->palette_changed = 1;
398
                }
399 5e29abf8 Roberto Togni
400 01f4895c Michael Niedermayer
                st->codec->codec_tag = tag1;
401 1a40491e Daniel Verkamp
                st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
402 31247669 Michael Niedermayer
                if(tag1 == MKTAG('D', 'V', 'R', ' ')){
403 57004ff1 Aurelien Jacobs
                    st->need_parsing = AVSTREAM_PARSE_FULL;
404 31247669 Michael Niedermayer
                    // 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
405
                    st->codec->width  =
406
                    st->codec->height = 0;
407
                    av_freep(&st->codec->extradata);
408
                    st->codec->extradata_size=0;
409
                }
410 1ed87f80 Alex Converse
                if(st->codec->codec_id == CODEC_ID_H264)
411
                    st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
412 de6d9b64 Fabrice Bellard
            }
413
            pos2 = url_ftell(pb);
414
            url_fskip(pb, gsize - (pos2 - pos1 + 24));
415 a750050f Peter Ross
        } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
416 de6d9b64 Fabrice Bellard
            int len1, len2, len3, len4, len5;
417
418
            len1 = get_le16(pb);
419
            len2 = get_le16(pb);
420
            len3 = get_le16(pb);
421
            len4 = get_le16(pb);
422
            len5 = get_le16(pb);
423 20e2a7ae Aurelien Jacobs
            get_tag(s, "title"    , 0, len1);
424
            get_tag(s, "author"   , 0, len2);
425
            get_tag(s, "copyright", 0, len3);
426
            get_tag(s, "comment"  , 0, len4);
427 bb270c08 Diego Biurrun
            url_fskip(pb, len5);
428 a750050f Peter Ross
        } else if (!ff_guidcmp(&g, &stream_bitrate_guid)) {
429 0e3b6a6f Ryan Martell
            int stream_count = get_le16(pb);
430
            int j;
431
432 871e62e5 Benoit Fouet
//            av_log(s, AV_LOG_ERROR, "stream bitrate properties\n");
433
//            av_log(s, AV_LOG_ERROR, "streams %d\n", streams);
434 0e3b6a6f Ryan Martell
            for(j = 0; j < stream_count; j++) {
435
                int flags, bitrate, stream_id;
436
437
                flags= get_le16(pb);
438
                bitrate= get_le32(pb);
439
                stream_id= (flags & 0x7f);
440 871e62e5 Benoit Fouet
//                av_log(s, AV_LOG_ERROR, "flags: 0x%x stream id %d, bitrate %d\n", flags, stream_id, bitrate);
441 a9054d91 Björn Axelsson
                asf->stream_bitrates[stream_id]= bitrate;
442 0e3b6a6f Ryan Martell
            }
443 a750050f Peter Ross
        } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
444 5f1ac36d Cyril Comparon
            int j;
445
            int stream_count = get_le16(pb);
446
            for(j = 0; j < stream_count; j++) {
447
                char lang[6];
448
                unsigned int lang_len = get_byte(pb);
449 93b78d12 Anton Khirnov
                if ((ret = avio_get_str16le(pb, lang_len, lang, sizeof(lang))) < lang_len)
450
                    url_fskip(pb, lang_len - ret);
451 5f1ac36d Cyril Comparon
                if (j < 128)
452
                    av_strlcpy(asf->stream_languages[j], lang, sizeof(*asf->stream_languages));
453
            }
454 a750050f Peter Ross
        } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
455 6d050dd6 avcoder
            int desc_count, i;
456
457
            desc_count = get_le16(pb);
458
            for(i=0;i<desc_count;i++) {
459
                    int name_len,value_type,value_len;
460
                    char name[1024];
461
462
                    name_len = get_le16(pb);
463 4a3104a9 Anton Khirnov
                    if (name_len%2)     // must be even, broken lavf versions wrote len-1
464
                        name_len += 1;
465 93b78d12 Anton Khirnov
                    if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
466
                        url_fskip(pb, name_len - ret);
467 6d050dd6 avcoder
                    value_type = get_le16(pb);
468
                    value_len  = get_le16(pb);
469 514b73cf Benoit Fouet
                    if (!value_type && value_len%2)
470 4a3104a9 Anton Khirnov
                        value_len += 1;
471 6ac6e3d1 Richard Buteau
                    /**
472
                     * My sample has that stream set to 0 maybe that mean the container.
473
                     * Asf stream count start at 1. I am using 0 to the container value since it's unused
474
                     */
475
                    if (!strcmp(name, "AspectRatioX")){
476 d42b0972 Anton Khirnov
                        asf->dar[0].num= get_value(s->pb, value_type);
477 6ac6e3d1 Richard Buteau
                    } else if(!strcmp(name, "AspectRatioY")){
478 d42b0972 Anton Khirnov
                        asf->dar[0].den= get_value(s->pb, value_type);
479 b7d56a17 Carl Eugen Hoyos
                    } else
480
                        get_tag(s, name, value_type, value_len);
481 6d050dd6 avcoder
            }
482 a750050f Peter Ross
        } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
483 30e88789 Michael Niedermayer
            int n, stream_num, name_len, value_len, value_type, value_num;
484
            n = get_le16(pb);
485
486
            for(i=0;i<n;i++) {
487
                char name[1024];
488
489
                get_le16(pb); //lang_list_index
490
                stream_num= get_le16(pb);
491
                name_len=   get_le16(pb);
492
                value_type= get_le16(pb);
493
                value_len=  get_le32(pb);
494
495 93b78d12 Anton Khirnov
                if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
496
                    url_fskip(pb, name_len - ret);
497 871e62e5 Benoit Fouet
//av_log(s, AV_LOG_ERROR, "%d %d %d %d %d <%s>\n", i, stream_num, name_len, value_type, value_len, name);
498 755bfeab Diego Biurrun
                value_num= get_le16(pb);//we should use get_value() here but it does not work 2 is le16 here but le32 elsewhere
499 30e88789 Michael Niedermayer
                url_fskip(pb, value_len - 2);
500
501
                if(stream_num<128){
502 d42b0972 Anton Khirnov
                    if     (!strcmp(name, "AspectRatioX")) asf->dar[stream_num].num= value_num;
503
                    else if(!strcmp(name, "AspectRatioY")) asf->dar[stream_num].den= value_num;
504 30e88789 Michael Niedermayer
                }
505
            }
506 a750050f Peter Ross
        } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
507 88141c91 Michael Niedermayer
            int ext_len, payload_ext_ct, stream_ct;
508 d8470737 Zuxy Meng
            uint32_t ext_d, leak_rate, stream_num;
509 5f1ac36d Cyril Comparon
            unsigned int stream_languageid_index;
510 88141c91 Michael Niedermayer
511 de3ae959 Michael Niedermayer
            get_le64(pb); // starttime
512
            get_le64(pb); // endtime
513 d8470737 Zuxy Meng
            leak_rate = get_le32(pb); // leak-datarate
514 de3ae959 Michael Niedermayer
            get_le32(pb); // bucket-datasize
515
            get_le32(pb); // init-bucket-fullness
516
            get_le32(pb); // alt-leak-datarate
517
            get_le32(pb); // alt-bucket-datasize
518
            get_le32(pb); // alt-init-bucket-fullness
519
            get_le32(pb); // max-object-size
520
            get_le32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
521 d8470737 Zuxy Meng
            stream_num = get_le16(pb); // stream-num
522 5f1ac36d Cyril Comparon
523
            stream_languageid_index = get_le16(pb); // stream-language-id-index
524
            if (stream_num < 128)
525
                asf->streams[stream_num].stream_language_index = stream_languageid_index;
526
527 de3ae959 Michael Niedermayer
            get_le64(pb); // avg frametime in 100ns units
528
            stream_ct = get_le16(pb); //stream-name-count
529
            payload_ext_ct = get_le16(pb); //payload-extension-system-count
530 88141c91 Michael Niedermayer
531 d8470737 Zuxy Meng
            if (stream_num < 128)
532 7c725380 Anton Khirnov
                asf->stream_bitrates[stream_num] = leak_rate;
533 d8470737 Zuxy Meng
534 88141c91 Michael Niedermayer
            for (i=0; i<stream_ct; i++){
535
                get_le16(pb);
536
                ext_len = get_le16(pb);
537
                url_fseek(pb, ext_len, SEEK_CUR);
538
            }
539
540
            for (i=0; i<payload_ext_ct; i++){
541 a750050f Peter Ross
                ff_get_guid(pb, &g);
542 88141c91 Michael Niedermayer
                ext_d=get_le16(pb);
543
                ext_len=get_le32(pb);
544
                url_fseek(pb, ext_len, SEEK_CUR);
545
            }
546
547
            // there could be a optional stream properties object to follow
548
            // if so the next iteration will pick it up
549 1d4c1c3b Michael Niedermayer
            continue;
550 a750050f Peter Ross
        } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
551 de6d9b64 Fabrice Bellard
            int v1, v2;
552 a750050f Peter Ross
            ff_get_guid(pb, &g);
553 de6d9b64 Fabrice Bellard
            v1 = get_le32(pb);
554
            v2 = get_le16(pb);
555 1d4c1c3b Michael Niedermayer
            continue;
556 a750050f Peter Ross
        } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
557 88b51ea9 Vladimir Pantelic
            int i, count, name_len;
558
            char name[1024];
559
560
            get_le64(pb);            // reserved 16 bytes
561
            get_le64(pb);            // ...
562
            count = get_le32(pb);    // markers count
563
            get_le16(pb);            // reserved 2 bytes
564
            name_len = get_le16(pb); // name length
565
            for(i=0;i<name_len;i++){
566
                get_byte(pb); // skip the name
567
            }
568
569
            for(i=0;i<count;i++){
570
                int64_t pres_time;
571
                int name_len;
572
573
                get_le64(pb);             // offset, 8 bytes
574
                pres_time = get_le64(pb); // presentation time
575
                get_le16(pb);             // entry length
576
                get_le32(pb);             // send time
577
                get_le32(pb);             // flags
578
                name_len = get_le32(pb);  // name length
579 93b78d12 Anton Khirnov
                if ((ret = avio_get_str16le(pb, name_len * 2, name, sizeof(name))) < name_len)
580
                    url_fskip(pb, name_len - ret);
581 88b51ea9 Vladimir Pantelic
                ff_new_chapter(s, i, (AVRational){1, 10000000}, pres_time, AV_NOPTS_VALUE, name );
582
            }
583 de6d9b64 Fabrice Bellard
        } else if (url_feof(pb)) {
584 ccafd472 Baptiste Coudurier
            return -1;
585 de6d9b64 Fabrice Bellard
        } else {
586 febd1c90 Daniel G. Taylor
            if (!s->keylen) {
587 a750050f Peter Ross
                if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
588 febd1c90 Daniel G. Taylor
                    av_log(s, AV_LOG_WARNING, "DRM protected stream detected, decoding will likely fail!\n");
589 a750050f Peter Ross
                } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
590 febd1c90 Daniel G. Taylor
                    av_log(s, AV_LOG_WARNING, "Ext DRM protected stream detected, decoding will likely fail!\n");
591 a750050f Peter Ross
                } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
592 febd1c90 Daniel G. Taylor
                    av_log(s, AV_LOG_WARNING, "Digital signature detected, decoding will likely fail!\n");
593
                }
594
            }
595 de6d9b64 Fabrice Bellard
        }
596 cb0061d4 Michael Niedermayer
        if(url_ftell(pb) != gpos + gsize)
597
            av_log(s, AV_LOG_DEBUG, "gpos mismatch our pos=%"PRIu64", end=%"PRIu64"\n", url_ftell(pb)-gpos, gsize);
598
        url_fseek(pb, gpos + gsize, SEEK_SET);
599 de6d9b64 Fabrice Bellard
    }
600 a750050f Peter Ross
    ff_get_guid(pb, &g);
601 de6d9b64 Fabrice Bellard
    get_le64(pb);
602
    get_byte(pb);
603
    get_byte(pb);
604 2141dc37 Zdenek Kabelac
    if (url_feof(pb))
605 ccafd472 Baptiste Coudurier
        return -1;
606 2141dc37 Zdenek Kabelac
    asf->data_offset = url_ftell(pb);
607 de6d9b64 Fabrice Bellard
    asf->packet_size_left = 0;
608
609 30e88789 Michael Niedermayer
610
    for(i=0; i<128; i++){
611
        int stream_num= asf->asfid2avid[i];
612 d8470737 Zuxy Meng
        if(stream_num>=0){
613 59729451 Aurelien Jacobs
            AVStream *st = s->streams[stream_num];
614
            if (!st->codec->bit_rate)
615 7c725380 Anton Khirnov
                st->codec->bit_rate = asf->stream_bitrates[i];
616 d42b0972 Anton Khirnov
            if (asf->dar[i].num > 0 && asf->dar[i].den > 0){
617 59729451 Aurelien Jacobs
                av_reduce(&st->sample_aspect_ratio.num,
618
                          &st->sample_aspect_ratio.den,
619 d42b0972 Anton Khirnov
                          asf->dar[i].num, asf->dar[i].den, INT_MAX);
620
            } 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.
621 6ac6e3d1 Richard Buteau
                av_reduce(&st->sample_aspect_ratio.num,
622
                          &st->sample_aspect_ratio.den,
623 d42b0972 Anton Khirnov
                          asf->dar[0].num, asf->dar[0].den, INT_MAX);
624 6ac6e3d1 Richard Buteau
625
//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);
626 5f1ac36d Cyril Comparon
627
            // copy and convert language codes to the frontend
628
            if (asf->streams[i].stream_language_index < 128) {
629
                const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
630
                if (rfc1766 && strlen(rfc1766) > 1) {
631
                    const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
632
                    const char *iso6392 = av_convert_lang_to(primary_tag, AV_LANG_ISO639_2_BIBL);
633
                    if (iso6392)
634 2ef6c124 Stefano Sabatini
                        av_metadata_set2(&st->metadata, "language", iso6392, 0);
635 5f1ac36d Cyril Comparon
                }
636
            }
637 30e88789 Michael Niedermayer
        }
638
    }
639
640 ad7768f4 Anton Khirnov
    ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
641 03700d39 Anton Khirnov
642 de6d9b64 Fabrice Bellard
    return 0;
643
}
644
645 2a10020b Zdenek Kabelac
#define DO_2BITS(bits, var, defval) \
646
    switch (bits & 3) \
647
    { \
648
    case 3: var = get_le32(pb); rsize += 4; break; \
649
    case 2: var = get_le16(pb); rsize += 2; break; \
650
    case 1: var = get_byte(pb); rsize++; break; \
651
    default: var = defval; break; \
652
    }
653
654 277a28e8 Ronald S. Bultje
/**
655
 * Load a single ASF packet into the demuxer.
656
 * @param s demux context
657
 * @param pb context to read data from
658 32e543f8 Benoit Fouet
 * @return 0 on success, <0 on error
659 277a28e8 Ronald S. Bultje
 */
660
static int ff_asf_get_packet(AVFormatContext *s, ByteIOContext *pb)
661 de6d9b64 Fabrice Bellard
{
662
    ASFContext *asf = s->priv_data;
663 2a10020b Zdenek Kabelac
    uint32_t packet_length, padsize;
664 0c0d17f3 Michael Niedermayer
    int rsize = 8;
665 0fd9e6f3 Michael Niedermayer
    int c, d, e, off;
666
667 68a10b40 Reimar Döffinger
    // if we do not know packet size, allow skipping up to 32 kB
668
    off= 32768;
669
    if (s->packet_size > 0)
670 b098b309 Reimar Döffinger
        off= (url_ftell(pb) - s->data_offset) % s->packet_size + 3;
671 0fd9e6f3 Michael Niedermayer
672
    c=d=e=-1;
673
    while(off-- > 0){
674
        c=d; d=e;
675
        e= get_byte(pb);
676
        if(c == 0x82 && !d && !e)
677
            break;
678
    }
679 115329f1 Diego Biurrun
680 1359c2d4 Zdenek Kabelac
    if (c != 0x82) {
681 2078ca16 Ronald S. Bultje
        /**
682
         * This code allows handling of -EAGAIN at packet boundaries (i.e.
683
         * if the packet sync code above triggers -EAGAIN). This does not
684
         * imply complete -EAGAIN handling support at random positions in
685
         * the stream.
686
         */
687
        if (url_ferror(pb) == AVERROR(EAGAIN))
688
            return AVERROR(EAGAIN);
689 5acdd6e6 Zdenek Kabelac
        if (!url_feof(pb))
690 bb270c08 Diego Biurrun
            av_log(s, AV_LOG_ERROR, "ff asf bad header %x  at:%"PRId64"\n", c, url_ftell(pb));
691 2a10020b Zdenek Kabelac
    }
692 0c0d17f3 Michael Niedermayer
    if ((c & 0x8f) == 0x82) {
693 0fd9e6f3 Michael Niedermayer
        if (d || e) {
694 5acdd6e6 Zdenek Kabelac
            if (!url_feof(pb))
695 bb270c08 Diego Biurrun
                av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
696 f7835507 Michael Niedermayer
            return -1;
697 bb270c08 Diego Biurrun
        }
698 0c0d17f3 Michael Niedermayer
        c= get_byte(pb);
699 0fd9e6f3 Michael Niedermayer
        d= get_byte(pb);
700 0c0d17f3 Michael Niedermayer
        rsize+=3;
701
    }else{
702
        url_fseek(pb, -1, SEEK_CUR); //FIXME
703 de6d9b64 Fabrice Bellard
    }
704 2a10020b Zdenek Kabelac
705 0c0d17f3 Michael Niedermayer
    asf->packet_flags    = c;
706
    asf->packet_property = d;
707 2a10020b Zdenek Kabelac
708 91d19d47 Ronald S. Bultje
    DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
709 2a10020b Zdenek Kabelac
    DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
710
    DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
711
712 5b11e081 Michael Niedermayer
    //the following checks prevent overflows and infinite loops
713 96f0731a Reimar Döffinger
    if(!packet_length || packet_length >= (1U<<29)){
714 5b11e081 Michael Niedermayer
        av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, url_ftell(pb));
715 ceba96cb Michael Niedermayer
        return -1;
716 5b11e081 Michael Niedermayer
    }
717 92216453 Michael Niedermayer
    if(padsize >= packet_length){
718 5b11e081 Michael Niedermayer
        av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, url_ftell(pb));
719 ceba96cb Michael Niedermayer
        return -1;
720 5b11e081 Michael Niedermayer
    }
721
722 2a10020b Zdenek Kabelac
    asf->packet_timestamp = get_le32(pb);
723 de6d9b64 Fabrice Bellard
    get_le16(pb); /* duration */
724 2141dc37 Zdenek Kabelac
    // rsize has at least 11 bytes which have to be present
725 2a10020b Zdenek Kabelac
726
    if (asf->packet_flags & 0x01) {
727 bb270c08 Diego Biurrun
        asf->packet_segsizetype = get_byte(pb); rsize++;
728 2a10020b Zdenek Kabelac
        asf->packet_segments = asf->packet_segsizetype & 0x3f;
729
    } else {
730 bb270c08 Diego Biurrun
        asf->packet_segments = 1;
731 2a10020b Zdenek Kabelac
        asf->packet_segsizetype = 0x80;
732
    }
733
    asf->packet_size_left = packet_length - padsize - rsize;
734 acbe6cfa Zdenek Kabelac
    if (packet_length < asf->hdr.min_pktsize)
735
        padsize += asf->hdr.min_pktsize - packet_length;
736 2a10020b Zdenek Kabelac
    asf->packet_padsize = padsize;
737 dfd2a005 Luca Barbato
    av_dlog(s, "packet: size=%d padsize=%d  left=%d\n", s->packet_size, asf->packet_padsize, asf->packet_size_left);
738 de6d9b64 Fabrice Bellard
    return 0;
739
}
740
741 ae60a857 Michael Niedermayer
/**
742
 *
743
 * @return <0 if error
744
 */
745 d7fb5a18 Ronald S. Bultje
static int asf_read_frame_header(AVFormatContext *s, ByteIOContext *pb){
746 ae60a857 Michael Niedermayer
    ASFContext *asf = s->priv_data;
747
    int rsize = 1;
748
    int num = get_byte(pb);
749 559a55ad Michael Niedermayer
    int64_t ts0, ts1;
750 ae60a857 Michael Niedermayer
751
    asf->packet_segments--;
752
    asf->packet_key_frame = num >> 7;
753
    asf->stream_index = asf->asfid2avid[num & 0x7f];
754
    // sequence should be ignored!
755
    DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
756
    DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
757
    DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
758
//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);
759
    if (asf->packet_replic_size >= 8) {
760
        asf->packet_obj_size = get_le32(pb);
761
        if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
762
            av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
763
            return -1;
764
        }
765
        asf->packet_frag_timestamp = get_le32(pb); // timestamp
766 559a55ad Michael Niedermayer
        if(asf->packet_replic_size >= 8+38+4){
767
//            for(i=0; i<asf->packet_replic_size-8; i++)
768
//                av_log(s, AV_LOG_DEBUG, "%02X ",get_byte(pb));
769
//            av_log(s, AV_LOG_DEBUG, "\n");
770
            url_fskip(pb, 10);
771
            ts0= get_le64(pb);
772
            ts1= get_le64(pb);
773
            url_fskip(pb, 12);
774
            get_le32(pb);
775
            url_fskip(pb, asf->packet_replic_size - 8 - 38 - 4);
776
            if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
777
            else         asf->packet_frag_timestamp= AV_NOPTS_VALUE;
778
        }else
779
            url_fskip(pb, asf->packet_replic_size - 8);
780 ae60a857 Michael Niedermayer
        rsize += asf->packet_replic_size; // FIXME - check validity
781
    } else if (asf->packet_replic_size==1){
782 efc90ebf Reimar Döffinger
        // multipacket - frag_offset is beginning timestamp
783 ae60a857 Michael Niedermayer
        asf->packet_time_start = asf->packet_frag_offset;
784
        asf->packet_frag_offset = 0;
785
        asf->packet_frag_timestamp = asf->packet_timestamp;
786
787
        asf->packet_time_delta = get_byte(pb);
788
        rsize++;
789
    }else if(asf->packet_replic_size!=0){
790
        av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
791
        return -1;
792
    }
793
    if (asf->packet_flags & 0x01) {
794
        DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
795 13f7e4b9 Michael Niedermayer
        if(asf->packet_frag_size > asf->packet_size_left - rsize){
796
            av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid\n");
797
            return -1;
798
        }
799 ae60a857 Michael Niedermayer
        //printf("Fragsize %d\n", asf->packet_frag_size);
800
    } else {
801
        asf->packet_frag_size = asf->packet_size_left - rsize;
802
        //printf("Using rest  %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
803
    }
804
    if (asf->packet_replic_size == 1) {
805
        asf->packet_multi_size = asf->packet_frag_size;
806
        if (asf->packet_multi_size > asf->packet_size_left)
807
            return -1;
808
    }
809
    asf->packet_size_left -= rsize;
810
    //printf("___objsize____  %d   %d    rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
811
812
    return 0;
813
}
814
815 277a28e8 Ronald S. Bultje
/**
816
 * Parse data from individual ASF packets (which were previously loaded
817
 * with asf_get_packet()).
818
 * @param s demux context
819
 * @param pb context to read data from
820
 * @param pkt pointer to store packet data into
821 32e543f8 Benoit Fouet
 * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
822 277a28e8 Ronald S. Bultje
 *          packets need to be loaded (through asf_get_packet())
823
 */
824
static int ff_asf_parse_packet(AVFormatContext *s, ByteIOContext *pb, AVPacket *pkt)
825 de6d9b64 Fabrice Bellard
{
826
    ASFContext *asf = s->priv_data;
827 2a10020b Zdenek Kabelac
    ASFStream *asf_st = 0;
828
    for (;;) {
829 4172951b Reimar Döffinger
        int ret;
830 ceba96cb Michael Niedermayer
        if(url_feof(pb))
831 799a0722 Ronald S. Bultje
            return AVERROR_EOF;
832 bb270c08 Diego Biurrun
        if (asf->packet_size_left < FRAME_HEADER_SIZE
833
            || asf->packet_segments < 1) {
834
            //asf->packet_size_left <= asf->packet_padsize) {
835
            int ret = asf->packet_size_left + asf->packet_padsize;
836 949b1a13 Steve L'Homme
            //printf("PacketLeftSize:%d  Pad:%d Pos:%"PRId64"\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb));
837 5b11e081 Michael Niedermayer
            assert(ret>=0);
838 bb270c08 Diego Biurrun
            /* fail safe */
839
            url_fskip(pb, ret);
840 b04d5d31 Michael Niedermayer
841 022f0b7c Ronald S. Bultje
            asf->packet_pos= url_ftell(pb);
842 b6eaae39 Kohn Emil Dan
            if (asf->data_object_size != (uint64_t)-1 &&
843
                (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
844 1d0036b0 Ronald S. Bultje
                return AVERROR_EOF; /* Do not exceed the size of the data object */
845 f27544fd Ronald S. Bultje
            return 1;
846 bb270c08 Diego Biurrun
        }
847
        if (asf->packet_time_start == 0) {
848 b0d75d60 Ronald S. Bultje
            if(asf_read_frame_header(s, pb) < 0){
849 ae60a857 Michael Niedermayer
                asf->packet_segments= 0;
850 236116cb Michael Niedermayer
                continue;
851 1305a9d5 Michael Niedermayer
            }
852 bb270c08 Diego Biurrun
            if (asf->stream_index < 0
853 f3356e9c Michael Niedermayer
                || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
854
                || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
855
                ) {
856 1359c2d4 Zdenek Kabelac
                asf->packet_time_start = 0;
857 bb270c08 Diego Biurrun
                /* unhandled packet (should not happen) */
858
                url_fskip(pb, asf->packet_frag_size);
859
                asf->packet_size_left -= asf->packet_frag_size;
860 b9866ebc Michael Niedermayer
                if(asf->stream_index < 0)
861 ae60a857 Michael Niedermayer
                    av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
862 1359c2d4 Zdenek Kabelac
                continue;
863 bb270c08 Diego Biurrun
            }
864
            asf->asf_st = s->streams[asf->stream_index]->priv_data;
865
        }
866
        asf_st = asf->asf_st;
867
868
        if (asf->packet_replic_size == 1) {
869 efc90ebf Reimar Döffinger
            // frag_offset is here used as the beginning timestamp
870 bb270c08 Diego Biurrun
            asf->packet_frag_timestamp = asf->packet_time_start;
871
            asf->packet_time_start += asf->packet_time_delta;
872
            asf->packet_obj_size = asf->packet_frag_size = get_byte(pb);
873
            asf->packet_size_left--;
874 2141dc37 Zdenek Kabelac
            asf->packet_multi_size--;
875 bb270c08 Diego Biurrun
            if (asf->packet_multi_size < asf->packet_obj_size)
876
            {
877
                asf->packet_time_start = 0;
878
                url_fskip(pb, asf->packet_multi_size);
879
                asf->packet_size_left -= asf->packet_multi_size;
880 2141dc37 Zdenek Kabelac
                continue;
881 bb270c08 Diego Biurrun
            }
882
            asf->packet_multi_size -= asf->packet_obj_size;
883
            //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);
884
        }
885 60ff1c44 Michael Niedermayer
        if(   /*asf->packet_frag_size == asf->packet_obj_size*/
886
              asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size
887
           && asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size){
888
            av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
889
                asf_st->frag_offset, asf->packet_frag_size,
890
                asf->packet_obj_size, asf_st->pkt.size);
891
            asf->packet_obj_size= asf_st->pkt.size;
892
        }
893
894 f8f0a0bd Michael Niedermayer
        if (   asf_st->pkt.size != asf->packet_obj_size
895
            || asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) { //FIXME is this condition sufficient?
896 200026aa Michael Niedermayer
            if(asf_st->pkt.data){
897
                av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, new %d\n", asf_st->pkt.size, asf->packet_obj_size);
898
                asf_st->frag_offset = 0;
899
                av_free_packet(&asf_st->pkt);
900
            }
901 bb270c08 Diego Biurrun
            /* new packet */
902
            av_new_packet(&asf_st->pkt, asf->packet_obj_size);
903
            asf_st->seq = asf->packet_seq;
904 eb63b051 Baptiste Coudurier
            asf_st->pkt.dts = asf->packet_frag_timestamp;
905 bb270c08 Diego Biurrun
            asf_st->pkt.stream_index = asf->stream_index;
906 115329f1 Diego Biurrun
            asf_st->pkt.pos =
907
            asf_st->packet_pos= asf->packet_pos;
908
//printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
909 cc947f04 Jean-Daniel Dupas
//asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & AV_PKT_FLAG_KEY,
910 72415b2a Stefano Sabatini
//s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO, asf->packet_obj_size);
911
            if (s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
912 bb270c08 Diego Biurrun
                asf->packet_key_frame = 1;
913
            if (asf->packet_key_frame)
914 cc947f04 Jean-Daniel Dupas
                asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
915 bb270c08 Diego Biurrun
        }
916
917
        /* read data */
918
        //printf("READ PACKET s:%d  os:%d  o:%d,%d  l:%d   DATA:%p\n",
919 91d19d47 Ronald S. Bultje
        //       s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
920 bb270c08 Diego Biurrun
        //       asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
921
        asf->packet_size_left -= asf->packet_frag_size;
922
        if (asf->packet_size_left < 0)
923 2141dc37 Zdenek Kabelac
            continue;
924 4c71d727 Michael Niedermayer
925
        if(   asf->packet_frag_offset >= asf_st->pkt.size
926
           || asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset){
927
            av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
928
                asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
929
            continue;
930
        }
931
932 4172951b Reimar Döffinger
        ret = get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
933
                         asf->packet_frag_size);
934 bf09a019 Reimar Döffinger
        if (ret != asf->packet_frag_size) {
935
            if (ret < 0 || asf->packet_frag_offset + ret == 0)
936
                return ret < 0 ? ret : AVERROR_EOF;
937
            if (asf_st->ds_span > 1) {
938
                // scrambling, we can either drop it completely or fill the remainder
939
                // TODO: should we fill the whole packet instead of just the current
940
                // fragment?
941
                memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
942
                       asf->packet_frag_size - ret);
943
                ret = asf->packet_frag_size;
944
            } else
945
                // no scrambling, so we can return partial packets
946
                av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
947
        }
948 72be7db4 Reimar Döffinger
        if (s->key && s->keylen == 20)
949
            ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
950 bf09a019 Reimar Döffinger
                            ret);
951
        asf_st->frag_offset += ret;
952 bb270c08 Diego Biurrun
        /* test if whole packet is read */
953
        if (asf_st->frag_offset == asf_st->pkt.size) {
954 da0b94f5 Michael Niedermayer
            //workaround for macroshit radio DVR-MS files
955
            if(   s->streams[asf->stream_index]->codec->codec_id == CODEC_ID_MPEG2VIDEO
956
               && asf_st->pkt.size > 100){
957
                int i;
958
                for(i=0; i<asf_st->pkt.size && !asf_st->pkt.data[i]; i++);
959
                if(i == asf_st->pkt.size){
960
                    av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
961
                    asf_st->frag_offset = 0;
962
                    av_free_packet(&asf_st->pkt);
963
                    continue;
964
                }
965
            }
966
967 bb270c08 Diego Biurrun
            /* return packet */
968
            if (asf_st->ds_span > 1) {
969 05219463 Michael Niedermayer
              if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span){
970 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);
971 05219463 Michael Niedermayer
              }else{
972 bb270c08 Diego Biurrun
                /* packet descrambling */
973 3c3ef81b Reimar Döffinger
                uint8_t *newdata = av_malloc(asf_st->pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
974 bb270c08 Diego Biurrun
                if (newdata) {
975
                    int offset = 0;
976 3c3ef81b Reimar Döffinger
                    memset(newdata + asf_st->pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
977 bb270c08 Diego Biurrun
                    while (offset < asf_st->pkt.size) {
978
                        int off = offset / asf_st->ds_chunk_size;
979
                        int row = off / asf_st->ds_span;
980
                        int col = off % asf_st->ds_span;
981
                        int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
982
                        //printf("off:%d  row:%d  col:%d  idx:%d\n", off, row, col, idx);
983 05219463 Michael Niedermayer
984
                        assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
985
                        assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
986 bb270c08 Diego Biurrun
                        memcpy(newdata + offset,
987
                               asf_st->pkt.data + idx * asf_st->ds_chunk_size,
988
                               asf_st->ds_chunk_size);
989
                        offset += asf_st->ds_chunk_size;
990
                    }
991
                    av_free(asf_st->pkt.data);
992
                    asf_st->pkt.data = newdata;
993
                }
994 05219463 Michael Niedermayer
              }
995 bb270c08 Diego Biurrun
            }
996
            asf_st->frag_offset = 0;
997 b83e83b9 Michael Niedermayer
            *pkt= asf_st->pkt;
998 bb270c08 Diego Biurrun
            //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
999
            asf_st->pkt.size = 0;
1000
            asf_st->pkt.data = 0;
1001
            break; // packet completed
1002
        }
1003 de6d9b64 Fabrice Bellard
    }
1004
    return 0;
1005
}
1006
1007 f27544fd Ronald S. Bultje
static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1008
{
1009
    ASFContext *asf = s->priv_data;
1010
1011
    for (;;) {
1012
        int ret;
1013
1014
        /* parse cached packets, if any */
1015 1d217cdb Ronald S. Bultje
        if ((ret = ff_asf_parse_packet(s, s->pb, pkt)) <= 0)
1016 f27544fd Ronald S. Bultje
            return ret;
1017 1d217cdb Ronald S. Bultje
        if ((ret = ff_asf_get_packet(s, s->pb)) < 0)
1018 f27544fd Ronald S. Bultje
            assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
1019
        asf->packet_time_start = 0;
1020
    }
1021
1022
    return 0;
1023
}
1024
1025 38376f00 Kevin Kuphal
// Added to support seeking after packets have been read
1026
// If information is not reset, read_packet fails due to
1027
// leftover information from previous reads
1028
static void asf_reset_header(AVFormatContext *s)
1029
{
1030
    ASFContext *asf = s->priv_data;
1031 82b9e4a2 Michael Niedermayer
    ASFStream *asf_st;
1032
    int i;
1033 38376f00 Kevin Kuphal
1034
    asf->packet_size_left = 0;
1035
    asf->packet_segments = 0;
1036
    asf->packet_flags = 0;
1037
    asf->packet_property = 0;
1038
    asf->packet_timestamp = 0;
1039
    asf->packet_segsizetype = 0;
1040
    asf->packet_segments = 0;
1041
    asf->packet_seq = 0;
1042
    asf->packet_replic_size = 0;
1043
    asf->packet_key_frame = 0;
1044
    asf->packet_padsize = 0;
1045
    asf->packet_frag_offset = 0;
1046
    asf->packet_frag_size = 0;
1047
    asf->packet_frag_timestamp = 0;
1048
    asf->packet_multi_size = 0;
1049
    asf->packet_obj_size = 0;
1050
    asf->packet_time_delta = 0;
1051
    asf->packet_time_start = 0;
1052 115329f1 Diego Biurrun
1053 82b9e4a2 Michael Niedermayer
    for(i=0; i<s->nb_streams; i++){
1054
        asf_st= s->streams[i]->priv_data;
1055
        av_free_packet(&asf_st->pkt);
1056
        asf_st->frag_offset=0;
1057
        asf_st->seq=0;
1058
    }
1059
    asf->asf_st= NULL;
1060 38376f00 Kevin Kuphal
}
1061
1062 5d25d2a9 Diego Biurrun
static int asf_read_close(AVFormatContext *s)
1063
{
1064
    int i;
1065
1066
    asf_reset_header(s);
1067
    for(i=0;i<s->nb_streams;i++) {
1068
        AVStream *st = s->streams[i];
1069
        av_free(st->codec->palctrl);
1070
    }
1071
    return 0;
1072
}
1073
1074 8d14a25c Michael Niedermayer
static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
1075 38376f00 Kevin Kuphal
{
1076
    AVPacket pkt1, *pkt = &pkt1;
1077 82b9e4a2 Michael Niedermayer
    ASFStream *asf_st;
1078 580fb5e7 Michael Niedermayer
    int64_t pts;
1079 82b9e4a2 Michael Niedermayer
    int64_t pos= *ppos;
1080 3e9245a9 Michael Niedermayer
    int i;
1081 30cdfb49 Måns Rullgård
    int64_t start_pos[ASF_MAX_STREAMS];
1082 115329f1 Diego Biurrun
1083 3e9245a9 Michael Niedermayer
    for(i=0; i<s->nb_streams; i++){
1084
        start_pos[i]= pos;
1085
    }
1086 115329f1 Diego Biurrun
1087 68a10b40 Reimar Döffinger
    if (s->packet_size > 0)
1088 b098b309 Reimar Döffinger
        pos= (pos+s->packet_size-1-s->data_offset)/s->packet_size*s->packet_size+ s->data_offset;
1089 8d14a25c Michael Niedermayer
    *ppos= pos;
1090 899681cd Björn Axelsson
    url_fseek(s->pb, pos, SEEK_SET);
1091 115329f1 Diego Biurrun
1092 82b9e4a2 Michael Niedermayer
//printf("asf_read_pts\n");
1093
    asf_reset_header(s);
1094 3e9245a9 Michael Niedermayer
    for(;;){
1095 82b9e4a2 Michael Niedermayer
        if (av_read_frame(s, pkt) < 0){
1096 0b93103a Michael Niedermayer
            av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
1097 bb270c08 Diego Biurrun
            return AV_NOPTS_VALUE;
1098 82b9e4a2 Michael Niedermayer
        }
1099 115329f1 Diego Biurrun
1100 38cf2a72 Michael Niedermayer
        pts= pkt->pts;
1101 580fb5e7 Michael Niedermayer
1102
        av_free_packet(pkt);
1103 cc947f04 Jean-Daniel Dupas
        if(pkt->flags&AV_PKT_FLAG_KEY){
1104 3e9245a9 Michael Niedermayer
            i= pkt->stream_index;
1105
1106
            asf_st= s->streams[i]->priv_data;
1107
1108 91d19d47 Ronald S. Bultje
//            assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
1109 8d14a25c Michael Niedermayer
            pos= asf_st->packet_pos;
1110 09646bab Michael Niedermayer
1111 52e54612 Michael Niedermayer
            av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
1112 8d14a25c Michael Niedermayer
            start_pos[i]= asf_st->packet_pos + 1;
1113 115329f1 Diego Biurrun
1114 3e9245a9 Michael Niedermayer
            if(pkt->stream_index == stream_index)
1115
               break;
1116
        }
1117
    }
1118
1119
    *ppos= pos;
1120 949b1a13 Steve L'Homme
//printf("found keyframe at %"PRId64" stream %d stamp:%"PRId64"\n", *ppos, stream_index, pts);
1121 580fb5e7 Michael Niedermayer
1122
    return pts;
1123 38376f00 Kevin Kuphal
}
1124
1125 56c96a22 Michael Niedermayer
static void asf_build_simple_index(AVFormatContext *s, int stream_index)
1126
{
1127 b8b00d94 Reimar Döffinger
    ff_asf_guid g;
1128 56c96a22 Michael Niedermayer
    ASFContext *asf = s->priv_data;
1129 d728e3cd Michael Niedermayer
    int64_t current_pos= url_ftell(s->pb);
1130 56c96a22 Michael Niedermayer
    int i;
1131
1132 899681cd Björn Axelsson
    url_fseek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET);
1133 a750050f Peter Ross
    ff_get_guid(s->pb, &g);
1134 77b7c334 Stefan Gehrer
1135
    /* the data object can be followed by other top-level objects,
1136
       skip them until the simple index object is reached */
1137 a750050f Peter Ross
    while (ff_guidcmp(&g, &index_guid)) {
1138 77b7c334 Stefan Gehrer
        int64_t gsize= get_le64(s->pb);
1139
        if (gsize < 24 || url_feof(s->pb)) {
1140
            url_fseek(s->pb, current_pos, SEEK_SET);
1141
            return;
1142
        }
1143
        url_fseek(s->pb, gsize-24, SEEK_CUR);
1144 a750050f Peter Ross
        ff_get_guid(s->pb, &g);
1145 77b7c334 Stefan Gehrer
    }
1146
1147
    {
1148 134271c2 Michael Niedermayer
        int64_t itime, last_pos=-1;
1149 d728e3cd Michael Niedermayer
        int pct, ict;
1150 dc1ed614 Diego Biurrun
        int64_t av_unused gsize= get_le64(s->pb);
1151 a750050f Peter Ross
        ff_get_guid(s->pb, &g);
1152 899681cd Björn Axelsson
        itime=get_le64(s->pb);
1153
        pct=get_le32(s->pb);
1154
        ict=get_le32(s->pb);
1155 871e62e5 Benoit Fouet
        av_log(s, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
1156 56c96a22 Michael Niedermayer
1157
        for (i=0;i<ict;i++){
1158 899681cd Björn Axelsson
            int pktnum=get_le32(s->pb);
1159
            int pktct =get_le16(s->pb);
1160 91d19d47 Ronald S. Bultje
            int64_t pos      = s->data_offset + s->packet_size*(int64_t)pktnum;
1161 d728e3cd Michael Niedermayer
            int64_t index_pts= av_rescale(itime, i, 10000);
1162 56c96a22 Michael Niedermayer
1163 134271c2 Michael Niedermayer
            if(pos != last_pos){
1164 d728e3cd Michael Niedermayer
            av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d\n", pktnum, pktct);
1165 91d19d47 Ronald S. Bultje
            av_add_index_entry(s->streams[stream_index], pos, index_pts, s->packet_size, 0, AVINDEX_KEYFRAME);
1166 134271c2 Michael Niedermayer
            last_pos=pos;
1167
            }
1168 56c96a22 Michael Niedermayer
        }
1169 52e54612 Michael Niedermayer
        asf->index_read= 1;
1170 56c96a22 Michael Niedermayer
    }
1171 899681cd Björn Axelsson
    url_fseek(s->pb, current_pos, SEEK_SET);
1172 56c96a22 Michael Niedermayer
}
1173
1174 3ba1438d Michael Niedermayer
static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
1175 2a10020b Zdenek Kabelac
{
1176 afda223c Fabrice Bellard
    ASFContext *asf = s->priv_data;
1177 56c96a22 Michael Niedermayer
    AVStream *st = s->streams[stream_index];
1178
    int64_t pos;
1179
    int index;
1180 115329f1 Diego Biurrun
1181 91d19d47 Ronald S. Bultje
    if (s->packet_size <= 0)
1182 38376f00 Kevin Kuphal
        return -1;
1183
1184 fa13095a Björn Axelsson
    /* Try using the protocol's read_seek if available */
1185 fd2982a0 Aurelien Jacobs
    if(s->pb) {
1186 fa13095a Björn Axelsson
        int ret = av_url_read_fseek(s->pb, stream_index, pts, flags);
1187
        if(ret >= 0)
1188
            asf_reset_header(s);
1189 fd2982a0 Aurelien Jacobs
        if (ret != AVERROR(ENOSYS))
1190 cd31f8dc Aurelien Jacobs
            return ret;
1191 fa13095a Björn Axelsson
    }
1192
1193 52e54612 Michael Niedermayer
    if (!asf->index_read)
1194 56c96a22 Michael Niedermayer
        asf_build_simple_index(s, stream_index);
1195
1196 52e54612 Michael Niedermayer
    if(!(asf->index_read && st->index_entries)){
1197 56c96a22 Michael Niedermayer
        if(av_seek_frame_binary(s, stream_index, pts, flags)<0)
1198
            return -1;
1199
    }else{
1200
        index= av_index_search_timestamp(st, pts, flags);
1201
        if(index<0)
1202
            return -1;
1203
1204
        /* find the position */
1205
        pos = st->index_entries[index].pos;
1206
1207
    // various attempts to find key frame have failed so far
1208
    //    asf_reset_header(s);
1209 899681cd Björn Axelsson
    //    url_fseek(s->pb, pos, SEEK_SET);
1210 56c96a22 Michael Niedermayer
    //    key_pos = pos;
1211
    //     for(i=0;i<16;i++){
1212 899681cd Björn Axelsson
    //         pos = url_ftell(s->pb);
1213 56c96a22 Michael Niedermayer
    //         if (av_read_frame(s, &pkt) < 0){
1214
    //             av_log(s, AV_LOG_INFO, "seek failed\n");
1215
    //             return -1;
1216
    //         }
1217
    //         asf_st = s->streams[stream_index]->priv_data;
1218
    //         pos += st->parser->frame_offset;
1219
    //
1220
    //         if (pkt.size > b) {
1221
    //             b = pkt.size;
1222
    //             key_pos = pos;
1223
    //         }
1224
    //
1225
    //         av_free_packet(&pkt);
1226
    //     }
1227
1228
        /* do the seek */
1229 871e62e5 Benoit Fouet
        av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
1230 899681cd Björn Axelsson
        url_fseek(s->pb, pos, SEEK_SET);
1231 56c96a22 Michael Niedermayer
    }
1232 38376f00 Kevin Kuphal
    asf_reset_header(s);
1233
    return 0;
1234 2a10020b Zdenek Kabelac
}
1235
1236 c6610a21 Diego Elio Pettenò
AVInputFormat ff_asf_demuxer = {
1237 c9a65ca8 Fabrice Bellard
    "asf",
1238 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("ASF format"),
1239 c9a65ca8 Fabrice Bellard
    sizeof(ASFContext),
1240
    asf_probe,
1241
    asf_read_header,
1242
    asf_read_packet,
1243
    asf_read_close,
1244 2a10020b Zdenek Kabelac
    asf_read_seek,
1245 8d14a25c Michael Niedermayer
    asf_read_pts,
1246 c9a65ca8 Fabrice Bellard
};