diff mbox series

[FFmpeg-devel] avcodec: add PFM image decoder

Message ID 20200527152042.23373-1-onemda@gmail.com
State Accepted
Headers show
Series [FFmpeg-devel] avcodec: add PFM image decoder
Related show

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Paul B Mahol May 27, 2020, 3:20 p.m. UTC
Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavcodec/Makefile     |  1 +
 libavcodec/allcodecs.c  |  1 +
 libavcodec/codec_desc.c |  7 ++++++
 libavcodec/codec_id.h   |  1 +
 libavcodec/pnm.c        | 21 +++++++++++++---
 libavcodec/pnm.h        |  2 ++
 libavcodec/pnmdec.c     | 55 +++++++++++++++++++++++++++++++++++++++++
 libavformat/img2.c      |  1 +
 8 files changed, 85 insertions(+), 4 deletions(-)

Comments

Paul B Mahol May 30, 2020, 7:58 a.m. UTC | #1
Will apply.

On 5/27/20, Paul B Mahol <onemda@gmail.com> wrote:
> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> ---
>  libavcodec/Makefile     |  1 +
>  libavcodec/allcodecs.c  |  1 +
>  libavcodec/codec_desc.c |  7 ++++++
>  libavcodec/codec_id.h   |  1 +
>  libavcodec/pnm.c        | 21 +++++++++++++---
>  libavcodec/pnm.h        |  2 ++
>  libavcodec/pnmdec.c     | 55 +++++++++++++++++++++++++++++++++++++++++
>  libavformat/img2.c      |  1 +
>  8 files changed, 85 insertions(+), 4 deletions(-)
>
diff mbox series

Patch

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 6d816308c7..fa7bda4c5a 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -530,6 +530,7 @@  OBJS-$(CONFIG_PBM_DECODER)             += pnmdec.o pnm.o
 OBJS-$(CONFIG_PBM_ENCODER)             += pnmenc.o
 OBJS-$(CONFIG_PCX_DECODER)             += pcx.o
 OBJS-$(CONFIG_PCX_ENCODER)             += pcxenc.o
+OBJS-$(CONFIG_PFM_DECODER)             += pnmdec.o pnm.o
 OBJS-$(CONFIG_PGM_DECODER)             += pnmdec.o pnm.o
 OBJS-$(CONFIG_PGM_ENCODER)             += pnmenc.o
 OBJS-$(CONFIG_PGMYUV_DECODER)          += pnmdec.o pnm.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index eb5044f80e..f34ce35b42 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -233,6 +233,7 @@  extern AVCodec ff_pbm_encoder;
 extern AVCodec ff_pbm_decoder;
 extern AVCodec ff_pcx_encoder;
 extern AVCodec ff_pcx_decoder;
+extern AVCodec ff_pfm_decoder;
 extern AVCodec ff_pgm_encoder;
 extern AVCodec ff_pgm_decoder;
 extern AVCodec ff_pgmyuv_encoder;
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index b94ad0b1e6..9f8847544f 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1770,6 +1770,13 @@  static const AVCodecDescriptor codec_descriptors[] = {
         .long_name = NULL_IF_CONFIG_SMALL("NotchLC"),
         .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
     },
+    {
+        .id        = AV_CODEC_ID_PFM,
+        .type      = AVMEDIA_TYPE_VIDEO,
+        .name      = "pfm",
+        .long_name = NULL_IF_CONFIG_SMALL("PFM (Portable FloatMap) image"),
+        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
+    },
 
     /* various PCM "codecs" */
     {
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index f7cb0a6056..d885962c9c 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -294,6 +294,7 @@  enum AVCodecID {
     AV_CODEC_ID_CDTOONS,
     AV_CODEC_ID_MV30,
     AV_CODEC_ID_NOTCHLC,
+    AV_CODEC_ID_PFM,
 
     /* various PCM "codecs" */
     AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
diff --git a/libavcodec/pnm.c b/libavcodec/pnm.c
index b5c2881948..a6ae01b494 100644
--- a/libavcodec/pnm.c
+++ b/libavcodec/pnm.c
@@ -24,6 +24,7 @@ 
 
 #include "libavutil/avassert.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/avstring.h"
 #include "avcodec.h"
 #include "internal.h"
 #include "pnm.h"
@@ -69,8 +70,9 @@  int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
 
     if (s->bytestream_end - s->bytestream < 3 ||
         s->bytestream[0] != 'P' ||
-        s->bytestream[1] < '1'  ||
-        s->bytestream[1] > '7') {
+        (s->bytestream[1] < '1' ||
+         s->bytestream[1] > '7' &&
+         s->bytestream[1] != 'F')) {
         s->bytestream += s->bytestream_end > s->bytestream;
         s->bytestream += s->bytestream_end > s->bytestream;
         return AVERROR_INVALIDDATA;
@@ -78,7 +80,9 @@  int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
     pnm_get(s, buf1, sizeof(buf1));
     s->type= buf1[1]-'0';
 
-    if (s->type==1 || s->type==4) {
+    if (buf1[1] == 'F') {
+        avctx->pix_fmt = AV_PIX_FMT_GBRPF32;
+    } else if (s->type==1 || s->type==4) {
         avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
     } else if (s->type==2 || s->type==5) {
         if (avctx->codec_id == AV_CODEC_ID_PGMYUV)
@@ -173,7 +177,16 @@  int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
     if (ret < 0)
         return ret;
 
-    if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE && avctx->pix_fmt != AV_PIX_FMT_MONOBLACK) {
+    if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32) {
+        pnm_get(s, buf1, sizeof(buf1));
+        if (av_sscanf(buf1, "%f", &s->scale) != 1) {
+            av_log(avctx, AV_LOG_ERROR, "Invalid scale.\n");
+            return AVERROR_INVALIDDATA;
+        }
+        s->endian = s->scale < 0.f;
+        s->scale = fabsf(s->scale);
+        s->maxval = (1ULL << 32) - 1;
+    } else if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE && avctx->pix_fmt != AV_PIX_FMT_MONOBLACK) {
         pnm_get(s, buf1, sizeof(buf1));
         s->maxval = atoi(buf1);
         if (s->maxval <= 0 || s->maxval > UINT16_MAX) {
diff --git a/libavcodec/pnm.h b/libavcodec/pnm.h
index 5bc0aad29f..89c3b5a2da 100644
--- a/libavcodec/pnm.h
+++ b/libavcodec/pnm.h
@@ -30,6 +30,8 @@  typedef struct PNMContext {
     uint8_t *bytestream_end;
     int maxval;                 ///< maximum value of a pixel
     int type;
+    int endian;
+    float scale;
 } PNMContext;
 
 int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s);
diff --git a/libavcodec/pnmdec.c b/libavcodec/pnmdec.c
index e03e52297f..05bd11b147 100644
--- a/libavcodec/pnmdec.c
+++ b/libavcodec/pnmdec.c
@@ -46,6 +46,7 @@  static int pnm_decode_frame(AVCodecContext *avctx, void *data,
     int i, j, k, n, linesize, h, upgrade = 0, is_mono = 0;
     unsigned char *ptr;
     int components, sample_len, ret;
+    float scale;
 
     s->bytestream_start =
     s->bytestream       = (uint8_t *)buf;
@@ -254,6 +255,48 @@  static int pnm_decode_frame(AVCodecContext *avctx, void *data,
             }
         }
         break;
+    case AV_PIX_FMT_GBRPF32:
+        if (avctx->width * avctx->height * 12 > s->bytestream_end - s->bytestream)
+            return AVERROR_INVALIDDATA;
+        scale = 1.f / s->scale;
+        if (s->endian) {
+            float *r, *g, *b;
+
+            r = (float *)p->data[2];
+            g = (float *)p->data[0];
+            b = (float *)p->data[1];
+            for (int i = 0; i < avctx->height; i++) {
+                for (int j = 0; j < avctx->width; j++) {
+                    r[j] = av_int2float(av_le2ne32(((uint32_t *)s->bytestream)[0])) * scale;
+                    g[j] = av_int2float(av_le2ne32(((uint32_t *)s->bytestream)[4])) * scale;
+                    b[j] = av_int2float(av_le2ne32(((uint32_t *)s->bytestream)[8])) * scale;
+                    s->bytestream += 12;
+                }
+
+                r += p->linesize[2] / 4;
+                g += p->linesize[0] / 4;
+                b += p->linesize[1] / 4;
+            }
+        } else {
+            float *r, *g, *b;
+
+            r = (float *)p->data[2];
+            g = (float *)p->data[0];
+            b = (float *)p->data[1];
+            for (int i = 0; i < avctx->height; i++) {
+                for (int j = 0; j < avctx->width; j++) {
+                    r[j] = av_int2float(av_be2ne32(((uint32_t *)s->bytestream)[0])) * scale;
+                    g[j] = av_int2float(av_be2ne32(((uint32_t *)s->bytestream)[4])) * scale;
+                    b[j] = av_int2float(av_be2ne32(((uint32_t *)s->bytestream)[8])) * scale;
+                    s->bytestream += 12;
+                }
+
+                r += p->linesize[2] / 4;
+                g += p->linesize[0] / 4;
+                b += p->linesize[1] / 4;
+            }
+        }
+        break;
     }
     *got_frame = 1;
 
@@ -320,3 +363,15 @@  AVCodec ff_pam_decoder = {
     .capabilities   = AV_CODEC_CAP_DR1,
 };
 #endif
+
+#if CONFIG_PFM_DECODER
+AVCodec ff_pfm_decoder = {
+    .name           = "pfm",
+    .long_name      = NULL_IF_CONFIG_SMALL("PFM (Portable FloatMap) image"),
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_PFM,
+    .priv_data_size = sizeof(PNMContext),
+    .decode         = pnm_decode_frame,
+    .capabilities   = AV_CODEC_CAP_DR1,
+};
+#endif
diff --git a/libavformat/img2.c b/libavformat/img2.c
index 16bc9d2abd..d243d6c125 100644
--- a/libavformat/img2.c
+++ b/libavformat/img2.c
@@ -40,6 +40,7 @@  const IdStrMap ff_img_tags[] = {
     { AV_CODEC_ID_PGMYUV,     "pgmyuv"   },
     { AV_CODEC_ID_PBM,        "pbm"      },
     { AV_CODEC_ID_PAM,        "pam"      },
+    { AV_CODEC_ID_PFM,        "pfm"      },
     { AV_CODEC_ID_ALIAS_PIX,  "pix"      },
     { AV_CODEC_ID_DDS,        "dds"      },
     { AV_CODEC_ID_MPEG1VIDEO, "mpg1-img" },