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.

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