1 |
/***************************************************************************** |
/************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - image module - |
* - Image management functions - |
5 |
* |
* |
6 |
* Copyright(C) 2002 Peter Ross <pross@xvid.org> |
* Copyright(C) 2001-2003 Peter Ross <pross@xvid.org> |
7 |
* |
* |
8 |
* This file is part of XviD, a free MPEG-4 video encoder/decoder |
* 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 |
|
* XviD is free software; you can redistribute it and/or modify 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 |
* the Free Software Foundation; either version 2 of the License, or |
11 |
* (at your option) any later version. |
* (at your option) any later version. |
12 |
* |
* |
19 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
* |
* |
|
* Under section 8 of the GNU General Public License, the copyright |
|
|
* holders of XVID explicitly forbid distribution in the following |
|
|
* countries: |
|
|
* |
|
|
* - Japan |
|
|
* - United States of America |
|
|
* |
|
|
* Linking XviD statically or dynamically with other modules is making a |
|
|
* combined work based on XviD. Thus, the terms and conditions of the |
|
|
* GNU General Public License cover the whole combination. |
|
|
* |
|
|
* As a special exception, the copyright holders of XviD give you |
|
|
* permission to link XviD with independent modules that communicate with |
|
|
* XviD solely through the VFW1.1 and DShow interfaces, regardless of the |
|
|
* license terms of these independent modules, and to copy and distribute |
|
|
* the resulting combined work under terms of your choice, provided that |
|
|
* every copy of the combined work is accompanied by a complete copy of |
|
|
* the source code of XviD (the version of XviD used to produce the |
|
|
* combined work), being distributed under the terms of the GNU General |
|
|
* Public License plus this exception. An independent module is a module |
|
|
* which is not derived from or based on XviD. |
|
|
* |
|
|
* Note that people who make modified versions of XviD are not obligated |
|
|
* to grant this special exception for their modified versions; it is |
|
|
* their choice whether to do so. The GNU General Public License gives |
|
|
* permission to release a modified version without this exception; this |
|
|
* exception also makes it possible to release a modified version which |
|
|
* carries forward this exception. |
|
|
* |
|
22 |
* $Id$ |
* $Id$ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
28 |
#include <math.h> |
#include <math.h> |
29 |
|
|
30 |
#include "../portab.h" |
#include "../portab.h" |
31 |
|
#include "../global.h" /* XVID_CSP_XXX's */ |
32 |
#include "../xvid.h" /* XVID_CSP_XXX's */ |
#include "../xvid.h" /* XVID_CSP_XXX's */ |
33 |
#include "image.h" |
#include "image.h" |
34 |
#include "colorspace.h" |
#include "colorspace.h" |
35 |
#include "interpolate8x8.h" |
#include "interpolate8x8.h" |
36 |
#include "../divx4.h" |
#include "reduced.h" |
37 |
#include "../utils/mem_align.h" |
#include "../utils/mem_align.h" |
38 |
|
|
39 |
|
#include "font.h" /* XXX: remove later */ |
40 |
|
|
41 |
#define SAFETY 64 |
#define SAFETY 64 |
42 |
#define EDGE_SIZE2 (EDGE_SIZE/2) |
#define EDGE_SIZE2 (EDGE_SIZE/2) |
43 |
|
|
105 |
image_swap(IMAGE * image1, |
image_swap(IMAGE * image1, |
106 |
IMAGE * image2) |
IMAGE * image2) |
107 |
{ |
{ |
108 |
uint8_t *tmp; |
SWAP(uint8_t*, image1->y, image2->y); |
109 |
|
SWAP(uint8_t*, image1->u, image2->u); |
110 |
tmp = image1->y; |
SWAP(uint8_t*, image1->v, image2->v); |
|
image1->y = image2->y; |
|
|
image2->y = tmp; |
|
|
|
|
|
tmp = image1->u; |
|
|
image1->u = image2->u; |
|
|
image2->u = tmp; |
|
|
|
|
|
tmp = image1->v; |
|
|
image1->v = image2->v; |
|
|
image2->v = tmp; |
|
111 |
} |
} |
112 |
|
|
113 |
|
|
229 |
IMAGE * refhv, |
IMAGE * refhv, |
230 |
uint32_t edged_width, |
uint32_t edged_width, |
231 |
uint32_t edged_height, |
uint32_t edged_height, |
232 |
|
uint32_t quarterpel, |
233 |
uint32_t rounding) |
uint32_t rounding) |
234 |
{ |
{ |
235 |
const uint32_t offset = EDGE_SIZE * (edged_width + 1); |
const uint32_t offset = EDGE_SIZE2 * (edged_width + 1); /* we only interpolate half of the edge area */ |
236 |
const uint32_t stride_add = 7 * edged_width; |
const uint32_t stride_add = 7 * edged_width; |
237 |
|
#if 0 |
238 |
|
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 |
uint8_t *n_ptr, *h_ptr, *v_ptr, *hv_ptr; |
uint8_t *n_ptr, *h_ptr, *v_ptr, *hv_ptr; |
244 |
uint32_t x, y; |
uint32_t x, y; |
245 |
|
|
254 |
v_ptr -= offset; |
v_ptr -= offset; |
255 |
hv_ptr -= offset; |
hv_ptr -= offset; |
256 |
|
|
257 |
for (y = 0; y < edged_height; y = y + 8) { |
if(quarterpel) { |
258 |
for (x = 0; x < edged_width; x = x + 8) { |
|
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 |
|
|
264 |
|
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); |
interpolate8x8_halfpel_h(h_ptr, n_ptr, edged_width, rounding); |
300 |
interpolate8x8_halfpel_v(v_ptr, n_ptr, edged_width, rounding); |
interpolate8x8_halfpel_v(v_ptr, n_ptr, edged_width, rounding); |
301 |
interpolate8x8_halfpel_hv(hv_ptr, n_ptr, edged_width, rounding); |
interpolate8x8_halfpel_hv(hv_ptr, n_ptr, edged_width, rounding); |
305 |
v_ptr += 8; |
v_ptr += 8; |
306 |
hv_ptr += 8; |
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; |
h_ptr += stride_add; |
315 |
v_ptr += stride_add; |
v_ptr += stride_add; |
316 |
hv_ptr += stride_add; |
hv_ptr += stride_add; |
317 |
n_ptr += stride_add; |
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 |
|
n_ptr += 8; |
339 |
|
h_ptr += 8; |
340 |
|
v_ptr += 8; |
341 |
|
hv_ptr += 8; |
342 |
|
} |
343 |
|
h_ptr += stride_add2; |
344 |
|
v_ptr += stride_add2; |
345 |
|
hv_ptr += stride_add2; |
346 |
|
n_ptr += stride_add2; |
347 |
|
} |
348 |
|
|
349 |
|
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 |
/* |
/* |
378 |
interpolate_halfpel_h( |
interpolate_halfpel_h( |
379 |
refh->y - offset, |
refh->y - offset, |
437 |
} |
} |
438 |
|
|
439 |
|
|
440 |
|
/* |
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 |
|
DPRINTF(XVID_DEBUG_DEBUG,"chroma_optimized_pixels = %i/%i\n", pixels, width*height/4); |
477 |
|
} |
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 |
int |
int |
524 |
image_input(IMAGE * image, |
image_input(IMAGE * image, |
525 |
uint32_t width, |
uint32_t width, |
526 |
int height, |
int height, |
527 |
uint32_t edged_width, |
uint32_t edged_width, |
528 |
uint8_t * src, |
uint8_t * src[4], |
529 |
int csp) |
int src_stride[4], |
530 |
{ |
int csp, |
531 |
|
int interlacing) |
|
/* if (csp & XVID_CSP_VFLIP) |
|
532 |
{ |
{ |
533 |
height = -height; |
const int edged_width2 = edged_width/2; |
534 |
} |
const int width2 = width/2; |
535 |
*/ |
const int height2 = height/2; |
536 |
|
#if 0 |
537 |
|
const int height_signed = (csp & XVID_CSP_VFLIP) ? -height : height; |
538 |
|
#endif |
539 |
|
|
540 |
switch (csp & ~XVID_CSP_VFLIP) { |
switch (csp & ~XVID_CSP_VFLIP) { |
541 |
case XVID_CSP_RGB555: |
case XVID_CSP_RGB555: |
542 |
rgb555_to_yv12(image->y, image->u, image->v, src, width, height, |
safe_packed_conv( |
543 |
edged_width); |
src[0], src_stride[0], image->y, image->u, image->v, |
544 |
return 0; |
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 |
|
|
549 |
case XVID_CSP_RGB565: |
case XVID_CSP_RGB565: |
550 |
rgb565_to_yv12(image->y, image->u, image->v, src, width, height, |
safe_packed_conv( |
551 |
edged_width); |
src[0], src_stride[0], image->y, image->u, image->v, |
552 |
return 0; |
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 |
|
|
557 |
|
|
558 |
case XVID_CSP_RGB24: |
case XVID_CSP_BGR: |
559 |
rgb24_to_yv12(image->y, image->u, image->v, src, width, height, |
safe_packed_conv( |
560 |
edged_width); |
src[0], src_stride[0], image->y, image->u, image->v, |
561 |
return 0; |
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 |
|
|
566 |
case XVID_CSP_RGB32: |
case XVID_CSP_BGRA: |
567 |
rgb32_to_yv12(image->y, image->u, image->v, src, width, height, |
safe_packed_conv( |
568 |
edged_width); |
src[0], src_stride[0], image->y, image->u, image->v, |
569 |
return 0; |
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 |
|
|
574 |
case XVID_CSP_I420: |
case XVID_CSP_ABGR : |
575 |
yuv_to_yv12(image->y, image->u, image->v, src, width, height, |
safe_packed_conv( |
576 |
edged_width); |
src[0], src_stride[0], image->y, image->u, image->v, |
577 |
return 0; |
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 |
|
|
582 |
case XVID_CSP_YV12: /* u/v swapped */ |
case XVID_CSP_RGBA : |
583 |
yuv_to_yv12(image->y, image->v, image->u, src, width, height, |
safe_packed_conv( |
584 |
edged_width); |
src[0], src_stride[0], image->y, image->u, image->v, |
585 |
return 0; |
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 |
|
|
590 |
case XVID_CSP_YUY2: |
case XVID_CSP_YUY2: |
591 |
yuyv_to_yv12(image->y, image->u, image->v, src, width, height, |
safe_packed_conv( |
592 |
edged_width); |
src[0], src_stride[0], image->y, image->u, image->v, |
593 |
return 0; |
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 |
|
|
598 |
case XVID_CSP_YVYU: /* u/v swapped */ |
case XVID_CSP_YVYU: /* u/v swapped */ |
599 |
yuyv_to_yv12(image->y, image->v, image->u, src, width, height, |
safe_packed_conv( |
600 |
edged_width); |
src[0], src_stride[0], image->y, image->v, image->y, |
601 |
return 0; |
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 |
|
|
606 |
case XVID_CSP_UYVY: |
case XVID_CSP_UYVY: |
607 |
uyvy_to_yv12(image->y, image->u, image->v, src, width, height, |
safe_packed_conv( |
608 |
edged_width); |
src[0], src_stride[0], image->y, image->u, image->v, |
609 |
return 0; |
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 |
|
|
614 |
|
case XVID_CSP_I420: |
615 |
|
yv12_to_yv12(image->y, image->u, image->v, edged_width, edged_width2, |
616 |
|
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 |
|
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 |
|
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 |
|
break; |
625 |
|
|
626 |
case XVID_CSP_USER: |
case XVID_CSP_USER: |
627 |
user_to_yuv_c(image->y, image->u, image->v, edged_width, |
/*XXX: support for different u & v strides */ |
628 |
(DEC_PICTURE *) src, width, height); |
yv12_to_yv12(image->y, image->u, image->v, edged_width, edged_width2, |
629 |
return 0; |
src[0], src[1], src[2], src_stride[0], src_stride[1], |
630 |
|
width, height, (csp & XVID_CSP_VFLIP)); |
631 |
|
break; |
632 |
|
|
633 |
case XVID_CSP_NULL: |
case XVID_CSP_NULL: |
634 |
break; |
break; |
635 |
|
|
636 |
|
default : |
637 |
|
return -1; |
638 |
} |
} |
639 |
|
|
640 |
return -1; |
|
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 |
} |
} |
688 |
|
|
689 |
|
|
693 |
uint32_t width, |
uint32_t width, |
694 |
int height, |
int height, |
695 |
uint32_t edged_width, |
uint32_t edged_width, |
696 |
uint8_t * dst, |
uint8_t * dst[4], |
697 |
uint32_t dst_stride, |
uint32_t dst_stride[4], |
698 |
int csp) |
int csp, |
699 |
|
int interlacing) |
700 |
{ |
{ |
701 |
if (csp & XVID_CSP_VFLIP) { |
const int edged_width2 = edged_width/2; |
702 |
height = -height; |
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 |
switch (csp & ~XVID_CSP_VFLIP) { |
switch (csp & ~XVID_CSP_VFLIP) { |
711 |
case XVID_CSP_RGB555: |
case XVID_CSP_RGB555: |
712 |
yv12_to_rgb555(dst, dst_stride, image->y, image->u, image->v, |
safe_packed_conv( |
713 |
edged_width, edged_width / 2, width, height); |
dst[0], dst_stride[0], image->y, image->u, image->v, |
714 |
|
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 |
return 0; |
return 0; |
718 |
|
|
719 |
case XVID_CSP_RGB565: |
case XVID_CSP_RGB565: |
720 |
yv12_to_rgb565(dst, dst_stride, image->y, image->u, image->v, |
safe_packed_conv( |
721 |
edged_width, edged_width / 2, width, height); |
dst[0], dst_stride[0], image->y, image->u, image->v, |
722 |
return 0; |
edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
723 |
|
interlacing?yv12_to_rgb565i :yv12_to_rgb565, |
724 |
case XVID_CSP_RGB24: |
interlacing?yv12_to_rgb565i_c:yv12_to_rgb565_c, 2); |
725 |
yv12_to_rgb24(dst, dst_stride, image->y, image->u, image->v, |
return 0; |
726 |
edged_width, edged_width / 2, width, height); |
|
727 |
return 0; |
case XVID_CSP_BGR: |
728 |
|
safe_packed_conv( |
729 |
case XVID_CSP_RGB32: |
dst[0], dst_stride[0], image->y, image->u, image->v, |
730 |
yv12_to_rgb32(dst, dst_stride, image->y, image->u, image->v, |
edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
731 |
edged_width, edged_width / 2, width, height); |
interlacing?yv12_to_bgri :yv12_to_bgr, |
732 |
return 0; |
interlacing?yv12_to_bgri_c:yv12_to_bgr_c, 3); |
733 |
|
return 0; |
734 |
case XVID_CSP_I420: |
|
735 |
yv12_to_yuv(dst, dst_stride, image->y, image->u, image->v, edged_width, |
case XVID_CSP_BGRA: |
736 |
edged_width / 2, width, height); |
safe_packed_conv( |
737 |
return 0; |
dst[0], dst_stride[0], image->y, image->u, image->v, |
738 |
|
edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
739 |
case XVID_CSP_YV12: /* u,v swapped */ |
interlacing?yv12_to_bgrai :yv12_to_bgra, |
740 |
yv12_to_yuv(dst, dst_stride, image->y, image->v, image->u, edged_width, |
interlacing?yv12_to_bgrai_c:yv12_to_bgra_c, 4); |
741 |
edged_width / 2, width, height); |
return 0; |
742 |
|
|
743 |
|
case XVID_CSP_ABGR: |
744 |
|
safe_packed_conv( |
745 |
|
dst[0], dst_stride[0], image->y, image->u, image->v, |
746 |
|
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 |
|
return 0; |
750 |
|
|
751 |
|
case XVID_CSP_RGBA: |
752 |
|
safe_packed_conv( |
753 |
|
dst[0], dst_stride[0], image->y, image->u, image->v, |
754 |
|
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 |
return 0; |
return 0; |
758 |
|
|
759 |
case XVID_CSP_YUY2: |
case XVID_CSP_YUY2: |
760 |
yv12_to_yuyv(dst, dst_stride, image->y, image->u, image->v, |
safe_packed_conv( |
761 |
edged_width, edged_width / 2, width, height); |
dst[0], dst_stride[0], image->y, image->u, image->v, |
762 |
|
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 |
return 0; |
return 0; |
766 |
|
|
767 |
case XVID_CSP_YVYU: /* u,v swapped */ |
case XVID_CSP_YVYU: /* u,v swapped */ |
768 |
yv12_to_yuyv(dst, dst_stride, image->y, image->v, image->u, |
safe_packed_conv( |
769 |
edged_width, edged_width / 2, width, height); |
dst[0], dst_stride[0], image->y, image->v, image->u, |
770 |
|
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 |
return 0; |
return 0; |
774 |
|
|
775 |
case XVID_CSP_UYVY: |
case XVID_CSP_UYVY: |
776 |
yv12_to_uyvy(dst, dst_stride, image->y, image->u, image->v, |
safe_packed_conv( |
777 |
edged_width, edged_width / 2, width, height); |
dst[0], dst_stride[0], image->y, image->u, image->v, |
778 |
|
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 |
return 0; |
return 0; |
782 |
|
|
783 |
case XVID_CSP_USER: |
case XVID_CSP_I420: |
784 |
((DEC_PICTURE *) dst)->y = image->y; |
yv12_to_yv12(dst[0], dst[0] + dst_stride[0]*height, dst[0] + dst_stride[0]*height + (dst_stride[0]/2)*height2, |
785 |
((DEC_PICTURE *) dst)->u = image->u; |
dst_stride[0], dst_stride[0]/2, |
786 |
((DEC_PICTURE *) dst)->v = image->v; |
image->y, image->u, image->v, edged_width, edged_width2, |
787 |
((DEC_PICTURE *) dst)->stride_y = edged_width; |
width, height, (csp & XVID_CSP_VFLIP)); |
788 |
((DEC_PICTURE *) dst)->stride_uv = edged_width / 2; |
return 0; |
789 |
|
|
790 |
|
case XVID_CSP_YV12: /* u,v swapped */ |
791 |
|
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 |
|
image->y, image->v, image->u, edged_width, edged_width2, |
794 |
|
width, height, (csp & XVID_CSP_VFLIP)); |
795 |
|
return 0; |
796 |
|
|
797 |
|
case XVID_CSP_USER : /* u,v swapped */ |
798 |
|
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 |
|
return 0; |
803 |
|
|
804 |
|
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; |
return 0; |
812 |
|
|
813 |
case XVID_CSP_NULL: |
case XVID_CSP_NULL: |
814 |
case XVID_CSP_EXTERN: |
case XVID_CSP_SLICE: |
815 |
return 0; |
return 0; |
816 |
|
|
817 |
} |
} |
851 |
return psnr_y; |
return psnr_y; |
852 |
} |
} |
853 |
|
|
854 |
|
|
855 |
|
float sse_to_PSNR(long sse, int pixels) |
856 |
|
{ |
857 |
|
if (sse==0) |
858 |
|
return 99.99F; |
859 |
|
|
860 |
|
return 48.131F - 10*(float)log10((float)sse/(float)(pixels)); /* log10(255*255)=4.8131 */ |
861 |
|
|
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 |
#if 0 |
#if 0 |
885 |
|
|
886 |
#include <stdio.h> |
#include <stdio.h> |
977 |
} |
} |
978 |
|
|
979 |
|
|
|
#define ABS(X) (((X)>0)?(X):-(X)) |
|
980 |
float |
float |
981 |
image_mad(const IMAGE * img1, |
image_mad(const IMAGE * img1, |
982 |
const IMAGE * img2, |
const IMAGE * img2, |
993 |
|
|
994 |
for (y = 0; y < height; y++) |
for (y = 0; y < height; y++) |
995 |
for (x = 0; x < width; x++) |
for (x = 0; x < width; x++) |
996 |
sum += ABS(img1->y[x + y * stride] - img2->y[x + y * stride]); |
sum += abs(img1->y[x + y * stride] - img2->y[x + y * stride]); |
997 |
|
|
998 |
for (y = 0; y < height2; y++) |
for (y = 0; y < height2; y++) |
999 |
for (x = 0; x < width2; x++) |
for (x = 0; x < width2; x++) |
1000 |
sum += ABS(img1->u[x + y * stride2] - img2->u[x + y * stride2]); |
sum += abs(img1->u[x + y * stride2] - img2->u[x + y * stride2]); |
1001 |
|
|
1002 |
for (y = 0; y < height2; y++) |
for (y = 0; y < height2; y++) |
1003 |
for (x = 0; x < width2; x++) |
for (x = 0; x < width2; x++) |
1004 |
sum += ABS(img1->v[x + y * stride2] - img2->v[x + y * stride2]); |
sum += abs(img1->v[x + y * stride2] - img2->v[x + y * stride2]); |
1005 |
|
|
1006 |
return (float) sum / (width * height * 3 / 2); |
return (float) sum / (width * height * 3 / 2); |
1007 |
} |
} |
1008 |
|
|
1009 |
void |
void |
1010 |
output_slice(IMAGE * cur, int std, int width, XVID_DEC_PICTURE* out_frm, int mbx, int mby,int mbl) { |
output_slice(IMAGE * cur, int std, int width, xvid_image_t* out_frm, int mbx, int mby,int mbl) { |
1011 |
uint8_t *dY,*dU,*dV,*sY,*sU,*sV; |
uint8_t *dY,*dU,*dV,*sY,*sU,*sV; |
1012 |
int std2 = std >> 1; |
int std2 = std >> 1; |
1013 |
int w = mbl << 4, w2,i; |
int w = mbl << 4, w2,i; |
1016 |
w = width; |
w = width; |
1017 |
w2 = w >> 1; |
w2 = w >> 1; |
1018 |
|
|
1019 |
dY = (uint8_t*)out_frm->y + (mby << 4) * out_frm->stride_y + (mbx << 4); |
dY = (uint8_t*)out_frm->plane[0] + (mby << 4) * out_frm->stride[0] + (mbx << 4); |
1020 |
dU = (uint8_t*)out_frm->u + (mby << 3) * out_frm->stride_u + (mbx << 3); |
dU = (uint8_t*)out_frm->plane[1] + (mby << 3) * out_frm->stride[1] + (mbx << 3); |
1021 |
dV = (uint8_t*)out_frm->v + (mby << 3) * out_frm->stride_v + (mbx << 3); |
dV = (uint8_t*)out_frm->plane[2] + (mby << 3) * out_frm->stride[2] + (mbx << 3); |
1022 |
sY = cur->y + (mby << 4) * std + (mbx << 4); |
sY = cur->y + (mby << 4) * std + (mbx << 4); |
1023 |
sU = cur->u + (mby << 3) * std2 + (mbx << 3); |
sU = cur->u + (mby << 3) * std2 + (mbx << 3); |
1024 |
sV = cur->v + (mby << 3) * std2 + (mbx << 3); |
sV = cur->v + (mby << 3) * std2 + (mbx << 3); |
1025 |
|
|
1026 |
for(i = 0 ; i < 16 ; i++) { |
for(i = 0 ; i < 16 ; i++) { |
1027 |
memcpy(dY,sY,w); |
memcpy(dY,sY,w); |
1028 |
dY += out_frm->stride_y; |
dY += out_frm->stride[0]; |
1029 |
sY += std; |
sY += std; |
1030 |
} |
} |
1031 |
for(i = 0 ; i < 8 ; i++) { |
for(i = 0 ; i < 8 ; i++) { |
1032 |
memcpy(dU,sU,w2); |
memcpy(dU,sU,w2); |
1033 |
dU += out_frm->stride_u; |
dU += out_frm->stride[1]; |
1034 |
sU += std2; |
sU += std2; |
1035 |
} |
} |
1036 |
for(i = 0 ; i < 8 ; i++) { |
for(i = 0 ; i < 8 ; i++) { |
1037 |
memcpy(dV,sV,w2); |
memcpy(dV,sV,w2); |
1038 |
dV += out_frm->stride_v; |
dV += out_frm->stride[2]; |
1039 |
sV += std2; |
sV += std2; |
1040 |
} |
} |
1041 |
} |
} |
1042 |
|
|
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 |
|
|
1086 |
|
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 |
|
|
1111 |
|
/* chroma */ |
1112 |
|
|
1113 |
|
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 |
|
|
1142 |
|
} |
1143 |
|
|