2 * Copyright (c) 2021 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
29 #include "libavutil/avassert.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/pixdesc.h"
35 #include "bytestream.h"
53 static const char abgr_chlist[4] = { 'A', 'B', 'G', 'R' };
54 static const char bgr_chlist[4] = { 'B', 'G', 'R', 'A' };
55 static const uint8_t gbra_order[4] = { 3, 1, 0, 2 };
56 static const uint8_t gbr_order[4] = { 1, 0, 2, 0 };
58 typedef struct EXRScanlineData {
59 uint8_t *compressed_data;
60 unsigned int compressed_size;
62 uint8_t *uncompressed_data;
63 unsigned int uncompressed_size;
66 unsigned int tmp_size;
71 typedef struct EXRContext {
80 const uint8_t *ch_order;
83 EXRScanlineData *scanline;
86 static int encode_init(AVCodecContext *avctx)
88 EXRContext *s = avctx->priv_data;
90 switch (avctx->pix_fmt) {
91 case AV_PIX_FMT_GBRPF32:
93 s->ch_names = bgr_chlist;
94 s->ch_order = gbr_order;
96 case AV_PIX_FMT_GBRAPF32:
98 s->ch_names = abgr_chlist;
99 s->ch_order = gbra_order;
105 switch (s->compression) {
109 s->scanline_height = 1;
110 s->nb_scanlines = avctx->height;
113 s->scanline_height = 16;
114 s->nb_scanlines = (avctx->height + s->scanline_height - 1) / s->scanline_height;
120 s->scanline = av_calloc(s->nb_scanlines, sizeof(*s->scanline));
122 return AVERROR(ENOMEM);
127 static int encode_close(AVCodecContext *avctx)
129 EXRContext *s = avctx->priv_data;
131 for (int y = 0; y < s->nb_scanlines && s->scanline; y++) {
132 EXRScanlineData *scanline = &s->scanline[y];
134 av_freep(&scanline->tmp);
135 av_freep(&scanline->compressed_data);
136 av_freep(&scanline->uncompressed_data);
139 av_freep(&s->scanline);
144 static void reorder_pixels(uint8_t *dst, const uint8_t *src, ptrdiff_t size)
146 const ptrdiff_t half_size = (size + 1) / 2;
148 uint8_t *t2 = dst + half_size;
150 for (ptrdiff_t i = 0; i < half_size; i++) {
156 static void predictor(uint8_t *src, ptrdiff_t size)
160 for (ptrdiff_t i = 1; i < size; i++) {
161 int d = src[i] - p + 384;
168 static int64_t rle_compress(uint8_t *out, int64_t out_size,
169 const uint8_t *in, int64_t in_size)
171 int64_t i = 0, o = 0, run = 1, copy = 0;
173 while (i < in_size) {
174 while (i + run < in_size && in[i] == in[i + run] && run < 128)
178 av_assert1(o + 2 <= out_size);
183 if (i + run < in_size)
185 while (i + copy < in_size && copy < 127 && in[i + copy] != in[i + copy - 1])
188 av_assert1(o + 1 + copy <= out_size);
191 for (int x = 0; x < copy; x++)
192 out[o + x] = in[i + x];
205 static int encode_scanline_rle(EXRContext *s, const AVFrame *frame)
207 for (int y = 0; y < frame->height; y++) {
208 EXRScanlineData *scanline = &s->scanline[y];
209 int64_t tmp_size = 4LL * s->planes * frame->width;
210 int64_t max_compressed_size = tmp_size * 3 / 2;
212 av_fast_padded_malloc(&scanline->uncompressed_data, &scanline->uncompressed_size, tmp_size);
213 if (!scanline->uncompressed_data)
214 return AVERROR(ENOMEM);
216 av_fast_padded_malloc(&scanline->tmp, &scanline->tmp_size, tmp_size);
218 return AVERROR(ENOMEM);
220 av_fast_padded_malloc(&scanline->compressed_data, &scanline->compressed_size, max_compressed_size);
221 if (!scanline->compressed_data)
222 return AVERROR(ENOMEM);
224 for (int p = 0; p < s->planes; p++) {
225 int ch = s->ch_order[p];
227 memcpy(scanline->uncompressed_data + frame->width * 4 * p,
228 frame->data[ch] + y * frame->linesize[ch], frame->width * 4);
231 reorder_pixels(scanline->tmp, scanline->uncompressed_data, tmp_size);
232 predictor(scanline->tmp, tmp_size);
233 scanline->actual_size = rle_compress(scanline->compressed_data,
235 scanline->tmp, tmp_size);
237 if (scanline->actual_size >= tmp_size) {
238 FFSWAP(uint8_t *, scanline->uncompressed_data, scanline->compressed_data);
239 FFSWAP(int, scanline->uncompressed_size, scanline->compressed_size);
240 scanline->actual_size = tmp_size;
247 static int encode_scanline_zip(EXRContext *s, const AVFrame *frame)
249 for (int y = 0; y < s->nb_scanlines; y++) {
250 EXRScanlineData *scanline = &s->scanline[y];
251 const int scanline_height = FFMIN(s->scanline_height, frame->height - y * s->scanline_height);
252 int64_t tmp_size = 4LL * s->planes * frame->width * scanline_height;
253 int64_t max_compressed_size = tmp_size * 3 / 2;
254 unsigned long actual_size, source_size;
256 av_fast_padded_malloc(&scanline->uncompressed_data, &scanline->uncompressed_size, tmp_size);
257 if (!scanline->uncompressed_data)
258 return AVERROR(ENOMEM);
260 av_fast_padded_malloc(&scanline->tmp, &scanline->tmp_size, tmp_size);
262 return AVERROR(ENOMEM);
264 av_fast_padded_malloc(&scanline->compressed_data, &scanline->compressed_size, max_compressed_size);
265 if (!scanline->compressed_data)
266 return AVERROR(ENOMEM);
268 for (int l = 0; l < scanline_height; l++) {
269 const int scanline_size = frame->width * 4 * s->planes;
271 for (int p = 0; p < s->planes; p++) {
272 int ch = s->ch_order[p];
274 memcpy(scanline->uncompressed_data + scanline_size * l + p * frame->width * 4,
275 frame->data[ch] + (y * s->scanline_height + l) * frame->linesize[ch],
280 reorder_pixels(scanline->tmp, scanline->uncompressed_data, tmp_size);
281 predictor(scanline->tmp, tmp_size);
282 source_size = tmp_size;
283 actual_size = max_compressed_size;
284 compress(scanline->compressed_data, &actual_size,
285 scanline->tmp, source_size);
287 scanline->actual_size = actual_size;
288 if (scanline->actual_size >= tmp_size) {
289 FFSWAP(uint8_t *, scanline->uncompressed_data, scanline->compressed_data);
290 FFSWAP(int, scanline->uncompressed_size, scanline->compressed_size);
291 scanline->actual_size = tmp_size;
298 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
299 const AVFrame *frame, int *got_packet)
301 EXRContext *s = avctx->priv_data;
302 PutByteContext *pb = &s->pb;
305 int64_t out_size = 2048LL + avctx->height * 16LL +
306 av_image_get_buffer_size(avctx->pix_fmt,
308 avctx->height, 64) * 3LL / 2;
310 if ((ret = ff_alloc_packet2(avctx, pkt, out_size, out_size)) < 0)
313 bytestream2_init_writer(pb, pkt->data, pkt->size);
315 bytestream2_put_le32(pb, 20000630);
316 bytestream2_put_byte(pb, 2);
317 bytestream2_put_le24(pb, 0);
318 bytestream2_put_buffer(pb, "channels\0chlist\0", 16);
319 bytestream2_put_le32(pb, s->planes * 18 + 1);
321 for (int p = 0; p < s->planes; p++) {
322 bytestream2_put_byte(pb, s->ch_names[p]);
323 bytestream2_put_byte(pb, 0);
324 bytestream2_put_le32(pb, EXR_FLOAT);
325 bytestream2_put_le32(pb, 0);
326 bytestream2_put_le32(pb, 1);
327 bytestream2_put_le32(pb, 1);
329 bytestream2_put_byte(pb, 0);
331 bytestream2_put_buffer(pb, "compression\0compression\0", 24);
332 bytestream2_put_le32(pb, 1);
333 bytestream2_put_byte(pb, s->compression);
335 bytestream2_put_buffer(pb, "dataWindow\0box2i\0", 17);
336 bytestream2_put_le32(pb, 16);
337 bytestream2_put_le32(pb, 0);
338 bytestream2_put_le32(pb, 0);
339 bytestream2_put_le32(pb, avctx->width - 1);
340 bytestream2_put_le32(pb, avctx->height - 1);
342 bytestream2_put_buffer(pb, "displayWindow\0box2i\0", 20);
343 bytestream2_put_le32(pb, 16);
344 bytestream2_put_le32(pb, 0);
345 bytestream2_put_le32(pb, 0);
346 bytestream2_put_le32(pb, avctx->width - 1);
347 bytestream2_put_le32(pb, avctx->height - 1);
349 bytestream2_put_buffer(pb, "lineOrder\0lineOrder\0", 20);
350 bytestream2_put_le32(pb, 1);
351 bytestream2_put_byte(pb, 0);
353 bytestream2_put_buffer(pb, "screenWindowCenter\0v2f\0", 23);
354 bytestream2_put_le32(pb, 8);
355 bytestream2_put_le64(pb, 0);
357 bytestream2_put_buffer(pb, "screenWindowWidth\0float\0", 24);
358 bytestream2_put_le32(pb, 4);
359 bytestream2_put_le32(pb, av_float2int(1.f));
361 if (avctx->sample_aspect_ratio.num && avctx->sample_aspect_ratio.den) {
362 bytestream2_put_buffer(pb, "pixelAspectRatio\0float\0", 23);
363 bytestream2_put_le32(pb, 4);
364 bytestream2_put_le32(pb, av_float2int(av_q2d(avctx->sample_aspect_ratio)));
367 if (avctx->framerate.num && avctx->framerate.den) {
368 bytestream2_put_buffer(pb, "framesPerSecond\0rational\0", 25);
369 bytestream2_put_le32(pb, 8);
370 bytestream2_put_le32(pb, avctx->framerate.num);
371 bytestream2_put_le32(pb, avctx->framerate.den);
374 bytestream2_put_buffer(pb, "gamma\0float\0", 12);
375 bytestream2_put_le32(pb, 4);
376 bytestream2_put_le32(pb, av_float2int(s->gamma));
378 bytestream2_put_buffer(pb, "writer\0string\0", 14);
379 bytestream2_put_le32(pb, 4);
380 bytestream2_put_buffer(pb, "lavc", 4);
381 bytestream2_put_byte(pb, 0);
383 switch (s->compression) {
388 encode_scanline_rle(s, frame);
392 encode_scanline_zip(s, frame);
398 switch (s->compression) {
400 offset = bytestream2_tell_p(pb) + avctx->height * 8LL;
402 for (int y = 0; y < avctx->height; y++) {
403 bytestream2_put_le64(pb, offset);
404 offset += avctx->width * s->planes * 4 + 8;
407 for (int y = 0; y < avctx->height; y++) {
408 bytestream2_put_le32(pb, y);
409 bytestream2_put_le32(pb, s->planes * avctx->width * 4);
410 for (int p = 0; p < s->planes; p++) {
411 int ch = s->ch_order[p];
412 bytestream2_put_buffer(pb, frame->data[ch] + y * frame->linesize[ch],
420 offset = bytestream2_tell_p(pb) + s->nb_scanlines * 8LL;
422 for (int y = 0; y < s->nb_scanlines; y++) {
423 EXRScanlineData *scanline = &s->scanline[y];
425 bytestream2_put_le64(pb, offset);
426 offset += scanline->actual_size + 8;
429 for (int y = 0; y < s->nb_scanlines; y++) {
430 EXRScanlineData *scanline = &s->scanline[y];
432 bytestream2_put_le32(pb, y * s->scanline_height);
433 bytestream2_put_le32(pb, scanline->actual_size);
434 bytestream2_put_buffer(pb, scanline->compressed_data,
435 scanline->actual_size);
442 av_shrink_packet(pkt, bytestream2_tell_p(pb));
444 pkt->flags |= AV_PKT_FLAG_KEY;
450 #define OFFSET(x) offsetof(EXRContext, x)
451 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
452 static const AVOption options[] = {
453 { "compression", "set compression type", OFFSET(compression), AV_OPT_TYPE_INT, {.i64=0}, 0, EXR_NBCOMPR-1, VE, "compr" },
454 { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64=EXR_RAW}, 0, 0, VE, "compr" },
455 { "rle" , "RLE", 0, AV_OPT_TYPE_CONST, {.i64=EXR_RLE}, 0, 0, VE, "compr" },
456 { "zip1", "ZIP1", 0, AV_OPT_TYPE_CONST, {.i64=EXR_ZIP1}, 0, 0, VE, "compr" },
457 { "zip16", "ZIP16", 0, AV_OPT_TYPE_CONST, {.i64=EXR_ZIP16}, 0, 0, VE, "compr" },
458 { "gamma", "set gamma", OFFSET(gamma), AV_OPT_TYPE_FLOAT, {.dbl=1.f}, 0.001, FLT_MAX, VE },
462 static const AVClass exr_class = {
464 .item_name = av_default_item_name,
466 .version = LIBAVUTIL_VERSION_INT,
469 AVCodec ff_exr_encoder = {
471 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
472 .priv_data_size = sizeof(EXRContext),
473 .priv_class = &exr_class,
474 .type = AVMEDIA_TYPE_VIDEO,
475 .id = AV_CODEC_ID_EXR,
477 .encode2 = encode_frame,
478 .close = encode_close,
479 .capabilities = AV_CODEC_CAP_FRAME_THREADS,
480 .pix_fmts = (const enum AVPixelFormat[]) {