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.

674 lines
24KB

  1. /*
  2. * JPEG 2000 encoding support via OpenJPEG
  3. * Copyright (c) 2011 Michael Bradshaw <mjbshaw gmail com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * JPEG 2000 encoder using libopenjpeg
  24. */
  25. #define OPJ_STATIC
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "libavutil/opt.h"
  31. #include "avcodec.h"
  32. #include "internal.h"
  33. #if HAVE_OPENJPEG_1_5_OPENJPEG_H
  34. # include <openjpeg-1.5/openjpeg.h>
  35. #else
  36. # include <openjpeg.h>
  37. #endif
  38. typedef struct LibOpenJPEGContext {
  39. AVClass *avclass;
  40. opj_image_t *image;
  41. opj_cparameters_t enc_params;
  42. opj_event_mgr_t event_mgr;
  43. int format;
  44. int profile;
  45. int prog_order;
  46. int cinema_mode;
  47. int numresolution;
  48. int numlayers;
  49. int disto_alloc;
  50. int fixed_alloc;
  51. int fixed_quality;
  52. } LibOpenJPEGContext;
  53. static void error_callback(const char *msg, void *data)
  54. {
  55. av_log(data, AV_LOG_ERROR, "%s\n", msg);
  56. }
  57. static void warning_callback(const char *msg, void *data)
  58. {
  59. av_log(data, AV_LOG_WARNING, "%s\n", msg);
  60. }
  61. static void info_callback(const char *msg, void *data)
  62. {
  63. av_log(data, AV_LOG_DEBUG, "%s\n", msg);
  64. }
  65. static void cinema_parameters(opj_cparameters_t *p)
  66. {
  67. p->tile_size_on = 0;
  68. p->cp_tdx = 1;
  69. p->cp_tdy = 1;
  70. /* Tile part */
  71. p->tp_flag = 'C';
  72. p->tp_on = 1;
  73. /* Tile and Image shall be at (0, 0) */
  74. p->cp_tx0 = 0;
  75. p->cp_ty0 = 0;
  76. p->image_offset_x0 = 0;
  77. p->image_offset_y0 = 0;
  78. /* Codeblock size= 32 * 32 */
  79. p->cblockw_init = 32;
  80. p->cblockh_init = 32;
  81. p->csty |= 0x01;
  82. /* The progression order shall be CPRL */
  83. p->prog_order = CPRL;
  84. /* No ROI */
  85. p->roi_compno = -1;
  86. /* No subsampling */
  87. p->subsampling_dx = 1;
  88. p->subsampling_dy = 1;
  89. /* 9-7 transform */
  90. p->irreversible = 1;
  91. p->tcp_mct = 1;
  92. }
  93. static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
  94. {
  95. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  96. opj_image_cmptparm_t cmptparm[4] = {{0}};
  97. opj_image_t *img;
  98. int i;
  99. int sub_dx[4];
  100. int sub_dy[4];
  101. int numcomps;
  102. OPJ_COLOR_SPACE color_space = CLRSPC_UNKNOWN;
  103. sub_dx[0] = sub_dx[3] = 1;
  104. sub_dy[0] = sub_dy[3] = 1;
  105. sub_dx[1] = sub_dx[2] = 1 << desc->log2_chroma_w;
  106. sub_dy[1] = sub_dy[2] = 1 << desc->log2_chroma_h;
  107. numcomps = desc->nb_components;
  108. switch (avctx->pix_fmt) {
  109. case AV_PIX_FMT_GRAY8:
  110. case AV_PIX_FMT_YA8:
  111. case AV_PIX_FMT_GRAY16:
  112. case AV_PIX_FMT_YA16:
  113. color_space = CLRSPC_GRAY;
  114. break;
  115. case AV_PIX_FMT_RGB24:
  116. case AV_PIX_FMT_RGBA:
  117. case AV_PIX_FMT_RGB48:
  118. case AV_PIX_FMT_RGBA64:
  119. case AV_PIX_FMT_GBR24P:
  120. case AV_PIX_FMT_GBRP9:
  121. case AV_PIX_FMT_GBRP10:
  122. case AV_PIX_FMT_GBRP12:
  123. case AV_PIX_FMT_GBRP14:
  124. case AV_PIX_FMT_GBRP16:
  125. case AV_PIX_FMT_XYZ12:
  126. color_space = CLRSPC_SRGB;
  127. break;
  128. case AV_PIX_FMT_YUV410P:
  129. case AV_PIX_FMT_YUV411P:
  130. case AV_PIX_FMT_YUV420P:
  131. case AV_PIX_FMT_YUV422P:
  132. case AV_PIX_FMT_YUV440P:
  133. case AV_PIX_FMT_YUV444P:
  134. case AV_PIX_FMT_YUVA420P:
  135. case AV_PIX_FMT_YUVA422P:
  136. case AV_PIX_FMT_YUVA444P:
  137. case AV_PIX_FMT_YUV420P9:
  138. case AV_PIX_FMT_YUV422P9:
  139. case AV_PIX_FMT_YUV444P9:
  140. case AV_PIX_FMT_YUVA420P9:
  141. case AV_PIX_FMT_YUVA422P9:
  142. case AV_PIX_FMT_YUVA444P9:
  143. case AV_PIX_FMT_YUV420P10:
  144. case AV_PIX_FMT_YUV422P10:
  145. case AV_PIX_FMT_YUV444P10:
  146. case AV_PIX_FMT_YUVA420P10:
  147. case AV_PIX_FMT_YUVA422P10:
  148. case AV_PIX_FMT_YUVA444P10:
  149. case AV_PIX_FMT_YUV420P12:
  150. case AV_PIX_FMT_YUV422P12:
  151. case AV_PIX_FMT_YUV444P12:
  152. case AV_PIX_FMT_YUV420P14:
  153. case AV_PIX_FMT_YUV422P14:
  154. case AV_PIX_FMT_YUV444P14:
  155. case AV_PIX_FMT_YUV420P16:
  156. case AV_PIX_FMT_YUV422P16:
  157. case AV_PIX_FMT_YUV444P16:
  158. case AV_PIX_FMT_YUVA420P16:
  159. case AV_PIX_FMT_YUVA422P16:
  160. case AV_PIX_FMT_YUVA444P16:
  161. color_space = CLRSPC_SYCC;
  162. break;
  163. default:
  164. av_log(avctx, AV_LOG_ERROR,
  165. "The requested pixel format '%s' is not supported\n",
  166. av_get_pix_fmt_name(avctx->pix_fmt));
  167. return NULL;
  168. }
  169. for (i = 0; i < numcomps; i++) {
  170. cmptparm[i].prec = desc->comp[i].depth_minus1 + 1;
  171. cmptparm[i].bpp = desc->comp[i].depth_minus1 + 1;
  172. cmptparm[i].sgnd = 0;
  173. cmptparm[i].dx = sub_dx[i];
  174. cmptparm[i].dy = sub_dy[i];
  175. cmptparm[i].w = (avctx->width + sub_dx[i] - 1) / sub_dx[i];
  176. cmptparm[i].h = (avctx->height + sub_dy[i] - 1) / sub_dy[i];
  177. }
  178. img = opj_image_create(numcomps, cmptparm, color_space);
  179. if (!img)
  180. return NULL;
  181. // x0, y0 is the top left corner of the image
  182. // x1, y1 is the width, height of the reference grid
  183. img->x0 = 0;
  184. img->y0 = 0;
  185. img->x1 = (avctx->width - 1) * parameters->subsampling_dx + 1;
  186. img->y1 = (avctx->height - 1) * parameters->subsampling_dy + 1;
  187. return img;
  188. }
  189. static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
  190. {
  191. LibOpenJPEGContext *ctx = avctx->priv_data;
  192. int err = AVERROR(ENOMEM);
  193. opj_set_default_encoder_parameters(&ctx->enc_params);
  194. ctx->enc_params.cp_rsiz = ctx->profile;
  195. ctx->enc_params.mode = !!avctx->global_quality;
  196. ctx->enc_params.cp_cinema = ctx->cinema_mode;
  197. ctx->enc_params.prog_order = ctx->prog_order;
  198. ctx->enc_params.numresolution = ctx->numresolution;
  199. ctx->enc_params.cp_disto_alloc = ctx->disto_alloc;
  200. ctx->enc_params.cp_fixed_alloc = ctx->fixed_alloc;
  201. ctx->enc_params.cp_fixed_quality = ctx->fixed_quality;
  202. ctx->enc_params.tcp_numlayers = ctx->numlayers;
  203. ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
  204. if (ctx->cinema_mode > 0) {
  205. cinema_parameters(&ctx->enc_params);
  206. }
  207. ctx->image = mj2_create_image(avctx, &ctx->enc_params);
  208. if (!ctx->image) {
  209. av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
  210. err = AVERROR(EINVAL);
  211. goto fail;
  212. }
  213. return 0;
  214. fail:
  215. opj_image_destroy(ctx->image);
  216. ctx->image = NULL;
  217. return err;
  218. }
  219. static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  220. {
  221. int compno;
  222. int x;
  223. int y;
  224. int *image_line;
  225. int frame_index;
  226. const int numcomps = image->numcomps;
  227. for (compno = 0; compno < numcomps; ++compno) {
  228. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  229. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  230. return 0;
  231. }
  232. }
  233. for (compno = 0; compno < numcomps; ++compno) {
  234. for (y = 0; y < avctx->height; ++y) {
  235. image_line = image->comps[compno].data + y * image->comps[compno].w;
  236. frame_index = y * frame->linesize[0] + compno;
  237. for (x = 0; x < avctx->width; ++x) {
  238. image_line[x] = frame->data[0][frame_index];
  239. frame_index += numcomps;
  240. }
  241. for (; x < image->comps[compno].w; ++x) {
  242. image_line[x] = image_line[x - 1];
  243. }
  244. }
  245. for (; y < image->comps[compno].h; ++y) {
  246. image_line = image->comps[compno].data + y * image->comps[compno].w;
  247. for (x = 0; x < image->comps[compno].w; ++x) {
  248. image_line[x] = image_line[x - image->comps[compno].w];
  249. }
  250. }
  251. }
  252. return 1;
  253. }
  254. // for XYZ 12 bit
  255. static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  256. {
  257. int compno;
  258. int x, y;
  259. int *image_line;
  260. int frame_index;
  261. const int numcomps = image->numcomps;
  262. uint16_t *frame_ptr = (uint16_t *)frame->data[0];
  263. for (compno = 0; compno < numcomps; ++compno) {
  264. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  265. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  266. return 0;
  267. }
  268. }
  269. for (compno = 0; compno < numcomps; ++compno) {
  270. for (y = 0; y < avctx->height; ++y) {
  271. image_line = image->comps[compno].data + y * image->comps[compno].w;
  272. frame_index = y * (frame->linesize[0] / 2) + compno;
  273. for (x = 0; x < avctx->width; ++x) {
  274. image_line[x] = frame_ptr[frame_index] >> 4;
  275. frame_index += numcomps;
  276. }
  277. for (; x < image->comps[compno].w; ++x) {
  278. image_line[x] = image_line[x - 1];
  279. }
  280. }
  281. for (; y < image->comps[compno].h; ++y) {
  282. image_line = image->comps[compno].data + y * image->comps[compno].w;
  283. for (x = 0; x < image->comps[compno].w; ++x) {
  284. image_line[x] = image_line[x - image->comps[compno].w];
  285. }
  286. }
  287. }
  288. return 1;
  289. }
  290. static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  291. {
  292. int compno;
  293. int x;
  294. int y;
  295. int *image_line;
  296. int frame_index;
  297. const int numcomps = image->numcomps;
  298. uint16_t *frame_ptr = (uint16_t*)frame->data[0];
  299. for (compno = 0; compno < numcomps; ++compno) {
  300. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  301. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  302. return 0;
  303. }
  304. }
  305. for (compno = 0; compno < numcomps; ++compno) {
  306. for (y = 0; y < avctx->height; ++y) {
  307. image_line = image->comps[compno].data + y * image->comps[compno].w;
  308. frame_index = y * (frame->linesize[0] / 2) + compno;
  309. for (x = 0; x < avctx->width; ++x) {
  310. image_line[x] = frame_ptr[frame_index];
  311. frame_index += numcomps;
  312. }
  313. for (; x < image->comps[compno].w; ++x) {
  314. image_line[x] = image_line[x - 1];
  315. }
  316. }
  317. for (; y < image->comps[compno].h; ++y) {
  318. image_line = image->comps[compno].data + y * image->comps[compno].w;
  319. for (x = 0; x < image->comps[compno].w; ++x) {
  320. image_line[x] = image_line[x - image->comps[compno].w];
  321. }
  322. }
  323. }
  324. return 1;
  325. }
  326. static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  327. {
  328. int compno;
  329. int x;
  330. int y;
  331. int width;
  332. int height;
  333. int *image_line;
  334. int frame_index;
  335. const int numcomps = image->numcomps;
  336. for (compno = 0; compno < numcomps; ++compno) {
  337. if (image->comps[compno].w > frame->linesize[compno]) {
  338. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  339. return 0;
  340. }
  341. }
  342. for (compno = 0; compno < numcomps; ++compno) {
  343. width = avctx->width / image->comps[compno].dx;
  344. height = avctx->height / image->comps[compno].dy;
  345. for (y = 0; y < height; ++y) {
  346. image_line = image->comps[compno].data + y * image->comps[compno].w;
  347. frame_index = y * frame->linesize[compno];
  348. for (x = 0; x < width; ++x)
  349. image_line[x] = frame->data[compno][frame_index++];
  350. for (; x < image->comps[compno].w; ++x) {
  351. image_line[x] = image_line[x - 1];
  352. }
  353. }
  354. for (; y < image->comps[compno].h; ++y) {
  355. image_line = image->comps[compno].data + y * image->comps[compno].w;
  356. for (x = 0; x < image->comps[compno].w; ++x) {
  357. image_line[x] = image_line[x - image->comps[compno].w];
  358. }
  359. }
  360. }
  361. return 1;
  362. }
  363. static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  364. {
  365. int compno;
  366. int x;
  367. int y;
  368. int width;
  369. int height;
  370. int *image_line;
  371. int frame_index;
  372. const int numcomps = image->numcomps;
  373. uint16_t *frame_ptr;
  374. for (compno = 0; compno < numcomps; ++compno) {
  375. if (image->comps[compno].w > frame->linesize[compno]) {
  376. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  377. return 0;
  378. }
  379. }
  380. for (compno = 0; compno < numcomps; ++compno) {
  381. width = avctx->width / image->comps[compno].dx;
  382. height = avctx->height / image->comps[compno].dy;
  383. frame_ptr = (uint16_t *)frame->data[compno];
  384. for (y = 0; y < height; ++y) {
  385. image_line = image->comps[compno].data + y * image->comps[compno].w;
  386. frame_index = y * (frame->linesize[compno] / 2);
  387. for (x = 0; x < width; ++x)
  388. image_line[x] = frame_ptr[frame_index++];
  389. for (; x < image->comps[compno].w; ++x) {
  390. image_line[x] = image_line[x - 1];
  391. }
  392. }
  393. for (; y < image->comps[compno].h; ++y) {
  394. image_line = image->comps[compno].data + y * image->comps[compno].w;
  395. for (x = 0; x < image->comps[compno].w; ++x) {
  396. image_line[x] = image_line[x - image->comps[compno].w];
  397. }
  398. }
  399. }
  400. return 1;
  401. }
  402. static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  403. const AVFrame *frame, int *got_packet)
  404. {
  405. LibOpenJPEGContext *ctx = avctx->priv_data;
  406. opj_image_t *image = ctx->image;
  407. opj_cinfo_t *compress = NULL;
  408. opj_cio_t *stream = NULL;
  409. int cpyresult = 0;
  410. int ret, len;
  411. AVFrame *gbrframe;
  412. switch (avctx->pix_fmt) {
  413. case AV_PIX_FMT_RGB24:
  414. case AV_PIX_FMT_RGBA:
  415. case AV_PIX_FMT_YA8:
  416. cpyresult = libopenjpeg_copy_packed8(avctx, frame, image);
  417. break;
  418. case AV_PIX_FMT_XYZ12:
  419. cpyresult = libopenjpeg_copy_packed12(avctx, frame, image);
  420. break;
  421. case AV_PIX_FMT_RGB48:
  422. case AV_PIX_FMT_RGBA64:
  423. case AV_PIX_FMT_YA16:
  424. cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
  425. break;
  426. case AV_PIX_FMT_GBR24P:
  427. case AV_PIX_FMT_GBRP9:
  428. case AV_PIX_FMT_GBRP10:
  429. case AV_PIX_FMT_GBRP12:
  430. case AV_PIX_FMT_GBRP14:
  431. case AV_PIX_FMT_GBRP16:
  432. gbrframe = av_frame_clone(frame);
  433. if (!gbrframe)
  434. return AVERROR(ENOMEM);
  435. gbrframe->data[0] = frame->data[2]; // swap to be rgb
  436. gbrframe->data[1] = frame->data[0];
  437. gbrframe->data[2] = frame->data[1];
  438. gbrframe->linesize[0] = frame->linesize[2];
  439. gbrframe->linesize[1] = frame->linesize[0];
  440. gbrframe->linesize[2] = frame->linesize[1];
  441. if (avctx->pix_fmt == AV_PIX_FMT_GBR24P) {
  442. cpyresult = libopenjpeg_copy_unpacked8(avctx, gbrframe, image);
  443. } else {
  444. cpyresult = libopenjpeg_copy_unpacked16(avctx, gbrframe, image);
  445. }
  446. av_frame_free(&gbrframe);
  447. break;
  448. case AV_PIX_FMT_GRAY8:
  449. case AV_PIX_FMT_YUV410P:
  450. case AV_PIX_FMT_YUV411P:
  451. case AV_PIX_FMT_YUV420P:
  452. case AV_PIX_FMT_YUV422P:
  453. case AV_PIX_FMT_YUV440P:
  454. case AV_PIX_FMT_YUV444P:
  455. case AV_PIX_FMT_YUVA420P:
  456. case AV_PIX_FMT_YUVA422P:
  457. case AV_PIX_FMT_YUVA444P:
  458. cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
  459. break;
  460. case AV_PIX_FMT_GRAY16:
  461. case AV_PIX_FMT_YUV420P9:
  462. case AV_PIX_FMT_YUV422P9:
  463. case AV_PIX_FMT_YUV444P9:
  464. case AV_PIX_FMT_YUVA420P9:
  465. case AV_PIX_FMT_YUVA422P9:
  466. case AV_PIX_FMT_YUVA444P9:
  467. case AV_PIX_FMT_YUV444P10:
  468. case AV_PIX_FMT_YUV422P10:
  469. case AV_PIX_FMT_YUV420P10:
  470. case AV_PIX_FMT_YUVA444P10:
  471. case AV_PIX_FMT_YUVA422P10:
  472. case AV_PIX_FMT_YUVA420P10:
  473. case AV_PIX_FMT_YUV420P12:
  474. case AV_PIX_FMT_YUV422P12:
  475. case AV_PIX_FMT_YUV444P12:
  476. case AV_PIX_FMT_YUV420P14:
  477. case AV_PIX_FMT_YUV422P14:
  478. case AV_PIX_FMT_YUV444P14:
  479. case AV_PIX_FMT_YUV444P16:
  480. case AV_PIX_FMT_YUV422P16:
  481. case AV_PIX_FMT_YUV420P16:
  482. case AV_PIX_FMT_YUVA444P16:
  483. case AV_PIX_FMT_YUVA422P16:
  484. case AV_PIX_FMT_YUVA420P16:
  485. cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
  486. break;
  487. default:
  488. av_log(avctx, AV_LOG_ERROR,
  489. "The frame's pixel format '%s' is not supported\n",
  490. av_get_pix_fmt_name(avctx->pix_fmt));
  491. return AVERROR(EINVAL);
  492. break;
  493. }
  494. if (!cpyresult) {
  495. av_log(avctx, AV_LOG_ERROR,
  496. "Could not copy the frame data to the internal image buffer\n");
  497. return -1;
  498. }
  499. compress = opj_create_compress(ctx->format);
  500. if (!compress) {
  501. av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
  502. return AVERROR(ENOMEM);
  503. }
  504. opj_setup_encoder(compress, &ctx->enc_params, image);
  505. stream = opj_cio_open((opj_common_ptr) compress, NULL, 0);
  506. if (!stream) {
  507. av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
  508. return AVERROR(ENOMEM);
  509. }
  510. memset(&ctx->event_mgr, 0, sizeof(ctx->event_mgr));
  511. ctx->event_mgr.info_handler = info_callback;
  512. ctx->event_mgr.error_handler = error_callback;
  513. ctx->event_mgr.warning_handler = warning_callback;
  514. opj_set_event_mgr((opj_common_ptr) compress, &ctx->event_mgr, avctx);
  515. if (!opj_encode(compress, stream, image, NULL)) {
  516. av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
  517. return -1;
  518. }
  519. len = cio_tell(stream);
  520. if ((ret = ff_alloc_packet2(avctx, pkt, len, 0)) < 0) {
  521. return ret;
  522. }
  523. memcpy(pkt->data, stream->buffer, len);
  524. pkt->flags |= AV_PKT_FLAG_KEY;
  525. *got_packet = 1;
  526. opj_cio_close(stream);
  527. stream = NULL;
  528. opj_destroy_compress(compress);
  529. compress = NULL;
  530. return 0;
  531. }
  532. static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
  533. {
  534. LibOpenJPEGContext *ctx = avctx->priv_data;
  535. opj_image_destroy(ctx->image);
  536. ctx->image = NULL;
  537. return 0;
  538. }
  539. #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
  540. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  541. static const AVOption options[] = {
  542. { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
  543. { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
  544. { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
  545. { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = STD_RSIZ }, STD_RSIZ, CINEMA4K, VE, "profile" },
  546. { "jpeg2000", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = STD_RSIZ }, 0, 0, VE, "profile" },
  547. { "cinema2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K }, 0, 0, VE, "profile" },
  548. { "cinema4k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA4K }, 0, 0, VE, "profile" },
  549. { "cinema_mode", "Digital Cinema", OFFSET(cinema_mode), AV_OPT_TYPE_INT, { .i64 = OFF }, OFF, CINEMA4K_24, VE, "cinema_mode" },
  550. { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OFF }, 0, 0, VE, "cinema_mode" },
  551. { "2k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K_24 }, 0, 0, VE, "cinema_mode" },
  552. { "2k_48", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K_48 }, 0, 0, VE, "cinema_mode" },
  553. { "4k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA4K_24 }, 0, 0, VE, "cinema_mode" },
  554. { "prog_order", "Progression Order", OFFSET(prog_order), AV_OPT_TYPE_INT, { .i64 = LRCP }, LRCP, CPRL, VE, "prog_order" },
  555. { "lrcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LRCP }, 0, 0, VE, "prog_order" },
  556. { "rlcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = RLCP }, 0, 0, VE, "prog_order" },
  557. { "rpcl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = RPCL }, 0, 0, VE, "prog_order" },
  558. { "pcrl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PCRL }, 0, 0, VE, "prog_order" },
  559. { "cprl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPRL }, 0, 0, VE, "prog_order" },
  560. { "numresolution", NULL, OFFSET(numresolution), AV_OPT_TYPE_INT, { .i64 = 6 }, 1, INT_MAX, VE },
  561. { "numlayers", NULL, OFFSET(numlayers), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 10, VE },
  562. { "disto_alloc", NULL, OFFSET(disto_alloc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
  563. { "fixed_alloc", NULL, OFFSET(fixed_alloc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  564. { "fixed_quality", NULL, OFFSET(fixed_quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  565. { NULL },
  566. };
  567. static const AVClass openjpeg_class = {
  568. .class_name = "libopenjpeg",
  569. .item_name = av_default_item_name,
  570. .option = options,
  571. .version = LIBAVUTIL_VERSION_INT,
  572. };
  573. AVCodec ff_libopenjpeg_encoder = {
  574. .name = "libopenjpeg",
  575. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
  576. .type = AVMEDIA_TYPE_VIDEO,
  577. .id = AV_CODEC_ID_JPEG2000,
  578. .priv_data_size = sizeof(LibOpenJPEGContext),
  579. .init = libopenjpeg_encode_init,
  580. .encode2 = libopenjpeg_encode_frame,
  581. .close = libopenjpeg_encode_close,
  582. .capabilities = CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
  583. .pix_fmts = (const enum AVPixelFormat[]) {
  584. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB48,
  585. AV_PIX_FMT_RGBA64, AV_PIX_FMT_GBR24P,
  586. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  587. AV_PIX_FMT_GRAY8, AV_PIX_FMT_YA8, AV_PIX_FMT_GRAY16, AV_PIX_FMT_YA16,
  588. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P,
  589. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA422P,
  590. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA444P,
  591. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  592. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  593. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  594. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  595. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
  596. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  597. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  598. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  599. AV_PIX_FMT_XYZ12,
  600. AV_PIX_FMT_NONE
  601. },
  602. .priv_class = &openjpeg_class,
  603. };