FFmpeg coverage


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