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.

412 lines
12KB

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