FFmpeg
hdrenc.c
Go to the documentation of this file.
1 /*
2  * Radiance HDR image format
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/imgutils.h"
22 #include "avcodec.h"
23 #include "bytestream.h"
24 #include "codec_internal.h"
25 #include "encode.h"
26 
27 typedef struct HDREncContext {
28  uint8_t *scanline;
30 
32 {
33  HDREncContext *s = avctx->priv_data;
34 
35  s->scanline = av_calloc(avctx->width * 4, sizeof(*s->scanline));
36  if (!s->scanline)
37  return AVERROR(ENOMEM);
38 
39  return 0;
40 }
41 
43 {
44  HDREncContext *s = avctx->priv_data;
45 
46  av_freep(&s->scanline);
47 
48  return 0;
49 }
50 
51 static void bytestream_put_str(uint8_t **buf, const char *const line)
52 {
53  bytestream_put_buffer(buf, line, strlen(line));
54 }
55 
56 static void float2rgbe(uint8_t *rgbe, float red, float green, float blue)
57 {
58  float v;
59  int e;
60 
61  v = FFMAX3(red, green, blue);
62 
63  if (v < 1e-32f) {
64  rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;
65  } else {
66  v = frexpf(v, &e) * 256.f / v;
67 
68  rgbe[0] = av_clip_uint8(red * v);
69  rgbe[1] = av_clip_uint8(green * v);
70  rgbe[2] = av_clip_uint8(blue * v);
71  rgbe[3] = av_clip_uint8(e + 128);
72  }
73 }
74 
75 static void rle(uint8_t **buffer, const uint8_t *data, int width)
76 {
77 #define MIN_RLE 4
78  int cur = 0;
79 
80  while (cur < width) {
81  int run_count = 0, old_run_count = 0;
82  int beg_run = cur;
83  uint8_t buf[2];
84 
85  while (run_count < MIN_RLE && beg_run < width) {
86  beg_run += run_count;
87  old_run_count = run_count;
88  run_count = 1;
89  while ((beg_run + run_count < width) && (run_count < 127) &&
90  (data[beg_run * 4] == data[(beg_run + run_count) * 4]))
91  run_count++;
92  }
93 
94  if ((old_run_count > 1) && (old_run_count == beg_run - cur)) {
95  buf[0] = 128 + old_run_count;
96  buf[1] = data[cur * 4];
97  bytestream_put_buffer(buffer, buf, sizeof(buf));
98  cur = beg_run;
99  }
100 
101  while (cur < beg_run) {
102  int nonrun_count = FFMIN(128, beg_run - cur);
103  buf[0] = nonrun_count;
104  bytestream_put_byte(buffer, buf[0]);
105  for (int n = 0; n < nonrun_count; n++)
106  bytestream_put_byte(buffer, data[(cur + n) * 4]);
107  cur += nonrun_count;
108  }
109 
110  if (run_count >= MIN_RLE) {
111  buf[0] = 128 + run_count;
112  buf[1] = data[beg_run * 4];
113  bytestream_put_buffer(buffer, buf, sizeof(buf));
114  cur += run_count;
115  }
116  }
117 }
118 
120  const AVFrame *frame, int *got_packet)
121 {
122  HDREncContext *s = avctx->priv_data;
123  int64_t packet_size;
124  uint8_t *buf;
125  int ret;
126 
127  packet_size = avctx->height * 4LL + avctx->width * avctx->height * 8LL + 1024LL;
128  if ((ret = ff_get_encode_buffer(avctx, pkt, packet_size, 0)) < 0)
129  return ret;
130 
131  buf = pkt->data;
132  bytestream_put_str(&buf, "#?RADIANCE\n");
133  bytestream_put_str(&buf, "SOFTWARE=lavc\n");
134  ret = snprintf(buf, 32, "PIXASPECT=%f\n", av_q2d(av_inv_q(avctx->sample_aspect_ratio)));
135  if (ret > 0)
136  buf += ret;
137  bytestream_put_str(&buf, "FORMAT=32-bit_rle_rgbe\n\n");
138  ret = snprintf(buf, 32, "-Y %d +X %d\n", avctx->height, avctx->width);
139  if (ret > 0)
140  buf += ret;
141 
142  for (int y = 0; y < avctx->height; y++) {
143  const float *red = (const float *)(frame->data[2] + y * frame->linesize[2]);
144  const float *green = (const float *)(frame->data[0] + y * frame->linesize[0]);
145  const float *blue = (const float *)(frame->data[1] + y * frame->linesize[1]);
146 
147  if (avctx->width < 8 || avctx->width > 0x7fff) {
148  for (int x = 0; x < avctx->width; x++) {
149  float2rgbe(buf, red[x], green[x], blue[x]);
150  buf += 4;
151  }
152  } else {
153  bytestream_put_byte(&buf, 2);
154  bytestream_put_byte(&buf, 2);
155  bytestream_put_byte(&buf, avctx->width >> 8);
156  bytestream_put_byte(&buf, avctx->width & 0xFF);
157 
158  for (int x = 0; x < avctx->width; x++)
159  float2rgbe(s->scanline + 4 * x, red[x], green[x], blue[x]);
160  for (int p = 0; p < 4; p++)
161  rle(&buf, s->scanline + p, avctx->width);
162  }
163  }
164 
166 
167  av_shrink_packet(pkt, buf - pkt->data);
168 
169  *got_packet = 1;
170 
171  return 0;
172 }
173 
175  .p.name = "hdr",
176  CODEC_LONG_NAME("HDR (Radiance RGBE format) image"),
177  .priv_data_size = sizeof(HDREncContext),
178  .p.type = AVMEDIA_TYPE_VIDEO,
179  .p.id = AV_CODEC_ID_RADIANCE_HDR,
180  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
182  .init = hdr_encode_init,
184  .close = hdr_encode_close,
185  .p.pix_fmts = (const enum AVPixelFormat[]){
188  },
189  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
190 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
HDREncContext::scanline
uint8_t * scanline
Definition: hdrenc.c:28
int64_t
long long int64_t
Definition: coverity.c:34
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
AVPacket::data
uint8_t * data
Definition: packet.h:522
encode.h
data
const char data[16]
Definition: mxf.c:148
float2rgbe
static void float2rgbe(uint8_t *rgbe, float red, float green, float blue)
Definition: hdrenc.c:56
FFCodec
Definition: codec_internal.h:127
hdr_encode_close
static av_cold int hdr_encode_close(AVCodecContext *avctx)
Definition: hdrenc.c:42
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:577
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:365
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:113
hdr_encode_init
static av_cold int hdr_encode_init(AVCodecContext *avctx)
Definition: hdrenc.c:31
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:296
pkt
AVPacket * pkt
Definition: movenc.c:59
av_cold
#define av_cold
Definition: attributes.h:90
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:159
hdr_encode_frame
static int hdr_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: hdrenc.c:119
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:110
f
f
Definition: af_crystalizer.c:121
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
codec_internal.h
AV_PIX_FMT_GBRPF32
#define AV_PIX_FMT_GBRPF32
Definition: pixfmt.h:508
MIN_RLE
#define MIN_RLE
line
Definition: graph2dot.c:48
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:528
ff_hdr_encoder
const FFCodec ff_hdr_encoder
Definition: hdrenc.c:174
bytestream_put_buffer
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:372
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
bytestream_put_str
static void bytestream_put_str(uint8_t **buf, const char *const line)
Definition: hdrenc.c:51
AVCodecContext::height
int height
Definition: avcodec.h:618
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:445
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:105
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
av_clip_uint8
#define av_clip_uint8
Definition: common.h:104
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
FFMAX3
#define FFMAX3(a, b, c)
Definition: macros.h:48
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
bytestream.h
imgutils.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:389
HDREncContext
Definition: hdrenc.c:27
AV_CODEC_ID_RADIANCE_HDR
@ AV_CODEC_ID_RADIANCE_HDR
Definition: codec_id.h:316
snprintf
#define snprintf
Definition: snprintf.h:34
AVCodecContext::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition: avcodec.h:642
rle
static void rle(uint8_t **buffer, const uint8_t *data, int width)
Definition: hdrenc.c:75