[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.5, Thu Mar 28 12:26:44 2002 UTC revision 1.14, Wed Jun 12 20:38:40 2002 UTC
# Line 41  Line 41 
41    *                                                                                                                                                        *    *                                                                                                                                                        *
42    *  Revision history:                                                         *    *  Revision history:                                                         *
43    *                                                                            *    *                                                                            *
44    *  26.03.2002 interlacing support    *  08.05.2002  add low_delay support for B_VOP decode                                            *
45      *              MinChen <chenm001@163.com>                                    *
46      *  06.05.2002 low_delay                                                      *
47      *  06.05.2002 fixed fincr/fbase error                                        *
48      *  01.05.2002 added BVOP support to BitstreamWriteVopHeader                  *
49      *  15.04.2002 rewrite log2bin use asm386  By MinChen <chenm001@163.com>      *
50      *  26.03.2002 interlacing support                                                                                        *
51    *  03.03.2002 qmatrix writing                                                                                            *    *  03.03.2002 qmatrix writing                                                                                            *
52    *  03.03.2002 merged BITREADER and BITWRITER                                                             *    *  03.03.2002 merged BITREADER and BITWRITER                                                             *
53    *      30.02.2002     intra_dc_threshold support                                                                         *    *      30.02.2002     intra_dc_threshold support                                                                         *
# Line 55  Line 61 
61  #include "zigzag.h"  #include "zigzag.h"
62  #include "../quant/quant_matrix.h"  #include "../quant/quant_matrix.h"
63    
64  static int __inline log2bin(int value)  
65    static uint32_t __inline
66    log2bin(uint32_t value)
67  {  {
68    /* Changed by Chenm001 */
69    #ifndef WIN32
70          int n = 0;          int n = 0;
71          while (value)  
72          {          while (value) {
73                  value >>= 1;                  value >>= 1;
74                  n++;                  n++;
75          }          }
76          return n;          return n;
77    #else
78            __asm {
79            bsr eax, value inc eax}
80    #endif
81  }  }
82    
83    
84  static const uint32_t intra_dc_threshold_table[] =  static const uint32_t intra_dc_threshold_table[] = {
 {  
85          32,     /* never use */          32,     /* never use */
86          13,          13,
87          15,          15,
# Line 80  Line 93 
93  };  };
94    
95    
96  void bs_get_matrix(Bitstream * bs, uint8_t * matrix)  void
97    bs_get_matrix(Bitstream * bs,
98                              uint8_t * matrix)
99  {  {
100          int i = 0;          int i = 0;
101      int last, value = 0;      int last, value = 0;
102    
103      do          do {
         {  
104                  last = value;                  last = value;
105          value = BitstreamGetBits(bs, 8);          value = BitstreamGetBits(bs, 8);
106          matrix[ scan_tables[0][i++] ]  = value;          matrix[ scan_tables[0][i++] ]  = value;
107      }      }
108      while (value != 0 && i < 64);      while (value != 0 && i < 64);
109    
110          while (i < 64)          while (i < 64) {
         {  
111                  matrix[ scan_tables[0][i++] ]  = last;                  matrix[ scan_tables[0][i++] ]  = last;
112          }          }
113  }  }
# Line 104  Line 117 
117  returns coding_type, or -1 if error  returns coding_type, or -1 if error
118  */  */
119    
120  int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, uint32_t * quant, uint32_t * fcode, uint32_t * intra_dc_threshold)  int
121    BitstreamReadHeaders(Bitstream * bs,
122                                             DECODER * dec,
123                                             uint32_t * rounding,
124                                             uint32_t * quant,
125                                             uint32_t * fcode_forward,
126                                             uint32_t * fcode_backward,
127                                             uint32_t * intra_dc_threshold)
128  {  {
129          uint32_t vol_ver_id;          uint32_t vol_ver_id;
130          uint32_t time_inc_resolution;          static uint32_t time_increment_resolution;
131          uint32_t coding_type;          uint32_t coding_type;
132          uint32_t start_code;          uint32_t start_code;
133            uint32_t time_incr = 0;
134            int32_t time_increment;
135    
136          do          do {
         {  
137                  BitstreamByteAlign(bs);                  BitstreamByteAlign(bs);
138                  start_code = BitstreamShowBits(bs, 32);                  start_code = BitstreamShowBits(bs, 32);
139    
140                  if (start_code == VISOBJSEQ_START_CODE)                  if (start_code == VISOBJSEQ_START_CODE) {
                 {  
141                          // DEBUG("visual_object_sequence");                          // DEBUG("visual_object_sequence");
142                          BitstreamSkip(bs, 32);                          // visual_object_sequence_start_code                          BitstreamSkip(bs, 32);                          // visual_object_sequence_start_code
143                          BitstreamSkip(bs, 8);                                   // profile_and_level_indication                          BitstreamSkip(bs, 8);                                   // profile_and_level_indication
144                  }                  } else if (start_code == VISOBJSEQ_STOP_CODE) {
                 else if (start_code == VISOBJSEQ_STOP_CODE)  
                 {  
145                          BitstreamSkip(bs, 32);                          // visual_object_sequence_stop_code                          BitstreamSkip(bs, 32);                          // visual_object_sequence_stop_code
146                  }                  } else if (start_code == VISOBJ_START_CODE) {
                 else if (start_code == VISOBJ_START_CODE)  
                 {  
147                          // DEBUG("visual_object");                          // DEBUG("visual_object");
148                          BitstreamSkip(bs,32);                                   // visual_object_start_code                          BitstreamSkip(bs,32);                                   // visual_object_start_code
149                          if (BitstreamGetBit(bs))                                // is_visual_object_identified                          if (BitstreamGetBit(bs))                                // is_visual_object_identified
150                          {                          {
151                                  vol_ver_id = BitstreamGetBits(bs,4);    // visual_object_ver_id                                  vol_ver_id = BitstreamGetBits(bs,4);    // visual_object_ver_id
152                                  BitstreamSkip(bs, 3);                           // visual_object_priority                                  BitstreamSkip(bs, 3);                           // visual_object_priority
153                          }                          } else {
                         else  
                         {  
154                                  vol_ver_id = 1;                                  vol_ver_id = 1;
155                          }                          }
156    
# Line 162  Line 176 
176                                          BitstreamSkip(bs, 8);                   // matrix_coefficients                                          BitstreamSkip(bs, 8);                   // matrix_coefficients
177                                  }                                  }
178                          }                          }
179                  }                  } else if ((start_code & ~0x1f) == VIDOBJ_START_CODE) {
                 else if ((start_code & ~0x1f) == VIDOBJ_START_CODE)  
                 {  
180                          BitstreamSkip(bs, 32);          // video_object_start_code                          BitstreamSkip(bs, 32);          // video_object_start_code
181                  }                  } else if ((start_code & ~0xf) == VIDOBJLAY_START_CODE) {
                 else if ((start_code & ~0xf) == VIDOBJLAY_START_CODE)  
                 {  
182                          // DEBUG("video_object_layer");                          // DEBUG("video_object_layer");
183                          BitstreamSkip(bs, 32);                                  // video_object_layer_start_code                          BitstreamSkip(bs, 32);                                  // video_object_layer_start_code
184    
185                          BitstreamSkip(bs, 1);                                                                   // random_accessible_vol                          BitstreamSkip(bs, 1);                                                                   // random_accessible_vol
186    
187                          // video_object_type_indication                          // video_object_type_indication
188                          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  
189                          {                          {
190                                  DEBUG1("video_object_type_indication not supported", BitstreamShowBits(bs, 8));                                  DEBUG1("video_object_type_indication not supported",
191                                               BitstreamShowBits(bs, 8));
192                                  return -1;                                  return -1;
193                          }                          }
194                          BitstreamSkip(bs, 8);                          BitstreamSkip(bs, 8);
# Line 191  Line 199 
199                                  DEBUG("+ is_object_layer_identifier");                                  DEBUG("+ is_object_layer_identifier");
200                                  vol_ver_id = BitstreamGetBits(bs,4);            // video_object_layer_verid                                  vol_ver_id = BitstreamGetBits(bs,4);            // video_object_layer_verid
201                                  BitstreamSkip(bs, 3);                                   // video_object_layer_priority                                  BitstreamSkip(bs, 3);                                   // video_object_layer_priority
202                          }                          } else {
                         else  
                         {  
203                                  vol_ver_id = 1;                                  vol_ver_id = 1;
204                          }                          }
205                          //DEBUGI("vol_ver_id", vol_ver_id);                          //DEBUGI("vol_ver_id", vol_ver_id);
# Line 209  Line 215 
215                          {                          {
216                                  DEBUG("+ vol_control_parameters");                                  DEBUG("+ vol_control_parameters");
217                                  BitstreamSkip(bs, 2);                                           // chroma_format                                  BitstreamSkip(bs, 2);                                           // chroma_format
218                                  BitstreamSkip(bs, 1);                                           // low_delay                                  dec->low_delay = BitstreamGetBit(bs);   // low_delay
219                                  if (BitstreamGetBit(bs))                                        // vbv_parameters                                  if (BitstreamGetBit(bs))                                        // vbv_parameters
220                                  {                                  {
221                                          DEBUG("+ vbv_parameters");                                          DEBUG("+ vbv_parameters");
# Line 231  Line 237 
237                          dec->shape = BitstreamGetBits(bs, 2);   // video_object_layer_shape                          dec->shape = BitstreamGetBits(bs, 2);   // video_object_layer_shape
238                          // DEBUG1("shape", dec->shape);                          // DEBUG1("shape", dec->shape);
239    
240                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1)                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) {
                         {  
241                                  BitstreamSkip(bs, 4);           // video_object_layer_shape_extension                                  BitstreamSkip(bs, 4);           // video_object_layer_shape_extension
242                          }                          }
243    
244                          READ_MARKER();                          READ_MARKER();
245    
246                          time_inc_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution  // *************************** for decode B-frame time ***********************
247                          time_inc_resolution--;                          time_increment_resolution = BitstreamGetBits(bs, 16);   // vop_time_increment_resolution
248                          if (time_inc_resolution > 0)                          time_increment_resolution--;
249                          {                          //DEBUG1("time_increment_resolution=",time_increment_resolution);
250                                  dec->time_inc_bits = log2bin(time_inc_resolution);                          if (time_increment_resolution > 0) {
251                          }                                  dec->time_inc_bits = log2bin(time_increment_resolution);
252                          else                          } else {
                         {  
253                                  // dec->time_inc_bits = 0;                                  // dec->time_inc_bits = 0;
254    
255                                  // for "old" xvid compatibility, set time_inc_bits = 1                                  // for "old" xvid compatibility, set time_inc_bits = 1
# Line 259  Line 263 
263                                  BitstreamSkip(bs, dec->time_inc_bits);  // fixed_vop_time_increment                                  BitstreamSkip(bs, dec->time_inc_bits);  // fixed_vop_time_increment
264                          }                          }
265    
266                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) {
                         {  
267    
268                                  if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR)                                  if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) {
                                 {  
269                                          uint32_t width, height;                                          uint32_t width, height;
270    
271                                          READ_MARKER();                                          READ_MARKER();
# Line 274  Line 276 
276                                          //DEBUGI("height", height);                                          //DEBUGI("height", height);
277                                          READ_MARKER();                                          READ_MARKER();
278    
279                                          if (width != dec->width || height != dec->height)                                          if (width != dec->width || height != dec->height) {
                                         {  
280                                                  DEBUG("FATAL: video dimension discrepancy ***");                                                  DEBUG("FATAL: video dimension discrepancy ***");
281                                                  DEBUG2("bitstream width/height", width, height);                                                  DEBUG2("bitstream width/height", width, height);
282                                                  DEBUG2("param width/height", dec->width, dec->height);                                                  DEBUG2("param width/height", dec->width, dec->height);
# Line 284  Line 285 
285    
286                                  }                                  }
287    
288                                  if ((dec->interlacing = BitstreamGetBit(bs)))                                  if ((dec->interlacing = BitstreamGetBit(bs))) {
                                 {  
289                                          DEBUG("vol: interlacing");                                          DEBUG("vol: interlacing");
290                                  }                                  }
291    
# Line 302  Line 302 
302                                          return -1;                                          return -1;
303                                  }                                  }
304    
305                                  if (vol_ver_id != 1 && dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)                                  if (vol_ver_id != 1 &&
306                                  {                                          dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
307                                          BitstreamSkip(bs, 1);                                   // sadct_disable                                          BitstreamSkip(bs, 1);                                   // sadct_disable
308                                  }                                  }
309    
# Line 312  Line 312 
312                                          DEBUG("+ not_8_bit [IGNORED/TODO]");                                          DEBUG("+ not_8_bit [IGNORED/TODO]");
313                                          dec->quant_bits = BitstreamGetBits(bs, 4);      // quant_precision                                          dec->quant_bits = BitstreamGetBits(bs, 4);      // quant_precision
314                                          BitstreamSkip(bs, 4);                                           // bits_per_pixel                                          BitstreamSkip(bs, 4);                                           // bits_per_pixel
315                                  }                                  } else {
                                 else  
                                 {  
316                                          dec->quant_bits = 5;                                          dec->quant_bits = 5;
317                                  }                                  }
318    
319                                  if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE)                                  if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {
                                 {  
320                                          BitstreamSkip(bs, 1);                   // no_gray_quant_update                                          BitstreamSkip(bs, 1);                   // no_gray_quant_update
321                                          BitstreamSkip(bs, 1);                   // composition_method                                          BitstreamSkip(bs, 1);                   // composition_method
322                                          BitstreamSkip(bs, 1);                   // linear_composition                                          BitstreamSkip(bs, 1);                   // linear_composition
# Line 328  Line 325 
325                                  dec->quant_type = BitstreamGetBit(bs);          // quant_type                                  dec->quant_type = BitstreamGetBit(bs);          // quant_type
326                                  // DEBUG1("**** quant_type", dec->quant_type);                                  // DEBUG1("**** quant_type", dec->quant_type);
327    
328                                  if (dec->quant_type)                                  if (dec->quant_type) {
                                 {  
329                                          if (BitstreamGetBit(bs))                // load_intra_quant_mat                                          if (BitstreamGetBit(bs))                // load_intra_quant_mat
330                                          {                                          {
331                                                  uint8_t matrix[64];                                                  uint8_t matrix[64];
332    
333                                                  bs_get_matrix(bs, matrix);                                                  bs_get_matrix(bs, matrix);
334                                                  set_intra_matrix(matrix);                                                  set_intra_matrix(matrix);
335                                          }                                          } else
                                         else  
336                                                  set_intra_matrix(get_default_intra_matrix());                                                  set_intra_matrix(get_default_intra_matrix());
337    
338                                          if (BitstreamGetBit(bs))                // load_inter_quant_mat                                          if (BitstreamGetBit(bs))                // load_inter_quant_mat
339                                          {                                          {
340                                                  uint8_t matrix[64];                                                  uint8_t matrix[64];
341    
342                                                  bs_get_matrix(bs, matrix);                                                  bs_get_matrix(bs, matrix);
343                                                  set_inter_matrix(matrix);                                                  set_inter_matrix(matrix);
344                                          }                                          } else
                                         else  
345                                                  set_inter_matrix(get_default_inter_matrix());                                                  set_inter_matrix(get_default_inter_matrix());
346    
347                                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE)                                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {
                                         {  
348                                                  // TODO                                                  // TODO
349                                                  DEBUG("TODO: grayscale matrix stuff");                                                  DEBUG("TODO: grayscale matrix stuff");
350                                                  return -1;                                                  return -1;
# Line 358  Line 353 
353                                  }                                  }
354    
355    
356                                  if (vol_ver_id != 1)                                  if (vol_ver_id != 1) {
                                 {  
357                                          dec->quarterpel = BitstreamGetBit(bs);  // quarter_sampe                                          dec->quarterpel = BitstreamGetBit(bs);  // quarter_sampe
358                                          if (dec->quarterpel)                                          if (dec->quarterpel) {
                                         {  
359                                                  DEBUG("IGNORED/TODO: quarter_sample");                                                  DEBUG("IGNORED/TODO: quarter_sample");
360                                          }                                          }
361                                  }                                  } else {
                                 else  
                                 {  
362                                          dec->quarterpel = 0;                                          dec->quarterpel = 0;
363                                  }                                  }
364    
# Line 390  Line 381 
381                                          BitstreamSkip(bs, 1);           // reversible_vlc                                          BitstreamSkip(bs, 1);           // reversible_vlc
382                                  }                                  }
383    
384                                  if (vol_ver_id != 1)                                  if (vol_ver_id != 1) {
                                 {  
385                                          if (BitstreamGetBit(bs))                        // newpred_enable                                          if (BitstreamGetBit(bs))                        // newpred_enable
386                                          {                                          {
387                                                  DEBUG("+ newpred_enable");                                                  DEBUG("+ newpred_enable");
# Line 406  Line 396 
396                                          }                                          }
397                                  }                                  }
398    
399                                  if (BitstreamGetBit(bs))        // scalability                                  if ((dec->scalability = BitstreamGetBit(bs)))   // scalability
400                                  {                                  {
401                                          // TODO                                          // TODO
402                                          DEBUG("TODO: scalability");                                          DEBUG("TODO: scalability");
403                                          return -1;                                          return -1;
404                                  }                                  }
405                          }                          } else                          // dec->shape == BINARY_ONLY
                         else    // dec->shape == BINARY_ONLY  
                         {  
                                 if (vol_ver_id != 1)  
406                                  {                                  {
407                                    if (vol_ver_id != 1) {
408                                          if (BitstreamGetBit(bs))        // scalability                                          if (BitstreamGetBit(bs))        // scalability
409                                          {                                          {
410                                                  // TODO                                                  // TODO
# Line 428  Line 416 
416    
417                          }                          }
418    
419                  }                  } else if (start_code == GRPOFVOP_START_CODE) {
                 else if (start_code == GRPOFVOP_START_CODE)  
                 {  
420                          // DEBUG("group_of_vop");                          // DEBUG("group_of_vop");
421                          BitstreamSkip(bs, 32);                          BitstreamSkip(bs, 32);
422                          {                          {
423                                  int hours, minutes, seconds;                                  int hours, minutes, seconds;
424    
425                                  hours = BitstreamGetBits(bs, 5);                                  hours = BitstreamGetBits(bs, 5);
426                                  minutes = BitstreamGetBits(bs, 6);                                  minutes = BitstreamGetBits(bs, 6);
427                                  READ_MARKER();                                  READ_MARKER();
# Line 443  Line 430 
430                          }                          }
431                          BitstreamSkip(bs, 1);                   // closed_gov                          BitstreamSkip(bs, 1);                   // closed_gov
432                          BitstreamSkip(bs, 1);                   // broken_link                          BitstreamSkip(bs, 1);                   // broken_link
433                  }                  } else if (start_code == VOP_START_CODE) {
                 else if (start_code == VOP_START_CODE)  
                 {  
434                          // DEBUG("vop_start_code");                          // DEBUG("vop_start_code");
435                          BitstreamSkip(bs, 32);                                          // vop_start_code                          BitstreamSkip(bs, 32);                                          // vop_start_code
436    
437                          coding_type = BitstreamGetBits(bs, 2);          // vop_coding_type                          coding_type = BitstreamGetBits(bs, 2);          // vop_coding_type
438                          //DEBUG1("coding_type", coding_type);                          //DEBUG1("coding_type", coding_type);
439    
440                          while (BitstreamGetBit(bs) != 0) ;                      // time_base  // *************************** for decode B-frame time ***********************
441                            while (BitstreamGetBit(bs) != 0)        // time_base
442                                    time_incr++;
443    
444                          READ_MARKER();                          READ_MARKER();
445    
446                          //DEBUG1("time_inc_bits", dec->time_inc_bits);                          //DEBUG1("time_inc_bits", dec->time_inc_bits);
447                          //DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits));                          //DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits));
448                          if (dec->time_inc_bits)                          if (dec->time_inc_bits) {
449                          {                                  //BitstreamSkip(bs, dec->time_inc_bits);    // vop_time_increment
450                                  BitstreamSkip(bs, dec->time_inc_bits);  // vop_time_increment                                  time_increment = (BitstreamGetBits(bs, dec->time_inc_bits));    // vop_time_increment
451                            }
452                            if (coding_type != B_VOP) {
453                                    dec->last_time_base = dec->time_base;
454                                    dec->time_base += time_incr;
455                                    dec->time =
456                                            dec->time_base * time_increment_resolution +
457                                            time_increment;
458                                    dec->time_pp = (uint32_t) (dec->time - dec->last_non_b_time);
459                                    dec->last_non_b_time = dec->time;
460                            } else {
461                                    dec->time =
462                                            (dec->last_time_base +
463                                             time_incr) * time_increment_resolution + time_increment;
464                                    dec->time_bp = (uint32_t) (dec->last_non_b_time - dec->time);
465                          }                          }
466                            //DEBUG1("time_increment=",time_increment);
467    
468                          READ_MARKER();                          READ_MARKER();
469    
# Line 475  Line 477 
477                          }                          }
478                          */                          */
479    
480                          if (coding_type != I_VOP)                          // fix a little bug by MinChen <chenm002@163.com>
481                          {                          if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) &&
482                                    (coding_type == P_VOP)) {
483                                  *rounding = BitstreamGetBit(bs);        // rounding_type                                  *rounding = BitstreamGetBit(bs);        // rounding_type
484                                  //DEBUG1("rounding", *rounding);                                  //DEBUG1("rounding", *rounding);
485                          }                          }
# Line 486  Line 489 
489                          }                          }
490                          */                          */
491    
492                          if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)                          if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
                         {  
493                                  uint32_t width, height;                                  uint32_t width, height;
494                                  uint32_t horiz_mc_ref, vert_mc_ref;                                  uint32_t horiz_mc_ref, vert_mc_ref;
495    
# Line 511  Line 513 
513                          }                          }
514    
515    
516                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) {
                         {  
517                                  // intra_dc_vlc_threshold                                  // intra_dc_vlc_threshold
518                                  *intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ];                                  *intra_dc_threshold =
519                                            intra_dc_threshold_table[BitstreamGetBits(bs, 3)];
520    
521                                  if (dec->interlacing)                                  if (dec->interlacing) {
522                                  {                                          if ((dec->top_field_first = BitstreamGetBit(bs))) {
                                         if ((dec->top_field_first = BitstreamGetBit(bs)))  
                                         {  
523                                                  DEBUG("vop: top_field_first");                                                  DEBUG("vop: top_field_first");
524                                          }                                          }
525                                          if ((dec->alternate_vertical_scan = BitstreamGetBit(bs)))                                          if ((dec->alternate_vertical_scan = BitstreamGetBit(bs))) {
                                         {  
526                                                  DEBUG("vop: alternate_vertical_scan");                                                  DEBUG("vop: alternate_vertical_scan");
527                                          }                                          }
528                                  }                                  }
529                          }                          }
530    
531                          *quant = BitstreamGetBits(bs, dec->quant_bits);         // vop_quant                          if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1)       // vop_quant
532                                    *quant = 1;
533    
534                          //DEBUG1("quant", *quant);                          //DEBUG1("quant", *quant);
535    
536                          if (coding_type != I_VOP)                          if (coding_type != I_VOP) {
537                          {                                  *fcode_forward = BitstreamGetBits(bs, 3);       // fcode_forward
                                 *fcode = BitstreamGetBits(bs, 3);                       // fcode_forward  
538                          }                          }
539    
540                          if (coding_type == B_VOP)                          if (coding_type == B_VOP) {
541                          {                                  *fcode_backward = BitstreamGetBits(bs, 3);      // fcode_backward
                                 // *fcode_backward = BitstreamGetBits(bs, 3);           // fcode_backward  
542                          }                          }
543                          return coding_type;                          if (!dec->scalability) {
544                                    if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) &&
545                                            (coding_type != I_VOP)) {
546                                            BitstreamSkip(bs, 1);   // vop_shape_coding_type
547                  }                  }
548                  else if (start_code == USERDATA_START_CODE)                          }
549                  {                          return coding_type;
550                    } else if (start_code == USERDATA_START_CODE) {
551                          // DEBUG("user_data");                          // DEBUG("user_data");
552                          BitstreamSkip(bs, 32);          // user_data_start_code                          BitstreamSkip(bs, 32);          // user_data_start_code
553                  }                  } else                                  // start_code == ?
                 else  // start_code == ?  
554                  {                  {
555                          if (BitstreamShowBits(bs, 24) == 0x000001)                          if (BitstreamShowBits(bs, 24) == 0x000001) {
556                          {                                  DEBUG1("*** WARNING: unknown start_code",
557                                  DEBUG1("*** WARNING: unknown start_code", BitstreamShowBits(bs, 32));                                             BitstreamShowBits(bs, 32));
558                          }                          }
559                          BitstreamSkip(bs, 8);                          BitstreamSkip(bs, 8);
560                  }                  }
# Line 566  Line 568 
568    
569  /* write custom quant matrix */  /* write custom quant matrix */
570    
571  static void bs_put_matrix(Bitstream * bs, const int16_t *matrix)  static void
572    bs_put_matrix(Bitstream * bs,
573                              const int16_t * matrix)
574  {  {
575          int i, j;          int i, j;
576          const int last = matrix[scan_tables[0][63]];          const int last = matrix[scan_tables[0][63]];
577    
578          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--) ;
579    
580          for (i = 0; i <= j; i++)          for (i = 0; i <= j; i++) {
         {  
581                  BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8);                  BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8);
582          }          }
583    
584          if (j < 63)          if (j < 63) {
         {  
585                  BitstreamPutBits(bs, 0, 8);                  BitstreamPutBits(bs, 0, 8);
586          }          }
587  }  }
# Line 588  Line 590 
590  /*  /*
591          write vol header          write vol header
592  */  */
593  void BitstreamWriteVolHeader(Bitstream * const bs,  void
594                                                  const MBParam * pParam)  BitstreamWriteVolHeader(Bitstream * const bs,
595                                                    const MBParam * pParam,
596                                                    const FRAMEINFO * frame)
597  {  {
598          // video object_start_code & vo_id          // video object_start_code & vo_id
599      BitstreamPad(bs);      BitstreamPad(bs);
# Line 604  Line 608 
608          BitstreamPutBits(bs, 0, 8);                     // video_object_type_indication          BitstreamPutBits(bs, 0, 8);                     // video_object_type_indication
609          BitstreamPutBit(bs, 0);                         // is_object_layer_identified (0=not given)          BitstreamPutBit(bs, 0);                         // is_object_layer_identified (0=not given)
610          BitstreamPutBits(bs, 1, 4);                     // aspect_ratio_info (1=1:1)          BitstreamPutBits(bs, 1, 4);                     // aspect_ratio_info (1=1:1)
611          BitstreamPutBit(bs, 0);                         // vol_control_parameters (0=not given)  
612    #ifdef BFRAMES
613            if (pParam->max_bframes > 0) {
614                    dprintf("low_delay=1");
615                    BitstreamPutBit(bs, 1); // vol_control_parameters
616                    BitstreamPutBits(bs, 1, 2);     // chroma_format 1="4:2:0"
617                    BitstreamPutBit(bs, 0); // low_delay
618                    BitstreamPutBit(bs, 0); // vbv_parameters (0=not given)
619            } else
620    #endif
621            {
622                    BitstreamPutBits(bs, 0, 1);     // vol_control_parameters (0=not given)
623            }
624    
625    
626          BitstreamPutBits(bs, 0, 2);                     // video_object_layer_shape (0=rectangular)          BitstreamPutBits(bs, 0, 2);                     // video_object_layer_shape (0=rectangular)
627    
628          WRITE_MARKER();          WRITE_MARKER();
# Line 614  Line 632 
632                          25fps           res=25          inc=1                          25fps           res=25          inc=1
633                          29.97fps        res=30000       inc=1001                          29.97fps        res=30000       inc=1001
634          */          */
635    #ifdef BFRAMES
636            BitstreamPutBits(bs, pParam->fbase, 16);
637    #else
638          BitstreamPutBits(bs, 2, 16);          BitstreamPutBits(bs, 2, 16);
639    #endif
640    
641          WRITE_MARKER();          WRITE_MARKER();
642    
# Line 630  Line 652 
652          BitstreamPutBits(bs, pParam->height, 13);               // height          BitstreamPutBits(bs, pParam->height, 13);               // height
653          WRITE_MARKER();          WRITE_MARKER();
654    
655          BitstreamPutBit(bs, pParam->global_flags & XVID_INTERLACING);           // interlace          BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING);    // interlace
656          BitstreamPutBit(bs, 1);         // obmc_disable (overlapped block motion compensation)          BitstreamPutBit(bs, 1);         // obmc_disable (overlapped block motion compensation)
657          BitstreamPutBit(bs, 0);         // sprite_enable          BitstreamPutBit(bs, 0);         // sprite_enable
658          BitstreamPutBit(bs, 0);         // not_in_bit          BitstreamPutBit(bs, 0);         // not_in_bit
659    
660          // quant_type   0=h.263  1=mpeg4(quantizer tables)          // quant_type   0=h.263  1=mpeg4(quantizer tables)
661          BitstreamPutBit(bs, pParam->quant_type);          BitstreamPutBit(bs, pParam->m_quant_type);
662    
663          if (pParam->quant_type)          if (pParam->m_quant_type) {
         {  
664                  BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat                  BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat
665                  if (get_intra_matrix_status())                  if (get_intra_matrix_status()) {
                 {  
666                          bs_put_matrix(bs, get_intra_matrix());                          bs_put_matrix(bs, get_intra_matrix());
667                  }                  }
668    
669                  BitstreamPutBit(bs, get_inter_matrix_status());         // load_inter_quant_mat                  BitstreamPutBit(bs, get_inter_matrix_status());         // load_inter_quant_mat
670                  if (get_inter_matrix_status())                  if (get_inter_matrix_status()) {
                 {  
671                          bs_put_matrix(bs, get_inter_matrix());                          bs_put_matrix(bs, get_inter_matrix());
672                  }                  }
673    
# Line 669  Line 688 
688    time_inc = nth of a second since last resync    time_inc = nth of a second since last resync
689    (decoder uses these values to determine precise time since last resync)    (decoder uses these values to determine precise time since last resync)
690  */  */
691  void BitstreamWriteVopHeader(Bitstream * const bs,  void
692                                                  const MBParam * pParam)  BitstreamWriteVopHeader(Bitstream * const bs,
693  {                                                  const MBParam * pParam,
694                                                    const FRAMEINFO * frame)
695    {
696    #ifdef BFRAMES
697            uint32_t i;
698    #endif
699      BitstreamPad(bs);      BitstreamPad(bs);
700      BitstreamPutBits(bs, VOP_START_CODE, 32);      BitstreamPutBits(bs, VOP_START_CODE, 32);
701    
702      BitstreamPutBits(bs, pParam->coding_type, 2);          BitstreamPutBits(bs, frame->coding_type, 2);
703    
704          // time_base = 0  write n x PutBit(1), PutBit(0)          // time_base = 0  write n x PutBit(1), PutBit(0)
705    #ifdef BFRAMES
706            for (i = 0; i < frame->seconds; i++) {
707                    BitstreamPutBit(bs, 1);
708            }
709            BitstreamPutBit(bs, 0);
710    #else
711          BitstreamPutBits(bs, 0, 1);          BitstreamPutBits(bs, 0, 1);
712    #endif
713    
714          WRITE_MARKER();          WRITE_MARKER();
715    
716          // time_increment: value=nth_of_sec, nbits = log2(resolution)          // time_increment: value=nth_of_sec, nbits = log2(resolution)
717    #ifdef BFRAMES
718            BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase));
719            dprintf("[%i:%i] %c\n", frame->seconds, frame->ticks,
720                            frame->coding_type == I_VOP ? 'I' : frame->coding_type ==
721                            P_VOP ? 'P' : 'B');
722    #else
723          BitstreamPutBits(bs, 1, 1);          BitstreamPutBits(bs, 1, 1);
724    #endif
725    
726          WRITE_MARKER();          WRITE_MARKER();
727    
728          BitstreamPutBits(bs, 1, 1);                             // vop_coded          BitstreamPutBits(bs, 1, 1);                             // vop_coded
729    
730          if (pParam->coding_type != I_VOP)          if (frame->coding_type == P_VOP)
731                  BitstreamPutBits(bs, pParam->rounding_type, 1);                  BitstreamPutBits(bs, frame->rounding_type, 1);
732    
733          BitstreamPutBits(bs, 0, 3);                             // intra_dc_vlc_threshold          BitstreamPutBits(bs, 0, 3);                             // intra_dc_vlc_threshold
734    
735          if (pParam->global_flags & XVID_INTERLACING)          if (frame->global_flags & XVID_INTERLACING) {
         {  
736                  BitstreamPutBit(bs, 1);         // top field first                  BitstreamPutBit(bs, 1);         // top field first
737                  BitstreamPutBit(bs, 0);         // alternate vertical scan                  BitstreamPutBit(bs, 0);         // alternate vertical scan
738          }          }
739    
740          BitstreamPutBits(bs, pParam->quant, 5);                 // quantizer          BitstreamPutBits(bs, frame->quant, 5);  // quantizer
741    
742            if (frame->coding_type != I_VOP)
743                    BitstreamPutBits(bs, frame->fcode, 3);  // forward_fixed_code
744    
745            if (frame->coding_type == B_VOP)
746                    BitstreamPutBits(bs, frame->bcode, 3);  // backward_fixed_code
747    
         if (pParam->coding_type != I_VOP)  
                 BitstreamPutBits(bs, pParam->fixed_code, 3);            // fixed_code = [1,4]  
748  }  }

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.14

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