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