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