[FFmpeg-devel] [PATCH] avcodec: add initial exr image encoder

James Almer jamrial at gmail.com
Mon Feb 15 19:05:04 EET 2021


On 2/15/2021 1:41 PM, Paul B Mahol wrote:
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
>   libavcodec/Makefile    |   1 +
>   libavcodec/allcodecs.c |   1 +
>   libavcodec/exrenc.c    | 480 +++++++++++++++++++++++++++++++++++++++++
>   libavformat/img2enc.c  |   2 +-
>   4 files changed, 483 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..686c7bac25
> --- /dev/null
> +++ b/libavcodec/exrenc.c

[...]

> +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;
> +    int64_t out_size = 2048LL + avctx->height * 16LL +
> +                      av_image_get_buffer_size(avctx->pix_fmt,
> +                                               avctx->width,
> +                                               avctx->height, 64) * 3LL / 2;
> +
> +    if ((ret = ff_alloc_packet2(avctx, pkt, out_size, 0)) < 0)

Pass out_size as min_packet too, to avoid using the internal 
byte_buffer. It will save a memcpy per packet.

> +        return ret;

[...]

> +    pkt->size   = bytestream2_tell_p(pb);

Use av_shrink_packet(), which will zero the padding bytes.

> +    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[] = {
> +    { "compression", "set compression type", OFFSET(compression), AV_OPT_TYPE_INT,   {.i64=0}, 0, EXR_NBCOMPR-1, VE, "compr" },
> +    { "none",        "none",                 0,                   AV_OPT_TYPE_CONST, {.i64=EXR_RAW}, 0, 0, VE, "compr" },
> +    { "rle" ,        "RLE",                  0,                   AV_OPT_TYPE_CONST, {.i64=EXR_RLE}, 0, 0, VE, "compr" },
> +    { "zip1",        "ZIP1",                 0,                   AV_OPT_TYPE_CONST, {.i64=EXR_ZIP1}, 0, 0, VE, "compr" },
> +    { "zip16",       "ZIP16",                0,                   AV_OPT_TYPE_CONST, {.i64=EXR_ZIP16}, 0, 0, VE, "compr" },
> +    { "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,
> +    .close          = encode_close,
> +    .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