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

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

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

revision 248, Fri Jun 28 15:14:40 2002 UTC revision 655, Sun Nov 17 00:57:58 2002 UTC
# Line 1  Line 1 
1   /******************************************************************************  /*****************************************************************************
   *                                                                            *  
   *  This file is part of XviD, a free MPEG-4 video encoder/decoder            *  
   *                                                                            *  
   *  XviD is an implementation of a part of one or more MPEG-4 Video tools     *  
   *  as specified in ISO/IEC 14496-2 standard.  Those intending to use this    *  
   *  software module in hardware or software products are advised that its     *  
   *  use may infringe existing patents or copyrights, and any such use         *  
   *  would be at such party's own risk.  The original developer of this        *  
   *  software module and his/her company, and subsequent editors and their     *  
   *  companies, will have no liability for use of this software or             *  
   *  modifications or derivatives thereof.                                     *  
   *                                                                            *  
   *  XviD is free software; you can redistribute it and/or modify it           *  
   *  under the terms of the GNU General Public License as published by         *  
   *  the Free Software Foundation; either version 2 of the License, or         *  
   *  (at your option) any later version.                                       *  
   *                                                                            *  
   *  XviD is distributed in the hope that it will be useful, but               *  
   *  WITHOUT ANY WARRANTY; without even the implied warranty of                *  
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *  
   *  GNU General Public License for more details.                              *  
   *                                                                            *  
   *  You should have received a copy of the GNU General Public License         *  
   *  along with this program; if not, write to the Free Software               *  
   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *  
   *                                                                            *  
   ******************************************************************************/  
   
  /******************************************************************************  
   *                                                                            *  
   *  bitstream.c                                                               *  
   *                                                                            *  
   *  Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au>                    *  
   *                                                                            *  
   *  For more information visit the XviD homepage: http://www.xvid.org         *  
   *                                                                            *  
   ******************************************************************************/  
   
  /******************************************************************************  
   *                                                                            *  
   *  Revision history:                                                         *  
   *                                                                            *  
   *      22.05.2002 bs_put_matrix fix  
   *  20.05.2002 added BitstreamWriteUserData                                   *  
   *  19.06.2002  Fix a little bug in use custom quant matrix                   *  
   *              MinChen <chenm001@163.com>                                    *  
   *  08.05.2002  add low_delay support for B_VOP decode                        *  
   *              MinChen <chenm001@163.com>                                    *  
   *  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                                         *  
2    *    *
3    ******************************************************************************/   *  XVID MPEG-4 VIDEO CODEC
4     *  - Bitstream reader/writer functions -
5     *
6     *  Copyright (C) 2001-2002 - Peter Ross <pross@xvid.org>
7     *
8     *  This file is part of XviD, a free MPEG-4 video encoder/decoder
9     *
10     *  XviD is free software; you can redistribute it and/or modify it
11     *  under the terms of the GNU General Public License as published by
12     *  the Free Software Foundation; either version 2 of the License, or
13     *  (at your option) any later version.
14     *
15     *  This program is distributed in the hope that it will be useful,
16     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     *  GNU General Public License for more details.
19     *
20     *  You should have received a copy of the GNU General Public License
21     *  along with this program; if not, write to the Free Software
22     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23     *
24     *  Under section 8 of the GNU General Public License, the copyright
25     *  holders of XVID explicitly forbid distribution in the following
26     *  countries:
27     *
28     *    - Japan
29     *    - United States of America
30     *
31     *  Linking XviD statically or dynamically with other modules is making a
32     *  combined work based on XviD.  Thus, the terms and conditions of the
33     *  GNU General Public License cover the whole combination.
34     *
35     *  As a special exception, the copyright holders of XviD give you
36     *  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     *  combined work), being distributed under the terms of the GNU General
43     *  Public License plus this exception.  An independent module is a module
44     *  which is not derived from or based on XviD.
45     *
46     *  Note that people who make modified versions of XviD are not obligated
47     *  to grant this special exception for their modified versions; it is
48     *  their choice whether to do so.  The GNU General Public License gives
49     *  permission to release a modified version without this exception; this
50     *  exception also makes it possible to release a modified version which
51     *  carries forward this exception.
52     *
53     * $Id: bitstream.c,v 1.35 2002-11-17 00:57:56 edgomez Exp $
54     *
55     ****************************************************************************/
56    
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"
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 125  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, int addbits)  read_video_packet_header(Bitstream *bs, const int addbits, int * quant)
125  {  {
126          int nbits;          int nbits;
127          int mbnum;          int mbnum;
         int quant;  
128          int hec;          int hec;
129    
130          nbits = NUMBITS_VP_RESYNC_MARKER;          nbits = NUMBITS_VP_RESYNC_MARKER + addbits;
131    
132          BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs));          BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs));
133          BitstreamSkip(bs, nbits);          BitstreamSkip(bs, nbits);
134    
135            DPRINTF(DPRINTF_STARTCODE, "<video_packet_header>");
136    
137          // if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {          // if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
138                  // hec                  // hec
139                  // vop_width                  // vop_width
# Line 147  Line 144 
144          //}          //}
145    
146          mbnum = BitstreamGetBits(bs, 9);          mbnum = BitstreamGetBits(bs, 9);
147          //printf("mbnum %i    [%i,%i]\n", mbnum, mbnum % dec->mb_width, mbnum / dec->mb_width);          DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum);
148    
149          // if (dec->shape != VIDOBJLAY_SHAPE_BINARYONLY)          // if (dec->shape != VIDOBJLAY_SHAPE_BINARYONLY)
150          quant = BitstreamGetBits(bs, 5);          *quant = BitstreamGetBits(bs, 5);
151            DPRINTF(DPRINTF_HEADER, "quant %i", *quant);
152    
153          // if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)          // if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)
154          hec = BitstreamGetBit(bs);          hec = BitstreamGetBit(bs);
155            DPRINTF(DPRINTF_HEADER, "header_extension_code %i", hec);
156          // if (hec)          // if (hec)
157          //   .. decoder hec-header ...          //   .. decoder hec-header ...
158    
# Line 167  Line 166 
166  returns coding_type, or -1 if error  returns coding_type, or -1 if error
167  */  */
168    
169    #define VIDOBJ_START_CODE_MASK          0x0000001f
170    #define VIDOBJLAY_START_CODE_MASK       0x0000000f
171    
172  int  int
173  BitstreamReadHeaders(Bitstream * bs,  BitstreamReadHeaders(Bitstream * bs,
174                                           DECODER * dec,                                           DECODER * dec,
# Line 181  Line 183 
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;
187    
188          do {          do {
189                  BitstreamByteAlign(bs);                  BitstreamByteAlign(bs);
190                  start_code = BitstreamShowBits(bs, 32);                  start_code = BitstreamShowBits(bs, 32);
191    
192                  if (start_code == VISOBJSEQ_START_CODE) {                  if (start_code == VISOBJSEQ_START_CODE) {
193                          // DPRINTF("visual_object_sequence");  
194                            int profile;
195    
196                            DPRINTF(DPRINTF_STARTCODE, "<visual_object_sequence>");
197    
198                          BitstreamSkip(bs, 32);  // visual_object_sequence_start_code                          BitstreamSkip(bs, 32);  // visual_object_sequence_start_code
199                          BitstreamSkip(bs, 8);   // profile_and_level_indication                          profile = BitstreamGetBits(bs, 8);      // profile_and_level_indication
200    
201                            DPRINTF(DPRINTF_HEADER, "profile_and_level_indication %i", profile);
202    
203                  } else if (start_code == VISOBJSEQ_STOP_CODE) {                  } else if (start_code == VISOBJSEQ_STOP_CODE) {
204    
205                          BitstreamSkip(bs, 32);  // visual_object_sequence_stop_code                          BitstreamSkip(bs, 32);  // visual_object_sequence_stop_code
206    
207                            DPRINTF(DPRINTF_STARTCODE, "</visual_object_sequence>");
208    
209                  } else if (start_code == VISOBJ_START_CODE) {                  } else if (start_code == VISOBJ_START_CODE) {
210                          //DPRINTF("visual_object");  
211                            DPRINTF(DPRINTF_STARTCODE, "<visual_object>");
212    
213                          BitstreamSkip(bs, 32);  // visual_object_start_code                          BitstreamSkip(bs, 32);  // visual_object_start_code
214                          if (BitstreamGetBit(bs))        // is_visual_object_identified                          if (BitstreamGetBit(bs))        // is_visual_object_identified
215                          {                          {
216                                  vol_ver_id = BitstreamGetBits(bs, 4);   // visual_object_ver_id                                  vol_ver_id = BitstreamGetBits(bs, 4);   // visual_object_ver_id
217                                    DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id);
218                                  BitstreamSkip(bs, 3);   // visual_object_priority                                  BitstreamSkip(bs, 3);   // visual_object_priority
219                          } else {                          } else {
220                                  vol_ver_id = 1;                                  vol_ver_id = 1;
# Line 206  Line 222 
222    
223                          if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO)      // visual_object_type                          if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO)      // visual_object_type
224                          {                          {
225                                  //DPRINTF("visual_object_type != video");                                  DPRINTF(DPRINTF_ERROR, "visual_object_type != video");
226                                  return -1;                                  return -1;
227                          }                          }
228                          BitstreamSkip(bs, 4);                          BitstreamSkip(bs, 4);
# Line 215  Line 231 
231    
232                          if (BitstreamGetBit(bs))        // video_signal_type                          if (BitstreamGetBit(bs))        // video_signal_type
233                          {                          {
234                                  //DPRINTF("+ video_signal_type");                                  DPRINTF(DPRINTF_HEADER,"+ video_signal_type");
235                                  BitstreamSkip(bs, 3);   // video_format                                  BitstreamSkip(bs, 3);   // video_format
236                                  BitstreamSkip(bs, 1);   // video_range                                  BitstreamSkip(bs, 1);   // video_range
237                                  if (BitstreamGetBit(bs))        // color_description                                  if (BitstreamGetBit(bs))        // color_description
238                                  {                                  {
239                                          //DPRINTF("+ color_description");                                          DPRINTF(DPRINTF_HEADER,"+ color_description");
240                                          BitstreamSkip(bs, 8);   // color_primaries                                          BitstreamSkip(bs, 8);   // color_primaries
241                                          BitstreamSkip(bs, 8);   // transfer_characteristics                                          BitstreamSkip(bs, 8);   // transfer_characteristics
242                                          BitstreamSkip(bs, 8);   // matrix_coefficients                                          BitstreamSkip(bs, 8);   // matrix_coefficients
243                                  }                                  }
244                          }                          }
245                  } else if ((start_code & ~0x1f) == VIDOBJ_START_CODE) {                  } else if ((start_code & ~VIDOBJ_START_CODE_MASK) == VIDOBJ_START_CODE) {
246    
247                            DPRINTF(DPRINTF_STARTCODE, "<video_object>");
248                            DPRINTF(DPRINTF_HEADER, "vo id %i", start_code & VIDOBJ_START_CODE_MASK);
249    
250                          BitstreamSkip(bs, 32);  // video_object_start_code                          BitstreamSkip(bs, 32);  // video_object_start_code
251                  } else if ((start_code & ~0xf) == VIDOBJLAY_START_CODE) {  
252                          //DPRINTF("video_object_layer");                  } else if ((start_code & ~VIDOBJLAY_START_CODE_MASK) == VIDOBJLAY_START_CODE) {
253    
254                            DPRINTF(DPRINTF_STARTCODE, "<video_object_layer>");
255                            DPRINTF(DPRINTF_HEADER, "vol id %i", start_code & VIDOBJLAY_START_CODE_MASK);
256    
257                          BitstreamSkip(bs, 32);  // video_object_layer_start_code                          BitstreamSkip(bs, 32);  // video_object_layer_start_code
258    
259                          BitstreamSkip(bs, 1);   // random_accessible_vol                          BitstreamSkip(bs, 1);   // random_accessible_vol
# Line 237  Line 261 
261                          // video_object_type_indication                          // video_object_type_indication
262                          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                          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
263                          {                          {
264                                  //DPRINTF("video_object_type_indication %i not supported ",                                  DPRINTF(DPRINTF_ERROR,"video_object_type_indication %i not supported ",
265                                  //  BitstreamShowBits(bs, 8));                                          BitstreamShowBits(bs, 8));
266                                  return -1;                                  return -1;
267                          }                          }
268                          BitstreamSkip(bs, 8);                          BitstreamSkip(bs, 8);
# Line 246  Line 270 
270    
271                          if (BitstreamGetBit(bs))        // is_object_layer_identifier                          if (BitstreamGetBit(bs))        // is_object_layer_identifier
272                          {                          {
273                                  //DPRINTF("+ is_object_layer_identifier");                                  DPRINTF(DPRINTF_HEADER, "+ is_object_layer_identifier");
274                                  vol_ver_id = BitstreamGetBits(bs, 4);   // video_object_layer_verid                                  vol_ver_id = BitstreamGetBits(bs, 4);   // video_object_layer_verid
275                                    DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id);
276                                  BitstreamSkip(bs, 3);   // video_object_layer_priority                                  BitstreamSkip(bs, 3);   // video_object_layer_priority
277                          } else {                          } else {
278                                  vol_ver_id = 1;                                  vol_ver_id = 1;
279                          }                          }
                         //DPRINTF("vol_ver_id %i", vol_ver_id);  
280    
281                          if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR)     // aspect_ratio_info                          if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR)     // aspect_ratio_info
282                          {                          {
283                                  //DPRINTF("+ aspect_ratio_info");                                  DPRINTF(DPRINTF_HEADER, "+ aspect_ratio_info");
284                                  BitstreamSkip(bs, 8);   // par_width                                  BitstreamSkip(bs, 8);   // par_width
285                                  BitstreamSkip(bs, 8);   // par_height                                  BitstreamSkip(bs, 8);   // par_height
286                          }                          }
287    
288                          if (BitstreamGetBit(bs))        // vol_control_parameters                          if (BitstreamGetBit(bs))        // vol_control_parameters
289                          {                          {
290                                  //DPRINTF("+ vol_control_parameters");                                  DPRINTF(DPRINTF_HEADER, "+ vol_control_parameters");
291                                  BitstreamSkip(bs, 2);   // chroma_format                                  BitstreamSkip(bs, 2);   // chroma_format
292                                  dec->low_delay = BitstreamGetBit(bs);   // low_delay                                  dec->low_delay = BitstreamGetBit(bs);   // low_delay
293                                    DPRINTF(DPRINTF_HEADER, "low_delay %i", dec->low_delay);
294                                  if (BitstreamGetBit(bs))        // vbv_parameters                                  if (BitstreamGetBit(bs))        // vbv_parameters
295                                  {                                  {
296                                          //DPRINTF("+ vbv_parameters");                                          DPRINTF(DPRINTF_HEADER,"+ vbv_parameters");
297                                          BitstreamSkip(bs, 15);  // first_half_bitrate                                          BitstreamSkip(bs, 15);  // first_half_bitrate
298                                          READ_MARKER();                                          READ_MARKER();
299                                          BitstreamSkip(bs, 15);  // latter_half_bitrate                                          BitstreamSkip(bs, 15);  // latter_half_bitrate
# Line 285  Line 310 
310                          }                          }
311    
312                          dec->shape = BitstreamGetBits(bs, 2);   // video_object_layer_shape                          dec->shape = BitstreamGetBits(bs, 2);   // video_object_layer_shape
313                          //DPRINTF("shape %i", dec->shape);                          DPRINTF(DPRINTF_HEADER, "shape %i", dec->shape);
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 295  Line 320 
320    
321  // *************************** for decode B-frame time ***********************  // *************************** for decode B-frame time ***********************
322                          time_increment_resolution = BitstreamGetBits(bs, 16);   // vop_time_increment_resolution                          time_increment_resolution = BitstreamGetBits(bs, 16);   // vop_time_increment_resolution
323                          time_increment_resolution--;  
324                          //DPRINTF("time_increment_resolution = %i",time_increment_resolution);                          DPRINTF(DPRINTF_HEADER,"vop_time_increment_resolution %i", time_increment_resolution);
325    
326    //                      time_increment_resolution--;
327    
328                          if (time_increment_resolution > 0) {                          if (time_increment_resolution > 0) {
329                                  dec->time_inc_bits = log2bin(time_increment_resolution);                                  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
333                                  dec->time_inc_bits = 1;                                  dec->time_inc_bits = 1;
334                          }                          }
# Line 310  Line 337 
337    
338                          if (BitstreamGetBit(bs))        // fixed_vop_rate                          if (BitstreamGetBit(bs))        // fixed_vop_rate
339                          {                          {
340                                    DPRINTF(DPRINTF_HEADER, "+ fixed_vop_rate");
341                                  BitstreamSkip(bs, dec->time_inc_bits);  // fixed_vop_time_increment                                  BitstreamSkip(bs, dec->time_inc_bits);  // fixed_vop_time_increment
342                          }                          }
343    
# Line 320  Line 348 
348    
349                                          READ_MARKER();                                          READ_MARKER();
350                                          width = BitstreamGetBits(bs, 13);       // video_object_layer_width                                          width = BitstreamGetBits(bs, 13);       // video_object_layer_width
                                         //DPRINTF("width %i", width);  
351                                          READ_MARKER();                                          READ_MARKER();
352                                          height = BitstreamGetBits(bs, 13);      // video_object_layer_height                                          height = BitstreamGetBits(bs, 13);      // video_object_layer_height
                                         //DPRINTF("height %i", height);  
353                                          READ_MARKER();                                          READ_MARKER();
354    
355                                            DPRINTF(DPRINTF_HEADER, "width %i", width);
356                                            DPRINTF(DPRINTF_HEADER, "height %i", height);
357    
358                                            // for auto set width & height
359                                            if (dec->width == 0)
360                                                    dec->width = width;
361                                            if (dec->height == 0)
362                                                    dec->height = height;
363    
364                                          if (width != dec->width || height != dec->height) {                                          if (width != dec->width || height != dec->height) {
365                                                  //DPRINTF("FATAL: video dimension discrepancy ***");                                                  DPRINTF(DPRINTF_ERROR, "XVID_DEC_PARAM width/height does not match bitstream");
                                                 //DPRINTF("bitstream width/height  %i x %i", width, height);  
                                                 //DPRINTF("param width/height  %i x %i", dec->width, dec->height);  
366                                                  return -1;                                                  return -1;
367                                          }                                          }
368    
369                                  }                                  }
370    
371                                  if ((dec->interlacing = BitstreamGetBit(bs))) {                                  dec->interlacing = BitstreamGetBit(bs);
372                                          //DPRINTF("vol: interlacing");                                  DPRINTF(DPRINTF_HEADER, "interlace", dec->interlacing);
                                 }  
373    
374                                  if (!BitstreamGetBit(bs))       // obmc_disable                                  if (!BitstreamGetBit(bs))       // obmc_disable
375                                  {                                  {
376                                          //DPRINTF("IGNORED/TODO: !obmc_disable");                                          DPRINTF(DPRINTF_ERROR, "obmc_disabled==false not supported");
377                                          // TODO                                          // TODO
378                                          // fucking divx4.02 has this enabled                                          // fucking divx4.02 has this enabled
379                                  }                                  }
380    
381                                  if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2)))    // sprite_enable                                  if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2)))    // sprite_enable
382                                  {                                  {
383                                          //DPRINTF("sprite_enable; not supported");                                          DPRINTF(DPRINTF_ERROR, "spriate_enabled not supported");
384                                          return -1;                                          return -1;
385                                  }                                  }
386    
# Line 359  Line 391 
391    
392                                  if (BitstreamGetBit(bs))        // not_8_bit                                  if (BitstreamGetBit(bs))        // not_8_bit
393                                  {                                  {
394                                          //DPRINTF("+ not_8_bit [IGNORED/TODO]");                                          DPRINTF(DPRINTF_HEADER, "not_8_bit==true (ignored)");
395                                          dec->quant_bits = BitstreamGetBits(bs, 4);      // quant_precision                                          dec->quant_bits = BitstreamGetBits(bs, 4);      // quant_precision
396                                          BitstreamSkip(bs, 4);   // bits_per_pixel                                          BitstreamSkip(bs, 4);   // bits_per_pixel
397                                  } else {                                  } else {
# Line 373  Line 405 
405                                  }                                  }
406    
407                                  dec->quant_type = BitstreamGetBit(bs);  // quant_type                                  dec->quant_type = BitstreamGetBit(bs);  // quant_type
408                                  //DPRINTF("**** quant_type %i", dec->quant_type);                                  DPRINTF(DPRINTF_HEADER, "quant_type %i", dec->quant_type);
409    
410                                  if (dec->quant_type) {                                  if (dec->quant_type) {
411                                          if (BitstreamGetBit(bs))        // load_intra_quant_mat                                          if (BitstreamGetBit(bs))        // load_intra_quant_mat
412                                          {                                          {
413                                                  uint8_t matrix[64];                                                  uint8_t matrix[64];
414    
415                                                    DPRINTF(DPRINTF_HEADER, "load_intra_quant_mat");
416    
417                                                  bs_get_matrix(bs, matrix);                                                  bs_get_matrix(bs, matrix);
418                                                  set_intra_matrix(matrix);                                                  set_intra_matrix(matrix);
419                                          } else                                          } else
# Line 389  Line 423 
423                                          {                                          {
424                                                  uint8_t matrix[64];                                                  uint8_t matrix[64];
425    
426                                                    DPRINTF(DPRINTF_HEADER, "load_inter_quant_mat");
427    
428                                                  bs_get_matrix(bs, matrix);                                                  bs_get_matrix(bs, matrix);
429                                                  set_inter_matrix(matrix);                                                  set_inter_matrix(matrix);
430                                          } else                                          } else
431                                                  set_inter_matrix(get_default_inter_matrix());                                                  set_inter_matrix(get_default_inter_matrix());
432    
433                                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {                                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {
434                                                  // TODO                                                  DPRINTF(DPRINTF_ERROR, "greyscale matrix not supported");
                                                 //DPRINTF("TODO: grayscale matrix stuff");  
435                                                  return -1;                                                  return -1;
436                                          }                                          }
437    
# Line 404  Line 439 
439    
440    
441                                  if (vol_ver_id != 1) {                                  if (vol_ver_id != 1) {
442                                          dec->quarterpel = BitstreamGetBit(bs);  // quarter_sampe                                          DPRINTF(DPRINTF_DEBUG, "QUARTERPEL BITSTREAM");
443                                          if (dec->quarterpel) {                                          dec->quarterpel = BitstreamGetBit(bs);  // quarter_sample
                                                 //DPRINTF("IGNORED/TODO: quarter_sample");  
444                                          }                                          }
445                                  } else {                                  else
446                                          dec->quarterpel = 0;                                          dec->quarterpel = 0;
447                                  }  
448    
449                                  if (!BitstreamGetBit(bs))       // complexity_estimation_disable                                  if (!BitstreamGetBit(bs))       // complexity_estimation_disable
450                                  {                                  {
451                                          //DPRINTF("TODO: complexity_estimation header");                                          DPRINTF(DPRINTF_ERROR, "complexity_estimation not supported");
                                         // TODO  
452                                          return -1;                                          return -1;
453                                  }                                  }
454    
455                                  if (!BitstreamGetBit(bs))       // resync_marker_disable                                  BitstreamSkip(bs, 1);   // resync_marker_disable
                                 {  
                                         //DPRINTF("IGNORED/TODO: !resync_marker_disable");  
                                         // TODO  
                                 }  
456    
457                                  if (BitstreamGetBit(bs))        // data_partitioned                                  if (BitstreamGetBit(bs))        // data_partitioned
458                                  {                                  {
459                                          //DPRINTF("+ data_partitioned");                                          DPRINTF(DPRINTF_ERROR, "data_partitioned not supported");
460                                          BitstreamSkip(bs, 1);   // reversible_vlc                                          BitstreamSkip(bs, 1);   // reversible_vlc
461                                  }                                  }
462    
463                                  if (vol_ver_id != 1) {                                  if (vol_ver_id != 1) {
464                                          if (BitstreamGetBit(bs))        // newpred_enable                                          if (BitstreamGetBit(bs))        // newpred_enable
465                                          {                                          {
466                                                  //DPRINTF("+ 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                                          if (BitstreamGetBit(bs))        // reduced_resolution_vop_enable                                          if (BitstreamGetBit(bs))        // reduced_resolution_vop_enable
471                                          {                                          {
472                                                  //DPRINTF("TODO: reduced_resolution_vop_enable");                                                  DPRINTF(DPRINTF_ERROR, "reduced_resolution_vop not supported");
                                                 // TODO  
473                                                  return -1;                                                  return -1;
474                                          }                                          }
475                                  }                                  }
476    
477                                  if ((dec->scalability = BitstreamGetBit(bs)))   // scalability                                  if ((dec->scalability = BitstreamGetBit(bs)))   // scalability
478                                  {                                  {
479                                          // TODO                                          DPRINTF(DPRINTF_ERROR, "scalability not supported");
                                         //DPRINTF("TODO: scalability");  
480                                          return -1;                                          return -1;
481                                  }                                  }
482                          } else                          // dec->shape == BINARY_ONLY                          } else                          // dec->shape == BINARY_ONLY
# Line 457  Line 484 
484                                  if (vol_ver_id != 1) {                                  if (vol_ver_id != 1) {
485                                          if (BitstreamGetBit(bs))        // scalability                                          if (BitstreamGetBit(bs))        // scalability
486                                          {                                          {
487                                                  // TODO                                          DPRINTF(DPRINTF_ERROR, "scalability not supported");
                                                 //DPRINTF("TODO: scalability");  
488                                                  return -1;                                                  return -1;
489                                          }                                          }
490                                  }                                  }
# Line 467  Line 493 
493                          }                          }
494    
495                  } else if (start_code == GRPOFVOP_START_CODE) {                  } else if (start_code == GRPOFVOP_START_CODE) {
496                          //DPRINTF("group_of_vop");  
497                            DPRINTF(DPRINTF_STARTCODE, "<group_of_vop>");
498    
499                          BitstreamSkip(bs, 32);                          BitstreamSkip(bs, 32);
500                          {                          {
501                                  int hours, minutes, seconds;                                  int hours, minutes, seconds;
# Line 476  Line 504 
504                                  minutes = BitstreamGetBits(bs, 6);                                  minutes = BitstreamGetBits(bs, 6);
505                                  READ_MARKER();                                  READ_MARKER();
506                                  seconds = BitstreamGetBits(bs, 6);                                  seconds = BitstreamGetBits(bs, 6);
507                                  //DPRINTF("%ih %im %is", hours, minutes, seconds);  
508                                    DPRINTF(DPRINTF_HEADER, "time %ih%im%is", hours);
509                          }                          }
510                          BitstreamSkip(bs, 1);   // closed_gov                          BitstreamSkip(bs, 1);   // closed_gov
511                          BitstreamSkip(bs, 1);   // broken_link                          BitstreamSkip(bs, 1);   // broken_link
512    
513                  } else if (start_code == VOP_START_CODE) {                  } else if (start_code == VOP_START_CODE) {
514                          //DPRINTF("vop_start_code");  
515                            DPRINTF(DPRINTF_STARTCODE, "<vop>");
516    
517                          BitstreamSkip(bs, 32);  // vop_start_code                          BitstreamSkip(bs, 32);  // vop_start_code
518    
519                          coding_type = BitstreamGetBits(bs, 2);  // vop_coding_type                          coding_type = BitstreamGetBits(bs, 2);  // vop_coding_type
520                          //DPRINTF("coding_type %i", coding_type);                          DPRINTF(DPRINTF_HEADER, "coding_type %i", coding_type);
521    
522  // *************************** for decode B-frame time ***********************  // *************************** for decode B-frame time ***********************
523                          while (BitstreamGetBit(bs) != 0)        // time_base                          while (BitstreamGetBit(bs) != 0)        // time_base
# Line 497  Line 529 
529                                  time_increment = (BitstreamGetBits(bs, dec->time_inc_bits));    // vop_time_increment                                  time_increment = (BitstreamGetBits(bs, dec->time_inc_bits));    // vop_time_increment
530                          }                          }
531    
532                          /*                          DPRINTF(DPRINTF_HEADER, "time_base %i", time_incr);
533                          DPRINTF("%c %i:%i",                          DPRINTF(DPRINTF_HEADER, "time_increment %i", time_increment);
534    
535                            DPRINTF(DPRINTF_TIMECODE, "%c %i:%i",
536                                  coding_type == I_VOP ? 'I' : coding_type == P_VOP ? 'P' : 'B',                                  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) {
540                                  dec->last_time_base = dec->time_base;                                  dec->last_time_base = dec->time_base;
541                                  dec->time_base += time_incr;                                  dec->time_base += time_incr;
542                                  dec->time =                                  dec->time = time_increment;
543                                          dec->time_base * time_increment_resolution +  
544    /*                                      dec->time_base * time_increment_resolution +
545                                          time_increment;                                          time_increment;
546                                  dec->time_pp = (uint32_t) (dec->time - dec->last_non_b_time);  */                              dec->time_pp = (uint32_t)
547                                            (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 =                                  dec->time = time_increment;
551    /*
552                                          (dec->last_time_base +                                          (dec->last_time_base +
553                                           time_incr) * time_increment_resolution + time_increment;                                           time_incr) * time_increment_resolution + time_increment;
554                                  dec->time_bp = (uint32_t) (dec->last_non_b_time - dec->time);  */
555                                    dec->time_bp = (uint32_t)
556                                            (time_increment_resolution + dec->last_non_b_time - dec->time)%time_increment_resolution;
557                          }                          }
                         //DPRINTF("time_increment %i",time_increment);  
558    
559                          READ_MARKER();                          READ_MARKER();
560    
561                          if (!BitstreamGetBit(bs))       // vop_coded                          if (!BitstreamGetBit(bs))       // vop_coded
562                          {                          {
563                                  //DPRINTF("**NOT CODED**");                                  DPRINTF(DPRINTF_HEADER, "vop_coded==false");
564                                  return N_VOP;                                  return N_VOP;
565                          }                          }
566    
# Line 536  Line 573 
573                          if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) &&                          if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) &&
574                                  (coding_type == P_VOP)) {                                  (coding_type == P_VOP)) {
575                                  *rounding = BitstreamGetBit(bs);        // rounding_type                                  *rounding = BitstreamGetBit(bs);        // rounding_type
576                                  //DPRINTF("rounding %i", *rounding);                                  DPRINTF(DPRINTF_HEADER, "rounding %i", *rounding);
577                          }                          }
578    
579                          /* if (reduced_resolution_enable)                          /* if (reduced_resolution_enable)
# Line 557  Line 594 
594                                  vert_mc_ref = BitstreamGetBits(bs, 13);                                  vert_mc_ref = BitstreamGetBits(bs, 13);
595                                  READ_MARKER();                                  READ_MARKER();
596    
597                                  //DPRINTF("vop_width/height %i x %i", width, height);                                  DPRINTF(DPRINTF_HEADER, "width %i", width);
598                                  //DPRINTF("ref              %i x %i", horiz_mc_ref, vert_mc_ref);                                  DPRINTF(DPRINTF_HEADER, "height %i", height);
599                                    DPRINTF(DPRINTF_HEADER, "horiz_mc_ref %i", horiz_mc_ref);
600                                    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 574  Line 613 
613                                          intra_dc_threshold_table[BitstreamGetBits(bs, 3)];                                          intra_dc_threshold_table[BitstreamGetBits(bs, 3)];
614    
615                                  if (dec->interlacing) {                                  if (dec->interlacing) {
616                                          if ((dec->top_field_first = BitstreamGetBit(bs))) {                                          dec->top_field_first = BitstreamGetBit(bs);
617                                                  //DPRINTF("vop: top_field_first");                                          DPRINTF(DPRINTF_HEADER, "interlace top_field_first %i", dec->top_field_first);
618                                          }                                          dec->alternate_vertical_scan = BitstreamGetBit(bs);
619                                          if ((dec->alternate_vertical_scan = BitstreamGetBit(bs))) {                                          DPRINTF(DPRINTF_HEADER, "interlace alternate_vertical_scan %i", dec->alternate_vertical_scan);
620                                                  //DPRINTF("vop: alternate_vertical_scan");  
                                         }  
621                                  }                                  }
622                          }                          }
623    
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("quant %i", *quant);                          DPRINTF(DPRINTF_HEADER, "quant %i", *quant);
628    
629                          if (coding_type != I_VOP) {                          if (coding_type != I_VOP) {
630                                  *fcode_forward = BitstreamGetBits(bs, 3);       // fcode_forward                                  *fcode_forward = BitstreamGetBits(bs, 3);       // fcode_forward
631                                    DPRINTF(DPRINTF_HEADER, "fcode_forward %i", *fcode_forward);
632                          }                          }
633    
634                          if (coding_type == B_VOP) {                          if (coding_type == B_VOP) {
635                                  *fcode_backward = BitstreamGetBits(bs, 3);      // fcode_backward                                  *fcode_backward = BitstreamGetBits(bs, 3);      // fcode_backward
636                                    DPRINTF(DPRINTF_HEADER, "fcode_backward %i", *fcode_backward);
637                          }                          }
638                          if (!dec->scalability) {                          if (!dec->scalability) {
639                                  if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) &&                                  if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) &&
# Line 602  Line 642 
642                                  }                                  }
643                          }                          }
644                          return coding_type;                          return coding_type;
645    
646                  } else if (start_code == USERDATA_START_CODE) {                  } else if (start_code == USERDATA_START_CODE) {
647                          //DPRINTF("user_data");  
648                            DPRINTF(DPRINTF_STARTCODE, "<user_data>");
649    
650                          BitstreamSkip(bs, 32);  // user_data_start_code                          BitstreamSkip(bs, 32);  // user_data_start_code
651    
652                  } else                                  // start_code == ?                  } else                                  // start_code == ?
653                  {                  {
654                          if (BitstreamShowBits(bs, 24) == 0x000001) {                          if (BitstreamShowBits(bs, 24) == 0x000001) {
655                                  //DPRINTF("*** WARNING: unknown start_code %x",                                  DPRINTF(DPRINTF_STARTCODE, "<unknown: %x>", BitstreamShowBits(bs, 32));
                                 //         BitstreamShowBits(bs, 32));  
656                          }                          }
657                          BitstreamSkip(bs, 8);                          BitstreamSkip(bs, 8);
658                  }                  }
# Line 664  Line 707 
707          BitstreamPutBit(bs, 0);         // is_object_layer_identified (0=not given)          BitstreamPutBit(bs, 0);         // is_object_layer_identified (0=not given)
708          BitstreamPutBits(bs, 1, 4);     // aspect_ratio_info (1=1:1)          BitstreamPutBits(bs, 1, 4);     // aspect_ratio_info (1=1:1)
709    
 #ifdef BFRAMES  
         if (pParam->max_bframes > 0) {  
                 //DPRINTF("low_delay=1");  
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"
                 BitstreamPutBit(bs, 0); // low_delay  
                 BitstreamPutBit(bs, 0); // vbv_parameters (0=not given)  
         } else  
 #endif  
         {  
                 BitstreamPutBits(bs, 0, 1);     // vol_control_parameters (0=not given)  
         }  
712    
713            BitstreamPutBit(bs, 1); // low_delay
714    
715            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_increment_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           */           */
 #ifdef BFRAMES  
727          BitstreamPutBits(bs, pParam->fbase, 16);          BitstreamPutBits(bs, pParam->fbase, 16);
728  #else  
         BitstreamPutBits(bs, 2, 16);  
 #endif  
729    
730          WRITE_MARKER();          WRITE_MARKER();
731    
 #ifdef BFRAMES  
732          BitstreamPutBit(bs, 1);         // fixed_vop_rate = 1          BitstreamPutBit(bs, 1);         // fixed_vop_rate = 1
733          BitstreamPutBits(bs, pParam->fincr, log2bin(pParam->fbase));    // fixed_vop_time_increment          BitstreamPutBits(bs, pParam->fincr, log2bin(pParam->fbase));    // fixed_vop_time_increment
 #else  
         BitstreamPutBit(bs, 0);         // fixed_vop_rate = 0  
 #endif  
734    
735          WRITE_MARKER();          WRITE_MARKER();
736          BitstreamPutBits(bs, pParam->width, 13);        // width          BitstreamPutBits(bs, pParam->width, 13);        // width
# Line 750  Line 780 
780                                                  const FRAMEINFO * frame,                                                  const FRAMEINFO * frame,
781                                                  int vop_coded)                                                  int vop_coded)
782  {  {
 #ifdef BFRAMES  
783          uint32_t i;          uint32_t i;
784  #endif  
785          BitstreamPad(bs);          BitstreamPad(bs);
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);
789    
790          // time_base = 0  write n x PutBit(1), PutBit(0)          // time_base = 0  write n x PutBit(1), PutBit(0)
 #ifdef BFRAMES  
791          for (i = 0; i < frame->seconds; i++) {          for (i = 0; i < frame->seconds; i++) {
792                  BitstreamPutBit(bs, 1);                  BitstreamPutBit(bs, 1);
793          }          }
794          BitstreamPutBit(bs, 0);          BitstreamPutBit(bs, 0);
 #else  
         BitstreamPutBits(bs, 0, 1);  
 #endif  
795    
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)
 #ifdef BFRAMES  
799          BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase));          BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase));
         DPRINTF("[%i:%i] %c\n", frame->seconds, frame->ticks,  
                         frame->coding_type == I_VOP ? 'I' : frame->coding_type ==  
                         P_VOP ? 'P' : 'B');  
 #else  
         BitstreamPutBits(bs, 1, 1);  
 #endif  
800    
801          WRITE_MARKER();          WRITE_MARKER();
802    
# Line 808  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.248  
changed lines
  Added in v.655

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