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