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