[svn] / trunk / xvidcore / src / bitstream / bitstream.c Repository:
ViewVC logotype

Diff of /trunk/xvidcore/src/bitstream/bitstream.c

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

revision 4, Fri Mar 8 19:17:46 2002 UTC revision 478, Thu Sep 12 19:06:37 2002 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 functions -
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@cs.rmit.edu.au>
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 program is an implementation of a part of one or more MPEG-4
9    *  would be at such party's own risk.  The original developer of this        *   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
10    *  software module and his/her company, and subsequent editors and their     *   *  to use this software module in hardware or software products are
11    *  companies, will have no liability for use of this software or             *   *  advised that its use may infringe existing patents or copyrights, and
12    *  modifications or derivatives thereof.                                     *   *  any such use would be at such party's own risk.  The original
13    *                                                                            *   *  developer of this software module and his/her company, and subsequent
14    *  XviD is free software; you can redistribute it and/or modify it           *   *  editors and their companies, will have no liability for use of this
15    *  under the terms of the GNU General Public License as published by         *   *  software or modifications or derivatives thereof.
16    *  the Free Software Foundation; either version 2 of the License, or         *   *
17    *  (at your option) any later version.                                       *   *  This program is free software ; you can redistribute it and/or modify
18    *                                                                            *   *  it under the terms of the GNU General Public License as published by
19    *  XviD is distributed in the hope that it will be useful, but               *   *  the Free Software Foundation ; either version 2 of the License, or
20    *  WITHOUT ANY WARRANTY; without even the implied warranty of                *   *  (at your option) any later version.
21    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *   *
22    *  GNU General Public License for more details.                              *   *  This program is distributed in the hope that it will be useful,
23    *                                                                            *   *  but WITHOUT ANY WARRANTY ; without even the implied warranty of
24    *  You should have received a copy of the GNU General Public License         *   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25    *  along with this program; if not, write to the Free Software               *   *  GNU General Public License for more details.
26    *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *   *
27    *                                                                            *   *  You should have received a copy of the GNU General Public License
28    ******************************************************************************/   *  along with this program ; if not, write to the Free Software
29     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
30   /******************************************************************************   *
31    *                                                                            *   * $Id: bitstream.c,v 1.30 2002-09-12 19:06:37 edgomez Exp $
32    *  bitstream.c                                                               *   *
33    *                                                                            *   ****************************************************************************/
   *  Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au>                    *  
   *                                                                            *  
   *  For more information visit the XviD homepage: http://www.xvid.org         *  
   *                                                                            *  
   ******************************************************************************/  
   
  /******************************************************************************  
   *                                                                                                                                                        *  
   *  Revision history:                                                         *  
   *                                                                            *  
   *  03.03.2002 qmatrix writing                                                                                            *  
   *  03.03.2002 merged BITREADER and BITWRITER                                                             *  
   *      30.02.2002     intra_dc_threshold support                                                                         *  
   *      04.12.2001     support for additional headers                                                             *  
   *      16.12.2001     inital version                                                     *  
   *                                                                                                                                                        *  
   ******************************************************************************/  
   
34    
35  #include "bitstream.h"  #include "bitstream.h"
36  #include "zigzag.h"  #include "zigzag.h"
37  #include "../quant/quant_matrix.h"  #include "../quant/quant_matrix.h"
38    
39  static int __inline log2bin(int value)  /*****************************************************************************
40     * Functions
41     ****************************************************************************/
42    
43    static uint32_t __inline
44    log2bin(uint32_t value)
45  {  {
46    /* Changed by Chenm001 */
47    #ifndef WIN32
48          int n = 0;          int n = 0;
49          while (value)  
50          {          while (value) {
51                  value >>= 1;                  value >>= 1;
52                  n++;                  n++;
53          }          }
54          return n;          return n;
55    #else
56            __asm {
57                    bsr eax, value
58                    inc eax
59            }
60    #endif
61  }  }
62    
63    
64  static const uint32_t intra_dc_threshold_table[] =  static const uint32_t intra_dc_threshold_table[] = {
 {  
65          32,     /* never use */          32,     /* never use */
66          13,          13,
67          15,          15,
# Line 79  Line 73 
73  };  };
74    
75    
76  void bs_get_matrix(Bitstream * bs, uint8_t * matrix)  void
77    bs_get_matrix(Bitstream * bs,
78                              uint8_t * matrix)
79  {  {
80          int i = 0;          int i = 0;
81      int last, value = 0;      int last, value = 0;
82    
83      do          do {
         {  
84                  last = value;                  last = value;
85          value = BitstreamGetBits(bs, 8);          value = BitstreamGetBits(bs, 8);
86          matrix[ scan_tables[0][i++] ]  = value;          matrix[ scan_tables[0][i++] ]  = value;
87      }      }
88      while (value != 0 && i < 64);      while (value != 0 && i < 64);
89            i--;    // fix little bug at coeff not full
90    
91          while (i < 64)          while (i < 64) {
         {  
92                  matrix[ scan_tables[0][i++] ]  = last;                  matrix[ scan_tables[0][i++] ]  = last;
93          }          }
94  }  }
95    
96    
97    
98    // for PVOP addbits == fcode - 1
99    // for BVOP addbits == max(fcode,bcode) - 1
100    // returns mbpos
101    int
102    read_video_packet_header(Bitstream *bs, const int addbits, int * quant)
103    {
104            int nbits;
105            int mbnum;
106            int hec;
107    
108            nbits = NUMBITS_VP_RESYNC_MARKER + addbits;
109    
110            BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs));
111            BitstreamSkip(bs, nbits);
112    
113            DPRINTF(DPRINTF_STARTCODE, "<video_packet_header>");
114    
115            // if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
116                    // hec
117                    // vop_width
118                    // marker_bit
119                    // vop_height
120                    // marker_bit
121    
122            //}
123    
124            mbnum = BitstreamGetBits(bs, 9);
125            DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum);
126    
127            // if (dec->shape != VIDOBJLAY_SHAPE_BINARYONLY)
128            *quant = BitstreamGetBits(bs, 5);
129            DPRINTF(DPRINTF_HEADER, "quant %i", *quant);
130    
131            // if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)
132            hec = BitstreamGetBit(bs);
133            DPRINTF(DPRINTF_HEADER, "header_extension_code %i", hec);
134            // if (hec)
135            //   .. decoder hec-header ...
136    
137            return mbnum;
138    }
139    
140    
141    
142  /*  /*
143  decode headers  decode headers
144  returns coding_type, or -1 if error  returns coding_type, or -1 if error
145  */  */
146    
147  int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, uint32_t * quant, uint32_t * fcode, uint32_t * intra_dc_threshold)  #define VIDOBJ_START_CODE_MASK          0x0000001f
148    #define VIDOBJLAY_START_CODE_MASK       0x0000000f
149    
150    int
151    BitstreamReadHeaders(Bitstream * bs,
152                                             DECODER * dec,
153                                             uint32_t * rounding,
154                                             uint32_t * quant,
155                                             uint32_t * fcode_forward,
156                                             uint32_t * fcode_backward,
157                                             uint32_t * intra_dc_threshold)
158  {  {
159          uint32_t vol_ver_id;          uint32_t vol_ver_id;
160          uint32_t time_inc_resolution;          static uint32_t time_increment_resolution;
161          uint32_t coding_type;          uint32_t coding_type;
162          uint32_t start_code;          uint32_t start_code;
163            uint32_t time_incr = 0;
164            int32_t time_increment = 0;
165    
166          do          do {
         {  
167                  BitstreamByteAlign(bs);                  BitstreamByteAlign(bs);
168                  start_code = BitstreamShowBits(bs, 32);                  start_code = BitstreamShowBits(bs, 32);
169    
170                  if (start_code == VISOBJSEQ_START_CODE)                  if (start_code == VISOBJSEQ_START_CODE) {
171                  {  
172                          // DEBUG("visual_object_sequence");                          int profile;
173    
174                            DPRINTF(DPRINTF_STARTCODE, "<visual_object_sequence>");
175    
176                          BitstreamSkip(bs, 32);                          // visual_object_sequence_start_code                          BitstreamSkip(bs, 32);                          // visual_object_sequence_start_code
177                          BitstreamSkip(bs, 8);                                   // profile_and_level_indication                          profile = BitstreamGetBits(bs, 8);      // profile_and_level_indication
178                  }  
179                  else if (start_code == VISOBJSEQ_STOP_CODE)                          DPRINTF(DPRINTF_HEADER, "profile_and_level_indication %i", profile);
180                  {  
181                    } else if (start_code == VISOBJSEQ_STOP_CODE) {
182    
183                          BitstreamSkip(bs, 32);                          // visual_object_sequence_stop_code                          BitstreamSkip(bs, 32);                          // visual_object_sequence_stop_code
184                  }  
185                  else if (start_code == VISOBJ_START_CODE)                          DPRINTF(DPRINTF_STARTCODE, "</visual_object_sequence>");
186                  {  
187                          // DEBUG("visual_object");                  } else if (start_code == VISOBJ_START_CODE) {
188    
189                            DPRINTF(DPRINTF_STARTCODE, "<visual_object>");
190    
191                          BitstreamSkip(bs,32);                                   // visual_object_start_code                          BitstreamSkip(bs,32);                                   // visual_object_start_code
192                          if (BitstreamGetBit(bs))                                // is_visual_object_identified                          if (BitstreamGetBit(bs))                                // is_visual_object_identified
193                          {                          {
194                                  vol_ver_id = BitstreamGetBits(bs,4);    // visual_object_ver_id                                  vol_ver_id = BitstreamGetBits(bs,4);    // visual_object_ver_id
195                                    DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id);
196                                  BitstreamSkip(bs, 3);                           // visual_object_priority                                  BitstreamSkip(bs, 3);                           // visual_object_priority
197                          }                          } else {
                         else  
                         {  
198                                  vol_ver_id = 1;                                  vol_ver_id = 1;
199                          }                          }
200    
201                          if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO)      // visual_object_type                          if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO)      // visual_object_type
202                          {                          {
203                                  DEBUG("visual_object_type != video");                                  DPRINTF(DPRINTF_ERROR, "visual_object_type != video");
204                                  return -1;                                  return -1;
205                          }                          }
206                          BitstreamSkip(bs, 4);                          BitstreamSkip(bs, 4);
# Line 150  Line 209 
209    
210                          if (BitstreamGetBit(bs))                                // video_signal_type                          if (BitstreamGetBit(bs))                                // video_signal_type
211                          {                          {
212                                  DEBUG("+ video_signal_type");                                  DPRINTF(DPRINTF_HEADER,"+ video_signal_type");
213                                  BitstreamSkip(bs, 3);                           // video_format                                  BitstreamSkip(bs, 3);                           // video_format
214                                  BitstreamSkip(bs, 1);                           // video_range                                  BitstreamSkip(bs, 1);                           // video_range
215                                  if (BitstreamGetBit(bs))                        // color_description                                  if (BitstreamGetBit(bs))                        // color_description
216                                  {                                  {
217                                          DEBUG("+ color_description");                                          DPRINTF(DPRINTF_HEADER,"+ color_description");
218                                          BitstreamSkip(bs, 8);                   // color_primaries                                          BitstreamSkip(bs, 8);                   // color_primaries
219                                          BitstreamSkip(bs, 8);                   // transfer_characteristics                                          BitstreamSkip(bs, 8);                   // transfer_characteristics
220                                          BitstreamSkip(bs, 8);                   // matrix_coefficients                                          BitstreamSkip(bs, 8);                   // matrix_coefficients
221                                  }                                  }
222                          }                          }
223                  }                  } else if ((start_code & ~VIDOBJ_START_CODE_MASK) == VIDOBJ_START_CODE) {
224                  else if ((start_code & ~0x1f) == VIDOBJ_START_CODE)  
225                  {                          DPRINTF(DPRINTF_STARTCODE, "<video_object>");
226                            DPRINTF(DPRINTF_HEADER, "vo id %i", start_code & VIDOBJ_START_CODE_MASK);
227    
228                          BitstreamSkip(bs, 32);          // video_object_start_code                          BitstreamSkip(bs, 32);          // video_object_start_code
229                  }  
230                  else if ((start_code & ~0xf) == VIDOBJLAY_START_CODE)                  } else if ((start_code & ~VIDOBJLAY_START_CODE_MASK) == VIDOBJLAY_START_CODE) {
231                  {  
232                          // DEBUG("video_object_layer");                          DPRINTF(DPRINTF_STARTCODE, "<video_object_layer>");
233                            DPRINTF(DPRINTF_HEADER, "vol id %i", start_code & VIDOBJLAY_START_CODE_MASK);
234    
235                          BitstreamSkip(bs, 32);                                  // video_object_layer_start_code                          BitstreamSkip(bs, 32);                                  // video_object_layer_start_code
236    
237                          BitstreamSkip(bs, 1);                                                                   // random_accessible_vol                          BitstreamSkip(bs, 1);                                                                   // random_accessible_vol
238    
239                          // video_object_type_indication                          // video_object_type_indication
240                          if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE &&                          if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE && BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE && BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN && BitstreamShowBits(bs, 8) != 0)   // BUGGY DIVX
                                 BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE &&  
                                 BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN &&  
                                 BitstreamShowBits(bs, 8) != 0)          // BUGGY DIVX  
241                          {                          {
242                                  DEBUG1("video_object_type_indication not supported", BitstreamShowBits(bs, 8));                                  DPRINTF(DPRINTF_ERROR,"video_object_type_indication %i not supported ",
243                                            BitstreamShowBits(bs, 8));
244                                  return -1;                                  return -1;
245                          }                          }
246                          BitstreamSkip(bs, 8);                          BitstreamSkip(bs, 8);
# Line 187  Line 248 
248    
249                          if (BitstreamGetBit(bs))                                        // is_object_layer_identifier                          if (BitstreamGetBit(bs))                                        // is_object_layer_identifier
250                          {                          {
251                                  DEBUG("+ is_object_layer_identifier");                                  DPRINTF(DPRINTF_HEADER, "+ is_object_layer_identifier");
252                                  vol_ver_id = BitstreamGetBits(bs,4);            // video_object_layer_verid                                  vol_ver_id = BitstreamGetBits(bs,4);            // video_object_layer_verid
253                                    DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id);
254                                  BitstreamSkip(bs, 3);                                   // video_object_layer_priority                                  BitstreamSkip(bs, 3);                                   // video_object_layer_priority
255                          }                          } else {
                         else  
                         {  
256                                  vol_ver_id = 1;                                  vol_ver_id = 1;
257                          }                          }
                         //DEBUGI("vol_ver_id", vol_ver_id);  
258    
259                          if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR)     // aspect_ratio_info                          if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR)     // aspect_ratio_info
260                          {                          {
261                                  DEBUG("+ aspect_ratio_info");                                  DPRINTF(DPRINTF_HEADER, "+ aspect_ratio_info");
262                                  BitstreamSkip(bs, 8);                                           // par_width                                  BitstreamSkip(bs, 8);                                           // par_width
263                                  BitstreamSkip(bs, 8);                                           // par_height                                  BitstreamSkip(bs, 8);                                           // par_height
264                          }                          }
265    
266                          if (BitstreamGetBit(bs))                // vol_control_parameters                          if (BitstreamGetBit(bs))                // vol_control_parameters
267                          {                          {
268                                  DEBUG("+ vol_control_parameters");                                  DPRINTF(DPRINTF_HEADER, "+ vol_control_parameters");
269                                  BitstreamSkip(bs, 2);                                           // chroma_format                                  BitstreamSkip(bs, 2);                                           // chroma_format
270                                  BitstreamSkip(bs, 1);                                           // low_delay                                  dec->low_delay = BitstreamGetBit(bs);   // low_delay
271                                    DPRINTF(DPRINTF_HEADER, "low_delay %i", dec->low_delay);
272                                  if (BitstreamGetBit(bs))                                        // vbv_parameters                                  if (BitstreamGetBit(bs))                                        // vbv_parameters
273                                  {                                  {
274                                          DEBUG("+ vbv_parameters");                                          DPRINTF(DPRINTF_HEADER,"+ vbv_parameters");
275                                          BitstreamSkip(bs, 15);                          // first_half_bitrate                                          BitstreamSkip(bs, 15);                          // first_half_bitrate
276                                          READ_MARKER();                                          READ_MARKER();
277                                          BitstreamSkip(bs, 15);                          // latter_half_bitrate                                          BitstreamSkip(bs, 15);                          // latter_half_bitrate
# Line 228  Line 288 
288                          }                          }
289    
290                          dec->shape = BitstreamGetBits(bs, 2);   // video_object_layer_shape                          dec->shape = BitstreamGetBits(bs, 2);   // video_object_layer_shape
291                          // DEBUG1("shape", dec->shape);                          DPRINTF(DPRINTF_HEADER, "shape %i", dec->shape);
292    
293                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1)                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) {
                         {  
294                                  BitstreamSkip(bs, 4);           // video_object_layer_shape_extension                                  BitstreamSkip(bs, 4);           // video_object_layer_shape_extension
295                          }                          }
296    
297                          READ_MARKER();                          READ_MARKER();
298    
299                          time_inc_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution  // *************************** for decode B-frame time ***********************
300                          time_inc_resolution--;                          time_increment_resolution = BitstreamGetBits(bs, 16);   // vop_time_increment_resolution
301                          if (time_inc_resolution > 0)  
302                          {                          DPRINTF(DPRINTF_HEADER,"vop_time_increment_resolution %i", time_increment_resolution);
                                 dec->time_inc_bits = log2bin(time_inc_resolution);  
                         }  
                         else  
                         {  
                                 // dec->time_inc_bits = 0;  
303    
304    //                      time_increment_resolution--;
305    
306                            if (time_increment_resolution > 0) {
307                                    dec->time_inc_bits = log2bin(time_increment_resolution-1);
308                            } else {
309                                    // dec->time_inc_bits = 0;
310                                  // for "old" xvid compatibility, set time_inc_bits = 1                                  // for "old" xvid compatibility, set time_inc_bits = 1
311                                  dec->time_inc_bits = 1;                                  dec->time_inc_bits = 1;
312                          }                          }
# Line 255  Line 315 
315    
316                          if (BitstreamGetBit(bs))                                                // fixed_vop_rate                          if (BitstreamGetBit(bs))                                                // fixed_vop_rate
317                          {                          {
318                                    DPRINTF(DPRINTF_HEADER, "+ fixed_vop_rate");
319                                  BitstreamSkip(bs, dec->time_inc_bits);  // fixed_vop_time_increment                                  BitstreamSkip(bs, dec->time_inc_bits);  // fixed_vop_time_increment
320                          }                          }
321    
322                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) {
                         {  
323    
324                                  if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR)                                  if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) {
                                 {  
325                                          uint32_t width, height;                                          uint32_t width, height;
326    
327                                          READ_MARKER();                                          READ_MARKER();
328                                          width = BitstreamGetBits(bs, 13);                       // video_object_layer_width                                          width = BitstreamGetBits(bs, 13);                       // video_object_layer_width
                                         //DEBUGI("width", width);  
329                                          READ_MARKER();                                          READ_MARKER();
330                                          height = BitstreamGetBits(bs, 13);              // video_object_layer_height                                          height = BitstreamGetBits(bs, 13);              // video_object_layer_height
                                         //DEBUGI("height", height);  
331                                          READ_MARKER();                                          READ_MARKER();
332    
333                                          if (width != dec->width || height != dec->height)                                          DPRINTF(DPRINTF_HEADER, "width %i", width);
334                                          {                                          DPRINTF(DPRINTF_HEADER, "height %i", height);
335                                                  DEBUG("FATAL: video dimension discrepancy ***");  
336                                                  DEBUG2("bitstream width/height", width, height);                                          // for auto set width & height
337                                                  DEBUG2("param width/height", dec->width, dec->height);                                          if (dec->width == 0)
338                                                    dec->width = width;
339                                            if (dec->height == 0)
340                                                    dec->height = height;
341    
342                                            if (width != dec->width || height != dec->height) {
343                                                    DPRINTF(DPRINTF_ERROR, "XVID_DEC_PARAM width/height does not match bitstream");
344                                                  return -1;                                                  return -1;
345                                          }                                          }
346    
347                                  }                                  }
348    
349                                  if (BitstreamGetBit(bs))                                // interlaced                                  dec->interlacing = BitstreamGetBit(bs);
350                                  {                                  DPRINTF(DPRINTF_HEADER, "interlace", dec->interlacing);
                                         DEBUG("TODO: interlaced");  
                                         // TODO  
                                         return -1;  
                                 }  
351    
352                                  if (!BitstreamGetBit(bs))                               // obmc_disable                                  if (!BitstreamGetBit(bs))                               // obmc_disable
353                                  {                                  {
354                                          DEBUG("IGNORED/TODO: !obmc_disable");                                          DPRINTF(DPRINTF_ERROR, "obmc_disabled==false not supported");
355                                          // TODO                                          // TODO
356                                          // fucking divx4.02 has this enabled                                          // fucking divx4.02 has this enabled
357                                  }                                  }
358    
359                                  if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2)))  // sprite_enable                                  if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2)))  // sprite_enable
360                                  {                                  {
361                                          DEBUG("sprite_enable; not supported");                                          DPRINTF(DPRINTF_ERROR, "spriate_enabled not supported");
362                                          return -1;                                          return -1;
363                                  }                                  }
364    
365                                  if (vol_ver_id != 1 && dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)                                  if (vol_ver_id != 1 &&
366                                  {                                          dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
367                                          BitstreamSkip(bs, 1);                                   // sadct_disable                                          BitstreamSkip(bs, 1);                                   // sadct_disable
368                                  }                                  }
369    
370                                  if (BitstreamGetBit(bs))                                                // not_8_bit                                  if (BitstreamGetBit(bs))                                                // not_8_bit
371                                  {                                  {
372                                          DEBUG("+ not_8_bit [IGNORED/TODO]");                                          DPRINTF(DPRINTF_HEADER, "not_8_bit==true (ignored)");
373                                          dec->quant_bits = BitstreamGetBits(bs, 4);      // quant_precision                                          dec->quant_bits = BitstreamGetBits(bs, 4);      // quant_precision
374                                          BitstreamSkip(bs, 4);                                           // bits_per_pixel                                          BitstreamSkip(bs, 4);                                           // bits_per_pixel
375                                  }                                  } else {
                                 else  
                                 {  
376                                          dec->quant_bits = 5;                                          dec->quant_bits = 5;
377                                  }                                  }
378    
379                                  if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE)                                  if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {
                                 {  
380                                          BitstreamSkip(bs, 1);                   // no_gray_quant_update                                          BitstreamSkip(bs, 1);                   // no_gray_quant_update
381                                          BitstreamSkip(bs, 1);                   // composition_method                                          BitstreamSkip(bs, 1);                   // composition_method
382                                          BitstreamSkip(bs, 1);                   // linear_composition                                          BitstreamSkip(bs, 1);                   // linear_composition
383                                  }                                  }
384    
385                                  dec->quant_type = BitstreamGetBit(bs);          // quant_type                                  dec->quant_type = BitstreamGetBit(bs);          // quant_type
386                                  // DEBUG1("**** quant_type", dec->quant_type);                                  DPRINTF(DPRINTF_HEADER, "quant_type %i", dec->quant_type);
387    
388                                  if (dec->quant_type)                                  if (dec->quant_type) {
                                 {  
389                                          if (BitstreamGetBit(bs))                // load_intra_quant_mat                                          if (BitstreamGetBit(bs))                // load_intra_quant_mat
390                                          {                                          {
391                                                  uint8_t *matrix;                                                  uint8_t matrix[64];
392    
393                                                    DPRINTF(DPRINTF_HEADER, "load_intra_quant_mat");
394    
395                                                  bs_get_matrix(bs, matrix);                                                  bs_get_matrix(bs, matrix);
396                                                  set_intra_matrix(matrix);                                                  set_intra_matrix(matrix);
397                                          }                                          } else
398                                                    set_intra_matrix(get_default_intra_matrix());
399    
400                                          if (BitstreamGetBit(bs))                // load_inter_quant_mat                                          if (BitstreamGetBit(bs))                // load_inter_quant_mat
401                                          {                                          {
402                                                  uint8_t *matrix;                                                  uint8_t matrix[64];
403    
404                                                    DPRINTF(DPRINTF_HEADER, "load_inter_quant_mat");
405    
406                                                  bs_get_matrix(bs, matrix);                                                  bs_get_matrix(bs, matrix);
407                                                  set_inter_matrix(matrix);                                                  set_inter_matrix(matrix);
408                                          }                                          } else
409                                                    set_inter_matrix(get_default_inter_matrix());
410    
411                                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE)                                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {
412                                          {                                                  DPRINTF(DPRINTF_ERROR, "greyscale matrix not supported");
                                                 // TODO  
                                                 DEBUG("TODO: grayscale matrix stuff");  
413                                                  return -1;                                                  return -1;
414                                          }                                          }
415    
416                                  }                                  }
417    
418    
419                                  if (vol_ver_id != 1)                                  if (vol_ver_id != 1) {
420                                  {                                          DEBUG("QUARTERPEL BITSTREAM");
421                                          dec->quarterpel = BitstreamGetBit(bs);  // quarter_sampe                                          dec->quarterpel = BitstreamGetBit(bs);  // quarter_sample
                                         if (dec->quarterpel)  
                                         {  
                                                 DEBUG("IGNORED/TODO: quarter_sample");  
                                         }  
422                                  }                                  }
423                                  else                                  else
                                 {  
424                                          dec->quarterpel = 0;                                          dec->quarterpel = 0;
425                                  }  
426    
427                                  if (!BitstreamGetBit(bs))                       // complexity_estimation_disable                                  if (!BitstreamGetBit(bs))                       // complexity_estimation_disable
428                                  {                                  {
429                                          DEBUG("TODO: complexity_estimation header");                                          DPRINTF(DPRINTF_ERROR, "complexity_estimation not supported");
                                         // TODO  
430                                          return -1;                                          return -1;
431                                  }                                  }
432    
433                                  if (!BitstreamGetBit(bs))                       // resync_marker_disable                                  BitstreamSkip(bs, 1);   // resync_marker_disable
                                 {  
                                         DEBUG("IGNORED/TODO: !resync_marker_disable");  
                                         // TODO  
                                 }  
434    
435                                  if (BitstreamGetBit(bs))                // data_partitioned                                  if (BitstreamGetBit(bs))                // data_partitioned
436                                  {                                  {
437                                          DEBUG("+ data_partitioned");                                          DPRINTF(DPRINTF_ERROR, "data_partitioned not supported");
438                                          BitstreamSkip(bs, 1);           // reversible_vlc                                          BitstreamSkip(bs, 1);           // reversible_vlc
439                                  }                                  }
440    
441                                  if (vol_ver_id != 1)                                  if (vol_ver_id != 1) {
                                 {  
442                                          if (BitstreamGetBit(bs))                        // newpred_enable                                          if (BitstreamGetBit(bs))                        // newpred_enable
443                                          {                                          {
444                                                  DEBUG("+ newpred_enable");                                                  DPRINTF(DPRINTF_HEADER, "+ newpred_enable");
445                                                  BitstreamSkip(bs, 2);                   // requested_upstream_message_type                                                  BitstreamSkip(bs, 2);                   // requested_upstream_message_type
446                                                  BitstreamSkip(bs, 1);                   // newpred_segment_type                                                  BitstreamSkip(bs, 1);                   // newpred_segment_type
447                                          }                                          }
448                                          if (BitstreamGetBit(bs))                        // reduced_resolution_vop_enable                                          if (BitstreamGetBit(bs))                        // reduced_resolution_vop_enable
449                                          {                                          {
450                                                  DEBUG("TODO: reduced_resolution_vop_enable");                                                  DPRINTF(DPRINTF_ERROR, "reduced_resolution_vop not supported");
                                                 // TODO  
451                                                  return -1;                                                  return -1;
452                                          }                                          }
453                                  }                                  }
454    
455                                  if (BitstreamGetBit(bs))        // scalability                                  if ((dec->scalability = BitstreamGetBit(bs)))   // scalability
456                                  {                                  {
457                                          // TODO                                          DPRINTF(DPRINTF_ERROR, "scalability not supported");
                                         DEBUG("TODO: scalability");  
458                                          return -1;                                          return -1;
459                                  }                                  }
460                          }                          } else                          // dec->shape == BINARY_ONLY
                         else    // dec->shape == BINARY_ONLY  
                         {  
                                 if (vol_ver_id != 1)  
461                                  {                                  {
462                                    if (vol_ver_id != 1) {
463                                          if (BitstreamGetBit(bs))        // scalability                                          if (BitstreamGetBit(bs))        // scalability
464                                          {                                          {
465                                                  // TODO                                          DPRINTF(DPRINTF_ERROR, "scalability not supported");
                                                 DEBUG("TODO: scalability");  
466                                                  return -1;                                                  return -1;
467                                          }                                          }
468                                  }                                  }
# Line 425  Line 470 
470    
471                          }                          }
472    
473                  }                  } else if (start_code == GRPOFVOP_START_CODE) {
474                  else if (start_code == GRPOFVOP_START_CODE)  
475                  {                          DPRINTF(DPRINTF_STARTCODE, "<group_of_vop>");
476                          // DEBUG("group_of_vop");  
477                          BitstreamSkip(bs, 32);                          BitstreamSkip(bs, 32);
478                          {                          {
479                                  int hours, minutes, seconds;                                  int hours, minutes, seconds;
480    
481                                  hours = BitstreamGetBits(bs, 5);                                  hours = BitstreamGetBits(bs, 5);
482                                  minutes = BitstreamGetBits(bs, 6);                                  minutes = BitstreamGetBits(bs, 6);
483                                  READ_MARKER();                                  READ_MARKER();
484                                  seconds = BitstreamGetBits(bs, 6);                                  seconds = BitstreamGetBits(bs, 6);
485                                  // DEBUG3("hms", hours, minutes, seconds);  
486                                    DPRINTF(DPRINTF_HEADER, "time %ih%im%is", hours);
487                          }                          }
488                          BitstreamSkip(bs, 1);                   // closed_gov                          BitstreamSkip(bs, 1);                   // closed_gov
489                          BitstreamSkip(bs, 1);                   // broken_link                          BitstreamSkip(bs, 1);                   // broken_link
490                  }  
491                  else if (start_code == VOP_START_CODE)                  } else if (start_code == VOP_START_CODE) {
492                  {  
493                          // DEBUG("vop_start_code");                          DPRINTF(DPRINTF_STARTCODE, "<vop>");
494    
495                          BitstreamSkip(bs, 32);                                          // vop_start_code                          BitstreamSkip(bs, 32);                                          // vop_start_code
496    
497                          coding_type = BitstreamGetBits(bs, 2);          // vop_coding_type                          coding_type = BitstreamGetBits(bs, 2);          // vop_coding_type
498                          //DEBUG1("coding_type", coding_type);                          DPRINTF(DPRINTF_HEADER, "coding_type %i", coding_type);
499    
500                          while (BitstreamGetBit(bs) != 0) ;                      // time_base  // *************************** for decode B-frame time ***********************
501                            while (BitstreamGetBit(bs) != 0)        // time_base
502                                    time_incr++;
503    
504                          READ_MARKER();                          READ_MARKER();
505    
506                          //DEBUG1("time_inc_bits", dec->time_inc_bits);                          if (dec->time_inc_bits) {
507                          //DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits));                                  time_increment = (BitstreamGetBits(bs, dec->time_inc_bits));    // vop_time_increment
508                          if (dec->time_inc_bits)                          }
509                          {  
510                                  BitstreamSkip(bs, dec->time_inc_bits);  // vop_time_increment                          DPRINTF(DPRINTF_HEADER, "time_base %i", time_incr);
511                            DPRINTF(DPRINTF_HEADER, "time_increment %i", time_increment);
512    
513                            DPRINTF(DPRINTF_TIMECODE, "%c %i:%i",
514                                    coding_type == I_VOP ? 'I' : coding_type == P_VOP ? 'P' : 'B',
515                                    time_incr, time_increment);
516    
517                            if (coding_type != B_VOP) {
518                                    dec->last_time_base = dec->time_base;
519                                    dec->time_base += time_incr;
520                                    dec->time = time_increment;
521    
522    /*                                      dec->time_base * time_increment_resolution +
523                                            time_increment;
524    */                              dec->time_pp = (uint32_t)
525                                            (time_increment_resolution + dec->time - dec->last_non_b_time)%time_increment_resolution;
526                                    dec->last_non_b_time = dec->time;
527                            } else {
528                                    dec->time = time_increment;
529    /*
530                                            (dec->last_time_base +
531                                             time_incr) * time_increment_resolution + time_increment;
532    */
533                                    dec->time_bp = (uint32_t)
534                                            (time_increment_resolution + dec->last_non_b_time - dec->time)%time_increment_resolution;
535                          }                          }
536    
537                          READ_MARKER();                          READ_MARKER();
538    
539                          if (!BitstreamGetBit(bs))                                       // vop_coded                          if (!BitstreamGetBit(bs))                                       // vop_coded
540                          {                          {
541                                    DPRINTF(DPRINTF_HEADER, "vop_coded==false");
542                                  return N_VOP;                                  return N_VOP;
543                          }                          }
544    
# Line 472  Line 547 
547                          }                          }
548                          */                          */
549    
550                          if (coding_type != I_VOP)                          // fix a little bug by MinChen <chenm002@163.com>
551                          {                          if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) &&
552                                    (coding_type == P_VOP)) {
553                                  *rounding = BitstreamGetBit(bs);        // rounding_type                                  *rounding = BitstreamGetBit(bs);        // rounding_type
554                                  //DEBUG1("rounding", *rounding);                                  DPRINTF(DPRINTF_HEADER, "rounding %i", *rounding);
555                          }                          }
556    
557                          /* if (reduced_resolution_enable)                          /* if (reduced_resolution_enable)
# Line 483  Line 559 
559                          }                          }
560                          */                          */
561    
562                          if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)                          if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
                         {  
563                                  uint32_t width, height;                                  uint32_t width, height;
564                                  uint32_t horiz_mc_ref, vert_mc_ref;                                  uint32_t horiz_mc_ref, vert_mc_ref;
565    
# Line 497  Line 572 
572                                  vert_mc_ref = BitstreamGetBits(bs, 13);                                  vert_mc_ref = BitstreamGetBits(bs, 13);
573                                  READ_MARKER();                                  READ_MARKER();
574    
575                                  // DEBUG2("vop_width/height", width, height);                                  DPRINTF(DPRINTF_HEADER, "width %i", width);
576                                  // DEBUG2("ref             ", horiz_mc_ref, vert_mc_ref);                                  DPRINTF(DPRINTF_HEADER, "height %i", height);
577                                    DPRINTF(DPRINTF_HEADER, "horiz_mc_ref %i", horiz_mc_ref);
578                                    DPRINTF(DPRINTF_HEADER, "vert_mc_ref %i", vert_mc_ref);
579    
580                                  BitstreamSkip(bs, 1);                           // change_conv_ratio_disable                                  BitstreamSkip(bs, 1);                           // change_conv_ratio_disable
581                                  if (BitstreamGetBit(bs))                        // vop_constant_alpha                                  if (BitstreamGetBit(bs))                        // vop_constant_alpha
# Line 508  Line 585 
585                          }                          }
586    
587    
588                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) {
                         {  
589                                  // intra_dc_vlc_threshold                                  // intra_dc_vlc_threshold
590                                  *intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ];                                  *intra_dc_threshold =
591                                            intra_dc_threshold_table[BitstreamGetBits(bs, 3)];
592    
593                                  /* if (interlaced)                                  if (dec->interlacing) {
594                                          {                                          dec->top_field_first = BitstreamGetBit(bs);
595                                                  BitstreamSkip(bs, 1);           // top_field_first                                          DPRINTF(DPRINTF_HEADER, "interlace top_field_first %i", dec->top_field_first);
596                                                  BitstreamSkip(bs, 1);           // alternative_vertical_scan_flag                                          dec->alternate_vertical_scan = BitstreamGetBit(bs);
597                                  */                                          DPRINTF(DPRINTF_HEADER, "interlace alternate_vertical_scan %i", dec->alternate_vertical_scan);
598    
599                                    }
600                          }                          }
601    
602                          *quant = BitstreamGetBits(bs, dec->quant_bits);         // vop_quant                          if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1)       // vop_quant
603                          //DEBUG1("quant", *quant);                                  *quant = 1;
604    
605                          if (coding_type != I_VOP)                          DPRINTF(DPRINTF_HEADER, "quant %i", *quant);
606                          {  
607                                  *fcode = BitstreamGetBits(bs, 3);                       // fcode_forward                          if (coding_type != I_VOP) {
608                                    *fcode_forward = BitstreamGetBits(bs, 3);       // fcode_forward
609                                    DPRINTF(DPRINTF_HEADER, "fcode_forward %i", *fcode_forward);
610                          }                          }
611    
612                          if (coding_type == B_VOP)                          if (coding_type == B_VOP) {
613                          {                                  *fcode_backward = BitstreamGetBits(bs, 3);      // fcode_backward
614                                  // *fcode_backward = BitstreamGetBits(bs, 3);           // fcode_backward                                  DPRINTF(DPRINTF_HEADER, "fcode_backward %i", *fcode_backward);
615                          }                          }
616                          return coding_type;                          if (!dec->scalability) {
617                                    if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) &&
618                                            (coding_type != I_VOP)) {
619                                            BitstreamSkip(bs, 1);   // vop_shape_coding_type
620                  }                  }
                 else if (start_code == USERDATA_START_CODE)  
                 {  
                         // DEBUG("user_data");  
                         BitstreamSkip(bs, 32);          // user_data_start_code  
621                  }                  }
622                  else  // start_code == ?                          return coding_type;
623                  {  
624                          if (BitstreamShowBits(bs, 24) == 0x000001)                  } else if (start_code == USERDATA_START_CODE) {
625    
626                            DPRINTF(DPRINTF_STARTCODE, "<user_data>");
627    
628                            BitstreamSkip(bs, 32);  // user_data_start_code
629    
630                    } else                                  // start_code == ?
631                          {                          {
632                                  DEBUG1("*** WARNING: unknown start_code", BitstreamShowBits(bs, 32));                          if (BitstreamShowBits(bs, 24) == 0x000001) {
633                                    DPRINTF(DPRINTF_STARTCODE, "<unknown: %x>", BitstreamShowBits(bs, 32));
634                          }                          }
635                          BitstreamSkip(bs, 8);                          BitstreamSkip(bs, 8);
636                  }                  }
637          }          }
638          while ((BitstreamPos(bs) >> 3) < bs->length);          while ((BitstreamPos(bs) >> 3) < bs->length);
639    
640          DEBUG("*** WARNING: no vop_start_code found");          //DPRINTF("*** WARNING: no vop_start_code found");
641          return -1; /* ignore it */          return -1; /* ignore it */
642  }  }
643    
644    
645  /* write custom quant matrix */  /* write custom quant matrix */
646    
647  static void bs_put_matrix(Bitstream * bs, const int16_t *matrix)  static void
648    bs_put_matrix(Bitstream * bs,
649                              const int16_t * matrix)
650  {  {
651          int i, j;          int i, j;
652          const int last = matrix[scan_tables[0][63]];          const int last = matrix[scan_tables[0][63]];
653    
654          for (j = 63; j >= 0 && matrix[scan_tables[0][j - 1]] == last; j--) ;          for (j = 63; j > 0 && matrix[scan_tables[0][j - 1]] == last; j--);
655    
656          for (i = 0; i <= j; i++)          for (i = 0; i <= j; i++) {
         {  
657                  BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8);                  BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8);
658          }          }
659    
660          if (j < 63)          if (j < 63) {
         {  
661                  BitstreamPutBits(bs, 0, 8);                  BitstreamPutBits(bs, 0, 8);
662          }          }
663  }  }
# Line 579  Line 666 
666  /*  /*
667          write vol header          write vol header
668  */  */
669  void BitstreamWriteVolHeader(Bitstream * const bs,  void
670                                                  const int width,  BitstreamWriteVolHeader(Bitstream * const bs,
671                                                  const int height,                                                  const MBParam * pParam,
672                                                  const int quant_type)                                                  const FRAMEINFO * frame)
673  {  {
674          // video object_start_code & vo_id          // video object_start_code & vo_id
675      BitstreamPad(bs);      BitstreamPad(bs);
# Line 597  Line 684 
684          BitstreamPutBits(bs, 0, 8);                     // video_object_type_indication          BitstreamPutBits(bs, 0, 8);                     // video_object_type_indication
685          BitstreamPutBit(bs, 0);                         // is_object_layer_identified (0=not given)          BitstreamPutBit(bs, 0);                         // is_object_layer_identified (0=not given)
686          BitstreamPutBits(bs, 1, 4);                     // aspect_ratio_info (1=1:1)          BitstreamPutBits(bs, 1, 4);                     // aspect_ratio_info (1=1:1)
687          BitstreamPutBit(bs, 0);                         // vol_control_parameters (0=not given)  
688            BitstreamPutBit(bs, 1); // vol_control_parameters
689            BitstreamPutBits(bs, 1, 2);     // chroma_format 1="4:2:0"
690    
691    #ifdef BFRAMES
692            if (pParam->max_bframes > 0) {
693                    BitstreamPutBit(bs, 0); // low_delay
694            } else
695    #endif
696            {
697                    BitstreamPutBit(bs, 1); // low_delay
698            }
699            BitstreamPutBit(bs, 0); // vbv_parameters (0=not given)
700    
701          BitstreamPutBits(bs, 0, 2);                     // video_object_layer_shape (0=rectangular)          BitstreamPutBits(bs, 0, 2);                     // video_object_layer_shape (0=rectangular)
702    
703          WRITE_MARKER();          WRITE_MARKER();
# Line 607  Line 707 
707                          25fps           res=25          inc=1                          25fps           res=25          inc=1
708                          29.97fps        res=30000       inc=1001                          29.97fps        res=30000       inc=1001
709          */          */
710          BitstreamPutBits(bs, 2, 16);  #ifdef BFRAMES
711            BitstreamPutBits(bs, pParam->fbase, 16);
712    #else
713            BitstreamPutBits(bs, pParam->fbase, 16);
714    #endif
715    
716          WRITE_MARKER();          WRITE_MARKER();
717    
718          // fixed_vop_rate  #ifdef BFRAMES
719          BitstreamPutBit(bs, 0);          BitstreamPutBit(bs, 1);         // fixed_vop_rate = 1
720            BitstreamPutBits(bs, pParam->fincr, log2bin(pParam->fbase));    // fixed_vop_time_increment
721          // fixed_time_increment: value=nth_of_sec, nbits = log2(resolution)  #else
722          // BitstreamPutBits(bs, 0, 15);          BitstreamPutBit(bs, 1);         // fixed_vop_rate = 1
723            BitstreamPutBits(bs, pParam->fincr, log2bin(pParam->fbase));    // fixed_vop_time_increment
724    #endif
725    
726          WRITE_MARKER();          WRITE_MARKER();
727          BitstreamPutBits(bs, width, 13);                // width          BitstreamPutBits(bs, pParam->width, 13);        // width
728          WRITE_MARKER();          WRITE_MARKER();
729          BitstreamPutBits(bs, height, 13);               // height          BitstreamPutBits(bs, pParam->height, 13);       // height
730          WRITE_MARKER();          WRITE_MARKER();
731    
732          BitstreamPutBit(bs, 0);         // interlace          BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING);    // interlace
733          BitstreamPutBit(bs, 1);         // obmc_disable (overlapped block motion compensation)          BitstreamPutBit(bs, 1);         // obmc_disable (overlapped block motion compensation)
734          BitstreamPutBit(bs, 0);         // sprite_enable          BitstreamPutBit(bs, 0);         // sprite_enable
735          BitstreamPutBit(bs, 0);         // not_in_bit          BitstreamPutBit(bs, 0);         // not_in_bit
736    
737          // quant_type   0=h.263  1=mpeg4(quantizer tables)          // quant_type   0=h.263  1=mpeg4(quantizer tables)
738          BitstreamPutBit(bs, quant_type);          BitstreamPutBit(bs, pParam->m_quant_type);
739    
740          if (quant_type)          if (pParam->m_quant_type) {
         {  
741                  BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat                  BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat
742                  if (get_intra_matrix_status())                  if (get_intra_matrix_status()) {
                 {  
743                          bs_put_matrix(bs, get_intra_matrix());                          bs_put_matrix(bs, get_intra_matrix());
744                  }                  }
745    
746                  BitstreamPutBit(bs, get_inter_matrix_status());         // load_inter_quant_mat                  BitstreamPutBit(bs, get_inter_matrix_status());         // load_inter_quant_mat
747                  if (get_inter_matrix_status())                  if (get_inter_matrix_status()) {
                 {  
748                          bs_put_matrix(bs, get_inter_matrix());                          bs_put_matrix(bs, get_inter_matrix());
749                  }                  }
750    
# Line 662  Line 765 
765    time_inc = nth of a second since last resync    time_inc = nth of a second since last resync
766    (decoder uses these values to determine precise time since last resync)    (decoder uses these values to determine precise time since last resync)
767  */  */
768  void BitstreamWriteVopHeader(Bitstream * const bs,  void
769                            VOP_TYPE prediction_type,  BitstreamWriteVopHeader(Bitstream * const bs,
770                            const int rounding_type,                                                  const MBParam * pParam,
771                            const uint32_t quant,                                                  const FRAMEINFO * frame,
772                            const uint32_t fcode)                                                  int vop_coded)
773  {  {
774            uint32_t i;
775    
776      BitstreamPad(bs);      BitstreamPad(bs);
777      BitstreamPutBits(bs, VOP_START_CODE, 32);      BitstreamPutBits(bs, VOP_START_CODE, 32);
778    
779      BitstreamPutBits(bs, prediction_type, 2);          BitstreamPutBits(bs, frame->coding_type, 2);
780    
781          // time_base = 0  write n x PutBit(1), PutBit(0)          // time_base = 0  write n x PutBit(1), PutBit(0)
782          BitstreamPutBits(bs, 0, 1);  #ifdef BFRAMES
783            for (i = 0; i < frame->seconds; i++) {
784                    BitstreamPutBit(bs, 1);
785            }
786            BitstreamPutBit(bs, 0);
787    #else
788            for (i = 0; i < frame->seconds; i++) {
789                    BitstreamPutBit(bs, 1);
790            }
791            BitstreamPutBit(bs, 0);
792    //      BitstreamPutBits(bs, 0, 1);
793    #endif
794    
795          WRITE_MARKER();          WRITE_MARKER();
796    
797          // time_increment: value=nth_of_sec, nbits = log2(resolution)          // time_increment: value=nth_of_sec, nbits = log2(resolution)
798          BitstreamPutBits(bs, 1, 1);  #ifdef BFRAMES
799            BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase));
800            /*DPRINTF("[%i:%i] %c\n", frame->seconds, frame->ticks,
801                            frame->coding_type == I_VOP ? 'I' : frame->coding_type ==
802                            P_VOP ? 'P' : 'B');*/
803    #else
804            BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase));
805    //      BitstreamPutBits(bs, 1, 1);
806    #endif
807    
808          WRITE_MARKER();          WRITE_MARKER();
809    
810            if (!vop_coded) {
811                    BitstreamPutBits(bs, 0, 1);
812                    return;
813            }
814    
815          BitstreamPutBits(bs, 1, 1);                             // vop_coded          BitstreamPutBits(bs, 1, 1);                             // vop_coded
816    
817          if (prediction_type != I_VOP)          if (frame->coding_type == P_VOP)
818                  BitstreamPutBits(bs, rounding_type, 1);                  BitstreamPutBits(bs, frame->rounding_type, 1);
819    
820          BitstreamPutBits(bs, 0, 3);                             // intra_dc_vlc_threshold          BitstreamPutBits(bs, 0, 3);                             // intra_dc_vlc_threshold
821    
822          BitstreamPutBits(bs, quant, 5);                 // quantizer          if (frame->global_flags & XVID_INTERLACING) {
823                    BitstreamPutBit(bs, 1); // top field first
824                    BitstreamPutBit(bs, 0); // alternate vertical scan
825            }
826    
827            BitstreamPutBits(bs, frame->quant, 5);  // quantizer
828    
829            if (frame->coding_type != I_VOP)
830                    BitstreamPutBits(bs, frame->fcode, 3);  // forward_fixed_code
831    
832            if (frame->coding_type == B_VOP)
833                    BitstreamPutBits(bs, frame->bcode, 3);  // backward_fixed_code
834    
         if (prediction_type != I_VOP)  
                 BitstreamPutBits(bs, fcode, 3);         // fixed_code = [1,4]  
835  }  }

Legend:
Removed from v.4  
changed lines
  Added in v.478

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