home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Multimed / Multimed.zip / mpegplay.zip / MOTIONVE.C < prev    next >
C/C++ Source or Header  |  1993-02-02  |  6KB  |  196 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. #include "video.h"
  22. #include "proto.h"
  23. #include "util.h"
  24.  
  25.  
  26. /*
  27.  *--------------------------------------------------------------
  28.  *
  29.  * ComputeVector --
  30.  *
  31.  *    Computes motion vector given parameters previously parsed
  32.  *      and reconstructed.
  33.  *
  34.  * Results:
  35.  *      Reconstructed motion vector info is put into recon_* parameters
  36.  *      passed to this function. Also updated previous motion vector
  37.  *      information.
  38.  *
  39.  * Side effects:
  40.  *      None.
  41.  *
  42.  *--------------------------------------------------------------
  43.  */
  44.  
  45. #define ComputeVector(recon_right_ptr, recon_down_ptr, recon_right_prev, recon_down_prev, f, full_pel_vector, motion_h_code, motion_v_code, motion_h_r, motion_v_r)                \
  46.                                     \
  47. {                                    \
  48.   int comp_h_r, comp_v_r;                        \
  49.   int right_little, right_big, down_little, down_big;            \
  50.   int max, min, new_vector;                        \
  51.                                     \
  52.   /* The following procedure for the reconstruction of motion vectors     \
  53.      is a direct and simple implementation of the instructions given    \
  54.      in the mpeg December 1991 standard draft.                 \
  55.   */                                    \
  56.                                     \
  57.   if (f == 1 || motion_h_code == 0)                    \
  58.     comp_h_r = 0;                            \
  59.   else                                     \
  60.     comp_h_r = f - 1 - motion_h_r;                    \
  61.                                     \
  62.   if (f == 1 || motion_v_code == 0)                    \
  63.     comp_v_r = 0;                            \
  64.   else                                     \
  65.     comp_v_r = f - 1 - motion_v_r;                    \
  66.                                     \
  67.   right_little = motion_h_code * f;                    \
  68.   if (right_little == 0)                        \
  69.     right_big = 0;                            \
  70.   else {                                \
  71.     if (right_little > 0) {                        \
  72.       right_little = right_little - comp_h_r;                \
  73.       right_big = right_little - 32 * f;                \
  74.     }                                    \
  75.     else {                                \
  76.       right_little = right_little + comp_h_r;                \
  77.       right_big = right_little + 32 * f;                \
  78.     }                                    \
  79.   }                                    \
  80.                                     \
  81.   down_little = motion_v_code * f;                    \
  82.   if (down_little == 0)                            \
  83.     down_big = 0;                            \
  84.   else {                                \
  85.     if (down_little > 0) {                        \
  86.       down_little = down_little - comp_v_r;                \
  87.       down_big = down_little - 32 * f;                    \
  88.     }                                    \
  89.     else {                                \
  90.       down_little = down_little + comp_v_r;                \
  91.       down_big = down_little + 32 * f;                    \
  92.     }                                    \
  93.   }                                    \
  94.                                       \
  95.   max = 16 * f - 1;                            \
  96.   min = -16 * f;                            \
  97.                                     \
  98.   new_vector = recon_right_prev + right_little;                \
  99.                                     \
  100.   if (new_vector <= max && new_vector >= min)                \
  101.     *recon_right_ptr = recon_right_prev + right_little;            \
  102.                       /* just new_vector */                \
  103.   else                                    \
  104.     *recon_right_ptr = recon_right_prev + right_big;            \
  105.   recon_right_prev = *recon_right_ptr;                    \
  106.   if (full_pel_vector)                            \
  107.     *recon_right_ptr = *recon_right_ptr << 1;                \
  108.                                     \
  109.   new_vector = recon_down_prev + down_little;                \
  110.   if (new_vector <= max && new_vector >= min)                \
  111.     *recon_down_ptr = recon_down_prev + down_little;            \
  112.                       /* just new_vector */                \
  113.   else                                    \
  114.     *recon_down_ptr = recon_down_prev + down_big;            \
  115.   recon_down_prev = *recon_down_ptr;                    \
  116.   if (full_pel_vector)                            \
  117.     *recon_down_ptr = *recon_down_ptr << 1;                \
  118. }
  119.  
  120. /*
  121.  *--------------------------------------------------------------
  122.  *
  123.  * ComputeForwVector --
  124.  *
  125.  *    Computes forward motion vector by calling ComputeVector
  126.  *      with appropriate parameters.
  127.  *
  128.  * Results:
  129.  *    Reconstructed motion vector placed in recon_right_for_ptr and
  130.  *      recon_down_for_ptr.
  131.  *
  132.  * Side effects:
  133.  *      None.
  134.  *
  135.  *--------------------------------------------------------------
  136.  */
  137.  
  138. void 
  139. ComputeForwVector(recon_right_for_ptr, recon_down_for_ptr)
  140.      int *recon_right_for_ptr;
  141.      int *recon_down_for_ptr;
  142. {
  143.  
  144.   Pict *picture;
  145.   Macroblock *mblock;
  146.  
  147.   picture = &(curVidStream->picture);
  148.   mblock = &(curVidStream->mblock);
  149.  
  150.   ComputeVector(recon_right_for_ptr, recon_down_for_ptr,
  151.         mblock->recon_right_for_prev, 
  152.         mblock->recon_down_for_prev,
  153.         picture->forw_f, picture->full_pel_forw_vector,
  154.         mblock->motion_h_forw_code, mblock->motion_v_forw_code,
  155.         mblock->motion_h_forw_r, mblock->motion_v_forw_r); 
  156. }
  157.  
  158.  
  159. /*
  160.  *--------------------------------------------------------------
  161.  *
  162.  * ComputeBackVector --
  163.  *
  164.  *    Computes backward motion vector by calling ComputeVector
  165.  *      with appropriate parameters.
  166.  *
  167.  * Results:
  168.  *    Reconstructed motion vector placed in recon_right_back_ptr and
  169.  *      recon_down_back_ptr.
  170.  *
  171.  * Side effects:
  172.  *      None.
  173.  *
  174.  *--------------------------------------------------------------
  175.  */
  176.  
  177. void 
  178. ComputeBackVector(recon_right_back_ptr, recon_down_back_ptr)
  179.      int *recon_right_back_ptr;
  180.      int *recon_down_back_ptr;
  181. {
  182.   Pict *picture;
  183.   Macroblock *mblock;
  184.  
  185.   picture = &(curVidStream->picture);
  186.   mblock = &(curVidStream->mblock);
  187.  
  188.   ComputeVector(recon_right_back_ptr, recon_down_back_ptr,
  189.         mblock->recon_right_back_prev, 
  190.         mblock->recon_down_back_prev,
  191.         picture->back_f, picture->full_pel_back_vector,
  192.         mblock->motion_h_back_code, mblock->motion_v_back_code,
  193.         mblock->motion_h_back_r, mblock->motion_v_back_r); 
  194.  
  195. }
  196.