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.

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