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.

582 lines
21KB

  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 {
  39. AVClass *avclass;
  40. opj_image_t *image;
  41. opj_cparameters_t enc_params;
  42. opj_cinfo_t *compress;
  43. opj_event_mgr_t event_mgr;
  44. int format;
  45. int profile;
  46. int prog_order;
  47. int cinema_mode;
  48. int numresolution;
  49. int numlayers;
  50. int disto_alloc;
  51. int fixed_alloc;
  52. int fixed_quality;
  53. } LibOpenJPEGContext;
  54. static void error_callback(const char *msg, void *data)
  55. {
  56. av_log(data, AV_LOG_ERROR, "%s\n", msg);
  57. }
  58. static void warning_callback(const char *msg, void *data)
  59. {
  60. av_log(data, AV_LOG_WARNING, "%s\n", msg);
  61. }
  62. static void info_callback(const char *msg, void *data)
  63. {
  64. av_log(data, AV_LOG_DEBUG, "%s\n", msg);
  65. }
  66. static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
  67. {
  68. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  69. opj_image_cmptparm_t *cmptparm;
  70. opj_image_t *img;
  71. int i;
  72. int sub_dx[4];
  73. int sub_dy[4];
  74. int numcomps;
  75. OPJ_COLOR_SPACE color_space = CLRSPC_UNKNOWN;
  76. sub_dx[0] = sub_dx[3] = 1;
  77. sub_dy[0] = sub_dy[3] = 1;
  78. sub_dx[1] = sub_dx[2] = 1 << desc->log2_chroma_w;
  79. sub_dy[1] = sub_dy[2] = 1 << desc->log2_chroma_h;
  80. numcomps = desc->nb_components;
  81. switch (avctx->pix_fmt) {
  82. case AV_PIX_FMT_GRAY8:
  83. case AV_PIX_FMT_GRAY8A:
  84. case AV_PIX_FMT_GRAY16:
  85. color_space = CLRSPC_GRAY;
  86. break;
  87. case AV_PIX_FMT_RGB24:
  88. case AV_PIX_FMT_RGBA:
  89. case AV_PIX_FMT_RGB48:
  90. case AV_PIX_FMT_RGBA64:
  91. case AV_PIX_FMT_GBR24P:
  92. case AV_PIX_FMT_GBRP9:
  93. case AV_PIX_FMT_GBRP10:
  94. case AV_PIX_FMT_GBRP12:
  95. case AV_PIX_FMT_GBRP14:
  96. case AV_PIX_FMT_GBRP16:
  97. color_space = CLRSPC_SRGB;
  98. break;
  99. case AV_PIX_FMT_YUV410P:
  100. case AV_PIX_FMT_YUV411P:
  101. case AV_PIX_FMT_YUV420P:
  102. case AV_PIX_FMT_YUV422P:
  103. case AV_PIX_FMT_YUV440P:
  104. case AV_PIX_FMT_YUV444P:
  105. case AV_PIX_FMT_YUVA420P:
  106. case AV_PIX_FMT_YUVA422P:
  107. case AV_PIX_FMT_YUVA444P:
  108. case AV_PIX_FMT_YUV420P9:
  109. case AV_PIX_FMT_YUV422P9:
  110. case AV_PIX_FMT_YUV444P9:
  111. case AV_PIX_FMT_YUVA420P9:
  112. case AV_PIX_FMT_YUVA422P9:
  113. case AV_PIX_FMT_YUVA444P9:
  114. case AV_PIX_FMT_YUV420P10:
  115. case AV_PIX_FMT_YUV422P10:
  116. case AV_PIX_FMT_YUV444P10:
  117. case AV_PIX_FMT_YUVA420P10:
  118. case AV_PIX_FMT_YUVA422P10:
  119. case AV_PIX_FMT_YUVA444P10:
  120. case AV_PIX_FMT_YUV420P12:
  121. case AV_PIX_FMT_YUV422P12:
  122. case AV_PIX_FMT_YUV444P12:
  123. case AV_PIX_FMT_YUV420P14:
  124. case AV_PIX_FMT_YUV422P14:
  125. case AV_PIX_FMT_YUV444P14:
  126. case AV_PIX_FMT_YUV420P16:
  127. case AV_PIX_FMT_YUV422P16:
  128. case AV_PIX_FMT_YUV444P16:
  129. case AV_PIX_FMT_YUVA420P16:
  130. case AV_PIX_FMT_YUVA422P16:
  131. case AV_PIX_FMT_YUVA444P16:
  132. color_space = CLRSPC_SYCC;
  133. break;
  134. default:
  135. av_log(avctx, AV_LOG_ERROR,
  136. "The requested pixel format '%s' is not supported\n",
  137. av_get_pix_fmt_name(avctx->pix_fmt));
  138. return NULL;
  139. }
  140. cmptparm = av_mallocz(numcomps * sizeof(*cmptparm));
  141. if (!cmptparm) {
  142. av_log(avctx, AV_LOG_ERROR, "Not enough memory\n");
  143. return NULL;
  144. }
  145. for (i = 0; i < numcomps; i++) {
  146. cmptparm[i].prec = desc->comp[i].depth_minus1 + 1;
  147. cmptparm[i].bpp = desc->comp[i].depth_minus1 + 1;
  148. cmptparm[i].sgnd = 0;
  149. cmptparm[i].dx = sub_dx[i];
  150. cmptparm[i].dy = sub_dy[i];
  151. cmptparm[i].w = avctx->width / sub_dx[i];
  152. cmptparm[i].h = avctx->height / sub_dy[i];
  153. }
  154. img = opj_image_create(numcomps, cmptparm, color_space);
  155. av_freep(&cmptparm);
  156. return img;
  157. }
  158. static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
  159. {
  160. LibOpenJPEGContext *ctx = avctx->priv_data;
  161. int err = AVERROR(ENOMEM);
  162. opj_set_default_encoder_parameters(&ctx->enc_params);
  163. ctx->enc_params.cp_rsiz = ctx->profile;
  164. ctx->enc_params.mode = !!avctx->global_quality;
  165. ctx->enc_params.cp_cinema = ctx->cinema_mode;
  166. ctx->enc_params.prog_order = ctx->prog_order;
  167. ctx->enc_params.numresolution = ctx->numresolution;
  168. ctx->enc_params.cp_disto_alloc = ctx->disto_alloc;
  169. ctx->enc_params.cp_fixed_alloc = ctx->fixed_alloc;
  170. ctx->enc_params.cp_fixed_quality = ctx->fixed_quality;
  171. ctx->enc_params.tcp_numlayers = ctx->numlayers;
  172. ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
  173. if (ctx->cinema_mode > 0) {
  174. ctx->enc_params.irreversible = 1;
  175. ctx->enc_params.tcp_mct = 1;
  176. ctx->enc_params.tile_size_on = 0;
  177. /* no subsampling */
  178. ctx->enc_params.cp_tdx=1;
  179. ctx->enc_params.cp_tdy=1;
  180. ctx->enc_params.subsampling_dx = 1;
  181. ctx->enc_params.subsampling_dy = 1;
  182. /* Tile and Image shall be at (0,0) */
  183. ctx->enc_params.cp_tx0 = 0;
  184. ctx->enc_params.cp_ty0 = 0;
  185. ctx->enc_params.image_offset_x0 = 0;
  186. ctx->enc_params.image_offset_y0 = 0;
  187. /* Codeblock size= 32*32 */
  188. ctx->enc_params.cblockw_init = 32;
  189. ctx->enc_params.cblockh_init = 32;
  190. ctx->enc_params.csty |= 0x01;
  191. /* No ROI */
  192. ctx->enc_params.roi_compno = -1;
  193. if (ctx->enc_params.prog_order != CPRL) {
  194. av_log(avctx, AV_LOG_ERROR, "prog_order forced to CPRL\n");
  195. ctx->enc_params.prog_order = CPRL;
  196. }
  197. ctx->enc_params.tp_flag = 'C';
  198. ctx->enc_params.tp_on = 1;
  199. }
  200. ctx->compress = opj_create_compress(ctx->format);
  201. if (!ctx->compress) {
  202. av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
  203. return AVERROR(ENOMEM);
  204. }
  205. avctx->coded_frame = avcodec_alloc_frame();
  206. if (!avctx->coded_frame) {
  207. av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
  208. goto fail;
  209. }
  210. ctx->image = mj2_create_image(avctx, &ctx->enc_params);
  211. if (!ctx->image) {
  212. av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
  213. err = AVERROR(EINVAL);
  214. goto fail;
  215. }
  216. memset(&ctx->event_mgr, 0, sizeof(opj_event_mgr_t));
  217. ctx->event_mgr.info_handler = info_callback;
  218. ctx->event_mgr.error_handler = error_callback;
  219. ctx->event_mgr.warning_handler = warning_callback;
  220. opj_set_event_mgr((opj_common_ptr)ctx->compress, &ctx->event_mgr, avctx);
  221. return 0;
  222. fail:
  223. av_freep(&ctx->compress);
  224. av_freep(&avctx->coded_frame);
  225. return err;
  226. }
  227. static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  228. {
  229. int compno;
  230. int x;
  231. int y;
  232. int image_index;
  233. int frame_index;
  234. const int numcomps = image->numcomps;
  235. for (compno = 0; compno < numcomps; ++compno) {
  236. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  237. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  238. return 0;
  239. }
  240. }
  241. for (compno = 0; compno < numcomps; ++compno) {
  242. for (y = 0; y < avctx->height; ++y) {
  243. image_index = y * avctx->width;
  244. frame_index = y * frame->linesize[0] + compno;
  245. for (x = 0; x < avctx->width; ++x) {
  246. image->comps[compno].data[image_index++] = frame->data[0][frame_index];
  247. frame_index += numcomps;
  248. }
  249. }
  250. }
  251. return 1;
  252. }
  253. static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  254. {
  255. int compno;
  256. int x;
  257. int y;
  258. int image_index;
  259. int frame_index;
  260. const int numcomps = image->numcomps;
  261. uint16_t *frame_ptr = (uint16_t*)frame->data[0];
  262. for (compno = 0; compno < numcomps; ++compno) {
  263. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  264. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  265. return 0;
  266. }
  267. }
  268. for (compno = 0; compno < numcomps; ++compno) {
  269. for (y = 0; y < avctx->height; ++y) {
  270. image_index = y * avctx->width;
  271. frame_index = y * (frame->linesize[0] / 2) + compno;
  272. for (x = 0; x < avctx->width; ++x) {
  273. image->comps[compno].data[image_index++] = frame_ptr[frame_index];
  274. frame_index += numcomps;
  275. }
  276. }
  277. }
  278. return 1;
  279. }
  280. static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  281. {
  282. int compno;
  283. int x;
  284. int y;
  285. int width;
  286. int height;
  287. int image_index;
  288. int frame_index;
  289. const int numcomps = image->numcomps;
  290. for (compno = 0; compno < numcomps; ++compno) {
  291. if (image->comps[compno].w > frame->linesize[compno]) {
  292. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  293. return 0;
  294. }
  295. }
  296. for (compno = 0; compno < numcomps; ++compno) {
  297. width = avctx->width / image->comps[compno].dx;
  298. height = avctx->height / image->comps[compno].dy;
  299. for (y = 0; y < height; ++y) {
  300. image_index = y * width;
  301. frame_index = y * frame->linesize[compno];
  302. for (x = 0; x < width; ++x)
  303. image->comps[compno].data[image_index++] = frame->data[compno][frame_index++];
  304. }
  305. }
  306. return 1;
  307. }
  308. static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  309. {
  310. int compno;
  311. int x;
  312. int y;
  313. int width;
  314. int height;
  315. int image_index;
  316. int frame_index;
  317. const int numcomps = image->numcomps;
  318. uint16_t *frame_ptr;
  319. for (compno = 0; compno < numcomps; ++compno) {
  320. if (image->comps[compno].w > frame->linesize[compno]) {
  321. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  322. return 0;
  323. }
  324. }
  325. for (compno = 0; compno < numcomps; ++compno) {
  326. width = avctx->width / image->comps[compno].dx;
  327. height = avctx->height / image->comps[compno].dy;
  328. frame_ptr = (uint16_t*)frame->data[compno];
  329. for (y = 0; y < height; ++y) {
  330. image_index = y * width;
  331. frame_index = y * (frame->linesize[compno] / 2);
  332. for (x = 0; x < width; ++x)
  333. image->comps[compno].data[image_index++] = frame_ptr[frame_index++];
  334. }
  335. }
  336. return 1;
  337. }
  338. static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  339. const AVFrame *frame, int *got_packet)
  340. {
  341. LibOpenJPEGContext *ctx = avctx->priv_data;
  342. opj_cinfo_t *compress = ctx->compress;
  343. opj_image_t *image = ctx->image;
  344. opj_cio_t *stream;
  345. int cpyresult = 0;
  346. int ret, len;
  347. AVFrame gbrframe;
  348. // x0, y0 is the top left corner of the image
  349. // x1, y1 is the width, height of the reference grid
  350. image->x0 = 0;
  351. image->y0 = 0;
  352. image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
  353. image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
  354. switch (avctx->pix_fmt) {
  355. case AV_PIX_FMT_RGB24:
  356. case AV_PIX_FMT_RGBA:
  357. case AV_PIX_FMT_GRAY8A:
  358. cpyresult = libopenjpeg_copy_packed8(avctx, frame, image);
  359. break;
  360. case AV_PIX_FMT_RGB48:
  361. case AV_PIX_FMT_RGBA64:
  362. cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
  363. break;
  364. case AV_PIX_FMT_GBR24P:
  365. case AV_PIX_FMT_GBRP9:
  366. case AV_PIX_FMT_GBRP10:
  367. case AV_PIX_FMT_GBRP12:
  368. case AV_PIX_FMT_GBRP14:
  369. case AV_PIX_FMT_GBRP16:
  370. gbrframe = *frame;
  371. gbrframe.data[0] = frame->data[2]; // swap to be rgb
  372. gbrframe.data[1] = frame->data[0];
  373. gbrframe.data[2] = frame->data[1];
  374. gbrframe.linesize[0] = frame->linesize[2];
  375. gbrframe.linesize[1] = frame->linesize[0];
  376. gbrframe.linesize[2] = frame->linesize[1];
  377. if (avctx->pix_fmt == AV_PIX_FMT_GBR24P) {
  378. cpyresult = libopenjpeg_copy_unpacked8(avctx, &gbrframe, image);
  379. } else {
  380. cpyresult = libopenjpeg_copy_unpacked16(avctx, &gbrframe, image);
  381. }
  382. break;
  383. case AV_PIX_FMT_GRAY8:
  384. case AV_PIX_FMT_YUV410P:
  385. case AV_PIX_FMT_YUV411P:
  386. case AV_PIX_FMT_YUV420P:
  387. case AV_PIX_FMT_YUV422P:
  388. case AV_PIX_FMT_YUV440P:
  389. case AV_PIX_FMT_YUV444P:
  390. case AV_PIX_FMT_YUVA420P:
  391. case AV_PIX_FMT_YUVA422P:
  392. case AV_PIX_FMT_YUVA444P:
  393. cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
  394. break;
  395. case AV_PIX_FMT_GRAY16:
  396. case AV_PIX_FMT_YUV420P9:
  397. case AV_PIX_FMT_YUV422P9:
  398. case AV_PIX_FMT_YUV444P9:
  399. case AV_PIX_FMT_YUVA420P9:
  400. case AV_PIX_FMT_YUVA422P9:
  401. case AV_PIX_FMT_YUVA444P9:
  402. case AV_PIX_FMT_YUV444P10:
  403. case AV_PIX_FMT_YUV422P10:
  404. case AV_PIX_FMT_YUV420P10:
  405. case AV_PIX_FMT_YUVA444P10:
  406. case AV_PIX_FMT_YUVA422P10:
  407. case AV_PIX_FMT_YUVA420P10:
  408. case AV_PIX_FMT_YUV420P12:
  409. case AV_PIX_FMT_YUV422P12:
  410. case AV_PIX_FMT_YUV444P12:
  411. case AV_PIX_FMT_YUV420P14:
  412. case AV_PIX_FMT_YUV422P14:
  413. case AV_PIX_FMT_YUV444P14:
  414. case AV_PIX_FMT_YUV444P16:
  415. case AV_PIX_FMT_YUV422P16:
  416. case AV_PIX_FMT_YUV420P16:
  417. case AV_PIX_FMT_YUVA444P16:
  418. case AV_PIX_FMT_YUVA422P16:
  419. case AV_PIX_FMT_YUVA420P16:
  420. cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
  421. break;
  422. default:
  423. av_log(avctx, AV_LOG_ERROR,
  424. "The frame's pixel format '%s' is not supported\n",
  425. av_get_pix_fmt_name(avctx->pix_fmt));
  426. return AVERROR(EINVAL);
  427. break;
  428. }
  429. if (!cpyresult) {
  430. av_log(avctx, AV_LOG_ERROR,
  431. "Could not copy the frame data to the internal image buffer\n");
  432. return -1;
  433. }
  434. opj_setup_encoder(compress, &ctx->enc_params, image);
  435. stream = opj_cio_open((opj_common_ptr)compress, NULL, 0);
  436. if (!stream) {
  437. av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
  438. return AVERROR(ENOMEM);
  439. }
  440. if (!opj_encode(compress, stream, image, NULL)) {
  441. opj_cio_close(stream);
  442. av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
  443. return -1;
  444. }
  445. len = cio_tell(stream);
  446. if ((ret = ff_alloc_packet2(avctx, pkt, len)) < 0) {
  447. opj_cio_close(stream);
  448. return ret;
  449. }
  450. memcpy(pkt->data, stream->buffer, len);
  451. pkt->flags |= AV_PKT_FLAG_KEY;
  452. *got_packet = 1;
  453. opj_cio_close(stream);
  454. return 0;
  455. }
  456. static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
  457. {
  458. LibOpenJPEGContext *ctx = avctx->priv_data;
  459. opj_destroy_compress(ctx->compress);
  460. opj_image_destroy(ctx->image);
  461. av_freep(&avctx->coded_frame);
  462. return 0;
  463. }
  464. #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
  465. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  466. static const AVOption options[] = {
  467. { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
  468. { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
  469. { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
  470. { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = STD_RSIZ }, STD_RSIZ, CINEMA4K, VE, "profile" },
  471. { "jpeg2000", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = STD_RSIZ }, 0, 0, VE, "profile" },
  472. { "cinema2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K }, 0, 0, VE, "profile" },
  473. { "cinema4k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA4K }, 0, 0, VE, "profile" },
  474. { "cinema_mode", "Digital Cinema", OFFSET(cinema_mode), AV_OPT_TYPE_INT, { .i64 = OFF }, OFF, CINEMA4K_24, VE, "cinema_mode" },
  475. { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OFF }, 0, 0, VE, "cinema_mode" },
  476. { "2k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K_24 }, 0, 0, VE, "cinema_mode" },
  477. { "2k_48", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K_48 }, 0, 0, VE, "cinema_mode" },
  478. { "4k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA4K_24 }, 0, 0, VE, "cinema_mode" },
  479. { "prog_order", "Progression Order", OFFSET(prog_order), AV_OPT_TYPE_INT, { .i64 = LRCP }, LRCP, CPRL, VE, "prog_order" },
  480. { "lrcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LRCP }, 0, 0, VE, "prog_order" },
  481. { "rlcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = RLCP }, 0, 0, VE, "prog_order" },
  482. { "rpcl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = RPCL }, 0, 0, VE, "prog_order" },
  483. { "pcrl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PCRL }, 0, 0, VE, "prog_order" },
  484. { "cprl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPRL }, 0, 0, VE, "prog_order" },
  485. { "numresolution", NULL, OFFSET(numresolution), AV_OPT_TYPE_INT, { .i64 = 6 }, 1, INT_MAX, VE },
  486. { "numlayers", NULL, OFFSET(numlayers), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 10, VE },
  487. { "disto_alloc", NULL, OFFSET(disto_alloc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
  488. { "fixed_alloc", NULL, OFFSET(fixed_alloc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  489. { "fixed_quality", NULL, OFFSET(fixed_quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  490. { NULL },
  491. };
  492. static const AVClass openjpeg_class = {
  493. .class_name = "libopenjpeg",
  494. .item_name = av_default_item_name,
  495. .option = options,
  496. .version = LIBAVUTIL_VERSION_INT,
  497. };
  498. AVCodec ff_libopenjpeg_encoder = {
  499. .name = "libopenjpeg",
  500. .type = AVMEDIA_TYPE_VIDEO,
  501. .id = AV_CODEC_ID_JPEG2000,
  502. .priv_data_size = sizeof(LibOpenJPEGContext),
  503. .init = libopenjpeg_encode_init,
  504. .encode2 = libopenjpeg_encode_frame,
  505. .close = libopenjpeg_encode_close,
  506. .capabilities = 0,
  507. .pix_fmts = (const enum AVPixelFormat[]) {
  508. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB48, AV_PIX_FMT_RGBA64,
  509. AV_PIX_FMT_GBR24P,
  510. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  511. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A, AV_PIX_FMT_GRAY16,
  512. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P,
  513. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA422P,
  514. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA444P,
  515. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  516. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  517. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  518. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  519. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
  520. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  521. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  522. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  523. AV_PIX_FMT_NONE
  524. },
  525. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
  526. .priv_class = &openjpeg_class,
  527. };