]> git.sesse.net Git - ffmpeg/blob - libavcodec/libopenjpegenc.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / libopenjpegenc.c
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 /**
23 * @file
24 * JPEG 2000 encoder using libopenjpeg
25 */
26
27 #include "libavutil/opt.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/avassert.h"
30 #include "avcodec.h"
31 #include "libavutil/intreadwrite.h"
32 #include "internal.h"
33 #define  OPJ_STATIC
34 #include <openjpeg.h>
35
36 typedef struct {
37     AVClass *avclass;
38     opj_image_t *image;
39     opj_cparameters_t enc_params;
40     opj_cinfo_t *compress;
41     opj_event_mgr_t event_mgr;
42     int format;
43     int profile;
44     int cinema_mode;
45     int prog_order;
46     int numresolution;
47     int numlayers;
48     int disto_alloc;
49     int fixed_alloc;
50     int fixed_quality;
51 } LibOpenJPEGContext;
52
53 static void error_callback(const char *msg, void *data)
54 {
55     av_log((AVCodecContext*)data, AV_LOG_ERROR, "libopenjpeg: %s\n", msg);
56 }
57
58 static void warning_callback(const char *msg, void *data)
59 {
60     av_log((AVCodecContext*)data, AV_LOG_WARNING, "libopenjpeg: %s\n", msg);
61 }
62
63 static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
64 {
65     opj_image_cmptparm_t *cmptparm;
66     opj_image_t *img;
67     int i;
68     int sub_dx[4];
69     int sub_dy[4];
70     int numcomps;
71     OPJ_COLOR_SPACE color_space = CLRSPC_UNKNOWN;
72
73     sub_dx[0] = sub_dx[3] = 1;
74     sub_dy[0] = sub_dy[3] = 1;
75     sub_dx[1] = sub_dx[2] = 1<<av_pix_fmt_descriptors[avctx->pix_fmt].log2_chroma_w;
76     sub_dy[1] = sub_dy[2] = 1<<av_pix_fmt_descriptors[avctx->pix_fmt].log2_chroma_h;
77
78     numcomps = av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
79
80     switch (avctx->pix_fmt) {
81     case PIX_FMT_GRAY8:
82     case PIX_FMT_GRAY8A:
83     case PIX_FMT_GRAY16:
84         color_space = CLRSPC_GRAY;
85         break;
86     case PIX_FMT_RGB24:
87     case PIX_FMT_RGBA:
88     case PIX_FMT_RGB48:
89     case PIX_FMT_RGBA64:
90         color_space = CLRSPC_SRGB;
91         break;
92     case PIX_FMT_YUV410P:
93     case PIX_FMT_YUV411P:
94     case PIX_FMT_YUV420P:
95     case PIX_FMT_YUV422P:
96     case PIX_FMT_YUV440P:
97     case PIX_FMT_YUV444P:
98     case PIX_FMT_YUVA420P:
99     case PIX_FMT_YUV420P9:
100     case PIX_FMT_YUV422P9:
101     case PIX_FMT_YUV444P9:
102     case PIX_FMT_YUV420P10:
103     case PIX_FMT_YUV422P10:
104     case PIX_FMT_YUV444P10:
105     case PIX_FMT_YUV420P16:
106     case PIX_FMT_YUV422P16:
107     case PIX_FMT_YUV444P16:
108         color_space = CLRSPC_SYCC;
109         break;
110     default:
111         av_log(avctx, AV_LOG_ERROR, "The requested pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
112         return NULL;
113     }
114
115     cmptparm = av_mallocz(numcomps * sizeof(opj_image_cmptparm_t));
116     if (!cmptparm) {
117         av_log(avctx, AV_LOG_ERROR, "Not enough memory");
118         return NULL;
119     }
120     for (i = 0; i < numcomps; i++) {
121         cmptparm[i].prec = av_pix_fmt_descriptors[avctx->pix_fmt].comp[i].depth_minus1 + 1;
122         cmptparm[i].bpp = av_pix_fmt_descriptors[avctx->pix_fmt].comp[i].depth_minus1 + 1;
123         cmptparm[i].sgnd = 0;
124         cmptparm[i].dx = sub_dx[i];
125         cmptparm[i].dy = sub_dy[i];
126         cmptparm[i].w = avctx->width / sub_dx[i];
127         cmptparm[i].h = avctx->height / sub_dy[i];
128     }
129
130     img = opj_image_create(numcomps, cmptparm, color_space);
131     av_freep(&cmptparm);
132     return img;
133 }
134
135 static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
136 {
137     LibOpenJPEGContext *ctx = avctx->priv_data;
138
139     opj_set_default_encoder_parameters(&ctx->enc_params);
140     ctx->enc_params.cp_rsiz = ctx->profile;
141     ctx->enc_params.mode = !!avctx->global_quality;
142     ctx->enc_params.cp_cinema = ctx->cinema_mode;
143     ctx->enc_params.prog_order = ctx->prog_order;
144     ctx->enc_params.numresolution = ctx->numresolution;
145     ctx->enc_params.cp_disto_alloc = ctx->disto_alloc;
146     ctx->enc_params.cp_fixed_alloc = ctx->fixed_alloc;
147     ctx->enc_params.cp_fixed_quality = ctx->fixed_quality;
148     ctx->enc_params.tcp_numlayers = ctx->numlayers;
149     ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
150
151     ctx->compress = opj_create_compress(ctx->format);
152     if (!ctx->compress) {
153         av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
154         return AVERROR(ENOMEM);
155     }
156
157     avctx->coded_frame = avcodec_alloc_frame();
158     if (!avctx->coded_frame) {
159         av_freep(&ctx->compress);
160         av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
161         return AVERROR(ENOMEM);
162     }
163
164     ctx->image = mj2_create_image(avctx, &ctx->enc_params);
165     if (!ctx->image) {
166         av_freep(&ctx->compress);
167         av_freep(&avctx->coded_frame);
168         av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
169         return AVERROR(EINVAL);
170     }
171
172     memset(&ctx->event_mgr, 0, sizeof(opj_event_mgr_t));
173     ctx->event_mgr.error_handler = error_callback;
174     ctx->event_mgr.warning_handler = warning_callback;
175     ctx->event_mgr.info_handler = NULL;
176     opj_set_event_mgr((opj_common_ptr)ctx->compress, &ctx->event_mgr, avctx);
177
178     return 0;
179 }
180
181 static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
182 {
183     int compno;
184     int x;
185     int y;
186     int image_index;
187     int frame_index;
188     const int numcomps = image->numcomps;
189
190     for (compno = 0; compno < numcomps; ++compno) {
191         if (image->comps[compno].w > frame->linesize[0] / numcomps) {
192             av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
193             return 0;
194         }
195     }
196
197     for (compno = 0; compno < numcomps; ++compno) {
198         for (y = 0; y < avctx->height; ++y) {
199             image_index = y * avctx->width;
200             frame_index = y * frame->linesize[0] + compno;
201             for (x = 0; x < avctx->width; ++x) {
202                 image->comps[compno].data[image_index++] = frame->data[0][frame_index];
203                 frame_index += numcomps;
204             }
205         }
206     }
207
208     return 1;
209 }
210
211 static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
212 {
213     int compno;
214     int x;
215     int y;
216     int image_index;
217     int frame_index;
218     const int numcomps = image->numcomps;
219     uint16_t *frame_ptr = (uint16_t*)frame->data[0];
220
221     for (compno = 0; compno < numcomps; ++compno) {
222         if (image->comps[compno].w > frame->linesize[0] / numcomps) {
223             av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
224             return 0;
225         }
226     }
227
228     for (compno = 0; compno < numcomps; ++compno) {
229         for (y = 0; y < avctx->height; ++y) {
230             image_index = y * avctx->width;
231             frame_index = y * (frame->linesize[0] / 2) + compno;
232             for (x = 0; x < avctx->width; ++x) {
233                 image->comps[compno].data[image_index++] = frame_ptr[frame_index];
234                 frame_index += numcomps;
235             }
236         }
237     }
238
239     return 1;
240 }
241
242 static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
243 {
244     int compno;
245     int x;
246     int y;
247     int width;
248     int height;
249     int image_index;
250     int frame_index;
251     const int numcomps = image->numcomps;
252
253     for (compno = 0; compno < numcomps; ++compno) {
254         if (image->comps[compno].w > frame->linesize[compno]) {
255             av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
256             return 0;
257         }
258     }
259
260     for (compno = 0; compno < numcomps; ++compno) {
261         width = avctx->width / image->comps[compno].dx;
262         height = avctx->height / image->comps[compno].dy;
263         for (y = 0; y < height; ++y) {
264             image_index = y * width;
265             frame_index = y * frame->linesize[compno];
266             for (x = 0; x < width; ++x) {
267                 image->comps[compno].data[image_index++] = frame->data[compno][frame_index++];
268             }
269         }
270     }
271
272     return 1;
273 }
274
275 static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
276 {
277     int compno;
278     int x;
279     int y;
280     int width;
281     int height;
282     int image_index;
283     int frame_index;
284     const int numcomps = image->numcomps;
285     uint16_t *frame_ptr;
286
287     for (compno = 0; compno < numcomps; ++compno) {
288         if (image->comps[compno].w > frame->linesize[compno]) {
289             av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
290             return 0;
291         }
292     }
293
294     for (compno = 0; compno < numcomps; ++compno) {
295         width = avctx->width / image->comps[compno].dx;
296         height = avctx->height / image->comps[compno].dy;
297         frame_ptr = (uint16_t*)frame->data[compno];
298         for (y = 0; y < height; ++y) {
299             image_index = y * width;
300             frame_index = y * (frame->linesize[compno] / 2);
301             for (x = 0; x < width; ++x) {
302                 image->comps[compno].data[image_index++] = frame_ptr[frame_index++];
303             }
304         }
305     }
306
307     return 1;
308 }
309
310 static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
311                                     const AVFrame *frame, int *got_packet)
312 {
313     LibOpenJPEGContext *ctx = avctx->priv_data;
314     opj_cinfo_t *compress = ctx->compress;
315     opj_image_t *image = ctx->image;
316     opj_cio_t *stream;
317     int cpyresult = 0;
318     int ret, len;
319
320     // x0, y0 is the top left corner of the image
321     // x1, y1 is the width, height of the reference grid
322     image->x0 = 0;
323     image->y0 = 0;
324     image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
325     image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
326
327     switch (avctx->pix_fmt) {
328     case PIX_FMT_RGB24:
329     case PIX_FMT_RGBA:
330     case PIX_FMT_GRAY8A:
331         cpyresult = libopenjpeg_copy_packed8(avctx, frame, image);
332         break;
333     case PIX_FMT_RGB48:
334     case PIX_FMT_RGBA64:
335         cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
336         break;
337     case PIX_FMT_GRAY8:
338     case PIX_FMT_YUV410P:
339     case PIX_FMT_YUV411P:
340     case PIX_FMT_YUV420P:
341     case PIX_FMT_YUV422P:
342     case PIX_FMT_YUV440P:
343     case PIX_FMT_YUV444P:
344     case PIX_FMT_YUVA420P:
345         cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
346         break;
347     case PIX_FMT_GRAY16:
348     case PIX_FMT_YUV420P9:
349     case PIX_FMT_YUV420P10:
350     case PIX_FMT_YUV420P16:
351     case PIX_FMT_YUV422P9:
352     case PIX_FMT_YUV422P10:
353     case PIX_FMT_YUV422P16:
354     case PIX_FMT_YUV444P9:
355     case PIX_FMT_YUV444P10:
356     case PIX_FMT_YUV444P16:
357         cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
358         break;
359     default:
360         av_log(avctx, AV_LOG_ERROR, "The frame's pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
361         return AVERROR(EINVAL);
362         break;
363     }
364
365     if (!cpyresult) {
366         av_log(avctx, AV_LOG_ERROR, "Could not copy the frame data to the internal image buffer\n");
367         return -1;
368     }
369
370     opj_setup_encoder(compress, &ctx->enc_params, image);
371     stream = opj_cio_open((opj_common_ptr)compress, NULL, 0);
372     if (!stream) {
373         av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
374         return AVERROR(ENOMEM);
375     }
376
377     if (!opj_encode(compress, stream, image, NULL)) {
378         opj_cio_close(stream);
379         av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
380         return -1;
381     }
382
383     len = cio_tell(stream);
384     if ((ret = ff_alloc_packet2(avctx, pkt, len)) < 0) {
385         opj_cio_close(stream);
386         return ret;
387     }
388
389     memcpy(pkt->data, stream->buffer, len);
390     pkt->flags |= AV_PKT_FLAG_KEY;
391     *got_packet = 1;
392     opj_cio_close(stream);
393     return 0;
394 }
395
396 static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
397 {
398     LibOpenJPEGContext *ctx = avctx->priv_data;
399
400     opj_destroy_compress(ctx->compress);
401     opj_image_destroy(ctx->image);
402     av_freep(&avctx->coded_frame);
403     return 0 ;
404 }
405
406 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
407 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
408 static const AVOption options[] = {
409     { "format",        "Codec Format",      OFFSET(format),        AV_OPT_TYPE_INT,   { CODEC_JP2   }, CODEC_J2K, CODEC_JP2,   VE, "format"      },
410     { "j2k",           NULL,                0,                     AV_OPT_TYPE_CONST, { CODEC_J2K   }, 0,         0,           VE, "format"      },
411     { "jp2",           NULL,                0,                     AV_OPT_TYPE_CONST, { CODEC_JP2   }, 0,         0,           VE, "format"      },
412     { "profile",       NULL,                OFFSET(profile),       AV_OPT_TYPE_INT,   { STD_RSIZ    }, STD_RSIZ,  CINEMA4K,    VE, "profile"     },
413     { "jpeg2000",      NULL,                0,                     AV_OPT_TYPE_CONST, { STD_RSIZ    }, 0,         0,           VE, "profile"     },
414     { "cinema2k",      NULL,                0,                     AV_OPT_TYPE_CONST, { CINEMA2K    }, 0,         0,           VE, "profile"     },
415     { "cinema4k",      NULL,                0,                     AV_OPT_TYPE_CONST, { CINEMA4K    }, 0,         0,           VE, "profile"     },
416     { "cinema_mode",   "Digital Cinema",    OFFSET(cinema_mode),   AV_OPT_TYPE_INT,   { OFF         }, OFF,       CINEMA4K_24, VE, "cinema_mode" },
417     { "off",           NULL,                0,                     AV_OPT_TYPE_CONST, { OFF         }, 0,         0,           VE, "cinema_mode" },
418     { "2k_24",         NULL,                0,                     AV_OPT_TYPE_CONST, { CINEMA2K_24 }, 0,         0,           VE, "cinema_mode" },
419     { "2k_48",         NULL,                0,                     AV_OPT_TYPE_CONST, { CINEMA2K_48 }, 0,         0,           VE, "cinema_mode" },
420     { "4k_24",         NULL,                0,                     AV_OPT_TYPE_CONST, { CINEMA4K_24 }, 0,         0,           VE, "cinema_mode" },
421     { "prog_order",    "Progression Order", OFFSET(prog_order),    AV_OPT_TYPE_INT,   { LRCP        }, LRCP,      CPRL,        VE, "prog_order"  },
422     { "lrcp",          NULL,                0,                     AV_OPT_TYPE_CONST, { LRCP        }, 0,         0,           VE, "prog_order"  },
423     { "rlcp",          NULL,                0,                     AV_OPT_TYPE_CONST, { RLCP        }, 0,         0,           VE, "prog_order"  },
424     { "rpcl",          NULL,                0,                     AV_OPT_TYPE_CONST, { RPCL        }, 0,         0,           VE, "prog_order"  },
425     { "pcrl",          NULL,                0,                     AV_OPT_TYPE_CONST, { PCRL        }, 0,         0,           VE, "prog_order"  },
426     { "cprl",          NULL,                0,                     AV_OPT_TYPE_CONST, { CPRL        }, 0,         0,           VE, "prog_order"  },
427     { "numresolution", NULL,                OFFSET(numresolution), AV_OPT_TYPE_INT,   { 6           }, 1,         10,          VE                },
428     { "numlayers",     NULL,                OFFSET(numlayers),     AV_OPT_TYPE_INT,   { 1           }, 1,         10,          VE                },
429     { "disto_alloc",   NULL,                OFFSET(disto_alloc),   AV_OPT_TYPE_INT,   { 1           }, 0,         1,           VE                },
430     { "fixed_alloc",   NULL,                OFFSET(fixed_alloc),   AV_OPT_TYPE_INT,   { 0           }, 0,         1,           VE                },
431     { "fixed_quality", NULL,                OFFSET(fixed_quality), AV_OPT_TYPE_INT,   { 0           }, 0,         1,           VE                },
432     { NULL },
433 };
434
435 static const AVClass class = {
436     .class_name = "libopenjpeg",
437     .item_name  = av_default_item_name,
438     .option     = options,
439     .version    = LIBAVUTIL_VERSION_INT,
440 };
441
442 AVCodec ff_libopenjpeg_encoder = {
443     .name           = "libopenjpeg",
444     .type           = AVMEDIA_TYPE_VIDEO,
445     .id             = CODEC_ID_JPEG2000,
446     .priv_data_size = sizeof(LibOpenJPEGContext),
447     .init           = libopenjpeg_encode_init,
448     .encode2        = libopenjpeg_encode_frame,
449     .close          = libopenjpeg_encode_close,
450     .capabilities   = 0,
451     .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24,PIX_FMT_RGBA,PIX_FMT_RGB48,PIX_FMT_RGBA64,
452                                            PIX_FMT_GRAY8,PIX_FMT_GRAY8A,PIX_FMT_GRAY16,
453                                            PIX_FMT_YUV420P,PIX_FMT_YUV422P,PIX_FMT_YUVA420P,
454                                            PIX_FMT_YUV440P,PIX_FMT_YUV444P,
455                                            PIX_FMT_YUV411P,PIX_FMT_YUV410P,
456                                            PIX_FMT_YUV420P9,PIX_FMT_YUV422P9,PIX_FMT_YUV444P9,
457                                            PIX_FMT_YUV420P10,PIX_FMT_YUV422P10,PIX_FMT_YUV444P10,
458                                            PIX_FMT_YUV420P16,PIX_FMT_YUV422P16,PIX_FMT_YUV444P16,
459                                            PIX_FMT_NONE},
460     .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
461     .priv_class     = &class,
462 };