/src/ffmpeg/libavcodec/hdrenc.c
Line | Count | Source |
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/mem.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; |
29 | | } HDREncContext; |
30 | | |
31 | | static av_cold int hdr_encode_init(AVCodecContext *avctx) |
32 | 548 | { |
33 | 548 | HDREncContext *s = avctx->priv_data; |
34 | | |
35 | 548 | s->scanline = av_calloc(avctx->width * 4, sizeof(*s->scanline)); |
36 | 548 | if (!s->scanline) |
37 | 0 | return AVERROR(ENOMEM); |
38 | | |
39 | 548 | return 0; |
40 | 548 | } |
41 | | |
42 | | static av_cold int hdr_encode_close(AVCodecContext *avctx) |
43 | 548 | { |
44 | 548 | HDREncContext *s = avctx->priv_data; |
45 | | |
46 | 548 | av_freep(&s->scanline); |
47 | | |
48 | 548 | return 0; |
49 | 548 | } |
50 | | |
51 | | static void bytestream_put_str(uint8_t **buf, const char *const line) |
52 | 18.3k | { |
53 | 18.3k | bytestream_put_buffer(buf, line, strlen(line)); |
54 | 18.3k | } |
55 | | |
56 | | static void float2rgbe(uint8_t *rgbe, float red, float green, float blue) |
57 | 14.2M | { |
58 | 14.2M | float v; |
59 | 14.2M | int e; |
60 | | |
61 | 14.2M | v = FFMAX3(red, green, blue); |
62 | | |
63 | 14.2M | if (v < 1e-32f) { |
64 | 14.0M | rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0; |
65 | 14.0M | } else { |
66 | 186k | v = frexpf(v, &e) * 256.f / v; |
67 | | |
68 | 186k | rgbe[0] = av_clip_uint8(red * v); |
69 | 186k | rgbe[1] = av_clip_uint8(green * v); |
70 | 186k | rgbe[2] = av_clip_uint8(blue * v); |
71 | 186k | rgbe[3] = av_clip_uint8(e + 128); |
72 | 186k | } |
73 | 14.2M | } |
74 | | |
75 | | static void rle(uint8_t **buffer, const uint8_t *data, int width) |
76 | 496k | { |
77 | 4.28M | #define MIN_RLE 4 |
78 | 496k | int cur = 0; |
79 | | |
80 | 1.23M | while (cur < width) { |
81 | 740k | int run_count = 0, old_run_count = 0; |
82 | 740k | int beg_run = cur; |
83 | 740k | uint8_t buf[2]; |
84 | | |
85 | 1.77M | while (run_count < MIN_RLE && beg_run < width) { |
86 | 1.02M | beg_run += run_count; |
87 | 1.02M | old_run_count = run_count; |
88 | 1.02M | run_count = 1; |
89 | 36.1M | while ((beg_run + run_count < width) && (run_count < 127) && |
90 | 35.3M | (data[beg_run * 4] == data[(beg_run + run_count) * 4])) |
91 | 35.0M | run_count++; |
92 | 1.02M | } |
93 | | |
94 | 740k | if ((old_run_count > 1) && (old_run_count == beg_run - cur)) { |
95 | 4.69k | buf[0] = 128 + old_run_count; |
96 | 4.69k | buf[1] = data[cur * 4]; |
97 | 4.69k | bytestream_put_buffer(buffer, buf, sizeof(buf)); |
98 | 4.69k | cur = beg_run; |
99 | 4.69k | } |
100 | | |
101 | 783k | while (cur < beg_run) { |
102 | 43.4k | int nonrun_count = FFMIN(128, beg_run - cur); |
103 | 43.4k | buf[0] = nonrun_count; |
104 | 43.4k | bytestream_put_byte(buffer, buf[0]); |
105 | 358k | for (int n = 0; n < nonrun_count; n++) |
106 | 315k | bytestream_put_byte(buffer, data[(cur + n) * 4]); |
107 | 43.4k | cur += nonrun_count; |
108 | 43.4k | } |
109 | | |
110 | 740k | if (run_count >= MIN_RLE) { |
111 | 704k | buf[0] = 128 + run_count; |
112 | 704k | buf[1] = data[beg_run * 4]; |
113 | 704k | bytestream_put_buffer(buffer, buf, sizeof(buf)); |
114 | 704k | cur += run_count; |
115 | 704k | } |
116 | 740k | } |
117 | 496k | } |
118 | | |
119 | | static int hdr_encode_frame(AVCodecContext *avctx, AVPacket *pkt, |
120 | | const AVFrame *frame, int *got_packet) |
121 | 6.10k | { |
122 | 6.10k | HDREncContext *s = avctx->priv_data; |
123 | 6.10k | int64_t packet_size; |
124 | 6.10k | uint8_t *buf; |
125 | 6.10k | int ret; |
126 | | |
127 | 6.10k | packet_size = avctx->height * 4LL + avctx->width * avctx->height * 8LL + 1024LL; |
128 | 6.10k | if ((ret = ff_get_encode_buffer(avctx, pkt, packet_size, 0)) < 0) |
129 | 0 | return ret; |
130 | | |
131 | 6.10k | buf = pkt->data; |
132 | 6.10k | bytestream_put_str(&buf, "#?RADIANCE\n"); |
133 | 6.10k | bytestream_put_str(&buf, "SOFTWARE=lavc\n"); |
134 | 6.10k | ret = snprintf(buf, 32, "PIXASPECT=%f\n", av_q2d(av_inv_q(avctx->sample_aspect_ratio))); |
135 | 6.10k | if (ret > 0) |
136 | 6.10k | buf += ret; |
137 | 6.10k | bytestream_put_str(&buf, "FORMAT=32-bit_rle_rgbe\n\n"); |
138 | 6.10k | ret = snprintf(buf, 32, "-Y %d +X %d\n", avctx->height, avctx->width); |
139 | 6.10k | if (ret > 0) |
140 | 6.10k | buf += ret; |
141 | | |
142 | 799k | for (int y = 0; y < avctx->height; y++) { |
143 | 793k | const float *red = (const float *)(frame->data[2] + y * frame->linesize[2]); |
144 | 793k | const float *green = (const float *)(frame->data[0] + y * frame->linesize[0]); |
145 | 793k | const float *blue = (const float *)(frame->data[1] + y * frame->linesize[1]); |
146 | | |
147 | 793k | if (avctx->width < 8 || avctx->width > 0x7fff) { |
148 | 5.87M | for (int x = 0; x < avctx->width; x++) { |
149 | 5.20M | float2rgbe(buf, red[x], green[x], blue[x]); |
150 | 5.20M | buf += 4; |
151 | 5.20M | } |
152 | 669k | } else { |
153 | 124k | bytestream_put_byte(&buf, 2); |
154 | 124k | bytestream_put_byte(&buf, 2); |
155 | 124k | bytestream_put_byte(&buf, avctx->width >> 8); |
156 | 124k | bytestream_put_byte(&buf, avctx->width & 0xFF); |
157 | | |
158 | 9.14M | for (int x = 0; x < avctx->width; x++) |
159 | 9.01M | float2rgbe(s->scanline + 4 * x, red[x], green[x], blue[x]); |
160 | 621k | for (int p = 0; p < 4; p++) |
161 | 496k | rle(&buf, s->scanline + p, avctx->width); |
162 | 124k | } |
163 | 793k | } |
164 | | |
165 | 6.10k | pkt->flags |= AV_PKT_FLAG_KEY; |
166 | | |
167 | 6.10k | av_shrink_packet(pkt, buf - pkt->data); |
168 | | |
169 | 6.10k | *got_packet = 1; |
170 | | |
171 | 6.10k | return 0; |
172 | 6.10k | } |
173 | | |
174 | | const FFCodec ff_hdr_encoder = { |
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 | |
181 | | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, |
182 | | .init = hdr_encode_init, |
183 | | FF_CODEC_ENCODE_CB(hdr_encode_frame), |
184 | | .close = hdr_encode_close, |
185 | | CODEC_PIXFMTS(AV_PIX_FMT_GBRPF32), |
186 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
187 | | }; |