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.

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