/* readpic.c, read source pictures */ /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */ /* * Disclaimer of Warranty * * These software programs are available to the user without any license fee or * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims * any and all warranties, whether express, implied, or statuary, including any * implied warranties or merchantability or of fitness for a particular * purpose. In no event shall the copyright-holder be liable for any * incidental, punitive, or consequential damages of any kind whatsoever * arising from the use of these programs. * * This disclaimer of warranty extends to the user of these programs and user's * customers, employees, agents, transferees, successors, and assigns. * * The MPEG Software Simulation Group does not represent or warrant that the * programs furnished hereunder are free of infringement of any third-party * patents. * * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware, * are subject to royalty fees to patent holders. Many of these patents are * general enough such that they are unavoidable regardless of implementation * design. * */ #include #include #include "config.h" #include "global.h" /* private prototypes */ static void read_y_u_v _ANSI_ARGS_((char *fname, unsigned char *frame[])); static void read_yuv _ANSI_ARGS_((char *fname, unsigned char *frame[])); static void read_ppm _ANSI_ARGS_((char *fname, unsigned char *frame[])); static void border_extend _ANSI_ARGS_((unsigned char *frame, int w1, int h1, int w2, int h2)); static void conv444to422 _ANSI_ARGS_((unsigned char *src, unsigned char *dst)); static void conv422to420 _ANSI_ARGS_((unsigned char *src, unsigned char *dst)); int pbm_getint(FILE *file); /*forward declaration */ void readframe(fname,frame) char *fname; unsigned char *frame[]; { switch (inputtype) { case T_Y_U_V: read_y_u_v(fname,frame); break; case T_YUV: read_yuv(fname,frame); break; case T_PPM: read_ppm(fname,frame); break; default: break; } } static void read_y_u_v(fname,frame) char *fname; unsigned char *frame[]; { int i; int chrom_hsize, chrom_vsize; char name[128]; FILE *fd; chrom_hsize = (chroma_format==CHROMA444) ? horizontal_size : horizontal_size>>1; chrom_vsize = (chroma_format!=CHROMA420) ? vertical_size : vertical_size>>1; sprintf(name,"%s.Y",fname); if (!(fd = fopen(name,"rb"))) { sprintf(errortext,"Couldn't open %s\n",name); error(errortext); } for (i=0; i>1; chrom_vsize = (chroma_format!=CHROMA420) ? vertical_size : vertical_size>>1; sprintf(name,"%s.yuv",fname); if (!(fd = fopen(name,"rb"))) { sprintf(errortext,"Couldn't open %s\n",name); error(errortext); } /* Y */ for (i=0; i8) i = 3; cr = coef[i-1][0]; cg = coef[i-1][1]; cb = coef[i-1][2]; cu = 0.5/(1.0-cb); cv = 0.5/(1.0-cr); if (chroma_format==CHROMA444) { u444 = frame[1]; v444 = frame[2]; } else { if (!u444) { if (!(u444 = (unsigned char *)malloc(width*height))) error("malloc failed"); if (!(v444 = (unsigned char *)malloc(width*height))) error("malloc failed"); if (chroma_format==CHROMA420) { if (!(u422 = (unsigned char *)malloc((width>>1)*height))) error("malloc failed"); if (!(v422 = (unsigned char *)malloc((width>>1)*height))) error("malloc failed"); } } } sprintf(name,"%s.ppm",fname); if (!(fd = fopen(name,"rb"))) { sprintf(errortext,"Couldn't open %s\n",name); error(errortext); } /* skip header */ getc(fd); getc(fd); /* magic number (P6) */ pbm_getint(fd); pbm_getint(fd); pbm_getint(fd); /* width height maxcolors */ for (i=0; i>1] = clp[(int)(228*(src[i]+src[ip1]) +70*(src[im1]+src[ip2]) -37*(src[im2]+src[ip3]) -21*(src[im3]+src[ip4]) +11*(src[im4]+src[ip5]) + 5*(src[im5]+src[ip6])+256)>>9]; } src+= width; dst+= width>>1; } } else { /* MPEG-2 */ for (j=0; j>1] = clp[(int)( 22*(src[im5]+src[ip5])-52*(src[im3]+src[ip3]) +159*(src[im1]+src[ip1])+256*src[i]+256)>>9]; } src+= width; dst+= width>>1; } } } /* vertical filter and 2:1 subsampling */ static void conv422to420(src,dst) unsigned char *src, *dst; { int w, i, j, jm6, jm5, jm4, jm3, jm2, jm1; int jp1, jp2, jp3, jp4, jp5, jp6; w = width>>1; if (prog_frame) { /* intra frame */ for (i=0; i>1)] = clp[(int)(228*(src[w*j]+src[w*jp1]) +70*(src[w*jm1]+src[w*jp2]) -37*(src[w*jm2]+src[w*jp3]) -21*(src[w*jm3]+src[w*jp4]) +11*(src[w*jm4]+src[w*jp5]) + 5*(src[w*jm5]+src[w*jp6])+256)>>9]; } src++; dst++; } } else { /* intra field */ for (i=0; i>1)] = clp[(int)(8*src[w*jm5] +5*src[w*jm4] -30*src[w*jm3] -18*src[w*jm2] +113*src[w*jm1] +242*src[w*j] +192*src[w*jp1] +35*src[w*jp2] -38*src[w*jp3] -10*src[w*jp4] +11*src[w*jp5] +2*src[w*jp6]+256)>>9]; /* bottom field */ jm6 = (j<9) ? 1 : j-9; jm5 = (j<7) ? 1 : j-7; jm4 = (j<5) ? 1 : j-5; jm3 = (j<3) ? 1 : j-3; jm2 = (j<1) ? 1 : j-1; jm1 = (j>1)+1)] = clp[(int)(8*src[w*jp6] +5*src[w*jp5] -30*src[w*jp4] -18*src[w*jp3] +113*src[w*jp2] +242*src[w*jp1] +192*src[w*jm1] +35*src[w*jm2] -38*src[w*jm3] -10*src[w*jm4] +11*src[w*jm5] +2*src[w*jm6]+256)>>9]; } src++; dst++; } } } /* pbm_getc() and pbm_getint() are essentially taken from * PBMPLUS (C) Jef Poskanzer * but without error/EOF checking */ char pbm_getc(file) FILE* file; { char ch; ch = getc(file); if (ch=='#') { do { ch = getc(file); } while (ch!='\n' && ch!='\r'); } return ch; } int pbm_getint(file) FILE* file; { char ch; int i; do { ch = pbm_getc(file); } while (ch==' ' || ch=='\t' || ch=='\n' || ch=='\r'); i = 0; do { i = i*10 + ch-'0'; ch = pbm_getc(file); } while (ch>='0' && ch<='9'); return i; }