You've already forked FFmpeg
							
							
				mirror of
				https://github.com/FFmpeg/FFmpeg.git
				synced 2025-10-30 23:18:11 +02:00 
			
		
		
		
	JPEG 2000 decoder for DCinema
Based on the 2007 GSoC project from Kamil Nowosad <k.nowosad@students.mimuw.edu.pl> Updated to current programming standards, style and many more small fixes by Diego Biurrun <diego@biurrun.de>. Signed-off-by: Diego Biurrun <diego@biurrun.de>
This commit is contained in:
		
				
					committed by
					
						 Diego Biurrun
						Diego Biurrun
					
				
			
			
				
	
			
			
			
						parent
						
							0c15a9aa7e
						
					
				
				
					commit
					c81a706381
				
			| @@ -11,6 +11,7 @@ version 10: | ||||
|   filtergraph description to be read from a file | ||||
| - uniform options syntax across all filters | ||||
| - new interlace filter | ||||
| - JPEG 2000 decoder | ||||
|  | ||||
|  | ||||
| version 9: | ||||
|   | ||||
| @@ -373,8 +373,8 @@ following image formats are supported: | ||||
|     @tab Digital Picture Exchange | ||||
| @item JPEG         @tab X @tab X | ||||
|     @tab Progressive JPEG is not supported. | ||||
| @item JPEG 2000    @tab E @tab E | ||||
|     @tab decoding supported through external library libopenjpeg | ||||
| @item JPEG 2000    @tab E @tab X | ||||
|     @tab encoding supported through external library libopenjpeg | ||||
| @item JPEG-LS      @tab X @tab X | ||||
| @item LJPEG        @tab X @tab | ||||
|     @tab Lossless JPEG | ||||
|   | ||||
| @@ -205,6 +205,8 @@ OBJS-$(CONFIG_INDEO4_DECODER)          += indeo4.o ivi_common.o ivi_dsp.o | ||||
| OBJS-$(CONFIG_INDEO5_DECODER)          += indeo5.o ivi_common.o ivi_dsp.o | ||||
| OBJS-$(CONFIG_INTERPLAY_DPCM_DECODER)  += dpcm.o | ||||
| OBJS-$(CONFIG_INTERPLAY_VIDEO_DECODER) += interplayvideo.o | ||||
| OBJS-$(CONFIG_JPEG2000_DECODER)        += jpeg2000dec.o jpeg2000.o      \ | ||||
|                                           jpeg2000dwt.o mqcdec.o mqc.o | ||||
| OBJS-$(CONFIG_JPEGLS_DECODER)          += jpeglsdec.o jpegls.o \ | ||||
|                                           mjpegdec.o mjpeg.o | ||||
| OBJS-$(CONFIG_JPEGLS_ENCODER)          += jpeglsenc.o jpegls.o | ||||
|   | ||||
| @@ -160,6 +160,7 @@ void avcodec_register_all(void) | ||||
|     REGISTER_DECODER(INDEO4,            indeo4); | ||||
|     REGISTER_DECODER(INDEO5,            indeo5); | ||||
|     REGISTER_DECODER(INTERPLAY_VIDEO,   interplay_video); | ||||
|     REGISTER_DECODER(JPEG2000,          jpeg2000); | ||||
|     REGISTER_ENCDEC (JPEGLS,            jpegls); | ||||
|     REGISTER_DECODER(JV,                jv); | ||||
|     REGISTER_DECODER(KGV1,              kgv1); | ||||
|   | ||||
| @@ -2570,6 +2570,12 @@ typedef struct AVCodecContext { | ||||
| #define FF_PROFILE_MPEG4_SIMPLE_STUDIO             14 | ||||
| #define FF_PROFILE_MPEG4_ADVANCED_SIMPLE           15 | ||||
|  | ||||
| #define FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0   0 | ||||
| #define FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1   1 | ||||
| #define FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION  2 | ||||
| #define FF_PROFILE_JPEG2000_DCINEMA_2K              3 | ||||
| #define FF_PROFILE_JPEG2000_DCINEMA_4K              4 | ||||
|  | ||||
|     /** | ||||
|      * level | ||||
|      * - encoding: Set by user. | ||||
|   | ||||
							
								
								
									
										478
									
								
								libavcodec/jpeg2000.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										478
									
								
								libavcodec/jpeg2000.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,478 @@ | ||||
| /* | ||||
|  * JPEG 2000 encoder and decoder common functions | ||||
|  * Copyright (c) 2007 Kamil Nowosad | ||||
|  * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com> | ||||
|  * | ||||
|  * This file is part of Libav. | ||||
|  * | ||||
|  * Libav is free software; you can redistribute it and/or | ||||
|  * modify it under the terms of the GNU Lesser General Public | ||||
|  * License as published by the Free Software Foundation; either | ||||
|  * version 2.1 of the License, or (at your option) any later version. | ||||
|  * | ||||
|  * Libav is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | ||||
|  * Lesser General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU Lesser General Public | ||||
|  * License along with Libav; if not, write to the Free Software | ||||
|  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||
|  */ | ||||
|  | ||||
| /** | ||||
|  * @file | ||||
|  * JPEG 2000 image encoder and decoder common functions | ||||
|  */ | ||||
|  | ||||
| #include "libavutil/common.h" | ||||
| #include "libavutil/mem.h" | ||||
| #include "avcodec.h" | ||||
| #include "jpeg2000.h" | ||||
|  | ||||
| #define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n)) | ||||
|  | ||||
| /* tag tree routines */ | ||||
|  | ||||
| /* allocate the memory for tag tree */ | ||||
| static int32_t tag_tree_size(uint16_t w, uint16_t h) | ||||
| { | ||||
|     uint32_t res = 0; | ||||
|     while (w > 1 || h > 1) { | ||||
|         res += w * h; | ||||
|         if (res + 1 >= INT32_MAX) | ||||
|             return -1; | ||||
|         w = (w + 1) >> 1; | ||||
|         h = (h + 1) >> 1; | ||||
|     } | ||||
|     return (int32_t)(res + 1); | ||||
| } | ||||
|  | ||||
| static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int h) | ||||
| { | ||||
|     int pw = w, ph = h; | ||||
|     Jpeg2000TgtNode *res, *t, *t2; | ||||
|     int32_t tt_size; | ||||
|  | ||||
|     tt_size = tag_tree_size(w, h); | ||||
|     if (tt_size == -1) | ||||
|         return NULL; | ||||
|  | ||||
|     t = res = av_mallocz_array(tt_size, sizeof(*t)); | ||||
|     if (!res) | ||||
|         return NULL; | ||||
|  | ||||
|     while (w > 1 || h > 1) { | ||||
|         int i, j; | ||||
|         pw = w; | ||||
|         ph = h; | ||||
|  | ||||
|         w  = (w + 1) >> 1; | ||||
|         h  = (h + 1) >> 1; | ||||
|         t2 = t + pw * ph; | ||||
|  | ||||
|         for (i = 0; i < ph; i++) | ||||
|             for (j = 0; j < pw; j++) | ||||
|                 t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)]; | ||||
|  | ||||
|         t = t2; | ||||
|     } | ||||
|     t[0].parent = NULL; | ||||
|     return res; | ||||
| } | ||||
|  | ||||
| uint8_t ff_jpeg2000_sigctxno_lut[256][4]; | ||||
|  | ||||
| static int getsigctxno(int flag, int bandno) | ||||
| { | ||||
|     int h, v, d; | ||||
|  | ||||
|     h = ((flag & JPEG2000_T1_SIG_E)  ? 1 : 0) + | ||||
|         ((flag & JPEG2000_T1_SIG_W)  ? 1 : 0); | ||||
|     v = ((flag & JPEG2000_T1_SIG_N)  ? 1 : 0) + | ||||
|         ((flag & JPEG2000_T1_SIG_S)  ? 1 : 0); | ||||
|     d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) + | ||||
|         ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) + | ||||
|         ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) + | ||||
|         ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0); | ||||
|     if (bandno < 3) { | ||||
|         if (bandno == 1) | ||||
|             FFSWAP(int, h, v); | ||||
|         if (h == 2) | ||||
|             return 8; | ||||
|         if (h == 1) { | ||||
|             if (v >= 1) | ||||
|                 return 7; | ||||
|             if (d >= 1) | ||||
|                 return 6; | ||||
|             return 5; | ||||
|         } | ||||
|         if (v == 2) | ||||
|             return 4; | ||||
|         if (v == 1) | ||||
|             return 3; | ||||
|         if (d >= 2) | ||||
|             return 2; | ||||
|         if (d == 1) | ||||
|             return 1; | ||||
|         return 0; | ||||
|     } else { | ||||
|         if (d >= 3) | ||||
|             return 8; | ||||
|         if (d == 2) { | ||||
|             if (h + v >= 1) | ||||
|                 return 7; | ||||
|             return 6; | ||||
|         } | ||||
|         if (d == 1) { | ||||
|             if (h + v >= 2) | ||||
|                 return 5; | ||||
|             if (h + v == 1) | ||||
|                 return 4; | ||||
|             return 3; | ||||
|         } | ||||
|         if (h + v >= 2) | ||||
|             return 2; | ||||
|         if (h + v == 1) | ||||
|             return 1; | ||||
|         return 0; | ||||
|     } | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| uint8_t ff_jpeg2000_sgnctxno_lut[16][16], ff_jpeg2000_xorbit_lut[16][16]; | ||||
|  | ||||
| static const int contribtab[3][3] = { {  0, -1,  1 }, { -1, -1,  0 }, {  1,  0,  1 } }; | ||||
| static const int  ctxlbltab[3][3] = { { 13, 12, 11 }, { 10,  9, 10 }, { 11, 12, 13 } }; | ||||
| static const int  xorbittab[3][3] = { {  1,  1,  1 }, {  1,  0,  0 }, {  0,  0,  0 } }; | ||||
|  | ||||
| static int getsgnctxno(int flag, uint8_t *xorbit) | ||||
| { | ||||
|     int vcontrib, hcontrib; | ||||
|  | ||||
|     hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0] | ||||
|                          [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1; | ||||
|     vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0] | ||||
|                          [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1; | ||||
|     *xorbit = xorbittab[hcontrib][vcontrib]; | ||||
|  | ||||
|     return ctxlbltab[hcontrib][vcontrib]; | ||||
| } | ||||
|  | ||||
| void ff_jpeg2000_init_tier1_luts(void) | ||||
| { | ||||
|     int i, j; | ||||
|     for (i = 0; i < 256; i++) | ||||
|         for (j = 0; j < 4; j++) | ||||
|             ff_jpeg2000_sigctxno_lut[i][j] = getsigctxno(i, j); | ||||
|     for (i = 0; i < 16; i++) | ||||
|         for (j = 0; j < 16; j++) | ||||
|             ff_jpeg2000_sgnctxno_lut[i][j] = | ||||
|                 getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]); | ||||
| } | ||||
|  | ||||
| void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y, | ||||
|                                   int negative) | ||||
| { | ||||
|     x++; | ||||
|     y++; | ||||
|     t1->flags[y][x] |= JPEG2000_T1_SIG; | ||||
|     if (negative) { | ||||
|         t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W; | ||||
|         t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E; | ||||
|         t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N; | ||||
|         t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S; | ||||
|     } else { | ||||
|         t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W; | ||||
|         t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E; | ||||
|         t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N; | ||||
|         t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S; | ||||
|     } | ||||
|     t1->flags[y + 1][x + 1] |= JPEG2000_T1_SIG_NW; | ||||
|     t1->flags[y + 1][x - 1] |= JPEG2000_T1_SIG_NE; | ||||
|     t1->flags[y - 1][x + 1] |= JPEG2000_T1_SIG_SW; | ||||
|     t1->flags[y - 1][x - 1] |= JPEG2000_T1_SIG_SE; | ||||
| } | ||||
|  | ||||
| static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } }; | ||||
|  | ||||
| int ff_jpeg2000_init_component(Jpeg2000Component *comp, | ||||
|                                Jpeg2000CodingStyle *codsty, | ||||
|                                Jpeg2000QuantStyle *qntsty, | ||||
|                                int cbps, int dx, int dy, | ||||
|                                AVCodecContext *avctx) | ||||
| { | ||||
|     uint8_t log2_band_prec_width, log2_band_prec_height; | ||||
|     int reslevelno, bandno, gbandno = 0, ret, i, j; | ||||
|     uint32_t csize = 1; | ||||
|  | ||||
|     if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord, | ||||
|                                    codsty->nreslevels2decode - 1, | ||||
|                                    codsty->transform)) | ||||
|         return ret; | ||||
|     // component size comp->coord is uint16_t so ir cannot overflow | ||||
|     csize = (comp->coord[0][1] - comp->coord[0][0]) * | ||||
|             (comp->coord[1][1] - comp->coord[1][0]); | ||||
|  | ||||
|     comp->data = av_malloc_array(csize, sizeof(*comp->data)); | ||||
|     if (!comp->data) | ||||
|         return AVERROR(ENOMEM); | ||||
|     comp->reslevel = av_malloc_array(codsty->nreslevels, sizeof(*comp->reslevel)); | ||||
|     if (!comp->reslevel) | ||||
|         return AVERROR(ENOMEM); | ||||
|     /* LOOP on resolution levels */ | ||||
|     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) { | ||||
|         int declvl = codsty->nreslevels - reslevelno;    // N_L -r see  ISO/IEC 15444-1:2002 B.5 | ||||
|         Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno; | ||||
|  | ||||
|         /* Compute borders for each resolution level. | ||||
|          * Computation of trx_0, trx_1, try_0 and try_1. | ||||
|          * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */ | ||||
|         for (i = 0; i < 2; i++) | ||||
|             for (j = 0; j < 2; j++) | ||||
|                 reslevel->coord[i][j] = | ||||
|                     ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1); | ||||
|         // update precincts size: 2^n value | ||||
|         reslevel->log2_prec_width  = codsty->log2_prec_widths[reslevelno]; | ||||
|         reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno]; | ||||
|  | ||||
|         /* Number of bands for each resolution level */ | ||||
|         if (reslevelno == 0) | ||||
|             reslevel->nbands = 1; | ||||
|         else | ||||
|             reslevel->nbands = 3; | ||||
|  | ||||
|         /* Number of precincts wich span the tile for resolution level reslevelno | ||||
|          * see B.6 in ISO/IEC 15444-1:2002 eq. B-16 | ||||
|          * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width) | ||||
|          * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width) | ||||
|          * for Dcinema profiles in JPEG 2000 | ||||
|          * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| | ||||
|          * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */ | ||||
|         if (reslevel->coord[0][1] == reslevel->coord[0][0]) | ||||
|             reslevel->num_precincts_x = 0; | ||||
|         else | ||||
|             reslevel->num_precincts_x = | ||||
|                 ff_jpeg2000_ceildivpow2(reslevel->coord[0][1], | ||||
|                                         reslevel->log2_prec_width) - | ||||
|                 (reslevel->coord[0][0] >> reslevel->log2_prec_width); | ||||
|  | ||||
|         if (reslevel->coord[1][1] == reslevel->coord[1][0]) | ||||
|             reslevel->num_precincts_y = 0; | ||||
|         else | ||||
|             reslevel->num_precincts_y = | ||||
|                 ff_jpeg2000_ceildivpow2(reslevel->coord[1][1], | ||||
|                                         reslevel->log2_prec_height) - | ||||
|                 (reslevel->coord[1][0] >> reslevel->log2_prec_height); | ||||
|  | ||||
|         reslevel->band = av_malloc_array(reslevel->nbands, sizeof(*reslevel->band)); | ||||
|         if (!reslevel->band) | ||||
|             return AVERROR(ENOMEM); | ||||
|  | ||||
|         for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) { | ||||
|             Jpeg2000Band *band = reslevel->band + bandno; | ||||
|             int cblkno, precno; | ||||
|             int nb_precincts; | ||||
|  | ||||
|             /* TODO: Implementation of quantization step not finished, | ||||
|              * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */ | ||||
|             switch (qntsty->quantsty) { | ||||
|                 uint8_t gain; | ||||
|                 int numbps; | ||||
|             case JPEG2000_QSTY_NONE: | ||||
|                 /* TODO: to verify. No quantization in this case */ | ||||
|                 numbps = cbps + | ||||
|                          lut_gain[codsty->transform][bandno + reslevelno > 0]; | ||||
|                 band->stepsize = (float)SHL(2048 + qntsty->mant[gbandno], | ||||
|                                             2 + numbps - qntsty->expn[gbandno]); | ||||
|                 break; | ||||
|             case JPEG2000_QSTY_SI: | ||||
|                 /*TODO: Compute formula to implement. */ | ||||
|                 band->stepsize = (float) (1 << 13); | ||||
|                 break; | ||||
|             case JPEG2000_QSTY_SE: | ||||
|                 /* Exponent quantization step. | ||||
|                  * Formula: | ||||
|                  * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11)) | ||||
|                  * R_b = R_I + log2 (gain_b ) | ||||
|                  * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */ | ||||
|                 /* TODO/WARN: value of log2 (gain_b ) not taken into account | ||||
|                  * but it works (compared to OpenJPEG). Why? | ||||
|                  * Further investigation needed. */ | ||||
|                 gain            = cbps; | ||||
|                 band->stepsize  = pow(2.0, gain - qntsty->expn[gbandno]); | ||||
|                 band->stepsize *= (float)qntsty->mant[gbandno] / 2048.0 + 1.0; | ||||
|                 /* FIXME: In openjepg code stespize = stepsize * 0.5. Why? | ||||
|                  * If not set output of entropic decoder is not correct. */ | ||||
|                 band->stepsize *= 0.5; | ||||
|                 break; | ||||
|             default: | ||||
|                 band->stepsize = 0; | ||||
|                 av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n"); | ||||
|                 break; | ||||
|             } | ||||
|             /* BITEXACT computing case --> convert to int */ | ||||
|             if (avctx->flags & CODEC_FLAG_BITEXACT) | ||||
|                 band->stepsize = (int32_t)(band->stepsize * (1 << 16)); | ||||
|  | ||||
|             /* computation of tbx_0, tbx_1, tby_0, tby_1 | ||||
|              * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1 | ||||
|              * codeblock width and height is computed for | ||||
|              * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */ | ||||
|             if (reslevelno == 0) { | ||||
|                 /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */ | ||||
|                 for (i = 0; i < 2; i++) | ||||
|                     for (j = 0; j < 2; j++) | ||||
|                         band->coord[i][j] = | ||||
|                             ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], | ||||
|                                                     declvl - 1); | ||||
|  | ||||
|                 log2_band_prec_width  = reslevel->log2_prec_width; | ||||
|                 log2_band_prec_height = reslevel->log2_prec_height; | ||||
|                 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */ | ||||
|                 band->log2_cblk_width  = FFMIN(codsty->log2_cblk_width, | ||||
|                                                reslevel->log2_prec_width); | ||||
|                 band->log2_cblk_height = FFMIN(codsty->log2_cblk_height, | ||||
|                                                reslevel->log2_prec_height); | ||||
|             } else { | ||||
|                 /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */ | ||||
|                 /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */ | ||||
|                 for (i = 0; i < 2; i++) | ||||
|                     for (j = 0; j < 2; j++) | ||||
|                         /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */ | ||||
|                         band->coord[i][j] = | ||||
|                             ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] - | ||||
|                                                     (((bandno + 1 >> i) & 1) << declvl - 1), | ||||
|                                                     declvl); | ||||
|                 /* TODO: Manage case of 3 band offsets here or | ||||
|                  * in coding/decoding function? */ | ||||
|  | ||||
|                 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */ | ||||
|                 band->log2_cblk_width  = FFMIN(codsty->log2_cblk_width, | ||||
|                                                reslevel->log2_prec_width - 1); | ||||
|                 band->log2_cblk_height = FFMIN(codsty->log2_cblk_height, | ||||
|                                                reslevel->log2_prec_height - 1); | ||||
|  | ||||
|                 log2_band_prec_width  = reslevel->log2_prec_width  - 1; | ||||
|                 log2_band_prec_height = reslevel->log2_prec_height - 1; | ||||
|             } | ||||
|  | ||||
|             band->prec = av_malloc_array(reslevel->num_precincts_x * | ||||
|                                          reslevel->num_precincts_y, | ||||
|                                          sizeof(*band->prec)); | ||||
|             if (!band->prec) | ||||
|                 return AVERROR(ENOMEM); | ||||
|  | ||||
|             nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y; | ||||
|  | ||||
|             for (precno = 0; precno < nb_precincts; precno++) { | ||||
|                 Jpeg2000Prec *prec = band->prec + precno; | ||||
|  | ||||
|                 /* TODO: Explain formula for JPEG200 DCINEMA. */ | ||||
|                 /* TODO: Verify with previous count of codeblocks per band */ | ||||
|  | ||||
|                 /* Compute P_x0 */ | ||||
|                 prec->coord[0][0] = (precno % reslevel->num_precincts_x) * | ||||
|                                     (1 << log2_band_prec_width); | ||||
|                 prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]); | ||||
|  | ||||
|                 /* Compute P_y0 */ | ||||
|                 prec->coord[1][0] = (precno / reslevel->num_precincts_x) * | ||||
|                                     (1 << log2_band_prec_height); | ||||
|                 prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]); | ||||
|  | ||||
|                 /* Compute P_x1 */ | ||||
|                 prec->coord[0][1] = prec->coord[0][0] + | ||||
|                                     (1 << log2_band_prec_width); | ||||
|                 prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]); | ||||
|  | ||||
|                 /* Compute P_y1 */ | ||||
|                 prec->coord[1][1] = prec->coord[1][0] + | ||||
|                                     (1 << log2_band_prec_height); | ||||
|                 prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]); | ||||
|  | ||||
|                 prec->nb_codeblocks_width = | ||||
|                     ff_jpeg2000_ceildivpow2(prec->coord[0][1] - | ||||
|                                             prec->coord[0][0], | ||||
|                                             band->log2_cblk_width); | ||||
|                 prec->nb_codeblocks_height = | ||||
|                     ff_jpeg2000_ceildivpow2(prec->coord[1][1] - | ||||
|                                             prec->coord[1][0], | ||||
|                                             band->log2_cblk_height); | ||||
|  | ||||
|                 /* Tag trees initialization */ | ||||
|                 prec->cblkincl = | ||||
|                     ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width, | ||||
|                                               prec->nb_codeblocks_height); | ||||
|                 if (!prec->cblkincl) | ||||
|                     return AVERROR(ENOMEM); | ||||
|  | ||||
|                 prec->zerobits = | ||||
|                     ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width, | ||||
|                                               prec->nb_codeblocks_height); | ||||
|                 if (!prec->zerobits) | ||||
|                     return AVERROR(ENOMEM); | ||||
|  | ||||
|                 prec->cblk = av_malloc_array(prec->nb_codeblocks_width * | ||||
|                                              prec->nb_codeblocks_height, | ||||
|                                              sizeof(*prec->cblk)); | ||||
|                 if (!prec->cblk) | ||||
|                     return AVERROR(ENOMEM); | ||||
|                 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) { | ||||
|                     Jpeg2000Cblk *cblk = prec->cblk + cblkno; | ||||
|                     uint16_t Cx0, Cy0; | ||||
|  | ||||
|                     /* Compute coordinates of codeblocks */ | ||||
|                     /* Compute Cx0*/ | ||||
|                     Cx0 = (prec->coord[0][0] >> band->log2_cblk_width) << band->log2_cblk_width; | ||||
|                     Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width)  << band->log2_cblk_width); | ||||
|                     cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]); | ||||
|  | ||||
|                     /* Compute Cy0*/ | ||||
|                     Cy0 = (prec->coord[1][0] >> band->log2_cblk_height) << band->log2_cblk_height; | ||||
|                     Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width)   << band->log2_cblk_height); | ||||
|                     cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]); | ||||
|  | ||||
|                     /* Compute Cx1 */ | ||||
|                     cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width), | ||||
|                                               prec->coord[0][1]); | ||||
|  | ||||
|                     /* Compute Cy1 */ | ||||
|                     cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height), | ||||
|                                               prec->coord[1][1]); | ||||
|                     cblk->zero      = 0; | ||||
|                     cblk->lblock    = 3; | ||||
|                     cblk->length    = 0; | ||||
|                     cblk->lengthinc = 0; | ||||
|                     cblk->npasses   = 0; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty) | ||||
| { | ||||
|     int reslevelno, bandno, precno; | ||||
|     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) { | ||||
|         Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno; | ||||
|  | ||||
|         for (bandno = 0; bandno < reslevel->nbands; bandno++) { | ||||
|             Jpeg2000Band *band = reslevel->band + bandno; | ||||
|             for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) { | ||||
|                 Jpeg2000Prec *prec = band->prec + precno; | ||||
|                 av_freep(&prec->zerobits); | ||||
|                 av_freep(&prec->cblkincl); | ||||
|                 av_freep(&prec->cblk); | ||||
|             } | ||||
|  | ||||
|             av_freep(&band->prec); | ||||
|         } | ||||
|         av_freep(&reslevel->band); | ||||
|     } | ||||
|  | ||||
|     ff_dwt_destroy(&comp->dwt); | ||||
|     av_freep(&comp->reslevel); | ||||
|     av_freep(&comp->data); | ||||
| } | ||||
							
								
								
									
										268
									
								
								libavcodec/jpeg2000.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										268
									
								
								libavcodec/jpeg2000.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,268 @@ | ||||
| /* | ||||
|  * JPEG 2000 common defines, structures and functions | ||||
|  * Copyright (c) 2007 Kamil Nowosad | ||||
|  * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com> | ||||
|  * | ||||
|  * This file is part of Libav. | ||||
|  * | ||||
|  * Libav is free software; you can redistribute it and/or | ||||
|  * modify it under the terms of the GNU Lesser General Public | ||||
|  * License as published by the Free Software Foundation; either | ||||
|  * version 2.1 of the License, or (at your option) any later version. | ||||
|  * | ||||
|  * Libav is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | ||||
|  * Lesser General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU Lesser General Public | ||||
|  * License along with Libav; if not, write to the Free Software | ||||
|  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||
|  */ | ||||
|  | ||||
| #ifndef AVCODEC_JPEG2000_H | ||||
| #define AVCODEC_JPEG2000_H | ||||
|  | ||||
| /** | ||||
|  * @file | ||||
|  * JPEG 2000 structures and defines common | ||||
|  * to encoder and decoder | ||||
|  */ | ||||
|  | ||||
| #include <stdint.h> | ||||
|  | ||||
| #include "avcodec.h" | ||||
| #include "mqc.h" | ||||
| #include "jpeg2000dwt.h" | ||||
|  | ||||
| enum Jpeg2000Markers { | ||||
|     JPEG2000_SOC = 0xff4f, // start of codestream | ||||
|     JPEG2000_SIZ = 0xff51, // image and tile size | ||||
|     JPEG2000_COD,          // coding style default | ||||
|     JPEG2000_COC,          // coding style component | ||||
|     JPEG2000_TLM = 0xff55, // packed packet headers, tile-part header | ||||
|     JPEG2000_PLM = 0xff57, // tile-part lengths | ||||
|     JPEG2000_PLT,          // packet length, main header | ||||
|     JPEG2000_QCD = 0xff5c, // quantization default | ||||
|     JPEG2000_QCC,          // quantization component | ||||
|     JPEG2000_RGN,          // region of interest | ||||
|     JPEG2000_POC,          // progression order change | ||||
|     JPEG2000_PPM,          // packet length, tile-part header | ||||
|     JPEG2000_PPT,          // packed packet headers, main header | ||||
|     JPEG2000_CRG = 0xff63, // component registration | ||||
|     JPEG2000_COM,          // comment | ||||
|     JPEG2000_SOT = 0xff90, // start of tile-part | ||||
|     JPEG2000_SOP,          // start of packet | ||||
|     JPEG2000_EPH,          // end of packet header | ||||
|     JPEG2000_SOD,          // start of data | ||||
|     JPEG2000_EOC = 0xffd9, // end of codestream | ||||
| }; | ||||
|  | ||||
| enum Jpeg2000Quantsty { // quantization style | ||||
|     JPEG2000_QSTY_NONE, // no quantization | ||||
|     JPEG2000_QSTY_SI,   // scalar derived | ||||
|     JPEG2000_QSTY_SE    // scalar expounded | ||||
| }; | ||||
|  | ||||
| #define JPEG2000_MAX_CBLKW 64 | ||||
| #define JPEG2000_MAX_CBLKH 64 | ||||
|  | ||||
| #define JPEG2000_MAX_RESLEVELS 33 | ||||
|  | ||||
| // T1 flags | ||||
| // flags determining significance of neighbor coefficients | ||||
| #define JPEG2000_T1_SIG_N  0x0001 | ||||
| #define JPEG2000_T1_SIG_E  0x0002 | ||||
| #define JPEG2000_T1_SIG_W  0x0004 | ||||
| #define JPEG2000_T1_SIG_S  0x0008 | ||||
| #define JPEG2000_T1_SIG_NE 0x0010 | ||||
| #define JPEG2000_T1_SIG_NW 0x0020 | ||||
| #define JPEG2000_T1_SIG_SE 0x0040 | ||||
| #define JPEG2000_T1_SIG_SW 0x0080 | ||||
| #define JPEG2000_T1_SIG_NB (JPEG2000_T1_SIG_N  | JPEG2000_T1_SIG_E  |   \ | ||||
|                             JPEG2000_T1_SIG_S  | JPEG2000_T1_SIG_W  |   \ | ||||
|                             JPEG2000_T1_SIG_NE | JPEG2000_T1_SIG_NW |   \ | ||||
|                             JPEG2000_T1_SIG_SE | JPEG2000_T1_SIG_SW) | ||||
| // flags determining sign bit of neighbor coefficients | ||||
| #define JPEG2000_T1_SGN_N  0x0100 | ||||
| #define JPEG2000_T1_SGN_S  0x0200 | ||||
| #define JPEG2000_T1_SGN_W  0x0400 | ||||
| #define JPEG2000_T1_SGN_E  0x0800 | ||||
|  | ||||
| #define JPEG2000_T1_VIS    0x1000 | ||||
| #define JPEG2000_T1_SIG    0x2000 | ||||
| #define JPEG2000_T1_REF    0x4000 | ||||
|  | ||||
| #define JPEG2000_T1_SGN    0x8000 | ||||
|  | ||||
| // Codeblock coding styles | ||||
| #define JPEG2000_CBLK_BYPASS    0x01 // Selective arithmetic coding bypass | ||||
| #define JPEG2000_CBLK_RESET     0x02 // Reset context probabilities | ||||
| #define JPEG2000_CBLK_TERMALL   0x04 // Terminate after each coding pass | ||||
| #define JPEG2000_CBLK_VSC       0x08 // Vertical stripe causal context formation | ||||
| #define JPEG2000_CBLK_PREDTERM  0x10 // Predictable termination | ||||
| #define JPEG2000_CBLK_SEGSYM    0x20 // Segmentation symbols present | ||||
|  | ||||
| // Coding styles | ||||
| #define JPEG2000_CSTY_PREC      0x01 // Precincts defined in coding style | ||||
| #define JPEG2000_CSTY_SOP       0x02 // SOP marker present | ||||
| #define JPEG2000_CSTY_EPH       0x04 // EPH marker present | ||||
|  | ||||
| // Progression orders | ||||
| #define JPEG2000_PGOD_LRCP      0x00  // Layer-resolution level-component-position progression | ||||
| #define JPEG2000_PGOD_RLCP      0x01  // Resolution level-layer-component-position progression | ||||
| #define JPEG2000_PGOD_RPCL      0x02  // Resolution level-position-component-layer progression | ||||
| #define JPEG2000_PGOD_PCRL      0x03  // Position-component-resolution level-layer progression | ||||
| #define JPEG2000_PGOD_CPRL      0x04  // Component-position-resolution level-layer progression | ||||
|  | ||||
| typedef struct Jpeg2000T1Context { | ||||
|     int data[JPEG2000_MAX_CBLKW][JPEG2000_MAX_CBLKH]; | ||||
|     int flags[JPEG2000_MAX_CBLKW + 2][JPEG2000_MAX_CBLKH + 2]; | ||||
|     MqcState mqc; | ||||
| } Jpeg2000T1Context; | ||||
|  | ||||
| typedef struct Jpeg2000TgtNode { | ||||
|     uint8_t val; | ||||
|     uint8_t vis; | ||||
|     struct Jpeg2000TgtNode *parent; | ||||
| } Jpeg2000TgtNode; | ||||
|  | ||||
| typedef struct Jpeg2000CodingStyle { | ||||
|     uint8_t nreslevels;       // number of resolution levels | ||||
|     uint8_t nreslevels2decode; // number of resolution levels to decode | ||||
|     uint8_t log2_cblk_width, | ||||
|             log2_cblk_height; // exponent of codeblock size | ||||
|     uint8_t transform;        // DWT type | ||||
|     uint8_t csty;             // coding style | ||||
|     uint8_t log2_prec_width, | ||||
|             log2_prec_height; // precinct size | ||||
|     uint8_t nlayers;          // number of layers | ||||
|     uint8_t mct;              // multiple component transformation | ||||
|     uint8_t cblk_style;       // codeblock coding style | ||||
|     uint8_t prog_order;       // progression order | ||||
|     uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS];  // precincts size according resolution levels | ||||
|     uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]; // TODO: initialize prec_size array with 0? | ||||
| } Jpeg2000CodingStyle; | ||||
|  | ||||
| typedef struct Jpeg2000QuantStyle { | ||||
|     uint8_t expn[32 * 3];  // quantization exponent | ||||
|     uint32_t mant[32 * 3]; // quantization mantissa | ||||
|     uint8_t quantsty;      // quantization style | ||||
|     uint8_t nguardbits;    // number of guard bits | ||||
| } Jpeg2000QuantStyle; | ||||
|  | ||||
| typedef struct Jpeg2000Pass { | ||||
|     uint16_t rate; | ||||
|     int64_t disto; | ||||
| } Jpeg2000Pass; | ||||
|  | ||||
| typedef struct Jpeg2000Cblk { | ||||
|     uint8_t npasses; | ||||
|     uint8_t ninclpasses; // number coding of passes included in codestream | ||||
|     uint8_t nonzerobits; | ||||
|     uint16_t length; | ||||
|     uint16_t lengthinc; | ||||
|     uint8_t lblock; | ||||
|     uint8_t zero; | ||||
|     uint8_t data[8192]; | ||||
|     Jpeg2000Pass passes[100]; | ||||
|     uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}} | ||||
| } Jpeg2000Cblk; // code block | ||||
|  | ||||
| typedef struct Jpeg2000Prec { | ||||
|     uint16_t xi0, yi0; // codeblock indexes ([xi0, xi1)) | ||||
|     uint16_t nb_codeblocks_width; | ||||
|     uint16_t nb_codeblocks_height; | ||||
|     Jpeg2000TgtNode *zerobits; | ||||
|     Jpeg2000TgtNode *cblkincl; | ||||
|     Jpeg2000Cblk *cblk; | ||||
|     uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}} | ||||
| } Jpeg2000Prec; // precinct | ||||
|  | ||||
| /* TODO: stepsize can be float or integer depending on | ||||
|  * reversible or irreversible transformation. */ | ||||
| typedef struct Jpeg2000Band { | ||||
|     uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}} | ||||
|     uint16_t log2_cblk_width, log2_cblk_height; | ||||
|     uint16_t cblknx, cblkny; | ||||
|     float stepsize; // quantization stepsize | ||||
|     Jpeg2000Prec *prec; | ||||
| } Jpeg2000Band; // subband | ||||
|  | ||||
| typedef struct Jpeg2000ResLevel { | ||||
|     uint8_t nbands; | ||||
|     uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}} | ||||
|     uint16_t num_precincts_x, num_precincts_y; // number of precincts in x/y direction | ||||
|     uint8_t log2_prec_width, log2_prec_height; // exponent of precinct size | ||||
|     Jpeg2000Band *band; | ||||
| } Jpeg2000ResLevel; // resolution level | ||||
|  | ||||
| /* TODO: data can be float of integer depending of reversible/irreversible | ||||
|  * transformation. | ||||
|  */ | ||||
| typedef struct Jpeg2000Component { | ||||
|     Jpeg2000ResLevel *reslevel; | ||||
|     DWTContext dwt; | ||||
|     float *data; | ||||
|     uint16_t coord[2][2];   // border coordinates {{x0, x1}, {y0, y1}} -- can be reduced with lowres option | ||||
|     uint16_t coord_o[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- original values from jpeg2000 headers | ||||
| } Jpeg2000Component; | ||||
|  | ||||
| /* misc tools */ | ||||
| static inline int ff_jpeg2000_ceildivpow2(int a, int b) | ||||
| { | ||||
|     return (a + (1 << b) - 1) >> b; | ||||
| } | ||||
|  | ||||
| static inline int ff_jpeg2000_ceildiv(int a, int b) | ||||
| { | ||||
|     return (a + b - 1) / b; | ||||
| } | ||||
|  | ||||
| /* TIER-1 routines */ | ||||
|  | ||||
| /* Set up lookup tables used in TIER-1. */ | ||||
| void ff_jpeg2000_init_tier1_luts(void); | ||||
|  | ||||
| /* Update significance of a coefficient at current position (x,y) and | ||||
|  * for neighbors. */ | ||||
| void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, | ||||
|                                   int x, int y, int negative); | ||||
|  | ||||
| extern uint8_t ff_jpeg2000_sigctxno_lut[256][4]; | ||||
|  | ||||
| /* Get context label (number in range[0..8]) of a coefficient for significance | ||||
|  * propagation and cleanup coding passes. */ | ||||
| static inline int ff_jpeg2000_getsigctxno(int flag, int bandno) | ||||
| { | ||||
|     return ff_jpeg2000_sigctxno_lut[flag & 255][bandno]; | ||||
| } | ||||
|  | ||||
| static const uint8_t refctxno_lut[2][2] = { { 14, 15 }, { 16, 16 } }; | ||||
|  | ||||
| /* Get context label (number in range[14..16]) of a coefficient for magnitude | ||||
|  * refinement pass. */ | ||||
| static inline int ff_jpeg2000_getrefctxno(int flag) | ||||
| { | ||||
|     return refctxno_lut[(flag >> 14) & 1][(flag & 255) != 0]; | ||||
| } | ||||
|  | ||||
| extern uint8_t ff_jpeg2000_sgnctxno_lut[16][16]; | ||||
| extern uint8_t ff_jpeg2000_xorbit_lut[16][16]; | ||||
|  | ||||
| /* Get context label (number in range[9..13]) for sign decoding. */ | ||||
| static inline int ff_jpeg2000_getsgnctxno(int flag, int *xorbit) | ||||
| { | ||||
|     *xorbit = ff_jpeg2000_xorbit_lut[flag & 15][(flag >> 8) & 15]; | ||||
|     return ff_jpeg2000_sgnctxno_lut[flag & 15][(flag >> 8) & 15]; | ||||
| } | ||||
|  | ||||
| int ff_jpeg2000_init_component(Jpeg2000Component *comp, | ||||
|                                Jpeg2000CodingStyle *codsty, | ||||
|                                Jpeg2000QuantStyle *qntsty, | ||||
|                                int cbps, int dx, int dy, | ||||
|                                AVCodecContext *ctx); | ||||
|  | ||||
| void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty); | ||||
|  | ||||
| #endif /* AVCODEC_JPEG2000_H */ | ||||
							
								
								
									
										1336
									
								
								libavcodec/jpeg2000dec.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1336
									
								
								libavcodec/jpeg2000dec.c
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										371
									
								
								libavcodec/jpeg2000dwt.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										371
									
								
								libavcodec/jpeg2000dwt.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,371 @@ | ||||
| /* | ||||
|  * Discrete wavelet transform | ||||
|  * Copyright (c) 2007 Kamil Nowosad | ||||
|  * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com> | ||||
|  * | ||||
|  * This file is part of Libav. | ||||
|  * | ||||
|  * Libav is free software; you can redistribute it and/or | ||||
|  * modify it under the terms of the GNU Lesser General Public | ||||
|  * License as published by the Free Software Foundation; either | ||||
|  * version 2.1 of the License, or (at your option) any later version. | ||||
|  * | ||||
|  * Libav is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | ||||
|  * Lesser General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU Lesser General Public | ||||
|  * License along with Libav; if not, write to the Free Software | ||||
|  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||
|  */ | ||||
|  | ||||
| /** | ||||
|  * @file | ||||
|  * Discrete wavelet transform | ||||
|  */ | ||||
|  | ||||
| #include "libavutil/common.h" | ||||
| #include "libavutil/mem.h" | ||||
| #include "jpeg2000dwt.h" | ||||
| #include "internal.h" | ||||
|  | ||||
| /* Defines for 9/7 DWT lifting parameters. | ||||
|  * Parameters are in float. */ | ||||
| #define F_LFTG_ALPHA  1.586134342059924f | ||||
| #define F_LFTG_BETA   0.052980118572961f | ||||
| #define F_LFTG_GAMMA  0.882911075530934f | ||||
| #define F_LFTG_DELTA  0.443506852043971f | ||||
| #define F_LFTG_K      1.230174104914001f | ||||
| #define F_LFTG_X      1.625732422f | ||||
| /* FIXME: Why use 1.625732422 instead of 1/F_LFTG_K? | ||||
|  * Incorrect value in JPEG2000 norm. | ||||
|  * see (ISO/IEC 15444:1 (version 2002) F.3.8.2 */ | ||||
|  | ||||
| /* Lifting parameters in integer format. | ||||
|  * Computed as param = (float param) * (1 << 16) */ | ||||
| #define I_LFTG_ALPHA  103949 | ||||
| #define I_LFTG_BETA     3472 | ||||
| #define I_LFTG_GAMMA   57862 | ||||
| #define I_LFTG_DELTA   29066 | ||||
| #define I_LFTG_K       80621 | ||||
| #define I_LFTG_X      106544 | ||||
|  | ||||
|  | ||||
| static inline void extend53(int *p, int i0, int i1) | ||||
| { | ||||
|     p[i0 - 1] = p[i0 + 1]; | ||||
|     p[i1]     = p[i1 - 2]; | ||||
|     p[i0 - 2] = p[i0 + 2]; | ||||
|     p[i1 + 1] = p[i1 - 3]; | ||||
| } | ||||
|  | ||||
| static inline void extend97_float(float *p, int i0, int i1) | ||||
| { | ||||
|     int i; | ||||
|  | ||||
|     for (i = 1; i <= 4; i++) { | ||||
|         p[i0 - i]     = p[i0 + i]; | ||||
|         p[i1 + i - 1] = p[i1 - i - 1]; | ||||
|     } | ||||
| } | ||||
|  | ||||
| static inline void extend97_int(int32_t *p, int i0, int i1) | ||||
| { | ||||
|     int i; | ||||
|  | ||||
|     for (i = 1; i <= 4; i++) { | ||||
|         p[i0 - i]     = p[i0 + i]; | ||||
|         p[i1 + i - 1] = p[i1 - i - 1]; | ||||
|     } | ||||
| } | ||||
|  | ||||
| static void sr_1d53(int *p, int i0, int i1) | ||||
| { | ||||
|     int i; | ||||
|  | ||||
|     if (i1 == i0 + 1) | ||||
|         return; | ||||
|  | ||||
|     extend53(p, i0, i1); | ||||
|  | ||||
|     for (i = i0 / 2; i < i1 / 2 + 1; i++) | ||||
|         p[2 * i] -= (p[2 * i - 1] + p[2 * i + 1] + 2) >> 2; | ||||
|     for (i = i0 / 2; i < i1 / 2; i++) | ||||
|         p[2 * i + 1] += (p[2 * i] + p[2 * i + 2]) >> 1; | ||||
| } | ||||
|  | ||||
| static void dwt_decode53(DWTContext *s, int *t) | ||||
| { | ||||
|     int lev; | ||||
|     int w     = s->linelen[s->ndeclevels - 1][0]; | ||||
|     int32_t *line = s->i_linebuf; | ||||
|     line += 3; | ||||
|  | ||||
|     for (lev = 0; lev < s->ndeclevels; lev++) { | ||||
|         int lh = s->linelen[lev][0], | ||||
|             lv = s->linelen[lev][1], | ||||
|             mh = s->mod[lev][0], | ||||
|             mv = s->mod[lev][1], | ||||
|             lp; | ||||
|         int *l; | ||||
|  | ||||
|         // HOR_SD | ||||
|         l = line + mh; | ||||
|         for (lp = 0; lp < lv; lp++) { | ||||
|             int i, j = 0; | ||||
|             // copy with interleaving | ||||
|             for (i = mh; i < lh; i += 2, j++) | ||||
|                 l[i] = t[w * lp + j]; | ||||
|             for (i = 1 - mh; i < lh; i += 2, j++) | ||||
|                 l[i] = t[w * lp + j]; | ||||
|  | ||||
|             sr_1d53(line, mh, mh + lh); | ||||
|  | ||||
|             for (i = 0; i < lh; i++) | ||||
|                 t[w * lp + i] = l[i]; | ||||
|         } | ||||
|  | ||||
|         // VER_SD | ||||
|         l = line + mv; | ||||
|         for (lp = 0; lp < lh; lp++) { | ||||
|             int i, j = 0; | ||||
|             // copy with interleaving | ||||
|             for (i = mv; i < lv; i += 2, j++) | ||||
|                 l[i] = t[w * j + lp]; | ||||
|             for (i = 1 - mv; i < lv; i += 2, j++) | ||||
|                 l[i] = t[w * j + lp]; | ||||
|  | ||||
|             sr_1d53(line, mv, mv + lv); | ||||
|  | ||||
|             for (i = 0; i < lv; i++) | ||||
|                 t[w * i + lp] = l[i]; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| static void sr_1d97_float(float *p, int i0, int i1) | ||||
| { | ||||
|     int i; | ||||
|  | ||||
|     if (i1 == i0 + 1) | ||||
|         return; | ||||
|  | ||||
|     extend97_float(p, i0, i1); | ||||
|  | ||||
|     /*step 1*/ | ||||
|     for (i = i0 / 2 - 1; i < i1 / 2 + 2; i++) | ||||
|         p[2 * i]     *= F_LFTG_K; | ||||
|     /* step 2*/ | ||||
|     for (i = i0 / 2 - 2; i < i1 / 2 + 2; i++) | ||||
|         p[2 * i + 1] *= F_LFTG_X; | ||||
|     /* step 3*/ | ||||
|     for (i = i0 / 2 - 1; i < i1 / 2 + 2; i++) | ||||
|         p[2 * i]     -= F_LFTG_DELTA * (p[2 * i - 1] + p[2 * i + 1]); | ||||
|     /* step 4 */ | ||||
|     for (i = i0 / 2 - 1; i < i1 / 2 + 1; i++) | ||||
|         p[2 * i + 1] -= F_LFTG_GAMMA * (p[2 * i]     + p[2 * i + 2]); | ||||
|     /*step 5*/ | ||||
|     for (i = i0 / 2; i < i1 / 2 + 1; i++) | ||||
|         p[2 * i]     += F_LFTG_BETA  * (p[2 * i - 1] + p[2 * i + 1]); | ||||
|     /* step 6 */ | ||||
|     for (i = i0 / 2; i < i1 / 2; i++) | ||||
|         p[2 * i + 1] += F_LFTG_ALPHA * (p[2 * i]     + p[2 * i + 2]); | ||||
| } | ||||
|  | ||||
| static void dwt_decode97_float(DWTContext *s, float *t) | ||||
| { | ||||
|     int lev; | ||||
|     int w       = s->linelen[s->ndeclevels - 1][0]; | ||||
|     float *line = s->f_linebuf; | ||||
|     float *data = t; | ||||
|     /* position at index O of line range [0-5,w+5] cf. extend function */ | ||||
|     line += 5; | ||||
|  | ||||
|     for (lev = 0; lev < s->ndeclevels; lev++) { | ||||
|         int lh = s->linelen[lev][0], | ||||
|             lv = s->linelen[lev][1], | ||||
|             mh = s->mod[lev][0], | ||||
|             mv = s->mod[lev][1], | ||||
|             lp; | ||||
|         float *l; | ||||
|         // HOR_SD | ||||
|         l = line + mh; | ||||
|         for (lp = 0; lp < lv; lp++) { | ||||
|             int i, j = 0; | ||||
|             // copy with interleaving | ||||
|             for (i = mh; i < lh; i += 2, j++) | ||||
|                 l[i] = data[w * lp + j]; | ||||
|             for (i = 1 - mh; i < lh; i += 2, j++) | ||||
|                 l[i] = data[w * lp + j]; | ||||
|  | ||||
|             sr_1d97_float(line, mh, mh + lh); | ||||
|  | ||||
|             for (i = 0; i < lh; i++) | ||||
|                 data[w * lp + i] = l[i]; | ||||
|         } | ||||
|  | ||||
|         // VER_SD | ||||
|         l = line + mv; | ||||
|         for (lp = 0; lp < lh; lp++) { | ||||
|             int i, j = 0; | ||||
|             // copy with interleaving | ||||
|             for (i = mv; i < lv; i += 2, j++) | ||||
|                 l[i] = data[w * j + lp]; | ||||
|             for (i = 1 - mv; i < lv; i += 2, j++) | ||||
|                 l[i] = data[w * j + lp]; | ||||
|  | ||||
|             sr_1d97_float(line, mv, mv + lv); | ||||
|  | ||||
|             for (i = 0; i < lv; i++) | ||||
|                 data[w * i + lp] = l[i]; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| static void sr_1d97_int(int32_t *p, int i0, int i1) | ||||
| { | ||||
|     int i; | ||||
|  | ||||
|     if (i1 == i0 + 1) | ||||
|         return; | ||||
|  | ||||
|     extend97_int(p, i0, i1); | ||||
|  | ||||
|     /*step 1*/ | ||||
|     for (i = i0 / 2 - 1; i < i1 / 2 + 2; i++) | ||||
|         p[2 * i]      = ((p[2 * i] * I_LFTG_K)     + (1 << 15)) >> 16; | ||||
|     /* step 2*/ | ||||
|     for (i = i0 / 2 - 2; i < i1 / 2 + 2; i++) | ||||
|         p[2 * i + 1]  = ((p[2 * i + 1] * I_LFTG_X) + (1 << 15)) >> 16; | ||||
|     /* step 3*/ | ||||
|     for (i = i0 / 2 - 1; i < i1 / 2 + 2; i++) | ||||
|         p[2 * i]     -= (I_LFTG_DELTA * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16; | ||||
|     /* step 4 */ | ||||
|     for (i = i0 / 2 - 1; i < i1 / 2 + 1; i++) | ||||
|         p[2 * i + 1] -= (I_LFTG_GAMMA * (p[2 * i]     + p[2 * i + 2]) + (1 << 15)) >> 16; | ||||
|     /*step 5*/ | ||||
|     for (i = i0 / 2; i < i1 / 2 + 1; i++) | ||||
|         p[2 * i]     += (I_LFTG_BETA  * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16; | ||||
|     /* step 6 */ | ||||
|     for (i = i0 / 2; i < i1 / 2; i++) | ||||
|         p[2 * i + 1] += (I_LFTG_ALPHA * (p[2 * i]     + p[2 * i + 2]) + (1 << 15)) >> 16; | ||||
| } | ||||
|  | ||||
| static void dwt_decode97_int(DWTContext *s, int32_t *t) | ||||
| { | ||||
|     int lev; | ||||
|     int w       = s->linelen[s->ndeclevels - 1][0]; | ||||
|     int32_t *line = s->i_linebuf; | ||||
|     int32_t *data = t; | ||||
|     /* position at index O of line range [0-5,w+5] cf. extend function */ | ||||
|     line += 5; | ||||
|  | ||||
|     for (lev = 0; lev < s->ndeclevels; lev++) { | ||||
|         int lh = s->linelen[lev][0], | ||||
|             lv = s->linelen[lev][1], | ||||
|             mh = s->mod[lev][0], | ||||
|             mv = s->mod[lev][1], | ||||
|             lp; | ||||
|         int32_t *l; | ||||
|         // HOR_SD | ||||
|         l = line + mh; | ||||
|         for (lp = 0; lp < lv; lp++) { | ||||
|             int i, j = 0; | ||||
|             // copy with interleaving | ||||
|             for (i = mh; i < lh; i += 2, j++) | ||||
|                 l[i] = data[w * lp + j]; | ||||
|             for (i = 1 - mh; i < lh; i += 2, j++) | ||||
|                 l[i] = data[w * lp + j]; | ||||
|  | ||||
|             sr_1d97_int(line, mh, mh + lh); | ||||
|  | ||||
|             for (i = 0; i < lh; i++) | ||||
|                 data[w * lp + i] = l[i]; | ||||
|         } | ||||
|  | ||||
|         // VER_SD | ||||
|         l = line + mv; | ||||
|         for (lp = 0; lp < lh; lp++) { | ||||
|             int i, j = 0; | ||||
|             // copy with interleaving | ||||
|             for (i = mv; i < lv; i += 2, j++) | ||||
|                 l[i] = data[w * j + lp]; | ||||
|             for (i = 1 - mv; i < lv; i += 2, j++) | ||||
|                 l[i] = data[w * j + lp]; | ||||
|  | ||||
|             sr_1d97_int(line, mv, mv + lv); | ||||
|  | ||||
|             for (i = 0; i < lv; i++) | ||||
|                 data[w * i + lp] = l[i]; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| int ff_jpeg2000_dwt_init(DWTContext *s, uint16_t border[2][2], | ||||
|                          int decomp_levels, int type) | ||||
| { | ||||
|     int i, j, lev = decomp_levels, maxlen, | ||||
|         b[2][2]; | ||||
|  | ||||
|     s->ndeclevels = decomp_levels; | ||||
|     s->type       = type; | ||||
|  | ||||
|     for (i = 0; i < 2; i++) | ||||
|         for (j = 0; j < 2; j++) | ||||
|             b[i][j] = border[i][j]; | ||||
|  | ||||
|     maxlen = FFMAX(b[0][1] - b[0][0], | ||||
|                    b[1][1] - b[1][0]); | ||||
|     while (--lev >= 0) | ||||
|         for (i = 0; i < 2; i++) { | ||||
|             s->linelen[lev][i] = b[i][1] - b[i][0]; | ||||
|             s->mod[lev][i]     = b[i][0] & 1; | ||||
|             for (j = 0; j < 2; j++) | ||||
|                 b[i][j] = (b[i][j] + 1) >> 1; | ||||
|         } | ||||
|     switch (type) { | ||||
|     case FF_DWT97: | ||||
|         s->f_linebuf = av_malloc((maxlen + 12) * sizeof(*s->f_linebuf)); | ||||
|         if (!s->f_linebuf) | ||||
|             return AVERROR(ENOMEM); | ||||
|         break; | ||||
|      case FF_DWT97_INT: | ||||
|         s->i_linebuf = av_malloc((maxlen + 12) * sizeof(*s->i_linebuf)); | ||||
|         if (!s->i_linebuf) | ||||
|             return AVERROR(ENOMEM); | ||||
|         break; | ||||
|     case FF_DWT53: | ||||
|         s->i_linebuf = av_malloc((maxlen +  6) * sizeof(*s->i_linebuf)); | ||||
|         if (!s->i_linebuf) | ||||
|             return AVERROR(ENOMEM); | ||||
|         break; | ||||
|     default: | ||||
|         return -1; | ||||
|     } | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| int ff_dwt_decode(DWTContext *s, void *t) | ||||
| { | ||||
|     switch (s->type) { | ||||
|     case FF_DWT97: | ||||
|         dwt_decode97_float(s, t); | ||||
|         break; | ||||
|     case FF_DWT97_INT: | ||||
|         dwt_decode97_int(s, t); | ||||
|         break; | ||||
|     case FF_DWT53: | ||||
|         dwt_decode53(s, t); | ||||
|         break; | ||||
|     default: | ||||
|         return -1; | ||||
|     } | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| void ff_dwt_destroy(DWTContext *s) | ||||
| { | ||||
|     av_freep(&s->f_linebuf); | ||||
|     av_freep(&s->i_linebuf); | ||||
| } | ||||
							
								
								
									
										64
									
								
								libavcodec/jpeg2000dwt.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								libavcodec/jpeg2000dwt.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,64 @@ | ||||
| /* | ||||
|  * Discrete wavelet transform | ||||
|  * Copyright (c) 2007 Kamil Nowosad | ||||
|  * | ||||
|  * This file is part of Libav. | ||||
|  * | ||||
|  * Libav is free software; you can redistribute it and/or | ||||
|  * modify it under the terms of the GNU Lesser General Public | ||||
|  * License as published by the Free Software Foundation; either | ||||
|  * version 2.1 of the License, or (at your option) any later version. | ||||
|  * | ||||
|  * Libav is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | ||||
|  * Lesser General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU Lesser General Public | ||||
|  * License along with Libav; if not, write to the Free Software | ||||
|  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||
|  */ | ||||
|  | ||||
| #ifndef AVCODEC_JPEG2000DWT_H | ||||
| #define AVCODEC_JPEG2000DWT_H | ||||
|  | ||||
| /** | ||||
|  * @file | ||||
|  * Discrete wavelet transform | ||||
|  */ | ||||
|  | ||||
| #include <stdint.h> | ||||
|  | ||||
| #define FF_DWT_MAX_DECLVLS 32 ///< max number of decomposition levels | ||||
|  | ||||
| enum DWTType { | ||||
|     FF_DWT97, | ||||
|     FF_DWT53, | ||||
|     FF_DWT97_INT | ||||
| }; | ||||
|  | ||||
| typedef struct DWTContext { | ||||
|     /// line lengths { horizontal, vertical } in consecutive decomposition levels | ||||
|     uint16_t linelen[FF_DWT_MAX_DECLVLS][2]; | ||||
|     uint8_t mod[FF_DWT_MAX_DECLVLS][2];  ///< coordinates (x0, y0) of decomp. levels mod 2 | ||||
|     uint8_t ndeclevels;                  ///< number of decomposition levels | ||||
|     uint8_t type;                        ///< 0 for 9/7; 1 for 5/3 | ||||
|     int32_t *i_linebuf;                  ///< int buffer used by transform | ||||
|     float   *f_linebuf;                  ///< float buffer used by transform | ||||
| } DWTContext; | ||||
|  | ||||
| /** | ||||
|  * Initialize DWT. | ||||
|  * @param s                 DWT context | ||||
|  * @param border            coordinates of transformed region {{x0, x1}, {y0, y1}} | ||||
|  * @param decomp_levels     number of decomposition levels | ||||
|  * @param type              0 for DWT 9/7; 1 for DWT 5/3 | ||||
|  */ | ||||
| int ff_jpeg2000_dwt_init(DWTContext *s, uint16_t border[2][2], | ||||
|                          int decomp_levels, int type); | ||||
|  | ||||
| int ff_dwt_decode(DWTContext *s, void *t); | ||||
|  | ||||
| void ff_dwt_destroy(DWTContext *s); | ||||
|  | ||||
| #endif /* AVCODEC_JPEG2000DWT_H */ | ||||
							
								
								
									
										112
									
								
								libavcodec/mqc.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								libavcodec/mqc.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,112 @@ | ||||
| /* | ||||
|  * MQ-coder encoder and decoder common functions | ||||
|  * Copyright (c) 2007 Kamil Nowosad | ||||
|  * | ||||
|  * This file is part of Libav. | ||||
|  * | ||||
|  * Libav is free software; you can redistribute it and/or | ||||
|  * modify it under the terms of the GNU Lesser General Public | ||||
|  * License as published by the Free Software Foundation; either | ||||
|  * version 2.1 of the License, or (at your option) any later version. | ||||
|  * | ||||
|  * Libav is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | ||||
|  * Lesser General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU Lesser General Public | ||||
|  * License along with Libav; if not, write to the Free Software | ||||
|  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||
|  */ | ||||
|  | ||||
| /** | ||||
|  * MQ-coder common (decoder/encoder) functions | ||||
|  * @file | ||||
|  * @author Kamil Nowosad | ||||
|  */ | ||||
|  | ||||
| #include <string.h> | ||||
| #include <stdint.h> | ||||
|  | ||||
| #include "mqc.h" | ||||
|  | ||||
| /* MQ coder context state structure */ | ||||
| typedef struct MqcCxState { | ||||
|     uint16_t qe; | ||||
|     uint8_t  nmps; | ||||
|     uint8_t  nlps; | ||||
|     uint8_t  sw; | ||||
| } MqcCxState; | ||||
|  | ||||
| static const MqcCxState cx_states[47] = { | ||||
|     { 0x5601,  1,  1, 1 }, | ||||
|     { 0x3401,  2,  6, 0 }, | ||||
|     { 0x1801,  3,  9, 0 }, | ||||
|     { 0x0AC1,  4, 12, 0 }, | ||||
|     { 0x0521,  5, 29, 0 }, | ||||
|     { 0x0221, 38, 33, 0 }, | ||||
|     { 0x5601,  7,  6, 1 }, | ||||
|     { 0x5401,  8, 14, 0 }, | ||||
|     { 0x4801,  9, 14, 0 }, | ||||
|     { 0x3801, 10, 14, 0 }, | ||||
|     { 0x3001, 11, 17, 0 }, | ||||
|     { 0x2401, 12, 18, 0 }, | ||||
|     { 0x1C01, 13, 20, 0 }, | ||||
|     { 0x1601, 29, 21, 0 }, | ||||
|     { 0x5601, 15, 14, 1 }, | ||||
|     { 0x5401, 16, 14, 0 }, | ||||
|     { 0x5101, 17, 15, 0 }, | ||||
|     { 0x4801, 18, 16, 0 }, | ||||
|     { 0x3801, 19, 17, 0 }, | ||||
|     { 0x3401, 20, 18, 0 }, | ||||
|     { 0x3001, 21, 19, 0 }, | ||||
|     { 0x2801, 22, 19, 0 }, | ||||
|     { 0x2401, 23, 20, 0 }, | ||||
|     { 0x2201, 24, 21, 0 }, | ||||
|     { 0x1C01, 25, 22, 0 }, | ||||
|     { 0x1801, 26, 23, 0 }, | ||||
|     { 0x1601, 27, 24, 0 }, | ||||
|     { 0x1401, 28, 25, 0 }, | ||||
|     { 0x1201, 29, 26, 0 }, | ||||
|     { 0x1101, 30, 27, 0 }, | ||||
|     { 0x0AC1, 31, 28, 0 }, | ||||
|     { 0x09C1, 32, 29, 0 }, | ||||
|     { 0x08A1, 33, 30, 0 }, | ||||
|     { 0x0521, 34, 31, 0 }, | ||||
|     { 0x0441, 35, 32, 0 }, | ||||
|     { 0x02A1, 36, 33, 0 }, | ||||
|     { 0x0221, 37, 34, 0 }, | ||||
|     { 0x0141, 38, 35, 0 }, | ||||
|     { 0x0111, 39, 36, 0 }, | ||||
|     { 0x0085, 40, 37, 0 }, | ||||
|     { 0x0049, 41, 38, 0 }, | ||||
|     { 0x0025, 42, 39, 0 }, | ||||
|     { 0x0015, 43, 40, 0 }, | ||||
|     { 0x0009, 44, 41, 0 }, | ||||
|     { 0x0005, 45, 42, 0 }, | ||||
|     { 0x0001, 45, 43, 0 }, | ||||
|     { 0x5601, 46, 46, 0 } | ||||
| }; | ||||
|  | ||||
| uint16_t ff_mqc_qe [2 * 47]; | ||||
| uint8_t ff_mqc_nlps[2 * 47]; | ||||
| uint8_t ff_mqc_nmps[2 * 47]; | ||||
|  | ||||
| void ff_mqc_init_contexts(MqcState *mqc) | ||||
| { | ||||
|     int i; | ||||
|     memset(mqc->cx_states, 0, sizeof(mqc->cx_states)); | ||||
|     mqc->cx_states[MQC_CX_UNI] = 2 * 46; | ||||
|     mqc->cx_states[MQC_CX_RL]  = 2 * 3; | ||||
|     mqc->cx_states[0]          = 2 * 4; | ||||
|  | ||||
|     for (i = 0; i < 47; i++) { | ||||
|         ff_mqc_qe[2 * i]     = | ||||
|         ff_mqc_qe[2 * i + 1] = cx_states[i].qe; | ||||
|  | ||||
|         ff_mqc_nlps[2 * i]     = 2 * cx_states[i].nlps + cx_states[i].sw; | ||||
|         ff_mqc_nlps[2 * i + 1] = 2 * cx_states[i].nlps + 1 - cx_states[i].sw; | ||||
|         ff_mqc_nmps[2 * i]     = 2 * cx_states[i].nmps; | ||||
|         ff_mqc_nmps[2 * i + 1] = 2 * cx_states[i].nmps + 1; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										73
									
								
								libavcodec/mqc.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								libavcodec/mqc.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,73 @@ | ||||
| /* | ||||
|  * MQ-coder: structures, common and decoder functions | ||||
|  * Copyright (c) 2007 Kamil Nowosad | ||||
|  * | ||||
|  * This file is part of Libav. | ||||
|  * | ||||
|  * Libav is free software; you can redistribute it and/or | ||||
|  * modify it under the terms of the GNU Lesser General Public | ||||
|  * License as published by the Free Software Foundation; either | ||||
|  * version 2.1 of the License, or (at your option) any later version. | ||||
|  * | ||||
|  * Libav is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | ||||
|  * Lesser General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU Lesser General Public | ||||
|  * License along with Libav; if not, write to the Free Software | ||||
|  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||
|  */ | ||||
|  | ||||
| #ifndef AVCODEC_MQC_H | ||||
| #define AVCODEC_MQC_H | ||||
|  | ||||
| /** | ||||
|  * MQ-coder | ||||
|  * @file | ||||
|  * @author Kamil Nowosad | ||||
|  */ | ||||
|  | ||||
| #include <stdint.h> | ||||
|  | ||||
| #define MQC_CX_UNI 17 | ||||
| #define MQC_CX_RL  18 | ||||
|  | ||||
| extern uint16_t ff_mqc_qe[2 * 47]; | ||||
| extern uint8_t  ff_mqc_nlps[2 * 47]; | ||||
| extern uint8_t  ff_mqc_nmps[2 * 47]; | ||||
|  | ||||
| typedef struct MqcState { | ||||
|     uint8_t *bp, *bpstart; | ||||
|     unsigned int a; | ||||
|     unsigned int c; | ||||
|     unsigned int ct; | ||||
|     uint8_t cx_states[19]; | ||||
| } MqcState; | ||||
|  | ||||
| /* decoder */ | ||||
|  | ||||
| /** | ||||
|  * Initialize MQ-decoder. | ||||
|  * @param mqc   MQ decoder state | ||||
|  * @param bp    byte poiter | ||||
|  */ | ||||
| void ff_mqc_initdec(MqcState *mqc, uint8_t *bp); | ||||
|  | ||||
| /** | ||||
|  * MQ decoder. | ||||
|  * @param mqc       MQ decoder state | ||||
|  * @param cxstate   Context | ||||
|  * @return          Decision (0 ot 1) | ||||
|  */ | ||||
| int ff_mqc_decode(MqcState *mqc, uint8_t *cxstate); | ||||
|  | ||||
| /* common */ | ||||
|  | ||||
| /** | ||||
|  * MQ-coder context initialisations. | ||||
|  * @param mqc       MQ-coder context | ||||
|  */ | ||||
| void ff_mqc_init_contexts(MqcState *mqc); | ||||
|  | ||||
| #endif /* AVCODEC_MQC_H */ | ||||
							
								
								
									
										93
									
								
								libavcodec/mqcdec.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								libavcodec/mqcdec.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,93 @@ | ||||
| /* | ||||
|  * MQ-coder decoder | ||||
|  * Copyright (c) 2007 Kamil Nowosad | ||||
|  * | ||||
|  * This file is part of Libav. | ||||
|  * | ||||
|  * Libav is free software; you can redistribute it and/or | ||||
|  * modify it under the terms of the GNU Lesser General Public | ||||
|  * License as published by the Free Software Foundation; either | ||||
|  * version 2.1 of the License, or (at your option) any later version. | ||||
|  * | ||||
|  * Libav is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | ||||
|  * Lesser General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU Lesser General Public | ||||
|  * License along with Libav; if not, write to the Free Software | ||||
|  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||
|  */ | ||||
|  | ||||
| /** | ||||
|  * MQ-coder decoder | ||||
|  * @file | ||||
|  * @author Kamil Nowosad | ||||
|  */ | ||||
|  | ||||
| #include "mqc.h" | ||||
|  | ||||
| static void bytein(MqcState *mqc) | ||||
| { | ||||
|     if (*mqc->bp == 0xff) { | ||||
|         if (*(mqc->bp + 1) > 0x8f) | ||||
|             mqc->c++; | ||||
|         else { | ||||
|             mqc->bp++; | ||||
|             mqc->c += 2 + 0xfe00 - (*mqc->bp << 9); | ||||
|         } | ||||
|     } else { | ||||
|         mqc->bp++; | ||||
|         mqc->c += 1 + 0xff00 - (*mqc->bp << 8); | ||||
|     } | ||||
| } | ||||
|  | ||||
| static int exchange(MqcState *mqc, uint8_t *cxstate, int lps) | ||||
| { | ||||
|     int d; | ||||
|     if ((mqc->a < ff_mqc_qe[*cxstate]) ^ (!lps)) { | ||||
|         if (lps) | ||||
|             mqc->a = ff_mqc_qe[*cxstate]; | ||||
|         d = *cxstate & 1; | ||||
|         *cxstate = ff_mqc_nmps[*cxstate]; | ||||
|     } else { | ||||
|         if (lps) | ||||
|             mqc->a = ff_mqc_qe[*cxstate]; | ||||
|         d = 1 - (*cxstate & 1); | ||||
|         *cxstate = ff_mqc_nlps[*cxstate]; | ||||
|     } | ||||
|     // do RENORMD: see ISO/IEC 15444-1:2002 §C.3.3 | ||||
|     do { | ||||
|         if (!(mqc->c & 0xff)) { | ||||
|             mqc->c -= 0x100; | ||||
|             bytein(mqc); | ||||
|         } | ||||
|         mqc->a += mqc->a; | ||||
|         mqc->c += mqc->c; | ||||
|     } while (!(mqc->a & 0x8000)); | ||||
|     return d; | ||||
| } | ||||
|  | ||||
| void ff_mqc_initdec(MqcState *mqc, uint8_t *bp) | ||||
| { | ||||
|     ff_mqc_init_contexts(mqc); | ||||
|     mqc->bp = bp; | ||||
|     mqc->c  = (*mqc->bp ^ 0xff) << 16; | ||||
|     bytein(mqc); | ||||
|     mqc->c = mqc->c << 7; | ||||
|     mqc->a = 0x8000; | ||||
| } | ||||
|  | ||||
| int ff_mqc_decode(MqcState *mqc, uint8_t *cxstate) | ||||
| { | ||||
|     mqc->a -= ff_mqc_qe[*cxstate]; | ||||
|     if ((mqc->c >> 16) < mqc->a) { | ||||
|         if (mqc->a & 0x8000) | ||||
|             return *cxstate & 1; | ||||
|         else | ||||
|             return exchange(mqc, cxstate, 0); | ||||
|     } else { | ||||
|         mqc->c -= mqc->a << 16; | ||||
|         return exchange(mqc, cxstate, 1); | ||||
|     } | ||||
| } | ||||
| @@ -151,6 +151,9 @@ fate-interplay-mve-8bit: CMD = framecrc -i $(SAMPLES)/interplay-mve/interplay-lo | ||||
| FATE_SAMPLES_AVCONV-$(call DEMDEC, IPMOVIE, INTERPLAY_VIDEO) += fate-interplay-mve-16bit | ||||
| fate-interplay-mve-16bit: CMD = framecrc -i $(SAMPLES)/interplay-mve/descent3-level5-16bit-partial.mve -pix_fmt rgb24 -an | ||||
|  | ||||
| FATE_SAMPLES_AVCONV-$(call DEMDEC, MXF, JPEG2000) += fate-jpeg2000-dcinema | ||||
| fate-jpeg2000-dcinema: CMD = framecrc -flags +bitexact -i $(SAMPLES)/jpeg2000/chiens_dcinema2K.mxf | ||||
|  | ||||
| FATE_SAMPLES_AVCONV-$(call DEMDEC, AVI, KGV1) += fate-kgv1 | ||||
| fate-kgv1: CMD = framecrc -i $(SAMPLES)/kega/kgv1.avi -pix_fmt rgb555le -an | ||||
|  | ||||
|   | ||||
							
								
								
									
										3
									
								
								tests/ref/fate/jpeg2000-dcinema
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								tests/ref/fate/jpeg2000-dcinema
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| #tb 0: 1/24 | ||||
| 0,          0,          0,        1, 12441600, 0xf0de508b | ||||
| 0,          1,          1,        1, 12441600, 0x8e50c249 | ||||
		Reference in New Issue
	
	Block a user