[cvs] / xvidcore / src / image / image.c Repository:
ViewVC logotype

Diff of /xvidcore/src/image/image.c

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

revision 1.20.2.5, Sun Oct 6 07:05:51 2002 UTC revision 1.26.2.7, Mon Jun 9 13:53:50 2003 UTC
# Line 1  Line 1 
1  /**************************************************************************  /**************************************************************************
2   *   *
3   *      XVID MPEG-4 VIDEO CODEC   *      XVID MPEG-4 VIDEO CODEC
4   *      image stuff   *  - Image management functions -
5   *   *
6   *      This program is an implementation of a part of one or more MPEG-4   *  Copyright(C) 2001-2003 Peter Ross <pross@xvid.org>
  *      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 24  Line 17 
17   *   *
18   *      You should have received a copy of the GNU General Public License   *      You should have received a copy of the GNU General Public License
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., 675 Mass Ave, Cambridge, MA 02139, USA.   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21   *   *
22   *************************************************************************/   * $Id$
   
 /**************************************************************************  
  *  
  *      History:  
23   *   *
24   *  05.10.2002  support for interpolated images in qpel mode - Isibaar   ****************************************************************************/
  *      01.05.2002      BFRAME image-based u,v interpolation  
  *  22.04.2002  added some B-frame support  
  *      14.04.2002      added image_dump_yuvpgm(), added image_mad()  
  *              XVID_CSP_USER input support  
  *  09.04.2002  PSNR calculations - Isibaar  
  *      06.04.2002      removed interlaced edging from U,V blocks (as per spec)  
  *  26.03.2002  interlacing support (field-based edging in set_edges)  
  *      26.01.2002      rgb555, rgb565  
  *      07.01.2001      commented u,v interpolation (not required for uv-block-based)  
  *  23.12.2001  removed #ifdefs, added function pointers + init_common()  
  *      22.12.2001      cpu #ifdefs  
  *  19.12.2001  image_dump(); useful for debugging  
  *       6.12.2001      inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>  
  *  
  *************************************************************************/  
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 128  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 198  Line 165 
165          }          }
166    
167    
168  //U          /* U */
169          dst = image->u - (EDGE_SIZE2 + EDGE_SIZE2 * edged_width2);          dst = image->u - (EDGE_SIZE2 + EDGE_SIZE2 * edged_width2);
170          src = image->u;          src = image->u;
171    
# Line 226  Line 193 
193          }          }
194    
195    
196  // V          /* V */
197          dst = image->v - (EDGE_SIZE2 + EDGE_SIZE2 * edged_width2);          dst = image->v - (EDGE_SIZE2 + EDGE_SIZE2 * edged_width2);
198          src = image->v;          src = image->v;
199    
# Line 254  Line 221 
221          }          }
222  }  }
223    
224  // bframe encoding requires image-based u,v interpolation  /* bframe encoding requires image-based u,v interpolation */
225  void  void
226  image_interpolate(const IMAGE * refn,  image_interpolate(const IMAGE * refn,
227                                    IMAGE * refh,                                    IMAGE * refh,
# Line 265  Line 232 
232                                    uint32_t quarterpel,                                    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
 #ifdef BFRAMES  
238          const uint32_t edged_width2 = edged_width / 2;          const uint32_t edged_width2 = edged_width / 2;
239          const uint32_t edged_height2 = edged_height / 2;          const uint32_t edged_height2 = edged_height / 2;
240          const uint32_t offset2 = EDGE_SIZE2 * (edged_width2 + 1);          const uint32_t offset2 = EDGE_SIZE2 * (edged_width2 + 1);
241          const uint32_t stride_add2 = 7 * edged_width2;          const uint32_t stride_add2 = 7 * edged_width2;
242  #endif  #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    
# Line 291  Line 256 
256    
257          if(quarterpel) {          if(quarterpel) {
258    
259                  for (y = 0; y < edged_height; y += 8) {                  for (y = 0; y < (edged_height - EDGE_SIZE); y += 8) {
260                          for (x = 0; x < edged_width; x += 8) {                          for (x = 0; x < (edged_width - EDGE_SIZE); x += 8) {
261                                  interpolate8x8_6tap_lowpass_h(h_ptr, n_ptr, edged_width, rounding);                                  interpolate8x8_6tap_lowpass_h(h_ptr, n_ptr, edged_width, rounding);
262                                  interpolate8x8_6tap_lowpass_v(v_ptr, n_ptr, edged_width, rounding);                                  interpolate8x8_6tap_lowpass_v(v_ptr, n_ptr, edged_width, rounding);
263    
# Line 301  Line 266 
266                                  v_ptr += 8;                                  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;                          h_ptr += stride_add;
274                          v_ptr += stride_add;                          v_ptr += stride_add;
275                          n_ptr += stride_add;                          n_ptr += stride_add;
# Line 309  Line 278 
278                  h_ptr = refh->y;                  h_ptr = refh->y;
279                  h_ptr -= offset;                  h_ptr -= offset;
280    
281                  for (y = 0; y < edged_height; y = y + 8) {                  for (y = 0; y < (edged_height - EDGE_SIZE); y = y + 8) {
282                          for (x = 0; x < edged_width; x = x + 8) {                          for (x = 0; x < (edged_width - EDGE_SIZE); x = x + 8) {
283                                  interpolate8x8_6tap_lowpass_v(hv_ptr, h_ptr, edged_width, rounding);                                  interpolate8x8_6tap_lowpass_v(hv_ptr, h_ptr, edged_width, rounding);
284                                  hv_ptr += 8;                                  hv_ptr += 8;
285                                  h_ptr += 8;                                  h_ptr += 8;
286                          }                          }
287    
288                            hv_ptr += EDGE_SIZE;
289                            h_ptr += EDGE_SIZE;
290    
291                          hv_ptr += stride_add;                          hv_ptr += stride_add;
292                          h_ptr += stride_add;                          h_ptr += stride_add;
293                  }                  }
294          }          }
295          else {          else {
296    
297                  for (y = 0; y < edged_height; y += 8) {                  for (y = 0; y < (edged_height - EDGE_SIZE); y += 8) {
298                          for (x = 0; x < edged_width; x += 8) {                          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);
# Line 333  Line 306 
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;
# Line 459  Line 437 
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)
532  {  {
533            const int edged_width2 = edged_width/2;
534  /*      if (csp & XVID_CSP_VFLIP)          const int width2 = width/2;
535          {          const int height2 = height/2;
536                  height = -height;  #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;          return -1;
638  }  }
639    
640    
641            /* pad out image when the width and/or height is not a multiple of 16 */
642    
643  int          if (width & 15)
644  image_output(IMAGE * image,          {
645                           uint32_t width,                  int i;
646                           int height,                  int pad_width = 16 - (width&15);
647                           uint32_t edged_width,                  for (i = 0; i < height; i++)
                          uint8_t * dst,  
                          uint32_t dst_stride,  
                          int csp)  
648  {  {
649          if (csp & XVID_CSP_VFLIP) {                          memset(image->y + i*edged_width + width,
650                  height = -height;                                   *(image->y + i*edged_width + width - 1), pad_width);
651          }          }
652                    for (i = 0; i < height/2; i++)
         // --- xvid 2.1 compatiblity patch ---  
         // --- remove when xvid_dec_frame->stride equals real stride  
         if ((csp & ~XVID_CSP_VFLIP) == XVID_CSP_RGB555 ||  
                 (csp & ~XVID_CSP_VFLIP) == XVID_CSP_RGB565 ||  
                 (csp & ~XVID_CSP_VFLIP) == XVID_CSP_YUY2 ||  
                 (csp & ~XVID_CSP_VFLIP) == XVID_CSP_YVYU ||  
                 (csp & ~XVID_CSP_VFLIP) == XVID_CSP_UYVY)  
653          {          {
654                  dst_stride *= 2;                          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          else if ((csp & ~XVID_CSP_VFLIP) == XVID_CSP_RGB24)          }
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                  dst_stride *= 3;                          memcpy(image->y + (height+i)*edged_width,
669                                       image->y + (height-1)*edged_width,length);
670          }          }
671          else if ((csp & ~XVID_CSP_VFLIP) == XVID_CSP_RGB32 ||  
672                  (csp & ~XVID_CSP_VFLIP) == XVID_CSP_ABGR ||                  for (i = 0; i < pad_height/2; i++)
                 (csp & ~XVID_CSP_VFLIP) == XVID_CSP_RGBA)  
673          {          {
674                  dst_stride *= 4;                          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          }          }
         // ^--- xvid 2.1 compatiblity fix ---^  
688    
689    
690    
691    int
692    image_output(IMAGE * image,
693                             uint32_t width,
694                             int height,
695                             uint32_t edged_width,
696                             uint8_t * dst[4],
697                             uint32_t dst_stride[4],
698                             int csp,
699                             int interlacing)
700    {
701            const int edged_width2 = edged_width/2;
702            int height2 = height/2;
703    
704    /*
705            if (interlacing)
706                    image_printf(image, edged_width, height, 5,100, "[i]=%i,%i",width,height);
707            image_dump_yuvpgm(image, edged_width, width, height, "\\decode.pgm");
708    */
709    
710          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                            interlacing?yv12_to_bgri_c:yv12_to_bgr_c, 3);
733                    return 0;
734    
735            case XVID_CSP_BGRA:
736                    safe_packed_conv(
737                            dst[0], dst_stride[0], image->y, image->u, image->v,
738                            edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
739                            interlacing?yv12_to_bgrai  :yv12_to_bgra,
740                            interlacing?yv12_to_bgrai_c:yv12_to_bgra_c, 4);
741                  return 0;                  return 0;
742    
743          case XVID_CSP_ABGR:          case XVID_CSP_ABGR:
744                  yv12_to_abgr(dst, dst_stride, image->y, image->u, image->v,                  safe_packed_conv(
745                                            edged_width, edged_width / 2, width, height);                          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;                  return 0;
750    
751          case XVID_CSP_RGBA:          case XVID_CSP_RGBA:
752                  yv12_to_rgba(dst, dst_stride, image->y, image->u, image->v,                  safe_packed_conv(
753                                            edged_width, edged_width / 2, width, height);                          dst[0], dst_stride[0], image->y, image->u, image->v,
754                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
755                            interlacing?yv12_to_rgbai  :yv12_to_rgba,
756          case XVID_CSP_I420:                          interlacing?yv12_to_rgbai_c:yv12_to_rgba_c, 4);
                 yv12_to_yuv(dst, dst_stride, image->y, image->u, image->v, edged_width,  
                                         edged_width / 2, width, height);  
                 return 0;  
   
         case XVID_CSP_YV12:             // u,v swapped  
                 yv12_to_yuv(dst, dst_stride, image->y, image->v, image->u, edged_width,  
                                         edged_width / 2, width, height);  
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          }          }
# Line 677  Line 851 
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
885    
886  #include <stdio.h>  #include <stdio.h>
887  #include <string.h>  #include <string.h>
# Line 701  Line 905 
905  }  }
906    
907    
908  // dump image+edges to yuv pgm files  /* dump image+edges to yuv pgm files */
909    
910  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)
911  {  {
# Line 724  Line 928 
928    
929          return 0;          return 0;
930  }  }
931  */  #endif
932    
933    
934    
# Line 773  Line 977 
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,
# Line 790  Line 993 
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;
# Line 813  Line 1016 
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    

Legend:
Removed from v.1.20.2.5  
changed lines
  Added in v.1.26.2.7

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