3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* image stuff |
* image stuff |
5 |
* |
* |
6 |
|
* 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 |
|
* |
15 |
* This program is free software; you can redistribute it and/or modify |
* 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 |
* 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 |
* the Free Software Foundation; either version 2 of the License, or |
32 |
* |
* |
33 |
* History: |
* History: |
34 |
* |
* |
35 |
|
* 05.10.2002 support for interpolated images in qpel mode - Isibaar |
36 |
* 01.05.2002 BFRAME image-based u,v interpolation |
* 01.05.2002 BFRAME image-based u,v interpolation |
37 |
* 22.04.2002 added some B-frame support |
* 22.04.2002 added some B-frame support |
38 |
* 14.04.2002 added image_dump_yuvpgm(), added image_mad() |
* 14.04.2002 added image_dump_yuvpgm(), added image_mad() |
39 |
* XVID_CSP_USER input support |
* XVID_CSP_USER input support |
40 |
* 09.04.2002 PSNR calculations |
* 09.04.2002 PSNR calculations - Isibaar |
41 |
* 06.04.2002 removed interlaced edging from U,V blocks (as per spec) |
* 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) |
* 26.03.2002 interlacing support (field-based edging in set_edges) |
43 |
* 26.01.2002 rgb555, rgb565 |
* 26.01.2002 rgb555, rgb565 |
54 |
#include <math.h> |
#include <math.h> |
55 |
|
|
56 |
#include "../portab.h" |
#include "../portab.h" |
57 |
|
#include "../global.h" // XVID_CSP_XXX's |
58 |
#include "../xvid.h" // XVID_CSP_XXX's |
#include "../xvid.h" // XVID_CSP_XXX's |
59 |
#include "image.h" |
#include "image.h" |
60 |
#include "colorspace.h" |
#include "colorspace.h" |
61 |
#include "interpolate8x8.h" |
#include "interpolate8x8.h" |
62 |
#include "../divx4.h" |
#include "reduced.h" |
63 |
#include "../utils/mem_align.h" |
#include "../utils/mem_align.h" |
64 |
|
|
65 |
|
#include "font.h" // XXX: remove later |
66 |
|
|
67 |
#define SAFETY 64 |
#define SAFETY 64 |
68 |
#define EDGE_SIZE2 (EDGE_SIZE/2) |
#define EDGE_SIZE2 (EDGE_SIZE/2) |
69 |
|
|
131 |
image_swap(IMAGE * image1, |
image_swap(IMAGE * image1, |
132 |
IMAGE * image2) |
IMAGE * image2) |
133 |
{ |
{ |
134 |
uint8_t *tmp; |
SWAP(uint8_t*, image1->y, image2->y); |
135 |
|
SWAP(uint8_t*, image1->u, image2->u); |
136 |
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; |
|
137 |
} |
} |
138 |
|
|
139 |
|
|
154 |
uint32_t edged_width, |
uint32_t edged_width, |
155 |
uint32_t edged_height, |
uint32_t edged_height, |
156 |
uint32_t width, |
uint32_t width, |
157 |
uint32_t height, |
uint32_t height) |
|
uint32_t interlacing) |
|
158 |
{ |
{ |
159 |
const uint32_t edged_width2 = edged_width / 2; |
const uint32_t edged_width2 = edged_width / 2; |
160 |
const uint32_t width2 = width / 2; |
const uint32_t width2 = width / 2; |
167 |
src = image->y; |
src = image->y; |
168 |
|
|
169 |
for (i = 0; i < EDGE_SIZE; i++) { |
for (i = 0; i < EDGE_SIZE; i++) { |
|
// if interlacing, edges contain top-most data from each field |
|
|
if (interlacing && (i & 1)) { |
|
|
memset(dst, *(src + edged_width), EDGE_SIZE); |
|
|
memcpy(dst + EDGE_SIZE, src + edged_width, width); |
|
|
memset(dst + edged_width - EDGE_SIZE, |
|
|
*(src + edged_width + width - 1), EDGE_SIZE); |
|
|
} else { |
|
170 |
memset(dst, *src, EDGE_SIZE); |
memset(dst, *src, EDGE_SIZE); |
171 |
memcpy(dst + EDGE_SIZE, src, width); |
memcpy(dst + EDGE_SIZE, src, width); |
172 |
memset(dst + edged_width - EDGE_SIZE, *(src + width - 1), |
memset(dst + edged_width - EDGE_SIZE, *(src + width - 1), |
173 |
EDGE_SIZE); |
EDGE_SIZE); |
|
} |
|
174 |
dst += edged_width; |
dst += edged_width; |
175 |
} |
} |
176 |
|
|
183 |
|
|
184 |
src -= edged_width; |
src -= edged_width; |
185 |
for (i = 0; i < EDGE_SIZE; i++) { |
for (i = 0; i < EDGE_SIZE; i++) { |
|
// if interlacing, edges contain bottom-most data from each field |
|
|
if (interlacing && !(i & 1)) { |
|
|
memset(dst, *(src - edged_width), EDGE_SIZE); |
|
|
memcpy(dst + EDGE_SIZE, src - edged_width, width); |
|
|
memset(dst + edged_width - EDGE_SIZE, |
|
|
*(src - edged_width + width - 1), EDGE_SIZE); |
|
|
} else { |
|
186 |
memset(dst, *src, EDGE_SIZE); |
memset(dst, *src, EDGE_SIZE); |
187 |
memcpy(dst + EDGE_SIZE, src, width); |
memcpy(dst + EDGE_SIZE, src, width); |
188 |
memset(dst + edged_width - EDGE_SIZE, *(src + width - 1), |
memset(dst + edged_width - EDGE_SIZE, *(src + width - 1), |
189 |
EDGE_SIZE); |
EDGE_SIZE); |
|
} |
|
190 |
dst += edged_width; |
dst += edged_width; |
191 |
} |
} |
192 |
|
|
255 |
IMAGE * refhv, |
IMAGE * refhv, |
256 |
uint32_t edged_width, |
uint32_t edged_width, |
257 |
uint32_t edged_height, |
uint32_t edged_height, |
258 |
|
uint32_t quarterpel, |
259 |
uint32_t rounding) |
uint32_t rounding) |
260 |
{ |
{ |
261 |
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 |
262 |
const uint32_t stride_add = 7 * edged_width; |
const uint32_t stride_add = 7 * edged_width; |
263 |
|
/* |
264 |
#ifdef BFRAMES |
#ifdef BFRAMES |
265 |
const uint32_t edged_width2 = edged_width / 2; |
const uint32_t edged_width2 = edged_width / 2; |
266 |
const uint32_t edged_height2 = edged_height / 2; |
const uint32_t edged_height2 = edged_height / 2; |
267 |
const uint32_t offset2 = EDGE_SIZE2 * (edged_width2 + 1); |
const uint32_t offset2 = EDGE_SIZE2 * (edged_width2 + 1); |
268 |
const uint32_t stride_add2 = 7 * edged_width2; |
const uint32_t stride_add2 = 7 * edged_width2; |
269 |
#endif |
#endif |
270 |
|
*/ |
271 |
uint8_t *n_ptr, *h_ptr, *v_ptr, *hv_ptr; |
uint8_t *n_ptr, *h_ptr, *v_ptr, *hv_ptr; |
272 |
uint32_t x, y; |
uint32_t x, y; |
273 |
|
|
282 |
v_ptr -= offset; |
v_ptr -= offset; |
283 |
hv_ptr -= offset; |
hv_ptr -= offset; |
284 |
|
|
285 |
for (y = 0; y < edged_height; y = y + 8) { |
if(quarterpel) { |
286 |
for (x = 0; x < edged_width; x = x + 8) { |
|
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 |
|
|
292 |
|
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); |
interpolate8x8_halfpel_h(h_ptr, n_ptr, edged_width, rounding); |
328 |
interpolate8x8_halfpel_v(v_ptr, n_ptr, edged_width, rounding); |
interpolate8x8_halfpel_v(v_ptr, n_ptr, edged_width, rounding); |
329 |
interpolate8x8_halfpel_hv(hv_ptr, n_ptr, edged_width, rounding); |
interpolate8x8_halfpel_hv(hv_ptr, n_ptr, edged_width, rounding); |
333 |
v_ptr += 8; |
v_ptr += 8; |
334 |
hv_ptr += 8; |
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; |
h_ptr += stride_add; |
343 |
v_ptr += stride_add; |
v_ptr += stride_add; |
344 |
hv_ptr += stride_add; |
hv_ptr += stride_add; |
345 |
n_ptr += stride_add; |
n_ptr += stride_add; |
346 |
} |
} |
347 |
|
} |
348 |
|
/* |
349 |
#ifdef BFRAMES |
#ifdef BFRAMES |
350 |
n_ptr = refn->u; |
n_ptr = refn->u; |
351 |
h_ptr = refh->u; |
h_ptr = refh->u; |
357 |
v_ptr -= offset2; |
v_ptr -= offset2; |
358 |
hv_ptr -= offset2; |
hv_ptr -= offset2; |
359 |
|
|
360 |
for (y = 0; y < edged_height2; y = y + 8) { |
for (y = 0; y < edged_height2; y += 8) { |
361 |
for (x = 0; x < edged_width2; x = x + 8) { |
for (x = 0; x < edged_width2; x += 8) { |
362 |
interpolate8x8_halfpel_h(h_ptr, n_ptr, edged_width2, rounding); |
interpolate8x8_halfpel_h(h_ptr, n_ptr, edged_width2, rounding); |
363 |
interpolate8x8_halfpel_v(v_ptr, n_ptr, edged_width2, rounding); |
interpolate8x8_halfpel_v(v_ptr, n_ptr, edged_width2, rounding); |
364 |
interpolate8x8_halfpel_hv(hv_ptr, n_ptr, edged_width2, rounding); |
interpolate8x8_halfpel_hv(hv_ptr, n_ptr, edged_width2, rounding); |
401 |
n_ptr += stride_add2; |
n_ptr += stride_add2; |
402 |
} |
} |
403 |
#endif |
#endif |
404 |
|
*/ |
405 |
/* |
/* |
406 |
interpolate_halfpel_h( |
interpolate_halfpel_h( |
407 |
refh->y - offset, |
refh->y - offset, |
465 |
} |
} |
466 |
|
|
467 |
|
|
468 |
|
/* |
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 |
int |
int |
552 |
image_input(IMAGE * image, |
image_input(IMAGE * image, |
553 |
uint32_t width, |
uint32_t width, |
554 |
int height, |
int height, |
555 |
uint32_t edged_width, |
uint32_t edged_width, |
556 |
uint8_t * src, |
uint8_t * src[4], |
557 |
int csp) |
int src_stride[4], |
558 |
{ |
int csp, |
559 |
|
int interlacing) |
|
/* if (csp & XVID_CSP_VFLIP) |
|
560 |
{ |
{ |
561 |
height = -height; |
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 |
|
|
566 |
switch (csp & ~XVID_CSP_VFLIP) { |
switch (csp & ~XVID_CSP_VFLIP) { |
567 |
case XVID_CSP_RGB555: |
case XVID_CSP_RGB555: |
568 |
rgb555_to_yv12(image->y, image->u, image->v, src, width, height, |
safe_packed_conv( |
569 |
edged_width); |
src[0], src_stride[0], image->y, image->u, image->v, |
570 |
return 0; |
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 |
|
|
575 |
case XVID_CSP_RGB565: |
case XVID_CSP_RGB565: |
576 |
rgb565_to_yv12(image->y, image->u, image->v, src, width, height, |
safe_packed_conv( |
577 |
edged_width); |
src[0], src_stride[0], image->y, image->u, image->v, |
578 |
return 0; |
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 |
|
|
583 |
|
|
584 |
case XVID_CSP_RGB24: |
case XVID_CSP_BGR: |
585 |
rgb24_to_yv12(image->y, image->u, image->v, src, width, height, |
safe_packed_conv( |
586 |
edged_width); |
src[0], src_stride[0], image->y, image->u, image->v, |
587 |
return 0; |
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 |
|
|
592 |
case XVID_CSP_RGB32: |
case XVID_CSP_BGRA: |
593 |
rgb32_to_yv12(image->y, image->u, image->v, src, width, height, |
safe_packed_conv( |
594 |
edged_width); |
src[0], src_stride[0], image->y, image->u, image->v, |
595 |
return 0; |
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 |
|
|
600 |
case XVID_CSP_I420: |
case XVID_CSP_ABGR : |
601 |
yuv_to_yv12(image->y, image->u, image->v, src, width, height, |
safe_packed_conv( |
602 |
edged_width); |
src[0], src_stride[0], image->y, image->u, image->v, |
603 |
return 0; |
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 |
|
|
608 |
case XVID_CSP_YV12: /* u/v swapped */ |
case XVID_CSP_RGBA : |
609 |
yuv_to_yv12(image->y, image->v, image->u, src, width, height, |
safe_packed_conv( |
610 |
edged_width); |
src[0], src_stride[0], image->y, image->u, image->v, |
611 |
return 0; |
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 |
|
|
616 |
case XVID_CSP_YUY2: |
case XVID_CSP_YUY2: |
617 |
yuyv_to_yv12(image->y, image->u, image->v, src, width, height, |
safe_packed_conv( |
618 |
edged_width); |
src[0], src_stride[0], image->y, image->u, image->v, |
619 |
return 0; |
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 |
|
|
624 |
case XVID_CSP_YVYU: /* u/v swapped */ |
case XVID_CSP_YVYU: /* u/v swapped */ |
625 |
yuyv_to_yv12(image->y, image->v, image->u, src, width, height, |
safe_packed_conv( |
626 |
edged_width); |
src[0], src_stride[0], image->y, image->v, image->y, |
627 |
return 0; |
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 |
|
|
632 |
case XVID_CSP_UYVY: |
case XVID_CSP_UYVY: |
633 |
uyvy_to_yv12(image->y, image->u, image->v, src, width, height, |
safe_packed_conv( |
634 |
edged_width); |
src[0], src_stride[0], image->y, image->u, image->v, |
635 |
return 0; |
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 |
|
|
640 |
|
case XVID_CSP_I420: |
641 |
|
yv12_to_yv12(image->y, image->u, image->v, edged_width, edged_width2, |
642 |
|
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 |
|
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 |
|
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 |
|
break; |
651 |
|
|
652 |
case XVID_CSP_USER: |
case XVID_CSP_USER: |
653 |
user_to_yuv_c(image->y, image->u, image->v, edged_width, |
/*XXX: support for different u & v strides */ |
654 |
(DEC_PICTURE *) src, width, height); |
yv12_to_yv12(image->y, image->u, image->v, edged_width, edged_width2, |
655 |
return 0; |
src[0], src[1], src[2], src_stride[0], src_stride[1], |
656 |
|
width, height, (csp & XVID_CSP_VFLIP)); |
657 |
|
break; |
658 |
|
|
659 |
case XVID_CSP_NULL: |
case XVID_CSP_NULL: |
660 |
break; |
break; |
661 |
|
|
662 |
|
default : |
663 |
|
return -1; |
664 |
} |
} |
665 |
|
|
666 |
return -1; |
|
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 |
} |
} |
714 |
|
|
715 |
|
|
719 |
uint32_t width, |
uint32_t width, |
720 |
int height, |
int height, |
721 |
uint32_t edged_width, |
uint32_t edged_width, |
722 |
uint8_t * dst, |
uint8_t * dst[4], |
723 |
uint32_t dst_stride, |
uint32_t dst_stride[4], |
724 |
int csp) |
int csp, |
725 |
|
int interlacing) |
726 |
{ |
{ |
727 |
if (csp & XVID_CSP_VFLIP) { |
const int edged_width2 = edged_width/2; |
728 |
height = -height; |
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 |
switch (csp & ~XVID_CSP_VFLIP) { |
switch (csp & ~XVID_CSP_VFLIP) { |
737 |
case XVID_CSP_RGB555: |
case XVID_CSP_RGB555: |
738 |
yv12_to_rgb555(dst, dst_stride, image->y, image->u, image->v, |
safe_packed_conv( |
739 |
edged_width, edged_width / 2, width, height); |
dst[0], dst_stride[0], image->y, image->u, image->v, |
740 |
|
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 |
return 0; |
return 0; |
744 |
|
|
745 |
case XVID_CSP_RGB565: |
case XVID_CSP_RGB565: |
746 |
yv12_to_rgb565(dst, dst_stride, image->y, image->u, image->v, |
safe_packed_conv( |
747 |
edged_width, edged_width / 2, width, height); |
dst[0], dst_stride[0], image->y, image->u, image->v, |
748 |
return 0; |
edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
749 |
|
interlacing?yv12_to_rgb565i :yv12_to_rgb565, |
750 |
case XVID_CSP_RGB24: |
interlacing?yv12_to_rgb565i_c:yv12_to_rgb565_c, 2); |
751 |
yv12_to_rgb24(dst, dst_stride, image->y, image->u, image->v, |
return 0; |
752 |
edged_width, edged_width / 2, width, height); |
|
753 |
return 0; |
case XVID_CSP_BGR: |
754 |
|
safe_packed_conv( |
755 |
case XVID_CSP_RGB32: |
dst[0], dst_stride[0], image->y, image->u, image->v, |
756 |
yv12_to_rgb32(dst, dst_stride, image->y, image->u, image->v, |
edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
757 |
edged_width, edged_width / 2, width, height); |
interlacing?yv12_to_bgri :yv12_to_bgr, |
758 |
return 0; |
interlacing?yv12_to_bgri_c:yv12_to_bgr_c, 3); |
759 |
|
return 0; |
760 |
case XVID_CSP_I420: |
|
761 |
yv12_to_yuv(dst, dst_stride, image->y, image->u, image->v, edged_width, |
case XVID_CSP_BGRA: |
762 |
edged_width / 2, width, height); |
safe_packed_conv( |
763 |
return 0; |
dst[0], dst_stride[0], image->y, image->u, image->v, |
764 |
|
edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
765 |
case XVID_CSP_YV12: // u,v swapped |
interlacing?yv12_to_bgrai :yv12_to_bgra, |
766 |
yv12_to_yuv(dst, dst_stride, image->y, image->v, image->u, edged_width, |
interlacing?yv12_to_bgrai_c:yv12_to_bgra_c, 4); |
767 |
edged_width / 2, width, height); |
return 0; |
768 |
|
|
769 |
|
case XVID_CSP_ABGR: |
770 |
|
safe_packed_conv( |
771 |
|
dst[0], dst_stride[0], image->y, image->u, image->v, |
772 |
|
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 |
|
return 0; |
776 |
|
|
777 |
|
case XVID_CSP_RGBA: |
778 |
|
safe_packed_conv( |
779 |
|
dst[0], dst_stride[0], image->y, image->u, image->v, |
780 |
|
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 |
return 0; |
return 0; |
784 |
|
|
785 |
case XVID_CSP_YUY2: |
case XVID_CSP_YUY2: |
786 |
yv12_to_yuyv(dst, dst_stride, image->y, image->u, image->v, |
safe_packed_conv( |
787 |
edged_width, edged_width / 2, width, height); |
dst[0], dst_stride[0], image->y, image->u, image->v, |
788 |
|
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 |
return 0; |
return 0; |
792 |
|
|
793 |
case XVID_CSP_YVYU: // u,v swapped |
case XVID_CSP_YVYU: // u,v swapped |
794 |
yv12_to_yuyv(dst, dst_stride, image->y, image->v, image->u, |
safe_packed_conv( |
795 |
edged_width, edged_width / 2, width, height); |
dst[0], dst_stride[0], image->y, image->v, image->u, |
796 |
|
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 |
return 0; |
return 0; |
800 |
|
|
801 |
case XVID_CSP_UYVY: |
case XVID_CSP_UYVY: |
802 |
yv12_to_uyvy(dst, dst_stride, image->y, image->u, image->v, |
safe_packed_conv( |
803 |
edged_width, edged_width / 2, width, height); |
dst[0], dst_stride[0], image->y, image->u, image->v, |
804 |
|
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 |
return 0; |
return 0; |
808 |
|
|
809 |
case XVID_CSP_USER: |
case XVID_CSP_I420: |
810 |
((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, |
811 |
((DEC_PICTURE *) dst)->u = image->u; |
dst_stride[0], dst_stride[0]/2, |
812 |
((DEC_PICTURE *) dst)->v = image->v; |
image->y, image->u, image->v, edged_width, edged_width2, |
813 |
((DEC_PICTURE *) dst)->stride_y = edged_width; |
width, height, (csp & XVID_CSP_VFLIP)); |
814 |
((DEC_PICTURE *) dst)->stride_uv = edged_width / 2; |
return 0; |
815 |
|
|
816 |
|
case XVID_CSP_YV12: // u,v swapped |
817 |
|
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 |
|
image->y, image->v, image->u, edged_width, edged_width2, |
820 |
|
width, height, (csp & XVID_CSP_VFLIP)); |
821 |
|
return 0; |
822 |
|
|
823 |
|
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 |
|
return 0; |
829 |
|
|
830 |
|
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; |
return 0; |
838 |
|
|
839 |
case XVID_CSP_NULL: |
case XVID_CSP_NULL: |
840 |
case XVID_CSP_EXTERN: |
case XVID_CSP_SLICE: |
841 |
return 0; |
return 0; |
842 |
|
|
843 |
} |
} |
877 |
return psnr_y; |
return psnr_y; |
878 |
} |
} |
879 |
|
|
880 |
|
|
881 |
|
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 |
#include <stdio.h> |
#include <stdio.h> |
1003 |
} |
} |
1004 |
|
|
1005 |
|
|
|
#define ABS(X) (((X)>0)?(X):-(X)) |
|
1006 |
float |
float |
1007 |
image_mad(const IMAGE * img1, |
image_mad(const IMAGE * img1, |
1008 |
const IMAGE * img2, |
const IMAGE * img2, |
1033 |
} |
} |
1034 |
|
|
1035 |
void |
void |
1036 |
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) { |
1037 |
uint8_t *dY,*dU,*dV,*sY,*sU,*sV; |
uint8_t *dY,*dU,*dV,*sY,*sU,*sV; |
1038 |
int std2 = std >> 1; |
int std2 = std >> 1; |
1039 |
int w = mbl << 4, w2,i; |
int w = mbl << 4, w2,i; |
1042 |
w = width; |
w = width; |
1043 |
w2 = w >> 1; |
w2 = w >> 1; |
1044 |
|
|
1045 |
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); |
1046 |
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); |
1047 |
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); |
1048 |
sY = cur->y + (mby << 4) * std + (mbx << 4); |
sY = cur->y + (mby << 4) * std + (mbx << 4); |
1049 |
sU = cur->u + (mby << 3) * std2 + (mbx << 3); |
sU = cur->u + (mby << 3) * std2 + (mbx << 3); |
1050 |
sV = cur->v + (mby << 3) * std2 + (mbx << 3); |
sV = cur->v + (mby << 3) * std2 + (mbx << 3); |
1051 |
|
|
1052 |
for(i = 0 ; i < 16 ; i++) { |
for(i = 0 ; i < 16 ; i++) { |
1053 |
memcpy(dY,sY,w); |
memcpy(dY,sY,w); |
1054 |
dY += out_frm->stride_y; |
dY += out_frm->stride[0]; |
1055 |
sY += std; |
sY += std; |
1056 |
} |
} |
1057 |
for(i = 0 ; i < 8 ; i++) { |
for(i = 0 ; i < 8 ; i++) { |
1058 |
memcpy(dU,sU,w2); |
memcpy(dU,sU,w2); |
1059 |
dU += out_frm->stride_u; |
dU += out_frm->stride[1]; |
1060 |
sU += std2; |
sU += std2; |
1061 |
} |
} |
1062 |
for(i = 0 ; i < 8 ; i++) { |
for(i = 0 ; i < 8 ; i++) { |
1063 |
memcpy(dV,sV,w2); |
memcpy(dV,sV,w2); |
1064 |
dV += out_frm->stride_v; |
dV += out_frm->stride[2]; |
1065 |
sV += std2; |
sV += std2; |
1066 |
} |
} |
1067 |
} |
} |
1068 |
|
|
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 |
|
|
1112 |
|
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 |
|
|
1137 |
|
/* chroma */ |
1138 |
|
|
1139 |
|
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 |
|
|
1168 |
|
} |
1169 |
|
|