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

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

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

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

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.21

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