6 /* private prototypes */
7 static void Read_Lower_Layer_Component_Framewise _ANSI_ARGS_((int comp, int lw, int lh));
8 static void Read_Lower_Layer_Component_Fieldwise _ANSI_ARGS_((int comp, int lw, int lh));
9 static void Make_Spatial_Prediction_Frame _ANSI_ARGS_((int progressive_frame,
10 int llprogressive_frame, unsigned char *fld0, unsigned char *fld1,
11 short *tmp, unsigned char *dst, int llx0, int lly0, int llw, int llh,
12 int horizontal_size, int vertical_size, int vm, int vn, int hm, int hn,
14 static void Deinterlace _ANSI_ARGS_((unsigned char *fld0, unsigned char *fld1,
15 int j0, int lx, int ly, int aperture));
16 static void Subsample_Vertical _ANSI_ARGS_((unsigned char *s, short *d,
17 int lx, int lys, int lyd, int m, int n, int j0, int dj));
18 static void Subsample_Horizontal _ANSI_ARGS_((short *s, unsigned char *d,
19 int x0, int lx, int lxs, int lxd, int ly, int m, int n));
23 /* get reference frame */
24 void Spatial_Prediction()
29 Read_Lower_Layer_Component_Framewise(0,lower_layer_prediction_horizontal_size,
30 lower_layer_prediction_vertical_size); /* Y */
31 Read_Lower_Layer_Component_Framewise(1,lower_layer_prediction_horizontal_size>>1,
32 lower_layer_prediction_vertical_size>>1); /* Cb ("U") */
33 Read_Lower_Layer_Component_Framewise(2,lower_layer_prediction_horizontal_size>>1,
34 lower_layer_prediction_vertical_size>>1); /* Cr ("V") */
38 Read_Lower_Layer_Component_Fieldwise(0,lower_layer_prediction_horizontal_size,
39 lower_layer_prediction_vertical_size); /* Y */
40 Read_Lower_Layer_Component_Fieldwise(1,lower_layer_prediction_horizontal_size>>1,
41 lower_layer_prediction_vertical_size>>1); /* Cb ("U") */
42 Read_Lower_Layer_Component_Fieldwise(2,lower_layer_prediction_horizontal_size>>1,
43 lower_layer_prediction_vertical_size>>1); /* Cr ("V") */
47 Make_Spatial_Prediction_Frame /* Y */
48 (progressive_frame,lower_layer_progressive_frame,llframe0[0],llframe1[0],
49 lltmp,current_frame[0],lower_layer_horizontal_offset,
50 lower_layer_vertical_offset,
51 lower_layer_prediction_horizontal_size,
52 lower_layer_prediction_vertical_size,
53 horizontal_size,vertical_size,vertical_subsampling_factor_m,
54 vertical_subsampling_factor_n,horizontal_subsampling_factor_m,
55 horizontal_subsampling_factor_n,
56 picture_structure!=FRAME_PICTURE); /* this changed from CD to DIS */
58 Make_Spatial_Prediction_Frame /* Cb */
59 (progressive_frame,lower_layer_progressive_frame,llframe0[1],llframe1[1],
60 lltmp,current_frame[1],lower_layer_horizontal_offset/2,
61 lower_layer_vertical_offset/2,
62 lower_layer_prediction_horizontal_size>>1,
63 lower_layer_prediction_vertical_size>>1,
64 horizontal_size>>1,vertical_size>>1,vertical_subsampling_factor_m,
65 vertical_subsampling_factor_n,horizontal_subsampling_factor_m,
66 horizontal_subsampling_factor_n,1);
68 Make_Spatial_Prediction_Frame /* Cr */
69 (progressive_frame,lower_layer_progressive_frame,llframe0[2],llframe1[2],
70 lltmp,current_frame[2],lower_layer_horizontal_offset/2,
71 lower_layer_vertical_offset/2,
72 lower_layer_prediction_horizontal_size>>1,
73 lower_layer_prediction_vertical_size>>1,
74 horizontal_size>>1,vertical_size>>1,vertical_subsampling_factor_m,
75 vertical_subsampling_factor_n,horizontal_subsampling_factor_m,
76 horizontal_subsampling_factor_n,1);
80 static void Read_Lower_Layer_Component_Framewise(comp,lw,lh)
86 char ext[3][3] = {".Y",".U",".V"};
87 /* char *ext = {".Y",".U",".V"}; */
90 sprintf(fname,Lower_Layer_Picture_Filename,True_Framenum);
91 strcat(fname,ext[comp]);
94 printf("reading %s\n",fname);
97 if (fd==NULL) exit(-1);
98 for (j=0; j<lh; j++) {
100 llframe0[comp][lw*j+i]=getc(fd);
101 if (! lower_layer_progressive_frame) {
104 llframe1[comp][lw*j+i]=getc(fd);
111 static void Read_Lower_Layer_Component_Fieldwise(comp,lw,lh)
117 char ext[3][3] = {".Y",".U",".V"};
118 /* char *ext = {".Y",".U",".V"}; */
121 sprintf(fname,Lower_Layer_Picture_Filename,True_Framenum,lower_layer_progressive_frame ? 'f':'a');
122 strcat(fname,ext[comp]);
125 printf("reading %s\n",fname);
127 fd=fopen(fname,"rb");
128 if (fd==NULL) exit(-1);
129 for (j=0; j<lh; j+=lower_layer_progressive_frame?1:2)
131 llframe0[comp][lw*j+i]=getc(fd);
134 if (! lower_layer_progressive_frame) {
135 sprintf(fname,Lower_Layer_Picture_Filename,True_Framenum,'b');
136 strcat(fname,ext[comp]);
139 printf("reading %s\n",fname);
141 fd=fopen(fname,"rb");
142 if (fd==NULL) exit(-1);
143 for (j=1; j<lh; j+=2)
145 llframe1[comp][lw*j+i]=getc(fd);
151 /* form spatial prediction */
152 static void Make_Spatial_Prediction_Frame(progressive_frame,
153 llprogressive_frame,fld0,fld1,tmp,dst,llx0,lly0,llw,llh,horizontal_size,
154 vertical_size,vm,vn,hm,hn,aperture)
155 int progressive_frame,llprogressive_frame;
156 unsigned char *fld0,*fld1;
159 int llx0,lly0,llw,llh,horizontal_size,vertical_size,vm,vn,hm,hn,aperture;
161 int w, h, x0, llw2, llh2;
166 if (llprogressive_frame)
168 /* progressive -> progressive / interlaced */
169 Subsample_Vertical(fld0,tmp,llw,llh,llh2,vm,vn,0,1);
171 else if (progressive_frame)
173 /* interlaced -> progressive */
174 if (lower_layer_deinterlaced_field_select)
176 Deinterlace(fld1,fld0,0,llw,llh,aperture);
177 Subsample_Vertical(fld1,tmp,llw,llh,llh2,vm,vn,0,1);
181 Deinterlace(fld0,fld1,1,llw,llh,aperture);
182 Subsample_Vertical(fld0,tmp,llw,llh,llh2,vm,vn,0,1);
187 /* interlaced -> interlaced */
188 Deinterlace(fld0,fld1,1,llw,llh,aperture);
189 Deinterlace(fld1,fld0,0,llw,llh,aperture);
190 Subsample_Vertical(fld0,tmp,llw,llh,llh2,vm,vn,0,2);
191 Subsample_Vertical(fld1,tmp,llw,llh,llh2,vm,vn,1,2);
194 /* vertical limits */
201 h = (vertical_size<llh2) ? vertical_size : llh2;
205 dst+= horizontal_size*lly0;
206 h= vertical_size - lly0;
211 /* horizontal limits */
218 w = (horizontal_size<llw2) ? horizontal_size : llw2;
224 w = horizontal_size - llx0;
229 Subsample_Horizontal(tmp,dst,x0,w,llw,horizontal_size,h,hm,hn);
232 /* deinterlace one field (interpolate opposite parity samples)
234 * deinterlacing is done in-place: if j0=1, fld0 contains the input field in
235 * its even lines and the odd lines are interpolated by this routine
236 * if j0=0, the input field is in the odd lines and the even lines are
239 * fld0: field to be deinterlaced
240 * fld1: other field (referenced by the two field aperture filter)
241 * j0: 0: interpolate even (top) lines, 1: interpolate odd (bottom) lines
242 * lx: width of fld0 and fld1
243 * ly: height of the deinterlaced field (has to be even)
244 * aperture: 1: use one field aperture filter (two field otherwise)
246 static void Deinterlace(fld0,fld1,j0,lx,ly,aperture)
247 unsigned char *fld0,*fld1;
248 int j0,lx,ly; /* ly has to be even */
252 unsigned char *p0, *p0m1, *p0p1, *p1, *p1m2, *p1p2;
254 /* deinterlace one field */
255 for (j=j0; j<ly; j+=2)
258 p0m1 = (j==0) ? p0+lx : p0-lx;
259 p0p1 = (j==ly-1) ? p0-lx : p0+lx;
263 p0[i] = (unsigned int)(p0m1[i] + p0p1[i] + 1)>>1;
267 p1m2 = (j<2) ? p1 : p1-2*lx;
268 p1p2 = (j>=ly-2) ? p1 : p1+2*lx;
271 v = 8*(p0m1[i]+p0p1[i]) + 2*p1[i] - p1m2[i] - p1p2[i];
272 p0[i] = Clip[(v + ((v>=0) ? 8 : 7))>>4];
278 /* vertical resampling */
279 static void Subsample_Vertical(s,d,lx,lys,lyd,m,n,j0,dj)
282 int lx, lys, lyd, m, n, j0, dj;
284 int i, j, c1, c2, jd;
285 unsigned char *s1, *s2;
288 for (j=j0; j<lyd; j+=dj)
293 s2 = (jd<lys-1)? s1+lx : s1;
294 c2 = (16*((j*m)%n) + (n>>1))/n;
297 d1[i] = c1*s1[i] + c2*s2[i];
301 /* horizontal resampling */
302 static void Subsample_Horizontal(s,d,x0,lx,lxs,lxd,ly,m,n)
305 int x0, lx, lxs, lxd, ly, m, n;
307 int i, i1, j, id, c1, c2, v;
311 for (i1=0; i1<lx; i1++)
317 s2 = (id<lxs-1) ? s1+1 : s1;
318 c2 = (16*((i*m)%n) + (n>>1))/n;
322 v = c1*(*s1) + c2*(*s2);
323 *d1 = (v + ((v>=0) ? 128 : 127))>>8;