[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 164, Mon May 6 10:07:19 2002 UTC revision 677, Tue Nov 26 23:44:11 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@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.c                                                               *   *  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    *  06.05.2002 low_delay                                                      *   *  which is not derived from or based on XviD.
45    *  06.05.2002 fixed fincr/fbase error                                        *   *
46    *  01.05.2002 added BVOP support to BitstreamWriteVopHeader                  *   *  Note that people who make modified versions of XviD are not obligated
47    *  15.04.2002 rewrite log2bin use asm386  By MinChen <chenm001@163.com>      *   *  to grant this special exception for their modified versions; it is
48    *  26.03.2002 interlacing support                                                                                        *   *  their choice whether to do so.  The GNU General Public License gives
49    *  03.03.2002 qmatrix writing                                                                                            *   *  permission to release a modified version without this exception; this
50    *  03.03.2002 merged BITREADER and BITWRITER                                                             *   *  exception also makes it possible to release a modified version which
51    *      30.02.2002     intra_dc_threshold support                                                                         *   *  carries forward this exception.
52    *      04.12.2001     support for additional headers                                                             *   *
53    *      16.12.2001     inital version                                                     *   * $Id: bitstream.c,v 1.36 2002-11-26 23:44:10 edgomez Exp $
54    *                                                                                                                                                        *   *
55    ******************************************************************************/   ****************************************************************************/
   
56    
57  #include "bitstream.h"  #include "bitstream.h"
58  #include "zigzag.h"  #include "zigzag.h"
59  #include "../quant/quant_matrix.h"  #include "../quant/quant_matrix.h"
60    
61    /*****************************************************************************
62     * Functions
63     ****************************************************************************/
64    
65  static uint32_t __inline log2bin(uint32_t value)  static uint32_t __inline
66    log2bin(uint32_t value)
67  {  {
68  /* Changed by Chenm001 */  /* Changed by Chenm001 */
69  #ifndef WIN32  #if !defined(_MSC_VER)
70          int n = 0;          int n = 0;
71          while (value)  
72          {          while (value) {
73                  value >>= 1;                  value >>= 1;
74                  n++;                  n++;
75          }          }
# Line 80  Line 83 
83  }  }
84    
85    
86  static const uint32_t intra_dc_threshold_table[] =  static const uint32_t intra_dc_threshold_table[] = {
 {  
87          32,     /* never use */          32,     /* never use */
88          13,          13,
89          15,          15,
# Line 93  Line 95 
95  };  };
96    
97    
98  void bs_get_matrix(Bitstream * bs, uint8_t * matrix)  void
99    bs_get_matrix(Bitstream * bs,
100                              uint8_t * matrix)
101  {  {
102          int i = 0;          int i = 0;
103      int last, value = 0;      int last, value = 0;
104    
105      do          do {
         {  
106                  last = value;                  last = value;
107          value = BitstreamGetBits(bs, 8);          value = BitstreamGetBits(bs, 8);
108          matrix[ scan_tables[0][i++] ]  = value;          matrix[ scan_tables[0][i++] ]  = value;
109      }      }
110      while (value != 0 && i < 64);      while (value != 0 && i < 64);
111            i--;    /* fix little bug at coeff not full */
112    
113          while (i < 64)          while (i < 64) {
         {  
114                  matrix[ scan_tables[0][i++] ]  = last;                  matrix[ scan_tables[0][i++] ]  = last;
115          }          }
116  }  }
117    
118    
119    
120    /* for PVOP addbits == fcode - 1 */
121    /* for BVOP addbits == max(fcode,bcode) - 1 */
122    /* returns mbpos */
123    int
124    read_video_packet_header(Bitstream *bs, const int addbits, int * quant)
125    {
126            int nbits;
127            int mbnum;
128            int hec;
129    
130            nbits = NUMBITS_VP_RESYNC_MARKER + addbits;
131    
132            BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs));
133            BitstreamSkip(bs, nbits);
134    
135            DPRINTF(DPRINTF_STARTCODE, "<video_packet_header>");
136    
137            /* if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { */
138                    /* hec */
139                    /* vop_width */
140                    /* marker_bit */
141                    /* vop_height */
142                    /* marker_bit */
143    
144            /*} */
145    
146            mbnum = BitstreamGetBits(bs, 9);
147            DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum);
148    
149            /* if (dec->shape != VIDOBJLAY_SHAPE_BINARYONLY) */
150            *quant = BitstreamGetBits(bs, 5);
151            DPRINTF(DPRINTF_HEADER, "quant %i", *quant);
152    
153            /* if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) */
154            hec = BitstreamGetBit(bs);
155            DPRINTF(DPRINTF_HEADER, "header_extension_code %i", hec);
156            /* if (hec) */
157            /*   .. decoder hec-header ... */
158    
159            return mbnum;
160    }
161    
162    
163    
164  /*  /*
165  decode headers  decode headers
166  returns coding_type, or -1 if error  returns coding_type, or -1 if error
167  */  */
168    
169  int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, uint32_t * quant, uint32_t * fcode_forward, uint32_t * fcode_backward, uint32_t * intra_dc_threshold)  #define VIDOBJ_START_CODE_MASK          0x0000001f
170    #define VIDOBJLAY_START_CODE_MASK       0x0000000f
171    
172    int
173    BitstreamReadHeaders(Bitstream * bs,
174                                             DECODER * dec,
175                                             uint32_t * rounding,
176                                             uint32_t * quant,
177                                             uint32_t * fcode_forward,
178                                             uint32_t * fcode_backward,
179                                             uint32_t * intra_dc_threshold)
180  {  {
181          uint32_t vol_ver_id;          uint32_t vol_ver_id;
182          static uint32_t time_increment_resolution;          static uint32_t time_increment_resolution;
183          uint32_t coding_type;          uint32_t coding_type;
184          uint32_t start_code;          uint32_t start_code;
185          uint32_t time_incr=0;          uint32_t time_incr=0;
186          int32_t  time_increment;          int32_t time_increment = 0;
187    
188          do          do {
         {  
189                  BitstreamByteAlign(bs);                  BitstreamByteAlign(bs);
190                  start_code = BitstreamShowBits(bs, 32);                  start_code = BitstreamShowBits(bs, 32);
191    
192                  if (start_code == VISOBJSEQ_START_CODE)                  if (start_code == VISOBJSEQ_START_CODE) {
193                  {  
194                          // DEBUG("visual_object_sequence");                          int profile;
195                          BitstreamSkip(bs, 32);                          // visual_object_sequence_start_code  
196                          BitstreamSkip(bs, 8);                                   // profile_and_level_indication                          DPRINTF(DPRINTF_STARTCODE, "<visual_object_sequence>");
197                  }  
198                  else if (start_code == VISOBJSEQ_STOP_CODE)                          BitstreamSkip(bs, 32);  /* visual_object_sequence_start_code */
199                  {                          profile = BitstreamGetBits(bs, 8);      /* profile_and_level_indication */
200                          BitstreamSkip(bs, 32);                          // visual_object_sequence_stop_code  
201                  }                          DPRINTF(DPRINTF_HEADER, "profile_and_level_indication %i", profile);
202                  else if (start_code == VISOBJ_START_CODE)  
203                  {                  } else if (start_code == VISOBJSEQ_STOP_CODE) {
204                          // DEBUG("visual_object");  
205                          BitstreamSkip(bs,32);                                   // visual_object_start_code                          BitstreamSkip(bs, 32);  /* visual_object_sequence_stop_code */
206                          if (BitstreamGetBit(bs))                                // is_visual_object_identified  
207                          {                          DPRINTF(DPRINTF_STARTCODE, "</visual_object_sequence>");
208                                  vol_ver_id = BitstreamGetBits(bs,4);    // visual_object_ver_id  
209                                  BitstreamSkip(bs, 3);                           // visual_object_priority                  } else if (start_code == VISOBJ_START_CODE) {
210                          }  
211                          else                          DPRINTF(DPRINTF_STARTCODE, "<visual_object>");
212    
213                            BitstreamSkip(bs, 32);  /* visual_object_start_code */
214                            if (BitstreamGetBit(bs))        /* is_visual_object_identified */
215                          {                          {
216                                    vol_ver_id = BitstreamGetBits(bs, 4);   /* visual_object_ver_id */
217                                    DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id);
218                                    BitstreamSkip(bs, 3);   /* visual_object_priority */
219                            } else {
220                                  vol_ver_id = 1;                                  vol_ver_id = 1;
221                          }                          }
222    
223                          if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO)      // visual_object_type                          if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO)      /* visual_object_type */
224                          {                          {
225                                  DEBUG("visual_object_type != video");                                  DPRINTF(DPRINTF_ERROR, "visual_object_type != video");
226                                  return -1;                                  return -1;
227                          }                          }
228                          BitstreamSkip(bs, 4);                          BitstreamSkip(bs, 4);
229    
230                          // video_signal_type                          /* video_signal_type */
231    
232                          if (BitstreamGetBit(bs))                                // video_signal_type                          if (BitstreamGetBit(bs))        /* video_signal_type */
233                          {                          {
234                                  DEBUG("+ video_signal_type");                                  DPRINTF(DPRINTF_HEADER,"+ video_signal_type");
235                                  BitstreamSkip(bs, 3);                           // video_format                                  BitstreamSkip(bs, 3);   /* video_format */
236                                  BitstreamSkip(bs, 1);                           // video_range                                  BitstreamSkip(bs, 1);   /* video_range */
237                                  if (BitstreamGetBit(bs))                        // color_description                                  if (BitstreamGetBit(bs))        /* color_description */
238                                  {                                  {
239                                          DEBUG("+ color_description");                                          DPRINTF(DPRINTF_HEADER,"+ color_description");
240                                          BitstreamSkip(bs, 8);                   // color_primaries                                          BitstreamSkip(bs, 8);   /* color_primaries */
241                                          BitstreamSkip(bs, 8);                   // transfer_characteristics                                          BitstreamSkip(bs, 8);   /* transfer_characteristics */
242                                          BitstreamSkip(bs, 8);                   // matrix_coefficients                                          BitstreamSkip(bs, 8);   /* matrix_coefficients */
                                 }  
243                          }                          }
244                  }                  }
245                  else if ((start_code & ~0x1f) == VIDOBJ_START_CODE)                  } else if ((start_code & ~VIDOBJ_START_CODE_MASK) == VIDOBJ_START_CODE) {
246                  {  
247                          BitstreamSkip(bs, 32);          // video_object_start_code                          DPRINTF(DPRINTF_STARTCODE, "<video_object>");
248                  }                          DPRINTF(DPRINTF_HEADER, "vo id %i", start_code & VIDOBJ_START_CODE_MASK);
249                  else if ((start_code & ~0xf) == VIDOBJLAY_START_CODE)  
250                  {                          BitstreamSkip(bs, 32);  /* video_object_start_code */
                         // DEBUG("video_object_layer");  
                         BitstreamSkip(bs, 32);                                  // video_object_layer_start_code  
251    
252                          BitstreamSkip(bs, 1);                                   // random_accessible_vol                  } else if ((start_code & ~VIDOBJLAY_START_CODE_MASK) == VIDOBJLAY_START_CODE) {
253    
254                          // video_object_type_indication                          DPRINTF(DPRINTF_STARTCODE, "<video_object_layer>");
255                          if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE &&                          DPRINTF(DPRINTF_HEADER, "vol id %i", start_code & VIDOBJLAY_START_CODE_MASK);
256                                  BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE &&  
257                                  BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN &&                          BitstreamSkip(bs, 32);  /* video_object_layer_start_code */
258                                  BitstreamShowBits(bs, 8) != 0)          // BUGGY DIVX  
259                            BitstreamSkip(bs, 1);   /* random_accessible_vol */
260    
261                            /* video_object_type_indication */
262                            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 */
263                          {                          {
264                                  DEBUG1("video_object_type_indication not supported", BitstreamShowBits(bs, 8));                                  DPRINTF(DPRINTF_ERROR,"video_object_type_indication %i not supported ",
265                                            BitstreamShowBits(bs, 8));
266                                  return -1;                                  return -1;
267                          }                          }
268                          BitstreamSkip(bs, 8);                          BitstreamSkip(bs, 8);
269    
270    
271                          if (BitstreamGetBit(bs))                                        // is_object_layer_identifier                          if (BitstreamGetBit(bs))        /* is_object_layer_identifier */
                         {  
                                 DEBUG("+ is_object_layer_identifier");  
                                 vol_ver_id = BitstreamGetBits(bs,4);            // video_object_layer_verid  
                                 BitstreamSkip(bs, 3);                                   // video_object_layer_priority  
                         }  
                         else  
272                          {                          {
273                                    DPRINTF(DPRINTF_HEADER, "+ is_object_layer_identifier");
274                                    vol_ver_id = BitstreamGetBits(bs, 4);   /* video_object_layer_verid */
275                                    DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id);
276                                    BitstreamSkip(bs, 3);   /* video_object_layer_priority */
277                            } else {
278                                  vol_ver_id = 1;                                  vol_ver_id = 1;
279                          }                          }
                         //DEBUGI("vol_ver_id", vol_ver_id);  
280    
281                          if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR)     // aspect_ratio_info                          if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR)     /* aspect_ratio_info */
282                          {                          {
283                                  DEBUG("+ aspect_ratio_info");                                  DPRINTF(DPRINTF_HEADER, "+ aspect_ratio_info");
284                                  BitstreamSkip(bs, 8);                                           // par_width                                  BitstreamSkip(bs, 8);   /* par_width */
285                                  BitstreamSkip(bs, 8);                                           // par_height                                  BitstreamSkip(bs, 8);   /* par_height */
286                          }                          }
287    
288                          if (BitstreamGetBit(bs))                                                // vol_control_parameters                          if (BitstreamGetBit(bs))        /* vol_control_parameters */
289                          {                          {
290                                  DEBUG("+ vol_control_parameters");                                  DPRINTF(DPRINTF_HEADER, "+ vol_control_parameters");
291                                  BitstreamSkip(bs, 2);                                           // chroma_format                                  BitstreamSkip(bs, 2);   /* chroma_format */
292                                  BitstreamSkip(bs, 1);                                           // low_delay                                  dec->low_delay = BitstreamGetBit(bs);   /* low_delay */
293                                  if (BitstreamGetBit(bs))                                        // vbv_parameters                                  DPRINTF(DPRINTF_HEADER, "low_delay %i", dec->low_delay);
294                                    if (BitstreamGetBit(bs))        /* vbv_parameters */
295                                  {                                  {
296                                          DEBUG("+ vbv_parameters");                                          DPRINTF(DPRINTF_HEADER,"+ vbv_parameters");
297                                          BitstreamSkip(bs, 15);                          // first_half_bitrate                                          BitstreamSkip(bs, 15);  /* first_half_bitrate */
298                                          READ_MARKER();                                          READ_MARKER();
299                                          BitstreamSkip(bs, 15);                          // latter_half_bitrate                                          BitstreamSkip(bs, 15);  /* latter_half_bitrate */
300                                          READ_MARKER();                                          READ_MARKER();
301                                          BitstreamSkip(bs, 15);                          // first_half_vbv_buffer_size                                          BitstreamSkip(bs, 15);  /* first_half_vbv_buffer_size */
302                                          READ_MARKER();                                          READ_MARKER();
303                                          BitstreamSkip(bs, 3);                                   // latter_half_vbv_buffer_size                                          BitstreamSkip(bs, 3);   /* latter_half_vbv_buffer_size */
304                                          BitstreamSkip(bs, 11);                          // first_half_vbv_occupancy                                          BitstreamSkip(bs, 11);  /* first_half_vbv_occupancy */
305                                          READ_MARKER();                                          READ_MARKER();
306                                          BitstreamSkip(bs, 15);                          // latter_half_vbv_occupancy                                          BitstreamSkip(bs, 15);  /* latter_half_vbv_occupancy */
307                                          READ_MARKER();                                          READ_MARKER();
308    
309                                  }                                  }
310                          }                          }
311    
312                          dec->shape = BitstreamGetBits(bs, 2);   // video_object_layer_shape                          dec->shape = BitstreamGetBits(bs, 2);   /* video_object_layer_shape */
313                          // DEBUG1("shape", dec->shape);                          DPRINTF(DPRINTF_HEADER, "shape %i", dec->shape);
314    
315                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1)                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) {
316                          {                                  BitstreamSkip(bs, 4);   /* video_object_layer_shape_extension */
                                 BitstreamSkip(bs, 4);           // video_object_layer_shape_extension  
317                          }                          }
318    
319                          READ_MARKER();                          READ_MARKER();
320    
321  // *************************** for decode B-frame time ***********************  /* *************************** for decode B-frame time *********************** */
322                          time_increment_resolution = BitstreamGetBits(bs, 16);   // vop_time_increment_resolution                          time_increment_resolution = BitstreamGetBits(bs, 16);   /* vop_time_increment_resolution */
323                          time_increment_resolution--;  
324                          //DEBUG1("time_increment_resolution=",time_increment_resolution);                          DPRINTF(DPRINTF_HEADER,"vop_time_increment_resolution %i", time_increment_resolution);
                         if (time_increment_resolution > 0)  
                         {  
                                 dec->time_inc_bits = log2bin(time_increment_resolution);  
                         }  
                         else  
                         {  
                                 // dec->time_inc_bits = 0;  
325    
326                                  // for "old" xvid compatibility, set time_inc_bits = 1  /*                      time_increment_resolution--; */
327    
328                            if (time_increment_resolution > 0) {
329                                    dec->time_inc_bits = log2bin(time_increment_resolution-1);
330                            } else {
331                                    /* dec->time_inc_bits = 0; */
332                                    /* for "old" xvid compatibility, set time_inc_bits = 1 */
333                                  dec->time_inc_bits = 1;                                  dec->time_inc_bits = 1;
334                          }                          }
335    
336                          READ_MARKER();                          READ_MARKER();
337    
338                          if (BitstreamGetBit(bs))                                                // fixed_vop_rate                          if (BitstreamGetBit(bs))        /* fixed_vop_rate */
339                          {                          {
340                                  BitstreamSkip(bs, dec->time_inc_bits);  // fixed_vop_time_increment                                  DPRINTF(DPRINTF_HEADER, "+ fixed_vop_rate");
341                                    BitstreamSkip(bs, dec->time_inc_bits);  /* fixed_vop_time_increment */
342                          }                          }
343    
344                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) {
                         {  
345    
346                                  if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR)                                  if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) {
                                 {  
347                                          uint32_t width, height;                                          uint32_t width, height;
348    
349                                          READ_MARKER();                                          READ_MARKER();
350                                          width = BitstreamGetBits(bs, 13);                       // video_object_layer_width                                          width = BitstreamGetBits(bs, 13);       /* video_object_layer_width */
                                         //DEBUGI("width", width);  
351                                          READ_MARKER();                                          READ_MARKER();
352                                          height = BitstreamGetBits(bs, 13);              // video_object_layer_height                                          height = BitstreamGetBits(bs, 13);      /* video_object_layer_height */
                                         //DEBUGI("height", height);  
353                                          READ_MARKER();                                          READ_MARKER();
354    
355                                          if (width != dec->width || height != dec->height)                                          DPRINTF(DPRINTF_HEADER, "width %i", width);
356                                          {                                          DPRINTF(DPRINTF_HEADER, "height %i", height);
357                                                  DEBUG("FATAL: video dimension discrepancy ***");  
358                                                  DEBUG2("bitstream width/height", width, height);                                          /* for auto set width & height */
359                                                  DEBUG2("param width/height", dec->width, dec->height);                                          if (dec->width == 0)
360                                                    dec->width = width;
361                                            if (dec->height == 0)
362                                                    dec->height = height;
363    
364                                            if (width != dec->width || height != dec->height) {
365                                                    DPRINTF(DPRINTF_ERROR, "XVID_DEC_PARAM width/height does not match bitstream");
366                                                  return -1;                                                  return -1;
367                                          }                                          }
368    
369                                  }                                  }
370    
371                                  if ((dec->interlacing = BitstreamGetBit(bs)))                                  dec->interlacing = BitstreamGetBit(bs);
372                                  {                                  DPRINTF(DPRINTF_HEADER, "interlace", dec->interlacing);
                                         DEBUG("vol: interlacing");  
                                 }  
373    
374                                  if (!BitstreamGetBit(bs))                               // obmc_disable                                  if (!BitstreamGetBit(bs))       /* obmc_disable */
375                                  {                                  {
376                                          DEBUG("IGNORED/TODO: !obmc_disable");                                          DPRINTF(DPRINTF_ERROR, "obmc_disabled==false not supported");
377                                          // TODO                                          /* TODO */
378                                          // fucking divx4.02 has this enabled                                          /* fucking divx4.02 has this enabled */
379                                  }                                  }
380    
381                                  if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2)))  // sprite_enable                                  if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2)))    /* sprite_enable */
382                                  {                                  {
383                                          DEBUG("sprite_enable; not supported");                                          DPRINTF(DPRINTF_ERROR, "spriate_enabled not supported");
384                                          return -1;                                          return -1;
385                                  }                                  }
386    
387                                  if (vol_ver_id != 1 && dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)                                  if (vol_ver_id != 1 &&
388                                  {                                          dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
389                                          BitstreamSkip(bs, 1);                                   // sadct_disable                                          BitstreamSkip(bs, 1);   /* sadct_disable */
390                                  }                                  }
391    
392                                  if (BitstreamGetBit(bs))                                                // not_8_bit                                  if (BitstreamGetBit(bs))        /* not_8_bit */
                                 {  
                                         DEBUG("+ not_8_bit [IGNORED/TODO]");  
                                         dec->quant_bits = BitstreamGetBits(bs, 4);      // quant_precision  
                                         BitstreamSkip(bs, 4);                                           // bits_per_pixel  
                                 }  
                                 else  
393                                  {                                  {
394                                            DPRINTF(DPRINTF_HEADER, "not_8_bit==true (ignored)");
395                                            dec->quant_bits = BitstreamGetBits(bs, 4);      /* quant_precision */
396                                            BitstreamSkip(bs, 4);   /* bits_per_pixel */
397                                    } else {
398                                          dec->quant_bits = 5;                                          dec->quant_bits = 5;
399                                  }                                  }
400    
401                                  if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE)                                  if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {
402                                  {                                          BitstreamSkip(bs, 1);   /* no_gray_quant_update */
403                                          BitstreamSkip(bs, 1);                   // no_gray_quant_update                                          BitstreamSkip(bs, 1);   /* composition_method */
404                                          BitstreamSkip(bs, 1);                   // composition_method                                          BitstreamSkip(bs, 1);   /* linear_composition */
                                         BitstreamSkip(bs, 1);                   // linear_composition  
405                                  }                                  }
406    
407                                  dec->quant_type = BitstreamGetBit(bs);          // quant_type                                  dec->quant_type = BitstreamGetBit(bs);  /* quant_type */
408                                  // DEBUG1("**** quant_type", dec->quant_type);                                  DPRINTF(DPRINTF_HEADER, "quant_type %i", dec->quant_type);
409    
410                                  if (dec->quant_type)                                  if (dec->quant_type) {
411                                  {                                          if (BitstreamGetBit(bs))        /* load_intra_quant_mat */
                                         if (BitstreamGetBit(bs))                // load_intra_quant_mat  
412                                          {                                          {
413                                                  uint8_t matrix[64];                                                  uint8_t matrix[64];
414    
415                                                    DPRINTF(DPRINTF_HEADER, "load_intra_quant_mat");
416    
417                                                  bs_get_matrix(bs, matrix);                                                  bs_get_matrix(bs, matrix);
418                                                  set_intra_matrix(matrix);                                                  set_intra_matrix(matrix);
419                                          }                                          } else
                                         else  
420                                                  set_intra_matrix(get_default_intra_matrix());                                                  set_intra_matrix(get_default_intra_matrix());
421    
422                                          if (BitstreamGetBit(bs))                // load_inter_quant_mat                                          if (BitstreamGetBit(bs))        /* load_inter_quant_mat */
423                                          {                                          {
424                                                  uint8_t matrix[64];                                                  uint8_t matrix[64];
425    
426                                                    DPRINTF(DPRINTF_HEADER, "load_inter_quant_mat");
427    
428                                                  bs_get_matrix(bs, matrix);                                                  bs_get_matrix(bs, matrix);
429                                                  set_inter_matrix(matrix);                                                  set_inter_matrix(matrix);
430                                          }                                          } else
                                         else  
431                                                  set_inter_matrix(get_default_inter_matrix());                                                  set_inter_matrix(get_default_inter_matrix());
432    
433                                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE)                                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {
434                                          {                                                  DPRINTF(DPRINTF_ERROR, "greyscale matrix not supported");
                                                 // TODO  
                                                 DEBUG("TODO: grayscale matrix stuff");  
435                                                  return -1;                                                  return -1;
436                                          }                                          }
437    
438                                  }                                  }
439    
440    
441                                  if (vol_ver_id != 1)                                  if (vol_ver_id != 1) {
442                                  {                                          DPRINTF(DPRINTF_DEBUG, "QUARTERPEL BITSTREAM");
443                                          dec->quarterpel = BitstreamGetBit(bs);  // quarter_sampe                                          dec->quarterpel = BitstreamGetBit(bs);  /* quarter_sample */
                                         if (dec->quarterpel)  
                                         {  
                                                 DEBUG("IGNORED/TODO: quarter_sample");  
                                         }  
444                                  }                                  }
445                                  else                                  else
                                 {  
446                                          dec->quarterpel = 0;                                          dec->quarterpel = 0;
                                 }  
447    
448                                  if (!BitstreamGetBit(bs))                       // complexity_estimation_disable  
449                                    if (!BitstreamGetBit(bs))       /* complexity_estimation_disable */
450                                  {                                  {
451                                          DEBUG("TODO: complexity_estimation header");                                          DPRINTF(DPRINTF_ERROR, "complexity_estimation not supported");
                                         // TODO  
452                                          return -1;                                          return -1;
453                                  }                                  }
454    
455                                  if (!BitstreamGetBit(bs))                       // resync_marker_disable                                  BitstreamSkip(bs, 1);   /* resync_marker_disable */
                                 {  
                                         DEBUG("IGNORED/TODO: !resync_marker_disable");  
                                         // TODO  
                                 }  
456    
457                                  if (BitstreamGetBit(bs))                // data_partitioned                                  if (BitstreamGetBit(bs))        /* data_partitioned */
458                                  {                                  {
459                                          DEBUG("+ data_partitioned");                                          DPRINTF(DPRINTF_ERROR, "data_partitioned not supported");
460                                          BitstreamSkip(bs, 1);           // reversible_vlc                                          BitstreamSkip(bs, 1);   /* reversible_vlc */
461                                  }                                  }
462    
463                                  if (vol_ver_id != 1)                                  if (vol_ver_id != 1) {
464                                  {                                          if (BitstreamGetBit(bs))        /* newpred_enable */
                                         if (BitstreamGetBit(bs))                        // newpred_enable  
465                                          {                                          {
466                                                  DEBUG("+ newpred_enable");                                                  DPRINTF(DPRINTF_HEADER, "+ newpred_enable");
467                                                  BitstreamSkip(bs, 2);                   // requested_upstream_message_type                                                  BitstreamSkip(bs, 2);   /* requested_upstream_message_type */
468                                                  BitstreamSkip(bs, 1);                   // newpred_segment_type                                                  BitstreamSkip(bs, 1);   /* newpred_segment_type */
469                                          }                                          }
470                                          if (BitstreamGetBit(bs))                        // reduced_resolution_vop_enable                                          if (BitstreamGetBit(bs))        /* reduced_resolution_vop_enable */
471                                          {                                          {
472                                                  DEBUG("TODO: reduced_resolution_vop_enable");                                                  DPRINTF(DPRINTF_ERROR, "reduced_resolution_vop not supported");
                                                 // TODO  
473                                                  return -1;                                                  return -1;
474                                          }                                          }
475                                  }                                  }
476    
477                                  if ((dec->scalability=BitstreamGetBit(bs)))     // scalability                                  if ((dec->scalability = BitstreamGetBit(bs)))   /* scalability */
478                                  {                                  {
479                                          // TODO                                          DPRINTF(DPRINTF_ERROR, "scalability not supported");
                                         DEBUG("TODO: scalability");  
480                                          return -1;                                          return -1;
481                                  }                                  }
482                          }                          } else                          /* dec->shape == BINARY_ONLY */
                         else    // dec->shape == BINARY_ONLY  
483                          {                          {
484                                  if (vol_ver_id != 1)                                  if (vol_ver_id != 1) {
485                                            if (BitstreamGetBit(bs))        /* scalability */
486                                  {                                  {
487                                          if (BitstreamGetBit(bs))        // scalability                                          DPRINTF(DPRINTF_ERROR, "scalability not supported");
                                         {  
                                                 // TODO  
                                                 DEBUG("TODO: scalability");  
488                                                  return -1;                                                  return -1;
489                                          }                                          }
490                                  }                                  }
491                                  BitstreamSkip(bs, 1);                   // resync_marker_disable                                  BitstreamSkip(bs, 1);   /* resync_marker_disable */
492    
493                          }                          }
494    
495                  }                  } else if (start_code == GRPOFVOP_START_CODE) {
496                  else if (start_code == GRPOFVOP_START_CODE)  
497                  {                          DPRINTF(DPRINTF_STARTCODE, "<group_of_vop>");
498                          // DEBUG("group_of_vop");  
499                          BitstreamSkip(bs, 32);                          BitstreamSkip(bs, 32);
500                          {                          {
501                                  int hours, minutes, seconds;                                  int hours, minutes, seconds;
502    
503                                  hours = BitstreamGetBits(bs, 5);                                  hours = BitstreamGetBits(bs, 5);
504                                  minutes = BitstreamGetBits(bs, 6);                                  minutes = BitstreamGetBits(bs, 6);
505                                  READ_MARKER();                                  READ_MARKER();
506                                  seconds = BitstreamGetBits(bs, 6);                                  seconds = BitstreamGetBits(bs, 6);
507                                  // DEBUG3("hms", hours, minutes, seconds);  
508                          }                                  DPRINTF(DPRINTF_HEADER, "time %ih%im%is", hours);
                         BitstreamSkip(bs, 1);                   // closed_gov  
                         BitstreamSkip(bs, 1);                   // broken_link  
509                  }                  }
510                  else if (start_code == VOP_START_CODE)                          BitstreamSkip(bs, 1);   /* closed_gov */
511                  {                          BitstreamSkip(bs, 1);   /* broken_link */
512                          // DEBUG("vop_start_code");  
513                          BitstreamSkip(bs, 32);                                          // vop_start_code                  } else if (start_code == VOP_START_CODE) {
514    
515                            DPRINTF(DPRINTF_STARTCODE, "<vop>");
516    
517                            BitstreamSkip(bs, 32);  /* vop_start_code */
518    
519                          coding_type = BitstreamGetBits(bs, 2);          // vop_coding_type                          coding_type = BitstreamGetBits(bs, 2);  /* vop_coding_type */
520                          //DEBUG1("coding_type", coding_type);                          DPRINTF(DPRINTF_HEADER, "coding_type %i", coding_type);
521    
522  // *************************** for decode B-frame time ***********************  /* *************************** for decode B-frame time *********************** */
523                          while (BitstreamGetBit(bs) != 0)                        // time_base                          while (BitstreamGetBit(bs) != 0)        /* time_base */
524                                  time_incr++;                                  time_incr++;
525    
526                          READ_MARKER();                          READ_MARKER();
527    
528                          //DEBUG1("time_inc_bits", dec->time_inc_bits);                          if (dec->time_inc_bits) {
529                          //DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits));                                  time_increment = (BitstreamGetBits(bs, dec->time_inc_bits));    /* vop_time_increment */
                         if (dec->time_inc_bits)  
                         {  
                                 //BitstreamSkip(bs, dec->time_inc_bits);        // vop_time_increment  
                                 time_increment = (BitstreamGetBits(bs, dec->time_inc_bits));    // vop_time_increment  
530                          }                          }
531    
532                            DPRINTF(DPRINTF_HEADER, "time_base %i", time_incr);
533                            DPRINTF(DPRINTF_HEADER, "time_increment %i", time_increment);
534    
535                            DPRINTF(DPRINTF_TIMECODE, "%c %i:%i",
536                                    coding_type == I_VOP ? 'I' : coding_type == P_VOP ? 'P' : 'B',
537                                    time_incr, time_increment);
538    
539                          if(coding_type != B_VOP){                          if(coding_type != B_VOP){
540                              dec->last_time_base = dec->time_base;                              dec->last_time_base = dec->time_base;
541                                  dec->time_base += time_incr;                                  dec->time_base += time_incr;
542                                  dec->time = dec->time_base*time_increment_resolution + time_increment;                                  dec->time = time_increment;
543                                  dec->time_pp= (uint32_t)(dec->time - dec->last_non_b_time);  
544    /*                                      dec->time_base * time_increment_resolution +
545                                            time_increment;
546    */                              dec->time_pp = (uint32_t)
547                                            (time_increment_resolution + dec->time - dec->last_non_b_time)%time_increment_resolution;
548                                  dec->last_non_b_time= dec->time;                                  dec->last_non_b_time= dec->time;
549                          }else{                          }else{
550                                  dec->time = (dec->last_time_base + time_incr)*time_increment_resolution + time_increment;                                  dec->time = time_increment;
551                                  dec->time_bp= (uint32_t)(dec->last_non_b_time - dec->time);  /*
552                                            (dec->last_time_base +
553                                             time_incr) * time_increment_resolution + time_increment;
554    */
555                                    dec->time_bp = (uint32_t)
556                                            (time_increment_resolution + dec->last_non_b_time - dec->time)%time_increment_resolution;
557                          }                          }
                         //DEBUG1("time_increment=",time_increment);  
558    
559                          READ_MARKER();                          READ_MARKER();
560    
561                          if (!BitstreamGetBit(bs))                                       // vop_coded                          if (!BitstreamGetBit(bs))       /* vop_coded */
562                          {                          {
563                                    DPRINTF(DPRINTF_HEADER, "vop_coded==false");
564                                  return N_VOP;                                  return N_VOP;
565                          }                          }
566    
# Line 506  Line 569 
569                          }                          }
570                          */                          */
571    
572                          // fix a little bug by MinChen <chenm002@163.com>                          /* fix a little bug by MinChen <chenm002@163.com> */
573                          if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && (coding_type == P_VOP))                          if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) &&
574                          {                                  (coding_type == P_VOP)) {
575                                  *rounding = BitstreamGetBit(bs);        // rounding_type                                  *rounding = BitstreamGetBit(bs);        /* rounding_type */
576                                  //DEBUG1("rounding", *rounding);                                  DPRINTF(DPRINTF_HEADER, "rounding %i", *rounding);
577                          }                          }
578    
579                          /* if (reduced_resolution_enable)                          /* if (reduced_resolution_enable)
# Line 518  Line 581 
581                          }                          }
582                          */                          */
583    
584                          if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)                          if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
                         {  
585                                  uint32_t width, height;                                  uint32_t width, height;
586                                  uint32_t horiz_mc_ref, vert_mc_ref;                                  uint32_t horiz_mc_ref, vert_mc_ref;
587    
# Line 532  Line 594 
594                                  vert_mc_ref = BitstreamGetBits(bs, 13);                                  vert_mc_ref = BitstreamGetBits(bs, 13);
595                                  READ_MARKER();                                  READ_MARKER();
596    
597                                  // DEBUG2("vop_width/height", width, height);                                  DPRINTF(DPRINTF_HEADER, "width %i", width);
598                                  // DEBUG2("ref             ", horiz_mc_ref, vert_mc_ref);                                  DPRINTF(DPRINTF_HEADER, "height %i", height);
599                                    DPRINTF(DPRINTF_HEADER, "horiz_mc_ref %i", horiz_mc_ref);
600                                    DPRINTF(DPRINTF_HEADER, "vert_mc_ref %i", vert_mc_ref);
601    
602                                  BitstreamSkip(bs, 1);                           // change_conv_ratio_disable                                  BitstreamSkip(bs, 1);   /* change_conv_ratio_disable */
603                                  if (BitstreamGetBit(bs))                        // vop_constant_alpha                                  if (BitstreamGetBit(bs))        /* vop_constant_alpha */
604                                  {                                  {
605                                          BitstreamSkip(bs, 8);                   // vop_constant_alpha_value                                          BitstreamSkip(bs, 8);   /* vop_constant_alpha_value */
606                                  }                                  }
607                          }                          }
608    
609    
610                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) {
611                          {                                  /* intra_dc_vlc_threshold */
612                                  // intra_dc_vlc_threshold                                  *intra_dc_threshold =
613                                  *intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ];                                          intra_dc_threshold_table[BitstreamGetBits(bs, 3)];
614    
615                                    if (dec->interlacing) {
616                                            dec->top_field_first = BitstreamGetBit(bs);
617                                            DPRINTF(DPRINTF_HEADER, "interlace top_field_first %i", dec->top_field_first);
618                                            dec->alternate_vertical_scan = BitstreamGetBit(bs);
619                                            DPRINTF(DPRINTF_HEADER, "interlace alternate_vertical_scan %i", dec->alternate_vertical_scan);
620    
                                 if (dec->interlacing)  
                                 {  
                                         if ((dec->top_field_first = BitstreamGetBit(bs)))  
                                         {  
                                                 DEBUG("vop: top_field_first");  
                                         }  
                                         if ((dec->alternate_vertical_scan = BitstreamGetBit(bs)))  
                                         {  
                                                 DEBUG("vop: alternate_vertical_scan");  
                                         }  
621                                  }                                  }
622                          }                          }
623    
624                          if((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1)                // vop_quant                          if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1)       /* vop_quant */
625                                  *quant = 1;                                  *quant = 1;
626    
627                          //DEBUG1("quant", *quant);                          DPRINTF(DPRINTF_HEADER, "quant %i", *quant);
628    
629                          if (coding_type != I_VOP)                          if (coding_type != I_VOP) {
630                          {                                  *fcode_forward = BitstreamGetBits(bs, 3);       /* fcode_forward */
631                                  *fcode_forward = BitstreamGetBits(bs, 3);               // fcode_forward                                  DPRINTF(DPRINTF_HEADER, "fcode_forward %i", *fcode_forward);
632                          }                          }
633    
634                          if (coding_type == B_VOP)                          if (coding_type == B_VOP) {
635                          {                                  *fcode_backward = BitstreamGetBits(bs, 3);      /* fcode_backward */
636                                  *fcode_backward = BitstreamGetBits(bs, 3);              // fcode_backward                                  DPRINTF(DPRINTF_HEADER, "fcode_backward %i", *fcode_backward);
637                          }                          }
638                          if (!dec->scalability){                          if (!dec->scalability){
639                                  if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) && (coding_type != I_VOP)){                                  if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) &&
640                                          BitstreamSkip(bs, 1);           // vop_shape_coding_type                                          (coding_type != I_VOP)) {
641                                            BitstreamSkip(bs, 1);   /* vop_shape_coding_type */
642                                  }                                  }
643                          }                          }
644                          return coding_type;                          return coding_type;
645                  }  
646                  else if (start_code == USERDATA_START_CODE)                  } else if (start_code == USERDATA_START_CODE) {
647                  {  
648                          // DEBUG("user_data");                          DPRINTF(DPRINTF_STARTCODE, "<user_data>");
649                          BitstreamSkip(bs, 32);          // user_data_start_code  
650                  }                          BitstreamSkip(bs, 32);  /* user_data_start_code */
651                  else  // start_code == ?  
652                  {                  } else                                  /* start_code == ? */
                         if (BitstreamShowBits(bs, 24) == 0x000001)  
653                          {                          {
654                                  DEBUG1("*** WARNING: unknown start_code", BitstreamShowBits(bs, 32));                          if (BitstreamShowBits(bs, 24) == 0x000001) {
655                                    DPRINTF(DPRINTF_STARTCODE, "<unknown: %x>", BitstreamShowBits(bs, 32));
656                          }                          }
657                          BitstreamSkip(bs, 8);                          BitstreamSkip(bs, 8);
658                  }                  }
659          }          }
660          while ((BitstreamPos(bs) >> 3) < bs->length);          while ((BitstreamPos(bs) >> 3) < bs->length);
661    
662          DEBUG("*** WARNING: no vop_start_code found");          /*DPRINTF("*** WARNING: no vop_start_code found"); */
663          return -1; /* ignore it */          return -1; /* ignore it */
664  }  }
665    
666    
667  /* write custom quant matrix */  /* write custom quant matrix */
668    
669  static void bs_put_matrix(Bitstream * bs, const int16_t *matrix)  static void
670    bs_put_matrix(Bitstream * bs,
671                              const int16_t * matrix)
672  {  {
673          int i, j;          int i, j;
674          const int last = matrix[scan_tables[0][63]];          const int last = matrix[scan_tables[0][63]];
675    
676          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--);
677    
678          for (i = 0; i <= j; i++)          for (i = 0; i <= j; i++) {
         {  
679                  BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8);                  BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8);
680          }          }
681    
682          if (j < 63)          if (j < 63) {
         {  
683                  BitstreamPutBits(bs, 0, 8);                  BitstreamPutBits(bs, 0, 8);
684          }          }
685  }  }
# Line 627  Line 688 
688  /*  /*
689          write vol header          write vol header
690  */  */
691  void BitstreamWriteVolHeader(Bitstream * const bs,  void
692                                                  const MBParam * pParam,  const FRAMEINFO * frame)  BitstreamWriteVolHeader(Bitstream * const bs,
693                                                    const MBParam * pParam,
694                                                    const FRAMEINFO * frame)
695  {  {
696          // video object_start_code & vo_id          /* video object_start_code & vo_id */
697      BitstreamPad(bs);      BitstreamPad(bs);
698          BitstreamPutBits(bs, VO_START_CODE, 27);          BitstreamPutBits(bs, VO_START_CODE, 27);
699      BitstreamPutBits(bs, 0, 5);      BitstreamPutBits(bs, 0, 5);
700    
701          // video_object_layer_start_code & vol_id          /* video_object_layer_start_code & vol_id */
702          BitstreamPutBits(bs, VOL_START_CODE, 28);          BitstreamPutBits(bs, VOL_START_CODE, 28);
703          BitstreamPutBits(bs, 0, 4);          BitstreamPutBits(bs, 0, 4);
704    
705          BitstreamPutBit(bs, 0);                         // random_accessible_vol          BitstreamPutBit(bs, 0);         /* random_accessible_vol */
706          BitstreamPutBits(bs, 0, 8);                     // video_object_type_indication          BitstreamPutBits(bs, 0, 8);     /* video_object_type_indication */
707          BitstreamPutBit(bs, 0);                         // is_object_layer_identified (0=not given)          BitstreamPutBit(bs, 0);         /* is_object_layer_identified (0=not given) */
708          BitstreamPutBits(bs, 1, 4);                     // aspect_ratio_info (1=1:1)          BitstreamPutBits(bs, 1, 4);     /* aspect_ratio_info (1=1:1) */
   
 #ifdef BFRAMES  
         if (pParam->max_bframes > 0)  
         {  
                 dprintf("low_delay=1");  
                 BitstreamPutBit(bs, 1);                         // vol_control_parameters  
                 BitstreamPutBits(bs, 1, 2);                     // chroma_format 1="4:2:0"  
                 BitstreamPutBit(bs, 0);                         // low_delay  
                 BitstreamPutBit(bs, 0);                         // vbv_parameters (0=not given)  
         }  
         else  
 #endif  
         {  
                 BitstreamPutBits(bs, 0, 1);                     // vol_control_parameters (0=not given)  
         }  
709    
710            BitstreamPutBit(bs, 1); /* vol_control_parameters */
711            BitstreamPutBits(bs, 1, 2);     /* chroma_format 1="4:2:0" */
712    
713          BitstreamPutBits(bs, 0, 2);                     // video_object_layer_shape (0=rectangular)          BitstreamPutBit(bs, 1); /* low_delay */
714    
715            BitstreamPutBit(bs, 0); /* vbv_parameters (0=not given) */
716    
717            BitstreamPutBits(bs, 0, 2);     /* video_object_layer_shape (0=rectangular) */
718    
719          WRITE_MARKER();          WRITE_MARKER();
720    
721          /* time_increment_resolution; ignored by current decore versions          /*
722                  eg. 2fps                res=2           inc=1           * time_increment_resolution; ignored by current decore versions
723                          25fps           res=25          inc=1           *  eg. 2fps     res=2       inc=1
724                          29.97fps        res=30000       inc=1001           *      25fps    res=25      inc=1
725             *      29.97fps res=30000   inc=1001
726          */          */
 #ifdef BFRAMES  
727          BitstreamPutBits(bs, pParam->fbase, 16);          BitstreamPutBits(bs, pParam->fbase, 16);
 #else  
         BitstreamPutBits(bs, 2, 16);  
 #endif  
728    
         WRITE_MARKER();  
729    
730          // fixed_vop_rate          WRITE_MARKER();
         BitstreamPutBit(bs, 0);  
731    
732          // fixed_time_increment: value=nth_of_sec, nbits = log2(resolution)          BitstreamPutBit(bs, 1);         /* fixed_vop_rate = 1 */
733          // BitstreamPutBits(bs, 0, 15);          BitstreamPutBits(bs, pParam->fincr, log2bin(pParam->fbase));    /* fixed_vop_time_increment */
734    
735          WRITE_MARKER();          WRITE_MARKER();
736          BitstreamPutBits(bs, pParam->width, 13);                // width          BitstreamPutBits(bs, pParam->width, 13);        /* width */
737          WRITE_MARKER();          WRITE_MARKER();
738          BitstreamPutBits(bs, pParam->height, 13);               // height          BitstreamPutBits(bs, pParam->height, 13);       /* height */
739          WRITE_MARKER();          WRITE_MARKER();
740    
741          BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING);            // interlace          BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING);    /* interlace */
742          BitstreamPutBit(bs, 1);         // obmc_disable (overlapped block motion compensation)          BitstreamPutBit(bs, 1);         /* obmc_disable (overlapped block motion compensation) */
743          BitstreamPutBit(bs, 0);         // sprite_enable          BitstreamPutBit(bs, 0);         /* sprite_enable */
744          BitstreamPutBit(bs, 0);         // not_in_bit          BitstreamPutBit(bs, 0);         /* not_in_bit */
745    
746          // quant_type   0=h.263  1=mpeg4(quantizer tables)          /* quant_type   0=h.263  1=mpeg4(quantizer tables) */
747          BitstreamPutBit(bs, pParam->m_quant_type);          BitstreamPutBit(bs, pParam->m_quant_type);
748    
749          if (pParam->m_quant_type)          if (pParam->m_quant_type) {
750          {                  BitstreamPutBit(bs, get_intra_matrix_status()); /* load_intra_quant_mat */
751                  BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat                  if (get_intra_matrix_status()) {
                 if (get_intra_matrix_status())  
                 {  
752                          bs_put_matrix(bs, get_intra_matrix());                          bs_put_matrix(bs, get_intra_matrix());
753                  }                  }
754    
755                  BitstreamPutBit(bs, get_inter_matrix_status());         // load_inter_quant_mat                  BitstreamPutBit(bs, get_inter_matrix_status()); /* load_inter_quant_mat */
756                  if (get_inter_matrix_status())                  if (get_inter_matrix_status()) {
                 {  
757                          bs_put_matrix(bs, get_inter_matrix());                          bs_put_matrix(bs, get_inter_matrix());
758                  }                  }
759    
760          }          }
761    
762          BitstreamPutBit(bs, 1);         // complexity_estimation_disable          BitstreamPutBit(bs, 1);         /* complexity_estimation_disable */
763          BitstreamPutBit(bs, 1);         // resync_marker_disable          BitstreamPutBit(bs, 1);         /* resync_marker_disable */
764          BitstreamPutBit(bs, 0);         // data_partitioned          BitstreamPutBit(bs, 0);         /* data_partitioned */
765          BitstreamPutBit(bs, 0);         // scalability          BitstreamPutBit(bs, 0);         /* scalability */
766  }  }
767    
768    
# Line 728  Line 774 
774    time_inc = nth of a second since last resync    time_inc = nth of a second since last resync
775    (decoder uses these values to determine precise time since last resync)    (decoder uses these values to determine precise time since last resync)
776  */  */
777  void BitstreamWriteVopHeader(Bitstream * const bs,  void
778    BitstreamWriteVopHeader(Bitstream * const bs,
779                                                  const MBParam * pParam,                                                  const MBParam * pParam,
780                                                  const FRAMEINFO * frame)                                                  const FRAMEINFO * frame,
781                                                    int vop_coded)
782  {  {
 #ifdef BFRAMES  
783          uint32_t i;          uint32_t i;
784  #endif  
785      BitstreamPad(bs);      BitstreamPad(bs);
786      BitstreamPutBits(bs, VOP_START_CODE, 32);      BitstreamPutBits(bs, VOP_START_CODE, 32);
787    
788      BitstreamPutBits(bs, frame->coding_type, 2);      BitstreamPutBits(bs, frame->coding_type, 2);
789    
790          // time_base = 0  write n x PutBit(1), PutBit(0)          /* time_base = 0  write n x PutBit(1), PutBit(0) */
791  #ifdef BFRAMES          for (i = 0; i < frame->seconds; i++) {
         for (i = 0; i < frame->seconds; i++)  
         {  
792                  BitstreamPutBit(bs, 1);                  BitstreamPutBit(bs, 1);
793          }          }
794          BitstreamPutBit(bs, 0);          BitstreamPutBit(bs, 0);
 #else  
         BitstreamPutBits(bs, 0, 1);  
 #endif  
795    
796          WRITE_MARKER();          WRITE_MARKER();
797    
798          // time_increment: value=nth_of_sec, nbits = log2(resolution)          /* time_increment: value=nth_of_sec, nbits = log2(resolution) */
 #ifdef BFRAMES  
799          BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase));          BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase));
         dprintf("[%i:%i] %c\n", frame->seconds, frame->ticks, frame->coding_type == I_VOP ? 'I' : frame->coding_type == P_VOP ? 'P' : 'B');  
 #else  
         BitstreamPutBits(bs, 1, 1);  
 #endif  
800    
801          WRITE_MARKER();          WRITE_MARKER();
802    
803          BitstreamPutBits(bs, 1, 1);                             // vop_coded          if (!vop_coded) {
804                    BitstreamPutBits(bs, 0, 1);
805                    return;
806            }
807    
808            BitstreamPutBits(bs, 1, 1);     /* vop_coded */
809    
810          if (frame->coding_type == P_VOP)          if (frame->coding_type == P_VOP)
811                  BitstreamPutBits(bs, frame->rounding_type, 1);                  BitstreamPutBits(bs, frame->rounding_type, 1);
812    
813          BitstreamPutBits(bs, 0, 3);                             // intra_dc_vlc_threshold          BitstreamPutBits(bs, 0, 3);     /* intra_dc_vlc_threshold */
814    
815          if (frame->global_flags & XVID_INTERLACING)          if (frame->global_flags & XVID_INTERLACING) {
816          {                  BitstreamPutBit(bs, 1); /* top field first */
817                  BitstreamPutBit(bs, 1);         // top field first                  BitstreamPutBit(bs, 0); /* alternate vertical scan */
                 BitstreamPutBit(bs, 0);         // alternate vertical scan  
818          }          }
819    
820          BitstreamPutBits(bs, frame->quant, 5);                  // quantizer          BitstreamPutBits(bs, frame->quant, 5);  /* quantizer */
821    
822          if (frame->coding_type != I_VOP)          if (frame->coding_type != I_VOP)
823                  BitstreamPutBits(bs, frame->fcode, 3);          // forward_fixed_code                  BitstreamPutBits(bs, frame->fcode, 3);  /* forward_fixed_code */
824    
825          if (frame->coding_type == B_VOP)          if (frame->coding_type == B_VOP)
826                  BitstreamPutBits(bs, frame->bcode, 3);          // backward_fixed_code                  BitstreamPutBits(bs, frame->bcode, 3);  /* backward_fixed_code */
827    
828  }  }

Legend:
Removed from v.164  
changed lines
  Added in v.677

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