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.

357 lines
11KB

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