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.

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