libopenjpeg.c 5.8 KB

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