@@ -75,6 +75,7 @@ typedef struct PNGDecContext {
int have_chrm;
uint32_t white_point[2];
uint32_t display_primaries[3][2];
+ int gamma;
int have_srgb;
int have_cicp;
enum AVColorPrimaries cicp_primaries;
@@ -1259,6 +1260,7 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
case MKTAG('s', 'R', 'G', 'B'):
case MKTAG('c', 'I', 'C', 'P'):
case MKTAG('c', 'H', 'R', 'M'):
+ case MKTAG('g', 'A', 'M', 'A'):
break;
default:
continue;
@@ -1361,10 +1363,10 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
case MKTAG('g', 'A', 'M', 'A'): {
AVBPrint bp;
char *gamma_str;
- int num = bytestream2_get_be32(&gb_chunk);
+ s->gamma = bytestream2_get_be32(&gb_chunk);
av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
- av_bprintf(&bp, "%i/%i", num, 100000);
+ av_bprintf(&bp, "%i/%i", s->gamma, 100000);
ret = av_bprint_finalize(&bp, &gamma_str);
if (ret < 0)
return ret;
@@ -1429,8 +1431,27 @@ exit_loop:
}
/* these chunks override gAMA */
- if (s->iccp_data || s->have_srgb || s->have_cicp)
+ if (s->iccp_data || s->have_srgb || s->have_cicp) {
av_dict_set(&s->frame_metadata, "gamma", NULL, 0);
+ } else if (s->gamma) {
+ /*
+ * These values are 100000/2.2, 100000/2.8, 100000/2.6, and
+ * 100000/1.0 respectively. 45455, 35714, and 38462, and 100000.
+ * There's a 0.001 gamma tolerance here in case of floating
+ * point issues when the PNG was written.
+ *
+ * None of the other enums have a pure gamma curve so it makes
+ * sense to leave those to sRGB and cICP.
+ */
+ if (s->gamma > 45355 && s->gamma < 45555)
+ avctx->color_trc = p->color_trc = AVCOL_TRC_GAMMA22;
+ else if (s->gamma > 35614 && s->gamma < 35814)
+ avctx->color_trc = p->color_trc = AVCOL_TRC_GAMMA28;
+ else if (s->gamma > 38362 && s->gamma < 38562)
+ avctx->color_trc = p->color_trc = AVCOL_TRC_SMPTE428;
+ else if (s->gamma > 99900 && s->gamma < 100100)
+ avctx->color_trc = p->color_trc = AVCOL_TRC_LINEAR;
+ }
avctx->colorspace = p->colorspace = AVCOL_SPC_RGB;
avctx->color_range = p->color_range = AVCOL_RANGE_JPEG;