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.

499 lines
18KB

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