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.

783 lines
27KB

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