790102b3787bfaf26351a48da07fc550ee472030
[libav.git] / libavformat / asfdec.c
1 /*
2 * Microsoft Advanced Streaming Format demuxer
3 * Copyright (c) 2014 Alexandra Hájková
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/attributes.h"
23 #include "libavutil/avassert.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/bswap.h"
26 #include "libavutil/common.h"
27 #include "libavutil/dict.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/time_internal.h"
32
33 #include "avformat.h"
34 #include "avio_internal.h"
35 #include "avlanguage.h"
36 #include "id3v2.h"
37 #include "internal.h"
38 #include "riff.h"
39 #include "asf.h"
40 #include "asfcrypt.h"
41
42 #define ASF_BOOL 0x2
43 #define ASF_WORD 0x5
44 #define ASF_GUID 0x6
45 #define ASF_DWORD 0x3
46 #define ASF_QWORD 0x4
47 #define ASF_UNICODE 0x0
48 #define ASF_FLAG_BROADCAST 0x1
49 #define ASF_BYTE_ARRAY 0x1
50 #define ASF_TYPE_AUDIO 0x2
51 #define ASF_TYPE_VIDEO 0x1
52 #define ASF_STREAM_NUM 0x7F
53 #define ASF_MAX_STREAMS 128
54 #define BMP_HEADER_SIZE 40
55 #define ASF_NUM_OF_PAYLOADS 0x3F
56 #define ASF_ERROR_CORRECTION_LENGTH_TYPE 0x60
57 #define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2
58
59 typedef struct GUIDParseTable {
60 const char *name;
61 ff_asf_guid guid;
62 int (*read_object)(AVFormatContext *, const struct GUIDParseTable *);
63 int is_subobject;
64 } GUIDParseTable;
65
66 typedef struct ASFPacket {
67 AVPacket avpkt;
68 int64_t dts;
69 uint32_t frame_num; // ASF payloads with the same number are parts of the same frame
70 int flags;
71 int data_size;
72 int duration;
73 int size_left;
74 uint8_t stream_index;
75 } ASFPacket;
76
77 typedef struct ASFStream {
78 uint8_t stream_index; // from packet header
79 int index; // stream index in AVFormatContext, set in asf_read_stream_properties
80 int type;
81 int indexed; // added index entries from the Simple Index Object or not
82 int8_t span; // for deinterleaving
83 uint16_t virtual_pkt_len;
84 uint16_t virtual_chunk_len;
85 int16_t lang_idx;
86 ASFPacket pkt;
87 } ASFStream;
88
89 typedef struct ASFStreamData{
90 char langs[32];
91 AVDictionary *asf_met; // for storing per-stream metadata
92 AVRational aspect_ratio;
93 } ASFStreamData;
94
95 typedef struct ASFContext {
96 int data_reached;
97 int is_simple_index; // is simple index present or not 1/0
98 int is_header;
99
100 uint64_t preroll;
101 uint64_t nb_packets; // ASF packets
102 uint32_t packet_size;
103 int64_t send_time;
104 int duration;
105
106 uint32_t b_flags; // flags with broadcast flag
107 uint32_t prop_flags; // file properties object flags
108
109 uint64_t data_size; // data object size
110 uint64_t unknown_size; // size of the unknown object
111
112 int64_t offset; // offset of the current object
113
114 int64_t data_offset;
115 int64_t first_packet_offset; // packet offset
116 int64_t unknown_offset; // for top level header objects or subobjects without specified behavior
117
118 // ASF file must not contain more than 128 streams according to the specification
119 ASFStream *asf_st[ASF_MAX_STREAMS];
120 ASFStreamData asf_sd[ASF_MAX_STREAMS];
121 int nb_streams;
122
123 int stream_index; // from packet header, for the subpayload case
124
125 // packet parameteres
126 uint64_t sub_header_offset; // offset of subplayload header
127 int64_t sub_dts;
128 uint8_t dts_delta; // for subpayloads
129 uint32_t packet_size_internal; // packet size stored inside ASFPacket, can be 0
130 int64_t dts;
131 int64_t packet_offset; // offset of the current packet inside Data Object
132 uint32_t pad_len; // padding after payload
133 uint32_t rep_data_len;
134
135 // packet state
136 uint64_t sub_left; // subpayloads left or not
137 unsigned int nb_sub; // number of subpayloads read so far from the current ASF packet
138 uint16_t mult_sub_len; // total length of subpayloads array inside multiple payload
139 uint64_t nb_mult_left; // multiple payloads left
140 int return_subpayload;
141 enum {
142 PARSE_PACKET_HEADER,
143 READ_SINGLE,
144 READ_MULTI,
145 READ_MULTI_SUB
146 } state;
147 } ASFContext;
148
149 static int detect_unknown_subobject(AVFormatContext *s, int64_t offset, int64_t size);
150 static const GUIDParseTable *find_guid(ff_asf_guid guid);
151
152 static int asf_probe(AVProbeData *pd)
153 {
154 /* check file header */
155 if (!ff_guidcmp(pd->buf, &ff_asf_header))
156 return AVPROBE_SCORE_MAX;
157 else
158 return 0;
159 }
160
161 static void swap_guid(ff_asf_guid guid)
162 {
163 FFSWAP(unsigned char, guid[0], guid[3]);
164 FFSWAP(unsigned char, guid[1], guid[2]);
165 FFSWAP(unsigned char, guid[4], guid[5]);
166 FFSWAP(unsigned char, guid[6], guid[7]);
167 }
168
169 static void align_position(AVIOContext *pb, int64_t offset, uint64_t size)
170 {
171 if (avio_tell(pb) != offset + size)
172 avio_seek(pb, offset + size, SEEK_SET);
173 }
174
175 static int asf_read_unknown(AVFormatContext *s, const GUIDParseTable *g)
176 {
177 ASFContext *asf = s->priv_data;
178 AVIOContext *pb = s->pb;
179 uint64_t size = avio_rl64(pb);
180 int ret;
181
182 if (asf->is_header)
183 asf->unknown_size = size;
184 asf->is_header = 0;
185 if (!g->is_subobject) {
186 if (!(ret = strcmp(g->name, "Header Extension")))
187 avio_skip(pb, 22); // skip reserved fields and Data Size
188 if ((ret = detect_unknown_subobject(s, asf->unknown_offset,
189 asf->unknown_size)) < 0)
190 return ret;
191 } else
192 avio_skip(pb, size - 24);
193
194 return 0;
195 }
196
197 static int get_asf_string(AVIOContext *pb, int maxlen, char *buf, int buflen)
198 {
199 char *q = buf;
200 int ret = 0;
201 if (buflen <= 0)
202 return AVERROR(EINVAL);
203 while (ret + 1 < maxlen) {
204 uint8_t tmp;
205 uint32_t ch;
206 GET_UTF16(ch, (ret += 2) <= maxlen ? avio_rl16(pb) : 0, break;);
207 PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)
208 }
209 *q = 0;
210
211 return ret;
212 }
213
214 static int asf_read_marker(AVFormatContext *s, const GUIDParseTable *g)
215 {
216 ASFContext *asf = s->priv_data;
217 AVIOContext *pb = s->pb;
218 uint64_t size = avio_rl64(pb);
219 int i, nb_markers, ret;
220 size_t len;
221 char name[1024];
222
223 avio_skip(pb, 8);
224 avio_skip(pb, 8); // skip reserved GUID
225 nb_markers = avio_rl32(pb);
226 avio_skip(pb, 2); // skip reserved field
227 len = avio_rl16(pb);
228 for (i = 0; i < len; i++)
229 avio_skip(pb, 1);
230
231 for (i = 0; i < nb_markers; i++) {
232 int64_t pts;
233
234 avio_skip(pb, 8);
235 pts = avio_rl64(pb);
236 pts -= asf->preroll * 10000;
237 avio_skip(pb, 2); // entry length
238 avio_skip(pb, 4); // send time
239 avio_skip(pb, 4); // flags
240 len = avio_rl32(pb);
241
242 if ((ret = avio_get_str16le(pb, len, name,
243 sizeof(name))) < len)
244 avio_skip(pb, len - ret);
245 avpriv_new_chapter(s, i, (AVRational) { 1, 10000000 }, pts,
246 AV_NOPTS_VALUE, name);
247 }
248 align_position(pb, asf->offset, size);
249
250 return 0;
251 }
252
253 static int asf_read_metadata(AVFormatContext *s, const char *title, uint16_t len,
254 unsigned char *ch, uint16_t buflen)
255 {
256 AVIOContext *pb = s->pb;
257
258 avio_get_str16le(pb, len, ch, buflen);
259 if (ch[0]) {
260 if (av_dict_set(&s->metadata, title, ch, 0) < 0)
261 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
262 }
263
264 return 0;
265 }
266
267 static int asf_read_value(AVFormatContext *s, uint8_t *name, uint16_t name_len,
268 uint16_t val_len, int type, AVDictionary **met)
269 {
270 int ret;
271 uint8_t *value;
272 uint16_t buflen = 2 * val_len + 1;
273 AVIOContext *pb = s->pb;
274
275 value = av_malloc(buflen);
276 if (!value)
277 return AVERROR(ENOMEM);
278 if (type == ASF_UNICODE) {
279 // get_asf_string reads UTF-16 and converts it to UTF-8 which needs longer buffer
280 if ((ret = get_asf_string(pb, val_len, value, buflen)) < 0)
281 goto failed;
282 if (av_dict_set(met, name, value, 0) < 0)
283 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
284 } else {
285 char buf[256];
286 if (val_len > sizeof(buf)) {
287 ret = AVERROR_INVALIDDATA;
288 goto failed;
289 }
290 if ((ret = avio_read(pb, value, val_len)) < 0)
291 goto failed;
292 if (ret < 2 * val_len)
293 value[ret] = '\0';
294 else
295 value[2 * val_len - 1] = '\0';
296 snprintf(buf, sizeof(buf), "%s", value);
297 if (av_dict_set(met, name, buf, 0) < 0)
298 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
299 }
300 av_freep(&value);
301
302 return 0;
303
304 failed:
305 av_freep(&value);
306 return ret;
307 }
308
309 static int asf_read_generic_value(AVFormatContext *s, uint8_t *name, uint16_t name_len,
310 int type, AVDictionary **met)
311 {
312 AVIOContext *pb = s->pb;
313 uint64_t value;
314 char buf[32];
315
316 switch (type) {
317 case ASF_BOOL:
318 value = avio_rl32(pb);
319 break;
320 case ASF_DWORD:
321 value = avio_rl32(pb);
322 break;
323 case ASF_QWORD:
324 value = avio_rl64(pb);
325 break;
326 case ASF_WORD:
327 value = avio_rl16(pb);
328 break;
329 default:
330 av_freep(&name);
331 return AVERROR_INVALIDDATA;
332 }
333 snprintf(buf, sizeof(buf), "%"PRIu64, value);
334 if (av_dict_set(met, name, buf, 0) < 0)
335 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
336
337 return 0;
338 }
339
340 /* MSDN claims that this should be "compatible with the ID3 frame, APIC",
341 * but in reality this is only loosely similar */
342 static int asf_read_picture(AVFormatContext *s, int len)
343 {
344 ASFContext *asf = s->priv_data;
345 AVPacket pkt = { 0 };
346 const CodecMime *mime = ff_id3v2_mime_tags;
347 enum AVCodecID id = AV_CODEC_ID_NONE;
348 char mimetype[64];
349 uint8_t *desc = NULL;
350 AVStream *st = NULL;
351 int ret, type, picsize, desc_len;
352 ASFStream *asf_st;
353
354 /* type + picsize + mime + desc */
355 if (len < 1 + 4 + 2 + 2) {
356 av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
357 return AVERROR_INVALIDDATA;
358 }
359
360 /* picture type */
361 type = avio_r8(s->pb);
362 len--;
363 if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
364 av_log(s, AV_LOG_WARNING, "Unknown attached picture type: %d.\n", type);
365 type = 0;
366 }
367
368 /* picture data size */
369 picsize = avio_rl32(s->pb);
370 len -= 4;
371
372 /* picture MIME type */
373 len -= avio_get_str16le(s->pb, len, mimetype, sizeof(mimetype));
374 while (mime->id != AV_CODEC_ID_NONE) {
375 if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
376 id = mime->id;
377 break;
378 }
379 mime++;
380 }
381 if (id == AV_CODEC_ID_NONE) {
382 av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
383 mimetype);
384 return 0;
385 }
386
387 if (picsize >= len) {
388 av_log(s, AV_LOG_ERROR, "Invalid attached picture data size: %d >= %d.\n",
389 picsize, len);
390 return AVERROR_INVALIDDATA;
391 }
392
393 /* picture description */
394 desc_len = (len - picsize) * 2 + 1;
395 desc = av_malloc(desc_len);
396 if (!desc)
397 return AVERROR(ENOMEM);
398 len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len);
399
400 ret = av_get_packet(s->pb, &pkt, picsize);
401 if (ret < 0)
402 goto fail;
403
404 st = avformat_new_stream(s, NULL);
405 if (!st) {
406 ret = AVERROR(ENOMEM);
407 goto fail;
408 }
409 asf->asf_st[asf->nb_streams] = av_mallocz(sizeof(*asf_st));
410 asf_st = asf->asf_st[asf->nb_streams];
411 if (!asf_st) {
412 ret = AVERROR(ENOMEM);
413 goto fail;
414 }
415
416 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
417 st->codec->codec_type = asf_st->type = AVMEDIA_TYPE_VIDEO;
418 st->codec->codec_id = id;
419 st->attached_pic = pkt;
420 st->attached_pic.stream_index = asf_st->index = st->index;
421 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
422
423 asf->nb_streams++;
424
425 if (*desc) {
426 if (av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL) < 0)
427 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
428 } else
429 av_freep(&desc);
430
431 if (av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0) < 0)
432 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
433
434 return 0;
435
436 fail:
437 av_freep(&desc);
438 av_free_packet(&pkt);
439 return ret;
440 }
441
442 static void get_id3_tag(AVFormatContext *s, int len)
443 {
444 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
445
446 ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
447 if (id3v2_extra_meta)
448 ff_id3v2_parse_apic(s, &id3v2_extra_meta);
449 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
450 }
451
452 static int process_metadata(AVFormatContext *s, uint8_t *name, uint16_t name_len,
453 uint16_t val_len, uint16_t type, AVDictionary **met)
454 {
455 int ret;
456 ff_asf_guid guid;
457
458 if (val_len) {
459 switch (type) {
460 case ASF_UNICODE:
461 asf_read_value(s, name, name_len, val_len, type, met);
462 break;
463 case ASF_BYTE_ARRAY:
464 if (!strcmp(name, "WM/Picture")) // handle cover art
465 asf_read_picture(s, val_len);
466 else if (!strcmp(name, "ID3")) // handle ID3 tag
467 get_id3_tag(s, val_len);
468 else
469 asf_read_value(s, name, name_len, val_len, type, met);
470 break;
471 case ASF_GUID:
472 ff_get_guid(s->pb, &guid);
473 break;
474 default:
475 if ((ret = asf_read_generic_value(s, name, name_len, type, met)) < 0)
476 return ret;
477 break;
478 }
479 }
480 av_freep(&name);
481
482 return 0;
483 }
484
485 static int asf_read_ext_content(AVFormatContext *s, const GUIDParseTable *g)
486 {
487 ASFContext *asf = s->priv_data;
488 AVIOContext *pb = s->pb;
489 uint64_t size = avio_rl64(pb);
490 uint16_t nb_desc = avio_rl16(pb);
491 int i, ret;
492
493 for (i = 0; i < nb_desc; i++) {
494 uint16_t name_len, type, val_len;
495 uint8_t *name = NULL;
496
497 name_len = avio_rl16(pb);
498 if (!name_len)
499 return AVERROR_INVALIDDATA;
500 name = av_malloc(name_len);
501 if (!name)
502 return AVERROR(ENOMEM);
503 avio_get_str16le(pb, name_len, name,
504 name_len);
505 type = avio_rl16(pb);
506 val_len = avio_rl16(pb);
507
508 if ((ret = process_metadata(s, name, name_len, val_len, type, &s->metadata)) < 0)
509 return ret;
510 }
511
512 align_position(pb, asf->offset, size);
513 return 0;
514 }
515
516 static AVStream *find_stream(AVFormatContext *s, uint16_t st_num)
517 {
518 AVStream *st = NULL;
519 ASFContext *asf = s->priv_data;
520 int i;
521
522 for (i = 0; i < asf->nb_streams; i++) {
523 if (asf->asf_st[i]->stream_index == st_num) {
524 st = s->streams[asf->asf_st[i]->index];
525 break;
526 }
527 }
528
529 return st;
530 }
531
532 static void asf_store_aspect_ratio(AVFormatContext *s, uint8_t st_num, uint8_t *name)
533 {
534 ASFContext *asf = s->priv_data;
535 AVIOContext *pb = s->pb;
536 uint16_t value = 0;
537
538 value = avio_rl16(pb);
539
540 if (st_num < ASF_MAX_STREAMS) {
541 if (!strcmp(name, "AspectRatioX"))
542 asf->asf_sd[st_num].aspect_ratio.num = value;
543 else
544 asf->asf_sd[st_num].aspect_ratio.den = value;
545 }
546 }
547
548 static int asf_read_metadata_obj(AVFormatContext *s, const GUIDParseTable *g)
549 {
550 ASFContext *asf = s->priv_data;
551 AVIOContext *pb = s->pb;
552 uint64_t size = avio_rl64(pb);
553 uint16_t nb_recs = avio_rl16(pb); // number of records in the Description Records list
554 int i, ret;
555
556 for (i = 0; i < nb_recs; i++) {
557 uint16_t name_len, buflen, type, val_len, st_num;
558 uint8_t *name = NULL;
559
560 avio_skip(pb, 2); // skip reserved field
561 st_num = avio_rl16(pb);
562 name_len = avio_rl16(pb);
563 buflen = 2 * name_len + 1;
564 if (!name_len)
565 break;
566 type = avio_rl16(pb);
567 val_len = avio_rl32(pb);
568 name = av_malloc(name_len);
569 if (!name)
570 return AVERROR(ENOMEM);
571 avio_get_str16le(pb, name_len, name,
572 buflen);
573
574 if (!strcmp(name, "AspectRatioX") || !strcmp(name, "AspectRatioY")) {
575 asf_store_aspect_ratio(s, st_num, name);
576 } else {
577 if (st_num < ASF_MAX_STREAMS) {
578 if ((ret = process_metadata(s, name, name_len, val_len, type,
579 &asf->asf_sd[st_num].asf_met)) < 0)
580 break;
581 } else
582 av_freep(&name);
583 }
584 }
585
586 align_position(pb, asf->offset, size);
587 return 0;
588 }
589
590 static int asf_read_content_desc(AVFormatContext *s, const GUIDParseTable *g)
591 {
592 ASFContext *asf = s->priv_data;
593 AVIOContext *pb = s->pb;
594 int i;
595 static const char *const titles[] =
596 { "Title", "Author", "Copyright", "Description", "Rate" };
597 uint16_t len[5], buflen[5] = { 0 };
598 uint8_t *ch;
599 uint64_t size = avio_rl64(pb);
600
601 for (i = 0; i < 5; i++) {
602 len[i] = avio_rl16(pb);
603 // utf8 string should be <= 2 * utf16 string, extra byte for the terminator
604 buflen[i] = 2 * len[i] + 1;
605 }
606
607 for (i = 0; i < 5; i++) {
608 ch = av_malloc(buflen[i]);
609 if (!ch)
610 return(AVERROR(ENOMEM));
611 asf_read_metadata(s, titles[i], len[i], ch, buflen[i]);
612 av_freep(&ch);
613 }
614 align_position(pb, asf->offset, size);
615
616 return 0;
617 }
618
619 static int asf_read_properties(AVFormatContext *s, const GUIDParseTable *g)
620 {
621 ASFContext *asf = s->priv_data;
622 AVIOContext *pb = s->pb;
623 uint64_t creation_time;
624
625 avio_rl64(pb); // read object size
626 avio_skip(pb, 16); // skip File ID
627 avio_skip(pb, 8); // skip File size
628 creation_time = avio_rl64(pb);
629 if (!(asf->b_flags & ASF_FLAG_BROADCAST)) {
630 struct tm tmbuf;
631 struct tm *tm;
632 char buf[64];
633
634 // creation date is in 100 ns units from 1 Jan 1601, conversion to s
635 creation_time /= 10000000;
636 // there are 11644473600 seconds between 1 Jan 1601 and 1 Jan 1970
637 creation_time -= 11644473600;
638 tm = gmtime_r(&creation_time, &tmbuf);
639 if (tm) {
640 if (!strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm))
641 buf[0] = '\0';
642 } else
643 buf[0] = '\0';
644 if (buf[0]) {
645 if (av_dict_set(&s->metadata, "creation_time", buf, 0) < 0)
646 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
647 }
648 }
649 asf->nb_packets = avio_rl64(pb);
650 asf->duration = avio_rl64(pb) / 10000; // stream duration
651 avio_skip(pb, 8); // skip send duration
652 asf->preroll = avio_rl64(pb);
653 asf->duration -= asf->preroll;
654 asf->b_flags = avio_rl32(pb);
655 avio_skip(pb, 4); // skip minimal packet size
656 asf->packet_size = avio_rl32(pb);
657 avio_skip(pb, 4); // skip max_bitrate
658
659 return 0;
660 }
661
662 static int parse_video_info(AVIOContext *pb, AVStream *st)
663 {
664 uint16_t size;
665 unsigned int tag;
666
667 st->codec->width = avio_rl32(pb);
668 st->codec->height = avio_rl32(pb);
669 avio_skip(pb, 1); // skip reserved flags
670 size = avio_rl16(pb); // size of the Format Data
671 tag = ff_get_bmp_header(pb, st);
672 st->codec->codec_tag = tag;
673 st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag);
674
675 if (size > BMP_HEADER_SIZE) {
676 int ret;
677 st->codec->extradata_size = size - BMP_HEADER_SIZE;
678 if (!(st->codec->extradata = av_malloc(st->codec->extradata_size +
679 AV_INPUT_BUFFER_PADDING_SIZE))) {
680 st->codec->extradata_size = 0;
681 return AVERROR(ENOMEM);
682 }
683 memset(st->codec->extradata + st->codec->extradata_size , 0,
684 AV_INPUT_BUFFER_PADDING_SIZE);
685 if ((ret = avio_read(pb, st->codec->extradata,
686 st->codec->extradata_size)) < 0)
687 return ret;
688 }
689 return 0;
690 }
691
692 static int asf_read_stream_properties(AVFormatContext *s, const GUIDParseTable *g)
693 {
694 ASFContext *asf = s->priv_data;
695 AVIOContext *pb = s->pb;
696 uint64_t size;
697 uint32_t err_data_len, ts_data_len; // type specific data length
698 uint16_t flags;
699 ff_asf_guid stream_type;
700 enum AVMediaType type;
701 int i, ret;
702 uint8_t stream_index;
703 AVStream *st;
704 ASFStream *asf_st;
705
706 // ASF file must not contain more than 128 streams according to the specification
707 if (asf->nb_streams >= ASF_MAX_STREAMS)
708 return AVERROR_INVALIDDATA;
709
710 size = avio_rl64(pb);
711 ff_get_guid(pb, &stream_type);
712 if (!ff_guidcmp(&stream_type, &ff_asf_audio_stream))
713 type = AVMEDIA_TYPE_AUDIO;
714 else if (!ff_guidcmp(&stream_type, &ff_asf_video_stream))
715 type = AVMEDIA_TYPE_VIDEO;
716 else if (!ff_guidcmp(&stream_type, &ff_asf_jfif_media))
717 type = AVMEDIA_TYPE_VIDEO;
718 else if (!ff_guidcmp(&stream_type, &ff_asf_command_stream))
719 type = AVMEDIA_TYPE_DATA;
720 else if (!ff_guidcmp(&stream_type,
721 &ff_asf_ext_stream_embed_stream_header))
722 type = AVMEDIA_TYPE_UNKNOWN;
723 else
724 return AVERROR_INVALIDDATA;
725
726 ff_get_guid(pb, &stream_type); // error correction type
727 avio_skip(pb, 8); // skip the time offset
728 ts_data_len = avio_rl32(pb);
729 err_data_len = avio_rl32(pb);
730 flags = avio_rl16(pb); // bit 15 - Encrypted Content
731
732 stream_index = flags & ASF_STREAM_NUM;
733 for (i = 0; i < asf->nb_streams; i++)
734 if (stream_index == asf->asf_st[i]->stream_index) {
735 av_log(s, AV_LOG_WARNING,
736 "Duplicate stream found, this stream will be ignored.\n");
737 align_position(pb, asf->offset, size);
738 return 0;
739 }
740
741 st = avformat_new_stream(s, NULL);
742 if (!st)
743 return AVERROR(ENOMEM);
744 avpriv_set_pts_info(st, 32, 1, 1000); // pts should be dword, in milliseconds
745 st->codec->codec_type = type;
746 asf->asf_st[asf->nb_streams] = av_mallocz(sizeof(*asf_st));
747 if (!asf->asf_st[asf->nb_streams])
748 return AVERROR(ENOMEM);
749 asf_st = asf->asf_st[asf->nb_streams];
750 asf->nb_streams++;
751 asf_st->stream_index = stream_index;
752 asf_st->index = st->index;
753 asf_st->indexed = 0;
754 st->id = flags & ASF_STREAM_NUM;
755 av_init_packet(&asf_st->pkt.avpkt);
756 asf_st->pkt.data_size = 0;
757 avio_skip(pb, 4); // skip reserved field
758
759 switch (type) {
760 case AVMEDIA_TYPE_AUDIO:
761 asf_st->type = AVMEDIA_TYPE_AUDIO;
762 if ((ret = ff_get_wav_header(s, pb, st->codec, ts_data_len)) < 0)
763 return ret;
764 break;
765 case AVMEDIA_TYPE_VIDEO:
766 asf_st->type = AVMEDIA_TYPE_VIDEO;
767 if ((ret = parse_video_info(pb, st)) < 0)
768 return ret;
769 break;
770 default:
771 avio_skip(pb, ts_data_len);
772 break;
773 }
774
775 if (err_data_len) {
776 if (type == AVMEDIA_TYPE_AUDIO) {
777 uint8_t span = avio_r8(pb);
778 if (span > 1) {
779 asf_st->span = span;
780 asf_st->virtual_pkt_len = avio_rl16(pb);
781 asf_st->virtual_chunk_len = avio_rl16(pb);
782 if (!asf_st->virtual_chunk_len || !asf_st->virtual_pkt_len)
783 return AVERROR_INVALIDDATA;
784 avio_skip(pb, err_data_len - 5);
785 } else
786 avio_skip(pb, err_data_len - 1);
787 } else
788 avio_skip(pb, err_data_len);
789 }
790
791 align_position(pb, asf->offset, size);
792
793 return 0;
794 }
795
796 static void set_language(AVFormatContext *s, const char *rfc1766, AVDictionary **met)
797 {
798 // language abbr should contain at least 2 chars
799 if (rfc1766 && strlen(rfc1766) > 1) {
800 const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
801 const char *iso6392 = av_convert_lang_to(primary_tag,
802 AV_LANG_ISO639_2_BIBL);
803 if (iso6392)
804 if (av_dict_set(met, "language", iso6392, 0) < 0)
805 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
806 }
807 }
808
809 static int asf_read_ext_stream_properties(AVFormatContext *s, const GUIDParseTable *g)
810 {
811 ASFContext *asf = s->priv_data;
812 AVIOContext *pb = s->pb;
813 AVStream *st = NULL;
814 ff_asf_guid guid;
815 uint16_t nb_st_name, nb_pay_exts, st_num, lang_idx;
816 int i, ret;
817 uint32_t bitrate;
818 uint64_t start_time, end_time, time_per_frame;
819 uint64_t size = avio_rl64(pb);
820
821 start_time = avio_rl64(pb);
822 end_time = avio_rl64(pb);
823 bitrate = avio_rl32(pb);
824 avio_skip(pb, 28); // skip some unused values
825 st_num = avio_rl16(pb);
826 st_num &= ASF_STREAM_NUM;
827 lang_idx = avio_rl16(pb); // Stream Language ID Index
828 for (i = 0; i < asf->nb_streams; i++) {
829 if (st_num == asf->asf_st[i]->stream_index) {
830 st = s->streams[asf->asf_st[i]->index];
831 asf->asf_st[i]->lang_idx = lang_idx;
832 break;
833 }
834 }
835 time_per_frame = avio_rl64(pb); // average time per frame
836 if (st) {
837 st->start_time = start_time;
838 st->duration = end_time - start_time;
839 st->codec->bit_rate = bitrate;
840 st->avg_frame_rate.num = 10000000;
841 st->avg_frame_rate.den = time_per_frame;
842 }
843 nb_st_name = avio_rl16(pb);
844 nb_pay_exts = avio_rl16(pb);
845 for (i = 0; i < nb_st_name; i++) {
846 uint16_t len;
847
848 avio_rl16(pb); // Language ID Index
849 len = avio_rl16(pb);
850 avio_skip(pb, len);
851 }
852
853 for (i = 0; i < nb_pay_exts; i++) {
854 uint32_t len;
855 avio_skip(pb, 16); // Extension System ID
856 avio_skip(pb, 2); // Extension Data Size
857 len = avio_rl32(pb);
858 avio_skip(pb, len);
859 }
860
861 if ((ret = ff_get_guid(pb, &guid)) < 0) {
862 align_position(pb, asf->offset, size);
863
864 return 0;
865 }
866
867 g = find_guid(guid);
868 if (g && !(strcmp(g->name, "Stream Properties"))) {
869 if ((ret = g->read_object(s, g)) < 0)
870 return ret;
871 }
872
873 align_position(pb, asf->offset, size);
874 return 0;
875 }
876
877 static int asf_read_language_list(AVFormatContext *s, const GUIDParseTable *g)
878 {
879 ASFContext *asf = s->priv_data;
880 AVIOContext *pb = s->pb;
881 int i, ret;
882 uint64_t size = avio_rl64(pb);
883 uint16_t nb_langs = avio_rl16(pb);
884
885 if (nb_langs < ASF_MAX_STREAMS) {
886 for (i = 0; i < nb_langs; i++) {
887 size_t len;
888 len = avio_r8(pb);
889 if (!len)
890 len = 6;
891 if ((ret = get_asf_string(pb, len, asf->asf_sd[i].langs,
892 sizeof(asf->asf_sd[i].langs))) < 0) {
893 return ret;
894 }
895 }
896 }
897
898 align_position(pb, asf->offset, size);
899 return 0;
900 }
901
902 // returns data object offset when reading this object for the first time
903 static int asf_read_data(AVFormatContext *s, const GUIDParseTable *g)
904 {
905 ASFContext *asf = s->priv_data;
906 AVIOContext *pb = s->pb;
907 uint64_t size = asf->data_size = avio_rl64(pb);
908 int i;
909
910 if (!asf->data_reached) {
911 asf->data_reached = 1;
912 asf->data_offset = asf->offset;
913 }
914
915 for (i = 0; i < asf->nb_streams; i++) {
916 if (!(asf->b_flags & ASF_FLAG_BROADCAST))
917 s->streams[i]->duration = asf->duration;
918 }
919 asf->nb_mult_left = 0;
920 asf->sub_left = 0;
921 asf->state = PARSE_PACKET_HEADER;
922 asf->return_subpayload = 0;
923 asf->packet_size_internal = 0;
924 avio_skip(pb, 16); // skip File ID
925 size = avio_rl64(pb); // Total Data Packets
926 if (size != asf->nb_packets)
927 av_log(s, AV_LOG_WARNING,
928 "Number of Packets from File Properties Object is not equal to Total"
929 "Datapackets value! num of packets %"PRIu64" total num %"PRIu64".\n",
930 size, asf->nb_packets);
931 avio_skip(pb, 2); // skip reserved field
932 asf->first_packet_offset = avio_tell(pb);
933 if (pb->seekable && !(asf->b_flags & ASF_FLAG_BROADCAST))
934 align_position(pb, asf->offset, asf->data_size);
935
936 return 0;
937 }
938
939 static int asf_read_simple_index(AVFormatContext *s, const GUIDParseTable *g)
940 {
941 ASFContext *asf = s->priv_data;
942 AVIOContext *pb = s->pb;
943 AVStream *st = NULL;
944 uint64_t interval; // index entry time interval in 100 ns units, usually it's 1s
945 uint32_t pkt_num, nb_entries;
946 int32_t prev_pkt_num = -1;
947 int i;
948 uint64_t size = avio_rl64(pb);
949
950 // simple index objects should be ordered by stream number, this loop tries to find
951 // the first not indexed video stream
952 for (i = 0; i < asf->nb_streams; i++) {
953 if ((asf->asf_st[i]->type == AVMEDIA_TYPE_VIDEO) && !asf->asf_st[i]->indexed) {
954 asf->asf_st[i]->indexed = 1;
955 st = s->streams[asf->asf_st[i]->index];
956 break;
957 }
958 }
959 if (!st) {
960 avio_skip(pb, size - 24); // if there's no video stream, skip index object
961 return 0;
962 }
963 avio_skip(pb, 16); // skip File ID
964 interval = avio_rl64(pb);
965 avio_skip(pb, 4);
966 nb_entries = avio_rl32(pb);
967 for (i = 0; i < nb_entries; i++) {
968 pkt_num = avio_rl32(pb);
969 avio_skip(pb, 2);
970 if (prev_pkt_num != pkt_num) {
971 av_add_index_entry(st, asf->first_packet_offset + asf->packet_size *
972 pkt_num, av_rescale(interval, i, 10000),
973 asf->packet_size, 0, AVINDEX_KEYFRAME);
974 prev_pkt_num = pkt_num;
975 }
976 }
977 asf->is_simple_index = 1;
978 align_position(pb, asf->offset, size);
979
980 return 0;
981 }
982
983 static const GUIDParseTable gdef[] = {
984 { "Data", { 0x75, 0xB2, 0x26, 0x36, 0x66, 0x8E, 0x11, 0xCF, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, asf_read_data, 1 },
985 { "Simple Index", { 0x33, 0x00, 0x08, 0x90, 0xE5, 0xB1, 0x11, 0xCF, 0x89, 0xF4, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xCB }, asf_read_simple_index, 1 },
986 { "Content Description", { 0x75, 0xB2, 0x26, 0x33, 0x66 ,0x8E, 0x11, 0xCF, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, asf_read_content_desc, 1 },
987 { "Extended Content Description", { 0xD2, 0xD0, 0xA4, 0x40, 0xE3, 0x07, 0x11, 0xD2, 0x97, 0xF0, 0x00, 0xA0, 0xC9, 0x5e, 0xA8, 0x50 }, asf_read_ext_content, 1 },
988 { "Stream Bitrate Properties", { 0x7B, 0xF8, 0x75, 0xCE, 0x46, 0x8D, 0x11, 0xD1, 0x8D, 0x82, 0x00, 0x60, 0x97, 0xC9, 0xA2, 0xB2 }, asf_read_unknown, 1 },
989 { "File Properties", { 0x8C, 0xAB, 0xDC, 0xA1, 0xA9, 0x47, 0x11, 0xCF, 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_properties, 1 },
990 { "Header Extension", { 0x5F, 0xBF, 0x03, 0xB5, 0xA9, 0x2E, 0x11, 0xCF, 0x8E, 0xE3, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_unknown, 0 },
991 { "Stream Properties", { 0xB7, 0xDC, 0x07, 0x91, 0xA9, 0xB7, 0x11, 0xCF, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_stream_properties, 1 },
992 { "Codec List", { 0x86, 0xD1, 0x52, 0x40, 0x31, 0x1D, 0x11, 0xD0, 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 }, asf_read_unknown, 1 },
993 { "Marker", { 0xF4, 0x87, 0xCD, 0x01, 0xA9, 0x51, 0x11, 0xCF, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_marker, 1 },
994 { "Script Command", { 0x1E, 0xFB, 0x1A, 0x30, 0x0B, 0x62, 0x11, 0xD0, 0xA3, 0x9B, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 }, asf_read_unknown, 1 },
995 { "Language List", { 0x7C, 0x43, 0x46, 0xa9, 0xef, 0xe0, 0x4B, 0xFC, 0xB2, 0x29, 0x39, 0x3e, 0xde, 0x41, 0x5c, 0x85 }, asf_read_language_list, 1},
996 { "Padding", { 0x18, 0x06, 0xD4, 0x74, 0xCA, 0xDF, 0x45, 0x09, 0xA4, 0xBA, 0x9A, 0xAB, 0xCB, 0x96, 0xAA, 0xE8 }, asf_read_unknown, 1 },
997 { "DRMv1 Header", { 0x22, 0x11, 0xB3, 0xFB, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 },
998 { "DRMv2 Header", { 0x29, 0x8A, 0xE6, 0x14, 0x26, 0x22, 0x4C, 0x17, 0xB9, 0x35, 0xDA, 0xE0, 0x7E, 0xE9, 0x28, 0x9c }, asf_read_unknown, 1 },
999 { "Index", { 0xD6, 0xE2, 0x29, 0xD3, 0x35, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1000 { "Media Object Index", { 0xFE, 0xB1, 0x03, 0xF8, 0x12, 0xAD, 0x4C, 0x64, 0x84, 0x0F, 0x2A, 0x1D, 0x2F, 0x7A, 0xD4, 0x8C }, asf_read_unknown, 1 },
1001 { "Timecode Index", { 0x3C, 0xB7, 0x3F, 0xD0, 0x0C, 0x4A, 0x48, 0x03, 0x95, 0x3D, 0xED, 0xF7, 0xB6, 0x22, 0x8F, 0x0C }, asf_read_unknown, 0 },
1002 { "Bitrate_Mutual_Exclusion", { 0xD6, 0xE2, 0x29, 0xDC, 0x35, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1003 { "Error Correction", { 0x75, 0xB2, 0x26, 0x35, 0x66, 0x8E, 0x11, 0xCF, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, asf_read_unknown, 1 },
1004 { "Content Branding", { 0x22, 0x11, 0xB3, 0xFA, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 },
1005 { "Content Encryption", { 0x22, 0x11, 0xB3, 0xFB, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 },
1006 { "Extended Content Encryption", { 0x29, 0x8A, 0xE6, 0x14, 0x26, 0x22, 0x4C, 0x17, 0xB9, 0x35, 0xDA, 0xE0, 0x7E, 0xE9, 0x28, 0x9C }, asf_read_unknown, 1 },
1007 { "Digital Signature", { 0x22, 0x11, 0xB3, 0xFC, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 },
1008 { "Extended Stream Properties", { 0x14, 0xE6, 0xA5, 0xCB, 0xC6, 0x72, 0x43, 0x32, 0x83, 0x99, 0xA9, 0x69, 0x52, 0x06, 0x5B, 0x5A }, asf_read_ext_stream_properties, 1 },
1009 { "Advanced Mutual Exclusion", { 0xA0, 0x86, 0x49, 0xCF, 0x47, 0x75, 0x46, 0x70, 0x8A, 0x16, 0x6E, 0x35, 0x35, 0x75, 0x66, 0xCD }, asf_read_unknown, 1 },
1010 { "Group Mutual Exclusion", { 0xD1, 0x46, 0x5A, 0x40, 0x5A, 0x79, 0x43, 0x38, 0xB7, 0x1B, 0xE3, 0x6B, 0x8F, 0xD6, 0xC2, 0x49 }, asf_read_unknown, 1},
1011 { "Stream Prioritization", { 0xD4, 0xFE, 0xD1, 0x5B, 0x88, 0xD3, 0x45, 0x4F, 0x81, 0xF0, 0xED, 0x5C, 0x45, 0x99, 0x9E, 0x24 }, asf_read_unknown, 1 },
1012 { "Bandwidth Sharing Object", { 0xA6, 0x96, 0x09, 0xE6, 0x51, 0x7B, 0x11, 0xD2, 0xB6, 0xAF, 0x00, 0xC0, 0x4F, 0xD9, 0x08, 0xE9 }, asf_read_unknown, 1 },
1013 { "Metadata", { 0xC5, 0xF8, 0xCB, 0xEA, 0x5B, 0xAF, 0x48, 0x77, 0x84, 0x67, 0xAA, 0x8C, 0x44, 0xFA, 0x4C, 0xCA }, asf_read_metadata_obj, 1 },
1014 { "Metadata Library", { 0x44, 0x23, 0x1C, 0x94, 0x94, 0x98, 0x49, 0xD1, 0xA1, 0x41, 0x1D, 0x13, 0x4E, 0x45, 0x70, 0x54 }, asf_read_metadata_obj, 1 },
1015 { "Audio Spread", { 0xBF, 0xC3, 0xCD, 0x50, 0x61, 0x8F, 0x11, 0xCF, 0x8B, 0xB2, 0x00, 0xAA, 0x00, 0xB4, 0xE2, 0x20 }, asf_read_unknown, 1 },
1016 { "Index Parameters", { 0xD6, 0xE2, 0x29, 0xDF, 0x35, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1017 { "Content Encryption System Windows Media DRM Network Devices",
1018 { 0x7A, 0x07, 0x9B, 0xB6, 0xDA, 0XA4, 0x4e, 0x12, 0xA5, 0xCA, 0x91, 0xD3, 0x8D, 0xC1, 0x1A, 0x8D }, asf_read_unknown, 1 },
1019 { "Mutex Language", { 0xD6, 0xE2, 0x2A, 0x00, 0x25, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1020 { "Mutex Bitrate", { 0xD6, 0xE2, 0x2A, 0x01, 0x25, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1021 { "Mutex Unknown", { 0xD6, 0xE2, 0x2A, 0x02, 0x25, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1022 { "Bandwith Sharing Exclusive", { 0xAF, 0x60, 0x60, 0xAA, 0x51, 0x97, 0x11, 0xD2, 0xB6, 0xAF, 0x00, 0xC0, 0x4F, 0xD9, 0x08, 0xE9 }, asf_read_unknown, 1 },
1023 { "Bandwith Sharing Partial", { 0xAF, 0x60, 0x60, 0xAB, 0x51, 0x97, 0x11, 0xD2, 0xB6, 0xAF, 0x00, 0xC0, 0x4F, 0xD9, 0x08, 0xE9 }, asf_read_unknown, 1 },
1024 { "Payload Extension System Timecode", { 0x39, 0x95, 0x95, 0xEC, 0x86, 0x67, 0x4E, 0x2D, 0x8F, 0xDB, 0x98, 0x81, 0x4C, 0xE7, 0x6C, 0x1E }, asf_read_unknown, 1 },
1025 { "Payload Extension System File Name", { 0xE1, 0x65, 0xEC, 0x0E, 0x19, 0xED, 0x45, 0xD7, 0xB4, 0xA7, 0x25, 0xCB, 0xD1, 0xE2, 0x8E, 0x9B }, asf_read_unknown, 1 },
1026 { "Payload Extension System Content Type", { 0xD5, 0x90, 0xDC, 0x20, 0x07, 0xBC, 0x43, 0x6C, 0x9C, 0xF7, 0xF3, 0xBB, 0xFB, 0xF1, 0xA4, 0xDC }, asf_read_unknown, 1 },
1027 { "Payload Extension System Pixel Aspect Ratio", { 0x1, 0x1E, 0xE5, 0x54, 0xF9, 0xEA, 0x4B, 0xC8, 0x82, 0x1A, 0x37, 0x6B, 0x74, 0xE4, 0xC4, 0xB8 }, asf_read_unknown, 1 },
1028 { "Payload Extension System Sample Duration", { 0xC6, 0xBD, 0x94, 0x50, 0x86, 0x7F, 0x49, 0x07, 0x83, 0xA3, 0xC7, 0x79, 0x21, 0xB7, 0x33, 0xAD }, asf_read_unknown, 1 },
1029 { "Payload Extension System Encryption Sample ID", { 0x66, 0x98, 0xB8, 0x4E, 0x0A, 0xFA, 0x43, 0x30, 0xAE, 0xB2, 0x1C, 0x0A, 0x98, 0xD7, 0xA4, 0x4D }, asf_read_unknown, 1 },
1030 { "Payload Extension System Degradable JPEG", { 0x00, 0xE1, 0xAF, 0x06, 0x7B, 0xEC, 0x11, 0xD1, 0xA5, 0x82, 0x00, 0xC0, 0x4F, 0xC2, 0x9C, 0xFB }, asf_read_unknown, 1 },
1031 };
1032
1033 #define READ_LEN(flag, name, len) \
1034 do { \
1035 if ((flag) == name ## IS_BYTE) \
1036 len = avio_r8(pb); \
1037 else if ((flag) == name ## IS_WORD) \
1038 len = avio_rl16(pb); \
1039 else if ((flag) == name ## IS_DWORD) \
1040 len = avio_rl32(pb); \
1041 else \
1042 len = 0; \
1043 } while(0)
1044
1045 static int asf_read_subpayload(AVFormatContext *s, AVPacket *pkt, int is_header)
1046 {
1047 ASFContext *asf = s->priv_data;
1048 AVIOContext *pb = s->pb;
1049 uint8_t sub_len;
1050 int ret, i;
1051
1052 if (is_header) {
1053 asf->dts_delta = avio_r8(pb);
1054 if (asf->nb_mult_left) {
1055 asf->mult_sub_len = avio_rl16(pb); // total
1056 }
1057 asf->sub_header_offset = avio_tell(pb);
1058 asf->nb_sub = 0;
1059 asf->sub_left = 1;
1060 }
1061 sub_len = avio_r8(pb);
1062 if ((ret = av_get_packet(pb, pkt, sub_len)) < 0) // each subpayload is entire frame
1063 return ret;
1064 for (i = 0; i < asf->nb_streams; i++) {
1065 if (asf->stream_index == asf->asf_st[i]->stream_index) {
1066 pkt->stream_index = asf->asf_st[i]->index;
1067 break;
1068 }
1069 }
1070 asf->return_subpayload = 1;
1071 if (!sub_len)
1072 asf->return_subpayload = 0;
1073
1074 if (sub_len)
1075 asf->nb_sub++;
1076 pkt->dts = asf->sub_dts + (asf->nb_sub - 1) * asf->dts_delta - asf->preroll;
1077 if (asf->nb_mult_left && (avio_tell(pb) >=
1078 (asf->sub_header_offset + asf->mult_sub_len))) {
1079 asf->sub_left = 0;
1080 asf->nb_mult_left--;
1081 }
1082 if (avio_tell(pb) >= asf->packet_offset + asf->packet_size - asf->pad_len) {
1083 asf->sub_left = 0;
1084 if (!asf->nb_mult_left) {
1085 avio_skip(pb, asf->pad_len);
1086 if (avio_tell(pb) != asf->packet_offset + asf->packet_size) {
1087 if (!asf->packet_size)
1088 return AVERROR_INVALIDDATA;
1089 av_log(s, AV_LOG_WARNING,
1090 "Position %"PRId64" wrong, should be %"PRId64"\n",
1091 avio_tell(pb), asf->packet_offset + asf->packet_size);
1092 avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET);
1093 }
1094 }
1095 }
1096
1097 return 0;
1098 }
1099
1100 static void reset_packet(ASFPacket *asf_pkt)
1101 {
1102 asf_pkt->size_left = 0;
1103 asf_pkt->data_size = 0;
1104 asf_pkt->duration = 0;
1105 asf_pkt->flags = 0;
1106 asf_pkt->dts = 0;
1107 asf_pkt->duration = 0;
1108 av_free_packet(&asf_pkt->avpkt);
1109 av_init_packet(&asf_pkt->avpkt);
1110 }
1111
1112 static int asf_read_replicated_data(AVFormatContext *s, ASFPacket *asf_pkt)
1113 {
1114 ASFContext *asf = s->priv_data;
1115 AVIOContext *pb = s->pb;
1116 int ret;
1117
1118 if (!asf_pkt->data_size) {
1119 asf_pkt->data_size = asf_pkt->size_left = avio_rl32(pb); // read media object size
1120 if (asf_pkt->data_size <= 0)
1121 return AVERROR_EOF;
1122 if ((ret = av_new_packet(&asf_pkt->avpkt, asf_pkt->data_size)) < 0)
1123 return ret;
1124 } else
1125 avio_skip(pb, 4); // reading of media object size is already done
1126 asf_pkt->dts = avio_rl32(pb); // read presentation time
1127 if (asf->rep_data_len && (asf->rep_data_len >= 8))
1128 avio_skip(pb, asf->rep_data_len - 8); // skip replicated data
1129
1130 return 0;
1131 }
1132
1133 static int asf_read_multiple_payload(AVFormatContext *s, AVPacket *pkt,
1134 ASFPacket *asf_pkt)
1135 {
1136 ASFContext *asf = s->priv_data;
1137 AVIOContext *pb = s->pb;
1138 uint16_t pay_len;
1139 unsigned char *p;
1140 int ret;
1141 int skip = 0;
1142
1143 // if replicated lenght is 1, subpayloads are present
1144 if (asf->rep_data_len == 1) {
1145 asf->sub_left = 1;
1146 asf->state = READ_MULTI_SUB;
1147 pkt->flags = asf_pkt->flags;
1148 if ((ret = asf_read_subpayload(s, pkt, 1)) < 0)
1149 return ret;
1150 } else {
1151 if (asf->rep_data_len)
1152 if ((ret = asf_read_replicated_data(s, asf_pkt)) < 0)
1153 return ret;
1154 pay_len = avio_rl16(pb); // payload length should be WORD
1155 if (pay_len > asf->packet_size) {
1156 av_log(s, AV_LOG_ERROR,
1157 "Error: invalid data packet size, pay_len %"PRIu16", "
1158 "asf->packet_size %"PRIu32", offset %"PRId64".\n",
1159 pay_len, asf->packet_size, avio_tell(pb));
1160 return AVERROR_INVALIDDATA;
1161 }
1162 p = asf_pkt->avpkt.data + asf_pkt->data_size - asf_pkt->size_left;
1163 if (pay_len > asf_pkt->size_left) {
1164 av_log(s, AV_LOG_ERROR,
1165 "Error: invalid buffer size, pay_len %d, data size left %d.\n",
1166 pay_len, asf_pkt->size_left);
1167 skip = pay_len - asf_pkt->size_left;
1168 pay_len = asf_pkt->size_left;
1169 }
1170 if ((ret = avio_read(pb, p, pay_len)) < 0)
1171 return ret;
1172 if (s->key && s->keylen == 20)
1173 ff_asfcrypt_dec(s->key, p, ret);
1174 avio_skip(pb, skip);
1175 asf_pkt->size_left -= pay_len;
1176 asf->nb_mult_left--;
1177 }
1178
1179 return 0;
1180 }
1181
1182 static int asf_read_single_payload(AVFormatContext *s, AVPacket *pkt,
1183 ASFPacket *asf_pkt)
1184 {
1185 ASFContext *asf = s->priv_data;
1186 AVIOContext *pb = s->pb;
1187 int64_t offset;
1188 uint64_t size;
1189 unsigned char *p;
1190 int ret;
1191
1192 if (!asf_pkt->data_size) {
1193 asf_pkt->data_size = asf_pkt->size_left = avio_rl32(pb); // read media object size
1194 if (asf_pkt->data_size <= 0)
1195 return AVERROR_EOF;
1196 if ((ret = av_new_packet(&asf_pkt->avpkt, asf_pkt->data_size)) < 0)
1197 return ret;
1198 } else
1199 avio_skip(pb, 4); // skip media object size
1200 asf_pkt->dts = avio_rl32(pb); // read presentation time
1201 if (asf->rep_data_len >= 8)
1202 avio_skip(pb, asf->rep_data_len - 8); // skip replicated data
1203 offset = avio_tell(pb);
1204
1205 // size of the payload - size of the packet without header and padding
1206 if (asf->packet_size_internal)
1207 size = asf->packet_size_internal - offset + asf->packet_offset - asf->pad_len;
1208 else
1209 size = asf->packet_size - offset + asf->packet_offset - asf->pad_len;
1210 if (size > asf->packet_size) {
1211 av_log(s, AV_LOG_ERROR,
1212 "Error: invalid data packet size, offset %"PRId64".\n",
1213 avio_tell(pb));
1214 return AVERROR_INVALIDDATA;
1215 }
1216 p = asf_pkt->avpkt.data + asf_pkt->data_size - asf_pkt->size_left;
1217 if (size > asf_pkt->size_left)
1218 return AVERROR_INVALIDDATA;
1219 if (asf_pkt->size_left > size)
1220 asf_pkt->size_left -= size;
1221 else
1222 asf_pkt->size_left = 0;
1223 if ((ret = avio_read(pb, p, size)) < 0)
1224 return ret;
1225 if (s->key && s->keylen == 20)
1226 ff_asfcrypt_dec(s->key, p, ret);
1227 if (asf->packet_size_internal)
1228 avio_skip(pb, asf->packet_size - asf->packet_size_internal);
1229 avio_skip(pb, asf->pad_len); // skip padding
1230
1231 return 0;
1232 }
1233
1234 static int asf_read_payload(AVFormatContext *s, AVPacket *pkt)
1235 {
1236 ASFContext *asf = s->priv_data;
1237 AVIOContext *pb = s->pb;
1238 int ret, i;
1239 ASFPacket *asf_pkt = NULL;
1240
1241 if (!asf->sub_left) {
1242 uint32_t off_len, media_len;
1243 uint8_t stream_num;
1244
1245 stream_num = avio_r8(pb);
1246 asf->stream_index = stream_num & ASF_STREAM_NUM;
1247 for (i = 0; i < asf->nb_streams; i++) {
1248 if (asf->stream_index == asf->asf_st[i]->stream_index) {
1249 asf_pkt = &asf->asf_st[i]->pkt;
1250 asf_pkt->stream_index = asf->asf_st[i]->index;
1251 asf_pkt->dts = asf->dts;
1252 break;
1253 }
1254 }
1255 if (!asf_pkt)
1256 return AVERROR_INVALIDDATA;
1257 if (stream_num >> 7)
1258 asf_pkt->flags |= AV_PKT_FLAG_KEY;
1259 READ_LEN(asf->prop_flags & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE,
1260 ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_, media_len);
1261 READ_LEN(asf->prop_flags & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE,
1262 ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_, off_len);
1263 READ_LEN(asf->prop_flags & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE,
1264 ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_, asf->rep_data_len);
1265 if (asf_pkt->size_left && (asf_pkt->frame_num != media_len)) {
1266 av_log(s, AV_LOG_WARNING, "Unfinished frame will be ignored\n");
1267 reset_packet(asf_pkt);
1268 }
1269 asf_pkt->frame_num = media_len;
1270 asf->sub_dts = off_len;
1271 if (asf->nb_mult_left) {
1272 if ((ret = asf_read_multiple_payload(s, pkt, asf_pkt)) < 0)
1273 return ret;
1274 } else if (asf->rep_data_len == 1) {
1275 asf->sub_left = 1;
1276 asf->state = READ_SINGLE;
1277 pkt->flags = asf_pkt->flags;
1278 if ((ret = asf_read_subpayload(s, pkt, 1)) < 0)
1279 return ret;
1280 } else {
1281 if ((ret = asf_read_single_payload(s, pkt, asf_pkt)) < 0)
1282 return ret;
1283 }
1284 } else {
1285 for (i = 0; i <= asf->nb_streams; i++) {
1286 if (asf->stream_index == asf->asf_st[i]->stream_index) {
1287 asf_pkt = &asf->asf_st[i]->pkt;
1288 break;
1289 }
1290 }
1291 if (!asf_pkt)
1292 return AVERROR_INVALIDDATA;
1293 pkt->flags = asf_pkt->flags;
1294 pkt->dts = asf_pkt->dts;
1295 pkt->stream_index = asf->asf_st[i]->index;
1296 if ((ret = asf_read_subpayload(s, pkt, 0)) < 0) // read subpayload without its header
1297 return ret;
1298 }
1299
1300 return 0;
1301 }
1302
1303 static int asf_read_packet_header(AVFormatContext *s)
1304 {
1305 ASFContext *asf = s->priv_data;
1306 AVIOContext *pb = s->pb;
1307 uint64_t size;
1308 uint32_t av_unused seq;
1309 unsigned char error_flags, len_flags, pay_flags;
1310
1311 asf->packet_offset = avio_tell(pb);
1312 error_flags = avio_r8(pb); // read Error Correction Flags
1313 if (error_flags & ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT) {
1314 if (!(error_flags & ASF_ERROR_CORRECTION_LENGTH_TYPE)) {
1315 size = error_flags & ASF_PACKET_ERROR_CORRECTION_DATA_SIZE;
1316 avio_skip(pb, size);
1317 }
1318 len_flags = avio_r8(pb);
1319 } else
1320 len_flags = error_flags;
1321 asf->prop_flags = avio_r8(pb);
1322 READ_LEN(len_flags & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE,
1323 ASF_PPI_FLAG_PACKET_LENGTH_FIELD_, asf->packet_size_internal);
1324 READ_LEN(len_flags & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE,
1325 ASF_PPI_FLAG_SEQUENCE_FIELD_, seq);
1326 READ_LEN(len_flags & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE,
1327 ASF_PPI_FLAG_PADDING_LENGTH_FIELD_, asf->pad_len );
1328 asf->send_time = avio_rl32(pb); // send time
1329 avio_skip(pb, 2); // skip duration
1330 if (len_flags & ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT) { // Multiple Payloads present
1331 pay_flags = avio_r8(pb);
1332 asf->nb_mult_left = (pay_flags & ASF_NUM_OF_PAYLOADS);
1333 }
1334
1335 return 0;
1336 }
1337
1338 static int asf_deinterleave(AVFormatContext *s, ASFPacket *asf_pkt, int st_num)
1339 {
1340 ASFContext *asf = s->priv_data;
1341 ASFStream *asf_st = asf->asf_st[st_num];
1342 unsigned char *p = asf_pkt->avpkt.data;
1343 uint16_t pkt_len = asf->asf_st[st_num]->virtual_pkt_len;
1344 uint16_t chunk_len = asf->asf_st[st_num]->virtual_chunk_len;
1345 int nchunks = pkt_len / chunk_len;
1346 AVPacket pkt;
1347 int pos = 0, j, l, ret;
1348
1349
1350 if ((ret = av_new_packet(&pkt, asf_pkt->data_size)) < 0)
1351 return ret;
1352
1353 while (asf_pkt->data_size >= asf_st->span * pkt_len + pos) {
1354 if (pos >= asf_pkt->data_size) {
1355 break;
1356 }
1357 for (l = 0; l < pkt_len; l++) {
1358 if (pos >= asf_pkt->data_size) {
1359 break;
1360 }
1361 for (j = 0; j < asf_st->span; j++) {
1362 if ((pos + chunk_len) >= asf_pkt->data_size)
1363 break;
1364 memcpy(pkt.data + pos,
1365 p + (j * nchunks + l) * chunk_len,
1366 chunk_len);
1367 pos += chunk_len;
1368 }
1369 }
1370 p += asf_st->span * pkt_len;
1371 if (p > asf_pkt->avpkt.data + asf_pkt->data_size)
1372 break;
1373 }
1374 av_free_packet(&asf_pkt->avpkt);
1375 asf_pkt->avpkt = pkt;
1376
1377 return 0;
1378 }
1379
1380 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1381 {
1382 ASFContext *asf = s->priv_data;
1383 AVIOContext *pb = s->pb;
1384 int ret, i;
1385
1386 if ((avio_tell(pb) >= asf->data_offset + asf->data_size) &&
1387 !(asf->b_flags & ASF_FLAG_BROADCAST))
1388 return AVERROR_EOF;
1389 while (!pb->eof_reached) {
1390 if (asf->state == PARSE_PACKET_HEADER) {
1391 asf_read_packet_header(s);
1392 if (!asf->nb_mult_left)
1393 asf->state = READ_SINGLE;
1394 else
1395 asf->state = READ_MULTI;
1396 }
1397 if ((ret = asf_read_payload(s, pkt)) < 0)
1398 return ret;
1399 switch (asf->state) {
1400 case READ_SINGLE:
1401 if (!asf->sub_left)
1402 asf->state = PARSE_PACKET_HEADER;
1403 break;
1404 case READ_MULTI_SUB:
1405 if (!asf->sub_left && !asf->nb_mult_left) {
1406 asf->state = PARSE_PACKET_HEADER;
1407 if (!asf->return_subpayload)
1408 avio_skip(pb, asf->pad_len); // skip padding
1409 if (asf->packet_offset + asf->packet_size > avio_tell(pb))
1410 avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET);
1411 } else if (!asf->sub_left)
1412 asf->state = READ_MULTI;
1413 break;
1414 case READ_MULTI:
1415 if (!asf->nb_mult_left) {
1416 asf->state = PARSE_PACKET_HEADER;
1417 if (!asf->return_subpayload) {
1418 avio_skip(pb, asf->pad_len); // skip padding
1419 }
1420 if (asf->packet_offset + asf->packet_size > avio_tell(pb))
1421 avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET);
1422 }
1423 break;
1424 }
1425 if (asf->return_subpayload) {
1426 asf->return_subpayload = 0;
1427 return 0;
1428 }
1429 for (i = 0; i < s->nb_streams; i++) {
1430 ASFPacket *asf_pkt = &asf->asf_st[i]->pkt;
1431 if (asf_pkt && !asf_pkt->size_left && asf_pkt->data_size) {
1432 if (asf->asf_st[i]->span > 1 &&
1433 asf->asf_st[i]->type == AVMEDIA_TYPE_AUDIO)
1434 if ((ret = asf_deinterleave(s, asf_pkt, i)) < 0)
1435 return ret;
1436 av_packet_move_ref(pkt, &asf_pkt->avpkt);
1437 pkt->stream_index = asf->asf_st[i]->index;
1438 pkt->flags = asf_pkt->flags;
1439 pkt->dts = asf_pkt->dts - asf->preroll;
1440 asf_pkt->data_size = 0;
1441 asf_pkt->frame_num = 0;
1442 return 0;
1443 }
1444 }
1445 }
1446
1447 if (pb->eof_reached)
1448 return AVERROR_EOF;
1449
1450 return 0;
1451 }
1452
1453 static int asf_read_close(AVFormatContext *s)
1454 {
1455 ASFContext *asf = s->priv_data;
1456 int i;
1457
1458 for (i = 0; i < ASF_MAX_STREAMS; i++) {
1459 av_dict_free(&asf->asf_sd[i].asf_met);
1460 if (i < asf->nb_streams) {
1461 av_free_packet(&asf->asf_st[i]->pkt.avpkt);
1462 av_freep(&asf->asf_st[i]);
1463 }
1464 }
1465
1466 asf->nb_streams = 0;
1467 return 0;
1468 }
1469
1470 static void reset_packet_state(AVFormatContext *s)
1471 {
1472 ASFContext *asf = s->priv_data;
1473 int i;
1474
1475 asf->state = PARSE_PACKET_HEADER;
1476 asf->offset = 0;
1477 asf->return_subpayload = 0;
1478 asf->sub_left = 0;
1479 asf->sub_header_offset = 0;
1480 asf->packet_offset = asf->first_packet_offset;
1481 asf->pad_len = 0;
1482 asf->rep_data_len = 0;
1483 asf->dts_delta = 0;
1484 asf->mult_sub_len = 0;
1485 asf->nb_mult_left = 0;
1486 asf->nb_sub = 0;
1487 asf->prop_flags = 0;
1488 asf->sub_dts = 0;
1489 asf->dts = 0;
1490 for (i = 0; i < asf->nb_streams; i++) {
1491 ASFPacket *pkt = &asf->asf_st[i]->pkt;
1492 pkt->size_left = 0;
1493 pkt->data_size = 0;
1494 pkt->duration = 0;
1495 pkt->flags = 0;
1496 pkt->dts = 0;
1497 pkt->duration = 0;
1498 av_free_packet(&pkt->avpkt);
1499 av_init_packet(&pkt->avpkt);
1500 }
1501 }
1502
1503 /*
1504 * Find a timestamp for the requested position within the payload
1505 * where the pos (position) is the offset inside the Data Object.
1506 * When position is not on the packet boundary, asf_read_timestamp tries
1507 * to find the closest packet offset after this position. If this packet
1508 * is a key frame, this packet timestamp is read and an index entry is created
1509 * for the packet. If this packet belongs to the requested stream,
1510 * asf_read_timestamp upgrades pos to the packet beginning offset and
1511 * returns this packet's dts. So returned dts is the dts of the first key frame with
1512 * matching stream number after given position.
1513 */
1514 static int64_t asf_read_timestamp(AVFormatContext *s, int stream_index,
1515 int64_t *pos, int64_t pos_limit)
1516 {
1517 ASFContext *asf = s->priv_data;
1518 int64_t pkt_pos = *pos, pkt_offset, dts = AV_NOPTS_VALUE, data_end;
1519 AVPacket pkt;
1520 int n;
1521
1522 data_end = asf->data_offset + asf->data_size;
1523
1524 n = (pkt_pos - asf->first_packet_offset + asf->packet_size - 1) /
1525 asf->packet_size;
1526 n = av_clip(n, 0, ((data_end - asf->first_packet_offset) / asf->packet_size - 1));
1527 pkt_pos = asf->first_packet_offset + n * asf->packet_size;
1528
1529 avio_seek(s->pb, pkt_pos, SEEK_SET);
1530 pkt_offset = pkt_pos;
1531
1532 reset_packet_state(s);
1533 while (avio_tell(s->pb) < data_end) {
1534
1535 int i, ret, st_found;
1536
1537 av_init_packet(&pkt);
1538 pkt_offset = avio_tell(s->pb);
1539 if ((ret = asf_read_packet(s, &pkt)) < 0) {
1540 dts = AV_NOPTS_VALUE;
1541 return ret;
1542 }
1543 // ASFPacket may contain fragments of packets belonging to different streams,
1544 // pkt_offset is the offset of the first fragment within it.
1545 if ((pkt_offset >= (pkt_pos + asf->packet_size)))
1546 pkt_pos += asf->packet_size;
1547 for (i = 0; i < asf->nb_streams; i++) {
1548 ASFStream *st = asf->asf_st[i];
1549
1550 st_found = 0;
1551 if (pkt.flags & AV_PKT_FLAG_KEY) {
1552 dts = pkt.dts;
1553 if (dts) {
1554 av_add_index_entry(s->streams[pkt.stream_index], pkt_pos,
1555 dts, pkt.size, 0, AVINDEX_KEYFRAME);
1556 if (stream_index == st->index) {
1557 st_found = 1;
1558 break;
1559 }
1560 }
1561 }
1562 }
1563 if (st_found)
1564 break;
1565 av_free_packet(&pkt);
1566 }
1567 *pos = pkt_pos;
1568
1569 av_free_packet(&pkt);
1570 return dts;
1571 }
1572
1573 static int asf_read_seek(AVFormatContext *s, int stream_index,
1574 int64_t timestamp, int flags)
1575 {
1576 ASFContext *asf = s->priv_data;
1577 int idx, ret;
1578
1579 if (s->streams[stream_index]->nb_index_entries && asf->is_simple_index) {
1580 idx = av_index_search_timestamp(s->streams[stream_index], timestamp, flags);
1581 if (idx < 0 || idx >= s->streams[stream_index]->nb_index_entries)
1582 return AVERROR_INVALIDDATA;
1583 avio_seek(s->pb, s->streams[stream_index]->index_entries[idx].pos, SEEK_SET);
1584 } else {
1585 if ((ret = ff_seek_frame_binary(s, stream_index, timestamp, flags)) < 0)
1586 return ret;
1587 }
1588
1589 reset_packet_state(s);
1590
1591 return 0;
1592 }
1593
1594 static const GUIDParseTable *find_guid(ff_asf_guid guid)
1595 {
1596 int j, ret;
1597 const GUIDParseTable *g;
1598
1599 swap_guid(guid);
1600 g = gdef;
1601 for (j = 0; j < FF_ARRAY_ELEMS(gdef); j++) {
1602 if (!(ret = memcmp(guid, g->guid, sizeof(g->guid))))
1603 return g;
1604 g++;
1605 }
1606
1607 return NULL;
1608 }
1609
1610 static int detect_unknown_subobject(AVFormatContext *s, int64_t offset, int64_t size)
1611 {
1612 ASFContext *asf = s->priv_data;
1613 AVIOContext *pb = s->pb;
1614 const GUIDParseTable *g = NULL;
1615 ff_asf_guid guid;
1616 int ret;
1617
1618 while (avio_tell(pb) <= offset + size) {
1619 if (avio_tell(pb) == asf->offset)
1620 break;
1621 asf->offset = avio_tell(pb);
1622 if ((ret = ff_get_guid(pb, &guid)) < 0)
1623 return ret;
1624 g = find_guid(guid);
1625 if (g) {
1626 if ((ret = g->read_object(s, g)) < 0)
1627 return ret;
1628 } else {
1629 GUIDParseTable g2;
1630
1631 g2.name = "Unknown";
1632 g2.is_subobject = 1;
1633 asf_read_unknown(s, &g2);
1634 }
1635 }
1636
1637 return 0;
1638 }
1639
1640 static int asf_read_header(AVFormatContext *s)
1641 {
1642 ASFContext *asf = s->priv_data;
1643 AVIOContext *pb = s->pb;
1644 const GUIDParseTable *g = NULL;
1645 ff_asf_guid guid;
1646 int i, ret;
1647 uint64_t size;
1648
1649 asf->preroll = 0;
1650 asf->is_simple_index = 0;
1651 ff_get_guid(pb, &guid);
1652 if (ff_guidcmp(&guid, &ff_asf_header))
1653 return AVERROR_INVALIDDATA;
1654 avio_skip(pb, 8); // skip header object size
1655 avio_skip(pb, 6); // skip number of header objects and 2 reserved bytes
1656 asf->data_reached = 0;
1657
1658 /* 1 is here instead of pb->eof_reached because (when not streaming), Data are skipped
1659 * for the first time,
1660 * Index object is processed and got eof and then seeking back to the Data is performed.
1661 */
1662 while (1) {
1663 // for the cases when object size is invalid
1664 if (avio_tell(pb) == asf->offset)
1665 break;
1666 asf->offset = avio_tell(pb);
1667 if ((ret = ff_get_guid(pb, &guid)) < 0) {
1668 if (ret == AVERROR_EOF && asf->data_reached)
1669 break;
1670 else
1671 goto failed;
1672 }
1673 g = find_guid(guid);
1674 if (g) {
1675 asf->unknown_offset = asf->offset;
1676 asf->is_header = 1;
1677 if ((ret = g->read_object(s, g)) < 0)
1678 goto failed;
1679 } else {
1680 size = avio_rl64(pb);
1681 align_position(pb, asf->offset, size);
1682 }
1683 if (asf->data_reached && (!pb->seekable || (asf->b_flags & ASF_FLAG_BROADCAST)))
1684 break;
1685 }
1686
1687 if (!asf->data_reached) {
1688 av_log(s, AV_LOG_ERROR, "Data Object was not found.\n");
1689 ret = AVERROR_INVALIDDATA;
1690 goto failed;
1691 }
1692 if (pb->seekable)
1693 avio_seek(pb, asf->first_packet_offset, SEEK_SET);
1694
1695 for (i = 0; i < asf->nb_streams; i++) {
1696 const char *rfc1766 = asf->asf_sd[asf->asf_st[i]->lang_idx].langs;
1697 AVStream *st = s->streams[asf->asf_st[i]->index];
1698 set_language(s, rfc1766, &st->metadata);
1699 }
1700
1701 for (i = 0; i < ASF_MAX_STREAMS; i++) {
1702 AVStream *st = NULL;
1703
1704 st = find_stream(s, i);
1705 if (st) {
1706 av_dict_copy(&st->metadata, asf->asf_sd[i].asf_met, AV_DICT_IGNORE_SUFFIX);
1707 if (asf->asf_sd[i].aspect_ratio.num > 0 && asf->asf_sd[i].aspect_ratio.den > 0) {
1708 st->sample_aspect_ratio.num = asf->asf_sd[i].aspect_ratio.num;
1709 st->sample_aspect_ratio.den = asf->asf_sd[i].aspect_ratio.den;
1710 }
1711 }
1712 }
1713
1714 return 0;
1715
1716 failed:
1717 asf_read_close(s);
1718 return ret;
1719 }
1720
1721 AVInputFormat ff_asf_demuxer = {
1722 .name = "asf",
1723 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1724 .priv_data_size = sizeof(ASFContext),
1725 .read_probe = asf_probe,
1726 .read_header = asf_read_header,
1727 .read_packet = asf_read_packet,
1728 .read_close = asf_read_close,
1729 .read_timestamp = asf_read_timestamp,
1730 .read_seek = asf_read_seek,
1731 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,
1732 };