FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/asfdec_o.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 4 952 0.4%
Functions: 1 40 2.5%
Branches: 2 576 0.3%

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