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.

682 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. avctx->coded_frame = av_frame_alloc();
  214. if (!avctx->coded_frame) {
  215. av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
  216. goto fail;
  217. }
  218. return 0;
  219. fail:
  220. opj_image_destroy(ctx->image);
  221. ctx->image = NULL;
  222. av_frame_free(&avctx->coded_frame);
  223. return err;
  224. }
  225. static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  226. {
  227. int compno;
  228. int x;
  229. int y;
  230. int *image_line;
  231. int frame_index;
  232. const int numcomps = image->numcomps;
  233. for (compno = 0; compno < numcomps; ++compno) {
  234. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  235. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  236. return 0;
  237. }
  238. }
  239. for (compno = 0; compno < numcomps; ++compno) {
  240. for (y = 0; y < avctx->height; ++y) {
  241. image_line = image->comps[compno].data + y * image->comps[compno].w;
  242. frame_index = y * frame->linesize[0] + compno;
  243. for (x = 0; x < avctx->width; ++x) {
  244. image_line[x] = frame->data[0][frame_index];
  245. frame_index += numcomps;
  246. }
  247. for (; x < image->comps[compno].w; ++x) {
  248. image_line[x] = image_line[x - 1];
  249. }
  250. }
  251. for (; y < image->comps[compno].h; ++y) {
  252. image_line = image->comps[compno].data + y * image->comps[compno].w;
  253. for (x = 0; x < image->comps[compno].w; ++x) {
  254. image_line[x] = image_line[x - image->comps[compno].w];
  255. }
  256. }
  257. }
  258. return 1;
  259. }
  260. // for XYZ 12 bit
  261. static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  262. {
  263. int compno;
  264. int x, y;
  265. int *image_line;
  266. int frame_index;
  267. const int numcomps = image->numcomps;
  268. uint16_t *frame_ptr = (uint16_t *)frame->data[0];
  269. for (compno = 0; compno < numcomps; ++compno) {
  270. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  271. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  272. return 0;
  273. }
  274. }
  275. for (compno = 0; compno < numcomps; ++compno) {
  276. for (y = 0; y < avctx->height; ++y) {
  277. image_line = image->comps[compno].data + y * image->comps[compno].w;
  278. frame_index = y * (frame->linesize[0] / 2) + compno;
  279. for (x = 0; x < avctx->width; ++x) {
  280. image_line[x] = frame_ptr[frame_index] >> 4;
  281. frame_index += numcomps;
  282. }
  283. for (; x < image->comps[compno].w; ++x) {
  284. image_line[x] = image_line[x - 1];
  285. }
  286. }
  287. for (; y < image->comps[compno].h; ++y) {
  288. image_line = image->comps[compno].data + y * image->comps[compno].w;
  289. for (x = 0; x < image->comps[compno].w; ++x) {
  290. image_line[x] = image_line[x - image->comps[compno].w];
  291. }
  292. }
  293. }
  294. return 1;
  295. }
  296. static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  297. {
  298. int compno;
  299. int x;
  300. int y;
  301. int *image_line;
  302. int frame_index;
  303. const int numcomps = image->numcomps;
  304. uint16_t *frame_ptr = (uint16_t*)frame->data[0];
  305. for (compno = 0; compno < numcomps; ++compno) {
  306. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  307. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  308. return 0;
  309. }
  310. }
  311. for (compno = 0; compno < numcomps; ++compno) {
  312. for (y = 0; y < avctx->height; ++y) {
  313. image_line = image->comps[compno].data + y * image->comps[compno].w;
  314. frame_index = y * (frame->linesize[0] / 2) + compno;
  315. for (x = 0; x < avctx->width; ++x) {
  316. image_line[x] = frame_ptr[frame_index];
  317. frame_index += numcomps;
  318. }
  319. for (; x < image->comps[compno].w; ++x) {
  320. image_line[x] = image_line[x - 1];
  321. }
  322. }
  323. for (; y < image->comps[compno].h; ++y) {
  324. image_line = image->comps[compno].data + y * image->comps[compno].w;
  325. for (x = 0; x < image->comps[compno].w; ++x) {
  326. image_line[x] = image_line[x - image->comps[compno].w];
  327. }
  328. }
  329. }
  330. return 1;
  331. }
  332. static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  333. {
  334. int compno;
  335. int x;
  336. int y;
  337. int width;
  338. int height;
  339. int *image_line;
  340. int frame_index;
  341. const int numcomps = image->numcomps;
  342. for (compno = 0; compno < numcomps; ++compno) {
  343. if (image->comps[compno].w > frame->linesize[compno]) {
  344. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  345. return 0;
  346. }
  347. }
  348. for (compno = 0; compno < numcomps; ++compno) {
  349. width = avctx->width / image->comps[compno].dx;
  350. height = avctx->height / image->comps[compno].dy;
  351. for (y = 0; y < height; ++y) {
  352. image_line = image->comps[compno].data + y * image->comps[compno].w;
  353. frame_index = y * frame->linesize[compno];
  354. for (x = 0; x < width; ++x)
  355. image_line[x] = frame->data[compno][frame_index++];
  356. for (; x < image->comps[compno].w; ++x) {
  357. image_line[x] = image_line[x - 1];
  358. }
  359. }
  360. for (; y < image->comps[compno].h; ++y) {
  361. image_line = image->comps[compno].data + y * image->comps[compno].w;
  362. for (x = 0; x < image->comps[compno].w; ++x) {
  363. image_line[x] = image_line[x - image->comps[compno].w];
  364. }
  365. }
  366. }
  367. return 1;
  368. }
  369. static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  370. {
  371. int compno;
  372. int x;
  373. int y;
  374. int width;
  375. int height;
  376. int *image_line;
  377. int frame_index;
  378. const int numcomps = image->numcomps;
  379. uint16_t *frame_ptr;
  380. for (compno = 0; compno < numcomps; ++compno) {
  381. if (image->comps[compno].w > frame->linesize[compno]) {
  382. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  383. return 0;
  384. }
  385. }
  386. for (compno = 0; compno < numcomps; ++compno) {
  387. width = avctx->width / image->comps[compno].dx;
  388. height = avctx->height / image->comps[compno].dy;
  389. frame_ptr = (uint16_t *)frame->data[compno];
  390. for (y = 0; y < height; ++y) {
  391. image_line = image->comps[compno].data + y * image->comps[compno].w;
  392. frame_index = y * (frame->linesize[compno] / 2);
  393. for (x = 0; x < width; ++x)
  394. image_line[x] = frame_ptr[frame_index++];
  395. for (; x < image->comps[compno].w; ++x) {
  396. image_line[x] = image_line[x - 1];
  397. }
  398. }
  399. for (; y < image->comps[compno].h; ++y) {
  400. image_line = image->comps[compno].data + y * image->comps[compno].w;
  401. for (x = 0; x < image->comps[compno].w; ++x) {
  402. image_line[x] = image_line[x - image->comps[compno].w];
  403. }
  404. }
  405. }
  406. return 1;
  407. }
  408. static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  409. const AVFrame *frame, int *got_packet)
  410. {
  411. LibOpenJPEGContext *ctx = avctx->priv_data;
  412. opj_image_t *image = ctx->image;
  413. opj_cinfo_t *compress = NULL;
  414. opj_cio_t *stream = NULL;
  415. int cpyresult = 0;
  416. int ret, len;
  417. AVFrame *gbrframe;
  418. switch (avctx->pix_fmt) {
  419. case AV_PIX_FMT_RGB24:
  420. case AV_PIX_FMT_RGBA:
  421. case AV_PIX_FMT_YA8:
  422. cpyresult = libopenjpeg_copy_packed8(avctx, frame, image);
  423. break;
  424. case AV_PIX_FMT_XYZ12:
  425. cpyresult = libopenjpeg_copy_packed12(avctx, frame, image);
  426. break;
  427. case AV_PIX_FMT_RGB48:
  428. case AV_PIX_FMT_RGBA64:
  429. case AV_PIX_FMT_YA16:
  430. cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
  431. break;
  432. case AV_PIX_FMT_GBR24P:
  433. case AV_PIX_FMT_GBRP9:
  434. case AV_PIX_FMT_GBRP10:
  435. case AV_PIX_FMT_GBRP12:
  436. case AV_PIX_FMT_GBRP14:
  437. case AV_PIX_FMT_GBRP16:
  438. gbrframe = av_frame_clone(frame);
  439. if (!gbrframe)
  440. return AVERROR(ENOMEM);
  441. gbrframe->data[0] = frame->data[2]; // swap to be rgb
  442. gbrframe->data[1] = frame->data[0];
  443. gbrframe->data[2] = frame->data[1];
  444. gbrframe->linesize[0] = frame->linesize[2];
  445. gbrframe->linesize[1] = frame->linesize[0];
  446. gbrframe->linesize[2] = frame->linesize[1];
  447. if (avctx->pix_fmt == AV_PIX_FMT_GBR24P) {
  448. cpyresult = libopenjpeg_copy_unpacked8(avctx, gbrframe, image);
  449. } else {
  450. cpyresult = libopenjpeg_copy_unpacked16(avctx, gbrframe, image);
  451. }
  452. av_frame_free(&gbrframe);
  453. break;
  454. case AV_PIX_FMT_GRAY8:
  455. case AV_PIX_FMT_YUV410P:
  456. case AV_PIX_FMT_YUV411P:
  457. case AV_PIX_FMT_YUV420P:
  458. case AV_PIX_FMT_YUV422P:
  459. case AV_PIX_FMT_YUV440P:
  460. case AV_PIX_FMT_YUV444P:
  461. case AV_PIX_FMT_YUVA420P:
  462. case AV_PIX_FMT_YUVA422P:
  463. case AV_PIX_FMT_YUVA444P:
  464. cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
  465. break;
  466. case AV_PIX_FMT_GRAY16:
  467. case AV_PIX_FMT_YUV420P9:
  468. case AV_PIX_FMT_YUV422P9:
  469. case AV_PIX_FMT_YUV444P9:
  470. case AV_PIX_FMT_YUVA420P9:
  471. case AV_PIX_FMT_YUVA422P9:
  472. case AV_PIX_FMT_YUVA444P9:
  473. case AV_PIX_FMT_YUV444P10:
  474. case AV_PIX_FMT_YUV422P10:
  475. case AV_PIX_FMT_YUV420P10:
  476. case AV_PIX_FMT_YUVA444P10:
  477. case AV_PIX_FMT_YUVA422P10:
  478. case AV_PIX_FMT_YUVA420P10:
  479. case AV_PIX_FMT_YUV420P12:
  480. case AV_PIX_FMT_YUV422P12:
  481. case AV_PIX_FMT_YUV444P12:
  482. case AV_PIX_FMT_YUV420P14:
  483. case AV_PIX_FMT_YUV422P14:
  484. case AV_PIX_FMT_YUV444P14:
  485. case AV_PIX_FMT_YUV444P16:
  486. case AV_PIX_FMT_YUV422P16:
  487. case AV_PIX_FMT_YUV420P16:
  488. case AV_PIX_FMT_YUVA444P16:
  489. case AV_PIX_FMT_YUVA422P16:
  490. case AV_PIX_FMT_YUVA420P16:
  491. cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
  492. break;
  493. default:
  494. av_log(avctx, AV_LOG_ERROR,
  495. "The frame's pixel format '%s' is not supported\n",
  496. av_get_pix_fmt_name(avctx->pix_fmt));
  497. return AVERROR(EINVAL);
  498. break;
  499. }
  500. if (!cpyresult) {
  501. av_log(avctx, AV_LOG_ERROR,
  502. "Could not copy the frame data to the internal image buffer\n");
  503. return -1;
  504. }
  505. compress = opj_create_compress(ctx->format);
  506. if (!compress) {
  507. av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
  508. return AVERROR(ENOMEM);
  509. }
  510. opj_setup_encoder(compress, &ctx->enc_params, image);
  511. stream = opj_cio_open((opj_common_ptr) compress, NULL, 0);
  512. if (!stream) {
  513. av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
  514. return AVERROR(ENOMEM);
  515. }
  516. memset(&ctx->event_mgr, 0, sizeof(opj_event_mgr_t));
  517. ctx->event_mgr.info_handler = info_callback;
  518. ctx->event_mgr.error_handler = error_callback;
  519. ctx->event_mgr.warning_handler = warning_callback;
  520. opj_set_event_mgr((opj_common_ptr) compress, &ctx->event_mgr, avctx);
  521. if (!opj_encode(compress, stream, image, NULL)) {
  522. av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
  523. return -1;
  524. }
  525. len = cio_tell(stream);
  526. if ((ret = ff_alloc_packet2(avctx, pkt, len)) < 0) {
  527. return ret;
  528. }
  529. memcpy(pkt->data, stream->buffer, len);
  530. pkt->flags |= AV_PKT_FLAG_KEY;
  531. *got_packet = 1;
  532. opj_cio_close(stream);
  533. stream = NULL;
  534. opj_destroy_compress(compress);
  535. compress = NULL;
  536. return 0;
  537. }
  538. static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
  539. {
  540. LibOpenJPEGContext *ctx = avctx->priv_data;
  541. opj_image_destroy(ctx->image);
  542. ctx->image = NULL;
  543. av_frame_free(&avctx->coded_frame);
  544. return 0;
  545. }
  546. #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
  547. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  548. static const AVOption options[] = {
  549. { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
  550. { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
  551. { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
  552. { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = STD_RSIZ }, STD_RSIZ, CINEMA4K, VE, "profile" },
  553. { "jpeg2000", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = STD_RSIZ }, 0, 0, VE, "profile" },
  554. { "cinema2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K }, 0, 0, VE, "profile" },
  555. { "cinema4k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA4K }, 0, 0, VE, "profile" },
  556. { "cinema_mode", "Digital Cinema", OFFSET(cinema_mode), AV_OPT_TYPE_INT, { .i64 = OFF }, OFF, CINEMA4K_24, VE, "cinema_mode" },
  557. { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OFF }, 0, 0, VE, "cinema_mode" },
  558. { "2k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K_24 }, 0, 0, VE, "cinema_mode" },
  559. { "2k_48", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K_48 }, 0, 0, VE, "cinema_mode" },
  560. { "4k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA4K_24 }, 0, 0, VE, "cinema_mode" },
  561. { "prog_order", "Progression Order", OFFSET(prog_order), AV_OPT_TYPE_INT, { .i64 = LRCP }, LRCP, CPRL, VE, "prog_order" },
  562. { "lrcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LRCP }, 0, 0, VE, "prog_order" },
  563. { "rlcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = RLCP }, 0, 0, VE, "prog_order" },
  564. { "rpcl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = RPCL }, 0, 0, VE, "prog_order" },
  565. { "pcrl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PCRL }, 0, 0, VE, "prog_order" },
  566. { "cprl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPRL }, 0, 0, VE, "prog_order" },
  567. { "numresolution", NULL, OFFSET(numresolution), AV_OPT_TYPE_INT, { .i64 = 6 }, 1, INT_MAX, VE },
  568. { "numlayers", NULL, OFFSET(numlayers), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 10, VE },
  569. { "disto_alloc", NULL, OFFSET(disto_alloc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
  570. { "fixed_alloc", NULL, OFFSET(fixed_alloc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  571. { "fixed_quality", NULL, OFFSET(fixed_quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  572. { NULL },
  573. };
  574. static const AVClass openjpeg_class = {
  575. .class_name = "libopenjpeg",
  576. .item_name = av_default_item_name,
  577. .option = options,
  578. .version = LIBAVUTIL_VERSION_INT,
  579. };
  580. AVCodec ff_libopenjpeg_encoder = {
  581. .name = "libopenjpeg",
  582. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
  583. .type = AVMEDIA_TYPE_VIDEO,
  584. .id = AV_CODEC_ID_JPEG2000,
  585. .priv_data_size = sizeof(LibOpenJPEGContext),
  586. .init = libopenjpeg_encode_init,
  587. .encode2 = libopenjpeg_encode_frame,
  588. .close = libopenjpeg_encode_close,
  589. .capabilities = CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
  590. .pix_fmts = (const enum AVPixelFormat[]) {
  591. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB48,
  592. AV_PIX_FMT_RGBA64, AV_PIX_FMT_GBR24P,
  593. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  594. AV_PIX_FMT_GRAY8, AV_PIX_FMT_YA8, AV_PIX_FMT_GRAY16, AV_PIX_FMT_YA16,
  595. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P,
  596. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA422P,
  597. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA444P,
  598. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  599. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  600. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  601. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  602. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
  603. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  604. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  605. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  606. AV_PIX_FMT_XYZ12,
  607. AV_PIX_FMT_NONE
  608. },
  609. .priv_class = &openjpeg_class,
  610. };