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.

394 lines
13KB

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