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