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.

423 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_packed8(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
  183. {
  184. int compno;
  185. int x;
  186. int y;
  187. const int numcomps = image->numcomps;
  188. for (compno = 0; compno < numcomps; ++compno) {
  189. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  190. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  191. return 0;
  192. }
  193. }
  194. for (compno = 0; compno < numcomps; ++compno) {
  195. for (y = 0; y < avctx->height; ++y) {
  196. for (x = 0; x < avctx->width; ++x) {
  197. image->comps[compno].data[y * avctx->width + x] = frame->data[0][y * frame->linesize[0] + x * numcomps + compno];
  198. }
  199. }
  200. }
  201. return 1;
  202. }
  203. static int libopenjpeg_copy_packed16(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
  204. {
  205. int compno;
  206. int x;
  207. int y;
  208. const int numcomps = image->numcomps;
  209. uint16_t *frame_ptr = (uint16_t*)frame->data[0];
  210. for (compno = 0; compno < numcomps; ++compno) {
  211. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  212. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  213. return 0;
  214. }
  215. }
  216. for (compno = 0; compno < numcomps; ++compno) {
  217. for (y = 0; y < avctx->height; ++y) {
  218. for (x = 0; x < avctx->width; ++x) {
  219. image->comps[compno].data[y * avctx->width + x] = frame_ptr[y * frame->linesize[0] / 2 + x * numcomps + compno];
  220. }
  221. }
  222. }
  223. return 1;
  224. }
  225. static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
  226. {
  227. int compno;
  228. int x;
  229. int y;
  230. int width;
  231. int height;
  232. const int numcomps = image->numcomps;
  233. for (compno = 0; compno < numcomps; ++compno) {
  234. if (image->comps[compno].w > frame->linesize[compno]) {
  235. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  236. return 0;
  237. }
  238. }
  239. for (compno = 0; compno < numcomps; ++compno) {
  240. width = avctx->width / image->comps[compno].dx;
  241. height = avctx->height / image->comps[compno].dy;
  242. for (y = 0; y < height; ++y) {
  243. for (x = 0; x < width; ++x) {
  244. image->comps[compno].data[y * width + x] = frame->data[compno][y * frame->linesize[compno] + x];
  245. }
  246. }
  247. }
  248. return 1;
  249. }
  250. static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
  251. {
  252. int compno;
  253. int x;
  254. int y;
  255. int width;
  256. int height;
  257. const int numcomps = image->numcomps;
  258. uint16_t *frame_ptr;
  259. for (compno = 0; compno < numcomps; ++compno) {
  260. if (image->comps[compno].w > frame->linesize[compno]) {
  261. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  262. return 0;
  263. }
  264. }
  265. for (compno = 0; compno < numcomps; ++compno) {
  266. width = avctx->width / image->comps[compno].dx;
  267. height = avctx->height / image->comps[compno].dy;
  268. frame_ptr = (uint16_t*)frame->data[compno];
  269. for (y = 0; y < height; ++y) {
  270. for (x = 0; x < width; ++x) {
  271. image->comps[compno].data[y * width + x] = frame_ptr[y * (frame->linesize[compno] / 2) + x];
  272. }
  273. }
  274. }
  275. return 1;
  276. }
  277. static int libopenjpeg_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
  278. {
  279. AVFrame *frame = data;
  280. LibOpenJPEGContext *ctx = avctx->priv_data;
  281. opj_cinfo_t *compress = ctx->compress;
  282. opj_image_t *image = ctx->image;
  283. opj_cio_t *stream;
  284. int cpyresult = 0;
  285. int len = 0;
  286. // x0, y0 is the top left corner of the image
  287. // x1, y1 is the width, height of the reference grid
  288. image->x0 = 0;
  289. image->y0 = 0;
  290. image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
  291. image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
  292. switch (avctx->pix_fmt) {
  293. case PIX_FMT_RGB24:
  294. case PIX_FMT_RGBA:
  295. cpyresult = libopenjpeg_copy_packed8(avctx, frame, image);
  296. break;
  297. case PIX_FMT_RGB48:
  298. case PIX_FMT_RGBA64:
  299. cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
  300. break;
  301. case PIX_FMT_GRAY8:
  302. case PIX_FMT_YUV420P:
  303. case PIX_FMT_YUV422P:
  304. case PIX_FMT_YUV440P:
  305. case PIX_FMT_YUV444P:
  306. case PIX_FMT_YUVA420P:
  307. cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
  308. break;
  309. case PIX_FMT_GRAY16:
  310. case PIX_FMT_YUV420P9:
  311. case PIX_FMT_YUV420P10:
  312. case PIX_FMT_YUV420P16:
  313. case PIX_FMT_YUV422P9:
  314. case PIX_FMT_YUV422P10:
  315. case PIX_FMT_YUV422P16:
  316. case PIX_FMT_YUV444P9:
  317. case PIX_FMT_YUV444P10:
  318. case PIX_FMT_YUV444P16:
  319. cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
  320. break;
  321. default:
  322. av_log(avctx, AV_LOG_ERROR, "The frame's pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
  323. return AVERROR(EINVAL);
  324. break;
  325. }
  326. if (!cpyresult) {
  327. av_log(avctx, AV_LOG_ERROR, "Could not copy the frame data to the internal image buffer\n");
  328. return -1;
  329. }
  330. opj_setup_encoder(compress, &ctx->enc_params, image);
  331. stream = opj_cio_open((opj_common_ptr)compress, NULL, 0);
  332. if (!stream) {
  333. av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
  334. return AVERROR(ENOMEM);
  335. }
  336. if (!opj_encode(compress, stream, image, NULL)) {
  337. opj_cio_close(stream);
  338. av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
  339. return -1;
  340. }
  341. len = cio_tell(stream);
  342. if (len > buf_size) {
  343. opj_cio_close(stream);
  344. av_log(avctx, AV_LOG_ERROR, "Error with buf_size, not large enough to hold the frame\n");
  345. return -1;
  346. }
  347. memcpy(buf, stream->buffer, len);
  348. opj_cio_close(stream);
  349. return len;
  350. }
  351. static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
  352. {
  353. LibOpenJPEGContext *ctx = avctx->priv_data;
  354. opj_destroy_compress(ctx->compress);
  355. opj_image_destroy(ctx->image);
  356. av_freep(&avctx->coded_frame);
  357. return 0 ;
  358. }
  359. AVCodec ff_libopenjpeg_encoder = {
  360. .name = "libopenjpeg",
  361. .type = AVMEDIA_TYPE_VIDEO,
  362. .id = CODEC_ID_JPEG2000,
  363. .priv_data_size = sizeof(LibOpenJPEGContext),
  364. .init = libopenjpeg_encode_init,
  365. .encode = libopenjpeg_encode_frame,
  366. .close = libopenjpeg_encode_close,
  367. .capabilities = 0,
  368. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24,PIX_FMT_RGBA,PIX_FMT_RGB48,PIX_FMT_RGBA64,
  369. PIX_FMT_GRAY8,PIX_FMT_GRAY16,
  370. PIX_FMT_YUV420P,PIX_FMT_YUV422P,PIX_FMT_YUVA420P,
  371. PIX_FMT_YUV440P,PIX_FMT_YUV444P,
  372. PIX_FMT_YUV420P9,PIX_FMT_YUV422P9,PIX_FMT_YUV444P9,
  373. PIX_FMT_YUV420P10,PIX_FMT_YUV422P10,PIX_FMT_YUV444P10,
  374. PIX_FMT_YUV420P16,PIX_FMT_YUV422P16,PIX_FMT_YUV444P16,
  375. PIX_FMT_NONE},
  376. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 encoder"),
  377. } ;