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