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.

227 lines
7.2KB

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