1 /* getpic.c, picture decoding */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
6 * Disclaimer of Warranty
8 * These software programs are available to the user without any license fee or
9 * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
10 * any and all warranties, whether express, implied, or statuary, including any
11 * implied warranties or merchantability or of fitness for a particular
12 * purpose. In no event shall the copyright-holder be liable for any
13 * incidental, punitive, or consequential damages of any kind whatsoever
14 * arising from the use of these programs.
16 * This disclaimer of warranty extends to the user of these programs and user's
17 * customers, employees, agents, transferees, successors, and assigns.
19 * The MPEG Software Simulation Group does not represent or warrant that the
20 * programs furnished hereunder are free of infringement of any third-party
23 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24 * are subject to royalty fees to patent holders. Many of these patents are
25 * general enough such that they are unavoidable regardless of implementation
33 /* private prototypes*/
34 static void picture_data _ANSI_ARGS_((int framenum));
35 static void macroblock_modes _ANSI_ARGS_((int *pmacroblock_type, int *pstwtype,
36 int *pstwclass, int *pmotion_type, int *pmotion_vector_count, int *pmv_format, int *pdmv,
37 int *pmvscale, int *pdct_type));
38 static void Clear_Block _ANSI_ARGS_((int comp));
39 static void Sum_Block _ANSI_ARGS_((int comp));
40 static void Saturate _ANSI_ARGS_((short *bp));
41 static void Add_Block _ANSI_ARGS_((int comp, int bx, int by,
42 int dct_type, int addflag));
43 static void Update_Picture_Buffers _ANSI_ARGS_((void));
44 static void frame_reorder _ANSI_ARGS_((int bitstream_framenum,
45 int sequence_framenum));
46 static void Decode_SNR_Macroblock _ANSI_ARGS_((int *SNRMBA, int *SNRMBAinc,
47 int MBA, int MBAmax, int *dct_type));
49 static void motion_compensation _ANSI_ARGS_((int MBA, int macroblock_type,
50 int motion_type, int PMV[2][2][2], int motion_vertical_field_select[2][2],
51 int dmvector[2], int stwtype, int dct_type));
53 static void skipped_macroblock _ANSI_ARGS_((int dc_dct_pred[3],
54 int PMV[2][2][2], int *motion_type, int motion_vertical_field_select[2][2],
55 int *stwtype, int *macroblock_type));
57 static int slice _ANSI_ARGS_((int framenum, int MBAmax));
59 static int start_of_slice _ANSI_ARGS_ ((int MBAmax, int *MBA,
60 int *MBAinc, int dc_dct_pred[3], int PMV[2][2][2]));
62 static int decode_macroblock _ANSI_ARGS_((int *macroblock_type,
63 int *stwtype, int *stwclass, int *motion_type, int *dct_type,
64 int PMV[2][2][2], int dc_dct_pred[3],
65 int motion_vertical_field_select[2][2], int dmvector[2]));
68 /* decode one frame or field picture */
69 void Decode_Picture(bitstream_framenum, sequence_framenum)
70 int bitstream_framenum, sequence_framenum;
73 if (picture_structure==FRAME_PICTURE && Second_Field)
75 /* recover from illegal number of field pictures */
76 printf("odd number of field pictures\n");
80 /* IMPLEMENTATION: update picture buffer pointers */
81 Update_Picture_Buffers();
84 Check_Headers(bitstream_framenum, sequence_framenum);
87 /* ISO/IEC 13818-4 section 2.4.5.4 "frame buffer intercept method" */
88 /* (section number based on November 1995 (Dallas) draft of the
89 conformance document) */
91 Substitute_Frame_Buffer(bitstream_framenum, sequence_framenum);
93 /* form spatial scalable picture */
95 /* form spatial scalable picture */
96 /* ISO/IEC 13818-2 section 7.7: Spatial scalability */
97 if (base.pict_scal && !Second_Field)
102 /* decode picture data ISO/IEC 13818-2 section 6.2.3.7 */
103 picture_data(bitstream_framenum);
105 /* write or display current or previously decoded reference frame */
106 /* ISO/IEC 13818-2 section 6.1.1.11: Frame reordering */
107 frame_reorder(bitstream_framenum, sequence_framenum);
109 if (picture_structure!=FRAME_PICTURE)
110 Second_Field = !Second_Field;
114 /* decode all macroblocks of the current picture */
115 /* stages described in ISO/IEC 13818-2 section 7 */
116 static void picture_data(framenum)
121 /* number of macroblocks per picture */
122 MBAmax = mb_width*mb_height;
124 if (picture_structure!=FRAME_PICTURE)
125 MBAmax>>=1; /* field picture has half as many macroblocks as frame */
129 if(slice(framenum, MBAmax)<0)
137 /* decode all macroblocks of the current picture */
138 /* ISO/IEC 13818-2 section 6.3.16 */
139 static int slice(framenum, MBAmax)
140 int framenum, MBAmax;
143 int MBAinc, macroblock_type, motion_type, dct_type;
145 int PMV[2][2][2], motion_vertical_field_select[2][2];
147 int stwtype, stwclass;
148 int SNRMBA, SNRMBAinc;
152 MBA = 0; /* macroblock address */
155 if((ret=start_of_slice(MBAmax, &MBA, &MBAinc, dc_dct_pred, PMV))!=1)
158 if (Two_Streams && enhan.scalable_mode==SC_SNR)
169 /* this is how we properly exit out of picture */
171 return(-1); /* all macroblocks decoded */
175 printf("frame %d, MB %d\n",framenum,MBA);
179 if (!progressive_frame && picture_structure==FRAME_PICTURE
180 && MBA==(MBAmax>>1) && framenum!=0 && Output_Type==T_X11
181 && !Display_Progressive_Flag)
183 Display_Second_Field();
191 if (base.scalable_mode==SC_DP && base.priority_breakpoint==1)
194 if (!Show_Bits(23) || Fault_Flag) /* next_start_code or fault */
196 resync: /* if Fault_Flag: resynchronize to next next_start_code */
198 return(0); /* trigger: go to next slice */
200 else /* neither next_start_code nor Fault_Flag */
202 if (base.scalable_mode==SC_DP && base.priority_breakpoint==1)
205 /* decode macroblock address increment */
206 MBAinc = Get_macroblock_address_increment();
208 if (Fault_Flag) goto resync;
214 /* MBAinc points beyond picture dimensions */
216 printf("Too many macroblocks in picture\n");
220 if (MBAinc==1) /* not skipped */
222 ret = decode_macroblock(¯oblock_type, &stwtype, &stwclass,
223 &motion_type, &dct_type, PMV, dc_dct_pred,
224 motion_vertical_field_select, dmvector);
233 else /* MBAinc!=1: skipped macroblock */
235 /* ISO/IEC 13818-2 section 7.6.6 */
236 skipped_macroblock(dc_dct_pred, PMV, &motion_type,
237 motion_vertical_field_select, &stwtype, ¯oblock_type);
240 /* SCALABILITY: SNR */
241 /* ISO/IEC 13818-2 section 7.8 */
242 /* NOTE: we currently ignore faults encountered in this routine */
243 if (Two_Streams && enhan.scalable_mode==SC_SNR)
244 Decode_SNR_Macroblock(&SNRMBA, &SNRMBAinc, MBA, MBAmax, &dct_type);
246 /* ISO/IEC 13818-2 section 7.6 */
247 motion_compensation(MBA, macroblock_type, motion_type, PMV,
248 motion_vertical_field_select, dmvector, stwtype, dct_type);
251 /* advance to next macroblock */
255 /* SCALABILITY: SNR */
256 if (Two_Streams && enhan.scalable_mode==SC_SNR)
263 return(-1); /* all macroblocks decoded */
268 /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
269 static void macroblock_modes(pmacroblock_type,pstwtype,pstwclass,
270 pmotion_type,pmotion_vector_count,pmv_format,pdmv,pmvscale,pdct_type)
271 int *pmacroblock_type, *pstwtype, *pstwclass;
272 int *pmotion_type, *pmotion_vector_count, *pmv_format, *pdmv, *pmvscale;
276 int stwtype, stwcode, stwclass;
278 int motion_vector_count, mv_format, dmv, mvscale;
280 static unsigned char stwc_table[3][4]
281 = { {6,3,7,4}, {2,1,5,4}, {2,5,7,4} };
282 static unsigned char stwclass_table[9]
283 = {0, 1, 2, 1, 1, 2, 3, 3, 4};
285 /* get macroblock_type */
286 macroblock_type = Get_macroblock_type();
288 if (Fault_Flag) return;
290 /* get spatial_temporal_weight_code */
291 if (macroblock_type & MB_WEIGHT)
293 if (spatial_temporal_weight_code_table_index==0)
297 stwcode = Get_Bits(2);
301 printf("spatial_temporal_weight_code (");
302 Print_Bits(stwcode,2,2);
303 printf("): %d\n",stwcode);
306 stwtype = stwc_table[spatial_temporal_weight_code_table_index-1][stwcode];
310 stwtype = (macroblock_type & MB_CLASS4) ? 8 : 0;
312 /* SCALABILITY: derive spatial_temporal_weight_class (Table 7-18) */
313 stwclass = stwclass_table[stwtype];
315 /* get frame/field motion type */
316 if (macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD))
318 if (picture_structure==FRAME_PICTURE) /* frame_motion_type */
320 motion_type = frame_pred_frame_dct ? MC_FRAME : Get_Bits(2);
322 if (!frame_pred_frame_dct && Trace_Flag)
324 printf("frame_motion_type (");
325 Print_Bits(motion_type,2,2);
326 printf("): %s\n",motion_type==MC_FIELD?"Field":
327 motion_type==MC_FRAME?"Frame":
328 motion_type==MC_DMV?"Dual_Prime":"Invalid");
332 else /* field_motion_type */
334 motion_type = Get_Bits(2);
338 printf("field_motion_type (");
339 Print_Bits(motion_type,2,2);
340 printf("): %s\n",motion_type==MC_FIELD?"Field":
341 motion_type==MC_16X8?"16x8 MC":
342 motion_type==MC_DMV?"Dual_Prime":"Invalid");
347 else if ((macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
349 /* concealment motion vectors */
350 motion_type = (picture_structure==FRAME_PICTURE) ? MC_FRAME : MC_FIELD;
355 printf("maroblock_modes(): unknown macroblock type\n");
360 /* derive motion_vector_count, mv_format and dmv, (table 6-17, 6-18) */
361 if (picture_structure==FRAME_PICTURE)
363 motion_vector_count = (motion_type==MC_FIELD && stwclass<2) ? 2 : 1;
364 mv_format = (motion_type==MC_FRAME) ? MV_FRAME : MV_FIELD;
368 motion_vector_count = (motion_type==MC_16X8) ? 2 : 1;
369 mv_format = MV_FIELD;
372 dmv = (motion_type==MC_DMV); /* dual prime */
374 /* field mv predictions in frame pictures have to be scaled
375 * ISO/IEC 13818-2 section 7.6.3.1 Decoding the motion vectors
376 * IMPLEMENTATION: mvscale is derived for later use in motion_vectors()
377 * it displaces the stage:
379 * if((mv_format=="field")&&(t==1)&&(picture_structure=="Frame picture"))
380 * prediction = PMV[r][s][t] DIV 2;
383 mvscale = ((mv_format==MV_FIELD) && (picture_structure==FRAME_PICTURE));
385 /* get dct_type (frame DCT / field DCT) */
386 dct_type = (picture_structure==FRAME_PICTURE)
387 && (!frame_pred_frame_dct)
388 && (macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA))
393 if (Trace_Flag && (picture_structure==FRAME_PICTURE)
394 && (!frame_pred_frame_dct)
395 && (macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA)))
396 printf("dct_type (%d): %s\n",dct_type,dct_type?"Field":"Frame");
400 *pmacroblock_type = macroblock_type;
402 *pstwclass = stwclass;
403 *pmotion_type = motion_type;
404 *pmotion_vector_count = motion_vector_count;
405 *pmv_format = mv_format;
408 *pdct_type = dct_type;
412 /* move/add 8x8-Block from block[comp] to backward_reference_frame */
413 /* copy reconstructed 8x8 block from block[comp] to current_frame[]
414 * ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data
415 * This stage also embodies some of the operations implied by:
416 * - ISO/IEC 13818-2 section 7.6.7: Combining predictions
417 * - ISO/IEC 13818-2 section 6.1.3: Macroblock
419 static void Add_Block(comp,bx,by,dct_type,addflag)
420 int comp,bx,by,dct_type,addflag;
427 /* derive color component index */
428 /* equivalent to ISO/IEC 13818-2 Table 7-1 */
429 cc = (comp<4) ? 0 : (comp&1)+1; /* color component index */
435 if (picture_structure==FRAME_PICTURE)
438 /* field DCT coding */
439 rfp = current_frame[0]
440 + Coded_Picture_Width*(by+((comp&2)>>1)) + bx + ((comp&1)<<3);
441 iincr = (Coded_Picture_Width<<1) - 8;
445 /* frame DCT coding */
446 rfp = current_frame[0]
447 + Coded_Picture_Width*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
448 iincr = Coded_Picture_Width - 8;
453 rfp = current_frame[0]
454 + (Coded_Picture_Width<<1)*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
455 iincr = (Coded_Picture_Width<<1) - 8;
462 /* scale coordinates */
463 if (chroma_format!=CHROMA444)
465 if (chroma_format==CHROMA420)
467 if (picture_structure==FRAME_PICTURE)
469 if (dct_type && (chroma_format!=CHROMA420))
471 /* field DCT coding */
472 rfp = current_frame[cc]
473 + Chroma_Width*(by+((comp&2)>>1)) + bx + (comp&8);
474 iincr = (Chroma_Width<<1) - 8;
478 /* frame DCT coding */
479 rfp = current_frame[cc]
480 + Chroma_Width*(by+((comp&2)<<2)) + bx + (comp&8);
481 iincr = Chroma_Width - 8;
487 rfp = current_frame[cc]
488 + (Chroma_Width<<1)*(by+((comp&2)<<2)) + bx + (comp&8);
489 iincr = (Chroma_Width<<1) - 8;
493 bp = ld->block[comp];
501 *rfp = Clip[*bp++ + *rfp];
513 *rfp++ = Clip[*bp++ + 128];
521 /* ISO/IEC 13818-2 section 7.8 */
522 static void Decode_SNR_Macroblock(SNRMBA, SNRMBAinc, MBA, MBAmax, dct_type)
523 int *SNRMBA, *SNRMBAinc;
527 int SNRmacroblock_type, SNRcoded_block_pattern, SNRdct_type, dummy;
528 int slice_vert_pos_ext, quantizer_scale_code, comp, code;
534 if (!Show_Bits(23)) /* next_start_code */
537 code = Show_Bits(32);
539 if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
541 /* only slice headers are allowed in picture_data */
543 printf("SNR: Premature end of picture\n");
549 /* decode slice header (may change quantizer_scale) */
550 slice_vert_pos_ext = slice_header();
552 /* decode macroblock address increment */
553 *SNRMBAinc = Get_macroblock_address_increment();
555 /* set current location */
557 ((slice_vert_pos_ext<<7) + (code&255) - 1)*mb_width + *SNRMBAinc - 1;
559 *SNRMBAinc = 1; /* first macroblock in slice: not skipped */
561 else /* not next_start_code */
566 printf("Too many macroblocks in picture\n");
570 /* decode macroblock address increment */
571 *SNRMBAinc = Get_macroblock_address_increment();
577 /* streams out of sync */
579 printf("Cant't synchronize streams\n");
583 if (*SNRMBAinc==1) /* not skipped */
585 macroblock_modes(&SNRmacroblock_type, &dummy, &dummy,
586 &dummy, &dummy, &dummy, &dummy, &dummy,
589 if (SNRmacroblock_type & MACROBLOCK_PATTERN)
590 *dct_type = SNRdct_type;
592 if (SNRmacroblock_type & MACROBLOCK_QUANT)
594 quantizer_scale_code = Get_Bits(5);
595 ld->quantizer_scale =
596 ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code] : quantizer_scale_code<<1;
599 /* macroblock_pattern */
600 if (SNRmacroblock_type & MACROBLOCK_PATTERN)
602 SNRcoded_block_pattern = Get_coded_block_pattern();
604 if (chroma_format==CHROMA422)
605 SNRcoded_block_pattern = (SNRcoded_block_pattern<<2) | Get_Bits(2); /* coded_block_pattern_1 */
606 else if (chroma_format==CHROMA444)
607 SNRcoded_block_pattern = (SNRcoded_block_pattern<<6) | Get_Bits(6); /* coded_block_pattern_2 */
610 SNRcoded_block_pattern = 0;
613 for (comp=0; comp<block_count; comp++)
617 if (SNRcoded_block_pattern & (1<<(block_count-1-comp)))
618 Decode_MPEG2_Non_Intra_Block(comp);
621 else /* SNRMBAinc!=1: skipped macroblock */
623 for (comp=0; comp<block_count; comp++)
632 /* IMPLEMENTATION: set scratch pad macroblock to zero */
633 static void Clear_Block(comp)
639 Block_Ptr = ld->block[comp];
646 /* SCALABILITY: add SNR enhancement layer block data to base layer */
647 /* ISO/IEC 13818-2 section 7.8.3.4: Addition of coefficients from the two layes */
648 static void Sum_Block(comp)
651 short *Block_Ptr1, *Block_Ptr2;
654 Block_Ptr1 = base.block[comp];
655 Block_Ptr2 = enhan.block[comp];
658 *Block_Ptr1++ += *Block_Ptr2++;
662 /* limit coefficients to -2048..2047 */
663 /* ISO/IEC 13818-2 section 7.4.3 and 7.4.4: Saturation and Mismatch control */
664 static void Saturate(Block_Ptr)
671 /* ISO/IEC 13818-2 section 7.4.3: Saturation */
685 /* ISO/IEC 13818-2 section 7.4.4: Mismatch control */
692 /* reuse old picture buffers as soon as they are no longer needed
693 based on life-time axioms of MPEG */
694 static void Update_Picture_Buffers()
696 int cc; /* color component index */
697 unsigned char *tmp; /* temporary swap pointer */
699 for (cc=0; cc<3; cc++)
701 /* B pictures do not need to be save for future reference */
702 if (picture_coding_type==B_TYPE)
704 current_frame[cc] = auxframe[cc];
708 /* only update at the beginning of the coded frame */
711 tmp = forward_reference_frame[cc];
713 /* the previously decoded reference frame is stored
714 coincident with the location where the backward
715 reference frame is stored (backwards prediction is not
716 needed in P pictures) */
717 forward_reference_frame[cc] = backward_reference_frame[cc];
719 /* update pointer for potential future B pictures */
720 backward_reference_frame[cc] = tmp;
723 /* can erase over old backward reference frame since it is not used
724 in a P picture, and since any subsequent B pictures will use the
725 previously decoded I or P frame as the backward_reference_frame */
726 current_frame[cc] = backward_reference_frame[cc];
730 one-time folding of a line offset into the pointer which stores the
731 memory address of the current frame saves offsets and conditional
732 branches throughout the remainder of the picture processing loop */
733 if (picture_structure==BOTTOM_FIELD)
734 current_frame[cc]+= (cc==0) ? Coded_Picture_Width : Chroma_Width;
739 /* store last frame */
741 void Output_Last_Frame_of_Sequence(Framenum)
745 printf("last frame incomplete, not stored\n");
747 Write_Frame(backward_reference_frame,Framenum-1);
752 static void frame_reorder(Bitstream_Framenum, Sequence_Framenum)
753 int Bitstream_Framenum, Sequence_Framenum;
755 /* tracking variables to insure proper output in spatial scalability */
756 static int Oldref_progressive_frame, Newref_progressive_frame;
758 if (Sequence_Framenum!=0)
760 if (picture_structure==FRAME_PICTURE || Second_Field)
762 if (picture_coding_type==B_TYPE)
763 Write_Frame(auxframe,Bitstream_Framenum-1);
766 Newref_progressive_frame = progressive_frame;
767 progressive_frame = Oldref_progressive_frame;
769 Write_Frame(forward_reference_frame,Bitstream_Framenum-1);
771 Oldref_progressive_frame = progressive_frame = Newref_progressive_frame;
775 else if (Output_Type==T_X11)
777 if(!Display_Progressive_Flag)
778 Display_Second_Field();
783 Oldref_progressive_frame = progressive_frame;
788 /* ISO/IEC 13818-2 section 7.6 */
789 static void motion_compensation(MBA, macroblock_type, motion_type, PMV,
790 motion_vertical_field_select, dmvector, stwtype, dct_type)
795 int motion_vertical_field_select[2][2];
803 /* derive current macroblock position within picture */
804 /* ISO/IEC 13818-2 section 6.3.1.6 and 6.3.1.7 */
805 bx = 16*(MBA%mb_width);
806 by = 16*(MBA/mb_width);
808 /* motion compensation */
809 if (!(macroblock_type & MACROBLOCK_INTRA))
810 form_predictions(bx,by,macroblock_type,motion_type,PMV,
811 motion_vertical_field_select,dmvector,stwtype);
813 /* SCALABILITY: Data Partitioning */
814 if (base.scalable_mode==SC_DP)
817 /* copy or add block data into picture */
818 for (comp=0; comp<block_count; comp++)
820 /* SCALABILITY: SNR */
821 /* ISO/IEC 13818-2 section 7.8.3.4: Addition of coefficients from
823 if (Two_Streams && enhan.scalable_mode==SC_SNR)
824 Sum_Block(comp); /* add SNR enhancement layer data to base layer */
826 /* MPEG-2 saturation and mismatch control */
827 /* base layer could be MPEG-1 stream, enhancement MPEG-2 SNR */
828 /* ISO/IEC 13818-2 section 7.4.3 and 7.4.4: Saturation and Mismatch control */
829 if ((Two_Streams && enhan.scalable_mode==SC_SNR) || ld->MPEG2_Flag)
830 Saturate(ld->block[comp]);
832 /* ISO/IEC 13818-2 section Annex A: inverse DCT */
833 if (Reference_IDCT_Flag)
834 Reference_IDCT(ld->block[comp]);
836 Fast_IDCT(ld->block[comp]);
838 /* ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data */
839 Add_Block(comp,bx,by,dct_type,(macroblock_type & MACROBLOCK_INTRA)==0);
846 /* ISO/IEC 13818-2 section 7.6.6 */
847 static void skipped_macroblock(dc_dct_pred, PMV, motion_type,
848 motion_vertical_field_select, stwtype, macroblock_type)
852 int motion_vertical_field_select[2][2];
854 int *macroblock_type;
858 /* SCALABILITY: Data Paritioning */
859 if (base.scalable_mode==SC_DP)
862 for (comp=0; comp<block_count; comp++)
865 /* reset intra_dc predictors */
866 /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
867 dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
869 /* reset motion vector predictors */
870 /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
871 if (picture_coding_type==P_TYPE)
872 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
874 /* derive motion_type */
875 if (picture_structure==FRAME_PICTURE)
876 *motion_type = MC_FRAME;
879 *motion_type = MC_FIELD;
881 /* predict from field of same parity */
882 /* ISO/IEC 13818-2 section 7.6.6.1 and 7.6.6.3: P field picture and B field
884 motion_vertical_field_select[0][0]=motion_vertical_field_select[0][1] =
885 (picture_structure==BOTTOM_FIELD);
888 /* skipped I are spatial-only predicted, */
889 /* skipped P and B are temporal-only predicted */
890 /* ISO/IEC 13818-2 section 7.7.6: Skipped macroblocks */
891 *stwtype = (picture_coding_type==I_TYPE) ? 8 : 0;
893 /* IMPLEMENTATION: clear MACROBLOCK_INTRA */
894 *macroblock_type&= ~MACROBLOCK_INTRA;
899 /* return==-1 means go to next picture */
900 /* the expression "start of slice" is used throughout the normative
901 body of the MPEG specification */
902 static int start_of_slice(MBAmax, MBA, MBAinc,
911 int slice_vert_pos_ext;
919 code = Show_Bits(32);
921 if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
923 /* only slice headers are allowed in picture_data */
925 printf("start_of_slice(): Premature end of picture\n");
927 return(-1); /* trigger: go to next picture */
932 /* decode slice header (may change quantizer_scale) */
933 slice_vert_pos_ext = slice_header();
936 /* SCALABILITY: Data Partitioning */
937 if (base.scalable_mode==SC_DP)
941 code = Show_Bits(32);
943 if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
945 /* only slice headers are allowed in picture_data */
947 printf("DP: Premature end of picture\n");
948 return(-1); /* trigger: go to next picture */
953 /* decode slice header (may change quantizer_scale) */
954 slice_vert_pos_ext = slice_header();
956 if (base.priority_breakpoint!=1)
960 /* decode macroblock address increment */
961 *MBAinc = Get_macroblock_address_increment();
965 printf("start_of_slice(): MBAinc unsuccessful\n");
966 return(0); /* trigger: go to next slice */
969 /* set current location */
970 /* NOTE: the arithmetic used to derive macroblock_address below is
971 * equivalent to ISO/IEC 13818-2 section 6.3.17: Macroblock
973 *MBA = ((slice_vert_pos_ext<<7) + (code&255) - 1)*mb_width + *MBAinc - 1;
974 *MBAinc = 1; /* first macroblock in slice: not skipped */
976 /* reset all DC coefficient and motion vector predictors */
977 /* reset all DC coefficient and motion vector predictors */
978 /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
979 dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
981 /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
982 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
983 PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
985 /* successfull: trigger decode macroblocks in slice */
990 /* ISO/IEC 13818-2 sections 7.2 through 7.5 */
991 static int decode_macroblock(macroblock_type, stwtype, stwclass,
992 motion_type, dct_type, PMV, dc_dct_pred,
993 motion_vertical_field_select, dmvector)
994 int *macroblock_type;
1001 int motion_vertical_field_select[2][2];
1005 int quantizer_scale_code;
1008 int motion_vector_count;
1012 int coded_block_pattern;
1014 /* SCALABILITY: Data Patitioning */
1015 if (base.scalable_mode==SC_DP)
1017 if (base.priority_breakpoint<=2)
1023 /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
1024 macroblock_modes(macroblock_type, stwtype, stwclass,
1025 motion_type, &motion_vector_count, &mv_format, &dmv, &mvscale,
1028 if (Fault_Flag) return(0); /* trigger: go to next slice */
1030 if (*macroblock_type & MACROBLOCK_QUANT)
1032 quantizer_scale_code = Get_Bits(5);
1037 printf("quantiser_scale_code (");
1038 Print_Bits(quantizer_scale_code,5,5);
1039 printf("): %d\n",quantizer_scale_code);
1043 /* ISO/IEC 13818-2 section 7.4.2.2: Quantizer scale factor */
1045 ld->quantizer_scale =
1046 ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code]
1047 : (quantizer_scale_code << 1);
1049 ld->quantizer_scale = quantizer_scale_code;
1051 /* SCALABILITY: Data Partitioning */
1052 if (base.scalable_mode==SC_DP)
1053 /* make sure base.quantizer_scale is valid */
1054 base.quantizer_scale = ld->quantizer_scale;
1057 /* motion vectors */
1060 /* ISO/IEC 13818-2 section 6.3.17.2: Motion vectors */
1062 /* decode forward motion vectors */
1063 if ((*macroblock_type & MACROBLOCK_MOTION_FORWARD)
1064 || ((*macroblock_type & MACROBLOCK_INTRA)
1065 && concealment_motion_vectors))
1068 motion_vectors(PMV,dmvector,motion_vertical_field_select,
1069 0,motion_vector_count,mv_format,f_code[0][0]-1,f_code[0][1]-1,
1072 motion_vector(PMV[0][0],dmvector,
1073 forward_f_code-1,forward_f_code-1,0,0,full_pel_forward_vector);
1076 if (Fault_Flag) return(0); /* trigger: go to next slice */
1078 /* decode backward motion vectors */
1079 if (*macroblock_type & MACROBLOCK_MOTION_BACKWARD)
1082 motion_vectors(PMV,dmvector,motion_vertical_field_select,
1083 1,motion_vector_count,mv_format,f_code[1][0]-1,f_code[1][1]-1,0,
1086 motion_vector(PMV[0][1],dmvector,
1087 backward_f_code-1,backward_f_code-1,0,0,full_pel_backward_vector);
1090 if (Fault_Flag) return(0); /* trigger: go to next slice */
1092 if ((*macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
1093 Flush_Buffer(1); /* remove marker_bit */
1095 if (base.scalable_mode==SC_DP && base.priority_breakpoint==3)
1098 /* macroblock_pattern */
1099 /* ISO/IEC 13818-2 section 6.3.17.4: Coded block pattern */
1100 if (*macroblock_type & MACROBLOCK_PATTERN)
1102 coded_block_pattern = Get_coded_block_pattern();
1104 if (chroma_format==CHROMA422)
1106 /* coded_block_pattern_1 */
1107 coded_block_pattern = (coded_block_pattern<<2) | Get_Bits(2);
1112 printf("coded_block_pattern_1: ");
1113 Print_Bits(coded_block_pattern,2,2);
1114 printf(" (%d)\n",coded_block_pattern&3);
1118 else if (chroma_format==CHROMA444)
1120 /* coded_block_pattern_2 */
1121 coded_block_pattern = (coded_block_pattern<<6) | Get_Bits(6);
1126 printf("coded_block_pattern_2: ");
1127 Print_Bits(coded_block_pattern,6,6);
1128 printf(" (%d)\n",coded_block_pattern&63);
1134 coded_block_pattern = (*macroblock_type & MACROBLOCK_INTRA) ?
1135 (1<<block_count)-1 : 0;
1137 if (Fault_Flag) return(0); /* trigger: go to next slice */
1140 for (comp=0; comp<block_count; comp++)
1142 /* SCALABILITY: Data Partitioning */
1143 if (base.scalable_mode==SC_DP)
1148 if (coded_block_pattern & (1<<(block_count-1-comp)))
1150 if (*macroblock_type & MACROBLOCK_INTRA)
1153 Decode_MPEG2_Intra_Block(comp,dc_dct_pred);
1155 Decode_MPEG1_Intra_Block(comp,dc_dct_pred);
1160 Decode_MPEG2_Non_Intra_Block(comp);
1162 Decode_MPEG1_Non_Intra_Block(comp);
1165 if (Fault_Flag) return(0); /* trigger: go to next slice */
1169 if(picture_coding_type==D_TYPE)
1171 /* remove end_of_macroblock (always 1, prevents startcode emulation) */
1172 /* ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
1173 marker_bit("D picture end_of_macroblock bit");
1176 /* reset intra_dc predictors */
1177 /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
1178 if (!(*macroblock_type & MACROBLOCK_INTRA))
1179 dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
1181 /* reset motion vector predictors */
1182 if ((*macroblock_type & MACROBLOCK_INTRA) && !concealment_motion_vectors)
1184 /* intra mb without concealment motion vectors */
1185 /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
1186 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
1187 PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
1190 /* special "No_MC" macroblock_type case */
1191 /* ISO/IEC 13818-2 section 7.6.3.5: Prediction in P pictures */
1192 if ((picture_coding_type==P_TYPE)
1193 && !(*macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_INTRA)))
1195 /* non-intra mb without forward mv in a P picture */
1196 /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
1197 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
1199 /* derive motion_type */
1200 /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes, frame_motion_type */
1201 if (picture_structure==FRAME_PICTURE)
1202 *motion_type = MC_FRAME;
1205 *motion_type = MC_FIELD;
1206 /* predict from field of same parity */
1207 motion_vertical_field_select[0][0] = (picture_structure==BOTTOM_FIELD);
1213 /* purely spatially predicted macroblock */
1214 /* ISO/IEC 13818-2 section 7.7.5.1: Resetting motion vector predictions */
1215 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
1216 PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
1219 /* successfully decoded macroblock */
1222 } /* decode_macroblock */