29 #include <linux/types.h> 39 #define IFIX(a) ((int)((a) * (1 << ISHIFT) + .5)) 60 int (*func)
__P((
void *));
92 unsigned char vals[256];
97 __P((
struct in *,
int *,
int,
struct scan *,
int *));
110 inline static void idct(
int *
in,
int *
out,
int *
quant,
long off,
int max);
120 typedef void (*
ftopict) (
int *
out,
unsigned char *pic,
int width) ;
173 #define dec_huffdc (dhuff + 0) 174 #define dec_huffac (dhuff + 2) 180 int m, l, i, j, lq, pq, tq;
205 for (i = 0; i < 64; i++)
216 unsigned char huffvals[256];
222 if (tc > 1 || th > 1)
224 for (i = 0; i < 16; i++)
228 for (i = 0; i < 16; i++) {
229 for (j = 0; j < hufflen[i]; j++)
239 printf(
"find DRI \n");
264 for (i = 0; i <
info.
ns; i++)
276 for (i = 0; i <
info.
ns; i++)
282 int jpeg_decode(
unsigned char **pic,
unsigned char *buf,
int *width,
286 int i, j, m, tac, tdc;
287 int intwidth, intheight;
288 int mcusx, mcusy, mx, my;
289 int ypitch ,xpitch,bpp,pitch,x,y;
294 int isInitHuffman = 0;
327 if ((intheight & 7) || (intwidth & 7)) {
336 for (i = 0; i <
info.
nc; i++) {
343 if (h > 3 || v > 3) {
347 if (
comps[i].tq > 3) {
359 printf(
"info ns %d/n",
info.
ns);
363 for (i = 0; i <
info.
ns; i++) {
368 if (tdc > 1 || tac > 1) {
372 for (j = 0; j <
info.
nc; j++)
389 if (i != 0 || j != 63 || m != 0) {
390 printf(
"hmm FW error,not seq DCT ??\n");
411 if (intwidth != *width || intheight != *height || *pic == NULL) {
416 (
unsigned char *) realloc((
unsigned char *) *pic,
417 (size_t) intwidth * (intheight +
426 mcusy = *height >> 4;
429 pitch = *width * bpp;
437 mcusy = *height >> 3;
440 pitch = *width * bpp;
446 mcusy = *height >> 3;
449 pitch = *width * bpp;
474 for (my = 0,y=0; my < mcusy; my++,y+=ypitch) {
475 for (mx = 0,x=0; mx < mcusx; mx++,x+=xpitch) {
485 IFIX(128.5), max[0]);
502 IFIX(128.5), max[0]);
515 IFIX(128.5), max[0]);
526 IFIX(128.5), max[0]);
531 convert(decdata->
out,*pic+y+x,pitch);
559 unsigned char huffvals[256];
565 if (tc > 1 || th > 1)
567 for (i = 0; i < 16; i++)
571 for (i = 0; i < 16; i++) {
572 for (j = 0; j < hufflen[i]; j++)
573 huffvals[k++] = *ptr++;
604 in->
bits = bi << 16, le += 16;
609 if (b == 0xff && (m = *in->p++) != 0) {
611 if (in->func && (m = in->func(in->data)) == 0)
616 bi = bi << 16, le += 16;
632 if ((m = in->marker) == 0)
639 #define LEBI_DCL int le, bi 640 #define LEBI_GET(in) (le = in->left, bi = in->bits) 641 #define LEBI_PUT(in) (in->left = le, in->bits = bi) 643 #define GETBITS(in, n) ( \ 644 (le < (n) ? le = fillbits(in, le, bi), bi = in->bits : 0), \ 646 bi >> le & ((1 << (n)) - 1) \ 649 #define UNGETBITS(in, n) ( \ 669 (c = ((c << 1) | GETBITS(in, 1))) >= (hu->
maxcode[i]); i++);
684 if (c < (1 << (i - 1)))
690 #define DEC_REC(in, hu, r, i) ( \ 691 r = GETBITS(in, DECBITS), \ 695 UNGETBITS(in, i & 127), \ 702 i = dec_rec2(in, hu, &r, r, i), \ 719 memset(dct, 0, n * 64 *
sizeof(*dct));
723 *dct++ = (sc->
dc +=
DEC_REC(in, hu, r, t));
729 if (t == 0 && r == 0) {
747 unsigned char *huffvals;
749 int code, k, i, j, d, x, c, v;
750 for (i = 0; i < (1 <<
DECBITS); i++)
765 for (i = 0; i < 16; i++, code <<= 1) {
767 for (j = 0; j < hufflen[i]; j++) {
768 hu->vals[k] = *huffvals++;
771 v = hu->vals[k] & 0x0f;
772 for (d = 1 << (
DECBITS - 1 - i); --d >= 0;) {
774 x = d >> (
DECBITS - 1 - v - i);
775 if (v && x < (1 << (v - 1)))
777 x = x << 16 | (hu->vals[k] & 0xf0) << 4 |
780 x = v << 16 | (hu->vals[k] & 0xf0) << 4 |
782 hu->llvals[c | d] = x;
788 hu->maxcode[i] = code;
790 hu->maxcode[16] = 0x20000;
798 #define IMULT(a, b) (((a) * (b)) >> ISHIFT) 799 #define ITOINT(a) ((a) >> ISHIFT) 801 #define S22 ((PREC)IFIX(2 * 0.382683432)) 802 #define C22 ((PREC)IFIX(2 * 0.923879532)) 803 #define IC4 ((PREC)IFIX(1 / 0.707106781)) 805 static unsigned char zig2[64] = {
806 0, 2, 3, 9, 10, 20, 21, 35,
807 14, 16, 25, 31, 39, 46, 50, 57,
808 5, 7, 12, 18, 23, 33, 37, 48,
809 27, 29, 41, 44, 52, 55, 59, 62,
810 15, 26, 30, 40, 45, 51, 56, 58,
811 1, 4, 8, 11, 19, 22, 34, 36,
812 28, 42, 43, 53, 54, 60, 61, 63,
813 6, 13, 17, 24, 32, 38, 47, 49
816 inline static void idct(
int *
in,
int *out,
int *quant,
long off,
int max)
818 long t0, t1, t2, t3, t4, t5, t6, t7;
819 long tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
822 unsigned char *zig2p;
826 t0 += in[0] * quant[0];
827 for (i = 0; i < 64; i++)
833 for (i = 0; i < 8; i++) {
835 t0 += in[j] * (long) quant[j];
837 t5 = in[j] * (long) quant[j];
839 t2 = in[j] * (long) quant[j];
841 t7 = in[j] * (long) quant[j];
843 t1 = in[j] * (long) quant[j];
845 t4 = in[j] * (long) quant[j];
847 t3 = in[j] * (long) quant[j];
849 t6 = in[j] * (long) quant[j];
852 if ((t1 | t2 | t3 | t4 | t5 | t6 | t7) == 0) {
891 tmpp[0 * 8] = tmp3 + t7;
892 tmpp[1 * 8] = tmp1 + t6;
893 tmpp[2 * 8] = tmp2 + t5;
894 tmpp[3 * 8] = t3 + t4;
895 tmpp[4 * 8] = t3 - t4;
896 tmpp[5 * 8] = tmp2 - t5;
897 tmpp[6 * 8] = tmp1 - t6;
898 tmpp[7 * 8] = tmp3 - t7;
902 for (i = 0, j = 0; i < 8; i++) {
911 if ((t1 | t2 | t3 | t4 | t5 | t6 | t7) == 0) {
948 out[j + 0] =
ITOINT(tmp3 + t7);
949 out[j + 1] =
ITOINT(tmp1 + t6);
950 out[j + 2] =
ITOINT(tmp2 + t5);
951 out[j + 3] =
ITOINT(t3 + t4);
952 out[j + 4] =
ITOINT(t3 - t4);
953 out[j + 5] =
ITOINT(tmp2 - t5);
954 out[j + 6] =
ITOINT(tmp1 - t6);
955 out[j + 7] =
ITOINT(tmp3 - t7);
962 #define FOUR_TWO_TWO 2 //Y00 Cb Y01 Cr 968 Pyuv422togray8(
unsigned char * input_ptr,
unsigned char * output_ptr,
unsigned int image_width,
unsigned int image_height)
970 unsigned int i, size;
971 unsigned char *buff = input_ptr;
972 unsigned char *output_pt = output_ptr;
973 size = image_width * image_height;
974 for (i = size; i > 0; i--) {
976 *output_pt++ = *buff;
982 Pyuv422torgb24(
unsigned char * input_ptr,
unsigned char * output_ptr,
unsigned int image_width,
unsigned int image_height)
984 unsigned int i, size;
985 unsigned char Y, Y1, U, V;
986 unsigned char *buff = input_ptr;
987 unsigned char *output_pt = output_ptr;
988 size = image_width * image_height /2;
989 for (i = size; i > 0; i--) {
1008 void Pyuv422tobgr24(
unsigned char *input_ptr,
unsigned char *output_ptr,
unsigned int image_width,
unsigned int image_height)
1010 unsigned int i, size;
1011 unsigned char Y, Y1, U, V;
1012 unsigned char *buff = input_ptr;
1013 unsigned char *output_pt = output_ptr;
1014 size = image_width * image_height / 2;
1015 for(i = size; i > 0; i--) {
1035 unsigned char *pic0, *pic1;
1036 int *outy, *outu, *outv;
1044 outu = out + 64 * 4;
1045 outv = out + 64 * 5;
1046 for (j = 0; j < 8; j++) {
1047 for (k = 0; k < 8; k++) {
1052 *pic0++ =
CLIP(outy[outy1]);
1053 *pic0++ =
CLIP(128 + *outu);
1054 *pic0++ =
CLIP(outy[outy1+1]);
1055 *pic0++ =
CLIP(128 + *outv);
1056 *pic1++ =
CLIP(outy[outy2]);
1057 *pic1++ =
CLIP(128 + *outu);
1058 *pic1++ =
CLIP(outy[outy2+1]);
1059 *pic1++ =
CLIP(128 + *outv);
1060 outy1 +=2; outy2 += 2; outu++; outv++;
1069 pic0 += 2 * (width -16);
1070 pic1 += 2 * (width -16);
1078 unsigned char *pic0, *pic1;
1079 int *outy, *outu, *outv;
1089 outu = out + 64 * 4;
1090 outv = out + 64 * 5;
1091 for (j = 0; j < 4; j++) {
1092 for (k = 0; k < 8; k++) {
1097 *pic0++ =
CLIP(outy[outy1]);
1098 *pic0++ =
CLIP(128 + outu[outu1]);
1099 *pic0++ =
CLIP(outy[outy1+1]);
1100 *pic0++ =
CLIP(128 + outv[outv1]);
1101 *pic1++ =
CLIP(outy[outy2]);
1102 *pic1++ =
CLIP(128 + outu[outu1+8]);
1103 *pic1++ =
CLIP(outy[outy2+1]);
1104 *pic1++ =
CLIP(128 + outv[outv1+8]);
1105 outv1 += 1; outu1 += 1;
1106 outy1 +=2; outy2 +=2;
1110 outy += 16;outu +=8; outv +=8;
1114 pic0 += 2 * (width -16);
1115 pic1 += 2 * (width -16);
1123 unsigned char *pic0, *pic1;
1124 int *outy, *outu, *outv;
1133 outu = out + 64 * 4;
1134 outv = out + 64 * 5;
1135 for (j = 0; j < 4; j++) {
1136 for (k = 0; k < 4; k++) {
1138 *pic0++ =
CLIP( outy[outy1]);
1139 *pic0++ =
CLIP( 128 + outu[outu1]);
1140 *pic0++ =
CLIP( outy[outy1+1]);
1141 *pic0++ =
CLIP( 128 + outv[outv1]);
1142 *pic1++ =
CLIP( outy[outy2]);
1143 *pic1++ =
CLIP( 128 + outu[outu1+8]);
1144 *pic1++ =
CLIP( outy[outy2+1]);
1145 *pic1++ =
CLIP( 128 + outv[outv1+8]);
1146 outv1 += 2; outu1 += 2;
1147 outy1 +=2; outy2 +=2;
1149 outy += 16;outu +=16; outv +=16;
1153 pic0 += 2 * (width -8);
1154 pic1 += 2 * (width -8);
1161 unsigned char *pic0, *pic1;
1169 for (j = 0; j < 4; j++) {
1170 for (k = 0; k < 4; k++) {
1171 *pic0++ =
CLIP(outy[outy1]);
1173 *pic0++ =
CLIP(outy[outy1+1]);
1175 *pic1++ =
CLIP(outy[outy2]);
1177 *pic1++ =
CLIP(outy[outy2+1]);
1179 outy1 +=2; outy2 +=2;
1184 pic0 += 2 * (width -8);
1185 pic1 += 2 * (width -8);
1189 static unsigned char zig[64] = {
1190 0, 1, 5, 6, 14, 15, 27, 28,
1191 2, 4, 7, 13, 16, 26, 29, 42,
1192 3, 8, 12, 17, 25, 30, 41, 43,
1193 9, 11, 18, 24, 31, 40, 44, 53,
1194 10, 19, 23, 32, 39, 45, 52, 54,
1195 20, 22, 33, 38, 46, 51, 55, 60,
1196 21, 34, 37, 47, 50, 56, 59, 61,
1197 35, 36, 48, 49, 57, 58, 62, 63
1201 IFIX(0.3535533906),
IFIX(0.4903926402),
1202 IFIX(0.4619397663),
IFIX(0.4157348062),
1203 IFIX(0.3535533906),
IFIX(0.2777851165),
1204 IFIX(0.1913417162),
IFIX(0.0975451610)
1214 for (i = 0; i < 8; i++)
1215 for (j = 0; j < 8; j++)
1216 qout[zig[i * 8 + j]] = qin[zig[i * 8 + j]] *
1217 IMULT(aaidct[i], aaidct[j]);
1224 unsigned char *ptbuf;
1227 while (((ptbuf[0] << 8) | ptbuf[1]) != 0xffda){
1230 if(((ptbuf[0] << 8) | ptbuf[1]) == 0xffc4)
1240 char *myext[] = {
"pnm",
"jpg" };
1244 memset (temp,
'\0',
sizeof (temp));
1246 tdate = localtime (&curdate);
1247 snprintf (temp, 26,
"P-%02d:%02d:%04d-%02d:%02d:%02d.%s%c",
1248 tdate->tm_mon + 1, tdate->tm_mday, tdate->tm_year + 1900,
1249 tdate->tm_hour, tdate->tm_min, tdate->tm_sec, myext[fmt],
'\0');
1251 memcpy (Picture, temp, strlen (temp));
1257 unsigned char *ptdeb,*ptcur = buf;
1260 name = calloc(80,1);
1262 file = fopen(name,
"wb");
1265 ptdeb = ptcur = buf;
1266 while (((ptcur[0] << 8) | ptcur[1]) != 0xffc0)
1268 sizein = ptcur-ptdeb;
1273 fwrite(ptcur,size-sizein,1,file);
1275 fwrite(ptcur,size,1,file);
1288 unsigned char *picture = NULL;
1290 name = calloc(80,1);
1292 picture = (
unsigned char *)malloc(width*height*3*
sizeof(
char));
1296 printf(
" no room to take a picture \n");
1300 foutpict = fopen (name,
"wb");
1301 fprintf (foutpict,
"P6\n%d %d\n255\n", width, height);
1302 fwrite (picture,
sizeof (
char), width * height * 3, foutpict);
unsigned char B_FROMYU(unsigned char y, unsigned char u)
static void idctqtab(unsigned char *qin, PREC *qout)
int get_picture(unsigned char *buf, int size)
static void idct(int *in, int *out, int *quant, long off, int max)
void(* ftopict)(int *out, unsigned char *pic, int width)
static struct scan dscans[MAXCOMP]
#define ERR_BAD_WIDTH_OR_HEIGHT
unsigned char G_FROMYUV(unsigned char y, unsigned char u, unsigned char v)
static void decode_mcus(struct in *in, int *dct, int n, struct scan *sc, int *maxp)
#define ERR_UNKNOWN_CID_IN_SCAN
static int dec_readmarker(struct in *in)
int get_pictureYV2(unsigned char *buf, int width, int height)
struct enc_hufftbl * ehuff
static unsigned char zig2[64]
#define ERR_QUANT_TABLE_SELECTOR
static struct comp comps[MAXCOMP]
static unsigned char zig[64]
static struct jpginfo info
#define JPG_HUFFMAN_TABLE_LENGTH
static void yuv422pto422(int *out, unsigned char *pic, int width)
void Pyuv422tobgr24(unsigned char *input_ptr, unsigned char *output_ptr, unsigned int image_width, unsigned int image_height)
static void yuv400pto422(int *out, unsigned char *pic, int width)
static void setinput(struct in *in, unsigned char *p)
static void dec_makehuff(struct dec_hufftbl *hu, int *hufflen, unsigned char *huffvals)
static struct dec_hufftbl dhuff[4]
int jpeg_decode(unsigned char **pic, unsigned char *buf, int *width, int *height)
static void getPictureName(char *Picture, int fmt)
unsigned int Pyuv422torgb24(unsigned char *input_ptr, unsigned char *output_ptr, unsigned int image_width, unsigned int image_height)
static int readtables(int till, int *isDHT)
#define ERR_TOO_MANY_COMPPS
static void yuv420pto422(int *out, unsigned char *pic, int width)
static int huffman_init(void)
#define ERR_NOT_YCBCR_221111
static unsigned char dht_data[DHT_SIZE]
static int dec_rec2(struct in *in, struct dec_hufftbl *hu, int *runp, int c, int i)
#define DEC_REC(in, hu, r, i)
static unsigned char quant[4][64]
unsigned char R_FROMYV(unsigned char y, unsigned char v)
unsigned int Pyuv422togray8(unsigned char *input_ptr, unsigned char *output_ptr, unsigned int image_width, unsigned int image_height)
static unsigned char * datap
static void yuv444pto422(int *out, unsigned char *pic, int width)
static int dec_checkmarker(void)
struct dec_hufftbl * dhuff
const unsigned char JPEGHuffmanTable[JPG_HUFFMAN_TABLE_LENGTH]
int is_huffman(unsigned char *buf)
static int fillbits(struct in *in, int le, unsigned int bi)
static void dec_initscans(void)