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.

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