FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/exrenc.c
Date: 2021-09-23 20:34:37
Exec Total Coverage
Lines: 0 283 0.0%
Branches: 0 127 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2021 Paul B Mahol
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 /**
22 * @file
23 * OpenEXR encoder
24 */
25
26 #include <float.h>
27 #include <zlib.h>
28
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"
34 #include "avcodec.h"
35 #include "bytestream.h"
36 #include "encode.h"
37 #include "internal.h"
38 #include "float2half.h"
39
40 enum ExrCompr {
41 EXR_RAW,
42 EXR_RLE,
43 EXR_ZIP1,
44 EXR_ZIP16,
45 EXR_NBCOMPR,
46 };
47
48 enum ExrPixelType {
49 EXR_UINT,
50 EXR_HALF,
51 EXR_FLOAT,
52 EXR_UNKNOWN,
53 };
54
55 static const char abgr_chlist[4] = { 'A', 'B', 'G', 'R' };
56 static const char bgr_chlist[4] = { 'B', 'G', 'R', 'A' };
57 static const uint8_t gbra_order[4] = { 3, 1, 0, 2 };
58 static const uint8_t gbr_order[4] = { 1, 0, 2, 0 };
59
60 typedef struct EXRScanlineData {
61 uint8_t *compressed_data;
62 unsigned int compressed_size;
63
64 uint8_t *uncompressed_data;
65 unsigned int uncompressed_size;
66
67 uint8_t *tmp;
68 unsigned int tmp_size;
69
70 int64_t actual_size;
71 } EXRScanlineData;
72
73 typedef struct EXRContext {
74 const AVClass *class;
75
76 int compression;
77 int pixel_type;
78 int planes;
79 int nb_scanlines;
80 int scanline_height;
81 float gamma;
82 const char *ch_names;
83 const uint8_t *ch_order;
84 PutByteContext pb;
85
86 EXRScanlineData *scanline;
87
88 uint16_t basetable[512];
89 uint8_t shifttable[512];
90 } EXRContext;
91
92 static int encode_init(AVCodecContext *avctx)
93 {
94 EXRContext *s = avctx->priv_data;
95
96 float2half_tables(s->basetable, s->shifttable);
97
98 switch (avctx->pix_fmt) {
99 case AV_PIX_FMT_GBRPF32:
100 s->planes = 3;
101 s->ch_names = bgr_chlist;
102 s->ch_order = gbr_order;
103 break;
104 case AV_PIX_FMT_GBRAPF32:
105 s->planes = 4;
106 s->ch_names = abgr_chlist;
107 s->ch_order = gbra_order;
108 break;
109 default:
110 av_assert0(0);
111 }
112
113 switch (s->compression) {
114 case EXR_RAW:
115 case EXR_RLE:
116 case EXR_ZIP1:
117 s->scanline_height = 1;
118 s->nb_scanlines = avctx->height;
119 break;
120 case EXR_ZIP16:
121 s->scanline_height = 16;
122 s->nb_scanlines = (avctx->height + s->scanline_height - 1) / s->scanline_height;
123 break;
124 default:
125 av_assert0(0);
126 }
127
128 s->scanline = av_calloc(s->nb_scanlines, sizeof(*s->scanline));
129 if (!s->scanline)
130 return AVERROR(ENOMEM);
131
132 return 0;
133 }
134
135 static int encode_close(AVCodecContext *avctx)
136 {
137 EXRContext *s = avctx->priv_data;
138
139 for (int y = 0; y < s->nb_scanlines && s->scanline; y++) {
140 EXRScanlineData *scanline = &s->scanline[y];
141
142 av_freep(&scanline->tmp);
143 av_freep(&scanline->compressed_data);
144 av_freep(&scanline->uncompressed_data);
145 }
146
147 av_freep(&s->scanline);
148
149 return 0;
150 }
151
152 static void reorder_pixels(uint8_t *dst, const uint8_t *src, ptrdiff_t size)
153 {
154 const ptrdiff_t half_size = (size + 1) / 2;
155 uint8_t *t1 = dst;
156 uint8_t *t2 = dst + half_size;
157
158 for (ptrdiff_t i = 0; i < half_size; i++) {
159 t1[i] = *(src++);
160 t2[i] = *(src++);
161 }
162 }
163
164 static void predictor(uint8_t *src, ptrdiff_t size)
165 {
166 int p = src[0];
167
168 for (ptrdiff_t i = 1; i < size; i++) {
169 int d = src[i] - p + 384;
170
171 p = src[i];
172 src[i] = d;
173 }
174 }
175
176 static int64_t rle_compress(uint8_t *out, int64_t out_size,
177 const uint8_t *in, int64_t in_size)
178 {
179 int64_t i = 0, o = 0, run = 1, copy = 0;
180
181 while (i < in_size) {
182 while (i + run < in_size && in[i] == in[i + run] && run < 128)
183 run++;
184
185 if (run >= 3) {
186 if (o + 2 >= out_size)
187 return -1;
188 out[o++] = run - 1;
189 out[o++] = in[i];
190 i += run;
191 } else {
192 if (i + run < in_size)
193 copy += run;
194 while (i + copy < in_size && copy < 127 && in[i + copy] != in[i + copy - 1])
195 copy++;
196
197 if (o + 1 + copy >= out_size)
198 return -1;
199 out[o++] = -copy;
200
201 for (int x = 0; x < copy; x++)
202 out[o + x] = in[i + x];
203
204 o += copy;
205 i += copy;
206 copy = 0;
207 }
208
209 run = 1;
210 }
211
212 return o;
213 }
214
215 static int encode_scanline_rle(EXRContext *s, const AVFrame *frame)
216 {
217 const int64_t element_size = s->pixel_type == EXR_HALF ? 2LL : 4LL;
218
219 for (int y = 0; y < frame->height; y++) {
220 EXRScanlineData *scanline = &s->scanline[y];
221 int64_t tmp_size = element_size * s->planes * frame->width;
222 int64_t max_compressed_size = tmp_size * 3 / 2;
223
224 av_fast_padded_malloc(&scanline->uncompressed_data, &scanline->uncompressed_size, tmp_size);
225 if (!scanline->uncompressed_data)
226 return AVERROR(ENOMEM);
227
228 av_fast_padded_malloc(&scanline->tmp, &scanline->tmp_size, tmp_size);
229 if (!scanline->tmp)
230 return AVERROR(ENOMEM);
231
232 av_fast_padded_malloc(&scanline->compressed_data, &scanline->compressed_size, max_compressed_size);
233 if (!scanline->compressed_data)
234 return AVERROR(ENOMEM);
235
236 switch (s->pixel_type) {
237 case EXR_FLOAT:
238 for (int p = 0; p < s->planes; p++) {
239 int ch = s->ch_order[p];
240
241 memcpy(scanline->uncompressed_data + frame->width * 4 * p,
242 frame->data[ch] + y * frame->linesize[ch], frame->width * 4);
243 }
244 break;
245 case EXR_HALF:
246 for (int p = 0; p < s->planes; p++) {
247 int ch = s->ch_order[p];
248 uint16_t *dst = (uint16_t *)(scanline->uncompressed_data + frame->width * 2 * p);
249 uint32_t *src = (uint32_t *)(frame->data[ch] + y * frame->linesize[ch]);
250
251 for (int x = 0; x < frame->width; x++)
252 dst[x] = float2half(src[x], s->basetable, s->shifttable);
253 }
254 break;
255 }
256
257 reorder_pixels(scanline->tmp, scanline->uncompressed_data, tmp_size);
258 predictor(scanline->tmp, tmp_size);
259 scanline->actual_size = rle_compress(scanline->compressed_data,
260 max_compressed_size,
261 scanline->tmp, tmp_size);
262
263 if (scanline->actual_size <= 0 || scanline->actual_size >= tmp_size) {
264 FFSWAP(uint8_t *, scanline->uncompressed_data, scanline->compressed_data);
265 FFSWAP(int, scanline->uncompressed_size, scanline->compressed_size);
266 scanline->actual_size = tmp_size;
267 }
268 }
269
270 return 0;
271 }
272
273 static int encode_scanline_zip(EXRContext *s, const AVFrame *frame)
274 {
275 const int64_t element_size = s->pixel_type == EXR_HALF ? 2LL : 4LL;
276
277 for (int y = 0; y < s->nb_scanlines; y++) {
278 EXRScanlineData *scanline = &s->scanline[y];
279 const int scanline_height = FFMIN(s->scanline_height, frame->height - y * s->scanline_height);
280 int64_t tmp_size = element_size * s->planes * frame->width * scanline_height;
281 int64_t max_compressed_size = tmp_size * 3 / 2;
282 unsigned long actual_size, source_size;
283
284 av_fast_padded_malloc(&scanline->uncompressed_data, &scanline->uncompressed_size, tmp_size);
285 if (!scanline->uncompressed_data)
286 return AVERROR(ENOMEM);
287
288 av_fast_padded_malloc(&scanline->tmp, &scanline->tmp_size, tmp_size);
289 if (!scanline->tmp)
290 return AVERROR(ENOMEM);
291
292 av_fast_padded_malloc(&scanline->compressed_data, &scanline->compressed_size, max_compressed_size);
293 if (!scanline->compressed_data)
294 return AVERROR(ENOMEM);
295
296 switch (s->pixel_type) {
297 case EXR_FLOAT:
298 for (int l = 0; l < scanline_height; l++) {
299 const int scanline_size = frame->width * 4 * s->planes;
300
301 for (int p = 0; p < s->planes; p++) {
302 int ch = s->ch_order[p];
303
304 memcpy(scanline->uncompressed_data + scanline_size * l + p * frame->width * 4,
305 frame->data[ch] + (y * s->scanline_height + l) * frame->linesize[ch],
306 frame->width * 4);
307 }
308 }
309 break;
310 case EXR_HALF:
311 for (int l = 0; l < scanline_height; l++) {
312 const int scanline_size = frame->width * 2 * s->planes;
313
314 for (int p = 0; p < s->planes; p++) {
315 int ch = s->ch_order[p];
316 uint16_t *dst = (uint16_t *)(scanline->uncompressed_data + scanline_size * l + p * frame->width * 2);
317 uint32_t *src = (uint32_t *)(frame->data[ch] + (y * s->scanline_height + l) * frame->linesize[ch]);
318
319 for (int x = 0; x < frame->width; x++)
320 dst[x] = float2half(src[x], s->basetable, s->shifttable);
321 }
322 }
323 break;
324 }
325
326 reorder_pixels(scanline->tmp, scanline->uncompressed_data, tmp_size);
327 predictor(scanline->tmp, tmp_size);
328 source_size = tmp_size;
329 actual_size = max_compressed_size;
330 compress(scanline->compressed_data, &actual_size,
331 scanline->tmp, source_size);
332
333 scanline->actual_size = actual_size;
334 if (scanline->actual_size >= tmp_size) {
335 FFSWAP(uint8_t *, scanline->uncompressed_data, scanline->compressed_data);
336 FFSWAP(int, scanline->uncompressed_size, scanline->compressed_size);
337 scanline->actual_size = tmp_size;
338 }
339 }
340
341 return 0;
342 }
343
344 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
345 const AVFrame *frame, int *got_packet)
346 {
347 EXRContext *s = avctx->priv_data;
348 PutByteContext *pb = &s->pb;
349 int64_t offset;
350 int ret;
351 int64_t out_size = 2048LL + avctx->height * 16LL +
352 av_image_get_buffer_size(avctx->pix_fmt,
353 avctx->width,
354 avctx->height, 64) * 3LL / 2;
355
356 if ((ret = ff_get_encode_buffer(avctx, pkt, out_size, 0)) < 0)
357 return ret;
358
359 bytestream2_init_writer(pb, pkt->data, pkt->size);
360
361 bytestream2_put_le32(pb, 20000630);
362 bytestream2_put_byte(pb, 2);
363 bytestream2_put_le24(pb, 0);
364 bytestream2_put_buffer(pb, "channels\0chlist\0", 16);
365 bytestream2_put_le32(pb, s->planes * 18 + 1);
366
367 for (int p = 0; p < s->planes; p++) {
368 bytestream2_put_byte(pb, s->ch_names[p]);
369 bytestream2_put_byte(pb, 0);
370 bytestream2_put_le32(pb, s->pixel_type);
371 bytestream2_put_le32(pb, 0);
372 bytestream2_put_le32(pb, 1);
373 bytestream2_put_le32(pb, 1);
374 }
375 bytestream2_put_byte(pb, 0);
376
377 bytestream2_put_buffer(pb, "compression\0compression\0", 24);
378 bytestream2_put_le32(pb, 1);
379 bytestream2_put_byte(pb, s->compression);
380
381 bytestream2_put_buffer(pb, "dataWindow\0box2i\0", 17);
382 bytestream2_put_le32(pb, 16);
383 bytestream2_put_le32(pb, 0);
384 bytestream2_put_le32(pb, 0);
385 bytestream2_put_le32(pb, avctx->width - 1);
386 bytestream2_put_le32(pb, avctx->height - 1);
387
388 bytestream2_put_buffer(pb, "displayWindow\0box2i\0", 20);
389 bytestream2_put_le32(pb, 16);
390 bytestream2_put_le32(pb, 0);
391 bytestream2_put_le32(pb, 0);
392 bytestream2_put_le32(pb, avctx->width - 1);
393 bytestream2_put_le32(pb, avctx->height - 1);
394
395 bytestream2_put_buffer(pb, "lineOrder\0lineOrder\0", 20);
396 bytestream2_put_le32(pb, 1);
397 bytestream2_put_byte(pb, 0);
398
399 bytestream2_put_buffer(pb, "screenWindowCenter\0v2f\0", 23);
400 bytestream2_put_le32(pb, 8);
401 bytestream2_put_le64(pb, 0);
402
403 bytestream2_put_buffer(pb, "screenWindowWidth\0float\0", 24);
404 bytestream2_put_le32(pb, 4);
405 bytestream2_put_le32(pb, av_float2int(1.f));
406
407 if (avctx->sample_aspect_ratio.num && avctx->sample_aspect_ratio.den) {
408 bytestream2_put_buffer(pb, "pixelAspectRatio\0float\0", 23);
409 bytestream2_put_le32(pb, 4);
410 bytestream2_put_le32(pb, av_float2int(av_q2d(avctx->sample_aspect_ratio)));
411 }
412
413 if (avctx->framerate.num && avctx->framerate.den) {
414 bytestream2_put_buffer(pb, "framesPerSecond\0rational\0", 25);
415 bytestream2_put_le32(pb, 8);
416 bytestream2_put_le32(pb, avctx->framerate.num);
417 bytestream2_put_le32(pb, avctx->framerate.den);
418 }
419
420 bytestream2_put_buffer(pb, "gamma\0float\0", 12);
421 bytestream2_put_le32(pb, 4);
422 bytestream2_put_le32(pb, av_float2int(s->gamma));
423
424 bytestream2_put_buffer(pb, "writer\0string\0", 14);
425 bytestream2_put_le32(pb, 4);
426 bytestream2_put_buffer(pb, "lavc", 4);
427 bytestream2_put_byte(pb, 0);
428
429 switch (s->compression) {
430 case EXR_RAW:
431 /* nothing to do */
432 break;
433 case EXR_RLE:
434 encode_scanline_rle(s, frame);
435 break;
436 case EXR_ZIP16:
437 case EXR_ZIP1:
438 encode_scanline_zip(s, frame);
439 break;
440 default:
441 av_assert0(0);
442 }
443
444 switch (s->compression) {
445 case EXR_RAW:
446 offset = bytestream2_tell_p(pb) + avctx->height * 8LL;
447
448 if (s->pixel_type == EXR_FLOAT) {
449
450 for (int y = 0; y < avctx->height; y++) {
451 bytestream2_put_le64(pb, offset);
452 offset += avctx->width * s->planes * 4 + 8;
453 }
454
455 for (int y = 0; y < avctx->height; y++) {
456 bytestream2_put_le32(pb, y);
457 bytestream2_put_le32(pb, s->planes * avctx->width * 4);
458 for (int p = 0; p < s->planes; p++) {
459 int ch = s->ch_order[p];
460 bytestream2_put_buffer(pb, frame->data[ch] + y * frame->linesize[ch],
461 avctx->width * 4);
462 }
463 }
464 } else {
465 for (int y = 0; y < avctx->height; y++) {
466 bytestream2_put_le64(pb, offset);
467 offset += avctx->width * s->planes * 2 + 8;
468 }
469
470 for (int y = 0; y < avctx->height; y++) {
471 bytestream2_put_le32(pb, y);
472 bytestream2_put_le32(pb, s->planes * avctx->width * 2);
473 for (int p = 0; p < s->planes; p++) {
474 int ch = s->ch_order[p];
475 uint32_t *src = (uint32_t *)(frame->data[ch] + y * frame->linesize[ch]);
476
477 for (int x = 0; x < frame->width; x++)
478 bytestream2_put_le16(pb, float2half(src[x], s->basetable, s->shifttable));
479 }
480 }
481 }
482 break;
483 case EXR_ZIP16:
484 case EXR_ZIP1:
485 case EXR_RLE:
486 offset = bytestream2_tell_p(pb) + s->nb_scanlines * 8LL;
487
488 for (int y = 0; y < s->nb_scanlines; y++) {
489 EXRScanlineData *scanline = &s->scanline[y];
490
491 bytestream2_put_le64(pb, offset);
492 offset += scanline->actual_size + 8;
493 }
494
495 for (int y = 0; y < s->nb_scanlines; y++) {
496 EXRScanlineData *scanline = &s->scanline[y];
497
498 bytestream2_put_le32(pb, y * s->scanline_height);
499 bytestream2_put_le32(pb, scanline->actual_size);
500 bytestream2_put_buffer(pb, scanline->compressed_data,
501 scanline->actual_size);
502 }
503 break;
504 default:
505 av_assert0(0);
506 }
507
508 av_shrink_packet(pkt, bytestream2_tell_p(pb));
509
510 pkt->flags |= AV_PKT_FLAG_KEY;
511 *got_packet = 1;
512
513 return 0;
514 }
515
516 #define OFFSET(x) offsetof(EXRContext, x)
517 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
518 static const AVOption options[] = {
519 { "compression", "set compression type", OFFSET(compression), AV_OPT_TYPE_INT, {.i64=0}, 0, EXR_NBCOMPR-1, VE, "compr" },
520 { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64=EXR_RAW}, 0, 0, VE, "compr" },
521 { "rle" , "RLE", 0, AV_OPT_TYPE_CONST, {.i64=EXR_RLE}, 0, 0, VE, "compr" },
522 { "zip1", "ZIP1", 0, AV_OPT_TYPE_CONST, {.i64=EXR_ZIP1}, 0, 0, VE, "compr" },
523 { "zip16", "ZIP16", 0, AV_OPT_TYPE_CONST, {.i64=EXR_ZIP16}, 0, 0, VE, "compr" },
524 { "format", "set pixel type", OFFSET(pixel_type), AV_OPT_TYPE_INT, {.i64=EXR_FLOAT}, EXR_HALF, EXR_UNKNOWN-1, VE, "pixel" },
525 { "half" , NULL, 0, AV_OPT_TYPE_CONST, {.i64=EXR_HALF}, 0, 0, VE, "pixel" },
526 { "float", NULL, 0, AV_OPT_TYPE_CONST, {.i64=EXR_FLOAT}, 0, 0, VE, "pixel" },
527 { "gamma", "set gamma", OFFSET(gamma), AV_OPT_TYPE_FLOAT, {.dbl=1.f}, 0.001, FLT_MAX, VE },
528 { NULL},
529 };
530
531 static const AVClass exr_class = {
532 .class_name = "exr",
533 .item_name = av_default_item_name,
534 .option = options,
535 .version = LIBAVUTIL_VERSION_INT,
536 };
537
538 const AVCodec ff_exr_encoder = {
539 .name = "exr",
540 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
541 .priv_data_size = sizeof(EXRContext),
542 .priv_class = &exr_class,
543 .type = AVMEDIA_TYPE_VIDEO,
544 .id = AV_CODEC_ID_EXR,
545 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
546 .init = encode_init,
547 .encode2 = encode_frame,
548 .close = encode_close,
549 .pix_fmts = (const enum AVPixelFormat[]) {
550 AV_PIX_FMT_GBRPF32,
551 AV_PIX_FMT_GBRAPF32,
552 AV_PIX_FMT_NONE },
553 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
554 };
555