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 <leo.izen@gmail.com>
Reviewed-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: James Almer <jamrial@gmail.com>
pull/388/head
Leo Izen 2 years ago committed by James Almer
parent 64007595dc
commit cd9dd03006
  1. 10
      libavcodec/pnmdec.c
  2. 18
      libavcodec/pnmenc.c
  3. 2
      tests/ref/lavf/gbrpf32be.pfm
  4. 2
      tests/ref/lavf/gbrpf32le.pfm
  5. 2
      tests/ref/lavf/grayf32be.pfm
  6. 2
      tests/ref/lavf/grayf32le.pfm

@ -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; break;
case AV_PIX_FMT_GRAYF32: case AV_PIX_FMT_GRAYF32:
if (!s->half) { 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; break;
} }
*got_frame = 1; *got_frame = 1;

@ -136,9 +136,10 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
if ((avctx->pix_fmt == AV_PIX_FMT_GBRPF32LE || if ((avctx->pix_fmt == AV_PIX_FMT_GBRPF32LE ||
avctx->pix_fmt == AV_PIX_FMT_GBRPF32BE) && c == 'F') { avctx->pix_fmt == AV_PIX_FMT_GBRPF32BE) && c == 'F') {
const float *r = (const float *)p->data[2]; /* PFM is encoded from bottom to top */
const float *g = (const float *)p->data[0]; const float *r = (const float *)(p->data[2] + p->linesize[2] * (avctx->height - 1));
const float *b = (const float *)p->data[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 i = 0; i < avctx->height; i++) {
for (int j = 0; j < avctx->width; j++) { for (int j = 0; j < avctx->width; j++) {
@ -148,13 +149,14 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
bytestream += 12; bytestream += 12;
} }
r += p->linesize[2] / 4; r -= p->linesize[2] / 4;
g += p->linesize[0] / 4; g -= p->linesize[0] / 4;
b += p->linesize[1] / 4; b -= p->linesize[1] / 4;
} }
} else if ((avctx->pix_fmt == AV_PIX_FMT_GRAYF32LE || } else if ((avctx->pix_fmt == AV_PIX_FMT_GRAYF32LE ||
avctx->pix_fmt == AV_PIX_FMT_GRAYF32BE) && c == 'f') { 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 i = 0; i < avctx->height; i++) {
for (int j = 0; j < avctx->width; j++) { for (int j = 0; j < avctx->width; j++) {
@ -162,7 +164,7 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
bytestream += 4; bytestream += 4;
} }
g += p->linesize[0] / 4; g -= p->linesize[0] / 4;
} }
} else if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32 && c == 'H') { } else if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32 && c == 'H') {
const float *r = (const float *)p->data[2]; const float *r = (const float *)p->data[2];

@ -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 1216532 tests/data/images/gbrpf32be.pfm/02.gbrpf32be.pfm
tests/data/images/gbrpf32be.pfm/%02d.gbrpf32be.pfm CRC=0x4b73053f tests/data/images/gbrpf32be.pfm/%02d.gbrpf32be.pfm CRC=0x4b73053f

@ -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 1216533 tests/data/images/gbrpf32le.pfm/02.gbrpf32le.pfm
tests/data/images/gbrpf32le.pfm/%02d.gbrpf32le.pfm CRC=0x95e1053f tests/data/images/gbrpf32le.pfm/%02d.gbrpf32le.pfm CRC=0x95e1053f

@ -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 405524 tests/data/images/grayf32be.pfm/02.grayf32be.pfm
tests/data/images/grayf32be.pfm/%02d.grayf32be.pfm CRC=0xe3fda443 tests/data/images/grayf32be.pfm/%02d.grayf32be.pfm CRC=0xe3fda443

@ -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 405525 tests/data/images/grayf32le.pfm/02.grayf32le.pfm
tests/data/images/grayf32le.pfm/%02d.grayf32le.pfm CRC=0x5443a443 tests/data/images/grayf32le.pfm/%02d.grayf32le.pfm CRC=0x5443a443

Loading…
Cancel
Save