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.

373 lines
11KB

  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/imgutils.h"
  26. #include "libavutil/avassert.h"
  27. #include "avcodec.h"
  28. #include "libavutil/intreadwrite.h"
  29. #define OPJ_STATIC
  30. #include <openjpeg.h>
  31. typedef struct {
  32. opj_image_t *image;
  33. opj_cparameters_t enc_params;
  34. opj_cinfo_t *compress;
  35. opj_event_mgr_t event_mgr;
  36. } LibOpenJPEGContext;
  37. static void error_callback(const char *msg, void *data)
  38. {
  39. av_log((AVCodecContext*)data, AV_LOG_ERROR, "libopenjpeg: %s\n", msg);
  40. }
  41. static void warning_callback(const char *msg, void *data)
  42. {
  43. av_log((AVCodecContext*)data, AV_LOG_WARNING, "libopenjpeg: %s\n", msg);
  44. }
  45. static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
  46. {
  47. opj_image_cmptparm_t *cmptparm;
  48. opj_image_t *img;
  49. int i;
  50. int bpp = 8;
  51. int sub_dx[4];
  52. int sub_dy[4];
  53. int numcomps = 0;
  54. OPJ_COLOR_SPACE color_space = CLRSPC_UNKNOWN;
  55. sub_dx[0] = sub_dx[3] = 1;
  56. sub_dy[0] = sub_dy[3] = 1;
  57. sub_dx[1] = sub_dx[2] = 1<<av_pix_fmt_descriptors[avctx->pix_fmt].log2_chroma_w;
  58. sub_dy[1] = sub_dy[2] = 1<<av_pix_fmt_descriptors[avctx->pix_fmt].log2_chroma_h;
  59. switch (avctx->pix_fmt) {
  60. case PIX_FMT_GRAY8:
  61. color_space = CLRSPC_GRAY;
  62. numcomps = 1;
  63. break;
  64. case PIX_FMT_RGB24:
  65. color_space = CLRSPC_SRGB;
  66. numcomps = 3;
  67. break;
  68. case PIX_FMT_RGBA:
  69. color_space = CLRSPC_SRGB;
  70. numcomps = 4;
  71. break;
  72. case PIX_FMT_YUV420P:
  73. color_space = CLRSPC_SYCC;
  74. numcomps = 3;
  75. break;
  76. case PIX_FMT_YUV422P:
  77. color_space = CLRSPC_SYCC;
  78. numcomps = 3;
  79. break;
  80. case PIX_FMT_YUV440P:
  81. color_space = CLRSPC_SYCC;
  82. numcomps = 3;
  83. break;
  84. case PIX_FMT_YUV444P:
  85. color_space = CLRSPC_SYCC;
  86. numcomps = 3;
  87. break;
  88. case PIX_FMT_YUV420P9:
  89. case PIX_FMT_YUV422P9:
  90. case PIX_FMT_YUV444P9:
  91. color_space = CLRSPC_SYCC;
  92. numcomps = 3;
  93. bpp = 9;
  94. break;
  95. case PIX_FMT_YUV420P10:
  96. case PIX_FMT_YUV422P10:
  97. case PIX_FMT_YUV444P10:
  98. color_space = CLRSPC_SYCC;
  99. numcomps = 3;
  100. bpp = 10;
  101. break;
  102. case PIX_FMT_YUV420P16:
  103. case PIX_FMT_YUV422P16:
  104. case PIX_FMT_YUV444P16:
  105. color_space = CLRSPC_SYCC;
  106. numcomps = 3;
  107. bpp = 16;
  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 = bpp;
  120. cmptparm[i].bpp = bpp;
  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.tcp_numlayers = 1;
  136. ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
  137. ctx->enc_params.cp_disto_alloc = 1;
  138. ctx->compress = opj_create_compress(CODEC_J2K);
  139. if (!ctx->compress) {
  140. av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
  141. return AVERROR(ENOMEM);
  142. }
  143. avctx->coded_frame = avcodec_alloc_frame();
  144. if (!avctx->coded_frame) {
  145. av_freep(&ctx->compress);
  146. av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
  147. return AVERROR(ENOMEM);
  148. }
  149. ctx->image = mj2_create_image(avctx, &ctx->enc_params);
  150. if (!ctx->image) {
  151. av_freep(&ctx->compress);
  152. av_freep(&avctx->coded_frame);
  153. av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
  154. return AVERROR(EINVAL);
  155. }
  156. memset(&ctx->event_mgr, 0, sizeof(opj_event_mgr_t));
  157. ctx->event_mgr.error_handler = error_callback;
  158. ctx->event_mgr.warning_handler = warning_callback;
  159. ctx->event_mgr.info_handler = NULL;
  160. opj_set_event_mgr((opj_common_ptr)ctx->compress, &ctx->event_mgr, avctx);
  161. return 0;
  162. }
  163. static int libopenjpeg_copy_rgba(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image, int numcomps)
  164. {
  165. int compno;
  166. int x;
  167. int y;
  168. av_assert0(numcomps == 1 || numcomps == 3 || numcomps == 4);
  169. for (compno = 0; compno < numcomps; ++compno) {
  170. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  171. return 0;
  172. }
  173. }
  174. for (compno = 0; compno < numcomps; ++compno) {
  175. for (y = 0; y < avctx->height; ++y) {
  176. for (x = 0; x < avctx->width; ++x) {
  177. image->comps[compno].data[y * avctx->width + x] = frame->data[0][y * frame->linesize[0] + x * numcomps + compno];
  178. }
  179. }
  180. }
  181. return 1;
  182. }
  183. static int libopenjpeg_copy_yuv8(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
  184. {
  185. int compno;
  186. int x;
  187. int y;
  188. int width;
  189. int height;
  190. const int numcomps = 3;
  191. for (compno = 0; compno < numcomps; ++compno) {
  192. if (image->comps[compno].w > frame->linesize[compno]) {
  193. return 0;
  194. }
  195. }
  196. for (compno = 0; compno < numcomps; ++compno) {
  197. width = avctx->width / image->comps[compno].dx;
  198. height = avctx->height / image->comps[compno].dy;
  199. for (y = 0; y < height; ++y) {
  200. for (x = 0; x < width; ++x) {
  201. image->comps[compno].data[y * width + x] = frame->data[compno][y * frame->linesize[compno] + x];
  202. }
  203. }
  204. }
  205. return 1;
  206. }
  207. static int libopenjpeg_copy_yuv16(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
  208. {
  209. int compno;
  210. int x;
  211. int y;
  212. int width;
  213. int height;
  214. const int numcomps = 3;
  215. uint16_t *frame_ptr;
  216. for (compno = 0; compno < numcomps; ++compno) {
  217. if (image->comps[compno].w > frame->linesize[compno]) {
  218. return 0;
  219. }
  220. }
  221. for (compno = 0; compno < numcomps; ++compno) {
  222. width = avctx->width / image->comps[compno].dx;
  223. height = avctx->height / image->comps[compno].dy;
  224. frame_ptr = (uint16_t*)frame->data[compno];
  225. for (y = 0; y < height; ++y) {
  226. for (x = 0; x < width; ++x) {
  227. image->comps[compno].data[y * width + x] = frame_ptr[y * (frame->linesize[compno] / 2) + x];
  228. }
  229. }
  230. }
  231. return 1;
  232. }
  233. static int libopenjpeg_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
  234. {
  235. AVFrame *frame = data;
  236. LibOpenJPEGContext *ctx = avctx->priv_data;
  237. opj_cinfo_t *compress = ctx->compress;
  238. opj_image_t *image = ctx->image;
  239. opj_cio_t *stream;
  240. int cpyresult = 0;
  241. int len = 0;
  242. // x0, y0 is the top left corner of the image
  243. // x1, y1 is the width, height of the reference grid
  244. image->x0 = 0;
  245. image->y0 = 0;
  246. image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
  247. image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
  248. switch (avctx->pix_fmt) {
  249. case PIX_FMT_GRAY8:
  250. cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 1);
  251. break;
  252. case PIX_FMT_RGB24:
  253. cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 3);
  254. break;
  255. case PIX_FMT_RGBA:
  256. cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 4);
  257. break;
  258. case PIX_FMT_YUV420P:
  259. case PIX_FMT_YUV422P:
  260. case PIX_FMT_YUV440P:
  261. case PIX_FMT_YUV444P:
  262. cpyresult = libopenjpeg_copy_yuv8(avctx, frame, image);
  263. break;
  264. case PIX_FMT_YUV420P9:
  265. case PIX_FMT_YUV420P10:
  266. case PIX_FMT_YUV420P16:
  267. case PIX_FMT_YUV422P9:
  268. case PIX_FMT_YUV422P10:
  269. case PIX_FMT_YUV422P16:
  270. case PIX_FMT_YUV444P9:
  271. case PIX_FMT_YUV444P10:
  272. case PIX_FMT_YUV444P16:
  273. cpyresult = libopenjpeg_copy_yuv16(avctx, frame, image);
  274. break;
  275. default:
  276. av_log(avctx, AV_LOG_ERROR, "The frame's pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
  277. return AVERROR(EINVAL);
  278. break;
  279. }
  280. if (!cpyresult) {
  281. av_log(avctx, AV_LOG_ERROR, "Could not copy the frame data to the internal image buffer\n");
  282. return -1;
  283. }
  284. opj_setup_encoder(compress, &ctx->enc_params, image);
  285. stream = opj_cio_open((opj_common_ptr)compress, NULL, 0);
  286. if (!stream) {
  287. av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
  288. return AVERROR(ENOMEM);
  289. }
  290. if (!opj_encode(compress, stream, image, NULL)) {
  291. opj_cio_close(stream);
  292. av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
  293. return -1;
  294. }
  295. len = cio_tell(stream);
  296. if (len > buf_size) {
  297. opj_cio_close(stream);
  298. av_log(avctx, AV_LOG_ERROR, "Error with buf_size, not large enough to hold the frame\n");
  299. return -1;
  300. }
  301. memcpy(buf, stream->buffer, len);
  302. opj_cio_close(stream);
  303. return len;
  304. }
  305. static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
  306. {
  307. LibOpenJPEGContext *ctx = avctx->priv_data;
  308. opj_destroy_compress(ctx->compress);
  309. opj_image_destroy(ctx->image);
  310. av_freep(&avctx->coded_frame);
  311. return 0 ;
  312. }
  313. AVCodec ff_libopenjpeg_encoder = {
  314. .name = "libopenjpeg",
  315. .type = AVMEDIA_TYPE_VIDEO,
  316. .id = CODEC_ID_JPEG2000,
  317. .priv_data_size = sizeof(LibOpenJPEGContext),
  318. .init = libopenjpeg_encode_init,
  319. .encode = libopenjpeg_encode_frame,
  320. .close = libopenjpeg_encode_close,
  321. .decode = NULL,
  322. .capabilities = 0,
  323. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24,PIX_FMT_RGBA,PIX_FMT_GRAY8,
  324. PIX_FMT_YUV420P,PIX_FMT_YUV422P,
  325. PIX_FMT_YUV440P,PIX_FMT_YUV444P,
  326. PIX_FMT_YUV420P9,PIX_FMT_YUV422P9,PIX_FMT_YUV444P9,
  327. PIX_FMT_YUV420P10,PIX_FMT_YUV422P10,PIX_FMT_YUV444P10,
  328. PIX_FMT_YUV420P16,PIX_FMT_YUV422P16,PIX_FMT_YUV444P16},
  329. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 encoder"),
  330. } ;