| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * H.264/HEVC common parsing code | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include <string.h> | ||
| 22 | |||
| 23 | #include "config.h" | ||
| 24 | |||
| 25 | #include "libavutil/error.h" | ||
| 26 | #include "libavutil/intmath.h" | ||
| 27 | #include "libavutil/intreadwrite.h" | ||
| 28 | #include "libavutil/mem.h" | ||
| 29 | |||
| 30 | #include "bytestream.h" | ||
| 31 | #include "h264.h" | ||
| 32 | #include "h2645_parse.h" | ||
| 33 | #include "vvc.h" | ||
| 34 | |||
| 35 | #include "hevc/hevc.h" | ||
| 36 | |||
| 37 | 233834 | int ff_h2645_extract_rbsp(const uint8_t *src, int length, | |
| 38 | H2645RBSP *rbsp, H2645NAL *nal, int small_padding) | ||
| 39 | { | ||
| 40 | int i, si, di; | ||
| 41 | uint8_t *dst; | ||
| 42 | |||
| 43 | 233834 | nal->skipped_bytes = 0; | |
| 44 | #define STARTCODE_TEST \ | ||
| 45 | if (i + 2 < length && src[i + 1] == 0 && \ | ||
| 46 | (src[i + 2] == 3 || src[i + 2] == 1)) { \ | ||
| 47 | if (src[i + 2] == 1) { \ | ||
| 48 | /* startcode, so we must be past the end */ \ | ||
| 49 | length = i; \ | ||
| 50 | } \ | ||
| 51 | break; \ | ||
| 52 | } | ||
| 53 | #if HAVE_FAST_UNALIGNED | ||
| 54 | #define FIND_FIRST_ZERO \ | ||
| 55 | if (i > 0 && !src[i]) \ | ||
| 56 | i--; \ | ||
| 57 | while (src[i]) \ | ||
| 58 | i++ | ||
| 59 | #if HAVE_FAST_64BIT | ||
| 60 |
2/2✓ Branch 0 taken 52476393 times.
✓ Branch 1 taken 104522 times.
|
52580915 | for (i = 0; i + 1 < length; i += 9) { |
| 61 | 103386022 | if (!((~AV_RN64(src + i) & | |
| 62 |
2/2✓ Branch 0 taken 50909629 times.
✓ Branch 1 taken 1566764 times.
|
52476393 | (AV_RN64(src + i) - 0x0100010001000101ULL)) & |
| 63 | 0x8000800080008080ULL)) | ||
| 64 | 50909629 | continue; | |
| 65 |
6/6✓ Branch 0 taken 1479191 times.
✓ Branch 1 taken 87573 times.
✓ Branch 2 taken 338822 times.
✓ Branch 3 taken 1140369 times.
✓ Branch 4 taken 4851726 times.
✓ Branch 5 taken 1566764 times.
|
6418490 | FIND_FIRST_ZERO; |
| 66 |
10/10✓ Branch 0 taken 1517967 times.
✓ Branch 1 taken 48797 times.
✓ Branch 2 taken 216802 times.
✓ Branch 3 taken 1301165 times.
✓ Branch 4 taken 207820 times.
✓ Branch 5 taken 8982 times.
✓ Branch 6 taken 120330 times.
✓ Branch 7 taken 87490 times.
✓ Branch 8 taken 120330 times.
✓ Branch 9 taken 8982 times.
|
1566764 | STARTCODE_TEST; |
| 67 | 1437452 | i -= 7; | |
| 68 | } | ||
| 69 | #else | ||
| 70 | for (i = 0; i + 1 < length; i += 5) { | ||
| 71 | if (!((~AV_RN32(src + i) & | ||
| 72 | (AV_RN32(src + i) - 0x01000101U)) & | ||
| 73 | 0x80008080U)) | ||
| 74 | continue; | ||
| 75 | FIND_FIRST_ZERO; | ||
| 76 | STARTCODE_TEST; | ||
| 77 | i -= 3; | ||
| 78 | } | ||
| 79 | #endif /* HAVE_FAST_64BIT */ | ||
| 80 | #else | ||
| 81 | for (i = 0; i + 1 < length; i += 2) { | ||
| 82 | if (src[i]) | ||
| 83 | continue; | ||
| 84 | if (i > 0 && src[i - 1] == 0) | ||
| 85 | i--; | ||
| 86 | STARTCODE_TEST; | ||
| 87 | } | ||
| 88 | #endif /* HAVE_FAST_UNALIGNED */ | ||
| 89 | |||
| 90 |
4/4✓ Branch 0 taken 224852 times.
✓ Branch 1 taken 8982 times.
✓ Branch 2 taken 174727 times.
✓ Branch 3 taken 50125 times.
|
233834 | if (i >= length - 1 && small_padding) { // no escaped 0 |
| 91 | 174727 | nal->data = | |
| 92 | 174727 | nal->raw_data = src; | |
| 93 | 174727 | nal->size = | |
| 94 | 174727 | nal->raw_size = length; | |
| 95 | 174727 | return length; | |
| 96 |
2/2✓ Branch 0 taken 24079 times.
✓ Branch 1 taken 35028 times.
|
59107 | } else if (i > length) |
| 97 | 24079 | i = length; | |
| 98 | |||
| 99 | 59107 | dst = &rbsp->rbsp_buffer[rbsp->rbsp_buffer_size]; | |
| 100 | |||
| 101 | 59107 | memcpy(dst, src, i); | |
| 102 | 59107 | si = di = i; | |
| 103 |
2/2✓ Branch 0 taken 10600663 times.
✓ Branch 1 taken 52845 times.
|
10653508 | while (si + 2 < length) { |
| 104 | // remove escapes (very rare 1:2^22) | ||
| 105 |
2/2✓ Branch 0 taken 10295551 times.
✓ Branch 1 taken 305112 times.
|
10600663 | if (src[si + 2] > 3) { |
| 106 | 10295551 | dst[di++] = src[si++]; | |
| 107 | 10295551 | dst[di++] = src[si++]; | |
| 108 |
6/6✓ Branch 0 taken 55969 times.
✓ Branch 1 taken 249143 times.
✓ Branch 2 taken 44805 times.
✓ Branch 3 taken 11164 times.
✓ Branch 4 taken 38608 times.
✓ Branch 5 taken 6197 times.
|
305112 | } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) { |
| 109 |
2/2✓ Branch 0 taken 32346 times.
✓ Branch 1 taken 6262 times.
|
38608 | if (src[si + 2] == 3) { // escape |
| 110 | 32346 | dst[di++] = 0; | |
| 111 | 32346 | dst[di++] = 0; | |
| 112 | 32346 | si += 3; | |
| 113 | |||
| 114 |
2/2✓ Branch 0 taken 25511 times.
✓ Branch 1 taken 6835 times.
|
32346 | if (nal->skipped_bytes_pos) { |
| 115 | 25511 | nal->skipped_bytes++; | |
| 116 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 25489 times.
|
25511 | if (nal->skipped_bytes_pos_size < nal->skipped_bytes) { |
| 117 | 22 | nal->skipped_bytes_pos_size *= 2; | |
| 118 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes); |
| 119 | 22 | av_reallocp_array(&nal->skipped_bytes_pos, | |
| 120 | 22 | nal->skipped_bytes_pos_size, | |
| 121 | sizeof(*nal->skipped_bytes_pos)); | ||
| 122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (!nal->skipped_bytes_pos) { |
| 123 | ✗ | nal->skipped_bytes_pos_size = 0; | |
| 124 | ✗ | return AVERROR(ENOMEM); | |
| 125 | } | ||
| 126 | } | ||
| 127 |
1/2✓ Branch 0 taken 25511 times.
✗ Branch 1 not taken.
|
25511 | if (nal->skipped_bytes_pos) |
| 128 | 25511 | nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1; | |
| 129 | } | ||
| 130 | 32346 | continue; | |
| 131 | } else // next start code | ||
| 132 | 6262 | goto nsc; | |
| 133 | } | ||
| 134 | |||
| 135 | 10562055 | dst[di++] = src[si++]; | |
| 136 | } | ||
| 137 |
2/2✓ Branch 0 taken 5823 times.
✓ Branch 1 taken 52845 times.
|
58668 | while (si < length) |
| 138 | 5823 | dst[di++] = src[si++]; | |
| 139 | |||
| 140 | 52845 | nsc: | |
| 141 | 59107 | memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
| 142 | |||
| 143 | 59107 | nal->data = dst; | |
| 144 | 59107 | nal->size = di; | |
| 145 | 59107 | nal->raw_data = src; | |
| 146 | 59107 | nal->raw_size = si; | |
| 147 | 59107 | rbsp->rbsp_buffer_size += si; | |
| 148 | |||
| 149 | 59107 | return si; | |
| 150 | } | ||
| 151 | |||
| 152 | static const char *const vvc_nal_type_name[32] = { | ||
| 153 | "TRAIL_NUT", // VVC_TRAIL_NUT | ||
| 154 | "STSA_NUT", // VVC_STSA_NUT | ||
| 155 | "RADL_NUT", // VVC_RADL_NUT | ||
| 156 | "RASL_NUT", // VVC_RASL_NUT | ||
| 157 | "RSV_VCL4", // VVC_RSV_VCL_4 | ||
| 158 | "RSV_VCL5", // VVC_RSV_VCL_5 | ||
| 159 | "RSV_VCL6", // VVC_RSV_VCL_6 | ||
| 160 | "IDR_W_RADL", // VVC_IDR_W_RADL | ||
| 161 | "IDR_N_LP", // VVC_IDR_N_LP | ||
| 162 | "CRA_NUT", // VVC_CRA_NUT | ||
| 163 | "GDR_NUT", // VVC_GDR_NUT | ||
| 164 | "RSV_IRAP_11", // VVC_RSV_IRAP_11 | ||
| 165 | "OPI_NUT", // VVC_OPI_NUT | ||
| 166 | "DCI_NUT", // VVC_DCI_NUT | ||
| 167 | "VPS_NUT", // VVC_VPS_NUT | ||
| 168 | "SPS_NUT", // VVC_SPS_NUT | ||
| 169 | "PPS_NUT", // VVC_PPS_NUT | ||
| 170 | "APS_PREFIX", // VVC_PREFIX_APS_NUT | ||
| 171 | "APS_SUFFIX", // VVC_SUFFIX_APS_NUT | ||
| 172 | "PH_NUT", // VVC_PH_NUT | ||
| 173 | "AUD_NUT", // VVC_AUD_NUT | ||
| 174 | "EOS_NUT", // VVC_EOS_NUT | ||
| 175 | "EOB_NUT", // VVC_EOB_NUT | ||
| 176 | "SEI_PREFIX", // VVC_PREFIX_SEI_NUT | ||
| 177 | "SEI_SUFFIX", // VVC_SUFFIX_SEI_NUT | ||
| 178 | "FD_NUT", // VVC_FD_NUT | ||
| 179 | "RSV_NVCL26", // VVC_RSV_NVCL_26 | ||
| 180 | "RSV_NVCL27", // VVC_RSV_NVCL_27 | ||
| 181 | "UNSPEC28", // VVC_UNSPEC_28 | ||
| 182 | "UNSPEC29", // VVC_UNSPEC_29 | ||
| 183 | "UNSPEC30", // VVC_UNSPEC_30 | ||
| 184 | "UNSPEC31", // VVC_UNSPEC_31 | ||
| 185 | }; | ||
| 186 | |||
| 187 | 19789 | static const char *vvc_nal_unit_name(int nal_type) | |
| 188 | { | ||
| 189 |
2/4✓ Branch 0 taken 19789 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19789 times.
|
19789 | av_assert0(nal_type >= 0 && nal_type < 32); |
| 190 | 19789 | return vvc_nal_type_name[nal_type]; | |
| 191 | } | ||
| 192 | |||
| 193 | static const char *const hevc_nal_type_name[64] = { | ||
| 194 | "TRAIL_N", // HEVC_NAL_TRAIL_N | ||
| 195 | "TRAIL_R", // HEVC_NAL_TRAIL_R | ||
| 196 | "TSA_N", // HEVC_NAL_TSA_N | ||
| 197 | "TSA_R", // HEVC_NAL_TSA_R | ||
| 198 | "STSA_N", // HEVC_NAL_STSA_N | ||
| 199 | "STSA_R", // HEVC_NAL_STSA_R | ||
| 200 | "RADL_N", // HEVC_NAL_RADL_N | ||
| 201 | "RADL_R", // HEVC_NAL_RADL_R | ||
| 202 | "RASL_N", // HEVC_NAL_RASL_N | ||
| 203 | "RASL_R", // HEVC_NAL_RASL_R | ||
| 204 | "RSV_VCL_N10", // HEVC_NAL_VCL_N10 | ||
| 205 | "RSV_VCL_R11", // HEVC_NAL_VCL_R11 | ||
| 206 | "RSV_VCL_N12", // HEVC_NAL_VCL_N12 | ||
| 207 | "RSV_VLC_R13", // HEVC_NAL_VCL_R13 | ||
| 208 | "RSV_VCL_N14", // HEVC_NAL_VCL_N14 | ||
| 209 | "RSV_VCL_R15", // HEVC_NAL_VCL_R15 | ||
| 210 | "BLA_W_LP", // HEVC_NAL_BLA_W_LP | ||
| 211 | "BLA_W_RADL", // HEVC_NAL_BLA_W_RADL | ||
| 212 | "BLA_N_LP", // HEVC_NAL_BLA_N_LP | ||
| 213 | "IDR_W_RADL", // HEVC_NAL_IDR_W_RADL | ||
| 214 | "IDR_N_LP", // HEVC_NAL_IDR_N_LP | ||
| 215 | "CRA_NUT", // HEVC_NAL_CRA_NUT | ||
| 216 | "RSV_IRAP_VCL22", // HEVC_NAL_RSV_IRAP_VCL22 | ||
| 217 | "RSV_IRAP_VCL23", // HEVC_NAL_RSV_IRAP_VCL23 | ||
| 218 | "RSV_VCL24", // HEVC_NAL_RSV_VCL24 | ||
| 219 | "RSV_VCL25", // HEVC_NAL_RSV_VCL25 | ||
| 220 | "RSV_VCL26", // HEVC_NAL_RSV_VCL26 | ||
| 221 | "RSV_VCL27", // HEVC_NAL_RSV_VCL27 | ||
| 222 | "RSV_VCL28", // HEVC_NAL_RSV_VCL28 | ||
| 223 | "RSV_VCL29", // HEVC_NAL_RSV_VCL29 | ||
| 224 | "RSV_VCL30", // HEVC_NAL_RSV_VCL30 | ||
| 225 | "RSV_VCL31", // HEVC_NAL_RSV_VCL31 | ||
| 226 | "VPS", // HEVC_NAL_VPS | ||
| 227 | "SPS", // HEVC_NAL_SPS | ||
| 228 | "PPS", // HEVC_NAL_PPS | ||
| 229 | "AUD", // HEVC_NAL_AUD | ||
| 230 | "EOS_NUT", // HEVC_NAL_EOS_NUT | ||
| 231 | "EOB_NUT", // HEVC_NAL_EOB_NUT | ||
| 232 | "FD_NUT", // HEVC_NAL_FD_NUT | ||
| 233 | "SEI_PREFIX", // HEVC_NAL_SEI_PREFIX | ||
| 234 | "SEI_SUFFIX", // HEVC_NAL_SEI_SUFFIX | ||
| 235 | "RSV_NVCL41", // HEVC_NAL_RSV_NVCL41 | ||
| 236 | "RSV_NVCL42", // HEVC_NAL_RSV_NVCL42 | ||
| 237 | "RSV_NVCL43", // HEVC_NAL_RSV_NVCL43 | ||
| 238 | "RSV_NVCL44", // HEVC_NAL_RSV_NVCL44 | ||
| 239 | "RSV_NVCL45", // HEVC_NAL_RSV_NVCL45 | ||
| 240 | "RSV_NVCL46", // HEVC_NAL_RSV_NVCL46 | ||
| 241 | "RSV_NVCL47", // HEVC_NAL_RSV_NVCL47 | ||
| 242 | "UNSPEC48", // HEVC_NAL_UNSPEC48 | ||
| 243 | "UNSPEC49", // HEVC_NAL_UNSPEC49 | ||
| 244 | "UNSPEC50", // HEVC_NAL_UNSPEC50 | ||
| 245 | "UNSPEC51", // HEVC_NAL_UNSPEC51 | ||
| 246 | "UNSPEC52", // HEVC_NAL_UNSPEC52 | ||
| 247 | "UNSPEC53", // HEVC_NAL_UNSPEC53 | ||
| 248 | "UNSPEC54", // HEVC_NAL_UNSPEC54 | ||
| 249 | "UNSPEC55", // HEVC_NAL_UNSPEC55 | ||
| 250 | "UNSPEC56", // HEVC_NAL_UNSPEC56 | ||
| 251 | "UNSPEC57", // HEVC_NAL_UNSPEC57 | ||
| 252 | "UNSPEC58", // HEVC_NAL_UNSPEC58 | ||
| 253 | "UNSPEC59", // HEVC_NAL_UNSPEC59 | ||
| 254 | "UNSPEC60", // HEVC_NAL_UNSPEC60 | ||
| 255 | "UNSPEC61", // HEVC_NAL_UNSPEC61 | ||
| 256 | "UNSPEC62", // HEVC_NAL_UNSPEC62 | ||
| 257 | "UNSPEC63", // HEVC_NAL_UNSPEC63 | ||
| 258 | }; | ||
| 259 | |||
| 260 | 100388 | static const char *hevc_nal_unit_name(int nal_type) | |
| 261 | { | ||
| 262 |
2/4✓ Branch 0 taken 100388 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 100388 times.
|
100388 | av_assert0(nal_type >= 0 && nal_type < 64); |
| 263 | 100388 | return hevc_nal_type_name[nal_type]; | |
| 264 | } | ||
| 265 | |||
| 266 | static const char *const h264_nal_type_name[32] = { | ||
| 267 | "Unspecified 0", //H264_NAL_UNSPECIFIED | ||
| 268 | "Coded slice of a non-IDR picture", // H264_NAL_SLICE | ||
| 269 | "Coded slice data partition A", // H264_NAL_DPA | ||
| 270 | "Coded slice data partition B", // H264_NAL_DPB | ||
| 271 | "Coded slice data partition C", // H264_NAL_DPC | ||
| 272 | "IDR", // H264_NAL_IDR_SLICE | ||
| 273 | "SEI", // H264_NAL_SEI | ||
| 274 | "SPS", // H264_NAL_SPS | ||
| 275 | "PPS", // H264_NAL_PPS | ||
| 276 | "AUD", // H264_NAL_AUD | ||
| 277 | "End of sequence", // H264_NAL_END_SEQUENCE | ||
| 278 | "End of stream", // H264_NAL_END_STREAM | ||
| 279 | "Filler data", // H264_NAL_FILLER_DATA | ||
| 280 | "SPS extension", // H264_NAL_SPS_EXT | ||
| 281 | "Prefix", // H264_NAL_PREFIX | ||
| 282 | "Subset SPS", // H264_NAL_SUB_SPS | ||
| 283 | "Depth parameter set", // H264_NAL_DPS | ||
| 284 | "Reserved 17", // H264_NAL_RESERVED17 | ||
| 285 | "Reserved 18", // H264_NAL_RESERVED18 | ||
| 286 | "Auxiliary coded picture without partitioning", // H264_NAL_AUXILIARY_SLICE | ||
| 287 | "Slice extension", // H264_NAL_EXTEN_SLICE | ||
| 288 | "Slice extension for a depth view or a 3D-AVC texture view", // H264_NAL_DEPTH_EXTEN_SLICE | ||
| 289 | "Reserved 22", // H264_NAL_RESERVED22 | ||
| 290 | "Reserved 23", // H264_NAL_RESERVED23 | ||
| 291 | "Unspecified 24", // H264_NAL_UNSPECIFIED24 | ||
| 292 | "Unspecified 25", // H264_NAL_UNSPECIFIED25 | ||
| 293 | "Unspecified 26", // H264_NAL_UNSPECIFIED26 | ||
| 294 | "Unspecified 27", // H264_NAL_UNSPECIFIED27 | ||
| 295 | "Unspecified 28", // H264_NAL_UNSPECIFIED28 | ||
| 296 | "Unspecified 29", // H264_NAL_UNSPECIFIED29 | ||
| 297 | "Unspecified 30", // H264_NAL_UNSPECIFIED30 | ||
| 298 | "Unspecified 31", // H264_NAL_UNSPECIFIED31 | ||
| 299 | }; | ||
| 300 | |||
| 301 | 63538 | static const char *h264_nal_unit_name(int nal_type) | |
| 302 | { | ||
| 303 |
2/4✓ Branch 0 taken 63538 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 63538 times.
|
63538 | av_assert0(nal_type >= 0 && nal_type < 32); |
| 304 | 63538 | return h264_nal_type_name[nal_type]; | |
| 305 | } | ||
| 306 | |||
| 307 | 183717 | static int get_bit_length(H2645NAL *nal, int min_size, int skip_trailing_zeros) | |
| 308 | { | ||
| 309 | 183717 | int size = nal->size; | |
| 310 | 183717 | int trailing_padding = 0; | |
| 311 | |||
| 312 |
5/6✓ Branch 0 taken 230361 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 230361 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 46648 times.
✓ Branch 5 taken 183713 times.
|
230365 | while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0) |
| 313 | 46648 | size--; | |
| 314 | |||
| 315 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 183717 times.
|
183717 | if (!size) |
| 316 | ✗ | return 0; | |
| 317 | |||
| 318 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 183653 times.
|
183717 | if (size <= min_size) { |
| 319 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | if (nal->size < min_size) |
| 320 | ✗ | return AVERROR_INVALIDDATA; | |
| 321 | 64 | size = min_size; | |
| 322 | } else { | ||
| 323 | 183653 | int v = nal->data[size - 1]; | |
| 324 | /* remove the stop bit and following trailing zeros, | ||
| 325 | * or nothing for damaged bitstreams */ | ||
| 326 |
1/2✓ Branch 0 taken 183653 times.
✗ Branch 1 not taken.
|
183653 | if (v) |
| 327 | 183653 | trailing_padding = ff_ctz(v) + 1; | |
| 328 | } | ||
| 329 | |||
| 330 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 183717 times.
|
183717 | if (size > INT_MAX / 8) |
| 331 | ✗ | return AVERROR(ERANGE); | |
| 332 | 183717 | size *= 8; | |
| 333 | |||
| 334 | 183717 | return size - trailing_padding; | |
| 335 | } | ||
| 336 | |||
| 337 | /** | ||
| 338 | * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit, | ||
| 339 | * 0 otherwise | ||
| 340 | */ | ||
| 341 | 19789 | static int vvc_parse_nal_header(H2645NAL *nal, void *logctx) | |
| 342 | { | ||
| 343 | 19789 | GetBitContext *gb = &nal->gb; | |
| 344 | |||
| 345 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 19789 times.
|
19789 | if (get_bits1(gb) != 0) //forbidden_zero_bit |
| 346 | ✗ | return AVERROR_INVALIDDATA; | |
| 347 | |||
| 348 | 19789 | skip_bits1(gb); //nuh_reserved_zero_bit | |
| 349 | |||
| 350 | 19789 | nal->nuh_layer_id = get_bits(gb, 6); | |
| 351 | 19789 | nal->type = get_bits(gb, 5); | |
| 352 | 19789 | nal->temporal_id = get_bits(gb, 3) - 1; | |
| 353 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19789 times.
|
19789 | if (nal->temporal_id < 0) |
| 354 | ✗ | return AVERROR_INVALIDDATA; | |
| 355 | |||
| 356 |
5/6✓ Branch 0 taken 12307 times.
✓ Branch 1 taken 7482 times.
✓ Branch 2 taken 1408 times.
✓ Branch 3 taken 10899 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1408 times.
|
19789 | if ((nal->type >= VVC_IDR_W_RADL && nal->type <= VVC_RSV_IRAP_11) && nal->temporal_id) |
| 357 | ✗ | return AVERROR_INVALIDDATA; | |
| 358 | |||
| 359 | 19789 | av_log(logctx, AV_LOG_DEBUG, | |
| 360 | "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n", | ||
| 361 | nal->type, vvc_nal_unit_name(nal->type), nal->nuh_layer_id, nal->temporal_id); | ||
| 362 | |||
| 363 | 19789 | return 0; | |
| 364 | } | ||
| 365 | |||
| 366 | 100390 | static int hevc_parse_nal_header(H2645NAL *nal, void *logctx) | |
| 367 | { | ||
| 368 | 100390 | GetBitContext *gb = &nal->gb; | |
| 369 | |||
| 370 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 100388 times.
|
100390 | if (get_bits1(gb) != 0) |
| 371 | 2 | return AVERROR_INVALIDDATA; | |
| 372 | |||
| 373 | 100388 | nal->type = get_bits(gb, 6); | |
| 374 | |||
| 375 | 100388 | nal->nuh_layer_id = get_bits(gb, 6); | |
| 376 | 100388 | nal->temporal_id = get_bits(gb, 3) - 1; | |
| 377 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 100388 times.
|
100388 | if (nal->temporal_id < 0) |
| 378 | ✗ | return AVERROR_INVALIDDATA; | |
| 379 | |||
| 380 | 100388 | av_log(logctx, AV_LOG_DEBUG, | |
| 381 | "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n", | ||
| 382 | nal->type, hevc_nal_unit_name(nal->type), nal->nuh_layer_id, nal->temporal_id); | ||
| 383 | |||
| 384 | 100388 | return 0; | |
| 385 | } | ||
| 386 | |||
| 387 | 63538 | static int h264_parse_nal_header(H2645NAL *nal, void *logctx) | |
| 388 | { | ||
| 389 | 63538 | GetBitContext *gb = &nal->gb; | |
| 390 | |||
| 391 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 63538 times.
|
63538 | if (get_bits1(gb) != 0) |
| 392 | ✗ | return AVERROR_INVALIDDATA; | |
| 393 | |||
| 394 | 63538 | nal->ref_idc = get_bits(gb, 2); | |
| 395 | 63538 | nal->type = get_bits(gb, 5); | |
| 396 | |||
| 397 | 63538 | av_log(logctx, AV_LOG_DEBUG, | |
| 398 | "nal_unit_type: %d(%s), nal_ref_idc: %d\n", | ||
| 399 | nal->type, h264_nal_unit_name(nal->type), nal->ref_idc); | ||
| 400 | |||
| 401 | 63538 | return 0; | |
| 402 | } | ||
| 403 | |||
| 404 | 174041 | static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc) | |
| 405 | { | ||
| 406 | 174041 | int i = 0; | |
| 407 | |||
| 408 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 174041 times.
|
174041 | if (buf + 3 >= next_avc) |
| 409 | ✗ | return next_avc - buf; | |
| 410 | |||
| 411 |
1/2✓ Branch 0 taken 232640 times.
✗ Branch 1 not taken.
|
232640 | while (buf + i + 3 < next_avc) { |
| 412 |
4/6✓ Branch 0 taken 232640 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 232640 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 174041 times.
✓ Branch 5 taken 58599 times.
|
232640 | if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1) |
| 413 | 174041 | break; | |
| 414 | 58599 | i++; | |
| 415 | } | ||
| 416 | 174041 | return i + 3; | |
| 417 | } | ||
| 418 | |||
| 419 | 66407 | static void alloc_rbsp_buffer(H2645RBSP *rbsp, unsigned int size, int use_ref) | |
| 420 | { | ||
| 421 | 66407 | int min_size = size; | |
| 422 | |||
| 423 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66407 times.
|
66407 | if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) |
| 424 | ✗ | goto fail; | |
| 425 | 66407 | size += AV_INPUT_BUFFER_PADDING_SIZE; | |
| 426 | |||
| 427 |
2/2✓ Branch 0 taken 61651 times.
✓ Branch 1 taken 4756 times.
|
66407 | if (rbsp->rbsp_buffer_alloc_size >= size && |
| 428 |
4/4✓ Branch 0 taken 11100 times.
✓ Branch 1 taken 50551 times.
✓ Branch 3 taken 10464 times.
✓ Branch 4 taken 636 times.
|
61651 | (!rbsp->rbsp_buffer_ref || av_buffer_is_writable(rbsp->rbsp_buffer_ref))) { |
| 429 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61015 times.
|
61015 | av_assert0(rbsp->rbsp_buffer); |
| 430 | 61015 | memset(rbsp->rbsp_buffer + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
| 431 | 61015 | return; | |
| 432 | } | ||
| 433 | |||
| 434 |
1/2✓ Branch 0 taken 5392 times.
✗ Branch 1 not taken.
|
5392 | size = FFMIN(size + size / 16 + 32, INT_MAX); |
| 435 | |||
| 436 |
2/2✓ Branch 0 taken 1628 times.
✓ Branch 1 taken 3764 times.
|
5392 | if (rbsp->rbsp_buffer_ref) |
| 437 | 1628 | av_buffer_unref(&rbsp->rbsp_buffer_ref); | |
| 438 | else | ||
| 439 | 3764 | av_free(rbsp->rbsp_buffer); | |
| 440 | |||
| 441 | 5392 | rbsp->rbsp_buffer = av_mallocz(size); | |
| 442 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5392 times.
|
5392 | if (!rbsp->rbsp_buffer) |
| 443 | ✗ | goto fail; | |
| 444 | 5392 | rbsp->rbsp_buffer_alloc_size = size; | |
| 445 | |||
| 446 |
2/2✓ Branch 0 taken 1861 times.
✓ Branch 1 taken 3531 times.
|
5392 | if (use_ref) { |
| 447 | 1861 | rbsp->rbsp_buffer_ref = av_buffer_create(rbsp->rbsp_buffer, size, | |
| 448 | NULL, NULL, 0); | ||
| 449 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1861 times.
|
1861 | if (!rbsp->rbsp_buffer_ref) |
| 450 | ✗ | goto fail; | |
| 451 | } | ||
| 452 | |||
| 453 | 5392 | return; | |
| 454 | |||
| 455 | ✗ | fail: | |
| 456 | ✗ | rbsp->rbsp_buffer_alloc_size = 0; | |
| 457 | ✗ | if (rbsp->rbsp_buffer_ref) { | |
| 458 | ✗ | av_buffer_unref(&rbsp->rbsp_buffer_ref); | |
| 459 | ✗ | rbsp->rbsp_buffer = NULL; | |
| 460 | } else | ||
| 461 | ✗ | av_freep(&rbsp->rbsp_buffer); | |
| 462 | |||
| 463 | ✗ | return; | |
| 464 | } | ||
| 465 | |||
| 466 | 66407 | int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, | |
| 467 | void *logctx, int nal_length_size, | ||
| 468 | enum AVCodecID codec_id, int flags) | ||
| 469 | { | ||
| 470 | GetByteContext bc; | ||
| 471 | 66407 | int consumed, ret = 0; | |
| 472 |
2/2✓ Branch 0 taken 61066 times.
✓ Branch 1 taken 5341 times.
|
66407 | int next_avc = (flags & H2645_FLAG_IS_NALFF) ? 0 : length; |
| 473 |
2/2✓ Branch 0 taken 37138 times.
✓ Branch 1 taken 29269 times.
|
66407 | int64_t padding = (flags & H2645_FLAG_SMALL_PADDING) ? 0 : MAX_MBPAIR_SIZE; |
| 474 | |||
| 475 | 66407 | bytestream2_init(&bc, buf, length); | |
| 476 | 66407 | alloc_rbsp_buffer(&pkt->rbsp, length + padding, !!(flags & H2645_FLAG_USE_REF)); | |
| 477 | |||
| 478 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66407 times.
|
66407 | if (!pkt->rbsp.rbsp_buffer) |
| 479 | ✗ | return AVERROR(ENOMEM); | |
| 480 | |||
| 481 | 66407 | pkt->rbsp.rbsp_buffer_size = 0; | |
| 482 | 66407 | pkt->nb_nals = 0; | |
| 483 |
2/2✓ Branch 1 taken 183718 times.
✓ Branch 2 taken 66406 times.
|
250124 | while (bytestream2_get_bytes_left(&bc) >= 4) { |
| 484 | H2645NAL *nal; | ||
| 485 | 183718 | int extract_length = 0; | |
| 486 | 183718 | int skip_trailing_zeros = 1; | |
| 487 | |||
| 488 |
2/2✓ Branch 1 taken 9677 times.
✓ Branch 2 taken 174041 times.
|
183718 | if (bytestream2_tell(&bc) == next_avc) { |
| 489 | 9677 | int i = 0; | |
| 490 | 9677 | extract_length = get_nalsize(nal_length_size, | |
| 491 | bc.buffer, bytestream2_get_bytes_left(&bc), &i, logctx); | ||
| 492 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9676 times.
|
9677 | if (extract_length < 0) |
| 493 | 1 | return extract_length; | |
| 494 | |||
| 495 | 9676 | bytestream2_skip(&bc, nal_length_size); | |
| 496 | |||
| 497 | 9676 | next_avc = bytestream2_tell(&bc) + extract_length; | |
| 498 | } else { | ||
| 499 | int buf_index; | ||
| 500 | |||
| 501 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 174041 times.
|
174041 | if (bytestream2_tell(&bc) > next_avc) |
| 502 | ✗ | av_log(logctx, AV_LOG_WARNING, "Exceeded next NALFF position, re-syncing.\n"); | |
| 503 | |||
| 504 | /* search start code */ | ||
| 505 | 174041 | buf_index = find_next_start_code(bc.buffer, buf + next_avc); | |
| 506 | |||
| 507 | 174041 | bytestream2_skip(&bc, buf_index); | |
| 508 | |||
| 509 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 174041 times.
|
174041 | if (!bytestream2_get_bytes_left(&bc)) { |
| 510 | ✗ | if (pkt->nb_nals > 0) { | |
| 511 | // No more start codes: we discarded some irrelevant | ||
| 512 | // bytes at the end of the packet. | ||
| 513 | ✗ | return 0; | |
| 514 | } else { | ||
| 515 | ✗ | av_log(logctx, AV_LOG_ERROR, "No start code is found.\n"); | |
| 516 | ✗ | return AVERROR_INVALIDDATA; | |
| 517 | } | ||
| 518 | } | ||
| 519 | |||
| 520 |
2/2✓ Branch 2 taken 6 times.
✓ Branch 3 taken 174035 times.
|
174041 | extract_length = FFMIN(bytestream2_get_bytes_left(&bc), next_avc - bytestream2_tell(&bc)); |
| 521 | |||
| 522 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 174041 times.
|
174041 | if (bytestream2_tell(&bc) >= next_avc) { |
| 523 | /* skip to the start of the next NAL */ | ||
| 524 | ✗ | bytestream2_skip(&bc, next_avc - bytestream2_tell(&bc)); | |
| 525 | ✗ | continue; | |
| 526 | } | ||
| 527 | } | ||
| 528 | |||
| 529 |
2/2✓ Branch 0 taken 14617 times.
✓ Branch 1 taken 169100 times.
|
183717 | if (pkt->nals_allocated < pkt->nb_nals + 1) { |
| 530 | 14617 | int new_size = pkt->nals_allocated + 1; | |
| 531 | void *tmp; | ||
| 532 | |||
| 533 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14617 times.
|
14617 | if (new_size >= INT_MAX / sizeof(*pkt->nals)) |
| 534 | ✗ | return AVERROR(ENOMEM); | |
| 535 | |||
| 536 | 14617 | tmp = av_fast_realloc(pkt->nals, &pkt->nal_buffer_size, new_size * sizeof(*pkt->nals)); | |
| 537 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14617 times.
|
14617 | if (!tmp) |
| 538 | ✗ | return AVERROR(ENOMEM); | |
| 539 | |||
| 540 | 14617 | pkt->nals = tmp; | |
| 541 | 14617 | memset(pkt->nals + pkt->nals_allocated, 0, sizeof(*pkt->nals)); | |
| 542 | |||
| 543 | 14617 | nal = &pkt->nals[pkt->nb_nals]; | |
| 544 |
2/2✓ Branch 0 taken 6204 times.
✓ Branch 1 taken 8413 times.
|
14617 | nal->skipped_bytes_pos_size = FFMIN(1024, extract_length/3+1); // initial buffer size |
| 545 | 14617 | nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos)); | |
| 546 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14617 times.
|
14617 | if (!nal->skipped_bytes_pos) |
| 547 | ✗ | return AVERROR(ENOMEM); | |
| 548 | |||
| 549 | 14617 | pkt->nals_allocated = new_size; | |
| 550 | } | ||
| 551 | 183717 | nal = &pkt->nals[pkt->nb_nals]; | |
| 552 | |||
| 553 | 183717 | consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, &pkt->rbsp, nal, | |
| 554 | 183717 | !!(flags & H2645_FLAG_SMALL_PADDING)); | |
| 555 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 183717 times.
|
183717 | if (consumed < 0) |
| 556 | ✗ | return consumed; | |
| 557 | |||
| 558 |
5/6✓ Branch 0 taken 9688 times.
✓ Branch 1 taken 174029 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 9676 times.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
|
183717 | if ((flags & H2645_FLAG_IS_NALFF) && (extract_length != consumed) && extract_length) |
| 559 | 12 | av_log(logctx, AV_LOG_DEBUG, | |
| 560 | "NALFF: Consumed only %d bytes instead of %d\n", | ||
| 561 | consumed, extract_length); | ||
| 562 | |||
| 563 | 183717 | bytestream2_skip(&bc, consumed); | |
| 564 | |||
| 565 | /* see commit 3566042a0 */ | ||
| 566 |
4/4✓ Branch 1 taken 117313 times.
✓ Branch 2 taken 66404 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 117309 times.
|
301030 | if (bytestream2_get_bytes_left(&bc) >= 4 && |
| 567 | 117313 | bytestream2_peek_be32(&bc) == 0x000001E0) | |
| 568 | 4 | skip_trailing_zeros = 0; | |
| 569 | |||
| 570 |
2/2✓ Branch 0 taken 100390 times.
✓ Branch 1 taken 83327 times.
|
183717 | nal->size_bits = get_bit_length(nal, 1 + (codec_id == AV_CODEC_ID_HEVC), |
| 571 | skip_trailing_zeros); | ||
| 572 | |||
| 573 |
2/4✓ Branch 0 taken 183717 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 183717 times.
|
183717 | if (nal->size <= 0 || nal->size_bits <= 0) |
| 574 | ✗ | continue; | |
| 575 | |||
| 576 | 183717 | ret = init_get_bits(&nal->gb, nal->data, nal->size_bits); | |
| 577 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 183717 times.
|
183717 | if (ret < 0) |
| 578 | ✗ | return ret; | |
| 579 | |||
| 580 | /* Reset type in case it contains a stale value from a previously parsed NAL */ | ||
| 581 | 183717 | nal->type = 0; | |
| 582 | |||
| 583 |
2/2✓ Branch 0 taken 19789 times.
✓ Branch 1 taken 163928 times.
|
183717 | if (codec_id == AV_CODEC_ID_VVC) |
| 584 | 19789 | ret = vvc_parse_nal_header(nal, logctx); | |
| 585 |
2/2✓ Branch 0 taken 100390 times.
✓ Branch 1 taken 63538 times.
|
163928 | else if (codec_id == AV_CODEC_ID_HEVC) { |
| 586 | 100390 | ret = hevc_parse_nal_header(nal, logctx); | |
| 587 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 100390 times.
|
100390 | if (nal->nuh_layer_id == 63) |
| 588 | ✗ | continue; | |
| 589 | } else | ||
| 590 | 63538 | ret = h264_parse_nal_header(nal, logctx); | |
| 591 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 183715 times.
|
183717 | if (ret < 0) { |
| 592 | 2 | av_log(logctx, AV_LOG_WARNING, | |
| 593 | "Failed to parse header of NALU (type %d): \"%s\". Skipping NALU.\n", | ||
| 594 | 2 | nal->type, av_err2str(ret)); | |
| 595 | 2 | continue; | |
| 596 | } | ||
| 597 | |||
| 598 | 183715 | pkt->nb_nals++; | |
| 599 | } | ||
| 600 | |||
| 601 | 66406 | return 0; | |
| 602 | } | ||
| 603 | |||
| 604 | 4171 | void ff_h2645_packet_uninit(H2645Packet *pkt) | |
| 605 | { | ||
| 606 | int i; | ||
| 607 |
2/2✓ Branch 0 taken 14617 times.
✓ Branch 1 taken 4171 times.
|
18788 | for (i = 0; i < pkt->nals_allocated; i++) { |
| 608 | 14617 | av_freep(&pkt->nals[i].skipped_bytes_pos); | |
| 609 | } | ||
| 610 | 4171 | av_freep(&pkt->nals); | |
| 611 | 4171 | pkt->nals_allocated = pkt->nal_buffer_size = 0; | |
| 612 |
2/2✓ Branch 0 taken 233 times.
✓ Branch 1 taken 3938 times.
|
4171 | if (pkt->rbsp.rbsp_buffer_ref) { |
| 613 | 233 | av_buffer_unref(&pkt->rbsp.rbsp_buffer_ref); | |
| 614 | 233 | pkt->rbsp.rbsp_buffer = NULL; | |
| 615 | } else | ||
| 616 | 3938 | av_freep(&pkt->rbsp.rbsp_buffer); | |
| 617 | 4171 | pkt->rbsp.rbsp_buffer_alloc_size = pkt->rbsp.rbsp_buffer_size = 0; | |
| 618 | 4171 | } | |
| 619 |