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.

194 lines
6.0KB

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