History

avcodec: add PFM image decoder

Paul B Mahol committed 6 years ago

Commit d49db991 parent 439128910 files · +87 −5Browse files

Unified · Split

10 files
  1. Changelog +1 −0
  2. libavcodec/Makefile +1 −0
  3. libavcodec/allcodecs.c +1 −0
  4. libavcodec/codec_desc.c +7 −0
  5. libavcodec/codec_id.h +1 −0
  6. libavcodec/pnm.c +17 −4
  7. libavcodec/pnm.h +2 −0
  8. libavcodec/pnmdec.c +55 −0
  9. libavcodec/version.h +1 −1
  10. libavformat/img2.c +1 −0
Changelog+1−0
@@ -72,6 +72,7 @@ version <next>: · 71 unmodified lines
7272 - MediaFoundation encoder wrapper
7373 - untile filter
7474 - Simon & Schuster Interactive ADPCM encoder
75+- PFM decoder
7576
7677
7778 version 4.2:
libavcodec/Makefile+1−0
@@ -530,6 +530,7 @@ OBJS-$(CONFIG_PBM_DECODER) += pnmdec.o pnm.o · 529 unmodified lines
530530 OBJS-$(CONFIG_PBM_ENCODER) += pnmenc.o
531531 OBJS-$(CONFIG_PCX_DECODER) += pcx.o
532532 OBJS-$(CONFIG_PCX_ENCODER) += pcxenc.o
533+OBJS-$(CONFIG_PFM_DECODER) += pnmdec.o pnm.o
533534 OBJS-$(CONFIG_PGM_DECODER) += pnmdec.o pnm.o
534535 OBJS-$(CONFIG_PGM_ENCODER) += pnmenc.o
535536 OBJS-$(CONFIG_PGMYUV_DECODER) += pnmdec.o pnm.o
libavcodec/allcodecs.c+1−0
@@ -233,6 +233,7 @@ extern AVCodec ff_pbm_encoder; · 232 unmodified lines
233233 extern AVCodec ff_pbm_decoder;
234234 extern AVCodec ff_pcx_encoder;
235235 extern AVCodec ff_pcx_decoder;
236+extern AVCodec ff_pfm_decoder;
236237 extern AVCodec ff_pgm_encoder;
237238 extern AVCodec ff_pgm_decoder;
238239 extern AVCodec ff_pgmyuv_encoder;
libavcodec/codec_desc.c+7−0
@@ -1770,6 +1770,13 @@ static const AVCodecDescriptor codec_descriptors[] = { · 1769 unmodified lines
17701770 .long_name = NULL_IF_CONFIG_SMALL("NotchLC"),
17711771 .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
17721772 },
1773+ {
1774+ .id = AV_CODEC_ID_PFM,
1775+ .type = AVMEDIA_TYPE_VIDEO,
1776+ .name = "pfm",
1777+ .long_name = NULL_IF_CONFIG_SMALL("PFM (Portable FloatMap) image"),
1778+ .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
1779+ },
17731780
17741781 /* various PCM "codecs" */
17751782 {
libavcodec/codec_id.h+1−0
@@ -294,6 +294,7 @@ enum AVCodecID { · 293 unmodified lines
294294 AV_CODEC_ID_CDTOONS,
295295 AV_CODEC_ID_MV30,
296296 AV_CODEC_ID_NOTCHLC,
297+ AV_CODEC_ID_PFM,
297298
298299 /* various PCM "codecs" */
299300 AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs
libavcodec/pnm.c+17−4
@@ -24,6 +24,7 @@ · 23 unmodified lines
2424
2525 #include "libavutil/avassert.h"
2626 #include "libavutil/imgutils.h"
27+#include "libavutil/avstring.h"
2728 #include "avcodec.h"
2829 #include "internal.h"
2930 #include "pnm.h"
@@ -69,8 +70,9 @@ int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s) · 39 unmodified lines
6970
7071 if (s->bytestream_end - s->bytestream < 3 ||
7172 s->bytestream[0] != 'P' ||
72− s->bytestream[1] < '1' ||
73− s->bytestream[1] > '7') {
73+ (s->bytestream[1] < '1' ||
74+ s->bytestream[1] > '7' &&
75+ s->bytestream[1] != 'F')) {
7476 s->bytestream += s->bytestream_end > s->bytestream;
7577 s->bytestream += s->bytestream_end > s->bytestream;
7678 return AVERROR_INVALIDDATA;
@@ -78,7 +80,9 @@ int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s) · 1 unmodified line
7880 pnm_get(s, buf1, sizeof(buf1));
7981 s->type= buf1[1]-'0';
8082
81− if (s->type==1 || s->type==4) {
83+ if (buf1[1] == 'F') {
84+ avctx->pix_fmt = AV_PIX_FMT_GBRPF32;
85+ } else if (s->type==1 || s->type==4) {
8286 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
8387 } else if (s->type==2 || s->type==5) {
8488 if (avctx->codec_id == AV_CODEC_ID_PGMYUV)
@@ -173,7 +177,16 @@ int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s) · 88 unmodified lines
173177 if (ret < 0)
174178 return ret;
175179
176− if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE && avctx->pix_fmt != AV_PIX_FMT_MONOBLACK) {
180+ if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32) {
181+ pnm_get(s, buf1, sizeof(buf1));
182+ if (av_sscanf(buf1, "%f", &s->scale) != 1) {
183+ av_log(avctx, AV_LOG_ERROR, "Invalid scale.\n");
184+ return AVERROR_INVALIDDATA;
185+ }
186+ s->endian = s->scale < 0.f;
187+ s->scale = fabsf(s->scale);
188+ s->maxval = (1ULL << 32) - 1;
189+ } else if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE && avctx->pix_fmt != AV_PIX_FMT_MONOBLACK) {
177190 pnm_get(s, buf1, sizeof(buf1));
178191 s->maxval = atoi(buf1);
179192 if (s->maxval <= 0 || s->maxval > UINT16_MAX) {
libavcodec/pnm.h+2−0
@@ -30,6 +30,8 @@ typedef struct PNMContext { · 29 unmodified lines
3030 uint8_t *bytestream_end;
3131 int maxval; ///< maximum value of a pixel
3232 int type;
33+ int endian;
34+ float scale;
3335 } PNMContext;
3436
3537 int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s);
libavcodec/pnmdec.c+55−0
@@ -46,6 +46,7 @@ static int pnm_decode_frame(AVCodecContext *avctx, void *data, · 45 unmodified lines
4646 int i, j, k, n, linesize, h, upgrade = 0, is_mono = 0;
4747 unsigned char *ptr;
4848 int components, sample_len, ret;
49+ float scale;
4950
5051 s->bytestream_start =
5152 s->bytestream = (uint8_t *)buf;
@@ -254,6 +255,48 @@ static int pnm_decode_frame(AVCodecContext *avctx, void *data, · 202 unmodified lines
254255 }
255256 }
256257 break;
258+ case AV_PIX_FMT_GBRPF32:
259+ if (avctx->width * avctx->height * 12 > s->bytestream_end - s->bytestream)
260+ return AVERROR_INVALIDDATA;
261+ scale = 1.f / s->scale;
262+ if (s->endian) {
263+ float *r, *g, *b;
264+
265+ r = (float *)p->data[2];
266+ g = (float *)p->data[0];
267+ b = (float *)p->data[1];
268+ for (int i = 0; i < avctx->height; i++) {
269+ for (int j = 0; j < avctx->width; j++) {
270+ r[j] = av_int2float(av_le2ne32(((uint32_t *)s->bytestream)[0])) * scale;
271+ g[j] = av_int2float(av_le2ne32(((uint32_t *)s->bytestream)[4])) * scale;
272+ b[j] = av_int2float(av_le2ne32(((uint32_t *)s->bytestream)[8])) * scale;
273+ s->bytestream += 12;
274+ }
275+
276+ r += p->linesize[2] / 4;
277+ g += p->linesize[0] / 4;
278+ b += p->linesize[1] / 4;
279+ }
280+ } else {
281+ float *r, *g, *b;
282+
283+ r = (float *)p->data[2];
284+ g = (float *)p->data[0];
285+ b = (float *)p->data[1];
286+ for (int i = 0; i < avctx->height; i++) {
287+ for (int j = 0; j < avctx->width; j++) {
288+ r[j] = av_int2float(av_be2ne32(((uint32_t *)s->bytestream)[0])) * scale;
289+ g[j] = av_int2float(av_be2ne32(((uint32_t *)s->bytestream)[4])) * scale;
290+ b[j] = av_int2float(av_be2ne32(((uint32_t *)s->bytestream)[8])) * scale;
291+ s->bytestream += 12;
292+ }
293+
294+ r += p->linesize[2] / 4;
295+ g += p->linesize[0] / 4;
296+ b += p->linesize[1] / 4;
297+ }
298+ }
299+ break;
257300 }
258301 *got_frame = 1;
259302
@@ -320,3 +363,15 @@ AVCodec ff_pam_decoder = { · 60 unmodified lines
320363 .capabilities = AV_CODEC_CAP_DR1,
321364 };
322365 #endif
366+
367+#if CONFIG_PFM_DECODER
368+AVCodec ff_pfm_decoder = {
369+ .name = "pfm",
370+ .long_name = NULL_IF_CONFIG_SMALL("PFM (Portable FloatMap) image"),
371+ .type = AVMEDIA_TYPE_VIDEO,
372+ .id = AV_CODEC_ID_PFM,
373+ .priv_data_size = sizeof(PNMContext),
374+ .decode = pnm_decode_frame,
375+ .capabilities = AV_CODEC_CAP_DR1,
376+};
377+#endif
libavcodec/version.h+1−1
@@ -28,7 +28,7 @@ · 27 unmodified lines
2828 #include "libavutil/version.h"
2929
3030 #define LIBAVCODEC_VERSION_MAJOR 58
31−#define LIBAVCODEC_VERSION_MINOR 89
31+#define LIBAVCODEC_VERSION_MINOR 90
3232 #define LIBAVCODEC_VERSION_MICRO 100
3333
3434 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
libavformat/img2.c+1−0
@@ -40,6 +40,7 @@ const IdStrMap ff_img_tags[] = { · 39 unmodified lines
4040 { AV_CODEC_ID_PGMYUV, "pgmyuv" },
4141 { AV_CODEC_ID_PBM, "pbm" },
4242 { AV_CODEC_ID_PAM, "pam" },
43+ { AV_CODEC_ID_PFM, "pfm" },
4344 { AV_CODEC_ID_ALIAS_PIX, "pix" },
4445 { AV_CODEC_ID_DDS, "dds" },
4546 { AV_CODEC_ID_MPEG1VIDEO, "mpg1-img" },

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t