History

avcodec/pngdec: read cLLi and mDVc chunks

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.

Leo Izen committed 2 years ago

Commit c7a57b01 parent eb4df271 file · +63 −0Browse files

Unified · Split

libavcodec/pngdec.c+63−0
@@ -29,6 +29,7 @@ · 28 unmodified lines
2929 #include "libavutil/csp.h"
3030 #include "libavutil/imgutils.h"
3131 #include "libavutil/intreadwrite.h"
32+#include "libavutil/mastering_display_metadata.h"
3233 #include "libavutil/pixfmt.h"
3334 #include "libavutil/rational.h"
3435 #include "libavutil/stereo3d.h"
@@ -81,6 +82,14 @@ typedef struct PNGDecContext { · 46 unmodified lines
8182 enum AVColorPrimaries cicp_primaries;
8283 enum AVColorTransferCharacteristic cicp_trc;
8384 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;
8493
8594 enum PNGHeaderState hdr_state;
8695 enum PNGImageState pic_state;
@@ -731,6 +740,36 @@ static int populate_avctx_color_fields(AVCodecContext *avctx, AVFrame *frame) · 644 unmodified lines
731740 if (!s->has_trns && s->significant_bits > 0)
732741 avctx->bits_per_raw_sample = s->significant_bits;
733742
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+
734773 return 0;
735774 }
736775
@@ -1508,6 +1547,30 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, · 771 unmodified lines
15081547
15091548 break;
15101549 }
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;
15111574 case MKTAG('I', 'E', 'N', 'D'):
15121575 if (!(s->pic_state & PNG_ALLIMAGE))
15131576 av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t