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.

318 lines
9.5KB

  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 "libavutil/pixfmt.h"
  27. #include "avcodec.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "thread.h"
  30. #define OPJ_STATIC
  31. #include <openjpeg.h>
  32. #define JP2_SIG_TYPE 0x6A502020
  33. #define JP2_SIG_VALUE 0x0D0A870A
  34. typedef struct {
  35. opj_dparameters_t dec_params;
  36. AVFrame image;
  37. } LibOpenJPEGContext;
  38. static enum PixelFormat check_image_attributes(AVCodecContext *avctx, opj_image_t *image)
  39. {
  40. opj_image_comp_t c0 = image->comps[0];
  41. opj_image_comp_t c1 = image->comps[1];
  42. opj_image_comp_t c2 = image->comps[2];
  43. int compRatio = 0;
  44. compRatio |= c0.dx << 15 | c0.dy << 12;
  45. compRatio |= c1.dx << 9 | c1.dy << 6;
  46. compRatio |= c2.dx << 3 | c2.dy;
  47. switch (compRatio) {
  48. case 0111111: goto libopenjpeg_yuv444_rgb;
  49. case 0112121: goto libopenjpeg_yuv422;
  50. case 0112222: goto libopenjpeg_yuv420;
  51. default: return PIX_FMT_RGB24;
  52. }
  53. libopenjpeg_yuv420:
  54. switch (c0.prec) {
  55. case 8: return PIX_FMT_YUV420P;
  56. case 9: return PIX_FMT_YUV420P9;
  57. case 10: return PIX_FMT_YUV420P10;
  58. case 16: return PIX_FMT_YUV420P16;
  59. }
  60. libopenjpeg_yuv422:
  61. switch (c0.prec) {
  62. case 8: return PIX_FMT_YUV422P;
  63. case 9: return PIX_FMT_YUV422P9;
  64. case 10: return PIX_FMT_YUV422P10;
  65. case 16: return PIX_FMT_YUV422P16;
  66. }
  67. libopenjpeg_yuv444_rgb:
  68. switch (c0.prec) {
  69. case 8: return PIX_FMT_RGB24;
  70. case 9: return PIX_FMT_YUV444P9;
  71. case 10: return PIX_FMT_YUV444P10;
  72. case 16: return PIX_FMT_YUV444P16;
  73. }
  74. return PIX_FMT_RGB24;
  75. }
  76. static inline int libopenjpeg_ispacked(enum PixelFormat pix_fmt) {
  77. int i, component_plane;
  78. component_plane = av_pix_fmt_descriptors[pix_fmt].comp[0].plane;
  79. for(i = 1; i < av_pix_fmt_descriptors[pix_fmt].nb_components; i++) {
  80. if (component_plane != av_pix_fmt_descriptors[pix_fmt].comp[i].plane)
  81. return 0;
  82. }
  83. return 1;
  84. }
  85. static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
  86. uint8_t *img_ptr;
  87. int index, x, y, c;
  88. for(y = 0; y < picture->height; y++) {
  89. index = y*picture->width;
  90. img_ptr = picture->data[0] + y*picture->linesize[0];
  91. for(x = 0; x < picture->width; x++, index++) {
  92. for(c = 0; c < image->numcomps; c++) {
  93. *img_ptr++ = image->comps[c].data[index];
  94. }
  95. }
  96. }
  97. }
  98. static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
  99. int *comp_data;
  100. uint8_t *img_ptr;
  101. int index, x, y;
  102. for(index = 0; index < image->numcomps; index++) {
  103. comp_data = image->comps[index].data;
  104. img_ptr = picture->data[index];
  105. for(y = 0; y < image->comps[index].h; y++) {
  106. for(x = 0; x < image->comps[index].w; x++) {
  107. *img_ptr = (uint8_t) *comp_data;
  108. img_ptr++;
  109. comp_data++;
  110. }
  111. }
  112. }
  113. }
  114. static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
  115. int *comp_data;
  116. uint16_t *img_ptr;
  117. int index, x, y;
  118. for(index = 0; index < image->numcomps; index++) {
  119. comp_data = image->comps[index].data;
  120. img_ptr = (uint16_t*) picture->data[index];
  121. for(y = 0; y < image->comps[index].h; y++) {
  122. for(x = 0; x < image->comps[index].w; x++) {
  123. *img_ptr = *comp_data;
  124. img_ptr++;
  125. comp_data++;
  126. }
  127. }
  128. }
  129. }
  130. static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
  131. {
  132. LibOpenJPEGContext *ctx = avctx->priv_data;
  133. opj_set_default_decoder_parameters(&ctx->dec_params);
  134. avcodec_get_frame_defaults(&ctx->image);
  135. avctx->coded_frame = &ctx->image;
  136. return 0;
  137. }
  138. static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
  139. {
  140. LibOpenJPEGContext *ctx = avctx->priv_data;
  141. avctx->coded_frame = &ctx->image;
  142. return 0;
  143. }
  144. static int libopenjpeg_decode_frame(AVCodecContext *avctx,
  145. void *data, int *data_size,
  146. AVPacket *avpkt)
  147. {
  148. uint8_t *buf = avpkt->data;
  149. int buf_size = avpkt->size;
  150. LibOpenJPEGContext *ctx = avctx->priv_data;
  151. AVFrame *picture = &ctx->image, *output = data;
  152. opj_dinfo_t *dec;
  153. opj_cio_t *stream;
  154. opj_image_t *image;
  155. int width, height, ret = -1;
  156. int pixel_size = 0;
  157. int ispacked = 0;
  158. *data_size = 0;
  159. // Check if input is a raw jpeg2k codestream or in jp2 wrapping
  160. if((AV_RB32(buf) == 12) &&
  161. (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
  162. (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
  163. dec = opj_create_decompress(CODEC_JP2);
  164. } else {
  165. // If the AVPacket contains a jp2c box, then skip to
  166. // the starting byte of the codestream.
  167. if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
  168. buf += 8;
  169. dec = opj_create_decompress(CODEC_J2K);
  170. }
  171. if(!dec) {
  172. av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
  173. return -1;
  174. }
  175. opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
  176. ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
  177. // Tie decoder with decoding parameters
  178. opj_setup_decoder(dec, &ctx->dec_params);
  179. stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
  180. if(!stream) {
  181. av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
  182. opj_destroy_decompress(dec);
  183. return -1;
  184. }
  185. // Decode the header only
  186. image = opj_decode_with_info(dec, stream, NULL);
  187. opj_cio_close(stream);
  188. if(!image) {
  189. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  190. opj_destroy_decompress(dec);
  191. return -1;
  192. }
  193. width = image->x1 - image->x0;
  194. height = image->y1 - image->y0;
  195. if(av_image_check_size(width, height, 0, avctx) < 0) {
  196. av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height);
  197. goto done;
  198. }
  199. avcodec_set_dimensions(avctx, width, height);
  200. switch (image->numcomps) {
  201. case 1: avctx->pix_fmt = PIX_FMT_GRAY8;
  202. break;
  203. case 3: avctx->pix_fmt = check_image_attributes(avctx, image);
  204. break;
  205. case 4: avctx->pix_fmt = PIX_FMT_RGBA;
  206. break;
  207. default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps);
  208. goto done;
  209. }
  210. if(picture->data[0])
  211. ff_thread_release_buffer(avctx, picture);
  212. if(ff_thread_get_buffer(avctx, picture) < 0){
  213. av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
  214. return -1;
  215. }
  216. ff_thread_finish_setup(avctx);
  217. ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
  218. ctx->dec_params.cp_reduce = avctx->lowres;
  219. // Tie decoder with decoding parameters
  220. opj_setup_decoder(dec, &ctx->dec_params);
  221. stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
  222. if(!stream) {
  223. av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
  224. opj_destroy_decompress(dec);
  225. return -1;
  226. }
  227. // Decode the codestream
  228. image = opj_decode_with_info(dec, stream, NULL);
  229. opj_cio_close(stream);
  230. pixel_size = av_pix_fmt_descriptors[avctx->pix_fmt].comp[0].step_minus1 + 1;
  231. ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
  232. switch (pixel_size) {
  233. case 1:
  234. if (ispacked) {
  235. libopenjpeg_copy_to_packed8(picture, image);
  236. } else {
  237. libopenjpeg_copyto8(picture, image);
  238. }
  239. break;
  240. case 2:
  241. libopenjpeg_copyto16(picture, image);
  242. break;
  243. case 3:
  244. if (ispacked) {
  245. libopenjpeg_copy_to_packed8(picture, image);
  246. }
  247. break;
  248. default:
  249. av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
  250. goto done;
  251. }
  252. *output = ctx->image;
  253. *data_size = sizeof(AVPicture);
  254. ret = buf_size;
  255. done:
  256. opj_image_destroy(image);
  257. opj_destroy_decompress(dec);
  258. return ret;
  259. }
  260. static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
  261. {
  262. LibOpenJPEGContext *ctx = avctx->priv_data;
  263. if(ctx->image.data[0])
  264. ff_thread_release_buffer(avctx, &ctx->image);
  265. return 0 ;
  266. }
  267. AVCodec ff_libopenjpeg_decoder = {
  268. .name = "libopenjpeg",
  269. .type = AVMEDIA_TYPE_VIDEO,
  270. .id = CODEC_ID_JPEG2000,
  271. .priv_data_size = sizeof(LibOpenJPEGContext),
  272. .init = libopenjpeg_decode_init,
  273. .close = libopenjpeg_decode_close,
  274. .decode = libopenjpeg_decode_frame,
  275. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  276. .max_lowres = 5,
  277. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
  278. .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy)
  279. };