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.

870 lines
30KB

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