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