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
35 /* private prototypes*/
36 static void picture_data _ANSI_ARGS_((int framenum));
37 static void macroblock_modes _ANSI_ARGS_((int *pmacroblock_type, int *pstwtype,
38 int *pstwclass, int *pmotion_type, int *pmotion_vector_count, int *pmv_format, int *pdmv,
39 int *pmvscale, int *pdct_type));
40 static void Clear_Block _ANSI_ARGS_((int comp));
41 static void Sum_Block _ANSI_ARGS_((int comp));
42 static void Saturate _ANSI_ARGS_((short *bp));
43 static void Add_Block _ANSI_ARGS_((int comp, int bx, int by,
44 int dct_type, int addflag));
45 static void Update_Picture_Buffers _ANSI_ARGS_((void));
46 static void frame_reorder _ANSI_ARGS_((int bitstream_framenum,
47 int sequence_framenum));
48 static void Decode_SNR_Macroblock _ANSI_ARGS_((int *SNRMBA, int *SNRMBAinc,
49 int MBA, int MBAmax, int *dct_type));
51 static void motion_compensation _ANSI_ARGS_((int MBA, int macroblock_type,
52 int motion_type, int PMV[2][2][2], int motion_vertical_field_select[2][2],
53 int dmvector[2], int stwtype, int dct_type));
55 static void skipped_macroblock _ANSI_ARGS_((int dc_dct_pred[3],
56 int PMV[2][2][2], int *motion_type, int motion_vertical_field_select[2][2],
57 int *stwtype, int *macroblock_type));
59 static int slice _ANSI_ARGS_((int framenum, int MBAmax));
61 static int start_of_slice _ANSI_ARGS_ ((int MBAmax, int *MBA,
62 int *MBAinc, int dc_dct_pred[3], int PMV[2][2][2]));
64 static int decode_macroblock _ANSI_ARGS_((int *macroblock_type,
65 int *stwtype, int *stwclass, int *motion_type, int *dct_type,
66 int PMV[2][2][2], int dc_dct_pred[3],
67 int motion_vertical_field_select[2][2], int dmvector[2]));
70 /* decode one frame or field picture */
71 void Decode_Picture(bitstream_framenum, sequence_framenum)
72 int bitstream_framenum, sequence_framenum;
75 if (picture_structure==FRAME_PICTURE && Second_Field)
77 /* recover from illegal number of field pictures */
78 printf("odd number of field pictures\n");
82 /* IMPLEMENTATION: update picture buffer pointers */
83 Update_Picture_Buffers();
86 Check_Headers(bitstream_framenum, sequence_framenum);
89 /* ISO/IEC 13818-4 section 2.4.5.4 "frame buffer intercept method" */
90 /* (section number based on November 1995 (Dallas) draft of the
91 conformance document) */
93 Substitute_Frame_Buffer(bitstream_framenum, sequence_framenum);
95 /* form spatial scalable picture */
97 /* form spatial scalable picture */
98 /* ISO/IEC 13818-2 section 7.7: Spatial scalability */
99 if (base.pict_scal && !Second_Field)
101 Spatial_Prediction();
104 /* decode picture data ISO/IEC 13818-2 section 6.2.3.7 */
105 picture_data(bitstream_framenum);
107 /* write or display current or previously decoded reference frame */
108 /* ISO/IEC 13818-2 section 6.1.1.11: Frame reordering */
109 frame_reorder(bitstream_framenum, sequence_framenum);
111 if (picture_structure!=FRAME_PICTURE)
112 Second_Field = !Second_Field;
116 /* decode all macroblocks of the current picture */
117 /* stages described in ISO/IEC 13818-2 section 7 */
118 static void picture_data(framenum)
124 /* number of macroblocks per picture */
125 MBAmax = mb_width*mb_height;
127 if (picture_structure!=FRAME_PICTURE)
128 MBAmax>>=1; /* field picture has half as mnay macroblocks as frame */
132 if((ret=slice(framenum, MBAmax))<0)
140 /* decode all macroblocks of the current picture */
141 /* ISO/IEC 13818-2 section 6.3.16 */
142 static int slice(framenum, MBAmax)
143 int framenum, MBAmax;
146 int MBAinc, macroblock_type, motion_type, dct_type;
148 int PMV[2][2][2], motion_vertical_field_select[2][2];
150 int stwtype, stwclass;
151 int SNRMBA, SNRMBAinc;
155 MBA = 0; /* macroblock address */
158 if((ret=start_of_slice(MBAmax, &MBA, &MBAinc, dc_dct_pred, PMV))!=1)
161 if (Two_Streams && enhan.scalable_mode==SC_SNR)
172 /* this is how we properly exit out of picture */
174 return(-1); /* all macroblocks decoded */
178 printf("frame %d, MB %d\n",framenum,MBA);
182 if (!progressive_frame && picture_structure==FRAME_PICTURE
183 && MBA==(MBAmax>>1) && framenum!=0 && Output_Type==T_X11
184 && !Display_Progressive_Flag)
186 Display_Second_Field();
194 if (base.scalable_mode==SC_DP && base.priority_breakpoint==1)
197 if (!Show_Bits(23) || Fault_Flag) /* next_start_code or fault */
199 resync: /* if Fault_Flag: resynchronize to next next_start_code */
201 return(0); /* trigger: go to next slice */
203 else /* neither next_start_code nor Fault_Flag */
205 if (base.scalable_mode==SC_DP && base.priority_breakpoint==1)
208 /* decode macroblock address increment */
209 MBAinc = Get_macroblock_address_increment();
211 if (Fault_Flag) goto resync;
217 /* MBAinc points beyond picture dimensions */
219 printf("Too many macroblocks in picture\n");
223 if (MBAinc==1) /* not skipped */
225 ret = decode_macroblock(¯oblock_type, &stwtype, &stwclass,
226 &motion_type, &dct_type, PMV, dc_dct_pred,
227 motion_vertical_field_select, dmvector);
236 else /* MBAinc!=1: skipped macroblock */
238 /* ISO/IEC 13818-2 section 7.6.6 */
239 skipped_macroblock(dc_dct_pred, PMV, &motion_type,
240 motion_vertical_field_select, &stwtype, ¯oblock_type);
243 /* SCALABILITY: SNR */
244 /* ISO/IEC 13818-2 section 7.8 */
245 /* NOTE: we currently ignore faults encountered in this routine */
246 if (Two_Streams && enhan.scalable_mode==SC_SNR)
247 Decode_SNR_Macroblock(&SNRMBA, &SNRMBAinc, MBA, MBAmax, &dct_type);
249 /* ISO/IEC 13818-2 section 7.6 */
250 motion_compensation(MBA, macroblock_type, motion_type, PMV,
251 motion_vertical_field_select, dmvector, stwtype, dct_type);
254 /* advance to next macroblock */
258 /* SCALABILITY: SNR */
259 if (Two_Streams && enhan.scalable_mode==SC_SNR)
266 return(-1); /* all macroblocks decoded */
271 /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
272 static void macroblock_modes(pmacroblock_type,pstwtype,pstwclass,
273 pmotion_type,pmotion_vector_count,pmv_format,pdmv,pmvscale,pdct_type)
274 int *pmacroblock_type, *pstwtype, *pstwclass;
275 int *pmotion_type, *pmotion_vector_count, *pmv_format, *pdmv, *pmvscale;
279 int stwtype, stwcode, stwclass;
281 int motion_vector_count, mv_format, dmv, mvscale;
283 static unsigned char stwc_table[3][4]
284 = { {6,3,7,4}, {2,1,5,4}, {2,5,7,4} };
285 static unsigned char stwclass_table[9]
286 = {0, 1, 2, 1, 1, 2, 3, 3, 4};
288 /* get macroblock_type */
289 macroblock_type = Get_macroblock_type();
291 if (Fault_Flag) return;
293 /* get spatial_temporal_weight_code */
294 if (macroblock_type & MB_WEIGHT)
296 if (spatial_temporal_weight_code_table_index==0)
300 stwcode = Get_Bits(2);
304 printf("spatial_temporal_weight_code (");
305 Print_Bits(stwcode,2,2);
306 printf("): %d\n",stwcode);
309 stwtype = stwc_table[spatial_temporal_weight_code_table_index-1][stwcode];
313 stwtype = (macroblock_type & MB_CLASS4) ? 8 : 0;
315 /* SCALABILITY: derive spatial_temporal_weight_class (Table 7-18) */
316 stwclass = stwclass_table[stwtype];
318 /* get frame/field motion type */
319 if (macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD))
321 if (picture_structure==FRAME_PICTURE) /* frame_motion_type */
323 motion_type = frame_pred_frame_dct ? MC_FRAME : Get_Bits(2);
325 if (!frame_pred_frame_dct && Trace_Flag)
327 printf("frame_motion_type (");
328 Print_Bits(motion_type,2,2);
329 printf("): %s\n",motion_type==MC_FIELD?"Field":
330 motion_type==MC_FRAME?"Frame":
331 motion_type==MC_DMV?"Dual_Prime":"Invalid");
335 else /* field_motion_type */
337 motion_type = Get_Bits(2);
341 printf("field_motion_type (");
342 Print_Bits(motion_type,2,2);
343 printf("): %s\n",motion_type==MC_FIELD?"Field":
344 motion_type==MC_16X8?"16x8 MC":
345 motion_type==MC_DMV?"Dual_Prime":"Invalid");
350 else if ((macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
352 /* concealment motion vectors */
353 motion_type = (picture_structure==FRAME_PICTURE) ? MC_FRAME : MC_FIELD;
358 printf("maroblock_modes(): unknown macroblock type\n");
363 /* derive motion_vector_count, mv_format and dmv, (table 6-17, 6-18) */
364 if (picture_structure==FRAME_PICTURE)
366 motion_vector_count = (motion_type==MC_FIELD && stwclass<2) ? 2 : 1;
367 mv_format = (motion_type==MC_FRAME) ? MV_FRAME : MV_FIELD;
371 motion_vector_count = (motion_type==MC_16X8) ? 2 : 1;
372 mv_format = MV_FIELD;
375 dmv = (motion_type==MC_DMV); /* dual prime */
377 /* field mv predictions in frame pictures have to be scaled
378 * ISO/IEC 13818-2 section 7.6.3.1 Decoding the motion vectors
379 * IMPLEMENTATION: mvscale is derived for later use in motion_vectors()
380 * it displaces the stage:
382 * if((mv_format=="field")&&(t==1)&&(picture_structure=="Frame picture"))
383 * prediction = PMV[r][s][t] DIV 2;
386 mvscale = ((mv_format==MV_FIELD) && (picture_structure==FRAME_PICTURE));
388 /* get dct_type (frame DCT / field DCT) */
389 dct_type = (picture_structure==FRAME_PICTURE)
390 && (!frame_pred_frame_dct)
391 && (macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA))
396 if (Trace_Flag && (picture_structure==FRAME_PICTURE)
397 && (!frame_pred_frame_dct)
398 && (macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA)))
399 printf("dct_type (%d): %s\n",dct_type,dct_type?"Field":"Frame");
403 *pmacroblock_type = macroblock_type;
405 *pstwclass = stwclass;
406 *pmotion_type = motion_type;
407 *pmotion_vector_count = motion_vector_count;
408 *pmv_format = mv_format;
411 *pdct_type = dct_type;
415 /* move/add 8x8-Block from block[comp] to backward_reference_frame */
416 /* copy reconstructed 8x8 block from block[comp] to current_frame[]
417 * ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data
418 * This stage also embodies some of the operations implied by:
419 * - ISO/IEC 13818-2 section 7.6.7: Combining predictions
420 * - ISO/IEC 13818-2 section 6.1.3: Macroblock
422 static void Add_Block(comp,bx,by,dct_type,addflag)
423 int comp,bx,by,dct_type,addflag;
430 /* derive color component index */
431 /* equivalent to ISO/IEC 13818-2 Table 7-1 */
432 cc = (comp<4) ? 0 : (comp&1)+1; /* color component index */
438 if (picture_structure==FRAME_PICTURE)
441 /* field DCT coding */
442 rfp = current_frame[0]
443 + Coded_Picture_Width*(by+((comp&2)>>1)) + bx + ((comp&1)<<3);
444 iincr = (Coded_Picture_Width<<1) - 8;
448 /* frame DCT coding */
449 rfp = current_frame[0]
450 + Coded_Picture_Width*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
451 iincr = Coded_Picture_Width - 8;
456 rfp = current_frame[0]
457 + (Coded_Picture_Width<<1)*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
458 iincr = (Coded_Picture_Width<<1) - 8;
465 /* scale coordinates */
466 if (chroma_format!=CHROMA444)
468 if (chroma_format==CHROMA420)
470 if (picture_structure==FRAME_PICTURE)
472 if (dct_type && (chroma_format!=CHROMA420))
474 /* field DCT coding */
475 rfp = current_frame[cc]
476 + Chroma_Width*(by+((comp&2)>>1)) + bx + (comp&8);
477 iincr = (Chroma_Width<<1) - 8;
481 /* frame DCT coding */
482 rfp = current_frame[cc]
483 + Chroma_Width*(by+((comp&2)<<2)) + bx + (comp&8);
484 iincr = Chroma_Width - 8;
490 rfp = current_frame[cc]
491 + (Chroma_Width<<1)*(by+((comp&2)<<2)) + bx + (comp&8);
492 iincr = (Chroma_Width<<1) - 8;
496 bp = ld->block[comp];
504 *rfp = Clip[*bp++ + *rfp];
516 *rfp++ = Clip[*bp++ + 128];
524 /* ISO/IEC 13818-2 section 7.8 */
525 static void Decode_SNR_Macroblock(SNRMBA, SNRMBAinc, MBA, MBAmax, dct_type)
526 int *SNRMBA, *SNRMBAinc;
530 int SNRmacroblock_type, SNRcoded_block_pattern, SNRdct_type, dummy;
531 int slice_vert_pos_ext, quantizer_scale_code, comp, code;
537 if (!Show_Bits(23)) /* next_start_code */
540 code = Show_Bits(32);
542 if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
544 /* only slice headers are allowed in picture_data */
546 printf("SNR: Premature end of picture\n");
552 /* decode slice header (may change quantizer_scale) */
553 slice_vert_pos_ext = slice_header();
555 /* decode macroblock address increment */
556 *SNRMBAinc = Get_macroblock_address_increment();
558 /* set current location */
560 ((slice_vert_pos_ext<<7) + (code&255) - 1)*mb_width + *SNRMBAinc - 1;
562 *SNRMBAinc = 1; /* first macroblock in slice: not skipped */
564 else /* not next_start_code */
569 printf("Too many macroblocks in picture\n");
573 /* decode macroblock address increment */
574 *SNRMBAinc = Get_macroblock_address_increment();
580 /* streams out of sync */
582 printf("Cant't synchronize streams\n");
586 if (*SNRMBAinc==1) /* not skipped */
588 macroblock_modes(&SNRmacroblock_type, &dummy, &dummy,
589 &dummy, &dummy, &dummy, &dummy, &dummy,
592 if (SNRmacroblock_type & MACROBLOCK_PATTERN)
593 *dct_type = SNRdct_type;
595 if (SNRmacroblock_type & MACROBLOCK_QUANT)
597 quantizer_scale_code = Get_Bits(5);
598 ld->quantizer_scale =
599 ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code] : quantizer_scale_code<<1;
602 /* macroblock_pattern */
603 if (SNRmacroblock_type & MACROBLOCK_PATTERN)
605 SNRcoded_block_pattern = Get_coded_block_pattern();
607 if (chroma_format==CHROMA422)
608 SNRcoded_block_pattern = (SNRcoded_block_pattern<<2) | Get_Bits(2); /* coded_block_pattern_1 */
609 else if (chroma_format==CHROMA444)
610 SNRcoded_block_pattern = (SNRcoded_block_pattern<<6) | Get_Bits(6); /* coded_block_pattern_2 */
613 SNRcoded_block_pattern = 0;
616 for (comp=0; comp<block_count; comp++)
620 if (SNRcoded_block_pattern & (1<<(block_count-1-comp)))
621 Decode_MPEG2_Non_Intra_Block(comp);
624 else /* SNRMBAinc!=1: skipped macroblock */
626 for (comp=0; comp<block_count; comp++)
635 /* IMPLEMENTATION: set scratch pad macroblock to zero */
636 static void Clear_Block(comp)
642 Block_Ptr = ld->block[comp];
649 /* SCALABILITY: add SNR enhancement layer block data to base layer */
650 /* ISO/IEC 13818-2 section 7.8.3.4: Addition of coefficients from the two layes */
651 static void Sum_Block(comp)
654 short *Block_Ptr1, *Block_Ptr2;
657 Block_Ptr1 = base.block[comp];
658 Block_Ptr2 = enhan.block[comp];
661 *Block_Ptr1++ += *Block_Ptr2++;
665 /* limit coefficients to -2048..2047 */
666 /* ISO/IEC 13818-2 section 7.4.3 and 7.4.4: Saturation and Mismatch control */
667 static void Saturate(Block_Ptr)
674 /* ISO/IEC 13818-2 section 7.4.3: Saturation */
688 /* ISO/IEC 13818-2 section 7.4.4: Mismatch control */
695 /* reuse old picture buffers as soon as they are no longer needed
696 based on life-time axioms of MPEG */
697 static void Update_Picture_Buffers()
699 int cc; /* color component index */
700 unsigned char *tmp; /* temporary swap pointer */
702 for (cc=0; cc<3; cc++)
704 /* B pictures do not need to be save for future reference */
705 if (picture_coding_type==B_TYPE)
707 current_frame[cc] = auxframe[cc];
711 /* only update at the beginning of the coded frame */
714 tmp = forward_reference_frame[cc];
716 /* the previously decoded reference frame is stored
717 coincident with the location where the backward
718 reference frame is stored (backwards prediction is not
719 needed in P pictures) */
720 forward_reference_frame[cc] = backward_reference_frame[cc];
722 /* update pointer for potential future B pictures */
723 backward_reference_frame[cc] = tmp;
726 /* can erase over old backward reference frame since it is not used
727 in a P picture, and since any subsequent B pictures will use the
728 previously decoded I or P frame as the backward_reference_frame */
729 current_frame[cc] = backward_reference_frame[cc];
733 one-time folding of a line offset into the pointer which stores the
734 memory address of the current frame saves offsets and conditional
735 branches throughout the remainder of the picture processing loop */
736 if (picture_structure==BOTTOM_FIELD)
737 current_frame[cc]+= (cc==0) ? Coded_Picture_Width : Chroma_Width;
742 /* store last frame */
744 void Output_Last_Frame_of_Sequence(Framenum)
748 printf("last frame incomplete, not stored\n");
750 Write_Frame(backward_reference_frame,Framenum-1);
755 static void frame_reorder(Bitstream_Framenum, Sequence_Framenum)
756 int Bitstream_Framenum, Sequence_Framenum;
758 /* tracking variables to insure proper output in spatial scalability */
759 static int Oldref_progressive_frame, Newref_progressive_frame;
761 if (Sequence_Framenum!=0)
763 if (picture_structure==FRAME_PICTURE || Second_Field)
765 if (picture_coding_type==B_TYPE)
766 Write_Frame(auxframe,Bitstream_Framenum-1);
769 Newref_progressive_frame = progressive_frame;
770 progressive_frame = Oldref_progressive_frame;
772 Write_Frame(forward_reference_frame,Bitstream_Framenum-1);
774 Oldref_progressive_frame = progressive_frame = Newref_progressive_frame;
778 else if (Output_Type==T_X11)
780 if(!Display_Progressive_Flag)
781 Display_Second_Field();
786 Oldref_progressive_frame = progressive_frame;
791 /* ISO/IEC 13818-2 section 7.6 */
792 static void motion_compensation(MBA, macroblock_type, motion_type, PMV,
793 motion_vertical_field_select, dmvector, stwtype, dct_type)
798 int motion_vertical_field_select[2][2];
806 /* derive current macroblock position within picture */
807 /* ISO/IEC 13818-2 section 6.3.1.6 and 6.3.1.7 */
808 bx = 16*(MBA%mb_width);
809 by = 16*(MBA/mb_width);
811 /* motion compensation */
812 if (!(macroblock_type & MACROBLOCK_INTRA))
813 form_predictions(bx,by,macroblock_type,motion_type,PMV,
814 motion_vertical_field_select,dmvector,stwtype);
816 /* SCALABILITY: Data Partitioning */
817 if (base.scalable_mode==SC_DP)
820 /* copy or add block data into picture */
821 for (comp=0; comp<block_count; comp++)
823 /* SCALABILITY: SNR */
824 /* ISO/IEC 13818-2 section 7.8.3.4: Addition of coefficients from
826 if (Two_Streams && enhan.scalable_mode==SC_SNR)
827 Sum_Block(comp); /* add SNR enhancement layer data to base layer */
829 /* MPEG-2 saturation and mismatch control */
830 /* base layer could be MPEG-1 stream, enhancement MPEG-2 SNR */
831 /* ISO/IEC 13818-2 section 7.4.3 and 7.4.4: Saturation and Mismatch control */
832 if ((Two_Streams && enhan.scalable_mode==SC_SNR) || ld->MPEG2_Flag)
833 Saturate(ld->block[comp]);
835 /* ISO/IEC 13818-2 section Annex A: inverse DCT */
836 if (Reference_IDCT_Flag)
837 Reference_IDCT(ld->block[comp]);
839 Fast_IDCT(ld->block[comp]);
841 /* ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data */
842 Add_Block(comp,bx,by,dct_type,(macroblock_type & MACROBLOCK_INTRA)==0);
849 /* ISO/IEC 13818-2 section 7.6.6 */
850 static void skipped_macroblock(dc_dct_pred, PMV, motion_type,
851 motion_vertical_field_select, stwtype, macroblock_type)
855 int motion_vertical_field_select[2][2];
857 int *macroblock_type;
861 /* SCALABILITY: Data Paritioning */
862 if (base.scalable_mode==SC_DP)
865 for (comp=0; comp<block_count; comp++)
868 /* reset intra_dc predictors */
869 /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
870 dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
872 /* reset motion vector predictors */
873 /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
874 if (picture_coding_type==P_TYPE)
875 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
877 /* derive motion_type */
878 if (picture_structure==FRAME_PICTURE)
879 *motion_type = MC_FRAME;
882 *motion_type = MC_FIELD;
884 /* predict from field of same parity */
885 /* ISO/IEC 13818-2 section 7.6.6.1 and 7.6.6.3: P field picture and B field
887 motion_vertical_field_select[0][0]=motion_vertical_field_select[0][1] =
888 (picture_structure==BOTTOM_FIELD);
891 /* skipped I are spatial-only predicted, */
892 /* skipped P and B are temporal-only predicted */
893 /* ISO/IEC 13818-2 section 7.7.6: Skipped macroblocks */
894 *stwtype = (picture_coding_type==I_TYPE) ? 8 : 0;
896 /* IMPLEMENTATION: clear MACROBLOCK_INTRA */
897 *macroblock_type&= ~MACROBLOCK_INTRA;
902 /* return==-1 means go to next picture */
903 /* the expression "start of slice" is used throughout the normative
904 body of the MPEG specification */
905 static int start_of_slice(MBAmax, MBA, MBAinc,
914 int slice_vert_pos_ext;
922 code = Show_Bits(32);
924 if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
926 /* only slice headers are allowed in picture_data */
928 printf("start_of_slice(): Premature end of picture\n");
930 return(-1); /* trigger: go to next picture */
935 /* decode slice header (may change quantizer_scale) */
936 slice_vert_pos_ext = slice_header();
939 /* SCALABILITY: Data Partitioning */
940 if (base.scalable_mode==SC_DP)
944 code = Show_Bits(32);
946 if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
948 /* only slice headers are allowed in picture_data */
950 printf("DP: Premature end of picture\n");
951 return(-1); /* trigger: go to next picture */
956 /* decode slice header (may change quantizer_scale) */
957 slice_vert_pos_ext = slice_header();
959 if (base.priority_breakpoint!=1)
963 /* decode macroblock address increment */
964 *MBAinc = Get_macroblock_address_increment();
968 printf("start_of_slice(): MBAinc unsuccessful\n");
969 return(0); /* trigger: go to next slice */
972 /* set current location */
973 /* NOTE: the arithmetic used to derive macroblock_address below is
974 * equivalent to ISO/IEC 13818-2 section 6.3.17: Macroblock
976 *MBA = ((slice_vert_pos_ext<<7) + (code&255) - 1)*mb_width + *MBAinc - 1;
977 *MBAinc = 1; /* first macroblock in slice: not skipped */
979 /* reset all DC coefficient and motion vector predictors */
980 /* reset all DC coefficient and motion vector predictors */
981 /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
982 dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
984 /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
985 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
986 PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
988 /* successfull: trigger decode macroblocks in slice */
993 /* ISO/IEC 13818-2 sections 7.2 through 7.5 */
994 static int decode_macroblock(macroblock_type, stwtype, stwclass,
995 motion_type, dct_type, PMV, dc_dct_pred,
996 motion_vertical_field_select, dmvector)
997 int *macroblock_type;
1004 int motion_vertical_field_select[2][2];
1008 int quantizer_scale_code;
1011 int motion_vector_count;
1015 int coded_block_pattern;
1017 /* SCALABILITY: Data Patitioning */
1018 if (base.scalable_mode==SC_DP)
1020 if (base.priority_breakpoint<=2)
1026 /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
1027 macroblock_modes(macroblock_type, stwtype, stwclass,
1028 motion_type, &motion_vector_count, &mv_format, &dmv, &mvscale,
1031 if (Fault_Flag) return(0); /* trigger: go to next slice */
1033 if (*macroblock_type & MACROBLOCK_QUANT)
1035 quantizer_scale_code = Get_Bits(5);
1040 printf("quantiser_scale_code (");
1041 Print_Bits(quantizer_scale_code,5,5);
1042 printf("): %d\n",quantizer_scale_code);
1046 /* ISO/IEC 13818-2 section 7.4.2.2: Quantizer scale factor */
1048 ld->quantizer_scale =
1049 ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code]
1050 : (quantizer_scale_code << 1);
1052 ld->quantizer_scale = quantizer_scale_code;
1054 /* SCALABILITY: Data Partitioning */
1055 if (base.scalable_mode==SC_DP)
1056 /* make sure base.quantizer_scale is valid */
1057 base.quantizer_scale = ld->quantizer_scale;
1060 /* motion vectors */
1063 /* ISO/IEC 13818-2 section 6.3.17.2: Motion vectors */
1065 /* decode forward motion vectors */
1066 if ((*macroblock_type & MACROBLOCK_MOTION_FORWARD)
1067 || ((*macroblock_type & MACROBLOCK_INTRA)
1068 && concealment_motion_vectors))
1071 motion_vectors(PMV,dmvector,motion_vertical_field_select,
1072 0,motion_vector_count,mv_format,f_code[0][0]-1,f_code[0][1]-1,
1075 motion_vector(PMV[0][0],dmvector,
1076 forward_f_code-1,forward_f_code-1,0,0,full_pel_forward_vector);
1079 if (Fault_Flag) return(0); /* trigger: go to next slice */
1081 /* decode backward motion vectors */
1082 if (*macroblock_type & MACROBLOCK_MOTION_BACKWARD)
1085 motion_vectors(PMV,dmvector,motion_vertical_field_select,
1086 1,motion_vector_count,mv_format,f_code[1][0]-1,f_code[1][1]-1,0,
1089 motion_vector(PMV[0][1],dmvector,
1090 backward_f_code-1,backward_f_code-1,0,0,full_pel_backward_vector);
1093 if (Fault_Flag) return(0); /* trigger: go to next slice */
1095 if ((*macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
1096 Flush_Buffer(1); /* remove marker_bit */
1098 if (base.scalable_mode==SC_DP && base.priority_breakpoint==3)
1101 /* macroblock_pattern */
1102 /* ISO/IEC 13818-2 section 6.3.17.4: Coded block pattern */
1103 if (*macroblock_type & MACROBLOCK_PATTERN)
1105 coded_block_pattern = Get_coded_block_pattern();
1107 if (chroma_format==CHROMA422)
1109 /* coded_block_pattern_1 */
1110 coded_block_pattern = (coded_block_pattern<<2) | Get_Bits(2);
1115 printf("coded_block_pattern_1: ");
1116 Print_Bits(coded_block_pattern,2,2);
1117 printf(" (%d)\n",coded_block_pattern&3);
1121 else if (chroma_format==CHROMA444)
1123 /* coded_block_pattern_2 */
1124 coded_block_pattern = (coded_block_pattern<<6) | Get_Bits(6);
1129 printf("coded_block_pattern_2: ");
1130 Print_Bits(coded_block_pattern,6,6);
1131 printf(" (%d)\n",coded_block_pattern&63);
1137 coded_block_pattern = (*macroblock_type & MACROBLOCK_INTRA) ?
1138 (1<<block_count)-1 : 0;
1140 if (Fault_Flag) return(0); /* trigger: go to next slice */
1143 for (comp=0; comp<block_count; comp++)
1145 /* SCALABILITY: Data Partitioning */
1146 if (base.scalable_mode==SC_DP)
1151 if (coded_block_pattern & (1<<(block_count-1-comp)))
1153 if (*macroblock_type & MACROBLOCK_INTRA)
1156 Decode_MPEG2_Intra_Block(comp,dc_dct_pred);
1158 Decode_MPEG1_Intra_Block(comp,dc_dct_pred);
1163 Decode_MPEG2_Non_Intra_Block(comp);
1165 Decode_MPEG1_Non_Intra_Block(comp);
1168 if (Fault_Flag) return(0); /* trigger: go to next slice */
1172 if(picture_coding_type==D_TYPE)
1174 /* remove end_of_macroblock (always 1, prevents startcode emulation) */
1175 /* ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
1176 marker_bit("D picture end_of_macroblock bit");
1179 /* reset intra_dc predictors */
1180 /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
1181 if (!(*macroblock_type & MACROBLOCK_INTRA))
1182 dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
1184 /* reset motion vector predictors */
1185 if ((*macroblock_type & MACROBLOCK_INTRA) && !concealment_motion_vectors)
1187 /* intra mb without concealment motion vectors */
1188 /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
1189 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
1190 PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
1193 /* special "No_MC" macroblock_type case */
1194 /* ISO/IEC 13818-2 section 7.6.3.5: Prediction in P pictures */
1195 if ((picture_coding_type==P_TYPE)
1196 && !(*macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_INTRA)))
1198 /* non-intra mb without forward mv in a P picture */
1199 /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
1200 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
1202 /* derive motion_type */
1203 /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes, frame_motion_type */
1204 if (picture_structure==FRAME_PICTURE)
1205 *motion_type = MC_FRAME;
1208 *motion_type = MC_FIELD;
1209 /* predict from field of same parity */
1210 motion_vertical_field_select[0][0] = (picture_structure==BOTTOM_FIELD);
1216 /* purely spatially predicted macroblock */
1217 /* ISO/IEC 13818-2 section 7.7.5.1: Resetting motion vector predictions */
1218 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
1219 PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
1222 /* successfully decoded macroblock */
1225 } /* decode_macroblock */