cbs_av1.c (39602B)
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/avassert.h" 20 #include "libavutil/opt.h" 21 #include "libavutil/pixfmt.h" 22 23 #include "cbs.h" 24 #include "cbs_internal.h" 25 #include "cbs_av1.h" 26 #include "defs.h" 27 #include "libavutil/refstruct.h" 28 29 30 static int cbs_av1_read_uvlc(CodedBitstreamContext *ctx, GetBitContext *gbc, 31 const char *name, uint32_t *write_to, 32 uint32_t range_min, uint32_t range_max) 33 { 34 uint32_t zeroes, bits_value, value; 35 36 CBS_TRACE_READ_START(); 37 38 zeroes = 0; 39 while (zeroes < 32) { 40 if (get_bits_left(gbc) < 1) { 41 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid uvlc code at " 42 "%s: bitstream ended.\n", name); 43 return AVERROR_INVALIDDATA; 44 } 45 46 if (get_bits1(gbc)) 47 break; 48 ++zeroes; 49 } 50 51 if (zeroes >= 32) { 52 // The spec allows at least thirty-two zero bits followed by a 53 // one to mean 2^32-1, with no constraint on the number of 54 // zeroes. The libaom reference decoder does not match this, 55 // instead reading thirty-two zeroes but not the following one 56 // to mean 2^32-1. These two interpretations are incompatible 57 // and other implementations may follow one or the other. 58 // Therefore we reject thirty-two zeroes because the intended 59 // behaviour is not clear. 60 av_log(ctx->log_ctx, AV_LOG_ERROR, "Thirty-two zero bits in " 61 "%s uvlc code: considered invalid due to conflicting " 62 "standard and reference decoder behaviour.\n", name); 63 return AVERROR_INVALIDDATA; 64 } else { 65 if (get_bits_left(gbc) < zeroes) { 66 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid uvlc code at " 67 "%s: bitstream ended.\n", name); 68 return AVERROR_INVALIDDATA; 69 } 70 71 bits_value = get_bits_long(gbc, zeroes); 72 value = bits_value + (UINT32_C(1) << zeroes) - 1; 73 } 74 75 CBS_TRACE_READ_END_NO_SUBSCRIPTS(); 76 77 if (value < range_min || value > range_max) { 78 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: " 79 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n", 80 name, value, range_min, range_max); 81 return AVERROR_INVALIDDATA; 82 } 83 84 *write_to = value; 85 return 0; 86 } 87 88 static int cbs_av1_write_uvlc(CodedBitstreamContext *ctx, PutBitContext *pbc, 89 const char *name, uint32_t value, 90 uint32_t range_min, uint32_t range_max) 91 { 92 uint32_t v; 93 int zeroes; 94 95 CBS_TRACE_WRITE_START(); 96 97 if (value < range_min || value > range_max) { 98 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: " 99 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n", 100 name, value, range_min, range_max); 101 return AVERROR_INVALIDDATA; 102 } 103 104 zeroes = av_log2(value + 1); 105 v = value - (1U << zeroes) + 1; 106 107 if (put_bits_left(pbc) < 2 * zeroes + 1) 108 return AVERROR(ENOSPC); 109 110 put_bits(pbc, zeroes, 0); 111 put_bits(pbc, 1, 1); 112 put_bits(pbc, zeroes, v); 113 114 CBS_TRACE_WRITE_END_NO_SUBSCRIPTS(); 115 116 return 0; 117 } 118 119 static int cbs_av1_read_leb128(CodedBitstreamContext *ctx, GetBitContext *gbc, 120 const char *name, uint64_t *write_to) 121 { 122 uint64_t value; 123 uint32_t byte; 124 int i; 125 126 CBS_TRACE_READ_START(); 127 128 value = 0; 129 for (i = 0; i < 8; i++) { 130 if (get_bits_left(gbc) < 8) { 131 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid leb128 at " 132 "%s: bitstream ended.\n", name); 133 return AVERROR_INVALIDDATA; 134 } 135 byte = get_bits(gbc, 8); 136 value |= (uint64_t)(byte & 0x7f) << (i * 7); 137 if (!(byte & 0x80)) 138 break; 139 } 140 141 if (value > UINT32_MAX) 142 return AVERROR_INVALIDDATA; 143 144 CBS_TRACE_READ_END_NO_SUBSCRIPTS(); 145 146 *write_to = value; 147 return 0; 148 } 149 150 static int cbs_av1_write_leb128(CodedBitstreamContext *ctx, PutBitContext *pbc, 151 const char *name, uint64_t value, int fixed_length) 152 { 153 int len, i; 154 uint8_t byte; 155 156 CBS_TRACE_WRITE_START(); 157 158 len = (av_log2(value) + 7) / 7; 159 160 if (fixed_length) { 161 if (fixed_length < len) { 162 av_log(ctx->log_ctx, AV_LOG_ERROR, "OBU is too large for " 163 "fixed length size field (%d > %d).\n", 164 len, fixed_length); 165 return AVERROR(EINVAL); 166 } 167 len = fixed_length; 168 } 169 170 for (i = 0; i < len; i++) { 171 if (put_bits_left(pbc) < 8) 172 return AVERROR(ENOSPC); 173 174 byte = value >> (7 * i) & 0x7f; 175 if (i < len - 1) 176 byte |= 0x80; 177 178 put_bits(pbc, 8, byte); 179 } 180 181 CBS_TRACE_WRITE_END_NO_SUBSCRIPTS(); 182 183 return 0; 184 } 185 186 static int cbs_av1_read_ns(CodedBitstreamContext *ctx, GetBitContext *gbc, 187 uint32_t n, const char *name, 188 const int *subscripts, uint32_t *write_to) 189 { 190 uint32_t m, v, extra_bit, value; 191 int w; 192 193 CBS_TRACE_READ_START(); 194 195 av_assert0(n > 0); 196 197 w = av_log2(n) + 1; 198 m = (1 << w) - n; 199 200 if (get_bits_left(gbc) < w) { 201 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid non-symmetric value at " 202 "%s: bitstream ended.\n", name); 203 return AVERROR_INVALIDDATA; 204 } 205 206 if (w - 1 > 0) 207 v = get_bits(gbc, w - 1); 208 else 209 v = 0; 210 211 if (v < m) { 212 value = v; 213 } else { 214 extra_bit = get_bits1(gbc); 215 value = (v << 1) - m + extra_bit; 216 } 217 218 CBS_TRACE_READ_END(); 219 220 *write_to = value; 221 return 0; 222 } 223 224 static int cbs_av1_write_ns(CodedBitstreamContext *ctx, PutBitContext *pbc, 225 uint32_t n, const char *name, 226 const int *subscripts, uint32_t value) 227 { 228 uint32_t w, m, v, extra_bit; 229 230 CBS_TRACE_WRITE_START(); 231 232 if (value > n) { 233 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: " 234 "%"PRIu32", but must be in [0,%"PRIu32"].\n", 235 name, value, n); 236 return AVERROR_INVALIDDATA; 237 } 238 239 w = av_log2(n) + 1; 240 m = (1 << w) - n; 241 242 if (put_bits_left(pbc) < w) 243 return AVERROR(ENOSPC); 244 245 if (value < m) { 246 v = value; 247 put_bits(pbc, w - 1, v); 248 } else { 249 v = m + ((value - m) >> 1); 250 extra_bit = (value - m) & 1; 251 put_bits(pbc, w - 1, v); 252 put_bits(pbc, 1, extra_bit); 253 } 254 255 CBS_TRACE_WRITE_END(); 256 257 return 0; 258 } 259 260 static int cbs_av1_read_increment(CodedBitstreamContext *ctx, GetBitContext *gbc, 261 uint32_t range_min, uint32_t range_max, 262 const char *name, uint32_t *write_to) 263 { 264 uint32_t value; 265 266 CBS_TRACE_READ_START(); 267 268 av_assert0(range_min <= range_max && range_max - range_min < 32); 269 270 for (value = range_min; value < range_max;) { 271 if (get_bits_left(gbc) < 1) { 272 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid increment value at " 273 "%s: bitstream ended.\n", name); 274 return AVERROR_INVALIDDATA; 275 } 276 if (get_bits1(gbc)) 277 ++value; 278 else 279 break; 280 } 281 282 CBS_TRACE_READ_END_NO_SUBSCRIPTS(); 283 284 *write_to = value; 285 return 0; 286 } 287 288 static int cbs_av1_write_increment(CodedBitstreamContext *ctx, PutBitContext *pbc, 289 uint32_t range_min, uint32_t range_max, 290 const char *name, uint32_t value) 291 { 292 int len; 293 294 CBS_TRACE_WRITE_START(); 295 296 av_assert0(range_min <= range_max && range_max - range_min < 32); 297 if (value < range_min || value > range_max) { 298 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: " 299 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n", 300 name, value, range_min, range_max); 301 return AVERROR_INVALIDDATA; 302 } 303 304 if (value == range_max) 305 len = range_max - range_min; 306 else 307 len = value - range_min + 1; 308 if (put_bits_left(pbc) < len) 309 return AVERROR(ENOSPC); 310 311 if (len > 0) 312 put_bits(pbc, len, (1U << len) - 1 - (value != range_max)); 313 314 CBS_TRACE_WRITE_END_NO_SUBSCRIPTS(); 315 316 return 0; 317 } 318 319 static int cbs_av1_read_subexp(CodedBitstreamContext *ctx, GetBitContext *gbc, 320 uint32_t range_max, const char *name, 321 const int *subscripts, uint32_t *write_to) 322 { 323 uint32_t value, max_len, len, range_offset, range_bits; 324 int err; 325 326 CBS_TRACE_READ_START(); 327 328 av_assert0(range_max > 0); 329 max_len = av_log2(range_max - 1) - 3; 330 331 err = cbs_av1_read_increment(ctx, gbc, 0, max_len, 332 "subexp_more_bits", &len); 333 if (err < 0) 334 return err; 335 336 if (len) { 337 range_bits = 2 + len; 338 range_offset = 1 << range_bits; 339 } else { 340 range_bits = 3; 341 range_offset = 0; 342 } 343 344 if (len < max_len) { 345 err = ff_cbs_read_simple_unsigned(ctx, gbc, range_bits, 346 "subexp_bits", &value); 347 if (err < 0) 348 return err; 349 350 } else { 351 err = cbs_av1_read_ns(ctx, gbc, range_max - range_offset, 352 "subexp_final_bits", NULL, &value); 353 if (err < 0) 354 return err; 355 } 356 value += range_offset; 357 358 CBS_TRACE_READ_END_VALUE_ONLY(); 359 360 *write_to = value; 361 return err; 362 } 363 364 static int cbs_av1_write_subexp(CodedBitstreamContext *ctx, PutBitContext *pbc, 365 uint32_t range_max, const char *name, 366 const int *subscripts, uint32_t value) 367 { 368 int err; 369 uint32_t max_len, len, range_offset, range_bits; 370 371 CBS_TRACE_WRITE_START(); 372 373 if (value > range_max) { 374 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: " 375 "%"PRIu32", but must be in [0,%"PRIu32"].\n", 376 name, value, range_max); 377 return AVERROR_INVALIDDATA; 378 } 379 380 av_assert0(range_max > 0); 381 max_len = av_log2(range_max - 1) - 3; 382 383 if (value < 8) { 384 range_bits = 3; 385 range_offset = 0; 386 len = 0; 387 } else { 388 range_bits = av_log2(value); 389 len = range_bits - 2; 390 if (len > max_len) { 391 // The top bin is combined with the one below it. 392 av_assert0(len == max_len + 1); 393 --range_bits; 394 len = max_len; 395 } 396 range_offset = 1 << range_bits; 397 } 398 399 err = cbs_av1_write_increment(ctx, pbc, 0, max_len, 400 "subexp_more_bits", len); 401 if (err < 0) 402 return err; 403 404 if (len < max_len) { 405 err = ff_cbs_write_simple_unsigned(ctx, pbc, range_bits, 406 "subexp_bits", 407 value - range_offset); 408 if (err < 0) 409 return err; 410 411 } else { 412 err = cbs_av1_write_ns(ctx, pbc, range_max - range_offset, 413 "subexp_final_bits", NULL, 414 value - range_offset); 415 if (err < 0) 416 return err; 417 } 418 419 CBS_TRACE_WRITE_END_VALUE_ONLY(); 420 421 return err; 422 } 423 424 425 static int cbs_av1_tile_log2(int blksize, int target) 426 { 427 int k; 428 for (k = 0; (blksize << k) < target; k++); 429 return k; 430 } 431 432 static int cbs_av1_get_relative_dist(const AV1RawSequenceHeader *seq, 433 unsigned int a, unsigned int b) 434 { 435 unsigned int diff, m; 436 if (!seq->enable_order_hint) 437 return 0; 438 diff = a - b; 439 m = 1 << seq->order_hint_bits_minus_1; 440 diff = (diff & (m - 1)) - (diff & m); 441 return diff; 442 } 443 444 static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc) 445 { 446 GetBitContext tmp = *gbc; 447 size_t size = 0; 448 for (int i = 0; get_bits_left(&tmp) >= 8; i++) { 449 if (get_bits(&tmp, 8)) 450 size = i; 451 } 452 return size; 453 } 454 455 456 #define HEADER(name) do { \ 457 ff_cbs_trace_header(ctx, name); \ 458 } while (0) 459 460 #define CHECK(call) do { \ 461 err = (call); \ 462 if (err < 0) \ 463 return err; \ 464 } while (0) 465 466 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name 467 #define FUNC_AV1(rw, name) FUNC_NAME(rw, av1, name) 468 #define FUNC(name) FUNC_AV1(READWRITE, name) 469 470 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL) 471 472 #define fc(width, name, range_min, range_max) \ 473 xf(width, name, current->name, range_min, range_max, 0, ) 474 #define flag(name) fb(1, name) 475 #define su(width, name) \ 476 xsu(width, name, current->name, 0, ) 477 478 #define fbs(width, name, subs, ...) \ 479 xf(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__) 480 #define fcs(width, name, range_min, range_max, subs, ...) \ 481 xf(width, name, current->name, range_min, range_max, subs, __VA_ARGS__) 482 #define flags(name, subs, ...) \ 483 xf(1, name, current->name, 0, 1, subs, __VA_ARGS__) 484 #define sus(width, name, subs, ...) \ 485 xsu(width, name, current->name, subs, __VA_ARGS__) 486 487 #define fixed(width, name, value) do { \ 488 av_unused uint32_t fixed_value = value; \ 489 xf(width, name, fixed_value, value, value, 0, ); \ 490 } while (0) 491 492 493 #define READ 494 #define READWRITE read 495 #define RWContext GetBitContext 496 497 #define fb(width, name) do { \ 498 uint32_t value; \ 499 CHECK(ff_cbs_read_simple_unsigned(ctx, rw, width, \ 500 #name, &value)); \ 501 current->name = value; \ 502 } while (0) 503 504 #define xf(width, name, var, range_min, range_max, subs, ...) do { \ 505 uint32_t value; \ 506 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \ 507 SUBSCRIPTS(subs, __VA_ARGS__), \ 508 &value, range_min, range_max)); \ 509 var = value; \ 510 } while (0) 511 512 #define xsu(width, name, var, subs, ...) do { \ 513 int32_t value; \ 514 CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \ 515 SUBSCRIPTS(subs, __VA_ARGS__), &value, \ 516 MIN_INT_BITS(width), \ 517 MAX_INT_BITS(width))); \ 518 var = value; \ 519 } while (0) 520 521 #define uvlc(name, range_min, range_max) do { \ 522 uint32_t value; \ 523 CHECK(cbs_av1_read_uvlc(ctx, rw, #name, \ 524 &value, range_min, range_max)); \ 525 current->name = value; \ 526 } while (0) 527 528 #define ns(max_value, name, subs, ...) do { \ 529 uint32_t value; \ 530 CHECK(cbs_av1_read_ns(ctx, rw, max_value, #name, \ 531 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \ 532 current->name = value; \ 533 } while (0) 534 535 #define increment(name, min, max) do { \ 536 uint32_t value; \ 537 CHECK(cbs_av1_read_increment(ctx, rw, min, max, #name, &value)); \ 538 current->name = value; \ 539 } while (0) 540 541 #define subexp(name, max, subs, ...) do { \ 542 uint32_t value; \ 543 CHECK(cbs_av1_read_subexp(ctx, rw, max, #name, \ 544 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \ 545 current->name = value; \ 546 } while (0) 547 548 #define delta_q(name) do { \ 549 uint8_t delta_coded; \ 550 int8_t delta_q; \ 551 xf(1, name.delta_coded, delta_coded, 0, 1, 0, ); \ 552 if (delta_coded) \ 553 xsu(1 + 6, name.delta_q, delta_q, 0, ); \ 554 else \ 555 delta_q = 0; \ 556 current->name = delta_q; \ 557 } while (0) 558 559 #define leb128(name) do { \ 560 uint64_t value; \ 561 CHECK(cbs_av1_read_leb128(ctx, rw, #name, &value)); \ 562 current->name = value; \ 563 } while (0) 564 565 #define infer(name, value) do { \ 566 current->name = value; \ 567 } while (0) 568 569 #define byte_alignment(rw) (get_bits_count(rw) % 8) 570 571 #include "cbs_av1_syntax_template.c" 572 573 #undef READ 574 #undef READWRITE 575 #undef RWContext 576 #undef fb 577 #undef xf 578 #undef xsu 579 #undef uvlc 580 #undef ns 581 #undef increment 582 #undef subexp 583 #undef delta_q 584 #undef leb128 585 #undef infer 586 #undef byte_alignment 587 588 589 #define WRITE 590 #define READWRITE write 591 #define RWContext PutBitContext 592 593 #define fb(width, name) do { \ 594 CHECK(ff_cbs_write_simple_unsigned(ctx, rw, width, #name, \ 595 current->name)); \ 596 } while (0) 597 598 #define xf(width, name, var, range_min, range_max, subs, ...) do { \ 599 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \ 600 SUBSCRIPTS(subs, __VA_ARGS__), \ 601 var, range_min, range_max)); \ 602 } while (0) 603 604 #define xsu(width, name, var, subs, ...) do { \ 605 CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \ 606 SUBSCRIPTS(subs, __VA_ARGS__), var, \ 607 MIN_INT_BITS(width), \ 608 MAX_INT_BITS(width))); \ 609 } while (0) 610 611 #define uvlc(name, range_min, range_max) do { \ 612 CHECK(cbs_av1_write_uvlc(ctx, rw, #name, current->name, \ 613 range_min, range_max)); \ 614 } while (0) 615 616 #define ns(max_value, name, subs, ...) do { \ 617 CHECK(cbs_av1_write_ns(ctx, rw, max_value, #name, \ 618 SUBSCRIPTS(subs, __VA_ARGS__), \ 619 current->name)); \ 620 } while (0) 621 622 #define increment(name, min, max) do { \ 623 CHECK(cbs_av1_write_increment(ctx, rw, min, max, #name, \ 624 current->name)); \ 625 } while (0) 626 627 #define subexp(name, max, subs, ...) do { \ 628 CHECK(cbs_av1_write_subexp(ctx, rw, max, #name, \ 629 SUBSCRIPTS(subs, __VA_ARGS__), \ 630 current->name)); \ 631 } while (0) 632 633 #define delta_q(name) do { \ 634 xf(1, name.delta_coded, current->name != 0, 0, 1, 0, ); \ 635 if (current->name) \ 636 xsu(1 + 6, name.delta_q, current->name, 0, ); \ 637 } while (0) 638 639 #define leb128(name) do { \ 640 CHECK(cbs_av1_write_leb128(ctx, rw, #name, current->name, 0)); \ 641 } while (0) 642 643 #define infer(name, value) do { \ 644 if (current->name != (value)) { \ 645 av_log(ctx->log_ctx, AV_LOG_ERROR, \ 646 "%s does not match inferred value: " \ 647 "%"PRId64", but should be %"PRId64".\n", \ 648 #name, (int64_t)current->name, (int64_t)(value)); \ 649 return AVERROR_INVALIDDATA; \ 650 } \ 651 } while (0) 652 653 #define byte_alignment(rw) (put_bits_count(rw) % 8) 654 655 #include "cbs_av1_syntax_template.c" 656 657 #undef WRITE 658 #undef READWRITE 659 #undef RWContext 660 #undef fb 661 #undef xf 662 #undef xsu 663 #undef uvlc 664 #undef ns 665 #undef increment 666 #undef subexp 667 #undef delta_q 668 #undef leb128 669 #undef infer 670 #undef byte_alignment 671 672 673 static int cbs_av1_split_fragment(CodedBitstreamContext *ctx, 674 CodedBitstreamFragment *frag, 675 int header) 676 { 677 GetBitContext gbc; 678 uint8_t *data; 679 size_t size; 680 uint64_t obu_length; 681 int pos, err, trace; 682 683 // Don't include this parsing in trace output. 684 trace = ctx->trace_enable; 685 ctx->trace_enable = 0; 686 687 data = frag->data; 688 size = frag->data_size; 689 690 if (INT_MAX / 8 < size) { 691 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid fragment: " 692 "too large (%"SIZE_SPECIFIER" bytes).\n", size); 693 err = AVERROR_INVALIDDATA; 694 goto fail; 695 } 696 697 if (header && size && data[0] & 0x80) { 698 // first bit is nonzero, the extradata does not consist purely of 699 // OBUs. Expect MP4/Matroska AV1CodecConfigurationRecord 700 int config_record_version = data[0] & 0x7f; 701 702 if (config_record_version != 1) { 703 av_log(ctx->log_ctx, AV_LOG_ERROR, 704 "Unknown version %d of AV1CodecConfigurationRecord " 705 "found!\n", 706 config_record_version); 707 err = AVERROR_INVALIDDATA; 708 goto fail; 709 } 710 711 if (size <= 4) { 712 if (size < 4) { 713 av_log(ctx->log_ctx, AV_LOG_WARNING, 714 "Undersized AV1CodecConfigurationRecord v%d found!\n", 715 config_record_version); 716 err = AVERROR_INVALIDDATA; 717 goto fail; 718 } 719 720 goto success; 721 } 722 723 // In AV1CodecConfigurationRecord v1, actual OBUs start after 724 // four bytes. Thus set the offset as required for properly 725 // parsing them. 726 data += 4; 727 size -= 4; 728 } 729 730 while (size > 0) { 731 AV1RawOBUHeader obu_header; 732 uint64_t obu_size; 733 734 init_get_bits(&gbc, data, 8 * size); 735 736 err = cbs_av1_read_obu_header(ctx, &gbc, &obu_header); 737 if (err < 0) 738 goto fail; 739 740 if (obu_header.obu_has_size_field) { 741 if (get_bits_left(&gbc) < 8) { 742 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU: fragment " 743 "too short (%"SIZE_SPECIFIER" bytes).\n", size); 744 err = AVERROR_INVALIDDATA; 745 goto fail; 746 } 747 err = cbs_av1_read_leb128(ctx, &gbc, "obu_size", &obu_size); 748 if (err < 0) 749 goto fail; 750 } else 751 obu_size = size - 1 - obu_header.obu_extension_flag; 752 753 pos = get_bits_count(&gbc); 754 av_assert0(pos % 8 == 0 && pos / 8 <= size); 755 756 obu_length = pos / 8 + obu_size; 757 758 if (size < obu_length) { 759 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU length: " 760 "%"PRIu64", but only %"SIZE_SPECIFIER" bytes remaining in fragment.\n", 761 obu_length, size); 762 err = AVERROR_INVALIDDATA; 763 goto fail; 764 } 765 766 err = ff_cbs_append_unit_data(frag, obu_header.obu_type, 767 data, obu_length, frag->data_ref); 768 if (err < 0) 769 goto fail; 770 771 data += obu_length; 772 size -= obu_length; 773 } 774 775 success: 776 err = 0; 777 fail: 778 ctx->trace_enable = trace; 779 return err; 780 } 781 782 static int cbs_av1_ref_tile_data(CodedBitstreamContext *ctx, 783 CodedBitstreamUnit *unit, 784 GetBitContext *gbc, 785 AV1RawTileData *td) 786 { 787 int pos; 788 789 pos = get_bits_count(gbc); 790 if (pos >= 8 * unit->data_size) { 791 av_log(ctx->log_ctx, AV_LOG_ERROR, "Bitstream ended before " 792 "any data in tile group (%d bits read).\n", pos); 793 return AVERROR_INVALIDDATA; 794 } 795 // Must be byte-aligned at this point. 796 av_assert0(pos % 8 == 0); 797 798 td->data_ref = av_buffer_ref(unit->data_ref); 799 if (!td->data_ref) 800 return AVERROR(ENOMEM); 801 802 td->data = unit->data + pos / 8; 803 td->data_size = unit->data_size - pos / 8; 804 805 return 0; 806 } 807 808 static int cbs_av1_read_unit(CodedBitstreamContext *ctx, 809 CodedBitstreamUnit *unit) 810 { 811 CodedBitstreamAV1Context *priv = ctx->priv_data; 812 AV1RawOBU *obu; 813 GetBitContext gbc; 814 int err, start_pos, end_pos; 815 816 err = ff_cbs_alloc_unit_content(ctx, unit); 817 if (err < 0) 818 return err; 819 obu = unit->content; 820 821 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size); 822 if (err < 0) 823 return err; 824 825 err = cbs_av1_read_obu_header(ctx, &gbc, &obu->header); 826 if (err < 0) 827 return err; 828 av_assert0(obu->header.obu_type == unit->type); 829 830 if (obu->header.obu_has_size_field) { 831 uint64_t obu_size; 832 err = cbs_av1_read_leb128(ctx, &gbc, "obu_size", &obu_size); 833 if (err < 0) 834 return err; 835 obu->obu_size = obu_size; 836 } else { 837 if (unit->data_size < 1 + obu->header.obu_extension_flag) { 838 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU length: " 839 "unit too short (%"SIZE_SPECIFIER").\n", unit->data_size); 840 return AVERROR_INVALIDDATA; 841 } 842 obu->obu_size = unit->data_size - 1 - obu->header.obu_extension_flag; 843 } 844 845 start_pos = get_bits_count(&gbc); 846 847 if (obu->header.obu_extension_flag) { 848 if (obu->header.obu_type != AV1_OBU_SEQUENCE_HEADER && 849 obu->header.obu_type != AV1_OBU_TEMPORAL_DELIMITER && 850 priv->operating_point_idc) { 851 int in_temporal_layer = 852 (priv->operating_point_idc >> priv->temporal_id ) & 1; 853 int in_spatial_layer = 854 (priv->operating_point_idc >> (priv->spatial_id + 8)) & 1; 855 if (!in_temporal_layer || !in_spatial_layer) { 856 return AVERROR(EAGAIN); // drop_obu() 857 } 858 } 859 } 860 861 switch (obu->header.obu_type) { 862 case AV1_OBU_SEQUENCE_HEADER: 863 { 864 err = cbs_av1_read_sequence_header_obu(ctx, &gbc, 865 &obu->obu.sequence_header); 866 if (err < 0) 867 return err; 868 869 if (priv->operating_point >= 0) { 870 AV1RawSequenceHeader *sequence_header = &obu->obu.sequence_header; 871 872 if (priv->operating_point > sequence_header->operating_points_cnt_minus_1) { 873 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid Operating Point %d requested. " 874 "Must not be higher than %u.\n", 875 priv->operating_point, sequence_header->operating_points_cnt_minus_1); 876 return AVERROR(EINVAL); 877 } 878 priv->operating_point_idc = sequence_header->operating_point_idc[priv->operating_point]; 879 } 880 881 av_refstruct_replace(&priv->sequence_header_ref, unit->content_ref); 882 priv->sequence_header = &obu->obu.sequence_header; 883 } 884 break; 885 case AV1_OBU_TEMPORAL_DELIMITER: 886 { 887 err = cbs_av1_read_temporal_delimiter_obu(ctx, &gbc); 888 if (err < 0) 889 return err; 890 } 891 break; 892 case AV1_OBU_FRAME_HEADER: 893 case AV1_OBU_REDUNDANT_FRAME_HEADER: 894 { 895 err = cbs_av1_read_frame_header_obu(ctx, &gbc, 896 &obu->obu.frame_header, 897 obu->header.obu_type == 898 AV1_OBU_REDUNDANT_FRAME_HEADER, 899 unit->data_ref); 900 if (err < 0) 901 return err; 902 } 903 break; 904 case AV1_OBU_TILE_GROUP: 905 { 906 err = cbs_av1_read_tile_group_obu(ctx, &gbc, 907 &obu->obu.tile_group); 908 if (err < 0) 909 return err; 910 911 err = cbs_av1_ref_tile_data(ctx, unit, &gbc, 912 &obu->obu.tile_group.tile_data); 913 if (err < 0) 914 return err; 915 } 916 break; 917 case AV1_OBU_FRAME: 918 { 919 err = cbs_av1_read_frame_obu(ctx, &gbc, &obu->obu.frame, 920 unit->data_ref); 921 if (err < 0) 922 return err; 923 924 err = cbs_av1_ref_tile_data(ctx, unit, &gbc, 925 &obu->obu.frame.tile_group.tile_data); 926 if (err < 0) 927 return err; 928 } 929 break; 930 case AV1_OBU_TILE_LIST: 931 { 932 err = cbs_av1_read_tile_list_obu(ctx, &gbc, 933 &obu->obu.tile_list); 934 if (err < 0) 935 return err; 936 937 err = cbs_av1_ref_tile_data(ctx, unit, &gbc, 938 &obu->obu.tile_list.tile_data); 939 if (err < 0) 940 return err; 941 } 942 break; 943 case AV1_OBU_METADATA: 944 { 945 err = cbs_av1_read_metadata_obu(ctx, &gbc, &obu->obu.metadata); 946 if (err < 0) 947 return err; 948 } 949 break; 950 case AV1_OBU_PADDING: 951 { 952 err = cbs_av1_read_padding_obu(ctx, &gbc, &obu->obu.padding); 953 if (err < 0) 954 return err; 955 } 956 break; 957 default: 958 return AVERROR(ENOSYS); 959 } 960 961 end_pos = get_bits_count(&gbc); 962 av_assert0(end_pos <= unit->data_size * 8); 963 964 if (obu->obu_size > 0 && 965 obu->header.obu_type != AV1_OBU_TILE_GROUP && 966 obu->header.obu_type != AV1_OBU_TILE_LIST && 967 obu->header.obu_type != AV1_OBU_FRAME) { 968 int nb_bits = obu->obu_size * 8 + start_pos - end_pos; 969 970 if (nb_bits <= 0) 971 return AVERROR_INVALIDDATA; 972 973 err = cbs_av1_read_trailing_bits(ctx, &gbc, nb_bits); 974 if (err < 0) 975 return err; 976 } 977 978 return 0; 979 } 980 981 static int cbs_av1_write_obu(CodedBitstreamContext *ctx, 982 CodedBitstreamUnit *unit, 983 PutBitContext *pbc) 984 { 985 CodedBitstreamAV1Context *priv = ctx->priv_data; 986 AV1RawOBU *obu = unit->content; 987 PutBitContext pbc_tmp; 988 AV1RawTileData *td; 989 size_t header_size; 990 int err, start_pos, end_pos, data_pos; 991 CodedBitstreamAV1Context av1ctx; 992 993 // OBUs in the normal bitstream format must contain a size field 994 // in every OBU (in annex B it is optional, but we don't support 995 // writing that). 996 obu->header.obu_has_size_field = 1; 997 av1ctx = *priv; 998 999 if (priv->sequence_header_ref) { 1000 av1ctx.sequence_header_ref = av_refstruct_ref(priv->sequence_header_ref); 1001 } 1002 1003 if (priv->frame_header_ref) { 1004 av1ctx.frame_header_ref = av_buffer_ref(priv->frame_header_ref); 1005 if (!av1ctx.frame_header_ref) { 1006 err = AVERROR(ENOMEM); 1007 goto error; 1008 } 1009 } 1010 1011 err = cbs_av1_write_obu_header(ctx, pbc, &obu->header); 1012 if (err < 0) 1013 goto error; 1014 1015 if (obu->header.obu_has_size_field) { 1016 pbc_tmp = *pbc; 1017 if (priv->fixed_obu_size_length) { 1018 for (int i = 0; i < priv->fixed_obu_size_length; i++) 1019 put_bits(pbc, 8, 0); 1020 } else { 1021 // Add space for the size field to fill later. 1022 put_bits32(pbc, 0); 1023 put_bits32(pbc, 0); 1024 } 1025 } 1026 1027 td = NULL; 1028 start_pos = put_bits_count(pbc); 1029 1030 switch (obu->header.obu_type) { 1031 case AV1_OBU_SEQUENCE_HEADER: 1032 { 1033 err = cbs_av1_write_sequence_header_obu(ctx, pbc, 1034 &obu->obu.sequence_header); 1035 if (err < 0) 1036 goto error; 1037 1038 av_refstruct_unref(&priv->sequence_header_ref); 1039 priv->sequence_header = NULL; 1040 1041 err = ff_cbs_make_unit_refcounted(ctx, unit); 1042 if (err < 0) 1043 goto error; 1044 1045 priv->sequence_header_ref = av_refstruct_ref(unit->content_ref); 1046 priv->sequence_header = &obu->obu.sequence_header; 1047 } 1048 break; 1049 case AV1_OBU_TEMPORAL_DELIMITER: 1050 { 1051 err = cbs_av1_write_temporal_delimiter_obu(ctx, pbc); 1052 if (err < 0) 1053 goto error; 1054 } 1055 break; 1056 case AV1_OBU_FRAME_HEADER: 1057 case AV1_OBU_REDUNDANT_FRAME_HEADER: 1058 { 1059 err = cbs_av1_write_frame_header_obu(ctx, pbc, 1060 &obu->obu.frame_header, 1061 obu->header.obu_type == 1062 AV1_OBU_REDUNDANT_FRAME_HEADER, 1063 NULL); 1064 if (err < 0) 1065 goto error; 1066 } 1067 break; 1068 case AV1_OBU_TILE_GROUP: 1069 { 1070 err = cbs_av1_write_tile_group_obu(ctx, pbc, 1071 &obu->obu.tile_group); 1072 if (err < 0) 1073 goto error; 1074 1075 td = &obu->obu.tile_group.tile_data; 1076 } 1077 break; 1078 case AV1_OBU_FRAME: 1079 { 1080 err = cbs_av1_write_frame_obu(ctx, pbc, &obu->obu.frame, NULL); 1081 if (err < 0) 1082 goto error; 1083 1084 td = &obu->obu.frame.tile_group.tile_data; 1085 } 1086 break; 1087 case AV1_OBU_TILE_LIST: 1088 { 1089 err = cbs_av1_write_tile_list_obu(ctx, pbc, &obu->obu.tile_list); 1090 if (err < 0) 1091 goto error; 1092 1093 td = &obu->obu.tile_list.tile_data; 1094 } 1095 break; 1096 case AV1_OBU_METADATA: 1097 { 1098 err = cbs_av1_write_metadata_obu(ctx, pbc, &obu->obu.metadata); 1099 if (err < 0) 1100 goto error; 1101 } 1102 break; 1103 case AV1_OBU_PADDING: 1104 { 1105 err = cbs_av1_write_padding_obu(ctx, pbc, &obu->obu.padding); 1106 if (err < 0) 1107 goto error; 1108 } 1109 break; 1110 default: 1111 err = AVERROR(ENOSYS); 1112 goto error; 1113 } 1114 1115 end_pos = put_bits_count(pbc); 1116 header_size = (end_pos - start_pos + 7) / 8; 1117 if (td) { 1118 obu->obu_size = header_size + td->data_size; 1119 } else if (header_size > 0) { 1120 // Add trailing bits and recalculate. 1121 err = cbs_av1_write_trailing_bits(ctx, pbc, 8 - end_pos % 8); 1122 if (err < 0) 1123 goto error; 1124 end_pos = put_bits_count(pbc); 1125 obu->obu_size = header_size = (end_pos - start_pos + 7) / 8; 1126 } else { 1127 // Empty OBU. 1128 obu->obu_size = 0; 1129 } 1130 1131 end_pos = put_bits_count(pbc); 1132 // Must now be byte-aligned. 1133 av_assert0(end_pos % 8 == 0); 1134 flush_put_bits(pbc); 1135 start_pos /= 8; 1136 end_pos /= 8; 1137 1138 *pbc = pbc_tmp; 1139 err = cbs_av1_write_leb128(ctx, pbc, "obu_size", obu->obu_size, 1140 priv->fixed_obu_size_length); 1141 if (err < 0) 1142 goto error; 1143 1144 data_pos = put_bits_count(pbc) / 8; 1145 flush_put_bits(pbc); 1146 av_assert0(data_pos <= start_pos); 1147 1148 if (8 * obu->obu_size > put_bits_left(pbc)) { 1149 av_refstruct_unref(&priv->sequence_header_ref); 1150 av_buffer_unref(&priv->frame_header_ref); 1151 *priv = av1ctx; 1152 1153 return AVERROR(ENOSPC); 1154 } 1155 1156 if (obu->obu_size > 0) { 1157 if (!priv->fixed_obu_size_length) { 1158 memmove(pbc->buf + data_pos, 1159 pbc->buf + start_pos, header_size); 1160 } else { 1161 // The size was fixed so the following data was 1162 // already written in the correct place. 1163 } 1164 skip_put_bytes(pbc, header_size); 1165 1166 if (td) { 1167 memcpy(pbc->buf + data_pos + header_size, 1168 td->data, td->data_size); 1169 skip_put_bytes(pbc, td->data_size); 1170 } 1171 } 1172 1173 // OBU data must be byte-aligned. 1174 av_assert0(put_bits_count(pbc) % 8 == 0); 1175 err = 0; 1176 1177 error: 1178 av_refstruct_unref(&av1ctx.sequence_header_ref); 1179 av_buffer_unref(&av1ctx.frame_header_ref); 1180 1181 return err; 1182 } 1183 1184 static int cbs_av1_assemble_fragment(CodedBitstreamContext *ctx, 1185 CodedBitstreamFragment *frag) 1186 { 1187 size_t size, pos; 1188 int i; 1189 1190 size = 0; 1191 for (i = 0; i < frag->nb_units; i++) 1192 size += frag->units[i].data_size; 1193 1194 frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE); 1195 if (!frag->data_ref) 1196 return AVERROR(ENOMEM); 1197 frag->data = frag->data_ref->data; 1198 memset(frag->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE); 1199 1200 pos = 0; 1201 for (i = 0; i < frag->nb_units; i++) { 1202 memcpy(frag->data + pos, frag->units[i].data, 1203 frag->units[i].data_size); 1204 pos += frag->units[i].data_size; 1205 } 1206 av_assert0(pos == size); 1207 frag->data_size = size; 1208 1209 return 0; 1210 } 1211 1212 static void cbs_av1_flush(CodedBitstreamContext *ctx) 1213 { 1214 CodedBitstreamAV1Context *priv = ctx->priv_data; 1215 1216 av_buffer_unref(&priv->frame_header_ref); 1217 priv->sequence_header = NULL; 1218 priv->frame_header = NULL; 1219 1220 memset(priv->ref, 0, sizeof(priv->ref)); 1221 priv->operating_point_idc = 0; 1222 priv->seen_frame_header = 0; 1223 priv->tile_num = 0; 1224 } 1225 1226 static void cbs_av1_close(CodedBitstreamContext *ctx) 1227 { 1228 CodedBitstreamAV1Context *priv = ctx->priv_data; 1229 1230 av_refstruct_unref(&priv->sequence_header_ref); 1231 av_buffer_unref(&priv->frame_header_ref); 1232 } 1233 1234 static void cbs_av1_free_metadata(AVRefStructOpaque unused, void *content) 1235 { 1236 AV1RawOBU *obu = content; 1237 AV1RawMetadata *md; 1238 1239 av_assert0(obu->header.obu_type == AV1_OBU_METADATA); 1240 md = &obu->obu.metadata; 1241 1242 switch (md->metadata_type) { 1243 case AV1_METADATA_TYPE_HDR_CLL: 1244 case AV1_METADATA_TYPE_HDR_MDCV: 1245 case AV1_METADATA_TYPE_SCALABILITY: 1246 case AV1_METADATA_TYPE_TIMECODE: 1247 break; 1248 case AV1_METADATA_TYPE_ITUT_T35: 1249 av_buffer_unref(&md->metadata.itut_t35.payload_ref); 1250 break; 1251 default: 1252 av_buffer_unref(&md->metadata.unknown.payload_ref); 1253 } 1254 } 1255 1256 static const CodedBitstreamUnitTypeDescriptor cbs_av1_unit_types[] = { 1257 CBS_UNIT_TYPE_POD(AV1_OBU_SEQUENCE_HEADER, AV1RawOBU), 1258 CBS_UNIT_TYPE_POD(AV1_OBU_TEMPORAL_DELIMITER, AV1RawOBU), 1259 CBS_UNIT_TYPE_POD(AV1_OBU_FRAME_HEADER, AV1RawOBU), 1260 CBS_UNIT_TYPE_POD(AV1_OBU_REDUNDANT_FRAME_HEADER, AV1RawOBU), 1261 1262 CBS_UNIT_TYPE_INTERNAL_REF(AV1_OBU_TILE_GROUP, AV1RawOBU, 1263 obu.tile_group.tile_data.data), 1264 CBS_UNIT_TYPE_INTERNAL_REF(AV1_OBU_FRAME, AV1RawOBU, 1265 obu.frame.tile_group.tile_data.data), 1266 CBS_UNIT_TYPE_INTERNAL_REF(AV1_OBU_TILE_LIST, AV1RawOBU, 1267 obu.tile_list.tile_data.data), 1268 CBS_UNIT_TYPE_INTERNAL_REF(AV1_OBU_PADDING, AV1RawOBU, 1269 obu.padding.payload), 1270 1271 CBS_UNIT_TYPE_COMPLEX(AV1_OBU_METADATA, AV1RawOBU, 1272 &cbs_av1_free_metadata), 1273 1274 CBS_UNIT_TYPE_END_OF_LIST 1275 }; 1276 1277 #define OFFSET(x) offsetof(CodedBitstreamAV1Context, x) 1278 static const AVOption cbs_av1_options[] = { 1279 { "operating_point", "Set operating point to select layers to parse from a scalable bitstream", 1280 OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, AV1_MAX_OPERATING_POINTS - 1, 0 }, 1281 { "fixed_obu_size_length", "Set fixed length of the obu_size field", 1282 OFFSET(fixed_obu_size_length), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 8, 0 }, 1283 { NULL } 1284 }; 1285 1286 static const AVClass cbs_av1_class = { 1287 .class_name = "cbs_av1", 1288 .item_name = av_default_item_name, 1289 .option = cbs_av1_options, 1290 .version = LIBAVUTIL_VERSION_INT, 1291 }; 1292 1293 const CodedBitstreamType ff_cbs_type_av1 = { 1294 .codec_id = AV_CODEC_ID_AV1, 1295 1296 .priv_class = &cbs_av1_class, 1297 .priv_data_size = sizeof(CodedBitstreamAV1Context), 1298 1299 .unit_types = cbs_av1_unit_types, 1300 1301 .split_fragment = &cbs_av1_split_fragment, 1302 .read_unit = &cbs_av1_read_unit, 1303 .write_unit = &cbs_av1_write_obu, 1304 .assemble_fragment = &cbs_av1_assemble_fragment, 1305 1306 .flush = &cbs_av1_flush, 1307 .close = &cbs_av1_close, 1308 };