FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/asfdec_f.c
Date: 2025-06-23 20:06:14
Exec Total Coverage
Lines: 682 907 75.2%
Functions: 21 22 95.5%
Branches: 317 550 57.6%

Line Branch Exec Source
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
24 #include "libavutil/attributes.h"
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"
31 #include "libavutil/mathematics.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
43 typedef struct ASFPayload {
44 uint8_t type;
45 uint16_t size;
46 } ASFPayload;
47
48 typedef struct ASFStream {
49 int num;
50 unsigned char seq;
51 /* use for reading */
52 AVPacket pkt;
53 int frag_offset;
54 int packet_obj_size;
55 int timestamp;
56 int64_t duration;
57 int skip_to_key;
58 int pkt_clean;
59
60 int ds_span; /* descrambling */
61 int ds_packet_size;
62 int ds_chunk_size;
63
64 int64_t packet_pos;
65
66 uint16_t stream_language_index;
67
68 int palette_changed;
69 uint32_t palette[256];
70
71 int payload_ext_ct;
72 ASFPayload payload[8];
73 } ASFStream;
74
75 typedef 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)
80 AVRational dar[128];
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 */
84 int packet_size_left;
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
89 int index_read;
90
91 ASFMainHeader hdr;
92
93 int packet_flags;
94 int packet_property;
95 int packet_timestamp;
96 int packet_segsizetype;
97 int packet_segments;
98 int packet_seq;
99 int packet_replic_size;
100 int packet_key_frame;
101 int packet_padsize;
102 unsigned int packet_frag_offset;
103 unsigned int packet_frag_size;
104 int64_t packet_frag_timestamp;
105 int ts_is_pts;
106 int packet_multi_size;
107 int packet_time_delta;
108 int64_t packet_time_start;
109 int64_t packet_pos;
110
111 int stream_index;
112
113 ASFStream *asf_st; ///< currently decoded stream
114
115 int no_resync_search;
116 int export_xmp;
117
118 int uses_std_ecc;
119 } ASFContext;
120
121 static 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
127 static 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 ASF_MAX_STREAMS 127
138 #define FRAME_HEADER_SIZE 6
139 // Fix Me! FRAME_HEADER_SIZE may be different.
140 // (7 is known to be too large for GipsyGuitar.wmv)
141
142 #ifdef DEBUG
143 static const ff_asf_guid stream_bitrate_guid = { /* (http://get.to/sdp) */
144 0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
145 };
146
147 static const ff_asf_guid asf_audio_conceal_none = {
148 // 0x40, 0xa4, 0xf1, 0x49, 0x4ece, 0x11d0, 0xa3, 0xac, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6
149 // New value lifted from avifile
150 0x00, 0x57, 0xfb, 0x20, 0x55, 0x5B, 0xCF, 0x11, 0xa8, 0xfd, 0x00, 0x80, 0x5f, 0x5c, 0x44, 0x2b
151 };
152
153 #define PRINT_IF_GUID(g, cmp) \
154 if (!ff_guidcmp(g, &(cmp))) \
155 av_log(NULL, AV_LOG_TRACE, "(GUID: %s) ", # cmp)
156
157 static void print_guid(ff_asf_guid *g)
158 {
159 int i;
160 PRINT_IF_GUID(g, ff_asf_header);
161 else PRINT_IF_GUID(g, ff_asf_file_header);
162 else PRINT_IF_GUID(g, ff_asf_stream_header);
163 else PRINT_IF_GUID(g, ff_asf_audio_stream);
164 else PRINT_IF_GUID(g, asf_audio_conceal_none);
165 else PRINT_IF_GUID(g, ff_asf_video_stream);
166 else PRINT_IF_GUID(g, ff_asf_video_conceal_none);
167 else PRINT_IF_GUID(g, ff_asf_command_stream);
168 else PRINT_IF_GUID(g, ff_asf_comment_header);
169 else PRINT_IF_GUID(g, ff_asf_codec_comment_header);
170 else PRINT_IF_GUID(g, ff_asf_codec_comment1_header);
171 else PRINT_IF_GUID(g, ff_asf_data_header);
172 else PRINT_IF_GUID(g, ff_asf_simple_index_header);
173 else PRINT_IF_GUID(g, ff_asf_head1_guid);
174 else PRINT_IF_GUID(g, ff_asf_head2_guid);
175 else PRINT_IF_GUID(g, ff_asf_my_guid);
176 else PRINT_IF_GUID(g, ff_asf_ext_stream_header);
177 else PRINT_IF_GUID(g, ff_asf_extended_content_header);
178 else PRINT_IF_GUID(g, ff_asf_ext_stream_embed_stream_header);
179 else PRINT_IF_GUID(g, ff_asf_ext_stream_audio_stream);
180 else PRINT_IF_GUID(g, ff_asf_metadata_header);
181 else PRINT_IF_GUID(g, ff_asf_metadata_library_header);
182 else PRINT_IF_GUID(g, ff_asf_marker_header);
183 else PRINT_IF_GUID(g, stream_bitrate_guid);
184 else PRINT_IF_GUID(g, ff_asf_language_guid);
185 else
186 av_log(NULL, AV_LOG_TRACE, "(GUID: unknown) ");
187 for (i = 0; i < 16; i++)
188 av_log(NULL, AV_LOG_TRACE, " 0x%02x,", (*g)[i]);
189 av_log(NULL, AV_LOG_TRACE, "}\n");
190 }
191 #undef PRINT_IF_GUID
192 #else
193 #define print_guid(g) while(0)
194 #endif
195
196 7236 static int asf_probe(const AVProbeData *pd)
197 {
198 /* check file header */
199
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 7203 times.
7236 if (!ff_guidcmp(pd->buf, &ff_asf_header))
200 33 return AVPROBE_SCORE_MAX;
201 else
202 7203 return 0;
203 }
204
205 /* size of type 2 (BOOL) is 32bit for "Extended Content Description Object"
206 * but 16 bit for "Metadata Object" and "Metadata Library Object" */
207 101 static int get_value(AVIOContext *pb, int type, int type2_size)
208 {
209
3/5
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 46 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
101 switch (type) {
210 52 case ASF_BOOL:
211
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 29 times.
52 return (type2_size == 32) ? avio_rl32(pb) : avio_rl16(pb);
212 46 case ASF_DWORD:
213 46 return avio_rl32(pb);
214 3 case ASF_QWORD:
215 3 return avio_rl64(pb);
216 case ASF_WORD:
217 return avio_rl16(pb);
218 default:
219 return INT_MIN;
220 }
221 }
222
223 323 static void get_tag(AVFormatContext *s, const char *key, int type, int len, int type2_size)
224 {
225 323 ASFContext *asf = s->priv_data;
226 323 char *value = NULL;
227 323 int64_t off = avio_tell(s->pb);
228 #define LEN 22
229
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 323 times.
323 av_assert0((unsigned)len < (INT_MAX - LEN) / 2);
231
232
2/4
✓ Branch 0 taken 323 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 323 times.
323 if (!asf->export_xmp && !strncmp(key, "xmp", 3))
233 goto finish;
234
235 323 value = av_malloc(2 * len + LEN);
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 323 times.
323 if (!value)
237 goto finish;
238
239
5/6
✓ Branch 0 taken 195 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 99 times.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
323 switch (type) {
240 195 case ASF_UNICODE:
241 195 avio_get_str16le(s->pb, len, value, 2 * len + 1);
242 195 break;
243 3 case -1: // ASCI
244 3 avio_read(s->pb, value, len);
245 3 value[len]=0;
246 3 break;
247 16 case ASF_BYTE_ARRAY:
248
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 4 times.
16 if (ff_asf_handle_byte_array(s, key, len) > 0)
249 12 av_log(s, AV_LOG_VERBOSE, "Unsupported byte array in tag %s.\n", key);
250 16 goto finish;
251 99 case ASF_BOOL:
252 case ASF_DWORD:
253 case ASF_QWORD:
254 case ASF_WORD: {
255 99 uint64_t num = get_value(s->pb, type, type2_size);
256 99 snprintf(value, LEN, "%"PRIu64, num);
257 99 break;
258 }
259 10 case ASF_GUID:
260 10 av_log(s, AV_LOG_DEBUG, "Unsupported GUID value in tag %s.\n", key);
261 10 goto finish;
262 default:
263 av_log(s, AV_LOG_DEBUG,
264 "Unsupported value type %d in tag %s.\n", type, key);
265 goto finish;
266 }
267
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 256 times.
297 if (*value)
268 256 av_dict_set(&s->metadata, key, value, 0);
269
270 41 finish:
271 323 av_freep(&value);
272 323 avio_seek(s->pb, off + len, SEEK_SET);
273 323 }
274
275 33 static int asf_read_file_properties(AVFormatContext *s)
276 {
277 33 ASFContext *asf = s->priv_data;
278 33 AVIOContext *pb = s->pb;
279
280 33 ff_get_guid(pb, &asf->hdr.guid);
281 33 asf->hdr.file_size = avio_rl64(pb);
282 33 asf->hdr.create_time = avio_rl64(pb);
283 33 avio_rl64(pb); /* number of packets */
284 33 asf->hdr.play_time = avio_rl64(pb);
285 33 asf->hdr.send_time = avio_rl64(pb);
286 33 asf->hdr.preroll = avio_rl32(pb);
287 33 asf->hdr.ignore = avio_rl32(pb);
288 33 asf->hdr.flags = avio_rl32(pb);
289 33 asf->hdr.min_pktsize = avio_rl32(pb);
290 33 asf->hdr.max_pktsize = avio_rl32(pb);
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (asf->hdr.min_pktsize >= (1U << 29))
292 return AVERROR_INVALIDDATA;
293 33 asf->hdr.max_bitrate = avio_rl32(pb);
294 33 s->packet_size = asf->hdr.max_pktsize;
295
296 33 return 0;
297 }
298
299 44 static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
300 {
301 44 ASFContext *asf = s->priv_data;
302 44 AVIOContext *pb = s->pb;
303 AVStream *st;
304 FFStream *sti;
305 ASFStream *asf_st;
306 ff_asf_guid g;
307 enum AVMediaType type;
308 int type_specific_size, sizeX;
309 unsigned int tag1;
310 int64_t pos1, pos2, start_time;
311 44 int test_for_ext_stream_audio, is_dvr_ms_audio = 0;
312
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (s->nb_streams == ASF_MAX_STREAMS) {
314 av_log(s, AV_LOG_ERROR, "too many streams\n");
315 return AVERROR(EINVAL);
316 }
317
318 44 pos1 = avio_tell(pb);
319
320 44 st = avformat_new_stream(s, NULL);
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (!st)
322 return AVERROR(ENOMEM);
323 44 sti = ffstream(st);
324 44 avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
325 44 start_time = asf->hdr.preroll;
326
327
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
328 44 int64_t fsize = avio_size(pb);
329
2/4
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
44 if (fsize <= 0 || (int64_t)asf->hdr.file_size <= 0 ||
330
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 17 times.
44 FFABS(fsize - (int64_t)asf->hdr.file_size) < FFMIN(fsize, asf->hdr.file_size)/20)
331 27 st->duration = asf->hdr.play_time /
332 27 (10000000 / 1000) - start_time;
333 }
334 44 ff_get_guid(pb, &g);
335
336 44 test_for_ext_stream_audio = 0;
337
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 20 times.
44 if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
338 24 type = AVMEDIA_TYPE_AUDIO;
339
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 1 times.
20 } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
340 19 type = AVMEDIA_TYPE_VIDEO;
341
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
342 type = AVMEDIA_TYPE_VIDEO;
343 st->codecpar->codec_id = AV_CODEC_ID_MJPEG;
344
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
345 1 type = AVMEDIA_TYPE_DATA;
346 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_embed_stream_header)) {
347 test_for_ext_stream_audio = 1;
348 type = AVMEDIA_TYPE_UNKNOWN;
349 } else {
350 return -1;
351 }
352 44 ff_get_guid(pb, &g);
353 44 avio_skip(pb, 8); /* total_size */
354 44 type_specific_size = avio_rl32(pb);
355 44 avio_rl32(pb);
356 44 st->id = avio_rl16(pb) & 0x7f; /* stream id */
357 // mapping of asf ID to AV stream ID;
358 44 asf->asfid2avid[st->id] = s->nb_streams - 1;
359 44 asf_st = &asf->streams[st->id];
360
361 44 avio_rl32(pb);
362
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (test_for_ext_stream_audio) {
364 ff_get_guid(pb, &g);
365 if (!ff_guidcmp(&g, &ff_asf_ext_stream_audio_stream)) {
366 type = AVMEDIA_TYPE_AUDIO;
367 is_dvr_ms_audio = 1;
368 ff_get_guid(pb, &g);
369 avio_rl32(pb);
370 avio_rl32(pb);
371 avio_rl32(pb);
372 ff_get_guid(pb, &g);
373 avio_rl32(pb);
374 }
375 }
376
377 44 st->codecpar->codec_type = type;
378
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 20 times.
44 if (type == AVMEDIA_TYPE_AUDIO) {
379 24 int ret = ff_get_wav_header(s, pb, st->codecpar, type_specific_size, 0);
380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0)
381 return ret;
382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (is_dvr_ms_audio) {
383 // codec_id and codec_tag are unreliable in dvr_ms
384 // files. Set them later by probing stream.
385 sti->request_probe = 1;
386 st->codecpar->codec_tag = 0;
387 }
388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (st->codecpar->codec_id == AV_CODEC_ID_AAC)
389 sti->need_parsing = AVSTREAM_PARSE_NONE;
390 else
391 24 sti->need_parsing = AVSTREAM_PARSE_FULL;
392 /* We have to init the frame size at some point .... */
393 24 pos2 = avio_tell(pb);
394
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (size >= (pos2 + 8 - pos1 + 24)) {
395 24 asf_st->ds_span = avio_r8(pb);
396 24 asf_st->ds_packet_size = avio_rl16(pb);
397 24 asf_st->ds_chunk_size = avio_rl16(pb);
398 24 avio_rl16(pb); // ds_data_size
399 24 avio_r8(pb); // ds_silence_data
400 }
401
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 23 times.
24 if (asf_st->ds_span > 1) {
402
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!asf_st->ds_chunk_size ||
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 (asf_st->ds_packet_size / asf_st->ds_chunk_size <= 1) ||
404 asf_st->ds_packet_size % asf_st->ds_chunk_size)
405 1 asf_st->ds_span = 0; // disable descrambling
406 }
407
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1 times.
20 } else if (type == AVMEDIA_TYPE_VIDEO &&
408
1/2
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
19 size - (avio_tell(pb) - pos1 + 24) >= 51) {
409 19 avio_rl32(pb);
410 19 avio_rl32(pb);
411 19 avio_r8(pb);
412 19 avio_rl16(pb); /* size */
413 19 sizeX = avio_rl32(pb); /* size */
414 19 st->codecpar->width = avio_rl32(pb);
415 19 st->codecpar->height = avio_rl32(pb);
416 /* not available for asf */
417 19 avio_rl16(pb); /* panes */
418 19 st->codecpar->bits_per_coded_sample = avio_rl16(pb); /* depth */
419 19 tag1 = avio_rl32(pb);
420 19 avio_skip(pb, 20);
421
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 9 times.
19 if (sizeX > 40) {
422
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
10 if (size < sizeX - 40 || sizeX - 40 > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
423 return AVERROR_INVALIDDATA;
424 10 st->codecpar->extradata_size = ffio_limit(pb, sizeX - 40);
425 10 st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size +
426 AV_INPUT_BUFFER_PADDING_SIZE);
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!st->codecpar->extradata)
428 return AVERROR(ENOMEM);
429 10 avio_read(pb, st->codecpar->extradata, st->codecpar->extradata_size);
430 }
431
432 /* Extract palette from extradata if bpp <= 8 */
433 /* This code assumes that extradata contains only palette */
434 /* This is true for all paletted codecs implemented in libavcodec */
435
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
19 if (st->codecpar->extradata_size && (st->codecpar->bits_per_coded_sample <= 8)) {
436 #if HAVE_BIGENDIAN
437 int i;
438 for (i = 0; i < FFMIN(st->codecpar->extradata_size, AVPALETTE_SIZE) / 4; i++)
439 asf_st->palette[i] = av_bswap32(((uint32_t *)st->codecpar->extradata)[i]);
440 #else
441 memcpy(asf_st->palette, st->codecpar->extradata,
442 FFMIN(st->codecpar->extradata_size, AVPALETTE_SIZE));
443 #endif
444 asf_st->palette_changed = 1;
445 }
446
447 19 st->codecpar->codec_tag = tag1;
448 19 st->codecpar->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (!st->codecpar->codec_id)
450 st->codecpar->codec_id = ff_codec_get_id(ff_codec_bmp_tags_unofficial, tag1);
451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (tag1 == MKTAG('D', 'V', 'R', ' ')) {
452 sti->need_parsing = AVSTREAM_PARSE_FULL;
453 /* issue658 contains wrong w/h and MS even puts a fake seq header
454 * with wrong w/h in extradata while a correct one is in the stream.
455 * maximum lameness */
456 st->codecpar->width =
457 st->codecpar->height = 0;
458 av_freep(&st->codecpar->extradata);
459 st->codecpar->extradata_size = 0;
460 }
461
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (st->codecpar->codec_id == AV_CODEC_ID_H264)
462 sti->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
463
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4)
464 sti->need_parsing = AVSTREAM_PARSE_FULL;
465
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (st->codecpar->codec_id == AV_CODEC_ID_HEVC)
466 sti->need_parsing = AVSTREAM_PARSE_FULL;
467 }
468 44 pos2 = avio_tell(pb);
469 44 avio_skip(pb, size - (pos2 - pos1 + 24));
470
471 44 return 0;
472 }
473
474 31 static int asf_read_ext_stream_properties(AVFormatContext *s)
475 {
476 31 ASFContext *asf = s->priv_data;
477 31 AVIOContext *pb = s->pb;
478 ff_asf_guid g;
479 int ext_len, payload_ext_ct, stream_ct, i;
480 uint32_t leak_rate, stream_num;
481 unsigned int stream_languageid_index;
482
483 31 avio_rl64(pb); // starttime
484 31 avio_rl64(pb); // endtime
485 31 leak_rate = avio_rl32(pb); // leak-datarate
486 31 avio_rl32(pb); // bucket-datasize
487 31 avio_rl32(pb); // init-bucket-fullness
488 31 avio_rl32(pb); // alt-leak-datarate
489 31 avio_rl32(pb); // alt-bucket-datasize
490 31 avio_rl32(pb); // alt-init-bucket-fullness
491 31 avio_rl32(pb); // max-object-size
492 31 avio_rl32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
493 31 stream_num = avio_rl16(pb); // stream-num
494
495 31 stream_languageid_index = avio_rl16(pb); // stream-language-id-index
496
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (stream_num < 128)
497 31 asf->streams[stream_num].stream_language_index = stream_languageid_index;
498
499 31 avio_rl64(pb); // avg frametime in 100ns units
500 31 stream_ct = avio_rl16(pb); // stream-name-count
501 31 payload_ext_ct = avio_rl16(pb); // payload-extension-system-count
502
503
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (stream_num < 128) {
504 31 asf->stream_bitrates[stream_num] = leak_rate;
505 31 asf->streams[stream_num].payload_ext_ct = 0;
506 }
507
508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 for (i = 0; i < stream_ct; i++) {
509 avio_rl16(pb);
510 ext_len = avio_rl16(pb);
511 avio_skip(pb, ext_len);
512 }
513
514
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 31 times.
44 for (i = 0; i < payload_ext_ct; i++) {
515 int size;
516 13 ff_get_guid(pb, &g);
517 13 size = avio_rl16(pb);
518 13 ext_len = avio_rl32(pb);
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ext_len < 0)
520 return AVERROR_INVALIDDATA;
521 13 avio_skip(pb, ext_len);
522
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 if (stream_num < 128 && i < FF_ARRAY_ELEMS(asf->streams[stream_num].payload)) {
523 13 ASFPayload *p = &asf->streams[stream_num].payload[i];
524 13 p->type = g[0];
525 13 p->size = size;
526 13 av_log(s, AV_LOG_DEBUG, "Payload extension %x %d\n", g[0], p->size );
527 13 asf->streams[stream_num].payload_ext_ct ++;
528 }
529 }
530
531 31 return 0;
532 }
533
534 17 static int asf_read_content_desc(AVFormatContext *s)
535 {
536 17 AVIOContext *pb = s->pb;
537 int len1, len2, len3, len4, len5;
538
539 17 len1 = avio_rl16(pb);
540 17 len2 = avio_rl16(pb);
541 17 len3 = avio_rl16(pb);
542 17 len4 = avio_rl16(pb);
543 17 len5 = avio_rl16(pb);
544 17 get_tag(s, "title", 0, len1, 32);
545 17 get_tag(s, "author", 0, len2, 32);
546 17 get_tag(s, "copyright", 0, len3, 32);
547 17 get_tag(s, "comment", 0, len4, 32);
548 17 avio_skip(pb, len5);
549
550 17 return 0;
551 }
552
553 33 static int asf_read_ext_content_desc(AVFormatContext *s)
554 {
555 33 AVIOContext *pb = s->pb;
556 33 ASFContext *asf = s->priv_data;
557 int desc_count, i, ret;
558
559 33 desc_count = avio_rl16(pb);
560
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 33 times.
183 for (i = 0; i < desc_count; i++) {
561 int name_len, value_type, value_len;
562 char name[1024];
563
564 150 name_len = avio_rl16(pb);
565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (name_len % 2) // must be even, broken lavf versions wrote len-1
566 name_len += 1;
567
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 150 times.
150 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
568 avio_skip(pb, name_len - ret);
569 150 value_type = avio_rl16(pb);
570 150 value_len = avio_rl16(pb);
571
3/4
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
150 if (!value_type && value_len % 2)
572 value_len += 1;
573 /* My sample has that stream set to 0 maybe that mean the container.
574 * ASF stream count starts at 1. I am using 0 to the container value
575 * since it's unused. */
576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (!strcmp(name, "AspectRatioX"))
577 asf->dar[0].num = get_value(s->pb, value_type, 32);
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 else if (!strcmp(name, "AspectRatioY"))
579 asf->dar[0].den = get_value(s->pb, value_type, 32);
580 else
581 150 get_tag(s, name, value_type, value_len, 32);
582 }
583
584 33 return 0;
585 }
586
587 24 static int asf_read_language_list(AVFormatContext *s)
588 {
589 24 AVIOContext *pb = s->pb;
590 24 ASFContext *asf = s->priv_data;
591 int j, ret;
592 24 int stream_count = avio_rl16(pb);
593
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 24 times.
50 for (j = 0; j < stream_count; j++) {
594 char lang[6];
595 26 unsigned int lang_len = avio_r8(pb);
596
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 22 times.
26 if ((ret = avio_get_str16le(pb, lang_len, lang,
597 sizeof(lang))) < lang_len)
598 4 avio_skip(pb, lang_len - ret);
599
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (j < 128)
600 26 av_strlcpy(asf->stream_languages[j], lang,
601 sizeof(*asf->stream_languages));
602 }
603
604 24 return 0;
605 }
606
607 26 static int asf_read_metadata(AVFormatContext *s)
608 {
609 26 AVIOContext *pb = s->pb;
610 26 ASFContext *asf = s->priv_data;
611 int n, stream_num, name_len_utf16, name_len_utf8;
612 unsigned int value_len;
613 int ret, i;
614 26 n = avio_rl16(pb);
615
616
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 26 times.
130 for (i = 0; i < n; i++) {
617 uint8_t *name;
618 int value_type;
619
620 104 avio_rl16(pb); // lang_list_index
621 104 stream_num = avio_rl16(pb);
622 104 name_len_utf16 = avio_rl16(pb);
623 104 value_type = avio_rl16(pb); /* value_type */
624 104 value_len = avio_rl32(pb);
625
626
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (value_len >= (INT_MAX - LEN) / 2)
627 return AVERROR_INVALIDDATA;
628
629 104 name_len_utf8 = 2*name_len_utf16 + 1;
630 104 name = av_malloc(name_len_utf8);
631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (!name)
632 return AVERROR(ENOMEM);
633
634
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 104 times.
104 if ((ret = avio_get_str16le(pb, name_len_utf16, name, name_len_utf8)) < name_len_utf16)
635 avio_skip(pb, name_len_utf16 - ret);
636 104 av_log(s, AV_LOG_TRACE, "%d stream %d name_len %2d type %d len %4d <%s>\n",
637 i, stream_num, name_len_utf16, value_type, value_len, name);
638
639
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 103 times.
104 if (!strcmp(name, "AspectRatioX")){
640 1 int aspect_x = get_value(s->pb, value_type, 16);
641
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if(stream_num < 128)
642 1 asf->dar[stream_num].num = aspect_x;
643
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 102 times.
103 } else if(!strcmp(name, "AspectRatioY")){
644 1 int aspect_y = get_value(s->pb, value_type, 16);
645
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if(stream_num < 128)
646 1 asf->dar[stream_num].den = aspect_y;
647 } else {
648 102 get_tag(s, name, value_type, value_len, 16);
649 }
650 104 av_freep(&name);
651 }
652
653 26 return 0;
654 }
655
656 static int asf_read_marker(AVFormatContext *s)
657 {
658 AVIOContext *pb = s->pb;
659 ASFContext *asf = s->priv_data;
660 int i, count, name_len, ret;
661 char name[1024];
662
663 avio_rl64(pb); // reserved 16 bytes
664 avio_rl64(pb); // ...
665 count = avio_rl32(pb); // markers count
666 avio_rl16(pb); // reserved 2 bytes
667 name_len = avio_rl16(pb); // name length
668 avio_skip(pb, name_len);
669
670 for (i = 0; i < count; i++) {
671 int64_t pres_time;
672 int name_len;
673
674 if (avio_feof(pb))
675 return AVERROR_INVALIDDATA;
676
677 avio_rl64(pb); // offset, 8 bytes
678 pres_time = avio_rl64(pb); // presentation time
679 pres_time = av_sat_sub64(pres_time, asf->hdr.preroll * 10000LL);
680 avio_rl16(pb); // entry length
681 avio_rl32(pb); // send time
682 avio_rl32(pb); // flags
683 name_len = avio_rl32(pb); // name length
684 if ((unsigned)name_len > INT_MAX / 2)
685 return AVERROR_INVALIDDATA;
686 if ((ret = avio_get_str16le(pb, name_len * 2, name,
687 sizeof(name))) < name_len)
688 avio_skip(pb, name_len - ret);
689 avpriv_new_chapter(s, i, (AVRational) { 1, 10000000 }, pres_time,
690 AV_NOPTS_VALUE, name);
691 }
692
693 return 0;
694 }
695
696 33 static int asf_read_header(AVFormatContext *s)
697 {
698 33 ASFContext *asf = s->priv_data;
699 ff_asf_guid g;
700 33 AVIOContext *pb = s->pb;
701 int i;
702 int64_t gsize;
703
704 33 ff_get_guid(pb, &g);
705
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if (ff_guidcmp(&g, &ff_asf_header))
706 return AVERROR_INVALIDDATA;
707 33 avio_rl64(pb);
708 33 avio_rl32(pb);
709 33 avio_r8(pb);
710 33 avio_r8(pb);
711 33 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
712
713
2/2
✓ Branch 0 taken 4224 times.
✓ Branch 1 taken 33 times.
4257 for (i = 0; i<128; i++)
714 4224 asf->streams[i].stream_language_index = 128; // invalid stream index means no language info
715
716 379 for (;;) {
717 412 uint64_t gpos = avio_tell(pb);
718 412 int ret = 0;
719 412 ff_get_guid(pb, &g);
720 412 gsize = avio_rl64(pb);
721 412 print_guid(&g);
722
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 379 times.
412 if (!ff_guidcmp(&g, &ff_asf_data_header)) {
723 33 asf->data_object_offset = avio_tell(pb);
724 /* If not streaming, gsize is not unlimited (how?),
725 * and there is enough space in the file.. */
726
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
33 if (!(asf->hdr.flags & 0x01) && gsize >= 100)
727 33 asf->data_object_size = gsize - 24;
728 else
729 asf->data_object_size = (uint64_t)-1;
730 33 break;
731 }
732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 379 times.
379 if (gsize < 24)
733 return AVERROR_INVALIDDATA;
734
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 346 times.
379 if (!ff_guidcmp(&g, &ff_asf_file_header)) {
735 33 ret = asf_read_file_properties(s);
736
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 302 times.
346 } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
737 44 ret = asf_read_stream_properties(s, gsize);
738
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 285 times.
302 } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
739 17 asf_read_content_desc(s);
740
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 261 times.
285 } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
741 24 asf_read_language_list(s);
742
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 228 times.
261 } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
743 33 asf_read_ext_content_desc(s);
744
2/2
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 205 times.
228 } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
745 23 asf_read_metadata(s);
746
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 202 times.
205 } else if (!ff_guidcmp(&g, &ff_asf_metadata_library_header)) {
747 3 asf_read_metadata(s);
748
2/2
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 171 times.
202 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
749 31 asf_read_ext_stream_properties(s);
750
751 // there could be an optional stream properties object to follow
752 // if so the next iteration will pick it up
753 31 continue;
754
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 138 times.
171 } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
755 33 ff_get_guid(pb, &g);
756 33 avio_skip(pb, 6);
757 33 continue;
758
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 138 times.
138 } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
759 asf_read_marker(s);
760
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 138 times.
138 } else if (avio_feof(pb)) {
761 return AVERROR_EOF;
762 } else {
763
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 16 times.
138 if (!s->keylen) {
764
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 121 times.
122 if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
765 1 AVPacket *const pkt = ffformatcontext(s)->parse_pkt;
766 unsigned int len;
767 1 av_log(s, AV_LOG_WARNING,
768 "DRM protected stream detected, decoding will likely fail!\n");
769 1 len= avio_rl32(pb);
770 1 av_log(s, AV_LOG_DEBUG, "Secret data:\n");
771
772
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = av_get_packet(pb, pkt, len)) < 0)
773 return ret;
774 1 av_hex_dump_log(s, AV_LOG_DEBUG, pkt->data, pkt->size);
775 1 av_packet_unref(pkt);
776
777 1 len= avio_rl32(pb);
778
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (len > UINT16_MAX)
779 return AVERROR_INVALIDDATA;
780 1 get_tag(s, "ASF_Protection_Type", -1, len, 32);
781
782 1 len= avio_rl32(pb);
783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (len > UINT16_MAX)
784 return AVERROR_INVALIDDATA;
785 1 get_tag(s, "ASF_Key_ID", -1, len, 32);
786
787 1 len= avio_rl32(pb);
788
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (len > UINT16_MAX)
789 return AVERROR_INVALIDDATA;
790 1 get_tag(s, "ASF_License_URL", -1, len, 32);
791
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 121 times.
121 } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
792 av_log(s, AV_LOG_WARNING,
793 "Ext DRM protected stream detected, decoding will likely fail!\n");
794 av_dict_set(&s->metadata, "encryption", "ASF Extended Content Encryption", 0);
795
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 121 times.
121 } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
796 av_log(s, AV_LOG_INFO, "Digital signature detected!\n");
797 }
798 }
799 }
800
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 315 times.
315 if (ret < 0)
801 return ret;
802
803
2/2
✓ Branch 1 taken 137 times.
✓ Branch 2 taken 178 times.
315 if (avio_tell(pb) != gpos + gsize)
804 137 av_log(s, AV_LOG_DEBUG,
805 "gpos mismatch our pos=%"PRIu64", end=%"PRId64"\n",
806 137 avio_tell(pb) - gpos, gsize);
807 315 avio_seek(pb, gpos + gsize, SEEK_SET);
808 }
809 33 ff_get_guid(pb, &g);
810 33 avio_rl64(pb);
811 33 avio_r8(pb);
812 33 avio_r8(pb);
813
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if (avio_feof(pb))
814 return AVERROR_EOF;
815 33 asf->data_offset = avio_tell(pb);
816 33 asf->packet_size_left = 0;
817
818
2/2
✓ Branch 0 taken 4224 times.
✓ Branch 1 taken 33 times.
4257 for (i = 0; i < 128; i++) {
819 4224 int stream_num = asf->asfid2avid[i];
820
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 4180 times.
4224 if (stream_num >= 0) {
821 44 AVStream *st = s->streams[stream_num];
822
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 24 times.
44 if (!st->codecpar->bit_rate)
823 20 st->codecpar->bit_rate = asf->stream_bitrates[i];
824
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
44 if (asf->dar[i].num > 0 && asf->dar[i].den > 0) {
825 1 av_reduce(&st->sample_aspect_ratio.num,
826 &st->sample_aspect_ratio.den,
827 1 asf->dar[i].num, asf->dar[i].den, INT_MAX);
828
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
43 } else if ((asf->dar[0].num > 0) && (asf->dar[0].den > 0) &&
829 // Use ASF container value if the stream doesn't set AR.
830 (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO))
831 av_reduce(&st->sample_aspect_ratio.num,
832 &st->sample_aspect_ratio.den,
833 asf->dar[0].num, asf->dar[0].den, INT_MAX);
834
835 44 av_log(s, AV_LOG_TRACE, "i=%d, st->codecpar->codec_type:%d, asf->dar %d:%d sar=%d:%d\n",
836 44 i, st->codecpar->codec_type, asf->dar[i].num, asf->dar[i].den,
837 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
838
839 // copy and convert language codes to the frontend
840
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 13 times.
44 if (asf->streams[i].stream_language_index < 128) {
841 31 const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
842
3/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 4 times.
31 if (rfc1766 && strlen(rfc1766) > 1) {
843 27 const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
844 27 const char *iso6392 = ff_convert_lang_to(primary_tag,
845 AV_LANG_ISO639_2_BIBL);
846
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2 times.
27 if (iso6392)
847 25 av_dict_set(&st->metadata, "language", iso6392, 0);
848 }
849 }
850 }
851 }
852
853 33 ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
854
855 33 return 0;
856 }
857
858 #define DO_2BITS(bits, var, defval) \
859 switch (bits & 3) { \
860 case 3: \
861 var = avio_rl32(pb); \
862 rsize += 4; \
863 break; \
864 case 2: \
865 var = avio_rl16(pb); \
866 rsize += 2; \
867 break; \
868 case 1: \
869 var = avio_r8(pb); \
870 rsize++; \
871 break; \
872 default: \
873 var = defval; \
874 break; \
875 }
876
877 /**
878 * Load a single ASF packet into the demuxer.
879 * @param s demux context
880 * @param pb context to read data from
881 * @return 0 on success, <0 on error
882 */
883 1958 static int asf_get_packet(AVFormatContext *s, AVIOContext *pb)
884 {
885 1958 ASFContext *asf = s->priv_data;
886 uint32_t packet_length, padsize;
887 1958 int rsize = 8;
888 int c, d, e, off;
889
890
2/2
✓ Branch 0 taken 1925 times.
✓ Branch 1 taken 33 times.
1958 if (asf->uses_std_ecc > 0) {
891 // if we do not know packet size, allow skipping up to 32 kB
892 1925 off = 32768;
893
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1925 times.
1925 if (asf->no_resync_search)
894 off = 3;
895 // else if (s->packet_size > 0 && !asf->uses_std_ecc)
896 // off = (avio_tell(pb) - ffformatcontext(s)->data_offset) % s->packet_size + 3;
897
898 1925 c = d = e = -1;
899
1/2
✓ Branch 0 taken 5775 times.
✗ Branch 1 not taken.
5775 while (off-- > 0) {
900 5775 c = d;
901 5775 d = e;
902 5775 e = avio_r8(pb);
903
4/6
✓ Branch 0 taken 1925 times.
✓ Branch 1 taken 3850 times.
✓ Branch 2 taken 1925 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1925 times.
✗ Branch 5 not taken.
5775 if (c == 0x82 && !d && !e)
904 1925 break;
905 }
906
907
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1925 times.
1925 if (c != 0x82) {
908 /* This code allows handling of -EAGAIN at packet boundaries (i.e.
909 * if the packet sync code above triggers -EAGAIN). This does not
910 * imply complete -EAGAIN handling support at random positions in
911 * the stream. */
912 if (pb->error == AVERROR(EAGAIN))
913 return AVERROR(EAGAIN);
914 if (!avio_feof(pb))
915 av_log(s, AV_LOG_ERROR,
916 "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
917 }
918
1/2
✓ Branch 0 taken 1925 times.
✗ Branch 1 not taken.
1925 if ((c & 0x8f) == 0x82) {
919
2/4
✓ Branch 0 taken 1925 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1925 times.
1925 if (d || e) {
920 if (!avio_feof(pb))
921 av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
922 return AVERROR_INVALIDDATA;
923 }
924 1925 c = avio_r8(pb);
925 1925 d = avio_r8(pb);
926 1925 rsize += 3;
927 } else if(!avio_feof(pb)) {
928 avio_seek(pb, -1, SEEK_CUR); // FIXME
929 }
930 } else {
931 33 c = avio_r8(pb);
932
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (c & 0x80) {
933 33 rsize ++;
934
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (!(c & 0x60)) {
935 33 d = avio_r8(pb);
936 33 e = avio_r8(pb);
937 33 avio_seek(pb, (c & 0xF) - 2, SEEK_CUR);
938 33 rsize += c & 0xF;
939 }
940
941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (c != 0x82)
942 avpriv_request_sample(s, "Invalid ECC byte");
943
944
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (!asf->uses_std_ecc)
945
3/6
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
33 asf->uses_std_ecc = (c == 0x82 && !d && !e) ? 1 : -1;
946
947 33 c = avio_r8(pb);
948 } else
949 asf->uses_std_ecc = -1;
950 33 d = avio_r8(pb);
951 }
952
953 1958 asf->packet_flags = c;
954 1958 asf->packet_property = d;
955
956
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1958 times.
1958 DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
957
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1958 times.
1958 DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
958
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
✓ Branch 2 taken 1408 times.
✓ Branch 3 taken 420 times.
1958 DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
959
960 // the following checks prevent overflows and infinite loops
961
2/4
✓ Branch 0 taken 1958 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1958 times.
1958 if (!packet_length || packet_length >= (1U << 29)) {
962 av_log(s, AV_LOG_ERROR,
963 "invalid packet_length %"PRIu32" at:%"PRId64"\n",
964 packet_length, avio_tell(pb));
965 return AVERROR_INVALIDDATA;
966 }
967
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1958 times.
1958 if (padsize >= packet_length) {
968 av_log(s, AV_LOG_ERROR,
969 "invalid padsize %"PRIu32" at:%"PRId64"\n", padsize, avio_tell(pb));
970 return AVERROR_INVALIDDATA;
971 }
972
973 1958 asf->packet_timestamp = avio_rl32(pb);
974 1958 avio_rl16(pb); /* duration */
975 // rsize has at least 11 bytes which have to be present
976
977
2/2
✓ Branch 0 taken 939 times.
✓ Branch 1 taken 1019 times.
1958 if (asf->packet_flags & 0x01) {
978 939 asf->packet_segsizetype = avio_r8(pb);
979 939 rsize++;
980 939 asf->packet_segments = asf->packet_segsizetype & 0x3f;
981 } else {
982 1019 asf->packet_segments = 1;
983 1019 asf->packet_segsizetype = 0x80;
984 }
985
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1958 times.
1958 if (rsize > packet_length - padsize) {
986 asf->packet_size_left = 0;
987 av_log(s, AV_LOG_ERROR,
988 "invalid packet header length %d for pktlen %"PRIu32"-%"PRIu32" at %"PRId64"\n",
989 rsize, packet_length, padsize, avio_tell(pb));
990 return AVERROR_INVALIDDATA;
991 }
992 1958 asf->packet_size_left = packet_length - padsize - rsize;
993
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1958 times.
1958 if (packet_length < asf->hdr.min_pktsize)
994 padsize += asf->hdr.min_pktsize - packet_length;
995 1958 asf->packet_padsize = padsize;
996 1958 av_log(s, AV_LOG_TRACE, "packet: size=%d padsize=%d left=%d\n",
997 s->packet_size, asf->packet_padsize, asf->packet_size_left);
998 1958 return 0;
999 }
1000
1001 /**
1002 *
1003 * @return <0 if error
1004 */
1005 3957 static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb)
1006 {
1007 3957 ASFContext *asf = s->priv_data;
1008 ASFStream *asfst;
1009 3957 int rsize = 1;
1010 3957 int num = avio_r8(pb);
1011 int i;
1012 int64_t ts0, ts1 av_unused;
1013
1014 3957 asf->packet_segments--;
1015 3957 asf->packet_key_frame = num >> 7;
1016 3957 asf->stream_index = asf->asfid2avid[num & 0x7f];
1017 3957 asfst = &asf->streams[num & 0x7f];
1018 // sequence should be ignored!
1019
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 3957 times.
✗ Branch 3 not taken.
3957 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
1020
1/4
✓ Branch 0 taken 3957 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3957 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
1021
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 3957 times.
✗ Branch 3 not taken.
3957 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
1022 3957 av_log(asf, AV_LOG_TRACE, "key:%d stream:%d seq:%d offset:%d replic_size:%d num:%X packet_property %X\n",
1023 asf->packet_key_frame, asf->stream_index, asf->packet_seq,
1024 asf->packet_frag_offset, asf->packet_replic_size, num, asf->packet_property);
1025
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3957 times.
3957 if (rsize+(int64_t)asf->packet_replic_size > asf->packet_size_left) {
1026 av_log(s, AV_LOG_ERROR, "packet_replic_size %d is invalid\n", asf->packet_replic_size);
1027 return AVERROR_INVALIDDATA;
1028 }
1029
2/2
✓ Branch 0 taken 3827 times.
✓ Branch 1 taken 130 times.
3957 if (asf->packet_replic_size >= 8) {
1030 3827 int64_t end = avio_tell(pb) + asf->packet_replic_size;
1031 AVRational aspect;
1032 3827 asfst->packet_obj_size = avio_rl32(pb);
1033
2/4
✓ Branch 0 taken 3827 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3827 times.
3827 if (asfst->packet_obj_size >= (1 << 24) || asfst->packet_obj_size < 0) {
1034 av_log(s, AV_LOG_ERROR, "packet_obj_size %d invalid\n", asfst->packet_obj_size);
1035 asfst->packet_obj_size = 0;
1036 return AVERROR_INVALIDDATA;
1037 }
1038 3827 asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
1039
1040
2/2
✓ Branch 0 taken 1326 times.
✓ Branch 1 taken 3827 times.
5153 for (i = 0; i < asfst->payload_ext_ct; i++) {
1041 1326 ASFPayload *p = &asfst->payload[i];
1042 1326 int size = p->size;
1043 int64_t payend;
1044
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1326 times.
1326 if (size == 0xFFFF)
1045 size = avio_rl16(pb);
1046 1326 payend = avio_tell(pb) + size;
1047
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1326 times.
1326 if (payend > end) {
1048 av_log(s, AV_LOG_ERROR, "too long payload\n");
1049 break;
1050 }
1051
2/5
✓ Branch 0 taken 1143 times.
✓ Branch 1 taken 183 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1326 switch (p->type) {
1052 1143 case 0x50:
1053 // duration = avio_rl16(pb);
1054 1143 break;
1055 183 case 0x54:
1056 183 aspect.num = avio_r8(pb);
1057 183 aspect.den = avio_r8(pb);
1058
3/6
✓ Branch 0 taken 183 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 183 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 183 times.
✗ Branch 5 not taken.
183 if (aspect.num > 0 && aspect.den > 0 && asf->stream_index >= 0) {
1059 183 s->streams[asf->stream_index]->sample_aspect_ratio = aspect;
1060 }
1061 183 break;
1062 case 0x2A:
1063 avio_skip(pb, 8);
1064 ts0 = avio_rl64(pb);
1065 ts1 = avio_rl64(pb);
1066 if (ts0!= -1) asf->packet_frag_timestamp = ts0/10000;
1067 else asf->packet_frag_timestamp = AV_NOPTS_VALUE;
1068 asf->ts_is_pts = 1;
1069 break;
1070 case 0x5B:
1071 case 0xB7:
1072 case 0xCC:
1073 case 0xC0:
1074 case 0xA0:
1075 //unknown
1076 break;
1077 }
1078 1326 avio_seek(pb, payend, SEEK_SET);
1079 }
1080
1081 3827 avio_seek(pb, end, SEEK_SET);
1082 3827 rsize += asf->packet_replic_size; // FIXME - check validity
1083
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 } else if (asf->packet_replic_size == 1) {
1084 // multipacket - frag_offset is beginning timestamp
1085 130 asf->packet_time_start = asf->packet_frag_offset;
1086 130 asf->packet_frag_offset = 0;
1087 130 asf->packet_frag_timestamp = asf->packet_timestamp;
1088
1089 130 asf->packet_time_delta = avio_r8(pb);
1090 130 rsize++;
1091 } else if (asf->packet_replic_size != 0) {
1092 av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n",
1093 asf->packet_replic_size);
1094 return AVERROR_INVALIDDATA;
1095 }
1096
2/2
✓ Branch 0 taken 2939 times.
✓ Branch 1 taken 1018 times.
3957 if (asf->packet_flags & 0x01) {
1097
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2939 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2939 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
1098
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2939 times.
2939 if (rsize > asf->packet_size_left) {
1099 av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
1100 return AVERROR_INVALIDDATA;
1101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2939 times.
2939 } else if (asf->packet_frag_size > asf->packet_size_left - rsize) {
1102 if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
1103 av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d>%d-%d+%d)\n",
1104 asf->packet_frag_size, asf->packet_size_left, rsize, asf->packet_padsize);
1105 return AVERROR_INVALIDDATA;
1106 } else {
1107 int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
1108 asf->packet_size_left += diff;
1109 asf->packet_padsize -= diff;
1110 }
1111 }
1112 } else {
1113 1018 asf->packet_frag_size = asf->packet_size_left - rsize;
1114 }
1115
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 3827 times.
3957 if (asf->packet_replic_size == 1) {
1116 130 asf->packet_multi_size = asf->packet_frag_size;
1117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 if (asf->packet_multi_size > asf->packet_size_left)
1118 return AVERROR_INVALIDDATA;
1119 }
1120 3957 asf->packet_size_left -= rsize;
1121
1122 3957 return 0;
1123 }
1124
1125 /**
1126 * Parse data from individual ASF packets (which were previously loaded
1127 * with asf_get_packet()).
1128 * @param s demux context
1129 * @param pb context to read data from
1130 * @param pkt pointer to store packet data into
1131 * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
1132 * packets need to be loaded (through asf_get_packet())
1133 */
1134 4423 static int asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
1135 {
1136 4423 ASFContext *asf = s->priv_data;
1137 4423 ASFStream *asf_st = 0;
1138 1588 for (;;) {
1139 int ret;
1140
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 6001 times.
6011 if (avio_feof(pb))
1141 10 return AVERROR_EOF;
1142
2/2
✓ Branch 0 taken 3998 times.
✓ Branch 1 taken 2003 times.
6001 if (asf->packet_size_left < FRAME_HEADER_SIZE ||
1143
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3997 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
3998 asf->packet_segments < 1 && asf->packet_time_start == 0) {
1144 2003 int ret = asf->packet_size_left + asf->packet_padsize;
1145
1146
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2003 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2003 if (asf->packet_size_left && asf->packet_size_left < FRAME_HEADER_SIZE)
1147 av_log(s, AV_LOG_WARNING, "Skip due to FRAME_HEADER_SIZE\n");
1148
1149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2003 times.
2003 assert(ret >= 0);
1150 /* fail safe */
1151 2003 avio_skip(pb, ret);
1152
1153 2003 asf->packet_pos = avio_tell(pb);
1154
1/2
✓ Branch 0 taken 2003 times.
✗ Branch 1 not taken.
2003 if (asf->data_object_size != (uint64_t)-1 &&
1155
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 1958 times.
2003 (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
1156 45 return AVERROR_EOF; /* Do not exceed the size of the data object */
1157 1958 return 1;
1158 }
1159
2/2
✓ Branch 0 taken 3957 times.
✓ Branch 1 taken 41 times.
3998 if (asf->packet_time_start == 0) {
1160
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3957 times.
3957 if (asf_read_frame_header(s, pb) < 0) {
1161 asf->packet_time_start = asf->packet_segments = 0;
1162 continue;
1163 }
1164
1/2
✓ Branch 0 taken 3957 times.
✗ Branch 1 not taken.
3957 if (asf->stream_index < 0 ||
1165
2/2
✓ Branch 0 taken 3664 times.
✓ Branch 1 taken 293 times.
3957 s->streams[asf->stream_index]->discard >= AVDISCARD_ALL ||
1166
2/2
✓ Branch 0 taken 3158 times.
✓ Branch 1 taken 506 times.
3664 (!asf->packet_key_frame &&
1167
3/4
✓ Branch 0 taken 3158 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 3143 times.
3158 (s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY || asf->streams[s->streams[asf->stream_index]->id].skip_to_key))) {
1168 308 asf->packet_time_start = 0;
1169 /* unhandled packet (should not happen) */
1170 308 avio_skip(pb, asf->packet_frag_size);
1171 308 asf->packet_size_left -= asf->packet_frag_size;
1172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 if (asf->stream_index < 0)
1173 av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n",
1174 asf->packet_frag_size);
1175 308 continue;
1176 }
1177 3649 asf->asf_st = &asf->streams[s->streams[asf->stream_index]->id];
1178
2/2
✓ Branch 0 taken 2397 times.
✓ Branch 1 taken 1252 times.
3649 if (!asf->packet_frag_offset)
1179 2397 asf->asf_st->skip_to_key = 0;
1180 }
1181 3690 asf_st = asf->asf_st;
1182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3690 times.
3690 av_assert0(asf_st);
1183
1184
4/4
✓ Branch 0 taken 2468 times.
✓ Branch 1 taken 1222 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 2438 times.
3690 if (!asf_st->frag_offset && asf->packet_frag_offset) {
1185 30 av_log(s, AV_LOG_TRACE, "skipping asf data pkt with fragment offset for "
1186 "stream:%d, expected:%d but got %d from pkt)\n",
1187 asf->stream_index, asf_st->frag_offset,
1188 asf->packet_frag_offset);
1189 30 avio_skip(pb, asf->packet_frag_size);
1190 30 asf->packet_size_left -= asf->packet_frag_size;
1191 30 continue;
1192 }
1193
1194
2/2
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 3545 times.
3660 if (asf->packet_replic_size == 1) {
1195 // frag_offset is here used as the beginning timestamp
1196 115 asf->packet_frag_timestamp = asf->packet_time_start;
1197 115 asf->packet_time_start += asf->packet_time_delta;
1198 115 asf_st->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
1199 115 asf->packet_size_left--;
1200 115 asf->packet_multi_size--;
1201
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 87 times.
115 if (asf->packet_multi_size < asf_st->packet_obj_size) {
1202 28 asf->packet_time_start = 0;
1203 28 avio_skip(pb, asf->packet_multi_size);
1204 28 asf->packet_size_left -= asf->packet_multi_size;
1205 28 continue;
1206 }
1207 87 asf->packet_multi_size -= asf_st->packet_obj_size;
1208 }
1209
1210
2/2
✓ Branch 0 taken 1222 times.
✓ Branch 1 taken 2410 times.
3632 if (asf_st->pkt.size != asf_st->packet_obj_size ||
1211 // FIXME is this condition sufficient?
1212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1222 times.
1222 asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) {
1213 int ret;
1214
1215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2410 times.
2410 if (asf_st->pkt.data) {
1216 av_log(s, AV_LOG_INFO,
1217 "freeing incomplete packet size %d, new %d\n",
1218 asf_st->pkt.size, asf_st->packet_obj_size);
1219 asf_st->frag_offset = 0;
1220 av_packet_unref(&asf_st->pkt);
1221 }
1222 /* new packet */
1223
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2410 times.
2410 if ((ret = av_new_packet(&asf_st->pkt, asf_st->packet_obj_size)) < 0)
1224 return ret;
1225 2410 asf_st->seq = asf->packet_seq;
1226
1/2
✓ Branch 0 taken 2410 times.
✗ Branch 1 not taken.
2410 if (asf->packet_frag_timestamp != AV_NOPTS_VALUE) {
1227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2410 times.
2410 if (asf->ts_is_pts) {
1228 asf_st->pkt.pts = asf->packet_frag_timestamp - asf->hdr.preroll;
1229 } else
1230 2410 asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
1231 }
1232 2410 asf_st->pkt.stream_index = asf->stream_index;
1233 2410 asf_st->pkt.pos = asf_st->packet_pos = asf->packet_pos;
1234 2410 asf_st->pkt_clean = 0;
1235
1236
2/4
✓ Branch 0 taken 2410 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2410 times.
2410 if (asf_st->pkt.data && asf_st->palette_changed) {
1237 uint8_t *pal;
1238 pal = av_packet_new_side_data(&asf_st->pkt, AV_PKT_DATA_PALETTE,
1239 AVPALETTE_SIZE);
1240 if (!pal) {
1241 av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
1242 } else {
1243 memcpy(pal, asf_st->palette, AVPALETTE_SIZE);
1244 asf_st->palette_changed = 0;
1245 }
1246 }
1247 2410 av_log(asf, AV_LOG_TRACE, "new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
1248 asf->stream_index, asf->packet_key_frame,
1249 2410 asf_st->pkt.flags & AV_PKT_FLAG_KEY,
1250 2410 s->streams[asf->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO,
1251 asf_st->packet_obj_size);
1252
2/2
✓ Branch 0 taken 1017 times.
✓ Branch 1 taken 1393 times.
2410 if (s->streams[asf->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
1253 1017 asf->packet_key_frame = 1;
1254
2/2
✓ Branch 0 taken 1062 times.
✓ Branch 1 taken 1348 times.
2410 if (asf->packet_key_frame)
1255 1062 asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
1256 }
1257
1258 /* read data */
1259 3632 av_log(asf, AV_LOG_TRACE, "READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
1260 s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
1261 asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
1262 3632 asf->packet_size_left -= asf->packet_frag_size;
1263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3632 times.
3632 if (asf->packet_size_left < 0)
1264 continue;
1265
1266
1/2
✓ Branch 0 taken 3632 times.
✗ Branch 1 not taken.
3632 if (asf->packet_frag_offset >= asf_st->pkt.size ||
1267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3632 times.
3632 asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset) {
1268 av_log(s, AV_LOG_ERROR,
1269 "packet fragment position invalid %u,%u not in %u\n",
1270 asf->packet_frag_offset, asf->packet_frag_size,
1271 asf_st->pkt.size);
1272 continue;
1273 }
1274
1275
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3632 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3632 if (asf->packet_frag_offset != asf_st->frag_offset && !asf_st->pkt_clean) {
1276 memset(asf_st->pkt.data + asf_st->frag_offset, 0, asf_st->pkt.size - asf_st->frag_offset);
1277 asf_st->pkt_clean = 1;
1278 }
1279
1280 3632 ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
1281 3632 asf->packet_frag_size);
1282
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3626 times.
3632 if (ret != asf->packet_frag_size) {
1283
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (ret < 0 || asf->packet_frag_offset + ret == 0)
1284 return ret < 0 ? ret : AVERROR_EOF;
1285
1286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (asf_st->ds_span > 1) {
1287 // scrambling, we can either drop it completely or fill the remainder
1288 // TODO: should we fill the whole packet instead of just the current
1289 // fragment?
1290 memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
1291 asf->packet_frag_size - ret);
1292 ret = asf->packet_frag_size;
1293 } else {
1294 // no scrambling, so we can return partial packets
1295 6 av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
1296 }
1297 }
1298
3/4
✓ Branch 0 taken 549 times.
✓ Branch 1 taken 3083 times.
✓ Branch 2 taken 549 times.
✗ Branch 3 not taken.
3632 if (s->key && s->keylen == 20)
1299 549 ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
1300 ret);
1301 3632 asf_st->frag_offset += ret;
1302 /* test if whole packet is read */
1303
2/2
✓ Branch 0 taken 2410 times.
✓ Branch 1 taken 1222 times.
3632 if (asf_st->frag_offset == asf_st->pkt.size) {
1304 // workaround for macroshit radio DVR-MS files
1305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2410 times.
2410 if (s->streams[asf->stream_index]->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
1306 asf_st->pkt.size > 100) {
1307 int i;
1308 for (i = 0; i < asf_st->pkt.size && !asf_st->pkt.data[i]; i++)
1309 ;
1310 if (i == asf_st->pkt.size) {
1311 av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
1312 asf_st->frag_offset = 0;
1313 av_packet_unref(&asf_st->pkt);
1314 continue;
1315 }
1316 }
1317
1318 /* return packet */
1319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2410 times.
2410 if (asf_st->ds_span > 1) {
1320 if (asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span) {
1321 av_log(s, AV_LOG_ERROR,
1322 "pkt.size != ds_packet_size * ds_span (%d %d %d)\n",
1323 asf_st->pkt.size, asf_st->ds_packet_size,
1324 asf_st->ds_span);
1325 } else {
1326 /* packet descrambling */
1327 AVBufferRef *buf = av_buffer_alloc(asf_st->pkt.size +
1328 AV_INPUT_BUFFER_PADDING_SIZE);
1329 if (buf) {
1330 uint8_t *newdata = buf->data;
1331 int offset = 0;
1332 memset(newdata + asf_st->pkt.size, 0,
1333 AV_INPUT_BUFFER_PADDING_SIZE);
1334 while (offset < asf_st->pkt.size) {
1335 int off = offset / asf_st->ds_chunk_size;
1336 int row = off / asf_st->ds_span;
1337 int col = off % asf_st->ds_span;
1338 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1339 assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
1340 assert(idx + 1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
1341 memcpy(newdata + offset,
1342 asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1343 asf_st->ds_chunk_size);
1344 offset += asf_st->ds_chunk_size;
1345 }
1346 av_buffer_unref(&asf_st->pkt.buf);
1347 asf_st->pkt.buf = buf;
1348 asf_st->pkt.data = buf->data;
1349 }
1350 }
1351 }
1352 2410 asf_st->frag_offset = 0;
1353 2410 *pkt = asf_st->pkt;
1354 2410 asf_st->pkt.buf = 0;
1355 2410 asf_st->pkt.size = 0;
1356 2410 asf_st->pkt.data = 0;
1357 2410 asf_st->pkt.side_data_elems = 0;
1358 2410 asf_st->pkt.side_data = NULL;
1359 2410 break; // packet completed
1360 }
1361 }
1362 2410 return 0;
1363 }
1364
1365 2465 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1366 {
1367 2465 ASFContext *asf = s->priv_data;
1368
1369 1958 for (;;) {
1370 int ret;
1371
1372 /* parse cached packets, if any */
1373
2/2
✓ Branch 1 taken 2465 times.
✓ Branch 2 taken 1958 times.
4423 if ((ret = asf_parse_packet(s, s->pb, pkt)) <= 0)
1374 2465 return ret;
1375
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1958 times.
1958 if ((ret = asf_get_packet(s, s->pb)) < 0)
1376 assert(asf->packet_size_left < FRAME_HEADER_SIZE ||
1377 asf->packet_segments < 1);
1378 1958 asf->packet_time_start = 0;
1379 }
1380 }
1381
1382 // Added to support seeking after packets have been read
1383 // If information is not reset, read_packet fails due to
1384 // leftover information from previous reads
1385 82 static void asf_reset_header(AVFormatContext *s)
1386 {
1387 82 ASFContext *asf = s->priv_data;
1388 ASFStream *asf_st;
1389 int i;
1390
1391 82 asf->packet_size_left = 0;
1392 82 asf->packet_flags = 0;
1393 82 asf->packet_property = 0;
1394 82 asf->packet_timestamp = 0;
1395 82 asf->packet_segsizetype = 0;
1396 82 asf->packet_segments = 0;
1397 82 asf->packet_seq = 0;
1398 82 asf->packet_replic_size = 0;
1399 82 asf->packet_key_frame = 0;
1400 82 asf->packet_padsize = 0;
1401 82 asf->packet_frag_offset = 0;
1402 82 asf->packet_frag_size = 0;
1403 82 asf->packet_frag_timestamp = 0;
1404 82 asf->packet_multi_size = 0;
1405 82 asf->packet_time_delta = 0;
1406 82 asf->packet_time_start = 0;
1407
1408
2/2
✓ Branch 0 taken 10496 times.
✓ Branch 1 taken 82 times.
10578 for (i = 0; i < 128; i++) {
1409 10496 asf_st = &asf->streams[i];
1410 10496 av_packet_unref(&asf_st->pkt);
1411 10496 asf_st->packet_obj_size = 0;
1412 10496 asf_st->frag_offset = 0;
1413 10496 asf_st->seq = 0;
1414 }
1415 82 asf->asf_st = NULL;
1416 82 }
1417
1418 26 static void skip_to_key(AVFormatContext *s)
1419 {
1420 26 ASFContext *asf = s->priv_data;
1421 int i;
1422
1423
2/2
✓ Branch 0 taken 3328 times.
✓ Branch 1 taken 26 times.
3354 for (i = 0; i < 128; i++) {
1424 3328 int j = asf->asfid2avid[i];
1425 3328 ASFStream *asf_st = &asf->streams[i];
1426
4/4
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 3276 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 26 times.
3328 if (j < 0 || s->streams[j]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
1427 3302 continue;
1428
1429 26 asf_st->skip_to_key = 1;
1430 }
1431 26 }
1432
1433 33 static int asf_read_close(AVFormatContext *s)
1434 {
1435 33 asf_reset_header(s);
1436
1437 33 return 0;
1438 }
1439
1440 23 static int64_t asf_read_pts(AVFormatContext *s, int stream_index,
1441 int64_t *ppos, int64_t pos_limit)
1442 {
1443 23 FFFormatContext *const si = ffformatcontext(s);
1444 23 ASFContext *asf = s->priv_data;
1445 23 AVPacket pkt1, *pkt = &pkt1;
1446 ASFStream *asf_st;
1447 int64_t pts;
1448 23 int64_t pos = *ppos;
1449 int i;
1450 int64_t start_pos[ASF_MAX_STREAMS];
1451
1452
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 23 times.
69 for (i = 0; i < s->nb_streams; i++)
1453 46 start_pos[i] = pos;
1454
1455
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 if (s->packet_size > 0)
1456 23 pos = (pos + s->packet_size - 1 - si->data_offset) /
1457 23 s->packet_size * s->packet_size +
1458 23 si->data_offset;
1459 23 *ppos = pos;
1460
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
23 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1461 return AV_NOPTS_VALUE;
1462
1463 23 ff_read_frame_flush(s);
1464 23 asf_reset_header(s);
1465 for (;;) {
1466
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 21 times.
36 if (av_read_frame(s, pkt) < 0) {
1467 15 av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
1468 15 return AV_NOPTS_VALUE;
1469 }
1470
1471 21 pts = pkt->dts;
1472
1473
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (pkt->flags & AV_PKT_FLAG_KEY) {
1474 21 i = pkt->stream_index;
1475
1476 21 asf_st = &asf->streams[s->streams[i]->id];
1477
1478 // assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
1479 21 pos = asf_st->packet_pos;
1480 av_assert1(pkt->pos == asf_st->packet_pos);
1481
1482 21 av_add_index_entry(s->streams[i], pos, pts, pkt->size,
1483 21 pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
1484 21 start_pos[i] = asf_st->packet_pos + 1;
1485
1486
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 13 times.
21 if (pkt->stream_index == stream_index) {
1487 8 av_packet_unref(pkt);
1488 8 break;
1489 }
1490 }
1491 13 av_packet_unref(pkt);
1492 }
1493
1494 8 *ppos = pos;
1495 8 return pts;
1496 }
1497
1498 1 static int asf_build_simple_index(AVFormatContext *s, int stream_index)
1499 {
1500 ff_asf_guid g;
1501 1 ASFContext *asf = s->priv_data;
1502 1 int64_t current_pos = avio_tell(s->pb);
1503 int64_t ret;
1504
1505
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if((ret = avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET)) < 0) {
1506 return ret;
1507 }
1508
1509
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = ff_get_guid(s->pb, &g)) < 0)
1510 goto end;
1511
1512 /* the data object can be followed by other top-level objects,
1513 * skip them until the simple index object is reached */
1514
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 while (ff_guidcmp(&g, &ff_asf_simple_index_header)) {
1515 int64_t gsize = avio_rl64(s->pb);
1516 if (gsize < 24 || avio_feof(s->pb)) {
1517 goto end;
1518 }
1519 avio_skip(s->pb, gsize - 24);
1520 if ((ret = ff_get_guid(s->pb, &g)) < 0)
1521 goto end;
1522 }
1523
1524 {
1525 1 int64_t itime, last_pos = -1;
1526 int pct, ict;
1527 int i;
1528 1 int64_t av_unused gsize = avio_rl64(s->pb);
1529
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = ff_get_guid(s->pb, &g)) < 0)
1530 goto end;
1531 1 itime = avio_rl64(s->pb);
1532 1 pct = avio_rl32(s->pb);
1533 1 ict = avio_rl32(s->pb);
1534 1 av_log(s, AV_LOG_DEBUG,
1535 "itime:0x%"PRIx64", pct:%d, ict:%d\n", itime, pct, ict);
1536
1537
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 for (i = 0; i < ict; i++) {
1538 6 int pktnum = avio_rl32(s->pb);
1539 6 int pktct = avio_rl16(s->pb);
1540 6 int64_t pos = ffformatcontext(s)->data_offset + s->packet_size * (int64_t)pktnum;
1541 6 int64_t index_pts = FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
1542
1543
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (avio_feof(s->pb)) {
1544 ret = AVERROR_INVALIDDATA;
1545 goto end;
1546 }
1547
1548
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (pos != last_pos) {
1549 3 av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n",
1550 pktnum, pktct, index_pts);
1551 3 av_add_index_entry(s->streams[stream_index], pos, index_pts,
1552 3 s->packet_size, 0, AVINDEX_KEYFRAME);
1553 3 last_pos = pos;
1554 }
1555 }
1556 1 asf->index_read = ict > 1;
1557 }
1558 1 end:
1559 // if (avio_feof(s->pb)) {
1560 // ret = 0;
1561 // }
1562 1 avio_seek(s->pb, current_pos, SEEK_SET);
1563 1 return ret;
1564 }
1565
1566 26 static int asf_read_seek(AVFormatContext *s, int stream_index,
1567 int64_t pts, int flags)
1568 {
1569 26 ASFContext *asf = s->priv_data;
1570 26 AVStream *st = s->streams[stream_index];
1571 26 FFStream *const sti = ffstream(st);
1572 26 int ret = 0;
1573
1574
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (s->packet_size <= 0)
1575 return -1;
1576
1577 /* Try using the protocol's read_seek if available */
1578
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (s->pb) {
1579 26 int64_t ret = avio_seek_time(s->pb, stream_index, pts, flags);
1580
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (ret >= 0)
1581 asf_reset_header(s);
1582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (ret != AVERROR(ENOSYS))
1583 return ret;
1584 }
1585
1586 /* explicitly handle the case of seeking to 0 */
1587
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (!pts) {
1588 asf_reset_header(s);
1589 avio_seek(s->pb, ffformatcontext(s)->data_offset, SEEK_SET);
1590 return 0;
1591 }
1592
1593
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 25 times.
26 if (!asf->index_read) {
1594 1 ret = asf_build_simple_index(s, stream_index);
1595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1596 asf->index_read = -1;
1597 }
1598
1599
2/4
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
26 if (asf->index_read > 0 && sti->index_entries) {
1600 26 int index = av_index_search_timestamp(st, pts, flags);
1601
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 8 times.
26 if (index >= 0) {
1602 /* find the position */
1603 18 uint64_t pos = sti->index_entries[index].pos;
1604
1605 /* do the seek */
1606 18 av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
1607
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if(avio_seek(s->pb, pos, SEEK_SET) < 0)
1608 return -1;
1609 18 asf_reset_header(s);
1610 18 skip_to_key(s);
1611 18 return 0;
1612 }
1613 }
1614 /* no index or seeking by index failed */
1615
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
1616 return -1;
1617 8 asf_reset_header(s);
1618 8 skip_to_key(s);
1619 8 return 0;
1620 }
1621
1622 const FFInputFormat ff_asf_demuxer = {
1623 .p.name = "asf",
1624 .p.long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1625 .p.flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,
1626 .p.priv_class = &asf_class,
1627 .priv_data_size = sizeof(ASFContext),
1628 .read_probe = asf_probe,
1629 .read_header = asf_read_header,
1630 .read_packet = asf_read_packet,
1631 .read_close = asf_read_close,
1632 .read_seek = asf_read_seek,
1633 .read_timestamp = asf_read_pts,
1634 };
1635