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.

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