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