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.

887 lines
31KB

  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. #include "libavutil/avassert.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/opt.h"
  30. #include "avcodec.h"
  31. #include "internal.h"
  32. #if HAVE_OPENJPEG_2_1_OPENJPEG_H
  33. # include <openjpeg-2.1/openjpeg.h>
  34. #elif HAVE_OPENJPEG_2_0_OPENJPEG_H
  35. # include <openjpeg-2.0/openjpeg.h>
  36. #elif HAVE_OPENJPEG_1_5_OPENJPEG_H
  37. # include <openjpeg-1.5/openjpeg.h>
  38. #else
  39. # include <openjpeg.h>
  40. #endif
  41. #if HAVE_OPENJPEG_2_1_OPENJPEG_H || HAVE_OPENJPEG_2_0_OPENJPEG_H
  42. # define OPENJPEG_MAJOR_VERSION 2
  43. # define OPJ(x) OPJ_##x
  44. #else
  45. # define OPENJPEG_MAJOR_VERSION 1
  46. # define OPJ(x) x
  47. #endif
  48. typedef struct LibOpenJPEGContext {
  49. AVClass *avclass;
  50. #if OPENJPEG_MAJOR_VERSION == 1
  51. opj_image_t *image;
  52. #endif // OPENJPEG_MAJOR_VERSION == 1
  53. opj_cparameters_t enc_params;
  54. #if OPENJPEG_MAJOR_VERSION == 1
  55. opj_event_mgr_t event_mgr;
  56. #endif // OPENJPEG_MAJOR_VERSION == 1
  57. int format;
  58. int profile;
  59. int prog_order;
  60. int cinema_mode;
  61. int numresolution;
  62. int numlayers;
  63. int disto_alloc;
  64. int fixed_alloc;
  65. int fixed_quality;
  66. } LibOpenJPEGContext;
  67. static void error_callback(const char *msg, void *data)
  68. {
  69. av_log(data, AV_LOG_ERROR, "%s\n", msg);
  70. }
  71. static void warning_callback(const char *msg, void *data)
  72. {
  73. av_log(data, AV_LOG_WARNING, "%s\n", msg);
  74. }
  75. static void info_callback(const char *msg, void *data)
  76. {
  77. av_log(data, AV_LOG_DEBUG, "%s\n", msg);
  78. }
  79. #if OPENJPEG_MAJOR_VERSION == 2
  80. typedef struct PacketWriter {
  81. int pos;
  82. AVPacket *packet;
  83. } PacketWriter;
  84. static OPJ_SIZE_T stream_write(void *out_buffer, OPJ_SIZE_T nb_bytes, void *user_data)
  85. {
  86. PacketWriter *writer = user_data;
  87. AVPacket *packet = writer->packet;
  88. int remaining = packet->size - writer->pos;
  89. if (nb_bytes > remaining) {
  90. OPJ_SIZE_T needed = nb_bytes - remaining;
  91. int max_growth = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - packet->size;
  92. if (needed > max_growth) {
  93. return (OPJ_SIZE_T)-1;
  94. }
  95. if (av_grow_packet(packet, (int)needed)) {
  96. return (OPJ_SIZE_T)-1;
  97. }
  98. }
  99. memcpy(packet->data + writer->pos, out_buffer, nb_bytes);
  100. writer->pos += (int)nb_bytes;
  101. return nb_bytes;
  102. }
  103. static OPJ_OFF_T stream_skip(OPJ_OFF_T nb_bytes, void *user_data)
  104. {
  105. PacketWriter *writer = user_data;
  106. AVPacket *packet = writer->packet;
  107. if (nb_bytes < 0) {
  108. if (writer->pos == 0) {
  109. return (OPJ_SIZE_T)-1;
  110. }
  111. if (nb_bytes + writer->pos < 0) {
  112. nb_bytes = -writer->pos;
  113. }
  114. } else {
  115. int remaining = packet->size - writer->pos;
  116. if (nb_bytes > remaining) {
  117. OPJ_SIZE_T needed = nb_bytes - remaining;
  118. int max_growth = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - packet->size;
  119. if (needed > max_growth) {
  120. return (OPJ_SIZE_T)-1;
  121. }
  122. if (av_grow_packet(packet, (int)needed)) {
  123. return (OPJ_SIZE_T)-1;
  124. }
  125. }
  126. }
  127. writer->pos += (int)nb_bytes;
  128. return nb_bytes;
  129. }
  130. static OPJ_BOOL stream_seek(OPJ_OFF_T nb_bytes, void *user_data)
  131. {
  132. PacketWriter *writer = user_data;
  133. AVPacket *packet = writer->packet;
  134. if (nb_bytes < 0) {
  135. return OPJ_FALSE;
  136. }
  137. if (nb_bytes > packet->size) {
  138. if (nb_bytes > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE ||
  139. av_grow_packet(packet, (int)nb_bytes - packet->size)) {
  140. return OPJ_FALSE;
  141. }
  142. }
  143. writer->pos = (int)nb_bytes;
  144. return OPJ_TRUE;
  145. }
  146. #endif // OPENJPEG_MAJOR_VERSION == 2
  147. static void cinema_parameters(opj_cparameters_t *p)
  148. {
  149. p->tile_size_on = 0;
  150. p->cp_tdx = 1;
  151. p->cp_tdy = 1;
  152. /* Tile part */
  153. p->tp_flag = 'C';
  154. p->tp_on = 1;
  155. /* Tile and Image shall be at (0, 0) */
  156. p->cp_tx0 = 0;
  157. p->cp_ty0 = 0;
  158. p->image_offset_x0 = 0;
  159. p->image_offset_y0 = 0;
  160. /* Codeblock size= 32 * 32 */
  161. p->cblockw_init = 32;
  162. p->cblockh_init = 32;
  163. p->csty |= 0x01;
  164. /* The progression order shall be CPRL */
  165. p->prog_order = OPJ(CPRL);
  166. /* No ROI */
  167. p->roi_compno = -1;
  168. /* No subsampling */
  169. p->subsampling_dx = 1;
  170. p->subsampling_dy = 1;
  171. /* 9-7 transform */
  172. p->irreversible = 1;
  173. p->tcp_mct = 1;
  174. }
  175. static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
  176. {
  177. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  178. opj_image_cmptparm_t cmptparm[4] = {{0}};
  179. opj_image_t *img;
  180. int i;
  181. int sub_dx[4];
  182. int sub_dy[4];
  183. int numcomps;
  184. OPJ_COLOR_SPACE color_space = OPJ(CLRSPC_UNKNOWN);
  185. sub_dx[0] = sub_dx[3] = 1;
  186. sub_dy[0] = sub_dy[3] = 1;
  187. sub_dx[1] = sub_dx[2] = 1 << desc->log2_chroma_w;
  188. sub_dy[1] = sub_dy[2] = 1 << desc->log2_chroma_h;
  189. numcomps = desc->nb_components;
  190. switch (avctx->pix_fmt) {
  191. case AV_PIX_FMT_GRAY8:
  192. case AV_PIX_FMT_YA8:
  193. case AV_PIX_FMT_GRAY16:
  194. case AV_PIX_FMT_YA16:
  195. color_space = OPJ(CLRSPC_GRAY);
  196. break;
  197. case AV_PIX_FMT_RGB24:
  198. case AV_PIX_FMT_RGBA:
  199. case AV_PIX_FMT_RGB48:
  200. case AV_PIX_FMT_RGBA64:
  201. case AV_PIX_FMT_GBR24P:
  202. case AV_PIX_FMT_GBRP9:
  203. case AV_PIX_FMT_GBRP10:
  204. case AV_PIX_FMT_GBRP12:
  205. case AV_PIX_FMT_GBRP14:
  206. case AV_PIX_FMT_GBRP16:
  207. case AV_PIX_FMT_XYZ12:
  208. color_space = OPJ(CLRSPC_SRGB);
  209. break;
  210. case AV_PIX_FMT_YUV410P:
  211. case AV_PIX_FMT_YUV411P:
  212. case AV_PIX_FMT_YUV420P:
  213. case AV_PIX_FMT_YUV422P:
  214. case AV_PIX_FMT_YUV440P:
  215. case AV_PIX_FMT_YUV444P:
  216. case AV_PIX_FMT_YUVA420P:
  217. case AV_PIX_FMT_YUVA422P:
  218. case AV_PIX_FMT_YUVA444P:
  219. case AV_PIX_FMT_YUV420P9:
  220. case AV_PIX_FMT_YUV422P9:
  221. case AV_PIX_FMT_YUV444P9:
  222. case AV_PIX_FMT_YUVA420P9:
  223. case AV_PIX_FMT_YUVA422P9:
  224. case AV_PIX_FMT_YUVA444P9:
  225. case AV_PIX_FMT_YUV420P10:
  226. case AV_PIX_FMT_YUV422P10:
  227. case AV_PIX_FMT_YUV444P10:
  228. case AV_PIX_FMT_YUVA420P10:
  229. case AV_PIX_FMT_YUVA422P10:
  230. case AV_PIX_FMT_YUVA444P10:
  231. case AV_PIX_FMT_YUV420P12:
  232. case AV_PIX_FMT_YUV422P12:
  233. case AV_PIX_FMT_YUV444P12:
  234. case AV_PIX_FMT_YUV420P14:
  235. case AV_PIX_FMT_YUV422P14:
  236. case AV_PIX_FMT_YUV444P14:
  237. case AV_PIX_FMT_YUV420P16:
  238. case AV_PIX_FMT_YUV422P16:
  239. case AV_PIX_FMT_YUV444P16:
  240. case AV_PIX_FMT_YUVA420P16:
  241. case AV_PIX_FMT_YUVA422P16:
  242. case AV_PIX_FMT_YUVA444P16:
  243. color_space = OPJ(CLRSPC_SYCC);
  244. break;
  245. default:
  246. av_log(avctx, AV_LOG_ERROR,
  247. "The requested pixel format '%s' is not supported\n",
  248. av_get_pix_fmt_name(avctx->pix_fmt));
  249. return NULL;
  250. }
  251. for (i = 0; i < numcomps; i++) {
  252. cmptparm[i].prec = desc->comp[i].depth;
  253. cmptparm[i].bpp = desc->comp[i].depth;
  254. cmptparm[i].sgnd = 0;
  255. cmptparm[i].dx = sub_dx[i];
  256. cmptparm[i].dy = sub_dy[i];
  257. cmptparm[i].w = (avctx->width + sub_dx[i] - 1) / sub_dx[i];
  258. cmptparm[i].h = (avctx->height + sub_dy[i] - 1) / sub_dy[i];
  259. }
  260. img = opj_image_create(numcomps, cmptparm, color_space);
  261. if (!img)
  262. return NULL;
  263. // x0, y0 is the top left corner of the image
  264. // x1, y1 is the width, height of the reference grid
  265. img->x0 = 0;
  266. img->y0 = 0;
  267. img->x1 = (avctx->width - 1) * parameters->subsampling_dx + 1;
  268. img->y1 = (avctx->height - 1) * parameters->subsampling_dy + 1;
  269. return img;
  270. }
  271. static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
  272. {
  273. LibOpenJPEGContext *ctx = avctx->priv_data;
  274. int err = 0;
  275. opj_set_default_encoder_parameters(&ctx->enc_params);
  276. #if HAVE_OPENJPEG_2_1_OPENJPEG_H
  277. switch (ctx->cinema_mode) {
  278. case OPJ_CINEMA2K_24:
  279. ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
  280. ctx->enc_params.max_cs_size = OPJ_CINEMA_24_CS;
  281. ctx->enc_params.max_comp_size = OPJ_CINEMA_24_COMP;
  282. break;
  283. case OPJ_CINEMA2K_48:
  284. ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
  285. ctx->enc_params.max_cs_size = OPJ_CINEMA_48_CS;
  286. ctx->enc_params.max_comp_size = OPJ_CINEMA_48_COMP;
  287. break;
  288. case OPJ_CINEMA4K_24:
  289. ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_4K;
  290. ctx->enc_params.max_cs_size = OPJ_CINEMA_24_CS;
  291. ctx->enc_params.max_comp_size = OPJ_CINEMA_24_COMP;
  292. break;
  293. }
  294. switch (ctx->profile) {
  295. case OPJ_CINEMA2K:
  296. if (ctx->enc_params.rsiz == OPJ_PROFILE_CINEMA_4K) {
  297. err = AVERROR(EINVAL);
  298. break;
  299. }
  300. ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
  301. break;
  302. case OPJ_CINEMA4K:
  303. if (ctx->enc_params.rsiz == OPJ_PROFILE_CINEMA_2K) {
  304. err = AVERROR(EINVAL);
  305. break;
  306. }
  307. ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_4K;
  308. break;
  309. }
  310. if (err) {
  311. av_log(avctx, AV_LOG_ERROR,
  312. "Invalid parameter pairing: cinema_mode and profile conflict.\n");
  313. goto fail;
  314. }
  315. #else
  316. ctx->enc_params.cp_rsiz = ctx->profile;
  317. ctx->enc_params.cp_cinema = ctx->cinema_mode;
  318. #endif
  319. if (!ctx->numresolution) {
  320. ctx->numresolution = 6;
  321. while (FFMIN(avctx->width, avctx->height) >> ctx->numresolution < 1)
  322. ctx->numresolution --;
  323. }
  324. ctx->enc_params.mode = !!avctx->global_quality;
  325. ctx->enc_params.prog_order = ctx->prog_order;
  326. ctx->enc_params.numresolution = ctx->numresolution;
  327. ctx->enc_params.cp_disto_alloc = ctx->disto_alloc;
  328. ctx->enc_params.cp_fixed_alloc = ctx->fixed_alloc;
  329. ctx->enc_params.cp_fixed_quality = ctx->fixed_quality;
  330. ctx->enc_params.tcp_numlayers = ctx->numlayers;
  331. ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
  332. if (ctx->cinema_mode > 0) {
  333. cinema_parameters(&ctx->enc_params);
  334. }
  335. #if OPENJPEG_MAJOR_VERSION == 1
  336. ctx->image = mj2_create_image(avctx, &ctx->enc_params);
  337. if (!ctx->image) {
  338. av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
  339. err = AVERROR(EINVAL);
  340. goto fail;
  341. }
  342. #endif // OPENJPEG_MAJOR_VERSION == 1
  343. return 0;
  344. fail:
  345. #if OPENJPEG_MAJOR_VERSION == 1
  346. opj_image_destroy(ctx->image);
  347. ctx->image = NULL;
  348. #endif // OPENJPEG_MAJOR_VERSION == 1
  349. return err;
  350. }
  351. static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  352. {
  353. int compno;
  354. int x;
  355. int y;
  356. int *image_line;
  357. int frame_index;
  358. const int numcomps = image->numcomps;
  359. for (compno = 0; compno < numcomps; ++compno) {
  360. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  361. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  362. return 0;
  363. }
  364. }
  365. for (compno = 0; compno < numcomps; ++compno) {
  366. for (y = 0; y < avctx->height; ++y) {
  367. image_line = image->comps[compno].data + y * image->comps[compno].w;
  368. frame_index = y * frame->linesize[0] + compno;
  369. for (x = 0; x < avctx->width; ++x) {
  370. image_line[x] = frame->data[0][frame_index];
  371. frame_index += numcomps;
  372. }
  373. for (; x < image->comps[compno].w; ++x) {
  374. image_line[x] = image_line[x - 1];
  375. }
  376. }
  377. for (; y < image->comps[compno].h; ++y) {
  378. image_line = image->comps[compno].data + y * image->comps[compno].w;
  379. for (x = 0; x < image->comps[compno].w; ++x) {
  380. image_line[x] = image_line[x - (int)image->comps[compno].w];
  381. }
  382. }
  383. }
  384. return 1;
  385. }
  386. // for XYZ 12 bit
  387. static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  388. {
  389. int compno;
  390. int x, y;
  391. int *image_line;
  392. int frame_index;
  393. const int numcomps = image->numcomps;
  394. uint16_t *frame_ptr = (uint16_t *)frame->data[0];
  395. for (compno = 0; compno < numcomps; ++compno) {
  396. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  397. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  398. return 0;
  399. }
  400. }
  401. for (compno = 0; compno < numcomps; ++compno) {
  402. for (y = 0; y < avctx->height; ++y) {
  403. image_line = image->comps[compno].data + y * image->comps[compno].w;
  404. frame_index = y * (frame->linesize[0] / 2) + compno;
  405. for (x = 0; x < avctx->width; ++x) {
  406. image_line[x] = frame_ptr[frame_index] >> 4;
  407. frame_index += numcomps;
  408. }
  409. for (; x < image->comps[compno].w; ++x) {
  410. image_line[x] = image_line[x - 1];
  411. }
  412. }
  413. for (; y < image->comps[compno].h; ++y) {
  414. image_line = image->comps[compno].data + y * image->comps[compno].w;
  415. for (x = 0; x < image->comps[compno].w; ++x) {
  416. image_line[x] = image_line[x - (int)image->comps[compno].w];
  417. }
  418. }
  419. }
  420. return 1;
  421. }
  422. static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  423. {
  424. int compno;
  425. int x;
  426. int y;
  427. int *image_line;
  428. int frame_index;
  429. const int numcomps = image->numcomps;
  430. uint16_t *frame_ptr = (uint16_t*)frame->data[0];
  431. for (compno = 0; compno < numcomps; ++compno) {
  432. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  433. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  434. return 0;
  435. }
  436. }
  437. for (compno = 0; compno < numcomps; ++compno) {
  438. for (y = 0; y < avctx->height; ++y) {
  439. image_line = image->comps[compno].data + y * image->comps[compno].w;
  440. frame_index = y * (frame->linesize[0] / 2) + compno;
  441. for (x = 0; x < avctx->width; ++x) {
  442. image_line[x] = frame_ptr[frame_index];
  443. frame_index += numcomps;
  444. }
  445. for (; x < image->comps[compno].w; ++x) {
  446. image_line[x] = image_line[x - 1];
  447. }
  448. }
  449. for (; y < image->comps[compno].h; ++y) {
  450. image_line = image->comps[compno].data + y * image->comps[compno].w;
  451. for (x = 0; x < image->comps[compno].w; ++x) {
  452. image_line[x] = image_line[x - (int)image->comps[compno].w];
  453. }
  454. }
  455. }
  456. return 1;
  457. }
  458. static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  459. {
  460. int compno;
  461. int x;
  462. int y;
  463. int width;
  464. int height;
  465. int *image_line;
  466. int frame_index;
  467. const int numcomps = image->numcomps;
  468. for (compno = 0; compno < numcomps; ++compno) {
  469. if (image->comps[compno].w > frame->linesize[compno]) {
  470. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  471. return 0;
  472. }
  473. }
  474. for (compno = 0; compno < numcomps; ++compno) {
  475. width = (avctx->width + image->comps[compno].dx - 1) / image->comps[compno].dx;
  476. height = (avctx->height + image->comps[compno].dy - 1) / image->comps[compno].dy;
  477. for (y = 0; y < height; ++y) {
  478. image_line = image->comps[compno].data + y * image->comps[compno].w;
  479. frame_index = y * frame->linesize[compno];
  480. for (x = 0; x < width; ++x)
  481. image_line[x] = frame->data[compno][frame_index++];
  482. for (; x < image->comps[compno].w; ++x) {
  483. image_line[x] = image_line[x - 1];
  484. }
  485. }
  486. for (; y < image->comps[compno].h; ++y) {
  487. image_line = image->comps[compno].data + y * image->comps[compno].w;
  488. for (x = 0; x < image->comps[compno].w; ++x) {
  489. image_line[x] = image_line[x - (int)image->comps[compno].w];
  490. }
  491. }
  492. }
  493. return 1;
  494. }
  495. static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  496. {
  497. int compno;
  498. int x;
  499. int y;
  500. int width;
  501. int height;
  502. int *image_line;
  503. int frame_index;
  504. const int numcomps = image->numcomps;
  505. uint16_t *frame_ptr;
  506. for (compno = 0; compno < numcomps; ++compno) {
  507. if (image->comps[compno].w > frame->linesize[compno]) {
  508. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  509. return 0;
  510. }
  511. }
  512. for (compno = 0; compno < numcomps; ++compno) {
  513. width = (avctx->width + image->comps[compno].dx - 1) / image->comps[compno].dx;
  514. height = (avctx->height + image->comps[compno].dy - 1) / image->comps[compno].dy;
  515. frame_ptr = (uint16_t *)frame->data[compno];
  516. for (y = 0; y < height; ++y) {
  517. image_line = image->comps[compno].data + y * image->comps[compno].w;
  518. frame_index = y * (frame->linesize[compno] / 2);
  519. for (x = 0; x < width; ++x)
  520. image_line[x] = frame_ptr[frame_index++];
  521. for (; x < image->comps[compno].w; ++x) {
  522. image_line[x] = image_line[x - 1];
  523. }
  524. }
  525. for (; y < image->comps[compno].h; ++y) {
  526. image_line = image->comps[compno].data + y * image->comps[compno].w;
  527. for (x = 0; x < image->comps[compno].w; ++x) {
  528. image_line[x] = image_line[x - (int)image->comps[compno].w];
  529. }
  530. }
  531. }
  532. return 1;
  533. }
  534. static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  535. const AVFrame *frame, int *got_packet)
  536. {
  537. LibOpenJPEGContext *ctx = avctx->priv_data;
  538. int ret;
  539. AVFrame *gbrframe;
  540. int cpyresult = 0;
  541. #if OPENJPEG_MAJOR_VERSION == 1
  542. opj_image_t *image = ctx->image;
  543. opj_cinfo_t *compress = NULL;
  544. opj_cio_t *stream = NULL;
  545. int len;
  546. #else // OPENJPEG_MAJOR_VERSION == 2
  547. PacketWriter writer = { 0 };
  548. opj_codec_t *compress = NULL;
  549. opj_stream_t *stream = NULL;
  550. opj_image_t *image = mj2_create_image(avctx, &ctx->enc_params);
  551. if (!image) {
  552. av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
  553. ret = AVERROR(EINVAL);
  554. goto done;
  555. }
  556. #endif // OPENJPEG_MAJOR_VERSION == 1
  557. switch (avctx->pix_fmt) {
  558. case AV_PIX_FMT_RGB24:
  559. case AV_PIX_FMT_RGBA:
  560. case AV_PIX_FMT_YA8:
  561. cpyresult = libopenjpeg_copy_packed8(avctx, frame, image);
  562. break;
  563. case AV_PIX_FMT_XYZ12:
  564. cpyresult = libopenjpeg_copy_packed12(avctx, frame, image);
  565. break;
  566. case AV_PIX_FMT_RGB48:
  567. case AV_PIX_FMT_RGBA64:
  568. case AV_PIX_FMT_YA16:
  569. cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
  570. break;
  571. case AV_PIX_FMT_GBR24P:
  572. case AV_PIX_FMT_GBRP9:
  573. case AV_PIX_FMT_GBRP10:
  574. case AV_PIX_FMT_GBRP12:
  575. case AV_PIX_FMT_GBRP14:
  576. case AV_PIX_FMT_GBRP16:
  577. gbrframe = av_frame_clone(frame);
  578. if (!gbrframe) {
  579. ret = AVERROR(ENOMEM);
  580. goto done;
  581. }
  582. gbrframe->data[0] = frame->data[2]; // swap to be rgb
  583. gbrframe->data[1] = frame->data[0];
  584. gbrframe->data[2] = frame->data[1];
  585. gbrframe->linesize[0] = frame->linesize[2];
  586. gbrframe->linesize[1] = frame->linesize[0];
  587. gbrframe->linesize[2] = frame->linesize[1];
  588. if (avctx->pix_fmt == AV_PIX_FMT_GBR24P) {
  589. cpyresult = libopenjpeg_copy_unpacked8(avctx, gbrframe, image);
  590. } else {
  591. cpyresult = libopenjpeg_copy_unpacked16(avctx, gbrframe, image);
  592. }
  593. av_frame_free(&gbrframe);
  594. break;
  595. case AV_PIX_FMT_GRAY8:
  596. case AV_PIX_FMT_YUV410P:
  597. case AV_PIX_FMT_YUV411P:
  598. case AV_PIX_FMT_YUV420P:
  599. case AV_PIX_FMT_YUV422P:
  600. case AV_PIX_FMT_YUV440P:
  601. case AV_PIX_FMT_YUV444P:
  602. case AV_PIX_FMT_YUVA420P:
  603. case AV_PIX_FMT_YUVA422P:
  604. case AV_PIX_FMT_YUVA444P:
  605. cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
  606. break;
  607. case AV_PIX_FMT_GRAY16:
  608. case AV_PIX_FMT_YUV420P9:
  609. case AV_PIX_FMT_YUV422P9:
  610. case AV_PIX_FMT_YUV444P9:
  611. case AV_PIX_FMT_YUVA420P9:
  612. case AV_PIX_FMT_YUVA422P9:
  613. case AV_PIX_FMT_YUVA444P9:
  614. case AV_PIX_FMT_YUV444P10:
  615. case AV_PIX_FMT_YUV422P10:
  616. case AV_PIX_FMT_YUV420P10:
  617. case AV_PIX_FMT_YUVA444P10:
  618. case AV_PIX_FMT_YUVA422P10:
  619. case AV_PIX_FMT_YUVA420P10:
  620. case AV_PIX_FMT_YUV420P12:
  621. case AV_PIX_FMT_YUV422P12:
  622. case AV_PIX_FMT_YUV444P12:
  623. case AV_PIX_FMT_YUV420P14:
  624. case AV_PIX_FMT_YUV422P14:
  625. case AV_PIX_FMT_YUV444P14:
  626. case AV_PIX_FMT_YUV444P16:
  627. case AV_PIX_FMT_YUV422P16:
  628. case AV_PIX_FMT_YUV420P16:
  629. case AV_PIX_FMT_YUVA444P16:
  630. case AV_PIX_FMT_YUVA422P16:
  631. case AV_PIX_FMT_YUVA420P16:
  632. cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
  633. break;
  634. default:
  635. av_log(avctx, AV_LOG_ERROR,
  636. "The frame's pixel format '%s' is not supported\n",
  637. av_get_pix_fmt_name(avctx->pix_fmt));
  638. ret = AVERROR(EINVAL);
  639. goto done;
  640. break;
  641. }
  642. if (!cpyresult) {
  643. av_log(avctx, AV_LOG_ERROR,
  644. "Could not copy the frame data to the internal image buffer\n");
  645. ret = -1;
  646. goto done;
  647. }
  648. #if OPENJPEG_MAJOR_VERSION == 2
  649. if ((ret = ff_alloc_packet2(avctx, pkt, 1024, 0)) < 0) {
  650. goto done;
  651. }
  652. #endif // OPENJPEG_MAJOR_VERSION == 2
  653. compress = opj_create_compress(ctx->format);
  654. if (!compress) {
  655. av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
  656. ret = AVERROR(ENOMEM);
  657. goto done;
  658. }
  659. #if OPENJPEG_MAJOR_VERSION == 1
  660. opj_setup_encoder(compress, &ctx->enc_params, image);
  661. stream = opj_cio_open((opj_common_ptr) compress, NULL, 0);
  662. #else // OPENJPEG_MAJOR_VERSION == 2
  663. if (!opj_set_error_handler(compress, error_callback, avctx) ||
  664. !opj_set_warning_handler(compress, warning_callback, avctx) ||
  665. !opj_set_info_handler(compress, info_callback, avctx)) {
  666. av_log(avctx, AV_LOG_ERROR, "Error setting the compressor handlers\n");
  667. ret = AVERROR_EXTERNAL;
  668. goto done;
  669. }
  670. if (!opj_setup_encoder(compress, &ctx->enc_params, image)) {
  671. av_log(avctx, AV_LOG_ERROR, "Error setting up the compressor\n");
  672. ret = AVERROR_EXTERNAL;
  673. goto done;
  674. }
  675. stream = opj_stream_default_create(OPJ_STREAM_WRITE);
  676. #endif // OPENJPEG_MAJOR_VERSION == 1
  677. if (!stream) {
  678. av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
  679. ret = AVERROR(ENOMEM);
  680. goto done;
  681. }
  682. #if OPENJPEG_MAJOR_VERSION == 1
  683. memset(&ctx->event_mgr, 0, sizeof(ctx->event_mgr));
  684. ctx->event_mgr.info_handler = info_callback;
  685. ctx->event_mgr.error_handler = error_callback;
  686. ctx->event_mgr.warning_handler = warning_callback;
  687. opj_set_event_mgr((opj_common_ptr) compress, &ctx->event_mgr, avctx);
  688. if (!opj_encode(compress, stream, image, NULL)) {
  689. av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
  690. ret = AVERROR_EXTERNAL;
  691. goto done;
  692. }
  693. len = cio_tell(stream);
  694. if ((ret = ff_alloc_packet2(avctx, pkt, len, 0)) < 0) {
  695. goto done;
  696. }
  697. memcpy(pkt->data, stream->buffer, len);
  698. #else // OPENJPEG_MAJOR_VERSION == 2
  699. writer.packet = pkt;
  700. opj_stream_set_write_function(stream, stream_write);
  701. opj_stream_set_skip_function(stream, stream_skip);
  702. opj_stream_set_seek_function(stream, stream_seek);
  703. #if HAVE_OPENJPEG_2_1_OPENJPEG_H
  704. opj_stream_set_user_data(stream, &writer, NULL);
  705. #elif HAVE_OPENJPEG_2_0_OPENJPEG_H
  706. opj_stream_set_user_data(stream, &writer);
  707. #else
  708. #error Missing call to opj_stream_set_user_data
  709. #endif
  710. if (!opj_start_compress(compress, image, stream) ||
  711. !opj_encode(compress, stream) ||
  712. !opj_end_compress(compress, stream)) {
  713. av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
  714. ret = AVERROR_EXTERNAL;
  715. goto done;
  716. }
  717. av_shrink_packet(pkt, writer.pos);
  718. #endif // OPENJPEG_MAJOR_VERSION == 1
  719. pkt->flags |= AV_PKT_FLAG_KEY;
  720. *got_packet = 1;
  721. ret = 0;
  722. done:
  723. #if OPENJPEG_MAJOR_VERSION == 2
  724. opj_stream_destroy(stream);
  725. opj_destroy_codec(compress);
  726. opj_image_destroy(image);
  727. #else
  728. opj_cio_close(stream);
  729. opj_destroy_compress(compress);
  730. #endif
  731. return ret;
  732. }
  733. static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
  734. {
  735. #if OPENJPEG_MAJOR_VERSION == 1
  736. LibOpenJPEGContext *ctx = avctx->priv_data;
  737. opj_image_destroy(ctx->image);
  738. ctx->image = NULL;
  739. #endif // OPENJPEG_MAJOR_VERSION == 1
  740. return 0;
  741. }
  742. #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
  743. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  744. static const AVOption options[] = {
  745. { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = OPJ(CODEC_JP2) }, OPJ(CODEC_J2K), OPJ(CODEC_JP2), VE, "format" },
  746. { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CODEC_J2K) }, 0, 0, VE, "format" },
  747. { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CODEC_JP2) }, 0, 0, VE, "format" },
  748. { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = OPJ(STD_RSIZ) }, OPJ(STD_RSIZ), OPJ(CINEMA4K), VE, "profile" },
  749. { "jpeg2000", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(STD_RSIZ) }, 0, 0, VE, "profile" },
  750. { "cinema2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA2K) }, 0, 0, VE, "profile" },
  751. { "cinema4k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA4K) }, 0, 0, VE, "profile" },
  752. { "cinema_mode", "Digital Cinema", OFFSET(cinema_mode), AV_OPT_TYPE_INT, { .i64 = OPJ(OFF) }, OPJ(OFF), OPJ(CINEMA4K_24), VE, "cinema_mode" },
  753. { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(OFF) }, 0, 0, VE, "cinema_mode" },
  754. { "2k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA2K_24) }, 0, 0, VE, "cinema_mode" },
  755. { "2k_48", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA2K_48) }, 0, 0, VE, "cinema_mode" },
  756. { "4k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA4K_24) }, 0, 0, VE, "cinema_mode" },
  757. { "prog_order", "Progression Order", OFFSET(prog_order), AV_OPT_TYPE_INT, { .i64 = OPJ(LRCP) }, OPJ(LRCP), OPJ(CPRL), VE, "prog_order" },
  758. { "lrcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(LRCP) }, 0, 0, VE, "prog_order" },
  759. { "rlcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(RLCP) }, 0, 0, VE, "prog_order" },
  760. { "rpcl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(RPCL) }, 0, 0, VE, "prog_order" },
  761. { "pcrl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(PCRL) }, 0, 0, VE, "prog_order" },
  762. { "cprl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CPRL) }, 0, 0, VE, "prog_order" },
  763. { "numresolution", NULL, OFFSET(numresolution), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
  764. { "numlayers", NULL, OFFSET(numlayers), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 10, VE },
  765. { "disto_alloc", NULL, OFFSET(disto_alloc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
  766. { "fixed_alloc", NULL, OFFSET(fixed_alloc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  767. { "fixed_quality", NULL, OFFSET(fixed_quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  768. { NULL },
  769. };
  770. static const AVClass openjpeg_class = {
  771. .class_name = "libopenjpeg",
  772. .item_name = av_default_item_name,
  773. .option = options,
  774. .version = LIBAVUTIL_VERSION_INT,
  775. };
  776. AVCodec ff_libopenjpeg_encoder = {
  777. .name = "libopenjpeg",
  778. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
  779. .type = AVMEDIA_TYPE_VIDEO,
  780. .id = AV_CODEC_ID_JPEG2000,
  781. .priv_data_size = sizeof(LibOpenJPEGContext),
  782. .init = libopenjpeg_encode_init,
  783. .encode2 = libopenjpeg_encode_frame,
  784. .close = libopenjpeg_encode_close,
  785. .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_INTRA_ONLY,
  786. .pix_fmts = (const enum AVPixelFormat[]) {
  787. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB48,
  788. AV_PIX_FMT_RGBA64, AV_PIX_FMT_GBR24P,
  789. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  790. AV_PIX_FMT_GRAY8, AV_PIX_FMT_YA8, AV_PIX_FMT_GRAY16, AV_PIX_FMT_YA16,
  791. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P,
  792. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA422P,
  793. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA444P,
  794. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  795. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  796. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  797. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  798. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
  799. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  800. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  801. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  802. AV_PIX_FMT_XYZ12,
  803. AV_PIX_FMT_NONE
  804. },
  805. .priv_class = &openjpeg_class,
  806. };