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