[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 677, Tue Nov 26 23:44:11 2002 UTC branches/dev-api-4/xvidcore/src/image/image.c revision 890, Sat Feb 22 08:49:45 2003 UTC
# Line 1  Line 1 
1  /*****************************************************************************  /**************************************************************************
2   *   *
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  - image module -   *      image stuff
5   *   *
6   *  Copyright(C) 2002 Peter Ross <pross@xvid.org>   *      This program is an implementation of a part of one or more MPEG-4
7     *      Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
8     *      to use this software module in hardware or software products are
9     *      advised that its use may infringe existing patents or copyrights, and
10     *      any such use would be at such party's own risk.  The original
11     *      developer of this software module and his/her company, and subsequent
12     *      editors and their companies, will have no liability for use of this
13     *      software or modifications or derivatives thereof.
14   *   *
15   *  This file is part of XviD, a free MPEG-4 video encoder/decoder   *      This program is free software; you can redistribute it and/or modify
16   *   *      it under the terms of the GNU General Public License as published by
  *  XviD is free software; you can redistribute it and/or modify it  
  *  under the terms of the GNU General Public License as published by  
17   *  the Free Software Foundation; either version 2 of the License, or   *  the Free Software Foundation; either version 2 of the License, or
18   *  (at your option) any later version.   *  (at your option) any later version.
19   *   *
# Line 19  Line 24 
24   *   *
25   *  You should have received a copy of the GNU General Public License   *  You should have received a copy of the GNU General Public License
26   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
27   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *  
  *  Under section 8 of the GNU General Public License, the copyright  
  *  holders of XVID explicitly forbid distribution in the following  
  *  countries:  
  *  
  *    - Japan  
  *    - United States of America  
  *  
  *  Linking XviD statically or dynamically with other modules is making a  
  *  combined work based on XviD.  Thus, the terms and conditions of the  
  *  GNU General Public License cover the whole combination.  
28   *   *
29   *  As a special exception, the copyright holders of XviD give you   *************************************************************************/
30   *  permission to link XviD with independent modules that communicate with  
31   *  XviD solely through the VFW1.1 and DShow interfaces, regardless of the  /**************************************************************************
  *  license terms of these independent modules, and to copy and distribute  
  *  the resulting combined work under terms of your choice, provided that  
  *  every copy of the combined work is accompanied by a complete copy of  
  *  the source code of XviD (the version of XviD used to produce the  
  *  combined work), being distributed under the terms of the GNU General  
  *  Public License plus this exception.  An independent module is a module  
  *  which is not derived from or based on XviD.  
32   *   *
33   *  Note that people who make modified versions of XviD are not obligated   *      History:
  *  to grant this special exception for their modified versions; it is  
  *  their choice whether to do so.  The GNU General Public License gives  
  *  permission to release a modified version without this exception; this  
  *  exception also makes it possible to release a modified version which  
  *  carries forward this exception.  
34   *   *
35   * $Id: image.c,v 1.25 2002-11-26 23:44:10 edgomez Exp $   *  05.10.2002  support for interpolated images in qpel mode - Isibaar
36     *      01.05.2002      BFRAME image-based u,v interpolation
37     *  22.04.2002  added some B-frame support
38     *      14.04.2002      added image_dump_yuvpgm(), added image_mad()
39     *              XVID_CSP_USER input support
40     *  09.04.2002  PSNR calculations - Isibaar
41     *      06.04.2002      removed interlaced edging from U,V blocks (as per spec)
42     *  26.03.2002  interlacing support (field-based edging in set_edges)
43     *      26.01.2002      rgb555, rgb565
44     *      07.01.2001      commented u,v interpolation (not required for uv-block-based)
45     *  23.12.2001  removed #ifdefs, added function pointers + init_common()
46     *      22.12.2001      cpu #ifdefs
47     *  19.12.2001  image_dump(); useful for debugging
48     *       6.12.2001      inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>
49   *   *
50   ****************************************************************************/   *************************************************************************/
51    
52  #include <stdlib.h>  #include <stdlib.h>
53  #include <string.h>                             /* memcpy, memset */  #include <string.h>                             // memcpy, memset
54  #include <math.h>  #include <math.h>
55    
56  #include "../portab.h"  #include "../portab.h"
57  #include "../xvid.h"                    /* XVID_CSP_XXX's */  #include "../global.h"                  // XVID_CSP_XXX's
58    #include "../xvid.h"                    // XVID_CSP_XXX's
59  #include "image.h"  #include "image.h"
60  #include "colorspace.h"  #include "colorspace.h"
61  #include "interpolate8x8.h"  #include "interpolate8x8.h"
62  #include "../divx4.h"  #include "reduced.h"
63  #include "../utils/mem_align.h"  #include "../utils/mem_align.h"
64    
65    #include "font.h"               // XXX: remove later
66    
67  #define SAFETY  64  #define SAFETY  64
68  #define EDGE_SIZE2  (EDGE_SIZE/2)  #define EDGE_SIZE2  (EDGE_SIZE/2)
69    
# Line 203  Line 201 
201          }          }
202    
203    
204  /*U */  //U
205          dst = image->u - (EDGE_SIZE2 + EDGE_SIZE2 * edged_width2);          dst = image->u - (EDGE_SIZE2 + EDGE_SIZE2 * edged_width2);
206          src = image->u;          src = image->u;
207    
# Line 231  Line 229 
229          }          }
230    
231    
232  /* V */  // V
233          dst = image->v - (EDGE_SIZE2 + EDGE_SIZE2 * edged_width2);          dst = image->v - (EDGE_SIZE2 + EDGE_SIZE2 * edged_width2);
234          src = image->v;          src = image->v;
235    
# Line 259  Line 257 
257          }          }
258  }  }
259    
260  /* bframe encoding requires image-based u,v interpolation */  // bframe encoding requires image-based u,v interpolation
261  void  void
262  image_interpolate(const IMAGE * refn,  image_interpolate(const IMAGE * refn,
263                                    IMAGE * refh,                                    IMAGE * refh,
# Line 267  Line 265 
265                                    IMAGE * refhv,                                    IMAGE * refhv,
266                                    uint32_t edged_width,                                    uint32_t edged_width,
267                                    uint32_t edged_height,                                    uint32_t edged_height,
268                                      uint32_t quarterpel,
269                                    uint32_t rounding)                                    uint32_t rounding)
270  {  {
271          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
272          const uint32_t stride_add = 7 * edged_width;          const uint32_t stride_add = 7 * edged_width;
273    /*
274    #ifdef BFRAMES
275            const uint32_t edged_width2 = edged_width / 2;
276            const uint32_t edged_height2 = edged_height / 2;
277            const uint32_t offset2 = EDGE_SIZE2 * (edged_width2 + 1);
278            const uint32_t stride_add2 = 7 * edged_width2;
279    #endif
280    */
281          uint8_t *n_ptr, *h_ptr, *v_ptr, *hv_ptr;          uint8_t *n_ptr, *h_ptr, *v_ptr, *hv_ptr;
282          uint32_t x, y;          uint32_t x, y;
283    
# Line 286  Line 292 
292          v_ptr -= offset;          v_ptr -= offset;
293          hv_ptr -= offset;          hv_ptr -= offset;
294    
295          for (y = 0; y < edged_height; y = y + 8) {          if(quarterpel) {
296                  for (x = 0; x < edged_width; x = x + 8) {  
297                    for (y = 0; y < (edged_height - EDGE_SIZE); y += 8) {
298                            for (x = 0; x < (edged_width - EDGE_SIZE); x += 8) {
299                                    interpolate8x8_6tap_lowpass_h(h_ptr, n_ptr, edged_width, rounding);
300                                    interpolate8x8_6tap_lowpass_v(v_ptr, n_ptr, edged_width, rounding);
301    
302                                    n_ptr += 8;
303                                    h_ptr += 8;
304                                    v_ptr += 8;
305                            }
306    
307                            n_ptr += EDGE_SIZE;
308                            h_ptr += EDGE_SIZE;
309                            v_ptr += EDGE_SIZE;
310    
311                            h_ptr += stride_add;
312                            v_ptr += stride_add;
313                            n_ptr += stride_add;
314                    }
315    
316                    h_ptr = refh->y;
317                    h_ptr -= offset;
318    
319                    for (y = 0; y < (edged_height - EDGE_SIZE); y = y + 8) {
320                            for (x = 0; x < (edged_width - EDGE_SIZE); x = x + 8) {
321                                    interpolate8x8_6tap_lowpass_v(hv_ptr, h_ptr, edged_width, rounding);
322                                    hv_ptr += 8;
323                                    h_ptr += 8;
324                            }
325    
326                            hv_ptr += EDGE_SIZE;
327                            h_ptr += EDGE_SIZE;
328    
329                            hv_ptr += stride_add;
330                            h_ptr += stride_add;
331                    }
332            }
333            else {
334    
335                    for (y = 0; y < (edged_height - EDGE_SIZE); y += 8) {
336                            for (x = 0; x < (edged_width - EDGE_SIZE); x += 8) {
337                          interpolate8x8_halfpel_h(h_ptr, n_ptr, edged_width, rounding);                          interpolate8x8_halfpel_h(h_ptr, n_ptr, edged_width, rounding);
338                          interpolate8x8_halfpel_v(v_ptr, n_ptr, edged_width, rounding);                          interpolate8x8_halfpel_v(v_ptr, n_ptr, edged_width, rounding);
339                          interpolate8x8_halfpel_hv(hv_ptr, n_ptr, edged_width, rounding);                          interpolate8x8_halfpel_hv(hv_ptr, n_ptr, edged_width, rounding);
# Line 297  Line 343 
343                          v_ptr += 8;                          v_ptr += 8;
344                          hv_ptr += 8;                          hv_ptr += 8;
345                  }                  }
346    
347                            h_ptr += EDGE_SIZE;
348                            v_ptr += EDGE_SIZE;
349                            hv_ptr += EDGE_SIZE;
350                            n_ptr += EDGE_SIZE;
351    
352                  h_ptr += stride_add;                  h_ptr += stride_add;
353                  v_ptr += stride_add;                  v_ptr += stride_add;
354                  hv_ptr += stride_add;                  hv_ptr += stride_add;
355                  n_ptr += stride_add;                  n_ptr += stride_add;
356          }          }
357            }
358    /*
359    #ifdef BFRAMES
360            n_ptr = refn->u;
361            h_ptr = refh->u;
362            v_ptr = refv->u;
363            hv_ptr = refhv->u;
364    
365            n_ptr -= offset2;
366            h_ptr -= offset2;
367            v_ptr -= offset2;
368            hv_ptr -= offset2;
369    
370            for (y = 0; y < edged_height2; y += 8) {
371                    for (x = 0; x < edged_width2; x += 8) {
372                            interpolate8x8_halfpel_h(h_ptr, n_ptr, edged_width2, rounding);
373                            interpolate8x8_halfpel_v(v_ptr, n_ptr, edged_width2, rounding);
374                            interpolate8x8_halfpel_hv(hv_ptr, n_ptr, edged_width2, rounding);
375    
376                            n_ptr += 8;
377                            h_ptr += 8;
378                            v_ptr += 8;
379                            hv_ptr += 8;
380                    }
381                    h_ptr += stride_add2;
382                    v_ptr += stride_add2;
383                    hv_ptr += stride_add2;
384                    n_ptr += stride_add2;
385            }
386    
387            n_ptr = refn->v;
388            h_ptr = refh->v;
389            v_ptr = refv->v;
390            hv_ptr = refhv->v;
391    
392            n_ptr -= offset2;
393            h_ptr -= offset2;
394            v_ptr -= offset2;
395            hv_ptr -= offset2;
396    
397            for (y = 0; y < edged_height2; y = y + 8) {
398                    for (x = 0; x < edged_width2; x = x + 8) {
399                            interpolate8x8_halfpel_h(h_ptr, n_ptr, edged_width2, rounding);
400                            interpolate8x8_halfpel_v(v_ptr, n_ptr, edged_width2, rounding);
401                            interpolate8x8_halfpel_hv(hv_ptr, n_ptr, edged_width2, rounding);
402    
403                            n_ptr += 8;
404                            h_ptr += 8;
405                            v_ptr += 8;
406                            hv_ptr += 8;
407                    }
408                    h_ptr += stride_add2;
409                    v_ptr += stride_add2;
410                    hv_ptr += stride_add2;
411                    n_ptr += stride_add2;
412            }
413    #endif
414    */
415          /*          /*
416             interpolate_halfpel_h(             interpolate_halfpel_h(
417             refh->y - offset,             refh->y - offset,
# Line 366  Line 475 
475  }  }
476    
477    
478    /*
479    chroma optimize filter, invented by mf
480    a chroma pixel is average from the surrounding pixels, when the
481    correpsonding luma pixels are pure black or white.
482    */
483    
484    void
485    image_chroma_optimize(IMAGE * img, int width, int height, int edged_width)
486    {
487            int x,y;
488            int pixels = 0;
489    
490            for (y = 1; y < height/2 - 1; y++)
491            for (x = 1; x < width/2 - 1; x++)
492            {
493    #define IS_PURE(a)  ((a)<=16||(a)>=235)
494    #define IMG_Y(Y,X)      img->y[(Y)*edged_width + (X)]
495    #define IMG_U(Y,X)      img->u[(Y)*edged_width/2 + (X)]
496    #define IMG_V(Y,X)      img->v[(Y)*edged_width/2 + (X)]
497    
498                    if (IS_PURE(IMG_Y(y*2  ,x*2  )) &&
499                            IS_PURE(IMG_Y(y*2  ,x*2+1)) &&
500                            IS_PURE(IMG_Y(y*2+1,x*2  )) &&
501                            IS_PURE(IMG_Y(y*2+1,x*2+1)))
502                    {
503                            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;
504                            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;
505                            pixels++;
506                    }
507    
508    #undef IS_PURE
509    #undef IMG_Y
510    #undef IMG_U
511    #undef IMG_V
512            }
513    
514            DPRINTF(DPRINTF_DEBUG,"chroma_optimized_pixels = %i/%i", pixels, width*height/4);
515    }
516    
517    
518    
519    
520    
521    /*
522      perform safe packed colorspace conversion, by splitting
523      the image up into an optimized area (pixel width divisible by 16),
524      and two unoptimized/plain-c areas (pixel width divisible by 2)
525    */
526    
527    static void
528    safe_packed_conv(uint8_t * x_ptr, int x_stride,
529                                     uint8_t * y_ptr, uint8_t * u_ptr, uint8_t * v_ptr,
530                                     int y_stride, int uv_stride,
531                                     int width, int height, int vflip,
532                                     packedFunc * func_opt, packedFunc func_c, int size)
533    {
534            int width_opt, width_c;
535    
536            if (func_opt != func_c && x_stride < size*((width+15)/16)*16)
537            {
538                    width_opt = width & (~15);
539                    width_c = width - width_opt;
540            }
541            else
542            {
543                    width_opt = width;
544                    width_c = 0;
545            }
546    
547            func_opt(x_ptr, x_stride,
548                            y_ptr, u_ptr, v_ptr, y_stride, uv_stride,
549                            width_opt, height, vflip);
550    
551            if (width_c)
552            {
553                    func_c(x_ptr + size*width_opt, x_stride,
554                            y_ptr + width_opt, u_ptr + width_opt/2, v_ptr + width_opt/2,
555                            y_stride, uv_stride, width_c, height, vflip);
556            }
557    }
558    
559    
560    
561  int  int
562  image_input(IMAGE * image,  image_input(IMAGE * image,
563                          uint32_t width,                          uint32_t width,
564                          int height,                          int height,
565                          uint32_t edged_width,                          uint32_t edged_width,
566                          uint8_t * src,                          uint8_t * src[4],
567                          int csp)                          int src_stride[4],
568  {                          int csp,
569                            int interlacing)
 /*      if (csp & XVID_CSP_VFLIP)  
570          {          {
571                  height = -height;          const int edged_width2 = edged_width/2;
572          }          const int width2 = width/2;
573  */          const int height2 = height/2;
574            //const int height_signed = (csp & XVID_CSP_VFLIP) ? -height : height;
575    
576          switch (csp & ~XVID_CSP_VFLIP) {          switch (csp & ~XVID_CSP_VFLIP) {
577          case XVID_CSP_RGB555:          case XVID_CSP_RGB555:
578                  rgb555_to_yv12(image->y, image->u, image->v, src, width, height,                  safe_packed_conv(
579                                             edged_width);                          src[0], src_stride[0], image->y, image->u, image->v,
580                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
581                            interlacing?rgb555i_to_yv12  :rgb555_to_yv12,
582                            interlacing?rgb555i_to_yv12_c:rgb555_to_yv12_c, 2);
583                    break;
584    
585          case XVID_CSP_RGB565:          case XVID_CSP_RGB565:
586                  rgb565_to_yv12(image->y, image->u, image->v, src, width, height,                  safe_packed_conv(
587                                             edged_width);                          src[0], src_stride[0], image->y, image->u, image->v,
588                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
589                            interlacing?rgb565i_to_yv12  :rgb565_to_yv12,
590                            interlacing?rgb565i_to_yv12_c:rgb565_to_yv12_c, 2);
591                    break;
592    
593    
594          case XVID_CSP_RGB24:          case XVID_CSP_BGR:
595                  rgb24_to_yv12(image->y, image->u, image->v, src, width, height,                  safe_packed_conv(
596                                            edged_width);                          src[0], src_stride[0], image->y, image->u, image->v,
597                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
598                            interlacing?bgri_to_yv12  :bgr_to_yv12,
599                            interlacing?bgri_to_yv12_c:bgr_to_yv12_c, 3);
600                    break;
601    
602          case XVID_CSP_RGB32:          case XVID_CSP_BGRA:
603                  rgb32_to_yv12(image->y, image->u, image->v, src, width, height,                  safe_packed_conv(
604                                            edged_width);                          src[0], src_stride[0], image->y, image->u, image->v,
605                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
606                            interlacing?bgrai_to_yv12  :bgra_to_yv12,
607                            interlacing?bgrai_to_yv12_c:bgra_to_yv12_c, 4);
608                    break;
609    
610          case XVID_CSP_I420:          case XVID_CSP_ABGR :
611                  yuv_to_yv12(image->y, image->u, image->v, src, width, height,                  safe_packed_conv(
612                                          edged_width);                          src[0], src_stride[0], image->y, image->u, image->v,
613                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
614                            interlacing?abgri_to_yv12  :abgr_to_yv12,
615                            interlacing?abgri_to_yv12_c:abgr_to_yv12_c, 4);
616                    break;
617    
618          case XVID_CSP_YV12:             /* u/v swapped */          case XVID_CSP_RGBA :
619                  yuv_to_yv12(image->y, image->v, image->u, src, width, height,                  safe_packed_conv(
620                                          edged_width);                          src[0], src_stride[0], image->y, image->u, image->v,
621                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
622                            interlacing?rgbai_to_yv12  :rgba_to_yv12,
623                            interlacing?rgbai_to_yv12_c:rgba_to_yv12_c, 4);
624                    break;
625    
626          case XVID_CSP_YUY2:          case XVID_CSP_YUY2:
627                  yuyv_to_yv12(image->y, image->u, image->v, src, width, height,                  safe_packed_conv(
628                                           edged_width);                          src[0], src_stride[0], image->y, image->u, image->v,
629                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
630                            interlacing?yuyvi_to_yv12  :yuyv_to_yv12,
631                            interlacing?yuyvi_to_yv12_c:yuyv_to_yv12_c, 2);
632                    break;
633    
634          case XVID_CSP_YVYU:             /* u/v swapped */          case XVID_CSP_YVYU:             /* u/v swapped */
635                  yuyv_to_yv12(image->y, image->v, image->u, src, width, height,                  safe_packed_conv(
636                                           edged_width);                          src[0], src_stride[0], image->y, image->v, image->y,
637                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
638                            interlacing?yuyvi_to_yv12  :yuyv_to_yv12,
639                            interlacing?yuyvi_to_yv12_c:yuyv_to_yv12_c, 2);
640                    break;
641    
642          case XVID_CSP_UYVY:          case XVID_CSP_UYVY:
643                  uyvy_to_yv12(image->y, image->u, image->v, src, width, height,                  safe_packed_conv(
644                                           edged_width);                          src[0], src_stride[0], image->y, image->u, image->v,
645                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
646                            interlacing?uyvyi_to_yv12  :uyvy_to_yv12,
647                            interlacing?uyvyi_to_yv12_c:uyvy_to_yv12_c, 2);
648                    break;
649    
650            case XVID_CSP_I420:
651                    yv12_to_yv12(image->y, image->u, image->v, edged_width, edged_width2,
652                            src[0], src[0] + src_stride[0]*height, src[0] + src_stride[0]*height + (src_stride[0]/2)*height2,
653                            src_stride[0], src_stride[0]/2, width, height, (csp & XVID_CSP_VFLIP));
654                    break
655                            ;
656            case XVID_CSP_YV12:             /* u/v swapped */
657                    yv12_to_yv12(image->y, image->v, image->u, edged_width, edged_width2,
658                            src[0], src[0] + src_stride[0]*height, src[0] + src_stride[0]*height + (src_stride[0]/2)*height2,
659                            src_stride[0], src_stride[0]/2, width, height, (csp & XVID_CSP_VFLIP));
660                    break;
661    
662          case XVID_CSP_USER:          case XVID_CSP_USER:
663                  user_to_yuv_c(image->y, image->u, image->v, edged_width,          /*XXX: support for different u & v strides */
664                                            (DEC_PICTURE *) src, width, height);                  yv12_to_yv12(image->y, image->u, image->v, edged_width, edged_width2,
665                  return 0;                          src[0], src[1], src[2], src_stride[0], src_stride[1],
666                            width, height, (csp & XVID_CSP_VFLIP));
667                    break;
668    
669          case XVID_CSP_NULL:          case XVID_CSP_NULL:
670                  break;                  break;
671    
672            default :
673                    return -1;
674          }          }
675    
676          return -1;  
677            /* pad out image when the width and/or height is not a multiple of 16 */
678    
679            if (width & 15)
680            {
681                    int i;
682                    int pad_width = 16 - (width&15);
683                    for (i = 0; i < height; i++)
684                    {
685                            memset(image->y + i*edged_width + width,
686                                     *(image->y + i*edged_width + width - 1), pad_width);
687                    }
688                    for (i = 0; i < height/2; i++)
689                    {
690                            memset(image->u + i*edged_width2 + width2,
691                                     *(image->u + i*edged_width2 + width2 - 1),pad_width/2);
692                            memset(image->v + i*edged_width2 + width2,
693                                     *(image->v + i*edged_width2 + width2 - 1),pad_width/2);
694                    }
695            }
696    
697            if (height & 15)
698            {
699                    int pad_height = 16 - (height&15);
700                    int length = ((width+15)/16)*16;
701                    int i;
702                    for (i = 0; i < pad_height; i++)
703                    {
704                            memcpy(image->y + (height+i)*edged_width,
705                                       image->y + (height-1)*edged_width,length);
706                    }
707    
708                    for (i = 0; i < pad_height/2; i++)
709                    {
710                            memcpy(image->u + (height2+i)*edged_width2,
711                                       image->u + (height2-1)*edged_width2,length/2);
712                            memcpy(image->v + (height2+i)*edged_width2,
713                                       image->v + (height2-1)*edged_width2,length/2);
714                    }
715            }
716    
717    /*
718            if (interlacing)
719                    image_printf(image, edged_width, height, 5,5, "[i]");
720            image_dump_yuvpgm(image, edged_width, ((width+15)/16)*16, ((height+15)/16)*16, "\\encode.pgm");
721    */
722            return 0;
723  }  }
724    
725    
# Line 448  Line 729 
729                           uint32_t width,                           uint32_t width,
730                           int height,                           int height,
731                           uint32_t edged_width,                           uint32_t edged_width,
732                           uint8_t * dst,                           uint8_t * dst[4],
733                           uint32_t dst_stride,                           uint32_t dst_stride[4],
734                           int csp)                           int csp,
735                             int interlacing)
736  {  {
737          if (csp & XVID_CSP_VFLIP) {          const int edged_width2 = edged_width/2;
738                  height = -height;          int height2 = height/2;
739          }  
740    /*
741            if (interlacing)
742                    image_printf(image, edged_width, height, 5,100, "[i]=%i,%i",width,height);
743            image_dump_yuvpgm(image, edged_width, width, height, "\\decode.pgm");
744    */
745    
746          switch (csp & ~XVID_CSP_VFLIP) {          switch (csp & ~XVID_CSP_VFLIP) {
747          case XVID_CSP_RGB555:          case XVID_CSP_RGB555:
748                  yv12_to_rgb555(dst, dst_stride, image->y, image->u, image->v,                  safe_packed_conv(
749                                             edged_width, edged_width / 2, width, height);                          dst[0], dst_stride[0], image->y, image->u, image->v,
750                            edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
751                            interlacing?yv12_to_rgb555i  :yv12_to_rgb555,
752                            interlacing?yv12_to_rgb555i_c:yv12_to_rgb555_c, 2);
753                  return 0;                  return 0;
754    
755          case XVID_CSP_RGB565:          case XVID_CSP_RGB565:
756                  yv12_to_rgb565(dst, dst_stride, image->y, image->u, image->v,                  safe_packed_conv(
757                                             edged_width, edged_width / 2, width, height);                          dst[0], dst_stride[0], image->y, image->u, image->v,
758                  return 0;                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
759                            interlacing?yv12_to_rgb565i  :yv12_to_rgb565,
760          case XVID_CSP_RGB24:                          interlacing?yv12_to_rgb565i_c:yv12_to_rgb565_c, 2);
761                  yv12_to_rgb24(dst, dst_stride, image->y, image->u, image->v,                  return 0;
762                                            edged_width, edged_width / 2, width, height);  
763                  return 0;      case XVID_CSP_BGR:
764                    safe_packed_conv(
765          case XVID_CSP_RGB32:                          dst[0], dst_stride[0], image->y, image->u, image->v,
766                  yv12_to_rgb32(dst, dst_stride, image->y, image->u, image->v,                          edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
767                                            edged_width, edged_width / 2, width, height);                          interlacing?yv12_to_bgri  :yv12_to_bgr,
768                  return 0;                          interlacing?yv12_to_bgri_c:yv12_to_bgr_c, 3);
769                    return 0;
770          case XVID_CSP_I420:  
771                  yv12_to_yuv(dst, dst_stride, image->y, image->u, image->v, edged_width,          case XVID_CSP_BGRA:
772                                          edged_width / 2, width, height);                  safe_packed_conv(
773                  return 0;                          dst[0], dst_stride[0], image->y, image->u, image->v,
774                            edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
775          case XVID_CSP_YV12:             /* u,v swapped */                          interlacing?yv12_to_bgrai  :yv12_to_bgra,
776                  yv12_to_yuv(dst, dst_stride, image->y, image->v, image->u, edged_width,                          interlacing?yv12_to_bgrai_c:yv12_to_bgra_c, 4);
777                                          edged_width / 2, width, height);                  return 0;
778    
779            case XVID_CSP_ABGR:
780                    safe_packed_conv(
781                            dst[0], dst_stride[0], image->y, image->u, image->v,
782                            edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
783                            interlacing?yv12_to_abgri  :yv12_to_abgr,
784                            interlacing?yv12_to_abgri_c:yv12_to_abgr_c, 4);
785                    return 0;
786    
787            case XVID_CSP_RGBA:
788                    safe_packed_conv(
789                            dst[0], dst_stride[0], image->y, image->u, image->v,
790                            edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
791                            interlacing?yv12_to_rgbai  :yv12_to_rgba,
792                            interlacing?yv12_to_rgbai_c:yv12_to_rgba_c, 4);
793                  return 0;                  return 0;
794    
795          case XVID_CSP_YUY2:          case XVID_CSP_YUY2:
796                  yv12_to_yuyv(dst, dst_stride, image->y, image->u, image->v,                  safe_packed_conv(
797                                           edged_width, edged_width / 2, width, height);                          dst[0], dst_stride[0], image->y, image->u, image->v,
798                            edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
799                            interlacing?yv12_to_yuyvi  :yv12_to_yuyv,
800                            interlacing?yv12_to_yuyvi_c:yv12_to_yuyv_c, 2);
801                  return 0;                  return 0;
802    
803          case XVID_CSP_YVYU:             /* u,v swapped */          case XVID_CSP_YVYU:             // u,v swapped
804                  yv12_to_yuyv(dst, dst_stride, image->y, image->v, image->u,                  safe_packed_conv(
805                                           edged_width, edged_width / 2, width, height);                          dst[0], dst_stride[0], image->y, image->v, image->u,
806                            edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
807                            interlacing?yv12_to_yuyvi  :yv12_to_yuyv,
808                            interlacing?yv12_to_yuyvi_c:yv12_to_yuyv_c, 2);
809                  return 0;                  return 0;
810    
811          case XVID_CSP_UYVY:          case XVID_CSP_UYVY:
812                  yv12_to_uyvy(dst, dst_stride, image->y, image->u, image->v,                  safe_packed_conv(
813                                           edged_width, edged_width / 2, width, height);                          dst[0], dst_stride[0], image->y, image->u, image->v,
814                            edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP),
815                            interlacing?yv12_to_uyvyi  :yv12_to_uyvy,
816                            interlacing?yv12_to_uyvyi_c:yv12_to_uyvy_c, 2);
817                  return 0;                  return 0;
818    
819          case XVID_CSP_USER:          case XVID_CSP_I420:
820                  ((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,
821                  ((DEC_PICTURE *) dst)->u = image->u;                          dst_stride[0], dst_stride[0]/2,
822                  ((DEC_PICTURE *) dst)->v = image->v;                          image->y, image->u, image->v, edged_width, edged_width2,
823                  ((DEC_PICTURE *) dst)->stride_y = edged_width;                          width, height, (csp & XVID_CSP_VFLIP));
824                  ((DEC_PICTURE *) dst)->stride_uv = edged_width / 2;                  return 0;
825    
826            case XVID_CSP_YV12:             // u,v swapped
827                    yv12_to_yv12(dst[0], dst[0] + dst_stride[0]*height, dst[0] + dst_stride[0]*height + (dst_stride[0]/2)*height2,
828                            dst_stride[0], dst_stride[0]/2,
829                            image->y, image->v, image->u, edged_width, edged_width2,
830                            width, height, (csp & XVID_CSP_VFLIP));
831                    return 0;
832    
833            case XVID_CSP_USER :            // u,v swapped
834                    yv12_to_yv12(dst[0], dst[1], dst[2],
835                            dst_stride[0], dst_stride[1],   /* v: dst_stride[2] */
836                            image->y, image->v, image->u, edged_width, edged_width2,
837                            width, height, (csp & XVID_CSP_VFLIP));
838                    return 0;
839    
840            case XVID_CSP_INTERNAL :
841                    dst[0] = image->y;
842                    dst[1] = image->u;
843                    dst[2] = image->v;
844                    dst_stride[0] = edged_width;
845                    dst_stride[1] = edged_width/2;
846                    dst_stride[2] = edged_width/2;
847                  return 0;                  return 0;
848    
849          case XVID_CSP_NULL:          case XVID_CSP_NULL:
850          case XVID_CSP_EXTERN:          case XVID_CSP_SLICE:
851                  return 0;                  return 0;
852    
853          }          }
# Line 551  Line 887 
887          return psnr_y;          return psnr_y;
888  }  }
889    
890  #if     0  
891    float sse_to_PSNR(long sse, int pixels)
892    {
893            if (sse==0)
894                    return 99.99F;
895    
896            return 48.131F - 10*(float)log10((float)sse/(float)(pixels));   // log10(255*255)=4.8131
897    
898    }
899    
900    long plane_sse(uint8_t * orig,
901                       uint8_t * recon,
902                       uint16_t stride,
903                       uint16_t width,
904                       uint16_t height)
905    {
906            int diff, x, y;
907            long sse=0;
908    
909            for (y = 0; y < height; y++) {
910                    for (x = 0; x < width; x++) {
911                            diff = *(orig + x) - *(recon + x);
912                            sse += diff * diff;
913                    }
914                    orig += stride;
915                    recon += stride;
916            }
917            return sse;
918    }
919    
920    /*
921    
922  #include <stdio.h>  #include <stdio.h>
923  #include <string.h>  #include <string.h>
# Line 575  Line 941 
941  }  }
942    
943    
944  /* dump image+edges to yuv pgm files  */  // dump image+edges to yuv pgm files
945    
946  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)
947  {  {
# Line 598  Line 964 
964    
965          return 0;          return 0;
966  }  }
967  #endif  */
968    
969    
970    
# Line 647  Line 1013 
1013  }  }
1014    
1015    
 #define ABS(X)    (((X)>0)?(X):-(X))  
1016  float  float
1017  image_mad(const IMAGE * img1,  image_mad(const IMAGE * img1,
1018                    const IMAGE * img2,                    const IMAGE * img2,
# Line 678  Line 1043 
1043  }  }
1044    
1045  void  void
1046  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) {
1047    uint8_t *dY,*dU,*dV,*sY,*sU,*sV;    uint8_t *dY,*dU,*dV,*sY,*sU,*sV;
1048    int std2 = std >> 1;    int std2 = std >> 1;
1049    int w = mbl << 4, w2,i;    int w = mbl << 4, w2,i;
# Line 687  Line 1052 
1052      w = width;      w = width;
1053    w2 = w >> 1;    w2 = w >> 1;
1054    
1055    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);
1056    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);
1057    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);
1058    sY = cur->y + (mby << 4) * std + (mbx << 4);    sY = cur->y + (mby << 4) * std + (mbx << 4);
1059    sU = cur->u + (mby << 3) * std2 + (mbx << 3);    sU = cur->u + (mby << 3) * std2 + (mbx << 3);
1060    sV = cur->v + (mby << 3) * std2 + (mbx << 3);    sV = cur->v + (mby << 3) * std2 + (mbx << 3);
1061    
1062    for(i = 0 ; i < 16 ; i++) {    for(i = 0 ; i < 16 ; i++) {
1063      memcpy(dY,sY,w);      memcpy(dY,sY,w);
1064      dY += out_frm->stride_y;      dY += out_frm->stride[0];
1065      sY += std;      sY += std;
1066    }    }
1067    for(i = 0 ; i < 8 ; i++) {    for(i = 0 ; i < 8 ; i++) {
1068      memcpy(dU,sU,w2);      memcpy(dU,sU,w2);
1069      dU += out_frm->stride_u;      dU += out_frm->stride[1];
1070      sU += std2;      sU += std2;
1071    }    }
1072    for(i = 0 ; i < 8 ; i++) {    for(i = 0 ; i < 8 ; i++) {
1073      memcpy(dV,sV,w2);      memcpy(dV,sV,w2);
1074      dV += out_frm->stride_v;      dV += out_frm->stride[2];
1075      sV += std2;      sV += std2;
1076    }    }
1077  }  }
1078    
1079    
1080    void
1081    image_clear(IMAGE * img, int width, int height, int edged_width,
1082                                            int y, int u, int v)
1083    {
1084            uint8_t * p;
1085            int i;
1086    
1087            p = img->y;
1088            for (i = 0; i < height; i++) {
1089                    memset(p, y, width);
1090                    p += edged_width;
1091            }
1092    
1093            p = img->u;
1094            for (i = 0; i < height/2; i++) {
1095                    memset(p, u, width/2);
1096                    p += edged_width/2;
1097            }
1098    
1099            p = img->v;
1100            for (i = 0; i < height/2; i++) {
1101                    memset(p, v, width/2);
1102                    p += edged_width/2;
1103            }
1104    }
1105    
1106    
1107    /* reduced resolution deblocking filter
1108            block = block size (16=rrv, 8=full resolution)
1109            flags = XVID_DEC_YDEBLOCK|XVID_DEC_UVDEBLOCK
1110    */
1111    void
1112    image_deblock_rrv(IMAGE * img, int edged_width,
1113                                    const MACROBLOCK * mbs, int mb_width, int mb_height, int mb_stride,
1114                                    int block, int flags)
1115    {
1116            const int edged_width2 = edged_width /2;
1117            const int nblocks = block / 8;  /* skals code uses 8pixel block uints */
1118            int i,j;
1119    
1120            /* luma: j,i in block units */
1121    
1122                    for (j = 1; j < mb_height*2; j++)               /* horizontal deblocking */
1123                    for (i = 0; i < mb_width*2; i++)
1124                    {
1125                            if (mbs[(j-1)/2*mb_stride + (i/2)].mode != MODE_NOT_CODED ||
1126                                    mbs[(j+0)/2*mb_stride + (i/2)].mode != MODE_NOT_CODED)
1127                            {
1128                                    hfilter_31(img->y + (j*block - 1)*edged_width + i*block,
1129                                                                      img->y + (j*block + 0)*edged_width + i*block, nblocks);
1130                            }
1131                    }
1132    
1133                    for (j = 0; j < mb_height*2; j++)               /* vertical deblocking */
1134                    for (i = 1; i < mb_width*2; i++)
1135                    {
1136                            if (mbs[(j/2)*mb_stride + (i-1)/2].mode != MODE_NOT_CODED ||
1137                                    mbs[(j/2)*mb_stride + (i+0)/2].mode != MODE_NOT_CODED)
1138                            {
1139                                    vfilter_31(img->y + (j*block)*edged_width + i*block - 1,
1140                                                       img->y + (j*block)*edged_width + i*block + 0,
1141                                                       edged_width, nblocks);
1142                            }
1143                    }
1144    
1145    
1146    
1147            /* chroma */
1148    
1149                    for (j = 1; j < mb_height; j++)         /* horizontal deblocking */
1150                    for (i = 0; i < mb_width; i++)
1151                    {
1152                            if (mbs[(j-1)*mb_stride + i].mode != MODE_NOT_CODED ||
1153                                    mbs[(j+0)*mb_stride + i].mode != MODE_NOT_CODED)
1154                            {
1155                                    hfilter_31(img->u + (j*block - 1)*edged_width2 + i*block,
1156                                                       img->u + (j*block + 0)*edged_width2 + i*block, nblocks);
1157                                    hfilter_31(img->v + (j*block - 1)*edged_width2 + i*block,
1158                                                       img->v + (j*block + 0)*edged_width2 + i*block, nblocks);
1159                            }
1160                    }
1161    
1162                    for (j = 0; j < mb_height; j++)         /* vertical deblocking */
1163                    for (i = 1; i < mb_width; i++)
1164                    {
1165                            if (mbs[j*mb_stride + i - 1].mode != MODE_NOT_CODED ||
1166                                    mbs[j*mb_stride + i + 0].mode != MODE_NOT_CODED)
1167                            {
1168                                    vfilter_31(img->u + (j*block)*edged_width2 + i*block - 1,
1169                                                       img->u + (j*block)*edged_width2 + i*block + 0,
1170                                                       edged_width2, nblocks);
1171                                    vfilter_31(img->v + (j*block)*edged_width2 + i*block - 1,
1172                                                       img->v + (j*block)*edged_width2 + i*block + 0,
1173                                                       edged_width2, nblocks);
1174                            }
1175                    }
1176    
1177    
1178    }
1179    

Legend:
Removed from v.677  
changed lines
  Added in v.890

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