]> git.sesse.net Git - ffmpeg/blob - libavcodec/libopenjpegenc.c
Merge commit '6c31ba226968f12f898120dbb928dab34e03782b'
[ffmpeg] / libavcodec / libopenjpegenc.c
1 /*
2  * JPEG 2000 encoding support via OpenJPEG
3  * Copyright (c) 2011 Michael Bradshaw <mjbshaw gmail 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/avassert.h"
28 #include "libavutil/common.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/opt.h"
32 #include "avcodec.h"
33 #include "internal.h"
34
35 #if HAVE_OPENJPEG_2_1_OPENJPEG_H
36 #  include <openjpeg-2.1/openjpeg.h>
37 #elif HAVE_OPENJPEG_2_0_OPENJPEG_H
38 #  include <openjpeg-2.0/openjpeg.h>
39 #elif HAVE_OPENJPEG_1_5_OPENJPEG_H
40 #  include <openjpeg-1.5/openjpeg.h>
41 #else
42 #  include <openjpeg.h>
43 #endif
44
45 #if HAVE_OPENJPEG_2_1_OPENJPEG_H || HAVE_OPENJPEG_2_0_OPENJPEG_H
46 #  define OPENJPEG_MAJOR_VERSION 2
47 #  define OPJ(x) OPJ_##x
48 #else
49 #  define OPENJPEG_MAJOR_VERSION 1
50 #  define OPJ(x) x
51 #endif
52
53 typedef struct LibOpenJPEGContext {
54     AVClass *avclass;
55 #if OPENJPEG_MAJOR_VERSION == 1
56     opj_image_t *image;
57 #endif // OPENJPEG_MAJOR_VERSION == 1
58     opj_cparameters_t enc_params;
59 #if OPENJPEG_MAJOR_VERSION == 1
60     opj_event_mgr_t event_mgr;
61 #endif // OPENJPEG_MAJOR_VERSION == 1
62     int format;
63     int profile;
64     int prog_order;
65     int cinema_mode;
66     int numresolution;
67     int irreversible;
68     int disto_alloc;
69     int fixed_quality;
70 } LibOpenJPEGContext;
71
72 static void error_callback(const char *msg, void *data)
73 {
74     av_log(data, AV_LOG_ERROR, "%s\n", msg);
75 }
76
77 static void warning_callback(const char *msg, void *data)
78 {
79     av_log(data, AV_LOG_WARNING, "%s\n", msg);
80 }
81
82 static void info_callback(const char *msg, void *data)
83 {
84     av_log(data, AV_LOG_DEBUG, "%s\n", msg);
85 }
86
87 #if OPENJPEG_MAJOR_VERSION == 2
88 typedef struct PacketWriter {
89     int pos;
90     AVPacket *packet;
91 } PacketWriter;
92
93 static OPJ_SIZE_T stream_write(void *out_buffer, OPJ_SIZE_T nb_bytes, void *user_data)
94 {
95     PacketWriter *writer = user_data;
96     AVPacket *packet = writer->packet;
97     int remaining = packet->size - writer->pos;
98     if (nb_bytes > remaining) {
99         OPJ_SIZE_T needed = nb_bytes - remaining;
100         int max_growth = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - packet->size;
101         if (needed > max_growth) {
102             return (OPJ_SIZE_T)-1;
103         }
104         if (av_grow_packet(packet, (int)needed)) {
105             return (OPJ_SIZE_T)-1;
106         }
107     }
108     memcpy(packet->data + writer->pos, out_buffer, nb_bytes);
109     writer->pos += (int)nb_bytes;
110     return nb_bytes;
111 }
112
113 static OPJ_OFF_T stream_skip(OPJ_OFF_T nb_bytes, void *user_data)
114 {
115     PacketWriter *writer = user_data;
116     AVPacket *packet = writer->packet;
117     if (nb_bytes < 0) {
118         if (writer->pos == 0) {
119             return (OPJ_SIZE_T)-1;
120         }
121         if (nb_bytes + writer->pos < 0) {
122             nb_bytes = -writer->pos;
123         }
124     } else {
125         int remaining = packet->size - writer->pos;
126         if (nb_bytes > remaining) {
127             OPJ_SIZE_T needed = nb_bytes - remaining;
128             int max_growth = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - packet->size;
129             if (needed > max_growth) {
130                 return (OPJ_SIZE_T)-1;
131             }
132             if (av_grow_packet(packet, (int)needed)) {
133                 return (OPJ_SIZE_T)-1;
134             }
135         }
136     }
137     writer->pos += (int)nb_bytes;
138     return nb_bytes;
139 }
140
141 static OPJ_BOOL stream_seek(OPJ_OFF_T nb_bytes, void *user_data)
142 {
143     PacketWriter *writer = user_data;
144     AVPacket *packet = writer->packet;
145     if (nb_bytes < 0) {
146         return OPJ_FALSE;
147     }
148     if (nb_bytes > packet->size) {
149         if (nb_bytes > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE ||
150             av_grow_packet(packet, (int)nb_bytes - packet->size)) {
151             return OPJ_FALSE;
152         }
153     }
154     writer->pos = (int)nb_bytes;
155     return OPJ_TRUE;
156 }
157 #endif // OPENJPEG_MAJOR_VERSION == 2
158
159 static void cinema_parameters(opj_cparameters_t *p)
160 {
161     p->tile_size_on = 0;
162     p->cp_tdx = 1;
163     p->cp_tdy = 1;
164
165     /* Tile part */
166     p->tp_flag = 'C';
167     p->tp_on = 1;
168
169     /* Tile and Image shall be at (0, 0) */
170     p->cp_tx0 = 0;
171     p->cp_ty0 = 0;
172     p->image_offset_x0 = 0;
173     p->image_offset_y0 = 0;
174
175     /* Codeblock size= 32 * 32 */
176     p->cblockw_init = 32;
177     p->cblockh_init = 32;
178     p->csty |= 0x01;
179
180     /* The progression order shall be CPRL */
181     p->prog_order = OPJ(CPRL);
182
183     /* No ROI */
184     p->roi_compno = -1;
185
186     /* No subsampling */
187     p->subsampling_dx = 1;
188     p->subsampling_dy = 1;
189
190     /* 9-7 transform */
191     p->irreversible = 1;
192
193     p->tcp_mct = 1;
194 }
195
196 static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
197 {
198     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
199     opj_image_cmptparm_t cmptparm[4] = {{0}};
200     opj_image_t *img;
201     int i;
202     int sub_dx[4];
203     int sub_dy[4];
204     int numcomps;
205     OPJ_COLOR_SPACE color_space = OPJ(CLRSPC_UNKNOWN);
206
207     sub_dx[0] = sub_dx[3] = 1;
208     sub_dy[0] = sub_dy[3] = 1;
209     sub_dx[1] = sub_dx[2] = 1 << desc->log2_chroma_w;
210     sub_dy[1] = sub_dy[2] = 1 << desc->log2_chroma_h;
211
212     numcomps = desc->nb_components;
213
214     switch (avctx->pix_fmt) {
215     case AV_PIX_FMT_GRAY8:
216     case AV_PIX_FMT_YA8:
217     case AV_PIX_FMT_GRAY16:
218     case AV_PIX_FMT_YA16:
219         color_space = OPJ(CLRSPC_GRAY);
220         break;
221     case AV_PIX_FMT_RGB24:
222     case AV_PIX_FMT_RGBA:
223     case AV_PIX_FMT_RGB48:
224     case AV_PIX_FMT_RGBA64:
225     case AV_PIX_FMT_GBR24P:
226     case AV_PIX_FMT_GBRP9:
227     case AV_PIX_FMT_GBRP10:
228     case AV_PIX_FMT_GBRP12:
229     case AV_PIX_FMT_GBRP14:
230     case AV_PIX_FMT_GBRP16:
231     case AV_PIX_FMT_XYZ12:
232         color_space = OPJ(CLRSPC_SRGB);
233         break;
234     case AV_PIX_FMT_YUV410P:
235     case AV_PIX_FMT_YUV411P:
236     case AV_PIX_FMT_YUV420P:
237     case AV_PIX_FMT_YUV422P:
238     case AV_PIX_FMT_YUV440P:
239     case AV_PIX_FMT_YUV444P:
240     case AV_PIX_FMT_YUVA420P:
241     case AV_PIX_FMT_YUVA422P:
242     case AV_PIX_FMT_YUVA444P:
243     case AV_PIX_FMT_YUV420P9:
244     case AV_PIX_FMT_YUV422P9:
245     case AV_PIX_FMT_YUV444P9:
246     case AV_PIX_FMT_YUVA420P9:
247     case AV_PIX_FMT_YUVA422P9:
248     case AV_PIX_FMT_YUVA444P9:
249     case AV_PIX_FMT_YUV420P10:
250     case AV_PIX_FMT_YUV422P10:
251     case AV_PIX_FMT_YUV444P10:
252     case AV_PIX_FMT_YUVA420P10:
253     case AV_PIX_FMT_YUVA422P10:
254     case AV_PIX_FMT_YUVA444P10:
255     case AV_PIX_FMT_YUV420P12:
256     case AV_PIX_FMT_YUV422P12:
257     case AV_PIX_FMT_YUV444P12:
258     case AV_PIX_FMT_YUV420P14:
259     case AV_PIX_FMT_YUV422P14:
260     case AV_PIX_FMT_YUV444P14:
261     case AV_PIX_FMT_YUV420P16:
262     case AV_PIX_FMT_YUV422P16:
263     case AV_PIX_FMT_YUV444P16:
264     case AV_PIX_FMT_YUVA420P16:
265     case AV_PIX_FMT_YUVA422P16:
266     case AV_PIX_FMT_YUVA444P16:
267         color_space = OPJ(CLRSPC_SYCC);
268         break;
269     default:
270         av_log(avctx, AV_LOG_ERROR,
271                "The requested pixel format '%s' is not supported\n",
272                av_get_pix_fmt_name(avctx->pix_fmt));
273         return NULL;
274     }
275
276     for (i = 0; i < numcomps; i++) {
277         cmptparm[i].prec = desc->comp[i].depth;
278         cmptparm[i].bpp  = desc->comp[i].depth;
279         cmptparm[i].sgnd = 0;
280         cmptparm[i].dx = sub_dx[i];
281         cmptparm[i].dy = sub_dy[i];
282         cmptparm[i].w = (avctx->width + sub_dx[i] - 1) / sub_dx[i];
283         cmptparm[i].h = (avctx->height + sub_dy[i] - 1) / sub_dy[i];
284     }
285
286     img = opj_image_create(numcomps, cmptparm, color_space);
287
288     if (!img)
289         return NULL;
290
291     // x0, y0 is the top left corner of the image
292     // x1, y1 is the width, height of the reference grid
293     img->x0 = 0;
294     img->y0 = 0;
295     img->x1 = (avctx->width  - 1) * parameters->subsampling_dx + 1;
296     img->y1 = (avctx->height - 1) * parameters->subsampling_dy + 1;
297
298     return img;
299 }
300
301 static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
302 {
303     LibOpenJPEGContext *ctx = avctx->priv_data;
304     int err = 0;
305
306     opj_set_default_encoder_parameters(&ctx->enc_params);
307
308 #if HAVE_OPENJPEG_2_1_OPENJPEG_H
309     switch (ctx->cinema_mode) {
310     case OPJ_CINEMA2K_24:
311         ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
312         ctx->enc_params.max_cs_size = OPJ_CINEMA_24_CS;
313         ctx->enc_params.max_comp_size = OPJ_CINEMA_24_COMP;
314         break;
315     case OPJ_CINEMA2K_48:
316         ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
317         ctx->enc_params.max_cs_size = OPJ_CINEMA_48_CS;
318         ctx->enc_params.max_comp_size = OPJ_CINEMA_48_COMP;
319         break;
320     case OPJ_CINEMA4K_24:
321         ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_4K;
322         ctx->enc_params.max_cs_size = OPJ_CINEMA_24_CS;
323         ctx->enc_params.max_comp_size = OPJ_CINEMA_24_COMP;
324         break;
325     }
326
327     switch (ctx->profile) {
328     case OPJ_CINEMA2K:
329         if (ctx->enc_params.rsiz == OPJ_PROFILE_CINEMA_4K) {
330             err = AVERROR(EINVAL);
331             break;
332         }
333         ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
334         break;
335     case OPJ_CINEMA4K:
336         if (ctx->enc_params.rsiz == OPJ_PROFILE_CINEMA_2K) {
337             err = AVERROR(EINVAL);
338             break;
339         }
340         ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_4K;
341         break;
342     }
343
344     if (err) {
345         av_log(avctx, AV_LOG_ERROR,
346                "Invalid parameter pairing: cinema_mode and profile conflict.\n");
347         goto fail;
348     }
349 #else
350     ctx->enc_params.cp_rsiz = ctx->profile;
351     ctx->enc_params.cp_cinema = ctx->cinema_mode;
352 #endif
353
354     if (!ctx->numresolution) {
355         ctx->numresolution = 6;
356         while (FFMIN(avctx->width, avctx->height) >> ctx->numresolution < 1)
357             ctx->numresolution --;
358     }
359
360     ctx->enc_params.prog_order = ctx->prog_order;
361     ctx->enc_params.numresolution = ctx->numresolution;
362     ctx->enc_params.irreversible = ctx->irreversible;
363     ctx->enc_params.cp_disto_alloc = ctx->disto_alloc;
364     ctx->enc_params.cp_fixed_quality = ctx->fixed_quality;
365     ctx->enc_params.tcp_numlayers = 1;
366     ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
367
368     if (ctx->cinema_mode > 0) {
369         cinema_parameters(&ctx->enc_params);
370     }
371
372 #if OPENJPEG_MAJOR_VERSION == 1
373     ctx->image = mj2_create_image(avctx, &ctx->enc_params);
374     if (!ctx->image) {
375         av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
376         err = AVERROR(EINVAL);
377         goto fail;
378     }
379 #endif // OPENJPEG_MAJOR_VERSION == 1
380
381     return 0;
382
383 fail:
384 #if OPENJPEG_MAJOR_VERSION == 1
385     opj_image_destroy(ctx->image);
386     ctx->image = NULL;
387 #endif // OPENJPEG_MAJOR_VERSION == 1
388     return err;
389 }
390
391 static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
392 {
393     int compno;
394     int x;
395     int y;
396     int *image_line;
397     int frame_index;
398     const int numcomps = image->numcomps;
399
400     for (compno = 0; compno < numcomps; ++compno) {
401         if (image->comps[compno].w > frame->linesize[0] / numcomps) {
402             av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
403             return 0;
404         }
405     }
406
407     for (compno = 0; compno < numcomps; ++compno) {
408         for (y = 0; y < avctx->height; ++y) {
409             image_line = image->comps[compno].data + y * image->comps[compno].w;
410             frame_index = y * frame->linesize[0] + compno;
411             for (x = 0; x < avctx->width; ++x) {
412                 image_line[x] = frame->data[0][frame_index];
413                 frame_index += numcomps;
414             }
415             for (; x < image->comps[compno].w; ++x) {
416                 image_line[x] = image_line[x - 1];
417             }
418         }
419         for (; y < image->comps[compno].h; ++y) {
420             image_line = image->comps[compno].data + y * image->comps[compno].w;
421             for (x = 0; x < image->comps[compno].w; ++x) {
422                 image_line[x] = image_line[x - (int)image->comps[compno].w];
423             }
424         }
425     }
426
427     return 1;
428 }
429
430 // for XYZ 12 bit
431 static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
432 {
433     int compno;
434     int x, y;
435     int *image_line;
436     int frame_index;
437     const int numcomps  = image->numcomps;
438     uint16_t *frame_ptr = (uint16_t *)frame->data[0];
439
440     for (compno = 0; compno < numcomps; ++compno) {
441         if (image->comps[compno].w > frame->linesize[0] / numcomps) {
442             av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
443             return 0;
444         }
445     }
446
447     for (compno = 0; compno < numcomps; ++compno) {
448         for (y = 0; y < avctx->height; ++y) {
449             image_line = image->comps[compno].data + y * image->comps[compno].w;
450             frame_index = y * (frame->linesize[0] / 2) + compno;
451             for (x = 0; x < avctx->width; ++x) {
452                 image_line[x] = frame_ptr[frame_index] >> 4;
453                 frame_index += numcomps;
454             }
455             for (; x < image->comps[compno].w; ++x) {
456                 image_line[x] = image_line[x - 1];
457             }
458         }
459         for (; y < image->comps[compno].h; ++y) {
460             image_line = image->comps[compno].data + y * image->comps[compno].w;
461             for (x = 0; x < image->comps[compno].w; ++x) {
462                 image_line[x] = image_line[x - (int)image->comps[compno].w];
463             }
464         }
465     }
466
467     return 1;
468 }
469
470 static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
471 {
472     int compno;
473     int x;
474     int y;
475     int *image_line;
476     int frame_index;
477     const int numcomps = image->numcomps;
478     uint16_t *frame_ptr = (uint16_t*)frame->data[0];
479
480     for (compno = 0; compno < numcomps; ++compno) {
481         if (image->comps[compno].w > frame->linesize[0] / numcomps) {
482             av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
483             return 0;
484         }
485     }
486
487     for (compno = 0; compno < numcomps; ++compno) {
488         for (y = 0; y < avctx->height; ++y) {
489             image_line = image->comps[compno].data + y * image->comps[compno].w;
490             frame_index = y * (frame->linesize[0] / 2) + compno;
491             for (x = 0; x < avctx->width; ++x) {
492                 image_line[x] = frame_ptr[frame_index];
493                 frame_index += numcomps;
494             }
495             for (; x < image->comps[compno].w; ++x) {
496                 image_line[x] = image_line[x - 1];
497             }
498         }
499         for (; y < image->comps[compno].h; ++y) {
500             image_line = image->comps[compno].data + y * image->comps[compno].w;
501             for (x = 0; x < image->comps[compno].w; ++x) {
502                 image_line[x] = image_line[x - (int)image->comps[compno].w];
503             }
504         }
505     }
506
507     return 1;
508 }
509
510 static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
511 {
512     int compno;
513     int x;
514     int y;
515     int width;
516     int height;
517     int *image_line;
518     int frame_index;
519     const int numcomps = image->numcomps;
520
521     for (compno = 0; compno < numcomps; ++compno) {
522         if (image->comps[compno].w > frame->linesize[compno]) {
523             av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
524             return 0;
525         }
526     }
527
528     for (compno = 0; compno < numcomps; ++compno) {
529         width  = (avctx->width + image->comps[compno].dx - 1) / image->comps[compno].dx;
530         height = (avctx->height + image->comps[compno].dy - 1) / image->comps[compno].dy;
531         for (y = 0; y < height; ++y) {
532             image_line = image->comps[compno].data + y * image->comps[compno].w;
533             frame_index = y * frame->linesize[compno];
534             for (x = 0; x < width; ++x)
535                 image_line[x] = frame->data[compno][frame_index++];
536             for (; x < image->comps[compno].w; ++x) {
537                 image_line[x] = image_line[x - 1];
538             }
539         }
540         for (; y < image->comps[compno].h; ++y) {
541             image_line = image->comps[compno].data + y * image->comps[compno].w;
542             for (x = 0; x < image->comps[compno].w; ++x) {
543                 image_line[x] = image_line[x - (int)image->comps[compno].w];
544             }
545         }
546     }
547
548     return 1;
549 }
550
551 static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
552 {
553     int compno;
554     int x;
555     int y;
556     int width;
557     int height;
558     int *image_line;
559     int frame_index;
560     const int numcomps = image->numcomps;
561     uint16_t *frame_ptr;
562
563     for (compno = 0; compno < numcomps; ++compno) {
564         if (image->comps[compno].w > frame->linesize[compno]) {
565             av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
566             return 0;
567         }
568     }
569
570     for (compno = 0; compno < numcomps; ++compno) {
571         width     = (avctx->width + image->comps[compno].dx - 1) / image->comps[compno].dx;
572         height    = (avctx->height + image->comps[compno].dy - 1) / image->comps[compno].dy;
573         frame_ptr = (uint16_t *)frame->data[compno];
574         for (y = 0; y < height; ++y) {
575             image_line = image->comps[compno].data + y * image->comps[compno].w;
576             frame_index = y * (frame->linesize[compno] / 2);
577             for (x = 0; x < width; ++x)
578                 image_line[x] = frame_ptr[frame_index++];
579             for (; x < image->comps[compno].w; ++x) {
580                 image_line[x] = image_line[x - 1];
581             }
582         }
583         for (; y < image->comps[compno].h; ++y) {
584             image_line = image->comps[compno].data + y * image->comps[compno].w;
585             for (x = 0; x < image->comps[compno].w; ++x) {
586                 image_line[x] = image_line[x - (int)image->comps[compno].w];
587             }
588         }
589     }
590
591     return 1;
592 }
593
594 static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
595                                     const AVFrame *frame, int *got_packet)
596 {
597     LibOpenJPEGContext *ctx = avctx->priv_data;
598     int ret;
599     AVFrame *gbrframe;
600     int cpyresult = 0;
601 #if OPENJPEG_MAJOR_VERSION == 1
602     opj_image_t *image      = ctx->image;
603     opj_cinfo_t *compress   = NULL;
604     opj_cio_t *stream       = NULL;
605     int len;
606 #else // OPENJPEG_MAJOR_VERSION == 2
607     PacketWriter writer     = { 0 };
608     opj_codec_t *compress   = NULL;
609     opj_stream_t *stream    = NULL;
610     opj_image_t *image      = mj2_create_image(avctx, &ctx->enc_params);
611     if (!image) {
612         av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
613         ret = AVERROR(EINVAL);
614         goto done;
615     }
616 #endif // OPENJPEG_MAJOR_VERSION == 1
617
618     switch (avctx->pix_fmt) {
619     case AV_PIX_FMT_RGB24:
620     case AV_PIX_FMT_RGBA:
621     case AV_PIX_FMT_YA8:
622         cpyresult = libopenjpeg_copy_packed8(avctx, frame, image);
623         break;
624     case AV_PIX_FMT_XYZ12:
625         cpyresult = libopenjpeg_copy_packed12(avctx, frame, image);
626         break;
627     case AV_PIX_FMT_RGB48:
628     case AV_PIX_FMT_RGBA64:
629     case AV_PIX_FMT_YA16:
630         cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
631         break;
632     case AV_PIX_FMT_GBR24P:
633     case AV_PIX_FMT_GBRP9:
634     case AV_PIX_FMT_GBRP10:
635     case AV_PIX_FMT_GBRP12:
636     case AV_PIX_FMT_GBRP14:
637     case AV_PIX_FMT_GBRP16:
638         gbrframe = av_frame_clone(frame);
639         if (!gbrframe) {
640             ret = AVERROR(ENOMEM);
641             goto done;
642         }
643         gbrframe->data[0] = frame->data[2]; // swap to be rgb
644         gbrframe->data[1] = frame->data[0];
645         gbrframe->data[2] = frame->data[1];
646         gbrframe->linesize[0] = frame->linesize[2];
647         gbrframe->linesize[1] = frame->linesize[0];
648         gbrframe->linesize[2] = frame->linesize[1];
649         if (avctx->pix_fmt == AV_PIX_FMT_GBR24P) {
650             cpyresult = libopenjpeg_copy_unpacked8(avctx, gbrframe, image);
651         } else {
652             cpyresult = libopenjpeg_copy_unpacked16(avctx, gbrframe, image);
653         }
654         av_frame_free(&gbrframe);
655         break;
656     case AV_PIX_FMT_GRAY8:
657     case AV_PIX_FMT_YUV410P:
658     case AV_PIX_FMT_YUV411P:
659     case AV_PIX_FMT_YUV420P:
660     case AV_PIX_FMT_YUV422P:
661     case AV_PIX_FMT_YUV440P:
662     case AV_PIX_FMT_YUV444P:
663     case AV_PIX_FMT_YUVA420P:
664     case AV_PIX_FMT_YUVA422P:
665     case AV_PIX_FMT_YUVA444P:
666         cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
667         break;
668     case AV_PIX_FMT_GRAY16:
669     case AV_PIX_FMT_YUV420P9:
670     case AV_PIX_FMT_YUV422P9:
671     case AV_PIX_FMT_YUV444P9:
672     case AV_PIX_FMT_YUVA420P9:
673     case AV_PIX_FMT_YUVA422P9:
674     case AV_PIX_FMT_YUVA444P9:
675     case AV_PIX_FMT_YUV444P10:
676     case AV_PIX_FMT_YUV422P10:
677     case AV_PIX_FMT_YUV420P10:
678     case AV_PIX_FMT_YUVA444P10:
679     case AV_PIX_FMT_YUVA422P10:
680     case AV_PIX_FMT_YUVA420P10:
681     case AV_PIX_FMT_YUV420P12:
682     case AV_PIX_FMT_YUV422P12:
683     case AV_PIX_FMT_YUV444P12:
684     case AV_PIX_FMT_YUV420P14:
685     case AV_PIX_FMT_YUV422P14:
686     case AV_PIX_FMT_YUV444P14:
687     case AV_PIX_FMT_YUV444P16:
688     case AV_PIX_FMT_YUV422P16:
689     case AV_PIX_FMT_YUV420P16:
690     case AV_PIX_FMT_YUVA444P16:
691     case AV_PIX_FMT_YUVA422P16:
692     case AV_PIX_FMT_YUVA420P16:
693         cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
694         break;
695     default:
696         av_log(avctx, AV_LOG_ERROR,
697                "The frame's pixel format '%s' is not supported\n",
698                av_get_pix_fmt_name(avctx->pix_fmt));
699         ret = AVERROR(EINVAL);
700         goto done;
701         break;
702     }
703
704     if (!cpyresult) {
705         av_log(avctx, AV_LOG_ERROR,
706                "Could not copy the frame data to the internal image buffer\n");
707         ret = -1;
708         goto done;
709     }
710
711 #if OPENJPEG_MAJOR_VERSION == 2
712     if ((ret = ff_alloc_packet2(avctx, pkt, 1024, 0)) < 0) {
713         goto done;
714     }
715 #endif // OPENJPEG_MAJOR_VERSION == 2
716
717     compress = opj_create_compress(ctx->format);
718     if (!compress) {
719         av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
720         ret = AVERROR(ENOMEM);
721         goto done;
722     }
723
724 #if OPENJPEG_MAJOR_VERSION == 1
725     opj_setup_encoder(compress, &ctx->enc_params, image);
726     stream = opj_cio_open((opj_common_ptr) compress, NULL, 0);
727 #else // OPENJPEG_MAJOR_VERSION == 2
728     if (!opj_set_error_handler(compress, error_callback, avctx) ||
729         !opj_set_warning_handler(compress, warning_callback, avctx) ||
730         !opj_set_info_handler(compress, info_callback, avctx)) {
731         av_log(avctx, AV_LOG_ERROR, "Error setting the compressor handlers\n");
732         ret = AVERROR_EXTERNAL;
733         goto done;
734     }
735
736     if (!opj_setup_encoder(compress, &ctx->enc_params, image)) {
737         av_log(avctx, AV_LOG_ERROR, "Error setting up the compressor\n");
738         ret = AVERROR_EXTERNAL;
739         goto done;
740     }
741     stream = opj_stream_default_create(OPJ_STREAM_WRITE);
742 #endif // OPENJPEG_MAJOR_VERSION == 1
743
744     if (!stream) {
745         av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
746         ret = AVERROR(ENOMEM);
747         goto done;
748     }
749 #if OPENJPEG_MAJOR_VERSION == 1
750     memset(&ctx->event_mgr, 0, sizeof(ctx->event_mgr));
751     ctx->event_mgr.info_handler    = info_callback;
752     ctx->event_mgr.error_handler   = error_callback;
753     ctx->event_mgr.warning_handler = warning_callback;
754     opj_set_event_mgr((opj_common_ptr) compress, &ctx->event_mgr, avctx);
755     if (!opj_encode(compress, stream, image, NULL)) {
756         av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
757         ret = AVERROR_EXTERNAL;
758         goto done;
759     }
760
761     len = cio_tell(stream);
762     if ((ret = ff_alloc_packet2(avctx, pkt, len, 0)) < 0) {
763         goto done;
764     }
765
766     memcpy(pkt->data, stream->buffer, len);
767 #else // OPENJPEG_MAJOR_VERSION == 2
768     writer.packet = pkt;
769     opj_stream_set_write_function(stream, stream_write);
770     opj_stream_set_skip_function(stream, stream_skip);
771     opj_stream_set_seek_function(stream, stream_seek);
772 #if HAVE_OPENJPEG_2_1_OPENJPEG_H
773     opj_stream_set_user_data(stream, &writer, NULL);
774 #elif HAVE_OPENJPEG_2_0_OPENJPEG_H
775     opj_stream_set_user_data(stream, &writer);
776 #else
777 #error Missing call to opj_stream_set_user_data
778 #endif
779
780     if (!opj_start_compress(compress, image, stream) ||
781         !opj_encode(compress, stream) ||
782         !opj_end_compress(compress, stream)) {
783         av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
784         ret = AVERROR_EXTERNAL;
785         goto done;
786     }
787
788     av_shrink_packet(pkt, writer.pos);
789 #endif // OPENJPEG_MAJOR_VERSION == 1
790
791     pkt->flags |= AV_PKT_FLAG_KEY;
792     *got_packet = 1;
793     ret = 0;
794
795 done:
796 #if OPENJPEG_MAJOR_VERSION == 2
797     opj_stream_destroy(stream);
798     opj_destroy_codec(compress);
799     opj_image_destroy(image);
800 #else
801     opj_cio_close(stream);
802     opj_destroy_compress(compress);
803 #endif
804     return ret;
805 }
806
807 static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
808 {
809 #if OPENJPEG_MAJOR_VERSION == 1
810     LibOpenJPEGContext *ctx = avctx->priv_data;
811
812     opj_image_destroy(ctx->image);
813     ctx->image = NULL;
814 #endif // OPENJPEG_MAJOR_VERSION == 1
815     return 0;
816 }
817
818 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
819 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
820 static const AVOption options[] = {
821     { "format",        "Codec Format",      OFFSET(format),        AV_OPT_TYPE_INT,   { .i64 = OPJ(CODEC_JP2)   }, OPJ(CODEC_J2K), OPJ(CODEC_JP2),   VE, "format"      },
822     { "j2k",           NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = OPJ(CODEC_J2K)   }, 0,         0,           VE, "format"      },
823     { "jp2",           NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = OPJ(CODEC_JP2)   }, 0,         0,           VE, "format"      },
824     { "profile",       NULL,                OFFSET(profile),       AV_OPT_TYPE_INT,   { .i64 = OPJ(STD_RSIZ)    }, OPJ(STD_RSIZ),  OPJ(CINEMA4K),    VE, "profile"     },
825     { "jpeg2000",      NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = OPJ(STD_RSIZ)    }, 0,         0,           VE, "profile"     },
826     { "cinema2k",      NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA2K)    }, 0,         0,           VE, "profile"     },
827     { "cinema4k",      NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA4K)    }, 0,         0,           VE, "profile"     },
828     { "cinema_mode",   "Digital Cinema",    OFFSET(cinema_mode),   AV_OPT_TYPE_INT,   { .i64 = OPJ(OFF)         }, OPJ(OFF),       OPJ(CINEMA4K_24), VE, "cinema_mode" },
829     { "off",           NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = OPJ(OFF)         }, 0,         0,           VE, "cinema_mode" },
830     { "2k_24",         NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA2K_24) }, 0,         0,           VE, "cinema_mode" },
831     { "2k_48",         NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA2K_48) }, 0,         0,           VE, "cinema_mode" },
832     { "4k_24",         NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA4K_24) }, 0,         0,           VE, "cinema_mode" },
833     { "prog_order",    "Progression Order", OFFSET(prog_order),    AV_OPT_TYPE_INT,   { .i64 = OPJ(LRCP)    }, OPJ(LRCP),  OPJ(CPRL),    VE, "prog_order"  },
834     { "lrcp",          NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = OPJ(LRCP)    }, 0,         0,           VE, "prog_order"  },
835     { "rlcp",          NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = OPJ(RLCP)    }, 0,         0,           VE, "prog_order"  },
836     { "rpcl",          NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = OPJ(RPCL)    }, 0,         0,           VE, "prog_order"  },
837     { "pcrl",          NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = OPJ(PCRL)    }, 0,         0,           VE, "prog_order"  },
838     { "cprl",          NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = OPJ(CPRL)    }, 0,         0,           VE, "prog_order"  },
839     { "numresolution", NULL,                OFFSET(numresolution), AV_OPT_TYPE_INT,   { .i64 = 6            }, 0,         33,          VE                },
840     { "irreversible",  NULL,                OFFSET(irreversible),  AV_OPT_TYPE_INT,   { .i64 = 0            }, 0,         1,           VE                },
841     { "disto_alloc",   NULL,                OFFSET(disto_alloc),   AV_OPT_TYPE_INT,   { .i64 = 1            }, 0,         1,           VE                },
842     { "fixed_quality", NULL,                OFFSET(fixed_quality), AV_OPT_TYPE_INT,   { .i64 = 0            }, 0,         1,           VE                },
843     { NULL },
844 };
845
846 static const AVClass openjpeg_class = {
847     .class_name = "libopenjpeg",
848     .item_name  = av_default_item_name,
849     .option     = options,
850     .version    = LIBAVUTIL_VERSION_INT,
851 };
852
853 AVCodec ff_libopenjpeg_encoder = {
854     .name           = "libopenjpeg",
855     .long_name      = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
856     .type           = AVMEDIA_TYPE_VIDEO,
857     .id             = AV_CODEC_ID_JPEG2000,
858     .priv_data_size = sizeof(LibOpenJPEGContext),
859     .init           = libopenjpeg_encode_init,
860     .encode2        = libopenjpeg_encode_frame,
861     .close          = libopenjpeg_encode_close,
862     .capabilities   = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_INTRA_ONLY,
863     .pix_fmts       = (const enum AVPixelFormat[]) {
864         AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB48,
865         AV_PIX_FMT_RGBA64, AV_PIX_FMT_GBR24P,
866         AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
867         AV_PIX_FMT_GRAY8, AV_PIX_FMT_YA8, AV_PIX_FMT_GRAY16, AV_PIX_FMT_YA16,
868         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P,
869         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA422P,
870         AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA444P,
871         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
872         AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
873         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
874         AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
875         AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
876         AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
877         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
878         AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
879         AV_PIX_FMT_XYZ12,
880         AV_PIX_FMT_NONE
881     },
882     .priv_class     = &openjpeg_class,
883 };