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