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.

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