From patchwork Tue Jul 5 15:45:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 36675 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:8b27:b0:88:1bbf:7fd2 with SMTP id l39csp3573358pzh; Tue, 5 Jul 2022 08:42:49 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vrHtr3WYfXWJBrSoHt3h1uz7WUo9HiZaoqcsl+SC8PEzt+M3R/WOvSAjS0StDVUn3x3WvQ X-Received: by 2002:a17:906:c150:b0:726:b4e7:772 with SMTP id dp16-20020a170906c15000b00726b4e70772mr33602841ejc.499.1657035769261; Tue, 05 Jul 2022 08:42:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657035769; cv=none; d=google.com; s=arc-20160816; b=FipjyIstM1v6Jo72BiEHLTBiS8tJU115RsW3tE3Oo5R3EGtbi5sag2pvcDSONPAx53 Q6c25VoKb0boxQqC5UhEZzOWlcpNXf15dPLrCAhAYMi58pBa7kz/CRYaY2EDvn5VEFGq ej4mtp/XgWDJPOMIrl1O3dGc3HC7H9UyAPf9Bh+PLz3Js25LpBlHenT3kVFTJhs+mOaS OF3KXJwpY2F6ZGQW64ueN0qdmxLIUnWS2D+y6yZeBHRObZ5jIPN9ubsRRzm8erc9zNeL VYZwoChpobiV6mvWaYo2FcFYTWqHFql5LDzZHEF1v+BRChN3uXAxrn93FjaDRqsabkzH o1cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject:to :message-id:date:from:mime-version:dkim-signature:delivered-to; bh=tTeSQWt27/67/tRtWyGg6Esl9mYUNSRnDyITVq++QAo=; b=wpUZL6tBWbwhDnrAvDEnqllM7IOzVfoFfZkZVD8bEaM2mu3oy5hpzjPlM/VjrJCqry Ml4hRYzmbdJBu0tz46yyUuNS7sQ7qwCkYY66Kp7gQDDLRMukZgAHV4165QOHy2lWu7Iv 7+XEK7+wxsQJnnpWO52aNIn4a297xzChkCOxiQTWnihXdetW9NNRMo7axgGFr0Yi3YXX ZRr84Fr1oyYHhIu220wcmgtQ/lKOcw/jyoFI3OhL0rI4aJdnzOlpZ4Ikj7wEUy3Z/6ah j0P2Zi7lixdddFwO5xbl2QCBJDpRMawvSI+Lpu/oV5/2VtBmcCyLIpCGoUauo+hK3JMQ zF9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=MZ2v6IGN; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id yz12-20020a170906dc4c00b0072aed3b27f5si589593ejb.729.2022.07.05.08.42.48; Tue, 05 Jul 2022 08:42:49 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=MZ2v6IGN; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1632368B76C; Tue, 5 Jul 2022 18:42:45 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.182]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3226468B76C for ; Tue, 5 Jul 2022 18:42:39 +0300 (EEST) Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-3137316bb69so112751207b3.10 for ; Tue, 05 Jul 2022 08:42:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:from:date:message-id:subject:to; bh=ZEL8OxTHUA8ULPlzYg89x+EVy1RCgXyaJl81yqvhHvY=; b=MZ2v6IGNyCCtsOfr4biFr4kRrPpydInABIemTy2An/w/cnNgG1i+ljiu3LNp6Os3mN fvYcUW7D5O/tJIVmd5+qA1Q31VyKm6VeeSx1yGDfuiRC/BU/27T8ougBuMoqldJTNM6f pGBaeJAmSrfSjTWvVhssBBQKUEURzgm8Fj0rq98+NN9Bp9fGcYy8FpTYz1MTT0nXDHOX mxFys8rp+pENGBll1nbkP3gFUWhmRUSbFlz+qSWVK+HNYnM8OPNwGni4IFajIUMEQcuH fDTAPcVnit7GnRTJWKcTVILNnkQds5EsuR+lDW4m1HoL1vPq9DESGt1rI2DFPKS/eNKx l/oA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=ZEL8OxTHUA8ULPlzYg89x+EVy1RCgXyaJl81yqvhHvY=; b=gb4HV6QcB+cwwcsT7++itlSrTuvuHL9R28Yxf/863hOarlZ27wOd8m2ZKbBUcmTqpY tg7AHRmeEYYtEQVmREZy4zFq5rNa32pktsAhKk9Yrcqmqj5Jo3n9Dt7ho4OZUzDKSYNY R15zH62HD1ZsBI8mTDRmKS35gaz+LYDgFgJ4+qgZpqTvsYsMzcXPNvZk33+6+PRcNJAw y8vAc66yUoMiHncHgGOuoo1nHLYrHfVtvbyhF6S21aTvFy3zVhoq/OIlsPl0l6yTqfDg ClbHpY0bI45aBbOvRn16PlKxLORRlpnTHkfN6w2KMkC1uG/7sgvOSX2nvuZjSV0W0k1G LqGA== X-Gm-Message-State: AJIora8y4dQ+N4Pfa8vs+eHrhamQLv1x26/lNwGePogxMSgtdWVlpEOd rUt63WUfERqWJ7PfkWGL1lffbmjuEoqMuk5h4dRfZJsu X-Received: by 2002:a81:1257:0:b0:31c:97fe:e416 with SMTP id 84-20020a811257000000b0031c97fee416mr12923726yws.34.1657035757449; Tue, 05 Jul 2022 08:42:37 -0700 (PDT) MIME-Version: 1.0 From: Paul B Mahol Date: Tue, 5 Jul 2022 17:45:20 +0200 Message-ID: To: FFmpeg development discussions and patches X-Content-Filtered-By: Mailman/MimeDel 2.1.29 Subject: [FFmpeg-devel] [PATCH] avcodec: add Radiance HDR image format support X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: +sdiR4dgn3mv Hello, Patch attached. From 62fce2bfd811eb4fb86b5907d62e67f0a2d033ff Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Sun, 3 Jul 2022 23:50:05 +0200 Subject: [PATCH] avcodec: add Radiance HDR image format support Signed-off-by: Paul B Mahol --- doc/general_contents.texi | 2 + libavcodec/Makefile | 2 + libavcodec/allcodecs.c | 2 + libavcodec/codec_desc.c | 7 ++ libavcodec/codec_id.h | 1 + libavcodec/hdrdec.c | 212 ++++++++++++++++++++++++++++++++++++++ libavcodec/hdrenc.c | 188 +++++++++++++++++++++++++++++++++ libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/img2.c | 1 + libavformat/img2dec.c | 8 ++ libavformat/img2enc.c | 2 +- 12 files changed, 426 insertions(+), 1 deletion(-) create mode 100644 libavcodec/hdrdec.c create mode 100644 libavcodec/hdrenc.c diff --git a/doc/general_contents.texi b/doc/general_contents.texi index b1d3e3aa05..f25c784d3b 100644 --- a/doc/general_contents.texi +++ b/doc/general_contents.texi @@ -749,6 +749,8 @@ following image formats are supported: @tab OpenEXR @item FITS @tab X @tab X @tab Flexible Image Transport System +@item HDR @tab X @tab X + @tab Radiance HDR RGBE Image format @item IMG @tab @tab X @tab GEM Raster image @item JPEG @tab X @tab X diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 457ec58377..df7e227a7f 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -403,6 +403,8 @@ OBJS-$(CONFIG_HAP_DECODER) += hapdec.o hap.o OBJS-$(CONFIG_HAP_ENCODER) += hapenc.o hap.o OBJS-$(CONFIG_HCA_DECODER) += hcadec.o OBJS-$(CONFIG_HCOM_DECODER) += hcom.o +OBJS-$(CONFIG_HDR_DECODER) += hdrdec.o +OBJS-$(CONFIG_HDR_ENCODER) += hdrenc.o OBJS-$(CONFIG_HEVC_DECODER) += hevcdec.o hevc_mvs.o \ hevc_cabac.o hevc_refs.o hevcpred.o \ hevcdsp.o hevc_filter.o hevc_data.o \ diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index bdfc2f6f45..31d2c5979c 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -469,6 +469,8 @@ extern const FFCodec ff_gsm_decoder; extern const FFCodec ff_gsm_ms_decoder; extern const FFCodec ff_hca_decoder; extern const FFCodec ff_hcom_decoder; +extern const FFCodec ff_hdr_encoder; +extern const FFCodec ff_hdr_decoder; extern const FFCodec ff_iac_decoder; extern const FFCodec ff_ilbc_decoder; extern const FFCodec ff_imc_decoder; diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index 44ad2d1fe8..eeea15b1ef 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -1893,6 +1893,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("PHM (Portable HalfFloatMap) image"), .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS, }, + { + .id = AV_CODEC_ID_HDR, + .type = AVMEDIA_TYPE_VIDEO, + .name = "hdr", + .long_name = NULL_IF_CONFIG_SMALL("HDR (Radiance RGBE format) image"), + .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY, + }, /* various PCM "codecs" */ { diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h index 81fb316cff..005efa6334 100644 --- a/libavcodec/codec_id.h +++ b/libavcodec/codec_id.h @@ -312,6 +312,7 @@ enum AVCodecID { AV_CODEC_ID_JPEGXL, AV_CODEC_ID_QOI, AV_CODEC_ID_PHM, + AV_CODEC_ID_HDR, /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff --git a/libavcodec/hdrdec.c b/libavcodec/hdrdec.c new file mode 100644 index 0000000000..2178a824bd --- /dev/null +++ b/libavcodec/hdrdec.c @@ -0,0 +1,212 @@ +/* + * Radiance HDR image format + * + * 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/imgutils.h" +#include "avcodec.h" +#include "internal.h" +#include "bytestream.h" +#include "codec_internal.h" +#include "thread.h" + +#define MINELEN 8 +#define MAXELEN 0x7fff + +static int hdr_get_line(GetByteContext *gb, uint8_t *buffer, int size) +{ + int n = 0, c; + + memset(buffer, 0, size); + + do { + c = bytestream2_get_byte(gb); + if (n < size - 1) + buffer[n++] = c; + } while (bytestream2_get_bytes_left(gb) > 0 && c != '\n'); + + return 0; +} + +static float convert(int expo, int val) +{ + if (expo == -128) { + return 0.f; + } else { + const float v = val / 256.f; + + return ldexpf(v, expo); + } +} + +static int decompress(uint8_t *scanline, int w, GetByteContext *gb) +{ + int rshift = 0; + + while (w > 0) { + scanline[0] = bytestream2_get_byte(gb); + scanline[1] = bytestream2_get_byte(gb); + scanline[2] = bytestream2_get_byte(gb); + scanline[3] = bytestream2_get_byte(gb); + + if (scanline[0] == 1 && + scanline[1] == 1 && + scanline[2] == 1) { + for (int i = scanline[3] << rshift; i > 0; i--) { + memcpy(scanline, scanline - 4, 4); + scanline += 4; + w--; + } + rshift += 8; + } else { + scanline += 4; + w--; + rshift = 0; + } + } + + return 1; +} + +static int hdr_decode_frame(AVCodecContext *avctx, AVFrame *p, + int *got_frame, AVPacket *avpkt) +{ + const uint8_t *buf = avpkt->data; + int ret, buf_size = avpkt->size; + int width = 0, height = 0; + GetByteContext gb; + uint8_t line[512]; + + bytestream2_init(&gb, buf, buf_size); + hdr_get_line(&gb, line, sizeof(line)); + if (memcmp("#?RADIANCE\n", line, 11)) + return AVERROR_INVALIDDATA; + + do { + hdr_get_line(&gb, line, sizeof(line)); + } while (line[0] != '\n'); + + hdr_get_line(&gb, line, sizeof(line)); + if (sscanf(line, "-Y %d +X %d\n", &height, &width) == 2) { + ; + } else if (sscanf(line, "+Y %d +X %d\n", &height, &width) == 2) { + ; + } else if (sscanf(line, "-Y %d -X %d\n", &height, &width) == 2) { + ; + } else if (sscanf(line, "+Y %d -X %d\n", &height, &width) == 2) { + ; + } else if (sscanf(line, "-X %d +Y %d\n", &width, &height) == 2) { + ; + } else if (sscanf(line, "+X %d +Y %d\n", &width, &height) == 2) { + ; + } else if (sscanf(line, "-X %d -Y %d\n", &width, &height) == 2) { + ; + } else if (sscanf(line, "+X %d -Y %d\n", &width, &height) == 2) { + ; + } + + if ((ret = ff_set_dimensions(avctx, width, height)) < 0) + return ret; + + avctx->pix_fmt = AV_PIX_FMT_GBRPF32; + if ((ret = ff_thread_get_buffer(avctx, p, 0)) < 0) + return ret; + + for (int y = 0; y < height; y++) { + float *dst_r = (float *)(p->data[2] + y * p->linesize[2]); + float *dst_g = (float *)(p->data[0] + y * p->linesize[0]); + float *dst_b = (float *)(p->data[1] + y * p->linesize[1]); + uint8_t *scanline = p->data[0] + y * p->linesize[0]; + int i; + + if (width < MINELEN || width > MAXELEN) { + decompress(scanline, width, &gb); + goto convert; + } + + i = bytestream2_peek_byte(&gb); + if (i != 2) { + decompress(scanline, width, &gb); + goto convert; + } + bytestream2_skip(&gb, 1); + + scanline[1] = bytestream2_get_byte(&gb); + scanline[2] = bytestream2_get_byte(&gb); + i = bytestream2_get_byte(&gb); + + if (scanline[1] != 2 || scanline[2] & 128) { + scanline[0] = 2; + scanline[3] = i; + decompress(scanline + 4, width - 1, &gb); + goto convert; + } + + for (int i = 0; i < 4; i++) { + uint8_t *scanline = p->data[0] + y * p->linesize[0] + i; + + for (int j = 0; j < width * 4; ) { + int run = bytestream2_get_byte(&gb); + if (run > 128) { + uint8_t val = bytestream2_get_byte(&gb); + run &= 127; + while (run--) { + scanline[j] = val; + j += 4; + } + } else { + while (run--) { + scanline[j] = bytestream2_get_byte(&gb); + j += 4; + } + } + } + } + +convert: + for (int x = 0; x < width; x++) { + uint8_t rgbe[4]; + int expo; + + memcpy(rgbe, p->data[0] + y * p->linesize[0] + x * 4, 4); + expo = rgbe[3] - 128; + + dst_r[x] = convert(expo, rgbe[0]); + dst_b[x] = convert(expo, rgbe[2]); + dst_g[x] = convert(expo, rgbe[1]); + } + } + + p->key_frame = 1; + p->pict_type = AV_PICTURE_TYPE_I; + + *got_frame = 1; + + return buf_size; +} + +const FFCodec ff_hdr_decoder = { + .p.name = "hdr", + .p.long_name = NULL_IF_CONFIG_SMALL("HDR (Radiance RGBE format) image"), + .p.type = AVMEDIA_TYPE_VIDEO, + .p.id = AV_CODEC_ID_HDR, + .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, + FF_CODEC_DECODE_CB(hdr_decode_frame), +}; diff --git a/libavcodec/hdrenc.c b/libavcodec/hdrenc.c new file mode 100644 index 0000000000..dfacb8d725 --- /dev/null +++ b/libavcodec/hdrenc.c @@ -0,0 +1,188 @@ +/* + * Radiance HDR image format + * + * 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 "libavutil/imgutils.h" +#include "avcodec.h" +#include "bytestream.h" +#include "codec_internal.h" +#include "encode.h" + +typedef struct HDREncContext { + uint8_t *scanline; +} HDREncContext; + +static av_cold int hdr_encode_init(AVCodecContext *avctx) +{ + HDREncContext *s = avctx->priv_data; + + s->scanline = av_calloc(avctx->width * 4, sizeof(*s->scanline)); + if (!s->scanline) + return AVERROR(ENOMEM); + + return 0; +} + +static av_cold int hdr_encode_close(AVCodecContext *avctx) +{ + HDREncContext *s = avctx->priv_data; + + av_freep(&s->scanline); + + return 0; +} + +static void bytestream_put_str(uint8_t **buf, const char *const line) +{ + bytestream_put_buffer(buf, line, strlen(line)); +} + +static void float2rgbe(uint8_t *rgbe, float red, float green, float blue) +{ + float v; + int e; + + v = FFMAX3(red, green, blue); + + if (v < 1e-32f) { + rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0; + } else { + v = frexpf(v, &e) * 256.f / v; + + rgbe[0] = av_clip_uint8(red * v); + rgbe[1] = av_clip_uint8(green * v); + rgbe[2] = av_clip_uint8(blue * v); + rgbe[3] = av_clip_uint8(e + 128); + } +} + +static void rle(uint8_t **buffer, const uint8_t *data, int width) +{ +#define MIN_RLE 4 + int cur = 0; + + while (cur < width) { + int run_count = 0, old_run_count = 0; + int beg_run = cur; + uint8_t buf[2]; + + while (run_count < MIN_RLE && beg_run < width) { + beg_run += run_count; + old_run_count = run_count; + run_count = 1; + while ((beg_run + run_count < width) && (run_count < 127) && + (data[beg_run * 4] == data[(beg_run + run_count) * 4])) + run_count++; + } + + if ((old_run_count > 1) && (old_run_count == beg_run - cur)) { + buf[0] = 128 + old_run_count; + buf[1] = data[cur * 4]; + bytestream_put_buffer(buffer, buf, sizeof(buf)); + cur = beg_run; + } + + while (cur < beg_run) { + int nonrun_count = FFMIN(128, beg_run - cur); + buf[0] = nonrun_count; + bytestream_put_byte(buffer, buf[0]); + for (int n = 0; n < nonrun_count; n++) + bytestream_put_byte(buffer, data[(cur + n) * 4]); + cur += nonrun_count; + } + + if (run_count >= MIN_RLE) { + buf[0] = 128 + run_count; + buf[1] = data[beg_run * 4]; + bytestream_put_buffer(buffer, buf, sizeof(buf)); + cur += run_count; + } + } +} + +static int hdr_encode_frame(AVCodecContext *avctx, AVPacket *pkt, + const AVFrame *frame, int *got_packet) +{ + HDREncContext *s = avctx->priv_data; + int64_t packet_size; + uint8_t *buf; + int ret; + + packet_size = avctx->width * avctx->height * 4LL + 1024LL; + if ((ret = ff_alloc_packet(avctx, pkt, packet_size)) < 0) + return ret; + + buf = pkt->data; + bytestream_put_str(&buf, "#?RADIANCE\n"); + bytestream_put_str(&buf, "FORMAT=32-bit_rle_rgbe\n\n"); + bytestream_put_str(&buf, "-Y "); + ret = snprintf(buf, 16, "%d ", avctx->height); + if (ret > 0) + buf += ret; + bytestream_put_str(&buf, "+X "); + ret = snprintf(buf, 16, "%d\n", avctx->width); + if (ret > 0) + buf += ret; + + for (int y = 0; y < avctx->height; y++) { + const float *red = (const float *)(frame->data[2] + y * frame->linesize[2]); + const float *green = (const float *)(frame->data[0] + y * frame->linesize[0]); + const float *blue = (const float *)(frame->data[1] + y * frame->linesize[1]); + + if (avctx->width < 8 || avctx->width > 0x7fff) { + for (int x = 0; x < avctx->width; x++) { + float2rgbe(buf, red[x], green[x], blue[x]); + buf += 4; + } + } else { + bytestream_put_byte(&buf, 2); + bytestream_put_byte(&buf, 2); + bytestream_put_byte(&buf, avctx->width >> 8); + bytestream_put_byte(&buf, avctx->width & 0xFF); + + for (int x = 0; x < avctx->width; x++) + float2rgbe(s->scanline + 4 * x, red[x], green[x], blue[x]); + for (int p = 0; p < 4; p++) + rle(&buf, s->scanline + p, avctx->width); + } + } + + pkt->size = buf - pkt->data; + + *got_packet = 1; + + return 0; +} + +const FFCodec ff_hdr_encoder = { + .p.name = "hdr", + .p.long_name = NULL_IF_CONFIG_SMALL("HDR (Radiance RGBE format) image"), + .priv_data_size = sizeof(HDREncContext), + .p.type = AVMEDIA_TYPE_VIDEO, + .p.id = AV_CODEC_ID_HDR, + .p.capabilities = AV_CODEC_CAP_FRAME_THREADS, + .init = hdr_encode_init, + FF_CODEC_ENCODE_CB(hdr_encode_frame), + .close = hdr_encode_close, + .p.pix_fmts = (const enum AVPixelFormat[]){ + AV_PIX_FMT_GBRPF32, + AV_PIX_FMT_NONE + }, + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, +}; diff --git a/libavformat/Makefile b/libavformat/Makefile index 6c6b779080..e420384355 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -273,6 +273,7 @@ OBJS-$(CONFIG_IMAGE_DPX_PIPE_DEMUXER) += img2dec.o img2.o OBJS-$(CONFIG_IMAGE_EXR_PIPE_DEMUXER) += img2dec.o img2.o OBJS-$(CONFIG_IMAGE_GEM_PIPE_DEMUXER) += img2dec.o img2.o OBJS-$(CONFIG_IMAGE_GIF_PIPE_DEMUXER) += img2dec.o img2.o +OBJS-$(CONFIG_IMAGE_HDR_PIPE_DEMUXER) += img2dec.o img2.o OBJS-$(CONFIG_IMAGE_J2K_PIPE_DEMUXER) += img2dec.o img2.o OBJS-$(CONFIG_IMAGE_JPEG_PIPE_DEMUXER) += img2dec.o img2.o OBJS-$(CONFIG_IMAGE_JPEGLS_PIPE_DEMUXER) += img2dec.o img2.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 32698b857f..ae4479fb7a 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -508,6 +508,7 @@ extern const AVInputFormat ff_image_dpx_pipe_demuxer; extern const AVInputFormat ff_image_exr_pipe_demuxer; extern const AVInputFormat ff_image_gem_pipe_demuxer; extern const AVInputFormat ff_image_gif_pipe_demuxer; +extern const AVInputFormat ff_image_hdr_pipe_demuxer; extern const AVInputFormat ff_image_j2k_pipe_demuxer; extern const AVInputFormat ff_image_jpeg_pipe_demuxer; extern const AVInputFormat ff_image_jpegls_pipe_demuxer; diff --git a/libavformat/img2.c b/libavformat/img2.c index 870d2ebbc5..b03075f3b0 100644 --- a/libavformat/img2.c +++ b/libavformat/img2.c @@ -91,6 +91,7 @@ const IdStrMap ff_img_tags[] = { { AV_CODEC_ID_VBN, "vbn" }, { AV_CODEC_ID_JPEGXL, "jxl" }, { AV_CODEC_ID_QOI, "qoi" }, + { AV_CODEC_ID_HDR, "hdr" }, { AV_CODEC_ID_NONE, NULL } }; diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c index e7ff26e5dd..4c6c70139c 100644 --- a/libavformat/img2dec.c +++ b/libavformat/img2dec.c @@ -1070,6 +1070,13 @@ static int pam_probe(const AVProbeData *p) return pnm_magic_check(p, 7) ? pnm_probe(p) : 0; } +static int hdr_probe(const AVProbeData *p) +{ + if (!memcmp(p->buf, "#?RADIANCE\n", 11)) + return AVPROBE_SCORE_MAX; + return 0; +} + static int xbm_probe(const AVProbeData *p) { if (!memcmp(p->buf, "/* XBM X10 format */", 20)) @@ -1221,6 +1228,7 @@ IMAGEAUTO_DEMUXER(dpx, DPX) IMAGEAUTO_DEMUXER(exr, EXR) IMAGEAUTO_DEMUXER(gem, GEM) IMAGEAUTO_DEMUXER(gif, GIF) +IMAGEAUTO_DEMUXER(hdr, HDR) IMAGEAUTO_DEMUXER_EXT(j2k, JPEG2000, J2K) IMAGEAUTO_DEMUXER_EXT(jpeg, MJPEG, JPEG) IMAGEAUTO_DEMUXER(jpegls, JPEGLS) diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c index 0a11fae34e..734f93c686 100644 --- a/libavformat/img2enc.c +++ b/libavformat/img2enc.c @@ -267,7 +267,7 @@ const AVOutputFormat ff_image2_muxer = { .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"), .extensions = "bmp,dpx,exr,jls,jpeg,jpg,jxl,ljpg,pam,pbm,pcx,pfm,pgm,pgmyuv,phm," "png,ppm,sgi,tga,tif,tiff,jp2,j2c,j2k,xwd,sun,ras,rs,im1,im8," - "im24,sunras,vbn,xbm,xface,pix,y,avif,qoi", + "im24,sunras,vbn,xbm,xface,pix,y,avif,qoi,hdr", .priv_data_size = sizeof(VideoMuxData), .video_codec = AV_CODEC_ID_MJPEG, .write_header = write_header, -- 2.36.1