../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
690 / 915
75.4%
Branches:
323 / 558
57.9%
Functions:
21 / 22
95.5%

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