| @@ -429,3 +429,208 @@ av_csp_trc_function av_csp_trc_func_inv_from_id(enum AVColorTransferCharacterist · 428 unmodified lines |
| 429 | 429 | return NULL; |
| 430 | 430 | return trc_inv_funcs[trc]; |
| 431 | 431 | } |
| 432 | + |
| 433 | +static void eotf_linear(const double Lw, const double Lb, double E[3]) |
| 434 | +{ |
| 435 | + for (int i = 0; i < 3; i++) |
| 436 | + E[i] = (Lw - Lb) * E[i] + Lb; |
| 437 | +} |
| 438 | + |
| 439 | +static void eotf_linear_inv(const double Lw, const double Lb, double L[3]) |
| 440 | +{ |
| 441 | + for (int i = 0; i < 3; i++) |
| 442 | + L[i] = (L[i] - Lb) / (Lw - Lb); |
| 443 | +} |
| 444 | + |
| 445 | +#define WRAP_SDR_OETF(name) \ |
| 446 | +static void oetf_##name(double L[3]) \ |
| 447 | +{ \ |
| 448 | + for (int i = 0; i < 3; i++) \ |
| 449 | + L[i] = trc_##name(L[i]); \ |
| 450 | +} \ |
| 451 | + \ |
| 452 | +static void oetf_##name##_inv(double E[3]) \ |
| 453 | +{ \ |
| 454 | + for (int i = 0; i < 3; i++) \ |
| 455 | + E[i] = trc_##name##_inv(E[i]); \ |
| 456 | +} |
| 457 | + |
| 458 | +WRAP_SDR_OETF(gamma22) |
| 459 | +WRAP_SDR_OETF(gamma28) |
| 460 | +WRAP_SDR_OETF(iec61966_2_1) |
| 461 | + |
| 462 | +#define WRAP_SDR_EOTF(name) \ |
| 463 | +static void eotf_##name(double Lw, double Lb, double E[3]) \ |
| 464 | +{ \ |
| 465 | + oetf_##name##_inv(E); \ |
| 466 | + eotf_linear(Lw, Lb, E); \ |
| 467 | +} \ |
| 468 | + \ |
| 469 | +static void eotf_##name##_inv(double Lw, double Lb, double L[3]) \ |
| 470 | +{ \ |
| 471 | + eotf_linear_inv(Lw, Lb, L); \ |
| 472 | + oetf_##name(L); \ |
| 473 | +} |
| 474 | + |
| 475 | +WRAP_SDR_EOTF(gamma22) |
| 476 | +WRAP_SDR_EOTF(gamma28) |
| 477 | +WRAP_SDR_EOTF(iec61966_2_1) |
| 478 | + |
| 479 | +static void eotf_bt1886(const double Lw, const double Lb, double E[3]) |
| 480 | +{ |
| 481 | + const double Lw_inv = pow(Lw, 1.0 / 2.4); |
| 482 | + const double Lb_inv = pow(Lb, 1.0 / 2.4); |
| 483 | + const double a = pow(Lw_inv - Lb_inv, 2.4); |
| 484 | + const double b = Lb_inv / (Lw_inv - Lb_inv); |
| 485 | + |
| 486 | + for (int i = 0; i < 3; i++) |
| 487 | + E[i] = (-b > E[i]) ? 0.0 : a * pow(E[i] + b, 2.4); |
| 488 | +} |
| 489 | + |
| 490 | +static void eotf_bt1886_inv(const double Lw, const double Lb, double L[3]) |
| 491 | +{ |
| 492 | + const double Lw_inv = pow(Lw, 1.0 / 2.4); |
| 493 | + const double Lb_inv = pow(Lb, 1.0 / 2.4); |
| 494 | + const double a = pow(Lw_inv - Lb_inv, 2.4); |
| 495 | + const double b = Lb_inv / (Lw_inv - Lb_inv); |
| 496 | + |
| 497 | + for (int i = 0; i < 3; i++) |
| 498 | + L[i] = (0.0 > L[i]) ? 0.0 : pow(L[i] / a, 1.0 / 2.4) - b; |
| 499 | +} |
| 500 | + |
| 501 | +static void eotf_smpte_st2084(const double Lw, const double Lb, double E[3]) |
| 502 | +{ |
| 503 | + for (int i = 0; i < 3; i++) |
| 504 | + E[i] = trc_smpte_st2084_inv(E[i]); |
| 505 | +} |
| 506 | + |
| 507 | +static void eotf_smpte_st2084_inv(const double Lw, const double Lb, double L[3]) |
| 508 | +{ |
| 509 | + for (int i = 0; i < 3; i++) |
| 510 | + L[i] = trc_smpte_st2084(L[i]); |
| 511 | +} |
| 512 | + |
| 513 | +/* This implementation assumes an SMPTE RP 431-2 reference projector (DCI) */ |
| 514 | +#define DCI_L 48.00 |
| 515 | +#define DCI_P 52.37 |
| 516 | +#define DCI_X (42.94 / DCI_L) |
| 517 | +#define DCI_Z (45.82 / DCI_L) |
| 518 | + |
| 519 | +static void eotf_smpte_st428_1(const double Lw_Y, const double Lb_Y, double E[3]) |
| 520 | +{ |
| 521 | + const double Lw[3] = { DCI_X * Lw_Y, Lw_Y, DCI_Z * Lw_Y }; |
| 522 | + const double Lb[3] = { DCI_X * Lb_Y, Lb_Y, DCI_Z * Lb_Y }; |
| 523 | + |
| 524 | + for (int i = 0; i < 3; i++) { |
| 525 | + E[i] = (0.0 > E[i]) ? 0.0 : pow(E[i], 2.6) * DCI_P / DCI_L; |
| 526 | + E[i] = E[i] * (Lw[i] - Lb[i]) + Lb[i]; |
| 527 | + } |
| 528 | +} |
| 529 | + |
| 530 | +static void eotf_smpte_st428_1_inv(const double Lw_Y, const double Lb_Y, double L[3]) |
| 531 | +{ |
| 532 | + const double Lw[3] = { DCI_X * Lw_Y, Lw_Y, DCI_Z * Lw_Y }; |
| 533 | + const double Lb[3] = { DCI_X * Lb_Y, Lb_Y, DCI_Z * Lb_Y }; |
| 534 | + |
| 535 | + for (int i = 0; i < 3; i++) { |
| 536 | + L[i] = (L[i] - Lb[i]) / (Lw[i] - Lb[i]); |
| 537 | + L[i] = (0.0 > L[i]) ? 0.0 : pow(L[i] * DCI_L / DCI_P, 1.0 / 2.6); |
| 538 | + } |
| 539 | +} |
| 540 | + |
| 541 | +static void eotf_arib_std_b67(const double Lw, const double Lb, double E[3]) |
| 542 | +{ |
| 543 | + const double gamma = fmax(1.2 + 0.42 * log10(Lw / 1000.0), 1.0); |
| 544 | + |
| 545 | + /** |
| 546 | + * Note: This equation is technically only accurate if the contrast ratio |
| 547 | + * Lw:Lb is greater than 12:1; otherwise we would need to use a different, |
| 548 | + * significantly more complicated solution. Ignore this as a highly |
| 549 | + * degenerate case, since any real world reference display will have a |
| 550 | + * static contrast ratio multiple orders of magnitude higher. |
| 551 | + */ |
| 552 | + const double beta = sqrt(3 * pow(Lb / Lw, 1.0 / gamma)); |
| 553 | + double luma; |
| 554 | + |
| 555 | + for (int i = 0; i < 3; i++) |
| 556 | + E[i] = trc_arib_std_b67_inv((1 - beta) * E[i] + beta); |
| 557 | + |
| 558 | + luma = 0.2627 * E[0] + 0.6780 * E[1] + 0.0593 * E[2]; |
| 559 | + luma = pow(fmax(luma, 0.0), gamma - 1.0); |
| 560 | + for (int i = 0; i < 3; i++) |
| 561 | + E[i] *= Lw * luma; |
| 562 | +} |
| 563 | + |
| 564 | +static void eotf_arib_std_b67_inv(const double Lw, const double Lb, double L[3]) |
| 565 | +{ |
| 566 | + const double gamma = fmax(1.2 + 0.42 * log10(Lw / 1000.0), 1.0); |
| 567 | + const double beta = sqrt(3 * pow(Lb / Lw, 1 / gamma)); |
| 568 | + double luma = 0.2627 * L[0] + 0.6780 * L[1] + 0.0593 * L[2]; |
| 569 | + |
| 570 | + if (luma > 0.0) { |
| 571 | + luma = pow(luma / Lw, (1 - gamma) / gamma); |
| 572 | + for (int i = 0; i < 3; i++) |
| 573 | + L[i] *= luma / Lw; |
| 574 | + } else { |
| 575 | + L[0] = L[1] = L[2] = 0.0; |
| 576 | + } |
| 577 | + |
| 578 | + for (int i = 0; i < 3; i++) |
| 579 | + L[i] = (trc_arib_std_b67(L[i]) - beta) / (1 - beta); |
| 580 | +} |
| 581 | + |
| 582 | +static const av_csp_eotf_function eotf_funcs[AVCOL_TRC_NB] = { |
| 583 | + [AVCOL_TRC_BT709] = eotf_bt1886, |
| 584 | + [AVCOL_TRC_GAMMA22] = eotf_gamma22, |
| 585 | + [AVCOL_TRC_GAMMA28] = eotf_gamma28, |
| 586 | + [AVCOL_TRC_SMPTE170M] = eotf_bt1886, |
| 587 | + [AVCOL_TRC_SMPTE240M] = eotf_bt1886, |
| 588 | + [AVCOL_TRC_LINEAR] = eotf_linear, |
| 589 | + /* There is no EOTF associated with these logarithmic encodings, since they |
| 590 | + * are defined purely for transmission of scene referred data. */ |
| 591 | + [AVCOL_TRC_LOG] = NULL, |
| 592 | + [AVCOL_TRC_LOG_SQRT] = NULL, |
| 593 | + /* BT.1886 is already defined for values below 0.0, as far as physically |
| 594 | + * meaningful, so we can directly use it for extended range encodings */ |
| 595 | + [AVCOL_TRC_IEC61966_2_4] = eotf_bt1886, |
| 596 | + [AVCOL_TRC_BT1361_ECG] = eotf_bt1886, |
| 597 | + [AVCOL_TRC_IEC61966_2_1] = eotf_iec61966_2_1, |
| 598 | + [AVCOL_TRC_BT2020_10] = eotf_bt1886, |
| 599 | + [AVCOL_TRC_BT2020_12] = eotf_bt1886, |
| 600 | + [AVCOL_TRC_SMPTE2084] = eotf_smpte_st2084, |
| 601 | + [AVCOL_TRC_SMPTE428] = eotf_smpte_st428_1, |
| 602 | + [AVCOL_TRC_ARIB_STD_B67] = eotf_arib_std_b67, |
| 603 | +}; |
| 604 | + |
| 605 | +av_csp_eotf_function av_csp_itu_eotf(enum AVColorTransferCharacteristic trc) |
| 606 | +{ |
| 607 | + if (trc < 0 || trc >= AVCOL_TRC_NB) |
| 608 | + return NULL; |
| 609 | + return eotf_funcs[trc]; |
| 610 | +} |
| 611 | + |
| 612 | +static const av_csp_eotf_function eotf_inv_funcs[AVCOL_TRC_NB] = { |
| 613 | + [AVCOL_TRC_BT709] = eotf_bt1886_inv, |
| 614 | + [AVCOL_TRC_GAMMA22] = eotf_gamma22_inv, |
| 615 | + [AVCOL_TRC_GAMMA28] = eotf_gamma28_inv, |
| 616 | + [AVCOL_TRC_SMPTE170M] = eotf_bt1886_inv, |
| 617 | + [AVCOL_TRC_SMPTE240M] = eotf_bt1886_inv, |
| 618 | + [AVCOL_TRC_LINEAR] = eotf_linear_inv, |
| 619 | + [AVCOL_TRC_LOG] = NULL, |
| 620 | + [AVCOL_TRC_LOG_SQRT] = NULL, |
| 621 | + [AVCOL_TRC_IEC61966_2_4] = eotf_bt1886_inv, |
| 622 | + [AVCOL_TRC_BT1361_ECG] = eotf_bt1886_inv, |
| 623 | + [AVCOL_TRC_IEC61966_2_1] = eotf_iec61966_2_1_inv, |
| 624 | + [AVCOL_TRC_BT2020_10] = eotf_bt1886_inv, |
| 625 | + [AVCOL_TRC_BT2020_12] = eotf_bt1886_inv, |
| 626 | + [AVCOL_TRC_SMPTE2084] = eotf_smpte_st2084_inv, |
| 627 | + [AVCOL_TRC_SMPTE428] = eotf_smpte_st428_1_inv, |
| 628 | + [AVCOL_TRC_ARIB_STD_B67] = eotf_arib_std_b67_inv, |
| 629 | +}; |
| 630 | + |
| 631 | +av_csp_eotf_function av_csp_itu_eotf_inv(enum AVColorTransferCharacteristic trc) |
| 632 | +{ |
| 633 | + if (trc < 0 || trc >= AVCOL_TRC_NB) |
| 634 | + return NULL; |
| 635 | + return eotf_inv_funcs[trc]; |
| 636 | +} |