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.

438 lines
16KB

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