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