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.

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