3 #include <string.h> // for strcat
4 #include <stdlib.h> // for exit
8 /* private prototypes */
9 static void Read_Lower_Layer_Component_Framewise _ANSI_ARGS_((int comp, int lw, int lh));
10 static void Read_Lower_Layer_Component_Fieldwise _ANSI_ARGS_((int comp, int lw, int lh));
11 static void Make_Spatial_Prediction_Frame _ANSI_ARGS_((int progressive_frame,
12 int llprogressive_frame, unsigned char *fld0, unsigned char *fld1,
13 short *tmp, unsigned char *dst, int llx0, int lly0, int llw, int llh,
14 int horizontal_size, int vertical_size, int vm, int vn, int hm, int hn,
16 static void Deinterlace _ANSI_ARGS_((unsigned char *fld0, unsigned char *fld1,
17 int j0, int lx, int ly, int aperture));
18 static void Subsample_Vertical _ANSI_ARGS_((unsigned char *s, short *d,
19 int lx, int lys, int lyd, int m, int n, int j0, int dj));
20 static void Subsample_Horizontal _ANSI_ARGS_((short *s, unsigned char *d,
21 int x0, int lx, int lxs, int lxd, int ly, int m, int n));
25 /* get reference frame */
26 void Spatial_Prediction()
31 Read_Lower_Layer_Component_Framewise(0,lower_layer_prediction_horizontal_size,
32 lower_layer_prediction_vertical_size); /* Y */
33 Read_Lower_Layer_Component_Framewise(1,lower_layer_prediction_horizontal_size>>1,
34 lower_layer_prediction_vertical_size>>1); /* Cb ("U") */
35 Read_Lower_Layer_Component_Framewise(2,lower_layer_prediction_horizontal_size>>1,
36 lower_layer_prediction_vertical_size>>1); /* Cr ("V") */
40 Read_Lower_Layer_Component_Fieldwise(0,lower_layer_prediction_horizontal_size,
41 lower_layer_prediction_vertical_size); /* Y */
42 Read_Lower_Layer_Component_Fieldwise(1,lower_layer_prediction_horizontal_size>>1,
43 lower_layer_prediction_vertical_size>>1); /* Cb ("U") */
44 Read_Lower_Layer_Component_Fieldwise(2,lower_layer_prediction_horizontal_size>>1,
45 lower_layer_prediction_vertical_size>>1); /* Cr ("V") */
49 Make_Spatial_Prediction_Frame /* Y */
50 (progressive_frame,lower_layer_progressive_frame,llframe0[0],llframe1[0],
51 lltmp,current_frame[0],lower_layer_horizontal_offset,
52 lower_layer_vertical_offset,
53 lower_layer_prediction_horizontal_size,
54 lower_layer_prediction_vertical_size,
55 horizontal_size,vertical_size,vertical_subsampling_factor_m,
56 vertical_subsampling_factor_n,horizontal_subsampling_factor_m,
57 horizontal_subsampling_factor_n,
58 picture_structure!=FRAME_PICTURE); /* this changed from CD to DIS */
60 Make_Spatial_Prediction_Frame /* Cb */
61 (progressive_frame,lower_layer_progressive_frame,llframe0[1],llframe1[1],
62 lltmp,current_frame[1],lower_layer_horizontal_offset/2,
63 lower_layer_vertical_offset/2,
64 lower_layer_prediction_horizontal_size>>1,
65 lower_layer_prediction_vertical_size>>1,
66 horizontal_size>>1,vertical_size>>1,vertical_subsampling_factor_m,
67 vertical_subsampling_factor_n,horizontal_subsampling_factor_m,
68 horizontal_subsampling_factor_n,1);
70 Make_Spatial_Prediction_Frame /* Cr */
71 (progressive_frame,lower_layer_progressive_frame,llframe0[2],llframe1[2],
72 lltmp,current_frame[2],lower_layer_horizontal_offset/2,
73 lower_layer_vertical_offset/2,
74 lower_layer_prediction_horizontal_size>>1,
75 lower_layer_prediction_vertical_size>>1,
76 horizontal_size>>1,vertical_size>>1,vertical_subsampling_factor_m,
77 vertical_subsampling_factor_n,horizontal_subsampling_factor_m,
78 horizontal_subsampling_factor_n,1);
82 static void Read_Lower_Layer_Component_Framewise(comp,lw,lh)
88 char ext[3][3] = {".Y",".U",".V"};
89 /* char *ext = {".Y",".U",".V"}; */
92 sprintf(fname,Lower_Layer_Picture_Filename,True_Framenum);
93 strcat(fname,ext[comp]);
96 printf("reading %s\n",fname);
99 if (fd==NULL) exit(-1);
100 for (j=0; j<lh; j++) {
102 llframe0[comp][lw*j+i]=getc(fd);
103 if (! lower_layer_progressive_frame) {
106 llframe1[comp][lw*j+i]=getc(fd);
113 static void Read_Lower_Layer_Component_Fieldwise(comp,lw,lh)
119 char ext[3][3] = {".Y",".U",".V"};
120 /* char *ext = {".Y",".U",".V"}; */
123 sprintf(fname,Lower_Layer_Picture_Filename,True_Framenum,lower_layer_progressive_frame ? 'f':'a');
124 strcat(fname,ext[comp]);
127 printf("reading %s\n",fname);
129 fd=fopen(fname,"rb");
130 if (fd==NULL) exit(-1);
131 for (j=0; j<lh; j+=lower_layer_progressive_frame?1:2)
133 llframe0[comp][lw*j+i]=getc(fd);
136 if (! lower_layer_progressive_frame) {
137 sprintf(fname,Lower_Layer_Picture_Filename,True_Framenum,'b');
138 strcat(fname,ext[comp]);
141 printf("reading %s\n",fname);
143 fd=fopen(fname,"rb");
144 if (fd==NULL) exit(-1);
145 for (j=1; j<lh; j+=2)
147 llframe1[comp][lw*j+i]=getc(fd);
153 /* form spatial prediction */
154 static void Make_Spatial_Prediction_Frame(progressive_frame,
155 llprogressive_frame,fld0,fld1,tmp,dst,llx0,lly0,llw,llh,horizontal_size,
156 vertical_size,vm,vn,hm,hn,aperture)
157 int progressive_frame,llprogressive_frame;
158 unsigned char *fld0,*fld1;
161 int llx0,lly0,llw,llh,horizontal_size,vertical_size,vm,vn,hm,hn,aperture;
163 int w, h, x0, llw2, llh2;
168 if (llprogressive_frame)
170 /* progressive -> progressive / interlaced */
171 Subsample_Vertical(fld0,tmp,llw,llh,llh2,vm,vn,0,1);
173 else if (progressive_frame)
175 /* interlaced -> progressive */
176 if (lower_layer_deinterlaced_field_select)
178 Deinterlace(fld1,fld0,0,llw,llh,aperture);
179 Subsample_Vertical(fld1,tmp,llw,llh,llh2,vm,vn,0,1);
183 Deinterlace(fld0,fld1,1,llw,llh,aperture);
184 Subsample_Vertical(fld0,tmp,llw,llh,llh2,vm,vn,0,1);
189 /* interlaced -> interlaced */
190 Deinterlace(fld0,fld1,1,llw,llh,aperture);
191 Deinterlace(fld1,fld0,0,llw,llh,aperture);
192 Subsample_Vertical(fld0,tmp,llw,llh,llh2,vm,vn,0,2);
193 Subsample_Vertical(fld1,tmp,llw,llh,llh2,vm,vn,1,2);
196 /* vertical limits */
203 h = (vertical_size<llh2) ? vertical_size : llh2;
207 dst+= horizontal_size*lly0;
208 h= vertical_size - lly0;
213 /* horizontal limits */
220 w = (horizontal_size<llw2) ? horizontal_size : llw2;
226 w = horizontal_size - llx0;
231 Subsample_Horizontal(tmp,dst,x0,w,llw,horizontal_size,h,hm,hn);
234 /* deinterlace one field (interpolate opposite parity samples)
236 * deinterlacing is done in-place: if j0=1, fld0 contains the input field in
237 * its even lines and the odd lines are interpolated by this routine
238 * if j0=0, the input field is in the odd lines and the even lines are
241 * fld0: field to be deinterlaced
242 * fld1: other field (referenced by the two field aperture filter)
243 * j0: 0: interpolate even (top) lines, 1: interpolate odd (bottom) lines
244 * lx: width of fld0 and fld1
245 * ly: height of the deinterlaced field (has to be even)
246 * aperture: 1: use one field aperture filter (two field otherwise)
248 static void Deinterlace(fld0,fld1,j0,lx,ly,aperture)
249 unsigned char *fld0,*fld1;
250 int j0,lx,ly; /* ly has to be even */
254 unsigned char *p0, *p0m1, *p0p1, *p1, *p1m2, *p1p2;
256 /* deinterlace one field */
257 for (j=j0; j<ly; j+=2)
260 p0m1 = (j==0) ? p0+lx : p0-lx;
261 p0p1 = (j==ly-1) ? p0-lx : p0+lx;
265 p0[i] = (unsigned int)(p0m1[i] + p0p1[i] + 1)>>1;
269 p1m2 = (j<2) ? p1 : p1-2*lx;
270 p1p2 = (j>=ly-2) ? p1 : p1+2*lx;
273 v = 8*(p0m1[i]+p0p1[i]) + 2*p1[i] - p1m2[i] - p1p2[i];
274 p0[i] = Clip[(v + ((v>=0) ? 8 : 7))>>4];
280 /* vertical resampling */
281 static void Subsample_Vertical(s,d,lx,lys,lyd,m,n,j0,dj)
284 int lx, lys, lyd, m, n, j0, dj;
286 int i, j, c1, c2, jd;
287 unsigned char *s1, *s2;
290 for (j=j0; j<lyd; j+=dj)
295 s2 = (jd<lys-1)? s1+lx : s1;
296 c2 = (16*((j*m)%n) + (n>>1))/n;
299 d1[i] = c1*s1[i] + c2*s2[i];
303 /* horizontal resampling */
304 static void Subsample_Horizontal(s,d,x0,lx,lxs,lxd,ly,m,n)
307 int x0, lx, lxs, lxd, ly, m, n;
309 int i, i1, j, id, c1, c2, v;
313 for (i1=0; i1<lx; i1++)
319 s2 = (id<lxs-1) ? s1+1 : s1;
320 c2 = (16*((i*m)%n) + (n>>1))/n;
324 v = c1*(*s1) + c2*(*s2);
325 *d1 = (v + ((v>=0) ? 128 : 127))>>8;