home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 9 / CD_ASCQ_09_1193.iso / news / 4441 / mpegcode / src / subsampl.c < prev   
C/C++ Source or Header  |  1993-09-27  |  30KB  |  773 lines

  1. /*===========================================================================*
  2.  * subsample.c                                     *
  3.  *                                         *
  4.  *    Procedures concerned with subsampling                     *
  5.  *                                         *
  6.  * EXPORTED PROCEDURES:                                 *
  7.  *    LumMotionErrorA                                 *
  8.  *    LumMotionErrorB                                 *
  9.  *    LumMotionErrorC                                 *
  10.  *    LumMotionErrorD                                 *
  11.  *                                         *
  12.  *===========================================================================*/
  13.  
  14. /*
  15.  * Copyright (c) 1993 The Regents of the University of California.
  16.  * All rights reserved.
  17.  *
  18.  * Permission to use, copy, modify, and distribute this software and its
  19.  * documentation for any purpose, without fee, and without written agreement is
  20.  * hereby granted, provided that the above copyright notice and the following
  21.  * two paragraphs appear in all copies of this software.
  22.  *
  23.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  24.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  25.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  26.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  *
  28.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  29.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  30.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  31.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  32.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  33.  */
  34.  
  35. /*  
  36.  *  $Header: /n/picasso/users/keving/encode/src/RCS/subsample.c,v 1.3 1993/07/22 22:23:43 keving Exp keving $
  37.  *  $Log: subsample.c,v $
  38.  * Revision 1.3  1993/07/22  22:23:43  keving
  39.  * nothing
  40.  *
  41.  * Revision 1.2  1993/06/30  20:06:09  keving
  42.  * nothing
  43.  *
  44.  * Revision 1.1  1993/06/22  21:56:05  keving
  45.  * nothing
  46.  *
  47.  */
  48.  
  49.  
  50. /*==============*
  51.  * HEADER FILES *
  52.  *==============*/
  53.  
  54. #include "all.h"
  55. #include "mtypes.h"
  56. #include "frames.h"
  57. #include "bitio.h"
  58. #include "prototypes.h"
  59.  
  60. #undef ABS
  61. #define ABS(x)    ((x < 0) ? (-x) : x)
  62.  
  63.  
  64. /*=====================*
  65.  * EXPORTED PROCEDURES *
  66.  *=====================*/
  67.  
  68.  
  69. /*===========================================================================*
  70.  *
  71.  * LumMotionErrorA
  72.  *
  73.  *    compute the motion error for the A subsampling pattern
  74.  *
  75.  * RETURNS:    the error, or some number greater if it is worse
  76.  *
  77.  * SIDE EFFECTS:    none
  78.  *
  79.  *===========================================================================*/
  80. int32
  81. LumMotionErrorA(currentBlock, prevFrame, by, bx, my, mx, bestSoFar)
  82.     LumBlock currentBlock;
  83.     MpegFrame *prevFrame;
  84.     int by;
  85.     int bx;
  86.     int my;
  87.     int mx;
  88.     int32 bestSoFar;
  89. {
  90.     register int32    diff = 0;        /* max value of diff is 255*256 = 65280 */
  91.     register int32 localDiff;
  92.     register uint8 *macross;
  93.     register uint8 **prev;
  94.     register int    fy, fx;
  95.     boolean xHalf, yHalf;
  96.  
  97.     xHalf = (ABS(mx) % 2 == 1);
  98.     yHalf = (ABS(my) % 2 == 1);
  99.  
  100.     MOTION_TO_FRAME_COORD(by, bx, my/2, mx/2, fy, fx);
  101.  
  102.     if ( xHalf ) {
  103.     if ( mx < 0 ) {
  104.         fx--;
  105.     }
  106.  
  107.     if ( yHalf ) {
  108.         if ( my < 0 ) {
  109.         fy--;
  110.         }
  111.         
  112.         prev = prevFrame->halfBoth;
  113.     } else {
  114.         prev = prevFrame->halfX;
  115.     }
  116.     } else if ( yHalf ) {
  117.     if ( my < 0 ) {
  118.         fy--;
  119.     }
  120.  
  121.     prev = prevFrame->halfY;
  122.     } else {
  123.     prev = prevFrame->ref_y;
  124.     }
  125.  
  126.     macross = &(prev[fy][fx]);
  127.  
  128.     localDiff = macross[0]-currentBlock[0][0];     diff += ABS(localDiff);
  129.     localDiff = macross[2]-currentBlock[0][2];     diff += ABS(localDiff);
  130.     localDiff = macross[4]-currentBlock[0][4];     diff += ABS(localDiff);
  131.     localDiff = macross[6]-currentBlock[0][6];     diff += ABS(localDiff);
  132.     localDiff = macross[8]-currentBlock[0][8];     diff += ABS(localDiff);
  133.     localDiff = macross[10]-currentBlock[0][10];     diff += ABS(localDiff);
  134.     localDiff = macross[12]-currentBlock[0][12];     diff += ABS(localDiff);
  135.     localDiff = macross[14]-currentBlock[0][14];     diff += ABS(localDiff);
  136.  
  137.     if ( diff > bestSoFar ) {
  138.     return diff;
  139.     }
  140.  
  141.     macross = &(prev[fy+2][fx]);
  142.  
  143.     localDiff = macross[0]-currentBlock[2][0];     diff += ABS(localDiff);
  144.     localDiff = macross[2]-currentBlock[2][2];     diff += ABS(localDiff);
  145.     localDiff = macross[4]-currentBlock[2][4];     diff += ABS(localDiff);
  146.     localDiff = macross[6]-currentBlock[2][6];     diff += ABS(localDiff);
  147.     localDiff = macross[8]-currentBlock[2][8];     diff += ABS(localDiff);
  148.     localDiff = macross[10]-currentBlock[2][10];     diff += ABS(localDiff);
  149.     localDiff = macross[12]-currentBlock[2][12];     diff += ABS(localDiff);
  150.     localDiff = macross[14]-currentBlock[2][14];     diff += ABS(localDiff);
  151.  
  152.     if ( diff > bestSoFar ) {
  153.     return diff;
  154.     }
  155.  
  156.     macross = &(prev[fy+4][fx]);
  157.  
  158.     localDiff = macross[0]-currentBlock[4][0];     diff += ABS(localDiff);
  159.     localDiff = macross[2]-currentBlock[4][2];     diff += ABS(localDiff);
  160.     localDiff = macross[4]-currentBlock[4][4];     diff += ABS(localDiff);
  161.     localDiff = macross[6]-currentBlock[4][6];     diff += ABS(localDiff);
  162.     localDiff = macross[8]-currentBlock[4][8];     diff += ABS(localDiff);
  163.     localDiff = macross[10]-currentBlock[4][10];     diff += ABS(localDiff);
  164.     localDiff = macross[12]-currentBlock[4][12];     diff += ABS(localDiff);
  165.     localDiff = macross[14]-currentBlock[4][14];     diff += ABS(localDiff);
  166.  
  167.     if ( diff > bestSoFar ) {
  168.     return diff;
  169.     }
  170.  
  171.     macross = &(prev[fy+6][fx]);
  172.  
  173.     localDiff = macross[0]-currentBlock[6][0];     diff += ABS(localDiff);
  174.     localDiff = macross[2]-currentBlock[6][2];     diff += ABS(localDiff);
  175.     localDiff = macross[4]-currentBlock[6][4];     diff += ABS(localDiff);
  176.     localDiff = macross[6]-currentBlock[6][6];     diff += ABS(localDiff);
  177.     localDiff = macross[8]-currentBlock[6][8];     diff += ABS(localDiff);
  178.     localDiff = macross[10]-currentBlock[6][10];     diff += ABS(localDiff);
  179.     localDiff = macross[12]-currentBlock[6][12];     diff += ABS(localDiff);
  180.     localDiff = macross[14]-currentBlock[6][14];     diff += ABS(localDiff);
  181.  
  182.     if ( diff > bestSoFar ) {
  183.     return diff;
  184.     }
  185.  
  186.     macross = &(prev[fy+8][fx]);
  187.  
  188.     localDiff = macross[0]-currentBlock[8][0];     diff += ABS(localDiff);
  189.     localDiff = macross[2]-currentBlock[8][2];     diff += ABS(localDiff);
  190.     localDiff = macross[4]-currentBlock[8][4];     diff += ABS(localDiff);
  191.     localDiff = macross[6]-currentBlock[8][6];     diff += ABS(localDiff);
  192.     localDiff = macross[8]-currentBlock[8][8];     diff += ABS(localDiff);
  193.     localDiff = macross[10]-currentBlock[8][10];     diff += ABS(localDiff);
  194.     localDiff = macross[12]-currentBlock[8][12];     diff += ABS(localDiff);
  195.     localDiff = macross[14]-currentBlock[8][14];     diff += ABS(localDiff);
  196.  
  197.     if ( diff > bestSoFar ) {
  198.     return diff;
  199.     }
  200.  
  201.     macross = &(prev[fy+10][fx]);
  202.  
  203.     localDiff = macross[0]-currentBlock[10][0];     diff += ABS(localDiff);
  204.     localDiff = macross[2]-currentBlock[10][2];     diff += ABS(localDiff);
  205.     localDiff = macross[4]-currentBlock[10][4];     diff += ABS(localDiff);
  206.     localDiff = macross[6]-currentBlock[10][6];     diff += ABS(localDiff);
  207.     localDiff = macross[8]-currentBlock[10][8];     diff += ABS(localDiff);
  208.     localDiff = macross[10]-currentBlock[10][10];     diff += ABS(localDiff);
  209.     localDiff = macross[12]-currentBlock[10][12];     diff += ABS(localDiff);
  210.     localDiff = macross[14]-currentBlock[10][14];     diff += ABS(localDiff);
  211.  
  212.     if ( diff > bestSoFar ) {
  213.     return diff;
  214.     }
  215.  
  216.     macross = &(prev[fy+12][fx]);
  217.  
  218.     localDiff = macross[0]-currentBlock[12][0];     diff += ABS(localDiff);
  219.     localDiff = macross[2]-currentBlock[12][2];     diff += ABS(localDiff);
  220.     localDiff = macross[4]-currentBlock[12][4];     diff += ABS(localDiff);
  221.     localDiff = macross[6]-currentBlock[12][6];     diff += ABS(localDiff);
  222.     localDiff = macross[8]-currentBlock[12][8];     diff += ABS(localDiff);
  223.     localDiff = macross[10]-currentBlock[12][10];     diff += ABS(localDiff);
  224.     localDiff = macross[12]-currentBlock[12][12];     diff += ABS(localDiff);
  225.     localDiff = macross[14]-currentBlock[12][14];     diff += ABS(localDiff);
  226.  
  227.     if ( diff > bestSoFar ) {
  228.     return diff;
  229.     }
  230.  
  231.     macross = &(prev[fy+14][fx]);
  232.  
  233.     localDiff = macross[0]-currentBlock[14][0];     diff += ABS(localDiff);
  234.     localDiff = macross[2]-currentBlock[14][2];     diff += ABS(localDiff);
  235.     localDiff = macross[4]-currentBlock[14][4];     diff += ABS(localDiff);
  236.     localDiff = macross[6]-currentBlock[14][6];     diff += ABS(localDiff);
  237.     localDiff = macross[8]-currentBlock[14][8];     diff += ABS(localDiff);
  238.     localDiff = macross[10]-currentBlock[14][10];     diff += ABS(localDiff);
  239.     localDiff = macross[12]-currentBlock[14][12];     diff += ABS(localDiff);
  240.     localDiff = macross[14]-currentBlock[14][14];     diff += ABS(localDiff);
  241.  
  242.     return diff;
  243. }
  244.  
  245.  
  246. /*===========================================================================*
  247.  *
  248.  * LumMotionErrorB
  249.  *
  250.  *    compute the motion error for the B subsampling pattern
  251.  *
  252.  * RETURNS:    the error, or some number greater if it is worse
  253.  *
  254.  * SIDE EFFECTS:    none
  255.  *
  256.  *===========================================================================*/
  257. int32
  258. LumMotionErrorB(currentBlock, prevFrame, by, bx, my, mx, bestSoFar)
  259.     LumBlock currentBlock;
  260.     MpegFrame *prevFrame;
  261.     int by;
  262.     int bx;
  263.     int my;
  264.     int mx;
  265.     int32 bestSoFar;
  266. {
  267.     register int32    diff = 0;        /* max value of diff is 255*256 = 65280 */
  268.     register int32 localDiff;
  269.     register uint8 *macross;
  270.     register uint8 **prev;
  271.     register int    fy, fx;
  272.     boolean xHalf, yHalf;
  273.  
  274.     xHalf = (ABS(mx) % 2 == 1);
  275.     yHalf = (ABS(my) % 2 == 1);
  276.  
  277.     MOTION_TO_FRAME_COORD(by, bx, my/2, mx/2, fy, fx);
  278.  
  279.     if ( xHalf ) {
  280.     if ( mx < 0 ) {
  281.         fx--;
  282.     }
  283.  
  284.     if ( yHalf ) {
  285.         if ( my < 0 ) {
  286.         fy--;
  287.         }
  288.         
  289.         prev = prevFrame->halfBoth;
  290.     } else {
  291.         prev = prevFrame->halfX;
  292.     }
  293.     } else if ( yHalf ) {
  294.     if ( my < 0 ) {
  295.         fy--;
  296.     }
  297.  
  298.     prev = prevFrame->halfY;
  299.     } else {
  300.     prev = prevFrame->ref_y;
  301.     }
  302.  
  303.     macross = &(prev[fy+0][fx]);
  304.  
  305.     localDiff = macross[1]-currentBlock[0][1];     diff += ABS(localDiff);
  306.     localDiff = macross[3]-currentBlock[0][3];     diff += ABS(localDiff);
  307.     localDiff = macross[5]-currentBlock[0][5];     diff += ABS(localDiff);
  308.     localDiff = macross[7]-currentBlock[0][7];     diff += ABS(localDiff);
  309.     localDiff = macross[9]-currentBlock[0][9];     diff += ABS(localDiff);
  310.     localDiff = macross[11]-currentBlock[0][11];     diff += ABS(localDiff);
  311.     localDiff = macross[13]-currentBlock[0][13];     diff += ABS(localDiff);
  312.     localDiff = macross[15]-currentBlock[0][15];     diff += ABS(localDiff);
  313.  
  314.     if ( diff > bestSoFar ) {
  315.     return diff;
  316.     }
  317.  
  318.     macross = &(prev[fy+2][fx]);
  319.  
  320.     localDiff = macross[1]-currentBlock[2][1];     diff += ABS(localDiff);
  321.     localDiff = macross[3]-currentBlock[2][3];     diff += ABS(localDiff);
  322.     localDiff = macross[5]-currentBlock[2][5];     diff += ABS(localDiff);
  323.     localDiff = macross[7]-currentBlock[2][7];     diff += ABS(localDiff);
  324.     localDiff = macross[9]-currentBlock[2][9];     diff += ABS(localDiff);
  325.     localDiff = macross[11]-currentBlock[2][11];     diff += ABS(localDiff);
  326.     localDiff = macross[13]-currentBlock[2][13];     diff += ABS(localDiff);
  327.     localDiff = macross[15]-currentBlock[2][15];     diff += ABS(localDiff);
  328.  
  329.     if ( diff > bestSoFar ) {
  330.     return diff;
  331.     }
  332.  
  333.     macross = &(prev[fy+4][fx]);
  334.  
  335.     localDiff = macross[1]-currentBlock[4][1];     diff += ABS(localDiff);
  336.     localDiff = macross[3]-currentBlock[4][3];     diff += ABS(localDiff);
  337.     localDiff = macross[5]-currentBlock[4][5];     diff += ABS(localDiff);
  338.     localDiff = macross[7]-currentBlock[4][7];     diff += ABS(localDiff);
  339.     localDiff = macross[9]-currentBlock[4][9];     diff += ABS(localDiff);
  340.     localDiff = macross[11]-currentBlock[4][11];     diff += ABS(localDiff);
  341.     localDiff = macross[13]-currentBlock[4][13];     diff += ABS(localDiff);
  342.     localDiff = macross[15]-currentBlock[4][15];     diff += ABS(localDiff);
  343.  
  344.     if ( diff > bestSoFar ) {
  345.     return diff;
  346.     }
  347.  
  348.     macross = &(prev[fy+6][fx]);
  349.  
  350.     localDiff = macross[1]-currentBlock[6][1];     diff += ABS(localDiff);
  351.     localDiff = macross[3]-currentBlock[6][3];     diff += ABS(localDiff);
  352.     localDiff = macross[5]-currentBlock[6][5];     diff += ABS(localDiff);
  353.     localDiff = macross[7]-currentBlock[6][7];     diff += ABS(localDiff);
  354.     localDiff = macross[9]-currentBlock[6][9];     diff += ABS(localDiff);
  355.     localDiff = macross[11]-currentBlock[6][11];     diff += ABS(localDiff);
  356.     localDiff = macross[13]-currentBlock[6][13];     diff += ABS(localDiff);
  357.     localDiff = macross[15]-currentBlock[6][15];     diff += ABS(localDiff);
  358.  
  359.     if ( diff > bestSoFar ) {
  360.     return diff;
  361.     }
  362.  
  363.     macross = &(prev[fy+8][fx]);
  364.  
  365.     localDiff = macross[1]-currentBlock[8][1];     diff += ABS(localDiff);
  366.     localDiff = macross[3]-currentBlock[8][3];     diff += ABS(localDiff);
  367.     localDiff = macross[5]-currentBlock[8][5];     diff += ABS(localDiff);
  368.     localDiff = macross[7]-currentBlock[8][7];     diff += ABS(localDiff);
  369.     localDiff = macross[9]-currentBlock[8][9];     diff += ABS(localDiff);
  370.     localDiff = macross[11]-currentBlock[8][11];     diff += ABS(localDiff);
  371.     localDiff = macross[13]-currentBlock[8][13];     diff += ABS(localDiff);
  372.     localDiff = macross[15]-currentBlock[8][15];     diff += ABS(localDiff);
  373.  
  374.     if ( diff > bestSoFar ) {
  375.     return diff;
  376.     }
  377.  
  378.     macross = &(prev[fy+10][fx]);
  379.  
  380.     localDiff = macross[1]-currentBlock[10][1];     diff += ABS(localDiff);
  381.     localDiff = macross[3]-currentBlock[10][3];     diff += ABS(localDiff);
  382.     localDiff = macross[5]-currentBlock[10][5];     diff += ABS(localDiff);
  383.     localDiff = macross[7]-currentBlock[10][7];     diff += ABS(localDiff);
  384.     localDiff = macross[9]-currentBlock[10][9];     diff += ABS(localDiff);
  385.     localDiff = macross[11]-currentBlock[10][11];     diff += ABS(localDiff);
  386.     localDiff = macross[13]-currentBlock[10][13];     diff += ABS(localDiff);
  387.     localDiff = macross[15]-currentBlock[10][15];     diff += ABS(localDiff);
  388.  
  389.     if ( diff > bestSoFar ) {
  390.     return diff;
  391.     }
  392.  
  393.     macross = &(prev[fy+12][fx]);
  394.  
  395.     localDiff = macross[1]-currentBlock[12][1];     diff += ABS(localDiff);
  396.     localDiff = macross[3]-currentBlock[12][3];     diff += ABS(localDiff);
  397.     localDiff = macross[5]-currentBlock[12][5];     diff += ABS(localDiff);
  398.     localDiff = macross[7]-currentBlock[12][7];     diff += ABS(localDiff);
  399.     localDiff = macross[9]-currentBlock[12][9];     diff += ABS(localDiff);
  400.     localDiff = macross[11]-currentBlock[12][11];     diff += ABS(localDiff);
  401.     localDiff = macross[13]-currentBlock[12][13];     diff += ABS(localDiff);
  402.     localDiff = macross[15]-currentBlock[12][15];     diff += ABS(localDiff);
  403.  
  404.     if ( diff > bestSoFar ) {
  405.     return diff;
  406.     }
  407.  
  408.     macross = &(prev[fy+14][fx]);
  409.  
  410.     localDiff = macross[1]-currentBlock[14][1];     diff += ABS(localDiff);
  411.     localDiff = macross[3]-currentBlock[14][3];     diff += ABS(localDiff);
  412.     localDiff = macross[5]-currentBlock[14][5];     diff += ABS(localDiff);
  413.     localDiff = macross[7]-currentBlock[14][7];     diff += ABS(localDiff);
  414.     localDiff = macross[9]-currentBlock[14][9];     diff += ABS(localDiff);
  415.     localDiff = macross[11]-currentBlock[14][11];     diff += ABS(localDiff);
  416.     localDiff = macross[13]-currentBlock[14][13];     diff += ABS(localDiff);
  417.     localDiff = macross[15]-currentBlock[14][15];     diff += ABS(localDiff);
  418.  
  419.     return diff;
  420. }
  421.  
  422.  
  423. /*===========================================================================*
  424.  *
  425.  * LumMotionErrorC
  426.  *
  427.  *    compute the motion error for the C subsampling pattern
  428.  *
  429.  * RETURNS:    the error, or some number greater if it is worse
  430.  *
  431.  * SIDE EFFECTS:    none
  432.  *
  433.  *===========================================================================*/
  434. int32
  435. LumMotionErrorC(currentBlock, prevFrame, by, bx, my, mx, bestSoFar)
  436.     LumBlock currentBlock;
  437.     MpegFrame *prevFrame;
  438.     int by;
  439.     int bx;
  440.     int my;
  441.     int mx;
  442.     int32 bestSoFar;
  443. {
  444.     register int32    diff = 0;        /* max value of diff is 255*256 = 65280 */
  445.     register int32 localDiff;
  446.     register uint8 *macross;
  447.     register uint8 **prev;
  448.     register int    fy, fx;
  449.     boolean xHalf, yHalf;
  450.  
  451.     xHalf = (ABS(mx) % 2 == 1);
  452.     yHalf = (ABS(my) % 2 == 1);
  453.  
  454.     MOTION_TO_FRAME_COORD(by, bx, my/2, mx/2, fy, fx);
  455.  
  456.     if ( xHalf ) {
  457.     if ( mx < 0 ) {
  458.         fx--;
  459.     }
  460.  
  461.     if ( yHalf ) {
  462.         if ( my < 0 ) {
  463.         fy--;
  464.         }
  465.         
  466.         prev = prevFrame->halfBoth;
  467.     } else {
  468.         prev = prevFrame->halfX;
  469.     }
  470.     } else if ( yHalf ) {
  471.     if ( my < 0 ) {
  472.         fy--;
  473.     }
  474.  
  475.     prev = prevFrame->halfY;
  476.     } else {
  477.     prev = prevFrame->ref_y;
  478.     }
  479.  
  480.     macross = &(prev[fy+1][fx]);
  481.  
  482.     localDiff = macross[0]-currentBlock[1][0];     diff += ABS(localDiff);
  483.     localDiff = macross[2]-currentBlock[1][2];     diff += ABS(localDiff);
  484.     localDiff = macross[4]-currentBlock[1][4];     diff += ABS(localDiff);
  485.     localDiff = macross[6]-currentBlock[1][6];     diff += ABS(localDiff);
  486.     localDiff = macross[8]-currentBlock[1][8];     diff += ABS(localDiff);
  487.     localDiff = macross[10]-currentBlock[1][10];     diff += ABS(localDiff);
  488.     localDiff = macross[12]-currentBlock[1][12];     diff += ABS(localDiff);
  489.     localDiff = macross[14]-currentBlock[1][14];     diff += ABS(localDiff);
  490.  
  491.     if ( diff > bestSoFar ) {
  492.     return diff;
  493.     }
  494.  
  495.     macross = &(prev[fy+3][fx]);
  496.  
  497.     localDiff = macross[0]-currentBlock[3][0];     diff += ABS(localDiff);
  498.     localDiff = macross[2]-currentBlock[3][2];     diff += ABS(localDiff);
  499.     localDiff = macross[4]-currentBlock[3][4];     diff += ABS(localDiff);
  500.     localDiff = macross[6]-currentBlock[3][6];     diff += ABS(localDiff);
  501.     localDiff = macross[8]-currentBlock[3][8];     diff += ABS(localDiff);
  502.     localDiff = macross[10]-currentBlock[3][10];     diff += ABS(localDiff);
  503.     localDiff = macross[12]-currentBlock[3][12];     diff += ABS(localDiff);
  504.     localDiff = macross[14]-currentBlock[3][14];     diff += ABS(localDiff);
  505.  
  506.     if ( diff > bestSoFar ) {
  507.     return diff;
  508.     }
  509.  
  510.     macross = &(prev[fy+5][fx]);
  511.  
  512.     localDiff = macross[0]-currentBlock[5][0];     diff += ABS(localDiff);
  513.     localDiff = macross[2]-currentBlock[5][2];     diff += ABS(localDiff);
  514.     localDiff = macross[4]-currentBlock[5][4];     diff += ABS(localDiff);
  515.     localDiff = macross[6]-currentBlock[5][6];     diff += ABS(localDiff);
  516.     localDiff = macross[8]-currentBlock[5][8];     diff += ABS(localDiff);
  517.     localDiff = macross[10]-currentBlock[5][10];     diff += ABS(localDiff);
  518.     localDiff = macross[12]-currentBlock[5][12];     diff += ABS(localDiff);
  519.     localDiff = macross[14]-currentBlock[5][14];     diff += ABS(localDiff);
  520.  
  521.     if ( diff > bestSoFar ) {
  522.     return diff;
  523.     }
  524.  
  525.     macross = &(prev[fy+7][fx]);
  526.  
  527.     localDiff = macross[0]-currentBlock[7][0];     diff += ABS(localDiff);
  528.     localDiff = macross[2]-currentBlock[7][2];     diff += ABS(localDiff);
  529.     localDiff = macross[4]-currentBlock[7][4];     diff += ABS(localDiff);
  530.     localDiff = macross[6]-currentBlock[7][6];     diff += ABS(localDiff);
  531.     localDiff = macross[8]-currentBlock[7][8];     diff += ABS(localDiff);
  532.     localDiff = macross[10]-currentBlock[7][10];     diff += ABS(localDiff);
  533.     localDiff = macross[12]-currentBlock[7][12];     diff += ABS(localDiff);
  534.     localDiff = macross[14]-currentBlock[7][14];     diff += ABS(localDiff);
  535.  
  536.     if ( diff > bestSoFar ) {
  537.     return diff;
  538.     }
  539.  
  540.     macross = &(prev[fy+9][fx]);
  541.  
  542.     localDiff = macross[0]-currentBlock[9][0];     diff += ABS(localDiff);
  543.     localDiff = macross[2]-currentBlock[9][2];     diff += ABS(localDiff);
  544.     localDiff = macross[4]-currentBlock[9][4];     diff += ABS(localDiff);
  545.     localDiff = macross[6]-currentBlock[9][6];     diff += ABS(localDiff);
  546.     localDiff = macross[8]-currentBlock[9][8];     diff += ABS(localDiff);
  547.     localDiff = macross[10]-currentBlock[9][10];     diff += ABS(localDiff);
  548.     localDiff = macross[12]-currentBlock[9][12];     diff += ABS(localDiff);
  549.     localDiff = macross[14]-currentBlock[9][14];     diff += ABS(localDiff);
  550.  
  551.     if ( diff > bestSoFar ) {
  552.     return diff;
  553.     }
  554.  
  555.     macross = &(prev[fy+11][fx]);
  556.  
  557.     localDiff = macross[0]-currentBlock[11][0];     diff += ABS(localDiff);
  558.     localDiff = macross[2]-currentBlock[11][2];     diff += ABS(localDiff);
  559.     localDiff = macross[4]-currentBlock[11][4];     diff += ABS(localDiff);
  560.     localDiff = macross[6]-currentBlock[11][6];     diff += ABS(localDiff);
  561.     localDiff = macross[8]-currentBlock[11][8];     diff += ABS(localDiff);
  562.     localDiff = macross[10]-currentBlock[11][10];     diff += ABS(localDiff);
  563.     localDiff = macross[12]-currentBlock[11][12];     diff += ABS(localDiff);
  564.     localDiff = macross[14]-currentBlock[11][14];     diff += ABS(localDiff);
  565.  
  566.     if ( diff > bestSoFar ) {
  567.     return diff;
  568.     }
  569.  
  570.     macross = &(prev[fy+13][fx]);
  571.  
  572.     localDiff = macross[0]-currentBlock[13][0];     diff += ABS(localDiff);
  573.     localDiff = macross[2]-currentBlock[13][2];     diff += ABS(localDiff);
  574.     localDiff = macross[4]-currentBlock[13][4];     diff += ABS(localDiff);
  575.     localDiff = macross[6]-currentBlock[13][6];     diff += ABS(localDiff);
  576.     localDiff = macross[8]-currentBlock[13][8];     diff += ABS(localDiff);
  577.     localDiff = macross[10]-currentBlock[13][10];     diff += ABS(localDiff);
  578.     localDiff = macross[12]-currentBlock[13][12];     diff += ABS(localDiff);
  579.     localDiff = macross[14]-currentBlock[13][14];     diff += ABS(localDiff);
  580.  
  581.     if ( diff > bestSoFar ) {
  582.     return diff;
  583.     }
  584.  
  585.     macross = &(prev[fy+15][fx]);
  586.  
  587.     localDiff = macross[0]-currentBlock[15][0];     diff += ABS(localDiff);
  588.     localDiff = macross[2]-currentBlock[15][2];     diff += ABS(localDiff);
  589.     localDiff = macross[4]-currentBlock[15][4];     diff += ABS(localDiff);
  590.     localDiff = macross[6]-currentBlock[15][6];     diff += ABS(localDiff);
  591.     localDiff = macross[8]-currentBlock[15][8];     diff += ABS(localDiff);
  592.     localDiff = macross[10]-currentBlock[15][10];     diff += ABS(localDiff);
  593.     localDiff = macross[12]-currentBlock[15][12];     diff += ABS(localDiff);
  594.     localDiff = macross[14]-currentBlock[15][14];     diff += ABS(localDiff);
  595.  
  596.     return diff;
  597. }
  598.  
  599.  
  600. /*===========================================================================*
  601.  *
  602.  * LumMotionErrorD
  603.  *
  604.  *    compute the motion error for the D subsampling pattern
  605.  *
  606.  * RETURNS:    the error, or some number greater if it is worse
  607.  *
  608.  * SIDE EFFECTS:    none
  609.  *
  610.  *===========================================================================*/
  611. int32
  612. LumMotionErrorD(currentBlock, prevFrame, by, bx, my, mx, bestSoFar)
  613.     LumBlock currentBlock;
  614.     MpegFrame *prevFrame;
  615.     int by;
  616.     int bx;
  617.     int my;
  618.     int mx;
  619.     int32 bestSoFar;
  620. {
  621.     register int32    diff = 0;        /* max value of diff is 255*256 = 65280 */
  622.     register int32 localDiff;
  623.     register uint8 *macross;
  624.     register uint8 **prev;
  625.     register int    fy, fx;
  626.     boolean xHalf, yHalf;
  627.  
  628.     xHalf = (ABS(mx) % 2 == 1);
  629.     yHalf = (ABS(my) % 2 == 1);
  630.  
  631.     MOTION_TO_FRAME_COORD(by, bx, my/2, mx/2, fy, fx);
  632.  
  633.     if ( xHalf ) {
  634.     if ( mx < 0 ) {
  635.         fx--;
  636.     }
  637.  
  638.     if ( yHalf ) {
  639.         if ( my < 0 ) {       
  640.         fy--;
  641.         }
  642.         prev = prevFrame->halfBoth;
  643.     } else {
  644.         prev = prevFrame->halfX;
  645.     }
  646.     } else if ( yHalf ) {
  647.     if ( my < 0 ) {
  648.         fy--;
  649.     }
  650.     prev = prevFrame->halfY;
  651.     } else {
  652.     prev = prevFrame->ref_y;
  653.     }
  654.  
  655.     macross = &(prev[fy+1][fx]);
  656.  
  657.     localDiff = macross[1]-currentBlock[1][1];     diff += ABS(localDiff);
  658.     localDiff = macross[3]-currentBlock[1][3];     diff += ABS(localDiff);
  659.     localDiff = macross[5]-currentBlock[1][5];     diff += ABS(localDiff);
  660.     localDiff = macross[7]-currentBlock[1][7];     diff += ABS(localDiff);
  661.     localDiff = macross[9]-currentBlock[1][9];     diff += ABS(localDiff);
  662.     localDiff = macross[11]-currentBlock[1][11];     diff += ABS(localDiff);
  663.     localDiff = macross[13]-currentBlock[1][13];     diff += ABS(localDiff);
  664.     localDiff = macross[15]-currentBlock[1][15];     diff += ABS(localDiff);
  665.  
  666.     if ( diff > bestSoFar ) {
  667.     return diff;
  668.     }
  669.  
  670.     macross = &(prev[fy+3][fx]);
  671.  
  672.     localDiff = macross[1]-currentBlock[3][1];     diff += ABS(localDiff);
  673.     localDiff = macross[3]-currentBlock[3][3];     diff += ABS(localDiff);
  674.     localDiff = macross[5]-currentBlock[3][5];     diff += ABS(localDiff);
  675.     localDiff = macross[7]-currentBlock[3][7];     diff += ABS(localDiff);
  676.     localDiff = macross[9]-currentBlock[3][9];     diff += ABS(localDiff);
  677.     localDiff = macross[11]-currentBlock[3][11];     diff += ABS(localDiff);
  678.     localDiff = macross[13]-currentBlock[3][13];     diff += ABS(localDiff);
  679.     localDiff = macross[15]-currentBlock[3][15];     diff += ABS(localDiff);
  680.  
  681.     if ( diff > bestSoFar ) {
  682.     return diff;
  683.     }
  684.  
  685.     macross = &(prev[fy+5][fx]);
  686.  
  687.     localDiff = macross[1]-currentBlock[5][1];     diff += ABS(localDiff);
  688.     localDiff = macross[3]-currentBlock[5][3];     diff += ABS(localDiff);
  689.     localDiff = macross[5]-currentBlock[5][5];     diff += ABS(localDiff);
  690.     localDiff = macross[7]-currentBlock[5][7];     diff += ABS(localDiff);
  691.     localDiff = macross[9]-currentBlock[5][9];     diff += ABS(localDiff);
  692.     localDiff = macross[11]-currentBlock[5][11];     diff += ABS(localDiff);
  693.     localDiff = macross[13]-currentBlock[5][13];     diff += ABS(localDiff);
  694.     localDiff = macross[15]-currentBlock[5][15];     diff += ABS(localDiff);
  695.  
  696.     if ( diff > bestSoFar ) {
  697.     return diff;
  698.     }
  699.  
  700.     macross = &(prev[fy+7][fx]);
  701.  
  702.     localDiff = macross[1]-currentBlock[7][1];     diff += ABS(localDiff);
  703.     localDiff = macross[3]-currentBlock[7][3];     diff += ABS(localDiff);
  704.     localDiff = macross[5]-currentBlock[7][5];     diff += ABS(localDiff);
  705.     localDiff = macross[7]-currentBlock[7][7];     diff += ABS(localDiff);
  706.     localDiff = macross[9]-currentBlock[7][9];     diff += ABS(localDiff);
  707.     localDiff = macross[11]-currentBlock[7][11];     diff += ABS(localDiff);
  708.     localDiff = macross[13]-currentBlock[7][13];     diff += ABS(localDiff);
  709.     localDiff = macross[15]-currentBlock[7][15];     diff += ABS(localDiff);
  710.  
  711.     if ( diff > bestSoFar ) {
  712.     return diff;
  713.     }
  714.  
  715.     macross = &(prev[fy+9][fx]);
  716.  
  717.     localDiff = macross[1]-currentBlock[9][1];     diff += ABS(localDiff);
  718.     localDiff = macross[3]-currentBlock[9][3];     diff += ABS(localDiff);
  719.     localDiff = macross[5]-currentBlock[9][5];     diff += ABS(localDiff);
  720.     localDiff = macross[7]-currentBlock[9][7];     diff += ABS(localDiff);
  721.     localDiff = macross[9]-currentBlock[9][9];     diff += ABS(localDiff);
  722.     localDiff = macross[11]-currentBlock[9][11];     diff += ABS(localDiff);
  723.     localDiff = macross[13]-currentBlock[9][13];     diff += ABS(localDiff);
  724.     localDiff = macross[15]-currentBlock[9][15];     diff += ABS(localDiff);
  725.  
  726.     if ( diff > bestSoFar ) {
  727.     return diff;
  728.     }
  729.  
  730.     macross = &(prev[fy+11][fx]);
  731.  
  732.     localDiff = macross[1]-currentBlock[11][1];     diff += ABS(localDiff);
  733.     localDiff = macross[3]-currentBlock[11][3];     diff += ABS(localDiff);
  734.     localDiff = macross[5]-currentBlock[11][5];     diff += ABS(localDiff);
  735.     localDiff = macross[7]-currentBlock[11][7];     diff += ABS(localDiff);
  736.     localDiff = macross[9]-currentBlock[11][9];     diff += ABS(localDiff);
  737.     localDiff = macross[11]-currentBlock[11][11];     diff += ABS(localDiff);
  738.     localDiff = macross[13]-currentBlock[11][13];     diff += ABS(localDiff);
  739.     localDiff = macross[15]-currentBlock[11][15];     diff += ABS(localDiff);
  740.  
  741.     if ( diff > bestSoFar ) {
  742.     return diff;
  743.     }
  744.  
  745.     macross = &(prev[fy+13][fx]);
  746.  
  747.     localDiff = macross[1]-currentBlock[13][1];     diff += ABS(localDiff);
  748.     localDiff = macross[3]-currentBlock[13][3];     diff += ABS(localDiff);
  749.     localDiff = macross[5]-currentBlock[13][5];     diff += ABS(localDiff);
  750.     localDiff = macross[7]-currentBlock[13][7];     diff += ABS(localDiff);
  751.     localDiff = macross[9]-currentBlock[13][9];     diff += ABS(localDiff);
  752.     localDiff = macross[11]-currentBlock[13][11];     diff += ABS(localDiff);
  753.     localDiff = macross[13]-currentBlock[13][13];     diff += ABS(localDiff);
  754.     localDiff = macross[15]-currentBlock[13][15];     diff += ABS(localDiff);
  755.  
  756.     if ( diff > bestSoFar ) {
  757.     return diff;
  758.     }
  759.  
  760.     macross = &(prev[fy+15][fx]);
  761.  
  762.     localDiff = macross[1]-currentBlock[15][1];     diff += ABS(localDiff);
  763.     localDiff = macross[3]-currentBlock[15][3];     diff += ABS(localDiff);
  764.     localDiff = macross[5]-currentBlock[15][5];     diff += ABS(localDiff);
  765.     localDiff = macross[7]-currentBlock[15][7];     diff += ABS(localDiff);
  766.     localDiff = macross[9]-currentBlock[15][9];     diff += ABS(localDiff);
  767.     localDiff = macross[11]-currentBlock[15][11];     diff += ABS(localDiff);
  768.     localDiff = macross[13]-currentBlock[15][13];     diff += ABS(localDiff);
  769.     localDiff = macross[15]-currentBlock[15][15];     diff += ABS(localDiff);
  770.  
  771.     return diff;
  772. }
  773.