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.

378 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_YUVA420P:
  89. color_space = CLRSPC_SYCC;
  90. numcomps = 4;
  91. break;
  92. case PIX_FMT_YUV420P9:
  93. case PIX_FMT_YUV422P9:
  94. case PIX_FMT_YUV444P9:
  95. color_space = CLRSPC_SYCC;
  96. numcomps = 3;
  97. bpp = 9;
  98. break;
  99. case PIX_FMT_YUV420P10:
  100. case PIX_FMT_YUV422P10:
  101. case PIX_FMT_YUV444P10:
  102. color_space = CLRSPC_SYCC;
  103. numcomps = 3;
  104. bpp = 10;
  105. break;
  106. case PIX_FMT_YUV420P16:
  107. case PIX_FMT_YUV422P16:
  108. case PIX_FMT_YUV444P16:
  109. color_space = CLRSPC_SYCC;
  110. numcomps = 3;
  111. bpp = 16;
  112. break;
  113. default:
  114. av_log(avctx, AV_LOG_ERROR, "The requested pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
  115. return NULL;
  116. }
  117. cmptparm = av_mallocz(numcomps * sizeof(opj_image_cmptparm_t));
  118. if (!cmptparm) {
  119. av_log(avctx, AV_LOG_ERROR, "Not enough memory");
  120. return NULL;
  121. }
  122. for (i = 0; i < numcomps; i++) {
  123. cmptparm[i].prec = bpp;
  124. cmptparm[i].bpp = bpp;
  125. cmptparm[i].sgnd = 0;
  126. cmptparm[i].dx = sub_dx[i];
  127. cmptparm[i].dy = sub_dy[i];
  128. cmptparm[i].w = avctx->width / sub_dx[i];
  129. cmptparm[i].h = avctx->height / sub_dy[i];
  130. }
  131. img = opj_image_create(numcomps, cmptparm, color_space);
  132. av_freep(&cmptparm);
  133. return img;
  134. }
  135. static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
  136. {
  137. LibOpenJPEGContext *ctx = avctx->priv_data;
  138. opj_set_default_encoder_parameters(&ctx->enc_params);
  139. ctx->enc_params.tcp_numlayers = 1;
  140. ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
  141. ctx->enc_params.cp_disto_alloc = 1;
  142. ctx->compress = opj_create_compress(CODEC_J2K);
  143. if (!ctx->compress) {
  144. av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
  145. return AVERROR(ENOMEM);
  146. }
  147. avctx->coded_frame = avcodec_alloc_frame();
  148. if (!avctx->coded_frame) {
  149. av_freep(&ctx->compress);
  150. av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
  151. return AVERROR(ENOMEM);
  152. }
  153. ctx->image = mj2_create_image(avctx, &ctx->enc_params);
  154. if (!ctx->image) {
  155. av_freep(&ctx->compress);
  156. av_freep(&avctx->coded_frame);
  157. av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
  158. return AVERROR(EINVAL);
  159. }
  160. memset(&ctx->event_mgr, 0, sizeof(opj_event_mgr_t));
  161. ctx->event_mgr.error_handler = error_callback;
  162. ctx->event_mgr.warning_handler = warning_callback;
  163. ctx->event_mgr.info_handler = NULL;
  164. opj_set_event_mgr((opj_common_ptr)ctx->compress, &ctx->event_mgr, avctx);
  165. return 0;
  166. }
  167. static int libopenjpeg_copy_rgba(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image, int numcomps)
  168. {
  169. int compno;
  170. int x;
  171. int y;
  172. av_assert0(numcomps == 1 || numcomps == 3 || numcomps == 4);
  173. for (compno = 0; compno < numcomps; ++compno) {
  174. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  175. return 0;
  176. }
  177. }
  178. for (compno = 0; compno < numcomps; ++compno) {
  179. for (y = 0; y < avctx->height; ++y) {
  180. for (x = 0; x < avctx->width; ++x) {
  181. image->comps[compno].data[y * avctx->width + x] = frame->data[0][y * frame->linesize[0] + x * numcomps + compno];
  182. }
  183. }
  184. }
  185. return 1;
  186. }
  187. static int libopenjpeg_copy_yuv8(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
  188. {
  189. int compno;
  190. int x;
  191. int y;
  192. int width;
  193. int height;
  194. const int numcomps = avctx->pix_fmt == PIX_FMT_YUVA420P ? 4 : 3;
  195. for (compno = 0; compno < numcomps; ++compno) {
  196. if (image->comps[compno].w > frame->linesize[compno]) {
  197. return 0;
  198. }
  199. }
  200. for (compno = 0; compno < numcomps; ++compno) {
  201. width = avctx->width / image->comps[compno].dx;
  202. height = avctx->height / image->comps[compno].dy;
  203. for (y = 0; y < height; ++y) {
  204. for (x = 0; x < width; ++x) {
  205. image->comps[compno].data[y * width + x] = frame->data[compno][y * frame->linesize[compno] + x];
  206. }
  207. }
  208. }
  209. return 1;
  210. }
  211. static int libopenjpeg_copy_yuv16(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
  212. {
  213. int compno;
  214. int x;
  215. int y;
  216. int width;
  217. int height;
  218. const int numcomps = 3;
  219. uint16_t *frame_ptr;
  220. for (compno = 0; compno < numcomps; ++compno) {
  221. if (image->comps[compno].w > frame->linesize[compno]) {
  222. return 0;
  223. }
  224. }
  225. for (compno = 0; compno < numcomps; ++compno) {
  226. width = avctx->width / image->comps[compno].dx;
  227. height = avctx->height / image->comps[compno].dy;
  228. frame_ptr = (uint16_t*)frame->data[compno];
  229. for (y = 0; y < height; ++y) {
  230. for (x = 0; x < width; ++x) {
  231. image->comps[compno].data[y * width + x] = frame_ptr[y * (frame->linesize[compno] / 2) + x];
  232. }
  233. }
  234. }
  235. return 1;
  236. }
  237. static int libopenjpeg_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
  238. {
  239. AVFrame *frame = data;
  240. LibOpenJPEGContext *ctx = avctx->priv_data;
  241. opj_cinfo_t *compress = ctx->compress;
  242. opj_image_t *image = ctx->image;
  243. opj_cio_t *stream;
  244. int cpyresult = 0;
  245. int len = 0;
  246. // x0, y0 is the top left corner of the image
  247. // x1, y1 is the width, height of the reference grid
  248. image->x0 = 0;
  249. image->y0 = 0;
  250. image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
  251. image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
  252. switch (avctx->pix_fmt) {
  253. case PIX_FMT_GRAY8:
  254. cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 1);
  255. break;
  256. case PIX_FMT_RGB24:
  257. cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 3);
  258. break;
  259. case PIX_FMT_RGBA:
  260. cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 4);
  261. break;
  262. case PIX_FMT_YUV420P:
  263. case PIX_FMT_YUV422P:
  264. case PIX_FMT_YUV440P:
  265. case PIX_FMT_YUV444P:
  266. case PIX_FMT_YUVA420P:
  267. cpyresult = libopenjpeg_copy_yuv8(avctx, frame, image);
  268. break;
  269. case PIX_FMT_YUV420P9:
  270. case PIX_FMT_YUV420P10:
  271. case PIX_FMT_YUV420P16:
  272. case PIX_FMT_YUV422P9:
  273. case PIX_FMT_YUV422P10:
  274. case PIX_FMT_YUV422P16:
  275. case PIX_FMT_YUV444P9:
  276. case PIX_FMT_YUV444P10:
  277. case PIX_FMT_YUV444P16:
  278. cpyresult = libopenjpeg_copy_yuv16(avctx, frame, image);
  279. break;
  280. default:
  281. av_log(avctx, AV_LOG_ERROR, "The frame's pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
  282. return AVERROR(EINVAL);
  283. break;
  284. }
  285. if (!cpyresult) {
  286. av_log(avctx, AV_LOG_ERROR, "Could not copy the frame data to the internal image buffer\n");
  287. return -1;
  288. }
  289. opj_setup_encoder(compress, &ctx->enc_params, image);
  290. stream = opj_cio_open((opj_common_ptr)compress, NULL, 0);
  291. if (!stream) {
  292. av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
  293. return AVERROR(ENOMEM);
  294. }
  295. if (!opj_encode(compress, stream, image, NULL)) {
  296. opj_cio_close(stream);
  297. av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
  298. return -1;
  299. }
  300. len = cio_tell(stream);
  301. if (len > buf_size) {
  302. opj_cio_close(stream);
  303. av_log(avctx, AV_LOG_ERROR, "Error with buf_size, not large enough to hold the frame\n");
  304. return -1;
  305. }
  306. memcpy(buf, stream->buffer, len);
  307. opj_cio_close(stream);
  308. return len;
  309. }
  310. static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
  311. {
  312. LibOpenJPEGContext *ctx = avctx->priv_data;
  313. opj_destroy_compress(ctx->compress);
  314. opj_image_destroy(ctx->image);
  315. av_freep(&avctx->coded_frame);
  316. return 0 ;
  317. }
  318. AVCodec ff_libopenjpeg_encoder = {
  319. .name = "libopenjpeg",
  320. .type = AVMEDIA_TYPE_VIDEO,
  321. .id = CODEC_ID_JPEG2000,
  322. .priv_data_size = sizeof(LibOpenJPEGContext),
  323. .init = libopenjpeg_encode_init,
  324. .encode = libopenjpeg_encode_frame,
  325. .close = libopenjpeg_encode_close,
  326. .capabilities = 0,
  327. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24,PIX_FMT_RGBA,PIX_FMT_GRAY8,
  328. PIX_FMT_YUV420P,PIX_FMT_YUV422P,PIX_FMT_YUVA420P,
  329. PIX_FMT_YUV440P,PIX_FMT_YUV444P,
  330. PIX_FMT_YUV420P9,PIX_FMT_YUV422P9,PIX_FMT_YUV444P9,
  331. PIX_FMT_YUV420P10,PIX_FMT_YUV422P10,PIX_FMT_YUV444P10,
  332. PIX_FMT_YUV420P16,PIX_FMT_YUV422P16,PIX_FMT_YUV444P16,
  333. PIX_FMT_NONE},
  334. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 encoder"),
  335. } ;