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