1/************************************************************************** 2 * 3 * Copyright 2020 Advanced Micro Devices, Inc. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28#include "pipe/p_video_codec.h" 29#include "util/u_memory.h" 30#include "util/u_video.h" 31#include "vl/vl_video_buffer.h" 32 33#include "entrypoint.h" 34#include "vid_dec.h" 35#include "vid_dec_av1.h" 36 37static unsigned av1_f(struct vl_vlc *vlc, unsigned n) 38{ 39 unsigned valid = vl_vlc_valid_bits(vlc); 40 41 if (n == 0) 42 return 0; 43 44 if (valid < 32) 45 vl_vlc_fillbits(vlc); 46 47 return vl_vlc_get_uimsbf(vlc, n); 48} 49 50static unsigned av1_uvlc(struct vl_vlc *vlc) 51{ 52 unsigned value; 53 unsigned leadingZeros = 0; 54 55 while (1) { 56 bool done = av1_f(vlc, 1); 57 if (done) 58 break; 59 leadingZeros++; 60 } 61 62 if (leadingZeros >= 32) 63 return 0xffffffff; 64 65 value = av1_f(vlc, leadingZeros); 66 67 return value + (1 << leadingZeros) - 1; 68} 69 70static int av1_le(struct vl_vlc *vlc, const unsigned n) 71{ 72 unsigned byte, t = 0; 73 unsigned i; 74 75 for (i = 0; i < n; ++i) { 76 byte = av1_f(vlc, 8); 77 t += (byte << (i * 8)); 78 } 79 80 return t; 81} 82 83static unsigned av1_uleb128(struct vl_vlc *vlc) 84{ 85 unsigned value = 0; 86 unsigned leb128Bytes = 0; 87 unsigned i; 88 89 for (i = 0; i < 8; ++i) { 90 leb128Bytes = av1_f(vlc, 8); 91 value |= ((leb128Bytes & 0x7f) << (i * 7)); 92 if (!(leb128Bytes & 0x80)) 93 break; 94 } 95 96 return value; 97} 98 99static int av1_su(struct vl_vlc *vlc, const unsigned n) 100{ 101 unsigned value = av1_f(vlc, n); 102 unsigned signMask = 1 << (n - 1); 103 104 if (value && signMask) 105 value = value - 2 * signMask; 106 107 return value; 108} 109 110static unsigned FloorLog2(unsigned x) 111{ 112 unsigned s = 0; 113 unsigned x1 = x; 114 115 while (x1 != 0) { 116 x1 = x1 >> 1; 117 s++; 118 } 119 120 return s - 1; 121} 122 123static unsigned av1_ns(struct vl_vlc *vlc, unsigned n) 124{ 125 unsigned w = FloorLog2(n) + 1; 126 unsigned m = (1 << w) - n; 127 unsigned v = av1_f(vlc, w - 1); 128 129 if (v < m) 130 return v; 131 132 bool extra_bit = av1_f(vlc, 1); 133 134 return (v << 1) - m + extra_bit; 135} 136 137static void av1_byte_alignment(struct vl_vlc *vlc) 138{ 139 vl_vlc_eatbits(vlc, vl_vlc_valid_bits(vlc) % 8); 140} 141 142static void sequence_header_obu(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 143{ 144 struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq); 145 bool timing_info_present_flag; 146 bool initial_display_delay_present_flag; 147 uint8_t seq_level_idx; 148 bool initial_display_delay_present_for_this_op; 149 bool high_bitdepth; 150 bool twelve_bit; 151 bool color_description_present_flag; 152 uint8_t color_primaries; 153 uint8_t transfer_characteristics; 154 uint8_t matrix_coefficients; 155 int i; 156 157 seq->seq_profile = av1_f(vlc, 3); 158 assert(seq->seq_profile < 3); 159 160 av1_f(vlc, 1); /* still_picture */ 161 seq->reduced_still_picture_header = av1_f(vlc, 1); 162 if (seq->reduced_still_picture_header) { 163 timing_info_present_flag = 0; 164 seq->decoder_model_info_present_flag = 0; 165 initial_display_delay_present_flag = 0; 166 seq->operating_points_cnt_minus_1 = 0; 167 seq->operating_point_idc[0] = 0; 168 seq_level_idx = av1_f(vlc, 5); 169 seq->decoder_model_present_for_this_op[0] = 0; 170 initial_display_delay_present_for_this_op = 0; 171 } else { 172 uint8_t buffer_delay_length_minus_1 = 0; 173 174 timing_info_present_flag = av1_f(vlc, 1); 175 if (timing_info_present_flag) { 176 av1_f(vlc, 32); /* num_units_in_display_tick */ 177 av1_f(vlc, 32); /* time_scale */ 178 seq->timing_info.equal_picture_interval = av1_f(vlc, 1); 179 if (seq->timing_info.equal_picture_interval) 180 av1_uvlc(vlc); /* num_ticks_per_picture_minus_1 */ 181 182 seq->decoder_model_info_present_flag = av1_f(vlc, 1); 183 if (seq->decoder_model_info_present_flag) { 184 /* decoder_model_info */ 185 buffer_delay_length_minus_1 = av1_f(vlc, 5); 186 seq->decoder_model_info.num_units_in_decoding_tick = av1_f(vlc, 32); 187 seq->decoder_model_info.buffer_removal_time_length_minus_1 = av1_f(vlc, 5); 188 seq->decoder_model_info.frame_presentation_time_length_minus_1 = av1_f(vlc, 5); 189 } 190 } else { 191 seq->decoder_model_info_present_flag = 0; 192 } 193 194 initial_display_delay_present_flag = av1_f(vlc, 1); 195 seq->operating_points_cnt_minus_1 = av1_f(vlc, 5); 196 for (i = 0; i < seq->operating_points_cnt_minus_1 + 1; ++i) { 197 seq->operating_point_idc[i] = av1_f(vlc, 12); 198 seq_level_idx = av1_f(vlc, 5); 199 if (seq_level_idx > 7) 200 av1_f(vlc, 1); /* seq_tier */ 201 202 if (seq->decoder_model_info_present_flag) { 203 seq->decoder_model_present_for_this_op[i] = av1_f(vlc, 1); 204 if (seq->decoder_model_present_for_this_op[i]) { 205 uint8_t n = buffer_delay_length_minus_1 + 1; 206 av1_f(vlc, n); /* decoder_buffer_delay */ 207 av1_f(vlc, n); /* encoder_buffer_delay */ 208 av1_f(vlc, 1); /* low_delay_mode_flag */ 209 } 210 } else { 211 seq->decoder_model_present_for_this_op[i] = 0; 212 } 213 214 if (initial_display_delay_present_flag) { 215 initial_display_delay_present_for_this_op = av1_f(vlc, 1); 216 if (initial_display_delay_present_for_this_op) 217 av1_f(vlc, 4); /* initial_display_delay_minus_1 */ 218 } 219 } 220 } 221 222 seq->frame_width_bits_minus_1 = av1_f(vlc, 4); 223 seq->frame_height_bits_minus_1 = av1_f(vlc, 4); 224 seq->max_frame_width_minus_1 = av1_f(vlc, seq->frame_width_bits_minus_1 + 1); 225 seq->max_frame_height_minus_1 = av1_f(vlc, seq->frame_height_bits_minus_1 + 1); 226 227 if (seq->reduced_still_picture_header) 228 seq->frame_id_numbers_present_flag = 0; 229 else 230 seq->frame_id_numbers_present_flag = av1_f(vlc, 1); 231 if (seq->frame_id_numbers_present_flag) { 232 seq->delta_frame_id_length_minus_2 = av1_f(vlc, 4); 233 seq->additional_frame_id_length_minus_1 = av1_f(vlc, 3); 234 } 235 236 seq->use_128x128_superblock = av1_f(vlc, 1); 237 seq->enable_filter_intra = av1_f(vlc, 1); 238 seq->enable_intra_edge_filter = av1_f(vlc, 1); 239 if (seq->reduced_still_picture_header) { 240 seq->enable_interintra_compound = 0; 241 seq->enable_masked_compound = 0; 242 seq->enable_warped_motion = 0; 243 seq->enable_dual_filter = 0; 244 seq->enable_order_hint = 0; 245 seq->enable_jnt_comp = 0; 246 seq->enable_ref_frame_mvs = 0; 247 seq->seq_force_screen_content_tools = AV1_SELECT_SCREEN_CONTENT_TOOLS; 248 seq->seq_force_integer_mv = AV1_SELECT_INTEGER_MV; 249 seq->OrderHintBits = 0; 250 } else { 251 bool seq_choose_screen_content_tools; 252 seq->enable_interintra_compound = av1_f(vlc, 1); 253 seq->enable_masked_compound = av1_f(vlc, 1); 254 seq->enable_warped_motion = av1_f(vlc, 1); 255 seq->enable_dual_filter = av1_f(vlc, 1); 256 seq->enable_order_hint = av1_f(vlc, 1); 257 if (seq->enable_order_hint) { 258 seq->enable_jnt_comp = av1_f(vlc, 1); 259 seq->enable_ref_frame_mvs = av1_f(vlc, 1); 260 } else { 261 seq->enable_jnt_comp = 0; 262 seq->enable_ref_frame_mvs = 0; 263 } 264 265 seq_choose_screen_content_tools = av1_f(vlc, 1); 266 seq->seq_force_screen_content_tools = 267 seq_choose_screen_content_tools ? AV1_SELECT_SCREEN_CONTENT_TOOLS : av1_f(vlc, 1); 268 269 if (seq->seq_force_screen_content_tools > 0) { 270 bool seq_choose_integer_mv = av1_f(vlc, 1); 271 seq->seq_force_integer_mv = 272 seq_choose_integer_mv ? AV1_SELECT_INTEGER_MV : av1_f(vlc, 1); 273 } else { 274 seq->seq_force_integer_mv = AV1_SELECT_INTEGER_MV; 275 } 276 277 if (seq->enable_order_hint) { 278 seq->order_hint_bits_minus_1 = av1_f(vlc, 3); 279 seq->OrderHintBits = seq->order_hint_bits_minus_1 + 1; 280 } else { 281 seq->OrderHintBits = 0; 282 } 283 } 284 285 seq->enable_superres = av1_f(vlc, 1); 286 seq->enable_cdef = av1_f(vlc, 1); 287 seq->enable_restoration = av1_f(vlc, 1); 288 289 high_bitdepth = av1_f(vlc, 1); 290 if (seq->seq_profile == 2 && high_bitdepth) { 291 twelve_bit = av1_f(vlc, 1); 292 seq->color_config.BitDepth = twelve_bit ? 12 : 10; 293 } else if (seq->seq_profile <= 2) { 294 seq->color_config.BitDepth = high_bitdepth ? 10 : 8; 295 } 296 297 seq->color_config.mono_chrome = (seq->seq_profile == 1) ? 0 : av1_f(vlc, 1); 298 seq->color_config.NumPlanes = seq->color_config.mono_chrome ? 1 : 3; 299 300 color_description_present_flag = av1_f(vlc, 1); 301 if (color_description_present_flag) { 302 color_primaries = av1_f(vlc, 8); 303 transfer_characteristics = av1_f(vlc, 8); 304 matrix_coefficients = av1_f(vlc, 8); 305 } else { 306 color_primaries = AV1_CP_UNSPECIFIED; 307 transfer_characteristics = AV1_TC_UNSPECIFIED; 308 matrix_coefficients = AV1_MC_UNSPECIFIED; 309 } 310 311 if (seq->color_config.mono_chrome) { 312 av1_f(vlc, 1); /* color_range */ 313 seq->color_config.subsampling_x = 1; 314 seq->color_config.subsampling_y = 1; 315 seq->color_config.separate_uv_delta_q = 0; 316 } else if (color_primaries == AV1_CP_BT_709 && 317 transfer_characteristics == AV1_TC_SRGB && 318 matrix_coefficients == AV1_MC_IDENTITY) { 319 seq->color_config.subsampling_x = 0; 320 seq->color_config.subsampling_y = 0; 321 } else { 322 av1_f(vlc, 1); /* color_range */ 323 if (seq->seq_profile == 0) { 324 seq->color_config.subsampling_x = 1; 325 seq->color_config.subsampling_y = 1; 326 } else if (seq->seq_profile == 1 ) { 327 seq->color_config.subsampling_x = 0; 328 seq->color_config.subsampling_y = 0; 329 } else { 330 if (seq->color_config.BitDepth == 12) { 331 seq->color_config.subsampling_x = av1_f(vlc, 1); 332 if (seq->color_config.subsampling_x) 333 seq->color_config.subsampling_y = av1_f(vlc, 1); 334 else 335 seq->color_config.subsampling_y = 0; 336 } else { 337 seq->color_config.subsampling_x = 1; 338 seq->color_config.subsampling_y = 0; 339 } 340 } 341 if (seq->color_config.subsampling_x && seq->color_config.subsampling_y) 342 av1_f(vlc, 2); /* chroma_sample_position */ 343 } 344 if (!seq->color_config.mono_chrome) 345 seq->color_config.separate_uv_delta_q = av1_f(vlc, 1); 346 347 seq->film_grain_params_present = av1_f(vlc, 1); 348 349 priv->picture.av1.picture_parameter.profile = seq->seq_profile; 350 priv->picture.av1.picture_parameter.seq_info_fields.use_128x128_superblock = 351 seq->use_128x128_superblock; 352 priv->picture.av1.picture_parameter.seq_info_fields.enable_filter_intra = 353 seq->enable_filter_intra; 354 priv->picture.av1.picture_parameter.seq_info_fields.enable_intra_edge_filter = 355 seq->enable_intra_edge_filter; 356 priv->picture.av1.picture_parameter.order_hint_bits_minus_1 = 357 seq->order_hint_bits_minus_1; 358 priv->picture.av1.picture_parameter.max_width = seq->max_frame_width_minus_1 + 1; 359 priv->picture.av1.picture_parameter.max_height = seq->max_frame_height_minus_1 + 1; 360 priv->picture.av1.picture_parameter.seq_info_fields.enable_interintra_compound = 361 seq->enable_interintra_compound; 362 priv->picture.av1.picture_parameter.seq_info_fields.enable_masked_compound = 363 seq->enable_masked_compound; 364 priv->picture.av1.picture_parameter.seq_info_fields.enable_dual_filter = 365 seq->enable_dual_filter; 366 priv->picture.av1.picture_parameter.seq_info_fields.enable_order_hint = 367 seq->enable_order_hint; 368 priv->picture.av1.picture_parameter.seq_info_fields.enable_jnt_comp = 369 seq->enable_jnt_comp; 370 priv->picture.av1.picture_parameter.seq_info_fields.ref_frame_mvs = 371 seq->enable_ref_frame_mvs; 372 priv->picture.av1.picture_parameter.bit_depth_idx = 373 (seq->color_config.BitDepth - 8) >> 1; 374 priv->picture.av1.picture_parameter.seq_info_fields.mono_chrome = 375 seq->color_config.mono_chrome; 376} 377 378static void superres_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 379{ 380 struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq); 381 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 382 unsigned coded_denom; 383 384 if (seq->enable_superres) 385 hdr->use_superres = av1_f(vlc, 1); 386 else 387 hdr->use_superres = 0; 388 389 if (hdr->use_superres) { 390 coded_denom = av1_f(vlc, 3 /* SUPERRES_DENOM_BITS */); 391 hdr->SuperresDenom = coded_denom + 9 /* SUPERRES_DENOM_MIN */; 392 } else { 393 hdr->SuperresDenom = 8 /* SUPERRES_NUM */; 394 } 395 396 hdr->UpscaledWidth = hdr->FrameWidth; 397 hdr->FrameWidth = (hdr->UpscaledWidth * 8 + (hdr->SuperresDenom / 2)) / 398 hdr->SuperresDenom; 399} 400 401static void compute_image_size(vid_dec_PrivateType *priv) 402{ 403 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 404 405 hdr->MiCols = 2 * ((hdr->FrameWidth + 7) >> 3); 406 hdr->MiRows = 2 * ((hdr->FrameHeight + 7) >> 3); 407} 408 409static void frame_size(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 410{ 411 struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq); 412 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 413 unsigned frame_width_minus_1; 414 unsigned frame_height_minus_1; 415 416 if (hdr->frame_size_override_flag) { 417 frame_width_minus_1 = av1_f(vlc, seq->frame_width_bits_minus_1 + 1); 418 frame_height_minus_1 = av1_f(vlc, seq->frame_height_bits_minus_1 + 1); 419 hdr->FrameWidth = frame_width_minus_1 + 1; 420 hdr->FrameHeight = frame_height_minus_1 + 1; 421 } else { 422 hdr->FrameWidth = seq->max_frame_width_minus_1 + 1; 423 hdr->FrameHeight = seq->max_frame_height_minus_1 + 1; 424 } 425 426 superres_params(priv, vlc); 427 compute_image_size(priv); 428} 429 430static void render_size(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 431{ 432 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 433 bool render_and_frame_size_different; 434 unsigned render_width_minus_1; 435 unsigned render_height_minus_1; 436 437 render_and_frame_size_different = av1_f(vlc, 1); 438 if (render_and_frame_size_different) { 439 render_width_minus_1 = av1_f(vlc, 16); 440 render_height_minus_1 = av1_f(vlc, 16); 441 hdr->RenderWidth = render_width_minus_1 + 1; 442 hdr->RenderHeight = render_height_minus_1 + 1; 443 } else { 444 hdr->RenderWidth = hdr->UpscaledWidth; 445 hdr->RenderHeight = hdr->FrameHeight; 446 } 447} 448 449static int get_relative_dist(vid_dec_PrivateType *priv, int a, int b) 450{ 451 struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq); 452 int diff; 453 unsigned m; 454 455 if (!seq->enable_order_hint) 456 return 0; 457 458 diff = a - b; 459 m = 1 << (seq->OrderHintBits - 1); 460 diff = (diff & (m - 1)) - (diff & m); 461 462 return diff; 463} 464 465static uint8_t find_latest_backward(vid_dec_PrivateType *priv) 466{ 467 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 468 uint8_t ref = 0xff; 469 unsigned latestOrderHint = 0; 470 int i; 471 472 for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) { 473 unsigned hint = hdr->shiftedOrderHints[i]; 474 if (!hdr->usedFrame[i] && 475 hint >= hdr->curFrameHint && 476 (ref == 0xff || hint >= latestOrderHint)) { 477 ref = i; 478 latestOrderHint = hint; 479 } 480 } 481 482 return ref; 483} 484 485static uint8_t find_earliest_backward(vid_dec_PrivateType *priv) 486{ 487 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 488 uint8_t ref = 0xff; 489 unsigned earliestOrderHint = 0; 490 int i; 491 492 for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) { 493 unsigned hint = hdr->shiftedOrderHints[i]; 494 if (!hdr->usedFrame[i] && 495 hint >= hdr->curFrameHint && 496 (ref == 0xff || hint < earliestOrderHint)) { 497 ref = i; 498 earliestOrderHint = hint; 499 } 500 } 501 502 return ref; 503} 504 505static uint8_t find_latest_forward(vid_dec_PrivateType *priv) 506{ 507 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 508 uint8_t ref = 0xff; 509 unsigned latestOrderHint = 0; 510 int i; 511 512 for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) { 513 unsigned hint = hdr->shiftedOrderHints[i]; 514 if (!hdr->usedFrame[i] && 515 hint < hdr->curFrameHint && 516 (ref == 0xff || hint >= latestOrderHint)) { 517 ref = i; 518 latestOrderHint = hint; 519 } 520 } 521 522 return ref; 523} 524 525static void set_frame_refs(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 526{ 527 struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq); 528 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 529 uint8_t Ref_Frame_List[5] = { AV1_LAST2_FRAME , AV1_LAST3_FRAME, AV1_BWDREF_FRAME, 530 AV1_ALTREF2_FRAME, AV1_ALTREF_FRAME }; 531 unsigned earliestOrderHint = 0; 532 uint8_t ref; 533 int i; 534 535 for (i = 0; i < AV1_REFS_PER_FRAME; ++i) 536 hdr->ref_frame_idx[i] = 0xff; 537 538 hdr->ref_frame_idx[0] = hdr->last_frame_idx; 539 hdr->ref_frame_idx[AV1_GOLDEN_FRAME - AV1_LAST_FRAME] = hdr->gold_frame_idx; 540 541 for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) 542 hdr->usedFrame[i] = 0; 543 544 hdr->usedFrame[hdr->last_frame_idx] = 1; 545 hdr->usedFrame[hdr->gold_frame_idx] = 1; 546 547 hdr->curFrameHint = 1 << (seq->OrderHintBits - 1); 548 549 for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) 550 hdr->shiftedOrderHints[i] = 551 hdr->curFrameHint + 552 get_relative_dist(priv, hdr->RefOrderHint[i], hdr->OrderHint); 553 554 ref = find_latest_backward(priv); 555 if (ref != 0xff) { 556 hdr->ref_frame_idx[AV1_ALTREF_FRAME - AV1_LAST_FRAME] = ref; 557 hdr->usedFrame[ref] = 1; 558 } 559 560 ref = find_earliest_backward(priv); 561 if (ref != 0xff) { 562 hdr->ref_frame_idx[AV1_BWDREF_FRAME - AV1_LAST_FRAME] = ref; 563 hdr->usedFrame[ref] = 1; 564 } 565 566 ref = find_earliest_backward(priv); 567 if (ref != 0xff) { 568 hdr->ref_frame_idx[AV1_ALTREF2_FRAME - AV1_LAST_FRAME] = ref; 569 hdr->usedFrame[ref] = 1; 570 } 571 572 for (i = 0; i < AV1_REFS_PER_FRAME - 2; ++i) { 573 uint8_t refFrame = Ref_Frame_List[i]; 574 if (hdr->ref_frame_idx[refFrame - AV1_LAST_FRAME] == 0xff) { 575 ref = find_latest_forward(priv); 576 if (ref != 0xff) { 577 hdr->ref_frame_idx[refFrame - AV1_LAST_FRAME] = ref; 578 hdr->usedFrame[ref] = 1; 579 } 580 } 581 } 582 583 ref = 0xff; 584 for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) { 585 unsigned hint = hdr->shiftedOrderHints[i]; 586 if (ref == 0xff || hint < earliestOrderHint) { 587 ref = i; 588 earliestOrderHint = hint; 589 } 590 } 591 592 for (i = 0; i < AV1_REFS_PER_FRAME; ++i) { 593 if (hdr->ref_frame_idx[i] == 0xff) 594 hdr->ref_frame_idx[i] = ref; 595 } 596} 597 598static void frame_size_with_refs(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 599{ 600 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 601 bool found_ref; 602 int i; 603 604 for (i = 0; i < AV1_REFS_PER_FRAME; ++i) { 605 found_ref = av1_f(vlc, 1); 606 if (found_ref) { 607 hdr->UpscaledWidth = 608 priv->codec_data.av1.RefFrames[hdr->ref_frame_idx[i]].RefUpscaledWidth; 609 hdr->FrameWidth = hdr->UpscaledWidth; 610 hdr->FrameHeight = 611 priv->codec_data.av1.RefFrames[hdr->ref_frame_idx[i]].RefFrameHeight; 612 hdr->RenderWidth = 613 priv->codec_data.av1.RefFrames[hdr->ref_frame_idx[i]].RefRenderWidth; 614 hdr->RenderHeight = 615 priv->codec_data.av1.RefFrames[hdr->ref_frame_idx[i]].RefRenderHeight; 616 break; 617 } 618 } 619 620 if (!found_ref) { 621 frame_size(priv, vlc); 622 render_size(priv, vlc); 623 } else { 624 superres_params(priv, vlc); 625 compute_image_size(priv); 626 } 627} 628 629static unsigned tile_log2(unsigned blkSize, unsigned target) 630{ 631 unsigned k = 0; 632 633 for (k = 0; (blkSize << k) < target; k++); 634 635 return k; 636} 637 638static void tile_info(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 639{ 640 struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq); 641 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 642 struct tile_info *ti = &(priv->codec_data.av1.uncompressed_header.ti); 643 unsigned sbCols; 644 unsigned sbRows; 645 int width_sb; 646 int height_sb; 647 unsigned sbSize; 648 unsigned maxTileWidthSb; 649 unsigned minLog2TileCols; 650 unsigned maxLog2TileCols; 651 unsigned maxLog2TileRows; 652 unsigned minLog2Tiles; 653 bool uniform_tile_spacing_flag; 654 unsigned maxTileAreaSb; 655 unsigned startSb, i; 656 657 sbCols = (seq->use_128x128_superblock) ? 658 ((hdr->MiCols + 31) >> 5) : ((hdr->MiCols + 15) >> 4); 659 sbRows = (seq->use_128x128_superblock) ? 660 ((hdr->MiRows + 31) >> 5) : ((hdr->MiRows + 15) >> 4); 661 width_sb = sbCols; 662 height_sb = sbRows; 663 sbSize = (seq->use_128x128_superblock ? 5 : 4) + 2; 664 maxTileWidthSb = AV1_MAX_TILE_WIDTH >> sbSize; 665 maxTileAreaSb = AV1_MAX_TILE_AREA >> (2 * sbSize); 666 minLog2TileCols = tile_log2(maxTileWidthSb, sbCols); 667 maxLog2TileCols = tile_log2(1, MIN2(sbCols, AV1_MAX_TILE_COLS)); 668 maxLog2TileRows = tile_log2(1, MIN2(sbRows, AV1_MAX_TILE_ROWS)); 669 minLog2Tiles = MAX2(minLog2TileCols, tile_log2(maxTileAreaSb, sbRows * sbCols)); 670 671 uniform_tile_spacing_flag = av1_f(vlc, 1); 672 if (uniform_tile_spacing_flag) { 673 unsigned tileWidthSb, tileHeightSb; 674 unsigned minLog2TileRows; 675 676 ti->TileColsLog2 = minLog2TileCols; 677 while (ti->TileColsLog2 < maxLog2TileCols) { 678 bool increment_tile_cols_log2 = av1_f(vlc, 1); 679 if (increment_tile_cols_log2) 680 ti->TileColsLog2++; 681 else 682 break; 683 } 684 tileWidthSb = (sbCols + (1 << ti->TileColsLog2) - 1) >> ti->TileColsLog2; 685 i = 0; 686 for (startSb = 0; startSb < sbCols; startSb += tileWidthSb) { 687 ti->tile_col_start_sb[i] = startSb; 688 i++; 689 } 690 ti->tile_col_start_sb[i] = sbCols; 691 ti->TileCols = i; 692 693 minLog2TileRows = (minLog2Tiles > ti->TileColsLog2)? 694 (minLog2Tiles - ti->TileColsLog2) : 0; 695 ti->TileRowsLog2 = minLog2TileRows; 696 while (ti->TileRowsLog2 < maxLog2TileRows) { 697 bool increment_tile_rows_log2 = av1_f(vlc, 1); 698 if (increment_tile_rows_log2) 699 ti->TileRowsLog2++; 700 else 701 break; 702 } 703 tileHeightSb = (sbRows + (1 << ti->TileRowsLog2) - 1) >> ti->TileRowsLog2; 704 i = 0; 705 for (startSb = 0; startSb < sbRows; startSb += tileHeightSb) { 706 ti->tile_row_start_sb[i] = startSb; 707 i++; 708 } 709 ti->tile_row_start_sb[i] = sbRows; 710 ti->TileRows = i; 711 } else { 712 unsigned widestTileSb = 0; 713 unsigned maxTileHeightSb; 714 715 startSb = 0; 716 for (i = 0; startSb < sbCols; ++i) { 717 uint8_t maxWidth; 718 unsigned sizeSb; 719 unsigned width_in_sbs_minus_1; 720 721 ti->tile_col_start_sb[i] = startSb; 722 maxWidth = MIN2(sbCols - startSb, maxTileWidthSb); 723 width_in_sbs_minus_1 = av1_ns(vlc, maxWidth); 724 sizeSb = width_in_sbs_minus_1 + 1; 725 widestTileSb = MAX2(sizeSb, widestTileSb); 726 startSb += sizeSb; 727 width_sb -= sizeSb; 728 } 729 ti->TileCols = i; 730 731 ti->tile_col_start_sb[i] = startSb + width_sb; 732 ti->TileColsLog2 = tile_log2(1, ti->TileCols); 733 734 if (minLog2Tiles > 0) 735 maxTileAreaSb = (sbRows * sbCols) >> (minLog2Tiles + 1); 736 else 737 maxTileAreaSb = (sbRows * sbCols); 738 maxTileHeightSb = MAX2(maxTileAreaSb / widestTileSb, 1); 739 740 startSb = 0; 741 for (i = 0; startSb < sbRows; ++i) { 742 uint8_t maxHeight; 743 unsigned height_in_sbs_minus_1; 744 745 maxHeight = MIN2(sbRows - startSb, maxTileHeightSb); 746 height_in_sbs_minus_1 = av1_ns(vlc, maxHeight); 747 ti->tile_row_start_sb[i] = startSb; 748 startSb += height_in_sbs_minus_1 + 1; 749 height_sb -= height_in_sbs_minus_1 + 1; 750 } 751 ti->TileRows = i; 752 ti->tile_row_start_sb[i] = startSb + height_sb; 753 ti->TileRowsLog2 = tile_log2(1, ti->TileRows); 754 } 755 756 if (ti->TileColsLog2 > 0 || ti->TileRowsLog2 > 0) { 757 ti->context_update_tile_id = 758 av1_f(vlc, ti->TileRowsLog2 + ti->TileColsLog2); 759 uint8_t tile_size_bytes_minus_1 = av1_f(vlc, 2); 760 ti->TileSizeBytes = tile_size_bytes_minus_1 + 1; 761 } else { 762 ti->context_update_tile_id = 0; 763 } 764} 765 766static int read_delta_q(struct vl_vlc *vlc) 767{ 768 bool delta_coded = av1_f(vlc, 1); 769 int delta_q = 0; 770 771 if (delta_coded) 772 delta_q = av1_su(vlc, 7); 773 774 return delta_q; 775} 776 777static void quantization_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 778{ 779 struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq); 780 struct quantization_params* qp = &(priv->codec_data.av1.uncompressed_header.qp); 781 bool using_qmatrix; 782 783 qp->base_q_idx = av1_f(vlc, 8); 784 qp->DeltaQYDc = read_delta_q(vlc); 785 if (seq->color_config.NumPlanes > 1) { 786 bool diff_uv_delta = 787 (seq->color_config.separate_uv_delta_q) ? av1_f(vlc, 1) : 0; 788 789 qp->DeltaQUDc = read_delta_q(vlc); 790 qp->DeltaQUAc = read_delta_q(vlc); 791 if (diff_uv_delta) { 792 qp->DeltaQVDc = read_delta_q(vlc); 793 qp->DeltaQVAc = read_delta_q(vlc); 794 } else { 795 qp->DeltaQVDc = qp->DeltaQUDc; 796 qp->DeltaQVAc = qp->DeltaQUAc; 797 } 798 } else { 799 qp->DeltaQVDc = 0; 800 qp->DeltaQVAc = 0; 801 qp->DeltaQUDc = 0; 802 qp->DeltaQUAc = 0; 803 } 804 805 using_qmatrix = av1_f(vlc, 1); 806 if (using_qmatrix) { 807 qp->qm_y = av1_f(vlc, 4); 808 qp->qm_u = av1_f(vlc, 4); 809 if (!seq->color_config.separate_uv_delta_q) 810 qp->qm_v = qp->qm_u; 811 else 812 qp->qm_v = av1_f(vlc, 4); 813 } else { 814 qp->qm_y = 0xf; 815 qp->qm_u = 0xf; 816 qp->qm_v = 0xf; 817 } 818} 819 820static void segmentation_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 821{ 822 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 823 struct segmentation_params* sp = &(priv->codec_data.av1.uncompressed_header.sp); 824 int i, j; 825 826 sp->segmentation_enabled = av1_f(vlc, 1); 827 if (sp->segmentation_enabled) { 828 bool segmentation_update_data; 829 830 if (hdr->primary_ref_frame == AV1_PRIMARY_REF_NONE) { 831 sp->segmentation_update_map = 1; 832 sp->segmentation_temporal_update = 0; 833 segmentation_update_data = 1; 834 } else { 835 sp->segmentation_update_map = av1_f(vlc, 1); 836 if (sp->segmentation_update_map) 837 sp->segmentation_temporal_update = av1_f(vlc, 1); 838 else 839 sp->segmentation_temporal_update = 0; 840 segmentation_update_data = av1_f(vlc, 1); 841 } 842 843 if (segmentation_update_data) { 844 uint8_t Segmentation_Feature_Bits[AV1_SEG_LVL_MAX] = { 8, 6, 6, 6, 6, 3, 0, 0 }; 845 bool Segmentation_Feature_Signed[AV1_SEG_LVL_MAX] = { 1, 1, 1, 1, 1, 0, 0, 0 }; 846 unsigned Segmentation_Feature_Max[AV1_SEG_LVL_MAX] = { 255, 63, 63, 63, 63, 7, 0, 0 }; 847 848 memset(sp->FeatureData, 0, sizeof(sp->FeatureData)); 849 memset(sp->FeatureMask, 0, sizeof(sp->FeatureMask)); 850 for (i = 0; i < AV1_MAX_SEGMENTS; ++i) { 851 for (j = 0; j < AV1_SEG_LVL_MAX; ++j) { 852 int feature_value = 0; 853 bool feature_enabled = av1_f(vlc, 1); 854 855 sp->FeatureEnabled[i][j] = feature_enabled; 856 int clippedValue = 0; 857 if (feature_enabled) { 858 uint8_t bitsToRead = Segmentation_Feature_Bits[j]; 859 int limit = Segmentation_Feature_Max[j]; 860 if (Segmentation_Feature_Signed[j]) { 861 feature_value = av1_su(vlc, 1 + bitsToRead); 862 clippedValue = CLAMP(feature_value, -limit, limit); 863 sp->FeatureMask[i] |= 1 << j; 864 } else { 865 feature_value = av1_f(vlc, bitsToRead); 866 clippedValue = CLAMP(feature_value, 0, limit); 867 sp->FeatureMask[i] |= 1 << j; 868 } 869 } 870 sp->FeatureData[i][j] = clippedValue; 871 } 872 } 873 } else { 874 int r = hdr->ref_frame_idx[hdr->primary_ref_frame]; 875 memcpy(sp, &(priv->codec_data.av1.refs[r].sp), sizeof(*sp)); 876 } 877 } else { 878 memset(sp, 0, sizeof(*sp)); 879 } 880} 881 882static void delta_q_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 883{ 884 struct quantization_params* qp = &(priv->codec_data.av1.uncompressed_header.qp); 885 struct delta_q_params * dqp = &(priv->codec_data.av1.uncompressed_header.dqp); 886 887 dqp->delta_q_present = 0; 888 dqp->delta_q_res = 0; 889 if (qp->base_q_idx > 0) 890 dqp->delta_q_present = av1_f(vlc, 1); 891 if (dqp->delta_q_present) 892 dqp->delta_q_res = av1_f(vlc, 2); 893} 894 895static void delta_lf_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 896{ 897 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 898 struct delta_q_params * dqp = &(priv->codec_data.av1.uncompressed_header.dqp); 899 struct delta_lf_params* dlfp = &(priv->codec_data.av1.uncompressed_header.dlfp); 900 901 dlfp->delta_lf_present = 0; 902 dlfp->delta_lf_res = 0; 903 dlfp->delta_lf_multi = 0; 904 if (dqp->delta_q_present) { 905 if (!hdr->allow_intrabc) 906 dlfp->delta_lf_present = av1_f(vlc, 1); 907 908 if (dlfp->delta_lf_present) { 909 dlfp->delta_lf_res = av1_f(vlc, 2); 910 dlfp->delta_lf_multi = av1_f(vlc, 1); 911 } 912 } 913} 914 915static unsigned get_qindex(vid_dec_PrivateType * priv, bool ignoreDeltaQ, unsigned segmentId) 916{ 917 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 918 struct segmentation_params* sp = &(priv->codec_data.av1.uncompressed_header.sp); 919 struct quantization_params* qp = &(priv->codec_data.av1.uncompressed_header.qp); 920 unsigned qindex = 0; 921 922 if (sp->segmentation_enabled && sp->FeatureEnabled[segmentId][AV1_SEG_LVL_ALT_Q]) { 923 unsigned data = sp->FeatureData[segmentId][AV1_SEG_LVL_ALT_Q]; 924 qindex = qp->base_q_idx + data; 925 if (!ignoreDeltaQ && hdr->dqp.delta_q_present) 926 qindex = data; 927 928 return CLAMP(qindex, 0, 255); 929 } 930 931 if (!ignoreDeltaQ && hdr->dqp.delta_q_present) 932 return 0; 933 934 return qp->base_q_idx; 935} 936 937static void loop_filter_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 938{ 939 struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq); 940 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 941 struct loop_filter_params *lfp = &(priv->codec_data.av1.uncompressed_header.lfp); 942 int i; 943 944 if (hdr->CodedLossless || hdr->allow_intrabc) { 945 lfp->loop_filter_level[0] = 0; 946 lfp->loop_filter_level[1] = 0; 947 lfp->loop_filter_ref_deltas[AV1_INTRA_FRAME] = 1; 948 lfp->loop_filter_ref_deltas[AV1_LAST_FRAME] = 0; 949 lfp->loop_filter_ref_deltas[AV1_LAST2_FRAME] = 0; 950 lfp->loop_filter_ref_deltas[AV1_LAST3_FRAME] = 0; 951 lfp->loop_filter_ref_deltas[AV1_BWDREF_FRAME] = 0; 952 lfp->loop_filter_ref_deltas[AV1_GOLDEN_FRAME] = -1; 953 lfp->loop_filter_ref_deltas[AV1_ALTREF2_FRAME] = -1; 954 lfp->loop_filter_ref_deltas[AV1_ALTREF_FRAME] = -1; 955 lfp->loop_filter_mode_deltas[0] = 0; 956 lfp->loop_filter_mode_deltas[1] = 0; 957 return; 958 } 959 960 if (hdr->primary_ref_frame == AV1_PRIMARY_REF_NONE) { 961 lfp->loop_filter_ref_deltas[AV1_INTRA_FRAME] = 1; 962 lfp->loop_filter_ref_deltas[AV1_LAST_FRAME] = 0; 963 lfp->loop_filter_ref_deltas[AV1_LAST2_FRAME] = 0; 964 lfp->loop_filter_ref_deltas[AV1_LAST3_FRAME] = 0; 965 lfp->loop_filter_ref_deltas[AV1_BWDREF_FRAME] = 0; 966 lfp->loop_filter_ref_deltas[AV1_GOLDEN_FRAME] = -1; 967 lfp->loop_filter_ref_deltas[AV1_ALTREF2_FRAME] = -1; 968 lfp->loop_filter_ref_deltas[AV1_ALTREF_FRAME] = -1; 969 lfp->loop_filter_mode_deltas[0] = 0; 970 lfp->loop_filter_mode_deltas[1] = 0; 971 } else { 972 int r = hdr->ref_frame_idx[hdr->primary_ref_frame]; 973 memcpy(lfp->loop_filter_ref_deltas, 974 priv->codec_data.av1.refs[r].lfp.loop_filter_ref_deltas, 8); 975 memcpy(lfp->loop_filter_mode_deltas, 976 priv->codec_data.av1.refs[r].lfp.loop_filter_mode_deltas, 2); 977 } 978 979 lfp->loop_filter_level[0] = av1_f(vlc, 6); 980 lfp->loop_filter_level[1] = av1_f(vlc, 6); 981 if (seq->color_config.NumPlanes > 1) { 982 if (lfp->loop_filter_level[0] || lfp->loop_filter_level[1]) { 983 lfp->loop_filter_level[2] = av1_f(vlc, 6); 984 lfp->loop_filter_level[3] = av1_f(vlc, 6); 985 } 986 } 987 988 lfp->loop_filter_sharpness = av1_f(vlc, 3); 989 lfp->loop_filter_delta_enabled = av1_f(vlc, 1); 990 if (lfp->loop_filter_delta_enabled) { 991 lfp->loop_filter_delta_update = av1_f(vlc, 1); 992 if (lfp->loop_filter_delta_update) { 993 for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) { 994 int8_t update_ref_delta = av1_f(vlc, 1); 995 if (update_ref_delta) 996 lfp->loop_filter_ref_deltas[i] = av1_su(vlc, 7); 997 } 998 999 for (i = 0; i < 2; ++i) { 1000 int8_t update_mode_delta = av1_f(vlc, 1); 1001 if (update_mode_delta) 1002 lfp->loop_filter_mode_deltas[i] = av1_su(vlc, 7); 1003 } 1004 } 1005 } 1006} 1007 1008static void cdef_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 1009{ 1010 struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq); 1011 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 1012 struct cdef_params *cdefp = &(priv->codec_data.av1.uncompressed_header.cdefp);; 1013 int i; 1014 1015 if (hdr->CodedLossless || hdr->allow_intrabc || !seq->enable_cdef) { 1016 cdefp->cdef_bits = 0; 1017 cdefp->cdef_y_strengths[0] = 0; 1018 cdefp->cdef_uv_strengths[0] = 0; 1019 return; 1020 } 1021 1022 cdefp->cdef_damping_minus_3 = av1_f(vlc, 2); 1023 cdefp->cdef_bits = av1_f(vlc, 2); 1024 for (i = 0; i < (1 << cdefp->cdef_bits); ++i) { 1025 cdefp->cdef_y_strengths[i] = av1_f(vlc, 6); 1026 if (seq->color_config.NumPlanes > 1) 1027 cdefp->cdef_uv_strengths[i] = av1_f(vlc, 6); 1028 } 1029} 1030 1031static void lr_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 1032{ 1033 struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq); 1034 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 1035 struct loop_restoration_params *lrp = &(priv->codec_data.av1.uncompressed_header.lrp); 1036 uint8_t Remap_Lr_Type[4] = 1037 { AV1_RESTORE_NONE, AV1_RESTORE_SWITCHABLE, AV1_RESTORE_WIENER, AV1_RESTORE_SGRPROJ }; 1038 bool UsesLr = false; 1039 bool UsesChromaLr = false; 1040 uint8_t lr_unit_shift, lr_uv_shift; 1041 int i; 1042 1043 if (hdr->AllLossless || hdr->allow_intrabc || !seq->enable_restoration) { 1044 lrp->FrameRestorationType[0] = AV1_RESTORE_NONE; 1045 lrp->FrameRestorationType[1] = AV1_RESTORE_NONE; 1046 lrp->FrameRestorationType[2] = AV1_RESTORE_NONE; 1047 return; 1048 } 1049 1050 for (i = 0; i < seq->color_config.NumPlanes; ++i) { 1051 uint8_t lr_type = av1_f(vlc, 2); 1052 lrp->FrameRestorationType[i] = Remap_Lr_Type[lr_type]; 1053 if (lrp->FrameRestorationType[i] != AV1_RESTORE_NONE) { 1054 UsesLr = true; 1055 if (i > 0) 1056 UsesChromaLr = true; 1057 } 1058 } 1059 1060 if (UsesLr) { 1061 if (seq->use_128x128_superblock) { 1062 lr_unit_shift = av1_f(vlc, 1) + 1; 1063 } else { 1064 lr_unit_shift = av1_f(vlc, 1); 1065 if (lr_unit_shift) { 1066 uint8_t lr_unit_extra_shift = av1_f(vlc, 1); 1067 lr_unit_shift += lr_unit_extra_shift; 1068 } 1069 } 1070 1071 lrp->LoopRestorationSize[0] = AV1_RESTORATION_TILESIZE >> (2 - lr_unit_shift); 1072 lr_uv_shift = 1073 (seq->color_config.subsampling_x && seq->color_config.subsampling_y && UsesChromaLr) ? 1074 av1_f(vlc, 1) : 0; 1075 1076 lrp->LoopRestorationSize[1] = lrp->LoopRestorationSize[0] >> lr_uv_shift; 1077 lrp->LoopRestorationSize[2] = lrp->LoopRestorationSize[0] >> lr_uv_shift; 1078 } else { 1079 lrp->LoopRestorationSize[0] = lrp->LoopRestorationSize[1] = 1080 lrp->LoopRestorationSize[2] = (1 << 8); 1081 } 1082} 1083 1084static void tx_mode(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 1085{ 1086 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 1087 struct tx_mode_params *tm = &(priv->codec_data.av1.uncompressed_header.tm); 1088 1089 if (hdr->CodedLossless) { 1090 tm->TxMode = AV1_ONLY_4X4; 1091 } else { 1092 bool tx_mode_select = av1_f(vlc, 1); 1093 tm->TxMode = (tx_mode_select) ? AV1_TX_MODE_SELECT : AV1_TX_MODE_LARGEST; 1094 } 1095} 1096 1097static void frame_reference_mode(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 1098{ 1099 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 1100 1101 if (hdr->FrameIsIntra) 1102 hdr->reference_select = SINGLE_REFERENCE; 1103 else 1104 hdr->reference_select = av1_f(vlc, 1) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE; 1105} 1106 1107static void skip_mode_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 1108{ 1109 struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq); 1110 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 1111 struct skip_mode_params *smp = &(priv->codec_data.av1.uncompressed_header.smp);; 1112 bool skipModeAllowed; 1113 int i; 1114 1115 if (hdr->FrameIsIntra || hdr->reference_select == SINGLE_REFERENCE || 1116 !seq->enable_order_hint) { 1117 skipModeAllowed = 0; 1118 } else { 1119 int ref_frame_offset[2] = { -1, INT_MAX }; 1120 int ref_idx[2] = { -1, -1 }; 1121 1122 skipModeAllowed = 0; 1123 for (i = 0; i < AV1_REFS_PER_FRAME; ++i) { 1124 unsigned ref_offset = priv->codec_data.av1.refs[hdr->ref_frame_idx[i]].OrderHint; 1125 if (get_relative_dist(priv, ref_offset, hdr->OrderHint) < 0) { 1126 if (ref_frame_offset[0] == -1 || 1127 get_relative_dist(priv, ref_offset, ref_frame_offset[0]) > 0) { 1128 ref_frame_offset[0] = ref_offset; 1129 ref_idx[0] = i; 1130 } 1131 } else if (get_relative_dist(priv, ref_offset, hdr->OrderHint) > 0) { 1132 if (ref_frame_offset[1] == INT_MAX || 1133 get_relative_dist(priv, ref_offset, ref_frame_offset[1]) < 0) { 1134 ref_frame_offset[1] = ref_offset; 1135 ref_idx[1] = i; 1136 } 1137 } 1138 } 1139 1140 if (ref_idx[0] != -1 && ref_idx[1] != -1) { 1141 skipModeAllowed = 1; 1142 } else if (ref_idx[0] != -1 && ref_idx[1] == -1) { 1143 ref_frame_offset[1] = -1; 1144 for (i = 0; i < AV1_ALTREF_FRAME - AV1_LAST_FRAME + 1; ++i) { 1145 unsigned ref_offset = priv->codec_data.av1.refs[hdr->ref_frame_idx[i]].OrderHint; 1146 if ((ref_frame_offset[0] != -1 && 1147 get_relative_dist(priv, ref_offset, ref_frame_offset[0]) < 0) && 1148 (ref_frame_offset[1] == -1 || 1149 get_relative_dist(priv, ref_offset, ref_frame_offset[1]) > 0)) { 1150 ref_frame_offset[1] = ref_offset; 1151 ref_idx[1] = i; 1152 } 1153 } 1154 if (ref_frame_offset[1] != -1) 1155 skipModeAllowed = 1; 1156 } 1157 } 1158 1159 smp->skip_mode_present = skipModeAllowed ? av1_f(vlc, 1) : 0; 1160} 1161 1162static unsigned inverse_recenter(unsigned r, unsigned v) 1163{ 1164 if (v > (2 * r)) 1165 return v; 1166 else if (v & 1) 1167 return (r - ((v + 1) >> 1)); 1168 else 1169 return (r + (v >> 1)); 1170} 1171 1172static unsigned decode_subexp(struct vl_vlc *vlc, unsigned numSyms) 1173{ 1174 unsigned i = 0; 1175 unsigned mk = 0; 1176 unsigned k = 3; 1177 1178 while (1) { 1179 unsigned b2 = (i) ? (k + i - 1) : k; 1180 unsigned a = 1 << b2; 1181 if (numSyms <= (mk + 3 * a)) { 1182 unsigned subexp_final_bits = av1_ns(vlc, (numSyms - mk)); 1183 return (subexp_final_bits + mk); 1184 } else { 1185 bool subexp_more_bits = av1_f(vlc, 1); 1186 if (subexp_more_bits) { 1187 i++; 1188 mk += a; 1189 } else { 1190 unsigned subexp_bits = av1_f(vlc, b2); 1191 return (subexp_bits + mk); 1192 } 1193 } 1194 } 1195} 1196 1197static unsigned decode_unsigned_subexp_with_ref(struct vl_vlc *vlc, 1198 unsigned mx, unsigned r) 1199{ 1200 unsigned smart; 1201 unsigned v = decode_subexp(vlc, mx); 1202 1203 if ((r << 1) <= mx) { 1204 smart = inverse_recenter(r, v); 1205 return smart; 1206 } else { 1207 smart = inverse_recenter(mx - 1 - r, v); 1208 return (mx - 1 - smart); 1209 } 1210} 1211 1212static int decode_signed_subexp_with_ref(struct vl_vlc *vlc, int low, int high, int r) 1213{ 1214 int x = decode_unsigned_subexp_with_ref(vlc, high - low, r - low); 1215 1216 return (x + low); 1217} 1218 1219static void read_global_param(struct global_motion_params* global_params, 1220 struct global_motion_params* ref_params, 1221 vid_dec_PrivateType *priv, struct vl_vlc *vlc, 1222 uint8_t type, uint8_t ref, uint8_t idx) 1223{ 1224 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 1225 uint8_t absBits = 12; /* GM_ABS_ALPHA_BITS */ 1226 uint8_t precBits = 15; /* GM_ALPHA_PREC_BITS */ 1227 int precDiff, round, sub, mx, r = 0; 1228 1229 if (idx < 2) { 1230 if (type == AV1_TRANSLATION) { 1231 absBits = 9 /* GM_ABS_TRANS_ONLY_BITS */ - !hdr->allow_high_precision_mv; 1232 precBits = 3 /* GM_TRANS_ONLY_PREC_BITS */ - !hdr->allow_high_precision_mv; 1233 } else { 1234 absBits = 12; /* GM_ABS_TRANS_BITS */ 1235 precBits = 6; /* GM_TRANS_PREC_BITS */; 1236 } 1237 } 1238 1239 precDiff = AV1_WARPEDMODEL_PREC_BITS - precBits; 1240 round = ((idx % 3) == 2) ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0; 1241 sub = ((idx % 3) == 2) ? (1 << precBits) : 0; 1242 mx = (int)(1 << absBits); 1243 if (ref_params) 1244 r = (ref_params->gm_params[ref][idx] >> precDiff) - sub; 1245 1246 global_params->gm_params[ref][idx] = 1247 (decode_signed_subexp_with_ref(vlc, -mx, mx + 1, r) << precDiff) + round; 1248} 1249 1250static void global_motion_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 1251{ 1252 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 1253 struct global_motion_params *gmp = &(priv->codec_data.av1.uncompressed_header.gmp); 1254 struct global_motion_params *ref_gmp = NULL; 1255 unsigned ref, i; 1256 1257 if (hdr->primary_ref_frame == AV1_PRIMARY_REF_NONE) { 1258 for (ref = 0; ref < AV1_NUM_REF_FRAMES; ++ref) { 1259 gmp->GmType[ref] = AV1_IDENTITY; 1260 for (i = 0; i < 6; ++i) 1261 gmp->gm_params[ref][i] = (((i % 3) == 2) ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0); 1262 } 1263 } else { 1264 const int r = hdr->ref_frame_idx[hdr->primary_ref_frame]; 1265 ref_gmp = &(priv->codec_data.av1.refs[r].gmp); 1266 } 1267 1268 for (ref = AV1_LAST_FRAME; ref <= AV1_ALTREF_FRAME; ++ref) { 1269 gmp->GmType[ref] = AV1_IDENTITY; 1270 for (i = 0; i < 6; ++i) 1271 gmp->gm_params[ref][i] = (((i % 3) == 2) ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0); 1272 } 1273 1274 if (hdr->FrameIsIntra) 1275 return; 1276 1277 for (ref = AV1_LAST_FRAME; ref <= AV1_ALTREF_FRAME; ++ref) { 1278 uint8_t type = AV1_IDENTITY; 1279 bool is_global; 1280 1281 gmp->GmType[ref] = AV1_IDENTITY; 1282 for (i = 0; i < 6; ++i) 1283 gmp->gm_params[ref][i] = (((i % 3) == 2) ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0); 1284 1285 is_global = av1_f(vlc, 1); 1286 if (is_global) { 1287 bool is_rot_zoom = av1_f(vlc, 1); 1288 if (is_rot_zoom) { 1289 type = AV1_ROTZOOM; 1290 } else { 1291 bool is_translation = av1_f(vlc, 1); 1292 type = is_translation ? AV1_TRANSLATION : AV1_AFFINE; 1293 } 1294 } 1295 1296 gmp->GmType[ref] = type; 1297 1298 if (type >= AV1_ROTZOOM) { 1299 read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 2); 1300 read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 3); 1301 if (type == AV1_AFFINE) { 1302 read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 4); 1303 read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 5); 1304 } else { 1305 gmp->gm_params[ref][4] = -gmp->gm_params[ref][3]; 1306 gmp->gm_params[ref][5] = gmp->gm_params[ref][2]; 1307 } 1308 } 1309 1310 if (type >= AV1_TRANSLATION) { 1311 read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 0); 1312 read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 1); 1313 } 1314 } 1315} 1316 1317static void film_grain_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 1318{ 1319 struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq); 1320 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 1321 struct film_grain_params *fgp = &(priv->codec_data.av1.uncompressed_header.fgp); 1322 1323 bool update_grain; 1324 uint8_t numPosLuma; 1325 uint8_t numPosChroma; 1326 unsigned i; 1327 1328 if (!seq->film_grain_params_present || 1329 (!hdr->show_frame && !hdr->showable_frame)) { 1330 memset(fgp, 0, sizeof(*fgp)); 1331 return; 1332 } 1333 1334 fgp->apply_grain = av1_f(vlc, 1); 1335 if (!fgp->apply_grain) { 1336 memset(fgp, 0, sizeof(*fgp)); 1337 return; 1338 } 1339 1340 fgp->grain_seed = av1_f(vlc, 16); 1341 update_grain = 1342 (hdr->frame_type == AV1_INTER_FRAME) ? av1_f(vlc, 1) : 1; 1343 1344 if (!update_grain) { 1345 uint8_t film_grain_params_ref_idx = av1_f(vlc, 3); 1346 uint16_t tempGrainSeed = fgp->grain_seed; 1347 memcpy(fgp, &(priv->codec_data.av1.refs[film_grain_params_ref_idx].fgp), 1348 sizeof(*fgp)); 1349 fgp->grain_seed = tempGrainSeed; 1350 return; 1351 } 1352 1353 fgp->num_y_points = av1_f(vlc, 4); 1354 for (i = 0; i < fgp->num_y_points; ++i) { 1355 fgp->point_y_value[i] = av1_f(vlc, 8); 1356 fgp->point_y_scaling[i] = av1_f(vlc, 8); 1357 } 1358 1359 fgp->chroma_scaling_from_luma = 1360 (seq->color_config.mono_chrome) ? 0 : av1_f(vlc, 1); 1361 if (seq->color_config.mono_chrome || fgp->chroma_scaling_from_luma || 1362 (seq->color_config.subsampling_x && seq->color_config.subsampling_y && 1363 (fgp->num_y_points == 0))) { 1364 fgp->num_cb_points = 0; 1365 fgp->num_cr_points = 0; 1366 } else { 1367 fgp->num_cb_points = av1_f(vlc, 4); 1368 for (i = 0; i < fgp->num_cb_points; ++i) { 1369 fgp->point_cb_value[i] = av1_f(vlc, 8); 1370 fgp->point_cb_scaling[i] = av1_f(vlc, 8); 1371 } 1372 fgp->num_cr_points = av1_f(vlc, 4); 1373 for (i = 0; i < fgp->num_cr_points; ++i) { 1374 fgp->point_cr_value[i] = av1_f(vlc, 8); 1375 fgp->point_cr_scaling[i] = av1_f(vlc, 8); 1376 } 1377 } 1378 1379 fgp->grain_scaling_minus_8 = av1_f(vlc, 2); 1380 fgp->ar_coeff_lag = av1_f(vlc, 2); 1381 numPosLuma = 2 * fgp->ar_coeff_lag * (fgp->ar_coeff_lag + 1); 1382 if (fgp->num_y_points) { 1383 numPosChroma = numPosLuma + 1; 1384 for (i = 0; i < numPosLuma; ++i) { 1385 uint8_t ar_coeffs_y_plus_128 = av1_f(vlc, 8); 1386 fgp->ar_coeffs_y[i] = ar_coeffs_y_plus_128 - 128; 1387 } 1388 } else { 1389 numPosChroma = numPosLuma; 1390 } 1391 1392 if (fgp->chroma_scaling_from_luma || fgp->num_cb_points) { 1393 for (i = 0; i < numPosChroma; ++i) { 1394 uint8_t ar_coeffs_cb_plus_128 = av1_f(vlc, 8); 1395 fgp->ar_coeffs_cb[i] = ar_coeffs_cb_plus_128 - 128; 1396 } 1397 } 1398 1399 if (fgp->chroma_scaling_from_luma || fgp->num_cr_points) { 1400 for (i = 0; i < numPosChroma; ++i) { 1401 uint8_t ar_coeffs_cr_plus_128 = av1_f(vlc, 8); 1402 fgp->ar_coeffs_cr[i] = ar_coeffs_cr_plus_128 - 128; 1403 } 1404 } 1405 1406 fgp->ar_coeff_shift_minus_6 = av1_f(vlc, 2); 1407 fgp->grain_scale_shift = av1_f(vlc, 2); 1408 if (fgp->num_cb_points) { 1409 fgp->cb_mult = av1_f(vlc, 8); 1410 fgp->cb_luma_mult = av1_f(vlc, 8); 1411 fgp->cb_offset = av1_f(vlc, 9); 1412 } 1413 1414 if (fgp->num_cr_points) { 1415 fgp->cr_mult = av1_f(vlc, 8); 1416 fgp->cr_luma_mult = av1_f(vlc, 8); 1417 fgp->cr_offset = av1_f(vlc, 9); 1418 } 1419 1420 fgp->overlap_flag = av1_f(vlc, 1); 1421 fgp->clip_to_restricted_range = av1_f(vlc, 1); 1422} 1423 1424static void frame_header_obu(vid_dec_PrivateType *priv, struct vl_vlc *vlc) 1425{ 1426 struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq); 1427 struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header); 1428 unsigned idLen = 0; 1429 unsigned allFrames; 1430 int i, j; 1431 1432 memset(hdr, 0, sizeof(*hdr)); 1433 1434 if (seq->frame_id_numbers_present_flag) 1435 idLen = seq->additional_frame_id_length_minus_1 + 1436 seq->delta_frame_id_length_minus_2 + 3; 1437 1438 allFrames = (1 << AV1_NUM_REF_FRAMES) - 1; 1439 if (seq->reduced_still_picture_header) { 1440 hdr->show_existing_frame = 0; 1441 hdr->frame_type = AV1_KEY_FRAME; 1442 hdr->FrameIsIntra = 1; 1443 hdr->show_frame = 1; 1444 hdr->showable_frame = 0; 1445 } else { 1446 hdr->show_existing_frame = av1_f(vlc, 1); 1447 if (hdr->show_existing_frame) { 1448 hdr->frame_to_show_map_idx = av1_f(vlc, 3); 1449 if (seq->decoder_model_info_present_flag && 1450 !seq->timing_info.equal_picture_interval) 1451 av1_f(vlc, seq->decoder_model_info. 1452 frame_presentation_time_length_minus_1 + 1); 1453 hdr->refresh_frame_flags = 0; 1454 if (seq->frame_id_numbers_present_flag) 1455 av1_f(vlc, idLen); /* display_frame_id */ 1456 1457 hdr->frame_type = 1458 priv->codec_data.av1.RefFrames[priv->codec_data.av1.uncompressed_header. 1459 frame_to_show_map_idx].RefFrameType; 1460 1461 return; 1462 } 1463 1464 hdr->frame_type = av1_f(vlc, 2); 1465 hdr->FrameIsIntra = (hdr->frame_type == AV1_INTRA_ONLY_FRAME || 1466 hdr->frame_type == AV1_KEY_FRAME); 1467 hdr->show_frame = av1_f(vlc, 1); 1468 1469 if (hdr->show_frame && seq->decoder_model_info_present_flag && 1470 !seq->timing_info.equal_picture_interval) 1471 av1_f(vlc, seq->decoder_model_info.frame_presentation_time_length_minus_1 + 1); 1472 1473 hdr->showable_frame = 1474 hdr->show_frame ? (hdr->frame_type != AV1_KEY_FRAME) : av1_f(vlc, 1); 1475 1476 hdr->error_resilient_mode = (hdr->frame_type == AV1_SWITCH_FRAME || 1477 (hdr->frame_type == AV1_KEY_FRAME && hdr->show_frame)) ? 1 : av1_f(vlc, 1); 1478 } 1479 1480 if (hdr->frame_type == AV1_KEY_FRAME && hdr->show_frame) { 1481 for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) 1482 hdr->RefOrderHint[i] = 0; 1483 } 1484 1485 hdr->disable_cdf_update = av1_f(vlc, 1); 1486 1487 hdr->allow_screen_content_tools = 1488 (seq->seq_force_screen_content_tools == AV1_SELECT_SCREEN_CONTENT_TOOLS) ? 1489 av1_f(vlc, 1) : seq->seq_force_screen_content_tools; 1490 1491 if (hdr->allow_screen_content_tools) { 1492 if (seq->seq_force_integer_mv == AV1_SELECT_INTEGER_MV) 1493 hdr->force_integer_mv = av1_f(vlc, 1); 1494 else 1495 hdr->force_integer_mv = seq->seq_force_integer_mv; 1496 } else { 1497 hdr->force_integer_mv = 0; 1498 } 1499 1500 if (hdr->FrameIsIntra) 1501 hdr->force_integer_mv = 1; 1502 1503 hdr->current_frame_id = 1504 seq->frame_id_numbers_present_flag ? av1_f(vlc, idLen) : 0; 1505 1506 if (hdr->frame_type == AV1_SWITCH_FRAME) 1507 hdr->frame_size_override_flag = 1; 1508 else if (seq->reduced_still_picture_header) 1509 hdr->frame_size_override_flag = 0; 1510 else 1511 hdr->frame_size_override_flag = av1_f(vlc, 1); 1512 1513 hdr->OrderHint = av1_f(vlc, seq->OrderHintBits); 1514 1515 if (hdr->FrameIsIntra || hdr->error_resilient_mode) 1516 hdr->primary_ref_frame = AV1_PRIMARY_REF_NONE; 1517 else 1518 hdr->primary_ref_frame = av1_f(vlc, 3); 1519 1520 if (seq->decoder_model_info_present_flag) { 1521 bool buffer_removal_time_present_flag = av1_f(vlc, 1); 1522 if (buffer_removal_time_present_flag) { 1523 for (i = 0; i <= seq->operating_points_cnt_minus_1; ++i) { 1524 if (seq->decoder_model_present_for_this_op[i]) { 1525 unsigned opPtIdc; 1526 bool inTemporalLayer; 1527 bool inSpatialLayer; 1528 opPtIdc = seq->operating_point_idc[i]; 1529 inTemporalLayer = 1530 (opPtIdc >> priv->codec_data.av1.ext.temporal_id) & 1; 1531 inSpatialLayer = 1532 (opPtIdc >> (priv->codec_data.av1.ext.spatial_id + 8)) & 1; 1533 if ((opPtIdc == 0) || (inTemporalLayer && inSpatialLayer)) 1534 av1_f(vlc, seq->decoder_model_info. 1535 buffer_removal_time_length_minus_1 + 1); 1536 } 1537 } 1538 } 1539 } 1540 1541 hdr->allow_high_precision_mv = 0; 1542 hdr->use_ref_frame_mvs = 0; 1543 hdr->allow_intrabc = 0; 1544 1545 hdr->refresh_frame_flags = allFrames = (hdr->frame_type == AV1_SWITCH_FRAME || 1546 (hdr->frame_type == AV1_KEY_FRAME && hdr->show_frame)) ? 1547 allFrames : av1_f(vlc, AV1_NUM_REF_FRAMES); 1548 1549 if (!hdr->FrameIsIntra || hdr->refresh_frame_flags != allFrames) { 1550 if (hdr->error_resilient_mode && seq->enable_order_hint) { 1551 for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) 1552 av1_f(vlc, seq->OrderHintBits); 1553 } 1554 } 1555 1556 if (hdr->FrameIsIntra) { 1557 frame_size(priv, vlc); 1558 render_size(priv, vlc); 1559 if (hdr->allow_screen_content_tools && (hdr->UpscaledWidth == hdr->FrameWidth)) 1560 hdr->allow_intrabc = av1_f(vlc, 1); 1561 } else { 1562 bool is_filter_switchable; 1563 bool frame_refs_short_signaling; 1564 1565 if (!seq->enable_order_hint) { 1566 frame_refs_short_signaling = 0; 1567 } else { 1568 frame_refs_short_signaling = av1_f(vlc, 1); 1569 if (frame_refs_short_signaling) { 1570 hdr->last_frame_idx = av1_f(vlc, 3); 1571 hdr->gold_frame_idx = av1_f(vlc, 3); 1572 set_frame_refs(priv, vlc); 1573 } 1574 } 1575 1576 for (i = 0; i < AV1_REFS_PER_FRAME; ++i) { 1577 if (!frame_refs_short_signaling) 1578 hdr->ref_frame_idx[i] = av1_f(vlc, 3); 1579 if (seq->frame_id_numbers_present_flag) 1580 av1_f(vlc, seq->delta_frame_id_length_minus_2 + 2); 1581 } 1582 1583 if (hdr->frame_size_override_flag && !hdr->error_resilient_mode) { 1584 frame_size_with_refs(priv, vlc); 1585 } else { 1586 frame_size(priv, vlc); 1587 render_size(priv, vlc); 1588 } 1589 1590 hdr->allow_high_precision_mv = hdr->force_integer_mv ? 0 : av1_f(vlc, 1); 1591 1592 is_filter_switchable = av1_f(vlc, 1); 1593 hdr->interpolation_filter = is_filter_switchable ? 4 /* SWITCHABLE */ : av1_f(vlc, 2); 1594 1595 hdr->is_motion_mode_switchable = av1_f(vlc, 1); 1596 hdr->use_ref_frame_mvs = 1597 (hdr->error_resilient_mode || !seq->enable_ref_frame_mvs) ? 0 : av1_f(vlc, 1); 1598 } 1599 1600 hdr->disable_frame_end_update_cdf = 1601 (seq->reduced_still_picture_header || hdr->disable_cdf_update) ? 1 : av1_f(vlc, 1); 1602 1603 tile_info(priv, vlc); 1604 quantization_params(priv, vlc); 1605 segmentation_params(priv, vlc); 1606 delta_q_params(priv, vlc); 1607 delta_lf_params(priv, vlc); 1608 1609 hdr->CodedLossless = 1; 1610 for (i = 0; i < AV1_MAX_SEGMENTS; ++i) { 1611 unsigned qindex = get_qindex(priv, 1, i); 1612 bool LosslessArray = 1613 (qindex == 0) && (hdr->qp.DeltaQYDc == 0) && 1614 (hdr->qp.DeltaQUAc == 0) && (hdr->qp.DeltaQUDc == 0) && 1615 (hdr->qp.DeltaQVAc == 0) && (hdr->qp.DeltaQVDc == 0); 1616 1617 if (!LosslessArray) 1618 hdr->CodedLossless = 0; 1619 } 1620 hdr->AllLossless = hdr->CodedLossless && (hdr->FrameWidth == hdr->UpscaledWidth); 1621 1622 loop_filter_params(priv, vlc); 1623 cdef_params(priv, vlc); 1624 lr_params(priv, vlc); 1625 tx_mode(priv, vlc); 1626 frame_reference_mode(priv, vlc); 1627 skip_mode_params(priv, vlc); 1628 1629 if (hdr->FrameIsIntra || hdr->error_resilient_mode || !seq->enable_warped_motion) 1630 hdr->allow_warped_motion = 0; 1631 else 1632 hdr->allow_warped_motion = av1_f(vlc, 1); 1633 hdr->reduced_tx_set = av1_f(vlc, 1); 1634 1635 global_motion_params(priv, vlc); 1636 1637 film_grain_params(priv, vlc); 1638 1639 priv->picture.av1.picture_parameter.pic_info_fields.frame_type = hdr->frame_type; 1640 priv->picture.av1.picture_parameter.pic_info_fields.show_frame = hdr->show_frame; 1641 priv->picture.av1.picture_parameter.pic_info_fields.error_resilient_mode = 1642 hdr->error_resilient_mode; 1643 priv->picture.av1.picture_parameter.pic_info_fields.disable_cdf_update = 1644 hdr->disable_cdf_update; 1645 priv->picture.av1.picture_parameter.pic_info_fields.allow_screen_content_tools = 1646 hdr->allow_screen_content_tools; 1647 priv->picture.av1.picture_parameter.pic_info_fields.force_integer_mv = 1648 hdr->force_integer_mv; 1649 priv->picture.av1.picture_parameter.current_frame_id = hdr->current_frame_id; 1650 priv->picture.av1.picture_parameter.order_hint = hdr->OrderHint; 1651 priv->picture.av1.picture_parameter.primary_ref_frame = hdr->primary_ref_frame; 1652 priv->picture.av1.picture_parameter.frame_width = hdr->FrameWidth; 1653 priv->picture.av1.picture_parameter.frame_height = hdr->FrameHeight; 1654 priv->picture.av1.picture_parameter.pic_info_fields.use_superres = 1655 hdr->use_superres; 1656 priv->picture.av1.picture_parameter.superres_scale_denominator = 1657 hdr->SuperresDenom; 1658 1659 for (i = 0; i < AV1_REFS_PER_FRAME; ++i) 1660 priv->picture.av1.picture_parameter.ref_frame_idx[i] = hdr->ref_frame_idx[i]; 1661 1662 priv->picture.av1.picture_parameter.pic_info_fields.allow_high_precision_mv = 1663 hdr->allow_high_precision_mv; 1664 priv->picture.av1.picture_parameter.pic_info_fields.allow_intrabc = hdr->allow_intrabc; 1665 priv->picture.av1.picture_parameter.pic_info_fields.use_ref_frame_mvs = 1666 hdr->use_ref_frame_mvs; 1667 priv->picture.av1.picture_parameter.interp_filter = hdr->interpolation_filter; 1668 priv->picture.av1.picture_parameter.pic_info_fields.is_motion_mode_switchable = 1669 hdr->is_motion_mode_switchable; 1670 priv->picture.av1.picture_parameter.refresh_frame_flags = 1671 hdr->refresh_frame_flags; 1672 priv->picture.av1.picture_parameter.pic_info_fields.disable_frame_end_update_cdf = 1673 hdr->disable_frame_end_update_cdf; 1674 1675 /* Tile Info */ 1676 priv->picture.av1.picture_parameter.tile_rows = hdr->ti.TileRows; 1677 priv->picture.av1.picture_parameter.tile_cols = hdr->ti.TileCols; 1678 priv->picture.av1.picture_parameter.context_update_tile_id = 1679 hdr->ti.context_update_tile_id; 1680 for (i = 0; i <AV1_MAX_TILE_ROWS; ++i) 1681 priv->picture.av1.picture_parameter.tile_row_start_sb[i] = 1682 hdr->ti.tile_row_start_sb[i]; 1683 for (i = 0; i <AV1_MAX_TILE_COLS; ++i) 1684 priv->picture.av1.picture_parameter.tile_col_start_sb[i] = 1685 hdr->ti.tile_col_start_sb[i]; 1686 1687 /* Quantization Params */ 1688 priv->picture.av1.picture_parameter.base_qindex = hdr->qp.base_q_idx; 1689 priv->picture.av1.picture_parameter.y_dc_delta_q = hdr->qp.DeltaQYDc; 1690 priv->picture.av1.picture_parameter.u_dc_delta_q = hdr->qp.DeltaQUDc; 1691 priv->picture.av1.picture_parameter.u_ac_delta_q = hdr->qp.DeltaQUAc; 1692 priv->picture.av1.picture_parameter.v_dc_delta_q = hdr->qp.DeltaQVDc; 1693 priv->picture.av1.picture_parameter.v_ac_delta_q = hdr->qp.DeltaQVAc; 1694 priv->picture.av1.picture_parameter.qmatrix_fields.qm_y = hdr->qp.qm_y; 1695 priv->picture.av1.picture_parameter.qmatrix_fields.qm_u = hdr->qp.qm_u; 1696 priv->picture.av1.picture_parameter.qmatrix_fields.qm_v = hdr->qp.qm_v; 1697 1698 /* Segmentation Params */ 1699 priv->picture.av1.picture_parameter.seg_info.segment_info_fields.enabled = 1700 hdr->sp.segmentation_enabled; 1701 priv->picture.av1.picture_parameter.seg_info.segment_info_fields.update_map = 1702 hdr->sp.segmentation_update_map; 1703 priv->picture.av1.picture_parameter.seg_info.segment_info_fields.temporal_update = 1704 hdr->sp.segmentation_temporal_update; 1705 for (i = 0; i < AV1_MAX_SEGMENTS; ++i) { 1706 for (j = 0; j < AV1_SEG_LVL_MAX; ++j) 1707 priv->picture.av1.picture_parameter.seg_info.feature_data[i][j] = 1708 hdr->sp.FeatureData[i][j]; 1709 priv->picture.av1.picture_parameter.seg_info.feature_mask[i] = 1710 hdr->sp.FeatureMask[i]; 1711 } 1712 1713 /* Delta Q Params */ 1714 priv->picture.av1.picture_parameter.mode_control_fields.delta_q_present_flag = 1715 hdr->dqp.delta_q_present; 1716 priv->picture.av1.picture_parameter.mode_control_fields.log2_delta_q_res = 1717 hdr->dqp.delta_q_res; 1718 1719 /* Delta LF Params */ 1720 priv->picture.av1.picture_parameter.mode_control_fields.delta_lf_present_flag = 1721 hdr->dlfp.delta_lf_present; 1722 priv->picture.av1.picture_parameter.mode_control_fields.log2_delta_lf_res = 1723 hdr->dlfp.delta_lf_res; 1724 priv->picture.av1.picture_parameter.mode_control_fields.delta_lf_multi = 1725 hdr->dlfp.delta_lf_multi; 1726 1727 /* Loop Filter Params */ 1728 for (i = 0; i < 2; ++i) 1729 priv->picture.av1.picture_parameter.filter_level[i] = hdr->lfp.loop_filter_level[i]; 1730 priv->picture.av1.picture_parameter.filter_level_u = hdr->lfp.loop_filter_level[2]; 1731 priv->picture.av1.picture_parameter.filter_level_v = hdr->lfp.loop_filter_level[3]; 1732 priv->picture.av1.picture_parameter.loop_filter_info_fields.sharpness_level = 1733 hdr->lfp.loop_filter_sharpness; 1734 priv->picture.av1.picture_parameter.loop_filter_info_fields.mode_ref_delta_enabled = 1735 hdr->lfp.loop_filter_delta_enabled; 1736 priv->picture.av1.picture_parameter.loop_filter_info_fields.mode_ref_delta_update = 1737 hdr->lfp.loop_filter_delta_update; 1738 for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) 1739 priv->picture.av1.picture_parameter.ref_deltas[i] = 1740 hdr->lfp.loop_filter_ref_deltas[i]; 1741 for (i = 0; i < 2; ++i) 1742 priv->picture.av1.picture_parameter.mode_deltas[i] = 1743 hdr->lfp.loop_filter_mode_deltas[i]; 1744 1745 /* CDEF Params */ 1746 priv->picture.av1.picture_parameter.cdef_damping_minus_3 = 1747 hdr->cdefp.cdef_damping_minus_3; 1748 priv->picture.av1.picture_parameter.cdef_bits = hdr->cdefp.cdef_bits; 1749 for (i = 0; i < AV1_MAX_CDEF_BITS_ARRAY; ++i) { 1750 priv->picture.av1.picture_parameter.cdef_y_strengths[i] = 1751 hdr->cdefp.cdef_y_strengths[i]; 1752 priv->picture.av1.picture_parameter.cdef_uv_strengths[i] = 1753 hdr->cdefp.cdef_uv_strengths[i]; 1754 } 1755 1756 /* Loop Restoration Params */ 1757 priv->picture.av1.picture_parameter.loop_restoration_fields.yframe_restoration_type = 1758 hdr->lrp.FrameRestorationType[0]; 1759 priv->picture.av1.picture_parameter.loop_restoration_fields.cbframe_restoration_type = 1760 hdr->lrp.FrameRestorationType[1]; 1761 priv->picture.av1.picture_parameter.loop_restoration_fields.crframe_restoration_type = 1762 hdr->lrp.FrameRestorationType[2]; 1763 for (i = 0; i < 3; ++i) 1764 priv->picture.av1.picture_parameter.lr_unit_size[i] = hdr->lrp.LoopRestorationSize[i]; 1765 1766 priv->picture.av1.picture_parameter.mode_control_fields.tx_mode = hdr->tm.TxMode; 1767 priv->picture.av1.picture_parameter.mode_control_fields.reference_select = 1768 (hdr->reference_select == REFERENCE_MODE_SELECT) ? COMPOUND_REFERENCE : SINGLE_REFERENCE; 1769 priv->picture.av1.picture_parameter.mode_control_fields.skip_mode_present = 1770 hdr->smp.skip_mode_present; 1771 priv->picture.av1.picture_parameter.pic_info_fields.allow_warped_motion = 1772 hdr->allow_warped_motion; 1773 priv->picture.av1.picture_parameter.mode_control_fields.reduced_tx_set_used = 1774 hdr->reduced_tx_set; 1775 1776 /* Global Motion Params */ 1777 for (i = 0; i < 7; ++i) { 1778 priv->picture.av1.picture_parameter.wm[i].wmtype = hdr->gmp.GmType[i + 1]; 1779 for (j = 0; j < 6; ++j) 1780 priv->picture.av1.picture_parameter.wm[i].wmmat[j] = hdr->gmp.gm_params[i + 1][j]; 1781 } 1782 1783 /* Film Grain Params */ 1784 priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields.apply_grain = 1785 hdr->fgp.apply_grain; 1786 priv->picture.av1.picture_parameter.film_grain_info.grain_seed = 1787 hdr->fgp.grain_seed; 1788 priv->picture.av1.picture_parameter.film_grain_info.num_y_points = 1789 hdr->fgp.num_y_points; 1790 for (i = 0; i < AV1_FG_MAX_NUM_Y_POINTS; ++i) { 1791 priv->picture.av1.picture_parameter.film_grain_info.point_y_value[i] = 1792 hdr->fgp.point_y_value[i]; 1793 priv->picture.av1.picture_parameter.film_grain_info.point_y_scaling[i] = 1794 hdr->fgp.point_y_scaling[i]; 1795 } 1796 priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields. 1797 chroma_scaling_from_luma = hdr->fgp.chroma_scaling_from_luma; 1798 priv->picture.av1.picture_parameter.film_grain_info.num_cb_points = 1799 hdr->fgp.num_cb_points; 1800 priv->picture.av1.picture_parameter.film_grain_info.num_cr_points = 1801 hdr->fgp.num_cr_points; 1802 for (i = 0; i < AV1_FG_MAX_NUM_CBR_POINTS; ++i) { 1803 priv->picture.av1.picture_parameter.film_grain_info.point_cb_value[i] = 1804 hdr->fgp.point_cb_value[i]; 1805 priv->picture.av1.picture_parameter.film_grain_info.point_cb_scaling[i] = 1806 hdr->fgp.point_cb_scaling[i]; 1807 priv->picture.av1.picture_parameter.film_grain_info.point_cr_value[i] = 1808 hdr->fgp.point_cr_value[i]; 1809 priv->picture.av1.picture_parameter.film_grain_info.point_cr_scaling[i] = 1810 hdr->fgp.point_cr_scaling[i]; 1811 } 1812 priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields. 1813 grain_scaling_minus_8 = hdr->fgp.grain_scaling_minus_8; 1814 priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields. 1815 ar_coeff_lag = hdr->fgp.ar_coeff_lag; 1816 for (i = 0; i < AV1_FG_MAX_NUM_POS_LUMA; ++i) 1817 priv->picture.av1.picture_parameter.film_grain_info.ar_coeffs_y[i] = 1818 hdr->fgp.ar_coeffs_y[i]; 1819 for (i = 0; i < AV1_FG_MAX_NUM_POS_CHROMA; ++i) { 1820 priv->picture.av1.picture_parameter.film_grain_info.ar_coeffs_cb[i] = 1821 hdr->fgp.ar_coeffs_cb[i]; 1822 priv->picture.av1.picture_parameter.film_grain_info.ar_coeffs_cr[i] = 1823 hdr->fgp.ar_coeffs_cr[i]; 1824 } 1825 priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields. 1826 ar_coeff_shift_minus_6 = hdr->fgp.ar_coeff_shift_minus_6; 1827 priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields. 1828 grain_scale_shift = hdr->fgp.grain_scale_shift; 1829 priv->picture.av1.picture_parameter.film_grain_info.cb_mult = hdr->fgp.cb_mult; 1830 priv->picture.av1.picture_parameter.film_grain_info.cb_luma_mult = hdr->fgp.cb_luma_mult; 1831 priv->picture.av1.picture_parameter.film_grain_info.cb_offset = hdr->fgp.cb_offset; 1832 priv->picture.av1.picture_parameter.film_grain_info.cr_mult = hdr->fgp.cr_mult; 1833 priv->picture.av1.picture_parameter.film_grain_info.cr_luma_mult = hdr->fgp.cr_luma_mult; 1834 priv->picture.av1.picture_parameter.film_grain_info.cr_offset = hdr->fgp.cr_offset; 1835 priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields. 1836 overlap_flag = hdr->fgp.overlap_flag; 1837 priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields. 1838 clip_to_restricted_range = hdr->fgp.clip_to_restricted_range; 1839} 1840 1841static void parse_tile_hdr(vid_dec_PrivateType *priv, struct vl_vlc *vlc, 1842 unsigned start_bits_pos, unsigned total_obu_len) 1843{ 1844 struct tile_info *ti = &(priv->codec_data.av1.uncompressed_header.ti); 1845 unsigned tg_start, tg_end; 1846 unsigned NumTiles, tileBits; 1847 bool tile_start_and_end_present_flag; 1848 unsigned size[AV1_MAX_NUM_TILES] = { 0 }; 1849 unsigned offset[AV1_MAX_NUM_TILES] = { 0 }; 1850 unsigned frame_header_size, left_size; 1851 unsigned i, j; 1852 1853 NumTiles = ti->TileCols * ti->TileRows; 1854 tile_start_and_end_present_flag = 0; 1855 if (NumTiles > 1) 1856 tile_start_and_end_present_flag = av1_f(vlc, 1); 1857 1858 if (NumTiles == 1 || !tile_start_and_end_present_flag) { 1859 tg_start = 0; 1860 tg_end = NumTiles - 1; 1861 } else { 1862 tileBits = ti->TileColsLog2 + ti->TileRowsLog2; 1863 tg_start = av1_f(vlc, tileBits); 1864 tg_end = av1_f(vlc, tileBits); 1865 } 1866 1867 av1_byte_alignment(vlc); 1868 1869 frame_header_size = (start_bits_pos - vl_vlc_bits_left(vlc)) / 8; 1870 left_size = total_obu_len - frame_header_size; 1871 for (i = tg_start; i <= tg_end; ++i) { 1872 if (i == tg_start) { 1873 offset[i] = priv->codec_data.av1.bs_obu_td_sz + 1874 priv->codec_data.av1.bs_obu_seq_sz + frame_header_size + 1875 ti->TileSizeBytes; 1876 if (tg_start == tg_end) { 1877 size[i] = left_size; 1878 for (j = 0; j < size[i]; ++j) { 1879 vl_vlc_fillbits(vlc); 1880 vl_vlc_eatbits(vlc, 8); 1881 } 1882 break; 1883 } 1884 } else { 1885 offset[i] = offset[i - 1] + ti->TileSizeBytes + size[i - 1]; 1886 left_size -= ti->TileSizeBytes + size[i - 1]; 1887 } 1888 1889 if (i != tg_end) { 1890 size[i] = av1_le(vlc, ti->TileSizeBytes) + 1; 1891 } else { 1892 offset[i] = offset[i - 1] + size[i - 1]; 1893 size[i] = left_size; 1894 } 1895 1896 for (j = 0; j < size[i]; ++j) { 1897 vl_vlc_fillbits(vlc); 1898 vl_vlc_eatbits(vlc, 8); 1899 } 1900 } 1901 1902 for (i = tg_start; i <= tg_end; ++i) { 1903 priv->picture.av1.slice_parameter.slice_data_offset[i] = offset[i]; 1904 priv->picture.av1.slice_parameter.slice_data_size[i] = size[i]; 1905 } 1906} 1907 1908static struct dec_av1_task *dec_av1_NeedTask(vid_dec_PrivateType *priv) 1909{ 1910 struct pipe_video_buffer templat = {}; 1911 struct dec_av1_task *task; 1912 struct vl_screen *omx_screen; 1913 struct pipe_screen *pscreen; 1914 1915 omx_screen = priv->screen; 1916 assert(omx_screen); 1917 1918 pscreen = omx_screen->pscreen; 1919 assert(pscreen); 1920 1921 if (!list_is_empty(&priv->codec_data.av1.free_tasks)) { 1922 task = LIST_ENTRY(struct dec_av1_task, 1923 priv->codec_data.av1.free_tasks.next, list); 1924 task->buf_ref_count = 1; 1925 list_del(&task->list); 1926 return task; 1927 } 1928 1929 task = CALLOC_STRUCT(dec_av1_task); 1930 if (!task) 1931 return NULL; 1932 1933 memset(&templat, 0, sizeof(templat)); 1934 templat.width = priv->codec->width; 1935 templat.height = priv->codec->height; 1936 templat.buffer_format = pscreen->get_video_param( 1937 pscreen, 1938 PIPE_VIDEO_PROFILE_UNKNOWN, 1939 PIPE_VIDEO_ENTRYPOINT_BITSTREAM, 1940 PIPE_VIDEO_CAP_PREFERED_FORMAT 1941 ); 1942 templat.interlaced = false; 1943 1944 task->buf = priv->pipe->create_video_buffer(priv->pipe, &templat); 1945 if (!task->buf) { 1946 FREE(task); 1947 return NULL; 1948 } 1949 task->buf_ref_count = 1; 1950 task->is_sef_task = false; 1951 1952 return task; 1953} 1954 1955static void dec_av1_ReleaseTask(vid_dec_PrivateType *priv, 1956 struct list_head *head) 1957{ 1958 if (!head || !head->next) 1959 return; 1960 1961 list_for_each_entry_safe(struct dec_av1_task, task, head, list) { 1962 task->buf->destroy(task->buf); 1963 FREE(task); 1964 } 1965} 1966 1967static void dec_av1_MoveTask(struct list_head *from, 1968 struct list_head *to) 1969{ 1970 to->prev->next = from->next; 1971 from->next->prev = to->prev; 1972 from->prev->next = to; 1973 to->prev = from->prev; 1974 list_inithead(from); 1975} 1976 1977static void dec_av1_SortTask(vid_dec_PrivateType *priv) 1978{ 1979 int i; 1980 1981 list_for_each_entry_safe(struct dec_av1_task, t, 1982 &priv->codec_data.av1.finished_tasks, list) { 1983 bool found = false; 1984 for (i = 0; i < 8; ++i) { 1985 if (t->buf == priv->picture.av1.ref[i]) { 1986 found = true; 1987 break; 1988 } 1989 } 1990 if (!found && t->buf_ref_count == 0) { 1991 list_del(&t->list); 1992 list_addtail(&t->list, &priv->codec_data.av1.free_tasks); 1993 } 1994 } 1995} 1996 1997static struct dec_av1_task *dec_av1_SearchTask(vid_dec_PrivateType *priv, 1998 struct list_head *tasks) 1999{ 2000 unsigned idx = 2001 priv->codec_data.av1.uncompressed_header.frame_to_show_map_idx; 2002 2003 list_for_each_entry_safe(struct dec_av1_task, t, tasks, list) { 2004 if (t->buf == priv->picture.av1.ref[idx]) 2005 return t; 2006 } 2007 2008 return NULL; 2009} 2010 2011static bool dec_av1_GetStartedTask(vid_dec_PrivateType *priv, 2012 struct dec_av1_task *task, struct list_head *tasks) 2013{ 2014 struct dec_av1_task *started_task; 2015 2016 ++priv->codec_data.av1.que_num; 2017 list_addtail(&task->list, &priv->codec_data.av1.started_tasks); 2018 if (priv->codec_data.av1.que_num <= 16) 2019 return false; 2020 2021 started_task = LIST_ENTRY(struct dec_av1_task, 2022 priv->codec_data.av1.started_tasks.next, list); 2023 list_del(&started_task->list); 2024 list_addtail(&started_task->list, tasks); 2025 --priv->codec_data.av1.que_num; 2026 2027 return true; 2028} 2029 2030static void dec_av1_ShowExistingframe(vid_dec_PrivateType *priv) 2031{ 2032 struct input_buf_private *inp = priv->in_buffers[0]->pInputPortPrivate; 2033 struct dec_av1_task *task, *existing_task; 2034 bool fnd; 2035 2036 task = CALLOC_STRUCT(dec_av1_task); 2037 if (!task) 2038 return; 2039 2040 task->is_sef_task = true; 2041 2042 mtx_lock(&priv->codec_data.av1.mutex); 2043 dec_av1_MoveTask(&inp->tasks, &priv->codec_data.av1.finished_tasks); 2044 dec_av1_SortTask(priv); 2045 existing_task = dec_av1_SearchTask(priv, &priv->codec_data.av1.started_tasks); 2046 if (existing_task) { 2047 ++existing_task->buf_ref_count; 2048 task->buf = existing_task->buf; 2049 task->buf_ref = &existing_task->buf; 2050 task->buf_ref_count = 0; 2051 } else { 2052 existing_task = dec_av1_SearchTask(priv, &priv->codec_data.av1.finished_tasks); 2053 if (existing_task) { 2054 struct vl_screen *omx_screen; 2055 struct pipe_screen *pscreen; 2056 struct pipe_video_buffer templat = {}; 2057 struct pipe_video_buffer *buf; 2058 struct pipe_box box={}; 2059 2060 omx_screen = priv->screen; 2061 assert(omx_screen); 2062 2063 pscreen = omx_screen->pscreen; 2064 assert(pscreen); 2065 2066 memset(&templat, 0, sizeof(templat)); 2067 templat.width = priv->codec->width; 2068 templat.height = priv->codec->height; 2069 templat.buffer_format = pscreen->get_video_param( 2070 pscreen, 2071 PIPE_VIDEO_PROFILE_UNKNOWN, 2072 PIPE_VIDEO_ENTRYPOINT_BITSTREAM, 2073 PIPE_VIDEO_CAP_PREFERED_FORMAT 2074 ); 2075 templat.interlaced = false; 2076 buf = priv->pipe->create_video_buffer(priv->pipe, &templat); 2077 if (!buf) { 2078 FREE(task); 2079 mtx_unlock(&priv->codec_data.av1.mutex); 2080 return; 2081 } 2082 2083 box.width = priv->codec->width; 2084 box.height = priv->codec->height; 2085 box.depth = 1; 2086 priv->pipe->resource_copy_region(priv->pipe, 2087 ((struct vl_video_buffer *)buf)->resources[0], 2088 0, 0, 0, 0, 2089 ((struct vl_video_buffer *)(existing_task->buf))->resources[0], 2090 0, &box); 2091 box.width /= 2; 2092 box.height/= 2; 2093 priv->pipe->resource_copy_region(priv->pipe, 2094 ((struct vl_video_buffer *)buf)->resources[1], 2095 0, 0, 0, 0, 2096 ((struct vl_video_buffer *)(existing_task->buf))->resources[1], 2097 0, &box); 2098 priv->pipe->flush(priv->pipe, NULL, 0); 2099 existing_task->buf_ref_count = 0; 2100 task->buf = buf; 2101 task->buf_ref_count = 1; 2102 } else { 2103 FREE(task); 2104 mtx_unlock(&priv->codec_data.av1.mutex); 2105 return; 2106 } 2107 } 2108 dec_av1_SortTask(priv); 2109 2110 fnd = dec_av1_GetStartedTask(priv, task, &inp->tasks); 2111 mtx_unlock(&priv->codec_data.av1.mutex); 2112 if (fnd) 2113 priv->frame_finished = 1; 2114} 2115 2116static struct dec_av1_task *dec_av1_BeginFrame(vid_dec_PrivateType *priv) 2117{ 2118 struct input_buf_private *inp = priv->in_buffers[0]->pInputPortPrivate; 2119 struct dec_av1_task *task; 2120 2121 if (priv->frame_started) 2122 return NULL; 2123 2124 if (!priv->codec) { 2125 struct vl_screen *omx_screen; 2126 struct pipe_screen *pscreen; 2127 struct pipe_video_codec templat = {}; 2128 bool supported; 2129 2130 omx_screen = priv->screen; 2131 assert(omx_screen); 2132 2133 pscreen = omx_screen->pscreen; 2134 assert(pscreen); 2135 2136 supported = pscreen->get_video_param(pscreen, priv->profile, 2137 PIPE_VIDEO_ENTRYPOINT_BITSTREAM, PIPE_VIDEO_CAP_SUPPORTED); 2138 assert(supported && "AV1 is not supported"); 2139 2140 templat.profile = priv->profile; 2141 templat.entrypoint = PIPE_VIDEO_ENTRYPOINT_BITSTREAM; 2142 templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420; 2143 templat.max_references = AV1_NUM_REF_FRAMES; 2144 templat.expect_chunked_decode = true; 2145 omx_base_video_PortType *port; 2146 port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_INPUTPORT_INDEX]; 2147 templat.width = port->sPortParam.format.video.nFrameWidth; 2148 templat.height = port->sPortParam.format.video.nFrameHeight; 2149 2150 priv->codec = priv->pipe->create_video_codec(priv->pipe, &templat); 2151 } 2152 2153 mtx_lock(&priv->codec_data.av1.mutex); 2154 dec_av1_MoveTask(&inp->tasks, &priv->codec_data.av1.finished_tasks); 2155 dec_av1_SortTask(priv); 2156 mtx_unlock(&priv->codec_data.av1.mutex); 2157 2158 task = dec_av1_NeedTask(priv); 2159 if (!task) 2160 return NULL; 2161 2162 priv->codec->begin_frame(priv->codec, task->buf, &priv->picture.base); 2163 priv->frame_started = true; 2164 2165 return task; 2166} 2167 2168static void dec_av1_EndFrame(vid_dec_PrivateType *priv, struct dec_av1_task *task) 2169{ 2170 struct input_buf_private *inp = priv->in_buffers[0]->pInputPortPrivate; 2171 unsigned refresh_frame_flags; 2172 bool fnd; 2173 unsigned i; 2174 2175 if (!priv->frame_started || ! task) 2176 return; 2177 2178 priv->codec->end_frame(priv->codec, task->buf, &priv->picture.base); 2179 priv->frame_started = false; 2180 2181 refresh_frame_flags = priv->codec_data.av1.uncompressed_header.refresh_frame_flags; 2182 for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) { 2183 if (refresh_frame_flags & (1 << i)) { 2184 memcpy(&priv->codec_data.av1.refs[i], &priv->codec_data.av1.uncompressed_header, 2185 sizeof(struct av1_uncompressed_header_obu)); 2186 priv->picture.av1.ref[i] = task->buf; 2187 priv->codec_data.av1.RefFrames[i].RefFrameType = 2188 priv->codec_data.av1.uncompressed_header.frame_type; 2189 priv->codec_data.av1.RefFrames[i].RefFrameId = 2190 priv->codec_data.av1.uncompressed_header.current_frame_id; 2191 priv->codec_data.av1.RefFrames[i].RefUpscaledWidth = 2192 priv->codec_data.av1.uncompressed_header.UpscaledWidth; 2193 priv->codec_data.av1.RefFrames[i].RefFrameWidth = 2194 priv->codec_data.av1.uncompressed_header.FrameWidth; 2195 priv->codec_data.av1.RefFrames[i].RefFrameHeight = 2196 priv->codec_data.av1.uncompressed_header.FrameHeight; 2197 priv->codec_data.av1.RefFrames[i].RefRenderWidth = 2198 priv->codec_data.av1.uncompressed_header.RenderWidth; 2199 priv->codec_data.av1.RefFrames[i].RefRenderHeight = 2200 priv->codec_data.av1.uncompressed_header.RenderHeight; 2201 } 2202 } 2203 if (!priv->picture.av1.picture_parameter.pic_info_fields.show_frame) 2204 task->no_show_frame = true; 2205 2206 mtx_lock(&priv->codec_data.av1.mutex); 2207 fnd = dec_av1_GetStartedTask(priv, task, &priv->codec_data.av1.decode_tasks); 2208 if (!fnd) { 2209 mtx_unlock(&priv->codec_data.av1.mutex); 2210 return; 2211 } 2212 if (!priv->codec_data.av1.stacked_frame) 2213 dec_av1_MoveTask(&priv->codec_data.av1.decode_tasks, &inp->tasks); 2214 mtx_unlock(&priv->codec_data.av1.mutex); 2215 priv->frame_finished = 1; 2216} 2217 2218static void dec_av1_Decode(vid_dec_PrivateType *priv, struct vl_vlc *vlc, 2219 unsigned min_bits_left) 2220{ 2221 unsigned start_bits_pos = vl_vlc_bits_left(vlc); 2222 unsigned start_bits = vl_vlc_valid_bits(vlc); 2223 unsigned start_bytes = start_bits / 8; 2224 const void *obu_data = vlc->data; 2225 uint8_t start_buf[8]; 2226 unsigned num_buffers = 0; 2227 void * const * buffers[4]; 2228 unsigned sizes[4]; 2229 unsigned obu_size = 0; 2230 unsigned total_obu_len; 2231 enum av1_obu_type type; 2232 bool obu_extension_flag; 2233 bool obu_has_size_field; 2234 unsigned i; 2235 2236 for (i = 0; i < start_bytes; ++i) 2237 start_buf[i] = 2238 vl_vlc_peekbits(vlc, start_bits) >> ((start_bytes - i - 1) * 8); 2239 2240 /* obu header */ 2241 av1_f(vlc, 1); /* obu_forbidden_bit */ 2242 type = av1_f(vlc, 4); 2243 obu_extension_flag = av1_f(vlc, 1); 2244 obu_has_size_field = av1_f(vlc, 1); 2245 av1_f(vlc, 1); /* obu_reserved_1bit */ 2246 if (obu_extension_flag) { 2247 priv->codec_data.av1.ext.temporal_id = av1_f(vlc, 3); 2248 priv->codec_data.av1.ext.spatial_id = av1_f(vlc, 2); 2249 av1_f(vlc, 3); /* extension_header_reserved_3bits */ 2250 } 2251 2252 obu_size = (obu_has_size_field) ? av1_uleb128(vlc) : 2253 (priv->sizes[0] - (unsigned)obu_extension_flag - 1); 2254 total_obu_len = (start_bits_pos - vl_vlc_bits_left(vlc)) / 8 + obu_size; 2255 2256 switch (type) { 2257 case AV1_OBU_SEQUENCE_HEADER: { 2258 sequence_header_obu(priv, vlc); 2259 av1_byte_alignment(vlc); 2260 priv->codec_data.av1.bs_obu_seq_sz = total_obu_len; 2261 memcpy(priv->codec_data.av1.bs_obu_seq_buf, start_buf, start_bytes); 2262 memcpy(priv->codec_data.av1.bs_obu_seq_buf + start_bytes, obu_data, 2263 total_obu_len - start_bytes); 2264 break; 2265 } 2266 case AV1_OBU_TEMPORAL_DELIMITER: 2267 av1_byte_alignment(vlc); 2268 priv->codec_data.av1.bs_obu_td_sz = total_obu_len; 2269 memcpy(priv->codec_data.av1.bs_obu_td_buf, start_buf, total_obu_len); 2270 break; 2271 case AV1_OBU_FRAME_HEADER: 2272 frame_header_obu(priv, vlc); 2273 if (priv->codec_data.av1.uncompressed_header.show_existing_frame) 2274 dec_av1_ShowExistingframe(priv); 2275 av1_byte_alignment(vlc); 2276 break; 2277 case AV1_OBU_FRAME: { 2278 struct dec_av1_task *task; 2279 2280 frame_header_obu(priv, vlc); 2281 av1_byte_alignment(vlc); 2282 2283 parse_tile_hdr(priv, vlc, start_bits_pos, total_obu_len); 2284 av1_byte_alignment(vlc); 2285 2286 task = dec_av1_BeginFrame(priv); 2287 if (!task) 2288 return; 2289 2290 if (priv->codec_data.av1.bs_obu_td_sz) { 2291 buffers[num_buffers] = (void *)priv->codec_data.av1.bs_obu_td_buf; 2292 sizes[num_buffers++] = priv->codec_data.av1.bs_obu_td_sz; 2293 priv->codec_data.av1.bs_obu_td_sz = 0; 2294 } 2295 if (priv->codec_data.av1.bs_obu_seq_sz) { 2296 buffers[num_buffers] = (void *)priv->codec_data.av1.bs_obu_seq_buf; 2297 sizes[num_buffers++] = priv->codec_data.av1.bs_obu_seq_sz; 2298 priv->codec_data.av1.bs_obu_seq_sz = 0; 2299 } 2300 buffers[num_buffers] = (void *)start_buf; 2301 sizes[num_buffers++] = start_bytes; 2302 buffers[num_buffers] = (void *)obu_data; 2303 sizes[num_buffers++] = total_obu_len - start_bytes; 2304 2305 priv->codec->decode_bitstream(priv->codec, priv->target, 2306 &priv->picture.base, num_buffers, (const void * const*)buffers, sizes); 2307 2308 priv->codec_data.av1.stacked_frame = 2309 (vl_vlc_bits_left(vlc) > min_bits_left) ? true : false; 2310 2311 dec_av1_EndFrame(priv, task); 2312 break; 2313 } 2314 default: 2315 av1_byte_alignment(vlc); 2316 break; 2317 } 2318 2319 return; 2320} 2321 2322OMX_ERRORTYPE vid_dec_av1_AllocateInBuffer(omx_base_PortType *port, 2323 OMX_INOUT OMX_BUFFERHEADERTYPE **buf, OMX_IN OMX_U32 idx, 2324 OMX_IN OMX_PTR private, OMX_IN OMX_U32 size) 2325{ 2326 struct input_buf_private *inp; 2327 OMX_ERRORTYPE r; 2328 2329 r = base_port_AllocateBuffer(port, buf, idx, private, size); 2330 if (r) 2331 return r; 2332 2333 inp = (*buf)->pInputPortPrivate = CALLOC_STRUCT(input_buf_private); 2334 if (!inp) { 2335 base_port_FreeBuffer(port, idx, *buf); 2336 return OMX_ErrorInsufficientResources; 2337 } 2338 2339 list_inithead(&inp->tasks); 2340 2341 return OMX_ErrorNone; 2342} 2343 2344OMX_ERRORTYPE vid_dec_av1_UseInBuffer(omx_base_PortType *port, 2345 OMX_BUFFERHEADERTYPE **buf, OMX_U32 idx, 2346 OMX_PTR private, OMX_U32 size, OMX_U8 *mem) 2347{ 2348 struct input_buf_private *inp; 2349 OMX_ERRORTYPE r; 2350 2351 r = base_port_UseBuffer(port, buf, idx, private, size, mem); 2352 if (r) 2353 return r; 2354 2355 inp = (*buf)->pInputPortPrivate = CALLOC_STRUCT(input_buf_private); 2356 if (!inp) { 2357 base_port_FreeBuffer(port, idx, *buf); 2358 return OMX_ErrorInsufficientResources; 2359 } 2360 2361 list_inithead(&inp->tasks); 2362 2363 return OMX_ErrorNone; 2364} 2365 2366void vid_dec_av1_FreeInputPortPrivate(vid_dec_PrivateType *priv, 2367 OMX_BUFFERHEADERTYPE *buf) 2368{ 2369 struct input_buf_private *inp = buf->pInputPortPrivate; 2370 2371 if (!inp || !inp->tasks.next) 2372 return; 2373 2374 list_for_each_entry_safe(struct dec_av1_task, task, &inp->tasks, list) { 2375 task->buf->destroy(task->buf); 2376 FREE(task); 2377 } 2378} 2379 2380void vid_dec_av1_ReleaseTasks(vid_dec_PrivateType *priv) 2381{ 2382 dec_av1_ReleaseTask(priv, &priv->codec_data.av1.free_tasks); 2383 dec_av1_ReleaseTask(priv, &priv->codec_data.av1.started_tasks); 2384 dec_av1_ReleaseTask(priv, &priv->codec_data.av1.decode_tasks); 2385 dec_av1_ReleaseTask(priv, &priv->codec_data.av1.finished_tasks); 2386 mtx_destroy(&priv->codec_data.av1.mutex); 2387} 2388 2389void vid_dec_av1_FrameDecoded(OMX_COMPONENTTYPE *comp, 2390 OMX_BUFFERHEADERTYPE* input, 2391 OMX_BUFFERHEADERTYPE* output) 2392{ 2393 vid_dec_PrivateType *priv = comp->pComponentPrivate; 2394 bool eos = !!(input->nFlags & OMX_BUFFERFLAG_EOS); 2395 struct input_buf_private *inp = input->pInputPortPrivate; 2396 struct dec_av1_task *task; 2397 bool stacked = false; 2398 2399 mtx_lock(&priv->codec_data.av1.mutex); 2400 if (list_length(&inp->tasks) > 1) 2401 stacked = true; 2402 2403 if (list_is_empty(&inp->tasks)) { 2404 task = LIST_ENTRY(struct dec_av1_task, 2405 priv->codec_data.av1.started_tasks.next, list); 2406 list_del(&task->list); 2407 list_addtail(&task->list, &inp->tasks); 2408 --priv->codec_data.av1.que_num; 2409 } 2410 2411 task = LIST_ENTRY(struct dec_av1_task, inp->tasks.next, list); 2412 2413 if (!task->no_show_frame) { 2414 vid_dec_FillOutput(priv, task->buf, output); 2415 output->nFilledLen = output->nAllocLen; 2416 output->nTimeStamp = input->nTimeStamp; 2417 } else { 2418 task->no_show_frame = false; 2419 output->nFilledLen = 0; 2420 } 2421 2422 if (task->is_sef_task) { 2423 if (task->buf_ref_count == 0) { 2424 struct dec_av1_task *t = container_of(task->buf_ref, struct dec_av1_task, buf); 2425 list_del(&task->list); 2426 t->buf_ref_count--; 2427 list_del(&t->list); 2428 list_addtail(&t->list, &priv->codec_data.av1.finished_tasks); 2429 } else if (task->buf_ref_count == 1) { 2430 list_del(&task->list); 2431 task->buf->destroy(task->buf); 2432 task->buf_ref_count--; 2433 } 2434 FREE(task); 2435 } else { 2436 if (task->buf_ref_count == 1) { 2437 list_del(&task->list); 2438 list_addtail(&task->list, &priv->codec_data.av1.finished_tasks); 2439 task->buf_ref_count--; 2440 } else if (task->buf_ref_count == 2) { 2441 list_del(&task->list); 2442 task->buf_ref_count--; 2443 list_addtail(&task->list, &priv->codec_data.av1.finished_tasks); 2444 } 2445 } 2446 2447 if (eos && input->pInputPortPrivate) { 2448 if (!priv->codec_data.av1.que_num) 2449 input->nFilledLen = 0; 2450 else 2451 vid_dec_av1_FreeInputPortPrivate(priv, input); 2452 } 2453 else { 2454 if (!stacked) 2455 input->nFilledLen = 0; 2456 } 2457 mtx_unlock(&priv->codec_data.av1.mutex); 2458} 2459 2460void vid_dec_av1_Init(vid_dec_PrivateType *priv) 2461{ 2462 priv->picture.base.profile = PIPE_VIDEO_PROFILE_AV1_MAIN; 2463 priv->Decode = dec_av1_Decode; 2464 list_inithead(&priv->codec_data.av1.free_tasks); 2465 list_inithead(&priv->codec_data.av1.started_tasks); 2466 list_inithead(&priv->codec_data.av1.decode_tasks); 2467 list_inithead(&priv->codec_data.av1.finished_tasks); 2468 (void)mtx_init(&priv->codec_data.av1.mutex, mtx_plain); 2469} 2470