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.

435 lines
13KB

  1. /*
  2. * JPEG 2000 encoding support via OpenJPEG
  3. * Copyright (c) 2011 Michael Bradshaw <mbradshaw@sorensonmedia.com>
  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 encoder using libopenjpeg
  24. */
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/avassert.h"
  27. #include "avcodec.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "internal.h"
  30. #define OPJ_STATIC
  31. #include <openjpeg.h>
  32. typedef struct {
  33. opj_image_t *image;
  34. opj_cparameters_t enc_params;
  35. opj_cinfo_t *compress;
  36. opj_event_mgr_t event_mgr;
  37. } LibOpenJPEGContext;
  38. static void error_callback(const char *msg, void *data)
  39. {
  40. av_log((AVCodecContext*)data, AV_LOG_ERROR, "libopenjpeg: %s\n", msg);
  41. }
  42. static void warning_callback(const char *msg, void *data)
  43. {
  44. av_log((AVCodecContext*)data, AV_LOG_WARNING, "libopenjpeg: %s\n", msg);
  45. }
  46. static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
  47. {
  48. opj_image_cmptparm_t *cmptparm;
  49. opj_image_t *img;
  50. int i;
  51. int bpp = 8;
  52. int sub_dx[4];
  53. int sub_dy[4];
  54. int numcomps;
  55. OPJ_COLOR_SPACE color_space = CLRSPC_UNKNOWN;
  56. sub_dx[0] = sub_dx[3] = 1;
  57. sub_dy[0] = sub_dy[3] = 1;
  58. sub_dx[1] = sub_dx[2] = 1<<av_pix_fmt_descriptors[avctx->pix_fmt].log2_chroma_w;
  59. sub_dy[1] = sub_dy[2] = 1<<av_pix_fmt_descriptors[avctx->pix_fmt].log2_chroma_h;
  60. numcomps = av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
  61. switch (avctx->pix_fmt) {
  62. case PIX_FMT_GRAY8:
  63. color_space = CLRSPC_GRAY;
  64. break;
  65. case PIX_FMT_GRAY8A:
  66. color_space = CLRSPC_GRAY;
  67. break;
  68. case PIX_FMT_GRAY16:
  69. color_space = CLRSPC_GRAY;
  70. bpp = 16;
  71. break;
  72. case PIX_FMT_RGB24:
  73. color_space = CLRSPC_SRGB;
  74. break;
  75. case PIX_FMT_RGBA:
  76. color_space = CLRSPC_SRGB;
  77. break;
  78. case PIX_FMT_RGB48:
  79. color_space = CLRSPC_SRGB;
  80. bpp = 16;
  81. break;
  82. case PIX_FMT_RGBA64:
  83. color_space = CLRSPC_SRGB;
  84. bpp = 16;
  85. break;
  86. case PIX_FMT_YUV420P:
  87. color_space = CLRSPC_SYCC;
  88. break;
  89. case PIX_FMT_YUV422P:
  90. color_space = CLRSPC_SYCC;
  91. break;
  92. case PIX_FMT_YUV440P:
  93. color_space = CLRSPC_SYCC;
  94. break;
  95. case PIX_FMT_YUV444P:
  96. color_space = CLRSPC_SYCC;
  97. break;
  98. case PIX_FMT_YUVA420P:
  99. color_space = CLRSPC_SYCC;
  100. break;
  101. case PIX_FMT_YUV420P9:
  102. case PIX_FMT_YUV422P9:
  103. case PIX_FMT_YUV444P9:
  104. color_space = CLRSPC_SYCC;
  105. bpp = 9;
  106. break;
  107. case PIX_FMT_YUV420P10:
  108. case PIX_FMT_YUV422P10:
  109. case PIX_FMT_YUV444P10:
  110. color_space = CLRSPC_SYCC;
  111. bpp = 10;
  112. break;
  113. case PIX_FMT_YUV420P16:
  114. case PIX_FMT_YUV422P16:
  115. case PIX_FMT_YUV444P16:
  116. color_space = CLRSPC_SYCC;
  117. bpp = 16;
  118. break;
  119. default:
  120. av_log(avctx, AV_LOG_ERROR, "The requested pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
  121. return NULL;
  122. }
  123. cmptparm = av_mallocz(numcomps * sizeof(opj_image_cmptparm_t));
  124. if (!cmptparm) {
  125. av_log(avctx, AV_LOG_ERROR, "Not enough memory");
  126. return NULL;
  127. }
  128. for (i = 0; i < numcomps; i++) {
  129. cmptparm[i].prec = bpp;
  130. cmptparm[i].bpp = bpp;
  131. cmptparm[i].sgnd = 0;
  132. cmptparm[i].dx = sub_dx[i];
  133. cmptparm[i].dy = sub_dy[i];
  134. cmptparm[i].w = avctx->width / sub_dx[i];
  135. cmptparm[i].h = avctx->height / sub_dy[i];
  136. }
  137. img = opj_image_create(numcomps, cmptparm, color_space);
  138. av_freep(&cmptparm);
  139. return img;
  140. }
  141. static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
  142. {
  143. LibOpenJPEGContext *ctx = avctx->priv_data;
  144. opj_set_default_encoder_parameters(&ctx->enc_params);
  145. ctx->enc_params.tcp_numlayers = 1;
  146. ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
  147. ctx->enc_params.cp_disto_alloc = 1;
  148. ctx->compress = opj_create_compress(CODEC_J2K);
  149. if (!ctx->compress) {
  150. av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
  151. return AVERROR(ENOMEM);
  152. }
  153. avctx->coded_frame = avcodec_alloc_frame();
  154. if (!avctx->coded_frame) {
  155. av_freep(&ctx->compress);
  156. av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
  157. return AVERROR(ENOMEM);
  158. }
  159. ctx->image = mj2_create_image(avctx, &ctx->enc_params);
  160. if (!ctx->image) {
  161. av_freep(&ctx->compress);
  162. av_freep(&avctx->coded_frame);
  163. av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
  164. return AVERROR(EINVAL);
  165. }
  166. memset(&ctx->event_mgr, 0, sizeof(opj_event_mgr_t));
  167. ctx->event_mgr.error_handler = error_callback;
  168. ctx->event_mgr.warning_handler = warning_callback;
  169. ctx->event_mgr.info_handler = NULL;
  170. opj_set_event_mgr((opj_common_ptr)ctx->compress, &ctx->event_mgr, avctx);
  171. return 0;
  172. }
  173. static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  174. {
  175. int compno;
  176. int x;
  177. int y;
  178. int image_index;
  179. int frame_index;
  180. const int numcomps = image->numcomps;
  181. for (compno = 0; compno < numcomps; ++compno) {
  182. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  183. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  184. return 0;
  185. }
  186. }
  187. for (compno = 0; compno < numcomps; ++compno) {
  188. for (y = 0; y < avctx->height; ++y) {
  189. image_index = y * avctx->width;
  190. frame_index = y * frame->linesize[0] + compno;
  191. for (x = 0; x < avctx->width; ++x) {
  192. image->comps[compno].data[image_index++] = frame->data[0][frame_index];
  193. frame_index += numcomps;
  194. }
  195. }
  196. }
  197. return 1;
  198. }
  199. static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  200. {
  201. int compno;
  202. int x;
  203. int y;
  204. int image_index;
  205. int frame_index;
  206. const int numcomps = image->numcomps;
  207. uint16_t *frame_ptr = (uint16_t*)frame->data[0];
  208. for (compno = 0; compno < numcomps; ++compno) {
  209. if (image->comps[compno].w > frame->linesize[0] / numcomps) {
  210. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  211. return 0;
  212. }
  213. }
  214. for (compno = 0; compno < numcomps; ++compno) {
  215. for (y = 0; y < avctx->height; ++y) {
  216. image_index = y * avctx->width;
  217. frame_index = y * (frame->linesize[0] / 2) + compno;
  218. for (x = 0; x < avctx->width; ++x) {
  219. image->comps[compno].data[image_index++] = frame_ptr[frame_index];
  220. frame_index += numcomps;
  221. }
  222. }
  223. }
  224. return 1;
  225. }
  226. static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  227. {
  228. int compno;
  229. int x;
  230. int y;
  231. int width;
  232. int height;
  233. int image_index;
  234. int frame_index;
  235. const int numcomps = image->numcomps;
  236. for (compno = 0; compno < numcomps; ++compno) {
  237. if (image->comps[compno].w > frame->linesize[compno]) {
  238. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  239. return 0;
  240. }
  241. }
  242. for (compno = 0; compno < numcomps; ++compno) {
  243. width = avctx->width / image->comps[compno].dx;
  244. height = avctx->height / image->comps[compno].dy;
  245. for (y = 0; y < height; ++y) {
  246. image_index = y * width;
  247. frame_index = y * frame->linesize[compno];
  248. for (x = 0; x < width; ++x) {
  249. image->comps[compno].data[image_index++] = frame->data[compno][frame_index++];
  250. }
  251. }
  252. }
  253. return 1;
  254. }
  255. static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
  256. {
  257. int compno;
  258. int x;
  259. int y;
  260. int width;
  261. int height;
  262. int image_index;
  263. int frame_index;
  264. const int numcomps = image->numcomps;
  265. uint16_t *frame_ptr;
  266. for (compno = 0; compno < numcomps; ++compno) {
  267. if (image->comps[compno].w > frame->linesize[compno]) {
  268. av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
  269. return 0;
  270. }
  271. }
  272. for (compno = 0; compno < numcomps; ++compno) {
  273. width = avctx->width / image->comps[compno].dx;
  274. height = avctx->height / image->comps[compno].dy;
  275. frame_ptr = (uint16_t*)frame->data[compno];
  276. for (y = 0; y < height; ++y) {
  277. image_index = y * width;
  278. frame_index = y * (frame->linesize[compno] / 2);
  279. for (x = 0; x < width; ++x) {
  280. image->comps[compno].data[image_index++] = frame_ptr[frame_index++];
  281. }
  282. }
  283. }
  284. return 1;
  285. }
  286. static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  287. const AVFrame *frame, int *got_packet)
  288. {
  289. LibOpenJPEGContext *ctx = avctx->priv_data;
  290. opj_cinfo_t *compress = ctx->compress;
  291. opj_image_t *image = ctx->image;
  292. opj_cio_t *stream;
  293. int cpyresult = 0;
  294. int ret, len;
  295. // x0, y0 is the top left corner of the image
  296. // x1, y1 is the width, height of the reference grid
  297. image->x0 = 0;
  298. image->y0 = 0;
  299. image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
  300. image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
  301. switch (avctx->pix_fmt) {
  302. case PIX_FMT_RGB24:
  303. case PIX_FMT_RGBA:
  304. case PIX_FMT_GRAY8A:
  305. cpyresult = libopenjpeg_copy_packed8(avctx, frame, image);
  306. break;
  307. case PIX_FMT_RGB48:
  308. case PIX_FMT_RGBA64:
  309. cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
  310. break;
  311. case PIX_FMT_GRAY8:
  312. case PIX_FMT_YUV420P:
  313. case PIX_FMT_YUV422P:
  314. case PIX_FMT_YUV440P:
  315. case PIX_FMT_YUV444P:
  316. case PIX_FMT_YUVA420P:
  317. cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
  318. break;
  319. case PIX_FMT_GRAY16:
  320. case PIX_FMT_YUV420P9:
  321. case PIX_FMT_YUV420P10:
  322. case PIX_FMT_YUV420P16:
  323. case PIX_FMT_YUV422P9:
  324. case PIX_FMT_YUV422P10:
  325. case PIX_FMT_YUV422P16:
  326. case PIX_FMT_YUV444P9:
  327. case PIX_FMT_YUV444P10:
  328. case PIX_FMT_YUV444P16:
  329. cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
  330. break;
  331. default:
  332. av_log(avctx, AV_LOG_ERROR, "The frame's pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
  333. return AVERROR(EINVAL);
  334. break;
  335. }
  336. if (!cpyresult) {
  337. av_log(avctx, AV_LOG_ERROR, "Could not copy the frame data to the internal image buffer\n");
  338. return -1;
  339. }
  340. opj_setup_encoder(compress, &ctx->enc_params, image);
  341. stream = opj_cio_open((opj_common_ptr)compress, NULL, 0);
  342. if (!stream) {
  343. av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
  344. return AVERROR(ENOMEM);
  345. }
  346. if (!opj_encode(compress, stream, image, NULL)) {
  347. opj_cio_close(stream);
  348. av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
  349. return -1;
  350. }
  351. len = cio_tell(stream);
  352. if ((ret = ff_alloc_packet2(avctx, pkt, len)) < 0) {
  353. opj_cio_close(stream);
  354. return ret;
  355. }
  356. memcpy(pkt->data, stream->buffer, len);
  357. pkt->flags |= AV_PKT_FLAG_KEY;
  358. *got_packet = 1;
  359. opj_cio_close(stream);
  360. return 0;
  361. }
  362. static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
  363. {
  364. LibOpenJPEGContext *ctx = avctx->priv_data;
  365. opj_destroy_compress(ctx->compress);
  366. opj_image_destroy(ctx->image);
  367. av_freep(&avctx->coded_frame);
  368. return 0 ;
  369. }
  370. AVCodec ff_libopenjpeg_encoder = {
  371. .name = "libopenjpeg",
  372. .type = AVMEDIA_TYPE_VIDEO,
  373. .id = CODEC_ID_JPEG2000,
  374. .priv_data_size = sizeof(LibOpenJPEGContext),
  375. .init = libopenjpeg_encode_init,
  376. .encode2 = libopenjpeg_encode_frame,
  377. .close = libopenjpeg_encode_close,
  378. .capabilities = 0,
  379. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24,PIX_FMT_RGBA,PIX_FMT_RGB48,PIX_FMT_RGBA64,
  380. PIX_FMT_GRAY8,PIX_FMT_GRAY8A,PIX_FMT_GRAY16,
  381. PIX_FMT_YUV420P,PIX_FMT_YUV422P,PIX_FMT_YUVA420P,
  382. PIX_FMT_YUV440P,PIX_FMT_YUV444P,
  383. PIX_FMT_YUV420P9,PIX_FMT_YUV422P9,PIX_FMT_YUV444P9,
  384. PIX_FMT_YUV420P10,PIX_FMT_YUV422P10,PIX_FMT_YUV444P10,
  385. PIX_FMT_YUV420P16,PIX_FMT_YUV422P16,PIX_FMT_YUV444P16,
  386. PIX_FMT_NONE},
  387. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
  388. } ;