avcodec/pngenc: avoid writing cICP when inappropriate

We parse the fallback cHRM on decode and correctly determine that we
have BT.709 primaries, but unknown TRC. This causes us to write cICP
where we shouldn't. Primaries without transfer can be handled entirely
by cHRM, so we should only write cICP if we actually know the transfer
function.

Additionally, we should avoid writing cICP if there's an ICC profile
because the spec says decoders must prioritize cICP over the ICC
profile.

Signed-off-by: Leo Izen <leo.izen@gmail.com>
This commit is contained in:
Leo Izen 2023-02-01 12:04:30 -05:00
parent c56f5be678
commit 5cf0bc4236
No known key found for this signature in database
GPG Key ID: 5A71C331FD2FA19A
1 changed files with 14 additions and 7 deletions

View File

@ -412,14 +412,25 @@ static int encode_headers(AVCodecContext *avctx, const AVFrame *pict)
}
}
side_data = av_frame_get_side_data(pict, AV_FRAME_DATA_ICC_PROFILE);
if ((ret = png_write_iccp(s, side_data)))
return ret;
/* write colorspace information */
if (pict->color_primaries == AVCOL_PRI_BT709 &&
pict->color_trc == AVCOL_TRC_IEC61966_2_1) {
s->buf[0] = 1; /* rendering intent, relative colorimetric by default */
png_write_chunk(&s->bytestream, MKTAG('s', 'R', 'G', 'B'), s->buf, 1);
} else if (pict->color_primaries != AVCOL_PRI_UNSPECIFIED ||
pict->color_trc != AVCOL_TRC_UNSPECIFIED) {
/* these values match H.273 so no translation is needed */
} else if (pict->color_trc != AVCOL_TRC_UNSPECIFIED && !side_data) {
/*
* Avoid writing cICP if the transfer is unknown. Known primaries
* with unknown transfer can be handled by cHRM.
*
* We also avoid writing cICP if an ICC Profile is present, because
* the standard requires that cICP overrides iCCP.
*
* These values match H.273 so no translation is needed.
*/
s->buf[0] = pict->color_primaries;
s->buf[1] = pict->color_trc;
s->buf[2] = 0; /* colorspace = RGB */
@ -432,10 +443,6 @@ static int encode_headers(AVCodecContext *avctx, const AVFrame *pict)
if (png_get_gama(pict->color_trc, s->buf))
png_write_chunk(&s->bytestream, MKTAG('g', 'A', 'M', 'A'), s->buf, 4);
side_data = av_frame_get_side_data(pict, AV_FRAME_DATA_ICC_PROFILE);
if ((ret = png_write_iccp(s, side_data)))
return ret;
/* put the palette if needed, must be after colorspace information */
if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
int has_alpha, alpha, i;