Rev 1624 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1624 | giacomo | 1 | |
2 | #include <stdio.h> |
||
3 | #include "config.h" |
||
4 | #include "global.h" |
||
5 | |||
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, |
||
13 | int aperture)); |
||
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)); |
||
20 | |||
21 | |||
22 | |||
23 | /* get reference frame */ |
||
24 | void Spatial_Prediction() |
||
25 | { |
||
26 | |||
27 | if(Frame_Store_Flag) |
||
28 | { |
||
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") */ |
||
35 | } |
||
36 | else |
||
37 | { |
||
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") */ |
||
44 | } |
||
45 | |||
46 | |||
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 */ |
||
57 | |||
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); |
||
67 | |||
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); |
||
77 | |||
78 | } |
||
79 | |||
80 | static void Read_Lower_Layer_Component_Framewise(comp,lw,lh) |
||
81 | int comp; |
||
82 | int lw, lh; |
||
83 | { |
||
84 | FILE *fd; |
||
85 | char fname[256]; |
||
86 | char ext[3][3] = {".Y",".U",".V"}; |
||
87 | /* char *ext = {".Y",".U",".V"}; */ |
||
88 | int i,j; |
||
89 | |||
90 | sprintf(fname,Lower_Layer_Picture_Filename,True_Framenum); |
||
91 | strcat(fname,ext[comp]); |
||
92 | #ifdef VERBOSE |
||
93 | if (Verbose_Flag>1) |
||
94 | printf("reading %s\n",fname); |
||
95 | #endif |
||
96 | fd=fopen(fname,"rb"); |
||
97 | if (fd==NULL) sys_abort(-1); // PJ exit(-1); |
||
98 | for (j=0; j<lh; j++) { |
||
99 | for (i=0; i<lw; i++) |
||
100 | llframe0[comp][lw*j+i]=getc(fd); |
||
101 | if (! lower_layer_progressive_frame) { |
||
102 | j++; |
||
103 | for (i=0; i<lw; i++) |
||
104 | llframe1[comp][lw*j+i]=getc(fd); |
||
105 | } |
||
106 | } |
||
107 | fclose(fd); |
||
108 | } |
||
109 | |||
110 | |||
111 | static void Read_Lower_Layer_Component_Fieldwise(comp,lw,lh) |
||
112 | int comp; |
||
113 | int lw, lh; |
||
114 | { |
||
115 | FILE *fd; |
||
116 | char fname[256]; |
||
117 | char ext[3][3] = {".Y",".U",".V"}; |
||
118 | /* char *ext = {".Y",".U",".V"}; */ |
||
119 | int i,j; |
||
120 | |||
121 | sprintf(fname,Lower_Layer_Picture_Filename,True_Framenum,lower_layer_progressive_frame ? 'f':'a'); |
||
122 | strcat(fname,ext[comp]); |
||
123 | #ifdef VERBOSE |
||
124 | if (Verbose_Flag>1) |
||
125 | printf("reading %s\n",fname); |
||
126 | #endif |
||
127 | fd=fopen(fname,"rb"); |
||
128 | if (fd==NULL) sys_abort(-1); // PJ exit(-1); |
||
129 | for (j=0; j<lh; j+=lower_layer_progressive_frame?1:2) |
||
130 | for (i=0; i<lw; i++) |
||
131 | llframe0[comp][lw*j+i]=getc(fd); |
||
132 | fclose(fd); |
||
133 | |||
134 | if (! lower_layer_progressive_frame) { |
||
135 | sprintf(fname,Lower_Layer_Picture_Filename,True_Framenum,'b'); |
||
136 | strcat(fname,ext[comp]); |
||
137 | #ifdef VERBOSE |
||
138 | if (Verbose_Flag>1) |
||
139 | printf("reading %s\n",fname); |
||
140 | #endif |
||
141 | fd=fopen(fname,"rb"); |
||
142 | if (fd==NULL) sys_abort(-1); // PJ exit(-1); |
||
143 | for (j=1; j<lh; j+=2) |
||
144 | for (i=0; i<lw; i++) |
||
145 | llframe1[comp][lw*j+i]=getc(fd); |
||
146 | fclose(fd); |
||
147 | } |
||
148 | } |
||
149 | |||
150 | |||
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; |
||
157 | short *tmp; |
||
158 | unsigned char *dst; |
||
159 | int llx0,lly0,llw,llh,horizontal_size,vertical_size,vm,vn,hm,hn,aperture; |
||
160 | { |
||
161 | int w, h, x0, llw2, llh2; |
||
162 | |||
163 | llw2 = (llw*hn)/hm; |
||
164 | llh2 = (llh*vn)/vm; |
||
165 | |||
166 | if (llprogressive_frame) |
||
167 | { |
||
168 | /* progressive -> progressive / interlaced */ |
||
169 | Subsample_Vertical(fld0,tmp,llw,llh,llh2,vm,vn,0,1); |
||
170 | } |
||
171 | else if (progressive_frame) |
||
172 | { |
||
173 | /* interlaced -> progressive */ |
||
174 | if (lower_layer_deinterlaced_field_select) |
||
175 | { |
||
176 | Deinterlace(fld1,fld0,0,llw,llh,aperture); |
||
177 | Subsample_Vertical(fld1,tmp,llw,llh,llh2,vm,vn,0,1); |
||
178 | } |
||
179 | else |
||
180 | { |
||
181 | Deinterlace(fld0,fld1,1,llw,llh,aperture); |
||
182 | Subsample_Vertical(fld0,tmp,llw,llh,llh2,vm,vn,0,1); |
||
183 | } |
||
184 | } |
||
185 | else |
||
186 | { |
||
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); |
||
192 | } |
||
193 | |||
194 | /* vertical limits */ |
||
195 | if (lly0<0) |
||
196 | { |
||
197 | tmp-= llw*lly0; |
||
198 | llh2+= lly0; |
||
199 | if (llh2<0) |
||
200 | llh2 = 0; |
||
201 | h = (vertical_size<llh2) ? vertical_size : llh2; |
||
202 | } |
||
203 | else |
||
204 | { |
||
205 | dst+= horizontal_size*lly0; |
||
206 | h= vertical_size - lly0; |
||
207 | if (h>llh2) |
||
208 | h = llh2; |
||
209 | } |
||
210 | |||
211 | /* horizontal limits */ |
||
212 | if (llx0<0) |
||
213 | { |
||
214 | x0 = -llx0; |
||
215 | llw2+= llx0; |
||
216 | if (llw2<0) |
||
217 | llw2 = 0; |
||
218 | w = (horizontal_size<llw2) ? horizontal_size : llw2; |
||
219 | } |
||
220 | else |
||
221 | { |
||
222 | dst+= llx0; |
||
223 | x0 = 0; |
||
224 | w = horizontal_size - llx0; |
||
225 | if (w>llw2) |
||
226 | w = llw2; |
||
227 | } |
||
228 | |||
229 | Subsample_Horizontal(tmp,dst,x0,w,llw,horizontal_size,h,hm,hn); |
||
230 | } |
||
231 | |||
232 | /* deinterlace one field (interpolate opposite parity samples) |
||
233 | * |
||
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 |
||
237 | * interpolated |
||
238 | * |
||
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) |
||
245 | */ |
||
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 */ |
||
249 | int aperture; |
||
250 | { |
||
251 | int i,j,v; |
||
252 | unsigned char *p0, *p0m1, *p0p1, *p1, *p1m2, *p1p2; |
||
253 | |||
254 | /* deinterlace one field */ |
||
255 | for (j=j0; j<ly; j+=2) |
||
256 | { |
||
257 | p0 = fld0+lx*j; |
||
258 | p0m1 = (j==0) ? p0+lx : p0-lx; |
||
259 | p0p1 = (j==ly-1) ? p0-lx : p0+lx; |
||
260 | |||
261 | if (aperture) |
||
262 | for (i=0; i<lx; i++) |
||
263 | p0[i] = (unsigned int)(p0m1[i] + p0p1[i] + 1)>>1; |
||
264 | else |
||
265 | { |
||
266 | p1 = fld1 + lx*j; |
||
267 | p1m2 = (j<2) ? p1 : p1-2*lx; |
||
268 | p1p2 = (j>=ly-2) ? p1 : p1+2*lx; |
||
269 | for (i=0; i<lx; i++) |
||
270 | { |
||
271 | v = 8*(p0m1[i]+p0p1[i]) + 2*p1[i] - p1m2[i] - p1p2[i]; |
||
272 | p0[i] = Clip[(v + ((v>=0) ? 8 : 7))>>4]; |
||
273 | } |
||
274 | } |
||
275 | } |
||
276 | } |
||
277 | |||
278 | /* vertical resampling */ |
||
279 | static void Subsample_Vertical(s,d,lx,lys,lyd,m,n,j0,dj) |
||
280 | unsigned char *s; |
||
281 | short *d; |
||
282 | int lx, lys, lyd, m, n, j0, dj; |
||
283 | { |
||
284 | int i, j, c1, c2, jd; |
||
285 | unsigned char *s1, *s2; |
||
286 | short *d1; |
||
287 | |||
288 | for (j=j0; j<lyd; j+=dj) |
||
289 | { |
||
290 | d1 = d + lx*j; |
||
291 | jd = (j*m)/n; |
||
292 | s1 = s + lx*jd; |
||
293 | s2 = (jd<lys-1)? s1+lx : s1; |
||
294 | c2 = (16*((j*m)%n) + (n>>1))/n; |
||
295 | c1 = 16 - c2; |
||
296 | for (i=0; i<lx; i++) |
||
297 | d1[i] = c1*s1[i] + c2*s2[i]; |
||
298 | } |
||
299 | } |
||
300 | |||
301 | /* horizontal resampling */ |
||
302 | static void Subsample_Horizontal(s,d,x0,lx,lxs,lxd,ly,m,n) |
||
303 | short *s; |
||
304 | unsigned char *d; |
||
305 | int x0, lx, lxs, lxd, ly, m, n; |
||
306 | { |
||
307 | int i, i1, j, id, c1, c2, v; |
||
308 | short *s1, *s2; |
||
309 | unsigned char *d1; |
||
310 | |||
311 | for (i1=0; i1<lx; i1++) |
||
312 | { |
||
313 | d1 = d + i1; |
||
314 | i = x0 + i1; |
||
315 | id = (i*m)/n; |
||
316 | s1 = s+id; |
||
317 | s2 = (id<lxs-1) ? s1+1 : s1; |
||
318 | c2 = (16*((i*m)%n) + (n>>1))/n; |
||
319 | c1 = 16 - c2; |
||
320 | for (j=0; j<ly; j++) |
||
321 | { |
||
322 | v = c1*(*s1) + c2*(*s2); |
||
323 | *d1 = (v + ((v>=0) ? 128 : 127))>>8; |
||
324 | d1+= lxd; |
||
325 | s1+= lxs; |
||
326 | s2+= lxs; |
||
327 | } |
||
328 | } |
||
329 | } |
||
330 | |||
331 |