Parent Directory | Revision Log
Revision 1054 - (view) (download)
1 : | edgomez | 851 | /************************************************************************** |
2 : | albeu | 315 | * |
3 : | edgomez | 1054 | * XVID MPEG-4 VIDEO CODEC |
4 : | * - Image management functions - | ||
5 : | albeu | 315 | * |
6 : | edgomez | 1054 | * Copyright(C) 2001-2003 Peter Ross <pross@xvid.org> |
7 : | albeu | 315 | * |
8 : | edgomez | 1054 | * This program is free software ; you can redistribute it and/or modify |
9 : | * it under the terms of the GNU General Public License as published by | ||
10 : | * the Free Software Foundation ; either version 2 of the License, or | ||
11 : | * (at your option) any later version. | ||
12 : | albeu | 315 | * |
13 : | edgomez | 1054 | * This program is distributed in the hope that it will be useful, |
14 : | * but WITHOUT ANY WARRANTY ; without even the implied warranty of | ||
15 : | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 : | * GNU General Public License for more details. | ||
17 : | albeu | 315 | * |
18 : | edgomez | 1054 | * You should have received a copy of the GNU General Public License |
19 : | * along with this program ; if not, write to the Free Software | ||
20 : | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
21 : | albeu | 315 | * |
22 : | edgomez | 1054 | * $Id: image.c,v 1.26.2.7 2003-06-09 13:53:50 edgomez Exp $ |
23 : | albeu | 315 | * |
24 : | edgomez | 1054 | ****************************************************************************/ |
25 : | albeu | 315 | |
26 : | #include <stdlib.h> | ||
27 : | edgomez | 1053 | #include <string.h> /* memcpy, memset */ |
28 : | albeu | 315 | #include <math.h> |
29 : | |||
30 : | #include "../portab.h" | ||
31 : | edgomez | 1053 | #include "../global.h" |
32 : | #include "../xvid.h" | ||
33 : | albeu | 315 | #include "image.h" |
34 : | #include "colorspace.h" | ||
35 : | #include "interpolate8x8.h" | ||
36 : | edgomez | 851 | #include "reduced.h" |
37 : | albeu | 315 | #include "../utils/mem_align.h" |
38 : | |||
39 : | edgomez | 1053 | #include "font.h" |
40 : | edgomez | 851 | |
41 : | albeu | 315 | #define SAFETY 64 |
42 : | #define EDGE_SIZE2 (EDGE_SIZE/2) | ||
43 : | |||
44 : | |||
45 : | int32_t | ||
46 : | image_create(IMAGE * image, | ||
47 : | uint32_t edged_width, | ||
48 : | uint32_t edged_height) | ||
49 : | { | ||
50 : | const uint32_t edged_width2 = edged_width / 2; | ||
51 : | const uint32_t edged_height2 = edged_height / 2; | ||
52 : | uint32_t i; | ||
53 : | |||
54 : | image->y = | ||
55 : | xvid_malloc(edged_width * (edged_height + 1) + SAFETY, CACHE_LINE); | ||
56 : | if (image->y == NULL) { | ||
57 : | return -1; | ||
58 : | } | ||
59 : | |||
60 : | for (i = 0; i < edged_width * edged_height + SAFETY; i++) { | ||
61 : | image->y[i] = 0; | ||
62 : | } | ||
63 : | |||
64 : | image->u = xvid_malloc(edged_width2 * edged_height2 + SAFETY, CACHE_LINE); | ||
65 : | if (image->u == NULL) { | ||
66 : | xvid_free(image->y); | ||
67 : | return -1; | ||
68 : | } | ||
69 : | image->v = xvid_malloc(edged_width2 * edged_height2 + SAFETY, CACHE_LINE); | ||
70 : | if (image->v == NULL) { | ||
71 : | xvid_free(image->u); | ||
72 : | xvid_free(image->y); | ||
73 : | return -1; | ||
74 : | } | ||
75 : | |||
76 : | image->y += EDGE_SIZE * edged_width + EDGE_SIZE; | ||
77 : | image->u += EDGE_SIZE2 * edged_width2 + EDGE_SIZE2; | ||
78 : | image->v += EDGE_SIZE2 * edged_width2 + EDGE_SIZE2; | ||
79 : | |||
80 : | return 0; | ||
81 : | } | ||
82 : | |||
83 : | |||
84 : | |||
85 : | void | ||
86 : | image_destroy(IMAGE * image, | ||
87 : | uint32_t edged_width, | ||
88 : | uint32_t edged_height) | ||
89 : | { | ||
90 : | const uint32_t edged_width2 = edged_width / 2; | ||
91 : | |||
92 : | if (image->y) { | ||
93 : | xvid_free(image->y - (EDGE_SIZE * edged_width + EDGE_SIZE)); | ||
94 : | } | ||
95 : | if (image->u) { | ||
96 : | xvid_free(image->u - (EDGE_SIZE2 * edged_width2 + EDGE_SIZE2)); | ||
97 : | } | ||
98 : | if (image->v) { | ||
99 : | xvid_free(image->v - (EDGE_SIZE2 * edged_width2 + EDGE_SIZE2)); | ||
100 : | } | ||
101 : | } | ||
102 : | |||
103 : | |||
104 : | void | ||
105 : | image_swap(IMAGE * image1, | ||
106 : | IMAGE * image2) | ||
107 : | { | ||
108 : | suxen_drol | 926 | SWAP(uint8_t*, image1->y, image2->y); |
109 : | SWAP(uint8_t*, image1->u, image2->u); | ||
110 : | SWAP(uint8_t*, image1->v, image2->v); | ||
111 : | albeu | 315 | } |
112 : | |||
113 : | |||
114 : | void | ||
115 : | image_copy(IMAGE * image1, | ||
116 : | IMAGE * image2, | ||
117 : | uint32_t edged_width, | ||
118 : | uint32_t height) | ||
119 : | { | ||
120 : | memcpy(image1->y, image2->y, edged_width * height); | ||
121 : | memcpy(image1->u, image2->u, edged_width * height / 4); | ||
122 : | memcpy(image1->v, image2->v, edged_width * height / 4); | ||
123 : | } | ||
124 : | |||
125 : | |||
126 : | void | ||
127 : | image_setedges(IMAGE * image, | ||
128 : | uint32_t edged_width, | ||
129 : | uint32_t edged_height, | ||
130 : | uint32_t width, | ||
131 : | h | 541 | uint32_t height) |
132 : | albeu | 315 | { |
133 : | const uint32_t edged_width2 = edged_width / 2; | ||
134 : | const uint32_t width2 = width / 2; | ||
135 : | uint32_t i; | ||
136 : | uint8_t *dst; | ||
137 : | uint8_t *src; | ||
138 : | |||
139 : | |||
140 : | dst = image->y - (EDGE_SIZE + EDGE_SIZE * edged_width); | ||
141 : | src = image->y; | ||
142 : | |||
143 : | for (i = 0; i < EDGE_SIZE; i++) { | ||
144 : | edgomez | 851 | memset(dst, *src, EDGE_SIZE); |
145 : | memcpy(dst + EDGE_SIZE, src, width); | ||
146 : | memset(dst + edged_width - EDGE_SIZE, *(src + width - 1), | ||
147 : | EDGE_SIZE); | ||
148 : | albeu | 315 | dst += edged_width; |
149 : | } | ||
150 : | |||
151 : | for (i = 0; i < height; i++) { | ||
152 : | memset(dst, *src, EDGE_SIZE); | ||
153 : | memset(dst + edged_width - EDGE_SIZE, src[width - 1], EDGE_SIZE); | ||
154 : | dst += edged_width; | ||
155 : | src += edged_width; | ||
156 : | } | ||
157 : | |||
158 : | src -= edged_width; | ||
159 : | for (i = 0; i < EDGE_SIZE; i++) { | ||
160 : | edgomez | 851 | memset(dst, *src, EDGE_SIZE); |
161 : | memcpy(dst + EDGE_SIZE, src, width); | ||
162 : | memset(dst + edged_width - EDGE_SIZE, *(src + width - 1), | ||
163 : | albeu | 315 | EDGE_SIZE); |
164 : | dst += edged_width; | ||
165 : | } | ||
166 : | |||
167 : | |||
168 : | edgomez | 1053 | /* U */ |
169 : | albeu | 315 | dst = image->u - (EDGE_SIZE2 + EDGE_SIZE2 * edged_width2); |
170 : | src = image->u; | ||
171 : | |||
172 : | for (i = 0; i < EDGE_SIZE2; i++) { | ||
173 : | memset(dst, *src, EDGE_SIZE2); | ||
174 : | memcpy(dst + EDGE_SIZE2, src, width2); | ||
175 : | memset(dst + edged_width2 - EDGE_SIZE2, *(src + width2 - 1), | ||
176 : | EDGE_SIZE2); | ||
177 : | dst += edged_width2; | ||
178 : | } | ||
179 : | |||
180 : | for (i = 0; i < height / 2; i++) { | ||
181 : | memset(dst, *src, EDGE_SIZE2); | ||
182 : | memset(dst + edged_width2 - EDGE_SIZE2, src[width2 - 1], EDGE_SIZE2); | ||
183 : | dst += edged_width2; | ||
184 : | src += edged_width2; | ||
185 : | } | ||
186 : | src -= edged_width2; | ||
187 : | for (i = 0; i < EDGE_SIZE2; i++) { | ||
188 : | memset(dst, *src, EDGE_SIZE2); | ||
189 : | memcpy(dst + EDGE_SIZE2, src, width2); | ||
190 : | memset(dst + edged_width2 - EDGE_SIZE2, *(src + width2 - 1), | ||
191 : | EDGE_SIZE2); | ||
192 : | dst += edged_width2; | ||
193 : | } | ||
194 : | |||
195 : | |||
196 : | edgomez | 1053 | /* V */ |
197 : | albeu | 315 | dst = image->v - (EDGE_SIZE2 + EDGE_SIZE2 * edged_width2); |
198 : | src = image->v; | ||
199 : | |||
200 : | for (i = 0; i < EDGE_SIZE2; i++) { | ||
201 : | memset(dst, *src, EDGE_SIZE2); | ||
202 : | memcpy(dst + EDGE_SIZE2, src, width2); | ||
203 : | memset(dst + edged_width2 - EDGE_SIZE2, *(src + width2 - 1), | ||
204 : | EDGE_SIZE2); | ||
205 : | dst += edged_width2; | ||
206 : | } | ||
207 : | |||
208 : | for (i = 0; i < height / 2; i++) { | ||
209 : | memset(dst, *src, EDGE_SIZE2); | ||
210 : | memset(dst + edged_width2 - EDGE_SIZE2, src[width2 - 1], EDGE_SIZE2); | ||
211 : | dst += edged_width2; | ||
212 : | src += edged_width2; | ||
213 : | } | ||
214 : | src -= edged_width2; | ||
215 : | for (i = 0; i < EDGE_SIZE2; i++) { | ||
216 : | memset(dst, *src, EDGE_SIZE2); | ||
217 : | memcpy(dst + EDGE_SIZE2, src, width2); | ||
218 : | memset(dst + edged_width2 - EDGE_SIZE2, *(src + width2 - 1), | ||
219 : | EDGE_SIZE2); | ||
220 : | dst += edged_width2; | ||
221 : | } | ||
222 : | } | ||
223 : | |||
224 : | edgomez | 1053 | /* bframe encoding requires image-based u,v interpolation */ |
225 : | albeu | 315 | void |
226 : | image_interpolate(const IMAGE * refn, | ||
227 : | IMAGE * refh, | ||
228 : | IMAGE * refv, | ||
229 : | IMAGE * refhv, | ||
230 : | uint32_t edged_width, | ||
231 : | uint32_t edged_height, | ||
232 : | edgomez | 851 | uint32_t quarterpel, |
233 : | albeu | 315 | uint32_t rounding) |
234 : | { | ||
235 : | edgomez | 1053 | const uint32_t offset = EDGE_SIZE2 * (edged_width + 1); /* we only interpolate half of the edge area */ |
236 : | albeu | 315 | const uint32_t stride_add = 7 * edged_width; |
237 : | edgomez | 1053 | #if 0 |
238 : | edgomez | 851 | const uint32_t edged_width2 = edged_width / 2; |
239 : | const uint32_t edged_height2 = edged_height / 2; | ||
240 : | const uint32_t offset2 = EDGE_SIZE2 * (edged_width2 + 1); | ||
241 : | const uint32_t stride_add2 = 7 * edged_width2; | ||
242 : | #endif | ||
243 : | albeu | 315 | uint8_t *n_ptr, *h_ptr, *v_ptr, *hv_ptr; |
244 : | uint32_t x, y; | ||
245 : | |||
246 : | |||
247 : | n_ptr = refn->y; | ||
248 : | h_ptr = refh->y; | ||
249 : | v_ptr = refv->y; | ||
250 : | hv_ptr = refhv->y; | ||
251 : | |||
252 : | n_ptr -= offset; | ||
253 : | h_ptr -= offset; | ||
254 : | v_ptr -= offset; | ||
255 : | hv_ptr -= offset; | ||
256 : | |||
257 : | edgomez | 851 | if(quarterpel) { |
258 : | |||
259 : | for (y = 0; y < (edged_height - EDGE_SIZE); y += 8) { | ||
260 : | for (x = 0; x < (edged_width - EDGE_SIZE); x += 8) { | ||
261 : | interpolate8x8_6tap_lowpass_h(h_ptr, n_ptr, edged_width, rounding); | ||
262 : | interpolate8x8_6tap_lowpass_v(v_ptr, n_ptr, edged_width, rounding); | ||
263 : | albeu | 315 | |
264 : | edgomez | 851 | n_ptr += 8; |
265 : | h_ptr += 8; | ||
266 : | v_ptr += 8; | ||
267 : | } | ||
268 : | |||
269 : | n_ptr += EDGE_SIZE; | ||
270 : | h_ptr += EDGE_SIZE; | ||
271 : | v_ptr += EDGE_SIZE; | ||
272 : | |||
273 : | h_ptr += stride_add; | ||
274 : | v_ptr += stride_add; | ||
275 : | n_ptr += stride_add; | ||
276 : | } | ||
277 : | |||
278 : | h_ptr = refh->y; | ||
279 : | h_ptr -= offset; | ||
280 : | |||
281 : | for (y = 0; y < (edged_height - EDGE_SIZE); y = y + 8) { | ||
282 : | for (x = 0; x < (edged_width - EDGE_SIZE); x = x + 8) { | ||
283 : | interpolate8x8_6tap_lowpass_v(hv_ptr, h_ptr, edged_width, rounding); | ||
284 : | hv_ptr += 8; | ||
285 : | h_ptr += 8; | ||
286 : | } | ||
287 : | |||
288 : | hv_ptr += EDGE_SIZE; | ||
289 : | h_ptr += EDGE_SIZE; | ||
290 : | |||
291 : | hv_ptr += stride_add; | ||
292 : | h_ptr += stride_add; | ||
293 : | } | ||
294 : | } | ||
295 : | else { | ||
296 : | |||
297 : | for (y = 0; y < (edged_height - EDGE_SIZE); y += 8) { | ||
298 : | for (x = 0; x < (edged_width - EDGE_SIZE); x += 8) { | ||
299 : | interpolate8x8_halfpel_h(h_ptr, n_ptr, edged_width, rounding); | ||
300 : | interpolate8x8_halfpel_v(v_ptr, n_ptr, edged_width, rounding); | ||
301 : | interpolate8x8_halfpel_hv(hv_ptr, n_ptr, edged_width, rounding); | ||
302 : | |||
303 : | n_ptr += 8; | ||
304 : | h_ptr += 8; | ||
305 : | v_ptr += 8; | ||
306 : | hv_ptr += 8; | ||
307 : | } | ||
308 : | |||
309 : | h_ptr += EDGE_SIZE; | ||
310 : | v_ptr += EDGE_SIZE; | ||
311 : | hv_ptr += EDGE_SIZE; | ||
312 : | n_ptr += EDGE_SIZE; | ||
313 : | |||
314 : | h_ptr += stride_add; | ||
315 : | v_ptr += stride_add; | ||
316 : | hv_ptr += stride_add; | ||
317 : | n_ptr += stride_add; | ||
318 : | } | ||
319 : | } | ||
320 : | /* | ||
321 : | #ifdef BFRAMES | ||
322 : | n_ptr = refn->u; | ||
323 : | h_ptr = refh->u; | ||
324 : | v_ptr = refv->u; | ||
325 : | hv_ptr = refhv->u; | ||
326 : | |||
327 : | n_ptr -= offset2; | ||
328 : | h_ptr -= offset2; | ||
329 : | v_ptr -= offset2; | ||
330 : | hv_ptr -= offset2; | ||
331 : | |||
332 : | for (y = 0; y < edged_height2; y += 8) { | ||
333 : | for (x = 0; x < edged_width2; x += 8) { | ||
334 : | interpolate8x8_halfpel_h(h_ptr, n_ptr, edged_width2, rounding); | ||
335 : | interpolate8x8_halfpel_v(v_ptr, n_ptr, edged_width2, rounding); | ||
336 : | interpolate8x8_halfpel_hv(hv_ptr, n_ptr, edged_width2, rounding); | ||
337 : | |||
338 : | albeu | 315 | n_ptr += 8; |
339 : | h_ptr += 8; | ||
340 : | v_ptr += 8; | ||
341 : | hv_ptr += 8; | ||
342 : | } | ||
343 : | edgomez | 851 | h_ptr += stride_add2; |
344 : | v_ptr += stride_add2; | ||
345 : | hv_ptr += stride_add2; | ||
346 : | n_ptr += stride_add2; | ||
347 : | albeu | 315 | } |
348 : | |||
349 : | edgomez | 851 | n_ptr = refn->v; |
350 : | h_ptr = refh->v; | ||
351 : | v_ptr = refv->v; | ||
352 : | hv_ptr = refhv->v; | ||
353 : | |||
354 : | n_ptr -= offset2; | ||
355 : | h_ptr -= offset2; | ||
356 : | v_ptr -= offset2; | ||
357 : | hv_ptr -= offset2; | ||
358 : | |||
359 : | for (y = 0; y < edged_height2; y = y + 8) { | ||
360 : | for (x = 0; x < edged_width2; x = x + 8) { | ||
361 : | interpolate8x8_halfpel_h(h_ptr, n_ptr, edged_width2, rounding); | ||
362 : | interpolate8x8_halfpel_v(v_ptr, n_ptr, edged_width2, rounding); | ||
363 : | interpolate8x8_halfpel_hv(hv_ptr, n_ptr, edged_width2, rounding); | ||
364 : | |||
365 : | n_ptr += 8; | ||
366 : | h_ptr += 8; | ||
367 : | v_ptr += 8; | ||
368 : | hv_ptr += 8; | ||
369 : | } | ||
370 : | h_ptr += stride_add2; | ||
371 : | v_ptr += stride_add2; | ||
372 : | hv_ptr += stride_add2; | ||
373 : | n_ptr += stride_add2; | ||
374 : | } | ||
375 : | #endif | ||
376 : | */ | ||
377 : | albeu | 315 | /* |
378 : | interpolate_halfpel_h( | ||
379 : | refh->y - offset, | ||
380 : | refn->y - offset, | ||
381 : | edged_width, edged_height, | ||
382 : | rounding); | ||
383 : | |||
384 : | interpolate_halfpel_v( | ||
385 : | refv->y - offset, | ||
386 : | refn->y - offset, | ||
387 : | edged_width, edged_height, | ||
388 : | rounding); | ||
389 : | |||
390 : | interpolate_halfpel_hv( | ||
391 : | refhv->y - offset, | ||
392 : | refn->y - offset, | ||
393 : | edged_width, edged_height, | ||
394 : | rounding); | ||
395 : | */ | ||
396 : | |||
397 : | /* uv-image-based compensation | ||
398 : | offset = EDGE_SIZE2 * (edged_width / 2 + 1); | ||
399 : | |||
400 : | interpolate_halfpel_h( | ||
401 : | refh->u - offset, | ||
402 : | refn->u - offset, | ||
403 : | edged_width / 2, edged_height / 2, | ||
404 : | rounding); | ||
405 : | |||
406 : | interpolate_halfpel_v( | ||
407 : | refv->u - offset, | ||
408 : | refn->u - offset, | ||
409 : | edged_width / 2, edged_height / 2, | ||
410 : | rounding); | ||
411 : | |||
412 : | interpolate_halfpel_hv( | ||
413 : | refhv->u - offset, | ||
414 : | refn->u - offset, | ||
415 : | edged_width / 2, edged_height / 2, | ||
416 : | rounding); | ||
417 : | |||
418 : | |||
419 : | interpolate_halfpel_h( | ||
420 : | refh->v - offset, | ||
421 : | refn->v - offset, | ||
422 : | edged_width / 2, edged_height / 2, | ||
423 : | rounding); | ||
424 : | |||
425 : | interpolate_halfpel_v( | ||
426 : | refv->v - offset, | ||
427 : | refn->v - offset, | ||
428 : | edged_width / 2, edged_height / 2, | ||
429 : | rounding); | ||
430 : | |||
431 : | interpolate_halfpel_hv( | ||
432 : | refhv->v - offset, | ||
433 : | refn->v - offset, | ||
434 : | edged_width / 2, edged_height / 2, | ||
435 : | rounding); | ||
436 : | */ | ||
437 : | } | ||
438 : | |||
439 : | |||
440 : | edgomez | 851 | /* |
441 : | chroma optimize filter, invented by mf | ||
442 : | a chroma pixel is average from the surrounding pixels, when the | ||
443 : | correpsonding luma pixels are pure black or white. | ||
444 : | */ | ||
445 : | |||
446 : | void | ||
447 : | image_chroma_optimize(IMAGE * img, int width, int height, int edged_width) | ||
448 : | { | ||
449 : | int x,y; | ||
450 : | int pixels = 0; | ||
451 : | |||
452 : | for (y = 1; y < height/2 - 1; y++) | ||
453 : | for (x = 1; x < width/2 - 1; x++) | ||
454 : | { | ||
455 : | #define IS_PURE(a) ((a)<=16||(a)>=235) | ||
456 : | #define IMG_Y(Y,X) img->y[(Y)*edged_width + (X)] | ||
457 : | #define IMG_U(Y,X) img->u[(Y)*edged_width/2 + (X)] | ||
458 : | #define IMG_V(Y,X) img->v[(Y)*edged_width/2 + (X)] | ||
459 : | |||
460 : | if (IS_PURE(IMG_Y(y*2 ,x*2 )) && | ||
461 : | IS_PURE(IMG_Y(y*2 ,x*2+1)) && | ||
462 : | IS_PURE(IMG_Y(y*2+1,x*2 )) && | ||
463 : | IS_PURE(IMG_Y(y*2+1,x*2+1))) | ||
464 : | { | ||
465 : | IMG_U(y,x) = (IMG_U(y,x-1) + IMG_U(y-1, x) + IMG_U(y, x+1) + IMG_U(y+1, x)) / 4; | ||
466 : | IMG_V(y,x) = (IMG_V(y,x-1) + IMG_V(y-1, x) + IMG_V(y, x+1) + IMG_V(y+1, x)) / 4; | ||
467 : | pixels++; | ||
468 : | } | ||
469 : | |||
470 : | #undef IS_PURE | ||
471 : | #undef IMG_Y | ||
472 : | #undef IMG_U | ||
473 : | #undef IMG_V | ||
474 : | } | ||
475 : | |||
476 : | edgomez | 1038 | DPRINTF(XVID_DEBUG_DEBUG,"chroma_optimized_pixels = %i/%i\n", pixels, width*height/4); |
477 : | edgomez | 851 | } |
478 : | |||
479 : | |||
480 : | |||
481 : | |||
482 : | |||
483 : | /* | ||
484 : | perform safe packed colorspace conversion, by splitting | ||
485 : | the image up into an optimized area (pixel width divisible by 16), | ||
486 : | and two unoptimized/plain-c areas (pixel width divisible by 2) | ||
487 : | */ | ||
488 : | |||
489 : | static void | ||
490 : | safe_packed_conv(uint8_t * x_ptr, int x_stride, | ||
491 : | uint8_t * y_ptr, uint8_t * u_ptr, uint8_t * v_ptr, | ||
492 : | int y_stride, int uv_stride, | ||
493 : | int width, int height, int vflip, | ||
494 : | packedFunc * func_opt, packedFunc func_c, int size) | ||
495 : | { | ||
496 : | int width_opt, width_c; | ||
497 : | |||
498 : | if (func_opt != func_c && x_stride < size*((width+15)/16)*16) | ||
499 : | { | ||
500 : | width_opt = width & (~15); | ||
501 : | width_c = width - width_opt; | ||
502 : | } | ||
503 : | else | ||
504 : | { | ||
505 : | width_opt = width; | ||
506 : | width_c = 0; | ||
507 : | } | ||
508 : | |||
509 : | func_opt(x_ptr, x_stride, | ||
510 : | y_ptr, u_ptr, v_ptr, y_stride, uv_stride, | ||
511 : | width_opt, height, vflip); | ||
512 : | |||
513 : | if (width_c) | ||
514 : | { | ||
515 : | func_c(x_ptr + size*width_opt, x_stride, | ||
516 : | y_ptr + width_opt, u_ptr + width_opt/2, v_ptr + width_opt/2, | ||
517 : | y_stride, uv_stride, width_c, height, vflip); | ||
518 : | } | ||
519 : | } | ||
520 : | |||
521 : | |||
522 : | |||
523 : | albeu | 315 | int |
524 : | image_input(IMAGE * image, | ||
525 : | uint32_t width, | ||
526 : | int height, | ||
527 : | uint32_t edged_width, | ||
528 : | suxen_drol | 890 | uint8_t * src[4], |
529 : | int src_stride[4], | ||
530 : | edgomez | 851 | int csp, |
531 : | int interlacing) | ||
532 : | albeu | 315 | { |
533 : | edgomez | 851 | const int edged_width2 = edged_width/2; |
534 : | const int width2 = width/2; | ||
535 : | const int height2 = height/2; | ||
536 : | edgomez | 1053 | #if 0 |
537 : | const int height_signed = (csp & XVID_CSP_VFLIP) ? -height : height; | ||
538 : | #endif | ||
539 : | albeu | 315 | |
540 : | switch (csp & ~XVID_CSP_VFLIP) { | ||
541 : | case XVID_CSP_RGB555: | ||
542 : | edgomez | 851 | safe_packed_conv( |
543 : | suxen_drol | 890 | src[0], src_stride[0], image->y, image->u, image->v, |
544 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
545 : | interlacing?rgb555i_to_yv12 :rgb555_to_yv12, | ||
546 : | interlacing?rgb555i_to_yv12_c:rgb555_to_yv12_c, 2); | ||
547 : | break; | ||
548 : | albeu | 315 | |
549 : | case XVID_CSP_RGB565: | ||
550 : | edgomez | 851 | safe_packed_conv( |
551 : | suxen_drol | 890 | src[0], src_stride[0], image->y, image->u, image->v, |
552 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
553 : | interlacing?rgb565i_to_yv12 :rgb565_to_yv12, | ||
554 : | interlacing?rgb565i_to_yv12_c:rgb565_to_yv12_c, 2); | ||
555 : | break; | ||
556 : | albeu | 315 | |
557 : | |||
558 : | suxen_drol | 890 | case XVID_CSP_BGR: |
559 : | edgomez | 851 | safe_packed_conv( |
560 : | suxen_drol | 890 | src[0], src_stride[0], image->y, image->u, image->v, |
561 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
562 : | interlacing?bgri_to_yv12 :bgr_to_yv12, | ||
563 : | interlacing?bgri_to_yv12_c:bgr_to_yv12_c, 3); | ||
564 : | break; | ||
565 : | albeu | 315 | |
566 : | suxen_drol | 890 | case XVID_CSP_BGRA: |
567 : | edgomez | 851 | safe_packed_conv( |
568 : | suxen_drol | 890 | src[0], src_stride[0], image->y, image->u, image->v, |
569 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
570 : | interlacing?bgrai_to_yv12 :bgra_to_yv12, | ||
571 : | interlacing?bgrai_to_yv12_c:bgra_to_yv12_c, 4); | ||
572 : | break; | ||
573 : | albeu | 315 | |
574 : | edgomez | 851 | case XVID_CSP_ABGR : |
575 : | safe_packed_conv( | ||
576 : | suxen_drol | 890 | src[0], src_stride[0], image->y, image->u, image->v, |
577 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
578 : | interlacing?abgri_to_yv12 :abgr_to_yv12, | ||
579 : | interlacing?abgri_to_yv12_c:abgr_to_yv12_c, 4); | ||
580 : | break; | ||
581 : | albeu | 315 | |
582 : | edgomez | 851 | case XVID_CSP_RGBA : |
583 : | safe_packed_conv( | ||
584 : | suxen_drol | 890 | src[0], src_stride[0], image->y, image->u, image->v, |
585 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
586 : | interlacing?rgbai_to_yv12 :rgba_to_yv12, | ||
587 : | interlacing?rgbai_to_yv12_c:rgba_to_yv12_c, 4); | ||
588 : | break; | ||
589 : | albeu | 315 | |
590 : | case XVID_CSP_YUY2: | ||
591 : | edgomez | 851 | safe_packed_conv( |
592 : | suxen_drol | 890 | src[0], src_stride[0], image->y, image->u, image->v, |
593 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
594 : | interlacing?yuyvi_to_yv12 :yuyv_to_yv12, | ||
595 : | interlacing?yuyvi_to_yv12_c:yuyv_to_yv12_c, 2); | ||
596 : | break; | ||
597 : | albeu | 315 | |
598 : | case XVID_CSP_YVYU: /* u/v swapped */ | ||
599 : | edgomez | 851 | safe_packed_conv( |
600 : | suxen_drol | 890 | src[0], src_stride[0], image->y, image->v, image->y, |
601 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
602 : | interlacing?yuyvi_to_yv12 :yuyv_to_yv12, | ||
603 : | interlacing?yuyvi_to_yv12_c:yuyv_to_yv12_c, 2); | ||
604 : | break; | ||
605 : | albeu | 315 | |
606 : | case XVID_CSP_UYVY: | ||
607 : | edgomez | 851 | safe_packed_conv( |
608 : | suxen_drol | 890 | src[0], src_stride[0], image->y, image->u, image->v, |
609 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
610 : | interlacing?uyvyi_to_yv12 :uyvy_to_yv12, | ||
611 : | interlacing?uyvyi_to_yv12_c:uyvy_to_yv12_c, 2); | ||
612 : | break; | ||
613 : | albeu | 315 | |
614 : | edgomez | 851 | case XVID_CSP_I420: |
615 : | yv12_to_yv12(image->y, image->u, image->v, edged_width, edged_width2, | ||
616 : | suxen_drol | 890 | src[0], src[0] + src_stride[0]*height, src[0] + src_stride[0]*height + (src_stride[0]/2)*height2, |
617 : | src_stride[0], src_stride[0]/2, width, height, (csp & XVID_CSP_VFLIP)); | ||
618 : | edgomez | 851 | break |
619 : | ; | ||
620 : | case XVID_CSP_YV12: /* u/v swapped */ | ||
621 : | yv12_to_yv12(image->y, image->v, image->u, edged_width, edged_width2, | ||
622 : | suxen_drol | 890 | src[0], src[0] + src_stride[0]*height, src[0] + src_stride[0]*height + (src_stride[0]/2)*height2, |
623 : | src_stride[0], src_stride[0]/2, width, height, (csp & XVID_CSP_VFLIP)); | ||
624 : | edgomez | 851 | break; |
625 : | |||
626 : | albeu | 315 | case XVID_CSP_USER: |
627 : | suxen_drol | 890 | /*XXX: support for different u & v strides */ |
628 : | yv12_to_yv12(image->y, image->u, image->v, edged_width, edged_width2, | ||
629 : | src[0], src[1], src[2], src_stride[0], src_stride[1], | ||
630 : | width, height, (csp & XVID_CSP_VFLIP)); | ||
631 : | edgomez | 851 | break; |
632 : | albeu | 315 | |
633 : | case XVID_CSP_NULL: | ||
634 : | break; | ||
635 : | |||
636 : | edgomez | 851 | default : |
637 : | return -1; | ||
638 : | albeu | 315 | } |
639 : | |||
640 : | edgomez | 851 | |
641 : | /* pad out image when the width and/or height is not a multiple of 16 */ | ||
642 : | |||
643 : | if (width & 15) | ||
644 : | { | ||
645 : | int i; | ||
646 : | int pad_width = 16 - (width&15); | ||
647 : | for (i = 0; i < height; i++) | ||
648 : | { | ||
649 : | memset(image->y + i*edged_width + width, | ||
650 : | *(image->y + i*edged_width + width - 1), pad_width); | ||
651 : | } | ||
652 : | for (i = 0; i < height/2; i++) | ||
653 : | { | ||
654 : | memset(image->u + i*edged_width2 + width2, | ||
655 : | *(image->u + i*edged_width2 + width2 - 1),pad_width/2); | ||
656 : | memset(image->v + i*edged_width2 + width2, | ||
657 : | *(image->v + i*edged_width2 + width2 - 1),pad_width/2); | ||
658 : | } | ||
659 : | } | ||
660 : | |||
661 : | if (height & 15) | ||
662 : | { | ||
663 : | int pad_height = 16 - (height&15); | ||
664 : | int length = ((width+15)/16)*16; | ||
665 : | int i; | ||
666 : | for (i = 0; i < pad_height; i++) | ||
667 : | { | ||
668 : | memcpy(image->y + (height+i)*edged_width, | ||
669 : | image->y + (height-1)*edged_width,length); | ||
670 : | } | ||
671 : | |||
672 : | for (i = 0; i < pad_height/2; i++) | ||
673 : | { | ||
674 : | memcpy(image->u + (height2+i)*edged_width2, | ||
675 : | image->u + (height2-1)*edged_width2,length/2); | ||
676 : | memcpy(image->v + (height2+i)*edged_width2, | ||
677 : | image->v + (height2-1)*edged_width2,length/2); | ||
678 : | } | ||
679 : | } | ||
680 : | |||
681 : | /* | ||
682 : | if (interlacing) | ||
683 : | image_printf(image, edged_width, height, 5,5, "[i]"); | ||
684 : | image_dump_yuvpgm(image, edged_width, ((width+15)/16)*16, ((height+15)/16)*16, "\\encode.pgm"); | ||
685 : | */ | ||
686 : | return 0; | ||
687 : | albeu | 315 | } |
688 : | |||
689 : | |||
690 : | |||
691 : | int | ||
692 : | image_output(IMAGE * image, | ||
693 : | uint32_t width, | ||
694 : | int height, | ||
695 : | uint32_t edged_width, | ||
696 : | suxen_drol | 890 | uint8_t * dst[4], |
697 : | uint32_t dst_stride[4], | ||
698 : | edgomez | 851 | int csp, |
699 : | int interlacing) | ||
700 : | albeu | 315 | { |
701 : | edgomez | 851 | const int edged_width2 = edged_width/2; |
702 : | int height2 = height/2; | ||
703 : | |||
704 : | /* | ||
705 : | if (interlacing) | ||
706 : | image_printf(image, edged_width, height, 5,100, "[i]=%i,%i",width,height); | ||
707 : | image_dump_yuvpgm(image, edged_width, width, height, "\\decode.pgm"); | ||
708 : | */ | ||
709 : | |||
710 : | albeu | 315 | switch (csp & ~XVID_CSP_VFLIP) { |
711 : | case XVID_CSP_RGB555: | ||
712 : | edgomez | 851 | safe_packed_conv( |
713 : | suxen_drol | 890 | dst[0], dst_stride[0], image->y, image->u, image->v, |
714 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
715 : | interlacing?yv12_to_rgb555i :yv12_to_rgb555, | ||
716 : | interlacing?yv12_to_rgb555i_c:yv12_to_rgb555_c, 2); | ||
717 : | albeu | 315 | return 0; |
718 : | |||
719 : | case XVID_CSP_RGB565: | ||
720 : | edgomez | 851 | safe_packed_conv( |
721 : | suxen_drol | 890 | dst[0], dst_stride[0], image->y, image->u, image->v, |
722 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
723 : | interlacing?yv12_to_rgb565i :yv12_to_rgb565, | ||
724 : | interlacing?yv12_to_rgb565i_c:yv12_to_rgb565_c, 2); | ||
725 : | albeu | 315 | return 0; |
726 : | |||
727 : | suxen_drol | 890 | case XVID_CSP_BGR: |
728 : | edgomez | 851 | safe_packed_conv( |
729 : | suxen_drol | 890 | dst[0], dst_stride[0], image->y, image->u, image->v, |
730 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
731 : | interlacing?yv12_to_bgri :yv12_to_bgr, | ||
732 : | interlacing?yv12_to_bgri_c:yv12_to_bgr_c, 3); | ||
733 : | albeu | 315 | return 0; |
734 : | |||
735 : | suxen_drol | 890 | case XVID_CSP_BGRA: |
736 : | edgomez | 851 | safe_packed_conv( |
737 : | suxen_drol | 890 | dst[0], dst_stride[0], image->y, image->u, image->v, |
738 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
739 : | interlacing?yv12_to_bgrai :yv12_to_bgra, | ||
740 : | interlacing?yv12_to_bgrai_c:yv12_to_bgra_c, 4); | ||
741 : | albeu | 315 | return 0; |
742 : | |||
743 : | edgomez | 851 | case XVID_CSP_ABGR: |
744 : | safe_packed_conv( | ||
745 : | suxen_drol | 890 | dst[0], dst_stride[0], image->y, image->u, image->v, |
746 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
747 : | interlacing?yv12_to_abgri :yv12_to_abgr, | ||
748 : | interlacing?yv12_to_abgri_c:yv12_to_abgr_c, 4); | ||
749 : | albeu | 315 | return 0; |
750 : | |||
751 : | edgomez | 851 | case XVID_CSP_RGBA: |
752 : | safe_packed_conv( | ||
753 : | suxen_drol | 890 | dst[0], dst_stride[0], image->y, image->u, image->v, |
754 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
755 : | interlacing?yv12_to_rgbai :yv12_to_rgba, | ||
756 : | interlacing?yv12_to_rgbai_c:yv12_to_rgba_c, 4); | ||
757 : | albeu | 315 | return 0; |
758 : | |||
759 : | case XVID_CSP_YUY2: | ||
760 : | edgomez | 851 | safe_packed_conv( |
761 : | suxen_drol | 890 | dst[0], dst_stride[0], image->y, image->u, image->v, |
762 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
763 : | interlacing?yv12_to_yuyvi :yv12_to_yuyv, | ||
764 : | interlacing?yv12_to_yuyvi_c:yv12_to_yuyv_c, 2); | ||
765 : | albeu | 315 | return 0; |
766 : | |||
767 : | edgomez | 1053 | case XVID_CSP_YVYU: /* u,v swapped */ |
768 : | edgomez | 851 | safe_packed_conv( |
769 : | suxen_drol | 890 | dst[0], dst_stride[0], image->y, image->v, image->u, |
770 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
771 : | interlacing?yv12_to_yuyvi :yv12_to_yuyv, | ||
772 : | interlacing?yv12_to_yuyvi_c:yv12_to_yuyv_c, 2); | ||
773 : | albeu | 315 | return 0; |
774 : | |||
775 : | case XVID_CSP_UYVY: | ||
776 : | edgomez | 851 | safe_packed_conv( |
777 : | suxen_drol | 890 | dst[0], dst_stride[0], image->y, image->u, image->v, |
778 : | edgomez | 851 | edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
779 : | interlacing?yv12_to_uyvyi :yv12_to_uyvy, | ||
780 : | interlacing?yv12_to_uyvyi_c:yv12_to_uyvy_c, 2); | ||
781 : | albeu | 315 | return 0; |
782 : | |||
783 : | edgomez | 851 | case XVID_CSP_I420: |
784 : | suxen_drol | 890 | yv12_to_yv12(dst[0], dst[0] + dst_stride[0]*height, dst[0] + dst_stride[0]*height + (dst_stride[0]/2)*height2, |
785 : | dst_stride[0], dst_stride[0]/2, | ||
786 : | edgomez | 851 | image->y, image->u, image->v, edged_width, edged_width2, |
787 : | width, height, (csp & XVID_CSP_VFLIP)); | ||
788 : | return 0; | ||
789 : | |||
790 : | edgomez | 1053 | case XVID_CSP_YV12: /* u,v swapped */ |
791 : | suxen_drol | 890 | yv12_to_yv12(dst[0], dst[0] + dst_stride[0]*height, dst[0] + dst_stride[0]*height + (dst_stride[0]/2)*height2, |
792 : | dst_stride[0], dst_stride[0]/2, | ||
793 : | edgomez | 851 | image->y, image->v, image->u, edged_width, edged_width2, |
794 : | width, height, (csp & XVID_CSP_VFLIP)); | ||
795 : | return 0; | ||
796 : | |||
797 : | edgomez | 1053 | case XVID_CSP_USER : /* u,v swapped */ |
798 : | suxen_drol | 890 | yv12_to_yv12(dst[0], dst[1], dst[2], |
799 : | dst_stride[0], dst_stride[1], /* v: dst_stride[2] */ | ||
800 : | image->y, image->v, image->u, edged_width, edged_width2, | ||
801 : | width, height, (csp & XVID_CSP_VFLIP)); | ||
802 : | albeu | 315 | return 0; |
803 : | |||
804 : | suxen_drol | 890 | case XVID_CSP_INTERNAL : |
805 : | dst[0] = image->y; | ||
806 : | dst[1] = image->u; | ||
807 : | dst[2] = image->v; | ||
808 : | dst_stride[0] = edged_width; | ||
809 : | dst_stride[1] = edged_width/2; | ||
810 : | dst_stride[2] = edged_width/2; | ||
811 : | return 0; | ||
812 : | |||
813 : | albeu | 315 | case XVID_CSP_NULL: |
814 : | suxen_drol | 890 | case XVID_CSP_SLICE: |
815 : | albeu | 315 | return 0; |
816 : | |||
817 : | } | ||
818 : | |||
819 : | return -1; | ||
820 : | } | ||
821 : | |||
822 : | float | ||
823 : | image_psnr(IMAGE * orig_image, | ||
824 : | IMAGE * recon_image, | ||
825 : | uint16_t stride, | ||
826 : | uint16_t width, | ||
827 : | uint16_t height) | ||
828 : | { | ||
829 : | int32_t diff, x, y, quad = 0; | ||
830 : | uint8_t *orig = orig_image->y; | ||
831 : | uint8_t *recon = recon_image->y; | ||
832 : | float psnr_y; | ||
833 : | |||
834 : | for (y = 0; y < height; y++) { | ||
835 : | for (x = 0; x < width; x++) { | ||
836 : | diff = *(orig + x) - *(recon + x); | ||
837 : | quad += diff * diff; | ||
838 : | } | ||
839 : | orig += stride; | ||
840 : | recon += stride; | ||
841 : | } | ||
842 : | |||
843 : | psnr_y = (float) quad / (float) (width * height); | ||
844 : | |||
845 : | if (psnr_y) { | ||
846 : | psnr_y = (float) (255 * 255) / psnr_y; | ||
847 : | psnr_y = 10 * (float) log10(psnr_y); | ||
848 : | } else | ||
849 : | psnr_y = (float) 99.99; | ||
850 : | |||
851 : | return psnr_y; | ||
852 : | } | ||
853 : | |||
854 : | |||
855 : | edgomez | 851 | float sse_to_PSNR(long sse, int pixels) |
856 : | { | ||
857 : | if (sse==0) | ||
858 : | return 99.99F; | ||
859 : | |||
860 : | edgomez | 1053 | return 48.131F - 10*(float)log10((float)sse/(float)(pixels)); /* log10(255*255)=4.8131 */ |
861 : | edgomez | 851 | |
862 : | } | ||
863 : | |||
864 : | long plane_sse(uint8_t * orig, | ||
865 : | uint8_t * recon, | ||
866 : | uint16_t stride, | ||
867 : | uint16_t width, | ||
868 : | uint16_t height) | ||
869 : | { | ||
870 : | int diff, x, y; | ||
871 : | long sse=0; | ||
872 : | |||
873 : | for (y = 0; y < height; y++) { | ||
874 : | for (x = 0; x < width; x++) { | ||
875 : | diff = *(orig + x) - *(recon + x); | ||
876 : | sse += diff * diff; | ||
877 : | } | ||
878 : | orig += stride; | ||
879 : | recon += stride; | ||
880 : | } | ||
881 : | return sse; | ||
882 : | } | ||
883 : | |||
884 : | edgomez | 1053 | #if 0 |
885 : | edgomez | 851 | |
886 : | albeu | 315 | #include <stdio.h> |
887 : | #include <string.h> | ||
888 : | |||
889 : | int image_dump_pgm(uint8_t * bmp, uint32_t width, uint32_t height, char * filename) | ||
890 : | { | ||
891 : | FILE * f; | ||
892 : | char hdr[1024]; | ||
893 : | |||
894 : | f = fopen(filename, "wb"); | ||
895 : | if ( f == NULL) | ||
896 : | { | ||
897 : | return -1; | ||
898 : | } | ||
899 : | sprintf(hdr, "P5\n#xvid\n%i %i\n255\n", width, height); | ||
900 : | fwrite(hdr, strlen(hdr), 1, f); | ||
901 : | fwrite(bmp, width, height, f); | ||
902 : | fclose(f); | ||
903 : | |||
904 : | return 0; | ||
905 : | } | ||
906 : | |||
907 : | |||
908 : | edgomez | 1053 | /* dump image+edges to yuv pgm files */ |
909 : | albeu | 315 | |
910 : | int image_dump(IMAGE * image, uint32_t edged_width, uint32_t edged_height, char * path, int number) | ||
911 : | { | ||
912 : | char filename[1024]; | ||
913 : | |||
914 : | sprintf(filename, "%s_%i_%c.pgm", path, number, 'y'); | ||
915 : | image_dump_pgm( | ||
916 : | image->y - (EDGE_SIZE * edged_width + EDGE_SIZE), | ||
917 : | edged_width, edged_height, filename); | ||
918 : | |||
919 : | sprintf(filename, "%s_%i_%c.pgm", path, number, 'u'); | ||
920 : | image_dump_pgm( | ||
921 : | image->u - (EDGE_SIZE2 * edged_width / 2 + EDGE_SIZE2), | ||
922 : | edged_width / 2, edged_height / 2, filename); | ||
923 : | |||
924 : | sprintf(filename, "%s_%i_%c.pgm", path, number, 'v'); | ||
925 : | image_dump_pgm( | ||
926 : | image->v - (EDGE_SIZE2 * edged_width / 2 + EDGE_SIZE2), | ||
927 : | edged_width / 2, edged_height / 2, filename); | ||
928 : | |||
929 : | return 0; | ||
930 : | } | ||
931 : | edgomez | 1053 | #endif |
932 : | albeu | 315 | |
933 : | |||
934 : | |||
935 : | /* dump image to yuvpgm file */ | ||
936 : | |||
937 : | #include <stdio.h> | ||
938 : | |||
939 : | int | ||
940 : | image_dump_yuvpgm(const IMAGE * image, | ||
941 : | const uint32_t edged_width, | ||
942 : | const uint32_t width, | ||
943 : | const uint32_t height, | ||
944 : | char *filename) | ||
945 : | { | ||
946 : | FILE *f; | ||
947 : | char hdr[1024]; | ||
948 : | uint32_t i; | ||
949 : | uint8_t *bmp1; | ||
950 : | uint8_t *bmp2; | ||
951 : | |||
952 : | |||
953 : | f = fopen(filename, "wb"); | ||
954 : | if (f == NULL) { | ||
955 : | return -1; | ||
956 : | } | ||
957 : | sprintf(hdr, "P5\n#xvid\n%i %i\n255\n", width, (3 * height) / 2); | ||
958 : | fwrite(hdr, strlen(hdr), 1, f); | ||
959 : | |||
960 : | bmp1 = image->y; | ||
961 : | for (i = 0; i < height; i++) { | ||
962 : | fwrite(bmp1, width, 1, f); | ||
963 : | bmp1 += edged_width; | ||
964 : | } | ||
965 : | |||
966 : | bmp1 = image->u; | ||
967 : | bmp2 = image->v; | ||
968 : | for (i = 0; i < height / 2; i++) { | ||
969 : | fwrite(bmp1, width / 2, 1, f); | ||
970 : | fwrite(bmp2, width / 2, 1, f); | ||
971 : | bmp1 += edged_width / 2; | ||
972 : | bmp2 += edged_width / 2; | ||
973 : | } | ||
974 : | |||
975 : | fclose(f); | ||
976 : | return 0; | ||
977 : | } | ||
978 : | |||
979 : | |||
980 : | float | ||
981 : | image_mad(const IMAGE * img1, | ||
982 : | const IMAGE * img2, | ||
983 : | uint32_t stride, | ||
984 : | uint32_t width, | ||
985 : | uint32_t height) | ||
986 : | { | ||
987 : | const uint32_t stride2 = stride / 2; | ||
988 : | const uint32_t width2 = width / 2; | ||
989 : | const uint32_t height2 = height / 2; | ||
990 : | |||
991 : | uint32_t x, y; | ||
992 : | uint32_t sum = 0; | ||
993 : | |||
994 : | for (y = 0; y < height; y++) | ||
995 : | for (x = 0; x < width; x++) | ||
996 : | edgomez | 982 | sum += abs(img1->y[x + y * stride] - img2->y[x + y * stride]); |
997 : | albeu | 315 | |
998 : | for (y = 0; y < height2; y++) | ||
999 : | for (x = 0; x < width2; x++) | ||
1000 : | edgomez | 982 | sum += abs(img1->u[x + y * stride2] - img2->u[x + y * stride2]); |
1001 : | albeu | 315 | |
1002 : | for (y = 0; y < height2; y++) | ||
1003 : | for (x = 0; x < width2; x++) | ||
1004 : | edgomez | 982 | sum += abs(img1->v[x + y * stride2] - img2->v[x + y * stride2]); |
1005 : | albeu | 315 | |
1006 : | return (float) sum / (width * height * 3 / 2); | ||
1007 : | } | ||
1008 : | |||
1009 : | void | ||
1010 : | suxen_drol | 890 | output_slice(IMAGE * cur, int std, int width, xvid_image_t* out_frm, int mbx, int mby,int mbl) { |
1011 : | albeu | 315 | uint8_t *dY,*dU,*dV,*sY,*sU,*sV; |
1012 : | int std2 = std >> 1; | ||
1013 : | int w = mbl << 4, w2,i; | ||
1014 : | |||
1015 : | if(w > width) | ||
1016 : | w = width; | ||
1017 : | w2 = w >> 1; | ||
1018 : | suxen_drol | 323 | |
1019 : | suxen_drol | 890 | dY = (uint8_t*)out_frm->plane[0] + (mby << 4) * out_frm->stride[0] + (mbx << 4); |
1020 : | dU = (uint8_t*)out_frm->plane[1] + (mby << 3) * out_frm->stride[1] + (mbx << 3); | ||
1021 : | dV = (uint8_t*)out_frm->plane[2] + (mby << 3) * out_frm->stride[2] + (mbx << 3); | ||
1022 : | albeu | 315 | sY = cur->y + (mby << 4) * std + (mbx << 4); |
1023 : | sU = cur->u + (mby << 3) * std2 + (mbx << 3); | ||
1024 : | sV = cur->v + (mby << 3) * std2 + (mbx << 3); | ||
1025 : | |||
1026 : | for(i = 0 ; i < 16 ; i++) { | ||
1027 : | memcpy(dY,sY,w); | ||
1028 : | suxen_drol | 890 | dY += out_frm->stride[0]; |
1029 : | suxen_drol | 323 | sY += std; |
1030 : | } | ||
1031 : | albeu | 315 | for(i = 0 ; i < 8 ; i++) { |
1032 : | memcpy(dU,sU,w2); | ||
1033 : | suxen_drol | 890 | dU += out_frm->stride[1]; |
1034 : | albeu | 315 | sU += std2; |
1035 : | } | ||
1036 : | for(i = 0 ; i < 8 ; i++) { | ||
1037 : | memcpy(dV,sV,w2); | ||
1038 : | suxen_drol | 890 | dV += out_frm->stride[2]; |
1039 : | albeu | 315 | sV += std2; |
1040 : | } | ||
1041 : | } | ||
1042 : | edgomez | 851 | |
1043 : | |||
1044 : | void | ||
1045 : | image_clear(IMAGE * img, int width, int height, int edged_width, | ||
1046 : | int y, int u, int v) | ||
1047 : | { | ||
1048 : | uint8_t * p; | ||
1049 : | int i; | ||
1050 : | |||
1051 : | p = img->y; | ||
1052 : | for (i = 0; i < height; i++) { | ||
1053 : | memset(p, y, width); | ||
1054 : | p += edged_width; | ||
1055 : | } | ||
1056 : | |||
1057 : | p = img->u; | ||
1058 : | for (i = 0; i < height/2; i++) { | ||
1059 : | memset(p, u, width/2); | ||
1060 : | p += edged_width/2; | ||
1061 : | } | ||
1062 : | |||
1063 : | p = img->v; | ||
1064 : | for (i = 0; i < height/2; i++) { | ||
1065 : | memset(p, v, width/2); | ||
1066 : | p += edged_width/2; | ||
1067 : | } | ||
1068 : | } | ||
1069 : | |||
1070 : | |||
1071 : | /* reduced resolution deblocking filter | ||
1072 : | block = block size (16=rrv, 8=full resolution) | ||
1073 : | flags = XVID_DEC_YDEBLOCK|XVID_DEC_UVDEBLOCK | ||
1074 : | */ | ||
1075 : | void | ||
1076 : | image_deblock_rrv(IMAGE * img, int edged_width, | ||
1077 : | const MACROBLOCK * mbs, int mb_width, int mb_height, int mb_stride, | ||
1078 : | int block, int flags) | ||
1079 : | { | ||
1080 : | const int edged_width2 = edged_width /2; | ||
1081 : | const int nblocks = block / 8; /* skals code uses 8pixel block uints */ | ||
1082 : | int i,j; | ||
1083 : | |||
1084 : | /* luma: j,i in block units */ | ||
1085 : | suxen_drol | 890 | |
1086 : | edgomez | 851 | for (j = 1; j < mb_height*2; j++) /* horizontal deblocking */ |
1087 : | for (i = 0; i < mb_width*2; i++) | ||
1088 : | { | ||
1089 : | if (mbs[(j-1)/2*mb_stride + (i/2)].mode != MODE_NOT_CODED || | ||
1090 : | mbs[(j+0)/2*mb_stride + (i/2)].mode != MODE_NOT_CODED) | ||
1091 : | { | ||
1092 : | hfilter_31(img->y + (j*block - 1)*edged_width + i*block, | ||
1093 : | img->y + (j*block + 0)*edged_width + i*block, nblocks); | ||
1094 : | } | ||
1095 : | } | ||
1096 : | |||
1097 : | for (j = 0; j < mb_height*2; j++) /* vertical deblocking */ | ||
1098 : | for (i = 1; i < mb_width*2; i++) | ||
1099 : | { | ||
1100 : | if (mbs[(j/2)*mb_stride + (i-1)/2].mode != MODE_NOT_CODED || | ||
1101 : | mbs[(j/2)*mb_stride + (i+0)/2].mode != MODE_NOT_CODED) | ||
1102 : | { | ||
1103 : | vfilter_31(img->y + (j*block)*edged_width + i*block - 1, | ||
1104 : | img->y + (j*block)*edged_width + i*block + 0, | ||
1105 : | edged_width, nblocks); | ||
1106 : | } | ||
1107 : | } | ||
1108 : | |||
1109 : | |||
1110 : | suxen_drol | 890 | |
1111 : | edgomez | 851 | /* chroma */ |
1112 : | suxen_drol | 890 | |
1113 : | edgomez | 851 | for (j = 1; j < mb_height; j++) /* horizontal deblocking */ |
1114 : | for (i = 0; i < mb_width; i++) | ||
1115 : | { | ||
1116 : | if (mbs[(j-1)*mb_stride + i].mode != MODE_NOT_CODED || | ||
1117 : | mbs[(j+0)*mb_stride + i].mode != MODE_NOT_CODED) | ||
1118 : | { | ||
1119 : | hfilter_31(img->u + (j*block - 1)*edged_width2 + i*block, | ||
1120 : | img->u + (j*block + 0)*edged_width2 + i*block, nblocks); | ||
1121 : | hfilter_31(img->v + (j*block - 1)*edged_width2 + i*block, | ||
1122 : | img->v + (j*block + 0)*edged_width2 + i*block, nblocks); | ||
1123 : | } | ||
1124 : | } | ||
1125 : | |||
1126 : | for (j = 0; j < mb_height; j++) /* vertical deblocking */ | ||
1127 : | for (i = 1; i < mb_width; i++) | ||
1128 : | { | ||
1129 : | if (mbs[j*mb_stride + i - 1].mode != MODE_NOT_CODED || | ||
1130 : | mbs[j*mb_stride + i + 0].mode != MODE_NOT_CODED) | ||
1131 : | { | ||
1132 : | vfilter_31(img->u + (j*block)*edged_width2 + i*block - 1, | ||
1133 : | img->u + (j*block)*edged_width2 + i*block + 0, | ||
1134 : | edged_width2, nblocks); | ||
1135 : | vfilter_31(img->v + (j*block)*edged_width2 + i*block - 1, | ||
1136 : | img->v + (j*block)*edged_width2 + i*block + 0, | ||
1137 : | edged_width2, nblocks); | ||
1138 : | } | ||
1139 : | } | ||
1140 : | |||
1141 : | suxen_drol | 890 | |
1142 : | edgomez | 851 | } |
1143 : |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |