50 #define SKIP_PICTURE (-10)
51 #define SKIP_TO_START_CODE (-1)
56 #define RING_BUF_SIZE 5
67 #define SEQ_END_CODE 0x000001b7
68 #define SEQ_START_CODE 0x000001b3
69 #define GOP_START_CODE 0x000001b8
70 #define PICTURE_START_CODE 0x00000100
71 #define SLICE_MIN_START_CODE 0x00000101
72 #define SLICE_MAX_START_CODE 0x000001af
73 #define EXT_START_CODE 0x000001b5
74 #define USER_START_CODE 0x000001b2
75 #define SEQUENCE_ERROR_CODE 0x000001b4
79 #define MB_QUANTUM 100
83 #define MB_STUFFING 34
88 #define DISPLAY_LOCK 0x01
89 #define PAST_LOCK 0x02
90 #define FUTURE_LOCK 0x04
92 #define MONO_THRESHOLD 11
97 extern const int scan[][8];
101 typedef int TimeStamp;
106 unsigned char *luminance;
109 unsigned char *display;
118 unsigned int tc_hours;
119 unsigned int tc_minutes;
120 unsigned int tc_seconds;
121 unsigned int tc_pictures;
132 unsigned int temp_ref;
133 unsigned int code_type;
134 unsigned int vbv_delay;
135 int full_pel_forw_vector;
137 unsigned int forw_r_size;
139 int full_pel_back_vector;
141 unsigned int back_r_size;
151 unsigned int vert_pos;
152 unsigned int quant_scale;
161 int motion_h_forw_code;
162 unsigned int motion_h_forw_r;
163 int motion_v_forw_code;
164 unsigned int motion_v_forw_r;
165 int motion_h_back_code;
166 unsigned int motion_h_back_r;
167 int motion_v_back_code;
168 unsigned int motion_v_back_r;
174 int recon_right_for_prev;
175 int recon_down_for_prev;
176 int recon_right_back_prev;
177 int recon_down_back_prev;
183 short int dct_recon[8][8];
184 short int dct_dc_y_past;
185 short int dct_dc_cr_past;
186 short int dct_dc_cb_past;
194 unsigned int mb_height;
195 unsigned int mb_width;
196 unsigned char aspect_ratio;
197 unsigned char picture_rate;
198 unsigned int bit_rate;
199 unsigned int vbv_buffer_size;
200 int const_param_flag;
201 unsigned char intra_quant_matrix[8][8];
203 unsigned char non_intra_quant_matrix[8][8];
214 unsigned int *buffer;
217 unsigned int *buf_start;
224 unsigned int num_left;
225 unsigned int leftover_bytes;
235 int gReservedStreamID;
236 int right_for,down_for;
237 int right_half_for, down_half_for;
238 unsigned int curBits;
244 double realTimeStart;
251 int ppm_width, ppm_height, ppm_modulus;
261 #define CONST_BITS 13
268 typedef short DCTELEM;
269 typedef DCTELEM DCTBLOCK[DCTSIZE2];
286 void init_stats P((
void ));
288 double ReadSysClock P((
void ));
290 void InitCrop P((
void ));
297 void DestroyPictImage P((
PictImage *apictimage));
299 void SetBFlag P((
int val ));
300 void SetPFlag P((
int val ));
307 void ComputeForwVector P((
int *recon_right_for_ptr ,
int *recon_down_for_ptr ,
mpeg_VidStream *the_stream ));
308 void ComputeBackVector P((
int *recon_right_back_ptr ,
int *recon_down_back_ptr,
mpeg_VidStream *the_stream ));
311 void mpeg_init_tables P((
void ));
312 void decodeDCTDCSizeLum P((
unsigned int *value ));
313 void decodeDCTDCSizeChrom P((
unsigned int *value ));
314 void decodeDCTCoeffFirst P((
unsigned int *run ,
int *level ));
315 void decodeDCTCoeffNext P((
unsigned int *run ,
int *level ));
320 int pure_get_more_data P((
unsigned int *buf_start ,
int max_length ,
int *length_ptr ,
unsigned int **buf_ptr,
mpeg_VidStream *
vid_stream ));
324 int ReadPackHeader P((
325 double *systemClockTime,
326 unsigned long *muxRate,
331 int find_start_code P(( FILE *input ));
335 void ReadTimeStamp P((
336 unsigned char *inputBuffer,
337 unsigned char *hiBit,
338 unsigned long *low4Bytes));
341 unsigned char *inputBuffer,
342 unsigned char *stdBufferScale,
343 unsigned long *stdBufferSize));
346 unsigned char *inputBuffer,
347 unsigned long *rate));
349 int MakeFloatClockTime P((
351 unsigned long low4Bytes,
352 double *floatClockTime));
359 #define NO_VID_STREAM (-1)
360 #define STREAM_UNDERFLOW (-2)
365 #define EXT_BUF_SIZE 1024
368 extern unsigned int bitMask[];
369 extern unsigned int nBitMask[];
370 extern unsigned int rBitMask[];
371 extern unsigned int bitTest[];
375 #define UPDATE_COUNT(numbits) bitCount += numbits
377 #define UPDATE_COUNT(numbits)
380 #ifdef NO_SANITY_CHECKS
381 #define get_bits1(result) \
384 result = ((vid_stream->curBits & 0x80000000) != 0); \
385 vid_stream->curBits <<= 1; \
386 vid_stream->bit_offset++; \
388 if (vid_stream->bit_offset & 0x20) { \
389 vid_stream->bit_offset = 0; \
390 vid_stream->buffer++; \
391 vid_stream->curBits = *vid_stream->buffer; \
392 vid_stream->buf_length--; \
396 #define get_bits2(result) \
399 vid_stream->bit_offset += 2; \
401 if (vid_stream->bit_offset & 0x20) { \
402 vid_stream->bit_offset -= 32; \
403 vid_stream->buffer++; \
404 vid_stream->buf_length--; \
405 if (vid_stream->bit_offset) { \
406 vid_stream->curBits |= \
407 (*vid_stream->buffer >> (2 - vid_stream->bit_offset)); \
409 result = ((vid_stream->curBits & 0xc0000000) >> 30); \
410 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset; \
413 result = ((vid_stream->curBits & 0xc0000000) >> 30); \
414 vid_stream->curBits <<= 2; \
417 #define get_bitsX(num, mask, shift, result) \
420 vid_stream->bit_offset += num; \
422 if (vid_stream->bit_offset & 0x20) { \
423 vid_stream->bit_offset -= 32; \
424 vid_stream->buffer++; \
425 vid_stream->buf_length--; \
426 if (vid_stream->bit_offset) { \
427 vid_stream->curBits |= (*vid_stream->buffer >> \
428 (num - vid_stream->bit_offset)); \
430 result = ((vid_stream->curBits & mask) >> shift); \
431 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset; \
434 result = ((vid_stream->curBits & mask) >> shift); \
435 vid_stream->curBits <<= num; \
440 #define get_bits1(result) \
444 if (vid_stream->buf_length < 2) { \
445 correct_underflow(vid_stream); \
448 result = ((vid_stream->curBits & 0x80000000) != 0); \
449 vid_stream->curBits <<= 1; \
450 vid_stream->bit_offset++; \
452 if (vid_stream->bit_offset & 0x20) { \
453 vid_stream->bit_offset = 0; \
454 vid_stream->buffer++; \
455 vid_stream->curBits = *vid_stream->buffer; \
456 vid_stream->buf_length--; \
460 #define get_bits2(result) \
464 if (vid_stream->buf_length < 2) { \
465 correct_underflow(vid_stream); \
468 vid_stream->bit_offset += 2; \
470 if (vid_stream->bit_offset & 0x20) { \
471 vid_stream->bit_offset -= 32; \
472 vid_stream->buffer++; \
473 vid_stream->buf_length--; \
474 if (vid_stream->bit_offset) { \
475 vid_stream->curBits |= (*vid_stream->buffer >> \
476 (2 - vid_stream->bit_offset)); \
478 result = ((vid_stream->curBits & 0xc0000000) >> 30); \
479 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset; \
482 result = ((vid_stream->curBits & 0xc0000000) >> 30); \
483 vid_stream->curBits <<= 2; \
486 #define get_bitsX(num, mask, shift, result) \
490 if (vid_stream->buf_length < 2) { \
491 correct_underflow(vid_stream); \
494 vid_stream->bit_offset += num; \
496 if (vid_stream->bit_offset & 0x20) { \
497 vid_stream->bit_offset -= 32; \
498 vid_stream->buffer++; \
499 vid_stream->buf_length--; \
500 if (vid_stream->bit_offset) { \
501 vid_stream->curBits |= (*vid_stream->buffer >> \
502 (num - vid_stream->bit_offset)); \
504 result = ((vid_stream->curBits & mask) >> shift); \
505 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset; \
508 result = ((vid_stream->curBits & mask) >> shift); \
509 vid_stream->curBits <<= num; \
514 #define get_bits3(result) get_bitsX(3, 0xe0000000, 29, result)
515 #define get_bits4(result) get_bitsX(4, 0xf0000000, 28, result)
516 #define get_bits5(result) get_bitsX(5, 0xf8000000, 27, result)
517 #define get_bits6(result) get_bitsX(6, 0xfc000000, 26, result)
518 #define get_bits7(result) get_bitsX(7, 0xfe000000, 25, result)
519 #define get_bits8(result) get_bitsX(8, 0xff000000, 24, result)
520 #define get_bits9(result) get_bitsX(9, 0xff800000, 23, result)
521 #define get_bits10(result) get_bitsX(10, 0xffc00000, 22, result)
522 #define get_bits11(result) get_bitsX(11, 0xffe00000, 21, result)
523 #define get_bits12(result) get_bitsX(12, 0xfff00000, 20, result)
524 #define get_bits14(result) get_bitsX(14, 0xfffc0000, 18, result)
525 #define get_bits16(result) get_bitsX(16, 0xffff0000, 16, result)
526 #define get_bits18(result) get_bitsX(18, 0xffffc000, 14, result)
527 #define get_bits32(result) get_bitsX(32, 0xffffffff, 0, result)
529 #define get_bitsn(num, result) get_bitsX((num), nBitMask[num], (32-(num)), result)
531 #ifdef NO_SANITY_CHECKS
532 #define show_bits32(result) \
534 if (vid_stream->bit_offset) { \
535 result = vid_stream->curBits | (*(vid_stream->buffer+1) >> \
536 (32 - vid_stream->bit_offset)); \
539 result = vid_stream->curBits; \
543 #define show_bitsX(num, mask, shift, result) \
546 bO = vid_stream->bit_offset + num; \
549 result = ((vid_stream->curBits & mask) >> shift) | \
550 (*(vid_stream->buffer+1) >> (shift + (num - bO))); \
553 result = ((vid_stream->curBits & mask) >> shift); \
558 #define show_bits32(result) \
561 if (vid_stream->buf_length < 2) { \
562 correct_underflow(vid_stream); \
564 if (vid_stream->bit_offset) { \
565 result = vid_stream->curBits | (*(vid_stream->buffer+1) >> \
566 (32 - vid_stream->bit_offset)); \
569 result = vid_stream->curBits; \
573 #define show_bitsX(num, mask, shift, result) \
578 if (vid_stream->buf_length < 2) { \
579 correct_underflow(vid_stream); \
581 bO = vid_stream->bit_offset + num; \
584 result = ((vid_stream->curBits & mask) >> shift) | \
585 (*(vid_stream->buffer+1) >> (shift + (num - bO))); \
588 result = ((vid_stream->curBits & mask) >> shift); \
593 #define show_bits1(result) show_bitsX(1, 0x80000000, 31, result)
594 #define show_bits2(result) show_bitsX(2, 0xc0000000, 30, result)
595 #define show_bits3(result) show_bitsX(3, 0xe0000000, 29, result)
596 #define show_bits4(result) show_bitsX(4, 0xf0000000, 28, result)
597 #define show_bits5(result) show_bitsX(5, 0xf8000000, 27, result)
598 #define show_bits6(result) show_bitsX(6, 0xfc000000, 26, result)
599 #define show_bits7(result) show_bitsX(7, 0xfe000000, 25, result)
600 #define show_bits8(result) show_bitsX(8, 0xff000000, 24, result)
601 #define show_bits9(result) show_bitsX(9, 0xff800000, 23, result)
602 #define show_bits10(result) show_bitsX(10, 0xffc00000, 22, result)
603 #define show_bits11(result) show_bitsX(11, 0xffe00000, 21, result)
604 #define show_bits12(result) show_bitsX(12, 0xfff00000, 20, result)
605 #define show_bits13(result) show_bitsX(13, 0xfff80000, 19, result)
606 #define show_bits14(result) show_bitsX(14, 0xfffc0000, 18, result)
607 #define show_bits15(result) show_bitsX(15, 0xfffe0000, 17, result)
608 #define show_bits16(result) show_bitsX(16, 0xffff0000, 16, result)
609 #define show_bits17(result) show_bitsX(17, 0xffff8000, 15, result)
610 #define show_bits18(result) show_bitsX(18, 0xffffc000, 14, result)
611 #define show_bits19(result) show_bitsX(19, 0xffffe000, 13, result)
612 #define show_bits20(result) show_bitsX(20, 0xfffff000, 12, result)
613 #define show_bits21(result) show_bitsX(21, 0xfffff800, 11, result)
614 #define show_bits22(result) show_bitsX(22, 0xfffffc00, 10, result)
615 #define show_bits23(result) show_bitsX(23, 0xfffffe00, 9, result)
616 #define show_bits24(result) show_bitsX(24, 0xffffff00, 8, result)
617 #define show_bits25(result) show_bitsX(25, 0xffffff80, 7, result)
618 #define show_bits26(result) show_bitsX(26, 0xffffffc0, 6, result)
619 #define show_bits27(result) show_bitsX(27, 0xffffffe0, 5, result)
620 #define show_bits28(result) show_bitsX(28, 0xfffffff0, 4, result)
621 #define show_bits29(result) show_bitsX(29, 0xfffffff8, 3, result)
622 #define show_bits30(result) show_bitsX(30, 0xfffffffc, 2, result)
623 #define show_bits31(result) show_bitsX(31, 0xfffffffe, 1, result)
625 #define show_bitsn(num,result) show_bitsX((num), (0xffffffff << (32-(num))), (32-(num)), result)
627 #ifdef NO_SANITY_CHECKS
628 #define flush_bits32 \
632 vid_stream->buffer++; \
633 vid_stream->buf_length--; \
634 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset;\
638 #define flush_bits(num) \
640 vid_stream->bit_offset += num; \
644 if (vid_stream->bit_offset & 0x20) { \
645 vid_stream->bit_offset -= 32; \
646 vid_stream->buffer++; \
647 vid_stream->buf_length--; \
648 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset;\
651 vid_stream->curBits <<= num; \
655 #define flush_bits32 \
657 if (vid_stream == NULL) { \
661 if (vid_stream->buf_length < 2) { \
662 correct_underflow(vid_stream); \
667 vid_stream->buffer++; \
668 vid_stream->buf_length--; \
669 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset;\
672 #define flush_bits(num) \
674 if (vid_stream== NULL) { \
678 if (vid_stream->buf_length < 2) { \
679 correct_underflow(vid_stream); \
684 vid_stream->bit_offset += num; \
686 if (vid_stream->bit_offset & 0x20) { \
687 vid_stream->buf_length--; \
688 vid_stream->bit_offset -= 32; \
689 vid_stream->buffer++; \
690 vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset;\
693 vid_stream->curBits <<= num; \
700 extern int LUM_RANGE;
706 #define CR_BASE (CB_BASE*CB_RANGE)
707 #define LUM_BASE (CR_BASE*CR_RANGE)
709 extern unsigned char pixel[256];
710 extern unsigned long wpixel[256];
711 extern int *lum_values;
712 extern int *cr_values;
713 extern int *cb_values;
715 #define Min(x,y) (((x) < (y)) ? (x) : (y))
716 #define Max(x,y) (((x) > (y)) ? (x) : (y))
718 #define GAMMA_CORRECTION(x) ((int)(pow((x) / 255.0, 1.0 / gammaCorrect) * 255.0))
719 #define CHROMA_CORRECTION256(x) ((x) >= 128 \
720 ? 128 + Min(127, (int)(((x) - 128.0) * chromaCorrect)) \
721 : 128 - Min(128, (int)((128.0 - (x)) * chromaCorrect)))
722 #define CHROMA_CORRECTION128(x) ((x) >= 0 \
723 ? Min(127, (int)(((x) * chromaCorrect))) \
724 : Max(-128, (int)(((x) * chromaCorrect))))
725 #define CHROMA_CORRECTION256D(x) ((x) >= 128 \
726 ? 128.0 + Min(127.0, (((x) - 128.0) * chromaCorrect)) \
727 : 128.0 - Min(128.0, (((128.0 - (x)) * chromaCorrect))))
728 #define CHROMA_CORRECTION128D(x) ((x) >= 0 \
729 ? Min(127.0, ((x) * chromaCorrect)) \
730 : Max(-128.0, ((x) * chromaCorrect)))
735 #define MPGERROR (-1)
738 #define MACRO_BLOCK_STUFFING 34
739 #define MACRO_BLOCK_ESCAPE 35
742 #define DCT_COEFF_FIRST 0
743 #define DCT_COEFF_NEXT 1
746 #define END_OF_BLOCK 62
758 unsigned int mb_quant;
759 unsigned int mb_motion_forward;
760 unsigned int mb_motion_backward;
761 unsigned int mb_pattern;
762 unsigned int mb_intra;
802 #define RUN_MASK 0xfc00
803 #define LEVEL_MASK 0x03f0
804 #define NUM_MASK 0x000f
806 #define LEVEL_SHIFT 4
808 #define DecodeDCTDCSizeLum(macro_val) \
810 unsigned int index; \
815 macro_val = dct_dc_size_luminance[index].value; \
816 flush_bits(dct_dc_size_luminance[index].num_bits); \
821 macro_val = dct_dc_size_luminance1[index].value; \
822 flush_bits(dct_dc_size_luminance1[index].num_bits); \
826 #define DecodeDCTDCSizeChrom(macro_val) \
828 unsigned int index; \
833 macro_val = dct_dc_size_chrominance[index].value; \
834 flush_bits(dct_dc_size_chrominance[index].num_bits); \
837 show_bits10(index); \
839 macro_val = dct_dc_size_chrominance1[index].value; \
840 flush_bits(dct_dc_size_chrominance1[index].num_bits); \
844 #define DecodeDCTCoeff(dct_coeff_tbl, run, level) \
846 unsigned int temp, index; \
847 unsigned int value, next32bits, flushed; \
849 show_bits32(next32bits); \
852 index = next32bits >> 24; \
855 value = dct_coeff_tbl[index]; \
856 run = value >> RUN_SHIFT; \
857 if (run != END_OF_BLOCK) { \
860 if (run != ESCAPE) { \
863 flushed = (value & NUM_MASK) + 2; \
864 level = (value & LEVEL_MASK) >> LEVEL_SHIFT; \
865 value = next32bits >> (32-flushed); \
867 if (value) level = -level; \
873 flushed = (value & NUM_MASK) + 1; \
874 temp = next32bits >> (18-flushed); \
882 level = next32bits >> (10-flushed); \
885 assert(level >= 128); \
886 } else if (temp != 128) { \
889 level = ((int) (temp << 24)) >> 24; \
892 level = next32bits >> (10-flushed); \
895 level = level - 256; \
896 assert(level <= -128 && level >= -255); \
900 flush_bits(flushed); \
901 assert (flushed <= 32); \
908 index = next32bits >> 22; \
909 value = dct_coeff_tbl_2[index & 3]; \
914 index = next32bits >> 22; \
915 value = dct_coeff_tbl_3[index & 3]; \
920 index = next32bits >> 20; \
921 value = dct_coeff_tbl_1[index & 15]; \
926 index = next32bits >> 16; \
927 value = dct_coeff_tbl_0[index & 255]; \
929 run = value >> RUN_SHIFT; \
930 level = (value & LEVEL_MASK) >> LEVEL_SHIFT; \
940 flushed = (value & NUM_MASK) + 2; \
941 value = next32bits >> (32-flushed); \
943 if (value) level = -level; \
946 flush_bits(flushed); \
947 assert (flushed <= 32); \
951 #define DecodeDCTCoeffFirst(runval, levelval) \
953 DecodeDCTCoeff(dct_coeff_first, runval, levelval); \
956 #define DecodeDCTCoeffNext(runval, levelval) \
958 DecodeDCTCoeff(dct_coeff_next, runval, levelval); \
961 #define DecodeMBAddrInc(val) \
963 unsigned int index; \
964 show_bits11(index); \
965 val = mb_addr_inc[index].value; \
966 flush_bits(mb_addr_inc[index].num_bits); \
968 #define DecodeMotionVectors(value) \
970 unsigned int index; \
971 show_bits11(index); \
972 value = motion_vectors[index].code; \
973 flush_bits(motion_vectors[index].num_bits); \
975 #define DecodeMBTypeB(quant, motion_fwd, motion_bwd, pat, intra) \
977 unsigned int index; \
981 quant = mb_type_B[index].mb_quant; \
982 motion_fwd = mb_type_B[index].mb_motion_forward; \
983 motion_bwd = mb_type_B[index].mb_motion_backward; \
984 pat = mb_type_B[index].mb_pattern; \
985 intra = mb_type_B[index].mb_intra; \
986 flush_bits(mb_type_B[index].num_bits); \
988 #define DecodeMBTypeI(quant, motion_fwd, motion_bwd, pat, intra) \
990 unsigned int index; \
991 static int quantTbl[4] = {MPGERROR, 1, 0, 0}; \
999 quant = quantTbl[index]; \
1001 flush_bits (1 + quant); \
1004 #define DecodeMBTypeP(quant, motion_fwd, motion_bwd, pat, intra) \
1006 unsigned int index; \
1008 show_bits6(index); \
1010 quant = mb_type_P[index].mb_quant; \
1011 motion_fwd = mb_type_P[index].mb_motion_forward; \
1012 motion_bwd = mb_type_P[index].mb_motion_backward; \
1013 pat = mb_type_P[index].mb_pattern; \
1014 intra = mb_type_P[index].mb_intra; \
1016 flush_bits(mb_type_P[index].num_bits); \
1018 #define DecodeCBP(coded_bp) \
1020 unsigned int index; \
1022 show_bits9(index); \
1023 coded_bp = coded_block_pattern[index].cbp; \
1024 flush_bits(coded_block_pattern[index].num_bits); \
1028 void j_rev_dct_sparse (DCTBLOCK data,
int pos);
1029 void j_rev_dct (DCTBLOCK data);