| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * "NUT" Container Format demuxer | ||
| 3 | * Copyright (c) 2004-2006 Michael Niedermayer | ||
| 4 | * Copyright (c) 2003 Alex Beregszaszi | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with FFmpeg; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #include "libavutil/avstring.h" | ||
| 24 | #include "libavutil/avassert.h" | ||
| 25 | #include "libavutil/bswap.h" | ||
| 26 | #include "libavutil/dict.h" | ||
| 27 | #include "libavutil/intreadwrite.h" | ||
| 28 | #include "libavutil/mathematics.h" | ||
| 29 | #include "libavutil/mem.h" | ||
| 30 | #include "libavutil/tree.h" | ||
| 31 | #include "libavcodec/bytestream.h" | ||
| 32 | #include "avio_internal.h" | ||
| 33 | #include "demux.h" | ||
| 34 | #include "isom.h" | ||
| 35 | #include "nut.h" | ||
| 36 | #include "riff.h" | ||
| 37 | |||
| 38 | #define NUT_MAX_STREAMS 256 /* arbitrary sanity check value */ | ||
| 39 | |||
| 40 | static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, | ||
| 41 | int64_t *pos_arg, int64_t pos_limit); | ||
| 42 | |||
| 43 | 304 | static int get_str(AVIOContext *bc, char *string, unsigned int maxlen) | |
| 44 | { | ||
| 45 | 304 | unsigned int len = ffio_read_varlen(bc); | |
| 46 | |||
| 47 |
2/4✓ Branch 0 taken 304 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 304 times.
✗ Branch 3 not taken.
|
304 | if (len && maxlen) |
| 48 | 304 | avio_read(bc, string, FFMIN(len, maxlen)); | |
| 49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 304 times.
|
304 | while (len > maxlen) { |
| 50 | ✗ | avio_r8(bc); | |
| 51 | ✗ | len--; | |
| 52 | ✗ | if (bc->eof_reached) | |
| 53 | ✗ | len = maxlen; | |
| 54 | } | ||
| 55 | |||
| 56 |
1/2✓ Branch 0 taken 304 times.
✗ Branch 1 not taken.
|
304 | if (maxlen) |
| 57 | 304 | string[FFMIN(len, maxlen - 1)] = 0; | |
| 58 | |||
| 59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 304 times.
|
304 | if (bc->eof_reached) |
| 60 | ✗ | return AVERROR_EOF; | |
| 61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 304 times.
|
304 | if (maxlen == len) |
| 62 | ✗ | return -1; | |
| 63 | else | ||
| 64 | 304 | return 0; | |
| 65 | } | ||
| 66 | |||
| 67 | 505 | static int64_t get_s(AVIOContext *bc) | |
| 68 | { | ||
| 69 | 505 | int64_t v = ffio_read_varlen(bc) + 1; | |
| 70 | |||
| 71 |
2/2✓ Branch 0 taken 369 times.
✓ Branch 1 taken 136 times.
|
505 | if (v & 1) |
| 72 | 369 | return -(v >> 1); | |
| 73 | else | ||
| 74 | 136 | return (v >> 1); | |
| 75 | } | ||
| 76 | |||
| 77 | 50 | static uint64_t get_fourcc(AVIOContext *bc) | |
| 78 | { | ||
| 79 | 50 | unsigned int len = ffio_read_varlen(bc); | |
| 80 | |||
| 81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | if (len == 2) |
| 82 | ✗ | return avio_rl16(bc); | |
| 83 |
1/2✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
|
50 | else if (len == 4) |
| 84 | 50 | return avio_rl32(bc); | |
| 85 | else { | ||
| 86 | ✗ | av_log(NULL, AV_LOG_ERROR, "Unsupported fourcc length %d\n", len); | |
| 87 | ✗ | return -1; | |
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | 1089 | static int get_packetheader(NUTContext *nut, AVIOContext *bc, | |
| 92 | int calculate_checksum, uint64_t startcode) | ||
| 93 | { | ||
| 94 | int64_t size; | ||
| 95 | |||
| 96 | 1089 | startcode = av_be2ne64(startcode); | |
| 97 | 1089 | startcode = ff_crc04C11DB7_update(0, (uint8_t*) &startcode, 8); | |
| 98 | |||
| 99 | 1089 | ffio_init_checksum(bc, ff_crc04C11DB7_update, startcode); | |
| 100 | 1089 | size = ffio_read_varlen(bc); | |
| 101 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1089 times.
|
1089 | if (size > 4096) |
| 102 | ✗ | avio_rb32(bc); | |
| 103 |
2/4✓ Branch 1 taken 1089 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1089 times.
|
1089 | if (ffio_get_checksum(bc) && size > 4096) |
| 104 | ✗ | return -1; | |
| 105 | |||
| 106 |
2/2✓ Branch 0 taken 1060 times.
✓ Branch 1 taken 29 times.
|
1089 | ffio_init_checksum(bc, calculate_checksum ? ff_crc04C11DB7_update : NULL, 0); |
| 107 | |||
| 108 | 1089 | return size; | |
| 109 | } | ||
| 110 | |||
| 111 | 326 | static uint64_t find_any_startcode(AVIOContext *bc, int64_t pos) | |
| 112 | { | ||
| 113 | 326 | uint64_t state = 0; | |
| 114 | |||
| 115 |
2/2✓ Branch 0 taken 296 times.
✓ Branch 1 taken 30 times.
|
326 | if (pos >= 0) |
| 116 | /* Note, this may fail if the stream is not seekable, but that should | ||
| 117 | * not matter, as in this case we simply start where we currently are */ | ||
| 118 | 296 | avio_seek(bc, pos, SEEK_SET); | |
| 119 |
1/2✓ Branch 1 taken 17692 times.
✗ Branch 2 not taken.
|
17692 | while (!avio_feof(bc)) { |
| 120 | 17692 | state = (state << 8) | avio_r8(bc); | |
| 121 |
2/2✓ Branch 0 taken 17335 times.
✓ Branch 1 taken 357 times.
|
17692 | if ((state >> 56) != 'N') |
| 122 | 17335 | continue; | |
| 123 |
2/2✓ Branch 0 taken 326 times.
✓ Branch 1 taken 31 times.
|
357 | switch (state) { |
| 124 | 326 | case MAIN_STARTCODE: | |
| 125 | case STREAM_STARTCODE: | ||
| 126 | case SYNCPOINT_STARTCODE: | ||
| 127 | case INFO_STARTCODE: | ||
| 128 | case INDEX_STARTCODE: | ||
| 129 | 326 | return state; | |
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | ✗ | return 0; | |
| 134 | } | ||
| 135 | |||
| 136 | /** | ||
| 137 | * Find the given startcode. | ||
| 138 | * @param code the startcode | ||
| 139 | * @param pos the start position of the search, or -1 if the current position | ||
| 140 | * @return the position of the startcode or -1 if not found | ||
| 141 | */ | ||
| 142 | 106 | static int64_t find_startcode(AVIOContext *bc, uint64_t code, int64_t pos) | |
| 143 | { | ||
| 144 | 30 | for (;;) { | |
| 145 | 136 | uint64_t startcode = find_any_startcode(bc, pos); | |
| 146 |
2/2✓ Branch 0 taken 106 times.
✓ Branch 1 taken 30 times.
|
136 | if (startcode == code) |
| 147 | 106 | return avio_tell(bc) - 8; | |
| 148 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | else if (startcode == 0) |
| 149 | ✗ | return -1; | |
| 150 | 30 | pos = -1; | |
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | 7279 | static int nut_probe(const AVProbeData *p) | |
| 155 | { | ||
| 156 | int i; | ||
| 157 | |||
| 158 |
2/2✓ Branch 0 taken 391725461 times.
✓ Branch 1 taken 7249 times.
|
391732710 | for (i = 0; i < p->buf_size-8; i++) { |
| 159 |
2/2✓ Branch 0 taken 391725431 times.
✓ Branch 1 taken 30 times.
|
391725461 | if (AV_RB32(p->buf+i) != MAIN_STARTCODE>>32) |
| 160 | 391725431 | continue; | |
| 161 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | if (AV_RB32(p->buf+i+4) == (MAIN_STARTCODE & 0xFFFFFFFF)) |
| 162 | 30 | return AVPROBE_SCORE_MAX; | |
| 163 | } | ||
| 164 | 7249 | return 0; | |
| 165 | } | ||
| 166 | |||
| 167 | #define GET_V(dst, check) \ | ||
| 168 | do { \ | ||
| 169 | tmp = ffio_read_varlen(bc); \ | ||
| 170 | if (!(check)) { \ | ||
| 171 | av_log(s, AV_LOG_ERROR, "Error " #dst " is (%"PRId64")\n", tmp); \ | ||
| 172 | ret = AVERROR_INVALIDDATA; \ | ||
| 173 | goto fail; \ | ||
| 174 | } \ | ||
| 175 | dst = tmp; \ | ||
| 176 | } while (0) | ||
| 177 | |||
| 178 | 1060 | static int skip_reserved(AVIOContext *bc, int64_t pos) | |
| 179 | { | ||
| 180 | 1060 | pos -= avio_tell(bc); | |
| 181 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1060 times.
|
1060 | if (pos < 0) { |
| 182 | ✗ | avio_seek(bc, pos, SEEK_CUR); | |
| 183 | ✗ | return AVERROR_INVALIDDATA; | |
| 184 | } else { | ||
| 185 |
2/2✓ Branch 0 taken 4480 times.
✓ Branch 1 taken 1060 times.
|
5540 | while (pos--) { |
| 186 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4480 times.
|
4480 | if (bc->eof_reached) |
| 187 | ✗ | return AVERROR_INVALIDDATA; | |
| 188 | 4480 | avio_r8(bc); | |
| 189 | } | ||
| 190 | 1060 | return 0; | |
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | 30 | static int decode_main_header(NUTContext *nut) | |
| 195 | { | ||
| 196 | 30 | AVFormatContext *s = nut->avf; | |
| 197 | 30 | AVIOContext *bc = s->pb; | |
| 198 | uint64_t tmp, end, length; | ||
| 199 | unsigned int stream_count; | ||
| 200 | int i, j, count, ret; | ||
| 201 | int tmp_stream, tmp_mul, tmp_pts, tmp_size, tmp_res, tmp_head_idx; | ||
| 202 | |||
| 203 | 30 | length = get_packetheader(nut, bc, 1, MAIN_STARTCODE); | |
| 204 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (length == (uint64_t)-1) |
| 205 | ✗ | return AVERROR_INVALIDDATA; | |
| 206 | 30 | end = length + avio_tell(bc); | |
| 207 | |||
| 208 | 30 | nut->version = ffio_read_varlen(bc); | |
| 209 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | if (nut->version < NUT_MIN_VERSION || |
| 210 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | nut->version > NUT_MAX_VERSION) { |
| 211 | ✗ | av_log(s, AV_LOG_ERROR, "Version %d not supported.\n", | |
| 212 | nut->version); | ||
| 213 | ✗ | return AVERROR(ENOSYS); | |
| 214 | } | ||
| 215 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 28 times.
|
30 | if (nut->version > 3) |
| 216 | 2 | nut->minor_version = ffio_read_varlen(bc); | |
| 217 | |||
| 218 |
2/4✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
|
30 | GET_V(stream_count, tmp > 0 && tmp <= NUT_MAX_STREAMS); |
| 219 | |||
| 220 | 30 | nut->max_distance = ffio_read_varlen(bc); | |
| 221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (nut->max_distance > 65536) { |
| 222 | ✗ | av_log(s, AV_LOG_DEBUG, "max_distance %d\n", nut->max_distance); | |
| 223 | ✗ | nut->max_distance = 65536; | |
| 224 | } | ||
| 225 | |||
| 226 |
3/6✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 30 times.
|
30 | GET_V(nut->time_base_count, tmp > 0 && tmp < INT_MAX / sizeof(AVRational) && tmp < length/2); |
| 227 | 30 | nut->time_base = av_malloc_array(nut->time_base_count, sizeof(AVRational)); | |
| 228 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (!nut->time_base) |
| 229 | ✗ | return AVERROR(ENOMEM); | |
| 230 | |||
| 231 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 30 times.
|
72 | for (i = 0; i < nut->time_base_count; i++) { |
| 232 |
2/4✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
|
42 | GET_V(nut->time_base[i].num, tmp > 0 && tmp < (1ULL << 31)); |
| 233 |
2/4✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
|
42 | GET_V(nut->time_base[i].den, tmp > 0 && tmp < (1ULL << 31)); |
| 234 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
|
42 | if (av_gcd(nut->time_base[i].num, nut->time_base[i].den) != 1) { |
| 235 | ✗ | av_log(s, AV_LOG_ERROR, "invalid time base %d/%d\n", | |
| 236 | ✗ | nut->time_base[i].num, | |
| 237 | ✗ | nut->time_base[i].den); | |
| 238 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 239 | ✗ | goto fail; | |
| 240 | } | ||
| 241 | } | ||
| 242 | 30 | tmp_pts = 0; | |
| 243 | 30 | tmp_mul = 1; | |
| 244 | 30 | tmp_stream = 0; | |
| 245 | 30 | tmp_head_idx = 0; | |
| 246 |
2/2✓ Branch 0 taken 321 times.
✓ Branch 1 taken 30 times.
|
351 | for (i = 0; i < 256;) { |
| 247 | 321 | int tmp_flags = ffio_read_varlen(bc); | |
| 248 | 321 | int tmp_fields = ffio_read_varlen(bc); | |
| 249 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 321 times.
|
321 | if (tmp_fields < 0) { |
| 250 | ✗ | av_log(s, AV_LOG_ERROR, "fields %d is invalid\n", tmp_fields); | |
| 251 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 252 | ✗ | goto fail; | |
| 253 | } | ||
| 254 | |||
| 255 |
2/2✓ Branch 0 taken 271 times.
✓ Branch 1 taken 50 times.
|
321 | if (tmp_fields > 0) |
| 256 | 271 | tmp_pts = get_s(bc); | |
| 257 |
2/2✓ Branch 0 taken 208 times.
✓ Branch 1 taken 113 times.
|
321 | if (tmp_fields > 1) |
| 258 | 208 | tmp_mul = ffio_read_varlen(bc); | |
| 259 |
2/2✓ Branch 0 taken 126 times.
✓ Branch 1 taken 195 times.
|
321 | if (tmp_fields > 2) |
| 260 | 126 | tmp_stream = ffio_read_varlen(bc); | |
| 261 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 213 times.
|
321 | if (tmp_fields > 3) |
| 262 | 108 | tmp_size = ffio_read_varlen(bc); | |
| 263 | else | ||
| 264 | 213 | tmp_size = 0; | |
| 265 |
2/2✓ Branch 0 taken 62 times.
✓ Branch 1 taken 259 times.
|
321 | if (tmp_fields > 4) |
| 266 | 62 | tmp_res = ffio_read_varlen(bc); | |
| 267 | else | ||
| 268 | 259 | tmp_res = 0; | |
| 269 |
2/2✓ Branch 0 taken 62 times.
✓ Branch 1 taken 259 times.
|
321 | if (tmp_fields > 5) |
| 270 | 62 | count = ffio_read_varlen(bc); | |
| 271 | else | ||
| 272 | 259 | count = tmp_mul - (unsigned)tmp_size; | |
| 273 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 319 times.
|
321 | if (tmp_fields > 6) |
| 274 | 2 | get_s(bc); | |
| 275 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 319 times.
|
321 | if (tmp_fields > 7) |
| 276 | 2 | tmp_head_idx = ffio_read_varlen(bc); | |
| 277 | |||
| 278 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 321 times.
|
321 | while (tmp_fields-- > 8) { |
| 279 | ✗ | if (bc->eof_reached) { | |
| 280 | ✗ | av_log(s, AV_LOG_ERROR, "reached EOF while decoding main header\n"); | |
| 281 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 282 | ✗ | goto fail; | |
| 283 | } | ||
| 284 | ✗ | ffio_read_varlen(bc); | |
| 285 | } | ||
| 286 | |||
| 287 |
4/6✓ Branch 0 taken 321 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 201 times.
✓ Branch 3 taken 120 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 321 times.
|
321 | if (count <= 0 || count > 256 - (i <= 'N') - i) { |
| 288 | ✗ | av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i); | |
| 289 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 290 | ✗ | goto fail; | |
| 291 | } | ||
| 292 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 321 times.
|
321 | if (tmp_stream >= stream_count) { |
| 293 | ✗ | av_log(s, AV_LOG_ERROR, "illegal stream number %d >= %d\n", | |
| 294 | tmp_stream, stream_count); | ||
| 295 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 296 | ✗ | goto fail; | |
| 297 | } | ||
| 298 |
2/4✓ Branch 0 taken 321 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 321 times.
|
321 | if (tmp_size < 0 || tmp_size > INT_MAX - count) { |
| 299 | ✗ | av_log(s, AV_LOG_ERROR, "illegal size\n"); | |
| 300 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 301 | ✗ | goto fail; | |
| 302 | } | ||
| 303 | |||
| 304 |
2/2✓ Branch 0 taken 7680 times.
✓ Branch 1 taken 321 times.
|
8001 | for (j = 0; j < count; j++, i++) { |
| 305 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 7650 times.
|
7680 | if (i == 'N') { |
| 306 | 30 | nut->frame_code[i].flags = FLAG_INVALID; | |
| 307 | 30 | j--; | |
| 308 | 30 | continue; | |
| 309 | } | ||
| 310 | 7650 | nut->frame_code[i].flags = tmp_flags; | |
| 311 | 7650 | nut->frame_code[i].pts_delta = tmp_pts; | |
| 312 | 7650 | nut->frame_code[i].stream_id = tmp_stream; | |
| 313 | 7650 | nut->frame_code[i].size_mul = tmp_mul; | |
| 314 | 7650 | nut->frame_code[i].size_lsb = tmp_size + j; | |
| 315 | 7650 | nut->frame_code[i].reserved_count = tmp_res; | |
| 316 | 7650 | nut->frame_code[i].header_idx = tmp_head_idx; | |
| 317 | } | ||
| 318 | } | ||
| 319 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | av_assert0(nut->frame_code['N'].flags == FLAG_INVALID); |
| 320 | |||
| 321 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | if (end > avio_tell(bc) + 4) { |
| 322 | 30 | int rem = 1024; | |
| 323 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
|
30 | GET_V(nut->header_count, tmp < 128U); |
| 324 | 30 | nut->header_count++; | |
| 325 |
2/2✓ Branch 0 taken 180 times.
✓ Branch 1 taken 30 times.
|
210 | for (i = 1; i < nut->header_count; i++) { |
| 326 | uint8_t *hdr; | ||
| 327 |
2/4✓ Branch 1 taken 180 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 180 times.
|
180 | GET_V(nut->header_len[i], tmp > 0 && tmp < 256); |
| 328 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 180 times.
|
180 | if (rem < nut->header_len[i]) { |
| 329 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 330 | "invalid elision header %d : %d > %d\n", | ||
| 331 | ✗ | i, nut->header_len[i], rem); | |
| 332 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 333 | ✗ | goto fail; | |
| 334 | } | ||
| 335 | 180 | rem -= nut->header_len[i]; | |
| 336 | 180 | hdr = av_malloc(nut->header_len[i]); | |
| 337 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 180 times.
|
180 | if (!hdr) { |
| 338 | ✗ | ret = AVERROR(ENOMEM); | |
| 339 | ✗ | goto fail; | |
| 340 | } | ||
| 341 | 180 | avio_read(bc, hdr, nut->header_len[i]); | |
| 342 | 180 | nut->header[i] = hdr; | |
| 343 | } | ||
| 344 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | av_assert0(nut->header_len[0] == 0); |
| 345 | } | ||
| 346 | |||
| 347 | // flags had been effectively introduced in version 4 | ||
| 348 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 28 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
|
30 | if (nut->version > 3 && end > avio_tell(bc) + 4) { |
| 349 | 2 | nut->flags = ffio_read_varlen(bc); | |
| 350 | } | ||
| 351 | |||
| 352 |
2/4✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 30 times.
|
30 | if (skip_reserved(bc, end) || ffio_get_checksum(bc)) { |
| 353 | ✗ | av_log(s, AV_LOG_ERROR, "main header checksum mismatch\n"); | |
| 354 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 355 | ✗ | goto fail; | |
| 356 | } | ||
| 357 | |||
| 358 | 30 | nut->stream = av_calloc(stream_count, sizeof(StreamContext)); | |
| 359 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (!nut->stream) { |
| 360 | ✗ | ret = AVERROR(ENOMEM); | |
| 361 | ✗ | goto fail; | |
| 362 | } | ||
| 363 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 30 times.
|
80 | for (i = 0; i < stream_count; i++) { |
| 364 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
|
50 | if (!avformat_new_stream(s, NULL)) { |
| 365 | ✗ | ret = AVERROR(ENOMEM); | |
| 366 | ✗ | goto fail; | |
| 367 | } | ||
| 368 | } | ||
| 369 | |||
| 370 | 30 | return 0; | |
| 371 | ✗ | fail: | |
| 372 | ✗ | av_freep(&nut->time_base); | |
| 373 | ✗ | for (i = 1; i < nut->header_count; i++) { | |
| 374 | ✗ | av_freep(&nut->header[i]); | |
| 375 | } | ||
| 376 | ✗ | nut->header_count = 0; | |
| 377 | ✗ | return ret; | |
| 378 | } | ||
| 379 | |||
| 380 | 50 | static int decode_stream_header(NUTContext *nut) | |
| 381 | { | ||
| 382 | 50 | AVFormatContext *s = nut->avf; | |
| 383 | 50 | AVIOContext *bc = s->pb; | |
| 384 | StreamContext *stc; | ||
| 385 | int class, stream_id, ret; | ||
| 386 | uint64_t tmp, end; | ||
| 387 | 50 | AVStream *st = NULL; | |
| 388 | |||
| 389 | 50 | end = get_packetheader(nut, bc, 1, STREAM_STARTCODE); | |
| 390 | 50 | end += avio_tell(bc); | |
| 391 | |||
| 392 |
2/4✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 50 times.
|
50 | GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base); |
| 393 | 50 | stc = &nut->stream[stream_id]; | |
| 394 | 50 | st = s->streams[stream_id]; | |
| 395 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | if (!st) |
| 396 | ✗ | return AVERROR(ENOMEM); | |
| 397 | |||
| 398 | 50 | class = ffio_read_varlen(bc); | |
| 399 | 50 | tmp = get_fourcc(bc); | |
| 400 | 50 | st->codecpar->codec_tag = tmp; | |
| 401 |
2/5✓ Branch 0 taken 27 times.
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
50 | switch (class) { |
| 402 | 27 | case 0: | |
| 403 | 27 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
| 404 | 27 | st->codecpar->codec_id = av_codec_get_id((const AVCodecTag * const []) { | |
| 405 | ff_nut_video_tags, | ||
| 406 | ff_codec_bmp_tags, | ||
| 407 | ff_codec_movvideo_tags, | ||
| 408 | 0 | ||
| 409 | }, | ||
| 410 | tmp); | ||
| 411 | 27 | break; | |
| 412 | 23 | case 1: | |
| 413 | 23 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 414 | 23 | st->codecpar->codec_id = av_codec_get_id((const AVCodecTag * const []) { | |
| 415 | ff_nut_audio_tags, | ||
| 416 | ff_codec_wav_tags, | ||
| 417 | ff_nut_audio_extra_tags, | ||
| 418 | 0 | ||
| 419 | }, | ||
| 420 | tmp); | ||
| 421 | 23 | break; | |
| 422 | ✗ | case 2: | |
| 423 | ✗ | st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; | |
| 424 | ✗ | st->codecpar->codec_id = ff_codec_get_id(ff_nut_subtitle_tags, tmp); | |
| 425 | ✗ | break; | |
| 426 | ✗ | case 3: | |
| 427 | ✗ | st->codecpar->codec_type = AVMEDIA_TYPE_DATA; | |
| 428 | ✗ | st->codecpar->codec_id = ff_codec_get_id(ff_nut_data_tags, tmp); | |
| 429 | ✗ | break; | |
| 430 | ✗ | default: | |
| 431 | ✗ | av_log(s, AV_LOG_ERROR, "unknown stream class (%d)\n", class); | |
| 432 | ✗ | return AVERROR(ENOSYS); | |
| 433 | } | ||
| 434 |
2/4✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 50 times.
|
50 | if (class < 3 && st->codecpar->codec_id == AV_CODEC_ID_NONE) |
| 435 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 436 | "Unknown codec tag '0x%04x' for stream number %d\n", | ||
| 437 | (unsigned int) tmp, stream_id); | ||
| 438 | |||
| 439 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
|
50 | GET_V(stc->time_base_id, tmp < nut->time_base_count); |
| 440 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
|
50 | GET_V(stc->msb_pts_shift, tmp < 16); |
| 441 | 50 | stc->max_pts_distance = ffio_read_varlen(bc); | |
| 442 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
|
50 | GET_V(stc->decode_delay, tmp < 1000); // sanity limit, raise this if Moore's law is true |
| 443 | 50 | st->codecpar->video_delay = stc->decode_delay; | |
| 444 | 50 | ffio_read_varlen(bc); // stream flags | |
| 445 | |||
| 446 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
|
50 | GET_V(st->codecpar->extradata_size, tmp < (1 << 30)); |
| 447 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 43 times.
|
50 | if (st->codecpar->extradata_size) { |
| 448 | 7 | ret = ff_get_extradata(s, st->codecpar, bc, | |
| 449 | 7 | st->codecpar->extradata_size); | |
| 450 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (ret < 0) |
| 451 | ✗ | return ret; | |
| 452 | } | ||
| 453 | |||
| 454 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 23 times.
|
50 | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { |
| 455 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
27 | GET_V(st->codecpar->width, tmp > 0); |
| 456 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
27 | GET_V(st->codecpar->height, tmp > 0); |
| 457 | 27 | st->sample_aspect_ratio.num = ffio_read_varlen(bc); | |
| 458 | 27 | st->sample_aspect_ratio.den = ffio_read_varlen(bc); | |
| 459 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if ((!st->sample_aspect_ratio.num) != (!st->sample_aspect_ratio.den)) { |
| 460 | ✗ | av_log(s, AV_LOG_ERROR, "invalid aspect ratio %d/%d\n", | |
| 461 | st->sample_aspect_ratio.num, st->sample_aspect_ratio.den); | ||
| 462 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 463 | ✗ | goto fail; | |
| 464 | } | ||
| 465 | 27 | ffio_read_varlen(bc); /* csp type */ | |
| 466 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { |
| 467 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
|
23 | GET_V(st->codecpar->sample_rate, tmp > 0); |
| 468 | 23 | ffio_read_varlen(bc); // samplerate_den | |
| 469 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
|
23 | GET_V(st->codecpar->ch_layout.nb_channels, tmp > 0); |
| 470 | } | ||
| 471 |
2/4✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 50 times.
|
50 | if (skip_reserved(bc, end) || ffio_get_checksum(bc)) { |
| 472 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 473 | "stream header %d checksum mismatch\n", stream_id); | ||
| 474 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 475 | ✗ | goto fail; | |
| 476 | } | ||
| 477 | 50 | stc->time_base = &nut->time_base[stc->time_base_id]; | |
| 478 | 50 | avpriv_set_pts_info(s->streams[stream_id], 63, stc->time_base->num, | |
| 479 | 50 | stc->time_base->den); | |
| 480 | 50 | return 0; | |
| 481 | ✗ | fail: | |
| 482 | ✗ | if (st && st->codecpar) { | |
| 483 | ✗ | av_freep(&st->codecpar->extradata); | |
| 484 | ✗ | st->codecpar->extradata_size = 0; | |
| 485 | } | ||
| 486 | ✗ | return ret; | |
| 487 | } | ||
| 488 | |||
| 489 | 10 | static void set_disposition_bits(AVFormatContext *avf, char *value, | |
| 490 | int stream_id) | ||
| 491 | { | ||
| 492 | 10 | int flag = 0, i; | |
| 493 | |||
| 494 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 10 times.
|
70 | for (i = 0; ff_nut_dispositions[i].flag; ++i) |
| 495 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 50 times.
|
60 | if (!strcmp(ff_nut_dispositions[i].str, value)) |
| 496 | 10 | flag = ff_nut_dispositions[i].flag; | |
| 497 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (!flag) |
| 498 | ✗ | av_log(avf, AV_LOG_INFO, "unknown disposition type '%s'\n", value); | |
| 499 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 10 times.
|
36 | for (i = 0; i < avf->nb_streams; ++i) |
| 500 |
3/4✓ Branch 0 taken 16 times.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
|
26 | if (stream_id == i || stream_id == -1) |
| 501 | 10 | avf->streams[i]->disposition |= flag; | |
| 502 | 10 | } | |
| 503 | |||
| 504 | 80 | static int decode_info_header(NUTContext *nut) | |
| 505 | { | ||
| 506 | 80 | AVFormatContext *s = nut->avf; | |
| 507 | 80 | AVIOContext *bc = s->pb; | |
| 508 | uint64_t tmp, chapter_start, chapter_len; | ||
| 509 | unsigned int stream_id_plus1, count; | ||
| 510 | 80 | int i, ret = 0; | |
| 511 | int64_t chapter_id, value, end; | ||
| 512 | char name[256], str_value[1024], type_str[256]; | ||
| 513 | const char *type; | ||
| 514 | 80 | int *event_flags = NULL; | |
| 515 | 80 | AVChapter *chapter = NULL; | |
| 516 | 80 | AVStream *st = NULL; | |
| 517 | 80 | AVDictionary **metadata = NULL; | |
| 518 | 80 | int metadata_flag = 0; | |
| 519 | |||
| 520 | 80 | end = get_packetheader(nut, bc, 1, INFO_STARTCODE); | |
| 521 | 80 | end += avio_tell(bc); | |
| 522 | |||
| 523 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
|
80 | GET_V(stream_id_plus1, tmp <= s->nb_streams); |
| 524 | 80 | chapter_id = get_s(bc); | |
| 525 | 80 | chapter_start = ffio_read_varlen(bc); | |
| 526 | 80 | chapter_len = ffio_read_varlen(bc); | |
| 527 | 80 | count = ffio_read_varlen(bc); | |
| 528 | |||
| 529 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
80 | if (chapter_id && !stream_id_plus1) { |
| 530 | ✗ | int64_t start = chapter_start / nut->time_base_count; | |
| 531 | ✗ | chapter = avpriv_new_chapter(s, chapter_id, | |
| 532 | ✗ | nut->time_base[chapter_start % | |
| 533 | ✗ | nut->time_base_count], | |
| 534 | ✗ | start, start + chapter_len, NULL); | |
| 535 | ✗ | if (!chapter) { | |
| 536 | ✗ | av_log(s, AV_LOG_ERROR, "Could not create chapter.\n"); | |
| 537 | ✗ | return AVERROR(ENOMEM); | |
| 538 | } | ||
| 539 | ✗ | metadata = &chapter->metadata; | |
| 540 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 30 times.
|
80 | } else if (stream_id_plus1) { |
| 541 | 50 | st = s->streams[stream_id_plus1 - 1]; | |
| 542 | 50 | metadata = &st->metadata; | |
| 543 | 50 | event_flags = &st->event_flags; | |
| 544 | 50 | metadata_flag = AVSTREAM_EVENT_FLAG_METADATA_UPDATED; | |
| 545 | } else { | ||
| 546 | 30 | metadata = &s->metadata; | |
| 547 | 30 | event_flags = &s->event_flags; | |
| 548 | 30 | metadata_flag = AVFMT_EVENT_FLAG_METADATA_UPDATED; | |
| 549 | } | ||
| 550 | |||
| 551 |
2/2✓ Branch 0 taken 151 times.
✓ Branch 1 taken 80 times.
|
231 | for (i = 0; i < count; i++) { |
| 552 | 151 | ret = get_str(bc, name, sizeof(name)); | |
| 553 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 151 times.
|
151 | if (ret < 0) { |
| 554 | ✗ | av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n"); | |
| 555 | ✗ | return ret; | |
| 556 | } | ||
| 557 | 151 | value = get_s(bc); | |
| 558 | 151 | str_value[0] = 0; | |
| 559 | |||
| 560 |
1/2✓ Branch 0 taken 151 times.
✗ Branch 1 not taken.
|
151 | if (value == -1) { |
| 561 | 151 | type = "UTF-8"; | |
| 562 | 151 | ret = get_str(bc, str_value, sizeof(str_value)); | |
| 563 | ✗ | } else if (value == -2) { | |
| 564 | ✗ | ret = get_str(bc, type_str, sizeof(type_str)); | |
| 565 | ✗ | if (ret < 0) { | |
| 566 | ✗ | av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n"); | |
| 567 | ✗ | return ret; | |
| 568 | } | ||
| 569 | ✗ | type = type_str; | |
| 570 | ✗ | ret = get_str(bc, str_value, sizeof(str_value)); | |
| 571 | ✗ | } else if (value == -3) { | |
| 572 | ✗ | type = "s"; | |
| 573 | ✗ | value = get_s(bc); | |
| 574 | ✗ | } else if (value == -4) { | |
| 575 | ✗ | type = "t"; | |
| 576 | ✗ | value = ffio_read_varlen(bc); | |
| 577 | ✗ | } else if (value < -4) { | |
| 578 | ✗ | type = "r"; | |
| 579 | ✗ | get_s(bc); | |
| 580 | } else { | ||
| 581 | ✗ | type = "v"; | |
| 582 | } | ||
| 583 | |||
| 584 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 151 times.
|
151 | if (ret < 0) { |
| 585 | ✗ | av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n"); | |
| 586 | ✗ | return ret; | |
| 587 | } | ||
| 588 | |||
| 589 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 151 times.
|
151 | if (stream_id_plus1 > s->nb_streams) { |
| 590 | ✗ | av_log(s, AV_LOG_WARNING, | |
| 591 | "invalid stream id %d for info packet\n", | ||
| 592 | stream_id_plus1); | ||
| 593 | ✗ | continue; | |
| 594 | } | ||
| 595 | |||
| 596 |
1/2✓ Branch 0 taken 151 times.
✗ Branch 1 not taken.
|
151 | if (!strcmp(type, "UTF-8")) { |
| 597 |
3/4✓ Branch 0 taken 151 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 141 times.
|
151 | if (chapter_id == 0 && !strcmp(name, "Disposition")) { |
| 598 | 10 | set_disposition_bits(s, str_value, stream_id_plus1 - 1); | |
| 599 | 10 | continue; | |
| 600 | } | ||
| 601 | |||
| 602 |
4/4✓ Branch 0 taken 109 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 82 times.
|
141 | if (stream_id_plus1 && !strcmp(name, "r_frame_rate")) { |
| 603 | 27 | sscanf(str_value, "%d/%d", &st->r_frame_rate.num, &st->r_frame_rate.den); | |
| 604 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1 times.
|
27 | if (st->r_frame_rate.num >= 1000LL*st->r_frame_rate.den || |
| 605 |
2/4✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
|
26 | st->r_frame_rate.num < 0 || st->r_frame_rate.den < 0) |
| 606 | 1 | st->r_frame_rate.num = st->r_frame_rate.den = 0; | |
| 607 | 27 | continue; | |
| 608 | } | ||
| 609 | |||
| 610 |
3/6✓ Branch 0 taken 114 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 114 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 114 times.
✗ Branch 6 not taken.
|
228 | if (metadata && av_strcasecmp(name, "Uses") && |
| 611 |
1/2✓ Branch 2 taken 114 times.
✗ Branch 3 not taken.
|
228 | av_strcasecmp(name, "Depends") && av_strcasecmp(name, "Replaces")) { |
| 612 |
1/2✓ Branch 0 taken 114 times.
✗ Branch 1 not taken.
|
114 | if (event_flags) |
| 613 | 114 | *event_flags |= metadata_flag; | |
| 614 | 114 | av_dict_set(metadata, name, str_value, 0); | |
| 615 | } | ||
| 616 | } | ||
| 617 | } | ||
| 618 | |||
| 619 |
2/4✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 80 times.
|
80 | if (skip_reserved(bc, end) || ffio_get_checksum(bc)) { |
| 620 | ✗ | av_log(s, AV_LOG_ERROR, "info header checksum mismatch\n"); | |
| 621 | ✗ | return AVERROR_INVALIDDATA; | |
| 622 | } | ||
| 623 | 80 | fail: | |
| 624 | 80 | return FFMIN(ret, 0); | |
| 625 | } | ||
| 626 | |||
| 627 | 870 | static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr) | |
| 628 | { | ||
| 629 | 870 | AVFormatContext *s = nut->avf; | |
| 630 | 870 | AVIOContext *bc = s->pb; | |
| 631 | int64_t end; | ||
| 632 | uint64_t tmp; | ||
| 633 | int ret; | ||
| 634 | |||
| 635 | 870 | nut->last_syncpoint_pos = avio_tell(bc) - 8; | |
| 636 | |||
| 637 | 870 | end = get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE); | |
| 638 | 870 | end += avio_tell(bc); | |
| 639 | |||
| 640 | 870 | tmp = ffio_read_varlen(bc); | |
| 641 | 870 | *back_ptr = nut->last_syncpoint_pos - 16 * ffio_read_varlen(bc); | |
| 642 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 870 times.
|
870 | if (*back_ptr < 0) |
| 643 | ✗ | return AVERROR_INVALIDDATA; | |
| 644 | |||
| 645 | 870 | ff_nut_reset_ts(nut, nut->time_base[tmp % nut->time_base_count], | |
| 646 | 870 | tmp / nut->time_base_count); | |
| 647 | |||
| 648 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 870 times.
|
870 | if (nut->flags & NUT_BROADCAST) { |
| 649 | ✗ | tmp = ffio_read_varlen(bc); | |
| 650 | ✗ | av_log(s, AV_LOG_VERBOSE, "Syncpoint wallclock %"PRId64"\n", | |
| 651 | ✗ | av_rescale_q(tmp / nut->time_base_count, | |
| 652 | ✗ | nut->time_base[tmp % nut->time_base_count], | |
| 653 | ✗ | AV_TIME_BASE_Q)); | |
| 654 | } | ||
| 655 | |||
| 656 |
2/4✓ Branch 1 taken 870 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 870 times.
|
870 | if (skip_reserved(bc, end) || ffio_get_checksum(bc)) { |
| 657 | ✗ | av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n"); | |
| 658 | ✗ | return AVERROR_INVALIDDATA; | |
| 659 | } | ||
| 660 | |||
| 661 | 1740 | *ts = tmp / nut->time_base_count * | |
| 662 | 870 | av_q2d(nut->time_base[tmp % nut->time_base_count]) * AV_TIME_BASE; | |
| 663 | |||
| 664 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 870 times.
|
870 | if ((ret = ff_nut_add_sp(nut, nut->last_syncpoint_pos, *back_ptr, *ts)) < 0) |
| 665 | ✗ | return ret; | |
| 666 | |||
| 667 | 870 | return 0; | |
| 668 | } | ||
| 669 | |||
| 670 | //FIXME calculate exactly, this is just a good approximation. | ||
| 671 | ✗ | static int64_t find_duration(NUTContext *nut, int64_t filesize) | |
| 672 | { | ||
| 673 | ✗ | AVFormatContext *s = nut->avf; | |
| 674 | ✗ | int64_t duration = 0; | |
| 675 | |||
| 676 | ✗ | ff_find_last_ts(s, -1, &duration, NULL, nut_read_timestamp); | |
| 677 | |||
| 678 | ✗ | if(duration > 0) | |
| 679 | ✗ | s->duration_estimation_method = AVFMT_DURATION_FROM_PTS; | |
| 680 | ✗ | return duration; | |
| 681 | } | ||
| 682 | |||
| 683 | 30 | static int find_and_decode_index(NUTContext *nut) | |
| 684 | { | ||
| 685 | 30 | AVFormatContext *s = nut->avf; | |
| 686 | 30 | AVIOContext *bc = s->pb; | |
| 687 | uint64_t tmp, end; | ||
| 688 | int i, j, syncpoint_count; | ||
| 689 | 30 | int64_t filesize = avio_size(bc); | |
| 690 | 30 | int64_t *syncpoints = NULL; | |
| 691 | uint64_t max_pts; | ||
| 692 | 30 | int8_t *has_keyframe = NULL; | |
| 693 | 30 | int ret = AVERROR_INVALIDDATA; | |
| 694 | |||
| 695 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if(filesize <= 0) |
| 696 | ✗ | return -1; | |
| 697 | |||
| 698 | 30 | avio_seek(bc, filesize - 12, SEEK_SET); | |
| 699 | 30 | avio_seek(bc, filesize - avio_rb64(bc), SEEK_SET); | |
| 700 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
|
30 | if (avio_rb64(bc) != INDEX_STARTCODE) { |
| 701 | ✗ | av_log(s, AV_LOG_WARNING, "no index at the end\n"); | |
| 702 | |||
| 703 | ✗ | if(s->duration<=0) | |
| 704 | ✗ | s->duration = find_duration(nut, filesize); | |
| 705 | ✗ | return ret; | |
| 706 | } | ||
| 707 | |||
| 708 | 30 | end = get_packetheader(nut, bc, 1, INDEX_STARTCODE); | |
| 709 | 30 | end += avio_tell(bc); | |
| 710 | |||
| 711 | 30 | max_pts = ffio_read_varlen(bc); | |
| 712 | 30 | s->duration = av_rescale_q(max_pts / nut->time_base_count, | |
| 713 | 30 | nut->time_base[max_pts % nut->time_base_count], | |
| 714 | 30 | AV_TIME_BASE_Q); | |
| 715 | 30 | s->duration_estimation_method = AVFMT_DURATION_FROM_PTS; | |
| 716 | |||
| 717 |
2/4✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
|
30 | GET_V(syncpoint_count, tmp < INT_MAX / 8 && tmp > 0); |
| 718 | 30 | syncpoints = av_malloc_array(syncpoint_count, sizeof(int64_t)); | |
| 719 | 30 | has_keyframe = av_malloc_array(syncpoint_count + 1, sizeof(int8_t)); | |
| 720 |
2/4✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
|
30 | if (!syncpoints || !has_keyframe) { |
| 721 | ✗ | ret = AVERROR(ENOMEM); | |
| 722 | ✗ | goto fail; | |
| 723 | } | ||
| 724 |
2/2✓ Branch 0 taken 854 times.
✓ Branch 1 taken 30 times.
|
884 | for (i = 0; i < syncpoint_count; i++) { |
| 725 | 854 | syncpoints[i] = ffio_read_varlen(bc); | |
| 726 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 854 times.
|
854 | if (syncpoints[i] <= 0) |
| 727 | ✗ | goto fail; | |
| 728 |
2/2✓ Branch 0 taken 824 times.
✓ Branch 1 taken 30 times.
|
854 | if (i) |
| 729 | 824 | syncpoints[i] += syncpoints[i - 1]; | |
| 730 | } | ||
| 731 | |||
| 732 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 30 times.
|
80 | for (i = 0; i < s->nb_streams; i++) { |
| 733 | 50 | int64_t last_pts = -1; | |
| 734 |
2/2✓ Branch 0 taken 159 times.
✓ Branch 1 taken 50 times.
|
209 | for (j = 0; j < syncpoint_count;) { |
| 735 | 159 | uint64_t x = ffio_read_varlen(bc); | |
| 736 | 159 | int type = x & 1; | |
| 737 | 159 | int n = j; | |
| 738 | 159 | x >>= 1; | |
| 739 |
1/2✓ Branch 0 taken 159 times.
✗ Branch 1 not taken.
|
159 | if (type) { |
| 740 | 159 | int flag = x & 1; | |
| 741 | 159 | x >>= 1; | |
| 742 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 159 times.
|
159 | if (n + x >= syncpoint_count + 1) { |
| 743 | ✗ | av_log(s, AV_LOG_ERROR, "index overflow A %d + %"PRIu64" >= %d\n", n, x, syncpoint_count + 1); | |
| 744 | ✗ | goto fail; | |
| 745 | } | ||
| 746 |
2/2✓ Branch 0 taken 936 times.
✓ Branch 1 taken 159 times.
|
1095 | while (x--) |
| 747 | 936 | has_keyframe[n++] = flag; | |
| 748 | 159 | has_keyframe[n++] = !flag; | |
| 749 | } else { | ||
| 750 | ✗ | if (x <= 1) { | |
| 751 | ✗ | av_log(s, AV_LOG_ERROR, "index: x %"PRIu64" is invalid\n", x); | |
| 752 | ✗ | goto fail; | |
| 753 | } | ||
| 754 | ✗ | while (x != 1) { | |
| 755 | ✗ | if (n >= syncpoint_count + 1) { | |
| 756 | ✗ | av_log(s, AV_LOG_ERROR, "index overflow B\n"); | |
| 757 | ✗ | goto fail; | |
| 758 | } | ||
| 759 | ✗ | has_keyframe[n++] = x & 1; | |
| 760 | ✗ | x >>= 1; | |
| 761 | } | ||
| 762 | } | ||
| 763 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 159 times.
|
159 | if (has_keyframe[0]) { |
| 764 | ✗ | av_log(s, AV_LOG_ERROR, "keyframe before first syncpoint in index\n"); | |
| 765 | ✗ | goto fail; | |
| 766 | } | ||
| 767 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 159 times.
|
159 | av_assert0(n <= syncpoint_count + 1); |
| 768 |
4/4✓ Branch 0 taken 1095 times.
✓ Branch 1 taken 136 times.
✓ Branch 2 taken 1072 times.
✓ Branch 3 taken 23 times.
|
1231 | for (; j < n && j < syncpoint_count; j++) { |
| 769 |
2/2✓ Branch 0 taken 812 times.
✓ Branch 1 taken 260 times.
|
1072 | if (has_keyframe[j]) { |
| 770 | 812 | uint64_t B, A = ffio_read_varlen(bc); | |
| 771 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 812 times.
|
812 | if (!A) { |
| 772 | ✗ | A = ffio_read_varlen(bc); | |
| 773 | ✗ | B = ffio_read_varlen(bc); | |
| 774 | // eor_pts[j][i] = last_pts + A + B | ||
| 775 | } else | ||
| 776 | 812 | B = 0; | |
| 777 | 812 | av_add_index_entry(s->streams[i], 16 * syncpoints[j - 1], | |
| 778 | 812 | last_pts + A, 0, 0, AVINDEX_KEYFRAME); | |
| 779 | 812 | last_pts += A + B; | |
| 780 | } | ||
| 781 | } | ||
| 782 | } | ||
| 783 | } | ||
| 784 | |||
| 785 |
2/4✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 30 times.
|
30 | if (skip_reserved(bc, end) || ffio_get_checksum(bc)) { |
| 786 | ✗ | av_log(s, AV_LOG_ERROR, "index checksum mismatch\n"); | |
| 787 | ✗ | goto fail; | |
| 788 | } | ||
| 789 | 30 | ret = 0; | |
| 790 | |||
| 791 | 30 | fail: | |
| 792 | 30 | av_free(syncpoints); | |
| 793 | 30 | av_free(has_keyframe); | |
| 794 | 30 | return ret; | |
| 795 | } | ||
| 796 | |||
| 797 | 30 | static int nut_read_close(AVFormatContext *s) | |
| 798 | { | ||
| 799 | 30 | NUTContext *nut = s->priv_data; | |
| 800 | int i; | ||
| 801 | |||
| 802 | 30 | av_freep(&nut->time_base); | |
| 803 | 30 | av_freep(&nut->stream); | |
| 804 | 30 | ff_nut_free_sp(nut); | |
| 805 |
2/2✓ Branch 0 taken 180 times.
✓ Branch 1 taken 30 times.
|
210 | for (i = 1; i < nut->header_count; i++) |
| 806 | 180 | av_freep(&nut->header[i]); | |
| 807 | |||
| 808 | 30 | return 0; | |
| 809 | } | ||
| 810 | |||
| 811 | 30 | static int nut_read_header(AVFormatContext *s) | |
| 812 | { | ||
| 813 | 30 | NUTContext *nut = s->priv_data; | |
| 814 | 30 | AVIOContext *bc = s->pb; | |
| 815 | int64_t pos; | ||
| 816 | int initialized_stream_count, ret; | ||
| 817 | |||
| 818 | 30 | nut->avf = s; | |
| 819 | |||
| 820 | /* main header */ | ||
| 821 | 30 | pos = 0; | |
| 822 | 30 | ret = 0; | |
| 823 | do { | ||
| 824 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (ret == AVERROR(ENOMEM)) |
| 825 | ✗ | return ret; | |
| 826 | |||
| 827 | 30 | pos = find_startcode(bc, MAIN_STARTCODE, pos) + 1; | |
| 828 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (pos < 0 + 1) { |
| 829 | ✗ | av_log(s, AV_LOG_ERROR, "No main startcode found.\n"); | |
| 830 | ✗ | return AVERROR_INVALIDDATA; | |
| 831 | } | ||
| 832 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
|
30 | } while ((ret = decode_main_header(nut)) < 0); |
| 833 | |||
| 834 | /* stream headers */ | ||
| 835 | 30 | pos = 0; | |
| 836 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 30 times.
|
80 | for (initialized_stream_count = 0; initialized_stream_count < s->nb_streams;) { |
| 837 | 50 | pos = find_startcode(bc, STREAM_STARTCODE, pos) + 1; | |
| 838 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | if (pos < 0 + 1) { |
| 839 | ✗ | av_log(s, AV_LOG_ERROR, "Not all stream headers found.\n"); | |
| 840 | ✗ | return AVERROR_INVALIDDATA; | |
| 841 | } | ||
| 842 |
1/2✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
|
50 | if (decode_stream_header(nut) >= 0) |
| 843 | 50 | initialized_stream_count++; | |
| 844 | } | ||
| 845 | |||
| 846 | /* info headers */ | ||
| 847 | 30 | pos = 0; | |
| 848 | 160 | for (;;) { | |
| 849 | 190 | uint64_t startcode = find_any_startcode(bc, pos); | |
| 850 | 190 | pos = avio_tell(bc); | |
| 851 | |||
| 852 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 190 times.
|
190 | if (startcode == 0) { |
| 853 | ✗ | av_log(s, AV_LOG_ERROR, "EOF before video frames\n"); | |
| 854 | ✗ | return AVERROR_INVALIDDATA; | |
| 855 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 160 times.
|
190 | } else if (startcode == SYNCPOINT_STARTCODE) { |
| 856 | 30 | nut->next_startcode = startcode; | |
| 857 | 30 | break; | |
| 858 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 80 times.
|
160 | } else if (startcode != INFO_STARTCODE) { |
| 859 | 80 | continue; | |
| 860 | } | ||
| 861 | |||
| 862 | 80 | decode_info_header(nut); | |
| 863 | } | ||
| 864 | |||
| 865 | 30 | ffformatcontext(s)->data_offset = pos - 8; | |
| 866 | |||
| 867 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | if (bc->seekable & AVIO_SEEKABLE_NORMAL) { |
| 868 | 30 | int64_t orig_pos = avio_tell(bc); | |
| 869 | 30 | find_and_decode_index(nut); | |
| 870 | 30 | avio_seek(bc, orig_pos, SEEK_SET); | |
| 871 | } | ||
| 872 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | av_assert0(nut->next_startcode == SYNCPOINT_STARTCODE); |
| 873 | |||
| 874 | 30 | ff_metadata_conv_ctx(s, NULL, ff_nut_metadata_conv); | |
| 875 | |||
| 876 | 30 | return 0; | |
| 877 | } | ||
| 878 | |||
| 879 | 2 | static int read_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta, int64_t maxpos) | |
| 880 | { | ||
| 881 | 2 | int count = ffio_read_varlen(bc); | |
| 882 | 2 | int skip_start = 0; | |
| 883 | 2 | int skip_end = 0; | |
| 884 | 2 | int sample_rate = 0; | |
| 885 | 2 | int width = 0; | |
| 886 | 2 | int height = 0; | |
| 887 | int i, ret; | ||
| 888 | |||
| 889 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | for (i=0; i<count; i++) { |
| 890 | uint8_t name[256], str_value[256], type_str[256]; | ||
| 891 | int value; | ||
| 892 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (avio_tell(bc) >= maxpos) |
| 893 | ✗ | return AVERROR_INVALIDDATA; | |
| 894 | 1 | ret = get_str(bc, name, sizeof(name)); | |
| 895 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) { |
| 896 | ✗ | av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n"); | |
| 897 | ✗ | return ret; | |
| 898 | } | ||
| 899 | 1 | value = get_s(bc); | |
| 900 | |||
| 901 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (value == -1) { |
| 902 | ✗ | ret = get_str(bc, str_value, sizeof(str_value)); | |
| 903 | ✗ | if (ret < 0) { | |
| 904 | ✗ | av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n"); | |
| 905 | ✗ | return ret; | |
| 906 | } | ||
| 907 | ✗ | av_log(s, AV_LOG_WARNING, "Unknown string %s / %s\n", name, str_value); | |
| 908 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | } else if (value == -2) { |
| 909 | 1 | uint8_t *dst = NULL; | |
| 910 | int64_t v64, value_len; | ||
| 911 | |||
| 912 | 1 | ret = get_str(bc, type_str, sizeof(type_str)); | |
| 913 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) { |
| 914 | ✗ | av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n"); | |
| 915 | ✗ | return ret; | |
| 916 | } | ||
| 917 | 1 | value_len = ffio_read_varlen(bc); | |
| 918 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | if (value_len < 0 || value_len >= maxpos - avio_tell(bc)) |
| 919 | ✗ | return AVERROR_INVALIDDATA; | |
| 920 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!strcmp(name, "Palette")) { |
| 921 | ✗ | dst = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, value_len); | |
| 922 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | } else if (!strcmp(name, "Extradata")) { |
| 923 | 1 | dst = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, value_len); | |
| 924 | ✗ | } else if (sscanf(name, "CodecSpecificSide%"SCNd64"", &v64) == 1) { | |
| 925 | ✗ | dst = av_packet_new_side_data(pkt, AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL, value_len + 8); | |
| 926 | ✗ | if(!dst) | |
| 927 | ✗ | return AVERROR(ENOMEM); | |
| 928 | ✗ | AV_WB64(dst, v64); | |
| 929 | ✗ | dst += 8; | |
| 930 | ✗ | } else if (!strcmp(name, "ChannelLayout") && value_len == 8) { | |
| 931 | // Ignored | ||
| 932 | ✗ | continue; | |
| 933 | } else { | ||
| 934 | ✗ | av_log(s, AV_LOG_WARNING, "Unknown data %s / %s\n", name, type_str); | |
| 935 | ✗ | avio_skip(bc, value_len); | |
| 936 | ✗ | continue; | |
| 937 | } | ||
| 938 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if(!dst) |
| 939 | ✗ | return AVERROR(ENOMEM); | |
| 940 | 1 | avio_read(bc, dst, value_len); | |
| 941 | ✗ | } else if (value == -3) { | |
| 942 | ✗ | value = get_s(bc); | |
| 943 | ✗ | } else if (value == -4) { | |
| 944 | ✗ | value = ffio_read_varlen(bc); | |
| 945 | ✗ | } else if (value < -4) { | |
| 946 | ✗ | get_s(bc); | |
| 947 | } else { | ||
| 948 | ✗ | if (!strcmp(name, "SkipStart")) { | |
| 949 | ✗ | skip_start = value; | |
| 950 | ✗ | } else if (!strcmp(name, "SkipEnd")) { | |
| 951 | ✗ | skip_end = value; | |
| 952 | ✗ | } else if (!strcmp(name, "Channels")) { | |
| 953 | // Ignored | ||
| 954 | ✗ | } else if (!strcmp(name, "SampleRate")) { | |
| 955 | ✗ | sample_rate = value; | |
| 956 | ✗ | } else if (!strcmp(name, "Width")) { | |
| 957 | ✗ | width = value; | |
| 958 | ✗ | } else if (!strcmp(name, "Height")) { | |
| 959 | ✗ | height = value; | |
| 960 | } else { | ||
| 961 | ✗ | av_log(s, AV_LOG_WARNING, "Unknown integer %s\n", name); | |
| 962 | } | ||
| 963 | } | ||
| 964 | } | ||
| 965 | |||
| 966 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
|
2 | if (sample_rate || width || height) { |
| 967 | ✗ | uint8_t *dst = av_packet_new_side_data(pkt, AV_PKT_DATA_PARAM_CHANGE, 16); | |
| 968 | ✗ | if (!dst) | |
| 969 | ✗ | return AVERROR(ENOMEM); | |
| 970 | ✗ | bytestream_put_le32(&dst, | |
| 971 | ✗ | AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE*(!!sample_rate) + | |
| 972 | ✗ | AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS*(!!(width|height)) | |
| 973 | ); | ||
| 974 | ✗ | if (sample_rate) | |
| 975 | ✗ | bytestream_put_le32(&dst, sample_rate); | |
| 976 | ✗ | if (width || height){ | |
| 977 | ✗ | bytestream_put_le32(&dst, width); | |
| 978 | ✗ | bytestream_put_le32(&dst, height); | |
| 979 | } | ||
| 980 | } | ||
| 981 | |||
| 982 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if (skip_start || skip_end) { |
| 983 | ✗ | uint8_t *dst = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10); | |
| 984 | ✗ | if (!dst) | |
| 985 | ✗ | return AVERROR(ENOMEM); | |
| 986 | ✗ | AV_WL32(dst, skip_start); | |
| 987 | ✗ | AV_WL32(dst+4, skip_end); | |
| 988 | } | ||
| 989 | |||
| 990 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (avio_tell(bc) >= maxpos) |
| 991 | ✗ | return AVERROR_INVALIDDATA; | |
| 992 | |||
| 993 | 2 | return 0; | |
| 994 | } | ||
| 995 | |||
| 996 | 1381 | static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id, | |
| 997 | uint8_t *header_idx, int frame_code) | ||
| 998 | { | ||
| 999 | 1381 | AVFormatContext *s = nut->avf; | |
| 1000 | 1381 | AVIOContext *bc = s->pb; | |
| 1001 | StreamContext *stc; | ||
| 1002 | int size, flags, size_mul, pts_delta, i, reserved_count, ret; | ||
| 1003 | uint64_t tmp; | ||
| 1004 | |||
| 1005 |
2/2✓ Branch 0 taken 1364 times.
✓ Branch 1 taken 17 times.
|
1381 | if (!(nut->flags & NUT_PIPE) && |
| 1006 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1364 times.
|
1364 | avio_tell(bc) > nut->last_syncpoint_pos + nut->max_distance) { |
| 1007 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 1008 | "Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n", | ||
| 1009 | avio_tell(bc), nut->last_syncpoint_pos, nut->max_distance); | ||
| 1010 | ✗ | return AVERROR_INVALIDDATA; | |
| 1011 | } | ||
| 1012 | |||
| 1013 | 1381 | flags = nut->frame_code[frame_code].flags; | |
| 1014 | 1381 | size_mul = nut->frame_code[frame_code].size_mul; | |
| 1015 | 1381 | size = nut->frame_code[frame_code].size_lsb; | |
| 1016 | 1381 | *stream_id = nut->frame_code[frame_code].stream_id; | |
| 1017 | 1381 | pts_delta = nut->frame_code[frame_code].pts_delta; | |
| 1018 | 1381 | reserved_count = nut->frame_code[frame_code].reserved_count; | |
| 1019 | 1381 | *header_idx = nut->frame_code[frame_code].header_idx; | |
| 1020 | |||
| 1021 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1381 times.
|
1381 | if (flags & FLAG_INVALID) |
| 1022 | ✗ | return AVERROR_INVALIDDATA; | |
| 1023 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 1285 times.
|
1381 | if (flags & FLAG_CODED) |
| 1024 | 96 | flags ^= ffio_read_varlen(bc); | |
| 1025 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1349 times.
|
1381 | if (flags & FLAG_STREAM_ID) { |
| 1026 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
|
32 | GET_V(*stream_id, tmp < s->nb_streams); |
| 1027 | } | ||
| 1028 | 1381 | stc = &nut->stream[*stream_id]; | |
| 1029 |
2/2✓ Branch 0 taken 1050 times.
✓ Branch 1 taken 331 times.
|
1381 | if (flags & FLAG_CODED_PTS) { |
| 1030 | 1050 | int64_t coded_pts = ffio_read_varlen(bc); | |
| 1031 | // FIXME check last_pts validity? | ||
| 1032 |
1/2✓ Branch 0 taken 1050 times.
✗ Branch 1 not taken.
|
1050 | if (coded_pts < (1LL << stc->msb_pts_shift)) { |
| 1033 | 1050 | *pts = ff_lsb2full(stc, coded_pts); | |
| 1034 | } else | ||
| 1035 | ✗ | *pts = coded_pts - (1LL << stc->msb_pts_shift); | |
| 1036 | } else | ||
| 1037 | 331 | *pts = stc->last_pts + pts_delta; | |
| 1038 |
2/2✓ Branch 0 taken 1350 times.
✓ Branch 1 taken 31 times.
|
1381 | if (flags & FLAG_SIZE_MSB) |
| 1039 | 1350 | size += size_mul * ffio_read_varlen(bc); | |
| 1040 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1381 times.
|
1381 | if (flags & FLAG_MATCH_TIME) |
| 1041 | ✗ | get_s(bc); | |
| 1042 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1381 times.
|
1381 | if (flags & FLAG_HEADER_IDX) |
| 1043 | ✗ | *header_idx = ffio_read_varlen(bc); | |
| 1044 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1381 times.
|
1381 | if (flags & FLAG_RESERVED) |
| 1045 | ✗ | reserved_count = ffio_read_varlen(bc); | |
| 1046 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1381 times.
|
1381 | for (i = 0; i < reserved_count; i++) { |
| 1047 | ✗ | if (bc->eof_reached) { | |
| 1048 | ✗ | av_log(s, AV_LOG_ERROR, "reached EOF while decoding frame header\n"); | |
| 1049 | ✗ | return AVERROR_INVALIDDATA; | |
| 1050 | } | ||
| 1051 | ✗ | ffio_read_varlen(bc); | |
| 1052 | } | ||
| 1053 | |||
| 1054 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1381 times.
|
1381 | if (*header_idx >= (unsigned)nut->header_count) { |
| 1055 | ✗ | av_log(s, AV_LOG_ERROR, "header_idx invalid\n"); | |
| 1056 | ✗ | return AVERROR_INVALIDDATA; | |
| 1057 | } | ||
| 1058 |
2/2✓ Branch 0 taken 903 times.
✓ Branch 1 taken 478 times.
|
1381 | if (size > 4096) |
| 1059 | 903 | *header_idx = 0; | |
| 1060 | 1381 | size -= nut->header_len[*header_idx]; | |
| 1061 | |||
| 1062 |
2/2✓ Branch 0 taken 83 times.
✓ Branch 1 taken 1298 times.
|
1381 | if (flags & FLAG_CHECKSUM) { |
| 1063 | 83 | avio_rb32(bc); // FIXME check this | |
| 1064 |
2/2✓ Branch 0 taken 1282 times.
✓ Branch 1 taken 16 times.
|
1298 | } else if (!(nut->flags & NUT_PIPE) && |
| 1065 |
1/2✓ Branch 0 taken 1282 times.
✗ Branch 1 not taken.
|
1282 | size > 2 * nut->max_distance || |
| 1066 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1298 times.
|
1298 | FFABS(stc->last_pts - *pts) > stc->max_pts_distance) { |
| 1067 | ✗ | av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n"); | |
| 1068 | ✗ | return AVERROR_INVALIDDATA; | |
| 1069 | } | ||
| 1070 | |||
| 1071 | 1381 | stc->last_pts = *pts; | |
| 1072 | 1381 | stc->last_flags = flags; | |
| 1073 | |||
| 1074 | 1381 | return size; | |
| 1075 | ✗ | fail: | |
| 1076 | ✗ | return ret; | |
| 1077 | } | ||
| 1078 | |||
| 1079 | 1381 | static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code) | |
| 1080 | { | ||
| 1081 | 1381 | AVFormatContext *s = nut->avf; | |
| 1082 | 1381 | AVIOContext *bc = s->pb; | |
| 1083 | int size, stream_id, discard, ret; | ||
| 1084 | int64_t pts, last_IP_pts; | ||
| 1085 | StreamContext *stc; | ||
| 1086 | uint8_t header_idx; | ||
| 1087 | |||
| 1088 | 1381 | size = decode_frame_header(nut, &pts, &stream_id, &header_idx, frame_code); | |
| 1089 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1381 times.
|
1381 | if (size < 0) |
| 1090 | ✗ | return size; | |
| 1091 | |||
| 1092 | 1381 | stc = &nut->stream[stream_id]; | |
| 1093 | |||
| 1094 |
2/2✓ Branch 0 taken 1204 times.
✓ Branch 1 taken 177 times.
|
1381 | if (stc->last_flags & FLAG_KEY) |
| 1095 | 1204 | stc->skip_until_key_frame = 0; | |
| 1096 | |||
| 1097 | 1381 | discard = s->streams[stream_id]->discard; | |
| 1098 | 1381 | last_IP_pts = ffstream(s->streams[stream_id])->last_IP_pts; | |
| 1099 |
5/6✓ Branch 0 taken 43 times.
✓ Branch 1 taken 1338 times.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 43 times.
✓ Branch 5 taken 1338 times.
|
1381 | if ((discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY)) || |
| 1100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
|
43 | (discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE && |
| 1101 |
2/4✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 1338 times.
✓ Branch 3 taken 43 times.
|
1381 | last_IP_pts > pts) || |
| 1102 | 1338 | discard >= AVDISCARD_ALL || | |
| 1103 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1331 times.
|
1338 | stc->skip_until_key_frame) { |
| 1104 | 50 | avio_skip(bc, size); | |
| 1105 | 50 | return 1; | |
| 1106 | } | ||
| 1107 | |||
| 1108 | 1331 | ret = av_new_packet(pkt, size + nut->header_len[header_idx]); | |
| 1109 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1331 times.
|
1331 | if (ret < 0) |
| 1110 | ✗ | return ret; | |
| 1111 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 1283 times.
|
1331 | if (nut->header[header_idx]) |
| 1112 | 48 | memcpy(pkt->data, nut->header[header_idx], nut->header_len[header_idx]); | |
| 1113 | 1331 | pkt->pos = avio_tell(bc); // FIXME | |
| 1114 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1330 times.
|
1331 | if (stc->last_flags & FLAG_SM_DATA) { |
| 1115 | int sm_size; | ||
| 1116 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (read_sm_data(s, bc, pkt, 0, pkt->pos + size) < 0) { |
| 1117 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 1118 | ✗ | goto fail; | |
| 1119 | } | ||
| 1120 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (read_sm_data(s, bc, pkt, 1, pkt->pos + size) < 0) { |
| 1121 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 1122 | ✗ | goto fail; | |
| 1123 | } | ||
| 1124 | 1 | sm_size = avio_tell(bc) - pkt->pos; | |
| 1125 | 1 | size -= sm_size; | |
| 1126 | } | ||
| 1127 | |||
| 1128 | 1331 | ret = avio_read(bc, pkt->data + nut->header_len[header_idx], size); | |
| 1129 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1331 times.
|
1331 | if (ret != size) { |
| 1130 | ✗ | if (ret < 0) | |
| 1131 | ✗ | goto fail; | |
| 1132 | } | ||
| 1133 | 1331 | av_shrink_packet(pkt, nut->header_len[header_idx] + ret); | |
| 1134 | |||
| 1135 | 1331 | pkt->stream_index = stream_id; | |
| 1136 |
2/2✓ Branch 0 taken 1161 times.
✓ Branch 1 taken 170 times.
|
1331 | if (stc->last_flags & FLAG_KEY) |
| 1137 | 1161 | pkt->flags |= AV_PKT_FLAG_KEY; | |
| 1138 | 1331 | pkt->pts = pts; | |
| 1139 | |||
| 1140 | 1331 | return 0; | |
| 1141 | ✗ | fail: | |
| 1142 | ✗ | av_packet_unref(pkt); | |
| 1143 | ✗ | return ret; | |
| 1144 | } | ||
| 1145 | |||
| 1146 | 1370 | static int nut_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 1147 | { | ||
| 1148 | 1370 | NUTContext *nut = s->priv_data; | |
| 1149 | 1370 | AVIOContext *bc = s->pb; | |
| 1150 | 1370 | int i, frame_code = 0, ret, skip; | |
| 1151 | int64_t ts, back_ptr; | ||
| 1152 | |||
| 1153 | 79 | for (;;) { | |
| 1154 | 1449 | int64_t pos = avio_tell(bc); | |
| 1155 | 1449 | uint64_t tmp = nut->next_startcode; | |
| 1156 | 1449 | nut->next_startcode = 0; | |
| 1157 | |||
| 1158 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1419 times.
|
1449 | if (tmp) { |
| 1159 | 30 | pos -= 8; | |
| 1160 | } else { | ||
| 1161 | 1419 | frame_code = avio_r8(bc); | |
| 1162 |
2/2✓ Branch 1 taken 39 times.
✓ Branch 2 taken 1380 times.
|
1419 | if (avio_feof(bc)) |
| 1163 | 39 | return AVERROR_EOF; | |
| 1164 |
2/2✓ Branch 0 taken 869 times.
✓ Branch 1 taken 511 times.
|
1380 | if (frame_code == 'N') { |
| 1165 | 869 | tmp = frame_code; | |
| 1166 |
2/2✓ Branch 0 taken 6083 times.
✓ Branch 1 taken 869 times.
|
6952 | for (i = 1; i < 8; i++) |
| 1167 | 6083 | tmp = (tmp << 8) + avio_r8(bc); | |
| 1168 | } | ||
| 1169 | } | ||
| 1170 |
3/5✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 870 times.
✓ Branch 3 taken 511 times.
✗ Branch 4 not taken.
|
1410 | switch (tmp) { |
| 1171 | 29 | case MAIN_STARTCODE: | |
| 1172 | case STREAM_STARTCODE: | ||
| 1173 | case INDEX_STARTCODE: | ||
| 1174 | 29 | skip = get_packetheader(nut, bc, 0, tmp); | |
| 1175 | 29 | avio_skip(bc, skip); | |
| 1176 | 29 | break; | |
| 1177 | ✗ | case INFO_STARTCODE: | |
| 1178 | ✗ | if (decode_info_header(nut) < 0) | |
| 1179 | ✗ | goto resync; | |
| 1180 | ✗ | break; | |
| 1181 | 870 | case SYNCPOINT_STARTCODE: | |
| 1182 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 870 times.
|
870 | if (decode_syncpoint(nut, &ts, &back_ptr) < 0) |
| 1183 | ✗ | goto resync; | |
| 1184 | 870 | frame_code = avio_r8(bc); | |
| 1185 | 1381 | case 0: | |
| 1186 | 1381 | ret = decode_frame(nut, pkt, frame_code); | |
| 1187 |
2/2✓ Branch 0 taken 1331 times.
✓ Branch 1 taken 50 times.
|
1381 | if (ret == 0) |
| 1188 | 1331 | return 0; | |
| 1189 |
1/2✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
|
50 | else if (ret == 1) // OK but discard packet |
| 1190 | 50 | break; | |
| 1191 | default: | ||
| 1192 | ✗ | resync: | |
| 1193 | ✗ | av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos); | |
| 1194 | ✗ | tmp = find_any_startcode(bc, FFMAX(nut->last_syncpoint_pos, nut->last_resync_pos) + 1); | |
| 1195 | ✗ | nut->last_resync_pos = avio_tell(bc); | |
| 1196 | ✗ | if (tmp == 0) | |
| 1197 | ✗ | return AVERROR_INVALIDDATA; | |
| 1198 | ✗ | av_log(s, AV_LOG_DEBUG, "sync\n"); | |
| 1199 | ✗ | nut->next_startcode = tmp; | |
| 1200 | } | ||
| 1201 | } | ||
| 1202 | } | ||
| 1203 | |||
| 1204 | ✗ | static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, | |
| 1205 | int64_t *pos_arg, int64_t pos_limit) | ||
| 1206 | { | ||
| 1207 | ✗ | NUTContext *nut = s->priv_data; | |
| 1208 | ✗ | AVIOContext *bc = s->pb; | |
| 1209 | int64_t pos, pts, back_ptr; | ||
| 1210 | ✗ | av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n", | |
| 1211 | stream_index, *pos_arg, pos_limit); | ||
| 1212 | |||
| 1213 | ✗ | pos = *pos_arg; | |
| 1214 | do { | ||
| 1215 | ✗ | pos = find_startcode(bc, SYNCPOINT_STARTCODE, pos) + 1; | |
| 1216 | ✗ | if (pos < 1) { | |
| 1217 | ✗ | av_log(s, AV_LOG_ERROR, "read_timestamp failed.\n"); | |
| 1218 | ✗ | return AV_NOPTS_VALUE; | |
| 1219 | } | ||
| 1220 | ✗ | } while (decode_syncpoint(nut, &pts, &back_ptr) < 0); | |
| 1221 | ✗ | *pos_arg = pos - 1; | |
| 1222 | ✗ | av_assert0(nut->last_syncpoint_pos == *pos_arg); | |
| 1223 | |||
| 1224 | ✗ | av_log(s, AV_LOG_DEBUG, "return %"PRId64" %"PRId64"\n", pts, back_ptr); | |
| 1225 | ✗ | if (stream_index == -2) | |
| 1226 | ✗ | return back_ptr; | |
| 1227 | ✗ | av_assert0(stream_index == -1); | |
| 1228 | ✗ | return pts; | |
| 1229 | } | ||
| 1230 | |||
| 1231 | 26 | static int read_seek(AVFormatContext *s, int stream_index, | |
| 1232 | int64_t pts, int flags) | ||
| 1233 | { | ||
| 1234 | 26 | NUTContext *nut = s->priv_data; | |
| 1235 | 26 | AVStream *st = s->streams[stream_index]; | |
| 1236 | 26 | FFStream *const sti = ffstream(st); | |
| 1237 | 26 | Syncpoint dummy = { .ts = pts * av_q2d(st->time_base) * AV_TIME_BASE }; | |
| 1238 | 26 | Syncpoint nopts_sp = { .ts = AV_NOPTS_VALUE, .back_ptr = AV_NOPTS_VALUE }; | |
| 1239 | 26 | Syncpoint *sp, *next_node[2] = { &nopts_sp, &nopts_sp }; | |
| 1240 | int64_t pos, pos2, ts; | ||
| 1241 | int i; | ||
| 1242 | |||
| 1243 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (nut->flags & NUT_PIPE) { |
| 1244 | ✗ | return AVERROR(ENOSYS); | |
| 1245 | } | ||
| 1246 | |||
| 1247 |
1/2✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
|
26 | if (sti->index_entries) { |
| 1248 | 26 | int index = av_index_search_timestamp(st, pts, flags); | |
| 1249 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 15 times.
|
26 | if (index < 0) |
| 1250 | 11 | index = av_index_search_timestamp(st, pts, flags ^ AVSEEK_FLAG_BACKWARD); | |
| 1251 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (index < 0) |
| 1252 | ✗ | return -1; | |
| 1253 | |||
| 1254 | 26 | pos2 = sti->index_entries[index].pos; | |
| 1255 | 26 | ts = sti->index_entries[index].timestamp; | |
| 1256 | } else { | ||
| 1257 | ✗ | av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pts_cmp, | |
| 1258 | (void **) next_node); | ||
| 1259 | ✗ | av_log(s, AV_LOG_DEBUG, "%"PRIu64"-%"PRIu64" %"PRId64"-%"PRId64"\n", | |
| 1260 | ✗ | next_node[0]->pos, next_node[1]->pos, next_node[0]->ts, | |
| 1261 | ✗ | next_node[1]->ts); | |
| 1262 | ✗ | pos = ff_gen_search(s, -1, dummy.ts, next_node[0]->pos, | |
| 1263 | ✗ | next_node[1]->pos, next_node[1]->pos, | |
| 1264 | ✗ | next_node[0]->ts, next_node[1]->ts, | |
| 1265 | AVSEEK_FLAG_BACKWARD, &ts, nut_read_timestamp); | ||
| 1266 | ✗ | if (pos < 0) | |
| 1267 | ✗ | return pos; | |
| 1268 | |||
| 1269 | ✗ | if (!(flags & AVSEEK_FLAG_BACKWARD)) { | |
| 1270 | ✗ | dummy.pos = pos + 16; | |
| 1271 | ✗ | next_node[1] = &nopts_sp; | |
| 1272 | ✗ | av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, | |
| 1273 | (void **) next_node); | ||
| 1274 | ✗ | pos2 = ff_gen_search(s, -2, dummy.pos, next_node[0]->pos, | |
| 1275 | ✗ | next_node[1]->pos, next_node[1]->pos, | |
| 1276 | ✗ | next_node[0]->back_ptr, next_node[1]->back_ptr, | |
| 1277 | flags, &ts, nut_read_timestamp); | ||
| 1278 | ✗ | if (pos2 >= 0) | |
| 1279 | ✗ | pos = pos2; | |
| 1280 | // FIXME dir but I think it does not matter | ||
| 1281 | } | ||
| 1282 | ✗ | dummy.pos = pos; | |
| 1283 | ✗ | sp = av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, | |
| 1284 | NULL); | ||
| 1285 | |||
| 1286 | ✗ | av_assert0(sp); | |
| 1287 | ✗ | pos2 = sp->back_ptr - 15; | |
| 1288 | } | ||
| 1289 | 26 | av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos2); | |
| 1290 | 26 | pos = find_startcode(s->pb, SYNCPOINT_STARTCODE, pos2); | |
| 1291 | 26 | avio_seek(s->pb, pos, SEEK_SET); | |
| 1292 | 26 | nut->last_syncpoint_pos = pos; | |
| 1293 | 26 | av_log(s, AV_LOG_DEBUG, "SP: %"PRId64"\n", pos); | |
| 1294 |
2/4✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
|
26 | if (pos2 > pos || pos2 + 15 < pos) |
| 1295 | ✗ | av_log(s, AV_LOG_ERROR, "no syncpoint at backptr pos\n"); | |
| 1296 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 26 times.
|
78 | for (i = 0; i < s->nb_streams; i++) |
| 1297 | 52 | nut->stream[i].skip_until_key_frame = 1; | |
| 1298 | |||
| 1299 | 26 | nut->last_resync_pos = 0; | |
| 1300 | |||
| 1301 | 26 | return 0; | |
| 1302 | } | ||
| 1303 | |||
| 1304 | const FFInputFormat ff_nut_demuxer = { | ||
| 1305 | .p.name = "nut", | ||
| 1306 | .p.long_name = NULL_IF_CONFIG_SMALL("NUT"), | ||
| 1307 | .p.flags = AVFMT_SEEK_TO_PTS, | ||
| 1308 | .p.extensions = "nut", | ||
| 1309 | .p.codec_tag = ff_nut_codec_tags, | ||
| 1310 | .priv_data_size = sizeof(NUTContext), | ||
| 1311 | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, | ||
| 1312 | .read_probe = nut_probe, | ||
| 1313 | .read_header = nut_read_header, | ||
| 1314 | .read_packet = nut_read_packet, | ||
| 1315 | .read_close = nut_read_close, | ||
| 1316 | .read_seek = read_seek, | ||
| 1317 | }; | ||
| 1318 |