[FFmpeg-devel] [PATCH] avcodec: add initial exr image encoder
Andreas Rheinhardt
andreas.rheinhardt at gmail.com
Mon Feb 15 03:28:54 EET 2021
Paul B Mahol:
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
> libavcodec/Makefile | 1 +
> libavcodec/allcodecs.c | 1 +
> libavcodec/exrenc.c | 209 +++++++++++++++++++++++++++++++++++++++++
> libavformat/img2enc.c | 2 +-
> 4 files changed, 212 insertions(+), 1 deletion(-)
> create mode 100644 libavcodec/exrenc.c
>
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index a98ec408bb..5fb735f129 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -321,6 +321,7 @@ OBJS-$(CONFIG_ESCAPE124_DECODER) += escape124.o
> OBJS-$(CONFIG_ESCAPE130_DECODER) += escape130.o
> OBJS-$(CONFIG_EVRC_DECODER) += evrcdec.o acelp_vectors.o lsp.o
> OBJS-$(CONFIG_EXR_DECODER) += exr.o exrdsp.o
> +OBJS-$(CONFIG_EXR_ENCODER) += exrenc.o
> OBJS-$(CONFIG_FASTAUDIO_DECODER) += fastaudio.o
> OBJS-$(CONFIG_FFV1_DECODER) += ffv1dec.o ffv1.o
> OBJS-$(CONFIG_FFV1_ENCODER) += ffv1enc.o ffv1.o
> diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
> index 16ec182a52..cb3f0e7c18 100644
> --- a/libavcodec/allcodecs.c
> +++ b/libavcodec/allcodecs.c
> @@ -109,6 +109,7 @@ extern AVCodec ff_eightsvx_exp_decoder;
> extern AVCodec ff_eightsvx_fib_decoder;
> extern AVCodec ff_escape124_decoder;
> extern AVCodec ff_escape130_decoder;
> +extern AVCodec ff_exr_encoder;
> extern AVCodec ff_exr_decoder;
> extern AVCodec ff_ffv1_encoder;
> extern AVCodec ff_ffv1_decoder;
> diff --git a/libavcodec/exrenc.c b/libavcodec/exrenc.c
> new file mode 100644
> index 0000000000..0954fec14a
> --- /dev/null
> +++ b/libavcodec/exrenc.c
> @@ -0,0 +1,209 @@
> +/*
> + * OpenEXR image format
> + *
> + * Copyright (c) 2021 Paul B Mahol
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#include <float.h>
> +
> +#include "libavutil/avassert.h"
> +#include "libavutil/opt.h"
> +#include "libavutil/intreadwrite.h"
> +#include "libavutil/imgutils.h"
> +#include "libavutil/pixdesc.h"
> +#include "avcodec.h"
> +#include "bytestream.h"
> +#include "internal.h"
> +
> +enum ExrPixelType {
> + EXR_UINT,
> + EXR_HALF,
> + EXR_FLOAT,
> + EXR_UNKNOWN,
> +};
> +
> +static const char abgr_chlist[4] = { 'A', 'B', 'G', 'R' };
> +static const char bgr_chlist[4] = { 'B', 'G', 'R', 'A' };
> +static const uint8_t gbra_order[4] = { 3, 1, 0, 2 };
> +static const uint8_t gbr_order[4] = { 1, 0, 2, 0 };
> +
> +typedef struct EXRContext {
> + const AVClass *class;
> +
> + int planes;
> + float gamma;
> + const char *ch_names;
> + const uint8_t *ch_order;
> + PutByteContext pb;
> +} EXRContext;
> +
> +static int encode_init(AVCodecContext *avctx)
> +{
> + EXRContext *s = avctx->priv_data;
> +
> + switch (avctx->pix_fmt) {
> + case AV_PIX_FMT_GBRPF32:
> + s->planes = 3;
> + s->ch_names = bgr_chlist;
> + s->ch_order = gbr_order;
> + break;
> + case AV_PIX_FMT_GBRAPF32:
> + s->planes = 4;
> + s->ch_names = abgr_chlist;
> + s->ch_order = gbra_order;
> + break;
> + default:
> + av_assert0(0);
> + }
> +
> + return 0;
> +}
> +
> +static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
> + const AVFrame *frame, int *got_packet)
> +{
> + EXRContext *s = avctx->priv_data;
> + PutByteContext *pb = &s->pb;
> + int64_t offset;
> + int ret;
> + size_t out_size = 2048LL + avctx->height * 16LL +
> + av_image_get_buffer_size(avctx->pix_fmt,
> + avctx->width,
> + avctx->height, 64);
There is a potential for information loss in the int64_t -> size_t
conversion here.
> +
> + if ((ret = ff_alloc_packet2(avctx, pkt, out_size, 0)) < 0)
> + return ret;
> +
> + bytestream2_init_writer(pb, pkt->data, pkt->size);
> +
> + bytestream2_put_le32(pb, 20000630);
> + bytestream2_put_byte(pb, 2);
> + bytestream2_put_le24(pb, 0);
> + bytestream2_put_buffer(pb, "channels\0chlist\0", 16);
> + bytestream2_put_le32(pb, s->planes * 18 + 1);
> +
> + for (int p = 0; p < s->planes; p++) {
> + bytestream2_put_byte(pb, s->ch_names[p]);
> + bytestream2_put_byte(pb, 0);
> + bytestream2_put_le32(pb, EXR_FLOAT);
> + bytestream2_put_le32(pb, 0);
> + bytestream2_put_le32(pb, 1);
> + bytestream2_put_le32(pb, 1);
> + }
> + bytestream2_put_byte(pb, 0);
> +
> + bytestream2_put_buffer(pb, "compression\0compression\0", 24);
> + bytestream2_put_le32(pb, 1);
> + bytestream2_put_byte(pb, 0);
> +
> + bytestream2_put_buffer(pb, "dataWindow\0box2i\0", 17);
> + bytestream2_put_le32(pb, 16);
> + bytestream2_put_le32(pb, 0);
> + bytestream2_put_le32(pb, 0);
> + bytestream2_put_le32(pb, avctx->width - 1);
> + bytestream2_put_le32(pb, avctx->height - 1);
> +
> + bytestream2_put_buffer(pb, "displayWindow\0box2i\0", 20);
> + bytestream2_put_le32(pb, 16);
> + bytestream2_put_le32(pb, 0);
> + bytestream2_put_le32(pb, 0);
> + bytestream2_put_le32(pb, avctx->width - 1);
> + bytestream2_put_le32(pb, avctx->height - 1);
> +
> + bytestream2_put_buffer(pb, "lineOrder\0lineOrder\0", 20);
> + bytestream2_put_le32(pb, 1);
> + bytestream2_put_byte(pb, 0);
> +
> + bytestream2_put_buffer(pb, "screenWindowCenter\0v2f\0", 23);
> + bytestream2_put_le32(pb, 8);
> + bytestream2_put_le64(pb, 0);
> +
> + bytestream2_put_buffer(pb, "screenWindowWidth\0float\0", 24);
> + bytestream2_put_le32(pb, 4);
> + bytestream2_put_le32(pb, av_float2int(1.f));
> +
> + if (avctx->sample_aspect_ratio.num && avctx->sample_aspect_ratio.den) {
> + bytestream2_put_buffer(pb, "pixelAspectRatio\0float\0", 23);
> + bytestream2_put_le32(pb, 4);
> + bytestream2_put_le32(pb, av_float2int(av_q2d(avctx->sample_aspect_ratio)));
> + }
> +
> + bytestream2_put_buffer(pb, "gamma\0float\0", 12);
> + bytestream2_put_le32(pb, 4);
> + bytestream2_put_le32(pb, av_float2int(s->gamma));
> +
> + bytestream2_put_buffer(pb, "writer\0string\0", 14);
> + bytestream2_put_le32(pb, 4);
> + bytestream2_put_buffer(pb, "lavc", 4);
> + bytestream2_put_byte(pb, 0);
> +
> + offset = bytestream2_tell_p(pb) + avctx->height * 8 + 8;
> + bytestream2_put_le64(pb, offset);
> +
> + for (int y = 0; y < avctx->height; y++) {
> + bytestream2_put_le64(pb, offset);
> + offset += avctx->width * s->planes * 4 + 8;
> + }
> +
> + for (int y = 0; y < avctx->height; y++) {
> + bytestream2_put_le32(pb, y);
> + bytestream2_put_le32(pb, s->planes * avctx->width * 4);
> + for (int p = 0; p < s->planes; p++) {
> + int ch = s->ch_order[p];
> + bytestream2_put_buffer(pb, frame->data[ch] + y * frame->linesize[ch],
> + avctx->width * 4);
> + }
> + }
> +
> + pkt->size = bytestream2_tell_p(pb);
> + pkt->flags |= AV_PKT_FLAG_KEY;
> + *got_packet = 1;
> +
> + return 0;
> +}
> +
> +#define OFFSET(x) offsetof(EXRContext, x)
> +#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
> +static const AVOption options[] = {
> + { "gamma", "set gamma", OFFSET(gamma), AV_OPT_TYPE_FLOAT, {.dbl=1.f}, 0.001, FLT_MAX, VE },
> + { NULL},
> +};
> +
> +static const AVClass exr_class = {
> + .class_name = "exr",
> + .item_name = av_default_item_name,
> + .option = options,
> + .version = LIBAVUTIL_VERSION_INT,
> +};
> +
> +AVCodec ff_exr_encoder = {
> + .name = "exr",
> + .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
> + .priv_data_size = sizeof(EXRContext),
> + .priv_class = &exr_class,
> + .type = AVMEDIA_TYPE_VIDEO,
> + .id = AV_CODEC_ID_EXR,
> + .init = encode_init,
> + .encode2 = encode_frame,
> + .capabilities = AV_CODEC_CAP_FRAME_THREADS,
> + .pix_fmts = (const enum AVPixelFormat[]) {
> + AV_PIX_FMT_GBRPF32,
> + AV_PIX_FMT_GBRAPF32,
> + AV_PIX_FMT_NONE },
> +};
> diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c
> index 84201d3beb..0f7a21ffa0 100644
> --- a/libavformat/img2enc.c
> +++ b/libavformat/img2enc.c
> @@ -260,7 +260,7 @@ static const AVClass img2mux_class = {
> AVOutputFormat ff_image2_muxer = {
> .name = "image2",
> .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
> - .extensions = "bmp,dpx,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pfm,pgm,pgmyuv,png,"
> + .extensions = "bmp,dpx,exr,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pfm,pgm,pgmyuv,png,"
> "ppm,sgi,tga,tif,tiff,jp2,j2c,j2k,xwd,sun,ras,rs,im1,im8,im24,"
> "sunras,xbm,xface,pix,y",
> .priv_data_size = sizeof(VideoMuxData),
>
More information about the ffmpeg-devel
mailing list