[cvs] / xvidcore / src / bitstream / bitstream.h Repository:
ViewVC logotype

Diff of /xvidcore/src/bitstream/bitstream.h

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

revision 1.2, Tue Mar 26 11:16:08 2002 UTC revision 1.16, Sun Feb 9 19:32:52 2003 UTC
# Line 1  Line 1 
1   /******************************************************************************  /*****************************************************************************
2    *                                                                            *   *
3    *  This file is part of XviD, a free MPEG-4 video encoder/decoder            *   *  XVID MPEG-4 VIDEO CODEC
4    *                                                                            *   *  - Bitstream reader/writer inlined functions and constants-
5    *  XviD is an implementation of a part of one or more MPEG-4 Video tools     *   *
6    *  as specified in ISO/IEC 14496-2 standard.  Those intending to use this    *   *  Copyright (C) 2001-2002 - Peter Ross <pross@xvid.org>
7    *  software module in hardware or software products are advised that its     *   *
8    *  use may infringe existing patents or copyrights, and any such use         *   *  This file is part of XviD, a free MPEG-4 video encoder/decoder
9    *  would be at such party's own risk.  The original developer of this        *   *
10    *  software module and his/her company, and subsequent editors and their     *   *  XviD is free software; you can redistribute it and/or modify it
11    *  companies, will have no liability for use of this software or             *   *  under the terms of the GNU General Public License as published by
12    *  modifications or derivatives thereof.                                     *   *  the Free Software Foundation; either version 2 of the License, or
13    *                                                                            *   *  (at your option) any later version.
14    *  XviD is free software; you can redistribute it and/or modify it           *   *
15    *  under the terms of the GNU General Public License as published by         *   *  This program is distributed in the hope that it will be useful,
16    *  the Free Software Foundation; either version 2 of the License, or         *   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17    *  (at your option) any later version.                                       *   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    *                                                                            *   *  GNU General Public License for more details.
19    *  XviD is distributed in the hope that it will be useful, but               *   *
20    *  WITHOUT ANY WARRANTY; without even the implied warranty of                *   *  You should have received a copy of the GNU General Public License
21    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *   *  along with this program; if not, write to the Free Software
22    *  GNU General Public License for more details.                              *   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23    *                                                                            *   *
24    *  You should have received a copy of the GNU General Public License         *   *  Under section 8 of the GNU General Public License, the copyright
25    *  along with this program; if not, write to the Free Software               *   *  holders of XVID explicitly forbid distribution in the following
26    *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *   *  countries:
27    *                                                                            *   *
28    ******************************************************************************/   *    - Japan
29     *    - United States of America
30   /******************************************************************************   *
31    *                                                                            *   *  Linking XviD statically or dynamically with other modules is making a
32    *  bitstream.h                                                               *   *  combined work based on XviD.  Thus, the terms and conditions of the
33    *                                                                            *   *  GNU General Public License cover the whole combination.
34    *  Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au>                    *   *
35    *                                                                            *   *  As a special exception, the copyright holders of XviD give you
36    *  For more information visit the XviD homepage: http://www.xvid.org         *   *  permission to link XviD with independent modules that communicate with
37    *                                                                            *   *  XviD solely through the VFW1.1 and DShow interfaces, regardless of the
38    ******************************************************************************/   *  license terms of these independent modules, and to copy and distribute
39     *  the resulting combined work under terms of your choice, provided that
40   /******************************************************************************   *  every copy of the combined work is accompanied by a complete copy of
41    *                                                                                                                                                        *   *  the source code of XviD (the version of XviD used to produce the
42    *  Revision history:                                                         *   *  combined work), being distributed under the terms of the GNU General
43    *                                                                            *   *  Public License plus this exception.  An independent module is a module
44    *  26.03.2002 interlacing support - modified putvol/vopheaders paramters   *  which is not derived from or based on XviD.
45    *  04.03.2002 putbits speedup (Isibaar)                                      *   *
46    *  03.03.2002 merged BITREADER and BITWRITER (Isibaar)                       *   *  Note that people who make modified versions of XviD are not obligated
47    *      16.12.2001     inital version                                                     *   *  to grant this special exception for their modified versions; it is
48    *                                                                                                                                                        *   *  their choice whether to do so.  The GNU General Public License gives
49    ******************************************************************************/   *  permission to release a modified version without this exception; this
50     *  exception also makes it possible to release a modified version which
51     *  carries forward this exception.
52     *
53     * $Id$
54     *
55     ****************************************************************************/
56    
57  #ifndef _BITSTREAM_H_  #ifndef _BITSTREAM_H_
58  #define _BITSTREAM_H_  #define _BITSTREAM_H_
# Line 55  Line 61 
61  #include "../decoder.h"  #include "../decoder.h"
62  #include "../encoder.h"  #include "../encoder.h"
63    
64  // comment any #defs we dont use  
65    /*****************************************************************************
66     * Constants
67     ****************************************************************************/
68    
69    /* comment any #defs we dont use */
70    
71  #define VIDOBJ_START_CODE               0x00000100      /* ..0x0000011f  */  #define VIDOBJ_START_CODE               0x00000100      /* ..0x0000011f  */
72  #define VIDOBJLAY_START_CODE    0x00000120      /* ..0x0000012f */  #define VIDOBJLAY_START_CODE    0x00000120      /* ..0x0000012f */
# Line 63  Line 74 
74  #define VISOBJSEQ_STOP_CODE             0x000001b1      /* ??? */  #define VISOBJSEQ_STOP_CODE             0x000001b1      /* ??? */
75  #define USERDATA_START_CODE             0x000001b2  #define USERDATA_START_CODE             0x000001b2
76  #define GRPOFVOP_START_CODE             0x000001b3  #define GRPOFVOP_START_CODE             0x000001b3
77  //#define VIDSESERR_ERROR_CODE  0x000001b4  /*#define VIDSESERR_ERROR_CODE  0x000001b4 */
78  #define VISOBJ_START_CODE               0x000001b5  #define VISOBJ_START_CODE               0x000001b5
79  //#define SLICE_START_CODE              0x000001b7  /*#define SLICE_START_CODE      0x000001b7 */
80  //#define EXT_START_CODE                0x000001b8  /*#define EXT_START_CODE        0x000001b8 */
81    
82    
83  #define VISOBJ_TYPE_VIDEO                               1  #define VISOBJ_TYPE_VIDEO                               1
84  //#define VISOBJ_TYPE_STILLTEXTURE              2  /*#define VISOBJ_TYPE_STILLTEXTURE      2 */
85  //#define VISOBJ_TYPE_MESH                              3  /*#define VISOBJ_TYPE_MESH              3 */
86  //#define VISOBJ_TYPE_FBA                               4  /*#define VISOBJ_TYPE_FBA               4 */
87  //#define VISOBJ_TYPE_3DMESH                    5  /*#define VISOBJ_TYPE_3DMESH            5 */
88    
89    
90  #define VIDOBJLAY_TYPE_SIMPLE                   1  #define VIDOBJLAY_TYPE_SIMPLE                   1
91  //#define VIDOBJLAY_TYPE_SIMPLE_SCALABLE        2  /*#define VIDOBJLAY_TYPE_SIMPLE_SCALABLE    2 */
92  #define VIDOBJLAY_TYPE_CORE                             3  #define VIDOBJLAY_TYPE_CORE                             3
93  #define VIDOBJLAY_TYPE_MAIN                             4  #define VIDOBJLAY_TYPE_MAIN                             4
94    
95    
96  //#define VIDOBJLAY_AR_SQUARE                   1  /*#define VIDOBJLAY_AR_SQUARE           1 */
97  //#define VIDOBJLAY_AR_625TYPE_43               2  /*#define VIDOBJLAY_AR_625TYPE_43       2 */
98  //#define VIDOBJLAY_AR_525TYPE_43               3  /*#define VIDOBJLAY_AR_525TYPE_43       3 */
99  //#define VIDOBJLAY_AR_625TYPE_169              8  /*#define VIDOBJLAY_AR_625TYPE_169      8 */
100  //#define VIDOBJLAY_AR_525TYPE_169              9  /*#define VIDOBJLAY_AR_525TYPE_169      9 */
101  #define VIDOBJLAY_AR_EXTPAR                             15  #define VIDOBJLAY_AR_EXTPAR                             15
102    
103    
# Line 102  Line 113 
113  #define READ_MARKER()   BitstreamSkip(bs, 1)  #define READ_MARKER()   BitstreamSkip(bs, 1)
114  #define WRITE_MARKER()  BitstreamPutBit(bs, 1)  #define WRITE_MARKER()  BitstreamPutBit(bs, 1)
115    
116  // vop coding types  /* vop coding types  */
117  // intra, prediction, backward, sprite, not_coded  /* intra, prediction, backward, sprite, not_coded */
118  #define I_VOP   0  #define I_VOP   0
119  #define P_VOP   1  #define P_VOP   1
120  #define B_VOP   2  #define B_VOP   2
121  #define S_VOP   3  #define S_VOP   3
122  #define N_VOP   4  #define N_VOP   4
123    
124    /* resync-specific */
125  typedef struct  #define NUMBITS_VP_RESYNC_MARKER  17
126  {  #define RESYNC_MARKER 1
127          uint32_t bufa;  
128          uint32_t bufb;  
129          uint32_t buf;  /*****************************************************************************
130          uint32_t pos;   * Prototypes
131          uint32_t *tail;   ****************************************************************************/
132          uint32_t *start;  
133          uint32_t length;  int
134  }  read_video_packet_header(Bitstream *bs, const int addbits, int * quant);
135  Bitstream;  
136    
137    /* header stuff */
138  // header stuff  int BitstreamReadHeaders(Bitstream * bs,
139  int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding,                                                   DECODER * dec,
140                                                   uint32_t * quant, uint32_t * fcode, uint32_t * intra_dc_threshold);                                                   uint32_t * rounding,
141                                                     uint32_t * quant,
142                                                     uint32_t * fcode_forward,
143                                                     uint32_t * fcode_backward,
144                                                     uint32_t * intra_dc_threshold);
145    
146    
147  void BitstreamWriteVolHeader(Bitstream * const bs,  void BitstreamWriteVolHeader(Bitstream * const bs,
148                                                  const MBParam * pParam);                                                           const MBParam * pParam,
149                                                             const FRAMEINFO * frame);
150    
151  void BitstreamWriteVopHeader(Bitstream * const bs,  void BitstreamWriteVopHeader(Bitstream * const bs,
152                                                  const MBParam * pParam);                                                           const MBParam * pParam,
153                                                             const FRAMEINFO * frame,
154                                                             int vop_coded);
155    
156    /*****************************************************************************
157     * Inlined functions
158     ****************************************************************************/
159    
160  /* initialise bitstream structure */  /* initialise bitstream structure */
161    
162  static void __inline BitstreamInit(Bitstream * const bs,  static void __inline
163    BitstreamInit(Bitstream * const bs,
164                                                                     void * const bitstream,                                                                     void * const bitstream,
165                                                                     uint32_t length)                                                                     uint32_t length)
166  {  {
# Line 167  Line 188 
188    
189  /* reset bitstream state */  /* reset bitstream state */
190    
191  static void __inline BitstreamReset(Bitstream * const bs)  static void __inline
192    BitstreamReset(Bitstream * const bs)
193  {  {
194          uint32_t tmp;          uint32_t tmp;
195    
# Line 192  Line 214 
214    
215  /* reads n bits from bitstream without changing the stream pos */  /* reads n bits from bitstream without changing the stream pos */
216    
217  static uint32_t __inline BitstreamShowBits(Bitstream * const bs,  static uint32_t __inline
218    BitstreamShowBits(Bitstream * const bs,
219                                                                                     const uint32_t bits)                                                                                     const uint32_t bits)
220  {  {
221          int nbit = (bits + bs->pos) - 32;          int nbit = (bits + bs->pos) - 32;
222          if (nbit > 0)  
223          {          if (nbit > 0) {
224                  return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) |                  return ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) | (bs->
225                                  (bs->bufb >> (32 - nbit));                                                                                                                                   bufb >> (32 -
226          }                                                                                                                                                    nbit));
227          else          } else {
         {  
228                  return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits);                  return (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits);
229          }          }
230  }  }
# Line 210  Line 232 
232    
233  /* skip n bits forward in bitstream */  /* skip n bits forward in bitstream */
234    
235  static __inline void BitstreamSkip(Bitstream * const bs, const uint32_t bits)  static void __inline
236    BitstreamSkip(Bitstream * const bs,
237                              const uint32_t bits)
238  {  {
239          bs->pos += bits;          bs->pos += bits;
240    
241          if (bs->pos >= 32)          if (bs->pos >= 32) {
         {  
242                  uint32_t tmp;                  uint32_t tmp;
243    
244                  bs->bufa = bs->bufb;                  bs->bufa = bs->bufb;
# Line 230  Line 253 
253  }  }
254    
255    
256    /* number of bits to next byte alignment */
257    static uint32_t __inline
258    BitstreamNumBitsToByteAlign(Bitstream *bs)
259    {
260            uint32_t n = (32 - bs->pos) % 8;
261            return n == 0 ? 8 : n;
262    }
263    
264    
265    /* show nbits from next byte alignment */
266    static uint32_t __inline
267    BitstreamShowBitsFromByteAlign(Bitstream *bs, int bits)
268    {
269            int bspos = bs->pos + BitstreamNumBitsToByteAlign(bs);
270            int nbit = (bits + bspos) - 32;
271    
272            if (bspos >= 32) {
273                    return bs->bufb >> (32 - nbit);
274            } else  if (nbit > 0) {
275                    return ((bs->bufa & (0xffffffff >> bspos)) << nbit) | (bs->
276                                                                                                                                     bufb >> (32 -
277                                                                                                                                                      nbit));
278            } else {
279                    return (bs->bufa & (0xffffffff >> bspos)) >> (32 - bspos - bits);
280            }
281    
282    }
283    
284    
285    
286  /* move forward to the next byte boundary */  /* move forward to the next byte boundary */
287    
288  static __inline void BitstreamByteAlign(Bitstream * const bs)  static void __inline
289    BitstreamByteAlign(Bitstream * const bs)
290  {  {
291          uint32_t remainder = bs->pos % 8;          uint32_t remainder = bs->pos % 8;
292          if (remainder)  
293          {          if (remainder) {
294                  BitstreamSkip(bs, 8 - remainder);                  BitstreamSkip(bs, 8 - remainder);
295          }          }
296  }  }
# Line 244  Line 298 
298    
299  /* bitstream length (unit bits) */  /* bitstream length (unit bits) */
300    
301  static uint32_t __inline BitstreamPos(const Bitstream * const bs)  static uint32_t __inline
302    BitstreamPos(const Bitstream * const bs)
303  {  {
304      return 8 * ((uint32_t)bs->tail - (uint32_t)bs->start) + bs->pos;          return((uint32_t)(8*((ptr_t)bs->tail - (ptr_t)bs->start) + bs->pos));
305  }  }
306    
307    
# Line 254  Line 309 
309          NOTE: assumes no futher bitstream functions will be called.          NOTE: assumes no futher bitstream functions will be called.
310   */   */
311    
312  static uint32_t __inline BitstreamLength(Bitstream * const bs)  static uint32_t __inline
313    BitstreamLength(Bitstream * const bs)
314  {  {
315          uint32_t len = (uint32_t) bs->tail - (uint32_t) bs->start;          uint32_t len = (uint32_t)((ptr_t)bs->tail - (ptr_t)bs->start);
316    
317      if (bs->pos)          if (bs->pos) {
     {  
318                  uint32_t b = bs->buf;                  uint32_t b = bs->buf;
319    
320  #ifndef ARCH_IS_BIG_ENDIAN  #ifndef ARCH_IS_BIG_ENDIAN
321                  BSWAP(b);                  BSWAP(b);
322  #endif  #endif
# Line 275  Line 331 
331    
332  /* move bitstream position forward by n bits and write out buffer if needed */  /* move bitstream position forward by n bits and write out buffer if needed */
333    
334  static void __inline BitstreamForward(Bitstream * const bs, const uint32_t bits)  static void __inline
335    BitstreamForward(Bitstream * const bs,
336                                     const uint32_t bits)
337  {  {
338      bs->pos += bits;      bs->pos += bits;
339    
340      if (bs->pos >= 32)          if (bs->pos >= 32) {
     {  
341                  uint32_t b = bs->buf;                  uint32_t b = bs->buf;
342    
343  #ifndef ARCH_IS_BIG_ENDIAN  #ifndef ARCH_IS_BIG_ENDIAN
344                  BSWAP(b);                  BSWAP(b);
345  #endif  #endif
# Line 294  Line 352 
352    
353  /* pad bitstream to the next byte boundary */  /* pad bitstream to the next byte boundary */
354    
355  static void __inline BitstreamPad(Bitstream * const bs)  static void __inline
356    BitstreamPad(Bitstream * const bs)
357  {  {
358          uint32_t remainder = bs->pos % 8;          uint32_t remainder = bs->pos % 8;
359    
360      if (remainder)          if (remainder) {
     {  
361                  BitstreamForward(bs, 8 - remainder);                  BitstreamForward(bs, 8 - remainder);
362      }      }
363  }  }
# Line 307  Line 365 
365    
366  /* read n bits from bitstream */  /* read n bits from bitstream */
367    
368  static uint32_t __inline BitstreamGetBits(Bitstream * const bs,  static uint32_t __inline
369    BitstreamGetBits(Bitstream * const bs,
370                                                                                    const uint32_t n)                                                                                    const uint32_t n)
371  {  {
372          uint32_t ret = BitstreamShowBits(bs, n);          uint32_t ret = BitstreamShowBits(bs, n);
373    
374          BitstreamSkip(bs, n);          BitstreamSkip(bs, n);
375          return ret;          return ret;
376  }  }
# Line 318  Line 378 
378    
379  /* read single bit from bitstream */  /* read single bit from bitstream */
380    
381  static uint32_t __inline BitstreamGetBit(Bitstream * const bs)  static uint32_t __inline
382    BitstreamGetBit(Bitstream * const bs)
383  {  {
384          return BitstreamGetBits(bs, 1);          return BitstreamGetBits(bs, 1);
385  }  }
# Line 326  Line 387 
387    
388  /* write single bit to bitstream */  /* write single bit to bitstream */
389    
390  static void __inline BitstreamPutBit(Bitstream * const bs,  static void __inline
391    BitstreamPutBit(Bitstream * const bs,
392                                                                          const uint32_t bit)                                                                          const uint32_t bit)
393  {  {
394      if (bit)      if (bit)
# Line 338  Line 400 
400    
401  /* write n bits to bitstream */  /* write n bits to bitstream */
402    
403  static void __inline BitstreamPutBits(Bitstream * const bs,  static void __inline
404    BitstreamPutBits(Bitstream * const bs,
405                                                                          const uint32_t value,                                                                          const uint32_t value,
406                                                                          const uint32_t size)                                                                          const uint32_t size)
407  {  {

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.16

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