]> git.sesse.net Git - x264/blob - encoder/set.c
print svn version number in SEI info and in CLI/VfW.
[x264] / encoder / set.c
1 /*****************************************************************************
2  * set: h264 encoder (SPS and PPS init and write)
3  *****************************************************************************
4  * Copyright (C) 2003 Laurent Aimar
5  * $Id: set.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_STDINT_H
25 #include <stdint.h>
26 #else
27 #include <inttypes.h>
28 #endif
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <math.h>
34
35 #include "x264.h"
36 #include "common/bs.h"
37 #include "common/set.h"
38 #include "config.h"
39
40 void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
41 {
42     sps->i_id               = i_id;
43
44     if( param->b_cabac || param->i_bframe > 0 )
45         sps->i_profile_idc      = PROFILE_MAIN;
46     else
47         sps->i_profile_idc      = PROFILE_BASELINE;
48     sps->i_level_idc = param->i_level_idc;
49
50     sps->b_constraint_set0  = 0;
51     sps->b_constraint_set1  = 0;
52     sps->b_constraint_set2  = 0;
53
54     sps->i_log2_max_frame_num = 4;  /* at least 4 */
55     while( (1 << sps->i_log2_max_frame_num) <= param->i_keyint_max )
56     {
57         sps->i_log2_max_frame_num++;
58     }
59     sps->i_log2_max_frame_num++;    /* just in case */
60
61     sps->i_poc_type = 0;
62     if( sps->i_poc_type == 0 )
63     {
64         sps->i_log2_max_poc_lsb = sps->i_log2_max_frame_num + 1;    /* max poc = 2*frame_num */
65     }
66     else if( sps->i_poc_type == 1 )
67     {
68         int i;
69
70         /* FIXME */
71         sps->b_delta_pic_order_always_zero = 1;
72         sps->i_offset_for_non_ref_pic = 0;
73         sps->i_offset_for_top_to_bottom_field = 0;
74         sps->i_num_ref_frames_in_poc_cycle = 0;
75
76         for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
77         {
78             sps->i_offset_for_ref_frame[i] = 0;
79         }
80     }
81
82     sps->i_num_ref_frames = param->i_frame_reference;
83     if( param->i_bframe )
84         sps->i_num_ref_frames++; /* for backwards ref in B */
85     if( param->b_bframe_pyramid )
86         sps->i_num_ref_frames++; /* for 2nd backwards ref */
87     sps->b_gaps_in_frame_num_value_allowed = 0;
88     sps->i_mb_width = ( param->i_width + 15 ) / 16;
89     sps->i_mb_height= ( param->i_height + 15 )/ 16;
90     sps->b_frame_mbs_only = 1;
91     sps->b_mb_adaptive_frame_field = 0;
92     sps->b_direct8x8_inference = 0;
93     if( sps->b_frame_mbs_only == 0 ||
94         !(param->analyse.inter & X264_ANALYSE_PSUB8x8) )
95     {
96         sps->b_direct8x8_inference = 1;
97     }
98
99     if( param->i_width % 16 != 0 || param->i_height % 16 != 0 )
100     {
101         sps->b_crop = 1;
102         sps->crop.i_left    = 0;
103         sps->crop.i_right   = ( 16 - param->i_width % 16)/2;
104         sps->crop.i_top     = 0;
105         sps->crop.i_bottom  = ( 16 - param->i_height % 16)/2;
106     }
107     else
108     {
109         sps->b_crop = 0;
110         sps->crop.i_left    = 0;
111         sps->crop.i_right   = 0;
112         sps->crop.i_top     = 0;
113         sps->crop.i_bottom  = 0;
114     }
115
116     sps->b_vui = 0;
117     sps->vui.b_aspect_ratio_info_present = 0;
118
119     if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
120     {
121         sps->vui.b_aspect_ratio_info_present = 1;
122         sps->vui.i_sar_width = param->vui.i_sar_width;
123         sps->vui.i_sar_height= param->vui.i_sar_height;
124     }
125     sps->b_vui |= sps->vui.b_aspect_ratio_info_present;
126
127     if( param->i_fps_num > 0 && param->i_fps_den > 0)
128     {
129         sps->vui.b_timing_info_present = 1;
130         /* The standard is confusing me, but this seems to work best
131            with other encoders */
132         sps->vui.i_num_units_in_tick = param->i_fps_den;
133         sps->vui.i_time_scale = param->i_fps_num;
134         sps->vui.b_fixed_frame_rate = 1;
135     }
136     sps->b_vui |= sps->vui.b_timing_info_present;
137
138     sps->vui.b_bitstream_restriction = param->i_bframe > 0;
139     if( sps->vui.b_bitstream_restriction )
140     {
141         sps->vui.b_motion_vectors_over_pic_boundaries = 1;
142         sps->vui.i_max_bytes_per_pic_denom = 0;
143         sps->vui.i_max_bits_per_mb_denom = 0;
144         sps->vui.i_log2_max_mv_length_horizontal =
145         sps->vui.i_log2_max_mv_length_vertical = (int)(log(param->analyse.i_mv_range*4-1)/log(2)) + 1;
146         sps->vui.i_num_reorder_frames = param->b_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
147         sps->vui.i_max_dec_frame_buffering = param->i_frame_reference + sps->vui.i_num_reorder_frames + 1;
148     }
149     sps->b_vui |= sps->vui.b_bitstream_restriction;
150 }
151
152
153 void x264_sps_write( bs_t *s, x264_sps_t *sps )
154 {
155     bs_write( s, 8, sps->i_profile_idc );
156     bs_write( s, 1, sps->b_constraint_set0 );
157     bs_write( s, 1, sps->b_constraint_set1 );
158     bs_write( s, 1, sps->b_constraint_set2 );
159
160     bs_write( s, 5, 0 );    /* reserved */
161
162     bs_write( s, 8, sps->i_level_idc );
163
164     bs_write_ue( s, sps->i_id );
165     bs_write_ue( s, sps->i_log2_max_frame_num - 4 );
166     bs_write_ue( s, sps->i_poc_type );
167     if( sps->i_poc_type == 0 )
168     {
169         bs_write_ue( s, sps->i_log2_max_poc_lsb - 4 );
170     }
171     else if( sps->i_poc_type == 1 )
172     {
173         int i;
174
175         bs_write( s, 1, sps->b_delta_pic_order_always_zero );
176         bs_write_se( s, sps->i_offset_for_non_ref_pic );
177         bs_write_se( s, sps->i_offset_for_top_to_bottom_field );
178         bs_write_ue( s, sps->i_num_ref_frames_in_poc_cycle );
179
180         for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
181         {
182             bs_write_se( s, sps->i_offset_for_ref_frame[i] );
183         }
184     }
185     bs_write_ue( s, sps->i_num_ref_frames );
186     bs_write( s, 1, sps->b_gaps_in_frame_num_value_allowed );
187     bs_write_ue( s, sps->i_mb_width - 1 );
188     bs_write_ue( s, sps->i_mb_height - 1);
189     bs_write( s, 1, sps->b_frame_mbs_only );
190     if( !sps->b_frame_mbs_only )
191     {
192         bs_write( s, 1, sps->b_mb_adaptive_frame_field );
193     }
194     bs_write( s, 1, sps->b_direct8x8_inference );
195
196     bs_write( s, 1, sps->b_crop );
197     if( sps->b_crop )
198     {
199         bs_write_ue( s, sps->crop.i_left );
200         bs_write_ue( s, sps->crop.i_right );
201         bs_write_ue( s, sps->crop.i_top );
202         bs_write_ue( s, sps->crop.i_bottom );
203     }
204
205     bs_write( s, 1, sps->b_vui );
206     if( sps->b_vui )
207     {
208         bs_write1( s, sps->vui.b_aspect_ratio_info_present );
209         if( sps->vui.b_aspect_ratio_info_present )
210         {
211             int i;
212             static const struct { int w, h; int sar; } sar[] =
213             {
214                 { 1,   1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
215                 { 40, 33, 5 }, { 24, 11, 6 }, { 20, 11, 7 }, { 32, 11, 8 },
216                 { 80, 33, 9 }, { 18, 11, 10}, { 15, 11, 11}, { 64, 33, 12},
217                 { 160,99, 13}, { 0, 0, -1 }
218             };
219             for( i = 0; sar[i].sar != -1; i++ )
220             {
221                 if( sar[i].w == sps->vui.i_sar_width &&
222                     sar[i].h == sps->vui.i_sar_height )
223                     break;
224             }
225             if( sar[i].sar != -1 )
226             {
227                 bs_write( s, 8, sar[i].sar );
228             }
229             else
230             {
231                 bs_write( s, 8, 255);   /* aspect_ratio_idc (extented) */
232                 bs_write( s, 16, sps->vui.i_sar_width );
233                 bs_write( s, 16, sps->vui.i_sar_height );
234             }
235         }
236
237         bs_write1( s, 0 );      /* overscan_info_present_flag */
238
239         bs_write1( s, 0 );      /* video_signal_type_present_flag */
240 #if 0
241         bs_write( s, 3, 5 );    /* unspecified video format */
242         bs_write1( s, 1 );      /* video full range flag */
243         bs_write1( s, 0 );      /* colour description present flag */
244 #endif
245         bs_write1( s, 0 );      /* chroma_loc_info_present_flag */
246
247         bs_write1( s, sps->vui.b_timing_info_present );
248         if( sps->vui.b_timing_info_present )
249         {
250             bs_write( s, 32, sps->vui.i_num_units_in_tick );
251             bs_write( s, 32, sps->vui.i_time_scale );
252             bs_write1( s, sps->vui.b_fixed_frame_rate );
253         }
254
255         bs_write1( s, 0 );      /* nal_hrd_parameters_present_flag */
256         bs_write1( s, 0 );      /* vcl_hrd_parameters_present_flag */
257         bs_write1( s, 0 );      /* pic_struct_present_flag */
258         bs_write1( s, sps->vui.b_bitstream_restriction );
259         if( sps->vui.b_bitstream_restriction )
260         {
261             bs_write1( s, sps->vui.b_motion_vectors_over_pic_boundaries );
262             bs_write_ue( s, sps->vui.i_max_bytes_per_pic_denom );
263             bs_write_ue( s, sps->vui.i_max_bits_per_mb_denom );
264             bs_write_ue( s, sps->vui.i_log2_max_mv_length_horizontal );
265             bs_write_ue( s, sps->vui.i_log2_max_mv_length_vertical );
266             bs_write_ue( s, sps->vui.i_num_reorder_frames );
267             bs_write_ue( s, sps->vui.i_max_dec_frame_buffering );
268         }
269     }
270
271     bs_rbsp_trailing( s );
272 }
273
274 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
275 {
276     pps->i_id = i_id;
277     pps->i_sps_id = sps->i_id;
278     pps->b_cabac = param->b_cabac;
279
280     pps->b_pic_order = 0;
281     pps->i_num_slice_groups = 1;
282
283     if( pps->i_num_slice_groups > 1 )
284     {
285         int i;
286
287         pps->i_slice_group_map_type = 0;
288         if( pps->i_slice_group_map_type == 0 )
289         {
290             for( i = 0; i < pps->i_num_slice_groups; i++ )
291             {
292                 pps->i_run_length[i] = 1;
293             }
294         }
295         else if( pps->i_slice_group_map_type == 2 )
296         {
297             for( i = 0; i < pps->i_num_slice_groups; i++ )
298             {
299                 pps->i_top_left[i] = 0;
300                 pps->i_bottom_right[i] = 0;
301             }
302         }
303         else if( pps->i_slice_group_map_type >= 3 &&
304                  pps->i_slice_group_map_type <= 5 )
305         {
306             pps->b_slice_group_change_direction = 0;
307             pps->i_slice_group_change_rate = 0;
308         }
309         else if( pps->i_slice_group_map_type == 6 )
310         {
311             pps->i_pic_size_in_map_units = 1;
312             for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
313             {
314                 pps->i_slice_group_id[i] = 0;
315             }
316         }
317     }
318     pps->i_num_ref_idx_l0_active = 1;
319     pps->i_num_ref_idx_l1_active = 1;
320
321     pps->b_weighted_pred = 0;
322     pps->b_weighted_bipred = param->analyse.b_weighted_bipred ? 2 : 0;
323
324     pps->i_pic_init_qp = 26;
325     pps->i_pic_init_qs = 26;
326
327     pps->i_chroma_qp_index_offset = param->analyse.i_chroma_qp_offset;
328     pps->b_deblocking_filter_control = 1;
329     pps->b_constrained_intra_pred = 0;
330     pps->b_redundant_pic_cnt = 0;
331 }
332
333 void x264_pps_write( bs_t *s, x264_pps_t *pps )
334 {
335     bs_write_ue( s, pps->i_id );
336     bs_write_ue( s, pps->i_sps_id );
337
338     bs_write( s, 1, pps->b_cabac );
339     bs_write( s, 1, pps->b_pic_order );
340     bs_write_ue( s, pps->i_num_slice_groups - 1 );
341
342     if( pps->i_num_slice_groups > 1 )
343     {
344         int i;
345
346         bs_write_ue( s, pps->i_slice_group_map_type );
347         if( pps->i_slice_group_map_type == 0 )
348         {
349             for( i = 0; i < pps->i_num_slice_groups; i++ )
350             {
351                 bs_write_ue( s, pps->i_run_length[i] - 1 );
352             }
353         }
354         else if( pps->i_slice_group_map_type == 2 )
355         {
356             for( i = 0; i < pps->i_num_slice_groups; i++ )
357             {
358                 bs_write_ue( s, pps->i_top_left[i] );
359                 bs_write_ue( s, pps->i_bottom_right[i] );
360             }
361         }
362         else if( pps->i_slice_group_map_type >= 3 &&
363                  pps->i_slice_group_map_type <= 5 )
364         {
365             bs_write( s, 1, pps->b_slice_group_change_direction );
366             bs_write_ue( s, pps->b_slice_group_change_direction - 1 );
367         }
368         else if( pps->i_slice_group_map_type == 6 )
369         {
370             bs_write_ue( s, pps->i_pic_size_in_map_units - 1 );
371             for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
372             {
373                 /* FIXME */
374                 /* bs_write( s, ceil( log2( pps->i_pic_size_in_map_units +1 ) ),
375                  *              pps->i_slice_group_id[i] );
376                  */
377             }
378         }
379     }
380
381     bs_write_ue( s, pps->i_num_ref_idx_l0_active - 1 );
382     bs_write_ue( s, pps->i_num_ref_idx_l1_active - 1 );
383     bs_write( s, 1, pps->b_weighted_pred );
384     bs_write( s, 2, pps->b_weighted_bipred );
385
386     bs_write_se( s, pps->i_pic_init_qp - 26 );
387     bs_write_se( s, pps->i_pic_init_qs - 26 );
388     bs_write_se( s, pps->i_chroma_qp_index_offset );
389
390     bs_write( s, 1, pps->b_deblocking_filter_control );
391     bs_write( s, 1, pps->b_constrained_intra_pred );
392     bs_write( s, 1, pps->b_redundant_pic_cnt );
393
394     bs_rbsp_trailing( s );
395 }
396
397 void x264_sei_version_write( bs_t *s )
398 {
399     int i;
400     // random ID number generated according to ISO-11578
401     const uint8_t uuid[16] = {
402         0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7,
403         0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef
404     };
405     char version[256];
406     int length;
407     sprintf( version, "x264 - core %d%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html",
408              X264_BUILD, X264_VERSION );
409     length = strlen(version)+1+16;
410
411     bs_write( s, 8, 0x5 ); // payload_type = user_data_unregistered
412     while( length > 255 )
413         bs_write( s, 8, 255 ), length -= 255;
414     bs_write( s, 8, length ); // payload_size
415
416     for( i = 0; i < 16; i++ )
417         bs_write( s, 8, uuid[i] );
418     for( i = 0; i < length-16; i++ )
419         bs_write( s, 8, version[i] );
420
421     bs_rbsp_trailing( s );
422 }