[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.39.2.17, Wed Sep 10 19:28:40 2003 UTC revision 1.52, Mon May 23 09:29:43 2005 UTC
# Line 33  Line 33 
33  #include "mbcoding.h"  #include "mbcoding.h"
34    
35    
36  static uint32_t __inline  static const uint8_t log2_tab_16[16] =  { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };
37  log2bin(uint32_t value)  
38    static uint32_t __inline log2bin(uint32_t value)
39  {  {
40          int n = 0;          int n = 0;
41      if (value & 0xffff0000) {
42          while (value) {      value >>= 16;
43                  value >>= 1;      n += 16;
44                  n++;    }
45      if (value & 0xff00) {
46        value >>= 8;
47        n += 8;
48      }
49      if (value & 0xf0) {
50        value >>= 4;
51        n += 4;
52          }          }
53          return n;   return n + log2_tab_16[value];
54  }  }
55    
   
56  static const uint32_t intra_dc_threshold_table[] = {  static const uint32_t intra_dc_threshold_table[] = {
57          32,                                                     /* never use */          32,                                                     /* never use */
58          13,          13,
# Line 71  Line 78 
78                  matrix[scan_tables[0][i++]] = value;                  matrix[scan_tables[0][i++]] = value;
79          }          }
80          while (value != 0 && i < 64);          while (value != 0 && i < 64);
         i--;    /* fix little bug at coeff not full */  
81    
82            if (value != 0) return;
83    
84            i--;
85          while (i < 64) {          while (i < 64) {
86                  matrix[scan_tables[0][i++]] = last;                  matrix[scan_tables[0][i++]] = last;
87          }          }
# Line 185  Line 194 
194                                  DPRINTF(XVID_DEBUG_HEADER,"fcode_backward %i\n", *fcode_backward);                                  DPRINTF(XVID_DEBUG_HEADER,"fcode_backward %i\n", *fcode_backward);
195                          }                          }
196                  }                  }
   
197          }          }
198    
199          if (dec->newpred_enable)          if (dec->newpred_enable)
# Line 379  Line 387 
387  BitstreamReadHeaders(Bitstream * bs,  BitstreamReadHeaders(Bitstream * bs,
388                                           DECODER * dec,                                           DECODER * dec,
389                                           uint32_t * rounding,                                           uint32_t * rounding,
                                          uint32_t * reduced_resolution,  
390                                           uint32_t * quant,                                           uint32_t * quant,
391                                           uint32_t * fcode_forward,                                           uint32_t * fcode_forward,
392                                           uint32_t * fcode_backward,                                           uint32_t * fcode_backward,
# Line 393  Line 400 
400          int32_t time_increment = 0;          int32_t time_increment = 0;
401          int resize = 0;          int resize = 0;
402    
403          do {          while ((BitstreamPos(bs) >> 3) + 4 <= bs->length) {
404    
405                  BitstreamByteAlign(bs);                  BitstreamByteAlign(bs);
406                  start_code = BitstreamShowBits(bs, 32);                  start_code = BitstreamShowBits(bs, 32);
# Line 542  Line 549 
549                          dec->time_inc_resolution = BitstreamGetBits(bs, 16);    /* vop_time_increment_resolution */                          dec->time_inc_resolution = BitstreamGetBits(bs, 16);    /* vop_time_increment_resolution */
550                          DPRINTF(XVID_DEBUG_HEADER,"vop_time_increment_resolution %i\n", dec->time_inc_resolution);                          DPRINTF(XVID_DEBUG_HEADER,"vop_time_increment_resolution %i\n", dec->time_inc_resolution);
551    
 #if 0  
                         dec->time_inc_resolution--;  
 #endif  
   
552                          if (dec->time_inc_resolution > 0) {                          if (dec->time_inc_resolution > 0) {
553                                  dec->time_inc_bits = log2bin(dec->time_inc_resolution-1);                                  dec->time_inc_bits = MAX(log2bin(dec->time_inc_resolution-1), 1);
554                          } else {                          } else {
 #if 0  
                                 dec->time_inc_bits = 0;  
 #endif  
555                                  /* for "old" xvid compatibility, set time_inc_bits = 1 */                                  /* for "old" xvid compatibility, set time_inc_bits = 1 */
556                                  dec->time_inc_bits = 1;                                  dec->time_inc_bits = 1;
557                          }                          }
# Line 662  Line 662 
662                                                  DPRINTF(XVID_DEBUG_HEADER, "load_intra_quant_mat\n");                                                  DPRINTF(XVID_DEBUG_HEADER, "load_intra_quant_mat\n");
663    
664                                                  bs_get_matrix(bs, matrix);                                                  bs_get_matrix(bs, matrix);
665                                                  set_intra_matrix(matrix);                                                  set_intra_matrix(dec->mpeg_quant_matrices, matrix);
666                                          } else                                          } else
667                                                  set_intra_matrix(get_default_intra_matrix());                                                  set_intra_matrix(dec->mpeg_quant_matrices, get_default_intra_matrix());
668    
669                                          if (BitstreamGetBit(bs))        /* load_inter_quant_mat */                                          if (BitstreamGetBit(bs))        /* load_inter_quant_mat */
670                                          {                                          {
# Line 673  Line 673 
673                                                  DPRINTF(XVID_DEBUG_HEADER, "load_inter_quant_mat\n");                                                  DPRINTF(XVID_DEBUG_HEADER, "load_inter_quant_mat\n");
674    
675                                                  bs_get_matrix(bs, matrix);                                                  bs_get_matrix(bs, matrix);
676                                                  set_inter_matrix(matrix);                                                  set_inter_matrix(dec->mpeg_quant_matrices, matrix);
677                                          } else                                          } else
678                                                  set_inter_matrix(get_default_inter_matrix());                                                  set_inter_matrix(dec->mpeg_quant_matrices, get_default_inter_matrix());
679    
680                                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {                                          if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) {
681                                                  DPRINTF(XVID_DEBUG_ERROR, "greyscale matrix not supported\n");                                                  DPRINTF(XVID_DEBUG_ERROR, "greyscale matrix not supported\n");
# Line 814  Line 814 
814                          if (coding_type != B_VOP) {                          if (coding_type != B_VOP) {
815                                  dec->last_time_base = dec->time_base;                                  dec->last_time_base = dec->time_base;
816                                  dec->time_base += time_incr;                                  dec->time_base += time_incr;
817                                  dec->time = time_increment;                                  dec->time = dec->time_base*dec->time_inc_resolution + time_increment;
818                                    dec->time_pp = (int32_t)(dec->time - dec->last_non_b_time);
 #if 0  
                                         dec->time_base * dec->time_inc_resolution +  
                                         time_increment;  
 #endif  
                                         dec->time_pp = (uint32_t)  
                                                 (dec->time_inc_resolution + dec->time - dec->last_non_b_time)%dec->time_inc_resolution;  
819                                  dec->last_non_b_time = dec->time;                                  dec->last_non_b_time = dec->time;
820                          } else {                          } else {
821                                  dec->time = time_increment;                                  dec->time = (dec->last_time_base + time_incr)*dec->time_inc_resolution + time_increment;
822  #if 0                                  dec->time_bp = dec->time_pp - (int32_t)(dec->last_non_b_time - dec->time);
                                         (dec->last_time_base +  
                                          time_incr) * dec->time_inc_resolution + time_increment;  
 #endif  
                                 dec->time_bp = (uint32_t)  
                                         (dec->time_inc_resolution + dec->last_non_b_time - dec->time)%dec->time_inc_resolution;  
823                          }                          }
824                          DPRINTF(XVID_DEBUG_HEADER,"time_pp=%i\n", dec->time_pp);                          DPRINTF(XVID_DEBUG_HEADER,"time_pp=%i\n", dec->time_pp);
825                          DPRINTF(XVID_DEBUG_HEADER,"time_bp=%i\n", dec->time_bp);                          DPRINTF(XVID_DEBUG_HEADER,"time_bp=%i\n", dec->time_bp);
# Line 871  Line 860 
860                                  dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR &&                                  dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR &&
861                                  (coding_type == P_VOP || coding_type == I_VOP)) {                                  (coding_type == P_VOP || coding_type == I_VOP)) {
862    
863                                  *reduced_resolution = BitstreamGetBit(bs);                                  if (BitstreamGetBit(bs));
864                                  DPRINTF(XVID_DEBUG_HEADER, "reduced_resolution %i\n", *reduced_resolution);                                          DPRINTF(XVID_DEBUG_ERROR, "RRV not supported (anymore)\n");
                         }  
                         else  
                         {  
                                 *reduced_resolution = 0;  
865                          }                          }
866    
867                          if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {                          if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) {
# Line 1001  Line 986 
986    
987                          BitstreamSkip(bs, 32);  /* user_data_start_code */                          BitstreamSkip(bs, 32);  /* user_data_start_code */
988    
989                            memset(tmp, 0, 256);
990                          tmp[0] = BitstreamShowBits(bs, 8);                          tmp[0] = BitstreamShowBits(bs, 8);
991    
992                          for(i = 1; i < 256; i++){                          for(i = 1; i < 256; i++){
# Line 1016  Line 1002 
1002    
1003                          /* read xvid bitstream version */                          /* read xvid bitstream version */
1004                          if(strncmp(tmp, "XviD", 4) == 0) {                          if(strncmp(tmp, "XviD", 4) == 0) {
1005                                    if (tmp[strlen(tmp)-1] == 'C') {
1006                                            sscanf(tmp, "XviD%dC", &dec->bs_version);
1007                                            dec->cartoon_mode = 1;
1008                                    }
1009                                    else
1010                                  sscanf(tmp, "XviD%d", &dec->bs_version);                                  sscanf(tmp, "XviD%d", &dec->bs_version);
1011                                  DPRINTF(XVID_DEBUG_HEADER, "xvid bitstream version=%i", dec->bs_version);  
1012                                    DPRINTF(XVID_DEBUG_HEADER, "xvid bitstream version=%i\n", dec->bs_version);
1013                          }                          }
1014    
1015                      /* divx detection */                      /* divx detection */
# Line 1040  Line 1032 
1032                          BitstreamSkip(bs, 8);                          BitstreamSkip(bs, 8);
1033                  }                  }
1034          }          }
         while ((BitstreamPos(bs) >> 3) < bs->length);  
1035    
1036  #if 0  #if 0
1037          DPRINTF("*** WARNING: no vop_start_code found");          DPRINTF("*** WARNING: no vop_start_code found");
# Line 1053  Line 1044 
1044    
1045  static void  static void
1046  bs_put_matrix(Bitstream * bs,  bs_put_matrix(Bitstream * bs,
1047                            const int16_t * matrix)                            const uint16_t * matrix)
1048  {  {
1049          int i, j;          int i, j;
1050          const int last = matrix[scan_tables[0][63]];          const int last = matrix[scan_tables[0][63]];
# Line 1075  Line 1066 
1066  */  */
1067  void  void
1068  BitstreamWriteVolHeader(Bitstream * const bs,  BitstreamWriteVolHeader(Bitstream * const bs,
1069                                                  const MBParam * pParam)                                                  const MBParam * pParam,
1070                                                    const FRAMEINFO * const frame)
1071  {  {
1072          static const unsigned int vo_id = 0;          static const unsigned int vo_id = 0;
1073          static const unsigned int vol_id = 0;          static const unsigned int vol_id = 0;
1074          int vol_ver_id = 1;          int vol_ver_id = 1;
1075          int vol_type_ind = VIDOBJLAY_TYPE_SIMPLE;          int vol_type_ind = VIDOBJLAY_TYPE_SIMPLE;
1076            int vol_profile = pParam->profile;
1077    
1078          if ( (pParam->vol_flags & XVID_VOL_QUARTERPEL) ||          if ( (pParam->vol_flags & XVID_VOL_QUARTERPEL) ||
1079           (pParam->vol_flags & XVID_VOL_GMC) ||           (pParam->vol_flags & XVID_VOL_GMC) ||
# Line 1091  Line 1084 
1084          vol_type_ind = VIDOBJLAY_TYPE_ART_SIMPLE;          vol_type_ind = VIDOBJLAY_TYPE_ART_SIMPLE;
1085      }      }
1086    
1087          if ((pParam->vol_flags & XVID_VOL_QUARTERPEL) ||      if ((pParam->vol_flags & (XVID_VOL_MPEGQUANT|XVID_VOL_QUARTERPEL|XVID_VOL_GMC|XVID_VOL_INTERLACING)) ||
1088          (pParam->vol_flags & XVID_VOL_GMC)) {           pParam->max_bframes>0) {
1089          vol_type_ind = VIDOBJLAY_TYPE_ASP;          vol_type_ind = VIDOBJLAY_TYPE_ASP;
1090      }      }
1091    
# Line 1106  Line 1099 
1099           * byte aligned, and that always 1-8 padding bits have been written           * byte aligned, and that always 1-8 padding bits have been written
1100           */           */
1101    
1102      if (pParam->profile) {      if (!vol_profile) {
1103              BitstreamPutBits(bs, VISOBJSEQ_START_CODE, 32);                  /* Profile was not set by client app, use the more permissive profile
1104              BitstreamPutBits(bs, pParam->profile, 8);                   * compatible with the vol_type_id */
1105                    switch(vol_type_ind) {
1106                    case VIDOBJLAY_TYPE_ASP:
1107                            vol_profile = 0xf5; /* ASP level 5 */
1108                            break;
1109                    case VIDOBJLAY_TYPE_ART_SIMPLE:
1110                            vol_profile = 0x94; /* ARTS level 4 */
1111                            break;
1112                    default:
1113                            vol_profile = 0x03; /* Simple level 3 */
1114                            break;
1115                    }
1116      }      }
1117    
1118            /* Write the VOS header */
1119            BitstreamPutBits(bs, VISOBJSEQ_START_CODE, 32);
1120            BitstreamPutBits(bs, vol_profile, 8);   /* profile_and_level_indication */
1121    
1122    
1123          /* visual_object_start_code */          /* visual_object_start_code */
1124          BitstreamPad(bs);          BitstreamPad(bs);
1125          BitstreamPutBits(bs, VISOBJ_START_CODE, 32);          BitstreamPutBits(bs, VISOBJ_START_CODE, 32);
# Line 1173  Line 1182 
1182    
1183      if (pParam->fincr>0) {      if (pParam->fincr>0) {
1184              BitstreamPutBit(bs, 1);             /* fixed_vop_rate = 1 */              BitstreamPutBit(bs, 1);             /* fixed_vop_rate = 1 */
1185              BitstreamPutBits(bs, pParam->fincr, log2bin(pParam->fbase-1));      /* fixed_vop_time_increment */      BitstreamPutBits(bs, pParam->fincr, MAX(log2bin(pParam->fbase-1),1));       /* fixed_vop_time_increment */
1186      }else{      }else{
1187          BitstreamPutBit(bs, 0);         /* fixed_vop_rate = 0 */          BitstreamPutBit(bs, 0);         /* fixed_vop_rate = 0 */
1188      }      }
# Line 1211  Line 1220 
1220          BitstreamPutBit(bs, pParam->vol_flags & XVID_VOL_MPEGQUANT);          BitstreamPutBit(bs, pParam->vol_flags & XVID_VOL_MPEGQUANT);
1221    
1222          if ((pParam->vol_flags & XVID_VOL_MPEGQUANT)) {          if ((pParam->vol_flags & XVID_VOL_MPEGQUANT)) {
1223                  BitstreamPutBit(bs, get_intra_matrix_status()); /* load_intra_quant_mat */                  BitstreamPutBit(bs, is_custom_intra_matrix(pParam->mpeg_quant_matrices));       /* load_intra_quant_mat */
1224                  if (get_intra_matrix_status()) {                  if(is_custom_intra_matrix(pParam->mpeg_quant_matrices))
1225                          bs_put_matrix(bs, get_intra_matrix());                          bs_put_matrix(bs, get_intra_matrix(pParam->mpeg_quant_matrices));
1226                  }  
1227                    BitstreamPutBit(bs, is_custom_inter_matrix(pParam->mpeg_quant_matrices));       /* load_inter_quant_mat */
1228                  BitstreamPutBit(bs, get_inter_matrix_status()); /* load_inter_quant_mat */                  if(is_custom_inter_matrix(pParam->mpeg_quant_matrices))
1229                  if (get_inter_matrix_status()) {                          bs_put_matrix(bs, get_inter_matrix(pParam->mpeg_quant_matrices));
                         bs_put_matrix(bs, get_inter_matrix());  
                 }  
   
1230          }          }
1231    
1232          if (vol_ver_id != 1) {          if (vol_ver_id != 1) {
# Line 1244  Line 1250 
1250    
1251          BitstreamPadAlways(bs); /* next_start_code(); */          BitstreamPadAlways(bs); /* next_start_code(); */
1252    
1253          /* fake divx5 id, to ensure compatibility with divx5 decoder */          /* divx5 userdata string */
1254  #define DIVX5_ID "DivX000b000p"  #define DIVX5_ID ((char *)"DivX503b1393")
1255          if (pParam->max_bframes > 0 && (pParam->global_flags & XVID_GLOBAL_PACKED)) {    if ((pParam->global_flags & XVID_GLOBAL_DIVX5_USERDATA)) {
1256                  BitstreamWriteUserData(bs, DIVX5_ID, strlen(DIVX5_ID));                  BitstreamWriteUserData(bs, DIVX5_ID, strlen(DIVX5_ID));
1257            if (pParam->max_bframes > 0 && (pParam->global_flags & XVID_GLOBAL_PACKED))
1258          BitstreamPutBits(bs, 'p', 8);
1259          }          }
1260    
1261          /* xvid id */          /* xvid id */
1262  #define XVID_ID "XviD" XVID_BS_VERSION          {
1263          BitstreamWriteUserData(bs, XVID_ID, strlen(XVID_ID));                  const char xvid_user_format[] = "XviD%04d%c";
1264                    char xvid_user_data[100];
1265                    sprintf(xvid_user_data,
1266                                    xvid_user_format,
1267                                    XVID_BS_VERSION,
1268                                    (frame->vop_flags & XVID_VOP_CARTOON)?'C':'\0');
1269                    BitstreamWriteUserData(bs, xvid_user_data, strlen(xvid_user_data));
1270            }
1271  }  }
1272    
1273    
# Line 1264  Line 1279 
1279                                                  Bitstream * const bs,                                                  Bitstream * const bs,
1280                                                  const MBParam * pParam,                                                  const MBParam * pParam,
1281                                                  const FRAMEINFO * const frame,                                                  const FRAMEINFO * const frame,
1282                                                  int vop_coded)                                                  int vop_coded,
1283                                                    unsigned int quant)
1284  {  {
1285          uint32_t i;          uint32_t i;
1286    
# Line 1292  Line 1308 
1308          WRITE_MARKER();          WRITE_MARKER();
1309    
1310          /* time_increment: value=nth_of_sec, nbits = log2(resolution) */          /* time_increment: value=nth_of_sec, nbits = log2(resolution) */
1311            BitstreamPutBits(bs, frame->ticks, MAX(log2bin(pParam->fbase-1), 1));
         BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase-1));  
1312  #if 0  #if 0
1313          DPRINTF("[%i:%i] %c",          DPRINTF("[%i:%i] %c",
1314                          frame->seconds, frame->ticks,                          frame->seconds, frame->ticks,
# Line 1323  Line 1338 
1338                  BitstreamPutBits(bs, frame->rounding_type, 1);                  BitstreamPutBits(bs, frame->rounding_type, 1);
1339    
1340          if ((frame->vol_flags & XVID_VOL_REDUCED_ENABLE))          if ((frame->vol_flags & XVID_VOL_REDUCED_ENABLE))
1341                  BitstreamPutBit(bs, (frame->vop_flags & XVID_VOP_REDUCED)?1:0);                  BitstreamPutBit(bs, 0);
1342    
1343          BitstreamPutBits(bs, 0, 3);     /* intra_dc_vlc_threshold */          BitstreamPutBits(bs, 0, 3);     /* intra_dc_vlc_threshold */
1344    
# Line 1357  Line 1372 
1372    
1373    
1374  #if 0  #if 0
1375          DPRINTF(XVID_DEBUG_HEADER, "quant = %i\n", frame->quant);          DPRINTF(XVID_DEBUG_HEADER, "quant = %i\n", quant);
1376  #endif  #endif
1377    
1378          BitstreamPutBits(bs, frame->quant, 5);  /* quantizer */          BitstreamPutBits(bs, quant, 5); /* quantizer */
1379    
1380          if (frame->coding_type != I_VOP)          if (frame->coding_type != I_VOP)
1381                  BitstreamPutBits(bs, frame->fcode, 3);  /* forward_fixed_code */                  BitstreamPutBits(bs, frame->fcode, 3);  /* forward_fixed_code */
# Line 1372  Line 1387 
1387    
1388  void  void
1389  BitstreamWriteUserData(Bitstream * const bs,  BitstreamWriteUserData(Bitstream * const bs,
1390                                                  uint8_t * data,                                                  const char *data,
1391                                                  const int length)                                                  const unsigned int length)
1392  {  {
1393          int i;          int i;
1394    
# Line 1385  Line 1400 
1400          }          }
1401    
1402  }  }
1403    
1404    /*
1405     * Group of VOP
1406     */
1407    void
1408    BitstreamWriteGroupOfVopHeader(Bitstream * const bs,
1409                                   const MBParam * pParam,
1410                                   uint32_t is_closed_gov)
1411    {
1412      int64_t time = (pParam->m_stamp + (pParam->fbase/2)) / pParam->fbase;
1413      int hours, minutes, seconds;
1414    
1415      /* compute time_code */
1416      seconds = time % 60; time /= 60;
1417      minutes = time % 60; time /= 60;
1418      hours = time % 24; /* don't overflow */
1419    
1420      BitstreamPutBits(bs, GRPOFVOP_START_CODE, 32);
1421      BitstreamPutBits(bs, hours, 5);
1422      BitstreamPutBits(bs, minutes, 6);
1423      BitstreamPutBit(bs, 1);
1424      BitstreamPutBits(bs, seconds, 6);
1425      BitstreamPutBits(bs, is_closed_gov, 1);
1426      BitstreamPutBits(bs, 0, 1); /* broken_link */
1427    }
1428    
1429    /*
1430     * End of Sequence
1431     */
1432    void
1433    BitstreamWriteEndOfSequence(Bitstream * const bs)
1434    {
1435        BitstreamPadAlways(bs);
1436        BitstreamPutBits(bs, VISOBJSEQ_STOP_CODE, 32);
1437    }
1438    
1439    /*
1440     * Video Packet (resync marker)
1441     */
1442    
1443    void write_video_packet_header(Bitstream * const bs,
1444                                   const MBParam * pParam,
1445                                   const FRAMEINFO * const frame,
1446                                   int mbnum)
1447    {
1448        const int mbnum_bits = log2bin(pParam->mb_width *  pParam->mb_height - 1);
1449        uint32_t nbitsresyncmarker;
1450    
1451        if (frame->coding_type == I_VOP)
1452          nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER;  /* 16 zeros followed by a 1. */
1453        else if (frame->coding_type == P_VOP)
1454          nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER-1 + frame->fcode;
1455        else /* B_VOP */
1456          nbitsresyncmarker = MAX(NUMBITS_VP_RESYNC_MARKER, NUMBITS_VP_RESYNC_MARKER-1 + MAX(frame->fcode, frame->bcode));
1457    
1458        BitstreamPadAlways(bs);
1459        BitstreamPutBits(bs, RESYNC_MARKER, nbitsresyncmarker);
1460        BitstreamPutBits(bs, mbnum, mbnum_bits);
1461        BitstreamPutBits(bs, frame->quant, 5);
1462        BitstreamPutBit(bs, 0); /* hec */
1463    }

Legend:
Removed from v.1.39.2.17  
changed lines
  Added in v.1.52

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