| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of FFmpeg. | ||
| 3 | * | ||
| 4 | * FFmpeg is free software; you can redistribute it and/or | ||
| 5 | * modify it under the terms of the GNU Lesser General Public | ||
| 6 | * License as published by the Free Software Foundation; either | ||
| 7 | * version 2.1 of the License, or (at your option) any later version. | ||
| 8 | * | ||
| 9 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 12 | * Lesser General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU Lesser General Public | ||
| 15 | * License along with FFmpeg; if not, write to the Free Software | ||
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 17 | */ | ||
| 18 | |||
| 19 | #include "libavutil/common.h" | ||
| 20 | #include "libavutil/opt.h" | ||
| 21 | |||
| 22 | #include "bsf.h" | ||
| 23 | #include "bsf_internal.h" | ||
| 24 | #include "cbs.h" | ||
| 25 | #include "cbs_bsf.h" | ||
| 26 | #include "cbs_h265.h" | ||
| 27 | #include "h2645data.h" | ||
| 28 | #include "h265_profile_level.h" | ||
| 29 | |||
| 30 | #include "hevc/hevc.h" | ||
| 31 | |||
| 32 | enum { | ||
| 33 | LEVEL_UNSET = -2, | ||
| 34 | LEVEL_AUTO = -1, | ||
| 35 | }; | ||
| 36 | |||
| 37 | typedef struct H265MetadataContext { | ||
| 38 | CBSBSFContext common; | ||
| 39 | |||
| 40 | H265RawAUD aud_nal; | ||
| 41 | |||
| 42 | int aud; | ||
| 43 | |||
| 44 | AVRational sample_aspect_ratio; | ||
| 45 | |||
| 46 | int video_format; | ||
| 47 | int video_full_range_flag; | ||
| 48 | int colour_primaries; | ||
| 49 | int transfer_characteristics; | ||
| 50 | int matrix_coefficients; | ||
| 51 | |||
| 52 | int chroma_sample_loc_type; | ||
| 53 | |||
| 54 | AVRational tick_rate; | ||
| 55 | int poc_proportional_to_timing_flag; | ||
| 56 | int num_ticks_poc_diff_one; | ||
| 57 | |||
| 58 | int crop_left; | ||
| 59 | int crop_right; | ||
| 60 | int crop_top; | ||
| 61 | int crop_bottom; | ||
| 62 | int width; | ||
| 63 | int height; | ||
| 64 | |||
| 65 | int level; | ||
| 66 | int level_guess; | ||
| 67 | int level_warned; | ||
| 68 | } H265MetadataContext; | ||
| 69 | |||
| 70 | |||
| 71 | ✗ | static void h265_metadata_guess_level(AVBSFContext *bsf, | |
| 72 | const CodedBitstreamFragment *au) | ||
| 73 | { | ||
| 74 | ✗ | H265MetadataContext *ctx = bsf->priv_data; | |
| 75 | const H265LevelDescriptor *desc; | ||
| 76 | ✗ | const H265RawProfileTierLevel *ptl = NULL; | |
| 77 | ✗ | const H265RawHRDParameters *hrd = NULL; | |
| 78 | ✗ | int64_t bit_rate = 0; | |
| 79 | ✗ | int width = 0, height = 0; | |
| 80 | ✗ | int tile_cols = 0, tile_rows = 0; | |
| 81 | ✗ | int max_dec_pic_buffering = 0; | |
| 82 | int i; | ||
| 83 | |||
| 84 | ✗ | for (i = 0; i < au->nb_units; i++) { | |
| 85 | ✗ | const CodedBitstreamUnit *unit = &au->units[i]; | |
| 86 | |||
| 87 | ✗ | if (unit->type == HEVC_NAL_VPS) { | |
| 88 | ✗ | const H265RawVPS *vps = unit->content; | |
| 89 | |||
| 90 | ✗ | ptl = &vps->profile_tier_level; | |
| 91 | ✗ | max_dec_pic_buffering = vps->vps_max_dec_pic_buffering_minus1[0] + 1; | |
| 92 | |||
| 93 | ✗ | if (vps->vps_num_hrd_parameters > 0) | |
| 94 | ✗ | hrd = &vps->hrd_parameters[0]; | |
| 95 | |||
| 96 | ✗ | } else if (unit->type == HEVC_NAL_SPS) { | |
| 97 | ✗ | const H265RawSPS *sps = unit->content; | |
| 98 | |||
| 99 | ✗ | ptl = &sps->profile_tier_level; | |
| 100 | ✗ | max_dec_pic_buffering = sps->sps_max_dec_pic_buffering_minus1[0] + 1; | |
| 101 | |||
| 102 | ✗ | width = sps->pic_width_in_luma_samples; | |
| 103 | ✗ | height = sps->pic_height_in_luma_samples; | |
| 104 | |||
| 105 | ✗ | if (sps->vui.vui_hrd_parameters_present_flag) | |
| 106 | ✗ | hrd = &sps->vui.hrd_parameters; | |
| 107 | |||
| 108 | ✗ | } else if (unit->type == HEVC_NAL_PPS) { | |
| 109 | ✗ | const H265RawPPS *pps = unit->content; | |
| 110 | |||
| 111 | ✗ | if (pps->tiles_enabled_flag) { | |
| 112 | ✗ | tile_cols = pps->num_tile_columns_minus1 + 1; | |
| 113 | ✗ | tile_rows = pps->num_tile_rows_minus1 + 1; | |
| 114 | } | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | ✗ | if (hrd) { | |
| 119 | ✗ | if (hrd->nal_hrd_parameters_present_flag) { | |
| 120 | ✗ | bit_rate = (hrd->nal_sub_layer_hrd_parameters[0].bit_rate_value_minus1[0] + 1) * | |
| 121 | ✗ | (INT64_C(1) << hrd->bit_rate_scale + 6); | |
| 122 | ✗ | } else if (hrd->vcl_hrd_parameters_present_flag) { | |
| 123 | ✗ | bit_rate = (hrd->vcl_sub_layer_hrd_parameters[0].bit_rate_value_minus1[0] + 1) * | |
| 124 | ✗ | (INT64_C(1) << hrd->bit_rate_scale + 6); | |
| 125 | // Adjust for VCL vs. NAL limits. | ||
| 126 | ✗ | bit_rate = bit_rate * 11 / 10; | |
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | ✗ | desc = ff_h265_guess_level(ptl, bit_rate, width, height, | |
| 131 | 0, tile_rows, tile_cols, | ||
| 132 | max_dec_pic_buffering); | ||
| 133 | ✗ | if (desc) { | |
| 134 | ✗ | av_log(bsf, AV_LOG_DEBUG, "Stream appears to conform to " | |
| 135 | ✗ | "level %s.\n", desc->name); | |
| 136 | ✗ | ctx->level_guess = desc->level_idc; | |
| 137 | } | ||
| 138 | ✗ | } | |
| 139 | |||
| 140 | 106 | static void h265_metadata_update_level(AVBSFContext *bsf, | |
| 141 | uint8_t *level_idc) | ||
| 142 | { | ||
| 143 | 106 | H265MetadataContext *ctx = bsf->priv_data; | |
| 144 | |||
| 145 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
|
106 | if (ctx->level != LEVEL_UNSET) { |
| 146 | ✗ | if (ctx->level == LEVEL_AUTO) { | |
| 147 | ✗ | if (ctx->level_guess) { | |
| 148 | ✗ | *level_idc = ctx->level_guess; | |
| 149 | } else { | ||
| 150 | ✗ | if (!ctx->level_warned) { | |
| 151 | ✗ | av_log(bsf, AV_LOG_WARNING, "Unable to determine level " | |
| 152 | "of stream: using level 8.5.\n"); | ||
| 153 | ✗ | ctx->level_warned = 1; | |
| 154 | } | ||
| 155 | ✗ | *level_idc = 255; | |
| 156 | } | ||
| 157 | } else { | ||
| 158 | ✗ | *level_idc = ctx->level; | |
| 159 | } | ||
| 160 | } | ||
| 161 | 106 | } | |
| 162 | |||
| 163 | 52 | static int h265_metadata_update_vps(AVBSFContext *bsf, | |
| 164 | H265RawVPS *vps) | ||
| 165 | { | ||
| 166 | 52 | H265MetadataContext *ctx = bsf->priv_data; | |
| 167 | |||
| 168 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
52 | if (ctx->tick_rate.num && ctx->tick_rate.den) { |
| 169 | int num, den; | ||
| 170 | |||
| 171 | ✗ | av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den, | |
| 172 | UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX); | ||
| 173 | |||
| 174 | ✗ | vps->vps_time_scale = num; | |
| 175 | ✗ | vps->vps_num_units_in_tick = den; | |
| 176 | |||
| 177 | ✗ | vps->vps_timing_info_present_flag = 1; | |
| 178 | |||
| 179 | ✗ | if (ctx->num_ticks_poc_diff_one > 0) { | |
| 180 | ✗ | vps->vps_num_ticks_poc_diff_one_minus1 = | |
| 181 | ✗ | ctx->num_ticks_poc_diff_one - 1; | |
| 182 | ✗ | vps->vps_poc_proportional_to_timing_flag = 1; | |
| 183 | ✗ | } else if (ctx->num_ticks_poc_diff_one == 0) { | |
| 184 | ✗ | vps->vps_poc_proportional_to_timing_flag = 0; | |
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | 52 | h265_metadata_update_level(bsf, &vps->profile_tier_level.general_level_idc); | |
| 189 | |||
| 190 | 52 | return 0; | |
| 191 | } | ||
| 192 | |||
| 193 | 54 | static int h265_metadata_deduce_crop(AVBSFContext *bsf, const H265RawSPS *sps, | |
| 194 | int *crop_left, int *crop_right, | ||
| 195 | int *crop_top, int *crop_bottom) | ||
| 196 | { | ||
| 197 | 54 | const H265MetadataContext *ctx = bsf->priv_data; | |
| 198 | 54 | int left = ctx->crop_left; | |
| 199 | 54 | int right = ctx->crop_right; | |
| 200 | 54 | int top = ctx->crop_top; | |
| 201 | 54 | int bottom = ctx->crop_bottom; | |
| 202 | |||
| 203 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
|
54 | if (ctx->width > 0) { |
| 204 | ✗ | if (ctx->width > sps->pic_width_in_luma_samples) { | |
| 205 | ✗ | av_log(bsf, AV_LOG_ERROR, | |
| 206 | "The width option value %d is larger than picture width %d\n", | ||
| 207 | ✗ | ctx->width, sps->pic_width_in_luma_samples); | |
| 208 | ✗ | return AVERROR(EINVAL); | |
| 209 | } | ||
| 210 | |||
| 211 | ✗ | if (left < 0) { | |
| 212 | ✗ | if (right > 0) | |
| 213 | ✗ | left = sps->pic_width_in_luma_samples - ctx->width - right; | |
| 214 | else | ||
| 215 | ✗ | left = 0; | |
| 216 | } | ||
| 217 | |||
| 218 | ✗ | if (right < 0) | |
| 219 | ✗ | right = sps->pic_width_in_luma_samples - ctx->width - left; | |
| 220 | |||
| 221 | ✗ | if (left < 0 || right < 0 || (left + right + ctx->width) != | |
| 222 | ✗ | sps->pic_width_in_luma_samples) { | |
| 223 | ✗ | av_log(bsf, AV_LOG_ERROR, | |
| 224 | "Invalid value for crop_left %d, crop_right %d, width after " | ||
| 225 | "crop %d, with picture width %d\n", | ||
| 226 | ✗ | ctx->crop_left, ctx->crop_right, ctx->width, | |
| 227 | ✗ | sps->pic_width_in_luma_samples); | |
| 228 | ✗ | return AVERROR(EINVAL); | |
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
|
54 | if (ctx->height > 0) { |
| 233 | ✗ | if (ctx->height > sps->pic_height_in_luma_samples) { | |
| 234 | ✗ | av_log(bsf, AV_LOG_ERROR, | |
| 235 | "The height option value %d is larger than picture height %d\n", | ||
| 236 | ✗ | ctx->height, sps->pic_height_in_luma_samples); | |
| 237 | ✗ | return AVERROR(EINVAL); | |
| 238 | } | ||
| 239 | |||
| 240 | ✗ | if (top < 0) { | |
| 241 | ✗ | if (bottom > 0) | |
| 242 | ✗ | top = sps->pic_height_in_luma_samples - ctx->height - bottom; | |
| 243 | else | ||
| 244 | ✗ | top = 0; | |
| 245 | } | ||
| 246 | |||
| 247 | ✗ | if (bottom < 0) | |
| 248 | ✗ | bottom = sps->pic_height_in_luma_samples - ctx->height - top; | |
| 249 | |||
| 250 | ✗ | if (top < 0 || bottom < 0 || (top + bottom + ctx->height) != | |
| 251 | ✗ | sps->pic_height_in_luma_samples) { | |
| 252 | ✗ | av_log(bsf, AV_LOG_ERROR, | |
| 253 | "Invalid value for crop_top %d, crop_bottom %d, height after " | ||
| 254 | "crop %d, with picture height %d\n", | ||
| 255 | ✗ | ctx->crop_top, ctx->crop_bottom, ctx->height, | |
| 256 | ✗ | sps->pic_height_in_luma_samples); | |
| 257 | ✗ | return AVERROR(EINVAL); | |
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | 54 | *crop_left = left; | |
| 262 | 54 | *crop_right = right; | |
| 263 | 54 | *crop_top = top; | |
| 264 | 54 | *crop_bottom = bottom; | |
| 265 | |||
| 266 | 54 | return 0; | |
| 267 | } | ||
| 268 | |||
| 269 | 54 | static int h265_metadata_update_sps(AVBSFContext *bsf, | |
| 270 | H265RawSPS *sps) | ||
| 271 | { | ||
| 272 | 54 | H265MetadataContext *ctx = bsf->priv_data; | |
| 273 | 54 | int need_vui = 0; | |
| 274 | int crop_unit_x, crop_unit_y; | ||
| 275 | /* Use local variables to avoid modifying context fields in case of video | ||
| 276 | * resolution changed. Crop doesn't work well with resolution change, this | ||
| 277 | * is the best we can do. | ||
| 278 | */ | ||
| 279 | int crop_left, crop_right, crop_top, crop_bottom; | ||
| 280 | int ret; | ||
| 281 | |||
| 282 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
54 | if (ctx->sample_aspect_ratio.num && ctx->sample_aspect_ratio.den) { |
| 283 | int num, den, i; | ||
| 284 | |||
| 285 | ✗ | av_reduce(&num, &den, ctx->sample_aspect_ratio.num, | |
| 286 | ✗ | ctx->sample_aspect_ratio.den, 65535); | |
| 287 | |||
| 288 | ✗ | for (i = 1; i < FF_ARRAY_ELEMS(ff_h2645_pixel_aspect); i++) { | |
| 289 | ✗ | if (num == ff_h2645_pixel_aspect[i].num && | |
| 290 | ✗ | den == ff_h2645_pixel_aspect[i].den) | |
| 291 | ✗ | break; | |
| 292 | } | ||
| 293 | ✗ | if (i == FF_ARRAY_ELEMS(ff_h2645_pixel_aspect)) { | |
| 294 | ✗ | sps->vui.aspect_ratio_idc = 255; | |
| 295 | ✗ | sps->vui.sar_width = num; | |
| 296 | ✗ | sps->vui.sar_height = den; | |
| 297 | } else { | ||
| 298 | ✗ | sps->vui.aspect_ratio_idc = i; | |
| 299 | } | ||
| 300 | ✗ | sps->vui.aspect_ratio_info_present_flag = 1; | |
| 301 | ✗ | need_vui = 1; | |
| 302 | } | ||
| 303 | |||
| 304 | #define SET_OR_INFER(field, value, present_flag, infer) do { \ | ||
| 305 | if (value >= 0) { \ | ||
| 306 | field = value; \ | ||
| 307 | need_vui = 1; \ | ||
| 308 | } else if (!present_flag) \ | ||
| 309 | field = infer; \ | ||
| 310 | } while (0) | ||
| 311 | |||
| 312 |
1/2✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
|
54 | if (ctx->video_format >= 0 || |
| 313 |
1/2✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
|
54 | ctx->video_full_range_flag >= 0 || |
| 314 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 4 times.
|
54 | ctx->colour_primaries >= 0 || |
| 315 |
1/2✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
|
50 | ctx->transfer_characteristics >= 0 || |
| 316 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | ctx->matrix_coefficients >= 0) { |
| 317 | |||
| 318 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | SET_OR_INFER(sps->vui.video_format, ctx->video_format, |
| 319 | sps->vui.video_signal_type_present_flag, 5); | ||
| 320 | |||
| 321 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | SET_OR_INFER(sps->vui.video_full_range_flag, |
| 322 | ctx->video_full_range_flag, | ||
| 323 | sps->vui.video_signal_type_present_flag, 0); | ||
| 324 | |||
| 325 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ctx->colour_primaries >= 0 || |
| 326 | ✗ | ctx->transfer_characteristics >= 0 || | |
| 327 | ✗ | ctx->matrix_coefficients >= 0) { | |
| 328 | |||
| 329 |
1/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
4 | SET_OR_INFER(sps->vui.colour_primaries, |
| 330 | ctx->colour_primaries, | ||
| 331 | sps->vui.colour_description_present_flag, 2); | ||
| 332 | |||
| 333 |
1/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
4 | SET_OR_INFER(sps->vui.transfer_characteristics, |
| 334 | ctx->transfer_characteristics, | ||
| 335 | sps->vui.colour_description_present_flag, 2); | ||
| 336 | |||
| 337 |
1/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
4 | SET_OR_INFER(sps->vui.matrix_coefficients, |
| 338 | ctx->matrix_coefficients, | ||
| 339 | sps->vui.colour_description_present_flag, 2); | ||
| 340 | |||
| 341 | 4 | sps->vui.colour_description_present_flag = 1; | |
| 342 | } | ||
| 343 | 4 | sps->vui.video_signal_type_present_flag = 1; | |
| 344 | 4 | need_vui = 1; | |
| 345 | } | ||
| 346 | |||
| 347 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
|
54 | if (ctx->chroma_sample_loc_type >= 0) { |
| 348 | ✗ | sps->vui.chroma_sample_loc_type_top_field = | |
| 349 | ✗ | ctx->chroma_sample_loc_type; | |
| 350 | ✗ | sps->vui.chroma_sample_loc_type_bottom_field = | |
| 351 | ✗ | ctx->chroma_sample_loc_type; | |
| 352 | ✗ | sps->vui.chroma_loc_info_present_flag = 1; | |
| 353 | ✗ | need_vui = 1; | |
| 354 | } | ||
| 355 | |||
| 356 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
54 | if (ctx->tick_rate.num && ctx->tick_rate.den) { |
| 357 | int num, den; | ||
| 358 | |||
| 359 | ✗ | av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den, | |
| 360 | UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX); | ||
| 361 | |||
| 362 | ✗ | sps->vui.vui_time_scale = num; | |
| 363 | ✗ | sps->vui.vui_num_units_in_tick = den; | |
| 364 | |||
| 365 | ✗ | sps->vui.vui_timing_info_present_flag = 1; | |
| 366 | ✗ | need_vui = 1; | |
| 367 | |||
| 368 | ✗ | if (ctx->num_ticks_poc_diff_one > 0) { | |
| 369 | ✗ | sps->vui.vui_num_ticks_poc_diff_one_minus1 = | |
| 370 | ✗ | ctx->num_ticks_poc_diff_one - 1; | |
| 371 | ✗ | sps->vui.vui_poc_proportional_to_timing_flag = 1; | |
| 372 | ✗ | } else if (ctx->num_ticks_poc_diff_one == 0) { | |
| 373 | ✗ | sps->vui.vui_poc_proportional_to_timing_flag = 0; | |
| 374 | } | ||
| 375 | } | ||
| 376 | |||
| 377 | 54 | ret = h265_metadata_deduce_crop(bsf, sps, &crop_left, &crop_right, | |
| 378 | &crop_top, &crop_bottom); | ||
| 379 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
|
54 | if (ret < 0) |
| 380 | ✗ | return ret; | |
| 381 | |||
| 382 |
2/4✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 54 times.
|
54 | if (sps->separate_colour_plane_flag || sps->chroma_format_idc == 0) { |
| 383 | ✗ | crop_unit_x = 1; | |
| 384 | ✗ | crop_unit_y = 1; | |
| 385 | } else { | ||
| 386 |
1/2✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
|
54 | crop_unit_x = 1 + (sps->chroma_format_idc < 3); |
| 387 |
1/2✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
|
54 | crop_unit_y = 1 + (sps->chroma_format_idc < 2); |
| 388 | } | ||
| 389 | #define CROP(border, unit) do { \ | ||
| 390 | if (crop_ ## border >= 0) { \ | ||
| 391 | if (crop_ ## border % unit != 0) { \ | ||
| 392 | av_log(bsf, AV_LOG_ERROR, "Invalid value for crop_%s: " \ | ||
| 393 | "must be a multiple of %d.\n", #border, unit); \ | ||
| 394 | return AVERROR(EINVAL); \ | ||
| 395 | } \ | ||
| 396 | sps->conf_win_ ## border ## _offset = \ | ||
| 397 | crop_ ## border / unit; \ | ||
| 398 | sps->conformance_window_flag = 1; \ | ||
| 399 | } \ | ||
| 400 | } while (0) | ||
| 401 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
54 | CROP(left, crop_unit_x); |
| 402 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
54 | CROP(right, crop_unit_x); |
| 403 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
54 | CROP(top, crop_unit_y); |
| 404 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
54 | CROP(bottom, crop_unit_y); |
| 405 | #undef CROP | ||
| 406 | |||
| 407 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 50 times.
|
54 | if (need_vui) |
| 408 | 4 | sps->vui_parameters_present_flag = 1; | |
| 409 | |||
| 410 | 54 | h265_metadata_update_level(bsf, &sps->profile_tier_level.general_level_idc); | |
| 411 | |||
| 412 | 54 | return 0; | |
| 413 | } | ||
| 414 | |||
| 415 | 2229 | static int h265_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt, | |
| 416 | CodedBitstreamFragment *au) | ||
| 417 | { | ||
| 418 | 2229 | H265MetadataContext *ctx = bsf->priv_data; | |
| 419 | int err, i; | ||
| 420 | |||
| 421 | // If an AUD is present, it must be the first NAL unit. | ||
| 422 |
3/4✓ Branch 0 taken 2229 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 2169 times.
|
2229 | if (au->nb_units && au->units[0].type == HEVC_NAL_AUD) { |
| 423 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
|
60 | if (ctx->aud == BSF_ELEMENT_REMOVE) |
| 424 | ✗ | ff_cbs_delete_unit(au, 0); | |
| 425 | } else { | ||
| 426 |
3/4✓ Branch 0 taken 2147 times.
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2147 times.
|
2169 | if (pkt && ctx->aud == BSF_ELEMENT_INSERT) { |
| 427 | ✗ | H265RawAUD *aud = &ctx->aud_nal; | |
| 428 | ✗ | int pic_type = 0, temporal_id = 8, layer_id = 0; | |
| 429 | |||
| 430 | ✗ | for (i = 0; i < au->nb_units; i++) { | |
| 431 | ✗ | const H265RawNALUnitHeader *nal = au->units[i].content; | |
| 432 | ✗ | if (!nal) | |
| 433 | ✗ | continue; | |
| 434 | ✗ | if (nal->nuh_temporal_id_plus1 < temporal_id + 1) | |
| 435 | ✗ | temporal_id = nal->nuh_temporal_id_plus1 - 1; | |
| 436 | |||
| 437 | ✗ | if (au->units[i].type <= HEVC_NAL_RSV_VCL31) { | |
| 438 | ✗ | const H265RawSlice *slice = au->units[i].content; | |
| 439 | ✗ | layer_id = nal->nuh_layer_id; | |
| 440 | ✗ | if (slice->header.slice_type == HEVC_SLICE_B && | |
| 441 | pic_type < 2) | ||
| 442 | ✗ | pic_type = 2; | |
| 443 | ✗ | if (slice->header.slice_type == HEVC_SLICE_P && | |
| 444 | pic_type < 1) | ||
| 445 | ✗ | pic_type = 1; | |
| 446 | } | ||
| 447 | } | ||
| 448 | |||
| 449 | ✗ | aud->nal_unit_header = (H265RawNALUnitHeader) { | |
| 450 | .nal_unit_type = HEVC_NAL_AUD, | ||
| 451 | .nuh_layer_id = layer_id, | ||
| 452 | ✗ | .nuh_temporal_id_plus1 = temporal_id + 1, | |
| 453 | }; | ||
| 454 | ✗ | aud->pic_type = pic_type; | |
| 455 | |||
| 456 | ✗ | err = ff_cbs_insert_unit_content(au, 0, HEVC_NAL_AUD, aud, NULL); | |
| 457 | ✗ | if (err < 0) { | |
| 458 | ✗ | av_log(bsf, AV_LOG_ERROR, "Failed to insert AUD.\n"); | |
| 459 | ✗ | return err; | |
| 460 | } | ||
| 461 | } | ||
| 462 | } | ||
| 463 | |||
| 464 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2229 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2229 | if (ctx->level == LEVEL_AUTO && !ctx->level_guess) |
| 465 | ✗ | h265_metadata_guess_level(bsf, au); | |
| 466 | |||
| 467 |
2/2✓ Branch 0 taken 7919 times.
✓ Branch 1 taken 2229 times.
|
10148 | for (i = 0; i < au->nb_units; i++) { |
| 468 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 7867 times.
|
7919 | if (au->units[i].type == HEVC_NAL_VPS) { |
| 469 | 52 | err = h265_metadata_update_vps(bsf, au->units[i].content); | |
| 470 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
|
52 | if (err < 0) |
| 471 | ✗ | return err; | |
| 472 | } | ||
| 473 |
2/2✓ Branch 0 taken 54 times.
✓ Branch 1 taken 7865 times.
|
7919 | if (au->units[i].type == HEVC_NAL_SPS) { |
| 474 | 54 | err = h265_metadata_update_sps(bsf, au->units[i].content); | |
| 475 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
|
54 | if (err < 0) |
| 476 | ✗ | return err; | |
| 477 | } | ||
| 478 | } | ||
| 479 | |||
| 480 | 2229 | return 0; | |
| 481 | } | ||
| 482 | |||
| 483 | static const CBSBSFType h265_metadata_type = { | ||
| 484 | .codec_id = AV_CODEC_ID_HEVC, | ||
| 485 | .fragment_name = "access unit", | ||
| 486 | .unit_name = "NAL unit", | ||
| 487 | .update_fragment = &h265_metadata_update_fragment, | ||
| 488 | }; | ||
| 489 | |||
| 490 | 22 | static int h265_metadata_init(AVBSFContext *bsf) | |
| 491 | { | ||
| 492 | 22 | return ff_cbs_bsf_generic_init(bsf, &h265_metadata_type); | |
| 493 | } | ||
| 494 | |||
| 495 | #define OFFSET(x) offsetof(H265MetadataContext, x) | ||
| 496 | #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM) | ||
| 497 | static const AVOption h265_metadata_options[] = { | ||
| 498 | BSF_ELEMENT_OPTIONS_PIR("aud", "Access Unit Delimiter NAL units", | ||
| 499 | aud, FLAGS), | ||
| 500 | |||
| 501 | { "sample_aspect_ratio", "Set sample aspect ratio (table E-1)", | ||
| 502 | OFFSET(sample_aspect_ratio), AV_OPT_TYPE_RATIONAL, | ||
| 503 | { .dbl = 0.0 }, 0, 65535, FLAGS }, | ||
| 504 | |||
| 505 | { "video_format", "Set video format (table E-2)", | ||
| 506 | OFFSET(video_format), AV_OPT_TYPE_INT, | ||
| 507 | { .i64 = -1 }, -1, 7, FLAGS }, | ||
| 508 | { "video_full_range_flag", "Set video full range flag", | ||
| 509 | OFFSET(video_full_range_flag), AV_OPT_TYPE_INT, | ||
| 510 | { .i64 = -1 }, -1, 1, FLAGS }, | ||
| 511 | { "colour_primaries", "Set colour primaries (table E-3)", | ||
| 512 | OFFSET(colour_primaries), AV_OPT_TYPE_INT, | ||
| 513 | { .i64 = -1 }, -1, 255, FLAGS }, | ||
| 514 | { "transfer_characteristics", "Set transfer characteristics (table E-4)", | ||
| 515 | OFFSET(transfer_characteristics), AV_OPT_TYPE_INT, | ||
| 516 | { .i64 = -1 }, -1, 255, FLAGS }, | ||
| 517 | { "matrix_coefficients", "Set matrix coefficients (table E-5)", | ||
| 518 | OFFSET(matrix_coefficients), AV_OPT_TYPE_INT, | ||
| 519 | { .i64 = -1 }, -1, 255, FLAGS }, | ||
| 520 | |||
| 521 | { "chroma_sample_loc_type", "Set chroma sample location type (figure E-1)", | ||
| 522 | OFFSET(chroma_sample_loc_type), AV_OPT_TYPE_INT, | ||
| 523 | { .i64 = -1 }, -1, 5, FLAGS }, | ||
| 524 | |||
| 525 | { "tick_rate", | ||
| 526 | "Set VPS and VUI tick rate (time_scale / num_units_in_tick)", | ||
| 527 | OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL, | ||
| 528 | { .dbl = 0.0 }, 0, UINT_MAX, FLAGS }, | ||
| 529 | { "num_ticks_poc_diff_one", | ||
| 530 | "Set VPS and VUI number of ticks per POC increment", | ||
| 531 | OFFSET(num_ticks_poc_diff_one), AV_OPT_TYPE_INT, | ||
| 532 | { .i64 = -1 }, -1, INT_MAX, FLAGS }, | ||
| 533 | |||
| 534 | { "crop_left", "Set left border crop offset", | ||
| 535 | OFFSET(crop_left), AV_OPT_TYPE_INT, | ||
| 536 | { .i64 = -1 }, -1, HEVC_MAX_WIDTH, FLAGS }, | ||
| 537 | { "crop_right", "Set right border crop offset", | ||
| 538 | OFFSET(crop_right), AV_OPT_TYPE_INT, | ||
| 539 | { .i64 = -1 }, -1, HEVC_MAX_WIDTH, FLAGS }, | ||
| 540 | { "crop_top", "Set top border crop offset", | ||
| 541 | OFFSET(crop_top), AV_OPT_TYPE_INT, | ||
| 542 | { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS }, | ||
| 543 | { "crop_bottom", "Set bottom border crop offset", | ||
| 544 | OFFSET(crop_bottom), AV_OPT_TYPE_INT, | ||
| 545 | { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS }, | ||
| 546 | { "width", "Set width after crop", | ||
| 547 | OFFSET(width), AV_OPT_TYPE_INT, | ||
| 548 | { .i64 = -1 }, -1, HEVC_MAX_WIDTH, FLAGS }, | ||
| 549 | { "height", "Set height after crop", | ||
| 550 | OFFSET(height), AV_OPT_TYPE_INT, | ||
| 551 | { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS }, | ||
| 552 | |||
| 553 | { "level", "Set level (tables A.6 and A.7)", | ||
| 554 | OFFSET(level), AV_OPT_TYPE_INT, | ||
| 555 | { .i64 = LEVEL_UNSET }, LEVEL_UNSET, 0xff, FLAGS, .unit = "level" }, | ||
| 556 | { "auto", "Attempt to guess level from stream properties", | ||
| 557 | 0, AV_OPT_TYPE_CONST, | ||
| 558 | { .i64 = LEVEL_AUTO }, .flags = FLAGS, .unit = "level" }, | ||
| 559 | #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \ | ||
| 560 | { .i64 = value }, .flags = FLAGS, .unit = "level" | ||
| 561 | { LEVEL("1", 30) }, | ||
| 562 | { LEVEL("2", 60) }, | ||
| 563 | { LEVEL("2.1", 63) }, | ||
| 564 | { LEVEL("3", 90) }, | ||
| 565 | { LEVEL("3.1", 93) }, | ||
| 566 | { LEVEL("4", 120) }, | ||
| 567 | { LEVEL("4.1", 123) }, | ||
| 568 | { LEVEL("5", 150) }, | ||
| 569 | { LEVEL("5.1", 153) }, | ||
| 570 | { LEVEL("5.2", 156) }, | ||
| 571 | { LEVEL("6", 180) }, | ||
| 572 | { LEVEL("6.1", 183) }, | ||
| 573 | { LEVEL("6.2", 186) }, | ||
| 574 | { LEVEL("8.5", 255) }, | ||
| 575 | #undef LEVEL | ||
| 576 | |||
| 577 | { NULL } | ||
| 578 | }; | ||
| 579 | |||
| 580 | static const AVClass h265_metadata_class = { | ||
| 581 | .class_name = "h265_metadata_bsf", | ||
| 582 | .item_name = av_default_item_name, | ||
| 583 | .option = h265_metadata_options, | ||
| 584 | .version = LIBAVUTIL_VERSION_INT, | ||
| 585 | }; | ||
| 586 | |||
| 587 | static const enum AVCodecID h265_metadata_codec_ids[] = { | ||
| 588 | AV_CODEC_ID_HEVC, AV_CODEC_ID_NONE, | ||
| 589 | }; | ||
| 590 | |||
| 591 | const FFBitStreamFilter ff_hevc_metadata_bsf = { | ||
| 592 | .p.name = "hevc_metadata", | ||
| 593 | .p.codec_ids = h265_metadata_codec_ids, | ||
| 594 | .p.priv_class = &h265_metadata_class, | ||
| 595 | .priv_data_size = sizeof(H265MetadataContext), | ||
| 596 | .init = &h265_metadata_init, | ||
| 597 | .close = &ff_cbs_bsf_generic_close, | ||
| 598 | .filter = &ff_cbs_bsf_generic_filter, | ||
| 599 | }; | ||
| 600 |