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