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.

518 lines
16KB

  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/common.h"
  26. #include "libavutil/imgutils.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/pixfmt.h"
  30. #include "avcodec.h"
  31. #include "internal.h"
  32. #include "thread.h"
  33. #include <openjpeg.h>
  34. #define JP2_SIG_TYPE 0x6A502020
  35. #define JP2_SIG_VALUE 0x0D0A870A
  36. // pix_fmts with lower bpp have to be listed before
  37. // similar pix_fmts with higher bpp.
  38. #define RGB_PIXEL_FORMATS AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, \
  39. AV_PIX_FMT_RGB48, AV_PIX_FMT_RGBA64
  40. #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8, AV_PIX_FMT_YA8, \
  41. AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, \
  42. AV_PIX_FMT_GRAY16, AV_PIX_FMT_YA16
  43. #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUVA420P, \
  44. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P, \
  45. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, \
  46. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, \
  47. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9, \
  48. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, \
  49. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10, \
  50. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, \
  51. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14, \
  52. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, \
  53. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16
  54. #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
  55. static const enum AVPixelFormat libopenjpeg_rgb_pix_fmts[] = {
  56. RGB_PIXEL_FORMATS
  57. };
  58. static const enum AVPixelFormat libopenjpeg_gray_pix_fmts[] = {
  59. GRAY_PIXEL_FORMATS
  60. };
  61. static const enum AVPixelFormat libopenjpeg_yuv_pix_fmts[] = {
  62. YUV_PIXEL_FORMATS
  63. };
  64. static const enum AVPixelFormat libopenjpeg_all_pix_fmts[] = {
  65. RGB_PIXEL_FORMATS, GRAY_PIXEL_FORMATS, YUV_PIXEL_FORMATS, XYZ_PIXEL_FORMATS
  66. };
  67. typedef struct LibOpenJPEGContext {
  68. AVClass *class;
  69. opj_dparameters_t dec_params;
  70. int lowqual;
  71. } LibOpenJPEGContext;
  72. static void error_callback(const char *msg, void *data)
  73. {
  74. av_log(data, AV_LOG_ERROR, "%s", msg);
  75. }
  76. static void warning_callback(const char *msg, void *data)
  77. {
  78. av_log(data, AV_LOG_WARNING, "%s", msg);
  79. }
  80. static void info_callback(const char *msg, void *data)
  81. {
  82. av_log(data, AV_LOG_DEBUG, "%s", msg);
  83. }
  84. typedef struct BufferReader {
  85. int pos;
  86. int size;
  87. const uint8_t *buffer;
  88. } BufferReader;
  89. static OPJ_SIZE_T stream_read(void *out_buffer, OPJ_SIZE_T nb_bytes, void *user_data)
  90. {
  91. BufferReader *reader = user_data;
  92. int remaining;
  93. if (reader->pos == reader->size) {
  94. return (OPJ_SIZE_T)-1;
  95. }
  96. remaining = reader->size - reader->pos;
  97. if (nb_bytes > remaining) {
  98. nb_bytes = remaining;
  99. }
  100. memcpy(out_buffer, reader->buffer + reader->pos, nb_bytes);
  101. reader->pos += (int)nb_bytes;
  102. return nb_bytes;
  103. }
  104. static OPJ_OFF_T stream_skip(OPJ_OFF_T nb_bytes, void *user_data)
  105. {
  106. BufferReader *reader = user_data;
  107. if (nb_bytes < 0) {
  108. if (reader->pos == 0) {
  109. return (OPJ_SIZE_T)-1;
  110. }
  111. if (nb_bytes + reader->pos < 0) {
  112. nb_bytes = -reader->pos;
  113. }
  114. } else {
  115. int remaining;
  116. if (reader->pos == reader->size) {
  117. return (OPJ_SIZE_T)-1;
  118. }
  119. remaining = reader->size - reader->pos;
  120. if (nb_bytes > remaining) {
  121. nb_bytes = remaining;
  122. }
  123. }
  124. reader->pos += (int)nb_bytes;
  125. return nb_bytes;
  126. }
  127. static OPJ_BOOL stream_seek(OPJ_OFF_T nb_bytes, void *user_data)
  128. {
  129. BufferReader *reader = user_data;
  130. if (nb_bytes < 0 || nb_bytes > reader->size) {
  131. return OPJ_FALSE;
  132. }
  133. reader->pos = (int)nb_bytes;
  134. return OPJ_TRUE;
  135. }
  136. static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum AVPixelFormat pix_fmt)
  137. {
  138. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  139. int match = 1;
  140. if (desc->nb_components != image->numcomps) {
  141. return 0;
  142. }
  143. switch (desc->nb_components) {
  144. case 4:
  145. match = match &&
  146. desc->comp[3].depth >= image->comps[3].prec &&
  147. 1 == image->comps[3].dx &&
  148. 1 == image->comps[3].dy;
  149. case 3:
  150. match = match &&
  151. desc->comp[2].depth >= image->comps[2].prec &&
  152. 1 << desc->log2_chroma_w == image->comps[2].dx &&
  153. 1 << desc->log2_chroma_h == image->comps[2].dy;
  154. case 2:
  155. match = match &&
  156. desc->comp[1].depth >= image->comps[1].prec &&
  157. 1 << desc->log2_chroma_w == image->comps[1].dx &&
  158. 1 << desc->log2_chroma_h == image->comps[1].dy;
  159. case 1:
  160. match = match &&
  161. desc->comp[0].depth >= image->comps[0].prec &&
  162. 1 == image->comps[0].dx &&
  163. 1 == image->comps[0].dy;
  164. default:
  165. break;
  166. }
  167. return match;
  168. }
  169. static inline enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
  170. int index;
  171. const enum AVPixelFormat *possible_fmts = NULL;
  172. int possible_fmts_nb = 0;
  173. switch (image->color_space) {
  174. case OPJ_CLRSPC_SRGB:
  175. possible_fmts = libopenjpeg_rgb_pix_fmts;
  176. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_rgb_pix_fmts);
  177. break;
  178. case OPJ_CLRSPC_GRAY:
  179. possible_fmts = libopenjpeg_gray_pix_fmts;
  180. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_gray_pix_fmts);
  181. break;
  182. case OPJ_CLRSPC_SYCC:
  183. possible_fmts = libopenjpeg_yuv_pix_fmts;
  184. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_yuv_pix_fmts);
  185. break;
  186. default:
  187. possible_fmts = libopenjpeg_all_pix_fmts;
  188. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_all_pix_fmts);
  189. break;
  190. }
  191. for (index = 0; index < possible_fmts_nb; ++index)
  192. if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
  193. return possible_fmts[index];
  194. }
  195. return AV_PIX_FMT_NONE;
  196. }
  197. static inline int libopenjpeg_ispacked(enum AVPixelFormat pix_fmt)
  198. {
  199. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  200. int i, component_plane;
  201. if (pix_fmt == AV_PIX_FMT_GRAY16)
  202. return 0;
  203. component_plane = desc->comp[0].plane;
  204. for (i = 1; i < desc->nb_components; i++)
  205. if (component_plane != desc->comp[i].plane)
  206. return 0;
  207. return 1;
  208. }
  209. static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
  210. uint8_t *img_ptr;
  211. int index, x, y, c;
  212. for (y = 0; y < picture->height; y++) {
  213. index = y * picture->width;
  214. img_ptr = picture->data[0] + y * picture->linesize[0];
  215. for (x = 0; x < picture->width; x++, index++)
  216. for (c = 0; c < image->numcomps; c++)
  217. *img_ptr++ = 0x80 * image->comps[c].sgnd + image->comps[c].data[index];
  218. }
  219. }
  220. static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
  221. uint16_t *img_ptr;
  222. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(picture->format);
  223. int index, x, y, c;
  224. int adjust[4];
  225. for (x = 0; x < image->numcomps; x++)
  226. adjust[x] = FFMAX(FFMIN(desc->comp[x].depth - image->comps[x].prec, 8), 0) + desc->comp[x].shift;
  227. for (y = 0; y < picture->height; y++) {
  228. index = y * picture->width;
  229. img_ptr = (uint16_t *) (picture->data[0] + y * picture->linesize[0]);
  230. for (x = 0; x < picture->width; x++, index++)
  231. for (c = 0; c < image->numcomps; c++)
  232. *img_ptr++ = (1 << image->comps[c].prec - 1) * image->comps[c].sgnd +
  233. (unsigned)image->comps[c].data[index] << adjust[c];
  234. }
  235. }
  236. static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
  237. int *comp_data;
  238. uint8_t *img_ptr;
  239. int index, x, y;
  240. for (index = 0; index < image->numcomps; index++) {
  241. comp_data = image->comps[index].data;
  242. for (y = 0; y < image->comps[index].h; y++) {
  243. img_ptr = picture->data[index] + y * picture->linesize[index];
  244. for (x = 0; x < image->comps[index].w; x++) {
  245. *img_ptr = 0x80 * image->comps[index].sgnd + *comp_data;
  246. img_ptr++;
  247. comp_data++;
  248. }
  249. }
  250. }
  251. }
  252. static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
  253. int *comp_data;
  254. uint16_t *img_ptr;
  255. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(picture->format);
  256. int index, x, y;
  257. int adjust[4];
  258. for (x = 0; x < image->numcomps; x++)
  259. adjust[x] = FFMAX(FFMIN(desc->comp[x].depth - image->comps[x].prec, 8), 0) + desc->comp[x].shift;
  260. for (index = 0; index < image->numcomps; index++) {
  261. comp_data = image->comps[index].data;
  262. for (y = 0; y < image->comps[index].h; y++) {
  263. img_ptr = (uint16_t *)(picture->data[index] + y * picture->linesize[index]);
  264. for (x = 0; x < image->comps[index].w; x++) {
  265. *img_ptr = (1 << image->comps[index].prec - 1) * image->comps[index].sgnd +
  266. (unsigned)*comp_data << adjust[index];
  267. img_ptr++;
  268. comp_data++;
  269. }
  270. }
  271. }
  272. }
  273. static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
  274. {
  275. LibOpenJPEGContext *ctx = avctx->priv_data;
  276. opj_set_default_decoder_parameters(&ctx->dec_params);
  277. return 0;
  278. }
  279. static int libopenjpeg_decode_frame(AVCodecContext *avctx,
  280. void *data, int *got_frame,
  281. AVPacket *avpkt)
  282. {
  283. uint8_t *buf = avpkt->data;
  284. int buf_size = avpkt->size;
  285. LibOpenJPEGContext *ctx = avctx->priv_data;
  286. ThreadFrame frame = { .f = data };
  287. AVFrame *picture = data;
  288. const AVPixFmtDescriptor *desc;
  289. int width, height, ret;
  290. int pixel_size = 0;
  291. int ispacked = 0;
  292. int i;
  293. opj_image_t *image = NULL;
  294. BufferReader reader = {0, avpkt->size, avpkt->data};
  295. opj_codec_t *dec = NULL;
  296. opj_stream_t *stream = NULL;
  297. *got_frame = 0;
  298. // Check if input is a raw jpeg2k codestream or in jp2 wrapping
  299. if ((AV_RB32(buf) == 12) &&
  300. (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
  301. (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
  302. dec = opj_create_decompress(OPJ_CODEC_JP2);
  303. } else {
  304. /* If the AVPacket contains a jp2c box, then skip to
  305. * the starting byte of the codestream. */
  306. if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
  307. buf += 8;
  308. dec = opj_create_decompress(OPJ_CODEC_J2K);
  309. }
  310. if (!dec) {
  311. av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
  312. ret = AVERROR_EXTERNAL;
  313. goto done;
  314. }
  315. if (!opj_set_error_handler(dec, error_callback, avctx) ||
  316. !opj_set_warning_handler(dec, warning_callback, avctx) ||
  317. !opj_set_info_handler(dec, info_callback, avctx)) {
  318. av_log(avctx, AV_LOG_ERROR, "Error setting decoder handlers.\n");
  319. ret = AVERROR_EXTERNAL;
  320. goto done;
  321. }
  322. ctx->dec_params.cp_layer = ctx->lowqual;
  323. ctx->dec_params.cp_reduce = avctx->lowres;
  324. // Tie decoder with decoding parameters
  325. opj_setup_decoder(dec, &ctx->dec_params);
  326. stream = opj_stream_default_create(OPJ_STREAM_READ);
  327. if (!stream) {
  328. av_log(avctx, AV_LOG_ERROR,
  329. "Codestream could not be opened for reading.\n");
  330. ret = AVERROR_EXTERNAL;
  331. goto done;
  332. }
  333. opj_stream_set_read_function(stream, stream_read);
  334. opj_stream_set_skip_function(stream, stream_skip);
  335. opj_stream_set_seek_function(stream, stream_seek);
  336. opj_stream_set_user_data(stream, &reader, NULL);
  337. opj_stream_set_user_data_length(stream, avpkt->size);
  338. // Decode the header only.
  339. ret = !opj_read_header(stream, dec, &image);
  340. if (ret) {
  341. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream header.\n");
  342. ret = AVERROR_EXTERNAL;
  343. goto done;
  344. }
  345. width = image->x1 - image->x0;
  346. height = image->y1 - image->y0;
  347. ret = ff_set_dimensions(avctx, width, height);
  348. if (ret < 0)
  349. goto done;
  350. if (avctx->pix_fmt != AV_PIX_FMT_NONE)
  351. if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
  352. avctx->pix_fmt = AV_PIX_FMT_NONE;
  353. if (avctx->pix_fmt == AV_PIX_FMT_NONE)
  354. avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
  355. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  356. av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format.\n");
  357. ret = AVERROR_UNKNOWN;
  358. goto done;
  359. }
  360. for (i = 0; i < image->numcomps; i++)
  361. if (image->comps[i].prec > avctx->bits_per_raw_sample)
  362. avctx->bits_per_raw_sample = image->comps[i].prec;
  363. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  364. goto done;
  365. ret = !opj_decode(dec, stream, image);
  366. if (ret) {
  367. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  368. ret = AVERROR_EXTERNAL;
  369. goto done;
  370. }
  371. for (i = 0; i < image->numcomps; i++) {
  372. if (!image->comps[i].data) {
  373. av_log(avctx, AV_LOG_ERROR,
  374. "Image component %d contains no data.\n", i);
  375. ret = AVERROR_INVALIDDATA;
  376. goto done;
  377. }
  378. }
  379. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  380. pixel_size = desc->comp[0].step;
  381. ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
  382. switch (pixel_size) {
  383. case 1:
  384. if (ispacked) {
  385. libopenjpeg_copy_to_packed8(picture, image);
  386. } else {
  387. libopenjpeg_copyto8(picture, image);
  388. }
  389. break;
  390. case 2:
  391. if (ispacked) {
  392. libopenjpeg_copy_to_packed8(picture, image);
  393. } else {
  394. libopenjpeg_copyto16(picture, image);
  395. }
  396. break;
  397. case 3:
  398. case 4:
  399. if (ispacked) {
  400. libopenjpeg_copy_to_packed8(picture, image);
  401. }
  402. break;
  403. case 6:
  404. case 8:
  405. if (ispacked) {
  406. libopenjpeg_copy_to_packed16(picture, image);
  407. }
  408. break;
  409. default:
  410. avpriv_report_missing_feature(avctx, "Pixel size %d", pixel_size);
  411. ret = AVERROR_PATCHWELCOME;
  412. goto done;
  413. }
  414. *got_frame = 1;
  415. picture->pict_type = AV_PICTURE_TYPE_I;
  416. picture->key_frame = 1;
  417. ret = buf_size;
  418. done:
  419. opj_image_destroy(image);
  420. opj_stream_destroy(stream);
  421. opj_destroy_codec(dec);
  422. return ret;
  423. }
  424. #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
  425. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  426. static const AVOption options[] = {
  427. { "lowqual", "Limit the number of layers used for decoding",
  428. OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
  429. { NULL },
  430. };
  431. static const AVClass openjpeg_class = {
  432. .class_name = "libopenjpeg",
  433. .item_name = av_default_item_name,
  434. .option = options,
  435. .version = LIBAVUTIL_VERSION_INT,
  436. };
  437. AVCodec ff_libopenjpeg_decoder = {
  438. .name = "libopenjpeg",
  439. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
  440. .type = AVMEDIA_TYPE_VIDEO,
  441. .id = AV_CODEC_ID_JPEG2000,
  442. .priv_data_size = sizeof(LibOpenJPEGContext),
  443. .init = libopenjpeg_decode_init,
  444. .decode = libopenjpeg_decode_frame,
  445. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  446. .max_lowres = 31,
  447. .priv_class = &openjpeg_class,
  448. .wrapper_name = "libopenjpeg",
  449. };