diff mbox series

[FFmpeg-devel] avcodec/pnm: avoid mirroring PFM images vertically

Message ID 20221116114306.251999-1-leo.izen@gmail.com
State Accepted
Commit cd9dd0300639689630171893b3dfb6c9ba63a692
Headers show
Series [FFmpeg-devel] avcodec/pnm: avoid mirroring PFM images vertically | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Leo Izen Nov. 16, 2022, 11:43 a.m. UTC
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(-)

Comments

Leo Izen Nov. 22, 2022, 4:46 p.m. UTC | #1
On 11/16/22 06:43, Leo Izen wrote:
> 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>


Bumping for review, thanks.

- Leo Izen
Leo Izen Nov. 28, 2022, 9:11 a.m. UTC | #2
On 11/22/22 11:46, Leo Izen wrote:
> On 11/16/22 06:43, Leo Izen wrote:
>> 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>
> 
> 
> Bumping for review, thanks.
> 
> - Leo Izen

Another bump, thanks.

- Leo Izen (thebombzen)
Anton Khirnov Nov. 29, 2022, 11:14 a.m. UTC | #3
Quoting Leo Izen (2022-11-16 12:43:06)
> 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(-)

Looks reasonable, will push soonish.
Leo Izen Dec. 14, 2022, 8:13 p.m. UTC | #4
On 11/29/22 06:14, Anton Khirnov wrote:
> Quoting Leo Izen (2022-11-16 12:43:06)
>> 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(-)
> 
> Looks reasonable, will push soonish.
> 

I got a LGTM, can someone push this? Thanks.

- Leo Izen (thebombzen)
Leo Izen Dec. 27, 2022, 1:38 p.m. UTC | #5
On 12/14/22 15:13, Leo Izen wrote:
> On 11/29/22 06:14, Anton Khirnov wrote:
>> Quoting Leo Izen (2022-11-16 12:43:06)
>>> 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(-)
>>
>> Looks reasonable, will push soonish.
>>
> 
> I got a LGTM, can someone push this? Thanks.
> 
> - Leo Izen (thebombzen)

Did this ever get applied?

- Leo Izen (thebombzen)
James Almer Dec. 27, 2022, 1:42 p.m. UTC | #6
On 12/27/2022 10:38 AM, Leo Izen wrote:
> On 12/14/22 15:13, Leo Izen wrote:
>> On 11/29/22 06:14, Anton Khirnov wrote:
>>> Quoting Leo Izen (2022-11-16 12:43:06)
>>>> 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(-)
>>>
>>> Looks reasonable, will push soonish.
>>>
>>
>> I got a LGTM, can someone push this? Thanks.
>>
>> - Leo Izen (thebombzen)
> 
> Did this ever get applied?
> 
> - Leo Izen (thebombzen)

No, guess he forgot. Just pushed it, sorry for the delay.
diff mbox series

Patch

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