[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.2, Fri Mar 8 19:17:24 2002 UTC revision 1.28.2.6, Tue Nov 12 15:53:23 2002 UTC
# Line 41  Line 41 
41    *                                                                                                                                                        *    *                                                                                                                                                        *
42    *  Revision history:                                                         *    *  Revision history:                                                         *
43    *                                                                            *    *                                                                            *
44      *  28.10.2002 GMC support - gruel                                                                                        *
45      *  04.10.2002 qpel support - Isibaar                                                                             *
46      *  11.07.2002 add VOP width & height return to dec when dec->width           *
47      *             or dec->height is 0  (for use in examples/ex1.c)               *
48      *             MinChen <chenm001@163.com>                                     *
49      *  22.05.2002 bs_put_matrix fix                                              *
50      *  20.05.2002 added BitstreamWriteUserData                                   *
51      *  19.06.2002 Fix a little bug in use custom quant matrix                    *
52      *             MinChen <chenm001@163.com>                                     *
53      *  08.05.2002 add low_delay support for B_VOP decode                         *
54      *             MinChen <chenm001@163.com>                                     *
55      *  06.05.2002 low_delay                                                      *
56      *  06.05.2002 fixed fincr/fbase error                                        *
57      *  01.05.2002 added BVOP support to BitstreamWriteVopHeader                  *
58      *  15.04.2002 rewrite log2bin use asm386  By MinChen <chenm001@163.com>      *
59      *  26.03.2002 interlacing support                                            *
60    *  03.03.2002 qmatrix writing                                                                                            *    *  03.03.2002 qmatrix writing                                                                                            *
61    *  03.03.2002 merged BITREADER and BITWRITER                                                             *    *  03.03.2002 merged BITREADER and BITWRITER                                                             *
62    *      30.02.2002     intra_dc_threshold support                                                                         *    *      30.02.2002     intra_dc_threshold support                                                                         *
# Line 50  Line 66 
66    ******************************************************************************/    ******************************************************************************/
67    
68    
69    #include <string.h>
70  #include "bitstream.h"  #include "bitstream.h"
71  #include "zigzag.h"  #include "zigzag.h"
72  #include "../quant/quant_matrix.h"  #include "../quant/quant_matrix.h"
73    #include "mbcoding.h"
74    
75  static int __inline log2bin(int value)  
76    static uint32_t __inline
77    log2bin(uint32_t value)
78  {  {
79    /* Changed by Chenm001 */
80    #ifndef WIN32
81          int n = 0;          int n = 0;
82          while (value)  
83          {          while (value) {
84                  value >>= 1;                  value >>= 1;
85                  n++;                  n++;
86          }          }
87          return n;          return n;
88    #else
89            __asm {
90                    bsr eax, value
91                    inc eax
92            }
93    #endif
94  }  }
95    
96    
97  static const uint32_t intra_dc_threshold_table[] =  static const uint32_t intra_dc_threshold_table[] = {
 {  
98          32,     /* never use */          32,     /* never use */
99          13,          13,
100          15,          15,
# Line 79  Line 106 
106  };  };
107    
108    
109  void bs_get_matrix(Bitstream * bs, uint8_t * matrix)  void
110    bs_get_matrix(Bitstream * bs,
111                              uint8_t * matrix)
112  {  {
113          int i = 0;          int i = 0;
114      int last, value = 0;      int last, value = 0;
115    
116      do          do {
         {  
117                  last = value;                  last = value;
118          value = BitstreamGetBits(bs, 8);          value = BitstreamGetBits(bs, 8);
119          matrix[ scan_tables[0][i++] ]  = value;          matrix[ scan_tables[0][i++] ]  = value;
120      }      }
121      while (value != 0 && i < 64);      while (value != 0 && i < 64);
122            i--;    // fix little bug at coeff not full
123    
124          while (i < 64)          while (i < 64) {
         {  
125                  matrix[ scan_tables[0][i++] ]  = last;                  matrix[ scan_tables[0][i++] ]  = last;
126          }          }
127  }  }
128    
129    
130    
131    // for PVOP addbits == fcode - 1
132    // for BVOP addbits == max(fcode,bcode) - 1
133    // returns mbpos
134    int
135    read_video_packet_header(Bitstream *bs,
136                                                    DECODER * dec,
137                                                    const int addbits,
138                                                    int * quant,
139                                                    int * fcode_forward,
140                                                    int  * fcode_backward,
141                                                    int * intra_dc_threshold)
142    {
143            int startcode_bits = NUMBITS_VP_RESYNC_MARKER + addbits;
144            int mbnum_bits = log2bin(dec->mb_width *  dec->mb_height - 1);
145            int mbnum;
146            int hec = 0;
147    
148            BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs));
149            BitstreamSkip(bs, startcode_bits);
150    
151            DPRINTF(DPRINTF_STARTCODE, "<video_packet_header>");
152    
153            if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)
154            {
155                    hec = BitstreamGetBit(bs);              /* header_extension_code */
156                    if (hec && !(dec->sprite_enable == SPRITE_STATIC /* && current_coding_type = I_VOP */))
157                    {
158                            BitstreamSkip(bs, 13);                  /* vop_width */
159                            READ_MARKER();
160                            BitstreamSkip(bs, 13);                  /* vop_height */
161                            READ_MARKER();
162                            BitstreamSkip(bs, 13);                  /* vop_horizontal_mc_spatial_ref */
163                            READ_MARKER();
164                            BitstreamSkip(bs, 13);                  /* vop_vertical_mc_spatial_ref */
165                            READ_MARKER();
166                    }
167            }
168    
169            mbnum = BitstreamGetBits(bs, mbnum_bits);               /* macroblock_number */
170            DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum);
171    
172            if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)
173            {
174                    *quant = BitstreamGetBits(bs, 5);       /* quant_scale */
175                    DPRINTF(DPRINTF_HEADER, "quant %i", *quant);
176            }
177    
178            if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR)
179                    hec = BitstreamGetBit(bs);              /* header_extension_code */
180    
181    
182            DPRINTF(DPRINTF_HEADER, "header_extension_code %i", hec);
183            if (hec)
184            {
185                    int time_base;
186                    int time_increment;
187                    int coding_type;
188    
189                    for (time_base=0; BitstreamGetBit(bs)!=0; time_base++);         /* modulo_time_base */
190                    READ_MARKER();
191                    if (dec->time_inc_bits)
192                            time_increment = (BitstreamGetBits(bs, dec->time_inc_bits));    /* vop_time_increment */
193                    READ_MARKER();
194                    DPRINTF(DPRINTF_HEADER,"time %i:%i", time_base, time_increment);
195    
196                    coding_type = BitstreamGetBits(bs, 2);
197                    DPRINTF(DPRINTF_HEADER,"coding_type %i", coding_type);
198    
199                    if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)
200                    {
201                            BitstreamSkip(bs, 1);   /* change_conv_ratio_disable */
202                            if (coding_type != I_VOP)
203                                    BitstreamSkip(bs, 1);   /* vop_shape_coding_type */
204                    }
205    
206                    if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)
207                    {
208                            *intra_dc_threshold = intra_dc_threshold_table[BitstreamGetBits(bs, 3)];
209    
210                            if (dec->sprite_enable == SPRITE_GMC && coding_type == S_VOP &&
211                                    dec->sprite_warping_points > 0)
212                            {
213                                    // TODO: sprite trajectory
214                            }
215                            if (dec->reduced_resolution_enable &&
216                                    dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR &&
217                                    (coding_type == P_VOP || coding_type == I_VOP))
218                            {
219                                    BitstreamSkip(bs, 1); /* vop_reduced_resolution */
220                            }
221    
222                            if (coding_type != I_VOP && fcode_forward)
223                            {
224                                    *fcode_forward = BitstreamGetBits(bs, 3);
225                                    DPRINTF(DPRINTF_HEADER,"fcode_forward %i", *fcode_forward);
226                            }
227    
228                            if (coding_type == B_VOP && fcode_backward)
229                            {
230                                    *fcode_backward = BitstreamGetBits(bs, 3);
231                                    DPRINTF(DPRINTF_HEADER,"fcode_backward %i", fcode_backward);
232                            }
233                    }
234    
235            }
236    
237            if (dec->newpred_enable)
238            {
239                    int vop_id;
240                    int vop_id_for_prediction;
241    
242                    vop_id = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15));
243                    DPRINTF(DPRINTF_HEADER, "vop_id %i", vop_id);
244                    if (BitstreamGetBit(bs))        /* vop_id_for_prediction_indication */
245                    {
246                            vop_id_for_prediction = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15));
247                            DPRINTF(DPRINTF_HEADER, "vop_id_for_prediction %i", vop_id_for_prediction);
248                    }
249                    READ_MARKER();
250            }
251    
252            return mbnum;
253    }
254    
255    
256    
257  /*  /*
258  decode headers  decode headers
259  returns coding_type, or -1 if error  returns coding_type, or -1 if error
260  */  */
261    
262  int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, uint32_t * quant, uint32_t * fcode, uint32_t * intra_dc_threshold)  #define VIDOBJ_START_CODE_MASK          0x0000001f
263    #define VIDOBJLAY_START_CODE_MASK       0x0000000f
264    
265    int
266    BitstreamReadHeaders(Bitstream * bs,
267                                             DECODER * dec,
268                                             uint32_t * rounding,
269                                             uint32_t * reduced_resolution,
270                                             uint32_t * quant,
271                                             uint32_t * fcode_forward,
272                                             uint32_t * fcode_backward,
273                                             uint32_t * intra_dc_threshold,
274                                             VECTOR * gmc_mv)
275  {  {
276          uint32_t vol_ver_id;          uint32_t vol_ver_id;
         uint32_t time_inc_resolution;  
277          uint32_t coding_type;          uint32_t coding_type;
278          uint32_t start_code;          uint32_t start_code;
279            uint32_t time_incr = 0;
280            int32_t time_increment;
281            int resize = 0;
282    
283            do {
284    
         do  
         {  
285                  BitstreamByteAlign(bs);                  BitstreamByteAlign(bs);
286                  start_code = BitstreamShowBits(bs, 32);                  start_code = BitstreamShowBits(bs, 32);
287    
288                  if (start_code == VISOBJSEQ_START_CODE)                  if (start_code == VISOBJSEQ_START_CODE) {
289                  {  
290                          // DEBUG("visual_object_sequence");                          int profile;
291    
292                            DPRINTF(DPRINTF_STARTCODE, "<visual_object_sequence>");
293    
294                          BitstreamSkip(bs, 32);                          // visual_object_sequence_start_code                          BitstreamSkip(bs, 32);                          // visual_object_sequence_start_code
295                          BitstreamSkip(bs, 8);                                   // profile_and_level_indication                          profile = BitstreamGetBits(bs, 8);      // profile_and_level_indication
296                  }  
297                  else if (start_code == VISOBJSEQ_STOP_CODE)                          DPRINTF(DPRINTF_HEADER, "profile_and_level_indication %i", profile);
298                  {  
299                    } else if (start_code == VISOBJSEQ_STOP_CODE) {
300    
301                          BitstreamSkip(bs, 32);                          // visual_object_sequence_stop_code                          BitstreamSkip(bs, 32);                          // visual_object_sequence_stop_code
302                  }  
303                  else if (start_code == VISOBJ_START_CODE)                          DPRINTF(DPRINTF_STARTCODE, "</visual_object_sequence>");
304                  {  
305                          // DEBUG("visual_object");                  } else if (start_code == VISOBJ_START_CODE) {
306    
307                            DPRINTF(DPRINTF_STARTCODE, "<visual_object>");
308    
309                          BitstreamSkip(bs,32);                                   // visual_object_start_code                          BitstreamSkip(bs,32);                                   // visual_object_start_code
310                          if (BitstreamGetBit(bs))                                // is_visual_object_identified                          if (BitstreamGetBit(bs))                                // is_visual_object_identified
311                          {                          {
312                                  vol_ver_id = BitstreamGetBits(bs,4);    // visual_object_ver_id                                  vol_ver_id = BitstreamGetBits(bs,4);    // visual_object_ver_id
313                                    DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id);
314                                  BitstreamSkip(bs, 3);                           // visual_object_priority                                  BitstreamSkip(bs, 3);                           // visual_object_priority
315                          }                          } else {
                         else  
                         {  
316                                  vol_ver_id = 1;                                  vol_ver_id = 1;
317                          }                          }
318    
319                          if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO)      // visual_object_type                          if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO)      // visual_object_type
320                          {                          {
321                                  DEBUG("visual_object_type != video");                                  DPRINTF(DPRINTF_ERROR, "visual_object_type != video");
322                                  return -1;                                  return -1;
323                          }                          }
324                          BitstreamSkip(bs, 4);                          BitstreamSkip(bs, 4);
# Line 150  Line 327 
327    
328                          if (BitstreamGetBit(bs))                                // video_signal_type                          if (BitstreamGetBit(bs))                                // video_signal_type
329                          {                          {
330                                  DEBUG("+ video_signal_type");                                  DPRINTF(DPRINTF_HEADER,"+ video_signal_type");
331                                  BitstreamSkip(bs, 3);                           // video_format                                  BitstreamSkip(bs, 3);                           // video_format
332                                  BitstreamSkip(bs, 1);                           // video_range                                  BitstreamSkip(bs, 1);                           // video_range
333                                  if (BitstreamGetBit(bs))                        // color_description                                  if (BitstreamGetBit(bs))                        // color_description
334                                  {                                  {
335                                          DEBUG("+ color_description");                                          DPRINTF(DPRINTF_HEADER,"+ color_description");
336                                          BitstreamSkip(bs, 8);                   // color_primaries                                          BitstreamSkip(bs, 8);                   // color_primaries
337                                          BitstreamSkip(bs, 8);                   // transfer_characteristics                                          BitstreamSkip(bs, 8);                   // transfer_characteristics
338                                          BitstreamSkip(bs, 8);                   // matrix_coefficients                                          BitstreamSkip(bs, 8);                   // matrix_coefficients
339                                  }                                  }
340                          }                          }
341                  }                  } else if ((start_code & ~VIDOBJ_START_CODE_MASK) == VIDOBJ_START_CODE) {
342                  else if ((start_code & ~0x1f) == VIDOBJ_START_CODE)  
343                  {                          DPRINTF(DPRINTF_STARTCODE, "<video_object>");
344                            DPRINTF(DPRINTF_HEADER, "vo id %i", start_code & VIDOBJ_START_CODE_MASK);
345    
346                          BitstreamSkip(bs, 32);          // video_object_start_code                          BitstreamSkip(bs, 32);          // video_object_start_code
347                  }  
348                  else if ((start_code & ~0xf) == VIDOBJLAY_START_CODE)                  } else if ((start_code & ~VIDOBJLAY_START_CODE_MASK) == VIDOBJLAY_START_CODE) {
349                  {  
350                          // DEBUG("video_object_layer");                          DPRINTF(DPRINTF_STARTCODE, "<video_object_layer>");
351                            DPRINTF(DPRINTF_HEADER, "vol id %i", start_code & VIDOBJLAY_START_CODE_MASK);
352    
353                          BitstreamSkip(bs, 32);                                  // video_object_layer_start_code                          BitstreamSkip(bs, 32);                                  // video_object_layer_start_code
354    
355                          BitstreamSkip(bs, 1);                                                                   // random_accessible_vol                          BitstreamSkip(bs, 1);                                                                   // random_accessible_vol
# Line 177  Line 358 
358                          if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE &&                          if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE &&
359                                  BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE &&                                  BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE &&
360                                  BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN &&                                  BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN &&
361                                    BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_ACE &&
362                                    BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_ART_SIMPLE &&
363                                  BitstreamShowBits(bs, 8) != 0)          // BUGGY DIVX                                  BitstreamShowBits(bs, 8) != 0)          // BUGGY DIVX
364                          {                          {
365                                  DEBUG1("video_object_type_indication not supported", BitstreamShowBits(bs, 8));                                  DPRINTF(DPRINTF_ERROR,"video_object_type_indication %i not supported ",
366                                            BitstreamShowBits(bs, 8));
367                                  return -1;                                  return -1;
368                          }                          }
369                          BitstreamSkip(bs, 8);                          BitstreamSkip(bs, 8);
# Line 187  Line 371 
371    
372                          if (BitstreamGetBit(bs))                                        // is_object_layer_identifier                          if (BitstreamGetBit(bs))                                        // is_object_layer_identifier
373                          {                          {
374                                  DEBUG("+ is_object_layer_identifier");                                  DPRINTF(DPRINTF_HEADER, "+ is_object_layer_identifier");
375                                  vol_ver_id = BitstreamGetBits(bs,4);            // video_object_layer_verid                                  vol_ver_id = BitstreamGetBits(bs,4);            // video_object_layer_verid
376                                    DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id);
377                                  BitstreamSkip(bs, 3);                                   // video_object_layer_priority                                  BitstreamSkip(bs, 3);                                   // video_object_layer_priority
378                          }                          } else {
                         else  
                         {  
379                                  vol_ver_id = 1;                                  vol_ver_id = 1;
380                          }                          }
                         //DEBUGI("vol_ver_id", vol_ver_id);  
381    
382                          if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR)     // aspect_ratio_info                          dec->aspect_ratio = BitstreamGetBits(bs, 4);
383    
384                            if (dec->aspect_ratio == VIDOBJLAY_AR_EXTPAR)   // aspect_ratio_info
385                          {                          {
386                                  DEBUG("+ aspect_ratio_info");                                  DPRINTF(DPRINTF_HEADER, "+ aspect_ratio_info");
387                                  BitstreamSkip(bs, 8);                                           // par_width                                  dec->par_width = BitstreamGetBits(bs, 8);       // par_width
388                                  BitstreamSkip(bs, 8);                                           // par_height                                  dec->par_height = BitstreamGetBits(bs, 8);      // par_height
389                          }                          }
390    
391                          if (BitstreamGetBit(bs))                // vol_control_parameters                          if (BitstreamGetBit(bs))                // vol_control_parameters
392                          {                          {
393                                  DEBUG("+ vol_control_parameters");                                  DPRINTF(DPRINTF_HEADER, "+ vol_control_parameters");
394                                  BitstreamSkip(bs, 2);                                           // chroma_format                                  BitstreamSkip(bs, 2);                                           // chroma_format
395                                  BitstreamSkip(bs, 1);                                           // low_delay                                  dec->low_delay = BitstreamGetBit(bs);   // low_delay
396                                    DPRINTF(DPRINTF_HEADER, "low_delay %i", dec->low_delay);
397                                  if (BitstreamGetBit(bs))                                        // vbv_parameters                                  if (BitstreamGetBit(bs))                                        // vbv_parameters
398                                  {                                  {
399                                          DEBUG("+ vbv_parameters");                                          DPRINTF(DPRINTF_HEADER,"+ vbv_parameters");
400                                          BitstreamSkip(bs, 15);                          // first_half_bitrate                                          BitstreamSkip(bs, 15);                          // first_half_bitrate
401                                          READ_MARKER();                                          READ_MARKER();
402                                          BitstreamSkip(bs, 15);                          // latter_half_bitrate                                          BitstreamSkip(bs, 15);                          // latter_half_bitrate
# Line 223  Line 408 
408                                          READ_MARKER();                                          READ_MARKER();
409                                          BitstreamSkip(bs, 15);                          // latter_half_vbv_occupancy                                          BitstreamSkip(bs, 15);                          // latter_half_vbv_occupancy
410                                          READ_MARKER();                                          READ_MARKER();
   
411                                  }                                  }
412                          }                          }
413    
414                          dec->shape = BitstreamGetBits(bs, 2);   // video_object_layer_shape                          dec->shape = BitstreamGetBits(bs, 2);   // video_object_layer_shape
                         // DEBUG1("shape", dec->shape);  
415    
416                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1)                          DPRINTF(DPRINTF_HEADER, "shape %i", dec->shape);
417                            if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)
418                          {                          {
419                                    DPRINTF(DPRINTF_ERROR,"non-rectangular shapes are not supported");
420                            }
421    
422                            if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) {
423                                  BitstreamSkip(bs, 4);           // video_object_layer_shape_extension                                  BitstreamSkip(bs, 4);           // video_object_layer_shape_extension
424                          }                          }
425    
426                          READ_MARKER();                          READ_MARKER();
427    
428                          time_inc_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution  // *************************** for decode B-frame time ***********************
429                          time_inc_resolution--;                          dec->time_inc_resolution = BitstreamGetBits(bs, 16);    // vop_time_increment_resolution
430                          if (time_inc_resolution > 0)                          DPRINTF(DPRINTF_HEADER,"vop_time_increment_resolution %i", dec->time_inc_resolution);
431                          {  
432                                  dec->time_inc_bits = log2bin(time_inc_resolution);  //                      dec->time_inc_resolution--;
433                          }  
434                          else                          if (dec->time_inc_resolution > 0) {
435                          {                                  dec->time_inc_bits = log2bin(dec->time_inc_resolution-1);
436                            } else {
437                                  // dec->time_inc_bits = 0;                                  // dec->time_inc_bits = 0;
   
438                                  // for "old" xvid compatibility, set time_inc_bits = 1                                  // for "old" xvid compatibility, set time_inc_bits = 1
439                                  dec->time_inc_bits = 1;                                  dec->time_inc_bits = 1;
440                          }                          }
# Line 255  Line 443 
443    
444                          if (BitstreamGetBit(bs))                                                // fixed_vop_rate                          if (BitstreamGetBit(bs))                                                // fixed_vop_rate
445                          {                          {
446                                    DPRINTF(DPRINTF_HEADER, "+ fixed_vop_rate");
447                                  BitstreamSkip(bs, dec->time_inc_bits);  // fixed_vop_time_increment                                  BitstreamSkip(bs, dec->time_inc_bits);  // fixed_vop_time_increment
448                          }                          }
449    
450                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) {
                         {  
451    
452                                  if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR)                                  if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) {
                                 {  
453                                          uint32_t width, height;                                          uint32_t width, height;
454    
455                                          READ_MARKER();                                          READ_MARKER();
456                                          width = BitstreamGetBits(bs, 13);                       // video_object_layer_width                                          width = BitstreamGetBits(bs, 13);                       // video_object_layer_width
                                         //DEBUGI("width", width);  
457                                          READ_MARKER();                                          READ_MARKER();
458                                          height = BitstreamGetBits(bs, 13);              // video_object_layer_height                                          height = BitstreamGetBits(bs, 13);              // video_object_layer_height
                                         //DEBUGI("height", height);  
459                                          READ_MARKER();                                          READ_MARKER();
460    
461                                          if (width != dec->width || height != dec->height)                                          DPRINTF(DPRINTF_HEADER, "width %i", width);
462                                            DPRINTF(DPRINTF_HEADER, "height %i", height);
463    
464                                            if (dec->width != width || dec->height != height)
465                                          {                                          {
466                                                  DEBUG("FATAL: video dimension discrepancy ***");                                                  if (dec->fixed_dimensions)
467                                                  DEBUG2("bitstream width/height", width, height);                                                  {
468                                                  DEBUG2("param width/height", dec->width, dec->height);                                                          DPRINTF(DPRINTF_ERROR, "XVID_DEC_PARAM width/height does not match bitstream");
469                                                  return -1;                                                  return -1;
470                                          }                                          }
471                                                    resize = 1;
472                                                    dec->width = width;
473                                                    dec->height = height;
474                                  }                                  }
   
                                 if (BitstreamGetBit(bs))                                // interlaced  
                                 {  
                                         DEBUG("TODO: interlaced");  
                                         // TODO  
                                         return -1;  
475                                  }                                  }
476    
477                                    dec->interlacing = BitstreamGetBit(bs);
478                                    DPRINTF(DPRINTF_HEADER, "interlacing %i", dec->interlacing);
479    
480                                  if (!BitstreamGetBit(bs))                               // obmc_disable                                  if (!BitstreamGetBit(bs))                               // obmc_disable
481                                  {                                  {
482                                          DEBUG("IGNORED/TODO: !obmc_disable");                                          DPRINTF(DPRINTF_ERROR, "obmc_disabled==false not supported");
483                                          // TODO                                          // TODO
484                                          // fucking divx4.02 has this enabled                                          // fucking divx4.02 has this enabled
485                                  }                                  }
486    
487                                  if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2)))  // sprite_enable                                  dec->sprite_enable = BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2));   // sprite_enable
488    
489                                    if (dec->sprite_enable == SPRITE_STATIC || dec->sprite_enable == SPRITE_GMC)
490                                  {                                  {
491                                          DEBUG("sprite_enable; not supported");                                          int low_latency_sprite_enable;
                                         return -1;  
                                 }  
492    
493                                  if (vol_ver_id != 1 && dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)                                          if (dec->sprite_enable != SPRITE_GMC)
494                                  {                                  {
495                                                    int sprite_width;
496                                                    int sprite_height;
497                                                    int sprite_left_coord;
498                                                    int sprite_top_coord;
499                                                    sprite_width = BitstreamGetBits(bs, 13);                // sprite_width
500                                                    READ_MARKER();
501                                                    sprite_height = BitstreamGetBits(bs, 13);       // sprite_height
502                                                    READ_MARKER();
503                                                    sprite_left_coord = BitstreamGetBits(bs, 13);   // sprite_left_coordinate
504                                                    READ_MARKER();
505                                                    sprite_top_coord = BitstreamGetBits(bs, 13);    // sprite_top_coordinate
506                                                    READ_MARKER();
507                                            }
508                                            dec->sprite_warping_points = BitstreamGetBits(bs, 6);           // no_of_sprite_warping_points
509                                            dec->sprite_warping_accuracy = BitstreamGetBits(bs, 2);         // sprite_warping_accuracy
510                                            dec->sprite_brightness_change = BitstreamGetBits(bs, 1);                // brightness_change
511                                            if (dec->sprite_enable != SPRITE_GMC)
512                                            {
513                                                    low_latency_sprite_enable = BitstreamGetBits(bs, 1);            // low_latency_sprite_enable
514                                            }
515                                    }
516    
517                                    if (vol_ver_id != 1 &&
518                                            dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
519                                          BitstreamSkip(bs, 1);                                   // sadct_disable                                          BitstreamSkip(bs, 1);                                   // sadct_disable
520                                  }                                  }
521    
522                                  if (BitstreamGetBit(bs))                                                // not_8_bit                                  if (BitstreamGetBit(bs))                                                // not_8_bit
523                                  {                                  {
524                                          DEBUG("+ not_8_bit [IGNORED/TODO]");                                          DPRINTF(DPRINTF_HEADER, "not_8_bit==true (ignored)");
525                                          dec->quant_bits = BitstreamGetBits(bs, 4);      // quant_precision                                          dec->quant_bits = BitstreamGetBits(bs, 4);      // quant_precision
526                                          BitstreamSkip(bs, 4);                                           // bits_per_pixel                                          BitstreamSkip(bs, 4);                                           // bits_per_pixel
527                                  }                                  } else {
                                 else  
                                 {  
528                                          dec->quant_bits = 5;                                          dec->quant_bits = 5;
529                                  }                                  }
530    
531                                  if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE)                                  if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {
                                 {  
532                                          BitstreamSkip(bs, 1);                   // no_gray_quant_update                                          BitstreamSkip(bs, 1);                   // no_gray_quant_update
533                                          BitstreamSkip(bs, 1);                   // composition_method                                          BitstreamSkip(bs, 1);                   // composition_method
534                                          BitstreamSkip(bs, 1);                   // linear_composition                                          BitstreamSkip(bs, 1);                   // linear_composition
535                                  }                                  }
536    
537                                  dec->quant_type = BitstreamGetBit(bs);          // quant_type                                  dec->quant_type = BitstreamGetBit(bs);          // quant_type
538                                  // DEBUG1("**** quant_type", dec->quant_type);                                  DPRINTF(DPRINTF_HEADER, "quant_type %i", dec->quant_type);
539    
540                                  if (dec->quant_type)                                  if (dec->quant_type) {
                                 {  
541                                          if (BitstreamGetBit(bs))                // load_intra_quant_mat                                          if (BitstreamGetBit(bs))                // load_intra_quant_mat
542                                          {                                          {
543                                                  uint8_t *matrix;                                                  uint8_t matrix[64];
544    
545                                                    DPRINTF(DPRINTF_HEADER, "load_intra_quant_mat");
546    
547                                                  bs_get_matrix(bs, matrix);                                                  bs_get_matrix(bs, matrix);
548                                                  set_intra_matrix(matrix);                                                  set_intra_matrix(matrix);
549                                          }                                          } else
550                                                    set_intra_matrix(get_default_intra_matrix());
551    
552                                          if (BitstreamGetBit(bs))                // load_inter_quant_mat                                          if (BitstreamGetBit(bs))                // load_inter_quant_mat
553                                          {                                          {
554                                                  uint8_t *matrix;                                                  uint8_t matrix[64];
555    
556                                                    DPRINTF(DPRINTF_HEADER, "load_inter_quant_mat");
557    
558                                                  bs_get_matrix(bs, matrix);                                                  bs_get_matrix(bs, matrix);
559                                                  set_inter_matrix(matrix);                                                  set_inter_matrix(matrix);
560                                          }                                          } else
561                                                    set_inter_matrix(get_default_inter_matrix());
562    
563                                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE)                                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {
564                                          {                                                  DPRINTF(DPRINTF_ERROR, "greyscale matrix not supported");
                                                 // TODO  
                                                 DEBUG("TODO: grayscale matrix stuff");  
565                                                  return -1;                                                  return -1;
566                                          }                                          }
567    
568                                  }                                  }
569    
570    
571                                  if (vol_ver_id != 1)                                  if (vol_ver_id != 1) {
572                                  {                                          dec->quarterpel = BitstreamGetBit(bs);  // quarter_sample
573                                          dec->quarterpel = BitstreamGetBit(bs);  // quarter_sampe                                          DPRINTF(DPRINTF_HEADER,"quarterpel %i", dec->quarterpel);
                                         if (dec->quarterpel)  
                                         {  
                                                 DEBUG("IGNORED/TODO: quarter_sample");  
                                         }  
574                                  }                                  }
575                                  else                                  else
                                 {  
576                                          dec->quarterpel = 0;                                          dec->quarterpel = 0;
577                                  }  
578    
579                                  if (!BitstreamGetBit(bs))                       // complexity_estimation_disable                                  if (!BitstreamGetBit(bs))                       // complexity_estimation_disable
580                                  {                                  {
581                                          DEBUG("TODO: complexity_estimation header");                                          DPRINTF(DPRINTF_ERROR, "complexity_estimation not supported");
                                         // TODO  
582                                          return -1;                                          return -1;
583                                  }                                  }
584    
585                                  if (!BitstreamGetBit(bs))                       // resync_marker_disable                                  BitstreamSkip(bs, 1);   // resync_marker_disable
                                 {  
                                         DEBUG("IGNORED/TODO: !resync_marker_disable");  
                                         // TODO  
                                 }  
586    
587                                  if (BitstreamGetBit(bs))                // data_partitioned                                  if (BitstreamGetBit(bs))                // data_partitioned
588                                  {                                  {
589                                          DEBUG("+ data_partitioned");                                          DPRINTF(DPRINTF_ERROR, "data_partitioned not supported");
590                                          BitstreamSkip(bs, 1);           // reversible_vlc                                          BitstreamSkip(bs, 1);           // reversible_vlc
591                                  }                                  }
592    
593                                  if (vol_ver_id != 1)                                  if (vol_ver_id != 1) {
594                                  {                                          dec->newpred_enable = BitstreamGetBit(bs);
595                                          if (BitstreamGetBit(bs))                        // newpred_enable                                          if (dec->newpred_enable)        // newpred_enable
596                                          {                                          {
597                                                  DEBUG("+ newpred_enable");                                                  DPRINTF(DPRINTF_HEADER, "+ newpred_enable");
598                                                  BitstreamSkip(bs, 2);                   // requested_upstream_message_type                                                  BitstreamSkip(bs, 2);                   // requested_upstream_message_type
599                                                  BitstreamSkip(bs, 1);                   // newpred_segment_type                                                  BitstreamSkip(bs, 1);                   // newpred_segment_type
600                                          }                                          }
601                                          if (BitstreamGetBit(bs))                        // reduced_resolution_vop_enable                                          dec->reduced_resolution_enable = BitstreamGetBit(bs);
602                                            if (dec->reduced_resolution_enable)     // reduced_resolution_vop_enable
603                                          {                                          {
604                                                  DEBUG("TODO: reduced_resolution_vop_enable");                                                  DPRINTF(DPRINTF_ERROR, "reduced_resolution_vop not supported");
605                                                  // TODO                                                  //return -1;
                                                 return -1;  
606                                          }                                          }
607                                  }                                  }
608                                    else
609                                    {
610                                            dec->newpred_enable = 0;
611                                            dec->reduced_resolution_enable = 0;
612                                    }
613    
614                                  if (BitstreamGetBit(bs))        // scalability                                  if ((dec->scalability = BitstreamGetBit(bs)))   // scalability
615                                  {                                  {
616                                          // TODO                                          DPRINTF(DPRINTF_ERROR, "scalability not supported");
                                         DEBUG("TODO: scalability");  
617                                          return -1;                                          return -1;
618                                  }                                  }
619                          }                          } else                          // dec->shape == BINARY_ONLY
                         else    // dec->shape == BINARY_ONLY  
                         {  
                                 if (vol_ver_id != 1)  
620                                  {                                  {
621                                    if (vol_ver_id != 1) {
622                                          if (BitstreamGetBit(bs))        // scalability                                          if (BitstreamGetBit(bs))        // scalability
623                                          {                                          {
624                                                  // TODO                                          DPRINTF(DPRINTF_ERROR, "scalability not supported");
                                                 DEBUG("TODO: scalability");  
625                                                  return -1;                                                  return -1;
626                                          }                                          }
627                                  }                                  }
# Line 425  Line 629 
629    
630                          }                          }
631    
632                  }                          return (resize ? -3 : -2 );     /* VOL */
633                  else if (start_code == GRPOFVOP_START_CODE)  
634                  {                  } else if (start_code == GRPOFVOP_START_CODE) {
635                          // DEBUG("group_of_vop");  
636                            DPRINTF(DPRINTF_STARTCODE, "<group_of_vop>");
637    
638                          BitstreamSkip(bs, 32);                          BitstreamSkip(bs, 32);
639                          {                          {
640                                  int hours, minutes, seconds;                                  int hours, minutes, seconds;
641    
642                                  hours = BitstreamGetBits(bs, 5);                                  hours = BitstreamGetBits(bs, 5);
643                                  minutes = BitstreamGetBits(bs, 6);                                  minutes = BitstreamGetBits(bs, 6);
644                                  READ_MARKER();                                  READ_MARKER();
645                                  seconds = BitstreamGetBits(bs, 6);                                  seconds = BitstreamGetBits(bs, 6);
646                                  // DEBUG3("hms", hours, minutes, seconds);  
647                                    DPRINTF(DPRINTF_HEADER, "time %ih%im%is", hours);
648                          }                          }
649                          BitstreamSkip(bs, 1);                   // closed_gov                          BitstreamSkip(bs, 1);                   // closed_gov
650                          BitstreamSkip(bs, 1);                   // broken_link                          BitstreamSkip(bs, 1);                   // broken_link
651                  }  
652                  else if (start_code == VOP_START_CODE)                  } else if (start_code == VOP_START_CODE) {
653                  {  
654                          // DEBUG("vop_start_code");                          DPRINTF(DPRINTF_STARTCODE, "<vop>");
655    
656                          BitstreamSkip(bs, 32);                                          // vop_start_code                          BitstreamSkip(bs, 32);                                          // vop_start_code
657    
658                          coding_type = BitstreamGetBits(bs, 2);          // vop_coding_type                          coding_type = BitstreamGetBits(bs, 2);          // vop_coding_type
659                          //DEBUG1("coding_type", coding_type);                          DPRINTF(DPRINTF_HEADER, "coding_type %i", coding_type);
660    
661                          while (BitstreamGetBit(bs) != 0) ;                      // time_base  // *************************** for decode B-frame time ***********************
662                            while (BitstreamGetBit(bs) != 0)        // time_base
663                                    time_incr++;
664    
665                          READ_MARKER();                          READ_MARKER();
666    
667                          //DEBUG1("time_inc_bits", dec->time_inc_bits);                          if (dec->time_inc_bits) {
668                          //DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits));                                  time_increment = (BitstreamGetBits(bs, dec->time_inc_bits));    // vop_time_increment
669                          if (dec->time_inc_bits)                          }
670                          {  
671                                  BitstreamSkip(bs, dec->time_inc_bits);  // vop_time_increment                          DPRINTF(DPRINTF_HEADER, "time_base %i", time_incr);
672                            DPRINTF(DPRINTF_HEADER, "time_increment %i", time_increment);
673    
674                            DPRINTF(DPRINTF_TIMECODE, "%c %i:%i",
675                                    coding_type == I_VOP ? 'I' : coding_type == P_VOP ? 'P' : coding_type == B_VOP ? 'B' : 'S',
676                                    time_incr, time_increment);
677    
678                            if (coding_type != B_VOP) {
679                                    dec->last_time_base = dec->time_base;
680                                    dec->time_base += time_incr;
681                                    dec->time = time_increment;
682    
683    /*                                      dec->time_base * dec->time_inc_resolution +
684                                            time_increment;
685    */                              dec->time_pp = (uint32_t)
686                                            (dec->time_inc_resolution + dec->time - dec->last_non_b_time)%dec->time_inc_resolution;
687                                    dec->last_non_b_time = dec->time;
688                            } else {
689                                    dec->time = time_increment;
690    /*
691                                            (dec->last_time_base +
692                                             time_incr) * dec->time_inc_resolution + time_increment;
693    */
694                                    dec->time_bp = (uint32_t)
695                                            (dec->time_inc_resolution + dec->last_non_b_time - dec->time)%dec->time_inc_resolution;
696                          }                          }
697    
698                          READ_MARKER();                          READ_MARKER();
699    
700                          if (!BitstreamGetBit(bs))                                       // vop_coded                          if (!BitstreamGetBit(bs))                                       // vop_coded
701                          {                          {
702                                    DPRINTF(DPRINTF_HEADER, "vop_coded==false");
703                                  return N_VOP;                                  return N_VOP;
704                          }                          }
705    
706                          /* if (newpred_enable)                          if (dec->newpred_enable)
707                          {                          {
708                          }                                  int vop_id;
709                          */                                  int vop_id_for_prediction;
710    
711                          if (coding_type != I_VOP)                                  vop_id = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15));
712                                    DPRINTF(DPRINTF_HEADER, "vop_id %i", vop_id);
713                                    if (BitstreamGetBit(bs))        /* vop_id_for_prediction_indication */
714                          {                          {
715                                            vop_id_for_prediction = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15));
716                                            DPRINTF(DPRINTF_HEADER, "vop_id_for_prediction %i", vop_id_for_prediction);
717                                    }
718                                    READ_MARKER();
719                            }
720    
721    
722    
723                            // fix a little bug by MinChen <chenm002@163.com>
724                            if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) &&
725                                    ( (coding_type == P_VOP) || (coding_type == S_VOP && dec->sprite_enable == SPRITE_GMC) ) ) {
726                                  *rounding = BitstreamGetBit(bs);        // rounding_type                                  *rounding = BitstreamGetBit(bs);        // rounding_type
727                                  //DEBUG1("rounding", *rounding);                                  DPRINTF(DPRINTF_HEADER, "rounding %i", *rounding);
728                          }                          }
729    
730                          /* if (reduced_resolution_enable)                          if (dec->reduced_resolution_enable &&
731                                    dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR &&
732                                    (coding_type == P_VOP || coding_type == I_VOP)) {
733    
734                                    *reduced_resolution = BitstreamGetBit(bs);
735                            }
736                            else
737                          {                          {
738                                    *reduced_resolution = 0;
739                          }                          }
                         */  
740    
741                          if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)                          if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
742                          {                                  if(!(dec->sprite_enable == SPRITE_STATIC && coding_type == I_VOP)) {
743    
744                                  uint32_t width, height;                                  uint32_t width, height;
745                                  uint32_t horiz_mc_ref, vert_mc_ref;                                  uint32_t horiz_mc_ref, vert_mc_ref;
746    
# Line 497  Line 753 
753                                  vert_mc_ref = BitstreamGetBits(bs, 13);                                  vert_mc_ref = BitstreamGetBits(bs, 13);
754                                  READ_MARKER();                                  READ_MARKER();
755    
756                                  // DEBUG2("vop_width/height", width, height);                                          DPRINTF(DPRINTF_HEADER, "width %i", width);
757                                  // DEBUG2("ref             ", horiz_mc_ref, vert_mc_ref);                                          DPRINTF(DPRINTF_HEADER, "height %i", height);
758                                            DPRINTF(DPRINTF_HEADER, "horiz_mc_ref %i", horiz_mc_ref);
759                                            DPRINTF(DPRINTF_HEADER, "vert_mc_ref %i", vert_mc_ref);
760                                    }
761    
762                                  BitstreamSkip(bs, 1);                           // change_conv_ratio_disable                                  BitstreamSkip(bs, 1);                           // change_conv_ratio_disable
763                                  if (BitstreamGetBit(bs))                        // vop_constant_alpha                                  if (BitstreamGetBit(bs))                        // vop_constant_alpha
# Line 507  Line 766 
766                                  }                                  }
767                          }                          }
768    
769                            if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) {
                         if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)  
                         {  
770                                  // intra_dc_vlc_threshold                                  // intra_dc_vlc_threshold
771                                  *intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ];                                  *intra_dc_threshold =
772                                            intra_dc_threshold_table[BitstreamGetBits(bs, 3)];
773    
774                                    dec->top_field_first = 0;
775                                    dec->alternate_vertical_scan = 0;
776    
777                                    if (dec->interlacing) {
778                                            dec->top_field_first = BitstreamGetBit(bs);
779                                            DPRINTF(DPRINTF_HEADER, "interlace top_field_first %i", dec->top_field_first);
780                                            dec->alternate_vertical_scan = BitstreamGetBit(bs);
781                                            DPRINTF(DPRINTF_HEADER, "interlace alternate_vertical_scan %i", dec->alternate_vertical_scan);
782    
783                                  /* if (interlaced)                                  }
                                         {  
                                                 BitstreamSkip(bs, 1);           // top_field_first  
                                                 BitstreamSkip(bs, 1);           // alternative_vertical_scan_flag  
                                 */  
784                          }                          }
785    
786                          *quant = BitstreamGetBits(bs, dec->quant_bits);         // vop_quant                          if ((dec->sprite_enable == SPRITE_STATIC || dec->sprite_enable== SPRITE_GMC) && coding_type == S_VOP) {
                         //DEBUG1("quant", *quant);  
787    
788                          if (coding_type != I_VOP)                                  int i;
789    
790                                    for (i = 0 ; i < dec->sprite_warping_points; i++)
791                          {                          {
792                                  *fcode = BitstreamGetBits(bs, 3);                       // fcode_forward                                          int length;
793                                            int x = 0, y = 0;
794    
795                                            /* sprite code borowed from ffmpeg; thx Michael Niedermayer <michaelni@gmx.at> */
796                                            length = bs_get_spritetrajectory(bs);
797                                            if(length){
798                                                    x= BitstreamGetBits(bs, length);
799                                                    if ((x >> (length - 1)) == 0) /* if MSB not set it is negative*/
800                                                            x = - (x ^ ((1 << length) - 1));
801                          }                          }
802                                            READ_MARKER();
803    
804                          if (coding_type == B_VOP)                                          length = bs_get_spritetrajectory(bs);
805                          {                                          if(length){
806                                  // *fcode_backward = BitstreamGetBits(bs, 3);           // fcode_backward                                                  y = BitstreamGetBits(bs, length);
807                                                    if ((y >> (length - 1)) == 0) /* if MSB not set it is negative*/
808                                                            y = - (y ^ ((1 << length) - 1));
809                          }                          }
810                          return coding_type;                                          READ_MARKER();
811    
812                                            gmc_mv[i].x = x;
813                                            gmc_mv[i].y = y;
814    
815                                            DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i)", i, x, y);
816                  }                  }
817                  else if (start_code == USERDATA_START_CODE)  
818                                    if (dec->sprite_brightness_change)
819                  {                  {
820                          // DEBUG("user_data");                                          // XXX: brightness_change_factor()
                         BitstreamSkip(bs, 32);          // user_data_start_code  
821                  }                  }
822                  else  // start_code == ?                                  if (dec->sprite_enable == SPRITE_STATIC)
823                  {                  {
824                          if (BitstreamShowBits(bs, 24) == 0x000001)                                          // XXX: todo
825                                    }
826    
827                            }
828    
829                            if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1)       // vop_quant
830                                    *quant = 1;
831                            DPRINTF(DPRINTF_HEADER, "quant %i", *quant);
832    
833                            if (coding_type != I_VOP) {
834                                    *fcode_forward = BitstreamGetBits(bs, 3);       // fcode_forward
835                                    DPRINTF(DPRINTF_HEADER, "fcode_forward %i", *fcode_forward);
836                            }
837    
838                            if (coding_type == B_VOP) {
839                                    *fcode_backward = BitstreamGetBits(bs, 3);      // fcode_backward
840                                    DPRINTF(DPRINTF_HEADER, "fcode_backward %i", *fcode_backward);
841                            }
842                            if (!dec->scalability) {
843                                    if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) &&
844                                            (coding_type != I_VOP)) {
845                                            BitstreamSkip(bs, 1);   // vop_shape_coding_type
846                                    }
847                            }
848                            return coding_type;
849    
850                    } else if (start_code == USERDATA_START_CODE) {
851                            char tmp[256];
852                        int i;
853    
854                            BitstreamSkip(bs, 32);  // user_data_start_code
855    
856                            tmp[0] = BitstreamShowBits(bs, 8);
857    
858                            for(i = 1; i < 256; i++){
859                                    tmp[i] = (BitstreamShowBits(bs, 16) & 0xFF);
860    
861                                    if(tmp[i] == 0)
862                                            break;
863    
864                                    BitstreamSkip(bs, 8);
865                            }
866    
867                            DPRINTF(DPRINTF_STARTCODE, "<user_data>: %s\n", tmp);
868    
869                            if(strncmp(tmp, "DivX501b481p", 12) == 0) {
870                                    dec->packed_mode = 1;
871                                    DPRINTF(DPRINTF_STARTCODE, "packed_mode = %d\n", dec->packed_mode);
872    
873                            }
874                    } else                                  // start_code == ?
875                          {                          {
876                                  DEBUG1("*** WARNING: unknown start_code", BitstreamShowBits(bs, 32));                          if (BitstreamShowBits(bs, 24) == 0x000001) {
877                                    DPRINTF(DPRINTF_STARTCODE, "<unknown: %x>", BitstreamShowBits(bs, 32));
878                          }                          }
879                          BitstreamSkip(bs, 8);                          BitstreamSkip(bs, 8);
880                  }                  }
881          }          }
882          while ((BitstreamPos(bs) >> 3) < bs->length);          while ((BitstreamPos(bs) >> 3) < bs->length);
883    
884          DEBUG("*** WARNING: no vop_start_code found");          //DPRINTF("*** WARNING: no vop_start_code found");
885          return -1; /* ignore it */          return -1; /* ignore it */
886  }  }
887    
888    
889  /* write custom quant matrix */  /* write custom quant matrix */
890    
891  static void bs_put_matrix(Bitstream * bs, const int16_t *matrix)  static void
892    bs_put_matrix(Bitstream * bs,
893                              const int16_t * matrix)
894  {  {
895          int i, j;          int i, j;
896          const int last = matrix[scan_tables[0][63]];          const int last = matrix[scan_tables[0][63]];
897    
898          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--);
899    
900          for (i = 0; i <= j; i++)          for (i = 0; i <= j; i++) {
         {  
901                  BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8);                  BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8);
902          }          }
903    
904          if (j < 63)          if (j < 63) {
         {  
905                  BitstreamPutBits(bs, 0, 8);                  BitstreamPutBits(bs, 0, 8);
906          }          }
907  }  }
# Line 579  Line 910 
910  /*  /*
911          write vol header          write vol header
912  */  */
913  void BitstreamWriteVolHeader(Bitstream * const bs,  void
914                                                  const int width,  BitstreamWriteVolHeader(Bitstream * const bs,
915                                                  const int height,                                                  const MBParam * pParam,
916                                                  const int quant_type)                                                  const FRAMEINFO * const frame)
917  {  {
918            int vol_ver_id=1;
919    
920            if ( (pParam->m_quarterpel) || (frame->global_flags & XVID_GMC) )
921                    vol_ver_id = 2;
922    
923          // video object_start_code & vo_id          // video object_start_code & vo_id
924      BitstreamPad(bs);      BitstreamPad(bs);
925          BitstreamPutBits(bs, VO_START_CODE, 27);          BitstreamPutBits(bs, VO_START_CODE, 27);
# Line 595  Line 931 
931    
932          BitstreamPutBit(bs, 0);                         // random_accessible_vol          BitstreamPutBit(bs, 0);                         // random_accessible_vol
933          BitstreamPutBits(bs, 0, 8);                     // video_object_type_indication          BitstreamPutBits(bs, 0, 8);                     // video_object_type_indication
934    
935            if (vol_ver_id == 1)
936            {
937          BitstreamPutBit(bs, 0);                         // is_object_layer_identified (0=not given)          BitstreamPutBit(bs, 0);                         // is_object_layer_identified (0=not given)
938            }
939            else
940            {
941                    BitstreamPutBit(bs, 1);         // is_object_layer_identified
942                    BitstreamPutBits(bs, vol_ver_id, 4);    // vol_ver_id == 2
943                    BitstreamPutBits(bs, 4, 3); // vol_ver_priority (1==lowest, 7==highest) ??
944            }
945    
946          BitstreamPutBits(bs, 1, 4);                     // aspect_ratio_info (1=1:1)          BitstreamPutBits(bs, 1, 4);                     // aspect_ratio_info (1=1:1)
947          BitstreamPutBit(bs, 0);                         // vol_control_parameters (0=not given)  
948            BitstreamPutBit(bs, 1); // vol_control_parameters
949            BitstreamPutBits(bs, 1, 2);     // chroma_format 1="4:2:0"
950    
951            if (pParam->max_bframes > 0) {
952                    BitstreamPutBit(bs, 0); // low_delay
953            } else
954            {
955                    BitstreamPutBit(bs, 1); // low_delay
956            }
957            BitstreamPutBit(bs, 0); // vbv_parameters (0=not given)
958    
959          BitstreamPutBits(bs, 0, 2);                     // video_object_layer_shape (0=rectangular)          BitstreamPutBits(bs, 0, 2);                     // video_object_layer_shape (0=rectangular)
960    
961          WRITE_MARKER();          WRITE_MARKER();
962    
963          /* time_increment_resolution; ignored by current decore versions          /* time_inc_resolution; ignored by current decore versions
964                  eg. 2fps                res=2           inc=1                  eg. 2fps                res=2           inc=1
965                          25fps           res=25          inc=1                          25fps           res=25          inc=1
966                          29.97fps        res=30000       inc=1001                          29.97fps        res=30000       inc=1001
967          */          */
968          BitstreamPutBits(bs, 2, 16);          BitstreamPutBits(bs, pParam->fbase, 16);
969    
970          WRITE_MARKER();          WRITE_MARKER();
971    
972          // fixed_vop_rate          BitstreamPutBit(bs, 1);         // fixed_vop_rate = 1
973          BitstreamPutBit(bs, 0);          BitstreamPutBits(bs, pParam->fincr, log2bin(pParam->fbase));    // fixed_vop_time_increment
   
         // fixed_time_increment: value=nth_of_sec, nbits = log2(resolution)  
         // BitstreamPutBits(bs, 0, 15);  
974    
975          WRITE_MARKER();          WRITE_MARKER();
976          BitstreamPutBits(bs, width, 13);                // width          BitstreamPutBits(bs, pParam->width, 13);        // width
977          WRITE_MARKER();          WRITE_MARKER();
978          BitstreamPutBits(bs, height, 13);               // height          BitstreamPutBits(bs, pParam->height, 13);       // height
979          WRITE_MARKER();          WRITE_MARKER();
980    
981          BitstreamPutBit(bs, 0);         // interlace          BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING);    // interlace
982          BitstreamPutBit(bs, 1);         // obmc_disable (overlapped block motion compensation)          BitstreamPutBit(bs, 1);         // obmc_disable (overlapped block motion compensation)
983          BitstreamPutBit(bs, 0);         // sprite_enable  
984          BitstreamPutBit(bs, 0);         // not_in_bit          if (vol_ver_id != 1)
985            {       if (frame->global_flags & XVID_GMC)
986                    {       BitstreamPutBits(bs, 2, 2);             // sprite_enable=='GMC'
987                            BitstreamPutBits(bs, 2, 6);             // no_of_sprite_warping_points
988                            BitstreamPutBits(bs, 3, 2);             // sprite_warping_accuracy 0==1/2, 1=1/4, 2=1/8, 3=1/16
989                            BitstreamPutBit(bs, 0);                 // sprite_brightness_change (not supported)
990    
991    /* currently we use no_of_sprite_warping_points==2, sprite_warping_accuracy==3
992       for DivX5 compatability */
993    
994                    } else
995                            BitstreamPutBits(bs, 0, 2);             // sprite_enable==off
996            }
997            else
998                    BitstreamPutBit(bs, 0);         // sprite_enable==off
999    
1000            BitstreamPutBit(bs, 0);         // not_8_bit
1001    
1002          // quant_type   0=h.263  1=mpeg4(quantizer tables)          // quant_type   0=h.263  1=mpeg4(quantizer tables)
1003          BitstreamPutBit(bs, quant_type);          BitstreamPutBit(bs, pParam->m_quant_type);
1004    
1005          if (quant_type)          if (pParam->m_quant_type) {
         {  
1006                  BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat                  BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat
1007                  if (get_intra_matrix_status())                  if (get_intra_matrix_status()) {
                 {  
1008                          bs_put_matrix(bs, get_intra_matrix());                          bs_put_matrix(bs, get_intra_matrix());
1009                  }                  }
1010    
1011                  BitstreamPutBit(bs, get_inter_matrix_status());         // load_inter_quant_mat                  BitstreamPutBit(bs, get_inter_matrix_status());         // load_inter_quant_mat
1012                  if (get_inter_matrix_status())                  if (get_inter_matrix_status()) {
                 {  
1013                          bs_put_matrix(bs, get_inter_matrix());                          bs_put_matrix(bs, get_inter_matrix());
1014                  }                  }
1015    
1016          }          }
1017    
1018            if (vol_ver_id != 1) {
1019                    if (pParam->m_quarterpel)
1020                            BitstreamPutBit(bs, 1);         //  quarterpel
1021                    else
1022                            BitstreamPutBit(bs, 0);         // no quarterpel
1023            }
1024    
1025          BitstreamPutBit(bs, 1);         // complexity_estimation_disable          BitstreamPutBit(bs, 1);         // complexity_estimation_disable
1026          BitstreamPutBit(bs, 1);         // resync_marker_disable          BitstreamPutBit(bs, 1);         // resync_marker_disable
1027          BitstreamPutBit(bs, 0);         // data_partitioned          BitstreamPutBit(bs, 0);         // data_partitioned
1028    
1029            if (vol_ver_id != 1)
1030            {
1031                    BitstreamPutBit(bs, 0);         // newpred_enable
1032                    BitstreamPutBit(bs, 0);         // reduced_resolution_vop_enabled
1033            }
1034    
1035          BitstreamPutBit(bs, 0);         // scalability          BitstreamPutBit(bs, 0);         // scalability
1036    
1037  }  }
1038    
1039    
1040  /*  /*
1041    write vop header    write vop header
   
   NOTE: doesnt handle bother with time_base & time_inc  
   time_base = n seconds since last resync (eg. last iframe)  
   time_inc = nth of a second since last resync  
   (decoder uses these values to determine precise time since last resync)  
1042  */  */
1043  void BitstreamWriteVopHeader(Bitstream * const bs,  void
1044                            VOP_TYPE prediction_type,  BitstreamWriteVopHeader(Bitstream * const bs,
1045                            const int rounding_type,                                                  const MBParam * pParam,
1046                            const uint32_t quant,                                                  const FRAMEINFO * const frame,
1047                            const uint32_t fcode)                                                  int vop_coded)
1048  {  {
1049            uint32_t i;
1050    
1051      BitstreamPad(bs);      BitstreamPad(bs);
1052      BitstreamPutBits(bs, VOP_START_CODE, 32);      BitstreamPutBits(bs, VOP_START_CODE, 32);
1053    
1054      BitstreamPutBits(bs, prediction_type, 2);          BitstreamPutBits(bs, frame->coding_type, 2);
1055            DPRINTF(DPRINTF_HEADER, "coding_type = %i", frame->coding_type);
1056    
1057          // time_base = 0  write n x PutBit(1), PutBit(0)          for (i = 0; i < frame->seconds; i++) {
1058          BitstreamPutBits(bs, 0, 1);                  BitstreamPutBit(bs, 1);
1059            }
1060            BitstreamPutBit(bs, 0);
1061    
1062          WRITE_MARKER();          WRITE_MARKER();
1063    
1064          // time_increment: value=nth_of_sec, nbits = log2(resolution)          // time_increment: value=nth_of_sec, nbits = log2(resolution)
1065          BitstreamPutBits(bs, 1, 1);  
1066            BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase));
1067            /*DPRINTF("[%i:%i] %c", frame->seconds, frame->ticks,
1068                            frame->coding_type == I_VOP ? 'I' : frame->coding_type ==
1069                            P_VOP ? 'P' : 'B');*/
1070    
1071          WRITE_MARKER();          WRITE_MARKER();
1072    
1073            if (!vop_coded) {
1074                    BitstreamPutBits(bs, 0, 1);
1075                    return;
1076            }
1077    
1078          BitstreamPutBits(bs, 1, 1);                             // vop_coded          BitstreamPutBits(bs, 1, 1);                             // vop_coded
1079    
1080          if (prediction_type != I_VOP)          if ( (frame->coding_type == P_VOP) || (frame->coding_type == S_VOP) )
1081                  BitstreamPutBits(bs, rounding_type, 1);                  BitstreamPutBits(bs, frame->rounding_type, 1);
1082    
1083          BitstreamPutBits(bs, 0, 3);                             // intra_dc_vlc_threshold          BitstreamPutBits(bs, 0, 3);                             // intra_dc_vlc_threshold
1084    
1085          BitstreamPutBits(bs, quant, 5);                 // quantizer          if (frame->global_flags & XVID_INTERLACING) {
1086                    BitstreamPutBit(bs, (frame->global_flags & XVID_TOPFIELDFIRST));
1087                    BitstreamPutBit(bs, (frame->global_flags & XVID_ALTERNATESCAN));
1088            }
1089    
1090            if (frame->coding_type == S_VOP) {
1091                    if (1)  {               // no_of_sprite_warping_points>=1
1092                            if (pParam->m_quarterpel)
1093                                    bs_put_spritetrajectory(bs, frame->GMC_MV.x/2 ); // du[0]
1094                            else
1095                                    bs_put_spritetrajectory(bs, frame->GMC_MV.x ); // du[0]
1096                            WRITE_MARKER();
1097    
1098                            if (pParam->m_quarterpel)
1099                                    bs_put_spritetrajectory(bs, frame->GMC_MV.y/2 ); // dv[0]
1100                            else
1101                                    bs_put_spritetrajectory(bs, frame->GMC_MV.y ); // dv[0]
1102                            WRITE_MARKER();
1103    
1104    
1105                            if (pParam->m_quarterpel)
1106                            {
1107                                    DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i) *QPEL*", 0, frame->GMC_MV.x/2, frame->GMC_MV.y/2);
1108                            }
1109                            else
1110                            {
1111                                    DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i)", 0, frame->GMC_MV.x, frame->GMC_MV.y);
1112                            }
1113    
1114                    }
1115    /* GMC is halfpel in bitstream, even though GMC_MV was pseudo-qpel (2*halfpel) */
1116    
1117                    if (2) {                // no_of_sprite_warping_points>=2 (for DivX5 compat)
1118                            bs_put_spritetrajectory(bs, 0 );
1119                            WRITE_MARKER();
1120                            bs_put_spritetrajectory(bs, 0 );
1121                            WRITE_MARKER();
1122                    }
1123                    // no support for brightness_change!
1124            }
1125    
1126            BitstreamPutBits(bs, frame->quant, 5);  // quantizer
1127    
1128            if (frame->coding_type != I_VOP)
1129                    BitstreamPutBits(bs, frame->fcode, 3);  // forward_fixed_code
1130    
1131            if (frame->coding_type == B_VOP)
1132                    BitstreamPutBits(bs, frame->bcode, 3);  // backward_fixed_code
1133    
1134    }
1135    
1136    void
1137    BitstreamWriteUserData(Bitstream * const bs,
1138                                                    uint8_t * data,
1139                                                    const int length)
1140    {
1141            int i;
1142    
1143            BitstreamPad(bs);
1144            BitstreamPutBits(bs, USERDATA_START_CODE, 32);
1145    
1146            for (i = 0; i < length; i++) {
1147                    BitstreamPutBits(bs, data[i], 8);
1148            }
1149    
         if (prediction_type != I_VOP)  
                 BitstreamPutBits(bs, fcode, 3);         // fixed_code = [1,4]  
1150  }  }

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

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