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.

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