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;
154 MBA = 0; /* macroblock address */
157 if((ret=start_of_slice(MBAmax, &MBA, &MBAinc, dc_dct_pred, PMV))!=1)
160 if (Two_Streams && enhan.scalable_mode==SC_SNR)
171 /* this is how we properly exit out of picture */
173 return(-1); /* all macroblocks decoded */
177 printf("frame %d, MB %d\n",framenum,MBA);
181 if (!progressive_frame && picture_structure==FRAME_PICTURE
182 && MBA==(MBAmax>>1) && framenum!=0 && Output_Type==T_X11
183 && !Display_Progressive_Flag)
185 Display_Second_Field();
193 if (base.scalable_mode==SC_DP && base.priority_breakpoint==1)
196 if (!Show_Bits(23) || Fault_Flag) /* next_start_code or fault */
198 resync: /* if Fault_Flag: resynchronize to next next_start_code */
200 return(0); /* trigger: go to next slice */
202 else /* neither next_start_code nor Fault_Flag */
204 if (base.scalable_mode==SC_DP && base.priority_breakpoint==1)
207 /* decode macroblock address increment */
208 MBAinc = Get_macroblock_address_increment();
210 if (Fault_Flag) goto resync;
216 /* MBAinc points beyond picture dimensions */
218 printf("Too many macroblocks in picture\n");
222 if (MBAinc==1) /* not skipped */
224 ret = decode_macroblock(¯oblock_type, &stwtype, &stwclass,
225 &motion_type, &dct_type, PMV, dc_dct_pred,
226 motion_vertical_field_select, dmvector);
235 else /* MBAinc!=1: skipped macroblock */
237 /* ISO/IEC 13818-2 section 7.6.6 */
238 skipped_macroblock(dc_dct_pred, PMV, &motion_type,
239 motion_vertical_field_select, &stwtype, ¯oblock_type);
242 /* SCALABILITY: SNR */
243 /* ISO/IEC 13818-2 section 7.8 */
244 /* NOTE: we currently ignore faults encountered in this routine */
245 if (Two_Streams && enhan.scalable_mode==SC_SNR)
246 Decode_SNR_Macroblock(&SNRMBA, &SNRMBAinc, MBA, MBAmax, &dct_type);
248 /* ISO/IEC 13818-2 section 7.6 */
249 motion_compensation(MBA, macroblock_type, motion_type, PMV,
250 motion_vertical_field_select, dmvector, stwtype, dct_type);
253 /* advance to next macroblock */
257 /* SCALABILITY: SNR */
258 if (Two_Streams && enhan.scalable_mode==SC_SNR)
265 return(-1); /* all macroblocks decoded */
270 /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
271 static void macroblock_modes(pmacroblock_type,pstwtype,pstwclass,
272 pmotion_type,pmotion_vector_count,pmv_format,pdmv,pmvscale,pdct_type)
273 int *pmacroblock_type, *pstwtype, *pstwclass;
274 int *pmotion_type, *pmotion_vector_count, *pmv_format, *pdmv, *pmvscale;
278 int stwtype, stwcode, stwclass;
280 int motion_vector_count, mv_format, dmv, mvscale;
282 static unsigned char stwc_table[3][4]
283 = { {6,3,7,4}, {2,1,5,4}, {2,5,7,4} };
284 static unsigned char stwclass_table[9]
285 = {0, 1, 2, 1, 1, 2, 3, 3, 4};
287 /* get macroblock_type */
288 macroblock_type = Get_macroblock_type();
290 if (Fault_Flag) return;
292 /* get spatial_temporal_weight_code */
293 if (macroblock_type & MB_WEIGHT)
295 if (spatial_temporal_weight_code_table_index==0)
299 stwcode = Get_Bits(2);
303 printf("spatial_temporal_weight_code (");
304 Print_Bits(stwcode,2,2);
305 printf("): %d\n",stwcode);
308 stwtype = stwc_table[spatial_temporal_weight_code_table_index-1][stwcode];
312 stwtype = (macroblock_type & MB_CLASS4) ? 8 : 0;
314 /* SCALABILITY: derive spatial_temporal_weight_class (Table 7-18) */
315 stwclass = stwclass_table[stwtype];
317 /* get frame/field motion type */
318 if (macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD))
320 if (picture_structure==FRAME_PICTURE) /* frame_motion_type */
322 motion_type = frame_pred_frame_dct ? MC_FRAME : Get_Bits(2);
324 if (!frame_pred_frame_dct && Trace_Flag)
326 printf("frame_motion_type (");
327 Print_Bits(motion_type,2,2);
328 printf("): %s\n",motion_type==MC_FIELD?"Field":
329 motion_type==MC_FRAME?"Frame":
330 motion_type==MC_DMV?"Dual_Prime":"Invalid");
334 else /* field_motion_type */
336 motion_type = Get_Bits(2);
340 printf("field_motion_type (");
341 Print_Bits(motion_type,2,2);
342 printf("): %s\n",motion_type==MC_FIELD?"Field":
343 motion_type==MC_16X8?"16x8 MC":
344 motion_type==MC_DMV?"Dual_Prime":"Invalid");
349 else if ((macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
351 /* concealment motion vectors */
352 motion_type = (picture_structure==FRAME_PICTURE) ? MC_FRAME : MC_FIELD;
357 printf("maroblock_modes(): unknown macroblock type\n");
362 /* derive motion_vector_count, mv_format and dmv, (table 6-17, 6-18) */
363 if (picture_structure==FRAME_PICTURE)
365 motion_vector_count = (motion_type==MC_FIELD && stwclass<2) ? 2 : 1;
366 mv_format = (motion_type==MC_FRAME) ? MV_FRAME : MV_FIELD;
370 motion_vector_count = (motion_type==MC_16X8) ? 2 : 1;
371 mv_format = MV_FIELD;
374 dmv = (motion_type==MC_DMV); /* dual prime */
376 /* field mv predictions in frame pictures have to be scaled
377 * ISO/IEC 13818-2 section 7.6.3.1 Decoding the motion vectors
378 * IMPLEMENTATION: mvscale is derived for later use in motion_vectors()
379 * it displaces the stage:
381 * if((mv_format=="field")&&(t==1)&&(picture_structure=="Frame picture"))
382 * prediction = PMV[r][s][t] DIV 2;
385 mvscale = ((mv_format==MV_FIELD) && (picture_structure==FRAME_PICTURE));
387 /* get dct_type (frame DCT / field DCT) */
388 dct_type = (picture_structure==FRAME_PICTURE)
389 && (!frame_pred_frame_dct)
390 && (macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA))
395 if (Trace_Flag && (picture_structure==FRAME_PICTURE)
396 && (!frame_pred_frame_dct)
397 && (macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA)))
398 printf("dct_type (%d): %s\n",dct_type,dct_type?"Field":"Frame");
402 *pmacroblock_type = macroblock_type;
404 *pstwclass = stwclass;
405 *pmotion_type = motion_type;
406 *pmotion_vector_count = motion_vector_count;
407 *pmv_format = mv_format;
410 *pdct_type = dct_type;
414 /* move/add 8x8-Block from block[comp] to backward_reference_frame */
415 /* copy reconstructed 8x8 block from block[comp] to current_frame[]
416 * ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data
417 * This stage also embodies some of the operations implied by:
418 * - ISO/IEC 13818-2 section 7.6.7: Combining predictions
419 * - ISO/IEC 13818-2 section 6.1.3: Macroblock
421 static void Add_Block(comp,bx,by,dct_type,addflag)
422 int comp,bx,by,dct_type,addflag;
429 /* derive color component index */
430 /* equivalent to ISO/IEC 13818-2 Table 7-1 */
431 cc = (comp<4) ? 0 : (comp&1)+1; /* color component index */
437 if (picture_structure==FRAME_PICTURE)
440 /* field DCT coding */
441 rfp = current_frame[0]
442 + Coded_Picture_Width*(by+((comp&2)>>1)) + bx + ((comp&1)<<3);
443 iincr = (Coded_Picture_Width<<1) - 8;
447 /* frame DCT coding */
448 rfp = current_frame[0]
449 + Coded_Picture_Width*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
450 iincr = Coded_Picture_Width - 8;
455 rfp = current_frame[0]
456 + (Coded_Picture_Width<<1)*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
457 iincr = (Coded_Picture_Width<<1) - 8;
464 /* scale coordinates */
465 if (chroma_format!=CHROMA444)
467 if (chroma_format==CHROMA420)
469 if (picture_structure==FRAME_PICTURE)
471 if (dct_type && (chroma_format!=CHROMA420))
473 /* field DCT coding */
474 rfp = current_frame[cc]
475 + Chroma_Width*(by+((comp&2)>>1)) + bx + (comp&8);
476 iincr = (Chroma_Width<<1) - 8;
480 /* frame DCT coding */
481 rfp = current_frame[cc]
482 + Chroma_Width*(by+((comp&2)<<2)) + bx + (comp&8);
483 iincr = Chroma_Width - 8;
489 rfp = current_frame[cc]
490 + (Chroma_Width<<1)*(by+((comp&2)<<2)) + bx + (comp&8);
491 iincr = (Chroma_Width<<1) - 8;
495 bp = ld->block[comp];
503 *rfp = Clip[*bp++ + *rfp];
515 *rfp++ = Clip[*bp++ + 128];
523 /* ISO/IEC 13818-2 section 7.8 */
524 static void Decode_SNR_Macroblock(SNRMBA, SNRMBAinc, MBA, MBAmax, dct_type)
525 int *SNRMBA, *SNRMBAinc;
529 int SNRmacroblock_type, SNRcoded_block_pattern, SNRdct_type, dummy;
530 int slice_vert_pos_ext, quantizer_scale_code, comp, code;
536 if (!Show_Bits(23)) /* next_start_code */
539 code = Show_Bits(32);
541 if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
543 /* only slice headers are allowed in picture_data */
545 printf("SNR: Premature end of picture\n");
551 /* decode slice header (may change quantizer_scale) */
552 slice_vert_pos_ext = slice_header();
554 /* decode macroblock address increment */
555 *SNRMBAinc = Get_macroblock_address_increment();
557 /* set current location */
559 ((slice_vert_pos_ext<<7) + (code&255) - 1)*mb_width + *SNRMBAinc - 1;
561 *SNRMBAinc = 1; /* first macroblock in slice: not skipped */
563 else /* not next_start_code */
568 printf("Too many macroblocks in picture\n");
572 /* decode macroblock address increment */
573 *SNRMBAinc = Get_macroblock_address_increment();
579 /* streams out of sync */
581 printf("Cant't synchronize streams\n");
585 if (*SNRMBAinc==1) /* not skipped */
587 macroblock_modes(&SNRmacroblock_type, &dummy, &dummy,
588 &dummy, &dummy, &dummy, &dummy, &dummy,
591 if (SNRmacroblock_type & MACROBLOCK_PATTERN)
592 *dct_type = SNRdct_type;
594 if (SNRmacroblock_type & MACROBLOCK_QUANT)
596 quantizer_scale_code = Get_Bits(5);
597 ld->quantizer_scale =
598 ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code] : quantizer_scale_code<<1;
601 /* macroblock_pattern */
602 if (SNRmacroblock_type & MACROBLOCK_PATTERN)
604 SNRcoded_block_pattern = Get_coded_block_pattern();
606 if (chroma_format==CHROMA422)
607 SNRcoded_block_pattern = (SNRcoded_block_pattern<<2) | Get_Bits(2); /* coded_block_pattern_1 */
608 else if (chroma_format==CHROMA444)
609 SNRcoded_block_pattern = (SNRcoded_block_pattern<<6) | Get_Bits(6); /* coded_block_pattern_2 */
612 SNRcoded_block_pattern = 0;
615 for (comp=0; comp<block_count; comp++)
619 if (SNRcoded_block_pattern & (1<<(block_count-1-comp)))
620 Decode_MPEG2_Non_Intra_Block(comp);
623 else /* SNRMBAinc!=1: skipped macroblock */
625 for (comp=0; comp<block_count; comp++)
634 /* IMPLEMENTATION: set scratch pad macroblock to zero */
635 static void Clear_Block(comp)
641 Block_Ptr = ld->block[comp];
648 /* SCALABILITY: add SNR enhancement layer block data to base layer */
649 /* ISO/IEC 13818-2 section 7.8.3.4: Addition of coefficients from the two layes */
650 static void Sum_Block(comp)
653 short *Block_Ptr1, *Block_Ptr2;
656 Block_Ptr1 = base.block[comp];
657 Block_Ptr2 = enhan.block[comp];
660 *Block_Ptr1++ += *Block_Ptr2++;
664 /* limit coefficients to -2048..2047 */
665 /* ISO/IEC 13818-2 section 7.4.3 and 7.4.4: Saturation and Mismatch control */
666 static void Saturate(Block_Ptr)
673 /* ISO/IEC 13818-2 section 7.4.3: Saturation */
687 /* ISO/IEC 13818-2 section 7.4.4: Mismatch control */
694 /* reuse old picture buffers as soon as they are no longer needed
695 based on life-time axioms of MPEG */
696 static void Update_Picture_Buffers()
698 int cc; /* color component index */
699 unsigned char *tmp; /* temporary swap pointer */
701 for (cc=0; cc<3; cc++)
703 /* B pictures do not need to be save for future reference */
704 if (picture_coding_type==B_TYPE)
706 current_frame[cc] = auxframe[cc];
710 /* only update at the beginning of the coded frame */
713 tmp = forward_reference_frame[cc];
715 /* the previously decoded reference frame is stored
716 coincident with the location where the backward
717 reference frame is stored (backwards prediction is not
718 needed in P pictures) */
719 forward_reference_frame[cc] = backward_reference_frame[cc];
721 /* update pointer for potential future B pictures */
722 backward_reference_frame[cc] = tmp;
725 /* can erase over old backward reference frame since it is not used
726 in a P picture, and since any subsequent B pictures will use the
727 previously decoded I or P frame as the backward_reference_frame */
728 current_frame[cc] = backward_reference_frame[cc];
732 one-time folding of a line offset into the pointer which stores the
733 memory address of the current frame saves offsets and conditional
734 branches throughout the remainder of the picture processing loop */
735 if (picture_structure==BOTTOM_FIELD)
736 current_frame[cc]+= (cc==0) ? Coded_Picture_Width : Chroma_Width;
741 /* store last frame */
743 void Output_Last_Frame_of_Sequence(Framenum)
747 printf("last frame incomplete, not stored\n");
749 Write_Frame(backward_reference_frame,Framenum-1);
754 static void frame_reorder(Bitstream_Framenum, Sequence_Framenum)
755 int Bitstream_Framenum, Sequence_Framenum;
757 /* tracking variables to insure proper output in spatial scalability */
758 static int Oldref_progressive_frame, Newref_progressive_frame;
760 if (Sequence_Framenum!=0)
762 if (picture_structure==FRAME_PICTURE || Second_Field)
764 if (picture_coding_type==B_TYPE)
765 Write_Frame(auxframe,Bitstream_Framenum-1);
768 Newref_progressive_frame = progressive_frame;
769 progressive_frame = Oldref_progressive_frame;
771 Write_Frame(forward_reference_frame,Bitstream_Framenum-1);
773 Oldref_progressive_frame = progressive_frame = Newref_progressive_frame;
777 else if (Output_Type==T_X11)
779 if(!Display_Progressive_Flag)
780 Display_Second_Field();
785 Oldref_progressive_frame = progressive_frame;
790 /* ISO/IEC 13818-2 section 7.6 */
791 static void motion_compensation(MBA, macroblock_type, motion_type, PMV,
792 motion_vertical_field_select, dmvector, stwtype, dct_type)
797 int motion_vertical_field_select[2][2];
805 /* derive current macroblock position within picture */
806 /* ISO/IEC 13818-2 section 6.3.1.6 and 6.3.1.7 */
807 bx = 16*(MBA%mb_width);
808 by = 16*(MBA/mb_width);
810 /* motion compensation */
811 if (!(macroblock_type & MACROBLOCK_INTRA))
812 form_predictions(bx,by,macroblock_type,motion_type,PMV,
813 motion_vertical_field_select,dmvector,stwtype);
815 /* SCALABILITY: Data Partitioning */
816 if (base.scalable_mode==SC_DP)
819 /* copy or add block data into picture */
820 for (comp=0; comp<block_count; comp++)
822 /* SCALABILITY: SNR */
823 /* ISO/IEC 13818-2 section 7.8.3.4: Addition of coefficients from
825 if (Two_Streams && enhan.scalable_mode==SC_SNR)
826 Sum_Block(comp); /* add SNR enhancement layer data to base layer */
828 /* MPEG-2 saturation and mismatch control */
829 /* base layer could be MPEG-1 stream, enhancement MPEG-2 SNR */
830 /* ISO/IEC 13818-2 section 7.4.3 and 7.4.4: Saturation and Mismatch control */
831 if ((Two_Streams && enhan.scalable_mode==SC_SNR) || ld->MPEG2_Flag)
832 Saturate(ld->block[comp]);
834 /* ISO/IEC 13818-2 section Annex A: inverse DCT */
835 if (Reference_IDCT_Flag)
836 Reference_IDCT(ld->block[comp]);
838 Fast_IDCT(ld->block[comp]);
840 /* ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data */
841 Add_Block(comp,bx,by,dct_type,(macroblock_type & MACROBLOCK_INTRA)==0);
848 /* ISO/IEC 13818-2 section 7.6.6 */
849 static void skipped_macroblock(dc_dct_pred, PMV, motion_type,
850 motion_vertical_field_select, stwtype, macroblock_type)
854 int motion_vertical_field_select[2][2];
856 int *macroblock_type;
860 /* SCALABILITY: Data Paritioning */
861 if (base.scalable_mode==SC_DP)
864 for (comp=0; comp<block_count; comp++)
867 /* reset intra_dc predictors */
868 /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
869 dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
871 /* reset motion vector predictors */
872 /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
873 if (picture_coding_type==P_TYPE)
874 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
876 /* derive motion_type */
877 if (picture_structure==FRAME_PICTURE)
878 *motion_type = MC_FRAME;
881 *motion_type = MC_FIELD;
883 /* predict from field of same parity */
884 /* ISO/IEC 13818-2 section 7.6.6.1 and 7.6.6.3: P field picture and B field
886 motion_vertical_field_select[0][0]=motion_vertical_field_select[0][1] =
887 (picture_structure==BOTTOM_FIELD);
890 /* skipped I are spatial-only predicted, */
891 /* skipped P and B are temporal-only predicted */
892 /* ISO/IEC 13818-2 section 7.7.6: Skipped macroblocks */
893 *stwtype = (picture_coding_type==I_TYPE) ? 8 : 0;
895 /* IMPLEMENTATION: clear MACROBLOCK_INTRA */
896 *macroblock_type&= ~MACROBLOCK_INTRA;
901 /* return==-1 means go to next picture */
902 /* the expression "start of slice" is used throughout the normative
903 body of the MPEG specification */
904 static int start_of_slice(MBAmax, MBA, MBAinc,
913 int slice_vert_pos_ext;
920 code = Show_Bits(32);
922 if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
924 /* only slice headers are allowed in picture_data */
926 printf("start_of_slice(): Premature end of picture\n");
928 return(-1); /* trigger: go to next picture */
933 /* decode slice header (may change quantizer_scale) */
934 slice_vert_pos_ext = slice_header();
937 /* SCALABILITY: Data Partitioning */
938 if (base.scalable_mode==SC_DP)
942 code = Show_Bits(32);
944 if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
946 /* only slice headers are allowed in picture_data */
948 printf("DP: Premature end of picture\n");
949 return(-1); /* trigger: go to next picture */
954 /* decode slice header (may change quantizer_scale) */
955 slice_vert_pos_ext = slice_header();
957 if (base.priority_breakpoint!=1)
961 /* decode macroblock address increment */
962 *MBAinc = Get_macroblock_address_increment();
966 printf("start_of_slice(): MBAinc unsuccessful\n");
967 return(0); /* trigger: go to next slice */
970 /* set current location */
971 /* NOTE: the arithmetic used to derive macroblock_address below is
972 * equivalent to ISO/IEC 13818-2 section 6.3.17: Macroblock
974 *MBA = ((slice_vert_pos_ext<<7) + (code&255) - 1)*mb_width + *MBAinc - 1;
975 *MBAinc = 1; /* first macroblock in slice: not skipped */
977 /* reset all DC coefficient and motion vector predictors */
978 /* reset all DC coefficient and motion vector predictors */
979 /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
980 dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
982 /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
983 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
984 PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
986 /* successfull: trigger decode macroblocks in slice */
991 /* ISO/IEC 13818-2 sections 7.2 through 7.5 */
992 static int decode_macroblock(macroblock_type, stwtype, stwclass,
993 motion_type, dct_type, PMV, dc_dct_pred,
994 motion_vertical_field_select, dmvector)
995 int *macroblock_type;
1002 int motion_vertical_field_select[2][2];
1006 int quantizer_scale_code;
1009 int motion_vector_count;
1013 int coded_block_pattern;
1015 /* SCALABILITY: Data Patitioning */
1016 if (base.scalable_mode==SC_DP)
1018 if (base.priority_breakpoint<=2)
1024 /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
1025 macroblock_modes(macroblock_type, stwtype, stwclass,
1026 motion_type, &motion_vector_count, &mv_format, &dmv, &mvscale,
1029 if (Fault_Flag) return(0); /* trigger: go to next slice */
1031 if (*macroblock_type & MACROBLOCK_QUANT)
1033 quantizer_scale_code = Get_Bits(5);
1038 printf("quantiser_scale_code (");
1039 Print_Bits(quantizer_scale_code,5,5);
1040 printf("): %d\n",quantizer_scale_code);
1044 /* ISO/IEC 13818-2 section 7.4.2.2: Quantizer scale factor */
1046 ld->quantizer_scale =
1047 ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code]
1048 : (quantizer_scale_code << 1);
1050 ld->quantizer_scale = quantizer_scale_code;
1052 /* SCALABILITY: Data Partitioning */
1053 if (base.scalable_mode==SC_DP)
1054 /* make sure base.quantizer_scale is valid */
1055 base.quantizer_scale = ld->quantizer_scale;
1058 /* motion vectors */
1061 /* ISO/IEC 13818-2 section 6.3.17.2: Motion vectors */
1063 /* decode forward motion vectors */
1064 if ((*macroblock_type & MACROBLOCK_MOTION_FORWARD)
1065 || ((*macroblock_type & MACROBLOCK_INTRA)
1066 && concealment_motion_vectors))
1069 motion_vectors(PMV,dmvector,motion_vertical_field_select,
1070 0,motion_vector_count,mv_format,f_code[0][0]-1,f_code[0][1]-1,
1073 motion_vector(PMV[0][0],dmvector,
1074 forward_f_code-1,forward_f_code-1,0,0,full_pel_forward_vector);
1077 if (Fault_Flag) return(0); /* trigger: go to next slice */
1079 /* decode backward motion vectors */
1080 if (*macroblock_type & MACROBLOCK_MOTION_BACKWARD)
1083 motion_vectors(PMV,dmvector,motion_vertical_field_select,
1084 1,motion_vector_count,mv_format,f_code[1][0]-1,f_code[1][1]-1,0,
1087 motion_vector(PMV[0][1],dmvector,
1088 backward_f_code-1,backward_f_code-1,0,0,full_pel_backward_vector);
1091 if (Fault_Flag) return(0); /* trigger: go to next slice */
1093 if ((*macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
1094 Flush_Buffer(1); /* remove marker_bit */
1096 if (base.scalable_mode==SC_DP && base.priority_breakpoint==3)
1099 /* macroblock_pattern */
1100 /* ISO/IEC 13818-2 section 6.3.17.4: Coded block pattern */
1101 if (*macroblock_type & MACROBLOCK_PATTERN)
1103 coded_block_pattern = Get_coded_block_pattern();
1105 if (chroma_format==CHROMA422)
1107 /* coded_block_pattern_1 */
1108 coded_block_pattern = (coded_block_pattern<<2) | Get_Bits(2);
1113 printf("coded_block_pattern_1: ");
1114 Print_Bits(coded_block_pattern,2,2);
1115 printf(" (%d)\n",coded_block_pattern&3);
1119 else if (chroma_format==CHROMA444)
1121 /* coded_block_pattern_2 */
1122 coded_block_pattern = (coded_block_pattern<<6) | Get_Bits(6);
1127 printf("coded_block_pattern_2: ");
1128 Print_Bits(coded_block_pattern,6,6);
1129 printf(" (%d)\n",coded_block_pattern&63);
1135 coded_block_pattern = (*macroblock_type & MACROBLOCK_INTRA) ?
1136 (1<<block_count)-1 : 0;
1138 if (Fault_Flag) return(0); /* trigger: go to next slice */
1141 for (comp=0; comp<block_count; comp++)
1143 /* SCALABILITY: Data Partitioning */
1144 if (base.scalable_mode==SC_DP)
1149 if (coded_block_pattern & (1<<(block_count-1-comp)))
1151 if (*macroblock_type & MACROBLOCK_INTRA)
1154 Decode_MPEG2_Intra_Block(comp,dc_dct_pred);
1156 Decode_MPEG1_Intra_Block(comp,dc_dct_pred);
1161 Decode_MPEG2_Non_Intra_Block(comp);
1163 Decode_MPEG1_Non_Intra_Block(comp);
1166 if (Fault_Flag) return(0); /* trigger: go to next slice */
1170 if(picture_coding_type==D_TYPE)
1172 /* remove end_of_macroblock (always 1, prevents startcode emulation) */
1173 /* ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
1174 marker_bit("D picture end_of_macroblock bit");
1177 /* reset intra_dc predictors */
1178 /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
1179 if (!(*macroblock_type & MACROBLOCK_INTRA))
1180 dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
1182 /* reset motion vector predictors */
1183 if ((*macroblock_type & MACROBLOCK_INTRA) && !concealment_motion_vectors)
1185 /* intra mb without concealment motion vectors */
1186 /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
1187 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
1188 PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
1191 /* special "No_MC" macroblock_type case */
1192 /* ISO/IEC 13818-2 section 7.6.3.5: Prediction in P pictures */
1193 if ((picture_coding_type==P_TYPE)
1194 && !(*macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_INTRA)))
1196 /* non-intra mb without forward mv in a P picture */
1197 /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
1198 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
1200 /* derive motion_type */
1201 /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes, frame_motion_type */
1202 if (picture_structure==FRAME_PICTURE)
1203 *motion_type = MC_FRAME;
1206 *motion_type = MC_FIELD;
1207 /* predict from field of same parity */
1208 motion_vertical_field_select[0][0] = (picture_structure==BOTTOM_FIELD);
1214 /* purely spatially predicted macroblock */
1215 /* ISO/IEC 13818-2 section 7.7.5.1: Resetting motion vector predictions */
1216 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
1217 PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
1220 /* successfully decoded macroblock */
1223 } /* decode_macroblock */