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