From patchwork Mon Feb 15 01:26:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 25630 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 2F6CE448E65 for ; Mon, 15 Feb 2021 03:26:20 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id F2B1268A181; Mon, 15 Feb 2021 03:26:19 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f50.google.com (mail-ej1-f50.google.com [209.85.218.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D3CBC689F3A for ; Mon, 15 Feb 2021 03:26:13 +0200 (EET) Received: by mail-ej1-f50.google.com with SMTP id f14so8638274ejc.8 for ; Sun, 14 Feb 2021 17:26:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=PZb+vRo9RL1hX3YoTSGSl7rBAUmDtD5t2DMqWvvp/lI=; b=uWoiYJUporISjaNW5PMxLD98gM9I4PRWqZiDe0aaVutp9tYkzrVWEmoW+D6VhOihNo u1K0JMN3rU1g1UfFG3L/b2SrmUFSf3YdytJcQNyqBv8EK5BsRsT+o+3g2WCLie2bB7Ij IsCpVjPKzMKbHx/ari1OF7bausWmmmyYK3K7+qaUw9M80BqXcpjxkegR8QuxO9mDbQ9W DKDSmkg3MhTrDcCYPUDOk+lGpgeQfNWC3C+F5/8rgqRBZ2G9ZDIgYp0gTmvflmKJt/Ww tkhvFaZgg89wULtLnvWZ+z/F5jIcQVvBoZJyOvQw4gaRivoNpaGal8sbZilU8ATOyWyo Ba9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=PZb+vRo9RL1hX3YoTSGSl7rBAUmDtD5t2DMqWvvp/lI=; b=f2zGHZwF+JZnnMgIFwVFrsOum1udhIb3cLAqhWJo+lcaG/S6U6kfSBtuFFrix03vHL gO3AbmyAIw0vOMLeHO1oLQ4tjaGpDJc2Q3J9F6XXW3VuQ8jSe+nS37rTGaCd1ZQSlj3M PvGb2hkmNYbznUWdokrbnyvdcTeZJN3dK/ClyMyPwSmzpmytglfxCH2EH8YgmIPHM6/e oYAnYMT9YFF20JsVrR2uYUKmJNQcmCTbW6e6jcmPr5h1Dt5Fkz9PD9rQMEQZuuhapNNj QbEZ31Nc/mI8ThimFD9Lrk2MWbD2caALUwAbcdlRKahcK+XFMaIulOPXkMUgy91qqOf6 /5Hw== X-Gm-Message-State: AOAM533jHAIbCQBZhoc/MBKhhfZPQ7n3lC8kXXvvf0PMUFunI7C7/xGB uD4bWh8wnJfAlif96He6STq3jiji4saqGg== X-Google-Smtp-Source: ABdhPJx9HgmzFcddYVUn62nSWMLQC/9YBizAqQMwyj37rwjaNIzBwMJz9okKMbQ5+ofbdatkmWdINA== X-Received: by 2002:a17:906:3881:: with SMTP id q1mr13392067ejd.490.1613352373140; Sun, 14 Feb 2021 17:26:13 -0800 (PST) Received: from localhost.localdomain ([94.250.162.225]) by smtp.gmail.com with ESMTPSA id g11sm2595309edq.49.2021.02.14.17.26.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Feb 2021 17:26:12 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Mon, 15 Feb 2021 02:26:02 +0100 Message-Id: <20210215012602.16493-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avcodec: add initial exr image encoder X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- 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 + +#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); + + 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),