[svn] / branches / dev-api-4 / xvidcore / src / image / image.c Repository:
ViewVC logotype

Diff of /branches/dev-api-4/xvidcore/src/image/image.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/xvidcore/src/image/image.c revision 435, Sat Sep 7 09:04:41 2002 UTC branches/dev-api-4/xvidcore/src/image/image.c revision 1128, Mon Aug 25 15:01:51 2003 UTC
# Line 1  Line 1 
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>
  *  
  *  This program is an implementation of a part of one or more MPEG-4  
  *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending  
  *  to use this software module in hardware or software products are  
  *  advised that its use may infringe existing patents or copyrights, and  
  *  any such use would be at such party's own risk.  The original  
  *  developer of this software module and his/her company, and subsequent  
  *  editors and their companies, will have no liability for use of this  
  *  software or modifications or derivatives thereof.  
7   *   *
8   *  This program is free software; you can redistribute it and/or modify   *  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   *  it under the terms of the GNU General Public License as published by
# Line 28  Line 19 
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   *   *
22     * $Id: image.c,v 1.26.2.8 2003-08-25 15:01:51 edgomez Exp $
23     *
24   ****************************************************************************/   ****************************************************************************/
25    
26  #include <stdlib.h>  #include <stdlib.h>
27  #include <string.h>                             // memcpy, memset  #include <string.h>                             /* memcpy, memset */
28  #include <math.h>  #include <math.h>
29    
30  #include "../portab.h"  #include "../portab.h"
31  #include "../xvid.h"                    // XVID_CSP_XXX's  #include "../global.h"                  /* XVID_CSP_XXX's */
32    #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    
# Line 109  Line 105 
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    
# Line 142  Line 128 
128                             uint32_t edged_width,                             uint32_t edged_width,
129                             uint32_t edged_height,                             uint32_t edged_height,
130                             uint32_t width,                             uint32_t width,
131                             uint32_t height,                             uint32_t height)
                            uint32_t interlacing)  
132  {  {
133          const uint32_t edged_width2 = edged_width / 2;          const uint32_t edged_width2 = edged_width / 2;
134          const uint32_t width2 = width / 2;          uint32_t width2;
135          uint32_t i;          uint32_t i;
136          uint8_t *dst;          uint8_t *dst;
137          uint8_t *src;          uint8_t *src;
# Line 155  Line 140 
140          dst = image->y - (EDGE_SIZE + EDGE_SIZE * edged_width);          dst = image->y - (EDGE_SIZE + EDGE_SIZE * edged_width);
141          src = image->y;          src = image->y;
142    
143            /* According to the Standard Clause 7.6.4, padding is done starting at 16
144             * pixel width and height multiples */
145            width  = (width+15)&~15;
146            height = (height+15)&~15;
147            width2 = width/2;
148    
149          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 {*/  
150                          memset(dst, *src, EDGE_SIZE);                          memset(dst, *src, EDGE_SIZE);
151                          memcpy(dst + EDGE_SIZE, src, width);                          memcpy(dst + EDGE_SIZE, src, width);
152                          memset(dst + edged_width - EDGE_SIZE, *(src + width - 1),                          memset(dst + edged_width - EDGE_SIZE, *(src + width - 1),
153                                     EDGE_SIZE);                                     EDGE_SIZE);
                 /*}*/  
154                  dst += edged_width;                  dst += edged_width;
155          }          }
156    
# Line 180  Line 163 
163    
164          src -= edged_width;          src -= edged_width;
165          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 {*/  
166                          memset(dst, *src, EDGE_SIZE);                          memset(dst, *src, EDGE_SIZE);
167                          memcpy(dst + EDGE_SIZE, src, width);                          memcpy(dst + EDGE_SIZE, src, width);
168                          memset(dst + edged_width - EDGE_SIZE, *(src + width - 1),                          memset(dst + edged_width - EDGE_SIZE, *(src + width - 1),
169                                     EDGE_SIZE);                                     EDGE_SIZE);
                 /*}*/  
170                  dst += edged_width;                  dst += edged_width;
171          }          }
172    
173    
174  //U          /* U */
175          dst = image->u - (EDGE_SIZE2 + EDGE_SIZE2 * edged_width2);          dst = image->u - (EDGE_SIZE2 + EDGE_SIZE2 * edged_width2);
176          src = image->u;          src = image->u;
177    
# Line 224  Line 199 
199          }          }
200    
201    
202  // V          /* V */
203          dst = image->v - (EDGE_SIZE2 + EDGE_SIZE2 * edged_width2);          dst = image->v - (EDGE_SIZE2 + EDGE_SIZE2 * edged_width2);
204          src = image->v;          src = image->v;
205    
# Line 252  Line 227 
227          }          }
228  }  }
229    
230  // bframe encoding requires image-based u,v interpolation  /* bframe encoding requires image-based u,v interpolation */
231  void  void
232  image_interpolate(const IMAGE * refn,  image_interpolate(const IMAGE * refn,
233                                    IMAGE * refh,                                    IMAGE * refh,
# Line 260  Line 235 
235                                    IMAGE * refhv,                                    IMAGE * refhv,
236                                    uint32_t edged_width,                                    uint32_t edged_width,
237                                    uint32_t edged_height,                                    uint32_t edged_height,
238                                      uint32_t quarterpel,
239                                    uint32_t rounding)                                    uint32_t rounding)
240  {  {
241          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 */
242          const uint32_t stride_add = 7 * edged_width;          const uint32_t stride_add = 7 * edged_width;
243    #if 0
244            const uint32_t edged_width2 = edged_width / 2;
245            const uint32_t edged_height2 = edged_height / 2;
246            const uint32_t offset2 = EDGE_SIZE2 * (edged_width2 + 1);
247            const uint32_t stride_add2 = 7 * edged_width2;
248    #endif
249          uint8_t *n_ptr, *h_ptr, *v_ptr, *hv_ptr;          uint8_t *n_ptr, *h_ptr, *v_ptr, *hv_ptr;
250          uint32_t x, y;          uint32_t x, y;
251    
# Line 279  Line 260 
260          v_ptr -= offset;          v_ptr -= offset;
261          hv_ptr -= offset;          hv_ptr -= offset;
262    
263          for (y = 0; y < edged_height; y = y + 8) {          if(quarterpel) {
264                  for (x = 0; x < edged_width; x = x + 8) {  
265                    for (y = 0; y < (edged_height - EDGE_SIZE); y += 8) {
266                            for (x = 0; x < (edged_width - EDGE_SIZE); x += 8) {
267                                    interpolate8x8_6tap_lowpass_h(h_ptr, n_ptr, edged_width, rounding);
268                                    interpolate8x8_6tap_lowpass_v(v_ptr, n_ptr, edged_width, rounding);
269    
270                                    n_ptr += 8;
271                                    h_ptr += 8;
272                                    v_ptr += 8;
273                            }
274    
275                            n_ptr += EDGE_SIZE;
276                            h_ptr += EDGE_SIZE;
277                            v_ptr += EDGE_SIZE;
278    
279                            h_ptr += stride_add;
280                            v_ptr += stride_add;
281                            n_ptr += stride_add;
282                    }
283    
284                    h_ptr = refh->y;
285                    h_ptr -= offset;
286    
287                    for (y = 0; y < (edged_height - EDGE_SIZE); y = y + 8) {
288                            for (x = 0; x < (edged_width - EDGE_SIZE); x = x + 8) {
289                                    interpolate8x8_6tap_lowpass_v(hv_ptr, h_ptr, edged_width, rounding);
290                                    hv_ptr += 8;
291                                    h_ptr += 8;
292                            }
293    
294                            hv_ptr += EDGE_SIZE;
295                            h_ptr += EDGE_SIZE;
296    
297                            hv_ptr += stride_add;
298                            h_ptr += stride_add;
299                    }
300            }
301            else {
302    
303                    for (y = 0; y < (edged_height - EDGE_SIZE); y += 8) {
304                            for (x = 0; x < (edged_width - EDGE_SIZE); x += 8) {
305                          interpolate8x8_halfpel_h(h_ptr, n_ptr, edged_width, rounding);                          interpolate8x8_halfpel_h(h_ptr, n_ptr, edged_width, rounding);
306                          interpolate8x8_halfpel_v(v_ptr, n_ptr, edged_width, rounding);                          interpolate8x8_halfpel_v(v_ptr, n_ptr, edged_width, rounding);
307                          interpolate8x8_halfpel_hv(hv_ptr, n_ptr, edged_width, rounding);                          interpolate8x8_halfpel_hv(hv_ptr, n_ptr, edged_width, rounding);
# Line 290  Line 311 
311                          v_ptr += 8;                          v_ptr += 8;
312                          hv_ptr += 8;                          hv_ptr += 8;
313                  }                  }
314    
315                            h_ptr += EDGE_SIZE;
316                            v_ptr += EDGE_SIZE;
317                            hv_ptr += EDGE_SIZE;
318                            n_ptr += EDGE_SIZE;
319    
320                  h_ptr += stride_add;                  h_ptr += stride_add;
321                  v_ptr += stride_add;                  v_ptr += stride_add;
322                  hv_ptr += stride_add;                  hv_ptr += stride_add;
323                  n_ptr += stride_add;                  n_ptr += stride_add;
324          }          }
325            }
326    /*
327    #ifdef BFRAMES
328            n_ptr = refn->u;
329            h_ptr = refh->u;
330            v_ptr = refv->u;
331            hv_ptr = refhv->u;
332    
333            n_ptr -= offset2;
334            h_ptr -= offset2;
335            v_ptr -= offset2;
336            hv_ptr -= offset2;
337    
338            for (y = 0; y < edged_height2; y += 8) {
339                    for (x = 0; x < edged_width2; x += 8) {
340                            interpolate8x8_halfpel_h(h_ptr, n_ptr, edged_width2, rounding);
341                            interpolate8x8_halfpel_v(v_ptr, n_ptr, edged_width2, rounding);
342                            interpolate8x8_halfpel_hv(hv_ptr, n_ptr, edged_width2, rounding);
343    
344                            n_ptr += 8;
345                            h_ptr += 8;
346                            v_ptr += 8;
347                            hv_ptr += 8;
348                    }
349                    h_ptr += stride_add2;
350                    v_ptr += stride_add2;
351                    hv_ptr += stride_add2;
352                    n_ptr += stride_add2;
353            }
354    
355            n_ptr = refn->v;
356            h_ptr = refh->v;
357            v_ptr = refv->v;
358            hv_ptr = refhv->v;
359    
360            n_ptr -= offset2;
361            h_ptr -= offset2;
362            v_ptr -= offset2;
363            hv_ptr -= offset2;
364    
365            for (y = 0; y < edged_height2; y = y + 8) {
366                    for (x = 0; x < edged_width2; x = x + 8) {
367                            interpolate8x8_halfpel_h(h_ptr, n_ptr, edged_width2, rounding);
368                            interpolate8x8_halfpel_v(v_ptr, n_ptr, edged_width2, rounding);
369                            interpolate8x8_halfpel_hv(hv_ptr, n_ptr, edged_width2, rounding);
370    
371                            n_ptr += 8;
372                            h_ptr += 8;
373                            v_ptr += 8;
374                            hv_ptr += 8;
375                    }
376                    h_ptr += stride_add2;
377                    v_ptr += stride_add2;
378                    hv_ptr += stride_add2;
379                    n_ptr += stride_add2;
380            }
381    #endif
382    */
383          /*          /*
384             interpolate_halfpel_h(             interpolate_halfpel_h(
385             refh->y - offset,             refh->y - offset,
# Line 359  Line 443 
443  }  }
444    
445    
446    /*
447    chroma optimize filter, invented by mf
448    a chroma pixel is average from the surrounding pixels, when the
449    correpsonding luma pixels are pure black or white.
450    */
451    
452    void
453    image_chroma_optimize(IMAGE * img, int width, int height, int edged_width)
454    {
455            int x,y;
456            int pixels = 0;
457    
458            for (y = 1; y < height/2 - 1; y++)
459            for (x = 1; x < width/2 - 1; x++)
460            {
461    #define IS_PURE(a)  ((a)<=16||(a)>=235)
462    #define IMG_Y(Y,X)      img->y[(Y)*edged_width + (X)]
463    #define IMG_U(Y,X)      img->u[(Y)*edged_width/2 + (X)]
464    #define IMG_V(Y,X)      img->v[(Y)*edged_width/2 + (X)]
465    
466                    if (IS_PURE(IMG_Y(y*2  ,x*2  )) &&
467                            IS_PURE(IMG_Y(y*2  ,x*2+1)) &&
468                            IS_PURE(IMG_Y(y*2+1,x*2  )) &&
469                            IS_PURE(IMG_Y(y*2+1,x*2+1)))
470                    {
471                            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;
472                            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;
473                            pixels++;
474                    }
475    
476    #undef IS_PURE
477    #undef IMG_Y
478    #undef IMG_U
479    #undef IMG_V
480            }
481    
482            DPRINTF(XVID_DEBUG_DEBUG,"chroma_optimized_pixels = %i/%i\n", pixels, width*height/4);
483    }
484    
485    
486    
487    
488    
489    /*
490      perform safe packed colorspace conversion, by splitting
491      the image up into an optimized area (pixel width divisible by 16),
492      and two unoptimized/plain-c areas (pixel width divisible by 2)
493    */
494    
495    static void
496    safe_packed_conv(uint8_t * x_ptr, int x_stride,
497                                     uint8_t * y_ptr, uint8_t * u_ptr, uint8_t * v_ptr,
498                                     int y_stride, int uv_stride,
499                                     int width, int height, int vflip,
500                                     packedFunc * func_opt, packedFunc func_c, int size)
501    {
502            int width_opt, width_c;
503    
504            if (func_opt != func_c && x_stride < size*((width+15)/16)*16)
505            {
506                    width_opt = width & (~15);
507                    width_c = width - width_opt;
508            }
509            else
510            {
511                    width_opt = width;
512                    width_c = 0;
513            }
514    
515            func_opt(x_ptr, x_stride,
516                            y_ptr, u_ptr, v_ptr, y_stride, uv_stride,
517                            width_opt, height, vflip);
518    
519            if (width_c)
520            {
521                    func_c(x_ptr + size*width_opt, x_stride,
522                            y_ptr + width_opt, u_ptr + width_opt/2, v_ptr + width_opt/2,
523                            y_stride, uv_stride, width_c, height, vflip);
524            }
525    }
526    
527    
528    
529  int  int
530  image_input(IMAGE * image,  image_input(IMAGE * image,
531                          uint32_t width,                          uint32_t width,
532                          int height,                          int height,
533                          uint32_t edged_width,                          uint32_t edged_width,
534                          uint8_t * src,                          uint8_t * src[4],
535                          int csp)                          int src_stride[4],
536                            int csp,
537                            int interlacing)
538  {  {
539            const int edged_width2 = edged_width/2;
540  /*      if (csp & XVID_CSP_VFLIP)          const int width2 = width/2;
541          {          const int height2 = height/2;
542                  height = -height;  #if 0
543          }          const int height_signed = (csp & XVID_CSP_VFLIP) ? -height : height;
544  */  #endif
545    
546          switch (csp & ~XVID_CSP_VFLIP) {          switch (csp & ~XVID_CSP_VFLIP) {
547          case XVID_CSP_RGB555:          case XVID_CSP_RGB555:
548                  rgb555_to_yv12(image->y, image->u, image->v, src, width, height,                  safe_packed_conv(
549                                             edged_width);                          src[0], src_stride[0], image->y, image->u, image->v,
550                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
551                            interlacing?rgb555i_to_yv12  :rgb555_to_yv12,
552                            interlacing?rgb555i_to_yv12_c:rgb555_to_yv12_c, 2);
553                    break;
554    
555          case XVID_CSP_RGB565:          case XVID_CSP_RGB565:
556                  rgb565_to_yv12(image->y, image->u, image->v, src, width, height,                  safe_packed_conv(
557                                             edged_width);                          src[0], src_stride[0], image->y, image->u, image->v,
558                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
559                            interlacing?rgb565i_to_yv12  :rgb565_to_yv12,
560                            interlacing?rgb565i_to_yv12_c:rgb565_to_yv12_c, 2);
561                    break;
562    
563    
564          case XVID_CSP_RGB24:          case XVID_CSP_BGR:
565                  rgb24_to_yv12(image->y, image->u, image->v, src, width, height,                  safe_packed_conv(
566                                            edged_width);                          src[0], src_stride[0], image->y, image->u, image->v,
567                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
568                            interlacing?bgri_to_yv12  :bgr_to_yv12,
569                            interlacing?bgri_to_yv12_c:bgr_to_yv12_c, 3);
570                    break;
571    
572          case XVID_CSP_RGB32:          case XVID_CSP_BGRA:
573                  rgb32_to_yv12(image->y, image->u, image->v, src, width, height,                  safe_packed_conv(
574                                            edged_width);                          src[0], src_stride[0], image->y, image->u, image->v,
575                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
576                            interlacing?bgrai_to_yv12  :bgra_to_yv12,
577                            interlacing?bgrai_to_yv12_c:bgra_to_yv12_c, 4);
578                    break;
579    
580          case XVID_CSP_I420:          case XVID_CSP_ABGR :
581                  yuv_to_yv12(image->y, image->u, image->v, src, width, height,                  safe_packed_conv(
582                                          edged_width);                          src[0], src_stride[0], image->y, image->u, image->v,
583                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
584                            interlacing?abgri_to_yv12  :abgr_to_yv12,
585                            interlacing?abgri_to_yv12_c:abgr_to_yv12_c, 4);
586                    break;
587    
588          case XVID_CSP_YV12:             /* u/v swapped */          case XVID_CSP_RGBA :
589                  yuv_to_yv12(image->y, image->v, image->u, src, width, height,                  safe_packed_conv(
590                                          edged_width);                          src[0], src_stride[0], image->y, image->u, image->v,
591                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
592                            interlacing?rgbai_to_yv12  :rgba_to_yv12,
593                            interlacing?rgbai_to_yv12_c:rgba_to_yv12_c, 4);
594                    break;
595    
596          case XVID_CSP_YUY2:          case XVID_CSP_YUY2:
597                  yuyv_to_yv12(image->y, image->u, image->v, src, width, height,                  safe_packed_conv(
598                                           edged_width);                          src[0], src_stride[0], image->y, image->u, image->v,
599                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
600                            interlacing?yuyvi_to_yv12  :yuyv_to_yv12,
601                            interlacing?yuyvi_to_yv12_c:yuyv_to_yv12_c, 2);
602                    break;
603    
604          case XVID_CSP_YVYU:             /* u/v swapped */          case XVID_CSP_YVYU:             /* u/v swapped */
605                  yuyv_to_yv12(image->y, image->v, image->u, src, width, height,                  safe_packed_conv(
606                                           edged_width);                          src[0], src_stride[0], image->y, image->v, image->y,
607                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
608                            interlacing?yuyvi_to_yv12  :yuyv_to_yv12,
609                            interlacing?yuyvi_to_yv12_c:yuyv_to_yv12_c, 2);
610                    break;
611    
612          case XVID_CSP_UYVY:          case XVID_CSP_UYVY:
613                  uyvy_to_yv12(image->y, image->u, image->v, src, width, height,                  safe_packed_conv(
614                                           edged_width);                          src[0], src_stride[0], image->y, image->u, image->v,
615                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
616                            interlacing?uyvyi_to_yv12  :uyvy_to_yv12,
617                            interlacing?uyvyi_to_yv12_c:uyvy_to_yv12_c, 2);
618                    break;
619    
620            case XVID_CSP_I420:
621                    yv12_to_yv12(image->y, image->u, image->v, 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_YV12:             /* u/v swapped */
627                    yv12_to_yv12(image->y, image->v, image->u, edged_width, edged_width2,
628                            src[0], src[0] + src_stride[0]*height, src[0] + src_stride[0]*height + (src_stride[0]/2)*height2,
629                            src_stride[0], src_stride[0]/2, width, height, (csp & XVID_CSP_VFLIP));
630                    break;
631    
632          case XVID_CSP_USER:          case XVID_CSP_USER:
633                  user_to_yuv_c(image->y, image->u, image->v, edged_width,          /*XXX: support for different u & v strides */
634                                            (DEC_PICTURE *) src, width, height);                  yv12_to_yv12(image->y, image->u, image->v, edged_width, edged_width2,
635                  return 0;                          src[0], src[1], src[2], src_stride[0], src_stride[1],
636                            width, height, (csp & XVID_CSP_VFLIP));
637                    break;
638    
639          case XVID_CSP_NULL:          case XVID_CSP_NULL:
640                  break;                  break;
641    
642            default :
643                    return -1;
644          }          }
645    
646          return -1;  
647            /* pad out image when the width and/or height is not a multiple of 16 */
648    
649            if (width & 15)
650            {
651                    int i;
652                    int pad_width = 16 - (width&15);
653                    for (i = 0; i < height; i++)
654                    {
655                            memset(image->y + i*edged_width + width,
656                                     *(image->y + i*edged_width + width - 1), pad_width);
657                    }
658                    for (i = 0; i < height/2; i++)
659                    {
660                            memset(image->u + i*edged_width2 + width2,
661                                     *(image->u + i*edged_width2 + width2 - 1),pad_width/2);
662                            memset(image->v + i*edged_width2 + width2,
663                                     *(image->v + i*edged_width2 + width2 - 1),pad_width/2);
664                    }
665            }
666    
667            if (height & 15)
668            {
669                    int pad_height = 16 - (height&15);
670                    int length = ((width+15)/16)*16;
671                    int i;
672                    for (i = 0; i < pad_height; i++)
673                    {
674                            memcpy(image->y + (height+i)*edged_width,
675                                       image->y + (height-1)*edged_width,length);
676                    }
677    
678                    for (i = 0; i < pad_height/2; i++)
679                    {
680                            memcpy(image->u + (height2+i)*edged_width2,
681                                       image->u + (height2-1)*edged_width2,length/2);
682                            memcpy(image->v + (height2+i)*edged_width2,
683                                       image->v + (height2-1)*edged_width2,length/2);
684                    }
685            }
686    
687    /*
688            if (interlacing)
689                    image_printf(image, edged_width, height, 5,5, "[i]");
690            image_dump_yuvpgm(image, edged_width, ((width+15)/16)*16, ((height+15)/16)*16, "\\encode.pgm");
691    */
692            return 0;
693  }  }
694    
695    
# Line 441  Line 699 
699                           uint32_t width,                           uint32_t width,
700                           int height,                           int height,
701                           uint32_t edged_width,                           uint32_t edged_width,
702                           uint8_t * dst,                           uint8_t * dst[4],
703                           uint32_t dst_stride,                           uint32_t dst_stride[4],
704                           int csp)                           int csp,
705                             int interlacing)
706  {  {
707          if (csp & XVID_CSP_VFLIP) {          const int edged_width2 = edged_width/2;
708                  height = -height;          int height2 = height/2;
709          }  
710    /*
711            if (interlacing)
712                    image_printf(image, edged_width, height, 5,100, "[i]=%i,%i",width,height);
713            image_dump_yuvpgm(image, edged_width, width, height, "\\decode.pgm");
714    */
715    
716          switch (csp & ~XVID_CSP_VFLIP) {          switch (csp & ~XVID_CSP_VFLIP) {
717          case XVID_CSP_RGB555:          case XVID_CSP_RGB555:
718                  yv12_to_rgb555(dst, dst_stride, image->y, image->u, image->v,                  safe_packed_conv(
719                                             edged_width, edged_width / 2, width, height);                          dst[0], dst_stride[0], image->y, image->u, image->v,
720                            edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
721                            interlacing?yv12_to_rgb555i  :yv12_to_rgb555,
722                            interlacing?yv12_to_rgb555i_c:yv12_to_rgb555_c, 2);
723                  return 0;                  return 0;
724    
725          case XVID_CSP_RGB565:          case XVID_CSP_RGB565:
726                  yv12_to_rgb565(dst, dst_stride, image->y, image->u, image->v,                  safe_packed_conv(
727                                             edged_width, edged_width / 2, width, height);                          dst[0], dst_stride[0], image->y, image->u, image->v,
728                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
729                            interlacing?yv12_to_rgb565i  :yv12_to_rgb565,
730          case XVID_CSP_RGB24:                          interlacing?yv12_to_rgb565i_c:yv12_to_rgb565_c, 2);
731                  yv12_to_rgb24(dst, dst_stride, image->y, image->u, image->v,                  return 0;
732                                            edged_width, edged_width / 2, width, height);  
733                  return 0;      case XVID_CSP_BGR:
734                    safe_packed_conv(
735          case XVID_CSP_RGB32:                          dst[0], dst_stride[0], image->y, image->u, image->v,
736                  yv12_to_rgb32(dst, dst_stride, image->y, image->u, image->v,                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
737                                            edged_width, edged_width / 2, width, height);                          interlacing?yv12_to_bgri  :yv12_to_bgr,
738                  return 0;                          interlacing?yv12_to_bgri_c:yv12_to_bgr_c, 3);
739                    return 0;
740          case XVID_CSP_I420:  
741                  yv12_to_yuv(dst, dst_stride, image->y, image->u, image->v, edged_width,          case XVID_CSP_BGRA:
742                                          edged_width / 2, width, height);                  safe_packed_conv(
743                  return 0;                          dst[0], dst_stride[0], image->y, image->u, image->v,
744                            edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
745          case XVID_CSP_YV12:             // u,v swapped                          interlacing?yv12_to_bgrai  :yv12_to_bgra,
746                  yv12_to_yuv(dst, dst_stride, image->y, image->v, image->u, edged_width,                          interlacing?yv12_to_bgrai_c:yv12_to_bgra_c, 4);
747                                          edged_width / 2, width, height);                  return 0;
748    
749            case XVID_CSP_ABGR:
750                    safe_packed_conv(
751                            dst[0], dst_stride[0], image->y, image->u, image->v,
752                            edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
753                            interlacing?yv12_to_abgri  :yv12_to_abgr,
754                            interlacing?yv12_to_abgri_c:yv12_to_abgr_c, 4);
755                    return 0;
756    
757            case XVID_CSP_RGBA:
758                    safe_packed_conv(
759                            dst[0], dst_stride[0], image->y, image->u, image->v,
760                            edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
761                            interlacing?yv12_to_rgbai  :yv12_to_rgba,
762                            interlacing?yv12_to_rgbai_c:yv12_to_rgba_c, 4);
763                  return 0;                  return 0;
764    
765          case XVID_CSP_YUY2:          case XVID_CSP_YUY2:
766                  yv12_to_yuyv(dst, dst_stride, image->y, image->u, image->v,                  safe_packed_conv(
767                                           edged_width, edged_width / 2, width, height);                          dst[0], dst_stride[0], image->y, image->u, image->v,
768                            edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
769                            interlacing?yv12_to_yuyvi  :yv12_to_yuyv,
770                            interlacing?yv12_to_yuyvi_c:yv12_to_yuyv_c, 2);
771                  return 0;                  return 0;
772    
773          case XVID_CSP_YVYU:             // u,v swapped          case XVID_CSP_YVYU:             /* u,v swapped */
774                  yv12_to_yuyv(dst, dst_stride, image->y, image->v, image->u,                  safe_packed_conv(
775                                           edged_width, edged_width / 2, width, height);                          dst[0], dst_stride[0], image->y, image->v, image->u,
776                            edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
777                            interlacing?yv12_to_yuyvi  :yv12_to_yuyv,
778                            interlacing?yv12_to_yuyvi_c:yv12_to_yuyv_c, 2);
779                  return 0;                  return 0;
780    
781          case XVID_CSP_UYVY:          case XVID_CSP_UYVY:
782                  yv12_to_uyvy(dst, dst_stride, image->y, image->u, image->v,                  safe_packed_conv(
783                                           edged_width, edged_width / 2, width, height);                          dst[0], dst_stride[0], image->y, image->u, image->v,
784                            edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
785                            interlacing?yv12_to_uyvyi  :yv12_to_uyvy,
786                            interlacing?yv12_to_uyvyi_c:yv12_to_uyvy_c, 2);
787                  return 0;                  return 0;
788    
789          case XVID_CSP_USER:          case XVID_CSP_I420:
790                  ((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,
791                  ((DEC_PICTURE *) dst)->u = image->u;                          dst_stride[0], dst_stride[0]/2,
792                  ((DEC_PICTURE *) dst)->v = image->v;                          image->y, image->u, image->v, edged_width, edged_width2,
793                  ((DEC_PICTURE *) dst)->stride_y = edged_width;                          width, height, (csp & XVID_CSP_VFLIP));
794                  ((DEC_PICTURE *) dst)->stride_uv = edged_width / 2;                  return 0;
795    
796            case XVID_CSP_YV12:             /* u,v swapped */
797                    yv12_to_yv12(dst[0], dst[0] + dst_stride[0]*height, dst[0] + dst_stride[0]*height + (dst_stride[0]/2)*height2,
798                            dst_stride[0], dst_stride[0]/2,
799                            image->y, image->v, image->u, edged_width, edged_width2,
800                            width, height, (csp & XVID_CSP_VFLIP));
801                    return 0;
802    
803            case XVID_CSP_USER :            /* u,v swapped */
804                    yv12_to_yv12(dst[0], dst[1], dst[2],
805                            dst_stride[0], dst_stride[1],   /* v: dst_stride[2] */
806                            image->y, image->v, image->u, edged_width, edged_width2,
807                            width, height, (csp & XVID_CSP_VFLIP));
808                    return 0;
809    
810            case XVID_CSP_INTERNAL :
811                    dst[0] = image->y;
812                    dst[1] = image->u;
813                    dst[2] = image->v;
814                    dst_stride[0] = edged_width;
815                    dst_stride[1] = edged_width/2;
816                    dst_stride[2] = edged_width/2;
817                  return 0;                  return 0;
818    
819          case XVID_CSP_NULL:          case XVID_CSP_NULL:
820          case XVID_CSP_EXTERN:          case XVID_CSP_SLICE:
821                  return 0;                  return 0;
822    
823          }          }
# Line 544  Line 857 
857          return psnr_y;          return psnr_y;
858  }  }
859    
860  /*  
861    float sse_to_PSNR(long sse, int pixels)
862    {
863            if (sse==0)
864                    return 99.99F;
865    
866            return 48.131F - 10*(float)log10((float)sse/(float)(pixels));   /* log10(255*255)=4.8131 */
867    
868    }
869    
870    long plane_sse(uint8_t * orig,
871                       uint8_t * recon,
872                       uint16_t stride,
873                       uint16_t width,
874                       uint16_t height)
875    {
876            int diff, x, y;
877            long sse=0;
878    
879            for (y = 0; y < height; y++) {
880                    for (x = 0; x < width; x++) {
881                            diff = *(orig + x) - *(recon + x);
882                            sse += diff * diff;
883                    }
884                    orig += stride;
885                    recon += stride;
886            }
887            return sse;
888    }
889    
890    #if 0
891    
892  #include <stdio.h>  #include <stdio.h>
893  #include <string.h>  #include <string.h>
# Line 568  Line 911 
911  }  }
912    
913    
914  // dump image+edges to yuv pgm files  /* dump image+edges to yuv pgm files */
915    
916  int image_dump(IMAGE * image, uint32_t edged_width, uint32_t edged_height, char * path, int number)  int image_dump(IMAGE * image, uint32_t edged_width, uint32_t edged_height, char * path, int number)
917  {  {
# Line 591  Line 934 
934    
935          return 0;          return 0;
936  }  }
937  */  #endif
938    
939    
940    
# Line 640  Line 983 
983  }  }
984    
985    
 #define ABS(X)    (((X)>0)?(X):-(X))  
986  float  float
987  image_mad(const IMAGE * img1,  image_mad(const IMAGE * img1,
988                    const IMAGE * img2,                    const IMAGE * img2,
# Line 657  Line 999 
999    
1000          for (y = 0; y < height; y++)          for (y = 0; y < height; y++)
1001                  for (x = 0; x < width; x++)                  for (x = 0; x < width; x++)
1002                          sum += ABS(img1->y[x + y * stride] - img2->y[x + y * stride]);                          sum += abs(img1->y[x + y * stride] - img2->y[x + y * stride]);
1003    
1004          for (y = 0; y < height2; y++)          for (y = 0; y < height2; y++)
1005                  for (x = 0; x < width2; x++)                  for (x = 0; x < width2; x++)
1006                          sum += ABS(img1->u[x + y * stride2] - img2->u[x + y * stride2]);                          sum += abs(img1->u[x + y * stride2] - img2->u[x + y * stride2]);
1007    
1008          for (y = 0; y < height2; y++)          for (y = 0; y < height2; y++)
1009                  for (x = 0; x < width2; x++)                  for (x = 0; x < width2; x++)
1010                          sum += ABS(img1->v[x + y * stride2] - img2->v[x + y * stride2]);                          sum += abs(img1->v[x + y * stride2] - img2->v[x + y * stride2]);
1011    
1012          return (float) sum / (width * height * 3 / 2);          return (float) sum / (width * height * 3 / 2);
1013  }  }
1014    
1015  void  void
1016  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) {
1017    uint8_t *dY,*dU,*dV,*sY,*sU,*sV;    uint8_t *dY,*dU,*dV,*sY,*sU,*sV;
1018    int std2 = std >> 1;    int std2 = std >> 1;
1019    int w = mbl << 4, w2,i;    int w = mbl << 4, w2,i;
# Line 680  Line 1022 
1022      w = width;      w = width;
1023    w2 = w >> 1;    w2 = w >> 1;
1024    
1025    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);
1026    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);
1027    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);
1028    sY = cur->y + (mby << 4) * std + (mbx << 4);    sY = cur->y + (mby << 4) * std + (mbx << 4);
1029    sU = cur->u + (mby << 3) * std2 + (mbx << 3);    sU = cur->u + (mby << 3) * std2 + (mbx << 3);
1030    sV = cur->v + (mby << 3) * std2 + (mbx << 3);    sV = cur->v + (mby << 3) * std2 + (mbx << 3);
1031    
1032    for(i = 0 ; i < 16 ; i++) {    for(i = 0 ; i < 16 ; i++) {
1033      memcpy(dY,sY,w);      memcpy(dY,sY,w);
1034      dY += out_frm->stride_y;      dY += out_frm->stride[0];
1035      sY += std;      sY += std;
1036    }    }
1037    for(i = 0 ; i < 8 ; i++) {    for(i = 0 ; i < 8 ; i++) {
1038      memcpy(dU,sU,w2);      memcpy(dU,sU,w2);
1039      dU += out_frm->stride_u;      dU += out_frm->stride[1];
1040      sU += std2;      sU += std2;
1041    }    }
1042    for(i = 0 ; i < 8 ; i++) {    for(i = 0 ; i < 8 ; i++) {
1043      memcpy(dV,sV,w2);      memcpy(dV,sV,w2);
1044      dV += out_frm->stride_v;      dV += out_frm->stride[2];
1045      sV += std2;      sV += std2;
1046    }    }
1047  }  }
1048    
1049    
1050    void
1051    image_clear(IMAGE * img, int width, int height, int edged_width,
1052                                            int y, int u, int v)
1053    {
1054            uint8_t * p;
1055            int i;
1056    
1057            p = img->y;
1058            for (i = 0; i < height; i++) {
1059                    memset(p, y, width);
1060                    p += edged_width;
1061            }
1062    
1063            p = img->u;
1064            for (i = 0; i < height/2; i++) {
1065                    memset(p, u, width/2);
1066                    p += edged_width/2;
1067            }
1068    
1069            p = img->v;
1070            for (i = 0; i < height/2; i++) {
1071                    memset(p, v, width/2);
1072                    p += edged_width/2;
1073            }
1074    }
1075    
1076    
1077    /* reduced resolution deblocking filter
1078            block = block size (16=rrv, 8=full resolution)
1079            flags = XVID_DEC_YDEBLOCK|XVID_DEC_UVDEBLOCK
1080    */
1081    void
1082    image_deblock_rrv(IMAGE * img, int edged_width,
1083                                    const MACROBLOCK * mbs, int mb_width, int mb_height, int mb_stride,
1084                                    int block, int flags)
1085    {
1086            const int edged_width2 = edged_width /2;
1087            const int nblocks = block / 8;  /* skals code uses 8pixel block uints */
1088            int i,j;
1089    
1090            /* luma: j,i in block units */
1091    
1092                    for (j = 1; j < mb_height*2; j++)               /* horizontal deblocking */
1093                    for (i = 0; i < mb_width*2; i++)
1094                    {
1095                            if (mbs[(j-1)/2*mb_stride + (i/2)].mode != MODE_NOT_CODED ||
1096                                    mbs[(j+0)/2*mb_stride + (i/2)].mode != MODE_NOT_CODED)
1097                            {
1098                                    hfilter_31(img->y + (j*block - 1)*edged_width + i*block,
1099                                                                      img->y + (j*block + 0)*edged_width + i*block, nblocks);
1100                            }
1101                    }
1102    
1103                    for (j = 0; j < mb_height*2; j++)               /* vertical deblocking */
1104                    for (i = 1; i < mb_width*2; i++)
1105                    {
1106                            if (mbs[(j/2)*mb_stride + (i-1)/2].mode != MODE_NOT_CODED ||
1107                                    mbs[(j/2)*mb_stride + (i+0)/2].mode != MODE_NOT_CODED)
1108                            {
1109                                    vfilter_31(img->y + (j*block)*edged_width + i*block - 1,
1110                                                       img->y + (j*block)*edged_width + i*block + 0,
1111                                                       edged_width, nblocks);
1112                            }
1113                    }
1114    
1115    
1116    
1117            /* chroma */
1118    
1119                    for (j = 1; j < mb_height; j++)         /* horizontal deblocking */
1120                    for (i = 0; i < mb_width; i++)
1121                    {
1122                            if (mbs[(j-1)*mb_stride + i].mode != MODE_NOT_CODED ||
1123                                    mbs[(j+0)*mb_stride + i].mode != MODE_NOT_CODED)
1124                            {
1125                                    hfilter_31(img->u + (j*block - 1)*edged_width2 + i*block,
1126                                                       img->u + (j*block + 0)*edged_width2 + i*block, nblocks);
1127                                    hfilter_31(img->v + (j*block - 1)*edged_width2 + i*block,
1128                                                       img->v + (j*block + 0)*edged_width2 + i*block, nblocks);
1129                            }
1130                    }
1131    
1132                    for (j = 0; j < mb_height; j++)         /* vertical deblocking */
1133                    for (i = 1; i < mb_width; i++)
1134                    {
1135                            if (mbs[j*mb_stride + i - 1].mode != MODE_NOT_CODED ||
1136                                    mbs[j*mb_stride + i + 0].mode != MODE_NOT_CODED)
1137                            {
1138                                    vfilter_31(img->u + (j*block)*edged_width2 + i*block - 1,
1139                                                       img->u + (j*block)*edged_width2 + i*block + 0,
1140                                                       edged_width2, nblocks);
1141                                    vfilter_31(img->v + (j*block)*edged_width2 + i*block - 1,
1142                                                       img->v + (j*block)*edged_width2 + i*block + 0,
1143                                                       edged_width2, nblocks);
1144                            }
1145                    }
1146    
1147    
1148    }
1149    

Legend:
Removed from v.435  
changed lines
  Added in v.1128

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4