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.

424 lines
14KB

  1. /*
  2. * JPEG 2000 decoding support via OpenJPEG
  3. * Copyright (c) 2009 Jaikrishnan Menon <realityman@gmx.net>
  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 decoder using libopenjpeg
  24. */
  25. #define OPJ_STATIC
  26. #include <openjpeg.h>
  27. #include "libavutil/common.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/pixfmt.h"
  31. #include "libavutil/opt.h"
  32. #include "avcodec.h"
  33. #include "thread.h"
  34. #define JP2_SIG_TYPE 0x6A502020
  35. #define JP2_SIG_VALUE 0x0D0A870A
  36. // pix_fmts with lower bpp have to be listed before
  37. // similar pix_fmts with higher bpp.
  38. #define RGB_PIXEL_FORMATS PIX_FMT_RGB24,PIX_FMT_RGBA,PIX_FMT_RGB48,PIX_FMT_RGBA64
  39. #define GRAY_PIXEL_FORMATS PIX_FMT_GRAY8,PIX_FMT_GRAY8A,PIX_FMT_GRAY16
  40. #define YUV_PIXEL_FORMATS PIX_FMT_YUV410P,PIX_FMT_YUV411P,PIX_FMT_YUVA420P, \
  41. PIX_FMT_YUV420P,PIX_FMT_YUV422P,PIX_FMT_YUVA422P, \
  42. PIX_FMT_YUV440P,PIX_FMT_YUV444P,PIX_FMT_YUVA444P, \
  43. PIX_FMT_YUV420P9,PIX_FMT_YUV422P9,PIX_FMT_YUV444P9, \
  44. PIX_FMT_YUV420P10,PIX_FMT_YUV422P10,PIX_FMT_YUV444P10, \
  45. PIX_FMT_YUV420P12,PIX_FMT_YUV422P12,PIX_FMT_YUV444P12, \
  46. PIX_FMT_YUV420P14,PIX_FMT_YUV422P14,PIX_FMT_YUV444P14, \
  47. PIX_FMT_YUV420P16,PIX_FMT_YUV422P16,PIX_FMT_YUV444P16
  48. static const enum PixelFormat libopenjpeg_rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
  49. static const enum PixelFormat libopenjpeg_gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
  50. static const enum PixelFormat libopenjpeg_yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
  51. static const enum PixelFormat libopenjpeg_all_pix_fmts[] = {RGB_PIXEL_FORMATS,GRAY_PIXEL_FORMATS,YUV_PIXEL_FORMATS};
  52. typedef struct {
  53. AVClass *class;
  54. opj_dparameters_t dec_params;
  55. AVFrame image;
  56. int lowqual;
  57. } LibOpenJPEGContext;
  58. static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum PixelFormat pix_fmt)
  59. {
  60. AVPixFmtDescriptor descriptor = av_pix_fmt_descriptors[pix_fmt];
  61. int match = 1;
  62. if (descriptor.nb_components != image->numcomps) {
  63. return 0;
  64. }
  65. switch (descriptor.nb_components) {
  66. case 4: match = match && descriptor.comp[3].depth_minus1 + 1 >= image->comps[3].prec &&
  67. 1 == image->comps[3].dx &&
  68. 1 == image->comps[3].dy;
  69. case 3: match = match && descriptor.comp[2].depth_minus1 + 1 >= image->comps[2].prec &&
  70. 1 << descriptor.log2_chroma_w == image->comps[2].dx &&
  71. 1 << descriptor.log2_chroma_h == image->comps[2].dy;
  72. case 2: match = match && descriptor.comp[1].depth_minus1 + 1 >= image->comps[1].prec &&
  73. 1 << descriptor.log2_chroma_w == image->comps[1].dx &&
  74. 1 << descriptor.log2_chroma_h == image->comps[1].dy;
  75. case 1: match = match && descriptor.comp[0].depth_minus1 + 1 >= image->comps[0].prec &&
  76. 1 == image->comps[0].dx &&
  77. 1 == image->comps[0].dy;
  78. default:
  79. break;
  80. }
  81. return match;
  82. }
  83. static inline enum PixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
  84. int index;
  85. const enum PixelFormat *possible_fmts = NULL;
  86. int possible_fmts_nb = 0;
  87. switch (image->color_space) {
  88. case CLRSPC_SRGB:
  89. possible_fmts = libopenjpeg_rgb_pix_fmts;
  90. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_rgb_pix_fmts);
  91. break;
  92. case CLRSPC_GRAY:
  93. possible_fmts = libopenjpeg_gray_pix_fmts;
  94. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_gray_pix_fmts);
  95. break;
  96. case CLRSPC_SYCC:
  97. possible_fmts = libopenjpeg_yuv_pix_fmts;
  98. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_yuv_pix_fmts);
  99. break;
  100. default:
  101. possible_fmts = libopenjpeg_all_pix_fmts;
  102. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_all_pix_fmts);
  103. break;
  104. }
  105. for (index = 0; index < possible_fmts_nb; ++index) {
  106. if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
  107. return possible_fmts[index];
  108. }
  109. }
  110. return PIX_FMT_NONE;
  111. }
  112. static inline int libopenjpeg_ispacked(enum PixelFormat pix_fmt) {
  113. int i, component_plane;
  114. if (pix_fmt == PIX_FMT_GRAY16)
  115. return 0;
  116. component_plane = av_pix_fmt_descriptors[pix_fmt].comp[0].plane;
  117. for (i = 1; i < av_pix_fmt_descriptors[pix_fmt].nb_components; i++) {
  118. if (component_plane != av_pix_fmt_descriptors[pix_fmt].comp[i].plane)
  119. return 0;
  120. }
  121. return 1;
  122. }
  123. static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
  124. uint8_t *img_ptr;
  125. int index, x, y, c;
  126. for (y = 0; y < picture->height; y++) {
  127. index = y*picture->width;
  128. img_ptr = picture->data[0] + y*picture->linesize[0];
  129. for (x = 0; x < picture->width; x++, index++) {
  130. for (c = 0; c < image->numcomps; c++) {
  131. *img_ptr++ = image->comps[c].data[index];
  132. }
  133. }
  134. }
  135. }
  136. static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
  137. uint16_t *img_ptr;
  138. int index, x, y, c;
  139. int adjust[4];
  140. for (x = 0; x < image->numcomps; x++)
  141. adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
  142. for (y = 0; y < picture->height; y++) {
  143. index = y*picture->width;
  144. img_ptr = (uint16_t*) (picture->data[0] + y*picture->linesize[0]);
  145. for (x = 0; x < picture->width; x++, index++) {
  146. for (c = 0; c < image->numcomps; c++) {
  147. *img_ptr++ = image->comps[c].data[index] << adjust[c];
  148. }
  149. }
  150. }
  151. }
  152. static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
  153. int *comp_data;
  154. uint8_t *img_ptr;
  155. int index, x, y;
  156. for (index = 0; index < image->numcomps; index++) {
  157. comp_data = image->comps[index].data;
  158. for (y = 0; y < image->comps[index].h; y++) {
  159. img_ptr = picture->data[index] + y * picture->linesize[index];
  160. for (x = 0; x < image->comps[index].w; x++) {
  161. *img_ptr = (uint8_t) *comp_data;
  162. img_ptr++;
  163. comp_data++;
  164. }
  165. }
  166. }
  167. }
  168. static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
  169. int *comp_data;
  170. uint16_t *img_ptr;
  171. int index, x, y;
  172. for (index = 0; index < image->numcomps; index++) {
  173. comp_data = image->comps[index].data;
  174. for (y = 0; y < image->comps[index].h; y++) {
  175. img_ptr = (uint16_t*) (picture->data[index] + y * picture->linesize[index]);
  176. for (x = 0; x < image->comps[index].w; x++) {
  177. *img_ptr = *comp_data;
  178. img_ptr++;
  179. comp_data++;
  180. }
  181. }
  182. }
  183. }
  184. static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
  185. {
  186. LibOpenJPEGContext *ctx = avctx->priv_data;
  187. opj_set_default_decoder_parameters(&ctx->dec_params);
  188. avcodec_get_frame_defaults(&ctx->image);
  189. avctx->coded_frame = &ctx->image;
  190. return 0;
  191. }
  192. static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
  193. {
  194. LibOpenJPEGContext *ctx = avctx->priv_data;
  195. avctx->coded_frame = &ctx->image;
  196. return 0;
  197. }
  198. static int libopenjpeg_decode_frame(AVCodecContext *avctx,
  199. void *data, int *data_size,
  200. AVPacket *avpkt)
  201. {
  202. uint8_t *buf = avpkt->data;
  203. int buf_size = avpkt->size;
  204. LibOpenJPEGContext *ctx = avctx->priv_data;
  205. AVFrame *picture = &ctx->image, *output = data;
  206. opj_dinfo_t *dec;
  207. opj_cio_t *stream;
  208. opj_image_t *image;
  209. int width, height, ret = -1;
  210. int pixel_size = 0;
  211. int ispacked = 0;
  212. int i;
  213. *data_size = 0;
  214. // Check if input is a raw jpeg2k codestream or in jp2 wrapping
  215. if ((AV_RB32(buf) == 12) &&
  216. (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
  217. (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
  218. dec = opj_create_decompress(CODEC_JP2);
  219. } else {
  220. /* If the AVPacket contains a jp2c box, then skip to
  221. * the starting byte of the codestream. */
  222. if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
  223. buf += 8;
  224. dec = opj_create_decompress(CODEC_J2K);
  225. }
  226. if (!dec) {
  227. av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
  228. return -1;
  229. }
  230. opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
  231. ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
  232. ctx->dec_params.cp_layer = ctx->lowqual;
  233. // Tie decoder with decoding parameters
  234. opj_setup_decoder(dec, &ctx->dec_params);
  235. stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
  236. if (!stream) {
  237. av_log(avctx, AV_LOG_ERROR,
  238. "Codestream could not be opened for reading.\n");
  239. opj_destroy_decompress(dec);
  240. return -1;
  241. }
  242. // Decode the header only.
  243. image = opj_decode_with_info(dec, stream, NULL);
  244. opj_cio_close(stream);
  245. if (!image) {
  246. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  247. opj_destroy_decompress(dec);
  248. return -1;
  249. }
  250. width = image->x1 - image->x0;
  251. height = image->y1 - image->y0;
  252. if (av_image_check_size(width, height, 0, avctx) < 0) {
  253. av_log(avctx, AV_LOG_ERROR,
  254. "%dx%d dimension invalid.\n", width, height);
  255. goto done;
  256. }
  257. avcodec_set_dimensions(avctx, width, height);
  258. if (avctx->pix_fmt != PIX_FMT_NONE)
  259. if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
  260. avctx->pix_fmt = PIX_FMT_NONE;
  261. if (avctx->pix_fmt == PIX_FMT_NONE)
  262. avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
  263. if (avctx->pix_fmt == PIX_FMT_NONE) {
  264. av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
  265. goto done;
  266. }
  267. for (i = 0; i < image->numcomps; i++)
  268. if (image->comps[i].prec > avctx->bits_per_raw_sample)
  269. avctx->bits_per_raw_sample = image->comps[i].prec;
  270. if (picture->data[0])
  271. ff_thread_release_buffer(avctx, picture);
  272. if (ff_thread_get_buffer(avctx, picture) < 0) {
  273. av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
  274. goto done;
  275. }
  276. ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
  277. ctx->dec_params.cp_reduce = avctx->lowres;
  278. // Tie decoder with decoding parameters.
  279. opj_setup_decoder(dec, &ctx->dec_params);
  280. stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
  281. if (!stream) {
  282. av_log(avctx, AV_LOG_ERROR,
  283. "Codestream could not be opened for reading.\n");
  284. goto done;
  285. }
  286. opj_image_destroy(image);
  287. // Decode the codestream
  288. image = opj_decode_with_info(dec, stream, NULL);
  289. opj_cio_close(stream);
  290. if (!image) {
  291. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  292. goto done;
  293. }
  294. pixel_size = av_pix_fmt_descriptors[avctx->pix_fmt].comp[0].step_minus1 + 1;
  295. ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
  296. switch (pixel_size) {
  297. case 1:
  298. if (ispacked) {
  299. libopenjpeg_copy_to_packed8(picture, image);
  300. } else {
  301. libopenjpeg_copyto8(picture, image);
  302. }
  303. break;
  304. case 2:
  305. if (ispacked) {
  306. libopenjpeg_copy_to_packed8(picture, image);
  307. } else {
  308. libopenjpeg_copyto16(picture, image);
  309. }
  310. break;
  311. case 3:
  312. case 4:
  313. if (ispacked) {
  314. libopenjpeg_copy_to_packed8(picture, image);
  315. }
  316. break;
  317. case 6:
  318. case 8:
  319. if (ispacked) {
  320. libopenjpeg_copy_to_packed16(picture, image);
  321. }
  322. break;
  323. default:
  324. av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
  325. goto done;
  326. }
  327. *output = ctx->image;
  328. *data_size = sizeof(AVPicture);
  329. ret = buf_size;
  330. done:
  331. opj_image_destroy(image);
  332. opj_destroy_decompress(dec);
  333. return ret;
  334. }
  335. static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
  336. {
  337. LibOpenJPEGContext *ctx = avctx->priv_data;
  338. if (ctx->image.data[0])
  339. ff_thread_release_buffer(avctx, &ctx->image);
  340. return 0;
  341. }
  342. #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
  343. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  344. static const AVOption options[] = {
  345. { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, VD },
  346. { NULL },
  347. };
  348. static const AVClass class = {
  349. .class_name = "libopenjpeg",
  350. .item_name = av_default_item_name,
  351. .option = options,
  352. .version = LIBAVUTIL_VERSION_INT,
  353. };
  354. AVCodec ff_libopenjpeg_decoder = {
  355. .name = "libopenjpeg",
  356. .type = AVMEDIA_TYPE_VIDEO,
  357. .id = AV_CODEC_ID_JPEG2000,
  358. .priv_data_size = sizeof(LibOpenJPEGContext),
  359. .init = libopenjpeg_decode_init,
  360. .close = libopenjpeg_decode_close,
  361. .decode = libopenjpeg_decode_frame,
  362. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  363. .max_lowres = 31,
  364. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
  365. .priv_class = &class,
  366. .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy),
  367. };