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