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