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.

469 lines
17KB

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