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.

313 lines
9.2KB

  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;
  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. bpp = 8;
  64. break;
  65. case PIX_FMT_RGB24:
  66. color_space = CLRSPC_SRGB;
  67. numcomps = 3;
  68. bpp = 24;
  69. break;
  70. case PIX_FMT_RGBA:
  71. color_space = CLRSPC_SRGB;
  72. numcomps = 4;
  73. bpp = 32;
  74. break;
  75. case PIX_FMT_YUV420P:
  76. color_space = CLRSPC_SYCC;
  77. numcomps = 3;
  78. bpp = 12;
  79. break;
  80. case PIX_FMT_YUV422P:
  81. color_space = CLRSPC_SYCC;
  82. numcomps = 3;
  83. bpp = 16;
  84. break;
  85. case PIX_FMT_YUV440P:
  86. color_space = CLRSPC_SYCC;
  87. numcomps = 3;
  88. bpp = 16;
  89. break;
  90. case PIX_FMT_YUV444P:
  91. color_space = CLRSPC_SYCC;
  92. numcomps = 3;
  93. bpp = 24;
  94. break;
  95. default:
  96. av_log(avctx, AV_LOG_ERROR, "The requested pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
  97. return NULL;
  98. }
  99. cmptparm = av_mallocz(numcomps * sizeof(opj_image_cmptparm_t));
  100. if (!cmptparm) {
  101. av_log(avctx, AV_LOG_ERROR, "Not enough memory");
  102. return NULL;
  103. }
  104. for (i = 0; i < numcomps; i++) {
  105. cmptparm[i].prec = 8;
  106. cmptparm[i].bpp = bpp;
  107. cmptparm[i].sgnd = 0;
  108. cmptparm[i].dx = sub_dx[i];
  109. cmptparm[i].dy = sub_dy[i];
  110. cmptparm[i].w = avctx->width / sub_dx[i];
  111. cmptparm[i].h = avctx->height / sub_dy[i];
  112. }
  113. img = opj_image_create(numcomps, cmptparm, color_space);
  114. av_freep(&cmptparm);
  115. return img;
  116. }
  117. static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
  118. {
  119. LibOpenJPEGContext *ctx = avctx->priv_data;
  120. opj_set_default_encoder_parameters(&ctx->enc_params);
  121. ctx->enc_params.tcp_numlayers = 1;
  122. ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0);
  123. ctx->enc_params.cp_disto_alloc = 1;
  124. ctx->compress = opj_create_compress(CODEC_J2K);
  125. if (!ctx->compress) {
  126. av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
  127. return AVERROR(ENOMEM);
  128. }
  129. avctx->coded_frame = avcodec_alloc_frame();
  130. if (!avctx->coded_frame) {
  131. av_freep(&ctx->compress);
  132. av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
  133. return AVERROR(ENOMEM);
  134. }
  135. ctx->image = mj2_create_image(avctx, &ctx->enc_params);
  136. if (!ctx->image) {
  137. av_freep(&ctx->compress);
  138. av_freep(&avctx->coded_frame);
  139. av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
  140. return AVERROR(EINVAL);
  141. }
  142. memset(&ctx->event_mgr, 0, sizeof(opj_event_mgr_t));
  143. ctx->event_mgr.error_handler = error_callback;
  144. ctx->event_mgr.warning_handler = warning_callback;
  145. ctx->event_mgr.info_handler = NULL;
  146. opj_set_event_mgr((opj_common_ptr)ctx->compress, &ctx->event_mgr, avctx);
  147. return 0;
  148. }
  149. static int libopenjpeg_copy_rgba(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image, int numcomps)
  150. {
  151. int compno;
  152. int x;
  153. int y;
  154. av_assert0(numcomps == 1 || numcomps == 3 || numcomps == 4);
  155. for (compno = 0; compno < numcomps; ++compno) {
  156. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  157. return 0;
  158. }
  159. }
  160. for (compno = 0; compno < numcomps; ++compno) {
  161. for (y = 0; y < avctx->height; ++y) {
  162. for (x = 0; x < avctx->width; ++x) {
  163. image->comps[compno].data[y * avctx->width + x] = frame->data[0][y * frame->linesize[0] + x * numcomps + compno];
  164. }
  165. }
  166. }
  167. return 1;
  168. }
  169. static int libopenjpeg_copy_yuv(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
  170. {
  171. int compno;
  172. int x;
  173. int y;
  174. int width;
  175. int height;
  176. const int numcomps = 3;
  177. for (compno = 0; compno < numcomps; ++compno) {
  178. if (image->comps[compno].w > frame->linesize[compno]) {
  179. return 0;
  180. }
  181. }
  182. for (compno = 0; compno < numcomps; ++compno) {
  183. width = avctx->width / image->comps[compno].dx;
  184. height = avctx->height / image->comps[compno].dy;
  185. for (y = 0; y < height; ++y) {
  186. for (x = 0; x < width; ++x) {
  187. image->comps[compno].data[y * width + x] = frame->data[compno][y * frame->linesize[compno] + x];
  188. }
  189. }
  190. }
  191. return 1;
  192. }
  193. static int libopenjpeg_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
  194. {
  195. AVFrame *frame = data;
  196. LibOpenJPEGContext *ctx = avctx->priv_data;
  197. opj_cinfo_t *compress = ctx->compress;
  198. opj_image_t *image = ctx->image;
  199. opj_cio_t *stream;
  200. int cpyresult = 0;
  201. int len = 0;
  202. // x0, y0 is the top left corner of the image
  203. // x1, y1 is the width, height of the reference grid
  204. image->x0 = 0;
  205. image->y0 = 0;
  206. image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
  207. image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
  208. switch (avctx->pix_fmt) {
  209. case PIX_FMT_GRAY8:
  210. cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 1);
  211. break;
  212. case PIX_FMT_RGB24:
  213. cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 3);
  214. break;
  215. case PIX_FMT_RGBA:
  216. cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 4);
  217. break;
  218. case PIX_FMT_YUV420P:
  219. case PIX_FMT_YUV422P:
  220. case PIX_FMT_YUV440P:
  221. case PIX_FMT_YUV444P:
  222. cpyresult = libopenjpeg_copy_yuv(avctx, frame, image);
  223. break;
  224. default:
  225. av_log(avctx, AV_LOG_ERROR, "The frame's pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
  226. return AVERROR(EINVAL);
  227. break;
  228. }
  229. if (!cpyresult) {
  230. av_log(avctx, AV_LOG_ERROR, "Could not copy the frame data to the internal image buffer\n");
  231. return -1;
  232. }
  233. opj_setup_encoder(compress, &ctx->enc_params, image);
  234. stream = opj_cio_open((opj_common_ptr)compress, NULL, 0);
  235. if (!stream) {
  236. av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
  237. return AVERROR(ENOMEM);
  238. }
  239. if (!opj_encode(compress, stream, image, NULL)) {
  240. opj_cio_close(stream);
  241. av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
  242. return -1;
  243. }
  244. len = cio_tell(stream);
  245. if (len > buf_size) {
  246. opj_cio_close(stream);
  247. av_log(avctx, AV_LOG_ERROR, "Error with buf_size, not large enough to hold the frame\n");
  248. return -1;
  249. }
  250. memcpy(buf, stream->buffer, len);
  251. opj_cio_close(stream);
  252. return len;
  253. }
  254. static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
  255. {
  256. LibOpenJPEGContext *ctx = avctx->priv_data;
  257. opj_destroy_compress(ctx->compress);
  258. opj_image_destroy(ctx->image);
  259. av_freep(&avctx->coded_frame);
  260. return 0 ;
  261. }
  262. AVCodec ff_libopenjpeg_encoder = {
  263. .name = "libopenjpeg",
  264. .type = AVMEDIA_TYPE_VIDEO,
  265. .id = CODEC_ID_JPEG2000,
  266. .priv_data_size = sizeof(LibOpenJPEGContext),
  267. .init = libopenjpeg_encode_init,
  268. .encode = libopenjpeg_encode_frame,
  269. .close = libopenjpeg_encode_close,
  270. .decode = NULL,
  271. .capabilities = 0,
  272. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_GRAY8,PIX_FMT_RGB24,PIX_FMT_RGBA,PIX_FMT_YUV420P,PIX_FMT_YUV422P,PIX_FMT_YUV440P,PIX_FMT_YUV444P},
  273. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 encoder"),
  274. } ;