libopenjpegenc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * JPEG 2000 encoding support via OpenJPEG
  3. * Copyright (c) 2011 Michael Bradshaw <mbradshaw@sorensonmedia.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 <openjpeg.h>
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/common.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/intreadwrite.h"
  31. #include "libavutil/opt.h"
  32. #include "avcodec.h"
  33. #include "internal.h"
  34. typedef struct {
  35. AVClass *avclass;
  36. opj_image_t *image;
  37. opj_cparameters_t enc_params;
  38. opj_cinfo_t *compress;
  39. opj_event_mgr_t event_mgr;
  40. int format;
  41. int profile;
  42. int prog_order;
  43. int cinema_mode;
  44. int numresolution;
  45. int numlayers;
  46. int disto_alloc;
  47. int fixed_alloc;
  48. int fixed_quality;
  49. } LibOpenJPEGContext;
  50. static void error_callback(const char *msg, void *data)
  51. {
  52. av_log(data, AV_LOG_ERROR, "%s\n", msg);
  53. }
  54. static void warning_callback(const char *msg, void *data)
  55. {
  56. av_log(data, AV_LOG_WARNING, "%s\n", msg);
  57. }
  58. static void info_callback(const char *msg, void *data)
  59. {
  60. av_log(data, AV_LOG_DEBUG, "%s\n", msg);
  61. }
  62. static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
  63. {
  64. opj_image_cmptparm_t *cmptparm;
  65. opj_image_t *img;
  66. int i;
  67. int sub_dx[4];
  68. int sub_dy[4];
  69. int numcomps;
  70. OPJ_COLOR_SPACE color_space = CLRSPC_UNKNOWN;
  71. sub_dx[0] = sub_dx[3] = 1;
  72. sub_dy[0] = sub_dy[3] = 1;
  73. sub_dx[1] = sub_dx[2] = 1<<av_pix_fmt_descriptors[avctx->pix_fmt].log2_chroma_w;
  74. sub_dy[1] = sub_dy[2] = 1<<av_pix_fmt_descriptors[avctx->pix_fmt].log2_chroma_h;
  75. numcomps = av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
  76. switch (avctx->pix_fmt) {
  77. case PIX_FMT_GRAY8:
  78. case PIX_FMT_GRAY8A:
  79. case PIX_FMT_GRAY16:
  80. color_space = CLRSPC_GRAY;
  81. break;
  82. case PIX_FMT_RGB24:
  83. case PIX_FMT_RGBA:
  84. case PIX_FMT_RGB48:
  85. case PIX_FMT_RGBA64:
  86. color_space = CLRSPC_SRGB;
  87. break;
  88. case PIX_FMT_YUV410P:
  89. case PIX_FMT_YUV411P:
  90. case PIX_FMT_YUV420P:
  91. case PIX_FMT_YUV422P:
  92. case PIX_FMT_YUV440P:
  93. case PIX_FMT_YUV444P:
  94. case PIX_FMT_YUVA420P:
  95. case PIX_FMT_YUVA422P:
  96. case PIX_FMT_YUVA444P:
  97. case PIX_FMT_YUV420P9:
  98. case PIX_FMT_YUV422P9:
  99. case PIX_FMT_YUV444P9:
  100. case PIX_FMT_YUV420P10:
  101. case PIX_FMT_YUV422P10:
  102. case PIX_FMT_YUV444P10:
  103. case PIX_FMT_YUV420P12:
  104. case PIX_FMT_YUV422P12:
  105. case PIX_FMT_YUV444P12:
  106. case PIX_FMT_YUV420P14:
  107. case PIX_FMT_YUV422P14:
  108. case PIX_FMT_YUV444P14:
  109. case PIX_FMT_YUV420P16:
  110. case PIX_FMT_YUV422P16:
  111. case PIX_FMT_YUV444P16:
  112. color_space = CLRSPC_SYCC;
  113. break;
  114. default:
  115. av_log(avctx, AV_LOG_ERROR,
  116. "The requested pixel format '%s' is not supported\n",
  117. av_get_pix_fmt_name(avctx->pix_fmt));
  118. return NULL;
  119. }
  120. cmptparm = av_mallocz(numcomps * sizeof(*cmptparm));
  121. if (!cmptparm) {
  122. av_log(avctx, AV_LOG_ERROR, "Not enough memory\n");
  123. return NULL;
  124. }
  125. for (i = 0; i < numcomps; i++) {
  126. cmptparm[i].prec = av_pix_fmt_descriptors[avctx->pix_fmt].comp[i].depth_minus1 + 1;
  127. cmptparm[i].bpp = av_pix_fmt_descriptors[avctx->pix_fmt].comp[i].depth_minus1 + 1;
  128. cmptparm[i].sgnd = 0;
  129. cmptparm[i].dx = sub_dx[i];
  130. cmptparm[i].dy = sub_dy[i];
  131. cmptparm[i].w = avctx->width / sub_dx[i];
  132. cmptparm[i].h = avctx->height / sub_dy[i];
  133. }
  134. img = opj_image_create(numcomps, cmptparm, color_space);
  135. av_freep(&cmptparm);
  136. return img;
  137. }
  138. static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
  139. {
  140. LibOpenJPEGContext *ctx = avctx->priv_data;
  141. int err = AVERROR(ENOMEM);
  142. opj_set_default_encoder_parameters(&ctx->enc_params);
  143. ctx->enc_params.cp_rsiz = ctx->profile;
  144. ctx->enc_params.mode = !!avctx->global_quality;
  145. ctx->enc_params.cp_cinema = ctx->cinema_mode;
  146. ctx->enc_params.prog_order = ctx->prog_order;
  147. ctx->enc_params.numresolution = ctx->numresolution;
  148. ctx->enc_params.cp_disto_alloc = ctx->disto_alloc;
  149. ctx->enc_params.cp_fixed_alloc = ctx->fixed_alloc;
  150. ctx->enc_params.cp_fixed_quality = ctx->fixed_quality;
  151. ctx->enc_params.tcp_numlayers = ctx->numlayers;
  152. ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
  153. ctx->compress = opj_create_compress(ctx->format);
  154. if (!ctx->compress) {
  155. av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
  156. return AVERROR(ENOMEM);
  157. }
  158. avctx->coded_frame = avcodec_alloc_frame();
  159. if (!avctx->coded_frame) {
  160. av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
  161. goto fail;
  162. }
  163. ctx->image = mj2_create_image(avctx, &ctx->enc_params);
  164. if (!ctx->image) {
  165. av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
  166. err = AVERROR(EINVAL);
  167. goto fail;
  168. }
  169. memset(&ctx->event_mgr, 0, sizeof(opj_event_mgr_t));
  170. ctx->event_mgr.info_handler = info_callback;
  171. ctx->event_mgr.error_handler = error_callback;
  172. ctx->event_mgr.warning_handler = warning_callback;
  173. opj_set_event_mgr((opj_common_ptr)ctx->compress, &ctx->event_mgr, avctx);
  174. return 0;
  175. fail:
  176. av_freep(&ctx->compress);
  177. av_freep(&avctx->coded_frame);
  178. return err;
  179. }
  180. static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  181. {
  182. int compno;
  183. int x;
  184. int y;
  185. int image_index;
  186. int frame_index;
  187. const int numcomps = image->numcomps;
  188. for (compno = 0; compno < numcomps; ++compno) {
  189. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  190. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  191. return 0;
  192. }
  193. }
  194. for (compno = 0; compno < numcomps; ++compno) {
  195. for (y = 0; y < avctx->height; ++y) {
  196. image_index = y * avctx->width;
  197. frame_index = y * frame->linesize[0] + compno;
  198. for (x = 0; x < avctx->width; ++x) {
  199. image->comps[compno].data[image_index++] = frame->data[0][frame_index];
  200. frame_index += numcomps;
  201. }
  202. }
  203. }
  204. return 1;
  205. }
  206. static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  207. {
  208. int compno;
  209. int x;
  210. int y;
  211. int image_index;
  212. int frame_index;
  213. const int numcomps = image->numcomps;
  214. uint16_t *frame_ptr = (uint16_t*)frame->data[0];
  215. for (compno = 0; compno < numcomps; ++compno) {
  216. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  217. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  218. return 0;
  219. }
  220. }
  221. for (compno = 0; compno < numcomps; ++compno) {
  222. for (y = 0; y < avctx->height; ++y) {
  223. image_index = y * avctx->width;
  224. frame_index = y * (frame->linesize[0] / 2) + compno;
  225. for (x = 0; x < avctx->width; ++x) {
  226. image->comps[compno].data[image_index++] = frame_ptr[frame_index];
  227. frame_index += numcomps;
  228. }
  229. }
  230. }
  231. return 1;
  232. }
  233. static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  234. {
  235. int compno;
  236. int x;
  237. int y;
  238. int width;
  239. int height;
  240. int image_index;
  241. int frame_index;
  242. const int numcomps = image->numcomps;
  243. for (compno = 0; compno < numcomps; ++compno) {
  244. if (image->comps[compno].w > frame->linesize[compno]) {
  245. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  246. return 0;
  247. }
  248. }
  249. for (compno = 0; compno < numcomps; ++compno) {
  250. width = avctx->width / image->comps[compno].dx;
  251. height = avctx->height / image->comps[compno].dy;
  252. for (y = 0; y < height; ++y) {
  253. image_index = y * width;
  254. frame_index = y * frame->linesize[compno];
  255. for (x = 0; x < width; ++x)
  256. image->comps[compno].data[image_index++] = frame->data[compno][frame_index++];
  257. }
  258. }
  259. return 1;
  260. }
  261. static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  262. {
  263. int compno;
  264. int x;
  265. int y;
  266. int width;
  267. int height;
  268. int image_index;
  269. int frame_index;
  270. const int numcomps = image->numcomps;
  271. uint16_t *frame_ptr;
  272. for (compno = 0; compno < numcomps; ++compno) {
  273. if (image->comps[compno].w > frame->linesize[compno]) {
  274. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  275. return 0;
  276. }
  277. }
  278. for (compno = 0; compno < numcomps; ++compno) {
  279. width = avctx->width / image->comps[compno].dx;
  280. height = avctx->height / image->comps[compno].dy;
  281. frame_ptr = (uint16_t*)frame->data[compno];
  282. for (y = 0; y < height; ++y) {
  283. image_index = y * width;
  284. frame_index = y * (frame->linesize[compno] / 2);
  285. for (x = 0; x < width; ++x)
  286. image->comps[compno].data[image_index++] = frame_ptr[frame_index++];
  287. }
  288. }
  289. return 1;
  290. }
  291. static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  292. const AVFrame *frame, int *got_packet)
  293. {
  294. LibOpenJPEGContext *ctx = avctx->priv_data;
  295. opj_cinfo_t *compress = ctx->compress;
  296. opj_image_t *image = ctx->image;
  297. opj_cio_t *stream;
  298. int cpyresult = 0;
  299. int ret, len;
  300. // x0, y0 is the top left corner of the image
  301. // x1, y1 is the width, height of the reference grid
  302. image->x0 = 0;
  303. image->y0 = 0;
  304. image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
  305. image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
  306. switch (avctx->pix_fmt) {
  307. case PIX_FMT_RGB24:
  308. case PIX_FMT_RGBA:
  309. case PIX_FMT_GRAY8A:
  310. cpyresult = libopenjpeg_copy_packed8(avctx, frame, image);
  311. break;
  312. case PIX_FMT_RGB48:
  313. case PIX_FMT_RGBA64:
  314. cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
  315. break;
  316. case PIX_FMT_GRAY8:
  317. case PIX_FMT_YUV410P:
  318. case PIX_FMT_YUV411P:
  319. case PIX_FMT_YUV420P:
  320. case PIX_FMT_YUV422P:
  321. case PIX_FMT_YUV440P:
  322. case PIX_FMT_YUV444P:
  323. case PIX_FMT_YUVA420P:
  324. case PIX_FMT_YUVA422P:
  325. case PIX_FMT_YUVA444P:
  326. cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
  327. break;
  328. case PIX_FMT_GRAY16:
  329. case PIX_FMT_YUV420P9:
  330. case PIX_FMT_YUV422P9:
  331. case PIX_FMT_YUV444P9:
  332. case PIX_FMT_YUV444P10:
  333. case PIX_FMT_YUV422P10:
  334. case PIX_FMT_YUV420P10:
  335. case PIX_FMT_YUV420P12:
  336. case PIX_FMT_YUV422P12:
  337. case PIX_FMT_YUV444P12:
  338. case PIX_FMT_YUV420P14:
  339. case PIX_FMT_YUV422P14:
  340. case PIX_FMT_YUV444P14:
  341. case PIX_FMT_YUV444P16:
  342. case PIX_FMT_YUV422P16:
  343. case PIX_FMT_YUV420P16:
  344. cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
  345. break;
  346. default:
  347. av_log(avctx, AV_LOG_ERROR,
  348. "The frame's pixel format '%s' is not supported\n",
  349. av_get_pix_fmt_name(avctx->pix_fmt));
  350. return AVERROR(EINVAL);
  351. break;
  352. }
  353. if (!cpyresult) {
  354. av_log(avctx, AV_LOG_ERROR,
  355. "Could not copy the frame data to the internal image buffer\n");
  356. return -1;
  357. }
  358. opj_setup_encoder(compress, &ctx->enc_params, image);
  359. stream = opj_cio_open((opj_common_ptr)compress, NULL, 0);
  360. if (!stream) {
  361. av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
  362. return AVERROR(ENOMEM);
  363. }
  364. if (!opj_encode(compress, stream, image, NULL)) {
  365. opj_cio_close(stream);
  366. av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
  367. return -1;
  368. }
  369. len = cio_tell(stream);
  370. if ((ret = ff_alloc_packet2(avctx, pkt, len)) < 0) {
  371. opj_cio_close(stream);
  372. return ret;
  373. }
  374. memcpy(pkt->data, stream->buffer, len);
  375. pkt->flags |= AV_PKT_FLAG_KEY;
  376. *got_packet = 1;
  377. opj_cio_close(stream);
  378. return 0;
  379. }
  380. static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
  381. {
  382. LibOpenJPEGContext *ctx = avctx->priv_data;
  383. opj_destroy_compress(ctx->compress);
  384. opj_image_destroy(ctx->image);
  385. av_freep(&avctx->coded_frame);
  386. return 0;
  387. }
  388. #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
  389. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  390. static const AVOption options[] = {
  391. { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
  392. { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
  393. { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
  394. { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT, { STD_RSIZ }, STD_RSIZ, CINEMA4K, VE, "profile" },
  395. { "jpeg2000", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = STD_RSIZ }, 0, 0, VE, "profile" },
  396. { "cinema2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K }, 0, 0, VE, "profile" },
  397. { "cinema4k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA4K }, 0, 0, VE, "profile" },
  398. { "cinema_mode", "Digital Cinema", OFFSET(cinema_mode), AV_OPT_TYPE_INT, { OFF }, OFF, CINEMA4K_24, VE, "cinema_mode" },
  399. { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OFF }, 0, 0, VE, "cinema_mode" },
  400. { "2k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K_24 }, 0, 0, VE, "cinema_mode" },
  401. { "2k_48", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K_48 }, 0, 0, VE, "cinema_mode" },
  402. { "4k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA4K_24 }, 0, 0, VE, "cinema_mode" },
  403. { "prog_order", "Progression Order", OFFSET(prog_order), AV_OPT_TYPE_INT, { LRCP }, LRCP, CPRL, VE, "prog_order" },
  404. { "lrcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LRCP }, 0, 0, VE, "prog_order" },
  405. { "rlcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = RLCP }, 0, 0, VE, "prog_order" },
  406. { "rpcl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = RPCL }, 0, 0, VE, "prog_order" },
  407. { "pcrl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PCRL }, 0, 0, VE, "prog_order" },
  408. { "cprl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPRL }, 0, 0, VE, "prog_order" },
  409. { "numresolution", NULL, OFFSET(numresolution), AV_OPT_TYPE_INT, { 6 }, 1, INT_MAX, VE },
  410. { "numlayers", NULL, OFFSET(numlayers), AV_OPT_TYPE_INT, { 1 }, 1, 10, VE },
  411. { "disto_alloc", NULL, OFFSET(disto_alloc), AV_OPT_TYPE_INT, { 1 }, 0, 1, VE },
  412. { "fixed_alloc", NULL, OFFSET(fixed_alloc), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
  413. { "fixed_quality", NULL, OFFSET(fixed_quality), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
  414. { NULL },
  415. };
  416. static const AVClass class = {
  417. .class_name = "libopenjpeg",
  418. .item_name = av_default_item_name,
  419. .option = options,
  420. .version = LIBAVUTIL_VERSION_INT,
  421. };
  422. AVCodec ff_libopenjpeg_encoder = {
  423. .name = "libopenjpeg",
  424. .type = AVMEDIA_TYPE_VIDEO,
  425. .id = AV_CODEC_ID_JPEG2000,
  426. .priv_data_size = sizeof(LibOpenJPEGContext),
  427. .init = libopenjpeg_encode_init,
  428. .encode2 = libopenjpeg_encode_frame,
  429. .close = libopenjpeg_encode_close,
  430. .capabilities = 0,
  431. .pix_fmts = (const enum PixelFormat[]) {
  432. PIX_FMT_RGB24, PIX_FMT_RGBA, PIX_FMT_RGB48, PIX_FMT_RGBA64,
  433. PIX_FMT_GRAY8, PIX_FMT_GRAY8A, PIX_FMT_GRAY16,
  434. PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUVA420P,
  435. PIX_FMT_YUV440P, PIX_FMT_YUV444P, PIX_FMT_YUVA422P,
  436. PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_YUVA444P,
  437. PIX_FMT_YUV420P9, PIX_FMT_YUV422P9, PIX_FMT_YUV444P9,
  438. PIX_FMT_YUV420P10, PIX_FMT_YUV422P10, PIX_FMT_YUV444P10,
  439. PIX_FMT_YUV420P12, PIX_FMT_YUV422P12, PIX_FMT_YUV444P12,
  440. PIX_FMT_YUV420P14, PIX_FMT_YUV422P14, PIX_FMT_YUV444P14,
  441. PIX_FMT_YUV420P16, PIX_FMT_YUV422P16, PIX_FMT_YUV444P16,
  442. PIX_FMT_NONE
  443. },
  444. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
  445. .priv_class = &class,
  446. };