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