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.

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