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