From: Niklas Haas <ffmpeg@haasn.xyz>
To: ffmpeg-devel@ffmpeg.org
Cc: Niklas Haas <git@haasn.dev>
Subject: [FFmpeg-devel] [PATCH v5] avcodec/pngenc: support writing iCCP chunks
Date: Tue, 15 Mar 2022 12:34:58 +0100
Message-ID: <20220315113457.36059-1-ffmpeg@haasn.xyz> (raw)
In-Reply-To: <20220315111041.51203-1-ffmpeg@haasn.xyz>
From: Niklas Haas <git@haasn.dev>
We re-use the PNGEncContext.zstream for deflate-related operations.
Other than that, the code is pretty straightforward. Special care needs
to be taken to avoid writing more than 79 characters of the profile
description (the maximum supported).
To write the (dynamically sized) deflate-encoded data, we allocate extra
space in the packet and use that directly as a scratch buffer. Modify
png_write_chunk slightly to allow pre-writing the chunk contents like
this.
Also add a FATE transcode test to ensure that the ICC profile gets
encoded correctly.
Signed-off-by: Niklas Haas <git@haasn.dev>
---
Changes in v5:
- Fix FATE failure (possible segfault in encode_apng)
- Fix commit message (still talked about deleted encode_zbuf)
- Add Signed-off-by line
---
libavcodec/pngenc.c | 88 +++++++++++++++++++++++++++++++++++++++++-
tests/fate/image.mak | 3 ++
tests/ref/fate/png-icc | 43 +++++++++++++++++++++
3 files changed, 132 insertions(+), 2 deletions(-)
create mode 100644 tests/ref/fate/png-icc
diff --git a/libavcodec/pngenc.c b/libavcodec/pngenc.c
index 3ebcc1e571..947138e3b5 100644
--- a/libavcodec/pngenc.c
+++ b/libavcodec/pngenc.c
@@ -235,7 +235,8 @@ static void png_write_chunk(uint8_t **f, uint32_t tag,
bytestream_put_be32(f, av_bswap32(tag));
if (length > 0) {
crc = av_crc(crc_table, crc, buf, length);
- memcpy(*f, buf, length);
+ if (*f != buf)
+ memcpy(*f, buf, length);
*f += length;
}
bytestream_put_be32(f, ~crc);
@@ -343,10 +344,53 @@ static int png_get_gama(enum AVColorTransferCharacteristic trc, uint8_t *buf)
return 1;
}
+static int png_write_iccp(AVCodecContext *avctx, const AVFrameSideData *sd)
+{
+ PNGEncContext *s = avctx->priv_data;
+ const AVDictionaryEntry *entry;
+ const char *name;
+ uint8_t *start, *buf;
+ int ret;
+
+ if (!sd || !sd->size)
+ return 0;
+ s->zstream.next_in = sd->data;
+ s->zstream.avail_in = sd->size;
+
+ /* write the chunk contents first */
+ start = s->bytestream + 8; /* make room for iCCP tag + length */
+ buf = start;
+
+ /* profile description */
+ entry = av_dict_get(sd->metadata, "name", NULL, 0);
+ name = (entry && entry->value[0]) ? entry->value : "icc";
+ for (int i = 0;; i++) {
+ char c = (i == 79) ? 0 : name[i];
+ bytestream_put_byte(&buf, c);
+ if (!c)
+ break;
+ }
+
+ /* compression method and profile data */
+ bytestream_put_byte(&buf, 0);
+ s->zstream.next_out = buf;
+ s->zstream.avail_out = s->bytestream_end - buf;
+ ret = deflate(&s->zstream, Z_FINISH);
+ deflateReset(&s->zstream);
+ if (ret != Z_STREAM_END)
+ return AVERROR_EXTERNAL;
+
+ /* rewind to the start and write the chunk header/crc */
+ png_write_chunk(&s->bytestream, MKTAG('i', 'C', 'C', 'P'), start,
+ s->zstream.next_out - start);
+ return 0;
+}
+
static int encode_headers(AVCodecContext *avctx, const AVFrame *pict)
{
AVFrameSideData *side_data;
PNGEncContext *s = avctx->priv_data;
+ int ret;
/* write png header */
AV_WB32(s->buf, avctx->width);
@@ -399,7 +443,13 @@ 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);
- /* put the palette if needed */
+ side_data = av_frame_get_side_data(pict, AV_FRAME_DATA_ICC_PROFILE);
+ if ((ret = png_write_iccp(avctx, side_data))) {
+ av_log(avctx, AV_LOG_WARNING, "Failed writing iCCP chunk\n");
+ 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;
unsigned int v;
@@ -522,6 +572,36 @@ the_end:
return ret;
}
+static int add_icc_profile_size(AVCodecContext *avctx, const AVFrame *pict,
+ size_t *max_packet_size)
+{
+ PNGEncContext *s = avctx->priv_data;
+ const AVFrameSideData *sd;
+ uLong bound;
+
+ if (!pict)
+ return 0;
+ sd = av_frame_get_side_data(pict, AV_FRAME_DATA_ICC_PROFILE);
+ if (!sd || !sd->size)
+ return 0;
+ if (sd->size > UINT_MAX)
+ goto too_large;
+
+ s->zstream.next_in = sd->data;
+ s->zstream.avail_in = sd->size;
+ bound = deflateBound(&s->zstream, sd->size) + 128; /* header */
+ deflateReset(&s->zstream);
+ if (bound > INT_MAX)
+ goto too_large;
+
+ *max_packet_size += bound;
+ return 0;
+
+too_large:
+ av_log(avctx, AV_LOG_WARNING, "ICC profile too large for iCCP chunk\n");
+ return AVERROR_INVALIDDATA;
+}
+
static int encode_png(AVCodecContext *avctx, AVPacket *pkt,
const AVFrame *pict, int *got_packet)
{
@@ -537,6 +617,8 @@ static int encode_png(AVCodecContext *avctx, AVPacket *pkt,
enc_row_size +
12 * (((int64_t)enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) // IDAT * ceil(enc_row_size / IOBUF_SIZE)
);
+ if ((ret = add_icc_profile_size(avctx, pict, &max_packet_size)))
+ return ret;
if (max_packet_size > INT_MAX)
return AVERROR(ENOMEM);
ret = ff_alloc_packet(avctx, pkt, max_packet_size);
@@ -867,6 +949,8 @@ static int encode_apng(AVCodecContext *avctx, AVPacket *pkt,
enc_row_size +
(4 + 12) * (((int64_t)enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) // fdAT * ceil(enc_row_size / IOBUF_SIZE)
);
+ if ((ret = add_icc_profile_size(avctx, pict, &max_packet_size)))
+ return ret;
if (max_packet_size > INT_MAX)
return AVERROR(ENOMEM);
diff --git a/tests/fate/image.mak b/tests/fate/image.mak
index 573d398915..f5fd64e3ee 100644
--- a/tests/fate/image.mak
+++ b/tests/fate/image.mak
@@ -385,6 +385,9 @@ FATE_PNG_PROBE += fate-png-side-data
fate-png-side-data: CMD = run ffprobe$(PROGSSUF)$(EXESUF) -show_frames \
-i $(TARGET_SAMPLES)/png1/lena-int_rgb24.png
+FATE_PNG += fate-png-icc
+fate-png-icc: CMD = transcode png_pipe $(TARGET_SAMPLES)/png1/lena-int_rgb24.png image2 "-c png" "" "" "-show_frames"
+
FATE_PNG-$(call DEMDEC, IMAGE2, PNG) += $(FATE_PNG)
FATE_PNG_PROBE-$(call DEMDEC, IMAGE2, PNG) += $(FATE_PNG_PROBE)
FATE_IMAGE += $(FATE_PNG-yes)
diff --git a/tests/ref/fate/png-icc b/tests/ref/fate/png-icc
new file mode 100644
index 0000000000..542bb76f9a
--- /dev/null
+++ b/tests/ref/fate/png-icc
@@ -0,0 +1,43 @@
+a50d37a0e72bddea2fcbba6fb773e2a0 *tests/data/fate/png-icc.image2
+49397 tests/data/fate/png-icc.image2
+#tb 0: 1/25
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 128x128
+#sar 0: 2835/2835
+0, 0, 0, 1, 49152, 0xe0013dee
+[FRAME]
+media_type=video
+stream_index=0
+key_frame=1
+pts=0
+pts_time=0.000000
+pkt_dts=0
+pkt_dts_time=0.000000
+best_effort_timestamp=0
+best_effort_timestamp_time=0.000000
+pkt_duration=1
+pkt_duration_time=0.040000
+pkt_pos=0
+pkt_size=49397
+width=128
+height=128
+pix_fmt=rgb24
+sample_aspect_ratio=1:1
+pict_type=I
+coded_picture_number=0
+display_picture_number=0
+interlaced_frame=0
+top_field_first=0
+repeat_pict=0
+color_range=pc
+color_space=unknown
+color_primaries=unknown
+color_transfer=unknown
+chroma_location=unspecified
+[SIDE_DATA]
+side_data_type=ICC profile
+name=Photoshop ICC profile
+size=3144
+[/SIDE_DATA]
+[/FRAME]
--
2.35.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
prev parent reply other threads:[~2022-03-15 11:36 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-11 10:14 [FFmpeg-devel] [PATCH] " Niklas Haas
2022-03-11 10:17 ` [FFmpeg-devel] [PATCH v2] " Niklas Haas
2022-03-11 10:21 ` Niklas Haas
2022-03-11 11:05 ` Andreas Rheinhardt
2022-03-11 13:11 ` Niklas Haas Haas
2022-03-11 11:18 ` Andreas Rheinhardt
2022-03-11 13:37 ` Niklas Haas
2022-03-12 11:10 ` Niklas Haas
2022-03-12 12:04 ` [FFmpeg-devel] [PATCH v3] " Niklas Haas
2022-03-15 6:44 ` Andreas Rheinhardt
2022-03-15 11:10 ` [FFmpeg-devel] [PATCH v4] " Niklas Haas
2022-03-15 11:34 ` Niklas Haas [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220315113457.36059-1-ffmpeg@haasn.xyz \
--to=ffmpeg@haasn.xyz \
--cc=ffmpeg-devel@ffmpeg.org \
--cc=git@haasn.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git