download_filter.cpp
上传用户:sun1608
上传日期:2007-02-02
资源大小:6116k
文件大小:11k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. /* $Id: download_filter.cpp,v 1.3 2001/09/05 19:50:26 wmay Exp $ */
  2. /****************************************************************************/
  3. /*   MPEG4 Visual Texture Coding (VTC) Mode Software                        */
  4. /*                                                                          */
  5. /*   This software was jointly developed by the following participants:     */
  6. /*                                                                          */
  7. /*   Single-quant,  multi-quant and flow control                            */
  8. /*   are provided by  Sarnoff Corporation                                   */
  9. /*     Iraj Sodagar   (iraj@sarnoff.com)                                    */
  10. /*     Hung-Ju Lee    (hjlee@sarnoff.com)                                   */
  11. /*     Paul Hatrack   (hatrack@sarnoff.com)                                 */
  12. /*     Shipeng Li     (shipeng@sarnoff.com)                                 */
  13. /*     Bing-Bing Chai (bchai@sarnoff.com)                                   */
  14. /*     B.S. Srinivas  (bsrinivas@sarnoff.com)                               */
  15. /*                                                                          */
  16. /*   Bi-level is provided by Texas Instruments                              */
  17. /*     Jie Liang      (liang@ti.com)                                        */
  18. /*                                                                          */
  19. /*   Shape Coding is provided by  OKI Electric Industry Co., Ltd.           */
  20. /*     Zhixiong Wu    (sgo@hlabs.oki.co.jp)                                 */
  21. /*     Yoshihiro Ueda (yueda@hlabs.oki.co.jp)                               */
  22. /*     Toshifumi Kanamaru (kanamaru@hlabs.oki.co.jp)                        */
  23. /*                                                                          */
  24. /*   OKI, Sharp, Sarnoff, TI and Microsoft contributed to bitstream         */
  25. /*   exchange and bug fixing.                                               */
  26. /*                                                                          */
  27. /*                                                                          */
  28. /* In the course of development of the MPEG-4 standard, this software       */
  29. /* module is an implementation of a part of one or more MPEG-4 tools as     */
  30. /* specified by the MPEG-4 standard.                                        */
  31. /*                                                                          */
  32. /* The copyright of this software belongs to ISO/IEC. ISO/IEC gives use     */
  33. /* of the MPEG-4 standard free license to use this  software module or      */
  34. /* modifications thereof for hardware or software products claiming         */
  35. /* conformance to the MPEG-4 standard.                                      */
  36. /*                                                                          */
  37. /* Those intending to use this software module in hardware or software      */
  38. /* products are advised that use may infringe existing  patents. The        */
  39. /* original developers of this software module and their companies, the     */
  40. /* subsequent editors and their companies, and ISO/IEC have no liability    */
  41. /* and ISO/IEC have no liability for use of this software module or         */
  42. /* modification thereof in an implementation.                               */
  43. /*                                                                          */
  44. /* Permission is granted to MPEG members to use, copy, modify,              */
  45. /* and distribute the software modules ( or portions thereof )              */
  46. /* for standardization activity within ISO/IEC JTC1/SC29/WG11.              */
  47. /*                                                                          */
  48. /* Copyright 1995, 1996, 1997, 1998 ISO/IEC                                 */
  49. /****************************************************************************/
  50. /************************************************************/
  51. /*     Sarnoff Very Low Bit Rate Still Image Coder          */
  52. /*     Copyright 1995, 1996, 1997, 1998 Sarnoff Corporation */
  53. /************************************************************/
  54. /************************************************************/
  55. /*  Filename: download_filter.c                             */
  56. /*  Author: Bing-Bing Chai                                  */
  57. /*  Date: Jan. 30, 1998                                     */
  58. /*                                                          */
  59. /*  Descriptions:                                           */
  60. /*    This file contains the routines to up/download        */
  61. /*    wavelet filters when desired. Proper conversions from */
  62. /*    Floating poInt to Int are taken care of whenever      */
  63. /*    needed.                                               */
  64. /*                                                          */
  65. /*    It is assumed that Short and Float                    */
  66. /*    are 2 bytes, and 4 bytes respectively.                */
  67. /*                                                          */
  68. /************************************************************/
  69. #define DOWN_DEBUG 0
  70. #include <math.h>
  71. #include <stdio.h>
  72. #include <stdlib.h>
  73. #include "dataStruct.hpp"
  74. #include "startcode.hpp"
  75. #include "dwt.h"
  76. #include "errorHandler.hpp"
  77. #include "msg.hpp"
  78. #include "bitpack.hpp"
  79. #include "download_filter.h"
  80. Void CVTCCommon::check_marker(Int marker_bit)
  81. {
  82.   if(marker_bit != MARKER_BIT)
  83.     errorHandler("Error in download wavelet filtersn");
  84. }
  85. Void CVTCCommon::check_symmetry(FILTER *filter)
  86. {
  87.   Int i,half;
  88.   /* -------- check lowpass filter --------- */
  89.   half=filter->LPLength>>1;
  90.   if(half<<1==filter->LPLength) 
  91.     filter->DWT_Class=DWT_EVEN_SYMMETRIC;      /* even filter */
  92.   else
  93.     filter->DWT_Class=DWT_ODD_SYMMETRIC;      /* odd filter */
  94.   for(i=0;i<half;i++)
  95.     if(filter->DWT_Type == 0){
  96.       if(((Short*)filter->LPCoeff)[i] !=
  97.  ((Short*)filter->LPCoeff)[filter->LPLength-i-1]) 
  98. errorHandler("Lowpass filter is not symmetric.n");
  99.     }
  100.     else
  101.       if(((double*)filter->LPCoeff)[i] !=
  102.  ((double*)filter->LPCoeff)[filter->LPLength-i-1]) 
  103. errorHandler("Lowpass filter is not symmetric.n");
  104.   /* -------- check highpass filter ---------- */
  105.   half=filter->HPLength>>1;
  106.   /* check for error */
  107.   if(half<<1==filter->HPLength && filter->DWT_Class==DWT_ODD_SYMMETRIC)
  108.     errorHandler("Lowpass filter has odd taps, while highpass filter has even"
  109.                " taps->n");
  110.   if(half<<1!=filter->HPLength && filter->DWT_Class==DWT_EVEN_SYMMETRIC) 
  111.     errorHandler("Lowpass filter has even taps, while highpass filter has odd"
  112.                " taps.n");
  113.   if(filter->DWT_Class==DWT_ODD_SYMMETRIC){  /* ODD_SYMMETRIC, symmetric */
  114.      for(i=0;i<half;i++)
  115.       if(filter->DWT_Type == 0){
  116.         if(((Short*)filter->HPCoeff)[i] !=
  117.    ((Short*)filter->HPCoeff)[filter->HPLength-i-1]) 
  118.   errorHandler("Highpass filter is not symmetric.n");
  119.       }
  120.       else
  121. if(((double*)filter->HPCoeff)[i] !=
  122.    ((double*)filter->HPCoeff)[filter->HPLength-i-1]) 
  123.   errorHandler("Highpass filter is not symmetric.n");
  124.   }
  125.   else{  /* EVEN_SYMMETRIC, antisymmetric */
  126.      for(i=0;i<half;i++)
  127.       if(filter->DWT_Type == 0){
  128.         if(((Short*)filter->HPCoeff)[i] !=-
  129.    ((Short*)filter->HPCoeff)[filter->HPLength-i-1]) 
  130.   errorHandler("Highpass filter is not antisymmetric.n");
  131.       }
  132.       else
  133. if(((double*)filter->HPCoeff)[i] !=-
  134.    ((double*)filter->HPCoeff)[filter->HPLength-i-1]) 
  135.   errorHandler("Highpass filter is not antisymmetric.n");
  136.   }
  137. Void CVTCCommon::upload_wavelet_filters(FILTER *filter)
  138. {
  139.   Int i;
  140.   Float f;
  141.   Short s;
  142.   UShort *usptr;
  143.   UInt *uIntptr;
  144.   /* poInters to filter taps */
  145.   usptr=(UShort *)&s;
  146.   uIntptr=(UInt *)&f;
  147.   /* filter lengths */
  148.   emit_bits((UShort)filter->LPLength,4);
  149.   emit_bits((UShort)filter->HPLength,4);
  150.   /* upload lowpass filter */
  151.   for(i=0;i<filter->LPLength;i++)
  152.     if(filter->DWT_Type == DWT_INT_TYPE){
  153.       s=((Short*)(filter->LPCoeff))[i];
  154.       emit_bits(*usptr,16);
  155.       emit_bits(MARKER_BIT,1);
  156.     }
  157.     else{
  158.       f=(Float)((double *)filter->LPCoeff)[i];
  159.       emit_bits((UShort)((*uIntptr)>>16),16);
  160.       emit_bits(MARKER_BIT,1);
  161.       emit_bits((UShort)(*uIntptr),16);
  162.       emit_bits(MARKER_BIT,1);
  163.     }
  164.   /* upload highpass filter */
  165.   for(i=0;i<filter->HPLength;i++)
  166.     if(filter->DWT_Type == DWT_INT_TYPE){
  167.       s=((Short*)(filter->HPCoeff))[i];
  168.       emit_bits(*usptr,16);
  169.       emit_bits(MARKER_BIT,1);
  170.     }
  171.     else{
  172.       f=(Float)((double *)filter->HPCoeff)[i];
  173.       emit_bits((UShort)((*uIntptr)>>16),16);
  174.       emit_bits(MARKER_BIT,1);
  175.       emit_bits((UShort)(*uIntptr),16);
  176.       emit_bits(MARKER_BIT,1);
  177.     }
  178.   if(filter->DWT_Type == DWT_INT_TYPE){
  179.     emit_bits((UShort)filter->Scale,16);
  180.     emit_bits(MARKER_BIT,1);  /* needed to prevent start code emulation. */
  181.                               /* this is not presented in the current syntax */
  182.   }
  183. }
  184. Void CVTCCommon::download_wavelet_filters(FILTER **Filter, Int type)
  185. {
  186.   Int i,marker_bit;
  187.   double *LPD=NULL, *HPD=NULL;
  188.   Short *LPS=NULL, *HPS=NULL,s;
  189.   UShort *usptr;
  190.   UInt *uIntptr;
  191.   Float f;
  192.   FILTER *filter; // hjlee 0901
  193.   usptr=(UShort *)&s;
  194.   uIntptr=(UInt *)&f;
  195.   /* hjlee 0901 */
  196.   filter = (FILTER *)malloc(sizeof(FILTER));
  197.   if(filter == NULL) 
  198.     errorHandler("Memory allocation errorn");
  199.   filter->DWT_Type = (type ==0)? DWT_INT_TYPE: DWT_DBL_TYPE;
  200.   
  201.   
  202.   /* get filter lengths */
  203.   filter->LPLength=get_X_bits(4);
  204.   filter->HPLength=get_X_bits(4);
  205.   /* allocate memory for filters*/
  206.   if(filter->DWT_Type==DWT_INT_TYPE){
  207.     LPS=(Short *)malloc(sizeof(Short)*filter->LPLength);
  208.     HPS=(Short *)malloc(sizeof(Short)*filter->HPLength);
  209.     if(LPS==NULL || HPS==NULL)
  210.       errorHandler("Cannot allocate memory to download wavelet filtersn");
  211.     filter->LPCoeff=LPS;
  212.     filter->HPCoeff=HPS;
  213.   }
  214.   else{
  215.     LPD=(double *)malloc(sizeof(double)*filter->LPLength);
  216.     HPD=(double *)malloc(sizeof(double)*filter->HPLength);
  217.     if(LPD==NULL || HPD==NULL)
  218.       errorHandler("Cannot allocate memory to download wavelet filtersn");
  219.     filter->LPCoeff=LPD;
  220.     filter->HPCoeff=HPD;
  221.   }
  222.   /* get lowpass filter */
  223.   for(i=0;i<filter->LPLength;i++)
  224.     if(filter->DWT_Type==DWT_INT_TYPE){
  225.       *usptr=get_X_bits(16);
  226.       LPS[i]=s;
  227.       marker_bit=get_X_bits(1);
  228.       check_marker(marker_bit);
  229.     }
  230.     else{
  231.       *uIntptr=get_X_bits(16);
  232.       marker_bit=get_X_bits(1);
  233.       check_marker(marker_bit);
  234.       *uIntptr=((*uIntptr)<<16)+get_X_bits(16);
  235.       marker_bit=get_X_bits(1);
  236.       check_marker(marker_bit);
  237.       LPD[i]=f;
  238.     }      
  239.    /* get highpass filter */
  240.   for(i=0;i<filter->HPLength;i++)
  241.     if(filter->DWT_Type==DWT_INT_TYPE){
  242.       *usptr=get_X_bits(16);
  243.       HPS[i]=s;
  244.       marker_bit=get_X_bits(1);
  245.       check_marker(marker_bit);
  246.     }
  247.     else{
  248.       *uIntptr=get_X_bits(16);
  249.       marker_bit=get_X_bits(1);
  250.       check_marker(marker_bit);
  251.       *uIntptr=((*uIntptr)<<16)+get_X_bits(16);
  252.       marker_bit=get_X_bits(1);
  253.       check_marker(marker_bit);
  254.       HPD[i]=f;
  255.     }      
  256.   check_symmetry(filter);
  257.   if(filter->DWT_Type==DWT_INT_TYPE){
  258.     filter->Scale=get_X_bits(16);
  259.     marker_bit=get_X_bits(1);
  260.     check_marker(marker_bit);
  261.   }
  262. }