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.

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