You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

553 lines
18KB

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