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