]> Creatis software - gdcm.git/blob - src/gdcmmpeg2/src/mpeg2enc/puthdr.c
ENH: Remove Makefile in case of in source build
[gdcm.git] / src / gdcmmpeg2 / src / mpeg2enc / puthdr.c
1 /* puthdr.c, generation of headers                                          */
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 <math.h>
32 #include "config.h"
33 #include "global.h"
34
35 /* private prototypes */
36 static int frametotc _ANSI_ARGS_((int frame));
37
38 /* generate sequence header (6.2.2.1, 6.3.3)
39  *
40  * matrix download not implemented
41  */
42 void putseqhdr()
43 {
44   int i;
45
46   alignbits();
47   putbits(SEQ_START_CODE,32); /* sequence_header_code */
48   putbits(horizontal_size,12); /* horizontal_size_value */
49   putbits(vertical_size,12); /* vertical_size_value */
50   putbits(aspectratio,4); /* aspect_ratio_information */
51   putbits(frame_rate_code,4); /* frame_rate_code */
52   putbits((int)ceil(bit_rate/400.0),18); /* bit_rate_value */
53   putbits(1,1); /* marker_bit */
54   putbits(vbv_buffer_size,10); /* vbv_buffer_size_value */
55   putbits(constrparms,1); /* constrained_parameters_flag */
56
57   putbits(load_iquant,1); /* load_intra_quantizer_matrix */
58   if (load_iquant)
59     for (i=0; i<64; i++)  /* matrices are always downloaded in zig-zag order */
60       putbits(intra_q[zig_zag_scan[i]],8); /* intra_quantizer_matrix */
61
62   putbits(load_niquant,1); /* load_non_intra_quantizer_matrix */
63   if (load_niquant)
64     for (i=0; i<64; i++)
65       putbits(inter_q[zig_zag_scan[i]],8); /* non_intra_quantizer_matrix */
66 }
67
68 /* generate sequence extension (6.2.2.3, 6.3.5) header (MPEG-2 only) */
69 void putseqext()
70 {
71   alignbits();
72   putbits(EXT_START_CODE,32); /* extension_start_code */
73   putbits(SEQ_ID,4); /* extension_start_code_identifier */
74   putbits((profile<<4)|level,8); /* profile_and_level_indication */
75   putbits(prog_seq,1); /* progressive sequence */
76   putbits(chroma_format,2); /* chroma_format */
77   putbits(horizontal_size>>12,2); /* horizontal_size_extension */
78   putbits(vertical_size>>12,2); /* vertical_size_extension */
79   putbits(((int)ceil(bit_rate/400.0))>>18,12); /* bit_rate_extension */
80   putbits(1,1); /* marker_bit */
81   putbits(vbv_buffer_size>>10,8); /* vbv_buffer_size_extension */
82   putbits(0,1); /* low_delay  -- currently not implemented */
83   putbits(0,2); /* frame_rate_extension_n */
84   putbits(0,5); /* frame_rate_extension_d */
85 }
86
87 /* generate sequence display extension (6.2.2.4, 6.3.6)
88  *
89  * content not yet user setable
90  */
91 void putseqdispext()
92 {
93   alignbits();
94   putbits(EXT_START_CODE,32); /* extension_start_code */
95   putbits(DISP_ID,4); /* extension_start_code_identifier */
96   putbits(video_format,3); /* video_format */
97   putbits(1,1); /* colour_description */
98   putbits(color_primaries,8); /* colour_primaries */
99   putbits(transfer_characteristics,8); /* transfer_characteristics */
100   putbits(matrix_coefficients,8); /* matrix_coefficients */
101   putbits(display_horizontal_size,14); /* display_horizontal_size */
102   putbits(1,1); /* marker_bit */
103   putbits(display_vertical_size,14); /* display_vertical_size */
104 }
105
106 /* output a zero terminated string as user data (6.2.2.2.2, 6.3.4.1)
107  *
108  * string must not emulate start codes
109  */
110 void putuserdata(userdata)
111 char *userdata;
112 {
113   alignbits();
114   putbits(USER_START_CODE,32); /* user_data_start_code */
115   while (*userdata)
116     putbits(*userdata++,8);
117 }
118
119 /* generate group of pictures header (6.2.2.6, 6.3.9)
120  *
121  * uses tc0 (timecode of first frame) and frame0 (number of first frame)
122  */
123 void putgophdr(frame,closed_gop)
124 int frame,closed_gop;
125 {
126   int tc;
127
128   alignbits();
129   putbits(GOP_START_CODE,32); /* group_start_code */
130   tc = frametotc(tc0+frame);
131   putbits(tc,25); /* time_code */
132   putbits(closed_gop,1); /* closed_gop */
133   putbits(0,1); /* broken_link */
134 }
135
136 /* convert frame number to time_code
137  *
138  * drop_frame not implemented
139  */
140 static int frametotc(frame)
141 int frame;
142 {
143   int fps, pict, sec, minute, hour, tc;
144
145   fps = (int)(frame_rate+0.5);
146   pict = frame%fps;
147   frame = (frame-pict)/fps;
148   sec = frame%60;
149   frame = (frame-sec)/60;
150   minute = frame%60;
151   frame = (frame-minute)/60;
152   hour = frame%24;
153   tc = (hour<<19) | (minute<<13) | (1<<12) | (sec<<6) | pict;
154
155   return tc;
156 }
157
158 /* generate picture header (6.2.3, 6.3.10) */
159 void putpicthdr()
160 {
161   alignbits();
162   putbits(PICTURE_START_CODE,32); /* picture_start_code */
163   calc_vbv_delay();
164   putbits(temp_ref,10); /* temporal_reference */
165   putbits(pict_type,3); /* picture_coding_type */
166   putbits(vbv_delay,16); /* vbv_delay */
167
168   if (pict_type==P_TYPE || pict_type==B_TYPE)
169   {
170     putbits(0,1); /* full_pel_forward_vector */
171     if (mpeg1)
172       putbits(forw_hor_f_code,3);
173     else
174       putbits(7,3); /* forward_f_code */
175   }
176
177   if (pict_type==B_TYPE)
178   {
179     putbits(0,1); /* full_pel_backward_vector */
180     if (mpeg1)
181       putbits(back_hor_f_code,3);
182     else
183       putbits(7,3); /* backward_f_code */
184   }
185
186   putbits(0,1); /* extra_bit_picture */
187 }
188
189 /* generate picture coding extension (6.2.3.1, 6.3.11)
190  *
191  * composite display information (v_axis etc.) not implemented
192  */
193 void putpictcodext()
194 {
195   alignbits();
196   putbits(EXT_START_CODE,32); /* extension_start_code */
197   putbits(CODING_ID,4); /* extension_start_code_identifier */
198   putbits(forw_hor_f_code,4); /* forward_horizontal_f_code */
199   putbits(forw_vert_f_code,4); /* forward_vertical_f_code */
200   putbits(back_hor_f_code,4); /* backward_horizontal_f_code */
201   putbits(back_vert_f_code,4); /* backward_vertical_f_code */
202   putbits(dc_prec,2); /* intra_dc_precision */
203   putbits(pict_struct,2); /* picture_structure */
204   putbits((pict_struct==FRAME_PICTURE)?topfirst:0,1); /* top_field_first */
205   putbits(frame_pred_dct,1); /* frame_pred_frame_dct */
206   putbits(0,1); /* concealment_motion_vectors  -- currently not implemented */
207   putbits(q_scale_type,1); /* q_scale_type */
208   putbits(intravlc,1); /* intra_vlc_format */
209   putbits(altscan,1); /* alternate_scan */
210   putbits(repeatfirst,1); /* repeat_first_field */
211   putbits(prog_frame,1); /* chroma_420_type */
212   putbits(prog_frame,1); /* progressive_frame */
213   putbits(0,1); /* composite_display_flag */
214 }
215
216 /* generate sequence_end_code (6.2.2) */
217 void putseqend()
218 {
219   alignbits();
220   putbits(SEQ_END_CODE,32);
221 }