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.

348 lines
10KB

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