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.

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