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.

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