FFmpeg coverage


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