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.

429 lines
13KB

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