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.

669 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. case AV_PIX_FMT_YA16:
  87. color_space = CLRSPC_GRAY;
  88. break;
  89. case AV_PIX_FMT_RGB24:
  90. case AV_PIX_FMT_RGBA:
  91. case AV_PIX_FMT_RGB48:
  92. case AV_PIX_FMT_RGBA64:
  93. case AV_PIX_FMT_GBR24P:
  94. case AV_PIX_FMT_GBRP9:
  95. case AV_PIX_FMT_GBRP10:
  96. case AV_PIX_FMT_GBRP12:
  97. case AV_PIX_FMT_GBRP14:
  98. case AV_PIX_FMT_GBRP16:
  99. case AV_PIX_FMT_XYZ12:
  100. color_space = CLRSPC_SRGB;
  101. break;
  102. case AV_PIX_FMT_YUV410P:
  103. case AV_PIX_FMT_YUV411P:
  104. case AV_PIX_FMT_YUV420P:
  105. case AV_PIX_FMT_YUV422P:
  106. case AV_PIX_FMT_YUV440P:
  107. case AV_PIX_FMT_YUV444P:
  108. case AV_PIX_FMT_YUVA420P:
  109. case AV_PIX_FMT_YUVA422P:
  110. case AV_PIX_FMT_YUVA444P:
  111. case AV_PIX_FMT_YUV420P9:
  112. case AV_PIX_FMT_YUV422P9:
  113. case AV_PIX_FMT_YUV444P9:
  114. case AV_PIX_FMT_YUVA420P9:
  115. case AV_PIX_FMT_YUVA422P9:
  116. case AV_PIX_FMT_YUVA444P9:
  117. case AV_PIX_FMT_YUV420P10:
  118. case AV_PIX_FMT_YUV422P10:
  119. case AV_PIX_FMT_YUV444P10:
  120. case AV_PIX_FMT_YUVA420P10:
  121. case AV_PIX_FMT_YUVA422P10:
  122. case AV_PIX_FMT_YUVA444P10:
  123. case AV_PIX_FMT_YUV420P12:
  124. case AV_PIX_FMT_YUV422P12:
  125. case AV_PIX_FMT_YUV444P12:
  126. case AV_PIX_FMT_YUV420P14:
  127. case AV_PIX_FMT_YUV422P14:
  128. case AV_PIX_FMT_YUV444P14:
  129. case AV_PIX_FMT_YUV420P16:
  130. case AV_PIX_FMT_YUV422P16:
  131. case AV_PIX_FMT_YUV444P16:
  132. case AV_PIX_FMT_YUVA420P16:
  133. case AV_PIX_FMT_YUVA422P16:
  134. case AV_PIX_FMT_YUVA444P16:
  135. color_space = CLRSPC_SYCC;
  136. break;
  137. default:
  138. av_log(avctx, AV_LOG_ERROR,
  139. "The requested pixel format '%s' is not supported\n",
  140. av_get_pix_fmt_name(avctx->pix_fmt));
  141. return NULL;
  142. }
  143. for (i = 0; i < numcomps; i++) {
  144. cmptparm[i].prec = desc->comp[i].depth_minus1 + 1;
  145. cmptparm[i].bpp = desc->comp[i].depth_minus1 + 1;
  146. cmptparm[i].sgnd = 0;
  147. cmptparm[i].dx = sub_dx[i];
  148. cmptparm[i].dy = sub_dy[i];
  149. cmptparm[i].w = (avctx->width + sub_dx[i] - 1) / sub_dx[i];
  150. cmptparm[i].h = (avctx->height + sub_dy[i] - 1) / sub_dy[i];
  151. }
  152. img = opj_image_create(numcomps, cmptparm, color_space);
  153. // x0, y0 is the top left corner of the image
  154. // x1, y1 is the width, height of the reference grid
  155. img->x0 = 0;
  156. img->y0 = 0;
  157. img->x1 = (avctx->width - 1) * parameters->subsampling_dx + 1;
  158. img->y1 = (avctx->height - 1) * parameters->subsampling_dy + 1;
  159. return img;
  160. }
  161. static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
  162. {
  163. LibOpenJPEGContext *ctx = avctx->priv_data;
  164. int err = AVERROR(ENOMEM);
  165. opj_set_default_encoder_parameters(&ctx->enc_params);
  166. ctx->enc_params.cp_rsiz = ctx->profile;
  167. ctx->enc_params.mode = !!avctx->global_quality;
  168. ctx->enc_params.cp_cinema = ctx->cinema_mode;
  169. ctx->enc_params.prog_order = ctx->prog_order;
  170. ctx->enc_params.numresolution = ctx->numresolution;
  171. ctx->enc_params.cp_disto_alloc = ctx->disto_alloc;
  172. ctx->enc_params.cp_fixed_alloc = ctx->fixed_alloc;
  173. ctx->enc_params.cp_fixed_quality = ctx->fixed_quality;
  174. ctx->enc_params.tcp_numlayers = ctx->numlayers;
  175. ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
  176. if (ctx->cinema_mode > 0) {
  177. ctx->enc_params.irreversible = 1;
  178. ctx->enc_params.tcp_mct = 1;
  179. ctx->enc_params.tile_size_on = 0;
  180. /* no subsampling */
  181. ctx->enc_params.cp_tdx=1;
  182. ctx->enc_params.cp_tdy=1;
  183. ctx->enc_params.subsampling_dx = 1;
  184. ctx->enc_params.subsampling_dy = 1;
  185. /* Tile and Image shall be at (0,0) */
  186. ctx->enc_params.cp_tx0 = 0;
  187. ctx->enc_params.cp_ty0 = 0;
  188. ctx->enc_params.image_offset_x0 = 0;
  189. ctx->enc_params.image_offset_y0 = 0;
  190. /* Codeblock size= 32*32 */
  191. ctx->enc_params.cblockw_init = 32;
  192. ctx->enc_params.cblockh_init = 32;
  193. ctx->enc_params.csty |= 0x01;
  194. /* No ROI */
  195. ctx->enc_params.roi_compno = -1;
  196. if (ctx->enc_params.prog_order != CPRL) {
  197. av_log(avctx, AV_LOG_ERROR, "prog_order forced to CPRL\n");
  198. ctx->enc_params.prog_order = CPRL;
  199. }
  200. ctx->enc_params.tp_flag = 'C';
  201. ctx->enc_params.tp_on = 1;
  202. }
  203. ctx->compress = opj_create_compress(ctx->format);
  204. if (!ctx->compress) {
  205. av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
  206. return AVERROR(ENOMEM);
  207. }
  208. ctx->image = mj2_create_image(avctx, &ctx->enc_params);
  209. if (!ctx->image) {
  210. av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
  211. err = AVERROR(EINVAL);
  212. goto fail;
  213. }
  214. avctx->coded_frame = av_frame_alloc();
  215. if (!avctx->coded_frame) {
  216. av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
  217. goto fail;
  218. }
  219. memset(&ctx->event_mgr, 0, sizeof(opj_event_mgr_t));
  220. ctx->event_mgr.info_handler = info_callback;
  221. ctx->event_mgr.error_handler = error_callback;
  222. ctx->event_mgr.warning_handler = warning_callback;
  223. opj_set_event_mgr((opj_common_ptr) ctx->compress, &ctx->event_mgr, avctx);
  224. return 0;
  225. fail:
  226. opj_destroy_compress(ctx->compress);
  227. ctx->compress = NULL;
  228. opj_image_destroy(ctx->image);
  229. ctx->image = NULL;
  230. av_freep(&avctx->coded_frame);
  231. return err;
  232. }
  233. static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  234. {
  235. int compno;
  236. int x;
  237. int y;
  238. int *image_line;
  239. int frame_index;
  240. const int numcomps = image->numcomps;
  241. for (compno = 0; compno < numcomps; ++compno) {
  242. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  243. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  244. return 0;
  245. }
  246. }
  247. for (compno = 0; compno < numcomps; ++compno) {
  248. for (y = 0; y < avctx->height; ++y) {
  249. image_line = image->comps[compno].data + y * image->comps[compno].w;
  250. frame_index = y * frame->linesize[0] + compno;
  251. for (x = 0; x < avctx->width; ++x) {
  252. image_line[x] = frame->data[0][frame_index];
  253. frame_index += numcomps;
  254. }
  255. for (; x < image->comps[compno].w; ++x) {
  256. image_line[x] = image_line[x - 1];
  257. }
  258. }
  259. for (; y < image->comps[compno].h; ++y) {
  260. image_line = image->comps[compno].data + y * image->comps[compno].w;
  261. for (x = 0; x < image->comps[compno].w; ++x) {
  262. image_line[x] = image_line[x - image->comps[compno].w];
  263. }
  264. }
  265. }
  266. return 1;
  267. }
  268. // for XYZ 12 bit
  269. static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  270. {
  271. int compno;
  272. int x, y;
  273. int *image_line;
  274. int frame_index;
  275. const int numcomps = image->numcomps;
  276. uint16_t *frame_ptr = (uint16_t *)frame->data[0];
  277. for (compno = 0; compno < numcomps; ++compno) {
  278. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  279. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  280. return 0;
  281. }
  282. }
  283. for (compno = 0; compno < numcomps; ++compno) {
  284. for (y = 0; y < avctx->height; ++y) {
  285. image_line = image->comps[compno].data + y * image->comps[compno].w;
  286. frame_index = y * (frame->linesize[0] / 2) + compno;
  287. for (x = 0; x < avctx->width; ++x) {
  288. image_line[x] = frame_ptr[frame_index] >> 4;
  289. frame_index += numcomps;
  290. }
  291. for (; x < image->comps[compno].w; ++x) {
  292. image_line[x] = image_line[x - 1];
  293. }
  294. }
  295. for (; y < image->comps[compno].h; ++y) {
  296. image_line = image->comps[compno].data + y * image->comps[compno].w;
  297. for (x = 0; x < image->comps[compno].w; ++x) {
  298. image_line[x] = image_line[x - image->comps[compno].w];
  299. }
  300. }
  301. }
  302. return 1;
  303. }
  304. static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  305. {
  306. int compno;
  307. int x;
  308. int y;
  309. int *image_line;
  310. int frame_index;
  311. const int numcomps = image->numcomps;
  312. uint16_t *frame_ptr = (uint16_t*)frame->data[0];
  313. for (compno = 0; compno < numcomps; ++compno) {
  314. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  315. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  316. return 0;
  317. }
  318. }
  319. for (compno = 0; compno < numcomps; ++compno) {
  320. for (y = 0; y < avctx->height; ++y) {
  321. image_line = image->comps[compno].data + y * image->comps[compno].w;
  322. frame_index = y * (frame->linesize[0] / 2) + compno;
  323. for (x = 0; x < avctx->width; ++x) {
  324. image_line[x] = frame_ptr[frame_index];
  325. frame_index += numcomps;
  326. }
  327. for (; x < image->comps[compno].w; ++x) {
  328. image_line[x] = image_line[x - 1];
  329. }
  330. }
  331. for (; y < image->comps[compno].h; ++y) {
  332. image_line = image->comps[compno].data + y * image->comps[compno].w;
  333. for (x = 0; x < image->comps[compno].w; ++x) {
  334. image_line[x] = image_line[x - image->comps[compno].w];
  335. }
  336. }
  337. }
  338. return 1;
  339. }
  340. static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  341. {
  342. int compno;
  343. int x;
  344. int y;
  345. int width;
  346. int height;
  347. int *image_line;
  348. int frame_index;
  349. const int numcomps = image->numcomps;
  350. for (compno = 0; compno < numcomps; ++compno) {
  351. if (image->comps[compno].w > frame->linesize[compno]) {
  352. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  353. return 0;
  354. }
  355. }
  356. for (compno = 0; compno < numcomps; ++compno) {
  357. width = avctx->width / image->comps[compno].dx;
  358. height = avctx->height / image->comps[compno].dy;
  359. for (y = 0; y < height; ++y) {
  360. image_line = image->comps[compno].data + y * image->comps[compno].w;
  361. frame_index = y * frame->linesize[compno];
  362. for (x = 0; x < width; ++x)
  363. image_line[x] = frame->data[compno][frame_index++];
  364. for (; x < image->comps[compno].w; ++x) {
  365. image_line[x] = image_line[x - 1];
  366. }
  367. }
  368. for (; y < image->comps[compno].h; ++y) {
  369. image_line = image->comps[compno].data + y * image->comps[compno].w;
  370. for (x = 0; x < image->comps[compno].w; ++x) {
  371. image_line[x] = image_line[x - image->comps[compno].w];
  372. }
  373. }
  374. }
  375. return 1;
  376. }
  377. static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  378. {
  379. int compno;
  380. int x;
  381. int y;
  382. int width;
  383. int height;
  384. int *image_line;
  385. int frame_index;
  386. const int numcomps = image->numcomps;
  387. uint16_t *frame_ptr;
  388. for (compno = 0; compno < numcomps; ++compno) {
  389. if (image->comps[compno].w > frame->linesize[compno]) {
  390. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  391. return 0;
  392. }
  393. }
  394. for (compno = 0; compno < numcomps; ++compno) {
  395. width = avctx->width / image->comps[compno].dx;
  396. height = avctx->height / image->comps[compno].dy;
  397. frame_ptr = (uint16_t *)frame->data[compno];
  398. for (y = 0; y < height; ++y) {
  399. image_line = image->comps[compno].data + y * image->comps[compno].w;
  400. frame_index = y * (frame->linesize[compno] / 2);
  401. for (x = 0; x < width; ++x)
  402. image_line[x] = frame_ptr[frame_index++];
  403. for (; x < image->comps[compno].w; ++x) {
  404. image_line[x] = image_line[x - 1];
  405. }
  406. }
  407. for (; y < image->comps[compno].h; ++y) {
  408. image_line = image->comps[compno].data + y * image->comps[compno].w;
  409. for (x = 0; x < image->comps[compno].w; ++x) {
  410. image_line[x] = image_line[x - image->comps[compno].w];
  411. }
  412. }
  413. }
  414. return 1;
  415. }
  416. static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  417. const AVFrame *frame, int *got_packet)
  418. {
  419. LibOpenJPEGContext *ctx = avctx->priv_data;
  420. opj_cinfo_t *compress = ctx->compress;
  421. opj_image_t *image = ctx->image;
  422. opj_cio_t *stream = ctx->stream;
  423. int cpyresult = 0;
  424. int ret, len;
  425. AVFrame *gbrframe;
  426. switch (avctx->pix_fmt) {
  427. case AV_PIX_FMT_RGB24:
  428. case AV_PIX_FMT_RGBA:
  429. case AV_PIX_FMT_YA8:
  430. cpyresult = libopenjpeg_copy_packed8(avctx, frame, image);
  431. break;
  432. case AV_PIX_FMT_XYZ12:
  433. cpyresult = libopenjpeg_copy_packed12(avctx, frame, image);
  434. break;
  435. case AV_PIX_FMT_RGB48:
  436. case AV_PIX_FMT_RGBA64:
  437. case AV_PIX_FMT_YA16:
  438. cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
  439. break;
  440. case AV_PIX_FMT_GBR24P:
  441. case AV_PIX_FMT_GBRP9:
  442. case AV_PIX_FMT_GBRP10:
  443. case AV_PIX_FMT_GBRP12:
  444. case AV_PIX_FMT_GBRP14:
  445. case AV_PIX_FMT_GBRP16:
  446. gbrframe = av_frame_clone(frame);
  447. if (!gbrframe)
  448. return AVERROR(ENOMEM);
  449. gbrframe->data[0] = frame->data[2]; // swap to be rgb
  450. gbrframe->data[1] = frame->data[0];
  451. gbrframe->data[2] = frame->data[1];
  452. gbrframe->linesize[0] = frame->linesize[2];
  453. gbrframe->linesize[1] = frame->linesize[0];
  454. gbrframe->linesize[2] = frame->linesize[1];
  455. if (avctx->pix_fmt == AV_PIX_FMT_GBR24P) {
  456. cpyresult = libopenjpeg_copy_unpacked8(avctx, gbrframe, image);
  457. } else {
  458. cpyresult = libopenjpeg_copy_unpacked16(avctx, gbrframe, image);
  459. }
  460. av_frame_free(&gbrframe);
  461. break;
  462. case AV_PIX_FMT_GRAY8:
  463. case AV_PIX_FMT_YUV410P:
  464. case AV_PIX_FMT_YUV411P:
  465. case AV_PIX_FMT_YUV420P:
  466. case AV_PIX_FMT_YUV422P:
  467. case AV_PIX_FMT_YUV440P:
  468. case AV_PIX_FMT_YUV444P:
  469. case AV_PIX_FMT_YUVA420P:
  470. case AV_PIX_FMT_YUVA422P:
  471. case AV_PIX_FMT_YUVA444P:
  472. cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
  473. break;
  474. case AV_PIX_FMT_GRAY16:
  475. case AV_PIX_FMT_YUV420P9:
  476. case AV_PIX_FMT_YUV422P9:
  477. case AV_PIX_FMT_YUV444P9:
  478. case AV_PIX_FMT_YUVA420P9:
  479. case AV_PIX_FMT_YUVA422P9:
  480. case AV_PIX_FMT_YUVA444P9:
  481. case AV_PIX_FMT_YUV444P10:
  482. case AV_PIX_FMT_YUV422P10:
  483. case AV_PIX_FMT_YUV420P10:
  484. case AV_PIX_FMT_YUVA444P10:
  485. case AV_PIX_FMT_YUVA422P10:
  486. case AV_PIX_FMT_YUVA420P10:
  487. case AV_PIX_FMT_YUV420P12:
  488. case AV_PIX_FMT_YUV422P12:
  489. case AV_PIX_FMT_YUV444P12:
  490. case AV_PIX_FMT_YUV420P14:
  491. case AV_PIX_FMT_YUV422P14:
  492. case AV_PIX_FMT_YUV444P14:
  493. case AV_PIX_FMT_YUV444P16:
  494. case AV_PIX_FMT_YUV422P16:
  495. case AV_PIX_FMT_YUV420P16:
  496. case AV_PIX_FMT_YUVA444P16:
  497. case AV_PIX_FMT_YUVA422P16:
  498. case AV_PIX_FMT_YUVA420P16:
  499. cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
  500. break;
  501. default:
  502. av_log(avctx, AV_LOG_ERROR,
  503. "The frame's pixel format '%s' is not supported\n",
  504. av_get_pix_fmt_name(avctx->pix_fmt));
  505. return AVERROR(EINVAL);
  506. break;
  507. }
  508. if (!cpyresult) {
  509. av_log(avctx, AV_LOG_ERROR,
  510. "Could not copy the frame data to the internal image buffer\n");
  511. return -1;
  512. }
  513. opj_setup_encoder(compress, &ctx->enc_params, image);
  514. stream = opj_cio_open((opj_common_ptr) compress, NULL, 0);
  515. if (!stream) {
  516. av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
  517. return AVERROR(ENOMEM);
  518. }
  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. return 0;
  531. }
  532. static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
  533. {
  534. LibOpenJPEGContext *ctx = avctx->priv_data;
  535. opj_cio_close(ctx->stream);
  536. ctx->stream = NULL;
  537. opj_destroy_compress(ctx->compress);
  538. ctx->compress = NULL;
  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. };