]> Creatis software - gdcm.git/blob - src/gdcmmpeg2/src/mpeg2enc/putseq.c
COMP: Fix stupid vs7 warning conversion from 'double' to 'unsigned char'
[gdcm.git] / src / gdcmmpeg2 / src / mpeg2enc / putseq.c
1 /* putseq.c, sequence level 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 <string.h>
32 #include "config.h"
33 #include "global.h"
34
35 void putseq()
36 {
37   /* this routine assumes (N % M) == 0 */
38   int i, j, k, f, f0, n, np, nb, sxf, syf, sxb, syb;
39   int ipflag;
40   char name[256];
41   unsigned char *neworg[3], *newref[3];
42   static char ipb[5] = {' ','I','P','B','D'};
43
44   rc_init_seq(); /* initialize rate control */
45
46   /* sequence header, sequence extension and sequence display extension */
47   putseqhdr();
48   if (!mpeg1)
49   {
50     putseqext();
51     putseqdispext();
52   }
53
54   /* optionally output some text data (description, copyright or whatever) */
55   if (strlen(id_string) > 1)
56     putuserdata(id_string);
57
58   /* loop through all frames in encoding/decoding order */
59   for (i=0; i<nframes; i++)
60   {
61     if (!quiet)
62     {
63       fprintf(stderr,"Encoding frame %d ",i);
64       fflush(stderr);
65     }
66
67     /* f0: lowest frame number in current GOP
68      *
69      * first GOP contains N-(M-1) frames,
70      * all other GOPs contain N frames
71      */
72     f0 = N*((i+(M-1))/N) - (M-1);
73
74     if (f0<0)
75       f0=0;
76
77     if (i==0 || (i-1)%M==0)
78     {
79       /* I or P frame */
80       for (j=0; j<3; j++)
81       {
82         /* shuffle reference frames */
83         neworg[j] = oldorgframe[j];
84         newref[j] = oldrefframe[j];
85         oldorgframe[j] = neworgframe[j];
86         oldrefframe[j] = newrefframe[j];
87         neworgframe[j] = neworg[j];
88         newrefframe[j] = newref[j];
89       }
90
91       /* f: frame number in display order */
92       f = (i==0) ? 0 : i+M-1;
93       if (f>=nframes)
94         f = nframes - 1;
95
96       if (i==f0) /* first displayed frame in GOP is I */
97       {
98         /* I frame */
99         pict_type = I_TYPE;
100         forw_hor_f_code = forw_vert_f_code = 15;
101         back_hor_f_code = back_vert_f_code = 15;
102
103         /* n: number of frames in current GOP
104          *
105          * first GOP contains (M-1) less (B) frames
106          */
107         n = (i==0) ? N-(M-1) : N;
108
109         /* last GOP may contain less frames */
110         if (n > nframes-f0)
111           n = nframes-f0;
112
113         /* number of P frames */
114         if (i==0)
115           np = (n + 2*(M-1))/M - 1; /* first GOP */
116         else
117           np = (n + (M-1))/M - 1;
118
119         /* number of B frames */
120         nb = n - np - 1;
121
122         rc_init_GOP(np,nb);
123
124         putgophdr(f0,i==0); /* set closed_GOP in first GOP only */
125       }
126       else
127       {
128         /* P frame */
129         pict_type = P_TYPE;
130         forw_hor_f_code = motion_data[0].forw_hor_f_code;
131         forw_vert_f_code = motion_data[0].forw_vert_f_code;
132         back_hor_f_code = back_vert_f_code = 15;
133         sxf = motion_data[0].sxf;
134         syf = motion_data[0].syf;
135       }
136     }
137     else
138     {
139       /* B frame */
140       for (j=0; j<3; j++)
141       {
142         neworg[j] = auxorgframe[j];
143         newref[j] = auxframe[j];
144       }
145
146       /* f: frame number in display order */
147       f = i - 1;
148       pict_type = B_TYPE;
149       n = (i-2)%M + 1; /* first B: n=1, second B: n=2, ... */
150       forw_hor_f_code = motion_data[n].forw_hor_f_code;
151       forw_vert_f_code = motion_data[n].forw_vert_f_code;
152       back_hor_f_code = motion_data[n].back_hor_f_code;
153       back_vert_f_code = motion_data[n].back_vert_f_code;
154       sxf = motion_data[n].sxf;
155       syf = motion_data[n].syf;
156       sxb = motion_data[n].sxb;
157       syb = motion_data[n].syb;
158     }
159
160     temp_ref = f - f0;
161     frame_pred_dct = frame_pred_dct_tab[pict_type-1];
162     q_scale_type = qscale_tab[pict_type-1];
163     intravlc = intravlc_tab[pict_type-1];
164     altscan = altscan_tab[pict_type-1];
165
166     fprintf(statfile,"\nFrame %d (#%d in display order):\n",i,f);
167     fprintf(statfile," picture_type=%c\n",ipb[pict_type]);
168     fprintf(statfile," temporal_reference=%d\n",temp_ref);
169     fprintf(statfile," frame_pred_frame_dct=%d\n",frame_pred_dct);
170     fprintf(statfile," q_scale_type=%d\n",q_scale_type);
171     fprintf(statfile," intra_vlc_format=%d\n",intravlc);
172     fprintf(statfile," alternate_scan=%d\n",altscan);
173
174     if (pict_type!=I_TYPE)
175     {
176       fprintf(statfile," forward search window: %d...%d / %d...%d\n",
177         -sxf,sxf,-syf,syf);
178       fprintf(statfile," forward vector range: %d...%d.5 / %d...%d.5\n",
179         -(4<<forw_hor_f_code),(4<<forw_hor_f_code)-1,
180         -(4<<forw_vert_f_code),(4<<forw_vert_f_code)-1);
181     }
182
183     if (pict_type==B_TYPE)
184     {
185       fprintf(statfile," backward search window: %d...%d / %d...%d\n",
186         -sxb,sxb,-syb,syb);
187       fprintf(statfile," backward vector range: %d...%d.5 / %d...%d.5\n",
188         -(4<<back_hor_f_code),(4<<back_hor_f_code)-1,
189         -(4<<back_vert_f_code),(4<<back_vert_f_code)-1);
190     }
191
192     sprintf(name,tplorg,f+frame0);
193     readframe(name,neworg);
194
195     if (fieldpic)
196     {
197       if (!quiet)
198       {
199         fprintf(stderr,"\nfirst field  (%s) ",topfirst ? "top" : "bot");
200         fflush(stderr);
201       }
202
203       pict_struct = topfirst ? TOP_FIELD : BOTTOM_FIELD;
204
205       motion_estimation(oldorgframe[0],neworgframe[0],
206                         oldrefframe[0],newrefframe[0],
207                         neworg[0],newref[0],
208                         sxf,syf,sxb,syb,mbinfo,0,0);
209
210       predict(oldrefframe,newrefframe,predframe,0,mbinfo);
211       dct_type_estimation(predframe[0],neworg[0],mbinfo);
212       transform(predframe,neworg,mbinfo,blocks);
213
214       putpict(neworg[0]);
215
216       for (k=0; k<mb_height2*mb_width; k++)
217       {
218         if (mbinfo[k].mb_type & MB_INTRA)
219           for (j=0; j<block_count; j++)
220             iquant_intra(blocks[k*block_count+j],blocks[k*block_count+j],
221                          dc_prec,intra_q,mbinfo[k].mquant);
222         else
223           for (j=0;j<block_count;j++)
224             iquant_non_intra(blocks[k*block_count+j],blocks[k*block_count+j],
225                              inter_q,mbinfo[k].mquant);
226       }
227
228       itransform(predframe,newref,mbinfo,blocks);
229       calcSNR(neworg,newref);
230       stats();
231
232       if (!quiet)
233       {
234         fprintf(stderr,"second field (%s) ",topfirst ? "bot" : "top");
235         fflush(stderr);
236       }
237
238       pict_struct = topfirst ? BOTTOM_FIELD : TOP_FIELD;
239
240       ipflag = (pict_type==I_TYPE);
241       if (ipflag)
242       {
243         /* first field = I, second field = P */
244         pict_type = P_TYPE;
245         forw_hor_f_code = motion_data[0].forw_hor_f_code;
246         forw_vert_f_code = motion_data[0].forw_vert_f_code;
247         back_hor_f_code = back_vert_f_code = 15;
248         sxf = motion_data[0].sxf;
249         syf = motion_data[0].syf;
250       }
251
252       motion_estimation(oldorgframe[0],neworgframe[0],
253                         oldrefframe[0],newrefframe[0],
254                         neworg[0],newref[0],
255                         sxf,syf,sxb,syb,mbinfo,1,ipflag);
256
257       predict(oldrefframe,newrefframe,predframe,1,mbinfo);
258       dct_type_estimation(predframe[0],neworg[0],mbinfo);
259       transform(predframe,neworg,mbinfo,blocks);
260
261       putpict(neworg[0]);
262
263       for (k=0; k<mb_height2*mb_width; k++)
264       {
265         if (mbinfo[k].mb_type & MB_INTRA)
266           for (j=0; j<block_count; j++)
267             iquant_intra(blocks[k*block_count+j],blocks[k*block_count+j],
268                          dc_prec,intra_q,mbinfo[k].mquant);
269         else
270           for (j=0;j<block_count;j++)
271             iquant_non_intra(blocks[k*block_count+j],blocks[k*block_count+j],
272                              inter_q,mbinfo[k].mquant);
273       }
274
275       itransform(predframe,newref,mbinfo,blocks);
276       calcSNR(neworg,newref);
277       stats();
278     }
279     else
280     {
281       pict_struct = FRAME_PICTURE;
282
283       /* do motion_estimation
284        *
285        * uses source frames (...orgframe) for full pel search
286        * and reconstructed frames (...refframe) for half pel search
287        */
288
289       motion_estimation(oldorgframe[0],neworgframe[0],
290                         oldrefframe[0],newrefframe[0],
291                         neworg[0],newref[0],
292                         sxf,syf,sxb,syb,mbinfo,0,0);
293
294       predict(oldrefframe,newrefframe,predframe,0,mbinfo);
295       dct_type_estimation(predframe[0],neworg[0],mbinfo);
296       transform(predframe,neworg,mbinfo,blocks);
297
298       putpict(neworg[0]);
299
300       for (k=0; k<mb_height*mb_width; k++)
301       {
302         if (mbinfo[k].mb_type & MB_INTRA)
303           for (j=0; j<block_count; j++)
304             iquant_intra(blocks[k*block_count+j],blocks[k*block_count+j],
305                          dc_prec,intra_q,mbinfo[k].mquant);
306         else
307           for (j=0;j<block_count;j++)
308             iquant_non_intra(blocks[k*block_count+j],blocks[k*block_count+j],
309                              inter_q,mbinfo[k].mquant);
310       }
311
312       itransform(predframe,newref,mbinfo,blocks);
313       calcSNR(neworg,newref);
314       stats();
315     }
316
317     sprintf(name,tplref,f+frame0);
318     writeframe(name,newref);
319
320   }
321
322   putseqend();
323 }