FFmpeg
Loading...
Searching...
No Matches
asfdec_f.c
Go to the documentation of this file.
1/*
2 * ASF compatible demuxer
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <inttypes.h>
23
25#include "libavutil/avassert.h"
26#include "libavutil/avstring.h"
27#include "libavutil/bswap.h"
28#include "libavutil/common.h"
29#include "libavutil/dict.h"
30#include "libavutil/internal.h"
32#include "libavutil/mem.h"
33#include "libavutil/opt.h"
34#include "avformat.h"
35#include "avio_internal.h"
36#include "avlanguage.h"
37#include "demux.h"
38#include "internal.h"
39#include "riff.h"
40#include "asf.h"
41#include "asfcrypt.h"
42
43typedef struct ASFPayload {
44 uint8_t type;
45 uint16_t size;
47
48typedef struct ASFStream {
49 int num;
50 unsigned char seq;
51 /* use for reading */
59
60 int ds_span; /* descrambling */
63
65
67
69 uint32_t palette[256];
70
73} ASFStream;
74
75typedef struct ASFContext {
76 const AVClass *class;
77 int asfid2avid[128]; ///< conversion table from asf ID 2 AVStream ID
78 ASFStream streams[128]; ///< it's max number and it's not that big
79 uint32_t stream_bitrates[128]; ///< max number of streams, bitrate for each (for streaming)
81 char stream_languages[128][6]; ///< max number of streams, language for each (RFC1766, e.g. en-US)
82 /* non streamed additonnal info */
83 /* packet filling */
85 /* only for reading */
86 uint64_t data_offset; ///< beginning of the first data packet
87 uint64_t data_object_offset; ///< data object offset (excl. GUID & size)
88 uint64_t data_object_size; ///< size of the data object
90
92
102 unsigned int packet_frag_offset;
103 unsigned int packet_frag_size;
110
112
113 ASFStream *asf_st; ///< currently decoded stream
114
117
119} ASFContext;
120
121static const AVOption options[] = {
122 { "no_resync_search", "Don't try to resynchronize by looking for a certain optional start code", offsetof(ASFContext, no_resync_search), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
123 { "export_xmp", "Export full XMP metadata", offsetof(ASFContext, export_xmp), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
124 { NULL },
125};
126
127static const AVClass asf_class = {
128 .class_name = "asf demuxer",
129 .item_name = av_default_item_name,
130 .option = options,
131 .version = LIBAVUTIL_VERSION_INT,
132};
133
134#undef NDEBUG
135#include <assert.h>
136
137#define FRAME_HEADER_SIZE 6
138// Fix Me! FRAME_HEADER_SIZE may be different.
139// (7 is known to be too large for GipsyGuitar.wmv)
140
141#ifdef DEBUG
142static const ff_asf_guid stream_bitrate_guid = { /* (http://get.to/sdp) */
143 0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
144};
145
146static const ff_asf_guid asf_audio_conceal_none = {
147 // 0x40, 0xa4, 0xf1, 0x49, 0x4ece, 0x11d0, 0xa3, 0xac, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6
148 // New value lifted from avifile
149 0x00, 0x57, 0xfb, 0x20, 0x55, 0x5B, 0xCF, 0x11, 0xa8, 0xfd, 0x00, 0x80, 0x5f, 0x5c, 0x44, 0x2b
150};
151
152#define PRINT_IF_GUID(g, cmp) \
153 if (!ff_guidcmp(g, &(cmp))) \
154 av_log(NULL, AV_LOG_TRACE, "(GUID: %s) ", # cmp)
155
156static void print_guid(ff_asf_guid *g)
157{
158 int i;
159 PRINT_IF_GUID(g, ff_asf_header);
160 else PRINT_IF_GUID(g, ff_asf_file_header);
161 else PRINT_IF_GUID(g, ff_asf_stream_header);
162 else PRINT_IF_GUID(g, ff_asf_audio_stream);
163 else PRINT_IF_GUID(g, asf_audio_conceal_none);
164 else PRINT_IF_GUID(g, ff_asf_video_stream);
165 else PRINT_IF_GUID(g, ff_asf_video_conceal_none);
166 else PRINT_IF_GUID(g, ff_asf_command_stream);
167 else PRINT_IF_GUID(g, ff_asf_comment_header);
168 else PRINT_IF_GUID(g, ff_asf_codec_comment_header);
169 else PRINT_IF_GUID(g, ff_asf_codec_comment1_header);
170 else PRINT_IF_GUID(g, ff_asf_data_header);
171 else PRINT_IF_GUID(g, ff_asf_simple_index_header);
172 else PRINT_IF_GUID(g, ff_asf_head1_guid);
173 else PRINT_IF_GUID(g, ff_asf_head2_guid);
174 else PRINT_IF_GUID(g, ff_asf_my_guid);
175 else PRINT_IF_GUID(g, ff_asf_ext_stream_header);
176 else PRINT_IF_GUID(g, ff_asf_extended_content_header);
177 else PRINT_IF_GUID(g, ff_asf_ext_stream_embed_stream_header);
178 else PRINT_IF_GUID(g, ff_asf_ext_stream_audio_stream);
179 else PRINT_IF_GUID(g, ff_asf_metadata_header);
180 else PRINT_IF_GUID(g, ff_asf_metadata_library_header);
181 else PRINT_IF_GUID(g, ff_asf_marker_header);
182 else PRINT_IF_GUID(g, stream_bitrate_guid);
183 else PRINT_IF_GUID(g, ff_asf_language_guid);
184 else
185 av_log(NULL, AV_LOG_TRACE, "(GUID: unknown) ");
186 for (i = 0; i < 16; i++)
187 av_log(NULL, AV_LOG_TRACE, " 0x%02x,", (*g)[i]);
188 av_log(NULL, AV_LOG_TRACE, "}\n");
189}
190#undef PRINT_IF_GUID
191#else
192#define print_guid(g) while(0)
193#endif
194
195static int asf_probe(const AVProbeData *pd)
196{
197 /* check file header */
198 if (!ff_guidcmp(pd->buf, &ff_asf_header))
199 return AVPROBE_SCORE_MAX;
200 else
201 return 0;
202}
203
204static uint64_t get_value(AVIOContext *pb, int type)
205{
206 switch (type) {
207 case ASF_BOOL:
208 return avio_rl16(pb);
209 case ASF_DWORD:
210 return avio_rl32(pb);
211 case ASF_QWORD:
212 return avio_rl64(pb);
213 case ASF_WORD:
214 return avio_rl16(pb);
215 default:
216 return 0;
217 }
218}
219
220static void get_tag(AVFormatContext *s, const char *key, int type, int len)
221{
222 ASFContext *asf = s->priv_data;
223 char *value = NULL;
224 int64_t off = avio_tell(s->pb);
225#define LEN 22
226
227 av_assert0((unsigned)len < (INT_MAX - LEN) / 2);
228
229 if (!asf->export_xmp && !strncmp(key, "xmp", 3))
230 goto finish;
231
232 value = av_malloc(2 * len + LEN);
233 if (!value)
234 goto finish;
235
236 switch (type) {
237 case ASF_UNICODE:
238 avio_get_str16le(s->pb, len, value, 2 * len + 1);
239 break;
240 case -1:; // ASCII
241 int ret = ffio_read_size(s->pb, value, len);
242 if (ret < 0)
243 goto finish;
244 value[len]=0;
245 break;
246 case ASF_BYTE_ARRAY:
248 av_log(s, AV_LOG_VERBOSE, "Unsupported byte array in tag %s.\n", key);
249 goto finish;
250 case ASF_BOOL:
251 case ASF_DWORD:
252 case ASF_QWORD:
253 case ASF_WORD: {
254 uint64_t num = get_value(s->pb, type);
255 snprintf(value, LEN, "%"PRIu64, num);
256 break;
257 }
258 case ASF_GUID:
259 av_log(s, AV_LOG_DEBUG, "Unsupported GUID value in tag %s.\n", key);
260 goto finish;
261 default:
263 "Unsupported value type %d in tag %s.\n", type, key);
264 goto finish;
265 }
266 if (*value)
267 av_dict_set(&s->metadata, key, value, 0);
268
269finish:
270 av_freep(&value);
271 avio_seek(s->pb, off + len, SEEK_SET);
272}
273
275{
276 ASFContext *asf = s->priv_data;
277 AVIOContext *pb = s->pb;
278
279 ff_get_guid(pb, &asf->hdr.guid);
280 asf->hdr.file_size = avio_rl64(pb);
281 asf->hdr.create_time = avio_rl64(pb);
282 avio_rl64(pb); /* number of packets */
283 asf->hdr.play_time = avio_rl64(pb);
284 asf->hdr.send_time = avio_rl64(pb);
285 asf->hdr.preroll = avio_rl32(pb);
286 asf->hdr.ignore = avio_rl32(pb);
287 asf->hdr.flags = avio_rl32(pb);
288 asf->hdr.min_pktsize = avio_rl32(pb);
289 asf->hdr.max_pktsize = avio_rl32(pb);
290 if (asf->hdr.min_pktsize >= (1U << 29))
291 return AVERROR_INVALIDDATA;
292 asf->hdr.max_bitrate = avio_rl32(pb);
293 s->packet_size = asf->hdr.max_pktsize;
294
295 ff_dict_set_timestamp(&s->metadata, "creation_time", ff_asf_filetime_to_avtime(asf->hdr.create_time));
296 return 0;
297}
298
300{
301 ASFContext *asf = s->priv_data;
302 AVIOContext *pb = s->pb;
303 AVStream *st;
304 FFStream *sti;
305 ASFStream *asf_st;
307 enum AVMediaType type;
308 int type_specific_size, sizeX;
309 unsigned int tag1;
310 int64_t pos1, pos2, start_time;
311 int test_for_ext_stream_audio, is_dvr_ms_audio = 0;
312
313 pos1 = avio_tell(pb);
314
316 if (!st)
317 return AVERROR(ENOMEM);
318 sti = ffstream(st);
319 avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
320 start_time = asf->hdr.preroll;
321
322 if (!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
323 int64_t fsize = avio_size(pb);
324 if (fsize <= 0 || (int64_t)asf->hdr.file_size <= 0 ||
325 FFABS(fsize - (int64_t)asf->hdr.file_size) < FFMIN(fsize, asf->hdr.file_size)/20)
326 st->duration = asf->hdr.play_time /
327 (10000000 / 1000) - start_time;
328 }
329 ff_get_guid(pb, &g);
330
331 test_for_ext_stream_audio = 0;
334 } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
336 } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
339 } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
342 test_for_ext_stream_audio = 1;
344 } else {
345 return -1;
346 }
347 ff_get_guid(pb, &g);
348 avio_skip(pb, 8); /* total_size */
349 type_specific_size = avio_rl32(pb);
350 avio_rl32(pb);
351 st->id = avio_rl16(pb) & 0x7f; /* stream id */
352 // mapping of asf ID to AV stream ID;
353 asf->asfid2avid[st->id] = s->nb_streams - 1;
354 asf_st = &asf->streams[st->id];
355
356 avio_rl32(pb);
357
358 if (test_for_ext_stream_audio) {
359 ff_get_guid(pb, &g);
362 is_dvr_ms_audio = 1;
363 ff_get_guid(pb, &g);
364 avio_rl32(pb);
365 avio_rl32(pb);
366 avio_rl32(pb);
367 ff_get_guid(pb, &g);
368 avio_rl32(pb);
369 }
370 }
371
372 st->codecpar->codec_type = type;
373 if (type == AVMEDIA_TYPE_AUDIO) {
374 int ret = ff_get_wav_header(s, pb, st->codecpar, type_specific_size, 0);
375 if (ret < 0)
376 return ret;
377 if (is_dvr_ms_audio) {
378 // codec_id and codec_tag are unreliable in dvr_ms
379 // files. Set them later by probing stream.
380 sti->request_probe = 1;
381 st->codecpar->codec_tag = 0;
382 }
385 else
387 /* We have to init the frame size at some point .... */
388 pos2 = avio_tell(pb);
389 if (size >= (pos2 + 8 - pos1 + 24)) {
390 asf_st->ds_span = avio_r8(pb);
391 asf_st->ds_packet_size = avio_rl16(pb);
392 asf_st->ds_chunk_size = avio_rl16(pb);
393 avio_rl16(pb); // ds_data_size
394 avio_r8(pb); // ds_silence_data
395 }
396 if (asf_st->ds_span > 1) {
397 if (!asf_st->ds_chunk_size ||
398 (asf_st->ds_packet_size / asf_st->ds_chunk_size <= 1) ||
399 asf_st->ds_packet_size % asf_st->ds_chunk_size)
400 asf_st->ds_span = 0; // disable descrambling
401 }
402 } else if (type == AVMEDIA_TYPE_VIDEO &&
403 size - (avio_tell(pb) - pos1 + 24) >= 51) {
404 avio_rl32(pb);
405 avio_rl32(pb);
406 avio_r8(pb);
407 avio_rl16(pb); /* size */
408 sizeX = avio_rl32(pb); /* size */
409 st->codecpar->width = avio_rl32(pb);
410 st->codecpar->height = avio_rl32(pb);
411 /* not available for asf */
412 avio_rl16(pb); /* panes */
413 st->codecpar->bits_per_coded_sample = avio_rl16(pb); /* depth */
414 tag1 = avio_rl32(pb);
415 avio_skip(pb, 20);
416 if (sizeX > 40) {
418 return AVERROR_INVALIDDATA;
419 st->codecpar->extradata_size = ffio_limit(pb, sizeX - 40);
422 if (!st->codecpar->extradata)
423 return AVERROR(ENOMEM);
425 }
426
427 /* Extract palette from extradata if bpp <= 8 */
428 /* This code assumes that extradata contains only palette */
429 /* This is true for all paletted codecs implemented in libavcodec */
430 if (st->codecpar->extradata_size && (st->codecpar->bits_per_coded_sample <= 8)) {
431#if HAVE_BIGENDIAN
432 int i;
433 for (i = 0; i < FFMIN(st->codecpar->extradata_size, AVPALETTE_SIZE) / 4; i++)
434 asf_st->palette[i] = av_bswap32(((uint32_t *)st->codecpar->extradata)[i]);
435#else
436 memcpy(asf_st->palette, st->codecpar->extradata,
438#endif
439 asf_st->palette_changed = 1;
440 }
441
442 st->codecpar->codec_tag = tag1;
444 if (!st->codecpar->codec_id)
446 if (tag1 == MKTAG('D', 'V', 'R', ' ')) {
448 /* issue658 contains wrong w/h and MS even puts a fake seq header
449 * with wrong w/h in extradata while a correct one is in the stream.
450 * maximum lameness */
451 st->codecpar->width =
452 st->codecpar->height = 0;
454 st->codecpar->extradata_size = 0;
455 }
462 }
463 pos2 = avio_tell(pb);
464 avio_skip(pb, size - (pos2 - pos1 + 24));
465
466 return 0;
467}
468
470{
471 ASFContext *asf = s->priv_data;
472 AVIOContext *pb = s->pb;
474 int ext_len, payload_ext_ct, stream_ct, i;
475 uint32_t leak_rate, stream_num;
476 unsigned int stream_languageid_index;
477
478 avio_rl64(pb); // starttime
479 avio_rl64(pb); // endtime
480 leak_rate = avio_rl32(pb); // leak-datarate
481 avio_rl32(pb); // bucket-datasize
482 avio_rl32(pb); // init-bucket-fullness
483 avio_rl32(pb); // alt-leak-datarate
484 avio_rl32(pb); // alt-bucket-datasize
485 avio_rl32(pb); // alt-init-bucket-fullness
486 avio_rl32(pb); // max-object-size
487 avio_rl32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
488 stream_num = avio_rl16(pb); // stream-num
489
490 stream_languageid_index = avio_rl16(pb); // stream-language-id-index
491 if (stream_num < 128)
492 asf->streams[stream_num].stream_language_index = stream_languageid_index;
493
494 avio_rl64(pb); // avg frametime in 100ns units
495 stream_ct = avio_rl16(pb); // stream-name-count
496 payload_ext_ct = avio_rl16(pb); // payload-extension-system-count
497
498 if (stream_num < 128) {
499 asf->stream_bitrates[stream_num] = leak_rate;
500 asf->streams[stream_num].payload_ext_ct = 0;
501 }
502
503 for (i = 0; i < stream_ct; i++) {
504 avio_rl16(pb);
505 ext_len = avio_rl16(pb);
506 avio_skip(pb, ext_len);
507 }
508
509 for (i = 0; i < payload_ext_ct; i++) {
510 int size;
511 ff_get_guid(pb, &g);
512 size = avio_rl16(pb);
513 ext_len = avio_rl32(pb);
514 if (ext_len < 0)
515 return AVERROR_INVALIDDATA;
516 avio_skip(pb, ext_len);
517 if (stream_num < 128 && i < FF_ARRAY_ELEMS(asf->streams[stream_num].payload)) {
518 ASFPayload *p = &asf->streams[stream_num].payload[i];
519 p->type = g[0];
520 p->size = size;
521 av_log(s, AV_LOG_DEBUG, "Payload extension %x %d\n", g[0], p->size );
522 asf->streams[stream_num].payload_ext_ct ++;
523 }
524 }
525
526 return 0;
527}
528
530{
531 AVIOContext *pb = s->pb;
532 int len1, len2, len3, len4, len5;
533
534 len1 = avio_rl16(pb);
535 len2 = avio_rl16(pb);
536 len3 = avio_rl16(pb);
537 len4 = avio_rl16(pb);
538 len5 = avio_rl16(pb);
539 get_tag(s, "title", 0, len1);
540 get_tag(s, "author", 0, len2);
541 get_tag(s, "copyright", 0, len3);
542 get_tag(s, "comment", 0, len4);
543 avio_skip(pb, len5);
544
545 return 0;
546}
547
549{
550 AVIOContext *pb = s->pb;
551 ASFContext *asf = s->priv_data;
552 int desc_count, i, ret;
553
554 desc_count = avio_rl16(pb);
555 for (i = 0; i < desc_count; i++) {
556 int name_len, value_type, value_len;
557 char name[1024];
558
559 name_len = avio_rl16(pb);
560 if (name_len % 2) // must be even, broken lavf versions wrote len-1
561 name_len += 1;
562 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
563 avio_skip(pb, name_len - ret);
564 value_type = avio_rl16(pb);
565 value_len = avio_rl16(pb);
566 if (!value_type && value_len % 2)
567 value_len += 1;
568 /* size of type 2 (BOOL) is 32bit for "Extended Content Description Object"
569 * but 16 bit for "Metadata Object" and "Metadata Library Object" */
570 if (value_type == ASF_BOOL)
571 value_type = ASF_DWORD;
572 /* My sample has that stream set to 0 maybe that mean the container.
573 * ASF stream count starts at 1. I am using 0 to the container value
574 * since it's unused. */
575 if (!strcmp(name, "AspectRatioX"))
576 asf->dar[0].num = get_value(s->pb, value_type);
577 else if (!strcmp(name, "AspectRatioY"))
578 asf->dar[0].den = get_value(s->pb, value_type);
579 else
580 get_tag(s, name, value_type, value_len);
581 }
582
583 return 0;
584}
585
587{
588 AVIOContext *pb = s->pb;
589 ASFContext *asf = s->priv_data;
590 int j, ret;
591 int stream_count = avio_rl16(pb);
592 for (j = 0; j < stream_count; j++) {
593 char lang[6];
594 unsigned int lang_len = avio_r8(pb);
595 if ((ret = avio_get_str16le(pb, lang_len, lang,
596 sizeof(lang))) < lang_len)
597 avio_skip(pb, lang_len - ret);
598 if (j < 128)
599 av_strlcpy(asf->stream_languages[j], lang,
600 sizeof(*asf->stream_languages));
601 }
602
603 return 0;
604}
605
607{
608 AVIOContext *pb = s->pb;
609 ASFContext *asf = s->priv_data;
610 int n, stream_num, name_len_utf16, name_len_utf8;
611 unsigned int value_len;
612 int ret, i;
613 n = avio_rl16(pb);
614
615 for (i = 0; i < n; i++) {
616 uint8_t *name;
617 int value_type;
618
619 avio_rl16(pb); // lang_list_index
620 stream_num = avio_rl16(pb);
621 name_len_utf16 = avio_rl16(pb);
622 value_type = avio_rl16(pb); /* value_type */
623 value_len = avio_rl32(pb);
624
625 if (value_len >= (INT_MAX - LEN) / 2)
626 return AVERROR_INVALIDDATA;
627
628 name_len_utf8 = 2*name_len_utf16 + 1;
629 name = av_malloc(name_len_utf8);
630 if (!name)
631 return AVERROR(ENOMEM);
632
633 if ((ret = avio_get_str16le(pb, name_len_utf16, name, name_len_utf8)) < name_len_utf16)
634 avio_skip(pb, name_len_utf16 - ret);
635 av_log(s, AV_LOG_TRACE, "%d stream %d name_len %2d type %d len %4d <%s>\n",
636 i, stream_num, name_len_utf16, value_type, value_len, name);
637
638 if (!strcmp(name, "AspectRatioX")){
639 int aspect_x = get_value(s->pb, value_type);
640 if(stream_num < 128)
641 asf->dar[stream_num].num = aspect_x;
642 } else if(!strcmp(name, "AspectRatioY")){
643 int aspect_y = get_value(s->pb, value_type);
644 if(stream_num < 128)
645 asf->dar[stream_num].den = aspect_y;
646 } else {
647 get_tag(s, name, value_type, value_len);
648 }
649 av_freep(&name);
650 }
651
652 return 0;
653}
654
656{
657 AVIOContext *pb = s->pb;
658 ASFContext *asf = s->priv_data;
659 int i, count, name_len, ret;
660 char name[1024];
661
662 avio_rl64(pb); // reserved 16 bytes
663 avio_rl64(pb); // ...
664 count = avio_rl32(pb); // markers count
665 avio_rl16(pb); // reserved 2 bytes
666 name_len = avio_rl16(pb); // name length
667 avio_skip(pb, name_len);
668
669 for (i = 0; i < count; i++) {
670 int64_t pres_time;
671
672 if (avio_feof(pb))
673 return AVERROR_INVALIDDATA;
674
675 avio_rl64(pb); // offset, 8 bytes
676 pres_time = avio_rl64(pb); // presentation time
677 pres_time = av_sat_sub64(pres_time, asf->hdr.preroll * 10000LL);
678 avio_rl16(pb); // entry length
679 avio_rl32(pb); // send time
680 avio_rl32(pb); // flags
681 name_len = avio_rl32(pb); // name length
682 if ((unsigned)name_len > INT_MAX / 2)
683 return AVERROR_INVALIDDATA;
684 if ((ret = avio_get_str16le(pb, name_len * 2, name,
685 sizeof(name))) < name_len)
686 avio_skip(pb, name_len - ret);
687 avpriv_new_chapter(s, i, (AVRational) { 1, 10000000 }, pres_time,
689 }
690
691 return 0;
692}
693
695{
696 ASFContext *asf = s->priv_data;
698 AVIOContext *pb = s->pb;
699 int i;
700 int64_t gsize;
701
702 ff_get_guid(pb, &g);
703 if (ff_guidcmp(&g, &ff_asf_header))
704 return AVERROR_INVALIDDATA;
705 avio_rl64(pb);
706 avio_rl32(pb);
707 avio_r8(pb);
708 avio_r8(pb);
709 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
710
711 for (i = 0; i<128; i++)
712 asf->streams[i].stream_language_index = 128; // invalid stream index means no language info
713
714 for (;;) {
715 uint64_t gpos = avio_tell(pb);
716 int ret = 0;
717 ff_get_guid(pb, &g);
718 gsize = avio_rl64(pb);
719 print_guid(&g);
721 asf->data_object_offset = avio_tell(pb);
722 /* If not streaming, gsize is not unlimited (how?),
723 * and there is enough space in the file.. */
724 if (!(asf->hdr.flags & 0x01) && gsize >= 100)
725 asf->data_object_size = gsize - 24;
726 else
727 asf->data_object_size = (uint64_t)-1;
728 break;
729 }
730 if (gsize < 24)
731 return AVERROR_INVALIDDATA;
734 } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
735 ret = asf_read_stream_properties(s, gsize);
736 } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
738 } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
742 } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
746 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
748
749 // there could be an optional stream properties object to follow
750 // if so the next iteration will pick it up
751 continue;
752 } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
753 ff_get_guid(pb, &g);
754 avio_skip(pb, 6);
755 continue;
756 } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
758 } else if (avio_feof(pb)) {
759 return AVERROR_EOF;
760 } else {
761 if (!s->keylen) {
764 unsigned int len;
766 "DRM protected stream detected, decoding will likely fail!\n");
767 len= avio_rl32(pb);
768 av_log(s, AV_LOG_DEBUG, "Secret data:\n");
769
770 if ((ret = av_get_packet(pb, pkt, len)) < 0)
771 return ret;
772 av_hex_dump_log(s, AV_LOG_DEBUG, pkt->data, pkt->size);
774
775 len= avio_rl32(pb);
776 if (len > UINT16_MAX)
777 return AVERROR_INVALIDDATA;
778 get_tag(s, "ASF_Protection_Type", -1, len);
779
780 len= avio_rl32(pb);
781 if (len > UINT16_MAX)
782 return AVERROR_INVALIDDATA;
783 get_tag(s, "ASF_Key_ID", -1, len);
784
785 len= avio_rl32(pb);
786 if (len > UINT16_MAX)
787 return AVERROR_INVALIDDATA;
788 get_tag(s, "ASF_License_URL", -1, len);
791 "Ext DRM protected stream detected, decoding will likely fail!\n");
792 av_dict_set(&s->metadata, "encryption", "ASF Extended Content Encryption", 0);
793 } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
794 av_log(s, AV_LOG_INFO, "Digital signature detected!\n");
795 }
796 }
797 }
798 if (ret < 0)
799 return ret;
800
801 if (avio_tell(pb) != gpos + gsize)
803 "gpos mismatch our pos=%"PRIu64", end=%"PRId64"\n",
804 avio_tell(pb) - gpos, gsize);
805 avio_seek(pb, gpos + gsize, SEEK_SET);
806 }
807 ff_get_guid(pb, &g);
808 avio_rl64(pb);
809 avio_r8(pb);
810 avio_r8(pb);
811 if (avio_feof(pb))
812 return AVERROR_EOF;
813 asf->data_offset = avio_tell(pb);
814 asf->packet_size_left = 0;
815
816 for (i = 0; i < 128; i++) {
817 int stream_num = asf->asfid2avid[i];
818 if (stream_num >= 0) {
819 AVStream *st = s->streams[stream_num];
820 if (!st->codecpar->bit_rate)
821 st->codecpar->bit_rate = asf->stream_bitrates[i];
822 if (asf->dar[i].num > 0 && asf->dar[i].den > 0) {
825 asf->dar[i].num, asf->dar[i].den, INT_MAX);
826 } else if ((asf->dar[0].num > 0) && (asf->dar[0].den > 0) &&
827 // Use ASF container value if the stream doesn't set AR.
831 asf->dar[0].num, asf->dar[0].den, INT_MAX);
832
833 av_log(s, AV_LOG_TRACE, "i=%d, st->codecpar->codec_type:%d, asf->dar %d:%d sar=%d:%d\n",
834 i, st->codecpar->codec_type, asf->dar[i].num, asf->dar[i].den,
836
837 // copy and convert language codes to the frontend
838 if (asf->streams[i].stream_language_index < 128) {
839 const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
840 if (rfc1766 && strlen(rfc1766) > 1) {
841 const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
842 const char *iso6392 = ff_convert_lang_to(primary_tag,
844 if (iso6392)
845 av_dict_set(&st->metadata, "language", iso6392, 0);
846 }
847 }
848 }
849 }
850
852
853 return 0;
854}
855
856#define DO_2BITS(bits, var, defval) \
857 switch (bits & 3) { \
858 case 3: \
859 var = avio_rl32(pb); \
860 rsize += 4; \
861 break; \
862 case 2: \
863 var = avio_rl16(pb); \
864 rsize += 2; \
865 break; \
866 case 1: \
867 var = avio_r8(pb); \
868 rsize++; \
869 break; \
870 default: \
871 var = defval; \
872 break; \
873 }
874
875/**
876 * Load a single ASF packet into the demuxer.
877 * @param s demux context
878 * @param pb context to read data from
879 * @return 0 on success, <0 on error
880 */
882{
883 ASFContext *asf = s->priv_data;
884 uint32_t packet_length, padsize;
885 int rsize = 8;
886 int c, d, e, off;
887
888 if (asf->uses_std_ecc > 0) {
889 // if we do not know packet size, allow skipping up to 32 kB
890 off = 32768;
891 if (asf->no_resync_search)
892 off = 3;
893// else if (s->packet_size > 0 && !asf->uses_std_ecc)
894// off = (avio_tell(pb) - ffformatcontext(s)->data_offset) % s->packet_size + 3;
895
896 c = d = e = -1;
897 while (off-- > 0) {
898 c = d;
899 d = e;
900 e = avio_r8(pb);
901 if (c == 0x82 && !d && !e)
902 break;
903 }
904
905 if (c != 0x82) {
906 /* This code allows handling of -EAGAIN at packet boundaries (i.e.
907 * if the packet sync code above triggers -EAGAIN). This does not
908 * imply complete -EAGAIN handling support at random positions in
909 * the stream. */
910 if (pb->error == AVERROR(EAGAIN))
911 return AVERROR(EAGAIN);
912 if (!avio_feof(pb))
914 "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
915 }
916 if ((c & 0x8f) == 0x82) {
917 if (d || e) {
918 if (!avio_feof(pb))
919 av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
920 return AVERROR_INVALIDDATA;
921 }
922 c = avio_r8(pb);
923 d = avio_r8(pb);
924 rsize += 3;
925 } else if(!avio_feof(pb)) {
926 avio_seek(pb, -1, SEEK_CUR); // FIXME
927 }
928 } else {
929 c = avio_r8(pb);
930 if (c & 0x80) {
931 rsize ++;
932 if (!(c & 0x60)) {
933 d = avio_r8(pb);
934 e = avio_r8(pb);
935 avio_seek(pb, (c & 0xF) - 2, SEEK_CUR);
936 rsize += c & 0xF;
937 }
938
939 if (c != 0x82)
940 avpriv_request_sample(s, "Invalid ECC byte");
941
942 if (!asf->uses_std_ecc)
943 asf->uses_std_ecc = (c == 0x82 && !d && !e) ? 1 : -1;
944
945 c = avio_r8(pb);
946 } else
947 asf->uses_std_ecc = -1;
948 d = avio_r8(pb);
949 }
950
951 asf->packet_flags = c;
952 asf->packet_property = d;
953
954 DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
955 DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
956 DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
957
958 // the following checks prevent overflows and infinite loops
959 if (!packet_length || packet_length >= (1U << 29)) {
961 "invalid packet_length %"PRIu32" at:%"PRId64"\n",
962 packet_length, avio_tell(pb));
963 return AVERROR_INVALIDDATA;
964 }
965 if (padsize >= packet_length) {
967 "invalid padsize %"PRIu32" at:%"PRId64"\n", padsize, avio_tell(pb));
968 return AVERROR_INVALIDDATA;
969 }
970
971 asf->packet_timestamp = avio_rl32(pb);
972 avio_rl16(pb); /* duration */
973 // rsize has at least 11 bytes which have to be present
974
975 if (asf->packet_flags & 0x01) {
976 asf->packet_segsizetype = avio_r8(pb);
977 rsize++;
978 asf->packet_segments = asf->packet_segsizetype & 0x3f;
979 } else {
980 asf->packet_segments = 1;
981 asf->packet_segsizetype = 0x80;
982 }
983 if (rsize > packet_length - padsize) {
984 asf->packet_size_left = 0;
986 "invalid packet header length %d for pktlen %"PRIu32"-%"PRIu32" at %"PRId64"\n",
987 rsize, packet_length, padsize, avio_tell(pb));
988 return AVERROR_INVALIDDATA;
989 }
990 asf->packet_size_left = packet_length - padsize - rsize;
991 if (packet_length < asf->hdr.min_pktsize)
992 padsize += asf->hdr.min_pktsize - packet_length;
993 asf->packet_padsize = padsize;
994 av_log(s, AV_LOG_TRACE, "packet: size=%d padsize=%d left=%d\n",
995 s->packet_size, asf->packet_padsize, asf->packet_size_left);
996 return 0;
997}
998
999/**
1000 *
1001 * @return <0 if error
1002 */
1004{
1005 ASFContext *asf = s->priv_data;
1006 ASFStream *asfst;
1007 int rsize = 1;
1008 int num = avio_r8(pb);
1009 int i;
1010 int64_t ts0, ts1 av_unused;
1011
1012 asf->packet_segments--;
1013 asf->packet_key_frame = num >> 7;
1014 asf->stream_index = asf->asfid2avid[num & 0x7f];
1015 asfst = &asf->streams[num & 0x7f];
1016 // sequence should be ignored!
1017 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
1018 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
1020 av_log(asf, AV_LOG_TRACE, "key:%d stream:%d seq:%d offset:%d replic_size:%d num:%X packet_property %X\n",
1021 asf->packet_key_frame, asf->stream_index, asf->packet_seq,
1023 if (rsize+(int64_t)asf->packet_replic_size > asf->packet_size_left) {
1024 av_log(s, AV_LOG_ERROR, "packet_replic_size %d is invalid\n", asf->packet_replic_size);
1025 return AVERROR_INVALIDDATA;
1026 }
1027 if (asf->packet_replic_size >= 8) {
1028 int64_t end = avio_tell(pb) + asf->packet_replic_size;
1029 AVRational aspect;
1030 asfst->packet_obj_size = avio_rl32(pb);
1031 if (asfst->packet_obj_size >= (1 << 24) || asfst->packet_obj_size < 0) {
1032 av_log(s, AV_LOG_ERROR, "packet_obj_size %d invalid\n", asfst->packet_obj_size);
1033 asfst->packet_obj_size = 0;
1034 return AVERROR_INVALIDDATA;
1035 }
1036 asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
1037
1038 for (i = 0; i < asfst->payload_ext_ct; i++) {
1039 ASFPayload *p = &asfst->payload[i];
1040 int size = p->size;
1041 int64_t payend;
1042 if (size == 0xFFFF)
1043 size = avio_rl16(pb);
1044 payend = avio_tell(pb) + size;
1045 if (payend > end) {
1046 av_log(s, AV_LOG_ERROR, "too long payload\n");
1047 break;
1048 }
1049 switch (p->type) {
1050 case 0x50:
1051// duration = avio_rl16(pb);
1052 break;
1053 case 0x54:
1054 aspect.num = avio_r8(pb);
1055 aspect.den = avio_r8(pb);
1056 if (aspect.num > 0 && aspect.den > 0 && asf->stream_index >= 0) {
1057 s->streams[asf->stream_index]->sample_aspect_ratio = aspect;
1058 }
1059 break;
1060 case 0x2A:
1061 avio_skip(pb, 8);
1062 ts0 = avio_rl64(pb);
1063 ts1 = avio_rl64(pb);
1064 if (ts0!= -1) asf->packet_frag_timestamp = ts0/10000;
1066 asf->ts_is_pts = 1;
1067 break;
1068 case 0x5B:
1069 case 0xB7:
1070 case 0xCC:
1071 case 0xC0:
1072 case 0xA0:
1073 //unknown
1074 break;
1075 }
1076 avio_seek(pb, payend, SEEK_SET);
1077 }
1078
1079 avio_seek(pb, end, SEEK_SET);
1080 rsize += asf->packet_replic_size; // FIXME - check validity
1081 } else if (asf->packet_replic_size == 1) {
1082 // multipacket - frag_offset is beginning timestamp
1084 asf->packet_frag_offset = 0;
1086
1087 asf->packet_time_delta = avio_r8(pb);
1088 rsize++;
1089 } else if (asf->packet_replic_size != 0) {
1090 av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n",
1091 asf->packet_replic_size);
1092 return AVERROR_INVALIDDATA;
1093 }
1094 if (asf->packet_flags & 0x01) {
1095 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
1096 if (rsize > asf->packet_size_left) {
1097 av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
1098 return AVERROR_INVALIDDATA;
1099 } else if (asf->packet_frag_size > asf->packet_size_left - rsize) {
1100 if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
1101 av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d>%d-%d+%d)\n",
1102 asf->packet_frag_size, asf->packet_size_left, rsize, asf->packet_padsize);
1103 return AVERROR_INVALIDDATA;
1104 } else {
1105 int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
1106 asf->packet_size_left += diff;
1107 asf->packet_padsize -= diff;
1108 }
1109 }
1110 } else {
1111 asf->packet_frag_size = asf->packet_size_left - rsize;
1112 }
1113 if (asf->packet_replic_size == 1) {
1115 if (asf->packet_multi_size > asf->packet_size_left)
1116 return AVERROR_INVALIDDATA;
1117 }
1118 asf->packet_size_left -= rsize;
1119
1120 return 0;
1121}
1122
1123/**
1124 * Parse data from individual ASF packets (which were previously loaded
1125 * with asf_get_packet()).
1126 * @param s demux context
1127 * @param pb context to read data from
1128 * @param pkt pointer to store packet data into
1129 * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
1130 * packets need to be loaded (through asf_get_packet())
1131 */
1133{
1134 ASFContext *asf = s->priv_data;
1135 ASFStream *asf_st = 0;
1136 for (;;) {
1137 int read;
1138 if (avio_feof(pb))
1139 return AVERROR_EOF;
1141 asf->packet_segments < 1 && asf->packet_time_start == 0) {
1142 int ret = asf->packet_size_left + asf->packet_padsize;
1143
1145 av_log(s, AV_LOG_WARNING, "Skip due to FRAME_HEADER_SIZE\n");
1146
1147 assert(ret >= 0);
1148 /* fail safe */
1149 avio_skip(pb, ret);
1150
1151 asf->packet_pos = avio_tell(pb);
1152 if (asf->data_object_size != (uint64_t)-1 &&
1153 (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
1154 return AVERROR_EOF; /* Do not exceed the size of the data object */
1155 return 1;
1156 }
1157 if (asf->packet_time_start == 0) {
1158 if (asf_read_frame_header(s, pb) < 0) {
1159 asf->packet_time_start = asf->packet_segments = 0;
1160 continue;
1161 }
1162 if (asf->stream_index < 0 ||
1163 s->streams[asf->stream_index]->discard >= AVDISCARD_ALL ||
1164 (!asf->packet_key_frame &&
1165 (s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY || asf->streams[s->streams[asf->stream_index]->id].skip_to_key))) {
1166 asf->packet_time_start = 0;
1167 /* unhandled packet (should not happen) */
1168 avio_skip(pb, asf->packet_frag_size);
1170 if (asf->stream_index < 0)
1171 av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n",
1172 asf->packet_frag_size);
1173 continue;
1174 }
1175 asf->asf_st = &asf->streams[s->streams[asf->stream_index]->id];
1176 if (!asf->packet_frag_offset)
1177 asf->asf_st->skip_to_key = 0;
1178 }
1179 asf_st = asf->asf_st;
1180 av_assert0(asf_st);
1181
1182 if (!asf_st->frag_offset && asf->packet_frag_offset) {
1183 av_log(s, AV_LOG_TRACE, "skipping asf data pkt with fragment offset for "
1184 "stream:%d, expected:%d but got %d from pkt)\n",
1185 asf->stream_index, asf_st->frag_offset,
1186 asf->packet_frag_offset);
1187 avio_skip(pb, asf->packet_frag_size);
1189 continue;
1190 }
1191
1192 if (asf->packet_replic_size == 1) {
1193 // frag_offset is here used as the beginning timestamp
1196 asf_st->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
1197 asf->packet_size_left--;
1198 asf->packet_multi_size--;
1199 if (asf->packet_multi_size < asf_st->packet_obj_size) {
1200 asf->packet_time_start = 0;
1201 avio_skip(pb, asf->packet_multi_size);
1203 continue;
1204 }
1205 asf->packet_multi_size -= asf_st->packet_obj_size;
1206 }
1207
1208 if (asf_st->pkt.size != asf_st->packet_obj_size ||
1209 // FIXME is this condition sufficient?
1210 asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) {
1211 int ret;
1212
1213 if (asf_st->pkt.data) {
1215 "freeing incomplete packet size %d, new %d\n",
1216 asf_st->pkt.size, asf_st->packet_obj_size);
1217 asf_st->frag_offset = 0;
1218 av_packet_unref(&asf_st->pkt);
1219 }
1220 /* new packet */
1221 if ((ret = av_new_packet(&asf_st->pkt, asf_st->packet_obj_size)) < 0)
1222 return ret;
1223 asf_st->seq = asf->packet_seq;
1225 if (asf->ts_is_pts) {
1226 asf_st->pkt.pts = asf->packet_frag_timestamp - asf->hdr.preroll;
1227 } else
1228 asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
1229 }
1230 asf_st->pkt.stream_index = asf->stream_index;
1231 asf_st->pkt.pos = asf_st->packet_pos = asf->packet_pos;
1232 asf_st->pkt_clean = 0;
1233
1234 if (asf_st->pkt.data && asf_st->palette_changed) {
1235 uint8_t *pal;
1238 if (!pal) {
1239 av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
1240 } else {
1241 memcpy(pal, asf_st->palette, AVPALETTE_SIZE);
1242 asf_st->palette_changed = 0;
1243 }
1244 }
1245 av_log(asf, AV_LOG_TRACE, "new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
1246 asf->stream_index, asf->packet_key_frame,
1247 asf_st->pkt.flags & AV_PKT_FLAG_KEY,
1248 s->streams[asf->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO,
1249 asf_st->packet_obj_size);
1250 if (s->streams[asf->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
1251 asf->packet_key_frame = 1;
1252 if (asf->packet_key_frame)
1253 asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
1254 }
1255
1256 /* read data */
1257 av_log(asf, AV_LOG_TRACE, "READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
1258 s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
1259 asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
1261 if (asf->packet_size_left < 0)
1262 continue;
1263
1264 if (asf->packet_frag_offset >= asf_st->pkt.size ||
1265 asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset) {
1267 "packet fragment position invalid %u,%u not in %u\n",
1269 asf_st->pkt.size);
1270 continue;
1271 }
1272
1273 if (asf->packet_frag_offset != asf_st->frag_offset && !asf_st->pkt_clean) {
1274 memset(asf_st->pkt.data + asf_st->frag_offset, 0, asf_st->pkt.size - asf_st->frag_offset);
1275 asf_st->pkt_clean = 1;
1276 }
1277
1278 read = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
1279 asf->packet_frag_size);
1280 if (read != asf->packet_frag_size) {
1281 if (read < 0 || asf->packet_frag_offset + read == 0)
1282 return read < 0 ? read : AVERROR_EOF;
1283
1284 if (asf_st->ds_span > 1) {
1285 // scrambling, we can either drop it completely or fill the remainder
1286 // TODO: should we fill the whole packet instead of just the current
1287 // fragment?
1288 memset(asf_st->pkt.data + asf->packet_frag_offset + read, 0,
1289 asf->packet_frag_size - read);
1290 read = asf->packet_frag_size;
1291 } else {
1292 // no scrambling, so we can return partial packets
1293 av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + read);
1294 }
1295 }
1296 if (s->key && s->keylen == 20)
1297 ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
1298 read);
1299 asf_st->frag_offset += read;
1300 /* test if whole packet is read */
1301 if (asf_st->frag_offset == asf_st->pkt.size) {
1302 // workaround for macroshit radio DVR-MS files
1303 if (s->streams[asf->stream_index]->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
1304 asf_st->pkt.size > 100) {
1305 int i;
1306 for (i = 0; i < asf_st->pkt.size && !asf_st->pkt.data[i]; i++)
1307 ;
1308 if (i == asf_st->pkt.size) {
1309 av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
1310 asf_st->frag_offset = 0;
1311 av_packet_unref(&asf_st->pkt);
1312 continue;
1313 }
1314 }
1315
1316 /* return packet */
1317 if (asf_st->ds_span > 1) {
1318 if (asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span) {
1320 "pkt.size != ds_packet_size * ds_span (%d %d %d)\n",
1321 asf_st->pkt.size, asf_st->ds_packet_size,
1322 asf_st->ds_span);
1323 } else {
1324 /* packet descrambling */
1325 AVBufferRef *buf = av_buffer_alloc(asf_st->pkt.size +
1327 if (buf) {
1328 uint8_t *newdata = buf->data;
1329 int offset = 0;
1330 memset(newdata + asf_st->pkt.size, 0,
1332 while (offset < asf_st->pkt.size) {
1333 int off = offset / asf_st->ds_chunk_size;
1334 int row = off / asf_st->ds_span;
1335 int col = off % asf_st->ds_span;
1336 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1337 assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
1338 assert(idx + 1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
1339 memcpy(newdata + offset,
1340 asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1341 asf_st->ds_chunk_size);
1342 offset += asf_st->ds_chunk_size;
1343 }
1344 av_buffer_unref(&asf_st->pkt.buf);
1345 asf_st->pkt.buf = buf;
1346 asf_st->pkt.data = buf->data;
1347 }
1348 }
1349 }
1350 asf_st->frag_offset = 0;
1351 *pkt = asf_st->pkt;
1352 asf_st->pkt.buf = 0;
1353 asf_st->pkt.size = 0;
1354 asf_st->pkt.data = 0;
1355 asf_st->pkt.side_data_elems = 0;
1356 asf_st->pkt.side_data = NULL;
1357 break; // packet completed
1358 }
1359 }
1360 return 0;
1361}
1362
1364{
1365 ASFContext *asf = s->priv_data;
1366
1367 for (;;) {
1368 int ret;
1369
1370 /* parse cached packets, if any */
1371 if ((ret = asf_parse_packet(s, s->pb, pkt)) <= 0)
1372 return ret;
1373 if ((ret = asf_get_packet(s, s->pb)) < 0)
1374 assert(asf->packet_size_left < FRAME_HEADER_SIZE ||
1375 asf->packet_segments < 1);
1376 asf->packet_time_start = 0;
1377 }
1378}
1379
1380// Added to support seeking after packets have been read
1381// If information is not reset, read_packet fails due to
1382// leftover information from previous reads
1384{
1385 ASFContext *asf = s->priv_data;
1386 ASFStream *asf_st;
1387 int i;
1388
1389 asf->packet_size_left = 0;
1390 asf->packet_flags = 0;
1391 asf->packet_property = 0;
1392 asf->packet_timestamp = 0;
1393 asf->packet_segsizetype = 0;
1394 asf->packet_segments = 0;
1395 asf->packet_seq = 0;
1396 asf->packet_replic_size = 0;
1397 asf->packet_key_frame = 0;
1398 asf->packet_padsize = 0;
1399 asf->packet_frag_offset = 0;
1400 asf->packet_frag_size = 0;
1401 asf->packet_frag_timestamp = 0;
1402 asf->packet_multi_size = 0;
1403 asf->packet_time_delta = 0;
1404 asf->packet_time_start = 0;
1405
1406 for (i = 0; i < 128; i++) {
1407 asf_st = &asf->streams[i];
1408 av_packet_unref(&asf_st->pkt);
1409 asf_st->packet_obj_size = 0;
1410 asf_st->frag_offset = 0;
1411 asf_st->seq = 0;
1412 }
1413 asf->asf_st = NULL;
1414}
1415
1417{
1418 ASFContext *asf = s->priv_data;
1419 int i;
1420
1421 for (i = 0; i < 128; i++) {
1422 int j = asf->asfid2avid[i];
1423 ASFStream *asf_st = &asf->streams[i];
1424 if (j < 0 || s->streams[j]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
1425 continue;
1426
1427 asf_st->skip_to_key = 1;
1428 }
1429}
1430
1432{
1434
1435 return 0;
1436}
1437
1438static int64_t asf_read_pts(AVFormatContext *s, int stream_index,
1439 int64_t *ppos, int64_t pos_limit)
1440{
1441 FFFormatContext *const si = ffformatcontext(s);
1442 ASFContext *asf = s->priv_data;
1443 AVPacket pkt1, *pkt = &pkt1;
1444 int64_t pts;
1445 int64_t pos = *ppos;
1446 int i;
1447 int64_t start_pos[FF_ARRAY_ELEMS(asf->streams)];
1448
1449 for (i = 0; i < FF_ARRAY_ELEMS(start_pos); i++)
1450 start_pos[i] = pos;
1451
1452 if (s->packet_size > 0)
1453 pos = (pos + s->packet_size - 1 - si->data_offset) /
1454 s->packet_size * s->packet_size +
1455 si->data_offset;
1456 *ppos = pos;
1457 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1458 return AV_NOPTS_VALUE;
1459
1462 for (;;) {
1463 if (av_read_frame(s, pkt) < 0) {
1464 av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
1465 return AV_NOPTS_VALUE;
1466 }
1467
1468 pts = pkt->dts;
1469
1470 if (pkt->flags & AV_PKT_FLAG_KEY) {
1471 AVStream *st = s->streams[pkt->stream_index];
1472
1473 pos = pkt->pos;
1474 av_add_index_entry(st, pos, pts, pkt->size,
1475 pos - start_pos[st->id] + 1, AVINDEX_KEYFRAME);
1476 start_pos[st->id] = pos + 1;
1477
1478 if (pkt->stream_index == stream_index) {
1480 break;
1481 }
1482 }
1484 }
1485
1486 *ppos = pos;
1487 return pts;
1488}
1489
1490static int asf_build_simple_index(AVFormatContext *s, int stream_index)
1491{
1492 ff_asf_guid g;
1493 ASFContext *asf = s->priv_data;
1494 int64_t current_pos = avio_tell(s->pb);
1495 int64_t ret;
1496
1497 if((ret = avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET)) < 0) {
1498 return ret;
1499 }
1500
1501 if ((ret = ff_get_guid(s->pb, &g)) < 0)
1502 goto end;
1503
1504 /* the data object can be followed by other top-level objects,
1505 * skip them until the simple index object is reached */
1507 int64_t gsize = avio_rl64(s->pb);
1508 if (gsize < 24 || avio_feof(s->pb)) {
1509 goto end;
1510 }
1511 avio_skip(s->pb, gsize - 24);
1512 if ((ret = ff_get_guid(s->pb, &g)) < 0)
1513 goto end;
1514 }
1515
1516 {
1517 int64_t itime, last_pos = -1;
1518 int pct, ict;
1519 int i;
1520 av_unused int64_t gsize = avio_rl64(s->pb);
1521 if ((ret = ff_get_guid(s->pb, &g)) < 0)
1522 goto end;
1523 itime = avio_rl64(s->pb);
1524 pct = avio_rl32(s->pb);
1525 ict = avio_rl32(s->pb);
1527 "itime:0x%"PRIx64", pct:%d, ict:%d\n", itime, pct, ict);
1528
1529 for (i = 0; i < ict; i++) {
1530 int pktnum = avio_rl32(s->pb);
1531 int pktct = avio_rl16(s->pb);
1532 int64_t pos = ffformatcontext(s)->data_offset + s->packet_size * (int64_t)pktnum;
1533 int64_t index_pts = FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
1534
1535 if (avio_feof(s->pb)) {
1536 ret = AVERROR_INVALIDDATA;
1537 goto end;
1538 }
1539
1540 if (pos != last_pos) {
1541 av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n",
1542 pktnum, pktct, index_pts);
1543 av_add_index_entry(s->streams[stream_index], pos, index_pts,
1544 s->packet_size, 0, AVINDEX_KEYFRAME);
1545 last_pos = pos;
1546 }
1547 }
1548 asf->index_read = ict > 1;
1549 }
1550end:
1551// if (avio_feof(s->pb)) {
1552// ret = 0;
1553// }
1554 avio_seek(s->pb, current_pos, SEEK_SET);
1555 return ret;
1556}
1557
1558static int asf_read_seek(AVFormatContext *s, int stream_index,
1559 int64_t pts, int flags)
1560{
1561 ASFContext *asf = s->priv_data;
1562 AVStream *st = s->streams[stream_index];
1563 FFStream *const sti = ffstream(st);
1564 int ret = 0;
1565
1566 if (s->packet_size <= 0)
1567 return -1;
1568
1569 /* Try using the protocol's read_seek if available */
1570 if (s->pb) {
1571 int64_t ret64 = avio_seek_time(s->pb, stream_index, pts, flags);
1572 if (ret64 >= 0)
1574 if (ret64 != AVERROR(ENOSYS))
1575 return ret64;
1576 }
1577
1578 /* explicitly handle the case of seeking to 0 */
1579 if (!pts) {
1581 avio_seek(s->pb, ffformatcontext(s)->data_offset, SEEK_SET);
1582 return 0;
1583 }
1584
1585 if (!asf->index_read) {
1586 ret = asf_build_simple_index(s, stream_index);
1587 if (ret < 0)
1588 asf->index_read = -1;
1589 }
1590
1591 if (asf->index_read > 0 && sti->index_entries) {
1593 if (index >= 0) {
1594 /* find the position */
1595 uint64_t pos = sti->index_entries[index].pos;
1596
1597 /* do the seek */
1598 av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
1599 if(avio_seek(s->pb, pos, SEEK_SET) < 0)
1600 return -1;
1602 skip_to_key(s);
1603 return 0;
1604 }
1605 }
1606 /* no index or seeking by index failed */
1607 if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
1608 return -1;
1610 skip_to_key(s);
1611 return 0;
1612}
1613
1615 .p.name = "asf",
1616 .p.long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1618 .p.priv_class = &asf_class,
1619 .priv_data_size = sizeof(ASFContext),
1626};
const FFInputFormat ff_asf_demuxer
Definition asfdec_f.c:1614
static void finish(void)
const AVMetadataConv ff_asf_metadata_conv[]
Definition asf.c:28
int ff_asf_handle_byte_array(AVFormatContext *s, const char *name, int val_len)
Handles both attached pictures as well as id3 tags.
Definition asf.c:145
const ff_asf_guid ff_asf_video_conceal_none
Definition asf_tags.c:55
const ff_asf_guid ff_asf_codec_comment1_header
Definition asf_tags.c:70
const ff_asf_guid ff_asf_video_stream
Definition asf_tags.c:47
const ff_asf_guid ff_asf_ext_stream_header
Definition asf_tags.c:35
const ff_asf_guid ff_asf_stream_header
Definition asf_tags.c:31
const ff_asf_guid ff_asf_command_stream
Definition asf_tags.c:59
const ff_asf_guid ff_asf_ext_stream_audio_stream
Definition asf_tags.c:98
const ff_asf_guid ff_asf_language_guid
Definition asf_tags.c:124
const ff_asf_guid ff_asf_header
Definition asf_tags.c:23
const ff_asf_guid ff_asf_audio_stream
Definition asf_tags.c:39
const ff_asf_guid ff_asf_simple_index_header
Definition asf_tags.c:90
const ff_asf_guid ff_asf_ext_stream_embed_stream_header
Definition asf_tags.c:94
const ff_asf_guid ff_asf_content_encryption
Definition asf_tags.c:128
const ff_asf_guid ff_asf_extended_content_header
Definition asf_tags.c:86
const ff_asf_guid ff_asf_head1_guid
Definition asf_tags.c:78
const ff_asf_guid ff_asf_head2_guid
Definition asf_tags.c:82
const ff_asf_guid ff_asf_marker_header
Definition asf_tags.c:110
const ff_asf_guid ff_asf_metadata_library_header
Definition asf_tags.c:106
static int64_t ff_asf_filetime_to_avtime(int64_t filetime)
Definition asf.h:123
const ff_asf_guid ff_asf_data_header
Definition asf_tags.c:74
const ff_asf_guid ff_asf_jfif_media
Definition asf_tags.c:51
const ff_asf_guid ff_asf_digital_signature
Definition asf_tags.c:136
const ff_asf_guid ff_asf_ext_content_encryption
Definition asf_tags.c:132
const ff_asf_guid ff_asf_codec_comment_header
Definition asf_tags.c:67
const ff_asf_guid ff_asf_my_guid
Definition asf_tags.c:120
const ff_asf_guid ff_asf_file_header
Definition asf_tags.c:27
const ff_asf_guid ff_asf_comment_header
Definition asf_tags.c:63
@ ASF_UNICODE
Definition asf.h:30
@ ASF_BOOL
Definition asf.h:32
@ ASF_QWORD
Definition asf.h:34
@ ASF_BYTE_ARRAY
Definition asf.h:31
@ ASF_DWORD
Definition asf.h:33
@ ASF_WORD
Definition asf.h:35
@ ASF_GUID
Definition asf.h:36
const ff_asf_guid ff_asf_metadata_header
Definition asf_tags.c:102
void ff_asfcrypt_dec(const uint8_t key[20], uint8_t *data, int len)
Definition asfcrypt.c:148
#define LEN
static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
Definition asfdec_f.c:299
static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb)
Definition asfdec_f.c:1003
static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
Definition asfdec_f.c:1438
static int asf_read_marker(AVFormatContext *s)
Definition asfdec_f.c:655
static void asf_reset_header(AVFormatContext *s)
Definition asfdec_f.c:1383
static int asf_read_header(AVFormatContext *s)
Definition asfdec_f.c:694
static int asf_read_file_properties(AVFormatContext *s)
Definition asfdec_f.c:274
static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition asfdec_f.c:1363
#define print_guid(g)
Definition asfdec_f.c:192
static const AVClass asf_class
Definition asfdec_f.c:127
#define DO_2BITS(bits, var, defval)
Definition asfdec_f.c:856
static int asf_build_simple_index(AVFormatContext *s, int stream_index)
Definition asfdec_f.c:1490
static int asf_read_content_desc(AVFormatContext *s)
Definition asfdec_f.c:529
static int asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
Parse data from individual ASF packets (which were previously loaded with asf_get_packet()).
Definition asfdec_f.c:1132
static void skip_to_key(AVFormatContext *s)
Definition asfdec_f.c:1416
static int asf_get_packet(AVFormatContext *s, AVIOContext *pb)
Load a single ASF packet into the demuxer.
Definition asfdec_f.c:881
static int asf_probe(const AVProbeData *pd)
Definition asfdec_f.c:195
static void get_tag(AVFormatContext *s, const char *key, int type, int len)
Definition asfdec_f.c:220
static uint64_t get_value(AVIOContext *pb, int type)
Definition asfdec_f.c:204
static int asf_read_ext_stream_properties(AVFormatContext *s)
Definition asfdec_f.c:469
static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
Definition asfdec_f.c:1558
static int asf_read_language_list(AVFormatContext *s)
Definition asfdec_f.c:586
static int asf_read_close(AVFormatContext *s)
Definition asfdec_f.c:1431
static int asf_read_ext_content_desc(AVFormatContext *s)
Definition asfdec_f.c:548
static int asf_read_metadata(AVFormatContext *s)
Definition asfdec_f.c:606
static int64_t fsize(FILE *f)
Definition audiomatch.c:29
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:839
Main libavformat public API header.
#define AVINDEX_KEYFRAME
Definition avformat.h:630
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:485
#define AVFMT_NOGENSEARCH
Format does not allow to fall back on generic search.
Definition avformat.h:507
#define AVFMT_NOBINSEARCH
Format does not allow to fall back on binary search via read_timestamp.
Definition avformat.h:506
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
@ AVSTREAM_PARSE_FULL_ONCE
full parsing and repack of the first frame only, only implemented for H.264 currently
Definition avformat.h:616
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition avformat.h:613
@ AVSTREAM_PARSE_NONE
Definition avformat.h:612
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a UTF-16 string from pb and convert it to UTF-8.
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
unsigned int avio_rl16(AVIOContext *s)
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
unsigned int avio_rl32(AVIOContext *s)
int64_t avio_seek_time(AVIOContext *h, int stream_index, int64_t timestamp, int flags)
Seek to a given timestamp relative to some component stream.
Definition aviobuf.c:1194
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
uint64_t avio_rl64(AVIOContext *s)
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
int ffio_limit(AVIOContext *s, int size)
Definition aviobuf.c:1023
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
const char * ff_convert_lang_to(const char *lang, enum AVLangCodespace target_codespace)
Convert a language code to a target codespace.
Definition avlanguage.c:741
@ AV_LANG_ISO639_2_BIBL
Definition avlanguage.h:28
static uint32_t BS_FUNC read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
byte swapping routines
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
common internal and external API header
#define av_sat_sub64
Definition common.h:142
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define FRAME_HEADER_SIZE
Definition cpia.c:31
AVChapter * avpriv_new_chapter(AVFormatContext *s, int64_t id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition demux_utils.c:46
int ff_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
Perform a binary search using av_index_search_timestamp() and FFInputFormat.read_timestamp().
Definition seek.c:290
void ff_read_frame_flush(AVFormatContext *s)
Flush the frame reader.
Definition seek.c:719
static AVPacket * pkt
Public dictionary API.
double value
Definition eval.c:102
const char * key
static int64_t start_time
Definition ffplay.c:398
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition opt.h:355
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
@ AV_CODEC_ID_AAC
Definition codec_id.h:458
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
@ AV_CODEC_ID_MJPEG
Definition codec_id.h:57
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition codec_id.h:52
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
@ AVDISCARD_ALL
discard all
Definition defs.h:246
@ AVDISCARD_NONKEY
discard all frames except keyframes
Definition defs.h:245
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition packet.h:47
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition packet.c:231
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition packet.c:113
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition demux.c:1588
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition seek.c:122
void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size)
Send a nice hexadecimal dump of a buffer to the log.
Definition dump.c:88
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition seek.c:245
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:140
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition buffer.c:78
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition rational.c:35
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
AVMediaType
Definition avutil.h:198
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition avutil.h:202
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition avutil.h:199
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition avstring.c:85
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int index
Definition gxfenc.c:90
uint32_t type
Definition jpegmpfenc.c:80
unsigned offset
Definition libaomenc.c:763
int ff_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp)
Set a dictionary value to an ISO-8601 compliant timestamp string.
Definition utils.c:626
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition internal.h:130
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition utils.c:143
Macro definitions for various function/variable attributes.
#define av_unused
Definition attributes.h:164
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition metadata.c:26
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
#define AVPALETTE_SIZE
Definition pixfmt.h:32
const char * name
Definition qsvenc.c:142
const AVCodecTag ff_codec_bmp_tags_unofficial[]
Definition riff.c:519
const AVCodecTag ff_codec_bmp_tags[]
Definition riff.c:36
internal header for RIFF based (de)muxers do NOT include this in end user applications
static av_always_inline int ff_guidcmp(const void *g1, const void *g2)
Definition riff.h:122
int ff_get_guid(AVIOContext *s, ff_asf_guid *g)
Definition riffdec.c:33
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int size, int big_endian)
Definition riffdec.c:141
uint8_t ff_asf_guid[16]
Definition riff.h:96
#define av_bswap32
Definition bswap.h:47
static int64_t read_timestamp(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit, int64_t(*read_timestamp)(struct AVFormatContext *, int, int64_t *, int64_t))
Definition seek.c:281
#define FF_ARRAY_ELEMS(a)
unsigned int pos
Definition spdifenc.c:431
char stream_languages[128][6]
max number of streams, language for each (RFC1766, e.g. en-US)
Definition asfdec_f.c:81
unsigned int packet_frag_size
Definition asfdec_f.c:103
int packet_padsize
Definition asfdec_f.c:101
ASFStream * asf_st
currently decoded stream
Definition asfdec_f.c:113
int packet_multi_size
Definition asfdec_f.c:106
ASFStream streams[128]
it's max number and it's not that big
Definition asfdec_f.c:78
int export_xmp
Definition asfdec_f.c:116
int packet_replic_size
Definition asfdec_f.c:99
AVRational dar[128]
Definition asfdec_f.c:80
int packet_key_frame
Definition asfdec_f.c:100
int64_t packet_frag_timestamp
Definition asfdec_f.c:104
int packet_property
Definition asfdec_f.c:94
uint64_t data_offset
beginning of the first data packet
Definition asfdec_f.c:86
int packet_flags
Definition asfdec_f.c:93
ASFMainHeader hdr
Definition asfdec_f.c:91
uint64_t data_object_offset
data object offset (excl. GUID & size)
Definition asfdec_f.c:87
int asfid2avid[128]
conversion table from asf ID 2 AVStream ID
Definition asfdec_f.c:77
int packet_size_left
Definition asfdec_f.c:84
int packet_timestamp
Definition asfdec_f.c:95
int index_read
Definition asfdec_f.c:89
int packet_seq
Definition asfdec_f.c:98
int stream_index
Definition asfdec_f.c:111
int64_t packet_time_start
Definition asfdec_f.c:108
int no_resync_search
Definition asfdec_f.c:115
uint32_t stream_bitrates[128]
max number of streams, bitrate for each (for streaming)
Definition asfdec_f.c:79
int ts_is_pts
Definition asfdec_f.c:105
int packet_segsizetype
Definition asfdec_f.c:96
unsigned int packet_frag_offset
Definition asfdec_f.c:102
uint64_t data_object_size
size of the data object
Definition asfdec_f.c:88
int64_t packet_pos
Definition asfdec_f.c:109
int uses_std_ecc
Definition asfdec_f.c:118
int packet_time_delta
Definition asfdec_f.c:107
int packet_segments
Definition asfdec_f.c:97
uint64_t create_time
time of creation, in 100-nanosecond units since 1.1.1601 invalid if broadcasting
Definition asf.h:43
uint32_t min_pktsize
size of a data packet invalid if broadcasting
Definition asf.h:55
uint32_t ignore
preroll is 64 bits - but let's just ignore it
Definition asf.h:51
ff_asf_guid guid
generated by client computer
Definition asf.h:40
uint32_t max_pktsize
shall be the same as for min_pktsize invalid if broadcasting
Definition asf.h:57
uint64_t send_time
time to send file, in 100-nanosecond units invalid if broadcasting (could be ignored)
Definition asf.h:47
uint32_t max_bitrate
bandwidth of stream in bps should be the sum of bitrates of the individual media streams
Definition asf.h:59
uint32_t flags
0x01 - broadcast 0x02 - seekable rest is reserved should be 0
Definition asf.h:52
uint64_t file_size
in bytes invalid if broadcasting
Definition asf.h:41
uint64_t play_time
play time, in 100-nanosecond units invalid if broadcasting
Definition asf.h:45
uint32_t preroll
timestamp of the first packet, in milliseconds if nonzero - subtract from time
Definition asf.h:49
uint8_t type
Definition asfdec_f.c:44
uint16_t size
Definition asfdec_f.c:45
int payload_ext_ct
Definition asfdec_f.c:71
unsigned char seq
Definition asfdec_f.c:50
AVPacket pkt
Definition asfdec_f.c:52
int ds_packet_size
Definition asfdec_f.c:61
uint16_t stream_language_index
Definition asfdec_f.c:66
int skip_to_key
Definition asfdec_f.c:57
int frag_offset
Definition asfdec_f.c:53
int ds_span
Definition asfdec_f.c:60
int64_t packet_pos
Definition asfdec_f.c:64
int ds_chunk_size
Definition asfdec_f.c:62
int64_t duration
Definition asfdec_f.c:56
int num
Definition asfdec_f.c:49
int pkt_clean
Definition asfdec_f.c:58
int palette_changed
Definition asfdec_f.c:68
uint32_t palette[256]
Definition asfdec_f.c:69
int packet_obj_size
Definition asfdec_f.c:54
ASFPayload payload[8]
Definition asfdec_f.c:72
int timestamp
Definition asfdec_f.c:55
A reference to a data buffer.
Definition buffer.h:82
uint8_t * data
The data buffer.
Definition buffer.h:90
Describe the class of an AVClass context structure.
Definition log.h:76
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
int width
The width of the video frame in pixels.
Definition codec_par.h:143
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1337
Bytestream IO Context.
Definition avio.h:160
int error
contains the error code or 0 if no error happened
Definition avio.h:239
int64_t pos
Definition avformat.h:623
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
int stream_index
Definition packet.h:605
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition packet.h:586
int flags
A combination of AV_PKT_FLAG values.
Definition packet.h:609
int size
Definition packet.h:604
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:596
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition packet.h:602
uint8_t * data
Definition packet.h:603
int64_t pos
byte position in stream, -1 if unknown
Definition packet.h:623
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition packet.h:614
int side_data_elems
Definition packet.h:615
This structure contains the data a format has to probe a file.
Definition avformat.h:473
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition avformat.h:475
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition avformat.h:846
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:827
AVDictionary * metadata
Definition avformat.h:848
int id
Format-specific stream ID.
Definition avformat.h:780
int64_t data_offset
offset of the first packet
Definition internal.h:89
AVPacket * parse_pkt
The generic code uses this as a temporary packet to parse packets or for muxing, especially flushing.
Definition internal.h:104
int request_probe
stream probing state -1 -> probing finished 0 -> no probing requested rest -> perform probing with re...
Definition internal.h:205
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition internal.h:191
enum AVStreamParseType need_parsing
Definition internal.h:321
#define av_mallocz(s)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
int num
Definition error.c:23
static int64_t pts
int size
const char * g
Definition vf_curves.c:128
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
int len
static double c[64]