]> Creatis software - gdcm.git/blob - src/gdcmmpeg2/src/mpeg2enc/putmpg.c
ENH: Remove Makefile in case of in source build
[gdcm.git] / src / gdcmmpeg2 / src / mpeg2enc / putmpg.c
1 /* putmpg.c, block and motion vector encoding routines                      */
2
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
4
5 /*
6  * Disclaimer of Warranty
7  *
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.
15  *
16  * This disclaimer of warranty extends to the user of these programs and user's
17  * customers, employees, agents, transferees, successors, and assigns.
18  *
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
21  * patents.
22  *
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
26  * design.
27  *
28  */
29
30 #include <stdio.h>
31 #include "config.h"
32 #include "global.h"
33
34 /* generate variable length codes for an intra-coded block (6.2.6, 6.3.17) */
35 void putintrablk(blk,cc)
36 short *blk;
37 int cc;
38 {
39   int n, dct_diff, run, signed_level;
40
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];
44
45   if (cc==0)
46     putDClum(dct_diff);
47   else
48     putDCchrom(dct_diff);
49
50   /* AC coefficients (7.2.2) */
51   run = 0;
52   for (n=1; n<64; n++)
53   {
54     /* use appropriate entropy scanning pattern */
55     signed_level = blk[(altscan ? alternate_scan : zig_zag_scan)[n]];
56     if (signed_level!=0)
57     {
58       putAC(run,signed_level,intravlc);
59       run = 0;
60     }
61     else
62       run++; /* count zero coefficients */
63   }
64
65   /* End of Block -- normative block punctuation */
66   if (intravlc)
67     putbits(6,4); /* 0110 (Table B-15) */
68   else
69     putbits(2,2); /* 10 (Table B-14) */
70 }
71
72 /* generate variable length codes for a non-intra-coded block (6.2.6, 6.3.17) */
73 void putnonintrablk(blk)
74 short *blk;
75 {
76   int n, run, signed_level, first;
77
78   run = 0;
79   first = 1;
80
81   for (n=0; n<64; n++)
82   {
83     /* use appropriate entropy scanning pattern */
84     signed_level = blk[(altscan ? alternate_scan : zig_zag_scan)[n]];
85
86     if (signed_level!=0)
87     {
88       if (first)
89       {
90         /* first coefficient in non-intra block */
91         putACfirst(run,signed_level);
92         first = 0;
93       }
94       else
95         putAC(run,signed_level,0);
96
97       run = 0;
98     }
99     else
100       run++; /* count zero coefficients */
101   }
102
103   /* End of Block -- normative block punctuation  */
104   putbits(2,2);
105 }
106
107 /* generate variable length code for a motion vector component (7.6.3.1) */
108 void putmv(dmv,f_code)
109 int dmv,f_code;
110 {
111   int r_size, f, vmin, vmax, dv, temp, motion_code, motion_residual;
112
113   r_size = f_code - 1; /* number of fixed length code ('residual') bits */
114   f = 1<<r_size;
115   vmin = -16*f; /* lower range limit */
116   vmax = 16*f - 1; /* upper range limit */
117   dv = 32*f;
118
119   /* fold vector difference into [vmin...vmax] */
120   if (dmv>vmax)
121     dmv-= dv;
122   else if (dmv<vmin)
123     dmv+= dv;
124
125   /* check value */
126   if (dmv<vmin || dmv>vmax)
127     if (!quiet)
128       fprintf(stderr,"invalid motion vector\n");
129
130   /* split dmv into motion_code and motion_residual */
131   temp = ((dmv<0) ? -dmv : dmv) + f - 1;
132   motion_code = temp>>r_size;
133   if (dmv<0)
134     motion_code = -motion_code;
135   motion_residual = temp & (f-1);
136
137   putmotioncode(motion_code); /* variable length code */
138
139   if (r_size!=0 && motion_code!=0)
140     putbits(motion_residual,r_size); /* fixed length code */
141 }