FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/asfdec_f.c
Date: 2025-08-19 23:55:23
Exec Total Coverage
Lines: 683 909 75.1%
Functions: 21 22 95.5%
Branches: 318 552 57.6%

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