📄 rtjpegn.c
字号:
/* RTjpeg (C) Justin Schoeman 1998 (justin@suntiger.ee.up.ac.za) With modifications by: (c) 1998, 1999 by Joerg Walter <trouble@moes.pmnet.uni-oldenburg.de> and (c) 1999 by Wim Taymans <wim.taymans@tvd.be> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include "config.h"#ifdef HAVE_MMX#define MMX#endif#include "libavutil/common.h"#include "mpbswap.h"#include "RTjpegN.h"#ifdef MMX#include "mmx.h"#endif//#define SHOWBLOCK 1#define BETTERCOMPRESSION 1static const unsigned char RTjpeg_ZZ[64]={0,8, 1,2, 9, 16,24, 17, 10, 3,4, 11, 18, 25, 32,40, 33, 26, 19, 12, 5,6, 13, 20, 27, 34, 41, 48,56, 49, 42, 35, 28, 21, 14, 7,15, 22, 29, 36, 43, 50, 57,58, 51, 44, 37, 30, 23,31, 38, 45, 52, 59,60, 53, 46, 39,47, 54, 61,62, 55,63 };static const __u64 RTjpeg_aan_tab[64]={4294967296ULL, 5957222912ULL, 5611718144ULL, 5050464768ULL, 4294967296ULL, 3374581504ULL, 2324432128ULL, 1184891264ULL, 5957222912ULL, 8263040512ULL, 7783580160ULL, 7005009920ULL, 5957222912ULL, 4680582144ULL, 3224107520ULL, 1643641088ULL, 5611718144ULL, 7783580160ULL, 7331904512ULL, 6598688768ULL, 5611718144ULL, 4408998912ULL, 3036936960ULL, 1548224000ULL, 5050464768ULL, 7005009920ULL, 6598688768ULL, 5938608128ULL, 5050464768ULL, 3968072960ULL, 2733115392ULL, 1393296000ULL, 4294967296ULL, 5957222912ULL, 5611718144ULL, 5050464768ULL, 4294967296ULL, 3374581504ULL, 2324432128ULL, 1184891264ULL, 3374581504ULL, 4680582144ULL, 4408998912ULL, 3968072960ULL, 3374581504ULL, 2651326208ULL, 1826357504ULL, 931136000ULL, 2324432128ULL, 3224107520ULL, 3036936960ULL, 2733115392ULL, 2324432128ULL, 1826357504ULL, 1258030336ULL, 641204288ULL, 1184891264ULL, 1643641088ULL, 1548224000ULL, 1393296000ULL, 1184891264ULL, 931136000ULL, 641204288ULL, 326894240ULL, };#ifndef MMXstatic __s32 RTjpeg_ws[64+31];#endif__u8 RTjpeg_alldata[2*64+4*64+4*64+4*64+4*64+32];static __s16 *block; // rhstatic __s16 *RTjpeg_block;static __s32 *RTjpeg_lqt;static __s32 *RTjpeg_cqt;static __u32 *RTjpeg_liqt;static __u32 *RTjpeg_ciqt;static unsigned char RTjpeg_lb8;static unsigned char RTjpeg_cb8;static int RTjpeg_width, RTjpeg_height;static int RTjpeg_Ywidth, RTjpeg_Cwidth;static int RTjpeg_Ysize, RTjpeg_Csize;static __s16 *RTjpeg_old=NULL;#ifdef MMXmmx_t RTjpeg_lmask;mmx_t RTjpeg_cmask;#else__u16 RTjpeg_lmask;__u16 RTjpeg_cmask;#endifint RTjpeg_mtest=0;static const unsigned char RTjpeg_lum_quant_tbl[64] = { 16, 11, 10, 16, 24, 40, 51, 61, 12, 12, 14, 19, 26, 58, 60, 55, 14, 13, 16, 24, 40, 57, 69, 56, 14, 17, 22, 29, 51, 87, 80, 62, 18, 22, 37, 56, 68, 109, 103, 77, 24, 35, 55, 64, 81, 104, 113, 92, 49, 64, 78, 87, 103, 121, 120, 101, 72, 92, 95, 98, 112, 100, 103, 99 };static const unsigned char RTjpeg_chrom_quant_tbl[64] = { 17, 18, 24, 47, 99, 99, 99, 99, 18, 21, 26, 66, 99, 99, 99, 99, 24, 26, 56, 99, 99, 99, 99, 99, 47, 66, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99 };#ifdef BETTERCOMPRESSION /*--------------------------------------------------*//* better encoding, but needs a lot more cpu time *//* seems to be more effective than old method +lzo *//* with this encoding lzo isn't efficient anymore *//* there is still more potential for better *//* encoding but that would need even more cputime *//* anyway your mileage may vary *//* *//* written by Martin BIELY and Roman HOCHLEITNER *//*--------------------------------------------------*//* +++++++++++++++++++++++++++++++++++++++++++++++++++*//* Block to Stream (encoding) *//* */int RTjpeg_b2s(__s16 *data, __s8 *strm, __u8 bt8){ register int ci, co=1; register __s16 ZZvalue; register unsigned char bitten; register unsigned char bitoff;#ifdef SHOWBLOCK int ii; for (ii=0; ii < 64; ii++) { fprintf(stdout, "%d ", data[RTjpeg_ZZ[ii]]); } fprintf(stdout, "\n\n");#endif// *strm++ = 0x10;// *strm = 0x00;//// return 2; // first byte allways written ((__u8*)strm)[0]= (__u8)(data[RTjpeg_ZZ[0]]>254) ? 254:((data[RTjpeg_ZZ[0]]<0)?0:data[RTjpeg_ZZ[0]]); ci=63; while (data[RTjpeg_ZZ[ci]]==0 && ci>0) ci--; bitten = ((unsigned char)ci) << 2; if (ci==0) { ((__u8*)strm)[1]= bitten; co = 2; return (int)co; } /* bitoff=0 because the high 6bit contain first non zero position */ bitoff = 0; co = 1; for(; ci>0; ci--) { ZZvalue = data[RTjpeg_ZZ[ci]]; switch(ZZvalue) { case 0: break; case 1: bitten |= (0x01<<bitoff); break; case -1: bitten |= (0x03<<bitoff); break; default: bitten |= (0x02<<bitoff); goto HERZWEH; break; } if( bitoff == 0 ) { ((__u8*)strm)[co]= bitten; bitten = 0; bitoff = 8; co++; } /* "fall through" */ bitoff-=2; } /* ci must be 0 */ if(bitoff != 6) { ((__u8*)strm)[co]= bitten; co++; } goto BAUCHWEH;HERZWEH:/* ci cannot be 0 *//* correct bitoff to nibble boundaries */ switch(bitoff){ case 4: case 6: bitoff = 0; break; case 2: case 0: ((__u8*)strm)[co]= bitten; bitoff = 4; co++; bitten = 0; // clear half nibble values in bitten break; default: break; } for(; ci>0; ci--) { ZZvalue = data[RTjpeg_ZZ[ci]]; if( (ZZvalue > 7) || (ZZvalue < -7) ) { bitten |= (0x08<<bitoff); goto HIRNWEH; } bitten |= (ZZvalue&0xf)<<bitoff; if( bitoff == 0 ) { ((__u8*)strm)[co]= bitten; bitten = 0; bitoff = 8; co++; } /* "fall thru" */ bitoff-=4; } /* ci must be 0 */ if( bitoff == 0 ) { ((__u8*)strm)[co]= bitten; co++; } goto BAUCHWEH;HIRNWEH: ((__u8*)strm)[co]= bitten; co++; /* bitting is over now we bite */ for(; ci>0; ci--) { ZZvalue = data[RTjpeg_ZZ[ci]]; if(ZZvalue>0) { strm[co++]=(__s8)(ZZvalue>127)?127:ZZvalue; } else { strm[co++]=(__s8)(ZZvalue<-128)?-128:ZZvalue; } } BAUCHWEH: /* we gotoo much now we are ill */#ifdef SHOWBLOCK{int i;fprintf(stdout, "\nco = '%d'\n", co); for (i=0; i < co+2; i++) { fprintf(stdout, "%d ", strm[i]); }fprintf(stdout, "\n\n");}#endif return (int)co;}/* +++++++++++++++++++++++++++++++++++++++++++++++++++*//* Stream to Block (decoding) *//* */int RTjpeg_s2b(__s16 *data, __s8 *strm, __u8 bt8, __u32 *qtbl){ int ci; register int co; register int i; register unsigned char bitten; register unsigned char bitoff; /* first byte always read */ i=RTjpeg_ZZ[0]; data[i]=((__u8)strm[0])*qtbl[i]; /* we start at the behind */ bitten = ((unsigned char)strm[1]) >> 2; co = 63; for(; co > bitten; co--) { data[RTjpeg_ZZ[co]] = 0; } if (co==0) { ci = 2; goto AUTOBAHN; } /* we have to read the last 2 bits of the second byte */ ci=1; bitoff = 0; for(; co>0; co--) { bitten = ((unsigned char)strm[ci]) >> bitoff; bitten &= 0x03; i=RTjpeg_ZZ[co]; switch( bitten ) { case 0x03: data[i]= -qtbl[i]; break; case 0x02: goto FUSSWEG; break; case 0x01: data[i]= qtbl[i]; break; case 0x00: data[i]= 0; break; default: break; } if( bitoff == 0 ) { bitoff = 8; ci++; } bitoff -= 2; } /* co is 0 now */ /* data is written properly */ /* if bitoff!=6 then ci is the index, but should be the byte count, so we increment by 1 */ if (bitoff!=6) ci++; goto AUTOBAHN; FUSSWEG:/* correct bitoff to nibble */ switch(bitoff){ case 4: case 6: bitoff = 0; break; case 2: case 0: /* we have to read from the next byte */ ci++; bitoff = 4; break; default: break; } for(; co>0; co--) { bitten = ((unsigned char)strm[ci]) >> bitoff; bitten &= 0x0f; i=RTjpeg_ZZ[co]; if( bitten == 0x08 ) { goto STRASSE; } /* the compiler cannot do sign extension for signed nibbles */ if( bitten & 0x08 ) { bitten |= 0xf0; } /* the unsigned char bitten now is a valid signed char */ data[i]=((signed char)bitten)*qtbl[i]; if( bitoff == 0 ) { bitoff = 8; ci++; } bitoff -= 4; } /* co is 0 */ /* if bitoff!=4 then ci is the index, but should be the byte count, so we increment by 1 */ if (bitoff!=4) ci++; goto AUTOBAHN;STRASSE: ci++; for(; co>0; co--) { i=RTjpeg_ZZ[co]; data[i]=strm[ci++]*qtbl[i]; } /* ci now is the count, because it points to next element => no incrementing */AUTOBAHN:#ifdef SHOWBLOCKfprintf(stdout, "\nci = '%d'\n", ci); for (i=0; i < 64; i++) { fprintf(stdout, "%d ", data[RTjpeg_ZZ[i]]); }fprintf(stdout, "\n\n");#endif return ci;}#elseint RTjpeg_b2s(__s16 *data, __s8 *strm, __u8 bt8){ register int ci, co=1, tmp; register __s16 ZZvalue;#ifdef SHOWBLOCK int ii; for (ii=0; ii < 64; ii++) { fprintf(stdout, "%d ", data[RTjpeg_ZZ[ii]]); } fprintf(stdout, "\n\n");#endif (__u8)strm[0]=(__u8)(data[RTjpeg_ZZ[0]]>254) ? 254:((data[RTjpeg_ZZ[0]]<0)?0:data[RTjpeg_ZZ[0]]); for(ci=1; ci<=bt8; ci++) { ZZvalue = data[RTjpeg_ZZ[ci]]; if(ZZvalue>0) { strm[co++]=(__s8)(ZZvalue>127)?127:ZZvalue; } else { strm[co++]=(__s8)(ZZvalue<-128)?-128:ZZvalue; } } for(; ci<64; ci++) { ZZvalue = data[RTjpeg_ZZ[ci]]; if(ZZvalue>0) { strm[co++]=(__s8)(ZZvalue>63)?63:ZZvalue; } else if(ZZvalue<0) { strm[co++]=(__s8)(ZZvalue<-64)?-64:ZZvalue; } else /* compress zeros */ { tmp=ci; do { ci++; } while((ci<64)&&(data[RTjpeg_ZZ[ci]]==0)); strm[co++]=(__s8)(63+(ci-tmp)); ci--; } } return (int)co;}int RTjpeg_s2b(__s16 *data, __s8 *strm, __u8 bt8, __u32 *qtbl)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -