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

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

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

revision 1.28.2.6, Tue Nov 12 15:53:23 2002 UTC revision 1.35, Sun Nov 17 00:57:56 2002 UTC
# Line 1  Line 1 
1   /******************************************************************************  /*****************************************************************************
2    *                                                                            *   *
3    *  This file is part of XviD, a free MPEG-4 video encoder/decoder            *   *  XVID MPEG-4 VIDEO CODEC
4    *                                                                            *   *  - Bitstream reader/writer functions -
5    *  XviD is an implementation of a part of one or more MPEG-4 Video tools     *   *
6    *  as specified in ISO/IEC 14496-2 standard.  Those intending to use this    *   *  Copyright (C) 2001-2002 - Peter Ross <pross@xvid.org>
7    *  software module in hardware or software products are advised that its     *   *
8    *  use may infringe existing patents or copyrights, and any such use         *   *  This file is part of XviD, a free MPEG-4 video encoder/decoder
9    *  would be at such party's own risk.  The original developer of this        *   *
10    *  software module and his/her company, and subsequent editors and their     *   *  XviD is free software; you can redistribute it and/or modify it
11    *  companies, will have no liability for use of this software or             *   *  under the terms of the GNU General Public License as published by
12    *  modifications or derivatives thereof.                                     *   *  the Free Software Foundation; either version 2 of the License, or
13    *                                                                            *   *  (at your option) any later version.
14    *  XviD is free software; you can redistribute it and/or modify it           *   *
15    *  under the terms of the GNU General Public License as published by         *   *  This program is distributed in the hope that it will be useful,
16    *  the Free Software Foundation; either version 2 of the License, or         *   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17    *  (at your option) any later version.                                       *   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    *                                                                            *   *  GNU General Public License for more details.
19    *  XviD is distributed in the hope that it will be useful, but               *   *
20    *  WITHOUT ANY WARRANTY; without even the implied warranty of                *   *  You should have received a copy of the GNU General Public License
21    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *   *  along with this program; if not, write to the Free Software
22    *  GNU General Public License for more details.                              *   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23    *                                                                            *   *
24    *  You should have received a copy of the GNU General Public License         *   *  Under section 8 of the GNU General Public License, the copyright
25    *  along with this program; if not, write to the Free Software               *   *  holders of XVID explicitly forbid distribution in the following
26    *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *   *  countries:
27    *                                                                            *   *
28    ******************************************************************************/   *    - Japan
29     *    - United States of America
30   /******************************************************************************   *
31    *                                                                            *   *  Linking XviD statically or dynamically with other modules is making a
32    *  bitstream.c                                                               *   *  combined work based on XviD.  Thus, the terms and conditions of the
33    *                                                                            *   *  GNU General Public License cover the whole combination.
34    *  Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au>                    *   *
35    *                                                                            *   *  As a special exception, the copyright holders of XviD give you
36    *  For more information visit the XviD homepage: http://www.xvid.org         *   *  permission to link XviD with independent modules that communicate with
37    *                                                                            *   *  XviD solely through the VFW1.1 and DShow interfaces, regardless of the
38    ******************************************************************************/   *  license terms of these independent modules, and to copy and distribute
39     *  the resulting combined work under terms of your choice, provided that
40   /******************************************************************************   *  every copy of the combined work is accompanied by a complete copy of
41    *                                                                            *   *  the source code of XviD (the version of XviD used to produce the
42    *  Revision history:                                                         *   *  combined work), being distributed under the terms of the GNU General
43    *                                                                            *   *  Public License plus this exception.  An independent module is a module
44    *  28.10.2002 GMC support - gruel                                                                                        *   *  which is not derived from or based on XviD.
45    *  04.10.2002 qpel support - Isibaar                                                                             *   *
46    *  11.07.2002 add VOP width & height return to dec when dec->width           *   *  Note that people who make modified versions of XviD are not obligated
47    *             or dec->height is 0  (for use in examples/ex1.c)               *   *  to grant this special exception for their modified versions; it is
48    *             MinChen <chenm001@163.com>                                     *   *  their choice whether to do so.  The GNU General Public License gives
49    *  22.05.2002 bs_put_matrix fix                                              *   *  permission to release a modified version without this exception; this
50    *  20.05.2002 added BitstreamWriteUserData                                   *   *  exception also makes it possible to release a modified version which
51    *  19.06.2002 Fix a little bug in use custom quant matrix                    *   *  carries forward this exception.
52    *             MinChen <chenm001@163.com>                                     *   *
53    *  08.05.2002 add low_delay support for B_VOP decode                         *   * $Id$
54    *             MinChen <chenm001@163.com>                                     *   *
55    *  06.05.2002 low_delay                                                      *   ****************************************************************************/
   *  06.05.2002 fixed fincr/fbase error                                        *  
   *  01.05.2002 added BVOP support to BitstreamWriteVopHeader                  *  
   *  15.04.2002 rewrite log2bin use asm386  By MinChen <chenm001@163.com>      *  
   *  26.03.2002 interlacing support                                            *  
   *  03.03.2002 qmatrix writing                                                *  
   *  03.03.2002 merged BITREADER and BITWRITER                                 *  
   *  30.02.2002 intra_dc_threshold support                                     *  
   *  04.12.2001 support for additional headers                                 *  
   *  16.12.2001 inital version                                                 *  
   *                                                                                                                                                        *  
   ******************************************************************************/  
56    
   
 #include <string.h>  
57  #include "bitstream.h"  #include "bitstream.h"
58  #include "zigzag.h"  #include "zigzag.h"
59  #include "../quant/quant_matrix.h"  #include "../quant/quant_matrix.h"
 #include "mbcoding.h"  
60    
61    /*****************************************************************************
62     * Functions
63     ****************************************************************************/
64    
65  static uint32_t __inline  static uint32_t __inline
66  log2bin(uint32_t value)  log2bin(uint32_t value)
67  {  {
68  /* Changed by Chenm001 */  /* Changed by Chenm001 */
69  #ifndef WIN32  #if !defined(_MSC_VER)
70          int n = 0;          int n = 0;
71    
72          while (value) {          while (value) {
# Line 132  Line 121 
121  // for BVOP addbits == max(fcode,bcode) - 1  // for BVOP addbits == max(fcode,bcode) - 1
122  // returns mbpos  // returns mbpos
123  int  int
124  read_video_packet_header(Bitstream *bs,  read_video_packet_header(Bitstream *bs, const int addbits, int * quant)
                                                 DECODER * dec,  
                                                 const int addbits,  
                                                 int * quant,  
                                                 int * fcode_forward,  
                                                 int  * fcode_backward,  
                                                 int * intra_dc_threshold)  
125  {  {
126          int startcode_bits = NUMBITS_VP_RESYNC_MARKER + addbits;          int nbits;
         int mbnum_bits = log2bin(dec->mb_width *  dec->mb_height - 1);  
127          int mbnum;          int mbnum;
128          int hec = 0;          int hec;
129    
130            nbits = NUMBITS_VP_RESYNC_MARKER + addbits;
131    
132          BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs));          BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs));
133          BitstreamSkip(bs, startcode_bits);          BitstreamSkip(bs, nbits);
134    
135          DPRINTF(DPRINTF_STARTCODE, "<video_packet_header>");          DPRINTF(DPRINTF_STARTCODE, "<video_packet_header>");
136    
137          if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)          // if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
138          {                  // hec
139                  hec = BitstreamGetBit(bs);              /* header_extension_code */                  // vop_width
140                  if (hec && !(dec->sprite_enable == SPRITE_STATIC /* && current_coding_type = I_VOP */))                  // marker_bit
141                  {                  // vop_height
142                          BitstreamSkip(bs, 13);                  /* vop_width */                  // marker_bit
143                          READ_MARKER();  
144                          BitstreamSkip(bs, 13);                  /* vop_height */          //}
                         READ_MARKER();  
                         BitstreamSkip(bs, 13);                  /* vop_horizontal_mc_spatial_ref */  
                         READ_MARKER();  
                         BitstreamSkip(bs, 13);                  /* vop_vertical_mc_spatial_ref */  
                         READ_MARKER();  
                 }  
         }  
145    
146          mbnum = BitstreamGetBits(bs, mbnum_bits);               /* macroblock_number */          mbnum = BitstreamGetBits(bs, 9);
147          DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum);          DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum);
148    
149          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)          // if (dec->shape != VIDOBJLAY_SHAPE_BINARYONLY)
150          {          *quant = BitstreamGetBits(bs, 5);
                 *quant = BitstreamGetBits(bs, 5);       /* quant_scale */  
151                  DPRINTF(DPRINTF_HEADER, "quant %i", *quant);                  DPRINTF(DPRINTF_HEADER, "quant %i", *quant);
         }  
   
         if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR)  
                 hec = BitstreamGetBit(bs);              /* header_extension_code */  
   
152    
153            // if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)
154            hec = BitstreamGetBit(bs);
155          DPRINTF(DPRINTF_HEADER, "header_extension_code %i", hec);          DPRINTF(DPRINTF_HEADER, "header_extension_code %i", hec);
156          if (hec)          // if (hec)
157          {          //   .. decoder hec-header ...
                 int time_base;  
                 int time_increment;  
                 int coding_type;  
   
                 for (time_base=0; BitstreamGetBit(bs)!=0; time_base++);         /* modulo_time_base */  
                 READ_MARKER();  
                 if (dec->time_inc_bits)  
                         time_increment = (BitstreamGetBits(bs, dec->time_inc_bits));    /* vop_time_increment */  
                 READ_MARKER();  
                 DPRINTF(DPRINTF_HEADER,"time %i:%i", time_base, time_increment);  
   
                 coding_type = BitstreamGetBits(bs, 2);  
                 DPRINTF(DPRINTF_HEADER,"coding_type %i", coding_type);  
   
                 if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)  
                 {  
                         BitstreamSkip(bs, 1);   /* change_conv_ratio_disable */  
                         if (coding_type != I_VOP)  
                                 BitstreamSkip(bs, 1);   /* vop_shape_coding_type */  
                 }  
   
                 if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)  
                 {  
                         *intra_dc_threshold = intra_dc_threshold_table[BitstreamGetBits(bs, 3)];  
   
                         if (dec->sprite_enable == SPRITE_GMC && coding_type == S_VOP &&  
                                 dec->sprite_warping_points > 0)  
                         {  
                                 // TODO: sprite trajectory  
                         }  
                         if (dec->reduced_resolution_enable &&  
                                 dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR &&  
                                 (coding_type == P_VOP || coding_type == I_VOP))  
                         {  
                                 BitstreamSkip(bs, 1); /* vop_reduced_resolution */  
                         }  
   
                         if (coding_type != I_VOP && fcode_forward)  
                         {  
                                 *fcode_forward = BitstreamGetBits(bs, 3);  
                                 DPRINTF(DPRINTF_HEADER,"fcode_forward %i", *fcode_forward);  
                         }  
   
                         if (coding_type == B_VOP && fcode_backward)  
                         {  
                                 *fcode_backward = BitstreamGetBits(bs, 3);  
                                 DPRINTF(DPRINTF_HEADER,"fcode_backward %i", fcode_backward);  
                         }  
                 }  
   
         }  
   
         if (dec->newpred_enable)  
         {  
                 int vop_id;  
                 int vop_id_for_prediction;  
   
                 vop_id = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15));  
                 DPRINTF(DPRINTF_HEADER, "vop_id %i", vop_id);  
                 if (BitstreamGetBit(bs))        /* vop_id_for_prediction_indication */  
                 {  
                         vop_id_for_prediction = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15));  
                         DPRINTF(DPRINTF_HEADER, "vop_id_for_prediction %i", vop_id_for_prediction);  
                 }  
                 READ_MARKER();  
         }  
158    
159          return mbnum;          return mbnum;
160  }  }
# Line 266  Line 173 
173  BitstreamReadHeaders(Bitstream * bs,  BitstreamReadHeaders(Bitstream * bs,
174                                           DECODER * dec,                                           DECODER * dec,
175                                           uint32_t * rounding,                                           uint32_t * rounding,
                                          uint32_t * reduced_resolution,  
176                                           uint32_t * quant,                                           uint32_t * quant,
177                                           uint32_t * fcode_forward,                                           uint32_t * fcode_forward,
178                                           uint32_t * fcode_backward,                                           uint32_t * fcode_backward,
179                                           uint32_t * intra_dc_threshold,                                           uint32_t * intra_dc_threshold)
                                          VECTOR * gmc_mv)  
180  {  {
181          uint32_t vol_ver_id;          uint32_t vol_ver_id;
182            static uint32_t time_increment_resolution;
183          uint32_t coding_type;          uint32_t coding_type;
184          uint32_t start_code;          uint32_t start_code;
185          uint32_t time_incr = 0;          uint32_t time_incr = 0;
186          int32_t time_increment;          int32_t time_increment = 0;
         int resize = 0;  
187    
188          do {          do {
   
189                  BitstreamByteAlign(bs);                  BitstreamByteAlign(bs);
190                  start_code = BitstreamShowBits(bs, 32);                  start_code = BitstreamShowBits(bs, 32);
191    
# Line 355  Line 259 
259                          BitstreamSkip(bs, 1);   // random_accessible_vol                          BitstreamSkip(bs, 1);   // random_accessible_vol
260    
261                          // video_object_type_indication                          // video_object_type_indication
262                          if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE &&                          if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE && BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE && BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN && BitstreamShowBits(bs, 8) != 0)   // BUGGY DIVX
                                 BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE &&  
                                 BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN &&  
                                 BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_ACE &&  
                                 BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_ART_SIMPLE &&  
                                 BitstreamShowBits(bs, 8) != 0)  // BUGGY DIVX  
263                          {                          {
264                                  DPRINTF(DPRINTF_ERROR,"video_object_type_indication %i not supported ",                                  DPRINTF(DPRINTF_ERROR,"video_object_type_indication %i not supported ",
265                                          BitstreamShowBits(bs, 8));                                          BitstreamShowBits(bs, 8));
# Line 379  Line 278 
278                                  vol_ver_id = 1;                                  vol_ver_id = 1;
279                          }                          }
280    
281                          dec->aspect_ratio = BitstreamGetBits(bs, 4);                          if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR)     // aspect_ratio_info
   
                         if (dec->aspect_ratio == VIDOBJLAY_AR_EXTPAR)   // aspect_ratio_info  
282                          {                          {
283                                  DPRINTF(DPRINTF_HEADER, "+ aspect_ratio_info");                                  DPRINTF(DPRINTF_HEADER, "+ aspect_ratio_info");
284                                  dec->par_width = BitstreamGetBits(bs, 8);       // par_width                                  BitstreamSkip(bs, 8);   // par_width
285                                  dec->par_height = BitstreamGetBits(bs, 8);      // par_height                                  BitstreamSkip(bs, 8);   // par_height
286                          }                          }
287    
288                          if (BitstreamGetBit(bs))        // vol_control_parameters                          if (BitstreamGetBit(bs))        // vol_control_parameters
# Line 408  Line 305 
305                                          READ_MARKER();                                          READ_MARKER();
306                                          BitstreamSkip(bs, 15);  // latter_half_vbv_occupancy                                          BitstreamSkip(bs, 15);  // latter_half_vbv_occupancy
307                                          READ_MARKER();                                          READ_MARKER();
308    
309                                  }                                  }
310                          }                          }
311    
312                          dec->shape = BitstreamGetBits(bs, 2);   // video_object_layer_shape                          dec->shape = BitstreamGetBits(bs, 2);   // video_object_layer_shape
   
313                          DPRINTF(DPRINTF_HEADER, "shape %i", dec->shape);                          DPRINTF(DPRINTF_HEADER, "shape %i", dec->shape);
                         if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)  
                         {  
                                 DPRINTF(DPRINTF_ERROR,"non-rectangular shapes are not supported");  
                         }  
314    
315                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) {                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) {
316                                  BitstreamSkip(bs, 4);   // video_object_layer_shape_extension                                  BitstreamSkip(bs, 4);   // video_object_layer_shape_extension
# Line 426  Line 319 
319                          READ_MARKER();                          READ_MARKER();
320    
321  // *************************** for decode B-frame time ***********************  // *************************** for decode B-frame time ***********************
322                          dec->time_inc_resolution = BitstreamGetBits(bs, 16);    // vop_time_increment_resolution                          time_increment_resolution = BitstreamGetBits(bs, 16);   // vop_time_increment_resolution
323                          DPRINTF(DPRINTF_HEADER,"vop_time_increment_resolution %i", dec->time_inc_resolution);  
324                            DPRINTF(DPRINTF_HEADER,"vop_time_increment_resolution %i", time_increment_resolution);
325    
326  //                      dec->time_inc_resolution--;  //                      time_increment_resolution--;
327    
328                          if (dec->time_inc_resolution > 0) {                          if (time_increment_resolution > 0) {
329                                  dec->time_inc_bits = log2bin(dec->time_inc_resolution-1);                                  dec->time_inc_bits = log2bin(time_increment_resolution-1);
330                          } else {                          } else {
331                                  // dec->time_inc_bits = 0;                                  // dec->time_inc_bits = 0;
332                                  // for "old" xvid compatibility, set time_inc_bits = 1                                  // for "old" xvid compatibility, set time_inc_bits = 1
# Line 461  Line 355 
355                                          DPRINTF(DPRINTF_HEADER, "width %i", width);                                          DPRINTF(DPRINTF_HEADER, "width %i", width);
356                                          DPRINTF(DPRINTF_HEADER, "height %i", height);                                          DPRINTF(DPRINTF_HEADER, "height %i", height);
357    
358                                          if (dec->width != width || dec->height != height)                                          // for auto set width & height
359                                          {                                          if (dec->width == 0)
                                                 if (dec->fixed_dimensions)  
                                                 {  
                                                         DPRINTF(DPRINTF_ERROR, "XVID_DEC_PARAM width/height does not match bitstream");  
                                                         return -1;  
                                                 }  
                                                 resize = 1;  
360                                                  dec->width = width;                                                  dec->width = width;
361                                            if (dec->height == 0)
362                                                  dec->height = height;                                                  dec->height = height;
363    
364                                            if (width != dec->width || height != dec->height) {
365                                                    DPRINTF(DPRINTF_ERROR, "XVID_DEC_PARAM width/height does not match bitstream");
366                                                    return -1;
367                                          }                                          }
368    
369                                  }                                  }
370    
371                                  dec->interlacing = BitstreamGetBit(bs);                                  dec->interlacing = BitstreamGetBit(bs);
372                                  DPRINTF(DPRINTF_HEADER, "interlacing %i", dec->interlacing);                                  DPRINTF(DPRINTF_HEADER, "interlace", dec->interlacing);
373    
374                                  if (!BitstreamGetBit(bs))       // obmc_disable                                  if (!BitstreamGetBit(bs))       // obmc_disable
375                                  {                                  {
# Line 484  Line 378 
378                                          // fucking divx4.02 has this enabled                                          // fucking divx4.02 has this enabled
379                                  }                                  }
380    
381                                  dec->sprite_enable = BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2));   // sprite_enable                                  if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2)))    // sprite_enable
   
                                 if (dec->sprite_enable == SPRITE_STATIC || dec->sprite_enable == SPRITE_GMC)  
382                                  {                                  {
383                                          int low_latency_sprite_enable;                                          DPRINTF(DPRINTF_ERROR, "spriate_enabled not supported");
384                                            return -1;
                                         if (dec->sprite_enable != SPRITE_GMC)  
                                         {  
                                                 int sprite_width;  
                                                 int sprite_height;  
                                                 int sprite_left_coord;  
                                                 int sprite_top_coord;  
                                                 sprite_width = BitstreamGetBits(bs, 13);                // sprite_width  
                                                 READ_MARKER();  
                                                 sprite_height = BitstreamGetBits(bs, 13);       // sprite_height  
                                                 READ_MARKER();  
                                                 sprite_left_coord = BitstreamGetBits(bs, 13);   // sprite_left_coordinate  
                                                 READ_MARKER();  
                                                 sprite_top_coord = BitstreamGetBits(bs, 13);    // sprite_top_coordinate  
                                                 READ_MARKER();  
                                         }  
                                         dec->sprite_warping_points = BitstreamGetBits(bs, 6);           // no_of_sprite_warping_points  
                                         dec->sprite_warping_accuracy = BitstreamGetBits(bs, 2);         // sprite_warping_accuracy  
                                         dec->sprite_brightness_change = BitstreamGetBits(bs, 1);                // brightness_change  
                                         if (dec->sprite_enable != SPRITE_GMC)  
                                         {  
                                                 low_latency_sprite_enable = BitstreamGetBits(bs, 1);            // low_latency_sprite_enable  
                                         }  
385                                  }                                  }
386    
387                                  if (vol_ver_id != 1 &&                                  if (vol_ver_id != 1 &&
# Line 569  Line 439 
439    
440    
441                                  if (vol_ver_id != 1) {                                  if (vol_ver_id != 1) {
442                                            DPRINTF(DPRINTF_DEBUG, "QUARTERPEL BITSTREAM");
443                                          dec->quarterpel = BitstreamGetBit(bs);  // quarter_sample                                          dec->quarterpel = BitstreamGetBit(bs);  // quarter_sample
                                         DPRINTF(DPRINTF_HEADER,"quarterpel %i", dec->quarterpel);  
444                                  }                                  }
445                                  else                                  else
446                                          dec->quarterpel = 0;                                          dec->quarterpel = 0;
# Line 591  Line 461 
461                                  }                                  }
462    
463                                  if (vol_ver_id != 1) {                                  if (vol_ver_id != 1) {
464                                          dec->newpred_enable = BitstreamGetBit(bs);                                          if (BitstreamGetBit(bs))        // newpred_enable
                                         if (dec->newpred_enable)        // newpred_enable  
465                                          {                                          {
466                                                  DPRINTF(DPRINTF_HEADER, "+ newpred_enable");                                                  DPRINTF(DPRINTF_HEADER, "+ newpred_enable");
467                                                  BitstreamSkip(bs, 2);   // requested_upstream_message_type                                                  BitstreamSkip(bs, 2);   // requested_upstream_message_type
468                                                  BitstreamSkip(bs, 1);   // newpred_segment_type                                                  BitstreamSkip(bs, 1);   // newpred_segment_type
469                                          }                                          }
470                                          dec->reduced_resolution_enable = BitstreamGetBit(bs);                                          if (BitstreamGetBit(bs))        // reduced_resolution_vop_enable
                                         if (dec->reduced_resolution_enable)     // reduced_resolution_vop_enable  
471                                          {                                          {
472                                                  DPRINTF(DPRINTF_ERROR, "reduced_resolution_vop not supported");                                                  DPRINTF(DPRINTF_ERROR, "reduced_resolution_vop not supported");
473                                                  //return -1;                                                  return -1;
                                         }  
474                                  }                                  }
                                 else  
                                 {  
                                         dec->newpred_enable = 0;  
                                         dec->reduced_resolution_enable = 0;  
475                                  }                                  }
476    
477                                  if ((dec->scalability = BitstreamGetBit(bs)))   // scalability                                  if ((dec->scalability = BitstreamGetBit(bs)))   // scalability
# Line 629  Line 492 
492    
493                          }                          }
494    
                         return (resize ? -3 : -2 );     /* VOL */  
   
495                  } else if (start_code == GRPOFVOP_START_CODE) {                  } else if (start_code == GRPOFVOP_START_CODE) {
496    
497                          DPRINTF(DPRINTF_STARTCODE, "<group_of_vop>");                          DPRINTF(DPRINTF_STARTCODE, "<group_of_vop>");
# Line 672  Line 533 
533                          DPRINTF(DPRINTF_HEADER, "time_increment %i", time_increment);                          DPRINTF(DPRINTF_HEADER, "time_increment %i", time_increment);
534    
535                          DPRINTF(DPRINTF_TIMECODE, "%c %i:%i",                          DPRINTF(DPRINTF_TIMECODE, "%c %i:%i",
536                                  coding_type == I_VOP ? 'I' : coding_type == P_VOP ? 'P' : coding_type == B_VOP ? 'B' : 'S',                                  coding_type == I_VOP ? 'I' : coding_type == P_VOP ? 'P' : 'B',
537                                  time_incr, time_increment);                                  time_incr, time_increment);
538    
539                          if (coding_type != B_VOP) {                          if (coding_type != B_VOP) {
# Line 680  Line 541 
541                                  dec->time_base += time_incr;                                  dec->time_base += time_incr;
542                                  dec->time = time_increment;                                  dec->time = time_increment;
543    
544  /*                                      dec->time_base * dec->time_inc_resolution +  /*                                      dec->time_base * time_increment_resolution +
545                                          time_increment;                                          time_increment;
546  */                              dec->time_pp = (uint32_t)  */                              dec->time_pp = (uint32_t)
547                                          (dec->time_inc_resolution + dec->time - dec->last_non_b_time)%dec->time_inc_resolution;                                          (time_increment_resolution + dec->time - dec->last_non_b_time)%time_increment_resolution;
548                                  dec->last_non_b_time = dec->time;                                  dec->last_non_b_time = dec->time;
549                          } else {                          } else {
550                                  dec->time = time_increment;                                  dec->time = time_increment;
551  /*  /*
552                                          (dec->last_time_base +                                          (dec->last_time_base +
553                                           time_incr) * dec->time_inc_resolution + time_increment;                                           time_incr) * time_increment_resolution + time_increment;
554  */  */
555                                  dec->time_bp = (uint32_t)                                  dec->time_bp = (uint32_t)
556                                          (dec->time_inc_resolution + dec->last_non_b_time - dec->time)%dec->time_inc_resolution;                                          (time_increment_resolution + dec->last_non_b_time - dec->time)%time_increment_resolution;
557                          }                          }
558    
559                          READ_MARKER();                          READ_MARKER();
# Line 703  Line 564 
564                                  return N_VOP;                                  return N_VOP;
565                          }                          }
566    
567                          if (dec->newpred_enable)                          /* if (newpred_enable)
568                          {                          {
                                 int vop_id;  
                                 int vop_id_for_prediction;  
   
                                 vop_id = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15));  
                                 DPRINTF(DPRINTF_HEADER, "vop_id %i", vop_id);  
                                 if (BitstreamGetBit(bs))        /* vop_id_for_prediction_indication */  
                                 {  
                                         vop_id_for_prediction = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15));  
                                         DPRINTF(DPRINTF_HEADER, "vop_id_for_prediction %i", vop_id_for_prediction);  
569                                  }                                  }
570                                  READ_MARKER();                           */
                         }  
   
   
571    
572                          // fix a little bug by MinChen <chenm002@163.com>                          // fix a little bug by MinChen <chenm002@163.com>
573                          if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) &&                          if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) &&
574                                  ( (coding_type == P_VOP) || (coding_type == S_VOP && dec->sprite_enable == SPRITE_GMC) ) ) {                                  (coding_type == P_VOP)) {
575                                  *rounding = BitstreamGetBit(bs);        // rounding_type                                  *rounding = BitstreamGetBit(bs);        // rounding_type
576                                  DPRINTF(DPRINTF_HEADER, "rounding %i", *rounding);                                  DPRINTF(DPRINTF_HEADER, "rounding %i", *rounding);
577                          }                          }
578    
579                          if (dec->reduced_resolution_enable &&                          /* if (reduced_resolution_enable)
                                 dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR &&  
                                 (coding_type == P_VOP || coding_type == I_VOP)) {  
   
                                 *reduced_resolution = BitstreamGetBit(bs);  
                         }  
                         else  
580                          {                          {
                                 *reduced_resolution = 0;  
581                          }                          }
582                             */
583    
584                          if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {                          if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
                                 if(!(dec->sprite_enable == SPRITE_STATIC && coding_type == I_VOP)) {  
   
585                                          uint32_t width, height;                                          uint32_t width, height;
586                                          uint32_t horiz_mc_ref, vert_mc_ref;                                          uint32_t horiz_mc_ref, vert_mc_ref;
587    
# Line 757  Line 598 
598                                          DPRINTF(DPRINTF_HEADER, "height %i", height);                                          DPRINTF(DPRINTF_HEADER, "height %i", height);
599                                          DPRINTF(DPRINTF_HEADER, "horiz_mc_ref %i", horiz_mc_ref);                                          DPRINTF(DPRINTF_HEADER, "horiz_mc_ref %i", horiz_mc_ref);
600                                          DPRINTF(DPRINTF_HEADER, "vert_mc_ref %i", vert_mc_ref);                                          DPRINTF(DPRINTF_HEADER, "vert_mc_ref %i", vert_mc_ref);
                                 }  
601    
602                                  BitstreamSkip(bs, 1);   // change_conv_ratio_disable                                  BitstreamSkip(bs, 1);   // change_conv_ratio_disable
603                                  if (BitstreamGetBit(bs))        // vop_constant_alpha                                  if (BitstreamGetBit(bs))        // vop_constant_alpha
# Line 766  Line 606 
606                                  }                                  }
607                          }                          }
608    
609    
610                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) {                          if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) {
611                                  // intra_dc_vlc_threshold                                  // intra_dc_vlc_threshold
612                                  *intra_dc_threshold =                                  *intra_dc_threshold =
613                                          intra_dc_threshold_table[BitstreamGetBits(bs, 3)];                                          intra_dc_threshold_table[BitstreamGetBits(bs, 3)];
614    
                                 dec->top_field_first = 0;  
                                 dec->alternate_vertical_scan = 0;  
   
615                                  if (dec->interlacing) {                                  if (dec->interlacing) {
616                                          dec->top_field_first = BitstreamGetBit(bs);                                          dec->top_field_first = BitstreamGetBit(bs);
617                                          DPRINTF(DPRINTF_HEADER, "interlace top_field_first %i", dec->top_field_first);                                          DPRINTF(DPRINTF_HEADER, "interlace top_field_first %i", dec->top_field_first);
# Line 783  Line 621 
621                                  }                                  }
622                          }                          }
623    
                         if ((dec->sprite_enable == SPRITE_STATIC || dec->sprite_enable== SPRITE_GMC) && coding_type == S_VOP) {  
   
                                 int i;  
   
                                 for (i = 0 ; i < dec->sprite_warping_points; i++)  
                                 {  
                                         int length;  
                                         int x = 0, y = 0;  
   
                                         /* sprite code borowed from ffmpeg; thx Michael Niedermayer <michaelni@gmx.at> */  
                                         length = bs_get_spritetrajectory(bs);  
                                         if(length){  
                                                 x= BitstreamGetBits(bs, length);  
                                                 if ((x >> (length - 1)) == 0) /* if MSB not set it is negative*/  
                                                         x = - (x ^ ((1 << length) - 1));  
                                         }  
                                         READ_MARKER();  
   
                                         length = bs_get_spritetrajectory(bs);  
                                         if(length){  
                                                 y = BitstreamGetBits(bs, length);  
                                                 if ((y >> (length - 1)) == 0) /* if MSB not set it is negative*/  
                                                         y = - (y ^ ((1 << length) - 1));  
                                         }  
                                         READ_MARKER();  
   
                                         gmc_mv[i].x = x;  
                                         gmc_mv[i].y = y;  
   
                                         DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i)", i, x, y);  
                                 }  
   
                                 if (dec->sprite_brightness_change)  
                                 {  
                                         // XXX: brightness_change_factor()  
                                 }  
                                 if (dec->sprite_enable == SPRITE_STATIC)  
                                 {  
                                         // XXX: todo  
                                 }  
   
                         }  
   
624                          if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1)       // vop_quant                          if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1)       // vop_quant
625                                  *quant = 1;                                  *quant = 1;
626    
627                          DPRINTF(DPRINTF_HEADER, "quant %i", *quant);                          DPRINTF(DPRINTF_HEADER, "quant %i", *quant);
628    
629                          if (coding_type != I_VOP) {                          if (coding_type != I_VOP) {
# Line 848  Line 644 
644                          return coding_type;                          return coding_type;
645    
646                  } else if (start_code == USERDATA_START_CODE) {                  } else if (start_code == USERDATA_START_CODE) {
                         char tmp[256];  
                     int i;  
   
                         BitstreamSkip(bs, 32);  // user_data_start_code  
647    
648                          tmp[0] = BitstreamShowBits(bs, 8);                          DPRINTF(DPRINTF_STARTCODE, "<user_data>");
649    
650                          for(i = 1; i < 256; i++){                          BitstreamSkip(bs, 32);  // user_data_start_code
                                 tmp[i] = (BitstreamShowBits(bs, 16) & 0xFF);  
   
                                 if(tmp[i] == 0)  
                                         break;  
   
                                 BitstreamSkip(bs, 8);  
                         }  
   
                         DPRINTF(DPRINTF_STARTCODE, "<user_data>: %s\n", tmp);  
   
                         if(strncmp(tmp, "DivX501b481p", 12) == 0) {  
                                 dec->packed_mode = 1;  
                                 DPRINTF(DPRINTF_STARTCODE, "packed_mode = %d\n", dec->packed_mode);  
651    
                         }  
652                  } else                                  // start_code == ?                  } else                                  // start_code == ?
653                  {                  {
654                          if (BitstreamShowBits(bs, 24) == 0x000001) {                          if (BitstreamShowBits(bs, 24) == 0x000001) {
# Line 913  Line 691 
691  void  void
692  BitstreamWriteVolHeader(Bitstream * const bs,  BitstreamWriteVolHeader(Bitstream * const bs,
693                                                  const MBParam * pParam,                                                  const MBParam * pParam,
694                                                  const FRAMEINFO * const frame)                                                  const FRAMEINFO * frame)
695  {  {
         int vol_ver_id=1;  
   
         if ( (pParam->m_quarterpel) || (frame->global_flags & XVID_GMC) )  
                 vol_ver_id = 2;  
   
696          // video object_start_code & vo_id          // video object_start_code & vo_id
697          BitstreamPad(bs);          BitstreamPad(bs);
698          BitstreamPutBits(bs, VO_START_CODE, 27);          BitstreamPutBits(bs, VO_START_CODE, 27);
# Line 931  Line 704 
704    
705          BitstreamPutBit(bs, 0);         // random_accessible_vol          BitstreamPutBit(bs, 0);         // random_accessible_vol
706          BitstreamPutBits(bs, 0, 8);     // video_object_type_indication          BitstreamPutBits(bs, 0, 8);     // video_object_type_indication
   
         if (vol_ver_id == 1)  
         {  
707                  BitstreamPutBit(bs, 0);                         // is_object_layer_identified (0=not given)                  BitstreamPutBit(bs, 0);                         // is_object_layer_identified (0=not given)
         }  
         else  
         {  
                 BitstreamPutBit(bs, 1);         // is_object_layer_identified  
                 BitstreamPutBits(bs, vol_ver_id, 4);    // vol_ver_id == 2  
                 BitstreamPutBits(bs, 4, 3); // vol_ver_priority (1==lowest, 7==highest) ??  
         }  
   
708          BitstreamPutBits(bs, 1, 4);     // aspect_ratio_info (1=1:1)          BitstreamPutBits(bs, 1, 4);     // aspect_ratio_info (1=1:1)
709    
710          BitstreamPutBit(bs, 1); // vol_control_parameters          BitstreamPutBit(bs, 1); // vol_control_parameters
711          BitstreamPutBits(bs, 1, 2);     // chroma_format 1="4:2:0"          BitstreamPutBits(bs, 1, 2);     // chroma_format 1="4:2:0"
712    
         if (pParam->max_bframes > 0) {  
                 BitstreamPutBit(bs, 0); // low_delay  
         } else  
         {  
713                  BitstreamPutBit(bs, 1); // low_delay                  BitstreamPutBit(bs, 1); // low_delay
714          }  
715          BitstreamPutBit(bs, 0); // vbv_parameters (0=not given)          BitstreamPutBit(bs, 0); // vbv_parameters (0=not given)
716    
717          BitstreamPutBits(bs, 0, 2);     // video_object_layer_shape (0=rectangular)          BitstreamPutBits(bs, 0, 2);     // video_object_layer_shape (0=rectangular)
718    
719          WRITE_MARKER();          WRITE_MARKER();
720    
721          /* time_inc_resolution; ignored by current decore versions          /*
722             eg. 2fps     res=2       inc=1           * time_increment_resolution; ignored by current decore versions
723             25fps        res=25      inc=1           *  eg. 2fps     res=2       inc=1
724             29.97fps res=30000   inc=1001           *      25fps    res=25      inc=1
725             *      29.97fps res=30000   inc=1001
726           */           */
727          BitstreamPutBits(bs, pParam->fbase, 16);          BitstreamPutBits(bs, pParam->fbase, 16);
728    
729    
730          WRITE_MARKER();          WRITE_MARKER();
731    
732          BitstreamPutBit(bs, 1);         // fixed_vop_rate = 1          BitstreamPutBit(bs, 1);         // fixed_vop_rate = 1
# Line 980  Line 740 
740    
741          BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING);    // interlace          BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING);    // interlace
742          BitstreamPutBit(bs, 1);         // obmc_disable (overlapped block motion compensation)          BitstreamPutBit(bs, 1);         // obmc_disable (overlapped block motion compensation)
743            BitstreamPutBit(bs, 0);         // sprite_enable
744          if (vol_ver_id != 1)          BitstreamPutBit(bs, 0);         // not_in_bit
         {       if (frame->global_flags & XVID_GMC)  
                 {       BitstreamPutBits(bs, 2, 2);             // sprite_enable=='GMC'  
                         BitstreamPutBits(bs, 2, 6);             // no_of_sprite_warping_points  
                         BitstreamPutBits(bs, 3, 2);             // sprite_warping_accuracy 0==1/2, 1=1/4, 2=1/8, 3=1/16  
                         BitstreamPutBit(bs, 0);                 // sprite_brightness_change (not supported)  
   
 /* currently we use no_of_sprite_warping_points==2, sprite_warping_accuracy==3  
    for DivX5 compatability */  
   
                 } else  
                         BitstreamPutBits(bs, 0, 2);             // sprite_enable==off  
         }  
         else  
                 BitstreamPutBit(bs, 0);         // sprite_enable==off  
   
         BitstreamPutBit(bs, 0);         // not_8_bit  
745    
746          // quant_type   0=h.263  1=mpeg4(quantizer tables)          // quant_type   0=h.263  1=mpeg4(quantizer tables)
747          BitstreamPutBit(bs, pParam->m_quant_type);          BitstreamPutBit(bs, pParam->m_quant_type);
# Line 1015  Line 759 
759    
760          }          }
761    
         if (vol_ver_id != 1) {  
                 if (pParam->m_quarterpel)  
                         BitstreamPutBit(bs, 1);         //  quarterpel  
                 else  
                         BitstreamPutBit(bs, 0);         // no quarterpel  
         }  
   
762          BitstreamPutBit(bs, 1);         // complexity_estimation_disable          BitstreamPutBit(bs, 1);         // complexity_estimation_disable
763          BitstreamPutBit(bs, 1);         // resync_marker_disable          BitstreamPutBit(bs, 1);         // resync_marker_disable
764          BitstreamPutBit(bs, 0);         // data_partitioned          BitstreamPutBit(bs, 0);         // data_partitioned
   
         if (vol_ver_id != 1)  
         {  
                 BitstreamPutBit(bs, 0);         // newpred_enable  
                 BitstreamPutBit(bs, 0);         // reduced_resolution_vop_enabled  
         }  
   
765          BitstreamPutBit(bs, 0);         // scalability          BitstreamPutBit(bs, 0);         // scalability
   
766  }  }
767    
768    
769  /*  /*
770    write vop header    write vop header
771    
772      NOTE: doesnt handle bother with time_base & time_inc
773      time_base = n seconds since last resync (eg. last iframe)
774      time_inc = nth of a second since last resync
775      (decoder uses these values to determine precise time since last resync)
776  */  */
777  void  void
778  BitstreamWriteVopHeader(Bitstream * const bs,  BitstreamWriteVopHeader(Bitstream * const bs,
779                                                  const MBParam * pParam,                                                  const MBParam * pParam,
780                                                  const FRAMEINFO * const frame,                                                  const FRAMEINFO * frame,
781                                                  int vop_coded)                                                  int vop_coded)
782  {  {
783          uint32_t i;          uint32_t i;
# Line 1052  Line 786 
786          BitstreamPutBits(bs, VOP_START_CODE, 32);          BitstreamPutBits(bs, VOP_START_CODE, 32);
787    
788          BitstreamPutBits(bs, frame->coding_type, 2);          BitstreamPutBits(bs, frame->coding_type, 2);
         DPRINTF(DPRINTF_HEADER, "coding_type = %i", frame->coding_type);  
789    
790            // time_base = 0  write n x PutBit(1), PutBit(0)
791          for (i = 0; i < frame->seconds; i++) {          for (i = 0; i < frame->seconds; i++) {
792                  BitstreamPutBit(bs, 1);                  BitstreamPutBit(bs, 1);
793          }          }
# Line 1062  Line 796 
796          WRITE_MARKER();          WRITE_MARKER();
797    
798          // time_increment: value=nth_of_sec, nbits = log2(resolution)          // time_increment: value=nth_of_sec, nbits = log2(resolution)
   
799          BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase));          BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase));
         /*DPRINTF("[%i:%i] %c", frame->seconds, frame->ticks,  
                         frame->coding_type == I_VOP ? 'I' : frame->coding_type ==  
                         P_VOP ? 'P' : 'B');*/  
800    
801          WRITE_MARKER();          WRITE_MARKER();
802    
# Line 1077  Line 807 
807    
808          BitstreamPutBits(bs, 1, 1);     // vop_coded          BitstreamPutBits(bs, 1, 1);     // vop_coded
809    
810          if ( (frame->coding_type == P_VOP) || (frame->coding_type == S_VOP) )          if (frame->coding_type == P_VOP)
811                  BitstreamPutBits(bs, frame->rounding_type, 1);                  BitstreamPutBits(bs, frame->rounding_type, 1);
812    
813          BitstreamPutBits(bs, 0, 3);     // intra_dc_vlc_threshold          BitstreamPutBits(bs, 0, 3);     // intra_dc_vlc_threshold
814    
815          if (frame->global_flags & XVID_INTERLACING) {          if (frame->global_flags & XVID_INTERLACING) {
816                  BitstreamPutBit(bs, (frame->global_flags & XVID_TOPFIELDFIRST));                  BitstreamPutBit(bs, 1); // top field first
817                  BitstreamPutBit(bs, (frame->global_flags & XVID_ALTERNATESCAN));                  BitstreamPutBit(bs, 0); // alternate vertical scan
         }  
   
         if (frame->coding_type == S_VOP) {  
                 if (1)  {               // no_of_sprite_warping_points>=1  
                         if (pParam->m_quarterpel)  
                                 bs_put_spritetrajectory(bs, frame->GMC_MV.x/2 ); // du[0]  
                         else  
                                 bs_put_spritetrajectory(bs, frame->GMC_MV.x ); // du[0]  
                         WRITE_MARKER();  
   
                         if (pParam->m_quarterpel)  
                                 bs_put_spritetrajectory(bs, frame->GMC_MV.y/2 ); // dv[0]  
                         else  
                                 bs_put_spritetrajectory(bs, frame->GMC_MV.y ); // dv[0]  
                         WRITE_MARKER();  
   
   
                         if (pParam->m_quarterpel)  
                         {  
                                 DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i) *QPEL*", 0, frame->GMC_MV.x/2, frame->GMC_MV.y/2);  
                         }  
                         else  
                         {  
                                 DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i)", 0, frame->GMC_MV.x, frame->GMC_MV.y);  
                         }  
   
                 }  
 /* GMC is halfpel in bitstream, even though GMC_MV was pseudo-qpel (2*halfpel) */  
   
                 if (2) {                // no_of_sprite_warping_points>=2 (for DivX5 compat)  
                         bs_put_spritetrajectory(bs, 0 );  
                         WRITE_MARKER();  
                         bs_put_spritetrajectory(bs, 0 );  
                         WRITE_MARKER();  
                 }  
                 // no support for brightness_change!  
818          }          }
819    
820          BitstreamPutBits(bs, frame->quant, 5);  // quantizer          BitstreamPutBits(bs, frame->quant, 5);  // quantizer
# Line 1132  Line 826 
826                  BitstreamPutBits(bs, frame->bcode, 3);  // backward_fixed_code                  BitstreamPutBits(bs, frame->bcode, 3);  // backward_fixed_code
827    
828  }  }
   
 void  
 BitstreamWriteUserData(Bitstream * const bs,  
                                                 uint8_t * data,  
                                                 const int length)  
 {  
         int i;  
   
         BitstreamPad(bs);  
         BitstreamPutBits(bs, USERDATA_START_CODE, 32);  
   
         for (i = 0; i < length; i++) {  
                 BitstreamPutBits(bs, data[i], 8);  
         }  
   
 }  

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

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