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.

482 lines
15KB

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