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