FATE: drop the last truncated frame from the wmapro tests
[libav.git] / libavformat / asfdec.c
CommitLineData
de6d9b64 1/*
7fbde343 2 * ASF compatible demuxer
406792e7 3 * Copyright (c) 2000, 2001 Fabrice Bellard
de6d9b64 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
245976da 21
d92024f1
DB
22#include <inttypes.h>
23
72eaba5e 24#include "libavutil/attributes.h"
1ef0e8a6 25#include "libavutil/avassert.h"
48a4ffa7 26#include "libavutil/avstring.h"
b122c651 27#include "libavutil/bswap.h"
245976da 28#include "libavutil/common.h"
d2d67e42 29#include "libavutil/dict.h"
7950e519 30#include "libavutil/internal.h"
0ebcdf5c 31#include "libavutil/mathematics.h"
75b7feae 32#include "libavutil/opt.h"
de6d9b64 33#include "avformat.h"
933e90a6 34#include "avio_internal.h"
48a4ffa7 35#include "avlanguage.h"
5e745cef 36#include "id3v2.h"
48a4ffa7 37#include "internal.h"
9d9f4119 38#include "riff.h"
542993b0 39#include "asf.h"
72be7db4 40#include "asfcrypt.h"
de6d9b64 41
daf8cf35 42typedef struct ASFContext {
75b7feae 43 const AVClass *class;
4bc328a2
AK
44 int asfid2avid[128]; ///< conversion table from asf ID 2 AVStream ID
45 ASFStream streams[128]; ///< it's max number and it's not that big
46 uint32_t stream_bitrates[128]; ///< max number of streams, bitrate for each (for streaming)
d42b0972 47 AVRational dar[128];
4bc328a2
AK
48 char stream_languages[128][6]; ///< max number of streams, language for each (RFC1766, e.g. en-US)
49 /* non streamed additonnal info */
4bc328a2
AK
50 /* packet filling */
51 int packet_size_left;
4bc328a2
AK
52 /* only for reading */
53 uint64_t data_offset; ///< beginning of the first data packet
54 uint64_t data_object_offset; ///< data object offset (excl. GUID & size)
55 uint64_t data_object_size; ///< size of the data object
56 int index_read;
57
58 ASFMainHeader hdr;
59
60 int packet_flags;
61 int packet_property;
62 int packet_timestamp;
63 int packet_segsizetype;
64 int packet_segments;
65 int packet_seq;
66 int packet_replic_size;
67 int packet_key_frame;
68 int packet_padsize;
69 unsigned int packet_frag_offset;
70 unsigned int packet_frag_size;
71 int64_t packet_frag_timestamp;
72 int packet_multi_size;
73 int packet_obj_size;
74 int packet_time_delta;
75 int packet_time_start;
76 int64_t packet_pos;
77
78 int stream_index;
79
48a4ffa7 80 ASFStream *asf_st; ///< currently decoded stream
75b7feae
MS
81
82 int no_resync_search;
0af3b658 83 int export_xmp;
4bc328a2
AK
84} ASFContext;
85
75b7feae 86static const AVOption options[] = {
48a4ffa7 87 { "no_resync_search", "Don't try to resynchronize by looking for a certain optional start code", offsetof(ASFContext, no_resync_search), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
0af3b658 88 { "export_xmp", "Export full XMP metadata", offsetof(ASFContext, export_xmp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
75b7feae
MS
89 { NULL },
90};
91
92static const AVClass asf_class = {
93 .class_name = "asf demuxer",
94 .item_name = av_default_item_name,
95 .option = options,
96 .version = LIBAVUTIL_VERSION_INT,
97};
98
580fb5e7
MN
99#undef NDEBUG
100#include <assert.h>
101
30cdfb49 102#define ASF_MAX_STREAMS 127
615b92fd 103#define FRAME_HEADER_SIZE 17
115329f1 104// Fix Me! FRAME_HEADER_SIZE may be different.
615b92fd 105
b8b00d94 106static const ff_asf_guid index_guid = {
24c14d6d 107 0x90, 0x08, 0x00, 0x33, 0xb1, 0xe5, 0xcf, 0x11, 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb
de6d9b64
FB
108};
109
91c9aa09 110#ifdef DEBUG
b8b00d94 111static const ff_asf_guid stream_bitrate_guid = { /* (http://get.to/sdp) */
0e3b6a6f
RM
112 0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
113};
de6d9b64 114
48a4ffa7
DB
115#define PRINT_IF_GUID(g, cmp) \
116 if (!ff_guidcmp(g, &cmp)) \
1a3eb042 117 av_log(NULL, AV_LOG_TRACE, "(GUID: %s) ", # cmp)
5082f759 118
36fab50e 119static void print_guid(ff_asf_guid *g)
de6d9b64
FB
120{
121 int i;
17af0525
AJ
122 PRINT_IF_GUID(g, ff_asf_header);
123 else PRINT_IF_GUID(g, ff_asf_file_header);
124 else PRINT_IF_GUID(g, ff_asf_stream_header);
125 else PRINT_IF_GUID(g, ff_asf_audio_stream);
126 else PRINT_IF_GUID(g, ff_asf_audio_conceal_none);
127 else PRINT_IF_GUID(g, ff_asf_video_stream);
128 else PRINT_IF_GUID(g, ff_asf_video_conceal_none);
129 else PRINT_IF_GUID(g, ff_asf_command_stream);
130 else PRINT_IF_GUID(g, ff_asf_comment_header);
131 else PRINT_IF_GUID(g, ff_asf_codec_comment_header);
132 else PRINT_IF_GUID(g, ff_asf_codec_comment1_header);
133 else PRINT_IF_GUID(g, ff_asf_data_header);
5082f759 134 else PRINT_IF_GUID(g, index_guid);
17af0525
AJ
135 else PRINT_IF_GUID(g, ff_asf_head1_guid);
136 else PRINT_IF_GUID(g, ff_asf_head2_guid);
137 else PRINT_IF_GUID(g, ff_asf_my_guid);
138 else PRINT_IF_GUID(g, ff_asf_ext_stream_header);
139 else PRINT_IF_GUID(g, ff_asf_extended_content_header);
140 else PRINT_IF_GUID(g, ff_asf_ext_stream_embed_stream_header);
141 else PRINT_IF_GUID(g, ff_asf_ext_stream_audio_stream);
142 else PRINT_IF_GUID(g, ff_asf_metadata_header);
84b721db 143 else PRINT_IF_GUID(g, ff_asf_metadata_library_header);
88b51ea9 144 else PRINT_IF_GUID(g, ff_asf_marker_header);
0e3b6a6f 145 else PRINT_IF_GUID(g, stream_bitrate_guid);
5f1ac36d 146 else PRINT_IF_GUID(g, ff_asf_language_guid);
5082f759 147 else
1a3eb042 148 av_log(NULL, AV_LOG_TRACE, "(GUID: unknown) ");
48a4ffa7 149 for (i = 0; i < 16; i++)
1a3eb042
VG
150 av_log(NULL, AV_LOG_TRACE, " 0x%02x,", (*g)[i]);
151 av_log(NULL, AV_LOG_TRACE, "}\n");
de6d9b64 152}
6df84c3a 153#undef PRINT_IF_GUID
f94981ea
MR
154#else
155#define print_guid(g)
de6d9b64
FB
156#endif
157
c9a65ca8
FB
158static int asf_probe(AVProbeData *pd)
159{
c9a65ca8 160 /* check file header */
a750050f 161 if (!ff_guidcmp(pd->buf, &ff_asf_header))
c9a65ca8
FB
162 return AVPROBE_SCORE_MAX;
163 else
164 return 0;
165}
166
61f9ad2d
VP
167/* size of type 2 (BOOL) is 32bit for "Extended Content Description Object"
168 * but 16 bit for "Metadata Object" and "Metadata Library Object" */
169static int get_value(AVIOContext *pb, int type, int type2_size)
48a4ffa7
DB
170{
171 switch (type) {
172 case 2:
61f9ad2d 173 return (type2_size == 32) ? avio_rl32(pb) : avio_rl16(pb);
48a4ffa7
DB
174 case 3:
175 return avio_rl32(pb);
176 case 4:
177 return avio_rl64(pb);
178 case 5:
179 return avio_rl16(pb);
180 default:
181 return INT_MIN;
30e88789
MN
182 }
183}
184
5e745cef
AK
185/* MSDN claims that this should be "compatible with the ID3 frame, APIC",
186 * but in reality this is only loosely similar */
187static int asf_read_picture(AVFormatContext *s, int len)
188{
48a4ffa7 189 AVPacket pkt = { 0 };
5e745cef 190 const CodecMime *mime = ff_id3v2_mime_tags;
48a4ffa7 191 enum AVCodecID id = AV_CODEC_ID_NONE;
5e745cef
AK
192 char mimetype[64];
193 uint8_t *desc = NULL;
5e745cef
AK
194 AVStream *st = NULL;
195 int ret, type, picsize, desc_len;
196
197 /* type + picsize + mime + desc */
198 if (len < 1 + 4 + 2 + 2) {
199 av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
200 return AVERROR_INVALIDDATA;
201 }
202
203 /* picture type */
204 type = avio_r8(s->pb);
205 len--;
206 if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
207 av_log(s, AV_LOG_WARNING, "Unknown attached picture type: %d.\n", type);
208 type = 0;
209 }
210
211 /* picture data size */
212 picsize = avio_rl32(s->pb);
48a4ffa7 213 len -= 4;
5e745cef
AK
214
215 /* picture MIME type */
216 len -= avio_get_str16le(s->pb, len, mimetype, sizeof(mimetype));
36ef5369 217 while (mime->id != AV_CODEC_ID_NONE) {
5e745cef
AK
218 if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
219 id = mime->id;
220 break;
221 }
222 mime++;
223 }
36ef5369 224 if (id == AV_CODEC_ID_NONE) {
5e745cef
AK
225 av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
226 mimetype);
227 return 0;
228 }
229
230 if (picsize >= len) {
231 av_log(s, AV_LOG_ERROR, "Invalid attached picture data size: %d >= %d.\n",
232 picsize, len);
233 return AVERROR_INVALIDDATA;
234 }
235
236 /* picture description */
237 desc_len = (len - picsize) * 2 + 1;
238 desc = av_malloc(desc_len);
239 if (!desc)
240 return AVERROR(ENOMEM);
241 len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len);
242
243 ret = av_get_packet(s->pb, &pkt, picsize);
244 if (ret < 0)
245 goto fail;
246
48a4ffa7 247 st = avformat_new_stream(s, NULL);
1ef0e8a6 248 if (!st) {
5e745cef
AK
249 ret = AVERROR(ENOMEM);
250 goto fail;
251 }
48a4ffa7
DB
252 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
253 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
254 st->codec->codec_id = id;
255 st->attached_pic = pkt;
5e745cef 256 st->attached_pic.stream_index = st->index;
48a4ffa7 257 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
5e745cef
AK
258
259 if (*desc)
260 av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
261 else
262 av_freep(&desc);
263
264 av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
265
266 return 0;
267
268fail:
5e745cef
AK
269 av_freep(&desc);
270 av_free_packet(&pkt);
271 return ret;
272}
273
f5fac6f7
VP
274static void get_id3_tag(AVFormatContext *s, int len)
275{
276 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
277
278 ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
279 if (id3v2_extra_meta)
280 ff_id3v2_parse_apic(s, &id3v2_extra_meta);
281 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
282}
283
61f9ad2d 284static void get_tag(AVFormatContext *s, const char *key, int type, int len, int type2_size)
20e2a7ae 285{
0af3b658
VG
286 ASFContext *asf = s->priv_data;
287 char *value = NULL;
a2704c97 288 int64_t off = avio_tell(s->pb);
12ad6671 289
48a4ffa7 290 if ((unsigned)len >= (UINT_MAX - 1) / 2)
12ad6671
MN
291 return;
292
0af3b658
VG
293 if (!asf->export_xmp && !strncmp(key, "xmp", 3))
294 goto finish;
295
48a4ffa7 296 value = av_malloc(2 * len + 1);
12ad6671 297 if (!value)
04295054 298 goto finish;
12ad6671 299
6538c0f1 300 if (type == 0) { // UTF16-LE
48a4ffa7 301 avio_get_str16le(s->pb, len, value, 2 * len + 1);
f5fac6f7
VP
302 } else if (type == 1) { // byte array
303 if (!strcmp(key, "WM/Picture")) { // handle cover art
304 asf_read_picture(s, len);
305 } else if (!strcmp(key, "ID3")) { // handle ID3 tag
306 get_id3_tag(s, len);
307 } else {
308 av_log(s, AV_LOG_VERBOSE, "Unsupported byte array in tag %s.\n", key);
309 }
310 goto finish;
6538c0f1 311 } else if (type > 1 && type <= 5) { // boolean or DWORD or QWORD or WORD
61f9ad2d 312 uint64_t num = get_value(s->pb, type, type2_size);
12ad6671 313 snprintf(value, len, "%"PRIu64, num);
84b721db
VP
314 } else if (type == 6) { // (don't) handle GUID
315 av_log(s, AV_LOG_DEBUG, "Unsupported GUID value in tag %s.\n", key);
316 goto finish;
20e2a7ae 317 } else {
48a4ffa7
DB
318 av_log(s, AV_LOG_DEBUG,
319 "Unsupported value type %d in tag %s.\n", type, key);
04295054 320 goto finish;
20e2a7ae 321 }
5cbd3b5d 322 if (*value)
d2d67e42 323 av_dict_set(&s->metadata, key, value, 0);
48a4ffa7 324
04295054 325finish:
fce44489 326 av_freep(&value);
6b4aa5da 327 avio_seek(s->pb, off + len, SEEK_SET);
20e2a7ae
AJ
328}
329
c1fea230
AK
330static int asf_read_file_properties(AVFormatContext *s, int64_t size)
331{
332 ASFContext *asf = s->priv_data;
ae628ec1 333 AVIOContext *pb = s->pb;
c1fea230
AK
334
335 ff_get_guid(pb, &asf->hdr.guid);
48a4ffa7
DB
336 asf->hdr.file_size = avio_rl64(pb);
337 asf->hdr.create_time = avio_rl64(pb);
b7effd4e 338 avio_rl64(pb); /* number of packets */
48a4ffa7
DB
339 asf->hdr.play_time = avio_rl64(pb);
340 asf->hdr.send_time = avio_rl64(pb);
341 asf->hdr.preroll = avio_rl32(pb);
342 asf->hdr.ignore = avio_rl32(pb);
343 asf->hdr.flags = avio_rl32(pb);
344 asf->hdr.min_pktsize = avio_rl32(pb);
345 asf->hdr.max_pktsize = avio_rl32(pb);
346 if (asf->hdr.min_pktsize >= (1U << 29))
6e57a02b 347 return AVERROR_INVALIDDATA;
48a4ffa7
DB
348 asf->hdr.max_bitrate = avio_rl32(pb);
349 s->packet_size = asf->hdr.max_pktsize;
c1fea230
AK
350
351 return 0;
352}
353
8bf6db1b
AK
354static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
355{
356 ASFContext *asf = s->priv_data;
ae628ec1 357 AVIOContext *pb = s->pb;
8bf6db1b
AK
358 AVStream *st;
359 ASFStream *asf_st;
360 ff_asf_guid g;
361 enum AVMediaType type;
362 int type_specific_size, sizeX;
8bf6db1b
AK
363 unsigned int tag1;
364 int64_t pos1, pos2, start_time;
48a4ffa7 365 int test_for_ext_stream_audio, is_dvr_ms_audio = 0;
8bf6db1b
AK
366
367 if (s->nb_streams == ASF_MAX_STREAMS) {
368 av_log(s, AV_LOG_ERROR, "too many streams\n");
369 return AVERROR(EINVAL);
370 }
371
a2704c97 372 pos1 = avio_tell(pb);
8bf6db1b 373
3b3bbdd3 374 st = avformat_new_stream(s, NULL);
8bf6db1b
AK
375 if (!st)
376 return AVERROR(ENOMEM);
c3f9ebf7 377 avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
8bf6db1b
AK
378 asf_st = av_mallocz(sizeof(ASFStream));
379 if (!asf_st)
380 return AVERROR(ENOMEM);
48a4ffa7 381 st->priv_data = asf_st;
de11ee90 382 st->start_time = 0;
48a4ffa7 383 start_time = asf->hdr.preroll;
8bf6db1b
AK
384
385 asf_st->stream_language_index = 128; // invalid stream index means no language info
386
48a4ffa7 387 if (!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
8bf6db1b 388 st->duration = asf->hdr.play_time /
48a4ffa7 389 (10000000 / 1000) - start_time;
8bf6db1b
AK
390 }
391 ff_get_guid(pb, &g);
392
393 test_for_ext_stream_audio = 0;
394 if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
395 type = AVMEDIA_TYPE_AUDIO;
396 } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
397 type = AVMEDIA_TYPE_VIDEO;
398 } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
48a4ffa7 399 type = AVMEDIA_TYPE_VIDEO;
36ef5369 400 st->codec->codec_id = AV_CODEC_ID_MJPEG;
8bf6db1b
AK
401 } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
402 type = AVMEDIA_TYPE_DATA;
403 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_embed_stream_header)) {
404 test_for_ext_stream_audio = 1;
48a4ffa7 405 type = AVMEDIA_TYPE_UNKNOWN;
8bf6db1b
AK
406 } else {
407 return -1;
408 }
409 ff_get_guid(pb, &g);
e65ab9d9 410 avio_skip(pb, 8); /* total_size */
b7effd4e
AK
411 type_specific_size = avio_rl32(pb);
412 avio_rl32(pb);
413 st->id = avio_rl16(pb) & 0x7f; /* stream id */
8bf6db1b
AK
414 // mapping of asf ID to AV stream ID;
415 asf->asfid2avid[st->id] = s->nb_streams - 1;
416
b7effd4e 417 avio_rl32(pb);
8bf6db1b
AK
418
419 if (test_for_ext_stream_audio) {
420 ff_get_guid(pb, &g);
421 if (!ff_guidcmp(&g, &ff_asf_ext_stream_audio_stream)) {
48a4ffa7
DB
422 type = AVMEDIA_TYPE_AUDIO;
423 is_dvr_ms_audio = 1;
8bf6db1b 424 ff_get_guid(pb, &g);
b7effd4e
AK
425 avio_rl32(pb);
426 avio_rl32(pb);
427 avio_rl32(pb);
8bf6db1b 428 ff_get_guid(pb, &g);
b7effd4e 429 avio_rl32(pb);
8bf6db1b
AK
430 }
431 }
432
433 st->codec->codec_type = type;
434 if (type == AVMEDIA_TYPE_AUDIO) {
ca402f32
MH
435 int ret = ff_get_wav_header(pb, st->codec, type_specific_size);
436 if (ret < 0)
437 return ret;
8bf6db1b
AK
438 if (is_dvr_ms_audio) {
439 // codec_id and codec_tag are unreliable in dvr_ms
440 // files. Set them later by probing stream.
48a4ffa7 441 st->codec->codec_id = AV_CODEC_ID_PROBE;
8bf6db1b
AK
442 st->codec->codec_tag = 0;
443 }
48a4ffa7 444 if (st->codec->codec_id == AV_CODEC_ID_AAC)
8bf6db1b 445 st->need_parsing = AVSTREAM_PARSE_NONE;
48a4ffa7 446 else
8bf6db1b 447 st->need_parsing = AVSTREAM_PARSE_FULL;
8bf6db1b 448 /* We have to init the frame size at some point .... */
a2704c97 449 pos2 = avio_tell(pb);
8bf6db1b 450 if (size >= (pos2 + 8 - pos1 + 24)) {
48a4ffa7 451 asf_st->ds_span = avio_r8(pb);
b7effd4e 452 asf_st->ds_packet_size = avio_rl16(pb);
48a4ffa7
DB
453 asf_st->ds_chunk_size = avio_rl16(pb);
454 avio_rl16(pb); // ds_data_size
455 avio_r8(pb); // ds_silence_data
8bf6db1b 456 }
8bf6db1b 457 if (asf_st->ds_span > 1) {
48a4ffa7
DB
458 if (!asf_st->ds_chunk_size ||
459 (asf_st->ds_packet_size / asf_st->ds_chunk_size <= 1) ||
460 asf_st->ds_packet_size % asf_st->ds_chunk_size)
461 asf_st->ds_span = 0; // disable descrambling
8bf6db1b 462 }
8bf6db1b 463 } else if (type == AVMEDIA_TYPE_VIDEO &&
48a4ffa7 464 size - (avio_tell(pb) - pos1 + 24) >= 51) {
b7effd4e
AK
465 avio_rl32(pb);
466 avio_rl32(pb);
467 avio_r8(pb);
468 avio_rl16(pb); /* size */
48a4ffa7
DB
469 sizeX = avio_rl32(pb); /* size */
470 st->codec->width = avio_rl32(pb);
b7effd4e 471 st->codec->height = avio_rl32(pb);
8bf6db1b 472 /* not available for asf */
b7effd4e
AK
473 avio_rl16(pb); /* panes */
474 st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
48a4ffa7 475 tag1 = avio_rl32(pb);
45a8a02a 476 avio_skip(pb, 20);
8bf6db1b
AK
477 if (sizeX > 40) {
478 st->codec->extradata_size = sizeX - 40;
48a4ffa7
DB
479 st->codec->extradata = av_mallocz(st->codec->extradata_size +
480 FF_INPUT_BUFFER_PADDING_SIZE);
b7effd4e 481 avio_read(pb, st->codec->extradata, st->codec->extradata_size);
8bf6db1b
AK
482 }
483
484 /* Extract palette from extradata if bpp <= 8 */
485 /* This code assumes that extradata contains only palette */
2f5df0b1 486 /* This is true for all paletted codecs implemented in libavcodec */
8bf6db1b 487 if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
8bf6db1b 488#if HAVE_BIGENDIAN
6df5c528 489 int i;
48a4ffa7
DB
490 for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE) / 4; i++)
491 asf_st->palette[i] = av_bswap32(((uint32_t *)st->codec->extradata)[i]);
8bf6db1b 492#else
2d8591c2
KS
493 memcpy(asf_st->palette, st->codec->extradata,
494 FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
8bf6db1b 495#endif
2d8591c2 496 asf_st->palette_changed = 1;
8bf6db1b
AK
497 }
498
499 st->codec->codec_tag = tag1;
48a4ffa7
DB
500 st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
501 if (tag1 == MKTAG('D', 'V', 'R', ' ')) {
8bf6db1b 502 st->need_parsing = AVSTREAM_PARSE_FULL;
48a4ffa7
DB
503 /* issue658 contains wrong w/h and MS even puts a fake seq header
504 * with wrong w/h in extradata while a correct one is in the stream.
505 * maximum lameness */
506 st->codec->width =
8bf6db1b
AK
507 st->codec->height = 0;
508 av_freep(&st->codec->extradata);
48a4ffa7 509 st->codec->extradata_size = 0;
8bf6db1b 510 }
48a4ffa7 511 if (st->codec->codec_id == AV_CODEC_ID_H264)
8bf6db1b
AK
512 st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
513 }
a2704c97 514 pos2 = avio_tell(pb);
45a8a02a 515 avio_skip(pb, size - (pos2 - pos1 + 24));
8bf6db1b
AK
516
517 return 0;
518}
519
c1fea230
AK
520static int asf_read_ext_stream_properties(AVFormatContext *s, int64_t size)
521{
522 ASFContext *asf = s->priv_data;
ae628ec1 523 AVIOContext *pb = s->pb;
c1fea230
AK
524 ff_asf_guid g;
525 int ext_len, payload_ext_ct, stream_ct, i;
e65ab9d9 526 uint32_t leak_rate, stream_num;
c1fea230
AK
527 unsigned int stream_languageid_index;
528
b7effd4e
AK
529 avio_rl64(pb); // starttime
530 avio_rl64(pb); // endtime
531 leak_rate = avio_rl32(pb); // leak-datarate
532 avio_rl32(pb); // bucket-datasize
533 avio_rl32(pb); // init-bucket-fullness
534 avio_rl32(pb); // alt-leak-datarate
535 avio_rl32(pb); // alt-bucket-datasize
536 avio_rl32(pb); // alt-init-bucket-fullness
537 avio_rl32(pb); // max-object-size
538 avio_rl32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
539 stream_num = avio_rl16(pb); // stream-num
540
541 stream_languageid_index = avio_rl16(pb); // stream-language-id-index
c1fea230
AK
542 if (stream_num < 128)
543 asf->streams[stream_num].stream_language_index = stream_languageid_index;
544
b7effd4e 545 avio_rl64(pb); // avg frametime in 100ns units
48a4ffa7
DB
546 stream_ct = avio_rl16(pb); // stream-name-count
547 payload_ext_ct = avio_rl16(pb); // payload-extension-system-count
c1fea230
AK
548
549 if (stream_num < 128)
550 asf->stream_bitrates[stream_num] = leak_rate;
551
48a4ffa7 552 for (i = 0; i < stream_ct; i++) {
b7effd4e
AK
553 avio_rl16(pb);
554 ext_len = avio_rl16(pb);
45a8a02a 555 avio_skip(pb, ext_len);
c1fea230
AK
556 }
557
48a4ffa7 558 for (i = 0; i < payload_ext_ct; i++) {
c1fea230 559 ff_get_guid(pb, &g);
e65ab9d9 560 avio_skip(pb, 2);
48a4ffa7 561 ext_len = avio_rl32(pb);
45a8a02a 562 avio_skip(pb, ext_len);
c1fea230
AK
563 }
564
565 return 0;
566}
567
568static int asf_read_content_desc(AVFormatContext *s, int64_t size)
569{
ae628ec1 570 AVIOContext *pb = s->pb;
c1fea230
AK
571 int len1, len2, len3, len4, len5;
572
b7effd4e
AK
573 len1 = avio_rl16(pb);
574 len2 = avio_rl16(pb);
575 len3 = avio_rl16(pb);
576 len4 = avio_rl16(pb);
577 len5 = avio_rl16(pb);
61f9ad2d
VP
578 get_tag(s, "title", 0, len1, 32);
579 get_tag(s, "author", 0, len2, 32);
580 get_tag(s, "copyright", 0, len3, 32);
581 get_tag(s, "comment", 0, len4, 32);
45a8a02a 582 avio_skip(pb, len5);
c1fea230
AK
583
584 return 0;
585}
586
587static int asf_read_ext_content_desc(AVFormatContext *s, int64_t size)
588{
ae628ec1 589 AVIOContext *pb = s->pb;
c1fea230
AK
590 ASFContext *asf = s->priv_data;
591 int desc_count, i, ret;
592
b7effd4e 593 desc_count = avio_rl16(pb);
48a4ffa7
DB
594 for (i = 0; i < desc_count; i++) {
595 int name_len, value_type, value_len;
c1fea230
AK
596 char name[1024];
597
b7effd4e 598 name_len = avio_rl16(pb);
48a4ffa7 599 if (name_len % 2) // must be even, broken lavf versions wrote len-1
c1fea230
AK
600 name_len += 1;
601 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
45a8a02a 602 avio_skip(pb, name_len - ret);
b7effd4e
AK
603 value_type = avio_rl16(pb);
604 value_len = avio_rl16(pb);
48a4ffa7 605 if (!value_type && value_len % 2)
c1fea230 606 value_len += 1;
48a4ffa7
DB
607 /* My sample has that stream set to 0 maybe that mean the container.
608 * ASF stream count starts at 1. I am using 0 to the container value
609 * since it's unused. */
610 if (!strcmp(name, "AspectRatioX"))
61f9ad2d 611 asf->dar[0].num = get_value(s->pb, value_type, 32);
48a4ffa7 612 else if (!strcmp(name, "AspectRatioY"))
61f9ad2d 613 asf->dar[0].den = get_value(s->pb, value_type, 32);
48a4ffa7 614 else
61f9ad2d 615 get_tag(s, name, value_type, value_len, 32);
c1fea230
AK
616 }
617
618 return 0;
619}
620
621static int asf_read_language_list(AVFormatContext *s, int64_t size)
622{
ae628ec1 623 AVIOContext *pb = s->pb;
c1fea230
AK
624 ASFContext *asf = s->priv_data;
625 int j, ret;
b7effd4e 626 int stream_count = avio_rl16(pb);
48a4ffa7 627 for (j = 0; j < stream_count; j++) {
c1fea230 628 char lang[6];
b7effd4e 629 unsigned int lang_len = avio_r8(pb);
48a4ffa7
DB
630 if ((ret = avio_get_str16le(pb, lang_len, lang,
631 sizeof(lang))) < lang_len)
45a8a02a 632 avio_skip(pb, lang_len - ret);
c1fea230 633 if (j < 128)
48a4ffa7
DB
634 av_strlcpy(asf->stream_languages[j], lang,
635 sizeof(*asf->stream_languages));
c1fea230
AK
636 }
637
638 return 0;
639}
640
641static int asf_read_metadata(AVFormatContext *s, int64_t size)
642{
ae628ec1 643 AVIOContext *pb = s->pb;
c1fea230 644 ASFContext *asf = s->priv_data;
61f9ad2d 645 int n, stream_num, name_len, value_len;
c1fea230 646 int ret, i;
b7effd4e 647 n = avio_rl16(pb);
c1fea230 648
48a4ffa7 649 for (i = 0; i < n; i++) {
c1fea230 650 char name[1024];
61f9ad2d 651 int value_type;
c1fea230 652
48a4ffa7
DB
653 avio_rl16(pb); // lang_list_index
654 stream_num = avio_rl16(pb);
655 name_len = avio_rl16(pb);
72eaba5e 656 value_type = avio_rl16(pb); /* value_type */
48a4ffa7 657 value_len = avio_rl32(pb);
c1fea230
AK
658
659 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
45a8a02a 660 avio_skip(pb, name_len - ret);
1a3eb042 661 av_log(s, AV_LOG_TRACE, "%d stream %d name_len %2d type %d len %4d <%s>\n",
72eaba5e 662 i, stream_num, name_len, value_type, value_len, name);
61f9ad2d
VP
663
664 if (!strcmp(name, "AspectRatioX")){
665 int aspect_x = get_value(s->pb, value_type, 16);
666 if(stream_num < 128)
667 asf->dar[stream_num].num = aspect_x;
668 } else if(!strcmp(name, "AspectRatioY")){
669 int aspect_y = get_value(s->pb, value_type, 16);
670 if(stream_num < 128)
671 asf->dar[stream_num].den = aspect_y;
672 } else {
673 get_tag(s, name, value_type, value_len, 16);
c1fea230
AK
674 }
675 }
676
677 return 0;
678}
679
680static int asf_read_marker(AVFormatContext *s, int64_t size)
681{
ae628ec1 682 AVIOContext *pb = s->pb;
5f408333 683 ASFContext *asf = s->priv_data;
c1fea230
AK
684 int i, count, name_len, ret;
685 char name[1024];
686
b7effd4e
AK
687 avio_rl64(pb); // reserved 16 bytes
688 avio_rl64(pb); // ...
689 count = avio_rl32(pb); // markers count
690 avio_rl16(pb); // reserved 2 bytes
691 name_len = avio_rl16(pb); // name length
48a4ffa7 692 for (i = 0; i < name_len; i++)
b7effd4e 693 avio_r8(pb); // skip the name
c1fea230 694
48a4ffa7 695 for (i = 0; i < count; i++) {
c1fea230
AK
696 int64_t pres_time;
697 int name_len;
698
b7effd4e
AK
699 avio_rl64(pb); // offset, 8 bytes
700 pres_time = avio_rl64(pb); // presentation time
5f408333 701 pres_time -= asf->hdr.preroll * 10000;
b7effd4e
AK
702 avio_rl16(pb); // entry length
703 avio_rl32(pb); // send time
704 avio_rl32(pb); // flags
705 name_len = avio_rl32(pb); // name length
48a4ffa7
DB
706 if ((ret = avio_get_str16le(pb, name_len * 2, name,
707 sizeof(name))) < name_len)
45a8a02a 708 avio_skip(pb, name_len - ret);
48a4ffa7
DB
709 avpriv_new_chapter(s, i, (AVRational) { 1, 10000000 }, pres_time,
710 AV_NOPTS_VALUE, name);
c1fea230
AK
711 }
712
713 return 0;
714}
715
6e9651d1 716static int asf_read_header(AVFormatContext *s)
de6d9b64 717{
c9a65ca8 718 ASFContext *asf = s->priv_data;
b8b00d94 719 ff_asf_guid g;
ae628ec1 720 AVIOContext *pb = s->pb;
0b1d291a 721 int i;
0c1a9eda 722 int64_t gsize;
de6d9b64 723
a750050f
PR
724 ff_get_guid(pb, &g);
725 if (ff_guidcmp(&g, &ff_asf_header))
ccafd472 726 return -1;
b7effd4e
AK
727 avio_rl64(pb);
728 avio_rl32(pb);
729 avio_r8(pb);
730 avio_r8(pb);
2141dc37 731 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
48a4ffa7
DB
732 for (;;) {
733 uint64_t gpos = avio_tell(pb);
a750050f 734 ff_get_guid(pb, &g);
b7effd4e 735 gsize = avio_rl64(pb);
de6d9b64 736 print_guid(&g);
a750050f 737 if (!ff_guidcmp(&g, &ff_asf_data_header)) {
a2704c97 738 asf->data_object_offset = avio_tell(pb);
48a4ffa7
DB
739 /* If not streaming, gsize is not unlimited (how?),
740 * and there is enough space in the file.. */
741 if (!(asf->hdr.flags & 0x01) && gsize >= 100)
09d9672a 742 asf->data_object_size = gsize - 24;
48a4ffa7 743 else
09d9672a 744 asf->data_object_size = (uint64_t)-1;
09d9672a
MN
745 break;
746 }
de6d9b64 747 if (gsize < 24)
ccafd472 748 return -1;
a750050f 749 if (!ff_guidcmp(&g, &ff_asf_file_header)) {
6e57a02b
RB
750 int ret = asf_read_file_properties(s, gsize);
751 if (ret < 0)
752 return ret;
a750050f 753 } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
cc41167a
MS
754 int ret = asf_read_stream_properties(s, gsize);
755 if (ret < 0)
756 return ret;
a750050f 757 } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
c1fea230 758 asf_read_content_desc(s, gsize);
a750050f 759 } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
c1fea230 760 asf_read_language_list(s, gsize);
a750050f 761 } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
c1fea230 762 asf_read_ext_content_desc(s, gsize);
a750050f 763 } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
c1fea230 764 asf_read_metadata(s, gsize);
84b721db
VP
765 } else if (!ff_guidcmp(&g, &ff_asf_metadata_library_header)) {
766 asf_read_metadata(s, gsize);
a750050f 767 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
c1fea230 768 asf_read_ext_stream_properties(s, gsize);
88141c91
MN
769
770 // there could be a optional stream properties object to follow
771 // if so the next iteration will pick it up
1d4c1c3b 772 continue;
a750050f 773 } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
a750050f 774 ff_get_guid(pb, &g);
e65ab9d9 775 avio_skip(pb, 6);
1d4c1c3b 776 continue;
a750050f 777 } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
c1fea230 778 asf_read_marker(s, gsize);
66e5b1df 779 } else if (pb->eof_reached) {
ccafd472 780 return -1;
de6d9b64 781 } else {
febd1c90 782 if (!s->keylen) {
a750050f 783 if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
48a4ffa7
DB
784 av_log(s, AV_LOG_WARNING,
785 "DRM protected stream detected, decoding will likely fail!\n");
a750050f 786 } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
48a4ffa7
DB
787 av_log(s, AV_LOG_WARNING,
788 "Ext DRM protected stream detected, decoding will likely fail!\n");
a750050f 789 } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
48a4ffa7
DB
790 av_log(s, AV_LOG_WARNING,
791 "Digital signature detected, decoding will likely fail!\n");
febd1c90
DT
792 }
793 }
de6d9b64 794 }
48a4ffa7
DB
795 if (avio_tell(pb) != gpos + gsize)
796 av_log(s, AV_LOG_DEBUG,
797 "gpos mismatch our pos=%"PRIu64", end=%"PRId64"\n",
798 avio_tell(pb) - gpos, gsize);
6b4aa5da 799 avio_seek(pb, gpos + gsize, SEEK_SET);
de6d9b64 800 }
a750050f 801 ff_get_guid(pb, &g);
b7effd4e
AK
802 avio_rl64(pb);
803 avio_r8(pb);
804 avio_r8(pb);
66e5b1df 805 if (pb->eof_reached)
ccafd472 806 return -1;
48a4ffa7 807 asf->data_offset = avio_tell(pb);
de6d9b64
FB
808 asf->packet_size_left = 0;
809
48a4ffa7
DB
810 for (i = 0; i < 128; i++) {
811 int stream_num = asf->asfid2avid[i];
812 if (stream_num >= 0) {
59729451
AJ
813 AVStream *st = s->streams[stream_num];
814 if (!st->codec->bit_rate)
7c725380 815 st->codec->bit_rate = asf->stream_bitrates[i];
48a4ffa7 816 if (asf->dar[i].num > 0 && asf->dar[i].den > 0) {
59729451
AJ
817 av_reduce(&st->sample_aspect_ratio.num,
818 &st->sample_aspect_ratio.den,
d42b0972 819 asf->dar[i].num, asf->dar[i].den, INT_MAX);
48a4ffa7
DB
820 } else if ((asf->dar[0].num > 0) && (asf->dar[0].den > 0) &&
821 // Use ASF container value if the stream doesn't set AR.
822 (st->codec->codec_type == AVMEDIA_TYPE_VIDEO))
6ac6e3d1
RB
823 av_reduce(&st->sample_aspect_ratio.num,
824 &st->sample_aspect_ratio.den,
d42b0972 825 asf->dar[0].num, asf->dar[0].den, INT_MAX);
6ac6e3d1 826
1a3eb042 827 av_log(s, AV_LOG_TRACE, "i=%d, st->codec->codec_type:%d, asf->dar %d:%d sar=%d:%d\n",
72eaba5e
DB
828 i, st->codec->codec_type, asf->dar[i].num, asf->dar[i].den,
829 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
5f1ac36d
CC
830
831 // copy and convert language codes to the frontend
832 if (asf->streams[i].stream_language_index < 128) {
833 const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
834 if (rfc1766 && strlen(rfc1766) > 1) {
835 const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
48a4ffa7
DB
836 const char *iso6392 = av_convert_lang_to(primary_tag,
837 AV_LANG_ISO639_2_BIBL);
5f1ac36d 838 if (iso6392)
d2d67e42 839 av_dict_set(&st->metadata, "language", iso6392, 0);
5f1ac36d
CC
840 }
841 }
30e88789
MN
842 }
843 }
844
ad7768f4 845 ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
03700d39 846
de6d9b64 847 return 0;
de6d9b64
FB
848}
849
48a4ffa7
DB
850#define DO_2BITS(bits, var, defval) \
851 switch (bits & 3) { \
852 case 3: \
853 var = avio_rl32(pb); \
854 rsize += 4; \
855 break; \
856 case 2: \
857 var = avio_rl16(pb); \
858 rsize += 2; \
859 break; \
860 case 1: \
861 var = avio_r8(pb); \
862 rsize++; \
863 break; \
864 default: \
865 var = defval; \
866 break; \
2a10020b
ZK
867 }
868
277a28e8
RB
869/**
870 * Load a single ASF packet into the demuxer.
871 * @param s demux context
872 * @param pb context to read data from
32e543f8 873 * @return 0 on success, <0 on error
277a28e8 874 */
e926b5ce 875static int asf_get_packet(AVFormatContext *s, AVIOContext *pb)
de6d9b64
FB
876{
877 ASFContext *asf = s->priv_data;
2a10020b 878 uint32_t packet_length, padsize;
0c0d17f3 879 int rsize = 8;
0fd9e6f3
MN
880 int c, d, e, off;
881
68a10b40 882 // if we do not know packet size, allow skipping up to 32 kB
48a4ffa7 883 off = 32768;
75b7feae
MS
884 if (asf->no_resync_search)
885 off = 3;
886 else if (s->packet_size > 0)
9deaec78 887 off = (avio_tell(pb) - s->internal->data_offset) % s->packet_size + 3;
48a4ffa7
DB
888
889 c = d = e = -1;
890 while (off-- > 0) {
891 c = d;
892 d = e;
893 e = avio_r8(pb);
894 if (c == 0x82 && !d && !e)
0fd9e6f3
MN
895 break;
896 }
115329f1 897
1359c2d4 898 if (c != 0x82) {
48a4ffa7 899 /* This code allows handling of -EAGAIN at packet boundaries (i.e.
2078ca16
RB
900 * if the packet sync code above triggers -EAGAIN). This does not
901 * imply complete -EAGAIN handling support at random positions in
48a4ffa7 902 * the stream. */
3e68b3ba 903 if (pb->error == AVERROR(EAGAIN))
2078ca16 904 return AVERROR(EAGAIN);
66e5b1df 905 if (!pb->eof_reached)
48a4ffa7
DB
906 av_log(s, AV_LOG_ERROR,
907 "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
2a10020b 908 }
0c0d17f3 909 if ((c & 0x8f) == 0x82) {
0fd9e6f3 910 if (d || e) {
66e5b1df 911 if (!pb->eof_reached)
bb270c08 912 av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
f7835507 913 return -1;
bb270c08 914 }
48a4ffa7
DB
915 c = avio_r8(pb);
916 d = avio_r8(pb);
917 rsize += 3;
bb6d5411 918 } else if (!pb->eof_reached) {
48a4ffa7 919 avio_seek(pb, -1, SEEK_CUR); // FIXME
de6d9b64 920 }
2a10020b 921
0c0d17f3
MN
922 asf->packet_flags = c;
923 asf->packet_property = d;
2a10020b 924
91d19d47 925 DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
2a10020b
ZK
926 DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
927 DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
928
48a4ffa7
DB
929 // the following checks prevent overflows and infinite loops
930 if (!packet_length || packet_length >= (1U << 29)) {
931 av_log(s, AV_LOG_ERROR,
d92024f1 932 "invalid packet_length %"PRIu32" at:%"PRId64"\n",
48a4ffa7 933 packet_length, avio_tell(pb));
ceba96cb 934 return -1;
5b11e081 935 }
48a4ffa7
DB
936 if (padsize >= packet_length) {
937 av_log(s, AV_LOG_ERROR,
d92024f1 938 "invalid padsize %"PRIu32" at:%"PRId64"\n", padsize, avio_tell(pb));
ceba96cb 939 return -1;
5b11e081
MN
940 }
941
b7effd4e
AK
942 asf->packet_timestamp = avio_rl32(pb);
943 avio_rl16(pb); /* duration */
2141dc37 944 // rsize has at least 11 bytes which have to be present
2a10020b
ZK
945
946 if (asf->packet_flags & 0x01) {
48a4ffa7
DB
947 asf->packet_segsizetype = avio_r8(pb);
948 rsize++;
2a10020b
ZK
949 asf->packet_segments = asf->packet_segsizetype & 0x3f;
950 } else {
48a4ffa7 951 asf->packet_segments = 1;
2a10020b
ZK
952 asf->packet_segsizetype = 0x80;
953 }
41afac7f
RB
954 if (rsize > packet_length - padsize) {
955 asf->packet_size_left = 0;
956 av_log(s, AV_LOG_ERROR,
d92024f1 957 "invalid packet header length %d for pktlen %"PRIu32"-%"PRIu32" at %"PRId64"\n",
41afac7f
RB
958 rsize, packet_length, padsize, avio_tell(pb));
959 return -1;
960 }
2a10020b 961 asf->packet_size_left = packet_length - padsize - rsize;
acbe6cfa
ZK
962 if (packet_length < asf->hdr.min_pktsize)
963 padsize += asf->hdr.min_pktsize - packet_length;
2a10020b 964 asf->packet_padsize = padsize;
1a3eb042 965 av_log(s, AV_LOG_TRACE, "packet: size=%d padsize=%d left=%d\n",
48a4ffa7 966 s->packet_size, asf->packet_padsize, asf->packet_size_left);
de6d9b64
FB
967 return 0;
968}
969
ae60a857
MN
970/**
971 *
972 * @return <0 if error
973 */
48a4ffa7
DB
974static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb)
975{
ae60a857 976 ASFContext *asf = s->priv_data;
48a4ffa7
DB
977 int rsize = 1;
978 int num = avio_r8(pb);
e65ab9d9 979 int64_t ts0;
ae60a857
MN
980
981 asf->packet_segments--;
982 asf->packet_key_frame = num >> 7;
48a4ffa7 983 asf->stream_index = asf->asfid2avid[num & 0x7f];
ae60a857
MN
984 // sequence should be ignored!
985 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
986 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
987 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
1a3eb042 988 av_log(asf, AV_LOG_TRACE, "key:%d stream:%d seq:%d offset:%d replic_size:%d\n",
72eaba5e
DB
989 asf->packet_key_frame, asf->stream_index, asf->packet_seq,
990 asf->packet_frag_offset, asf->packet_replic_size);
ae60a857 991 if (asf->packet_replic_size >= 8) {
b7effd4e 992 asf->packet_obj_size = avio_rl32(pb);
48a4ffa7 993 if (asf->packet_obj_size >= (1 << 24) || asf->packet_obj_size <= 0) {
ae60a857
MN
994 av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
995 return -1;
996 }
b7effd4e 997 asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
48a4ffa7 998 if (asf->packet_replic_size >= 8 + 38 + 4) {
45a8a02a 999 avio_skip(pb, 10);
48a4ffa7
DB
1000 ts0 = avio_rl64(pb);
1001 avio_skip(pb, 8);
45a8a02a 1002 avio_skip(pb, 12);
b7effd4e 1003 avio_rl32(pb);
45a8a02a 1004 avio_skip(pb, asf->packet_replic_size - 8 - 38 - 4);
48a4ffa7
DB
1005 if (ts0 != -1)
1006 asf->packet_frag_timestamp = ts0 / 10000;
1007 else
1008 asf->packet_frag_timestamp = AV_NOPTS_VALUE;
1009 } else
45a8a02a 1010 avio_skip(pb, asf->packet_replic_size - 8);
ae60a857 1011 rsize += asf->packet_replic_size; // FIXME - check validity
48a4ffa7 1012 } else if (asf->packet_replic_size == 1) {
efc90ebf 1013 // multipacket - frag_offset is beginning timestamp
48a4ffa7
DB
1014 asf->packet_time_start = asf->packet_frag_offset;
1015 asf->packet_frag_offset = 0;
ae60a857
MN
1016 asf->packet_frag_timestamp = asf->packet_timestamp;
1017
b7effd4e 1018 asf->packet_time_delta = avio_r8(pb);
ae60a857 1019 rsize++;
48a4ffa7
DB
1020 } else if (asf->packet_replic_size != 0) {
1021 av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n",
1022 asf->packet_replic_size);
ae60a857
MN
1023 return -1;
1024 }
1025 if (asf->packet_flags & 0x01) {
1026 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
29fa570d
UU
1027 if (rsize > asf->packet_size_left) {
1028 av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
1029 return -1;
48a4ffa7 1030 } else if (asf->packet_frag_size > asf->packet_size_left - rsize) {
f8b12459 1031 if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
48a4ffa7
DB
1032 av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d-%d)\n",
1033 asf->packet_size_left, rsize);
f8b12459
RB
1034 return -1;
1035 } else {
1036 int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
1037 asf->packet_size_left += diff;
1038 asf->packet_padsize -= diff;
1039 }
13f7e4b9 1040 }
ae60a857 1041 } else {
0bd433a9
UU
1042 if (rsize > asf->packet_size_left) {
1043 av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
1044 return -1;
1045 }
ae60a857 1046 asf->packet_frag_size = asf->packet_size_left - rsize;
ae60a857
MN
1047 }
1048 if (asf->packet_replic_size == 1) {
1049 asf->packet_multi_size = asf->packet_frag_size;
1050 if (asf->packet_multi_size > asf->packet_size_left)
1051 return -1;
1052 }
1053 asf->packet_size_left -= rsize;
ae60a857
MN
1054
1055 return 0;
1056}
1057
277a28e8
RB
1058/**
1059 * Parse data from individual ASF packets (which were previously loaded
1060 * with asf_get_packet()).
1061 * @param s demux context
1062 * @param pb context to read data from
1063 * @param pkt pointer to store packet data into
32e543f8 1064 * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
277a28e8
RB
1065 * packets need to be loaded (through asf_get_packet())
1066 */
e926b5ce 1067static int asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
de6d9b64 1068{
48a4ffa7 1069 ASFContext *asf = s->priv_data;
2a10020b 1070 ASFStream *asf_st = 0;
2a10020b 1071 for (;;) {
4172951b 1072 int ret;
292a08cb
AK
1073
1074 if (pb->eof_reached)
799a0722 1075 return AVERROR_EOF;
292a08cb
AK
1076
1077 if (asf->packet_size_left < FRAME_HEADER_SIZE ||
1078 asf->packet_segments < 1) {
bb270c08 1079 int ret = asf->packet_size_left + asf->packet_padsize;
292a08cb
AK
1080
1081 assert(ret >= 0);
bb270c08 1082 /* fail safe */
45a8a02a 1083 avio_skip(pb, ret);
b04d5d31 1084
48a4ffa7 1085 asf->packet_pos = avio_tell(pb);
b6eaae39
KED
1086 if (asf->data_object_size != (uint64_t)-1 &&
1087 (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
48a4ffa7 1088 return AVERROR_EOF; /* Do not exceed the size of the data object */
f27544fd 1089 return 1;
bb270c08
DB
1090 }
1091 if (asf->packet_time_start == 0) {
292a08cb
AK
1092 if (asf_read_frame_header(s, pb) < 0) {
1093 asf->packet_segments = 0;
236116cb 1094 continue;
1305a9d5 1095 }
292a08cb
AK
1096 if (asf->stream_index < 0 ||
1097 s->streams[asf->stream_index]->discard >= AVDISCARD_ALL ||
1098 (!asf->packet_key_frame &&
1099 s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)) {
1359c2d4 1100 asf->packet_time_start = 0;
bb270c08 1101 /* unhandled packet (should not happen) */
45a8a02a 1102 avio_skip(pb, asf->packet_frag_size);
bb270c08 1103 asf->packet_size_left -= asf->packet_frag_size;
292a08cb 1104 if (asf->stream_index < 0)
48a4ffa7
DB
1105 av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n",
1106 asf->packet_frag_size);
1359c2d4 1107 continue;
bb270c08
DB
1108 }
1109 asf->asf_st = s->streams[asf->stream_index]->priv_data;
1110 }
1111 asf_st = asf->asf_st;
1ef0e8a6 1112 av_assert0(asf_st);
bb270c08 1113
f5d92d9c 1114 if (!asf_st->frag_offset && asf->packet_frag_offset) {
1a3eb042 1115 av_log(s, AV_LOG_TRACE, "skipping asf data pkt with fragment offset for "
f5d92d9c
JG
1116 "stream:%d, expected:%d but got %d from pkt)\n",
1117 asf->stream_index, asf_st->frag_offset,
1118 asf->packet_frag_offset);
1119 avio_skip(pb, asf->packet_frag_size);
1120 asf->packet_size_left -= asf->packet_frag_size;
1121 continue;
1122 }
1123
bb270c08 1124 if (asf->packet_replic_size == 1) {
efc90ebf 1125 // frag_offset is here used as the beginning timestamp
bb270c08 1126 asf->packet_frag_timestamp = asf->packet_time_start;
292a08cb
AK
1127 asf->packet_time_start += asf->packet_time_delta;
1128 asf->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
bb270c08 1129 asf->packet_size_left--;
2141dc37 1130 asf->packet_multi_size--;
292a08cb 1131 if (asf->packet_multi_size < asf->packet_obj_size) {
bb270c08 1132 asf->packet_time_start = 0;
45a8a02a 1133 avio_skip(pb, asf->packet_multi_size);
bb270c08 1134 asf->packet_size_left -= asf->packet_multi_size;
2141dc37 1135 continue;
bb270c08
DB
1136 }
1137 asf->packet_multi_size -= asf->packet_obj_size;
bb270c08 1138 }
292a08cb
AK
1139 if (asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size &&
1140 asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size) {
60ff1c44 1141 av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
292a08cb
AK
1142 asf_st->frag_offset, asf->packet_frag_size,
1143 asf->packet_obj_size, asf_st->pkt.size);
1144 asf->packet_obj_size = asf_st->pkt.size;
60ff1c44
MN
1145 }
1146
292a08cb 1147 if (asf_st->pkt.size != asf->packet_obj_size ||
48a4ffa7 1148 // FIXME is this condition sufficient?
292a08cb
AK
1149 asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) {
1150 if (asf_st->pkt.data) {
48a4ffa7
DB
1151 av_log(s, AV_LOG_INFO,
1152 "freeing incomplete packet size %d, new %d\n",
1153 asf_st->pkt.size, asf->packet_obj_size);
200026aa
MN
1154 asf_st->frag_offset = 0;
1155 av_free_packet(&asf_st->pkt);
1156 }
bb270c08
DB
1157 /* new packet */
1158 av_new_packet(&asf_st->pkt, asf->packet_obj_size);
292a08cb
AK
1159 asf_st->seq = asf->packet_seq;
1160 asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
bb270c08 1161 asf_st->pkt.stream_index = asf->stream_index;
292a08cb
AK
1162 asf_st->pkt.pos = asf_st->packet_pos = asf->packet_pos;
1163
2d8591c2
KS
1164 if (asf_st->pkt.data && asf_st->palette_changed) {
1165 uint8_t *pal;
e9dc9201 1166 pal = av_packet_new_side_data(&asf_st->pkt, AV_PKT_DATA_PALETTE,
2d8591c2
KS
1167 AVPALETTE_SIZE);
1168 if (!pal) {
1169 av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
1170 } else {
1171 memcpy(pal, asf_st->palette, AVPALETTE_SIZE);
1172 asf_st->palette_changed = 0;
1173 }
1174 }
1a3eb042 1175 av_log(asf, AV_LOG_TRACE, "new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
72eaba5e
DB
1176 asf->stream_index, asf->packet_key_frame,
1177 asf_st->pkt.flags & AV_PKT_FLAG_KEY,
1178 s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO,
1179 asf->packet_obj_size);
72415b2a 1180 if (s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
bb270c08
DB
1181 asf->packet_key_frame = 1;
1182 if (asf->packet_key_frame)
cc947f04 1183 asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
bb270c08
DB
1184 }
1185
1186 /* read data */
1a3eb042 1187 av_log(asf, AV_LOG_TRACE, "READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
72eaba5e
DB
1188 s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
1189 asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
bb270c08
DB
1190 asf->packet_size_left -= asf->packet_frag_size;
1191 if (asf->packet_size_left < 0)
2141dc37 1192 continue;
4c71d727 1193
292a08cb
AK
1194 if (asf->packet_frag_offset >= asf_st->pkt.size ||
1195 asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset) {
48a4ffa7
DB
1196 av_log(s, AV_LOG_ERROR,
1197 "packet fragment position invalid %u,%u not in %u\n",
1198 asf->packet_frag_offset, asf->packet_frag_size,
1199 asf_st->pkt.size);
4c71d727
MN
1200 continue;
1201 }
1202
b7effd4e 1203 ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
292a08cb 1204 asf->packet_frag_size);
bf09a019
RD
1205 if (ret != asf->packet_frag_size) {
1206 if (ret < 0 || asf->packet_frag_offset + ret == 0)
1207 return ret < 0 ? ret : AVERROR_EOF;
292a08cb 1208
bf09a019
RD
1209 if (asf_st->ds_span > 1) {
1210 // scrambling, we can either drop it completely or fill the remainder
1211 // TODO: should we fill the whole packet instead of just the current
1212 // fragment?
1213 memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
1214 asf->packet_frag_size - ret);
1215 ret = asf->packet_frag_size;
292a08cb 1216 } else {
bf09a019
RD
1217 // no scrambling, so we can return partial packets
1218 av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
292a08cb 1219 }
bf09a019 1220 }
72be7db4
RD
1221 if (s->key && s->keylen == 20)
1222 ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
bf09a019
RD
1223 ret);
1224 asf_st->frag_offset += ret;
bb270c08
DB
1225 /* test if whole packet is read */
1226 if (asf_st->frag_offset == asf_st->pkt.size) {
48a4ffa7 1227 // workaround for macroshit radio DVR-MS files
292a08cb
AK
1228 if (s->streams[asf->stream_index]->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
1229 asf_st->pkt.size > 100) {
da0b94f5 1230 int i;
48a4ffa7
DB
1231 for (i = 0; i < asf_st->pkt.size && !asf_st->pkt.data[i]; i++)
1232 ;
292a08cb 1233 if (i == asf_st->pkt.size) {
da0b94f5
MN
1234 av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
1235 asf_st->frag_offset = 0;
1236 av_free_packet(&asf_st->pkt);
1237 continue;
1238 }
1239 }
1240
bb270c08
DB
1241 /* return packet */
1242 if (asf_st->ds_span > 1) {
48a4ffa7
DB
1243 if (asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span) {
1244 av_log(s, AV_LOG_ERROR,
1245 "pkt.size != ds_packet_size * ds_span (%d %d %d)\n",
1246 asf_st->pkt.size, asf_st->ds_packet_size,
1247 asf_st->ds_span);
292a08cb
AK
1248 } else {
1249 /* packet descrambling */
1afddbe5
AK
1250 AVBufferRef *buf = av_buffer_alloc(asf_st->pkt.size +
1251 FF_INPUT_BUFFER_PADDING_SIZE);
1252 if (buf) {
1253 uint8_t *newdata = buf->data;
292a08cb 1254 int offset = 0;
48a4ffa7
DB
1255 memset(newdata + asf_st->pkt.size, 0,
1256 FF_INPUT_BUFFER_PADDING_SIZE);
292a08cb
AK
1257 while (offset < asf_st->pkt.size) {
1258 int off = offset / asf_st->ds_chunk_size;
1259 int row = off / asf_st->ds_span;
1260 int col = off % asf_st->ds_span;
1261 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1262 assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
48a4ffa7 1263 assert(idx + 1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
292a08cb
AK
1264 memcpy(newdata + offset,
1265 asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1266 asf_st->ds_chunk_size);
1267 offset += asf_st->ds_chunk_size;
1268 }
1afddbe5
AK
1269 av_buffer_unref(&asf_st->pkt.buf);
1270 asf_st->pkt.buf = buf;
1271 asf_st->pkt.data = buf->data;
bb270c08 1272 }
bb270c08
DB
1273 }
1274 }
292a08cb
AK
1275 asf_st->frag_offset = 0;
1276 *pkt = asf_st->pkt;
1afddbe5 1277#if FF_API_DESTRUCT_PACKET
7950e519 1278FF_DISABLE_DEPRECATION_WARNINGS
1afddbe5 1279 asf_st->pkt.destruct = NULL;
7950e519 1280FF_ENABLE_DEPRECATION_WARNINGS
1afddbe5
AK
1281#endif
1282 asf_st->pkt.buf = 0;
292a08cb
AK
1283 asf_st->pkt.size = 0;
1284 asf_st->pkt.data = 0;
e73c6aaa 1285 asf_st->pkt.side_data_elems = 0;
292a08cb 1286 asf_st->pkt.side_data = NULL;
bb270c08
DB
1287 break; // packet completed
1288 }
de6d9b64 1289 }
de6d9b64
FB
1290 return 0;
1291}
1292
f27544fd
RB
1293static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1294{
1295 ASFContext *asf = s->priv_data;
1296
1297 for (;;) {
1298 int ret;
1299
1300 /* parse cached packets, if any */
e926b5ce 1301 if ((ret = asf_parse_packet(s, s->pb, pkt)) <= 0)
f27544fd 1302 return ret;
e926b5ce 1303 if ((ret = asf_get_packet(s, s->pb)) < 0)
48a4ffa7
DB
1304 assert(asf->packet_size_left < FRAME_HEADER_SIZE ||
1305 asf->packet_segments < 1);
f27544fd
RB
1306 asf->packet_time_start = 0;
1307 }
f27544fd
RB
1308}
1309
38376f00
KK
1310// Added to support seeking after packets have been read
1311// If information is not reset, read_packet fails due to
1312// leftover information from previous reads
1313static void asf_reset_header(AVFormatContext *s)
1314{
1315 ASFContext *asf = s->priv_data;
82b9e4a2
MN
1316 ASFStream *asf_st;
1317 int i;
38376f00 1318
48a4ffa7
DB
1319 asf->packet_size_left = 0;
1320 asf->packet_segments = 0;
1321 asf->packet_flags = 0;
1322 asf->packet_property = 0;
1323 asf->packet_timestamp = 0;
1324 asf->packet_segsizetype = 0;
1325 asf->packet_segments = 0;
1326 asf->packet_seq = 0;
1327 asf->packet_replic_size = 0;
1328 asf->packet_key_frame = 0;
1329 asf->packet_padsize = 0;
1330 asf->packet_frag_offset = 0;
1331 asf->packet_frag_size = 0;
38376f00 1332 asf->packet_frag_timestamp = 0;
48a4ffa7
DB
1333 asf->packet_multi_size = 0;
1334 asf->packet_obj_size = 0;
1335 asf->packet_time_delta = 0;
1336 asf->packet_time_start = 0;
115329f1 1337
48a4ffa7
DB
1338 for (i = 0; i < s->nb_streams; i++) {
1339 asf_st = s->streams[i]->priv_data;
1ef0e8a6
AK
1340 if (!asf_st)
1341 continue;
82b9e4a2 1342 av_free_packet(&asf_st->pkt);
48a4ffa7
DB
1343 asf_st->frag_offset = 0;
1344 asf_st->seq = 0;
82b9e4a2 1345 }
48a4ffa7 1346 asf->asf_st = NULL;
38376f00
KK
1347}
1348
5d25d2a9
DB
1349static int asf_read_close(AVFormatContext *s)
1350{
5d25d2a9 1351 asf_reset_header(s);
0b5ae1fd 1352
5d25d2a9
DB
1353 return 0;
1354}
1355
48a4ffa7
DB
1356static int64_t asf_read_pts(AVFormatContext *s, int stream_index,
1357 int64_t *ppos, int64_t pos_limit)
38376f00 1358{
38376f00 1359 AVPacket pkt1, *pkt = &pkt1;
82b9e4a2 1360 ASFStream *asf_st;
580fb5e7 1361 int64_t pts;
48a4ffa7 1362 int64_t pos = *ppos;
3e9245a9 1363 int i;
30cdfb49 1364 int64_t start_pos[ASF_MAX_STREAMS];
115329f1 1365
48a4ffa7
DB
1366 for (i = 0; i < s->nb_streams; i++)
1367 start_pos[i] = pos;
115329f1 1368
68a10b40 1369 if (s->packet_size > 0)
9deaec78 1370 pos = (pos + s->packet_size - 1 - s->internal->data_offset) /
48a4ffa7 1371 s->packet_size * s->packet_size +
9deaec78 1372 s->internal->data_offset;
48a4ffa7 1373 *ppos = pos;
6b4aa5da 1374 avio_seek(s->pb, pos, SEEK_SET);
115329f1 1375
82b9e4a2 1376 asf_reset_header(s);
48a4ffa7
DB
1377 for (;;) {
1378 if (asf_read_packet(s, pkt) < 0) {
0b93103a 1379 av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
bb270c08 1380 return AV_NOPTS_VALUE;
82b9e4a2 1381 }
115329f1 1382
b88eb876 1383 pts = pkt->dts;
580fb5e7
MN
1384
1385 av_free_packet(pkt);
48a4ffa7
DB
1386 if (pkt->flags & AV_PKT_FLAG_KEY) {
1387 i = pkt->stream_index;
3e9245a9 1388
48a4ffa7 1389 asf_st = s->streams[i]->priv_data;
1ef0e8a6 1390 av_assert0(asf_st);
3e9245a9 1391
91d19d47 1392// assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
48a4ffa7 1393 pos = asf_st->packet_pos;
09646bab 1394
48a4ffa7
DB
1395 av_add_index_entry(s->streams[i], pos, pts, pkt->size,
1396 pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
1397 start_pos[i] = asf_st->packet_pos + 1;
115329f1 1398
48a4ffa7
DB
1399 if (pkt->stream_index == stream_index)
1400 break;
3e9245a9
MN
1401 }
1402 }
1403
48a4ffa7 1404 *ppos = pos;
580fb5e7 1405 return pts;
38376f00
KK
1406}
1407
0ebb523f 1408static int asf_build_simple_index(AVFormatContext *s, int stream_index)
56c96a22 1409{
b8b00d94 1410 ff_asf_guid g;
48a4ffa7
DB
1411 ASFContext *asf = s->priv_data;
1412 int64_t current_pos = avio_tell(s->pb);
0ebb523f 1413 int i, ret = 0;
56c96a22 1414
6b4aa5da 1415 avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET);
0ebb523f
JG
1416 if ((ret = ff_get_guid(s->pb, &g)) < 0)
1417 goto end;
77b7c334
SG
1418
1419 /* the data object can be followed by other top-level objects,
48a4ffa7 1420 * skip them until the simple index object is reached */
a750050f 1421 while (ff_guidcmp(&g, &index_guid)) {
48a4ffa7 1422 int64_t gsize = avio_rl64(s->pb);
66e5b1df 1423 if (gsize < 24 || s->pb->eof_reached) {
0ebb523f 1424 goto end;
77b7c334 1425 }
48a4ffa7 1426 avio_skip(s->pb, gsize - 24);
0ebb523f
JG
1427 if ((ret = ff_get_guid(s->pb, &g)) < 0)
1428 goto end;
77b7c334
SG
1429 }
1430
1431 {
48a4ffa7 1432 int64_t itime, last_pos = -1;
d728e3cd 1433 int pct, ict;
48a4ffa7 1434 int64_t av_unused gsize = avio_rl64(s->pb);
0ebb523f
JG
1435 if ((ret = ff_get_guid(s->pb, &g)) < 0)
1436 goto end;
48a4ffa7
DB
1437 itime = avio_rl64(s->pb);
1438 pct = avio_rl32(s->pb);
1439 ict = avio_rl32(s->pb);
1440 av_log(s, AV_LOG_DEBUG,
1441 "itime:0x%"PRIx64", pct:%d, ict:%d\n", itime, pct, ict);
1442
1443 for (i = 0; i < ict; i++) {
1444 int pktnum = avio_rl32(s->pb);
1445 int pktct = avio_rl16(s->pb);
9deaec78 1446 int64_t pos = s->internal->data_offset + s->packet_size * (int64_t)pktnum;
48a4ffa7
DB
1447 int64_t index_pts = FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
1448
1449 if (pos != last_pos) {
1450 av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n",
1451 pktnum, pktct, index_pts);
1452 av_add_index_entry(s->streams[stream_index], pos, index_pts,
1453 s->packet_size, 0, AVINDEX_KEYFRAME);
1454 last_pos = pos;
134271c2 1455 }
56c96a22 1456 }
48a4ffa7 1457 asf->index_read = ict > 0;
56c96a22 1458 }
0ebb523f
JG
1459end:
1460 if (s->pb->eof_reached)
1461 ret = 0;
6b4aa5da 1462 avio_seek(s->pb, current_pos, SEEK_SET);
0ebb523f 1463 return ret;
56c96a22
MN
1464}
1465
48a4ffa7
DB
1466static int asf_read_seek(AVFormatContext *s, int stream_index,
1467 int64_t pts, int flags)
2a10020b 1468{
afda223c 1469 ASFContext *asf = s->priv_data;
48a4ffa7 1470 AVStream *st = s->streams[stream_index];
56c96a22 1471 int64_t pos;
0ebb523f 1472 int index, ret = 0;
115329f1 1473
91d19d47 1474 if (s->packet_size <= 0)
38376f00
KK
1475 return -1;
1476
fa13095a 1477 /* Try using the protocol's read_seek if available */
48a4ffa7 1478 if (s->pb) {
ff1ec0c3 1479 int ret = avio_seek_time(s->pb, stream_index, pts, flags);
48a4ffa7 1480 if (ret >= 0)
fa13095a 1481 asf_reset_header(s);
fd2982a0 1482 if (ret != AVERROR(ENOSYS))
cd31f8dc 1483 return ret;
fa13095a
BA
1484 }
1485
0c082565
AK
1486 /* explicitly handle the case of seeking to 0 */
1487 if (!pts) {
1488 asf_reset_header(s);
9deaec78 1489 avio_seek(s->pb, s->internal->data_offset, SEEK_SET);
0c082565
AK
1490 return 0;
1491 }
1492
52e54612 1493 if (!asf->index_read)
0ebb523f 1494 ret = asf_build_simple_index(s, stream_index);
56c96a22 1495
0ebb523f 1496 if (!ret && asf->index_read && st->index_entries) {
48a4ffa7
DB
1497 index = av_index_search_timestamp(st, pts, flags);
1498 if (index >= 0) {
b58bc17c
VP
1499 /* find the position */
1500 pos = st->index_entries[index].pos;
56c96a22 1501
b58bc17c
VP
1502 /* do the seek */
1503 av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
1504 avio_seek(s->pb, pos, SEEK_SET);
1505 asf_reset_header(s);
1506 return 0;
c7bd5eda 1507 }
56c96a22 1508 }
c7bd5eda 1509 /* no index or seeking by index failed */
a2faa951 1510 if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
c7bd5eda 1511 return -1;
38376f00
KK
1512 asf_reset_header(s);
1513 return 0;
2a10020b
ZK
1514}
1515
c6610a21 1516AVInputFormat ff_asf_demuxer = {
dfc2c4d9 1517 .name = "asf",
0177b7d2 1518 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
dfc2c4d9
AK
1519 .priv_data_size = sizeof(ASFContext),
1520 .read_probe = asf_probe,
1521 .read_header = asf_read_header,
1522 .read_packet = asf_read_packet,
1523 .read_close = asf_read_close,
1524 .read_seek = asf_read_seek,
1525 .read_timestamp = asf_read_pts,
20234a4b 1526 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,
75b7feae 1527 .priv_class = &asf_class,
c9a65ca8 1528};