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.

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