]> git.sesse.net Git - ffmpeg/blob - libavcodec/libopenjpegenc.c
737cd587dd920ec502a05db60717a760e06ae17a
[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/imgutils.h"
28 #include "avcodec.h"
29 #include "libavutil/intreadwrite.h"
30 #define  OPJ_STATIC
31 #include <openjpeg.h>
32
33 typedef struct {
34     opj_image_t *image;
35     opj_cparameters_t enc_params;
36     opj_cinfo_t *compress;
37     opj_event_mgr_t event_mgr;
38 } LibOpenJPEGContext;
39
40 static void error_callback(const char *msg, void *data)
41 {
42     av_log((AVCodecContext*)data, AV_LOG_ERROR, "libopenjpeg: %s\n", msg);
43 }
44
45 static void warning_callback(const char *msg, void *data)
46 {
47     av_log((AVCodecContext*)data, AV_LOG_WARNING, "libopenjpeg: %s\n", msg);
48 }
49
50 static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
51 {
52     opj_image_cmptparm_t *cmptparm;
53     opj_image_t *img;
54     int i;
55     int bpp;
56     int sub_dx[4];
57     int sub_dy[4];
58     int numcomps = 0;
59     OPJ_COLOR_SPACE color_space = CLRSPC_UNKNOWN;
60
61     switch (avctx->pix_fmt) {
62     case PIX_FMT_GRAY8:
63         color_space = CLRSPC_GRAY;
64         numcomps = 1;
65         bpp = 8;
66         sub_dx[0] = 1;
67         sub_dy[0] = 1;
68         break;
69     case PIX_FMT_RGB24:
70         color_space = CLRSPC_SRGB;
71         numcomps = 3;
72         bpp = 24;
73         sub_dx[0] = sub_dx[1] = sub_dx[2] = 1;
74         sub_dy[0] = sub_dy[1] = sub_dy[2] = 1;
75         break;
76     case PIX_FMT_RGBA:
77         color_space = CLRSPC_SRGB;
78         numcomps = 4;
79         bpp = 32;
80         sub_dx[0] = sub_dx[1] = sub_dx[2] = sub_dx[3] = 1;
81         sub_dy[0] = sub_dy[1] = sub_dy[2] = sub_dy[3] = 1;
82         break;
83     case PIX_FMT_YUV420P:
84         color_space = CLRSPC_SYCC;
85         numcomps = 3;
86         bpp = 12;
87         sub_dx[0] = 1;
88         sub_dx[1] = sub_dx[2] = 2;
89         sub_dy[0] = 1;
90         sub_dy[1] = sub_dy[2] = 2;
91         break;
92     case PIX_FMT_YUV422P:
93         color_space = CLRSPC_SYCC;
94         numcomps = 3;
95         bpp = 16;
96         sub_dx[0] = 1;
97         sub_dx[1] = sub_dx[2] = 2;
98         sub_dy[0] = sub_dy[1] = sub_dy[2] = 1;
99         break;
100     case PIX_FMT_YUV440P:
101         color_space = CLRSPC_SYCC;
102         numcomps = 3;
103         bpp = 16;
104         sub_dx[0] = sub_dx[1] = sub_dx[2] = 1;
105         sub_dy[0] = 1;
106         sub_dy[1] = sub_dy[2] = 2;
107             break;
108     case PIX_FMT_YUV444P:
109         color_space = CLRSPC_SYCC;
110         numcomps = 3;
111         bpp = 24;
112         sub_dx[0] = sub_dx[1] = sub_dx[2] = 1;
113         sub_dy[0] = sub_dy[1] = sub_dy[2] = 1;
114         break;
115     default:
116         av_log(avctx, AV_LOG_ERROR, "The requested pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
117         return NULL;
118     }
119
120     cmptparm = av_malloc(numcomps * sizeof(opj_image_cmptparm_t));
121     if (!cmptparm) {
122         av_log(avctx, AV_LOG_ERROR, "Not enough memory");
123         return NULL;
124     }
125     memset(cmptparm, 0, numcomps * sizeof(opj_image_cmptparm_t));
126     for (i = 0; i < numcomps; i++) {
127         cmptparm[i].prec = 8;
128         cmptparm[i].bpp = bpp;
129         cmptparm[i].sgnd = 0;
130         cmptparm[i].dx = sub_dx[i];
131         cmptparm[i].dy = sub_dy[i];
132         cmptparm[i].w = avctx->width / sub_dx[i];
133         cmptparm[i].h = avctx->height / sub_dy[i];
134     }
135
136     img = opj_image_create(numcomps, cmptparm, color_space);
137     av_freep(&cmptparm);
138     return img;
139 }
140
141 static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
142 {
143     LibOpenJPEGContext *ctx = avctx->priv_data;
144
145     opj_set_default_encoder_parameters(&ctx->enc_params);
146     ctx->enc_params.tcp_numlayers = 1;
147     ctx->enc_params.tcp_rates[0] = avctx->compression_level > 0 ? avctx->compression_level : 0;
148     ctx->enc_params.cp_disto_alloc = 1;
149
150     ctx->compress = opj_create_compress(CODEC_J2K);
151     if (!ctx->compress) {
152         av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
153         return AVERROR(ENOMEM);
154     }
155
156     avctx->coded_frame = avcodec_alloc_frame();
157     if (!avctx->coded_frame) {
158         av_freep(&ctx->compress);
159         ctx->compress = NULL;
160         av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
161         return AVERROR(ENOMEM);
162     }
163
164     ctx->image = mj2_create_image(avctx, &ctx->enc_params);
165     if (!ctx->image) {
166         av_freep(&ctx->compress);
167         ctx->compress = NULL;
168         av_freep(&avctx->coded_frame);
169         avctx->coded_frame = NULL;
170         av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
171         return AVERROR(EINVAL);
172     }
173
174     memset(&ctx->event_mgr, 0, sizeof(opj_event_mgr_t));
175     ctx->event_mgr.error_handler = error_callback;
176     ctx->event_mgr.warning_handler = warning_callback;
177     ctx->event_mgr.info_handler = NULL;
178     opj_set_event_mgr((opj_common_ptr)ctx->compress, &ctx->event_mgr, avctx);
179
180     return 0;
181 }
182
183 static int libopenjpeg_copy_rgba(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image, int numcomps)
184 {
185     int compno;
186     int x;
187     int y;
188
189     if (numcomps != 1 && numcomps != 3 && numcomps != 4) {
190         return 0;
191     }
192
193     for (compno = 0; compno < numcomps; ++compno) {
194         if (image->comps[compno].w > frame->linesize[0] / numcomps) {
195             return 0;
196         }
197     }
198
199     for (compno = 0; compno < numcomps; ++compno) {
200         for (y = 0; y < avctx->height; ++y) {
201             for (x = 0; x < avctx->width; ++x) {
202                 image->comps[compno].data[y * avctx->width + x] = frame->data[0][y * frame->linesize[0] + x * numcomps + compno];
203             }
204         }
205     }
206
207     return 1;
208 }
209
210 static int libopenjpeg_copy_yuv(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
211 {
212     int compno;
213     int x;
214     int y;
215     int width;
216     int height;
217     const int numcomps = 3;
218
219     for (compno = 0; compno < numcomps; ++compno) {
220         if (image->comps[compno].w > frame->linesize[compno]) {
221             return 0;
222         }
223     }
224
225     for (compno = 0; compno < numcomps; ++compno) {
226         width = avctx->width / image->comps[compno].dx;
227         height = avctx->height / image->comps[compno].dy;
228         for (y = 0; y < height; ++y) {
229             for (x = 0; x < width; ++x) {
230                 image->comps[compno].data[y * width + x] = frame->data[compno][y * frame->linesize[compno] + x];
231             }
232         }
233     }
234
235     return 1;
236 }
237
238 static int libopenjpeg_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
239 {
240     AVFrame *frame = data;
241     LibOpenJPEGContext *ctx = avctx->priv_data;
242     opj_cinfo_t *compress = ctx->compress;
243     opj_image_t *image = ctx->image;
244     opj_cio_t *stream;
245     int cpyresult = 0;
246     int len = 0;
247
248     // x0, y0 is the top left corner of the image
249     // x1, y1 is the width, height of the reference grid
250     image->x0 = 0;
251     image->y0 = 0;
252     image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
253     image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
254
255     switch (avctx->pix_fmt) {
256     case PIX_FMT_GRAY8:
257         cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 1);
258         break;
259     case PIX_FMT_RGB24:
260         cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 3);
261         break;
262     case PIX_FMT_RGBA:
263         cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 4);
264         break;
265     case PIX_FMT_YUV420P:
266     case PIX_FMT_YUV422P:
267     case PIX_FMT_YUV440P:
268     case PIX_FMT_YUV444P:
269         cpyresult = libopenjpeg_copy_yuv(avctx, frame, image);
270         break;
271     default:
272         av_log(avctx, AV_LOG_ERROR, "The frame's pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
273         return AVERROR(EINVAL);
274         break;
275     }
276
277     if (!cpyresult) {
278         av_log(avctx, AV_LOG_ERROR, "Could not copy the frame data to the internal image buffer\n");
279         return -1;
280     }
281
282     opj_setup_encoder(compress, &ctx->enc_params, image);
283     stream = opj_cio_open((opj_common_ptr)compress, NULL, 0);
284     if (!stream) {
285         av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
286         return AVERROR(ENOMEM);
287     }
288
289     if (!opj_encode(compress, stream, image, NULL)) {
290         opj_cio_close(stream);
291         av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
292         return -1;
293     }
294
295     len = cio_tell(stream);
296     if (len > buf_size) {
297         opj_cio_close(stream);
298         av_log(avctx, AV_LOG_ERROR, "Error with buf_size, not large enough to hold the frame\n");
299         return -1;
300     }
301
302     memcpy(buf, stream->buffer, len);
303     opj_cio_close(stream);
304     return len;
305 }
306
307 static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
308 {
309     LibOpenJPEGContext *ctx = avctx->priv_data;
310
311     opj_destroy_compress(ctx->compress);
312     opj_image_destroy(ctx->image);
313     av_freep(&avctx->coded_frame);
314     return 0 ;
315 }
316
317
318 AVCodec ff_libopenjpeg_encoder = {
319     .name = "libopenjpeg",
320     .type = AVMEDIA_TYPE_VIDEO,
321     .id = CODEC_ID_JPEG2000,
322     .priv_data_size = sizeof(LibOpenJPEGContext),
323     .init = libopenjpeg_encode_init,
324     .encode = libopenjpeg_encode_frame,
325     .close = libopenjpeg_encode_close,
326     .decode = NULL,
327     .capabilities = 0,
328     .pix_fmts = (const enum PixelFormat[]){PIX_FMT_GRAY8,PIX_FMT_RGB24,PIX_FMT_RGBA,PIX_FMT_YUV420P,PIX_FMT_YUV422P,PIX_FMT_YUV440P,PIX_FMT_YUV444P},
329     .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 encoder/decoder"),
330 } ;