1 /* putmpg.c, block and motion vector encoding routines */
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
34 /* generate variable length codes for an intra-coded block (6.2.6, 6.3.17) */
35 void putintrablk(blk,cc)
39 int n, dct_diff, run, signed_level;
41 /* DC coefficient (7.2.1) */
42 dct_diff = blk[0] - dc_dct_pred[cc]; /* difference to previous block */
43 dc_dct_pred[cc] = blk[0];
50 /* AC coefficients (7.2.2) */
54 /* use appropriate entropy scanning pattern */
55 signed_level = blk[(altscan ? alternate_scan : zig_zag_scan)[n]];
58 putAC(run,signed_level,intravlc);
62 run++; /* count zero coefficients */
65 /* End of Block -- normative block punctuation */
67 putbits(6,4); /* 0110 (Table B-15) */
69 putbits(2,2); /* 10 (Table B-14) */
72 /* generate variable length codes for a non-intra-coded block (6.2.6, 6.3.17) */
73 void putnonintrablk(blk)
76 int n, run, signed_level, first;
83 /* use appropriate entropy scanning pattern */
84 signed_level = blk[(altscan ? alternate_scan : zig_zag_scan)[n]];
90 /* first coefficient in non-intra block */
91 putACfirst(run,signed_level);
95 putAC(run,signed_level,0);
100 run++; /* count zero coefficients */
103 /* End of Block -- normative block punctuation */
107 /* generate variable length code for a motion vector component (7.6.3.1) */
108 void putmv(dmv,f_code)
111 int r_size, f, vmin, vmax, dv, temp, motion_code, motion_residual;
113 r_size = f_code - 1; /* number of fixed length code ('residual') bits */
115 vmin = -16*f; /* lower range limit */
116 vmax = 16*f - 1; /* upper range limit */
119 /* fold vector difference into [vmin...vmax] */
126 if (dmv<vmin || dmv>vmax)
128 fprintf(stderr,"invalid motion vector\n");
130 /* split dmv into motion_code and motion_residual */
131 temp = ((dmv<0) ? -dmv : dmv) + f - 1;
132 motion_code = temp>>r_size;
134 motion_code = -motion_code;
135 motion_residual = temp & (f-1);
137 putmotioncode(motion_code); /* variable length code */
139 if (r_size!=0 && motion_code!=0)
140 putbits(motion_residual,r_size); /* fixed length code */