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.

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