]> 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_GRAY16:
73         color_space = CLRSPC_GRAY;
74         numcomps = 1;
75         bpp = 16;
76         break;
77     case PIX_FMT_RGB24:
78         color_space = CLRSPC_SRGB;
79         numcomps = 3;
80         break;
81     case PIX_FMT_RGBA:
82         color_space = CLRSPC_SRGB;
83         numcomps = 4;
84         break;
85     case PIX_FMT_RGB48:
86         color_space = CLRSPC_SRGB;
87         numcomps = 3;
88         bpp = 16;
89         break;
90     case PIX_FMT_RGBA64:
91         color_space = CLRSPC_SRGB;
92         numcomps = 4;
93         bpp = 16;
94         break;
95     case PIX_FMT_YUV420P:
96         color_space = CLRSPC_SYCC;
97         numcomps = 3;
98         break;
99     case PIX_FMT_YUV422P:
100         color_space = CLRSPC_SYCC;
101         numcomps = 3;
102         break;
103     case PIX_FMT_YUV440P:
104         color_space = CLRSPC_SYCC;
105         numcomps = 3;
106         break;
107     case PIX_FMT_YUV444P:
108         color_space = CLRSPC_SYCC;
109         numcomps = 3;
110         break;
111     case PIX_FMT_YUVA420P:
112         color_space = CLRSPC_SYCC;
113         numcomps = 4;
114         break;
115     case PIX_FMT_YUV420P9:
116     case PIX_FMT_YUV422P9:
117     case PIX_FMT_YUV444P9:
118         color_space = CLRSPC_SYCC;
119         numcomps = 3;
120         bpp = 9;
121         break;
122     case PIX_FMT_YUV420P10:
123     case PIX_FMT_YUV422P10:
124     case PIX_FMT_YUV444P10:
125         color_space = CLRSPC_SYCC;
126         numcomps = 3;
127         bpp = 10;
128         break;
129     case PIX_FMT_YUV420P16:
130     case PIX_FMT_YUV422P16:
131     case PIX_FMT_YUV444P16:
132         color_space = CLRSPC_SYCC;
133         numcomps = 3;
134         bpp = 16;
135         break;
136     default:
137         av_log(avctx, AV_LOG_ERROR, "The requested pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
138         return NULL;
139     }
140
141     cmptparm = av_mallocz(numcomps * sizeof(opj_image_cmptparm_t));
142     if (!cmptparm) {
143         av_log(avctx, AV_LOG_ERROR, "Not enough memory");
144         return NULL;
145     }
146     for (i = 0; i < numcomps; i++) {
147         cmptparm[i].prec = bpp;
148         cmptparm[i].bpp = bpp;
149         cmptparm[i].sgnd = 0;
150         cmptparm[i].dx = sub_dx[i];
151         cmptparm[i].dy = sub_dy[i];
152         cmptparm[i].w = avctx->width / sub_dx[i];
153         cmptparm[i].h = avctx->height / sub_dy[i];
154     }
155
156     img = opj_image_create(numcomps, cmptparm, color_space);
157     av_freep(&cmptparm);
158     return img;
159 }
160
161 static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
162 {
163     LibOpenJPEGContext *ctx = avctx->priv_data;
164
165     opj_set_default_encoder_parameters(&ctx->enc_params);
166     ctx->enc_params.tcp_numlayers = 1;
167     ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
168     ctx->enc_params.cp_disto_alloc = 1;
169
170     ctx->compress = opj_create_compress(CODEC_J2K);
171     if (!ctx->compress) {
172         av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
173         return AVERROR(ENOMEM);
174     }
175
176     avctx->coded_frame = avcodec_alloc_frame();
177     if (!avctx->coded_frame) {
178         av_freep(&ctx->compress);
179         av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
180         return AVERROR(ENOMEM);
181     }
182
183     ctx->image = mj2_create_image(avctx, &ctx->enc_params);
184     if (!ctx->image) {
185         av_freep(&ctx->compress);
186         av_freep(&avctx->coded_frame);
187         av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
188         return AVERROR(EINVAL);
189     }
190
191     memset(&ctx->event_mgr, 0, sizeof(opj_event_mgr_t));
192     ctx->event_mgr.error_handler = error_callback;
193     ctx->event_mgr.warning_handler = warning_callback;
194     ctx->event_mgr.info_handler = NULL;
195     opj_set_event_mgr((opj_common_ptr)ctx->compress, &ctx->event_mgr, avctx);
196
197     return 0;
198 }
199
200 static int libopenjpeg_copy_rgba(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image, int numcomps)
201 {
202     int compno;
203     int x;
204     int y;
205
206     av_assert0(numcomps == 1 || numcomps == 3 || numcomps == 4);
207
208     for (compno = 0; compno < numcomps; ++compno) {
209         if (image->comps[compno].w > frame->linesize[0] / numcomps) {
210             return 0;
211         }
212     }
213
214     for (compno = 0; compno < numcomps; ++compno) {
215         for (y = 0; y < avctx->height; ++y) {
216             for (x = 0; x < avctx->width; ++x) {
217                 image->comps[compno].data[y * avctx->width + x] = frame->data[0][y * frame->linesize[0] + x * numcomps + compno];
218             }
219         }
220     }
221
222     return 1;
223 }
224
225 static int libopenjpeg_copy_rgb16(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image, int numcomps)
226 {
227     int compno;
228     int x;
229     int y;
230     uint16_t *frame_ptr = (uint16_t*)frame->data[0];
231
232     av_assert0(numcomps == 1 || numcomps == 3 || numcomps == 4);
233
234     for (compno = 0; compno < numcomps; ++compno) {
235         if (image->comps[compno].w > frame->linesize[0] / numcomps) {
236             return 0;
237         }
238     }
239
240     for (compno = 0; compno < numcomps; ++compno) {
241         for (y = 0; y < avctx->height; ++y) {
242             for (x = 0; x < avctx->width; ++x) {
243                 image->comps[compno].data[y * avctx->width + x] = frame_ptr[y * frame->linesize[0] / 2 + x * numcomps + compno];
244             }
245         }
246     }
247
248     return 1;
249 }
250
251 static int libopenjpeg_copy_yuv8(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
252 {
253     int compno;
254     int x;
255     int y;
256     int width;
257     int height;
258     const int numcomps = avctx->pix_fmt == PIX_FMT_YUVA420P ? 4 : 3;
259
260     for (compno = 0; compno < numcomps; ++compno) {
261         if (image->comps[compno].w > frame->linesize[compno]) {
262             return 0;
263         }
264     }
265
266     for (compno = 0; compno < numcomps; ++compno) {
267         width = avctx->width / image->comps[compno].dx;
268         height = avctx->height / image->comps[compno].dy;
269         for (y = 0; y < height; ++y) {
270             for (x = 0; x < width; ++x) {
271                 image->comps[compno].data[y * width + x] = frame->data[compno][y * frame->linesize[compno] + x];
272             }
273         }
274     }
275
276     return 1;
277 }
278
279 static int libopenjpeg_copy_yuv16(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
280 {
281     int compno;
282     int x;
283     int y;
284     int width;
285     int height;
286     const int numcomps = 3;
287     uint16_t *frame_ptr;
288
289     for (compno = 0; compno < numcomps; ++compno) {
290         if (image->comps[compno].w > frame->linesize[compno]) {
291             return 0;
292         }
293     }
294
295     for (compno = 0; compno < numcomps; ++compno) {
296         width = avctx->width / image->comps[compno].dx;
297         height = avctx->height / image->comps[compno].dy;
298         frame_ptr = (uint16_t*)frame->data[compno];
299         for (y = 0; y < height; ++y) {
300             for (x = 0; x < width; ++x) {
301                 image->comps[compno].data[y * width + x] = frame_ptr[y * (frame->linesize[compno] / 2) + x];
302             }
303         }
304     }
305
306     return 1;
307 }
308
309 static int libopenjpeg_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
310 {
311     AVFrame *frame = data;
312     LibOpenJPEGContext *ctx = avctx->priv_data;
313     opj_cinfo_t *compress = ctx->compress;
314     opj_image_t *image = ctx->image;
315     opj_cio_t *stream;
316     int cpyresult = 0;
317     int len = 0;
318
319     // x0, y0 is the top left corner of the image
320     // x1, y1 is the width, height of the reference grid
321     image->x0 = 0;
322     image->y0 = 0;
323     image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
324     image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
325
326     switch (avctx->pix_fmt) {
327     case PIX_FMT_GRAY8:
328         cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 1);
329         break;
330     case PIX_FMT_GRAY16:
331         cpyresult = libopenjpeg_copy_rgb16(avctx, frame, image, 1);
332         break;
333     case PIX_FMT_RGB24:
334         cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 3);
335         break;
336     case PIX_FMT_RGBA:
337         cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 4);
338         break;
339     case PIX_FMT_RGB48:
340         cpyresult = libopenjpeg_copy_rgb16(avctx, frame, image, 3);
341         break;
342     case PIX_FMT_RGBA64:
343         cpyresult = libopenjpeg_copy_rgb16(avctx, frame, image, 4);
344         break;
345     case PIX_FMT_YUV420P:
346     case PIX_FMT_YUV422P:
347     case PIX_FMT_YUV440P:
348     case PIX_FMT_YUV444P:
349     case PIX_FMT_YUVA420P:
350         cpyresult = libopenjpeg_copy_yuv8(avctx, frame, image);
351         break;
352     case PIX_FMT_YUV420P9:
353     case PIX_FMT_YUV420P10:
354     case PIX_FMT_YUV420P16:
355     case PIX_FMT_YUV422P9:
356     case PIX_FMT_YUV422P10:
357     case PIX_FMT_YUV422P16:
358     case PIX_FMT_YUV444P9:
359     case PIX_FMT_YUV444P10:
360     case PIX_FMT_YUV444P16:
361         cpyresult = libopenjpeg_copy_yuv16(avctx, frame, image);
362         break;
363     default:
364         av_log(avctx, AV_LOG_ERROR, "The frame's pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
365         return AVERROR(EINVAL);
366         break;
367     }
368
369     if (!cpyresult) {
370         av_log(avctx, AV_LOG_ERROR, "Could not copy the frame data to the internal image buffer\n");
371         return -1;
372     }
373
374     opj_setup_encoder(compress, &ctx->enc_params, image);
375     stream = opj_cio_open((opj_common_ptr)compress, NULL, 0);
376     if (!stream) {
377         av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
378         return AVERROR(ENOMEM);
379     }
380
381     if (!opj_encode(compress, stream, image, NULL)) {
382         opj_cio_close(stream);
383         av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
384         return -1;
385     }
386
387     len = cio_tell(stream);
388     if (len > buf_size) {
389         opj_cio_close(stream);
390         av_log(avctx, AV_LOG_ERROR, "Error with buf_size, not large enough to hold the frame\n");
391         return -1;
392     }
393
394     memcpy(buf, stream->buffer, len);
395     opj_cio_close(stream);
396     return len;
397 }
398
399 static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
400 {
401     LibOpenJPEGContext *ctx = avctx->priv_data;
402
403     opj_destroy_compress(ctx->compress);
404     opj_image_destroy(ctx->image);
405     av_freep(&avctx->coded_frame);
406     return 0 ;
407 }
408
409
410 AVCodec ff_libopenjpeg_encoder = {
411     .name           = "libopenjpeg",
412     .type           = AVMEDIA_TYPE_VIDEO,
413     .id             = CODEC_ID_JPEG2000,
414     .priv_data_size = sizeof(LibOpenJPEGContext),
415     .init           = libopenjpeg_encode_init,
416     .encode         = libopenjpeg_encode_frame,
417     .close          = libopenjpeg_encode_close,
418     .capabilities   = 0,
419     .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24,PIX_FMT_RGBA,PIX_FMT_RGB48,PIX_FMT_RGBA64,
420                                            PIX_FMT_GRAY8,PIX_FMT_GRAY16,
421                                            PIX_FMT_YUV420P,PIX_FMT_YUV422P,PIX_FMT_YUVA420P,
422                                            PIX_FMT_YUV440P,PIX_FMT_YUV444P,
423                                            PIX_FMT_YUV420P9,PIX_FMT_YUV422P9,PIX_FMT_YUV444P9,
424                                            PIX_FMT_YUV420P10,PIX_FMT_YUV422P10,PIX_FMT_YUV444P10,
425                                            PIX_FMT_YUV420P16,PIX_FMT_YUV422P16,PIX_FMT_YUV444P16,
426                                            PIX_FMT_NONE},
427     .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 encoder"),
428 } ;