1 /* puthdr.c, generation of headers */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
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
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
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
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 */
57 putbits(load_iquant,1); /* load_intra_quantizer_matrix */
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 */
62 putbits(load_niquant,1); /* load_non_intra_quantizer_matrix */
65 putbits(inter_q[zig_zag_scan[i]],8); /* non_intra_quantizer_matrix */
68 /* generate sequence extension (6.2.2.3, 6.3.5) header (MPEG-2 only) */
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 */
87 /* generate sequence display extension (6.2.2.4, 6.3.6)
89 * content not yet user setable
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 */
106 /* output a zero terminated string as user data (6.2.2.2.2, 6.3.4.1)
108 * string must not emulate start codes
110 void putuserdata(userdata)
114 putbits(USER_START_CODE,32); /* user_data_start_code */
116 putbits(*userdata++,8);
119 /* generate group of pictures header (6.2.2.6, 6.3.9)
121 * uses tc0 (timecode of first frame) and frame0 (number of first frame)
123 void putgophdr(frame,closed_gop)
124 int frame,closed_gop;
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 */
136 /* convert frame number to time_code
138 * drop_frame not implemented
140 static int frametotc(frame)
143 int fps, pict, sec, minute, hour, tc;
145 fps = (int)(frame_rate+0.5);
147 frame = (frame-pict)/fps;
149 frame = (frame-sec)/60;
151 frame = (frame-minute)/60;
153 tc = (hour<<19) | (minute<<13) | (1<<12) | (sec<<6) | pict;
158 /* generate picture header (6.2.3, 6.3.10) */
162 putbits(PICTURE_START_CODE,32); /* picture_start_code */
164 putbits(temp_ref,10); /* temporal_reference */
165 putbits(pict_type,3); /* picture_coding_type */
166 putbits(vbv_delay,16); /* vbv_delay */
168 if (pict_type==P_TYPE || pict_type==B_TYPE)
170 putbits(0,1); /* full_pel_forward_vector */
172 putbits(forw_hor_f_code,3);
174 putbits(7,3); /* forward_f_code */
177 if (pict_type==B_TYPE)
179 putbits(0,1); /* full_pel_backward_vector */
181 putbits(back_hor_f_code,3);
183 putbits(7,3); /* backward_f_code */
186 putbits(0,1); /* extra_bit_picture */
189 /* generate picture coding extension (6.2.3.1, 6.3.11)
191 * composite display information (v_axis etc.) not implemented
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 */
216 /* generate sequence_end_code (6.2.2) */
220 putbits(SEQ_END_CODE,32);