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.

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