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