From cd9dd0300639689630171893b3dfb6c9ba63a692 Mon Sep 17 00:00:00 2001 From: Leo Izen Date: Wed, 16 Nov 2022 06:43:06 -0500 Subject: [PATCH] avcodec/pnm: avoid mirroring PFM images vertically PFM (aka Portable FloatMap) encodes its scanlines from bottom-to-top, not from top-to-bottom, unlike other NetPBM formats. Without this patch, FFmpeg ignores this exception and decodes/encodes PFM images mirrored vertically from their proper orientation. For reference, see the NetPBM tool pfmtopam, which encodes a .pam from a .pfm, using the correct orientation (and which FFmpeg reads correctly). Also compare ffplay to magick display, which shows the correct orientation as well. See: http://www.pauldebevec.com/Research/HDR/PFM/ and see: https://netpbm.sourceforge.net/doc/pfm.html for descriptions of this image format. Signed-off-by: Leo Izen Reviewed-by: Anton Khirnov Signed-off-by: James Almer --- libavcodec/pnmdec.c | 10 ++++++++++ libavcodec/pnmenc.c | 18 ++++++++++-------- tests/ref/lavf/gbrpf32be.pfm | 2 +- tests/ref/lavf/gbrpf32le.pfm | 2 +- tests/ref/lavf/grayf32be.pfm | 2 +- tests/ref/lavf/grayf32le.pfm | 2 +- 6 files changed, 24 insertions(+), 12 deletions(-) diff --git a/libavcodec/pnmdec.c b/libavcodec/pnmdec.c index e95b4072eb..978e4c037e 100644 --- a/libavcodec/pnmdec.c +++ b/libavcodec/pnmdec.c @@ -346,6 +346,13 @@ static int pnm_decode_frame(AVCodecContext *avctx, AVFrame *p, } } } + /* PFM is encoded from bottom to top */ + p->data[0] += (avctx->height - 1) * p->linesize[0]; + p->data[1] += (avctx->height - 1) * p->linesize[1]; + p->data[2] += (avctx->height - 1) * p->linesize[2]; + p->linesize[0] = -p->linesize[0]; + p->linesize[1] = -p->linesize[1]; + p->linesize[2] = -p->linesize[2]; break; case AV_PIX_FMT_GRAYF32: if (!s->half) { @@ -395,6 +402,9 @@ static int pnm_decode_frame(AVCodecContext *avctx, AVFrame *p, } } } + /* PFM is encoded from bottom to top */ + p->data[0] += (avctx->height - 1) * p->linesize[0]; + p->linesize[0] = -p->linesize[0]; break; } *got_frame = 1; diff --git a/libavcodec/pnmenc.c b/libavcodec/pnmenc.c index c998dd410c..4bdd2e032f 100644 --- a/libavcodec/pnmenc.c +++ b/libavcodec/pnmenc.c @@ -136,9 +136,10 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, if ((avctx->pix_fmt == AV_PIX_FMT_GBRPF32LE || avctx->pix_fmt == AV_PIX_FMT_GBRPF32BE) && c == 'F') { - const float *r = (const float *)p->data[2]; - const float *g = (const float *)p->data[0]; - const float *b = (const float *)p->data[1]; + /* PFM is encoded from bottom to top */ + const float *r = (const float *)(p->data[2] + p->linesize[2] * (avctx->height - 1)); + const float *g = (const float *)(p->data[0] + p->linesize[0] * (avctx->height - 1)); + const float *b = (const float *)(p->data[1] + p->linesize[1] * (avctx->height - 1)); for (int i = 0; i < avctx->height; i++) { for (int j = 0; j < avctx->width; j++) { @@ -148,13 +149,14 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, bytestream += 12; } - r += p->linesize[2] / 4; - g += p->linesize[0] / 4; - b += p->linesize[1] / 4; + r -= p->linesize[2] / 4; + g -= p->linesize[0] / 4; + b -= p->linesize[1] / 4; } } else if ((avctx->pix_fmt == AV_PIX_FMT_GRAYF32LE || avctx->pix_fmt == AV_PIX_FMT_GRAYF32BE) && c == 'f') { - const float *g = (const float *)p->data[0]; + /* PFM is encoded from bottom to top */ + const float *g = (const float *)(p->data[0] + p->linesize[0] * (avctx->height - 1)); for (int i = 0; i < avctx->height; i++) { for (int j = 0; j < avctx->width; j++) { @@ -162,7 +164,7 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, bytestream += 4; } - g += p->linesize[0] / 4; + g -= p->linesize[0] / 4; } } else if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32 && c == 'H') { const float *r = (const float *)p->data[2]; diff --git a/tests/ref/lavf/gbrpf32be.pfm b/tests/ref/lavf/gbrpf32be.pfm index ca5e1b1659..aa8d098838 100644 --- a/tests/ref/lavf/gbrpf32be.pfm +++ b/tests/ref/lavf/gbrpf32be.pfm @@ -1,3 +1,3 @@ -6d470f8d6018b95b45afafc14b7d161a *tests/data/images/gbrpf32be.pfm/02.gbrpf32be.pfm +4ac5ecc53ff2ca0c9360031ea4c13236 *tests/data/images/gbrpf32be.pfm/02.gbrpf32be.pfm 1216532 tests/data/images/gbrpf32be.pfm/02.gbrpf32be.pfm tests/data/images/gbrpf32be.pfm/%02d.gbrpf32be.pfm CRC=0x4b73053f diff --git a/tests/ref/lavf/gbrpf32le.pfm b/tests/ref/lavf/gbrpf32le.pfm index b3947a9fcb..fb9f90e883 100644 --- a/tests/ref/lavf/gbrpf32le.pfm +++ b/tests/ref/lavf/gbrpf32le.pfm @@ -1,3 +1,3 @@ -892c5a05e1cbb3d2f7761d51e18b9c4c *tests/data/images/gbrpf32le.pfm/02.gbrpf32le.pfm +887bd04126ce36509578c51e692f3d62 *tests/data/images/gbrpf32le.pfm/02.gbrpf32le.pfm 1216533 tests/data/images/gbrpf32le.pfm/02.gbrpf32le.pfm tests/data/images/gbrpf32le.pfm/%02d.gbrpf32le.pfm CRC=0x95e1053f diff --git a/tests/ref/lavf/grayf32be.pfm b/tests/ref/lavf/grayf32be.pfm index 19a2ca85b6..3ce4ad5133 100644 --- a/tests/ref/lavf/grayf32be.pfm +++ b/tests/ref/lavf/grayf32be.pfm @@ -1,3 +1,3 @@ -0f6df0d68d7dd30e67386b1255f443c9 *tests/data/images/grayf32be.pfm/02.grayf32be.pfm +d2c3a37f7bf52be25f3f56239b5fdd92 *tests/data/images/grayf32be.pfm/02.grayf32be.pfm 405524 tests/data/images/grayf32be.pfm/02.grayf32be.pfm tests/data/images/grayf32be.pfm/%02d.grayf32be.pfm CRC=0xe3fda443 diff --git a/tests/ref/lavf/grayf32le.pfm b/tests/ref/lavf/grayf32le.pfm index aba861ec72..3a883ad2ac 100644 --- a/tests/ref/lavf/grayf32le.pfm +++ b/tests/ref/lavf/grayf32le.pfm @@ -1,3 +1,3 @@ -145715872a894b1fde0105d8a0106191 *tests/data/images/grayf32le.pfm/02.grayf32le.pfm +ea7aad8650d06c7cc8c80cc57cbac672 *tests/data/images/grayf32le.pfm/02.grayf32le.pfm 405525 tests/data/images/grayf32le.pfm/02.grayf32le.pfm tests/data/images/grayf32le.pfm/%02d.grayf32le.pfm CRC=0x5443a443