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.

252 lines
8.1KB

  1. /*
  2. * JPEG 2000 decoding support via OpenJPEG
  3. * Copyright (c) 2009 Jaikrishnan Menon <realityman@gmx.net>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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/opt.h"
  26. #include "libavutil/imgutils.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. typedef struct {
  35. AVClass *class;
  36. opj_dparameters_t dec_params;
  37. AVFrame image;
  38. int lowres;
  39. int lowqual;
  40. } LibOpenJPEGContext;
  41. static int check_image_attributes(opj_image_t *image)
  42. {
  43. return image->comps[0].dx == image->comps[1].dx &&
  44. image->comps[1].dx == image->comps[2].dx &&
  45. image->comps[0].dy == image->comps[1].dy &&
  46. image->comps[1].dy == image->comps[2].dy &&
  47. image->comps[0].prec == image->comps[1].prec &&
  48. image->comps[1].prec == image->comps[2].prec;
  49. }
  50. static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
  51. {
  52. LibOpenJPEGContext *ctx = avctx->priv_data;
  53. opj_set_default_decoder_parameters(&ctx->dec_params);
  54. avctx->coded_frame = &ctx->image;
  55. return 0;
  56. }
  57. static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
  58. {
  59. LibOpenJPEGContext *ctx = avctx->priv_data;
  60. avctx->coded_frame = &ctx->image;
  61. return 0;
  62. }
  63. static int libopenjpeg_decode_frame(AVCodecContext *avctx,
  64. void *data, int *data_size,
  65. AVPacket *avpkt)
  66. {
  67. uint8_t *buf = avpkt->data;
  68. int buf_size = avpkt->size;
  69. LibOpenJPEGContext *ctx = avctx->priv_data;
  70. AVFrame *picture = &ctx->image, *output = data;
  71. opj_dinfo_t *dec;
  72. opj_cio_t *stream;
  73. opj_image_t *image;
  74. int width, height, has_alpha = 0, ret = -1;
  75. int x, y, index;
  76. uint8_t *img_ptr;
  77. int adjust[4];
  78. *data_size = 0;
  79. // Check if input is a raw jpeg2k codestream or in jp2 wrapping
  80. if((AV_RB32(buf) == 12) &&
  81. (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
  82. (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
  83. dec = opj_create_decompress(CODEC_JP2);
  84. } else {
  85. // If the AVPacket contains a jp2c box, then skip to
  86. // the starting byte of the codestream.
  87. if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
  88. buf += 8;
  89. dec = opj_create_decompress(CODEC_J2K);
  90. }
  91. if(!dec) {
  92. av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
  93. return -1;
  94. }
  95. opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
  96. ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
  97. ctx->dec_params.cp_reduce = ctx->lowres;
  98. ctx->dec_params.cp_layer = ctx->lowqual;
  99. // Tie decoder with decoding parameters
  100. opj_setup_decoder(dec, &ctx->dec_params);
  101. stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
  102. if(!stream) {
  103. av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
  104. opj_destroy_decompress(dec);
  105. return -1;
  106. }
  107. // Decode the header only
  108. image = opj_decode_with_info(dec, stream, NULL);
  109. opj_cio_close(stream);
  110. if(!image) {
  111. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  112. opj_destroy_decompress(dec);
  113. return -1;
  114. }
  115. width = image->x1 - image->x0;
  116. height = image->y1 - image->y0;
  117. if (ctx->lowres) {
  118. width = (width + (1 << ctx->lowres) - 1) >> ctx->lowres;
  119. height = (height + (1 << ctx->lowres) - 1) >> ctx->lowres;
  120. }
  121. if(av_image_check_size(width, height, 0, avctx) < 0) {
  122. av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height);
  123. goto done;
  124. }
  125. avcodec_set_dimensions(avctx, width, height);
  126. switch(image->numcomps)
  127. {
  128. case 1: avctx->pix_fmt = PIX_FMT_GRAY8;
  129. break;
  130. case 3: if(check_image_attributes(image)) {
  131. avctx->pix_fmt = PIX_FMT_RGB24;
  132. } else {
  133. avctx->pix_fmt = PIX_FMT_GRAY8;
  134. av_log(avctx, AV_LOG_ERROR, "Only first component will be used.\n");
  135. }
  136. break;
  137. case 4: has_alpha = 1;
  138. avctx->pix_fmt = PIX_FMT_RGBA;
  139. break;
  140. default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps);
  141. goto done;
  142. }
  143. if(picture->data[0])
  144. ff_thread_release_buffer(avctx, picture);
  145. if(ff_thread_get_buffer(avctx, picture) < 0){
  146. av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
  147. return -1;
  148. }
  149. ff_thread_finish_setup(avctx);
  150. ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
  151. // Tie decoder with decoding parameters
  152. opj_setup_decoder(dec, &ctx->dec_params);
  153. stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
  154. if(!stream) {
  155. av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
  156. opj_destroy_decompress(dec);
  157. return -1;
  158. }
  159. // Decode the codestream
  160. image = opj_decode_with_info(dec, stream, NULL);
  161. opj_cio_close(stream);
  162. for(x = 0; x < image->numcomps; x++) {
  163. adjust[x] = FFMAX(image->comps[x].prec - 8, 0);
  164. }
  165. for(y = 0; y < avctx->height; y++) {
  166. index = y*avctx->width;
  167. img_ptr = picture->data[0] + y*picture->linesize[0];
  168. for(x = 0; x < avctx->width; x++, index++) {
  169. *img_ptr++ = image->comps[0].data[index] >> adjust[0];
  170. if(image->numcomps > 2 && check_image_attributes(image)) {
  171. *img_ptr++ = image->comps[1].data[index] >> adjust[1];
  172. *img_ptr++ = image->comps[2].data[index] >> adjust[2];
  173. if(has_alpha)
  174. *img_ptr++ = image->comps[3].data[index] >> adjust[3];
  175. }
  176. }
  177. }
  178. *output = ctx->image;
  179. *data_size = sizeof(AVPicture);
  180. ret = buf_size;
  181. done:
  182. opj_image_destroy(image);
  183. opj_destroy_decompress(dec);
  184. return ret;
  185. }
  186. static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
  187. {
  188. LibOpenJPEGContext *ctx = avctx->priv_data;
  189. if(ctx->image.data[0])
  190. ff_thread_release_buffer(avctx, &ctx->image);
  191. return 0 ;
  192. }
  193. #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
  194. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  195. static const AVOption options[] = {
  196. { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, VD },
  197. { "lowres", "Lower the decoding resolution by a power of two", OFFSET(lowres), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, VD },
  198. { NULL },
  199. };
  200. static const AVClass class = {
  201. .class_name = "libopenjpeg",
  202. .item_name = av_default_item_name,
  203. .option = options,
  204. .version = LIBAVUTIL_VERSION_INT,
  205. };
  206. AVCodec ff_libopenjpeg_decoder = {
  207. .name = "libopenjpeg",
  208. .type = AVMEDIA_TYPE_VIDEO,
  209. .id = CODEC_ID_JPEG2000,
  210. .priv_data_size = sizeof(LibOpenJPEGContext),
  211. .init = libopenjpeg_decode_init,
  212. .close = libopenjpeg_decode_close,
  213. .decode = libopenjpeg_decode_frame,
  214. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  215. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
  216. .priv_class = &class,
  217. .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy),
  218. };