[svn] / branches / dev-api-4 / xvidcore / src / bitstream / bitstream.c Repository:
ViewVC logotype

Diff of /branches/dev-api-4/xvidcore/src/bitstream/bitstream.c

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

trunk/xvidcore/src/bitstream/bitstream.c revision 157, Fri May 3 08:37:55 2002 UTC branches/dev-api-4/xvidcore/src/bitstream/bitstream.c revision 1053, Mon Jun 9 01:25:19 2003 UTC
# Line 41  Line 41 
41    *                                                                                                                                                        *    *                                                                                                                                                        *
42    *  Revision history:                                                         *    *  Revision history:                                                         *
43    *                                                                                                                                                        *    *                                                                                                                                                        *
44    *  01.05.2002 added BVOP support to BitstreamWriteVopHeader    *  05.01.2003 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>      *    *  15.04.2002 rewrite log2bin use asm386  By MinChen <chenm001@163.com>      *
59    *  26.03.2002 interlacing support                                                                                        *    *  26.03.2002 interlacing support                                                                                        *
60    *  03.03.2002 qmatrix writing                                                                                            *    *  03.03.2002 qmatrix writing                                                                                            *
# Line 53  Line 66 
66    ******************************************************************************/    ******************************************************************************/
67    
68    
69    #include <string.h>
70    #include <stdio.h>
71    
72  #include "bitstream.h"  #include "bitstream.h"
73  #include "zigzag.h"  #include "zigzag.h"
74  #include "../quant/quant_matrix.h"  #include "../quant/quant_matrix.h"
75    #include "mbcoding.h"
76    
77    
78  static int __inline log2bin(int value)  static uint32_t __inline
79    log2bin(uint32_t value)
80  {  {
81  /* Changed by Chenm001 */  /* Changed by Chenm001 */
82  #ifndef WIN32  #if !defined(_MSC_VER)
83          int n = 0;          int n = 0;
84          while (value)  
85          {          while (value) {
86                  value >>= 1;                  value >>= 1;
87                  n++;                  n++;
88          }          }
# Line 78  Line 96 
96  }  }
97    
98    
99  static const uint32_t intra_dc_threshold_table[] =  static const uint32_t intra_dc_threshold_table[] = {
 {  
100          32,     /* never use */          32,     /* never use */
101          13,          13,
102          15,          15,
# Line 91  Line 108 
108  };  };
109    
110    
111  void bs_get_matrix(Bitstream * bs, uint8_t * matrix)  void
112    bs_get_matrix(Bitstream * bs,
113                              uint8_t * matrix)
114  {  {
115          int i = 0;          int i = 0;
116      int last, value = 0;      int last, value = 0;
117    
118      do          do {
         {  
119                  last = value;                  last = value;
120          value = BitstreamGetBits(bs, 8);          value = BitstreamGetBits(bs, 8);
121          matrix[ scan_tables[0][i++] ]  = value;          matrix[ scan_tables[0][i++] ]  = value;
122      }      }
123      while (value != 0 && i < 64);      while (value != 0 && i < 64);
124            i--;    /* fix little bug at coeff not full */
125    
126          while (i < 64)          while (i < 64) {
         {  
127                  matrix[ scan_tables[0][i++] ]  = last;                  matrix[ scan_tables[0][i++] ]  = last;
128          }          }
129  }  }
130    
131    
132    
133  /*  /*
134  decode headers   * for PVOP addbits == fcode - 1
135  returns coding_type, or -1 if error   * for BVOP addbits == max(fcode,bcode) - 1
136     * returns mbpos
137  */  */
138    int
139    read_video_packet_header(Bitstream *bs,
140                                                    DECODER * dec,
141                                                    const int addbits,
142                                                    int * quant,
143                                                    int * fcode_forward,
144                                                    int  * fcode_backward,
145                                                    int * intra_dc_threshold)
146    {
147            int startcode_bits = NUMBITS_VP_RESYNC_MARKER + addbits;
148            int mbnum_bits = log2bin(dec->mb_width *  dec->mb_height - 1);
149            int mbnum;
150            int hec = 0;
151    
152            BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs));
153            BitstreamSkip(bs, startcode_bits);
154    
155            DPRINTF(XVID_DEBUG_STARTCODE, "<video_packet_header>\n");
156    
157  int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, uint32_t * quant, uint32_t * fcode_forward, uint32_t * fcode_backward, uint32_t * intra_dc_threshold)          if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)
158  {  {
159          uint32_t vol_ver_id;                  hec = BitstreamGetBit(bs);              /* header_extension_code */
160          static uint32_t time_increment_resolution;                  if (hec && !(dec->sprite_enable == SPRITE_STATIC /* && current_coding_type = I_VOP */))
161          uint32_t coding_type;                  {
162          uint32_t start_code;                          BitstreamSkip(bs, 13);                  /* vop_width */
163          uint32_t time_incr=0;                          READ_MARKER();
164          int32_t  time_increment;                          BitstreamSkip(bs, 13);                  /* vop_height */
165                            READ_MARKER();
166                            BitstreamSkip(bs, 13);                  /* vop_horizontal_mc_spatial_ref */
167                            READ_MARKER();
168                            BitstreamSkip(bs, 13);                  /* vop_vertical_mc_spatial_ref */
169                            READ_MARKER();
170                    }
171            }
172    
173          do          mbnum = BitstreamGetBits(bs, mbnum_bits);               /* macroblock_number */
174            DPRINTF(XVID_DEBUG_HEADER, "mbnum %i\n", mbnum);
175    
176            if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)
177          {          {
178                  BitstreamByteAlign(bs);                  *quant = BitstreamGetBits(bs, dec->quant_bits); /* quant_scale */
179                  start_code = BitstreamShowBits(bs, 32);                  DPRINTF(XVID_DEBUG_HEADER, "quant %i\n", *quant);
180            }
181    
182            if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR)
183                    hec = BitstreamGetBit(bs);              /* header_extension_code */
184    
185    
186            DPRINTF(XVID_DEBUG_HEADER, "header_extension_code %i\n", hec);
187            if (hec)
188            {
189                    int time_base;
190                    int time_increment;
191                    int coding_type;
192    
193                    for (time_base=0; BitstreamGetBit(bs)!=0; time_base++);         /* modulo_time_base */
194                    READ_MARKER();
195                    if (dec->time_inc_bits)
196                            time_increment = (BitstreamGetBits(bs, dec->time_inc_bits));    /* vop_time_increment */
197                    READ_MARKER();
198                    DPRINTF(XVID_DEBUG_HEADER,"time %i:%i\n", time_base, time_increment);
199    
200                    coding_type = BitstreamGetBits(bs, 2);
201                    DPRINTF(XVID_DEBUG_HEADER,"coding_type %i\n", coding_type);
202    
203                  if (start_code == VISOBJSEQ_START_CODE)                  if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)
204                  {                  {
205                          // DEBUG("visual_object_sequence");                          BitstreamSkip(bs, 1);   /* change_conv_ratio_disable */
206                          BitstreamSkip(bs, 32);                          // visual_object_sequence_start_code                          if (coding_type != I_VOP)
207                          BitstreamSkip(bs, 8);                                   // profile_and_level_indication                                  BitstreamSkip(bs, 1);   /* vop_shape_coding_type */
208                  }                  }
209                  else if (start_code == VISOBJSEQ_STOP_CODE)  
210                    if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)
211                    {
212                            *intra_dc_threshold = intra_dc_threshold_table[BitstreamGetBits(bs, 3)];
213    
214                            if (dec->sprite_enable == SPRITE_GMC && coding_type == S_VOP &&
215                                    dec->sprite_warping_points > 0)
216                  {                  {
217                          BitstreamSkip(bs, 32);                          // visual_object_sequence_stop_code                                  /* TODO: sprite trajectory */
218                  }                  }
219                  else if (start_code == VISOBJ_START_CODE)                          if (dec->reduced_resolution_enable &&
220                                    dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR &&
221                                    (coding_type == P_VOP || coding_type == I_VOP))
222                  {                  {
223                          // DEBUG("visual_object");                                  BitstreamSkip(bs, 1); /* XXX: vop_reduced_resolution */
224                          BitstreamSkip(bs,32);                                   // visual_object_start_code                          }
225                          if (BitstreamGetBit(bs))                                // is_visual_object_identified  
226                            if (coding_type != I_VOP && fcode_forward)
227                          {                          {
228                                  vol_ver_id = BitstreamGetBits(bs,4);    // visual_object_ver_id                                  *fcode_forward = BitstreamGetBits(bs, 3);
229                                  BitstreamSkip(bs, 3);                           // visual_object_priority                                  DPRINTF(XVID_DEBUG_HEADER,"fcode_forward %i\n", *fcode_forward);
230                          }                          }
231                          else  
232                            if (coding_type == B_VOP && fcode_backward)
233                          {                          {
234                                  vol_ver_id = 1;                                  *fcode_backward = BitstreamGetBits(bs, 3);
235                                    DPRINTF(XVID_DEBUG_HEADER,"fcode_backward %i\n", *fcode_backward);
236                            }
237                    }
238    
239                          }                          }
240    
241                          if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO)      // visual_object_type          if (dec->newpred_enable)
242                          {                          {
243                                  DEBUG("visual_object_type != video");                  int vop_id;
244                                  return -1;                  int vop_id_for_prediction;
245    
246                    vop_id = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15));
247                    DPRINTF(XVID_DEBUG_HEADER, "vop_id %i\n", vop_id);
248                    if (BitstreamGetBit(bs))        /* vop_id_for_prediction_indication */
249                    {
250                            vop_id_for_prediction = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15));
251                            DPRINTF(XVID_DEBUG_HEADER, "vop_id_for_prediction %i\n", vop_id_for_prediction);
252                    }
253                    READ_MARKER();
254                          }                          }
                         BitstreamSkip(bs, 4);  
255    
256                          // video_signal_type          return mbnum;
257    }
258    
259    
260    
261    
262    /* vol estimation header */
263    static void
264    read_vol_complexity_estimation_header(Bitstream * bs, DECODER * dec)
265    {
266            ESTIMATION * e = &dec->estimation;
267    
268                          if (BitstreamGetBit(bs))                                // video_signal_type          e->method = BitstreamGetBits(bs, 2);    /* estimation_method */
269            DPRINTF(XVID_DEBUG_HEADER,"+ complexity_estimation_header; method=%i\n", e->method);
270    
271            if (e->method == 0 || e->method == 1)
272                          {                          {
273                                  DEBUG("+ video_signal_type");                  if (!BitstreamGetBit(bs))               /* shape_complexity_estimation_disable */
                                 BitstreamSkip(bs, 3);                           // video_format  
                                 BitstreamSkip(bs, 1);                           // video_range  
                                 if (BitstreamGetBit(bs))                        // color_description  
274                                  {                                  {
275                                          DEBUG("+ color_description");                          e->opaque = BitstreamGetBit(bs);                /* opaque */
276                                          BitstreamSkip(bs, 8);                   // color_primaries                          e->transparent = BitstreamGetBit(bs);           /* transparent */
277                                          BitstreamSkip(bs, 8);                   // transfer_characteristics                          e->intra_cae = BitstreamGetBit(bs);             /* intra_cae */
278                                          BitstreamSkip(bs, 8);                   // matrix_coefficients                          e->inter_cae = BitstreamGetBit(bs);             /* inter_cae */
279                            e->no_update = BitstreamGetBit(bs);             /* no_update */
280                            e->upsampling = BitstreamGetBit(bs);            /* upsampling */
281                                  }                                  }
282    
283                    if (!BitstreamGetBit(bs))       /* texture_complexity_estimation_set_1_disable */
284                    {
285                            e->intra_blocks = BitstreamGetBit(bs);          /* intra_blocks */
286                            e->inter_blocks = BitstreamGetBit(bs);          /* inter_blocks */
287                            e->inter4v_blocks = BitstreamGetBit(bs);                /* inter4v_blocks */
288                            e->not_coded_blocks = BitstreamGetBit(bs);              /* not_coded_blocks */
289                          }                          }
290                  }                  }
291                  else if ((start_code & ~0x1f) == VIDOBJ_START_CODE)  
292            READ_MARKER();
293    
294            if (!BitstreamGetBit(bs))               /* texture_complexity_estimation_set_2_disable */
295                  {                  {
296                          BitstreamSkip(bs, 32);          // video_object_start_code                  e->dct_coefs = BitstreamGetBit(bs);             /* dct_coefs */
297                    e->dct_lines = BitstreamGetBit(bs);             /* dct_lines */
298                    e->vlc_symbols = BitstreamGetBit(bs);           /* vlc_symbols */
299                    e->vlc_bits = BitstreamGetBit(bs);              /* vlc_bits */
300            }
301    
302            if (!BitstreamGetBit(bs))               /* motion_compensation_complexity_disable */
303            {
304                    e->apm = BitstreamGetBit(bs);           /* apm */
305                    e->npm = BitstreamGetBit(bs);           /* npm */
306                    e->interpolate_mc_q = BitstreamGetBit(bs);              /* interpolate_mc_q */
307                    e->forw_back_mc_q = BitstreamGetBit(bs);                /* forw_back_mc_q */
308                    e->halfpel2 = BitstreamGetBit(bs);              /* halfpel2 */
309                    e->halfpel4 = BitstreamGetBit(bs);              /* halfpel4 */
310                  }                  }
311                  else if ((start_code & ~0xf) == VIDOBJLAY_START_CODE)  
312            READ_MARKER();
313    
314            if (e->method == 1)
315                  {                  {
316                          // DEBUG("video_object_layer");                  if (!BitstreamGetBit(bs))       /* version2_complexity_estimation_disable */
317                          BitstreamSkip(bs, 32);                                  // video_object_layer_start_code                  {
318                            e->sadct = BitstreamGetBit(bs);         /* sadct */
319                            e->quarterpel = BitstreamGetBit(bs);            /* quarterpel */
320                    }
321            }
322    }
323    
324    
325    /* vop estimation header */
326    static void
327    read_vop_complexity_estimation_header(Bitstream * bs, DECODER * dec, int coding_type)
328    {
329            ESTIMATION * e = &dec->estimation;
330    
331            if (e->method == 0 || e->method == 1)
332            {
333                    if (coding_type == I_VOP) {
334                            if (e->opaque)          BitstreamSkip(bs, 8);   /* dcecs_opaque */
335                            if (e->transparent) BitstreamSkip(bs, 8);       /* */
336                            if (e->intra_cae)       BitstreamSkip(bs, 8);   /* */
337                            if (e->inter_cae)       BitstreamSkip(bs, 8);   /* */
338                            if (e->no_update)       BitstreamSkip(bs, 8);   /* */
339                            if (e->upsampling)      BitstreamSkip(bs, 8);   /* */
340                            if (e->intra_blocks) BitstreamSkip(bs, 8);      /* */
341                            if (e->not_coded_blocks) BitstreamSkip(bs, 8);  /* */
342                            if (e->dct_coefs)       BitstreamSkip(bs, 8);   /* */
343                            if (e->dct_lines)       BitstreamSkip(bs, 8);   /* */
344                            if (e->vlc_symbols) BitstreamSkip(bs, 8);       /* */
345                            if (e->vlc_bits)        BitstreamSkip(bs, 8);   /* */
346                            if (e->sadct)           BitstreamSkip(bs, 8);   /* */
347                    }
348    
349                    if (coding_type == P_VOP) {
350                            if (e->opaque) BitstreamSkip(bs, 8);            /* */
351                            if (e->transparent) BitstreamSkip(bs, 8);       /* */
352                            if (e->intra_cae)       BitstreamSkip(bs, 8);   /* */
353                            if (e->inter_cae)       BitstreamSkip(bs, 8);   /* */
354                            if (e->no_update)       BitstreamSkip(bs, 8);   /* */
355                            if (e->upsampling) BitstreamSkip(bs, 8);        /* */
356                            if (e->intra_blocks) BitstreamSkip(bs, 8);      /* */
357                            if (e->not_coded_blocks)        BitstreamSkip(bs, 8);   /* */
358                            if (e->dct_coefs)       BitstreamSkip(bs, 8);   /* */
359                            if (e->dct_lines)       BitstreamSkip(bs, 8);   /* */
360                            if (e->vlc_symbols) BitstreamSkip(bs, 8);       /* */
361                            if (e->vlc_bits)        BitstreamSkip(bs, 8);   /* */
362                            if (e->inter_blocks) BitstreamSkip(bs, 8);      /* */
363                            if (e->inter4v_blocks) BitstreamSkip(bs, 8);    /* */
364                            if (e->apm)                     BitstreamSkip(bs, 8);   /* */
365                            if (e->npm)                     BitstreamSkip(bs, 8);   /* */
366                            if (e->forw_back_mc_q) BitstreamSkip(bs, 8);    /* */
367                            if (e->halfpel2)        BitstreamSkip(bs, 8);   /* */
368                            if (e->halfpel4)        BitstreamSkip(bs, 8);   /* */
369                            if (e->sadct)           BitstreamSkip(bs, 8);   /* */
370                            if (e->quarterpel)      BitstreamSkip(bs, 8);   /* */
371                    }
372                    if (coding_type == B_VOP) {
373                            if (e->opaque)          BitstreamSkip(bs, 8);   /* */
374                            if (e->transparent)     BitstreamSkip(bs, 8);   /* */
375                            if (e->intra_cae)       BitstreamSkip(bs, 8);   /* */
376                            if (e->inter_cae)       BitstreamSkip(bs, 8);   /* */
377                            if (e->no_update)       BitstreamSkip(bs, 8);   /* */
378                            if (e->upsampling)      BitstreamSkip(bs, 8);   /* */
379                            if (e->intra_blocks) BitstreamSkip(bs, 8);      /* */
380                            if (e->not_coded_blocks) BitstreamSkip(bs, 8);  /* */
381                            if (e->dct_coefs)       BitstreamSkip(bs, 8);   /* */
382                            if (e->dct_lines)       BitstreamSkip(bs, 8);   /* */
383                            if (e->vlc_symbols)     BitstreamSkip(bs, 8);   /* */
384                            if (e->vlc_bits)        BitstreamSkip(bs, 8);   /* */
385                            if (e->inter_blocks) BitstreamSkip(bs, 8);      /* */
386                            if (e->inter4v_blocks) BitstreamSkip(bs, 8);    /* */
387                            if (e->apm)                     BitstreamSkip(bs, 8);   /* */
388                            if (e->npm)                     BitstreamSkip(bs, 8);   /* */
389                            if (e->forw_back_mc_q) BitstreamSkip(bs, 8);    /* */
390                            if (e->halfpel2)        BitstreamSkip(bs, 8);   /* */
391                            if (e->halfpel4)        BitstreamSkip(bs, 8);   /* */
392                            if (e->interpolate_mc_q) BitstreamSkip(bs, 8);  /* */
393                            if (e->sadct)           BitstreamSkip(bs, 8);   /* */
394                            if (e->quarterpel)      BitstreamSkip(bs, 8);   /* */
395                    }
396    
397                    if (coding_type == S_VOP && dec->sprite_enable == SPRITE_STATIC) {
398                            if (e->intra_blocks) BitstreamSkip(bs, 8);      /* */
399                            if (e->not_coded_blocks) BitstreamSkip(bs, 8);  /* */
400                            if (e->dct_coefs)       BitstreamSkip(bs, 8);   /* */
401                            if (e->dct_lines)       BitstreamSkip(bs, 8);   /* */
402                            if (e->vlc_symbols)     BitstreamSkip(bs, 8);   /* */
403                            if (e->vlc_bits)        BitstreamSkip(bs, 8);   /* */
404                            if (e->inter_blocks) BitstreamSkip(bs, 8);      /* */
405                            if (e->inter4v_blocks)  BitstreamSkip(bs, 8);   /* */
406                            if (e->apm)                     BitstreamSkip(bs, 8);   /* */
407                            if (e->npm)                     BitstreamSkip(bs, 8);   /* */
408                            if (e->forw_back_mc_q)  BitstreamSkip(bs, 8);   /* */
409                            if (e->halfpel2)        BitstreamSkip(bs, 8);   /* */
410                            if (e->halfpel4)        BitstreamSkip(bs, 8);   /* */
411                            if (e->interpolate_mc_q) BitstreamSkip(bs, 8);  /* */
412                    }
413            }
414    }
415    
                         BitstreamSkip(bs, 1);                                   // random_accessible_vol  
416    
417                          // video_object_type_indication  
418                          if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE &&  
419                                  BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE &&  
420                                  BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN &&  /*
421                                  BitstreamShowBits(bs, 8) != 0)          // BUGGY DIVX  decode headers
422    returns coding_type, or -1 if error
423    */
424    
425    #define VIDOBJ_START_CODE_MASK          0x0000001f
426    #define VIDOBJLAY_START_CODE_MASK       0x0000000f
427    
428    int
429    BitstreamReadHeaders(Bitstream * bs,
430                                             DECODER * dec,
431                                             uint32_t * rounding,
432                                             uint32_t * reduced_resolution,
433                                             uint32_t * quant,
434                                             uint32_t * fcode_forward,
435                                             uint32_t * fcode_backward,
436                                             uint32_t * intra_dc_threshold,
437                                             WARPPOINTS *gmc_warp)
438                          {                          {
439                                  DEBUG1("video_object_type_indication not supported", BitstreamShowBits(bs, 8));          uint32_t vol_ver_id;
440            uint32_t coding_type;
441            uint32_t start_code;
442            uint32_t time_incr = 0;
443            int32_t time_increment = 0;
444            int resize = 0;
445    
446            do {
447    
448                    BitstreamByteAlign(bs);
449                    start_code = BitstreamShowBits(bs, 32);
450    
451                    if (start_code == VISOBJSEQ_START_CODE) {
452    
453                            int profile;
454    
455                            DPRINTF(XVID_DEBUG_STARTCODE, "<visual_object_sequence>\n");
456    
457                            BitstreamSkip(bs, 32);  /* visual_object_sequence_start_code */
458                            profile = BitstreamGetBits(bs, 8);      /* profile_and_level_indication */
459    
460                            DPRINTF(XVID_DEBUG_HEADER, "profile_and_level_indication %i\n", profile);
461    
462                    } else if (start_code == VISOBJSEQ_STOP_CODE) {
463    
464                            BitstreamSkip(bs, 32);  /* visual_object_sequence_stop_code */
465    
466                            DPRINTF(XVID_DEBUG_STARTCODE, "</visual_object_sequence>\n");
467    
468                    } else if (start_code == VISOBJ_START_CODE) {
469                            int visobj_ver_id;
470    
471                            DPRINTF(XVID_DEBUG_STARTCODE, "<visual_object>\n");
472    
473                            BitstreamSkip(bs, 32);  /* visual_object_start_code */
474                            if (BitstreamGetBit(bs))        /* is_visual_object_identified */
475                            {
476                                    visobj_ver_id = BitstreamGetBits(bs, 4);        /* visual_object_ver_id */
477                                    DPRINTF(XVID_DEBUG_HEADER,"visobj_ver_id %i\n", visobj_ver_id);
478                                    BitstreamSkip(bs, 3);   /* visual_object_priority */
479                            } else {
480                                    visobj_ver_id = 1;
481                            }
482    
483                            if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO)      /* visual_object_type */
484                            {
485                                    DPRINTF(XVID_DEBUG_ERROR, "visual_object_type != video\n");
486                                  return -1;                                  return -1;
487                          }                          }
488                          BitstreamSkip(bs, 8);                          BitstreamSkip(bs, 4);
489    
490                            /* video_signal_type */
491    
492                          if (BitstreamGetBit(bs))                                        // is_object_layer_identifier                          if (BitstreamGetBit(bs))        /* video_signal_type */
493                            {
494                                    DPRINTF(XVID_DEBUG_HEADER,"+ video_signal_type\n");
495                                    BitstreamSkip(bs, 3);   /* video_format */
496                                    BitstreamSkip(bs, 1);   /* video_range */
497                                    if (BitstreamGetBit(bs))        /* color_description */
498                          {                          {
499                                  DEBUG("+ is_object_layer_identifier");                                          DPRINTF(XVID_DEBUG_HEADER,"+ color_description");
500                                  vol_ver_id = BitstreamGetBits(bs,4);            // video_object_layer_verid                                          BitstreamSkip(bs, 8);   /* color_primaries */
501                                  BitstreamSkip(bs, 3);                                   // video_object_layer_priority                                          BitstreamSkip(bs, 8);   /* transfer_characteristics */
502                                            BitstreamSkip(bs, 8);   /* matrix_coefficients */
503                          }                          }
504                          else                          }
505                    } else if ((start_code & ~VIDOBJ_START_CODE_MASK) == VIDOBJ_START_CODE) {
506    
507                            DPRINTF(XVID_DEBUG_STARTCODE, "<video_object>\n");
508                            DPRINTF(XVID_DEBUG_HEADER, "vo id %i\n", start_code & VIDOBJ_START_CODE_MASK);
509    
510                            BitstreamSkip(bs, 32);  /* video_object_start_code */
511    
512                    } else if ((start_code & ~VIDOBJLAY_START_CODE_MASK) == VIDOBJLAY_START_CODE) {
513    
514                            DPRINTF(XVID_DEBUG_STARTCODE, "<video_object_layer>\n");
515                            DPRINTF(XVID_DEBUG_HEADER, "vol id %i\n", start_code & VIDOBJLAY_START_CODE_MASK);
516    
517                            BitstreamSkip(bs, 32);  /* video_object_layer_start_code */
518                            BitstreamSkip(bs, 1);   /* random_accessible_vol */
519    
520                BitstreamSkip(bs, 8);   /* video_object_type_indication */
521    
522                            if (BitstreamGetBit(bs))        /* is_object_layer_identifier */
523                          {                          {
524                                    DPRINTF(XVID_DEBUG_HEADER, "+ is_object_layer_identifier\n");
525                                    vol_ver_id = BitstreamGetBits(bs, 4);   /* video_object_layer_verid */
526                                    DPRINTF(XVID_DEBUG_HEADER,"ver_id %i\n", vol_ver_id);
527                                    BitstreamSkip(bs, 3);   /* video_object_layer_priority */
528                            } else {
529                                  vol_ver_id = 1;                                  vol_ver_id = 1;
530                          }                          }
                         //DEBUGI("vol_ver_id", vol_ver_id);  
531    
532                          if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR)     // aspect_ratio_info                          dec->aspect_ratio = BitstreamGetBits(bs, 4);
533    
534                            if (dec->aspect_ratio == VIDOBJLAY_AR_EXTPAR)   /* aspect_ratio_info */
535                          {                          {
536                                  DEBUG("+ aspect_ratio_info");                                  DPRINTF(XVID_DEBUG_HEADER, "+ aspect_ratio_info\n");
537                                  BitstreamSkip(bs, 8);                                           // par_width                                  dec->par_width = BitstreamGetBits(bs, 8);       /* par_width */
538                                  BitstreamSkip(bs, 8);                                           // par_height                                  dec->par_height = BitstreamGetBits(bs, 8);      /* par_height */
539                          }                          }
540    
541                          if (BitstreamGetBit(bs))                                                // vol_control_parameters                          if (BitstreamGetBit(bs))        /* vol_control_parameters */
542                          {                          {
543                                  DEBUG("+ vol_control_parameters");                                  DPRINTF(XVID_DEBUG_HEADER, "+ vol_control_parameters\n");
544                                  BitstreamSkip(bs, 2);                                           // chroma_format                                  BitstreamSkip(bs, 2);   /* chroma_format */
545                                  BitstreamSkip(bs, 1);                                           // low_delay                                  dec->low_delay = BitstreamGetBit(bs);   /* low_delay */
546                                  if (BitstreamGetBit(bs))                                        // vbv_parameters                                  DPRINTF(XVID_DEBUG_HEADER, "low_delay %i\n", dec->low_delay);
547                                    if (BitstreamGetBit(bs))        /* vbv_parameters */
548                                  {                                  {
549                                          DEBUG("+ vbv_parameters");                                          unsigned int bitrate;
550                                          BitstreamSkip(bs, 15);                          // first_half_bitrate                                          unsigned int buffer_size;
551                                            unsigned int occupancy;
552    
553                                            DPRINTF(XVID_DEBUG_HEADER,"+ vbv_parameters\n");
554    
555                                            bitrate = BitstreamGetBits(bs,15) << 15;        /* first_half_bit_rate */
556                                          READ_MARKER();                                          READ_MARKER();
557                                          BitstreamSkip(bs, 15);                          // latter_half_bitrate                                          bitrate |= BitstreamGetBits(bs,15);             /* latter_half_bit_rate */
558                                          READ_MARKER();                                          READ_MARKER();
559                                          BitstreamSkip(bs, 15);                          // first_half_vbv_buffer_size  
560                                            buffer_size = BitstreamGetBits(bs, 15) << 3;    /* first_half_vbv_buffer_size */
561                                          READ_MARKER();                                          READ_MARKER();
562                                          BitstreamSkip(bs, 3);                                   // latter_half_vbv_buffer_size                                          buffer_size |= BitstreamGetBits(bs, 3);         /* latter_half_vbv_buffer_size */
563                                          BitstreamSkip(bs, 11);                          // first_half_vbv_occupancy  
564                                            occupancy = BitstreamGetBits(bs, 11) << 15;     /* first_half_vbv_occupancy */
565                                          READ_MARKER();                                          READ_MARKER();
566                                          BitstreamSkip(bs, 15);                          // latter_half_vbv_occupancy                                          occupancy |= BitstreamGetBits(bs, 15);  /* latter_half_vbv_occupancy */
567                                          READ_MARKER();                                          READ_MARKER();
568    
569                                            DPRINTF(XVID_DEBUG_HEADER,"bitrate %d (unit=400 bps)\n", bitrate);
570                                            DPRINTF(XVID_DEBUG_HEADER,"buffer_size %d (unit=16384 bits)\n", buffer_size);
571                                            DPRINTF(XVID_DEBUG_HEADER,"occupancy %d (unit=64 bits)\n", occupancy);
572                                  }                                  }
573                            }else{
574                                    dec->low_delay = dec->low_delay_default;
575                          }                          }
576    
577                          dec->shape = BitstreamGetBits(bs, 2);   // video_object_layer_shape                          dec->shape = BitstreamGetBits(bs, 2);   /* video_object_layer_shape */
                         // DEBUG1("shape", dec->shape);  
578    
579                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1)                          DPRINTF(XVID_DEBUG_HEADER, "shape %i\n", dec->shape);
580                            if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)
581                          {                          {
582                                  BitstreamSkip(bs, 4);           // video_object_layer_shape_extension                                  DPRINTF(XVID_DEBUG_ERROR,"non-rectangular shapes are not supported\n");
583                            }
584    
585                            if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) {
586                                    BitstreamSkip(bs, 4);   /* video_object_layer_shape_extension */
587                          }                          }
588    
589                          READ_MARKER();                          READ_MARKER();
590    
591  // *************************** for decode B-frame time ***********************                          /********************** for decode B-frame time ***********************/
592                          time_increment_resolution = BitstreamGetBits(bs, 16);   // vop_time_increment_resolution                          dec->time_inc_resolution = BitstreamGetBits(bs, 16);    /* vop_time_increment_resolution */
593                          time_increment_resolution--;                          DPRINTF(XVID_DEBUG_HEADER,"vop_time_increment_resolution %i\n", dec->time_inc_resolution);
                         //DEBUG1("time_increment_resolution=",time_increment_resolution);  
                         if (time_increment_resolution > 0)  
                         {  
                                 dec->time_inc_bits = log2bin(time_increment_resolution);  
                         }  
                         else  
                         {  
                                 // dec->time_inc_bits = 0;  
594    
595                                  // for "old" xvid compatibility, set time_inc_bits = 1  #if 0
596                            dec->time_inc_resolution--;
597    #endif
598    
599                            if (dec->time_inc_resolution > 0) {
600                                    dec->time_inc_bits = log2bin(dec->time_inc_resolution-1);
601                            } else {
602    #if 0
603                                    dec->time_inc_bits = 0;
604    #endif
605                                    /* for "old" xvid compatibility, set time_inc_bits = 1 */
606                                  dec->time_inc_bits = 1;                                  dec->time_inc_bits = 1;
607                          }                          }
608    
609                          READ_MARKER();                          READ_MARKER();
610    
611                          if (BitstreamGetBit(bs))                                                // fixed_vop_rate                          if (BitstreamGetBit(bs))        /* fixed_vop_rate */
612                          {                          {
613                                  BitstreamSkip(bs, dec->time_inc_bits);  // fixed_vop_time_increment                                  DPRINTF(XVID_DEBUG_HEADER, "+ fixed_vop_rate\n");
614                                    BitstreamSkip(bs, dec->time_inc_bits);  /* fixed_vop_time_increment */
615                          }                          }
616    
617                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) {
                         {  
618    
619                                  if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR)                                  if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) {
                                 {  
620                                          uint32_t width, height;                                          uint32_t width, height;
621    
622                                          READ_MARKER();                                          READ_MARKER();
623                                          width = BitstreamGetBits(bs, 13);                       // video_object_layer_width                                          width = BitstreamGetBits(bs, 13);       /* video_object_layer_width */
                                         //DEBUGI("width", width);  
624                                          READ_MARKER();                                          READ_MARKER();
625                                          height = BitstreamGetBits(bs, 13);              // video_object_layer_height                                          height = BitstreamGetBits(bs, 13);      /* video_object_layer_height */
                                         //DEBUGI("height", height);  
626                                          READ_MARKER();                                          READ_MARKER();
627    
628                                          if (width != dec->width || height != dec->height)                                          DPRINTF(XVID_DEBUG_HEADER, "width %i\n", width);
629                                            DPRINTF(XVID_DEBUG_HEADER, "height %i\n", height);
630    
631                                            if (dec->width != width || dec->height != height)
632                                            {
633                                                    if (dec->fixed_dimensions)
634                                          {                                          {
635                                                  DEBUG("FATAL: video dimension discrepancy ***");                                                          DPRINTF(XVID_DEBUG_ERROR, "decoder width/height does not match bitstream\n");
                                                 DEBUG2("bitstream width/height", width, height);  
                                                 DEBUG2("param width/height", dec->width, dec->height);  
636                                                  return -1;                                                  return -1;
637                                          }                                          }
638                                                    resize = 1;
639                                                    dec->width = width;
640                                                    dec->height = height;
641                                            }
642                                  }                                  }
643    
644                                  if ((dec->interlacing = BitstreamGetBit(bs)))                                  dec->interlacing = BitstreamGetBit(bs);
645                                    DPRINTF(XVID_DEBUG_HEADER, "interlacing %i\n", dec->interlacing);
646    
647                                    if (!BitstreamGetBit(bs))       /* obmc_disable */
648                                  {                                  {
649                                          DEBUG("vol: interlacing");                                          DPRINTF(XVID_DEBUG_ERROR, "obmc_disabled==false not supported\n");
650                                            /* TODO */
651                                            /* fucking divx4.02 has this enabled */
652                                  }                                  }
653    
654                                  if (!BitstreamGetBit(bs))                               // obmc_disable                                  dec->sprite_enable = BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2));   /* sprite_enable */
655    
656                                    if (dec->sprite_enable == SPRITE_STATIC || dec->sprite_enable == SPRITE_GMC)
657                                  {                                  {
658                                          DEBUG("IGNORED/TODO: !obmc_disable");                                          int low_latency_sprite_enable;
                                         // TODO  
                                         // fucking divx4.02 has this enabled  
                                 }  
659    
660                                  if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2)))  // sprite_enable                                          if (dec->sprite_enable != SPRITE_GMC)
661                                  {                                  {
662                                          DEBUG("sprite_enable; not supported");                                                  int sprite_width;
663                                          return -1;                                                  int sprite_height;
664                                                    int sprite_left_coord;
665                                                    int sprite_top_coord;
666                                                    sprite_width = BitstreamGetBits(bs, 13);                /* sprite_width */
667                                                    READ_MARKER();
668                                                    sprite_height = BitstreamGetBits(bs, 13);       /* sprite_height */
669                                                    READ_MARKER();
670                                                    sprite_left_coord = BitstreamGetBits(bs, 13);   /* sprite_left_coordinate */
671                                                    READ_MARKER();
672                                                    sprite_top_coord = BitstreamGetBits(bs, 13);    /* sprite_top_coordinate */
673                                                    READ_MARKER();
674                                  }                                  }
675                                            dec->sprite_warping_points = BitstreamGetBits(bs, 6);           /* no_of_sprite_warping_points */
676                                  if (vol_ver_id != 1 && dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)                                          dec->sprite_warping_accuracy = BitstreamGetBits(bs, 2);         /* sprite_warping_accuracy */
677                                            dec->sprite_brightness_change = BitstreamGetBits(bs, 1);                /* brightness_change */
678                                            if (dec->sprite_enable != SPRITE_GMC)
679                                  {                                  {
680                                          BitstreamSkip(bs, 1);                                   // sadct_disable                                                  low_latency_sprite_enable = BitstreamGetBits(bs, 1);            /* low_latency_sprite_enable */
681                                            }
682                                  }                                  }
683    
684                                  if (BitstreamGetBit(bs))                                                // not_8_bit                                  if (vol_ver_id != 1 &&
685                                  {                                          dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
686                                          DEBUG("+ not_8_bit [IGNORED/TODO]");                                          BitstreamSkip(bs, 1);   /* sadct_disable */
                                         dec->quant_bits = BitstreamGetBits(bs, 4);      // quant_precision  
                                         BitstreamSkip(bs, 4);                                           // bits_per_pixel  
687                                  }                                  }
688                                  else  
689                                    if (BitstreamGetBit(bs))        /* not_8_bit */
690                                  {                                  {
691                                            DPRINTF(XVID_DEBUG_HEADER, "not_8_bit==true (ignored)\n");
692                                            dec->quant_bits = BitstreamGetBits(bs, 4);      /* quant_precision */
693                                            BitstreamSkip(bs, 4);   /* bits_per_pixel */
694                                    } else {
695                                          dec->quant_bits = 5;                                          dec->quant_bits = 5;
696                                  }                                  }
697    
698                                  if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE)                                  if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {
699                                  {                                          BitstreamSkip(bs, 1);   /* no_gray_quant_update */
700                                          BitstreamSkip(bs, 1);                   // no_gray_quant_update                                          BitstreamSkip(bs, 1);   /* composition_method */
701                                          BitstreamSkip(bs, 1);                   // composition_method                                          BitstreamSkip(bs, 1);   /* linear_composition */
                                         BitstreamSkip(bs, 1);                   // linear_composition  
702                                  }                                  }
703    
704                                  dec->quant_type = BitstreamGetBit(bs);          // quant_type                                  dec->quant_type = BitstreamGetBit(bs);  /* quant_type */
705                                  // DEBUG1("**** quant_type", dec->quant_type);                                  DPRINTF(XVID_DEBUG_HEADER, "quant_type %i\n", dec->quant_type);
706    
707                                  if (dec->quant_type)                                  if (dec->quant_type) {
708                                  {                                          if (BitstreamGetBit(bs))        /* load_intra_quant_mat */
                                         if (BitstreamGetBit(bs))                // load_intra_quant_mat  
709                                          {                                          {
710                                                  uint8_t matrix[64];                                                  uint8_t matrix[64];
711    
712                                                    DPRINTF(XVID_DEBUG_HEADER, "load_intra_quant_mat\n");
713    
714                                                  bs_get_matrix(bs, matrix);                                                  bs_get_matrix(bs, matrix);
715                                                  set_intra_matrix(matrix);                                                  set_intra_matrix(matrix);
716                                          }                                          } else
                                         else  
717                                                  set_intra_matrix(get_default_intra_matrix());                                                  set_intra_matrix(get_default_intra_matrix());
718    
719                                          if (BitstreamGetBit(bs))                // load_inter_quant_mat                                          if (BitstreamGetBit(bs))        /* load_inter_quant_mat */
720                                          {                                          {
721                                                  uint8_t matrix[64];                                                  uint8_t matrix[64];
722    
723                                                    DPRINTF(XVID_DEBUG_HEADER, "load_inter_quant_mat\n");
724    
725                                                  bs_get_matrix(bs, matrix);                                                  bs_get_matrix(bs, matrix);
726                                                  set_inter_matrix(matrix);                                                  set_inter_matrix(matrix);
727                                          }                                          } else
                                         else  
728                                                  set_inter_matrix(get_default_inter_matrix());                                                  set_inter_matrix(get_default_inter_matrix());
729    
730                                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE)                                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {
731                                          {                                                  DPRINTF(XVID_DEBUG_ERROR, "greyscale matrix not supported\n");
                                                 // TODO  
                                                 DEBUG("TODO: grayscale matrix stuff");  
732                                                  return -1;                                                  return -1;
733                                          }                                          }
734    
735                                  }                                  }
736    
737    
738                                  if (vol_ver_id != 1)                                  if (vol_ver_id != 1) {
739                                  {                                          dec->quarterpel = BitstreamGetBit(bs);  /* quarter_sample */
740                                          dec->quarterpel = BitstreamGetBit(bs);  // quarter_sampe                                          DPRINTF(XVID_DEBUG_HEADER,"quarterpel %i\n", dec->quarterpel);
                                         if (dec->quarterpel)  
                                         {  
                                                 DEBUG("IGNORED/TODO: quarter_sample");  
                                         }  
741                                  }                                  }
742                                  else                                  else
                                 {  
743                                          dec->quarterpel = 0;                                          dec->quarterpel = 0;
                                 }  
744    
                                 if (!BitstreamGetBit(bs))                       // complexity_estimation_disable  
                                 {  
                                         DEBUG("TODO: complexity_estimation header");  
                                         // TODO  
                                         return -1;  
                                 }  
745    
746                                  if (!BitstreamGetBit(bs))                       // resync_marker_disable                                  dec->complexity_estimation_disable = BitstreamGetBit(bs);       /* complexity estimation disable */
747                                    if (!dec->complexity_estimation_disable)
748                                  {                                  {
749                                          DEBUG("IGNORED/TODO: !resync_marker_disable");                                          read_vol_complexity_estimation_header(bs, dec);
                                         // TODO  
750                                  }                                  }
751    
752                                  if (BitstreamGetBit(bs))                // data_partitioned                                  BitstreamSkip(bs, 1);   /* resync_marker_disable */
753    
754                                    if (BitstreamGetBit(bs))        /* data_partitioned */
755                                  {                                  {
756                                          DEBUG("+ data_partitioned");                                          DPRINTF(XVID_DEBUG_ERROR, "data_partitioned not supported\n");
757                                          BitstreamSkip(bs, 1);           // reversible_vlc                                          BitstreamSkip(bs, 1);   /* reversible_vlc */
758                                  }                                  }
759    
760                                  if (vol_ver_id != 1)                                  if (vol_ver_id != 1) {
761                                            dec->newpred_enable = BitstreamGetBit(bs);
762                                            if (dec->newpred_enable)        /* newpred_enable */
763                                  {                                  {
764                                          if (BitstreamGetBit(bs))                        // newpred_enable                                                  DPRINTF(XVID_DEBUG_HEADER, "+ newpred_enable\n");
765                                          {                                                  BitstreamSkip(bs, 2);   /* requested_upstream_message_type */
766                                                  DEBUG("+ newpred_enable");                                                  BitstreamSkip(bs, 1);   /* newpred_segment_type */
                                                 BitstreamSkip(bs, 2);                   // requested_upstream_message_type  
                                                 BitstreamSkip(bs, 1);                   // newpred_segment_type  
767                                          }                                          }
768                                          if (BitstreamGetBit(bs))                        // reduced_resolution_vop_enable                                          dec->reduced_resolution_enable = BitstreamGetBit(bs);   /* reduced_resolution_vop_enable */
769                                          {                                          DPRINTF(XVID_DEBUG_HEADER, "reduced_resolution_enable %i\n", dec->reduced_resolution_enable);
                                                 DEBUG("TODO: reduced_resolution_vop_enable");  
                                                 // TODO  
                                                 return -1;  
770                                          }                                          }
771                                    else
772                                    {
773                                            dec->newpred_enable = 0;
774                                            dec->reduced_resolution_enable = 0;
775                                  }                                  }
776    
777                                  if ((dec->scalability=BitstreamGetBit(bs)))     // scalability                                  dec->scalability = BitstreamGetBit(bs); /* scalability */
778                                    if (dec->scalability)
779                                  {                                  {
780                                          // TODO                                          DPRINTF(XVID_DEBUG_ERROR, "scalability not supported\n");
781                                          DEBUG("TODO: scalability");                                          BitstreamSkip(bs, 1);   /* hierarchy_type */
782                                          return -1;                                          BitstreamSkip(bs, 4);   /* ref_layer_id */
783                                            BitstreamSkip(bs, 1);   /* ref_layer_sampling_direc */
784                                            BitstreamSkip(bs, 5);   /* hor_sampling_factor_n */
785                                            BitstreamSkip(bs, 5);   /* hor_sampling_factor_m */
786                                            BitstreamSkip(bs, 5);   /* vert_sampling_factor_n */
787                                            BitstreamSkip(bs, 5);   /* vert_sampling_factor_m */
788                                            BitstreamSkip(bs, 1);   /* enhancement_type */
789                                            if(dec->shape == VIDOBJLAY_SHAPE_BINARY /* && hierarchy_type==0 */) {
790                                                    BitstreamSkip(bs, 1);   /* use_ref_shape */
791                                                    BitstreamSkip(bs, 1);   /* use_ref_texture */
792                                                    BitstreamSkip(bs, 5);   /* shape_hor_sampling_factor_n */
793                                                    BitstreamSkip(bs, 5);   /* shape_hor_sampling_factor_m */
794                                                    BitstreamSkip(bs, 5);   /* shape_vert_sampling_factor_n */
795                                                    BitstreamSkip(bs, 5);   /* shape_vert_sampling_factor_m */
796                                  }                                  }
797                                            return -1;
798                          }                          }
799                          else    // dec->shape == BINARY_ONLY                          } else                          /* dec->shape == BINARY_ONLY */
                         {  
                                 if (vol_ver_id != 1)  
800                                  {                                  {
801                                          if (BitstreamGetBit(bs))        // scalability                                  if (vol_ver_id != 1) {
802                                          {                                          dec->scalability = BitstreamGetBit(bs); /* scalability */
803                                                  // TODO                                          if (dec->scalability)
804                                                  DEBUG("TODO: scalability");                                          {
805                                                    DPRINTF(XVID_DEBUG_ERROR, "scalability not supported\n");
806                                                    BitstreamSkip(bs, 4);   /* ref_layer_id */
807                                                    BitstreamSkip(bs, 5);   /* hor_sampling_factor_n */
808                                                    BitstreamSkip(bs, 5);   /* hor_sampling_factor_m */
809                                                    BitstreamSkip(bs, 5);   /* vert_sampling_factor_n */
810                                                    BitstreamSkip(bs, 5);   /* vert_sampling_factor_m */
811                                                  return -1;                                                  return -1;
812                                          }                                          }
813                                  }                                  }
814                                  BitstreamSkip(bs, 1);                   // resync_marker_disable                                  BitstreamSkip(bs, 1);   /* resync_marker_disable */
815    
816                          }                          }
817    
818                  }                          return (resize ? -3 : -2 );     /* VOL */
819                  else if (start_code == GRPOFVOP_START_CODE)  
820                  {                  } else if (start_code == GRPOFVOP_START_CODE) {
821                          // DEBUG("group_of_vop");  
822                            DPRINTF(XVID_DEBUG_STARTCODE, "<group_of_vop>\n");
823    
824                          BitstreamSkip(bs, 32);                          BitstreamSkip(bs, 32);
825                          {                          {
826                                  int hours, minutes, seconds;                                  int hours, minutes, seconds;
827    
828                                  hours = BitstreamGetBits(bs, 5);                                  hours = BitstreamGetBits(bs, 5);
829                                  minutes = BitstreamGetBits(bs, 6);                                  minutes = BitstreamGetBits(bs, 6);
830                                  READ_MARKER();                                  READ_MARKER();
831                                  seconds = BitstreamGetBits(bs, 6);                                  seconds = BitstreamGetBits(bs, 6);
832                                  // DEBUG3("hms", hours, minutes, seconds);  
833                          }                                  DPRINTF(XVID_DEBUG_HEADER, "time %ih%im%is\n", hours,minutes,seconds);
                         BitstreamSkip(bs, 1);                   // closed_gov  
                         BitstreamSkip(bs, 1);                   // broken_link  
834                  }                  }
835                  else if (start_code == VOP_START_CODE)                          BitstreamSkip(bs, 1);   /* closed_gov */
836                  {                          BitstreamSkip(bs, 1);   /* broken_link */
837                          // DEBUG("vop_start_code");  
838                          BitstreamSkip(bs, 32);                                          // vop_start_code                  } else if (start_code == VOP_START_CODE) {
839    
840                            DPRINTF(XVID_DEBUG_STARTCODE, "<vop>\n");
841    
842                          coding_type = BitstreamGetBits(bs, 2);          // vop_coding_type                          BitstreamSkip(bs, 32);  /* vop_start_code */
                         //DEBUG1("coding_type", coding_type);  
843    
844  // *************************** for decode B-frame time ***********************                          coding_type = BitstreamGetBits(bs, 2);  /* vop_coding_type */
845                          while (BitstreamGetBit(bs) != 0)                        // time_base                          DPRINTF(XVID_DEBUG_HEADER, "coding_type %i\n", coding_type);
846    
847                            /*********************** for decode B-frame time ***********************/
848                            while (BitstreamGetBit(bs) != 0)        /* time_base */
849                                  time_incr++;                                  time_incr++;
850    
851                          READ_MARKER();                          READ_MARKER();
852    
853                          //DEBUG1("time_inc_bits", dec->time_inc_bits);                          if (dec->time_inc_bits) {
854                          //DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits));                                  time_increment = (BitstreamGetBits(bs, dec->time_inc_bits));    /* vop_time_increment */
                         if (dec->time_inc_bits)  
                         {  
                                 //BitstreamSkip(bs, dec->time_inc_bits);        // vop_time_increment  
                                 time_increment = (BitstreamGetBits(bs, dec->time_inc_bits));    // vop_time_increment  
855                          }                          }
856    
857                            DPRINTF(XVID_DEBUG_HEADER, "time_base %i\n", time_incr);
858                            DPRINTF(XVID_DEBUG_HEADER, "time_increment %i\n", time_increment);
859    
860                            DPRINTF(XVID_DEBUG_TIMECODE, "%c %i:%i\n",
861                                    coding_type == I_VOP ? 'I' : coding_type == P_VOP ? 'P' : coding_type == B_VOP ? 'B' : 'S',
862                                    time_incr, time_increment);
863    
864                          if(coding_type != B_VOP){                          if(coding_type != B_VOP){
865                              dec->last_time_base = dec->time_base;                              dec->last_time_base = dec->time_base;
866                                  dec->time_base += time_incr;                                  dec->time_base += time_incr;
867                                  dec->time = dec->time_base*time_increment_resolution + time_increment;                                  dec->time = time_increment;
868                                  dec->time_pp= (uint32_t)(dec->time - dec->last_non_b_time);  
869    #if 0
870                                            dec->time_base * dec->time_inc_resolution +
871                                            time_increment;
872    #endif
873                                            dec->time_pp = (uint32_t)
874                                                    (dec->time_inc_resolution + dec->time - dec->last_non_b_time)%dec->time_inc_resolution;
875                                  dec->last_non_b_time= dec->time;                                  dec->last_non_b_time= dec->time;
876                          }else{                          }else{
877                                  dec->time = (dec->last_time_base + time_incr)*time_increment_resolution + time_increment;                                  dec->time = time_increment;
878                                  dec->time_bp= (uint32_t)(dec->last_non_b_time - dec->time);  #if 0
879                                            (dec->last_time_base +
880                                             time_incr) * dec->time_inc_resolution + time_increment;
881    #endif
882                                    dec->time_bp = (uint32_t)
883                                            (dec->time_inc_resolution + dec->last_non_b_time - dec->time)%dec->time_inc_resolution;
884                          }                          }
885                          //DEBUG1("time_increment=",time_increment);                          DPRINTF(XVID_DEBUG_HEADER,"time_pp=%i\n", dec->time_pp);
886                            DPRINTF(XVID_DEBUG_HEADER,"time_bp=%i\n", dec->time_bp);
887    
888                          READ_MARKER();                          READ_MARKER();
889    
890                          if (!BitstreamGetBit(bs))                                       // vop_coded                          if (!BitstreamGetBit(bs))       /* vop_coded */
891                          {                          {
892                                    DPRINTF(XVID_DEBUG_HEADER, "vop_coded==false\n");
893                                  return N_VOP;                                  return N_VOP;
894                          }                          }
895    
896                          /* if (newpred_enable)                          if (dec->newpred_enable)
897                          {                          {
898                          }                                  int vop_id;
899                          */                                  int vop_id_for_prediction;
900    
901                          // fix a little bug by MinChen <chenm002@163.com>                                  vop_id = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15));
902                          if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && (coding_type == P_VOP))                                  DPRINTF(XVID_DEBUG_HEADER, "vop_id %i\n", vop_id);
903                                    if (BitstreamGetBit(bs))        /* vop_id_for_prediction_indication */
904                          {                          {
905                                  *rounding = BitstreamGetBit(bs);        // rounding_type                                          vop_id_for_prediction = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15));
906                                  //DEBUG1("rounding", *rounding);                                          DPRINTF(XVID_DEBUG_HEADER, "vop_id_for_prediction %i\n", vop_id_for_prediction);
907                                    }
908                                    READ_MARKER();
909                          }                          }
910    
911                          /* if (reduced_resolution_enable)  
912                          {  
913                            /* fix a little bug by MinChen <chenm002@163.com> */
914                            if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) &&
915                                    ( (coding_type == P_VOP) || (coding_type == S_VOP && dec->sprite_enable == SPRITE_GMC) ) ) {
916                                    *rounding = BitstreamGetBit(bs);        /* rounding_type */
917                                    DPRINTF(XVID_DEBUG_HEADER, "rounding %i\n", *rounding);
918                          }                          }
                         */  
919    
920                          if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)                          if (dec->reduced_resolution_enable &&
921                                    dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR &&
922                                    (coding_type == P_VOP || coding_type == I_VOP)) {
923    
924                                    *reduced_resolution = BitstreamGetBit(bs);
925                                    DPRINTF(XVID_DEBUG_HEADER, "reduced_resolution %i\n", *reduced_resolution);
926                            }
927                            else
928                          {                          {
929                                    *reduced_resolution = 0;
930                            }
931    
932                            if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
933                                    if(!(dec->sprite_enable == SPRITE_STATIC && coding_type == I_VOP)) {
934    
935                                  uint32_t width, height;                                  uint32_t width, height;
936                                  uint32_t horiz_mc_ref, vert_mc_ref;                                  uint32_t horiz_mc_ref, vert_mc_ref;
937    
# Line 530  Line 944 
944                                  vert_mc_ref = BitstreamGetBits(bs, 13);                                  vert_mc_ref = BitstreamGetBits(bs, 13);
945                                  READ_MARKER();                                  READ_MARKER();
946    
947                                  // DEBUG2("vop_width/height", width, height);                                          DPRINTF(XVID_DEBUG_HEADER, "width %i\n", width);
948                                  // DEBUG2("ref             ", horiz_mc_ref, vert_mc_ref);                                          DPRINTF(XVID_DEBUG_HEADER, "height %i\n", height);
949                                            DPRINTF(XVID_DEBUG_HEADER, "horiz_mc_ref %i\n", horiz_mc_ref);
950                                            DPRINTF(XVID_DEBUG_HEADER, "vert_mc_ref %i\n", vert_mc_ref);
951                                    }
952    
953                                  BitstreamSkip(bs, 1);                           // change_conv_ratio_disable                                  BitstreamSkip(bs, 1);   /* change_conv_ratio_disable */
954                                  if (BitstreamGetBit(bs))                        // vop_constant_alpha                                  if (BitstreamGetBit(bs))        /* vop_constant_alpha */
955                                  {                                  {
956                                          BitstreamSkip(bs, 8);                   // vop_constant_alpha_value                                          BitstreamSkip(bs, 8);   /* vop_constant_alpha_value */
957                                  }                                  }
958                          }                          }
959    
960                            if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) {
961    
962                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)                                  if (!dec->complexity_estimation_disable)
963                          {                          {
964                                  // intra_dc_vlc_threshold                                          read_vop_complexity_estimation_header(bs, dec, coding_type);
965                                  *intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ];                                  }
966    
967                                  if (dec->interlacing)                                  /* intra_dc_vlc_threshold */
968                                  {                                  *intra_dc_threshold =
969                                          if ((dec->top_field_first = BitstreamGetBit(bs)))                                          intra_dc_threshold_table[BitstreamGetBits(bs, 3)];
970    
971                                    dec->top_field_first = 0;
972                                    dec->alternate_vertical_scan = 0;
973    
974                                    if (dec->interlacing) {
975                                            dec->top_field_first = BitstreamGetBit(bs);
976                                            DPRINTF(XVID_DEBUG_HEADER, "interlace top_field_first %i\n", dec->top_field_first);
977                                            dec->alternate_vertical_scan = BitstreamGetBit(bs);
978                                            DPRINTF(XVID_DEBUG_HEADER, "interlace alternate_vertical_scan %i\n", dec->alternate_vertical_scan);
979    
980                                    }
981                            }
982    
983                            if ((dec->sprite_enable == SPRITE_STATIC || dec->sprite_enable== SPRITE_GMC) && coding_type == S_VOP) {
984    
985                                    int i;
986    
987                                    for (i = 0 ; i < dec->sprite_warping_points; i++)
988                                          {                                          {
989                                                  DEBUG("vop: top_field_first");                                          int length;
990                                            int x = 0, y = 0;
991    
992                                            /* sprite code borowed from ffmpeg; thx Michael Niedermayer <michaelni@gmx.at> */
993                                            length = bs_get_spritetrajectory(bs);
994                                            if(length){
995                                                    x= BitstreamGetBits(bs, length);
996                                                    if ((x >> (length - 1)) == 0) /* if MSB not set it is negative*/
997                                                            x = - (x ^ ((1 << length) - 1));
998                                            }
999                                            READ_MARKER();
1000    
1001                                            length = bs_get_spritetrajectory(bs);
1002                                            if(length){
1003                                                    y = BitstreamGetBits(bs, length);
1004                                                    if ((y >> (length - 1)) == 0) /* if MSB not set it is negative*/
1005                                                            y = - (y ^ ((1 << length) - 1));
1006                                            }
1007                                            READ_MARKER();
1008    
1009                                            gmc_warp->duv[i].x = x;
1010                                            gmc_warp->duv[i].y = y;
1011    
1012                                            DPRINTF(XVID_DEBUG_HEADER,"sprite_warping_point[%i] xy=(%i,%i)\n", i, x, y);
1013                                          }                                          }
1014                                          if ((dec->alternate_vertical_scan = BitstreamGetBit(bs)))  
1015                                    if (dec->sprite_brightness_change)
1016                                          {                                          {
1017                                                  DEBUG("vop: alternate_vertical_scan");                                          /* XXX: brightness_change_factor() */
1018                                          }                                          }
1019                                    if (dec->sprite_enable == SPRITE_STATIC)
1020                                    {
1021                                            /* XXX: todo */
1022                                  }                                  }
1023    
1024                          }                          }
1025    
1026                          if((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1)                // vop_quant                          if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1)       /* vop_quant */
1027                                  *quant = 1;                                  *quant = 1;
1028                            DPRINTF(XVID_DEBUG_HEADER, "quant %i\n", *quant);
1029    
1030                          //DEBUG1("quant", *quant);                          if (coding_type != I_VOP) {
1031                                    *fcode_forward = BitstreamGetBits(bs, 3);       /* fcode_forward */
1032                          if (coding_type != I_VOP)                                  DPRINTF(XVID_DEBUG_HEADER, "fcode_forward %i\n", *fcode_forward);
                         {  
                                 *fcode_forward = BitstreamGetBits(bs, 3);               // fcode_forward  
1033                          }                          }
1034    
1035                          if (coding_type == B_VOP)                          if (coding_type == B_VOP) {
1036                          {                                  *fcode_backward = BitstreamGetBits(bs, 3);      /* fcode_backward */
1037                                  *fcode_backward = BitstreamGetBits(bs, 3);              // fcode_backward                                  DPRINTF(XVID_DEBUG_HEADER, "fcode_backward %i\n", *fcode_backward);
1038                          }                          }
1039                          if (!dec->scalability){                          if (!dec->scalability){
1040                                  if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) && (coding_type != I_VOP)){                                  if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) &&
1041                                          BitstreamSkip(bs, 1);           // vop_shape_coding_type                                          (coding_type != I_VOP)) {
1042                                            BitstreamSkip(bs, 1);   /* vop_shape_coding_type */
1043                                  }                                  }
1044                          }                          }
1045                          return coding_type;                          return coding_type;
1046    
1047                    } else if (start_code == USERDATA_START_CODE) {
1048                            char tmp[256];
1049                        int i, version, build;
1050                            char packed;
1051    
1052                            BitstreamSkip(bs, 32);  /* user_data_start_code */
1053    
1054                            tmp[0] = BitstreamShowBits(bs, 8);
1055    
1056                            for(i = 1; i < 256; i++){
1057                                    tmp[i] = (BitstreamShowBits(bs, 16) & 0xFF);
1058    
1059                                    if(tmp[i] == 0)
1060                                            break;
1061    
1062                                    BitstreamSkip(bs, 8);
1063                  }                  }
1064                  else if (start_code == USERDATA_START_CODE)  
1065                            DPRINTF(XVID_DEBUG_STARTCODE, "<user_data>: %s\n", tmp);
1066    
1067                        /* divx detection */
1068                            i = sscanf(tmp, "DivX%dBuild%d%c", &version, &build, &packed);
1069                            if (i < 2)
1070                                    i = sscanf(tmp, "DivX%db%d%c", &version, &build, &packed);
1071    
1072                            if (i >= 2)
1073                  {                  {
1074                          // DEBUG("user_data");                                  dec->packed_mode = (i == 3 && packed == 'p');
1075                          BitstreamSkip(bs, 32);          // user_data_start_code                                  DPRINTF(XVID_DEBUG_HEADER, "divx version=%i, build=%i packed=%i\n",
1076                                                    version, build, dec->packed_mode);
1077                  }                  }
1078                  else  // start_code == ?  
1079                  {                  } else                                  /* start_code == ? */
                         if (BitstreamShowBits(bs, 24) == 0x000001)  
1080                          {                          {
1081                                  DEBUG1("*** WARNING: unknown start_code", BitstreamShowBits(bs, 32));                          if (BitstreamShowBits(bs, 24) == 0x000001) {
1082                                    DPRINTF(XVID_DEBUG_STARTCODE, "<unknown: %x>\n", BitstreamShowBits(bs, 32));
1083                          }                          }
1084                          BitstreamSkip(bs, 8);                          BitstreamSkip(bs, 8);
1085                  }                  }
1086          }          }
1087          while ((BitstreamPos(bs) >> 3) < bs->length);          while ((BitstreamPos(bs) >> 3) < bs->length);
1088    
1089          DEBUG("*** WARNING: no vop_start_code found");  #if 0
1090            DPRINTF("*** WARNING: no vop_start_code found");
1091    #endif
1092          return -1; /* ignore it */          return -1; /* ignore it */
1093  }  }
1094    
1095    
1096  /* write custom quant matrix */  /* write custom quant matrix */
1097    
1098  static void bs_put_matrix(Bitstream * bs, const int16_t *matrix)  static void
1099    bs_put_matrix(Bitstream * bs,
1100                              const int16_t * matrix)
1101  {  {
1102          int i, j;          int i, j;
1103          const int last = matrix[scan_tables[0][63]];          const int last = matrix[scan_tables[0][63]];
1104    
1105          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--);
1106    
1107          for (i = 0; i <= j; i++)          for (i = 0; i <= j; i++) {
         {  
1108                  BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8);                  BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8);
1109          }          }
1110    
1111          if (j < 63)          if (j < 63) {
         {  
1112                  BitstreamPutBits(bs, 0, 8);                  BitstreamPutBits(bs, 0, 8);
1113          }          }
1114  }  }
# Line 625  Line 1117 
1117  /*  /*
1118          write vol header          write vol header
1119  */  */
1120  void BitstreamWriteVolHeader(Bitstream * const bs,  void
1121                                                  const MBParam * pParam,  const FRAMEINFO * frame)  BitstreamWriteVolHeader(Bitstream * const bs,
1122                                                    const MBParam * pParam)
1123  {  {
1124          // video object_start_code & vo_id          static const unsigned int vo_id = 0;
1125            static const unsigned int vol_id = 0;
1126            int vol_ver_id=1;
1127        int vol_type_ind=VIDOBJLAY_TYPE_SIMPLE;
1128    
1129            if ( (pParam->vol_flags & XVID_VOL_QUARTERPEL) ||
1130             (pParam->vol_flags & XVID_VOL_GMC) ||
1131                     (pParam->vol_flags & XVID_VOL_REDUCED_ENABLE))
1132                    vol_ver_id = 2;
1133    
1134        if ((pParam->vol_flags & XVID_VOL_REDUCED_ENABLE)) {
1135            vol_type_ind = VIDOBJLAY_TYPE_ART_SIMPLE;
1136        }
1137    
1138            if ((pParam->vol_flags & XVID_VOL_QUARTERPEL) ||
1139            (pParam->vol_flags & XVID_VOL_GMC)) {
1140            vol_type_ind = VIDOBJLAY_TYPE_ASP;
1141        }
1142    
1143            /* visual_object_sequence_start_code */
1144    #if 0
1145      BitstreamPad(bs);      BitstreamPad(bs);
1146          BitstreamPutBits(bs, VO_START_CODE, 27);  #endif
     BitstreamPutBits(bs, 0, 5);  
1147    
1148          // video_object_layer_start_code & vol_id          /*
1149          BitstreamPutBits(bs, VOL_START_CODE, 28);           * no padding here, anymore. You have to make sure that you are
1150          BitstreamPutBits(bs, 0, 4);           * byte aligned, and that always 1-8 padding bits have been written
1151             */
1152          BitstreamPutBit(bs, 0);                         // random_accessible_vol  
1153          BitstreamPutBits(bs, 0, 8);                     // video_object_type_indication      if (pParam->profile) {
1154          BitstreamPutBit(bs, 0);                         // is_object_layer_identified (0=not given)              BitstreamPutBits(bs, VISOBJSEQ_START_CODE, 32);
1155          BitstreamPutBits(bs, 1, 4);                     // aspect_ratio_info (1=1:1)              BitstreamPutBits(bs, pParam->profile, 8);
1156          BitstreamPutBit(bs, 0);                         // vol_control_parameters (0=not given)      }
1157          BitstreamPutBits(bs, 0, 2);                     // video_object_layer_shape (0=rectangular)  
1158            /* visual_object_start_code */
1159            BitstreamPad(bs);
1160            BitstreamPutBits(bs, VISOBJ_START_CODE, 32);
1161            BitstreamPutBits(bs, 0, 1);             /* is_visual_object_identifier */
1162            BitstreamPutBits(bs, VISOBJ_TYPE_VIDEO, 4);             /* visual_object_type */
1163    
1164            /* video object_start_code & vo_id */
1165            BitstreamPad(bs);
1166            BitstreamPutBits(bs, VIDOBJ_START_CODE|(vo_id&0x5), 32);
1167    
1168            /* video_object_layer_start_code & vol_id */
1169            BitstreamPad(bs);
1170            BitstreamPutBits(bs, VIDOBJLAY_START_CODE|(vol_id&0x4), 32);
1171    
1172            BitstreamPutBit(bs, 0);         /* random_accessible_vol */
1173            BitstreamPutBits(bs, vol_type_ind, 8);  /* video_object_type_indication */
1174    
1175            if (vol_ver_id == 1)
1176            {
1177                    BitstreamPutBit(bs, 0);                         /* is_object_layer_identified (0=not given) */
1178            }
1179            else
1180            {
1181                    BitstreamPutBit(bs, 1);         /* is_object_layer_identified */
1182                    BitstreamPutBits(bs, vol_ver_id, 4);    /* vol_ver_id == 2 */
1183                    BitstreamPutBits(bs, 4, 3); /* vol_ver_priority (1==lowest, 7==highest) ?? */
1184            }
1185    
1186            BitstreamPutBits(bs, 1, 4);     /* aspect_ratio_info (1=1:1) */
1187    
1188            BitstreamPutBit(bs, 1); /* vol_control_parameters */
1189            BitstreamPutBits(bs, 1, 2);     /* chroma_format 1="4:2:0" */
1190    
1191            if (pParam->max_bframes > 0) {
1192                    BitstreamPutBit(bs, 0); /* low_delay */
1193            } else
1194            {
1195                    BitstreamPutBit(bs, 1); /* low_delay */
1196            }
1197            BitstreamPutBit(bs, 0); /* vbv_parameters (0=not given) */
1198    
1199            BitstreamPutBits(bs, 0, 2);     /* video_object_layer_shape (0=rectangular) */
1200    
1201          WRITE_MARKER();          WRITE_MARKER();
1202    
1203          /* time_increment_resolution; ignored by current decore versions          /*
1204                  eg. 2fps                res=2           inc=1           * time_inc_resolution; ignored by current decore versions
1205                          25fps           res=25          inc=1           * eg. 2fps     res=2       inc=1
1206                          29.97fps        res=30000       inc=1001           *     25fps    res=25      inc=1
1207             *     29.97fps res=30000   inc=1001
1208          */          */
1209          BitstreamPutBits(bs, 2, 16);          BitstreamPutBits(bs, pParam->fbase, 16);
1210    
1211          WRITE_MARKER();          WRITE_MARKER();
1212    
1213          // fixed_vop_rate      if (pParam->fincr>0) {
1214          BitstreamPutBit(bs, 0);              BitstreamPutBit(bs, 1);             /* fixed_vop_rate = 1 */
1215                BitstreamPutBits(bs, pParam->fincr, log2bin(pParam->fbase));        /* fixed_vop_time_increment */
1216          // fixed_time_increment: value=nth_of_sec, nbits = log2(resolution)      }else{
1217          // BitstreamPutBits(bs, 0, 15);          BitstreamPutBit(bs, 0);         /* fixed_vop_rate = 0 */
1218        }
1219    
1220          WRITE_MARKER();          WRITE_MARKER();
1221          BitstreamPutBits(bs, pParam->width, 13);                // width          BitstreamPutBits(bs, pParam->width, 13);        /* width */
1222          WRITE_MARKER();          WRITE_MARKER();
1223          BitstreamPutBits(bs, pParam->height, 13);               // height          BitstreamPutBits(bs, pParam->height, 13);       /* height */
1224          WRITE_MARKER();          WRITE_MARKER();
1225    
1226          BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING);            // interlace          BitstreamPutBit(bs, pParam->vol_flags & XVID_VOL_INTERLACING);  /* interlace */
1227          BitstreamPutBit(bs, 1);         // obmc_disable (overlapped block motion compensation)          BitstreamPutBit(bs, 1);         /* obmc_disable (overlapped block motion compensation) */
         BitstreamPutBit(bs, 0);         // sprite_enable  
         BitstreamPutBit(bs, 0);         // not_in_bit  
1228    
1229          // quant_type   0=h.263  1=mpeg4(quantizer tables)          if (vol_ver_id != 1)
1230          BitstreamPutBit(bs, pParam->m_quant_type);          {       if ((pParam->vol_flags & XVID_VOL_GMC))
1231                    {       BitstreamPutBits(bs, 2, 2);             /* sprite_enable=='GMC' */
1232                            BitstreamPutBits(bs, 2, 6);             /* no_of_sprite_warping_points */
1233                            BitstreamPutBits(bs, 3, 2);             /* sprite_warping_accuracy 0==1/2, 1=1/4, 2=1/8, 3=1/16 */
1234                            BitstreamPutBit(bs, 0);                 /* sprite_brightness_change (not supported) */
1235    
1236          if (pParam->m_quant_type)                          /*
1237          {                           * currently we use no_of_sprite_warping_points==2, sprite_warping_accuracy==3
1238                  BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat                           * for DivX5 compatability
1239                  if (get_intra_matrix_status())                           */
1240                  {  
1241                    } else
1242                            BitstreamPutBits(bs, 0, 2);             /* sprite_enable==off */
1243            }
1244            else
1245                    BitstreamPutBit(bs, 0);         /* sprite_enable==off */
1246    
1247            BitstreamPutBit(bs, 0);         /* not_8_bit */
1248    
1249            /* quant_type   0=h.263  1=mpeg4(quantizer tables) */
1250            BitstreamPutBit(bs, pParam->vol_flags & XVID_VOL_MPEGQUANT);
1251    
1252            if ((pParam->vol_flags & XVID_VOL_MPEGQUANT)) {
1253                    BitstreamPutBit(bs, get_intra_matrix_status()); /* load_intra_quant_mat */
1254                    if (get_intra_matrix_status()) {
1255                          bs_put_matrix(bs, get_intra_matrix());                          bs_put_matrix(bs, get_intra_matrix());
1256                  }                  }
1257    
1258                  BitstreamPutBit(bs, get_inter_matrix_status());         // load_inter_quant_mat                  BitstreamPutBit(bs, get_inter_matrix_status()); /* load_inter_quant_mat */
1259                  if (get_inter_matrix_status())                  if (get_inter_matrix_status()) {
                 {  
1260                          bs_put_matrix(bs, get_inter_matrix());                          bs_put_matrix(bs, get_inter_matrix());
1261                  }                  }
1262    
1263          }          }
1264    
1265          BitstreamPutBit(bs, 1);         // complexity_estimation_disable          if (vol_ver_id != 1) {
1266          BitstreamPutBit(bs, 1);         // resync_marker_disable                  if ((pParam->vol_flags & XVID_VOL_QUARTERPEL))
1267          BitstreamPutBit(bs, 0);         // data_partitioned                          BitstreamPutBit(bs, 1);         /* quarterpel  */
1268          BitstreamPutBit(bs, 0);         // scalability                  else
1269                            BitstreamPutBit(bs, 0);         /* no quarterpel */
1270            }
1271    
1272            BitstreamPutBit(bs, 1);         /* complexity_estimation_disable */
1273            BitstreamPutBit(bs, 1);         /* resync_marker_disable */
1274            BitstreamPutBit(bs, 0);         /* data_partitioned */
1275    
1276            if (vol_ver_id != 1)
1277            {
1278                    BitstreamPutBit(bs, 0);         /* newpred_enable */
1279    
1280                    BitstreamPutBit(bs, (pParam->vol_flags & XVID_VOL_REDUCED_ENABLE)?1:0);
1281                                                                            /* reduced_resolution_vop_enabled */
1282            }
1283    
1284            BitstreamPutBit(bs, 0);         /* scalability */
1285    
1286            /* fake divx5 id, to ensure compatibility with divx5 decoder */
1287    #define DIVX5_ID "DivX000b000p"
1288            if (pParam->max_bframes > 0 && (pParam->global_flags & XVID_GLOBAL_PACKED)) {
1289                    BitstreamWriteUserData(bs, DIVX5_ID, strlen(DIVX5_ID));
1290            }
1291    
1292            /* xvid id */
1293    #define XVID_ID "XviD" XVID_BS_VERSION
1294            BitstreamWriteUserData(bs, XVID_ID, strlen(XVID_ID));
1295  }  }
1296    
1297    
1298  /*  /*
1299    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)  
1300  */  */
1301  void BitstreamWriteVopHeader(Bitstream * const bs,  void
1302    BitstreamWriteVopHeader(
1303                                                    Bitstream * const bs,
1304                                                  const MBParam * pParam,                                                  const MBParam * pParam,
1305                                                  const FRAMEINFO * frame)                                                  const FRAMEINFO * const frame,
1306                                                    int vop_coded)
1307  {  {
 #ifdef BFRAMES  
1308          uint32_t i;          uint32_t i;
1309  #endif  
1310    #if 0
1311      BitstreamPad(bs);      BitstreamPad(bs);
1312    #endif
1313    
1314            /*
1315             * no padding here, anymore. You have to make sure that you are
1316             * byte aligned, and that always 1-8 padding bits have been written
1317             */
1318    
1319      BitstreamPutBits(bs, VOP_START_CODE, 32);      BitstreamPutBits(bs, VOP_START_CODE, 32);
1320    
1321      BitstreamPutBits(bs, frame->coding_type, 2);      BitstreamPutBits(bs, frame->coding_type, 2);
1322    #if 0
1323            DPRINTF(XVID_DEBUG_HEADER, "coding_type = %i\n", frame->coding_type);
1324    #endif
1325    
1326          // time_base = 0  write n x PutBit(1), PutBit(0)          for (i = 0; i < frame->seconds; i++) {
 #ifdef BFRAMES  
         for (i = 0; i < frame->seconds; i++)  
         {  
1327                  BitstreamPutBit(bs, 1);                  BitstreamPutBit(bs, 1);
1328          }          }
1329          BitstreamPutBit(bs, 0);          BitstreamPutBit(bs, 0);
 #else  
         BitstreamPutBits(bs, 0, 1);  
 #endif  
1330    
1331          WRITE_MARKER();          WRITE_MARKER();
1332    
1333          // time_increment: value=nth_of_sec, nbits = log2(resolution)          /* time_increment: value=nth_of_sec, nbits = log2(resolution) */
1334  #ifdef BFRAMES  
1335          BitstreamPutBits(bs, frame->ticks, 5);          BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase));
1336          dprintf("[%i:%i] %c\n", frame->seconds, frame->ticks, frame->coding_type == I_VOP ? 'I' : frame->coding_type == P_VOP ? 'P' : 'B');  #if 0
1337  #else          DPRINTF("[%i:%i] %c",
1338          BitstreamPutBits(bs, 1, 1);                          frame->seconds, frame->ticks,
1339                            frame->coding_type == I_VOP ? 'I' :
1340                            frame->coding_type == P_VOP ? 'P' :
1341                            frame->coding_type == S_VOP ? 'S' :     'B');
1342  #endif  #endif
1343    
1344          WRITE_MARKER();          WRITE_MARKER();
1345    
1346          BitstreamPutBits(bs, 1, 1);                             // vop_coded          if (!vop_coded) {
1347                    BitstreamPutBits(bs, 0, 1);
1348                    return;
1349            }
1350    
1351          if (frame->coding_type != I_VOP)          BitstreamPutBits(bs, 1, 1);     /* vop_coded */
1352    
1353            if ( (frame->coding_type == P_VOP) || (frame->coding_type == S_VOP) )
1354                  BitstreamPutBits(bs, frame->rounding_type, 1);                  BitstreamPutBits(bs, frame->rounding_type, 1);
1355    
1356          BitstreamPutBits(bs, 0, 3);                             // intra_dc_vlc_threshold          if ((frame->vol_flags & XVID_VOL_REDUCED_ENABLE))
1357                    BitstreamPutBit(bs, (frame->vop_flags & XVID_VOP_REDUCED)?1:0);
1358    
1359            BitstreamPutBits(bs, 0, 3);     /* intra_dc_vlc_threshold */
1360    
1361          if (frame->global_flags & XVID_INTERLACING)          if ((frame->vol_flags & XVID_VOL_INTERLACING)) {
1362                    BitstreamPutBit(bs, (frame->vop_flags & XVID_VOP_TOPFIELDFIRST));
1363                    BitstreamPutBit(bs, (frame->vop_flags & XVID_VOP_ALTERNATESCAN));
1364            }
1365    
1366            if (frame->coding_type == S_VOP) {
1367                    if (1)  {               /* no_of_sprite_warping_points>=1 (we use 2!) */
1368                            int k;
1369                            for (k=0;k<2;k++)
1370                            {
1371                                    bs_put_spritetrajectory(bs, frame->warp.duv[k].x ); /* du[k]  */
1372                                    WRITE_MARKER();
1373    
1374                                    bs_put_spritetrajectory(bs, frame->warp.duv[k].y ); /* dv[k]  */
1375                                    WRITE_MARKER();
1376    
1377                            if ((frame->vol_flags & XVID_VOL_QUARTERPEL))
1378          {          {
1379                  BitstreamPutBit(bs, 1);         // top field first                                  DPRINTF(XVID_DEBUG_HEADER,"sprite_warping_point[%i] xy=(%i,%i) *QPEL*\n", k, frame->warp.duv[k].x/2, frame->warp.duv[k].y/2);
1380                  BitstreamPutBit(bs, 0);         // alternate vertical scan                          }
1381                            else
1382                            {
1383                                    DPRINTF(XVID_DEBUG_HEADER,"sprite_warping_point[%i] xy=(%i,%i)\n", k, frame->warp.duv[k].x, frame->warp.duv[k].y);
1384                            }
1385                            }
1386                    }
1387          }          }
1388    
1389          BitstreamPutBits(bs, frame->quant, 5);                  // quantizer  
1390    #if 0
1391            DPRINTF(XVID_DEBUG_HEADER, "quant = %i\n", frame->quant);
1392    #endif
1393    
1394            BitstreamPutBits(bs, frame->quant, 5);  /* quantizer */
1395    
1396          if (frame->coding_type != I_VOP)          if (frame->coding_type != I_VOP)
1397                  BitstreamPutBits(bs, frame->fcode, 3);          // forward_fixed_code                  BitstreamPutBits(bs, frame->fcode, 3);  /* forward_fixed_code */
1398    
1399          if (frame->coding_type == B_VOP)          if (frame->coding_type == B_VOP)
1400                  BitstreamPutBits(bs, frame->bcode, 3);          // backward_fixed_code                  BitstreamPutBits(bs, frame->bcode, 3);  /* backward_fixed_code */
1401    
1402    }
1403    
1404    void
1405    BitstreamWriteUserData(Bitstream * const bs,
1406                                                    uint8_t * data,
1407                                                    const int length)
1408    {
1409            int i;
1410    
1411            BitstreamPad(bs);
1412            BitstreamPutBits(bs, USERDATA_START_CODE, 32);
1413    
1414            for (i = 0; i < length; i++) {
1415                    BitstreamPutBits(bs, data[i], 8);
1416            }
1417    
1418  }  }

Legend:
Removed from v.157  
changed lines
  Added in v.1053

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