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