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.

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