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.

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