20 |
* along with this program ; if not, write to the Free Software |
* along with this program ; if not, write to the Free Software |
21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
* |
* |
23 |
* $Id: bitstream.c,v 1.54 2005-10-06 10:46:42 Isibaar Exp $ |
* $Id$ |
24 |
* |
* |
25 |
****************************************************************************/ |
****************************************************************************/ |
26 |
|
|
65 |
}; |
}; |
66 |
|
|
67 |
|
|
68 |
void |
static void |
69 |
bs_get_matrix(Bitstream * bs, |
bs_get_matrix(Bitstream * bs, |
70 |
uint8_t * matrix) |
uint8_t * matrix) |
71 |
{ |
{ |
153 |
READ_MARKER(); |
READ_MARKER(); |
154 |
if (dec->time_inc_bits) |
if (dec->time_inc_bits) |
155 |
time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); /* vop_time_increment */ |
time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); /* vop_time_increment */ |
156 |
|
else |
157 |
|
time_increment = 0; |
158 |
READ_MARKER(); |
READ_MARKER(); |
159 |
DPRINTF(XVID_DEBUG_HEADER,"time %i:%i\n", time_base, time_increment); |
DPRINTF(XVID_DEBUG_HEADER,"time %i:%i\n", time_base, time_increment); |
160 |
|
|
400 |
uint32_t start_code; |
uint32_t start_code; |
401 |
uint32_t time_incr = 0; |
uint32_t time_incr = 0; |
402 |
int32_t time_increment = 0; |
int32_t time_increment = 0; |
|
int resize = 0; |
|
403 |
|
|
404 |
while ((BitstreamPos(bs) >> 3) + 4 <= bs->length) { |
while ((BitstreamPos(bs) >> 3) + 4 <= bs->length) { |
405 |
|
|
424 |
DPRINTF(XVID_DEBUG_STARTCODE, "</visual_object_sequence>\n"); |
DPRINTF(XVID_DEBUG_STARTCODE, "</visual_object_sequence>\n"); |
425 |
|
|
426 |
} else if (start_code == VISOBJ_START_CODE) { |
} else if (start_code == VISOBJ_START_CODE) { |
|
int visobj_ver_id; |
|
427 |
|
|
428 |
DPRINTF(XVID_DEBUG_STARTCODE, "<visual_object>\n"); |
DPRINTF(XVID_DEBUG_STARTCODE, "<visual_object>\n"); |
429 |
|
|
430 |
BitstreamSkip(bs, 32); /* visual_object_start_code */ |
BitstreamSkip(bs, 32); /* visual_object_start_code */ |
431 |
if (BitstreamGetBit(bs)) /* is_visual_object_identified */ |
if (BitstreamGetBit(bs)) /* is_visual_object_identified */ |
432 |
{ |
{ |
433 |
visobj_ver_id = BitstreamGetBits(bs, 4); /* visual_object_ver_id */ |
dec->ver_id = BitstreamGetBits(bs, 4); /* visual_object_ver_id */ |
434 |
DPRINTF(XVID_DEBUG_HEADER,"visobj_ver_id %i\n", visobj_ver_id); |
DPRINTF(XVID_DEBUG_HEADER,"visobj_ver_id %i\n", dec->ver_id); |
435 |
BitstreamSkip(bs, 3); /* visual_object_priority */ |
BitstreamSkip(bs, 3); /* visual_object_priority */ |
436 |
} else { |
} else { |
437 |
visobj_ver_id = 1; |
dec->ver_id = 1; |
438 |
} |
} |
439 |
|
|
440 |
if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) /* visual_object_type */ |
if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) /* visual_object_type */ |
467 |
BitstreamSkip(bs, 32); /* video_object_start_code */ |
BitstreamSkip(bs, 32); /* video_object_start_code */ |
468 |
|
|
469 |
} else if ((start_code & ~VIDOBJLAY_START_CODE_MASK) == VIDOBJLAY_START_CODE) { |
} else if ((start_code & ~VIDOBJLAY_START_CODE_MASK) == VIDOBJLAY_START_CODE) { |
470 |
|
uint32_t width = 0, height = 0; |
471 |
|
|
472 |
DPRINTF(XVID_DEBUG_STARTCODE, "<video_object_layer>\n"); |
DPRINTF(XVID_DEBUG_STARTCODE, "<video_object_layer>\n"); |
473 |
DPRINTF(XVID_DEBUG_HEADER, "vol id %i\n", start_code & VIDOBJLAY_START_CODE_MASK); |
DPRINTF(XVID_DEBUG_HEADER, "vol id %i\n", start_code & VIDOBJLAY_START_CODE_MASK); |
484 |
DPRINTF(XVID_DEBUG_HEADER,"ver_id %i\n", vol_ver_id); |
DPRINTF(XVID_DEBUG_HEADER,"ver_id %i\n", vol_ver_id); |
485 |
BitstreamSkip(bs, 3); /* video_object_layer_priority */ |
BitstreamSkip(bs, 3); /* video_object_layer_priority */ |
486 |
} else { |
} else { |
487 |
vol_ver_id = 1; |
vol_ver_id = dec->ver_id; |
488 |
} |
} |
489 |
|
|
490 |
dec->aspect_ratio = BitstreamGetBits(bs, 4); |
dec->aspect_ratio = BitstreamGetBits(bs, 4); |
568 |
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) { |
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) { |
569 |
|
|
570 |
if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) { |
if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) { |
|
uint32_t width, height; |
|
|
|
|
571 |
READ_MARKER(); |
READ_MARKER(); |
572 |
width = BitstreamGetBits(bs, 13); /* video_object_layer_width */ |
width = BitstreamGetBits(bs, 13); /* video_object_layer_width */ |
573 |
READ_MARKER(); |
READ_MARKER(); |
576 |
|
|
577 |
DPRINTF(XVID_DEBUG_HEADER, "width %i\n", width); |
DPRINTF(XVID_DEBUG_HEADER, "width %i\n", width); |
578 |
DPRINTF(XVID_DEBUG_HEADER, "height %i\n", height); |
DPRINTF(XVID_DEBUG_HEADER, "height %i\n", height); |
|
|
|
|
if (dec->width != width || dec->height != height) |
|
|
{ |
|
|
if (dec->fixed_dimensions) |
|
|
{ |
|
|
DPRINTF(XVID_DEBUG_ERROR, "decoder width/height does not match bitstream\n"); |
|
|
return -1; |
|
|
} |
|
|
resize = 1; |
|
|
dec->width = width; |
|
|
dec->height = height; |
|
|
} |
|
579 |
} |
} |
580 |
|
|
581 |
dec->interlacing = BitstreamGetBit(bs); |
dec->interlacing = BitstreamGetBit(bs); |
752 |
|
|
753 |
} |
} |
754 |
|
|
755 |
return (resize ? -3 : -2 ); /* VOL */ |
if (((width > 0) && (height > 0)) && (dec->width != width || dec->height != height)) |
756 |
|
{ |
757 |
|
if (dec->fixed_dimensions) |
758 |
|
{ |
759 |
|
DPRINTF(XVID_DEBUG_ERROR, "decoder width/height does not match bitstream\n"); |
760 |
|
return -1; |
761 |
|
} |
762 |
|
dec->width = width; |
763 |
|
dec->height = height; |
764 |
|
return -3; |
765 |
|
} |
766 |
|
|
767 |
|
return -2; /* VOL */ |
768 |
|
|
769 |
} else if (start_code == GRPOFVOP_START_CODE) { |
} else if (start_code == GRPOFVOP_START_CODE) { |
770 |
|
|
820 |
dec->time = (dec->last_time_base + time_incr)*dec->time_inc_resolution + time_increment; |
dec->time = (dec->last_time_base + time_incr)*dec->time_inc_resolution + time_increment; |
821 |
dec->time_bp = dec->time_pp - (int32_t)(dec->last_non_b_time - dec->time); |
dec->time_bp = dec->time_pp - (int32_t)(dec->last_non_b_time - dec->time); |
822 |
} |
} |
823 |
|
if (dec->time_pp <= 0) dec->time_pp = 1; |
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); |
826 |
|
|
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 |
if (BitstreamGetBit(bs)); |
if (BitstreamGetBit(bs)) { |
864 |
DPRINTF(XVID_DEBUG_ERROR, "RRV not supported (anymore)\n"); |
DPRINTF(XVID_DEBUG_ERROR, "RRV not supported (anymore)\n"); |
865 |
} |
} |
866 |
|
} |
867 |
|
|
868 |
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { |
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { |
869 |
if(!(dec->sprite_enable == SPRITE_STATIC && coding_type == I_VOP)) { |
if(!(dec->sprite_enable == SPRITE_STATIC && coding_type == I_VOP)) { |
920 |
|
|
921 |
int i; |
int i; |
922 |
|
|
923 |
for (i = 0 ; i < dec->sprite_warping_points; i++) |
for (i = 0 ; i < MIN(4, dec->sprite_warping_points); i++) |
924 |
{ |
{ |
925 |
int length; |
int length; |
926 |
int x = 0, y = 0; |
int x = 0, y = 0; |
982 |
|
|
983 |
} else if (start_code == USERDATA_START_CODE) { |
} else if (start_code == USERDATA_START_CODE) { |
984 |
char tmp[256]; |
char tmp[256]; |
985 |
int i, version, build; |
int i, version = 0, build = 0; |
986 |
char packed; |
char packed; |
987 |
|
|
988 |
BitstreamSkip(bs, 32); /* user_data_start_code */ |
BitstreamSkip(bs, 32); /* user_data_start_code */ |
990 |
memset(tmp, 0, 256); |
memset(tmp, 0, 256); |
991 |
tmp[0] = BitstreamShowBits(bs, 8); |
tmp[0] = BitstreamShowBits(bs, 8); |
992 |
|
|
993 |
for(i = 1; i < 256; i++){ |
for(i = 1; i < 255; i++){ |
994 |
tmp[i] = (BitstreamShowBits(bs, 16) & 0xFF); |
tmp[i] = (BitstreamShowBits(bs, 16) & 0xFF); |
995 |
|
|
996 |
if(tmp[i] == 0) |
if(tmp[i] == 0) |
1025 |
version, build, dec->packed_mode); |
version, build, dec->packed_mode); |
1026 |
} |
} |
1027 |
|
|
1028 |
|
if ((dec->bs_version == 0) && (build > 0) && |
1029 |
|
(build != 1393)) { /* non-xvid stream with xvid fourcc */ |
1030 |
|
dec->bs_version = 0xffff; |
1031 |
|
} |
1032 |
|
|
1033 |
} else /* start_code == ? */ |
} else /* start_code == ? */ |
1034 |
{ |
{ |
1035 |
if (BitstreamShowBits(bs, 24) == 0x000001) { |
if (BitstreamShowBits(bs, 24) == 0x000001) { |
1073 |
void |
void |
1074 |
BitstreamWriteVolHeader(Bitstream * const bs, |
BitstreamWriteVolHeader(Bitstream * const bs, |
1075 |
const MBParam * pParam, |
const MBParam * pParam, |
1076 |
const FRAMEINFO * const frame) |
const FRAMEINFO * const frame, |
1077 |
|
const int num_slices) |
1078 |
{ |
{ |
1079 |
static const unsigned int vo_id = 0; |
static const unsigned int vo_id = 0; |
1080 |
static const unsigned int vol_id = 0; |
static const unsigned int vol_id = 0; |
1239 |
} |
} |
1240 |
|
|
1241 |
BitstreamPutBit(bs, 1); /* complexity_estimation_disable */ |
BitstreamPutBit(bs, 1); /* complexity_estimation_disable */ |
1242 |
BitstreamPutBit(bs, 1); /* resync_marker_disable */ |
|
1243 |
|
if (num_slices > 1) |
1244 |
|
BitstreamPutBit(bs, 0); /* resync_marker_enabled */ |
1245 |
|
else |
1246 |
|
BitstreamPutBit(bs, 1); /* resync_marker_disabled */ |
1247 |
|
|
1248 |
BitstreamPutBit(bs, 0); /* data_partitioned */ |
BitstreamPutBit(bs, 0); /* data_partitioned */ |
1249 |
|
|
1250 |
if (vol_ver_id != 1) { |
if (vol_ver_id != 1) { |
1259 |
/* divx5 userdata string */ |
/* divx5 userdata string */ |
1260 |
#define DIVX5_ID ((char *)"DivX503b1393") |
#define DIVX5_ID ((char *)"DivX503b1393") |
1261 |
if ((pParam->global_flags & XVID_GLOBAL_DIVX5_USERDATA)) { |
if ((pParam->global_flags & XVID_GLOBAL_DIVX5_USERDATA)) { |
1262 |
BitstreamWriteUserData(bs, DIVX5_ID, strlen(DIVX5_ID)); |
BitstreamWriteUserData(bs, DIVX5_ID, (uint32_t) strlen(DIVX5_ID)); |
1263 |
if (pParam->max_bframes > 0 && (pParam->global_flags & XVID_GLOBAL_PACKED)) |
if (pParam->max_bframes > 0 && (pParam->global_flags & XVID_GLOBAL_PACKED)) |
1264 |
BitstreamPutBits(bs, 'p', 8); |
BitstreamPutBits(bs, 'p', 8); |
1265 |
} |
} |
1272 |
xvid_user_format, |
xvid_user_format, |
1273 |
XVID_BS_VERSION, |
XVID_BS_VERSION, |
1274 |
(frame->vop_flags & XVID_VOP_CARTOON)?'C':'\0'); |
(frame->vop_flags & XVID_VOP_CARTOON)?'C':'\0'); |
1275 |
BitstreamWriteUserData(bs, xvid_user_data, strlen(xvid_user_data)); |
BitstreamWriteUserData(bs, xvid_user_data, (uint32_t) strlen(xvid_user_data)); |
1276 |
} |
} |
1277 |
} |
} |
1278 |
|
|
1393 |
const char *data, |
const char *data, |
1394 |
const unsigned int length) |
const unsigned int length) |
1395 |
{ |
{ |
1396 |
int i; |
unsigned int i; |
1397 |
|
|
1398 |
BitstreamPad(bs); |
BitstreamPad(bs); |
1399 |
BitstreamPutBits(bs, USERDATA_START_CODE, 32); |
BitstreamPutBits(bs, USERDATA_START_CODE, 32); |
1453 |
|
|
1454 |
if (frame->coding_type == I_VOP) |
if (frame->coding_type == I_VOP) |
1455 |
nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER; /* 16 zeros followed by a 1. */ |
nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER; /* 16 zeros followed by a 1. */ |
1456 |
else if (frame->coding_type == P_VOP) |
else if (frame->coding_type == B_VOP) /* B_VOP */ |
1457 |
nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER-1 + frame->fcode; |
nbitsresyncmarker = MAX(NUMBITS_VP_RESYNC_MARKER+1, NUMBITS_VP_RESYNC_MARKER + MAX(frame->fcode, frame->bcode) - 1); |
1458 |
else /* B_VOP */ |
else /*(frame->coding_type == P_VOP)*/ |
1459 |
nbitsresyncmarker = MAX(NUMBITS_VP_RESYNC_MARKER, NUMBITS_VP_RESYNC_MARKER-1 + MAX(frame->fcode, frame->bcode)); |
nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + frame->fcode - 1; |
1460 |
|
|
|
BitstreamPadAlways(bs); |
|
1461 |
BitstreamPutBits(bs, RESYNC_MARKER, nbitsresyncmarker); |
BitstreamPutBits(bs, RESYNC_MARKER, nbitsresyncmarker); |
1462 |
BitstreamPutBits(bs, mbnum, mbnum_bits); |
BitstreamPutBits(bs, mbnum, mbnum_bits); |
1463 |
BitstreamPutBits(bs, frame->quant, 5); |
BitstreamPutBits(bs, frame->quant, 5); |