aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/pngenc.c
diff options
context:
space:
mode:
authorLeo Izen <leo.izen@gmail.com>2023-01-17 11:09:29 -0500
committerLeo Izen <leo.izen@gmail.com>2023-01-25 08:09:16 -0500
commit2548c32cc10f256cc0ff90457631ef7ef94070af (patch)
tree4b869f29ca8079402f3bded4faafa68b8bbe67f9 /libavcodec/pngenc.c
parent843a4467131fd677aacb46b8bbdea898e4fe12d5 (diff)
downloadffmpeg-2548c32cc10f256cc0ff90457631ef7ef94070af.tar.gz
avcodec/png: use libavutil/csp.h for cHRM chunks
The cHRM chunk is descriptive. That is, it describes the primaries that should be used to interpret the pixel data in the PNG file. This is notably different from Mastering Display Metadata, which describes which subset of the presented gamut is relevant. MDM describes a gamut and says colors outside the gamut are not required to be preserved, but it does not actually describe the gamut that the pixel data from the frame resides in. Thus, to decode a cHRM chunk present in a PNG file to Mastering Display Metadata is incorrect. This commit changes this behavior so the cHRM chunk, if present, is decoded to color metadata. For example, if the cHRM chunk describes BT.709 primaries, the resulting AVFrame will be tagged with AVCOL_PRI_BT709, as a description of its pixel data. To do this, it utilizes libavutil/csp.h, which exposes a funcction av_csp_primaries_id_from_desc, to detect which enum value accurately describes the white point and primaries represented by the cHRM chunk. This commit also changes pngenc.c to utilize the libavuitl/csp.h API, since it previously duplicated code contained in that API. Instead, taking advantage of the API that exists makes more sense. pngenc.c does properly utilize the color tags rather than incorrectly using MDM, so that required no change. Signed-off-by: Leo Izen <leo.izen@gmail.com>
Diffstat (limited to 'libavcodec/pngenc.c')
-rw-r--r--libavcodec/pngenc.c53
1 files changed, 16 insertions, 37 deletions
diff --git a/libavcodec/pngenc.c b/libavcodec/pngenc.c
index ca1a186ca8..4333d5837f 100644
--- a/libavcodec/pngenc.c
+++ b/libavcodec/pngenc.c
@@ -29,10 +29,12 @@
#include "zlib_wrapper.h"
#include "libavutil/avassert.h"
+#include "libavutil/color_utils.h"
#include "libavutil/crc.h"
+#include "libavutil/csp.h"
#include "libavutil/libm.h"
#include "libavutil/opt.h"
-#include "libavutil/color_utils.h"
+#include "libavutil/rational.h"
#include "libavutil/stereo3d.h"
#include <zlib.h>
@@ -294,45 +296,22 @@ static int png_write_row(AVCodecContext *avctx, const uint8_t *data, int size)
}
#define AV_WB32_PNG(buf, n) AV_WB32(buf, lrint((n) * 100000))
+#define AV_WB32_PNG_D(buf, d) AV_WB32_PNG(buf, av_q2d(d))
static int png_get_chrm(enum AVColorPrimaries prim, uint8_t *buf)
{
- double rx, ry, gx, gy, bx, by, wx = 0.3127, wy = 0.3290;
- switch (prim) {
- case AVCOL_PRI_BT709:
- rx = 0.640; ry = 0.330;
- gx = 0.300; gy = 0.600;
- bx = 0.150; by = 0.060;
- break;
- case AVCOL_PRI_BT470M:
- rx = 0.670; ry = 0.330;
- gx = 0.210; gy = 0.710;
- bx = 0.140; by = 0.080;
- wx = 0.310; wy = 0.316;
- break;
- case AVCOL_PRI_BT470BG:
- rx = 0.640; ry = 0.330;
- gx = 0.290; gy = 0.600;
- bx = 0.150; by = 0.060;
- break;
- case AVCOL_PRI_SMPTE170M:
- case AVCOL_PRI_SMPTE240M:
- rx = 0.630; ry = 0.340;
- gx = 0.310; gy = 0.595;
- bx = 0.155; by = 0.070;
- break;
- case AVCOL_PRI_BT2020:
- rx = 0.708; ry = 0.292;
- gx = 0.170; gy = 0.797;
- bx = 0.131; by = 0.046;
- break;
- default:
- return 0;
- }
+ const AVColorPrimariesDesc *desc = av_csp_primaries_desc_from_id(prim);
+ if (!desc)
+ return 0;
+
+ AV_WB32_PNG_D(buf, desc->wp.x);
+ AV_WB32_PNG_D(buf + 4, desc->wp.y);
+ AV_WB32_PNG_D(buf + 8, desc->prim.r.x);
+ AV_WB32_PNG_D(buf + 12, desc->prim.r.y);
+ AV_WB32_PNG_D(buf + 16, desc->prim.g.x);
+ AV_WB32_PNG_D(buf + 20, desc->prim.g.y);
+ AV_WB32_PNG_D(buf + 24, desc->prim.b.x);
+ AV_WB32_PNG_D(buf + 28, desc->prim.b.y);
- AV_WB32_PNG(buf , wx); AV_WB32_PNG(buf + 4 , wy);
- AV_WB32_PNG(buf + 8 , rx); AV_WB32_PNG(buf + 12, ry);
- AV_WB32_PNG(buf + 16, gx); AV_WB32_PNG(buf + 20, gy);
- AV_WB32_PNG(buf + 24, bx); AV_WB32_PNG(buf + 28, by);
return 1;
}