]> Creatis software - gdcm.git/blob - src/gdcmmpeg2/src/mpeg2enc/putpic.c
ENH: Revamp a bit the build process to make the exe more like a library...
[gdcm.git] / src / gdcmmpeg2 / src / mpeg2enc / putpic.c
1 /* putpic.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 /* private prototypes */
35 static void putmvs _ANSI_ARGS_((int MV[2][2][2], int PMV[2][2][2],
36   int mv_field_sel[2][2], int dmvector[2], int s, int motion_type,
37   int hor_f_code, int vert_f_code));
38
39 /* quantization / variable length encoding of a complete picture */
40 void putpict(frame)
41 unsigned char *frame;
42 {
43   int i, j, k, comp, cc;
44   int mb_type;
45   int PMV[2][2][2];
46   int prev_mquant;
47   int cbp, MBAinc;
48
49   rc_init_pict(frame); /* set up rate control */
50
51   /* picture header and picture coding extension */
52   putpicthdr();
53
54   if (!mpeg1)
55     putpictcodext();
56
57   prev_mquant = rc_start_mb(); /* initialize quantization parameter */
58
59   k = 0;
60
61   for (j=0; j<mb_height2; j++)
62   {
63     /* macroblock row loop */
64
65     for (i=0; i<mb_width; i++)
66     {
67       /* macroblock loop */
68       if (i==0)
69       {
70         /* slice header (6.2.4) */
71         alignbits();
72
73         if (mpeg1 || vertical_size<=2800)
74           putbits(SLICE_MIN_START+j,32); /* slice_start_code */
75         else
76         {
77           putbits(SLICE_MIN_START+(j&127),32); /* slice_start_code */
78           putbits(j>>7,3); /* slice_vertical_position_extension */
79         }
80   
81         /* quantiser_scale_code */
82         putbits(q_scale_type ? map_non_linear_mquant[prev_mquant]
83                              : prev_mquant >> 1, 5);
84   
85         putbits(0,1); /* extra_bit_slice */
86   
87         /* reset predictors */
88
89         for (cc=0; cc<3; cc++)
90           dc_dct_pred[cc] = 0;
91
92         PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
93         PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
94   
95         MBAinc = i + 1; /* first MBAinc denotes absolute position */
96       }
97
98       mb_type = mbinfo[k].mb_type;
99
100       /* determine mquant (rate control) */
101       mbinfo[k].mquant = rc_calc_mquant(k);
102
103       /* quantize macroblock */
104       if (mb_type & MB_INTRA)
105       {
106         for (comp=0; comp<block_count; comp++)
107           quant_intra(blocks[k*block_count+comp],blocks[k*block_count+comp],
108                       dc_prec,intra_q,mbinfo[k].mquant);
109         mbinfo[k].cbp = cbp = (1<<block_count) - 1;
110       }
111       else
112       {
113         cbp = 0;
114         for (comp=0;comp<block_count;comp++)
115           cbp = (cbp<<1) | quant_non_intra(blocks[k*block_count+comp],
116                                            blocks[k*block_count+comp],
117                                            inter_q,mbinfo[k].mquant);
118
119         mbinfo[k].cbp = cbp;
120
121         if (cbp)
122           mb_type|= MB_PATTERN;
123       }
124
125       /* output mquant if it has changed */
126       if (cbp && prev_mquant!=mbinfo[k].mquant)
127         mb_type|= MB_QUANT;
128
129       /* check if macroblock can be skipped */
130       if (i!=0 && i!=mb_width-1 && !cbp)
131       {
132         /* no DCT coefficients and neither first nor last macroblock of slice */
133
134         if (pict_type==P_TYPE && !(mb_type&MB_FORWARD))
135         {
136           /* P picture, no motion vectors -> skip */
137
138           /* reset predictors */
139
140           for (cc=0; cc<3; cc++)
141             dc_dct_pred[cc] = 0;
142
143           PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
144           PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
145
146           mbinfo[k].mb_type = mb_type;
147           mbinfo[k].skipped = 1;
148           MBAinc++;
149           k++;
150           continue;
151         }
152
153         if (pict_type==B_TYPE && pict_struct==FRAME_PICTURE
154             && mbinfo[k].motion_type==MC_FRAME
155             && ((mbinfo[k-1].mb_type^mb_type)&(MB_FORWARD|MB_BACKWARD))==0
156             && (!(mb_type&MB_FORWARD) ||
157                 (PMV[0][0][0]==mbinfo[k].MV[0][0][0] &&
158                  PMV[0][0][1]==mbinfo[k].MV[0][0][1]))
159             && (!(mb_type&MB_BACKWARD) ||
160                 (PMV[0][1][0]==mbinfo[k].MV[0][1][0] &&
161                  PMV[0][1][1]==mbinfo[k].MV[0][1][1])))
162         {
163           /* conditions for skipping in B frame pictures:
164            * - must be frame predicted
165            * - must be the same prediction type (forward/backward/interp.)
166            *   as previous macroblock
167            * - relevant vectors (forward/backward/both) have to be the same
168            *   as in previous macroblock
169            */
170
171           mbinfo[k].mb_type = mb_type;
172           mbinfo[k].skipped = 1;
173           MBAinc++;
174           k++;
175           continue;
176         }
177
178         if (pict_type==B_TYPE && pict_struct!=FRAME_PICTURE
179             && mbinfo[k].motion_type==MC_FIELD
180             && ((mbinfo[k-1].mb_type^mb_type)&(MB_FORWARD|MB_BACKWARD))==0
181             && (!(mb_type&MB_FORWARD) ||
182                 (PMV[0][0][0]==mbinfo[k].MV[0][0][0] &&
183                  PMV[0][0][1]==mbinfo[k].MV[0][0][1] &&
184                  mbinfo[k].mv_field_sel[0][0]==(pict_struct==BOTTOM_FIELD)))
185             && (!(mb_type&MB_BACKWARD) ||
186                 (PMV[0][1][0]==mbinfo[k].MV[0][1][0] &&
187                  PMV[0][1][1]==mbinfo[k].MV[0][1][1] &&
188                  mbinfo[k].mv_field_sel[0][1]==(pict_struct==BOTTOM_FIELD))))
189         {
190           /* conditions for skipping in B field pictures:
191            * - must be field predicted
192            * - must be the same prediction type (forward/backward/interp.)
193            *   as previous macroblock
194            * - relevant vectors (forward/backward/both) have to be the same
195            *   as in previous macroblock
196            * - relevant motion_vertical_field_selects have to be of same
197            *   parity as current field
198            */
199
200           mbinfo[k].mb_type = mb_type;
201           mbinfo[k].skipped = 1;
202           MBAinc++;
203           k++;
204           continue;
205         }
206       }
207
208       /* macroblock cannot be skipped */
209       mbinfo[k].skipped = 0;
210
211       /* there's no VLC for 'No MC, Not Coded':
212        * we have to transmit (0,0) motion vectors
213        */
214       if (pict_type==P_TYPE && !cbp && !(mb_type&MB_FORWARD))
215         mb_type|= MB_FORWARD;
216
217       putaddrinc(MBAinc); /* macroblock_address_increment */
218       MBAinc = 1;
219
220       putmbtype(pict_type,mb_type); /* macroblock type */
221
222       if (mb_type & (MB_FORWARD|MB_BACKWARD) && !frame_pred_dct)
223         putbits(mbinfo[k].motion_type,2);
224
225       if (pict_struct==FRAME_PICTURE && cbp && !frame_pred_dct)
226         putbits(mbinfo[k].dct_type,1);
227
228       if (mb_type & MB_QUANT)
229       {
230         putbits(q_scale_type ? map_non_linear_mquant[mbinfo[k].mquant]
231                              : mbinfo[k].mquant>>1,5);
232         prev_mquant = mbinfo[k].mquant;
233       }
234
235       if (mb_type & MB_FORWARD)
236       {
237         /* forward motion vectors, update predictors */
238         putmvs(mbinfo[k].MV,PMV,mbinfo[k].mv_field_sel,mbinfo[k].dmvector,0,
239           mbinfo[k].motion_type,forw_hor_f_code,forw_vert_f_code);
240       }
241
242       if (mb_type & MB_BACKWARD)
243       {
244         /* backward motion vectors, update predictors */
245         putmvs(mbinfo[k].MV,PMV,mbinfo[k].mv_field_sel,mbinfo[k].dmvector,1,
246           mbinfo[k].motion_type,back_hor_f_code,back_vert_f_code);
247       }
248
249       if (mb_type & MB_PATTERN)
250       {
251         putcbp((cbp >> (block_count-6)) & 63);
252         if (chroma_format!=CHROMA420)
253           putbits(cbp,block_count-6);
254       }
255
256       for (comp=0; comp<block_count; comp++)
257       {
258         /* block loop */
259         if (cbp & (1<<(block_count-1-comp)))
260         {
261           if (mb_type & MB_INTRA)
262           {
263             cc = (comp<4) ? 0 : (comp&1)+1;
264             putintrablk(blocks[k*block_count+comp],cc);
265           }
266           else
267             putnonintrablk(blocks[k*block_count+comp]);
268         }
269       }
270
271       /* reset predictors */
272       if (!(mb_type & MB_INTRA))
273         for (cc=0; cc<3; cc++)
274           dc_dct_pred[cc] = 0;
275
276       if (mb_type & MB_INTRA || (pict_type==P_TYPE && !(mb_type & MB_FORWARD)))
277       {
278         PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
279         PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
280       }
281
282       mbinfo[k].mb_type = mb_type;
283       k++;
284     }
285   }
286
287   rc_update_pict();
288   vbv_end_of_picture();
289 }
290
291
292 /* output motion vectors (6.2.5.2, 6.3.16.2)
293  *
294  * this routine also updates the predictions for motion vectors (PMV)
295  */
296  
297 static void putmvs(MV,PMV,mv_field_sel,dmvector,s,motion_type,
298   hor_f_code,vert_f_code)
299 int MV[2][2][2],PMV[2][2][2];
300 int mv_field_sel[2][2];
301 int dmvector[2];
302 int s,motion_type,hor_f_code,vert_f_code;
303 {
304   if (pict_struct==FRAME_PICTURE)
305   {
306     if (motion_type==MC_FRAME)
307     {
308       /* frame prediction */
309       putmv(MV[0][s][0]-PMV[0][s][0],hor_f_code);
310       putmv(MV[0][s][1]-PMV[0][s][1],vert_f_code);
311       PMV[0][s][0]=PMV[1][s][0]=MV[0][s][0];
312       PMV[0][s][1]=PMV[1][s][1]=MV[0][s][1];
313     }
314     else if (motion_type==MC_FIELD)
315     {
316       /* field prediction */
317       putbits(mv_field_sel[0][s],1);
318       putmv(MV[0][s][0]-PMV[0][s][0],hor_f_code);
319       putmv((MV[0][s][1]>>1)-(PMV[0][s][1]>>1),vert_f_code);
320       putbits(mv_field_sel[1][s],1);
321       putmv(MV[1][s][0]-PMV[1][s][0],hor_f_code);
322       putmv((MV[1][s][1]>>1)-(PMV[1][s][1]>>1),vert_f_code);
323       PMV[0][s][0]=MV[0][s][0];
324       PMV[0][s][1]=MV[0][s][1];
325       PMV[1][s][0]=MV[1][s][0];
326       PMV[1][s][1]=MV[1][s][1];
327     }
328     else
329     {
330       /* dual prime prediction */
331       putmv(MV[0][s][0]-PMV[0][s][0],hor_f_code);
332       putdmv(dmvector[0]);
333       putmv((MV[0][s][1]>>1)-(PMV[0][s][1]>>1),vert_f_code);
334       putdmv(dmvector[1]);
335       PMV[0][s][0]=PMV[1][s][0]=MV[0][s][0];
336       PMV[0][s][1]=PMV[1][s][1]=MV[0][s][1];
337     }
338   }
339   else
340   {
341     /* field picture */
342     if (motion_type==MC_FIELD)
343     {
344       /* field prediction */
345       putbits(mv_field_sel[0][s],1);
346       putmv(MV[0][s][0]-PMV[0][s][0],hor_f_code);
347       putmv(MV[0][s][1]-PMV[0][s][1],vert_f_code);
348       PMV[0][s][0]=PMV[1][s][0]=MV[0][s][0];
349       PMV[0][s][1]=PMV[1][s][1]=MV[0][s][1];
350     }
351     else if (motion_type==MC_16X8)
352     {
353       /* 16x8 prediction */
354       putbits(mv_field_sel[0][s],1);
355       putmv(MV[0][s][0]-PMV[0][s][0],hor_f_code);
356       putmv(MV[0][s][1]-PMV[0][s][1],vert_f_code);
357       putbits(mv_field_sel[1][s],1);
358       putmv(MV[1][s][0]-PMV[1][s][0],hor_f_code);
359       putmv(MV[1][s][1]-PMV[1][s][1],vert_f_code);
360       PMV[0][s][0]=MV[0][s][0];
361       PMV[0][s][1]=MV[0][s][1];
362       PMV[1][s][0]=MV[1][s][0];
363       PMV[1][s][1]=MV[1][s][1];
364     }
365     else
366     {
367       /* dual prime prediction */
368       putmv(MV[0][s][0]-PMV[0][s][0],hor_f_code);
369       putdmv(dmvector[0]);
370       putmv(MV[0][s][1]-PMV[0][s][1],vert_f_code);
371       putdmv(dmvector[1]);
372       PMV[0][s][0]=PMV[1][s][0]=MV[0][s][0];
373       PMV[0][s][1]=PMV[1][s][1]=MV[0][s][1];
374     }
375   }
376 }