| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Microsoft Advanced Streaming Format demuxer | ||
| 3 | * Copyright (c) 2014 Alexandra Hájková | ||
| 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 "libavutil/attributes.h" | ||
| 23 | #include "libavutil/common.h" | ||
| 24 | #include "libavutil/dict.h" | ||
| 25 | #include "libavutil/internal.h" | ||
| 26 | #include "libavutil/mathematics.h" | ||
| 27 | #include "libavutil/mem.h" | ||
| 28 | |||
| 29 | #include "avformat.h" | ||
| 30 | #include "avlanguage.h" | ||
| 31 | #include "demux.h" | ||
| 32 | #include "internal.h" | ||
| 33 | #include "riff.h" | ||
| 34 | #include "asf.h" | ||
| 35 | #include "asfcrypt.h" | ||
| 36 | |||
| 37 | #define ASF_FLAG_BROADCAST 0x1 | ||
| 38 | #define ASF_TYPE_AUDIO 0x2 | ||
| 39 | #define ASF_TYPE_VIDEO 0x1 | ||
| 40 | #define ASF_STREAM_NUM 0x7F | ||
| 41 | #define ASF_MAX_STREAMS 128 | ||
| 42 | #define BMP_HEADER_SIZE 40 | ||
| 43 | #define ASF_NUM_OF_PAYLOADS 0x3F | ||
| 44 | #define ASF_ERROR_CORRECTION_LENGTH_TYPE 0x60 | ||
| 45 | #define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2 | ||
| 46 | |||
| 47 | typedef struct GUIDParseTable { | ||
| 48 | const char *name; | ||
| 49 | ff_asf_guid guid; | ||
| 50 | int (*read_object)(AVFormatContext *, const struct GUIDParseTable *); | ||
| 51 | int is_subobject; | ||
| 52 | } GUIDParseTable; | ||
| 53 | |||
| 54 | typedef struct ASFPacket { | ||
| 55 | AVPacket *avpkt; | ||
| 56 | int64_t dts; | ||
| 57 | uint32_t frame_num; // ASF payloads with the same number are parts of the same frame | ||
| 58 | int flags; | ||
| 59 | int data_size; | ||
| 60 | int duration; | ||
| 61 | int size_left; | ||
| 62 | uint8_t stream_index; | ||
| 63 | } ASFPacket; | ||
| 64 | |||
| 65 | typedef struct ASFStream { | ||
| 66 | uint8_t stream_index; // from packet header | ||
| 67 | int index; // stream index in AVFormatContext, set in asf_read_stream_properties | ||
| 68 | int type; | ||
| 69 | int indexed; // added index entries from the Simple Index Object or not | ||
| 70 | int8_t span; // for deinterleaving | ||
| 71 | uint16_t virtual_pkt_len; | ||
| 72 | uint16_t virtual_chunk_len; | ||
| 73 | int16_t lang_idx; | ||
| 74 | ASFPacket pkt; | ||
| 75 | } ASFStream; | ||
| 76 | |||
| 77 | typedef struct ASFStreamData{ | ||
| 78 | char langs[32]; | ||
| 79 | AVDictionary *asf_met; // for storing per-stream metadata | ||
| 80 | AVRational aspect_ratio; | ||
| 81 | } ASFStreamData; | ||
| 82 | |||
| 83 | typedef struct ASFContext { | ||
| 84 | int data_reached; | ||
| 85 | int is_simple_index; // is simple index present or not 1/0 | ||
| 86 | int is_header; | ||
| 87 | |||
| 88 | uint64_t preroll; | ||
| 89 | uint64_t nb_packets; // ASF packets | ||
| 90 | uint32_t packet_size; | ||
| 91 | int64_t send_time; | ||
| 92 | int duration; | ||
| 93 | |||
| 94 | uint32_t b_flags; // flags with broadcast flag | ||
| 95 | uint32_t prop_flags; // file properties object flags | ||
| 96 | |||
| 97 | uint64_t data_size; // data object size | ||
| 98 | uint64_t unknown_size; // size of the unknown object | ||
| 99 | |||
| 100 | int64_t offset; // offset of the current object | ||
| 101 | |||
| 102 | int64_t data_offset; | ||
| 103 | int64_t first_packet_offset; // packet offset | ||
| 104 | int64_t unknown_offset; // for top level header objects or subobjects without specified behavior | ||
| 105 | int in_asf_read_unknown; | ||
| 106 | |||
| 107 | // ASF file must not contain more than 128 streams according to the specification | ||
| 108 | ASFStream *asf_st[ASF_MAX_STREAMS]; | ||
| 109 | ASFStreamData asf_sd[ASF_MAX_STREAMS]; | ||
| 110 | int nb_streams; | ||
| 111 | |||
| 112 | int stream_index; // from packet header, for the subpayload case | ||
| 113 | |||
| 114 | // packet parameters | ||
| 115 | uint64_t sub_header_offset; // offset of subpayload header | ||
| 116 | int64_t sub_dts; | ||
| 117 | uint8_t dts_delta; // for subpayloads | ||
| 118 | uint32_t packet_size_internal; // packet size stored inside ASFPacket, can be 0 | ||
| 119 | int64_t packet_offset; // offset of the current packet inside Data Object | ||
| 120 | uint32_t pad_len; // padding after payload | ||
| 121 | uint32_t rep_data_len; | ||
| 122 | |||
| 123 | // packet state | ||
| 124 | uint64_t sub_left; // subpayloads left or not | ||
| 125 | unsigned int nb_sub; // number of subpayloads read so far from the current ASF packet | ||
| 126 | uint16_t mult_sub_len; // total length of subpayloads array inside multiple payload | ||
| 127 | uint64_t nb_mult_left; // multiple payloads left | ||
| 128 | int return_subpayload; | ||
| 129 | enum { | ||
| 130 | PARSE_PACKET_HEADER, | ||
| 131 | READ_SINGLE, | ||
| 132 | READ_MULTI, | ||
| 133 | READ_MULTI_SUB | ||
| 134 | } state; | ||
| 135 | } ASFContext; | ||
| 136 | |||
| 137 | static int detect_unknown_subobject(AVFormatContext *s, int64_t offset, int64_t size); | ||
| 138 | static const GUIDParseTable *find_guid(ff_asf_guid guid); | ||
| 139 | |||
| 140 | 7778 | static int asf_probe(const AVProbeData *pd) | |
| 141 | { | ||
| 142 | /* check file header */ | ||
| 143 |
2/2✓ Branch 1 taken 36 times.
✓ Branch 2 taken 7742 times.
|
7778 | if (!ff_guidcmp(pd->buf, &ff_asf_header)) |
| 144 | 36 | return AVPROBE_SCORE_MAX/2; | |
| 145 | else | ||
| 146 | 7742 | return 0; | |
| 147 | } | ||
| 148 | |||
| 149 | 10 | static void swap_guid(ff_asf_guid guid) | |
| 150 | { | ||
| 151 | 10 | FFSWAP(unsigned char, guid[0], guid[3]); | |
| 152 | 10 | FFSWAP(unsigned char, guid[1], guid[2]); | |
| 153 | 10 | FFSWAP(unsigned char, guid[4], guid[5]); | |
| 154 | 10 | FFSWAP(unsigned char, guid[6], guid[7]); | |
| 155 | 10 | } | |
| 156 | |||
| 157 | 5 | static void align_position(AVIOContext *pb, int64_t offset, uint64_t size) | |
| 158 | { | ||
| 159 |
3/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 4 times.
|
5 | if (size < INT64_MAX - offset && avio_tell(pb) != offset + size) |
| 160 | 1 | avio_seek(pb, offset + size, SEEK_SET); | |
| 161 | 5 | } | |
| 162 | |||
| 163 | 4 | static int asf_read_unknown(AVFormatContext *s, const GUIDParseTable *g) | |
| 164 | { | ||
| 165 | 4 | ASFContext *asf = s->priv_data; | |
| 166 | 4 | AVIOContext *pb = s->pb; | |
| 167 | 4 | uint64_t size = avio_rl64(pb); | |
| 168 | int ret; | ||
| 169 | |||
| 170 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (size > INT64_MAX || asf->in_asf_read_unknown > 5) |
| 171 | ✗ | return AVERROR_INVALIDDATA; | |
| 172 | |||
| 173 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | if (asf->is_header) |
| 174 | 3 | asf->unknown_size = size; | |
| 175 | 4 | asf->is_header = 0; | |
| 176 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
|
4 | if (!g->is_subobject) { |
| 177 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!(ret = strcmp(g->name, "Header Extension"))) |
| 178 | 1 | avio_skip(pb, 22); // skip reserved fields and Data Size | |
| 179 | 1 | asf->in_asf_read_unknown ++; | |
| 180 | 1 | ret = detect_unknown_subobject(s, asf->unknown_offset, | |
| 181 | 1 | asf->unknown_size); | |
| 182 | 1 | asf->in_asf_read_unknown --; | |
| 183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
| 184 | ✗ | return ret; | |
| 185 | } else { | ||
| 186 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (size < 24) { |
| 187 | ✗ | av_log(s, AV_LOG_ERROR, "Too small size %"PRIu64" (< 24).\n", size); | |
| 188 | ✗ | return AVERROR_INVALIDDATA; | |
| 189 | } | ||
| 190 | 3 | avio_skip(pb, size - 24); | |
| 191 | } | ||
| 192 | |||
| 193 | 4 | return 0; | |
| 194 | } | ||
| 195 | |||
| 196 | 9 | static int get_asf_string(AVIOContext *pb, int maxlen, char *buf, int buflen) | |
| 197 | { | ||
| 198 | 9 | char *q = buf; | |
| 199 | 9 | int ret = 0; | |
| 200 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (buflen <= 0) |
| 201 | ✗ | return AVERROR(EINVAL); | |
| 202 |
2/2✓ Branch 0 taken 101 times.
✓ Branch 1 taken 9 times.
|
110 | while (ret + 1 < maxlen) { |
| 203 | uint8_t tmp; | ||
| 204 | uint32_t ch; | ||
| 205 |
2/10✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 101 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
101 | GET_UTF16(ch, (ret += 2) <= maxlen ? avio_rl16(pb) : 0, break;); |
| 206 |
7/10✓ Branch 0 taken 100 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 100 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 1 times.
|
102 | PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;) |
| 207 | } | ||
| 208 | 9 | *q = 0; | |
| 209 | |||
| 210 | 9 | return ret; | |
| 211 | } | ||
| 212 | |||
| 213 | ✗ | static int asf_read_marker(AVFormatContext *s, const GUIDParseTable *g) | |
| 214 | { | ||
| 215 | ✗ | ASFContext *asf = s->priv_data; | |
| 216 | ✗ | AVIOContext *pb = s->pb; | |
| 217 | ✗ | uint64_t size = avio_rl64(pb); | |
| 218 | int i, nb_markers, ret; | ||
| 219 | size_t len; | ||
| 220 | char name[1024]; | ||
| 221 | |||
| 222 | ✗ | avio_skip(pb, 8); | |
| 223 | ✗ | avio_skip(pb, 8); // skip reserved GUID | |
| 224 | ✗ | nb_markers = avio_rl32(pb); | |
| 225 | ✗ | avio_skip(pb, 2); // skip reserved field | |
| 226 | ✗ | len = avio_rl16(pb); | |
| 227 | ✗ | for (i = 0; i < len; i++) | |
| 228 | ✗ | avio_skip(pb, 1); | |
| 229 | |||
| 230 | ✗ | for (i = 0; i < nb_markers; i++) { | |
| 231 | int64_t pts; | ||
| 232 | |||
| 233 | ✗ | avio_skip(pb, 8); | |
| 234 | ✗ | pts = avio_rl64(pb); | |
| 235 | ✗ | pts -= asf->preroll * 10000; | |
| 236 | ✗ | avio_skip(pb, 2); // entry length | |
| 237 | ✗ | avio_skip(pb, 4); // send time | |
| 238 | ✗ | avio_skip(pb, 4); // flags | |
| 239 | ✗ | len = avio_rl32(pb); | |
| 240 | |||
| 241 | ✗ | if (avio_feof(pb)) | |
| 242 | ✗ | return AVERROR_INVALIDDATA; | |
| 243 | |||
| 244 | ✗ | if ((ret = avio_get_str16le(pb, len, name, | |
| 245 | sizeof(name))) < len) | ||
| 246 | ✗ | avio_skip(pb, len - ret); | |
| 247 | ✗ | avpriv_new_chapter(s, i, (AVRational) { 1, 10000000 }, pts, | |
| 248 | AV_NOPTS_VALUE, name); | ||
| 249 | } | ||
| 250 | ✗ | align_position(pb, asf->offset, size); | |
| 251 | |||
| 252 | ✗ | return 0; | |
| 253 | } | ||
| 254 | |||
| 255 | 5 | static int asf_read_metadata(AVFormatContext *s, const char *title, uint16_t len, | |
| 256 | unsigned char *ch, uint16_t buflen) | ||
| 257 | { | ||
| 258 | 5 | AVIOContext *pb = s->pb; | |
| 259 | |||
| 260 | 5 | avio_get_str16le(pb, len, ch, buflen); | |
| 261 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
|
5 | if (ch[0]) { |
| 262 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (av_dict_set(&s->metadata, title, ch, 0) < 0) |
| 263 | ✗ | av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n"); | |
| 264 | } | ||
| 265 | |||
| 266 | 5 | return 0; | |
| 267 | } | ||
| 268 | |||
| 269 | 10 | static int asf_read_value(AVFormatContext *s, const uint8_t *name, | |
| 270 | uint16_t val_len, int type, AVDictionary **met) | ||
| 271 | { | ||
| 272 | int ret; | ||
| 273 | uint8_t *value; | ||
| 274 | 10 | uint16_t buflen = 2 * val_len + 1; | |
| 275 | 10 | AVIOContext *pb = s->pb; | |
| 276 | |||
| 277 | 10 | value = av_malloc(buflen); | |
| 278 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (!value) |
| 279 | ✗ | return AVERROR(ENOMEM); | |
| 280 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
|
10 | if (type == ASF_UNICODE) { |
| 281 | // get_asf_string reads UTF-16 and converts it to UTF-8 which needs longer buffer | ||
| 282 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
|
9 | if ((ret = get_asf_string(pb, val_len, value, buflen)) < 0) |
| 283 | ✗ | goto failed; | |
| 284 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
|
9 | if (av_dict_set(met, name, value, 0) < 0) |
| 285 | ✗ | av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n"); | |
| 286 | } else { | ||
| 287 | char buf[256]; | ||
| 288 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (val_len > sizeof(buf)) { |
| 289 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 290 | ✗ | goto failed; | |
| 291 | } | ||
| 292 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = avio_read(pb, value, val_len)) < 0) |
| 293 | ✗ | goto failed; | |
| 294 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ret < 2 * val_len) |
| 295 | 1 | value[ret] = '\0'; | |
| 296 | else | ||
| 297 | ✗ | value[2 * val_len - 1] = '\0'; | |
| 298 | 1 | snprintf(buf, sizeof(buf), "%s", value); | |
| 299 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (av_dict_set(met, name, buf, 0) < 0) |
| 300 | ✗ | av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n"); | |
| 301 | } | ||
| 302 | 10 | av_freep(&value); | |
| 303 | |||
| 304 | 10 | return 0; | |
| 305 | |||
| 306 | ✗ | failed: | |
| 307 | ✗ | av_freep(&value); | |
| 308 | ✗ | return ret; | |
| 309 | } | ||
| 310 | 3 | static int asf_read_generic_value(AVIOContext *pb, int type, uint64_t *value) | |
| 311 | { | ||
| 312 | |||
| 313 |
2/5✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
3 | switch (type) { |
| 314 | ✗ | case ASF_BOOL: | |
| 315 | ✗ | *value = avio_rl16(pb); | |
| 316 | ✗ | break; | |
| 317 | 2 | case ASF_DWORD: | |
| 318 | 2 | *value = avio_rl32(pb); | |
| 319 | 2 | break; | |
| 320 | 1 | case ASF_QWORD: | |
| 321 | 1 | *value = avio_rl64(pb); | |
| 322 | 1 | break; | |
| 323 | ✗ | case ASF_WORD: | |
| 324 | ✗ | *value = avio_rl16(pb); | |
| 325 | ✗ | break; | |
| 326 | ✗ | default: | |
| 327 | ✗ | return AVERROR_INVALIDDATA; | |
| 328 | } | ||
| 329 | |||
| 330 | 3 | return 0; | |
| 331 | } | ||
| 332 | |||
| 333 | 3 | static int asf_set_metadata(AVFormatContext *s, const uint8_t *name, | |
| 334 | int type, AVDictionary **met) | ||
| 335 | { | ||
| 336 | 3 | AVIOContext *pb = s->pb; | |
| 337 | uint64_t value; | ||
| 338 | char buf[32]; | ||
| 339 | int ret; | ||
| 340 | |||
| 341 | 3 | ret = asf_read_generic_value(pb, type, &value); | |
| 342 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ret < 0) |
| 343 | ✗ | return ret; | |
| 344 | |||
| 345 | 3 | snprintf(buf, sizeof(buf), "%"PRIu64, value); | |
| 346 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (av_dict_set(met, name, buf, 0) < 0) |
| 347 | ✗ | av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n"); | |
| 348 | |||
| 349 | 3 | return 0; | |
| 350 | } | ||
| 351 | |||
| 352 | 19 | static int process_metadata(AVFormatContext *s, const uint8_t *name, uint16_t name_len, | |
| 353 | uint16_t val_len, uint16_t type, AVDictionary **met) | ||
| 354 | { | ||
| 355 | int ret; | ||
| 356 | ff_asf_guid guid; | ||
| 357 | |||
| 358 |
1/2✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
|
19 | if (val_len) { |
| 359 |
4/4✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 3 times.
|
19 | switch (type) { |
| 360 | 9 | case ASF_UNICODE: | |
| 361 | 9 | asf_read_value(s, name, val_len, type, met); | |
| 362 | 9 | break; | |
| 363 | 2 | case ASF_BYTE_ARRAY: | |
| 364 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | if (ff_asf_handle_byte_array(s, name, val_len) > 0) |
| 365 | 1 | asf_read_value(s, name, val_len, type, met); | |
| 366 | 2 | break; | |
| 367 | 5 | case ASF_GUID: | |
| 368 | 5 | ff_get_guid(s->pb, &guid); | |
| 369 | 5 | break; | |
| 370 | 3 | default: | |
| 371 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if ((ret = asf_set_metadata(s, name, type, met)) < 0) |
| 372 | ✗ | return ret; | |
| 373 | 3 | break; | |
| 374 | } | ||
| 375 | } | ||
| 376 | |||
| 377 | 19 | return 0; | |
| 378 | } | ||
| 379 | |||
| 380 | 1 | static int asf_read_ext_content(AVFormatContext *s, const GUIDParseTable *g) | |
| 381 | { | ||
| 382 | 1 | ASFContext *asf = s->priv_data; | |
| 383 | 1 | AVIOContext *pb = s->pb; | |
| 384 | 1 | uint64_t size = avio_rl64(pb); | |
| 385 | 1 | uint16_t nb_desc = avio_rl16(pb); | |
| 386 | int i, ret; | ||
| 387 | |||
| 388 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 1 times.
|
15 | for (i = 0; i < nb_desc; i++) { |
| 389 | uint16_t name_len, type, val_len; | ||
| 390 | 14 | uint8_t *name = NULL; | |
| 391 | |||
| 392 | 14 | name_len = avio_rl16(pb); | |
| 393 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (!name_len) |
| 394 | ✗ | return AVERROR_INVALIDDATA; | |
| 395 | 14 | name = av_malloc(name_len); | |
| 396 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (!name) |
| 397 | ✗ | return AVERROR(ENOMEM); | |
| 398 | 14 | avio_get_str16le(pb, name_len, name, | |
| 399 | name_len); | ||
| 400 | 14 | type = avio_rl16(pb); | |
| 401 | // BOOL values are 16 bits long in the Metadata Object | ||
| 402 | // but 32 bits long in the Extended Content Description Object | ||
| 403 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (type == ASF_BOOL) |
| 404 | ✗ | type = ASF_DWORD; | |
| 405 | 14 | val_len = avio_rl16(pb); | |
| 406 | |||
| 407 | 14 | ret = process_metadata(s, name, name_len, val_len, type, &s->metadata); | |
| 408 | 14 | av_freep(&name); | |
| 409 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (ret < 0) |
| 410 | ✗ | return ret; | |
| 411 | } | ||
| 412 | |||
| 413 | 1 | align_position(pb, asf->offset, size); | |
| 414 | 1 | return 0; | |
| 415 | } | ||
| 416 | |||
| 417 | 128 | static AVStream *find_stream(AVFormatContext *s, uint16_t st_num) | |
| 418 | { | ||
| 419 | 128 | AVStream *st = NULL; | |
| 420 | 128 | ASFContext *asf = s->priv_data; | |
| 421 | int i; | ||
| 422 | |||
| 423 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 127 times.
|
255 | for (i = 0; i < asf->nb_streams; i++) { |
| 424 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 127 times.
|
128 | if (asf->asf_st[i]->stream_index == st_num) { |
| 425 | 1 | st = s->streams[asf->asf_st[i]->index]; | |
| 426 | 1 | break; | |
| 427 | } | ||
| 428 | } | ||
| 429 | |||
| 430 | 128 | return st; | |
| 431 | } | ||
| 432 | |||
| 433 | ✗ | static int asf_store_aspect_ratio(AVFormatContext *s, uint8_t st_num, uint8_t *name, int type) | |
| 434 | { | ||
| 435 | ✗ | ASFContext *asf = s->priv_data; | |
| 436 | ✗ | AVIOContext *pb = s->pb; | |
| 437 | ✗ | uint64_t value = 0; | |
| 438 | int ret; | ||
| 439 | |||
| 440 | ✗ | ret = asf_read_generic_value(pb, type, &value); | |
| 441 | ✗ | if (ret < 0) | |
| 442 | ✗ | return ret; | |
| 443 | |||
| 444 | ✗ | if (st_num < ASF_MAX_STREAMS) { | |
| 445 | ✗ | if (!strcmp(name, "AspectRatioX")) | |
| 446 | ✗ | asf->asf_sd[st_num].aspect_ratio.num = value; | |
| 447 | else | ||
| 448 | ✗ | asf->asf_sd[st_num].aspect_ratio.den = value; | |
| 449 | } | ||
| 450 | ✗ | return 0; | |
| 451 | } | ||
| 452 | |||
| 453 | 1 | static int asf_read_metadata_obj(AVFormatContext *s, const GUIDParseTable *g) | |
| 454 | { | ||
| 455 | 1 | ASFContext *asf = s->priv_data; | |
| 456 | 1 | AVIOContext *pb = s->pb; | |
| 457 | 1 | uint64_t size = avio_rl64(pb); | |
| 458 | 1 | uint16_t nb_recs = avio_rl16(pb); // number of records in the Description Records list | |
| 459 | int i, ret; | ||
| 460 | |||
| 461 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | for (i = 0; i < nb_recs; i++) { |
| 462 | uint16_t name_len, buflen, type, val_len, st_num; | ||
| 463 | 5 | uint8_t *name = NULL; | |
| 464 | |||
| 465 | 5 | avio_skip(pb, 2); // skip reserved field | |
| 466 | 5 | st_num = avio_rl16(pb); | |
| 467 | 5 | name_len = avio_rl16(pb); | |
| 468 | 5 | buflen = 2 * name_len + 1; | |
| 469 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (!name_len) |
| 470 | ✗ | break; | |
| 471 | 5 | type = avio_rl16(pb); | |
| 472 | 5 | val_len = avio_rl32(pb); | |
| 473 | 5 | name = av_malloc(buflen); | |
| 474 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (!name) |
| 475 | ✗ | return AVERROR(ENOMEM); | |
| 476 | 5 | avio_get_str16le(pb, name_len, name, | |
| 477 | buflen); | ||
| 478 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
|
5 | if (!strcmp(name, "AspectRatioX") || !strcmp(name, "AspectRatioY")) { |
| 479 | ✗ | ret = asf_store_aspect_ratio(s, st_num, name, type); | |
| 480 | ✗ | if (ret < 0) { | |
| 481 | ✗ | av_freep(&name); | |
| 482 | ✗ | break; | |
| 483 | } | ||
| 484 | } else { | ||
| 485 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (st_num < ASF_MAX_STREAMS) { |
| 486 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
|
5 | if ((ret = process_metadata(s, name, name_len, val_len, type, |
| 487 | ✗ | st_num ? &asf->asf_sd[st_num].asf_met | |
| 488 | : &s->metadata)) < 0) { | ||
| 489 | ✗ | av_freep(&name); | |
| 490 | ✗ | break; | |
| 491 | } | ||
| 492 | } | ||
| 493 | } | ||
| 494 | 5 | av_freep(&name); | |
| 495 | } | ||
| 496 | |||
| 497 | 1 | align_position(pb, asf->offset, size); | |
| 498 | 1 | return 0; | |
| 499 | } | ||
| 500 | |||
| 501 | 1 | static int asf_read_content_desc(AVFormatContext *s, const GUIDParseTable *g) | |
| 502 | { | ||
| 503 | 1 | ASFContext *asf = s->priv_data; | |
| 504 | 1 | AVIOContext *pb = s->pb; | |
| 505 | int i; | ||
| 506 | static const char *const titles[] = | ||
| 507 | { "Title", "Author", "Copyright", "Description", "Rate" }; | ||
| 508 | 1 | uint16_t len[5], buflen[5] = { 0 }; | |
| 509 | uint8_t *ch; | ||
| 510 | 1 | uint64_t size = avio_rl64(pb); | |
| 511 | |||
| 512 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | for (i = 0; i < 5; i++) { |
| 513 | 5 | len[i] = avio_rl16(pb); | |
| 514 | // utf8 string should be <= 2 * utf16 string, extra byte for the terminator | ||
| 515 | 5 | buflen[i] = 2 * len[i] + 1; | |
| 516 | } | ||
| 517 | |||
| 518 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | for (i = 0; i < 5; i++) { |
| 519 | 5 | ch = av_malloc(buflen[i]); | |
| 520 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (!ch) |
| 521 | ✗ | return(AVERROR(ENOMEM)); | |
| 522 | 5 | asf_read_metadata(s, titles[i], len[i], ch, buflen[i]); | |
| 523 | 5 | av_freep(&ch); | |
| 524 | } | ||
| 525 | 1 | align_position(pb, asf->offset, size); | |
| 526 | |||
| 527 | 1 | return 0; | |
| 528 | } | ||
| 529 | |||
| 530 | 1 | static int asf_read_properties(AVFormatContext *s, const GUIDParseTable *g) | |
| 531 | { | ||
| 532 | 1 | ASFContext *asf = s->priv_data; | |
| 533 | 1 | AVIOContext *pb = s->pb; | |
| 534 | int64_t creation_time; | ||
| 535 | |||
| 536 | 1 | avio_rl64(pb); // read object size | |
| 537 | 1 | avio_skip(pb, 16); // skip File ID | |
| 538 | 1 | avio_skip(pb, 8); // skip File size | |
| 539 | 1 | creation_time = avio_rl64(pb); | |
| 540 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!(asf->b_flags & ASF_FLAG_BROADCAST)) { |
| 541 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (ff_dict_set_timestamp(&s->metadata, "creation_time", ff_asf_filetime_to_avtime(creation_time)) < 0) |
| 542 | ✗ | av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n"); | |
| 543 | } | ||
| 544 | 1 | asf->nb_packets = avio_rl64(pb); | |
| 545 | 1 | asf->duration = avio_rl64(pb) / 10000; // stream duration | |
| 546 | 1 | avio_skip(pb, 8); // skip send duration | |
| 547 | 1 | asf->preroll = avio_rl64(pb); | |
| 548 | 1 | asf->duration -= asf->preroll; | |
| 549 | 1 | asf->b_flags = avio_rl32(pb); | |
| 550 | 1 | avio_skip(pb, 4); // skip minimal packet size | |
| 551 | 1 | asf->packet_size = avio_rl32(pb); | |
| 552 | 1 | avio_skip(pb, 4); // skip max_bitrate | |
| 553 | |||
| 554 | 1 | return 0; | |
| 555 | } | ||
| 556 | |||
| 557 | ✗ | static int parse_video_info(AVFormatContext *avfmt, AVIOContext *pb, AVStream *st) | |
| 558 | { | ||
| 559 | uint16_t size_asf; // ASF-specific Format Data size | ||
| 560 | uint32_t size_bmp; // BMP_HEADER-specific Format Data size | ||
| 561 | unsigned int tag; | ||
| 562 | |||
| 563 | ✗ | st->codecpar->width = avio_rl32(pb); | |
| 564 | ✗ | st->codecpar->height = avio_rl32(pb); | |
| 565 | ✗ | avio_skip(pb, 1); // skip reserved flags | |
| 566 | ✗ | size_asf = avio_rl16(pb); | |
| 567 | ✗ | tag = ff_get_bmp_header(pb, st, &size_bmp); | |
| 568 | ✗ | st->codecpar->codec_tag = tag; | |
| 569 | ✗ | st->codecpar->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag); | |
| 570 | ✗ | size_bmp = FFMAX(size_asf, size_bmp); | |
| 571 | |||
| 572 | ✗ | if (size_bmp > BMP_HEADER_SIZE) { | |
| 573 | ✗ | int ret = ff_get_extradata(avfmt, st->codecpar, pb, size_bmp - BMP_HEADER_SIZE); | |
| 574 | |||
| 575 | ✗ | if (ret < 0) | |
| 576 | ✗ | return ret; | |
| 577 | } | ||
| 578 | ✗ | return 0; | |
| 579 | } | ||
| 580 | |||
| 581 | 1 | static int asf_read_stream_properties(AVFormatContext *s, const GUIDParseTable *g) | |
| 582 | { | ||
| 583 | 1 | ASFContext *asf = s->priv_data; | |
| 584 | 1 | AVIOContext *pb = s->pb; | |
| 585 | uint64_t size; | ||
| 586 | uint32_t err_data_len, ts_data_len; // type specific data length | ||
| 587 | uint16_t flags; | ||
| 588 | ff_asf_guid stream_type; | ||
| 589 | enum AVMediaType type; | ||
| 590 | int i, ret; | ||
| 591 | uint8_t stream_index; | ||
| 592 | AVStream *st; | ||
| 593 | ASFStream *asf_st; | ||
| 594 | |||
| 595 | // ASF file must not contain more than 128 streams according to the specification | ||
| 596 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (asf->nb_streams >= ASF_MAX_STREAMS) |
| 597 | ✗ | return AVERROR_INVALIDDATA; | |
| 598 | |||
| 599 | 1 | size = avio_rl64(pb); | |
| 600 | 1 | ff_get_guid(pb, &stream_type); | |
| 601 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (!ff_guidcmp(&stream_type, &ff_asf_audio_stream)) |
| 602 | 1 | type = AVMEDIA_TYPE_AUDIO; | |
| 603 | ✗ | else if (!ff_guidcmp(&stream_type, &ff_asf_video_stream)) | |
| 604 | ✗ | type = AVMEDIA_TYPE_VIDEO; | |
| 605 | ✗ | else if (!ff_guidcmp(&stream_type, &ff_asf_jfif_media)) | |
| 606 | ✗ | type = AVMEDIA_TYPE_VIDEO; | |
| 607 | ✗ | else if (!ff_guidcmp(&stream_type, &ff_asf_command_stream)) | |
| 608 | ✗ | type = AVMEDIA_TYPE_DATA; | |
| 609 | ✗ | else if (!ff_guidcmp(&stream_type, | |
| 610 | &ff_asf_ext_stream_embed_stream_header)) | ||
| 611 | ✗ | type = AVMEDIA_TYPE_UNKNOWN; | |
| 612 | else | ||
| 613 | ✗ | return AVERROR_INVALIDDATA; | |
| 614 | |||
| 615 | 1 | ff_get_guid(pb, &stream_type); // error correction type | |
| 616 | 1 | avio_skip(pb, 8); // skip the time offset | |
| 617 | 1 | ts_data_len = avio_rl32(pb); | |
| 618 | 1 | err_data_len = avio_rl32(pb); | |
| 619 | 1 | flags = avio_rl16(pb); // bit 15 - Encrypted Content | |
| 620 | |||
| 621 | 1 | stream_index = flags & ASF_STREAM_NUM; | |
| 622 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | for (i = 0; i < asf->nb_streams; i++) |
| 623 | ✗ | if (stream_index == asf->asf_st[i]->stream_index) { | |
| 624 | ✗ | av_log(s, AV_LOG_WARNING, | |
| 625 | "Duplicate stream found, this stream will be ignored.\n"); | ||
| 626 | ✗ | align_position(pb, asf->offset, size); | |
| 627 | ✗ | return 0; | |
| 628 | } | ||
| 629 | |||
| 630 | 1 | st = avformat_new_stream(s, NULL); | |
| 631 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!st) |
| 632 | ✗ | return AVERROR(ENOMEM); | |
| 633 | 1 | avpriv_set_pts_info(st, 32, 1, 1000); // pts should be dword, in milliseconds | |
| 634 | 1 | st->codecpar->codec_type = type; | |
| 635 | 1 | asf->asf_st[asf->nb_streams] = av_mallocz(sizeof(*asf_st)); | |
| 636 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!asf->asf_st[asf->nb_streams]) |
| 637 | ✗ | return AVERROR(ENOMEM); | |
| 638 | 1 | asf_st = asf->asf_st[asf->nb_streams]; | |
| 639 | 1 | asf->nb_streams++; | |
| 640 | 1 | asf_st->stream_index = stream_index; | |
| 641 | 1 | asf_st->index = st->index; | |
| 642 | 1 | asf_st->indexed = 0; | |
| 643 | 1 | st->id = flags & ASF_STREAM_NUM; | |
| 644 | 1 | asf_st->pkt.data_size = 0; | |
| 645 | 1 | asf_st->pkt.avpkt = av_packet_alloc(); | |
| 646 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!asf_st->pkt.avpkt) |
| 647 | ✗ | return AVERROR(ENOMEM); | |
| 648 | 1 | avio_skip(pb, 4); // skip reserved field | |
| 649 | |||
| 650 |
1/3✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
1 | switch (type) { |
| 651 | 1 | case AVMEDIA_TYPE_AUDIO: | |
| 652 | 1 | asf_st->type = AVMEDIA_TYPE_AUDIO; | |
| 653 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = ff_get_wav_header(s, pb, st->codecpar, ts_data_len, 0)) < 0) |
| 654 | ✗ | return ret; | |
| 655 | 1 | break; | |
| 656 | ✗ | case AVMEDIA_TYPE_VIDEO: | |
| 657 | ✗ | asf_st->type = AVMEDIA_TYPE_VIDEO; | |
| 658 | ✗ | if ((ret = parse_video_info(s, pb, st)) < 0) | |
| 659 | ✗ | return ret; | |
| 660 | ✗ | break; | |
| 661 | ✗ | default: | |
| 662 | ✗ | avio_skip(pb, ts_data_len); | |
| 663 | ✗ | break; | |
| 664 | } | ||
| 665 | |||
| 666 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (err_data_len) { |
| 667 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (type == AVMEDIA_TYPE_AUDIO) { |
| 668 | 1 | uint8_t span = avio_r8(pb); | |
| 669 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (span > 1) { |
| 670 | ✗ | asf_st->span = span; | |
| 671 | ✗ | asf_st->virtual_pkt_len = avio_rl16(pb); | |
| 672 | ✗ | asf_st->virtual_chunk_len = avio_rl16(pb); | |
| 673 | ✗ | if (!asf_st->virtual_chunk_len || !asf_st->virtual_pkt_len) | |
| 674 | ✗ | return AVERROR_INVALIDDATA; | |
| 675 | ✗ | avio_skip(pb, err_data_len - 5); | |
| 676 | } else | ||
| 677 | 1 | avio_skip(pb, err_data_len - 1); | |
| 678 | } else | ||
| 679 | ✗ | avio_skip(pb, err_data_len); | |
| 680 | } | ||
| 681 | |||
| 682 | 1 | align_position(pb, asf->offset, size); | |
| 683 | |||
| 684 | 1 | return 0; | |
| 685 | } | ||
| 686 | |||
| 687 | 1 | static void set_language(AVFormatContext *s, const char *rfc1766, AVDictionary **met) | |
| 688 | { | ||
| 689 | // language abbr should contain at least 2 chars | ||
| 690 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (rfc1766 && strlen(rfc1766) > 1) { |
| 691 | ✗ | const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any | |
| 692 | ✗ | const char *iso6392 = ff_convert_lang_to(primary_tag, | |
| 693 | AV_LANG_ISO639_2_BIBL); | ||
| 694 | ✗ | if (iso6392) | |
| 695 | ✗ | if (av_dict_set(met, "language", iso6392, 0) < 0) | |
| 696 | ✗ | av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n"); | |
| 697 | } | ||
| 698 | 1 | } | |
| 699 | |||
| 700 | ✗ | static int asf_read_ext_stream_properties(AVFormatContext *s, const GUIDParseTable *g) | |
| 701 | { | ||
| 702 | ✗ | ASFContext *asf = s->priv_data; | |
| 703 | ✗ | AVIOContext *pb = s->pb; | |
| 704 | ✗ | AVStream *st = NULL; | |
| 705 | ff_asf_guid guid; | ||
| 706 | uint16_t nb_st_name, nb_pay_exts, st_num, lang_idx; | ||
| 707 | int i, ret; | ||
| 708 | uint32_t bitrate; | ||
| 709 | uint64_t start_time, end_time, time_per_frame; | ||
| 710 | ✗ | uint64_t size = avio_rl64(pb); | |
| 711 | |||
| 712 | ✗ | start_time = avio_rl64(pb); | |
| 713 | ✗ | end_time = avio_rl64(pb); | |
| 714 | ✗ | bitrate = avio_rl32(pb); | |
| 715 | ✗ | avio_skip(pb, 28); // skip some unused values | |
| 716 | ✗ | st_num = avio_rl16(pb); | |
| 717 | ✗ | st_num &= ASF_STREAM_NUM; | |
| 718 | ✗ | lang_idx = avio_rl16(pb); // Stream Language ID Index | |
| 719 | ✗ | if (lang_idx >= ASF_MAX_STREAMS) | |
| 720 | ✗ | return AVERROR_INVALIDDATA; | |
| 721 | ✗ | for (i = 0; i < asf->nb_streams; i++) { | |
| 722 | ✗ | if (st_num == asf->asf_st[i]->stream_index) { | |
| 723 | ✗ | st = s->streams[asf->asf_st[i]->index]; | |
| 724 | ✗ | asf->asf_st[i]->lang_idx = lang_idx; | |
| 725 | ✗ | break; | |
| 726 | } | ||
| 727 | } | ||
| 728 | ✗ | time_per_frame = avio_rl64(pb); // average time per frame | |
| 729 | ✗ | if (st) { | |
| 730 | ✗ | st->start_time = start_time; | |
| 731 | ✗ | st->duration = end_time - start_time; | |
| 732 | ✗ | st->codecpar->bit_rate = bitrate; | |
| 733 | ✗ | st->avg_frame_rate.num = 10000000; | |
| 734 | ✗ | st->avg_frame_rate.den = time_per_frame; | |
| 735 | } | ||
| 736 | ✗ | nb_st_name = avio_rl16(pb); | |
| 737 | ✗ | nb_pay_exts = avio_rl16(pb); | |
| 738 | ✗ | for (i = 0; i < nb_st_name; i++) { | |
| 739 | uint16_t len; | ||
| 740 | |||
| 741 | ✗ | avio_rl16(pb); // Language ID Index | |
| 742 | ✗ | len = avio_rl16(pb); | |
| 743 | ✗ | avio_skip(pb, len); | |
| 744 | } | ||
| 745 | |||
| 746 | ✗ | for (i = 0; i < nb_pay_exts; i++) { | |
| 747 | uint32_t len; | ||
| 748 | ✗ | avio_skip(pb, 16); // Extension System ID | |
| 749 | ✗ | avio_skip(pb, 2); // Extension Data Size | |
| 750 | ✗ | len = avio_rl32(pb); | |
| 751 | ✗ | avio_skip(pb, len); | |
| 752 | } | ||
| 753 | |||
| 754 | ✗ | if ((ret = ff_get_guid(pb, &guid)) < 0) { | |
| 755 | ✗ | align_position(pb, asf->offset, size); | |
| 756 | |||
| 757 | ✗ | return 0; | |
| 758 | } | ||
| 759 | |||
| 760 | ✗ | g = find_guid(guid); | |
| 761 | ✗ | if (g && !(strcmp(g->name, "Stream Properties"))) { | |
| 762 | ✗ | if ((ret = g->read_object(s, g)) < 0) | |
| 763 | ✗ | return ret; | |
| 764 | } | ||
| 765 | |||
| 766 | ✗ | align_position(pb, asf->offset, size); | |
| 767 | ✗ | return 0; | |
| 768 | } | ||
| 769 | |||
| 770 | ✗ | static int asf_read_language_list(AVFormatContext *s, const GUIDParseTable *g) | |
| 771 | { | ||
| 772 | ✗ | ASFContext *asf = s->priv_data; | |
| 773 | ✗ | AVIOContext *pb = s->pb; | |
| 774 | int i, ret; | ||
| 775 | ✗ | uint64_t size = avio_rl64(pb); | |
| 776 | ✗ | uint16_t nb_langs = avio_rl16(pb); | |
| 777 | |||
| 778 | ✗ | if (nb_langs < ASF_MAX_STREAMS) { | |
| 779 | ✗ | for (i = 0; i < nb_langs; i++) { | |
| 780 | size_t len; | ||
| 781 | ✗ | len = avio_r8(pb); | |
| 782 | ✗ | if (!len) | |
| 783 | ✗ | len = 6; | |
| 784 | ✗ | if ((ret = get_asf_string(pb, len, asf->asf_sd[i].langs, | |
| 785 | sizeof(asf->asf_sd[i].langs))) < 0) { | ||
| 786 | ✗ | return ret; | |
| 787 | } | ||
| 788 | } | ||
| 789 | } | ||
| 790 | |||
| 791 | ✗ | align_position(pb, asf->offset, size); | |
| 792 | ✗ | return 0; | |
| 793 | } | ||
| 794 | |||
| 795 | // returns data object offset when reading this object for the first time | ||
| 796 | 1 | static int asf_read_data(AVFormatContext *s, const GUIDParseTable *g) | |
| 797 | { | ||
| 798 | 1 | ASFContext *asf = s->priv_data; | |
| 799 | 1 | AVIOContext *pb = s->pb; | |
| 800 | 1 | uint64_t size = asf->data_size = avio_rl64(pb); | |
| 801 | int i; | ||
| 802 | |||
| 803 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!asf->data_reached) { |
| 804 | 1 | asf->data_reached = 1; | |
| 805 | 1 | asf->data_offset = asf->offset; | |
| 806 | } | ||
| 807 | |||
| 808 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for (i = 0; i < asf->nb_streams; i++) { |
| 809 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!(asf->b_flags & ASF_FLAG_BROADCAST)) |
| 810 | 1 | s->streams[i]->duration = asf->duration; | |
| 811 | } | ||
| 812 | 1 | asf->nb_mult_left = 0; | |
| 813 | 1 | asf->sub_left = 0; | |
| 814 | 1 | asf->state = PARSE_PACKET_HEADER; | |
| 815 | 1 | asf->return_subpayload = 0; | |
| 816 | 1 | asf->packet_size_internal = 0; | |
| 817 | 1 | avio_skip(pb, 16); // skip File ID | |
| 818 | 1 | size = avio_rl64(pb); // Total Data Packets | |
| 819 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (size != asf->nb_packets) |
| 820 | ✗ | av_log(s, AV_LOG_WARNING, | |
| 821 | "Number of Packets from File Properties Object is not equal to Total" | ||
| 822 | "Datapackets value! num of packets %"PRIu64" total num %"PRIu64".\n", | ||
| 823 | size, asf->nb_packets); | ||
| 824 | 1 | avio_skip(pb, 2); // skip reserved field | |
| 825 | 1 | asf->first_packet_offset = avio_tell(pb); | |
| 826 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if ((pb->seekable & AVIO_SEEKABLE_NORMAL) && !(asf->b_flags & ASF_FLAG_BROADCAST)) |
| 827 | 1 | align_position(pb, asf->offset, asf->data_size); | |
| 828 | |||
| 829 | 1 | return 0; | |
| 830 | } | ||
| 831 | |||
| 832 | ✗ | static int asf_read_simple_index(AVFormatContext *s, const GUIDParseTable *g) | |
| 833 | { | ||
| 834 | ✗ | ASFContext *asf = s->priv_data; | |
| 835 | ✗ | AVIOContext *pb = s->pb; | |
| 836 | ✗ | AVStream *st = NULL; | |
| 837 | uint64_t interval; // index entry time interval in 100 ns units, usually it's 1s | ||
| 838 | uint32_t pkt_num, nb_entries; | ||
| 839 | ✗ | int32_t prev_pkt_num = -1; | |
| 840 | int i; | ||
| 841 | int64_t offset; | ||
| 842 | ✗ | uint64_t size = avio_rl64(pb); | |
| 843 | |||
| 844 | ✗ | if (size < 24) | |
| 845 | ✗ | return AVERROR_INVALIDDATA; | |
| 846 | |||
| 847 | // simple index objects should be ordered by stream number, this loop tries to find | ||
| 848 | // the first not indexed video stream | ||
| 849 | ✗ | for (i = 0; i < asf->nb_streams; i++) { | |
| 850 | ✗ | if ((asf->asf_st[i]->type == AVMEDIA_TYPE_VIDEO) && !asf->asf_st[i]->indexed) { | |
| 851 | ✗ | asf->asf_st[i]->indexed = 1; | |
| 852 | ✗ | st = s->streams[asf->asf_st[i]->index]; | |
| 853 | ✗ | break; | |
| 854 | } | ||
| 855 | } | ||
| 856 | ✗ | if (!st) { | |
| 857 | ✗ | avio_skip(pb, size - 24); // if there's no video stream, skip index object | |
| 858 | ✗ | return 0; | |
| 859 | } | ||
| 860 | ✗ | avio_skip(pb, 16); // skip File ID | |
| 861 | ✗ | interval = avio_rl64(pb); | |
| 862 | ✗ | avio_skip(pb, 4); | |
| 863 | ✗ | nb_entries = avio_rl32(pb); | |
| 864 | ✗ | for (i = 0; i < nb_entries; i++) { | |
| 865 | ✗ | pkt_num = avio_rl32(pb); | |
| 866 | ✗ | offset = avio_skip(pb, 2); | |
| 867 | ✗ | if (offset < 0) { | |
| 868 | ✗ | av_log(s, AV_LOG_ERROR, "Skipping failed in asf_read_simple_index.\n"); | |
| 869 | ✗ | return offset; | |
| 870 | } | ||
| 871 | ✗ | if (asf->first_packet_offset > INT64_MAX - asf->packet_size * pkt_num) | |
| 872 | ✗ | return AVERROR_INVALIDDATA; | |
| 873 | ✗ | if (prev_pkt_num != pkt_num) { | |
| 874 | ✗ | av_add_index_entry(st, asf->first_packet_offset + asf->packet_size * | |
| 875 | pkt_num, av_rescale(interval, i, 10000), | ||
| 876 | ✗ | asf->packet_size, 0, AVINDEX_KEYFRAME); | |
| 877 | ✗ | prev_pkt_num = pkt_num; | |
| 878 | } | ||
| 879 | } | ||
| 880 | ✗ | asf->is_simple_index = 1; | |
| 881 | ✗ | align_position(pb, asf->offset, size); | |
| 882 | |||
| 883 | ✗ | return 0; | |
| 884 | } | ||
| 885 | |||
| 886 | static const GUIDParseTable gdef[] = { | ||
| 887 | { "Data", { 0x75, 0xB2, 0x26, 0x36, 0x66, 0x8E, 0x11, 0xCF, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, asf_read_data, 1 }, | ||
| 888 | { "Simple Index", { 0x33, 0x00, 0x08, 0x90, 0xE5, 0xB1, 0x11, 0xCF, 0x89, 0xF4, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xCB }, asf_read_simple_index, 1 }, | ||
| 889 | { "Content Description", { 0x75, 0xB2, 0x26, 0x33, 0x66 ,0x8E, 0x11, 0xCF, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, asf_read_content_desc, 1 }, | ||
| 890 | { "Extended Content Description", { 0xD2, 0xD0, 0xA4, 0x40, 0xE3, 0x07, 0x11, 0xD2, 0x97, 0xF0, 0x00, 0xA0, 0xC9, 0x5e, 0xA8, 0x50 }, asf_read_ext_content, 1 }, | ||
| 891 | { "Stream Bitrate Properties", { 0x7B, 0xF8, 0x75, 0xCE, 0x46, 0x8D, 0x11, 0xD1, 0x8D, 0x82, 0x00, 0x60, 0x97, 0xC9, 0xA2, 0xB2 }, asf_read_unknown, 1 }, | ||
| 892 | { "File Properties", { 0x8C, 0xAB, 0xDC, 0xA1, 0xA9, 0x47, 0x11, 0xCF, 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_properties, 1 }, | ||
| 893 | { "Header Extension", { 0x5F, 0xBF, 0x03, 0xB5, 0xA9, 0x2E, 0x11, 0xCF, 0x8E, 0xE3, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_unknown, 0 }, | ||
| 894 | { "Stream Properties", { 0xB7, 0xDC, 0x07, 0x91, 0xA9, 0xB7, 0x11, 0xCF, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_stream_properties, 1 }, | ||
| 895 | { "Codec List", { 0x86, 0xD1, 0x52, 0x40, 0x31, 0x1D, 0x11, 0xD0, 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 }, asf_read_unknown, 1 }, | ||
| 896 | { "Marker", { 0xF4, 0x87, 0xCD, 0x01, 0xA9, 0x51, 0x11, 0xCF, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_marker, 1 }, | ||
| 897 | { "Script Command", { 0x1E, 0xFB, 0x1A, 0x30, 0x0B, 0x62, 0x11, 0xD0, 0xA3, 0x9B, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 }, asf_read_unknown, 1 }, | ||
| 898 | { "Language List", { 0x7C, 0x43, 0x46, 0xa9, 0xef, 0xe0, 0x4B, 0xFC, 0xB2, 0x29, 0x39, 0x3e, 0xde, 0x41, 0x5c, 0x85 }, asf_read_language_list, 1}, | ||
| 899 | { "Padding", { 0x18, 0x06, 0xD4, 0x74, 0xCA, 0xDF, 0x45, 0x09, 0xA4, 0xBA, 0x9A, 0xAB, 0xCB, 0x96, 0xAA, 0xE8 }, asf_read_unknown, 1 }, | ||
| 900 | { "DRMv1 Header", { 0x22, 0x11, 0xB3, 0xFB, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 }, | ||
| 901 | { "DRMv2 Header", { 0x29, 0x8A, 0xE6, 0x14, 0x26, 0x22, 0x4C, 0x17, 0xB9, 0x35, 0xDA, 0xE0, 0x7E, 0xE9, 0x28, 0x9c }, asf_read_unknown, 1 }, | ||
| 902 | { "Index", { 0xD6, 0xE2, 0x29, 0xD3, 0x35, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 }, | ||
| 903 | { "Media Object Index", { 0xFE, 0xB1, 0x03, 0xF8, 0x12, 0xAD, 0x4C, 0x64, 0x84, 0x0F, 0x2A, 0x1D, 0x2F, 0x7A, 0xD4, 0x8C }, asf_read_unknown, 1 }, | ||
| 904 | { "Timecode Index", { 0x3C, 0xB7, 0x3F, 0xD0, 0x0C, 0x4A, 0x48, 0x03, 0x95, 0x3D, 0xED, 0xF7, 0xB6, 0x22, 0x8F, 0x0C }, asf_read_unknown, 0 }, | ||
| 905 | { "Bitrate_Mutual_Exclusion", { 0xD6, 0xE2, 0x29, 0xDC, 0x35, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 }, | ||
| 906 | { "Error Correction", { 0x75, 0xB2, 0x26, 0x35, 0x66, 0x8E, 0x11, 0xCF, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, asf_read_unknown, 1 }, | ||
| 907 | { "Content Branding", { 0x22, 0x11, 0xB3, 0xFA, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 }, | ||
| 908 | { "Content Encryption", { 0x22, 0x11, 0xB3, 0xFB, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 }, | ||
| 909 | { "Extended Content Encryption", { 0x29, 0x8A, 0xE6, 0x14, 0x26, 0x22, 0x4C, 0x17, 0xB9, 0x35, 0xDA, 0xE0, 0x7E, 0xE9, 0x28, 0x9C }, asf_read_unknown, 1 }, | ||
| 910 | { "Digital Signature", { 0x22, 0x11, 0xB3, 0xFC, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 }, | ||
| 911 | { "Extended Stream Properties", { 0x14, 0xE6, 0xA5, 0xCB, 0xC6, 0x72, 0x43, 0x32, 0x83, 0x99, 0xA9, 0x69, 0x52, 0x06, 0x5B, 0x5A }, asf_read_ext_stream_properties, 1 }, | ||
| 912 | { "Advanced Mutual Exclusion", { 0xA0, 0x86, 0x49, 0xCF, 0x47, 0x75, 0x46, 0x70, 0x8A, 0x16, 0x6E, 0x35, 0x35, 0x75, 0x66, 0xCD }, asf_read_unknown, 1 }, | ||
| 913 | { "Group Mutual Exclusion", { 0xD1, 0x46, 0x5A, 0x40, 0x5A, 0x79, 0x43, 0x38, 0xB7, 0x1B, 0xE3, 0x6B, 0x8F, 0xD6, 0xC2, 0x49 }, asf_read_unknown, 1}, | ||
| 914 | { "Stream Prioritization", { 0xD4, 0xFE, 0xD1, 0x5B, 0x88, 0xD3, 0x45, 0x4F, 0x81, 0xF0, 0xED, 0x5C, 0x45, 0x99, 0x9E, 0x24 }, asf_read_unknown, 1 }, | ||
| 915 | { "Bandwidth Sharing Object", { 0xA6, 0x96, 0x09, 0xE6, 0x51, 0x7B, 0x11, 0xD2, 0xB6, 0xAF, 0x00, 0xC0, 0x4F, 0xD9, 0x08, 0xE9 }, asf_read_unknown, 1 }, | ||
| 916 | { "Metadata", { 0xC5, 0xF8, 0xCB, 0xEA, 0x5B, 0xAF, 0x48, 0x77, 0x84, 0x67, 0xAA, 0x8C, 0x44, 0xFA, 0x4C, 0xCA }, asf_read_metadata_obj, 1 }, | ||
| 917 | { "Metadata Library", { 0x44, 0x23, 0x1C, 0x94, 0x94, 0x98, 0x49, 0xD1, 0xA1, 0x41, 0x1D, 0x13, 0x4E, 0x45, 0x70, 0x54 }, asf_read_metadata_obj, 1 }, | ||
| 918 | { "Audio Spread", { 0xBF, 0xC3, 0xCD, 0x50, 0x61, 0x8F, 0x11, 0xCF, 0x8B, 0xB2, 0x00, 0xAA, 0x00, 0xB4, 0xE2, 0x20 }, asf_read_unknown, 1 }, | ||
| 919 | { "Index Parameters", { 0xD6, 0xE2, 0x29, 0xDF, 0x35, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 }, | ||
| 920 | { "Content Encryption System Windows Media DRM Network Devices", | ||
| 921 | { 0x7A, 0x07, 0x9B, 0xB6, 0xDA, 0XA4, 0x4e, 0x12, 0xA5, 0xCA, 0x91, 0xD3, 0x8D, 0xC1, 0x1A, 0x8D }, asf_read_unknown, 1 }, | ||
| 922 | { "Mutex Language", { 0xD6, 0xE2, 0x2A, 0x00, 0x25, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 }, | ||
| 923 | { "Mutex Bitrate", { 0xD6, 0xE2, 0x2A, 0x01, 0x25, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 }, | ||
| 924 | { "Mutex Unknown", { 0xD6, 0xE2, 0x2A, 0x02, 0x25, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 }, | ||
| 925 | { "Bandwidth Sharing Exclusive", { 0xAF, 0x60, 0x60, 0xAA, 0x51, 0x97, 0x11, 0xD2, 0xB6, 0xAF, 0x00, 0xC0, 0x4F, 0xD9, 0x08, 0xE9 }, asf_read_unknown, 1 }, | ||
| 926 | { "Bandwidth Sharing Partial", { 0xAF, 0x60, 0x60, 0xAB, 0x51, 0x97, 0x11, 0xD2, 0xB6, 0xAF, 0x00, 0xC0, 0x4F, 0xD9, 0x08, 0xE9 }, asf_read_unknown, 1 }, | ||
| 927 | { "Payload Extension System Timecode", { 0x39, 0x95, 0x95, 0xEC, 0x86, 0x67, 0x4E, 0x2D, 0x8F, 0xDB, 0x98, 0x81, 0x4C, 0xE7, 0x6C, 0x1E }, asf_read_unknown, 1 }, | ||
| 928 | { "Payload Extension System File Name", { 0xE1, 0x65, 0xEC, 0x0E, 0x19, 0xED, 0x45, 0xD7, 0xB4, 0xA7, 0x25, 0xCB, 0xD1, 0xE2, 0x8E, 0x9B }, asf_read_unknown, 1 }, | ||
| 929 | { "Payload Extension System Content Type", { 0xD5, 0x90, 0xDC, 0x20, 0x07, 0xBC, 0x43, 0x6C, 0x9C, 0xF7, 0xF3, 0xBB, 0xFB, 0xF1, 0xA4, 0xDC }, asf_read_unknown, 1 }, | ||
| 930 | { "Payload Extension System Pixel Aspect Ratio", { 0x1, 0x1E, 0xE5, 0x54, 0xF9, 0xEA, 0x4B, 0xC8, 0x82, 0x1A, 0x37, 0x6B, 0x74, 0xE4, 0xC4, 0xB8 }, asf_read_unknown, 1 }, | ||
| 931 | { "Payload Extension System Sample Duration", { 0xC6, 0xBD, 0x94, 0x50, 0x86, 0x7F, 0x49, 0x07, 0x83, 0xA3, 0xC7, 0x79, 0x21, 0xB7, 0x33, 0xAD }, asf_read_unknown, 1 }, | ||
| 932 | { "Payload Extension System Encryption Sample ID", { 0x66, 0x98, 0xB8, 0x4E, 0x0A, 0xFA, 0x43, 0x30, 0xAE, 0xB2, 0x1C, 0x0A, 0x98, 0xD7, 0xA4, 0x4D }, asf_read_unknown, 1 }, | ||
| 933 | { "Payload Extension System Degradable JPEG", { 0x00, 0xE1, 0xAF, 0x06, 0x7B, 0xEC, 0x11, 0xD1, 0xA5, 0x82, 0x00, 0xC0, 0x4F, 0xC2, 0x9C, 0xFB }, asf_read_unknown, 1 }, | ||
| 934 | }; | ||
| 935 | |||
| 936 | #define READ_LEN(flag, name, len) \ | ||
| 937 | do { \ | ||
| 938 | if ((flag) == name ## IS_BYTE) \ | ||
| 939 | len = avio_r8(pb); \ | ||
| 940 | else if ((flag) == name ## IS_WORD) \ | ||
| 941 | len = avio_rl16(pb); \ | ||
| 942 | else if ((flag) == name ## IS_DWORD) \ | ||
| 943 | len = avio_rl32(pb); \ | ||
| 944 | else \ | ||
| 945 | len = 0; \ | ||
| 946 | } while(0) | ||
| 947 | |||
| 948 | ✗ | static int asf_read_subpayload(AVFormatContext *s, AVPacket *pkt, int is_header) | |
| 949 | { | ||
| 950 | ✗ | ASFContext *asf = s->priv_data; | |
| 951 | ✗ | AVIOContext *pb = s->pb; | |
| 952 | uint8_t sub_len; | ||
| 953 | int ret, i; | ||
| 954 | |||
| 955 | ✗ | if (is_header) { | |
| 956 | ✗ | asf->dts_delta = avio_r8(pb); | |
| 957 | ✗ | if (asf->nb_mult_left) { | |
| 958 | ✗ | asf->mult_sub_len = avio_rl16(pb); // total | |
| 959 | } | ||
| 960 | ✗ | asf->sub_header_offset = avio_tell(pb); | |
| 961 | ✗ | asf->nb_sub = 0; | |
| 962 | ✗ | asf->sub_left = 1; | |
| 963 | } | ||
| 964 | ✗ | sub_len = avio_r8(pb); | |
| 965 | ✗ | if ((ret = av_get_packet(pb, pkt, sub_len)) < 0) // each subpayload is entire frame | |
| 966 | ✗ | return ret; | |
| 967 | ✗ | for (i = 0; i < asf->nb_streams; i++) { | |
| 968 | ✗ | if (asf->stream_index == asf->asf_st[i]->stream_index) { | |
| 969 | ✗ | pkt->stream_index = asf->asf_st[i]->index; | |
| 970 | ✗ | break; | |
| 971 | } | ||
| 972 | } | ||
| 973 | ✗ | asf->return_subpayload = 1; | |
| 974 | ✗ | if (!sub_len) | |
| 975 | ✗ | asf->return_subpayload = 0; | |
| 976 | |||
| 977 | ✗ | if (sub_len) | |
| 978 | ✗ | asf->nb_sub++; | |
| 979 | ✗ | pkt->dts = asf->sub_dts + (asf->nb_sub - 1) * asf->dts_delta - asf->preroll; | |
| 980 | ✗ | if (asf->nb_mult_left && (avio_tell(pb) >= | |
| 981 | ✗ | (asf->sub_header_offset + asf->mult_sub_len))) { | |
| 982 | ✗ | asf->sub_left = 0; | |
| 983 | ✗ | asf->nb_mult_left--; | |
| 984 | } | ||
| 985 | ✗ | if (avio_tell(pb) >= asf->packet_offset + asf->packet_size - asf->pad_len) { | |
| 986 | ✗ | asf->sub_left = 0; | |
| 987 | ✗ | if (!asf->nb_mult_left) { | |
| 988 | ✗ | avio_skip(pb, asf->pad_len); | |
| 989 | ✗ | if (avio_tell(pb) != asf->packet_offset + asf->packet_size) { | |
| 990 | ✗ | if (!asf->packet_size) | |
| 991 | ✗ | return AVERROR_INVALIDDATA; | |
| 992 | ✗ | av_log(s, AV_LOG_WARNING, | |
| 993 | "Position %"PRId64" wrong, should be %"PRId64"\n", | ||
| 994 | ✗ | avio_tell(pb), asf->packet_offset + asf->packet_size); | |
| 995 | ✗ | avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET); | |
| 996 | } | ||
| 997 | } | ||
| 998 | } | ||
| 999 | |||
| 1000 | ✗ | return 0; | |
| 1001 | } | ||
| 1002 | |||
| 1003 | ✗ | static void reset_packet(ASFPacket *asf_pkt) | |
| 1004 | { | ||
| 1005 | ✗ | asf_pkt->size_left = 0; | |
| 1006 | ✗ | asf_pkt->data_size = 0; | |
| 1007 | ✗ | asf_pkt->duration = 0; | |
| 1008 | ✗ | asf_pkt->flags = 0; | |
| 1009 | ✗ | asf_pkt->dts = 0; | |
| 1010 | ✗ | av_packet_unref(asf_pkt->avpkt); | |
| 1011 | ✗ | } | |
| 1012 | |||
| 1013 | ✗ | static int asf_read_replicated_data(AVFormatContext *s, ASFPacket *asf_pkt) | |
| 1014 | { | ||
| 1015 | ✗ | ASFContext *asf = s->priv_data; | |
| 1016 | ✗ | AVIOContext *pb = s->pb; | |
| 1017 | int ret, data_size; | ||
| 1018 | |||
| 1019 | ✗ | if (!asf_pkt->data_size) { | |
| 1020 | ✗ | data_size = avio_rl32(pb); // read media object size | |
| 1021 | ✗ | if (data_size <= 0) | |
| 1022 | ✗ | return AVERROR_INVALIDDATA; | |
| 1023 | ✗ | if ((ret = av_new_packet(asf_pkt->avpkt, data_size)) < 0) | |
| 1024 | ✗ | return ret; | |
| 1025 | ✗ | asf_pkt->data_size = asf_pkt->size_left = data_size; | |
| 1026 | } else | ||
| 1027 | ✗ | avio_skip(pb, 4); // reading of media object size is already done | |
| 1028 | ✗ | asf_pkt->dts = avio_rl32(pb); // read presentation time | |
| 1029 | ✗ | if (asf->rep_data_len >= 8) | |
| 1030 | ✗ | avio_skip(pb, asf->rep_data_len - 8); // skip replicated data | |
| 1031 | |||
| 1032 | ✗ | return 0; | |
| 1033 | } | ||
| 1034 | |||
| 1035 | ✗ | static int asf_read_multiple_payload(AVFormatContext *s, AVPacket *pkt, | |
| 1036 | ASFPacket *asf_pkt) | ||
| 1037 | { | ||
| 1038 | ✗ | ASFContext *asf = s->priv_data; | |
| 1039 | ✗ | AVIOContext *pb = s->pb; | |
| 1040 | uint16_t pay_len; | ||
| 1041 | unsigned char *p; | ||
| 1042 | int ret; | ||
| 1043 | ✗ | int skip = 0; | |
| 1044 | |||
| 1045 | // if replicated length is 1, subpayloads are present | ||
| 1046 | ✗ | if (asf->rep_data_len == 1) { | |
| 1047 | ✗ | asf->sub_left = 1; | |
| 1048 | ✗ | asf->state = READ_MULTI_SUB; | |
| 1049 | ✗ | pkt->flags = asf_pkt->flags; | |
| 1050 | ✗ | if ((ret = asf_read_subpayload(s, pkt, 1)) < 0) | |
| 1051 | ✗ | return ret; | |
| 1052 | } else { | ||
| 1053 | ✗ | if (asf->rep_data_len) | |
| 1054 | ✗ | if ((ret = asf_read_replicated_data(s, asf_pkt)) < 0) | |
| 1055 | ✗ | return ret; | |
| 1056 | ✗ | pay_len = avio_rl16(pb); // payload length should be WORD | |
| 1057 | ✗ | if (pay_len > asf->packet_size) { | |
| 1058 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 1059 | "Error: invalid data packet size, pay_len %"PRIu16", " | ||
| 1060 | "asf->packet_size %"PRIu32", offset %"PRId64".\n", | ||
| 1061 | pay_len, asf->packet_size, avio_tell(pb)); | ||
| 1062 | ✗ | return AVERROR_INVALIDDATA; | |
| 1063 | } | ||
| 1064 | ✗ | p = asf_pkt->avpkt->data + asf_pkt->data_size - asf_pkt->size_left; | |
| 1065 | ✗ | if (pay_len > asf_pkt->size_left) { | |
| 1066 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 1067 | "Error: invalid buffer size, pay_len %d, data size left %d.\n", | ||
| 1068 | pay_len, asf_pkt->size_left); | ||
| 1069 | ✗ | skip = pay_len - asf_pkt->size_left; | |
| 1070 | ✗ | pay_len = asf_pkt->size_left; | |
| 1071 | } | ||
| 1072 | ✗ | if (asf_pkt->size_left <= 0) | |
| 1073 | ✗ | return AVERROR_INVALIDDATA; | |
| 1074 | ✗ | if ((ret = avio_read(pb, p, pay_len)) < 0) | |
| 1075 | ✗ | return ret; | |
| 1076 | ✗ | if (s->key && s->keylen == 20) | |
| 1077 | ✗ | ff_asfcrypt_dec(s->key, p, ret); | |
| 1078 | ✗ | avio_skip(pb, skip); | |
| 1079 | ✗ | asf_pkt->size_left -= pay_len; | |
| 1080 | ✗ | asf->nb_mult_left--; | |
| 1081 | } | ||
| 1082 | |||
| 1083 | ✗ | return 0; | |
| 1084 | } | ||
| 1085 | |||
| 1086 | 1 | static int asf_read_single_payload(AVFormatContext *s, ASFPacket *asf_pkt) | |
| 1087 | { | ||
| 1088 | 1 | ASFContext *asf = s->priv_data; | |
| 1089 | 1 | AVIOContext *pb = s->pb; | |
| 1090 | int64_t offset; | ||
| 1091 | uint64_t size; | ||
| 1092 | unsigned char *p; | ||
| 1093 | int ret, data_size; | ||
| 1094 | |||
| 1095 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!asf_pkt->data_size) { |
| 1096 | 1 | data_size = avio_rl32(pb); // read media object size | |
| 1097 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (data_size <= 0) |
| 1098 | ✗ | return AVERROR_EOF; | |
| 1099 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = av_new_packet(asf_pkt->avpkt, data_size)) < 0) |
| 1100 | ✗ | return ret; | |
| 1101 | 1 | asf_pkt->data_size = asf_pkt->size_left = data_size; | |
| 1102 | } else | ||
| 1103 | ✗ | avio_skip(pb, 4); // skip media object size | |
| 1104 | 1 | asf_pkt->dts = avio_rl32(pb); // read presentation time | |
| 1105 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (asf->rep_data_len >= 8) |
| 1106 | 1 | avio_skip(pb, asf->rep_data_len - 8); // skip replicated data | |
| 1107 | 1 | offset = avio_tell(pb); | |
| 1108 | |||
| 1109 | // size of the payload - size of the packet without header and padding | ||
| 1110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (asf->packet_size_internal) |
| 1111 | ✗ | size = asf->packet_size_internal - offset + asf->packet_offset - asf->pad_len; | |
| 1112 | else | ||
| 1113 | 1 | size = asf->packet_size - offset + asf->packet_offset - asf->pad_len; | |
| 1114 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (size > asf->packet_size) { |
| 1115 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 1116 | "Error: invalid data packet size, offset %"PRId64".\n", | ||
| 1117 | avio_tell(pb)); | ||
| 1118 | ✗ | return AVERROR_INVALIDDATA; | |
| 1119 | } | ||
| 1120 | 1 | p = asf_pkt->avpkt->data + asf_pkt->data_size - asf_pkt->size_left; | |
| 1121 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (size > asf_pkt->size_left || asf_pkt->size_left <= 0) |
| 1122 | ✗ | return AVERROR_INVALIDDATA; | |
| 1123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (asf_pkt->size_left > size) |
| 1124 | ✗ | asf_pkt->size_left -= size; | |
| 1125 | else | ||
| 1126 | 1 | asf_pkt->size_left = 0; | |
| 1127 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = avio_read(pb, p, size)) < 0) |
| 1128 | ✗ | return ret; | |
| 1129 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | if (s->key && s->keylen == 20) |
| 1130 | ✗ | ff_asfcrypt_dec(s->key, p, ret); | |
| 1131 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (asf->packet_size_internal) |
| 1132 | ✗ | avio_skip(pb, asf->packet_size - asf->packet_size_internal); | |
| 1133 | 1 | avio_skip(pb, asf->pad_len); // skip padding | |
| 1134 | |||
| 1135 | 1 | return 0; | |
| 1136 | } | ||
| 1137 | |||
| 1138 | 1 | static int asf_read_payload(AVFormatContext *s, AVPacket *pkt) | |
| 1139 | { | ||
| 1140 | 1 | ASFContext *asf = s->priv_data; | |
| 1141 | 1 | AVIOContext *pb = s->pb; | |
| 1142 | int ret, i; | ||
| 1143 | 1 | ASFPacket *asf_pkt = NULL; | |
| 1144 | |||
| 1145 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!asf->sub_left) { |
| 1146 | uint32_t off_len, media_len; | ||
| 1147 | uint8_t stream_num; | ||
| 1148 | |||
| 1149 | 1 | stream_num = avio_r8(pb); | |
| 1150 | 1 | asf->stream_index = stream_num & ASF_STREAM_NUM; | |
| 1151 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | for (i = 0; i < asf->nb_streams; i++) { |
| 1152 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (asf->stream_index == asf->asf_st[i]->stream_index) { |
| 1153 | 1 | asf_pkt = &asf->asf_st[i]->pkt; | |
| 1154 | 1 | asf_pkt->stream_index = asf->asf_st[i]->index; | |
| 1155 | 1 | break; | |
| 1156 | } | ||
| 1157 | } | ||
| 1158 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!asf_pkt) { |
| 1159 | ✗ | if (asf->packet_offset + asf->packet_size <= asf->data_offset + asf->data_size) { | |
| 1160 | ✗ | if (!asf->packet_size) { | |
| 1161 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid packet size 0.\n"); | |
| 1162 | ✗ | return AVERROR_INVALIDDATA; | |
| 1163 | } | ||
| 1164 | ✗ | avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET); | |
| 1165 | ✗ | av_log(s, AV_LOG_WARNING, "Skipping the stream with the invalid stream index %d.\n", | |
| 1166 | asf->stream_index); | ||
| 1167 | ✗ | return AVERROR(EAGAIN); | |
| 1168 | } else | ||
| 1169 | ✗ | return AVERROR_INVALIDDATA; | |
| 1170 | } | ||
| 1171 | |||
| 1172 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (stream_num >> 7) |
| 1173 | ✗ | asf_pkt->flags |= AV_PKT_FLAG_KEY; | |
| 1174 |
1/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
1 | READ_LEN(asf->prop_flags & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE, |
| 1175 | ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_, media_len); | ||
| 1176 |
3/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
|
1 | READ_LEN(asf->prop_flags & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE, |
| 1177 | ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_, off_len); | ||
| 1178 |
1/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
1 | READ_LEN(asf->prop_flags & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE, |
| 1179 | ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_, asf->rep_data_len); | ||
| 1180 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | if (asf_pkt->size_left && (asf_pkt->frame_num != media_len)) { |
| 1181 | ✗ | av_log(s, AV_LOG_WARNING, "Unfinished frame will be ignored\n"); | |
| 1182 | ✗ | reset_packet(asf_pkt); | |
| 1183 | } | ||
| 1184 | 1 | asf_pkt->frame_num = media_len; | |
| 1185 | 1 | asf->sub_dts = off_len; | |
| 1186 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (asf->nb_mult_left) { |
| 1187 | ✗ | if ((ret = asf_read_multiple_payload(s, pkt, asf_pkt)) < 0) | |
| 1188 | ✗ | return ret; | |
| 1189 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | } else if (asf->rep_data_len == 1) { |
| 1190 | ✗ | asf->sub_left = 1; | |
| 1191 | ✗ | asf->state = READ_SINGLE; | |
| 1192 | ✗ | pkt->flags = asf_pkt->flags; | |
| 1193 | ✗ | if ((ret = asf_read_subpayload(s, pkt, 1)) < 0) | |
| 1194 | ✗ | return ret; | |
| 1195 | } else { | ||
| 1196 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = asf_read_single_payload(s, asf_pkt)) < 0) |
| 1197 | ✗ | return ret; | |
| 1198 | } | ||
| 1199 | } else { | ||
| 1200 | ✗ | for (i = 0; i <= asf->nb_streams; i++) { | |
| 1201 | ✗ | if (asf->stream_index == asf->asf_st[i]->stream_index) { | |
| 1202 | ✗ | asf_pkt = &asf->asf_st[i]->pkt; | |
| 1203 | ✗ | break; | |
| 1204 | } | ||
| 1205 | } | ||
| 1206 | ✗ | if (!asf_pkt) | |
| 1207 | ✗ | return AVERROR_INVALIDDATA; | |
| 1208 | ✗ | pkt->flags = asf_pkt->flags; | |
| 1209 | ✗ | pkt->dts = asf_pkt->dts; | |
| 1210 | ✗ | pkt->stream_index = asf->asf_st[i]->index; | |
| 1211 | ✗ | if ((ret = asf_read_subpayload(s, pkt, 0)) < 0) // read subpayload without its header | |
| 1212 | ✗ | return ret; | |
| 1213 | } | ||
| 1214 | |||
| 1215 | 1 | return 0; | |
| 1216 | } | ||
| 1217 | |||
| 1218 | 1 | static int asf_read_packet_header(AVFormatContext *s) | |
| 1219 | { | ||
| 1220 | 1 | ASFContext *asf = s->priv_data; | |
| 1221 | 1 | AVIOContext *pb = s->pb; | |
| 1222 | uint64_t size; | ||
| 1223 | av_unused uint32_t seq; | ||
| 1224 | unsigned char error_flags, len_flags, pay_flags; | ||
| 1225 | |||
| 1226 | 1 | asf->packet_offset = avio_tell(pb); | |
| 1227 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (asf->packet_offset > INT64_MAX/2) |
| 1228 | ✗ | asf->packet_offset = 0; | |
| 1229 | 1 | error_flags = avio_r8(pb); // read Error Correction Flags | |
| 1230 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (error_flags & ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT) { |
| 1231 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!(error_flags & ASF_ERROR_CORRECTION_LENGTH_TYPE)) { |
| 1232 | 1 | size = error_flags & ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; | |
| 1233 | 1 | avio_skip(pb, size); | |
| 1234 | } | ||
| 1235 | 1 | len_flags = avio_r8(pb); | |
| 1236 | } else | ||
| 1237 | ✗ | len_flags = error_flags; | |
| 1238 | 1 | asf->prop_flags = avio_r8(pb); | |
| 1239 |
3/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
|
1 | READ_LEN(len_flags & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE, |
| 1240 | ASF_PPI_FLAG_PACKET_LENGTH_FIELD_, asf->packet_size_internal); | ||
| 1241 |
3/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
|
1 | READ_LEN(len_flags & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE, |
| 1242 | ASF_PPI_FLAG_SEQUENCE_FIELD_, seq); | ||
| 1243 |
1/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
1 | READ_LEN(len_flags & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE, |
| 1244 | ASF_PPI_FLAG_PADDING_LENGTH_FIELD_, asf->pad_len ); | ||
| 1245 | 1 | asf->send_time = avio_rl32(pb); // send time | |
| 1246 | 1 | avio_skip(pb, 2); // skip duration | |
| 1247 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (len_flags & ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT) { // Multiple Payloads present |
| 1248 | ✗ | pay_flags = avio_r8(pb); | |
| 1249 | ✗ | asf->nb_mult_left = (pay_flags & ASF_NUM_OF_PAYLOADS); | |
| 1250 | } | ||
| 1251 | |||
| 1252 | 1 | return 0; | |
| 1253 | } | ||
| 1254 | |||
| 1255 | ✗ | static int asf_deinterleave(AVFormatContext *s, ASFPacket *asf_pkt, int st_num) | |
| 1256 | { | ||
| 1257 | ✗ | ASFContext *asf = s->priv_data; | |
| 1258 | ✗ | ASFStream *asf_st = asf->asf_st[st_num]; | |
| 1259 | ✗ | unsigned char *p = asf_pkt->avpkt->data; | |
| 1260 | ✗ | uint16_t pkt_len = asf->asf_st[st_num]->virtual_pkt_len; | |
| 1261 | ✗ | uint16_t chunk_len = asf->asf_st[st_num]->virtual_chunk_len; | |
| 1262 | ✗ | int nchunks = pkt_len / chunk_len; | |
| 1263 | uint8_t *data; | ||
| 1264 | ✗ | int pos = 0, j, l, ret; | |
| 1265 | |||
| 1266 | |||
| 1267 | ✗ | data = av_malloc(asf_pkt->data_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 1268 | ✗ | if (!data) | |
| 1269 | ✗ | return AVERROR(ENOMEM); | |
| 1270 | ✗ | memset(data + asf_pkt->data_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
| 1271 | |||
| 1272 | ✗ | while (asf_pkt->data_size >= asf_st->span * pkt_len + pos) { | |
| 1273 | ✗ | if (pos >= asf_pkt->data_size) { | |
| 1274 | ✗ | break; | |
| 1275 | } | ||
| 1276 | ✗ | for (l = 0; l < pkt_len; l++) { | |
| 1277 | ✗ | if (pos >= asf_pkt->data_size) { | |
| 1278 | ✗ | break; | |
| 1279 | } | ||
| 1280 | ✗ | for (j = 0; j < asf_st->span; j++) { | |
| 1281 | ✗ | if ((pos + chunk_len) >= asf_pkt->data_size) | |
| 1282 | ✗ | break; | |
| 1283 | ✗ | memcpy(data + pos, | |
| 1284 | ✗ | p + (j * nchunks + l) * chunk_len, | |
| 1285 | chunk_len); | ||
| 1286 | ✗ | pos += chunk_len; | |
| 1287 | } | ||
| 1288 | } | ||
| 1289 | ✗ | p += asf_st->span * pkt_len; | |
| 1290 | ✗ | if (p > asf_pkt->avpkt->data + asf_pkt->data_size) | |
| 1291 | ✗ | break; | |
| 1292 | } | ||
| 1293 | ✗ | av_packet_unref(asf_pkt->avpkt); | |
| 1294 | ✗ | ret = av_packet_from_data(asf_pkt->avpkt, data, asf_pkt->data_size); | |
| 1295 | ✗ | if (ret < 0) | |
| 1296 | ✗ | av_free(data); | |
| 1297 | |||
| 1298 | ✗ | return ret; | |
| 1299 | } | ||
| 1300 | |||
| 1301 | 1 | static int asf_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 1302 | { | ||
| 1303 | 1 | ASFContext *asf = s->priv_data; | |
| 1304 | 1 | AVIOContext *pb = s->pb; | |
| 1305 | int ret, i; | ||
| 1306 | |||
| 1307 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((avio_tell(pb) >= asf->data_offset + asf->data_size) && |
| 1308 | ✗ | !(asf->b_flags & ASF_FLAG_BROADCAST)) | |
| 1309 | ✗ | return AVERROR_EOF; | |
| 1310 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | while (!pb->eof_reached) { |
| 1311 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (asf->state == PARSE_PACKET_HEADER) { |
| 1312 | 1 | asf_read_packet_header(s); | |
| 1313 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (pb->eof_reached) |
| 1314 | ✗ | break; | |
| 1315 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!asf->nb_mult_left) |
| 1316 | 1 | asf->state = READ_SINGLE; | |
| 1317 | else | ||
| 1318 | ✗ | asf->state = READ_MULTI; | |
| 1319 | } | ||
| 1320 | 1 | ret = asf_read_payload(s, pkt); | |
| 1321 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret == AVERROR(EAGAIN)) { |
| 1322 | ✗ | asf->state = PARSE_PACKET_HEADER; | |
| 1323 | ✗ | continue; | |
| 1324 | } | ||
| 1325 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | else if (ret < 0) |
| 1326 | ✗ | return ret; | |
| 1327 | |||
| 1328 |
1/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | switch (asf->state) { |
| 1329 | 1 | case READ_SINGLE: | |
| 1330 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!asf->sub_left) |
| 1331 | 1 | asf->state = PARSE_PACKET_HEADER; | |
| 1332 | 1 | break; | |
| 1333 | ✗ | case READ_MULTI_SUB: | |
| 1334 | ✗ | if (!asf->sub_left && !asf->nb_mult_left) { | |
| 1335 | ✗ | asf->state = PARSE_PACKET_HEADER; | |
| 1336 | ✗ | if (!asf->return_subpayload && | |
| 1337 | ✗ | (avio_tell(pb) <= asf->packet_offset + | |
| 1338 | ✗ | asf->packet_size - asf->pad_len)) | |
| 1339 | ✗ | avio_skip(pb, asf->pad_len); // skip padding | |
| 1340 | ✗ | if (asf->packet_offset + asf->packet_size > avio_tell(pb)) | |
| 1341 | ✗ | avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET); | |
| 1342 | ✗ | } else if (!asf->sub_left) | |
| 1343 | ✗ | asf->state = READ_MULTI; | |
| 1344 | ✗ | break; | |
| 1345 | ✗ | case READ_MULTI: | |
| 1346 | ✗ | if (!asf->nb_mult_left) { | |
| 1347 | ✗ | asf->state = PARSE_PACKET_HEADER; | |
| 1348 | ✗ | if (!asf->return_subpayload && | |
| 1349 | ✗ | (avio_tell(pb) <= asf->packet_offset + | |
| 1350 | ✗ | asf->packet_size - asf->pad_len)) | |
| 1351 | ✗ | avio_skip(pb, asf->pad_len); // skip padding | |
| 1352 | ✗ | if (asf->packet_offset + asf->packet_size > avio_tell(pb)) | |
| 1353 | ✗ | avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET); | |
| 1354 | } | ||
| 1355 | ✗ | break; | |
| 1356 | } | ||
| 1357 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (asf->return_subpayload) { |
| 1358 | ✗ | asf->return_subpayload = 0; | |
| 1359 | ✗ | return 0; | |
| 1360 | } | ||
| 1361 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | for (i = 0; i < asf->nb_streams; i++) { |
| 1362 | 1 | ASFPacket *asf_pkt = &asf->asf_st[i]->pkt; | |
| 1363 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | if (asf_pkt && !asf_pkt->size_left && asf_pkt->data_size) { |
| 1364 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (asf->asf_st[i]->span > 1 && |
| 1365 | ✗ | asf->asf_st[i]->type == AVMEDIA_TYPE_AUDIO) | |
| 1366 | ✗ | if ((ret = asf_deinterleave(s, asf_pkt, i)) < 0) | |
| 1367 | ✗ | return ret; | |
| 1368 | 1 | av_packet_move_ref(pkt, asf_pkt->avpkt); | |
| 1369 | 1 | pkt->stream_index = asf->asf_st[i]->index; | |
| 1370 | 1 | pkt->flags = asf_pkt->flags; | |
| 1371 | 1 | pkt->dts = asf_pkt->dts - asf->preroll; | |
| 1372 | 1 | asf_pkt->data_size = 0; | |
| 1373 | 1 | asf_pkt->frame_num = 0; | |
| 1374 | 1 | return 0; | |
| 1375 | } | ||
| 1376 | } | ||
| 1377 | } | ||
| 1378 | |||
| 1379 | ✗ | if (pb->eof_reached) | |
| 1380 | ✗ | return AVERROR_EOF; | |
| 1381 | |||
| 1382 | ✗ | return 0; | |
| 1383 | } | ||
| 1384 | |||
| 1385 | 1 | static int asf_read_close(AVFormatContext *s) | |
| 1386 | { | ||
| 1387 | 1 | ASFContext *asf = s->priv_data; | |
| 1388 | int i; | ||
| 1389 | |||
| 1390 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 1 times.
|
129 | for (i = 0; i < ASF_MAX_STREAMS; i++) { |
| 1391 | 128 | av_dict_free(&asf->asf_sd[i].asf_met); | |
| 1392 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 127 times.
|
128 | if (i < asf->nb_streams) { |
| 1393 | 1 | av_packet_free(&asf->asf_st[i]->pkt.avpkt); | |
| 1394 | 1 | av_freep(&asf->asf_st[i]); | |
| 1395 | } | ||
| 1396 | } | ||
| 1397 | |||
| 1398 | 1 | asf->nb_streams = 0; | |
| 1399 | 1 | return 0; | |
| 1400 | } | ||
| 1401 | |||
| 1402 | ✗ | static void reset_packet_state(AVFormatContext *s) | |
| 1403 | { | ||
| 1404 | ✗ | ASFContext *asf = s->priv_data; | |
| 1405 | int i; | ||
| 1406 | |||
| 1407 | ✗ | asf->state = PARSE_PACKET_HEADER; | |
| 1408 | ✗ | asf->offset = 0; | |
| 1409 | ✗ | asf->return_subpayload = 0; | |
| 1410 | ✗ | asf->sub_left = 0; | |
| 1411 | ✗ | asf->sub_header_offset = 0; | |
| 1412 | ✗ | asf->packet_offset = asf->first_packet_offset; | |
| 1413 | ✗ | asf->pad_len = 0; | |
| 1414 | ✗ | asf->rep_data_len = 0; | |
| 1415 | ✗ | asf->dts_delta = 0; | |
| 1416 | ✗ | asf->mult_sub_len = 0; | |
| 1417 | ✗ | asf->nb_mult_left = 0; | |
| 1418 | ✗ | asf->nb_sub = 0; | |
| 1419 | ✗ | asf->prop_flags = 0; | |
| 1420 | ✗ | asf->sub_dts = 0; | |
| 1421 | ✗ | for (i = 0; i < asf->nb_streams; i++) { | |
| 1422 | ✗ | ASFPacket *pkt = &asf->asf_st[i]->pkt; | |
| 1423 | ✗ | reset_packet(pkt); | |
| 1424 | } | ||
| 1425 | ✗ | } | |
| 1426 | |||
| 1427 | /* | ||
| 1428 | * Find a timestamp for the requested position within the payload | ||
| 1429 | * where the pos (position) is the offset inside the Data Object. | ||
| 1430 | * When position is not on the packet boundary, asf_read_timestamp tries | ||
| 1431 | * to find the closest packet offset after this position. If this packet | ||
| 1432 | * is a key frame, this packet timestamp is read and an index entry is created | ||
| 1433 | * for the packet. If this packet belongs to the requested stream, | ||
| 1434 | * asf_read_timestamp upgrades pos to the packet beginning offset and | ||
| 1435 | * returns this packet's dts. So returned dts is the dts of the first key frame with | ||
| 1436 | * matching stream number after given position. | ||
| 1437 | */ | ||
| 1438 | ✗ | static int64_t asf_read_timestamp(AVFormatContext *s, int stream_index, | |
| 1439 | int64_t *pos, int64_t pos_limit) | ||
| 1440 | { | ||
| 1441 | ✗ | ASFContext *asf = s->priv_data; | |
| 1442 | ✗ | int64_t pkt_pos = *pos, pkt_offset, dts = AV_NOPTS_VALUE, data_end; | |
| 1443 | ✗ | AVPacket *pkt = av_packet_alloc(); | |
| 1444 | int n; | ||
| 1445 | |||
| 1446 | ✗ | if (!pkt) | |
| 1447 | ✗ | return AVERROR(ENOMEM); | |
| 1448 | |||
| 1449 | ✗ | data_end = asf->data_offset + asf->data_size; | |
| 1450 | |||
| 1451 | ✗ | n = (pkt_pos - asf->first_packet_offset + asf->packet_size - 1) / | |
| 1452 | ✗ | asf->packet_size; | |
| 1453 | ✗ | n = av_clip(n, 0, ((data_end - asf->first_packet_offset) / asf->packet_size - 1)); | |
| 1454 | ✗ | pkt_pos = asf->first_packet_offset + n * asf->packet_size; | |
| 1455 | |||
| 1456 | ✗ | avio_seek(s->pb, pkt_pos, SEEK_SET); | |
| 1457 | ✗ | pkt_offset = pkt_pos; | |
| 1458 | |||
| 1459 | ✗ | reset_packet_state(s); | |
| 1460 | ✗ | while (avio_tell(s->pb) < data_end) { | |
| 1461 | |||
| 1462 | int i, ret, st_found; | ||
| 1463 | |||
| 1464 | ✗ | pkt_offset = avio_tell(s->pb); | |
| 1465 | ✗ | if ((ret = asf_read_packet(s, pkt)) < 0) { | |
| 1466 | ✗ | av_packet_free(&pkt); | |
| 1467 | ✗ | dts = AV_NOPTS_VALUE; | |
| 1468 | ✗ | return ret; | |
| 1469 | } | ||
| 1470 | // ASFPacket may contain fragments of packets belonging to different streams, | ||
| 1471 | // pkt_offset is the offset of the first fragment within it. | ||
| 1472 | ✗ | if ((pkt_offset >= (pkt_pos + asf->packet_size))) | |
| 1473 | ✗ | pkt_pos += asf->packet_size; | |
| 1474 | ✗ | for (i = 0; i < asf->nb_streams; i++) { | |
| 1475 | ✗ | ASFStream *st = asf->asf_st[i]; | |
| 1476 | |||
| 1477 | ✗ | st_found = 0; | |
| 1478 | ✗ | if (pkt->flags & AV_PKT_FLAG_KEY) { | |
| 1479 | ✗ | dts = pkt->dts; | |
| 1480 | ✗ | if (dts) { | |
| 1481 | ✗ | av_add_index_entry(s->streams[pkt->stream_index], pkt_pos, | |
| 1482 | ✗ | dts, pkt->size, 0, AVINDEX_KEYFRAME); | |
| 1483 | ✗ | if (stream_index == st->index) { | |
| 1484 | ✗ | st_found = 1; | |
| 1485 | ✗ | break; | |
| 1486 | } | ||
| 1487 | } | ||
| 1488 | } | ||
| 1489 | } | ||
| 1490 | ✗ | if (st_found) | |
| 1491 | ✗ | break; | |
| 1492 | ✗ | av_packet_unref(pkt); | |
| 1493 | } | ||
| 1494 | ✗ | *pos = pkt_pos; | |
| 1495 | |||
| 1496 | ✗ | av_packet_free(&pkt); | |
| 1497 | ✗ | return dts; | |
| 1498 | } | ||
| 1499 | |||
| 1500 | ✗ | static int asf_read_seek(AVFormatContext *s, int stream_index, | |
| 1501 | int64_t timestamp, int flags) | ||
| 1502 | { | ||
| 1503 | ✗ | ASFContext *asf = s->priv_data; | |
| 1504 | ✗ | AVStream *const st = s->streams[stream_index]; | |
| 1505 | ✗ | FFStream *const sti = ffstream(st); | |
| 1506 | int idx, ret; | ||
| 1507 | |||
| 1508 | ✗ | if (sti->nb_index_entries && asf->is_simple_index) { | |
| 1509 | ✗ | idx = av_index_search_timestamp(st, timestamp, flags); | |
| 1510 | ✗ | if (idx < 0 || idx >= sti->nb_index_entries) | |
| 1511 | ✗ | return AVERROR_INVALIDDATA; | |
| 1512 | ✗ | avio_seek(s->pb, sti->index_entries[idx].pos, SEEK_SET); | |
| 1513 | } else { | ||
| 1514 | ✗ | if ((ret = ff_seek_frame_binary(s, stream_index, timestamp, flags)) < 0) | |
| 1515 | ✗ | return ret; | |
| 1516 | } | ||
| 1517 | |||
| 1518 | ✗ | reset_packet_state(s); | |
| 1519 | |||
| 1520 | ✗ | return 0; | |
| 1521 | } | ||
| 1522 | |||
| 1523 | 10 | static const GUIDParseTable *find_guid(ff_asf_guid guid) | |
| 1524 | { | ||
| 1525 | int j, ret; | ||
| 1526 | const GUIDParseTable *g; | ||
| 1527 | |||
| 1528 | 10 | swap_guid(guid); | |
| 1529 | 10 | g = gdef; | |
| 1530 |
1/2✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
|
87 | for (j = 0; j < FF_ARRAY_ELEMS(gdef); j++) { |
| 1531 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 77 times.
|
87 | if (!(ret = memcmp(guid, g->guid, sizeof(g->guid)))) |
| 1532 | 10 | return g; | |
| 1533 | 77 | g++; | |
| 1534 | } | ||
| 1535 | |||
| 1536 | ✗ | return NULL; | |
| 1537 | } | ||
| 1538 | |||
| 1539 | 1 | static int detect_unknown_subobject(AVFormatContext *s, int64_t offset, int64_t size) | |
| 1540 | { | ||
| 1541 | 1 | ASFContext *asf = s->priv_data; | |
| 1542 | 1 | AVIOContext *pb = s->pb; | |
| 1543 | 1 | const GUIDParseTable *g = NULL; | |
| 1544 | ff_asf_guid guid; | ||
| 1545 | int ret; | ||
| 1546 | |||
| 1547 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (offset > INT64_MAX - size) |
| 1548 | ✗ | return AVERROR_INVALIDDATA; | |
| 1549 | |||
| 1550 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
|
4 | while (avio_tell(pb) <= offset + size) { |
| 1551 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (avio_tell(pb) == asf->offset) |
| 1552 | ✗ | break; | |
| 1553 | 3 | asf->offset = avio_tell(pb); | |
| 1554 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if ((ret = ff_get_guid(pb, &guid)) < 0) |
| 1555 | ✗ | return ret; | |
| 1556 | 3 | g = find_guid(guid); | |
| 1557 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (g) { |
| 1558 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if ((ret = g->read_object(s, g)) < 0) |
| 1559 | ✗ | return ret; | |
| 1560 | } else { | ||
| 1561 | GUIDParseTable g2; | ||
| 1562 | |||
| 1563 | ✗ | g2.name = "Unknown"; | |
| 1564 | ✗ | g2.is_subobject = 1; | |
| 1565 | ✗ | asf_read_unknown(s, &g2); | |
| 1566 | } | ||
| 1567 | } | ||
| 1568 | |||
| 1569 | 1 | return 0; | |
| 1570 | } | ||
| 1571 | |||
| 1572 | 1 | static int asf_read_header(AVFormatContext *s) | |
| 1573 | { | ||
| 1574 | 1 | ASFContext *asf = s->priv_data; | |
| 1575 | 1 | AVIOContext *pb = s->pb; | |
| 1576 | 1 | const GUIDParseTable *g = NULL; | |
| 1577 | ff_asf_guid guid; | ||
| 1578 | int i, ret; | ||
| 1579 | uint64_t size; | ||
| 1580 | |||
| 1581 | 1 | asf->preroll = 0; | |
| 1582 | 1 | asf->is_simple_index = 0; | |
| 1583 | 1 | ff_get_guid(pb, &guid); | |
| 1584 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (ff_guidcmp(&guid, &ff_asf_header)) |
| 1585 | ✗ | return AVERROR_INVALIDDATA; | |
| 1586 | 1 | avio_skip(pb, 8); // skip header object size | |
| 1587 | 1 | avio_skip(pb, 6); // skip number of header objects and 2 reserved bytes | |
| 1588 | 1 | asf->data_reached = 0; | |
| 1589 | |||
| 1590 | /* 1 is here instead of pb->eof_reached because (when not streaming), Data are skipped | ||
| 1591 | * for the first time, | ||
| 1592 | * Index object is processed and got eof and then seeking back to the Data is performed. | ||
| 1593 | */ | ||
| 1594 | while (1) { | ||
| 1595 | // for the cases when object size is invalid | ||
| 1596 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
|
8 | if (avio_tell(pb) == asf->offset) |
| 1597 | ✗ | break; | |
| 1598 | 8 | asf->offset = avio_tell(pb); | |
| 1599 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 7 times.
|
8 | if ((ret = ff_get_guid(pb, &guid)) < 0) { |
| 1600 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | if (avio_feof(pb) && asf->data_reached) |
| 1601 | break; | ||
| 1602 | else | ||
| 1603 | ✗ | goto failed; | |
| 1604 | } | ||
| 1605 | 7 | g = find_guid(guid); | |
| 1606 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | if (g) { |
| 1607 | 7 | asf->unknown_offset = asf->offset; | |
| 1608 | 7 | asf->is_header = 1; | |
| 1609 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
|
7 | if ((ret = g->read_object(s, g)) < 0) |
| 1610 | ✗ | goto failed; | |
| 1611 | } else { | ||
| 1612 | ✗ | size = avio_rl64(pb); | |
| 1613 | ✗ | align_position(pb, asf->offset, size); | |
| 1614 | } | ||
| 1615 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
|
7 | if (asf->data_reached && |
| 1616 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | (!(pb->seekable & AVIO_SEEKABLE_NORMAL) || |
| 1617 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | (asf->b_flags & ASF_FLAG_BROADCAST))) |
| 1618 | break; | ||
| 1619 | } | ||
| 1620 | |||
| 1621 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!asf->data_reached) { |
| 1622 | ✗ | av_log(s, AV_LOG_ERROR, "Data Object was not found.\n"); | |
| 1623 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 1624 | ✗ | goto failed; | |
| 1625 | } | ||
| 1626 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (pb->seekable & AVIO_SEEKABLE_NORMAL) |
| 1627 | 1 | avio_seek(pb, asf->first_packet_offset, SEEK_SET); | |
| 1628 | |||
| 1629 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for (i = 0; i < asf->nb_streams; i++) { |
| 1630 | 1 | const char *rfc1766 = asf->asf_sd[asf->asf_st[i]->lang_idx].langs; | |
| 1631 | 1 | AVStream *st = s->streams[asf->asf_st[i]->index]; | |
| 1632 | 1 | set_language(s, rfc1766, &st->metadata); | |
| 1633 | } | ||
| 1634 | |||
| 1635 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 1 times.
|
129 | for (i = 0; i < ASF_MAX_STREAMS; i++) { |
| 1636 | 128 | AVStream *st = NULL; | |
| 1637 | |||
| 1638 | 128 | st = find_stream(s, i); | |
| 1639 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 127 times.
|
128 | if (st) { |
| 1640 | 1 | av_dict_copy(&st->metadata, asf->asf_sd[i].asf_met, AV_DICT_IGNORE_SUFFIX); | |
| 1641 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | if (asf->asf_sd[i].aspect_ratio.num > 0 && asf->asf_sd[i].aspect_ratio.den > 0) { |
| 1642 | ✗ | st->sample_aspect_ratio.num = asf->asf_sd[i].aspect_ratio.num; | |
| 1643 | ✗ | st->sample_aspect_ratio.den = asf->asf_sd[i].aspect_ratio.den; | |
| 1644 | } | ||
| 1645 | } | ||
| 1646 | } | ||
| 1647 | |||
| 1648 | 1 | return 0; | |
| 1649 | |||
| 1650 | ✗ | failed: | |
| 1651 | ✗ | asf_read_close(s); | |
| 1652 | ✗ | return ret; | |
| 1653 | } | ||
| 1654 | |||
| 1655 | const FFInputFormat ff_asf_o_demuxer = { | ||
| 1656 | .p.name = "asf_o", | ||
| 1657 | .p.long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"), | ||
| 1658 | .p.flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH, | ||
| 1659 | .priv_data_size = sizeof(ASFContext), | ||
| 1660 | .read_probe = asf_probe, | ||
| 1661 | .read_header = asf_read_header, | ||
| 1662 | .read_packet = asf_read_packet, | ||
| 1663 | .read_close = asf_read_close, | ||
| 1664 | .read_timestamp = asf_read_timestamp, | ||
| 1665 | .read_seek = asf_read_seek, | ||
| 1666 | }; | ||
| 1667 |