Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / mpeg2enc / puthdr.c
blob31226ea4ed5742448f550f5ab7e0fe9fef9b3a90
1 /* puthdr.c, generation of headers */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
5 /*
6 * Disclaimer of Warranty
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.
16 * This disclaimer of warranty extends to the user of these programs and user's
17 * customers, employees, agents, transferees, successors, and assigns.
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.
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.
30 #include <stdio.h>
31 #include <math.h>
32 #include "config.h"
33 #include "global.h"
35 /* private prototypes */
36 static int frametotc _ANSI_ARGS_((int frame));
38 /* generate sequence header (6.2.2.1, 6.3.3)
40 * matrix download not implemented
42 void putseqhdr()
44 int i;
46 alignbits();
47 mpeg2enc_putbits(SEQ_START_CODE,32); /* sequence_header_code */
48 mpeg2enc_putbits(horizontal_size,12); /* horizontal_size_value */
49 mpeg2enc_putbits(vertical_size,12); /* vertical_size_value */
50 mpeg2enc_putbits(aspectratio,4); /* aspect_ratio_information */
51 mpeg2enc_putbits(frame_rate_code,4); /* frame_rate_code */
52 mpeg2enc_putbits((int)ceil(bit_rate/400.0),18); /* bit_rate_value */
53 mpeg2enc_putbits(1,1); /* marker_bit */
54 mpeg2enc_putbits(vbv_buffer_size,10); /* vbv_buffer_size_value */
55 mpeg2enc_putbits(constrparms,1); /* constrained_parameters_flag */
57 mpeg2enc_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 mpeg2enc_putbits(intra_q[mpeg2_zig_zag_scan[i]],8); /* intra_quantizer_matrix */
62 mpeg2enc_putbits(load_niquant,1); /* load_non_intra_quantizer_matrix */
63 if (load_niquant)
64 for (i=0; i<64; i++)
65 mpeg2enc_putbits(inter_q[mpeg2_zig_zag_scan[i]],8); /* non_intra_quantizer_matrix */
66 if (!mpeg1)
68 putseqext();
69 putseqdispext();
73 /* generate sequence extension (6.2.2.3, 6.3.5) header (MPEG-2 only) */
74 void putseqext()
76 alignbits();
77 mpeg2enc_putbits(EXT_START_CODE,32); /* extension_start_code */
78 mpeg2enc_putbits(SEQ_ID,4); /* extension_start_code_identifier */
79 mpeg2enc_putbits((profile<<4)|level,8); /* profile_and_level_indication */
80 mpeg2enc_putbits(prog_seq,1); /* progressive sequence */
81 mpeg2enc_putbits(chroma_format,2); /* chroma_format */
82 mpeg2enc_putbits(horizontal_size>>12,2); /* horizontal_size_extension */
83 mpeg2enc_putbits(vertical_size>>12,2); /* vertical_size_extension */
84 mpeg2enc_putbits(((int)ceil(bit_rate/400.0))>>18,12); /* bit_rate_extension */
85 mpeg2enc_putbits(1,1); /* marker_bit */
86 mpeg2enc_putbits(vbv_buffer_size>>10,8); /* vbv_buffer_size_extension */
87 mpeg2enc_putbits(0,1); /* low_delay -- currently not implemented */
88 mpeg2enc_putbits(0,2); /* frame_rate_extension_n */
89 mpeg2enc_putbits(0,5); /* frame_rate_extension_d */
92 /* generate sequence display extension (6.2.2.4, 6.3.6)
94 * content not yet user setable
96 void putseqdispext()
98 alignbits();
99 mpeg2enc_putbits(EXT_START_CODE,32); /* extension_start_code */
100 mpeg2enc_putbits(DISP_ID,4); /* extension_start_code_identifier */
101 mpeg2enc_putbits(video_format,3); /* video_format */
102 mpeg2enc_putbits(1,1); /* colour_description */
103 mpeg2enc_putbits(color_primaries,8); /* colour_primaries */
104 mpeg2enc_putbits(transfer_characteristics,8); /* transfer_characteristics */
105 mpeg2enc_putbits(matrix_coefficients,8); /* matrix_coefficients */
106 mpeg2enc_putbits(display_horizontal_size,14); /* display_horizontal_size */
107 mpeg2enc_putbits(1,1); /* marker_bit */
108 mpeg2enc_putbits(display_vertical_size,14); /* display_vertical_size */
111 /* output a zero terminated string as user data (6.2.2.2.2, 6.3.4.1)
113 * string must not emulate start codes
115 void putuserdata(userdata)
116 char *userdata;
118 alignbits();
119 mpeg2enc_putbits(USER_START_CODE,32); /* user_data_start_code */
120 while (*userdata)
121 mpeg2enc_putbits(*userdata++,8);
124 /* generate group of pictures header (6.2.2.6, 6.3.9)
126 * uses tc0 (timecode of first frame) and frame0 (number of first frame)
128 void putgophdr(frame,closed_gop)
129 int frame,closed_gop;
131 int tc;
133 alignbits();
134 mpeg2enc_putbits(GOP_START_CODE,32); /* group_start_code */
135 tc = frametotc(tc0+frame);
136 mpeg2enc_putbits(tc,25); /* time_code */
137 mpeg2enc_putbits(closed_gop,1); /* closed_gop */
138 mpeg2enc_putbits(0,1); /* broken_link */
141 /* convert frame number to time_code
143 * drop_frame not implemented
145 static int frametotc(frame)
146 int frame;
148 int fps, pict, sec, minute, hour, tc;
150 fps = (int)(frame_rate+0.5);
151 pict = frame%fps;
152 frame = (frame-pict)/fps;
153 sec = frame%60;
154 frame = (frame-sec)/60;
155 minute = frame%60;
156 frame = (frame-minute)/60;
157 hour = frame%24;
158 tc = (hour<<19) | (minute<<13) | (1<<12) | (sec<<6) | pict;
160 return tc;
163 /* generate picture header (6.2.3, 6.3.10) */
164 void putpicthdr(pict_data_s *picture)
166 alignbits();
167 mpeg2enc_putbits(PICTURE_START_CODE,32); /* picture_start_code */
168 mpeg2enc_putbits(picture->temp_ref,10); /* temporal_reference */
169 mpeg2enc_putbits(picture->pict_type,3); /* picture_coding_type */
170 mpeg2enc_putbits(picture->vbv_delay,16); /* vbv_delay */
171 //printf("putpicthdr %d %d %d\n", picture->temp_ref, picture->pict_type, picture->vbv_delay);
173 if (picture->pict_type==P_TYPE || picture->pict_type==B_TYPE)
175 mpeg2enc_putbits(0,1); /* full_pel_forward_vector */
176 if (mpeg1)
177 mpeg2enc_putbits(picture->forw_hor_f_code,3);
178 else
179 mpeg2enc_putbits(7,3); /* forward_f_code */
182 if (picture->pict_type==B_TYPE)
184 mpeg2enc_putbits(0,1); /* full_pel_backward_vector */
185 if (mpeg1)
186 mpeg2enc_putbits(picture->back_hor_f_code,3);
187 else
188 mpeg2enc_putbits(7,3); /* backward_f_code */
191 mpeg2enc_putbits(0,1); /* extra_bit_picture */
194 /* generate picture coding extension (6.2.3.1, 6.3.11)
196 * composite display information (v_axis etc.) not implemented
198 void putpictcodext(pict_data_s *picture)
200 alignbits();
201 mpeg2enc_putbits(EXT_START_CODE,32); /* extension_start_code */
202 mpeg2enc_putbits(CODING_ID,4); /* extension_start_code_identifier */
203 mpeg2enc_putbits(picture->forw_hor_f_code,4); /* forward_horizontal_f_code */
204 mpeg2enc_putbits(picture->forw_vert_f_code,4); /* forward_vertical_f_code */
205 mpeg2enc_putbits(picture->back_hor_f_code,4); /* backward_horizontal_f_code */
206 mpeg2enc_putbits(picture->back_vert_f_code,4); /* backward_vertical_f_code */
207 mpeg2enc_putbits(picture->dc_prec,2); /* intra_dc_precision */
208 mpeg2enc_putbits(picture->pict_struct,2); /* picture_structure */
209 mpeg2enc_putbits((picture->pict_struct==FRAME_PICTURE)?picture->topfirst:0,1); /* top_field_first */
210 mpeg2enc_putbits(picture->frame_pred_dct,1); /* frame_pred_frame_dct */
211 mpeg2enc_putbits(0,1); /* concealment_motion_vectors -- currently not implemented */
212 mpeg2enc_putbits(picture->q_scale_type,1); /* q_scale_type */
213 mpeg2enc_putbits(picture->intravlc,1); /* intra_vlc_format */
214 mpeg2enc_putbits(picture->altscan,1); /* alternate_scan_hv */
215 mpeg2enc_putbits(picture->repeatfirst,1); /* repeat_first_field */
216 mpeg2enc_putbits(picture->prog_frame,1); /* chroma_420_type */
217 mpeg2enc_putbits(picture->prog_frame,1); /* progressive_frame */
218 mpeg2enc_putbits(0,1); /* composite_display_flag */
221 /* generate sequence_end_code (6.2.2) */
222 void putseqend()
224 alignbits();
225 mpeg2enc_putbits(SEQ_END_CODE,32);