mirror of https://github.com/FFmpeg/FFmpeg.git
parent
4136405c86
commit
307ab7fe8a
17 changed files with 521 additions and 3 deletions
@ -0,0 +1,79 @@ |
||||
/*
|
||||
* Radiance HDR parser |
||||
* Copyright (c) 2022 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 |
||||
*/ |
||||
|
||||
/**
|
||||
* @file |
||||
* Radiance HDR parser |
||||
*/ |
||||
|
||||
#include "libavutil/intreadwrite.h" |
||||
#include "parser.h" |
||||
|
||||
typedef struct HDRParseContext { |
||||
ParseContext pc; |
||||
} HDRParseContext; |
||||
|
||||
static int hdr_parse(AVCodecParserContext *s, AVCodecContext *avctx, |
||||
const uint8_t **poutbuf, int *poutbuf_size, |
||||
const uint8_t *buf, int buf_size) |
||||
{ |
||||
HDRParseContext *ipc = s->priv_data; |
||||
uint64_t state = ipc->pc.state64; |
||||
int next = END_NOT_FOUND, i = 0; |
||||
|
||||
*poutbuf_size = 0; |
||||
*poutbuf = NULL; |
||||
|
||||
if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
||||
next = buf_size; |
||||
} else { |
||||
for (; i < buf_size; i++) { |
||||
state = (state << 8) | buf[i]; |
||||
if (state == AV_RB64("ADIANCE\n") && (i > 10 || ipc->pc.index > 10)) { |
||||
next = i - 10; |
||||
break; |
||||
} |
||||
} |
||||
|
||||
ipc->pc.state64 = state; |
||||
if (ff_combine_frame(&ipc->pc, next, &buf, &buf_size) < 0) { |
||||
*poutbuf = NULL; |
||||
*poutbuf_size = 0; |
||||
return buf_size; |
||||
} |
||||
} |
||||
|
||||
*poutbuf = buf; |
||||
*poutbuf_size = buf_size; |
||||
|
||||
s->pict_type = AV_PICTURE_TYPE_I; |
||||
s->key_frame = 1; |
||||
s->duration = 1; |
||||
|
||||
return next; |
||||
} |
||||
|
||||
const AVCodecParser ff_hdr_parser = { |
||||
.codec_ids = { AV_CODEC_ID_RADIANCE_HDR }, |
||||
.priv_data_size = sizeof(HDRParseContext), |
||||
.parser_parse = hdr_parse, |
||||
.parser_close = ff_parse_close, |
||||
}; |
@ -0,0 +1,222 @@ |
||||
/*
|
||||
* 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 <stdlib.h> |
||||
|
||||
#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, const uint8_t *start) |
||||
{ |
||||
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) { |
||||
int run = scanline[3]; |
||||
for (int i = run << rshift; i > 0 && w > 0 && scanline >= start + 4; i--) { |
||||
memcpy(scanline, scanline - 4, 4); |
||||
scanline += 4; |
||||
} |
||||
w -= run << rshift; |
||||
rshift += 8; |
||||
if (rshift > 16) |
||||
break; |
||||
} 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]; |
||||
float sar; |
||||
|
||||
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)); |
||||
if (sscanf(line, "PIXASPECT=%f\n", &sar) == 1) |
||||
avctx->sample_aspect_ratio = p->sample_aspect_ratio = av_inv_q(av_d2q(sar, 4096)); |
||||
} 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, scanline); |
||||
goto convert; |
||||
} |
||||
|
||||
i = bytestream2_peek_byte(&gb); |
||||
if (i != 2) { |
||||
decompress(scanline, width, &gb, scanline); |
||||
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, scanline); |
||||
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 && bytestream2_get_bytes_left(&gb) > 0;) { |
||||
int run = bytestream2_get_byte(&gb); |
||||
if (run > 128) { |
||||
uint8_t val = bytestream2_get_byte(&gb); |
||||
run &= 127; |
||||
while (run--) { |
||||
if (j >= width * 4) |
||||
break; |
||||
scanline[j] = val; |
||||
j += 4; |
||||
} |
||||
} else if (run > 0) { |
||||
while (run--) { |
||||
if (j >= width * 4) |
||||
break; |
||||
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_RADIANCE_HDR, |
||||
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, |
||||
FF_CODEC_DECODE_CB(hdr_decode_frame), |
||||
}; |
@ -0,0 +1,189 @@ |
||||
/*
|
||||
* 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_get_encode_buffer(avctx, pkt, packet_size, 0)) < 0) |
||||
return ret; |
||||
|
||||
buf = pkt->data; |
||||
bytestream_put_str(&buf, "#?RADIANCE\n"); |
||||
bytestream_put_str(&buf, "SOFTWARE=lavc\n"); |
||||
ret = snprintf(buf, 32, "PIXASPECT=%f\n", av_q2d(av_inv_q(avctx->sample_aspect_ratio))); |
||||
if (ret > 0) |
||||
buf += ret; |
||||
bytestream_put_str(&buf, "FORMAT=32-bit_rle_rgbe\n\n"); |
||||
ret = snprintf(buf, 32, "-Y %d +X %d\n", avctx->height, 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->flags |= AV_PKT_FLAG_KEY; |
||||
|
||||
av_shrink_packet(pkt, 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_RADIANCE_HDR, |
||||
.p.capabilities = AV_CODEC_CAP_DR1 | 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 | FF_CODEC_CAP_INIT_CLEANUP, |
||||
}; |
Loading…
Reference in new issue