These chunks contain the Content Light Level Information and the Mastering Display Color Volume information that FFmpeg already supports as AVFrameSideData. This patch adds support for the png decoder to read these chunks if present and attach the corresponding side data to the decoded frame.
Showing the changes to libavcodec (1 of 1 file). Show all
Unified · Split
| @@ -29,6 +29,7 @@ · 28 unmodified lines | |||
| 29 | #include "libavutil/csp.h" | 29 | #include "libavutil/csp.h" |
| 30 | #include "libavutil/imgutils.h" | 30 | #include "libavutil/imgutils.h" |
| 31 | #include "libavutil/intreadwrite.h" | 31 | #include "libavutil/intreadwrite.h" |
| 32 | #include "libavutil/mastering_display_metadata.h" | ||
| 32 | #include "libavutil/pixfmt.h" | 33 | #include "libavutil/pixfmt.h" |
| 33 | #include "libavutil/rational.h" | 34 | #include "libavutil/rational.h" |
| 34 | #include "libavutil/stereo3d.h" | 35 | #include "libavutil/stereo3d.h" |
| @@ -81,6 +82,14 @@ typedef struct PNGDecContext { · 46 unmodified lines | |||
| 81 | enum AVColorPrimaries cicp_primaries; | 82 | enum AVColorPrimaries cicp_primaries; |
| 82 | enum AVColorTransferCharacteristic cicp_trc; | 83 | enum AVColorTransferCharacteristic cicp_trc; |
| 83 | enum AVColorRange cicp_range; | 84 | enum AVColorRange cicp_range; |
| 85 | int have_clli; | ||
| 86 | uint32_t clli_max; | ||
| 87 | uint32_t clli_avg; | ||
| 88 | int have_mdvc; | ||
| 89 | uint16_t mdvc_primaries[3][2]; | ||
| 90 | uint16_t mdvc_white_point[2]; | ||
| 91 | uint32_t mdvc_max_lum; | ||
| 92 | uint32_t mdvc_min_lum; | ||
| 84 | 93 | ||
| 85 | enum PNGHeaderState hdr_state; | 94 | enum PNGHeaderState hdr_state; |
| 86 | enum PNGImageState pic_state; | 95 | enum PNGImageState pic_state; |
| @@ -731,6 +740,36 @@ static int populate_avctx_color_fields(AVCodecContext *avctx, AVFrame *frame) · 644 unmodified lines | |||
| 731 | if (!s->has_trns && s->significant_bits > 0) | 740 | if (!s->has_trns && s->significant_bits > 0) |
| 732 | avctx->bits_per_raw_sample = s->significant_bits; | 741 | avctx->bits_per_raw_sample = s->significant_bits; |
| 733 | 742 | ||
| 743 | if (s->have_clli) { | ||
| 744 | AVContentLightMetadata *clli = | ||
| 745 | av_content_light_metadata_create_side_data(frame); | ||
| 746 | if (!clli) | ||
| 747 | return AVERROR(ENOMEM); | ||
| 748 | /* | ||
| 749 | * 0.0001 divisor value | ||
| 750 | * see: https://www.w3.org/TR/png-3/#cLLi-chunk | ||
| 751 | */ | ||
| 752 | clli->MaxCLL = s->clli_max / 10000; | ||
| 753 | clli->MaxFALL = s->clli_avg / 10000; | ||
| 754 | } | ||
| 755 | |||
| 756 | if (s->have_mdvc) { | ||
| 757 | AVMasteringDisplayMetadata *mdvc = | ||
| 758 | av_mastering_display_metadata_create_side_data(frame); | ||
| 759 | if (!mdvc) | ||
| 760 | return AVERROR(ENOMEM); | ||
| 761 | mdvc->has_primaries = 1; | ||
| 762 | for (int i = 0; i < 3; i++) { | ||
| 763 | mdvc->display_primaries[i][0] = av_make_q(s->mdvc_primaries[i][0], 50000); | ||
| 764 | mdvc->display_primaries[i][1] = av_make_q(s->mdvc_primaries[i][1], 50000); | ||
| 765 | } | ||
| 766 | mdvc->white_point[0] = av_make_q(s->mdvc_white_point[0], 50000); | ||
| 767 | mdvc->white_point[1] = av_make_q(s->mdvc_white_point[1], 50000); | ||
| 768 | mdvc->has_luminance = 1; | ||
| 769 | mdvc->max_luminance = av_make_q(s->mdvc_max_lum, 10000); | ||
| 770 | mdvc->min_luminance = av_make_q(s->mdvc_min_lum, 10000); | ||
| 771 | } | ||
| 772 | |||
| 734 | return 0; | 773 | return 0; |
| 735 | } | 774 | } |
| 736 | 775 | ||
| @@ -1508,6 +1547,30 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, · 771 unmodified lines | |||
| 1508 | 1547 | ||
| 1509 | break; | 1548 | break; |
| 1510 | } | 1549 | } |
| 1550 | case MKTAG('c', 'L', 'L', 'i'): | ||
| 1551 | if (bytestream2_get_bytes_left(&gb_chunk) != 8) { | ||
| 1552 | av_log(avctx, AV_LOG_WARNING, "Invalid cLLi chunk size: %d\n", bytestream2_get_bytes_left(&gb_chunk)); | ||
| 1553 | break; | ||
| 1554 | } | ||
| 1555 | s->have_clli = 1; | ||
| 1556 | s->clli_max = bytestream2_get_be32u(&gb_chunk); | ||
| 1557 | s->clli_avg = bytestream2_get_be32u(&gb_chunk); | ||
| 1558 | break; | ||
| 1559 | case MKTAG('m', 'D', 'V', 'c'): | ||
| 1560 | if (bytestream2_get_bytes_left(&gb_chunk) != 24) { | ||
| 1561 | av_log(avctx, AV_LOG_WARNING, "Invalid mDVc chunk size: %d\n", bytestream2_get_bytes_left(&gb_chunk)); | ||
| 1562 | break; | ||
| 1563 | } | ||
| 1564 | s->have_mdvc = 1; | ||
| 1565 | for (int i = 0; i < 3; i++) { | ||
| 1566 | s->mdvc_primaries[i][0] = bytestream2_get_be16u(&gb_chunk); | ||
| 1567 | s->mdvc_primaries[i][1] = bytestream2_get_be16u(&gb_chunk); | ||
| 1568 | } | ||
| 1569 | s->mdvc_white_point[0] = bytestream2_get_be16u(&gb_chunk); | ||
| 1570 | s->mdvc_white_point[1] = bytestream2_get_be16u(&gb_chunk); | ||
| 1571 | s->mdvc_max_lum = bytestream2_get_be32u(&gb_chunk); | ||
| 1572 | s->mdvc_min_lum = bytestream2_get_be32u(&gb_chunk); | ||
| 1573 | break; | ||
| 1511 | case MKTAG('I', 'E', 'N', 'D'): | 1574 | case MKTAG('I', 'E', 'N', 'D'): |
| 1512 | if (!(s->pic_state & PNG_ALLIMAGE)) | 1575 | if (!(s->pic_state & PNG_ALLIMAGE)) |
| 1513 | av_log(avctx, AV_LOG_ERROR, "IEND without all image\n"); | 1576 | av_log(avctx, AV_LOG_ERROR, "IEND without all image\n"); |