Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] avcodec/pnm: avoid mirroring PFM images vertically
@ 2022-11-16 11:43 Leo Izen
  2022-11-22 16:46 ` Leo Izen
  2022-11-29 11:14 ` Anton Khirnov
  0 siblings, 2 replies; 7+ messages in thread
From: Leo Izen @ 2022-11-16 11:43 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Leo Izen

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 <leo.izen@gmail.com>
---
 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 9eb663306d..5fa1f52e1e 100644
--- a/libavcodec/pnmenc.c
+++ b/libavcodec/pnmenc.c
@@ -133,9 +133,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++) {
@@ -145,13 +146,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++) {
@@ -159,7 +161,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
-- 
2.38.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".

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-12-27 13:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-16 11:43 [FFmpeg-devel] [PATCH] avcodec/pnm: avoid mirroring PFM images vertically Leo Izen
2022-11-22 16:46 ` Leo Izen
2022-11-28  9:11   ` Leo Izen
2022-11-29 11:14 ` Anton Khirnov
2022-12-14 20:13   ` Leo Izen
2022-12-27 13:38     ` Leo Izen
2022-12-27 13:42       ` James Almer

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