diff options
author | Paul B Mahol <onemda@gmail.com> | 2022-07-01 10:06:15 +0200 |
---|---|---|
committer | Paul B Mahol <onemda@gmail.com> | 2022-07-03 15:16:31 +0200 |
commit | ff1450e449f848ad4b37b3cf448315ba4581364e (patch) | |
tree | 4c6e245afa51c3ec58bb0865ed27f2940acf4f4a /libavcodec/pnmenc.c | |
parent | 33fe3b73c2d4dec674509de5dc48698895f0806b (diff) | |
download | ffmpeg-ff1450e449f848ad4b37b3cf448315ba4581364e.tar.gz |
avcodec: add PHM decoder and encoder
Diffstat (limited to 'libavcodec/pnmenc.c')
-rw-r--r-- | libavcodec/pnmenc.c | 75 |
1 files changed, 73 insertions, 2 deletions
diff --git a/libavcodec/pnmenc.c b/libavcodec/pnmenc.c index c1820ac79e..258d919d32 100644 --- a/libavcodec/pnmenc.c +++ b/libavcodec/pnmenc.c @@ -27,10 +27,17 @@ #include "avcodec.h" #include "codec_internal.h" #include "encode.h" +#include "float2half.h" + +typedef struct PHMEncContext { + uint16_t basetable[512]; + uint8_t shifttable[512]; +} PHMEncContext; static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *p, int *got_packet) { + PHMEncContext *s = avctx->priv_data; uint8_t *bytestream, *bytestream_start, *bytestream_end; int i, h, h1, c, n, linesize, ret; uint8_t *ptr, *ptr1, *ptr2; @@ -82,12 +89,22 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, h1 = (h * 3) / 2; break; case AV_PIX_FMT_GBRPF32: + if (avctx->codec_id == AV_CODEC_ID_PFM) { c = 'F'; n = avctx->width * 4; + } else { + c = 'H'; + n = avctx->width * 2; + } break; case AV_PIX_FMT_GRAYF32: + if (avctx->codec_id == AV_CODEC_ID_PFM) { c = 'f'; n = avctx->width * 4; + } else { + c = 'h'; + n = avctx->width * 2; + } break; default: return -1; @@ -110,7 +127,7 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, bytestream += strlen(bytestream); } - if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32) { + if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32 && c == 'F') { float *r = (float *)p->data[2]; float *g = (float *)p->data[0]; float *b = (float *)p->data[1]; @@ -127,7 +144,7 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, g += p->linesize[0] / 4; b += p->linesize[1] / 4; } - } else if (avctx->pix_fmt == AV_PIX_FMT_GRAYF32) { + } else if (avctx->pix_fmt == AV_PIX_FMT_GRAYF32 && c == 'f') { const float *g = (const float *)p->data[0]; for (int i = 0; i < avctx->height; i++) { @@ -138,6 +155,34 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, g += p->linesize[0] / 4; } + } else if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32 && c == 'H') { + float *r = (float *)p->data[2]; + float *g = (float *)p->data[0]; + float *b = (float *)p->data[1]; + + for (int i = 0; i < avctx->height; i++) { + for (int j = 0; j < avctx->width; j++) { + AV_WN16(bytestream + 0, float2half(av_float2int(r[j]), s->basetable, s->shifttable)); + AV_WN16(bytestream + 2, float2half(av_float2int(g[j]), s->basetable, s->shifttable)); + AV_WN16(bytestream + 4, float2half(av_float2int(b[j]), s->basetable, s->shifttable)); + bytestream += 6; + } + + r += p->linesize[2] / 4; + g += p->linesize[0] / 4; + b += p->linesize[1] / 4; + } + } else if (avctx->pix_fmt == AV_PIX_FMT_GRAYF32 && c == 'h') { + const float *g = (const float *)p->data[0]; + + for (int i = 0; i < avctx->height; i++) { + for (int j = 0; j < avctx->width; j++) { + AV_WN16(bytestream, float2half(av_float2int(g[j]), s->basetable, s->shifttable)); + bytestream += 2; + } + + g += p->linesize[0] / 4; + } } else { ptr = p->data[0]; linesize = p->linesize[0]; @@ -241,3 +286,29 @@ const FFCodec ff_pfm_encoder = { .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, }; #endif + +#if CONFIG_PHM_ENCODER +static av_cold int phm_enc_init(AVCodecContext *avctx) +{ + PHMEncContext *s = avctx->priv_data; + + float2half_tables(s->basetable, s->shifttable); + + return 0; +} + +const FFCodec ff_phm_encoder = { + .p.name = "phm", + .p.long_name = NULL_IF_CONFIG_SMALL("PHM (Portable HalfFloatMap) image"), + .p.type = AVMEDIA_TYPE_VIDEO, + .p.id = AV_CODEC_ID_PHM, + .p.capabilities = AV_CODEC_CAP_DR1, + .priv_data_size = sizeof(PHMEncContext), + .init = phm_enc_init, + FF_CODEC_ENCODE_CB(pnm_encode_frame), + .p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_GBRPF32, + AV_PIX_FMT_GRAYF32, + AV_PIX_FMT_NONE }, + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, +}; +#endif |