]> git.sesse.net Git - x264/blob - encoder/set.c
simplify calvc mb type
[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
34 #include "../x264.h"
35 #include "../common/bs.h"
36 #include "../common/set.h"
37
38 void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
39 {
40     sps->i_id               = i_id;
41
42     if( param->b_cabac || param->i_bframe > 0 )
43         sps->i_profile_idc      = PROFILE_MAIN;
44     else
45         sps->i_profile_idc      = PROFILE_BASELINE;
46
47     sps->i_level_idc        = 21;               /* FIXME ? */
48     sps->b_constraint_set0  = 0;
49     sps->b_constraint_set1  = 0;
50     sps->b_constraint_set2  = 0;
51
52     sps->i_log2_max_frame_num = 4;  /* at least 4 */
53     while( (1 << sps->i_log2_max_frame_num) <= param->i_idrframe * param->i_iframe )
54     {
55         sps->i_log2_max_frame_num++;
56     }
57     sps->i_log2_max_frame_num++;    /* just in case */
58
59     sps->i_poc_type = 0;
60     if( sps->i_poc_type == 0 )
61     {
62         sps->i_log2_max_poc_lsb = sps->i_log2_max_frame_num + 1;    /* max poc = 2*frame_num */
63     }
64     else if( sps->i_poc_type == 1 )
65     {
66         int i;
67
68         /* FIXME */
69         sps->b_delta_pic_order_always_zero = 1;
70         sps->i_offset_for_non_ref_pic = 0;
71         sps->i_offset_for_top_to_bottom_field = 0;
72         sps->i_num_ref_frames_in_poc_cycle = 0;
73
74         for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
75         {
76             sps->i_offset_for_ref_frame[i] = 0;
77         }
78     }
79
80     sps->i_num_ref_frames = param->i_frame_reference + 1; /* +1 for 2 ref in B */
81     sps->b_gaps_in_frame_num_value_allowed = 0;
82     sps->i_mb_width = ( param->i_width + 15 ) / 16;
83     sps->i_mb_height= ( param->i_height + 15 )/ 16;
84     sps->b_frame_mbs_only = 1;
85     sps->b_mb_adaptive_frame_field = 0;
86     sps->b_direct8x8_inference = 0;
87     if( sps->b_frame_mbs_only == 0 ||
88         !(param->analyse.inter & X264_ANALYSE_PSUB8x8) )
89     {
90         sps->b_direct8x8_inference = 1;
91     }
92
93     if( param->i_width % 16 != 0 || param->i_height % 16 != 0 )
94     {
95         sps->b_crop = 1;
96         sps->crop.i_left    = 0;
97         sps->crop.i_right   = ( 16 - param->i_width % 16)/2;
98         sps->crop.i_top     = 0;
99         sps->crop.i_bottom  = ( 16 - param->i_height % 16)/2;
100     }
101     else
102     {
103         sps->b_crop = 0;
104         sps->crop.i_left    = 0;
105         sps->crop.i_right   = 0;
106         sps->crop.i_top     = 0;
107         sps->crop.i_bottom  = 0;
108     }
109
110     sps->b_vui = 0;
111     sps->vui.b_aspect_ratio_info_present = 0;
112
113     if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
114     {
115         sps->vui.b_aspect_ratio_info_present = 1;
116         sps->vui.i_sar_width = param->vui.i_sar_width;
117         sps->vui.i_sar_height= param->vui.i_sar_height;
118     }
119     sps->b_vui |= sps->vui.b_aspect_ratio_info_present;
120
121     if( param->i_fps_num > 0 && param->i_fps_den > 0)
122     {
123         sps->vui.b_timing_info_present = 1;
124         /* The standard is confusing me, but this seems to work best
125            with other encoders */
126         sps->vui.i_num_units_in_tick = param->i_fps_den;
127         sps->vui.i_time_scale = param->i_fps_num;
128         sps->vui.b_fixed_frame_rate = 1;
129     }
130     sps->b_vui |= sps->vui.b_timing_info_present;
131 }
132
133
134 void x264_sps_write( bs_t *s, x264_sps_t *sps )
135 {
136     bs_write( s, 8, sps->i_profile_idc );
137     bs_write( s, 1, sps->b_constraint_set0 );
138     bs_write( s, 1, sps->b_constraint_set1 );
139     bs_write( s, 1, sps->b_constraint_set2 );
140
141     bs_write( s, 5, 0 );    /* reserved */
142
143     bs_write( s, 8, sps->i_level_idc );
144
145     bs_write_ue( s, sps->i_id );
146     bs_write_ue( s, sps->i_log2_max_frame_num - 4 );
147     bs_write_ue( s, sps->i_poc_type );
148     if( sps->i_poc_type == 0 )
149     {
150         bs_write_ue( s, sps->i_log2_max_poc_lsb - 4 );
151     }
152     else if( sps->i_poc_type == 1 )
153     {
154         int i;
155
156         bs_write( s, 1, sps->b_delta_pic_order_always_zero );
157         bs_write_se( s, sps->i_offset_for_non_ref_pic );
158         bs_write_se( s, sps->i_offset_for_top_to_bottom_field );
159         bs_write_ue( s, sps->i_num_ref_frames_in_poc_cycle );
160
161         for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
162         {
163             bs_write_se( s, sps->i_offset_for_ref_frame[i] );
164         }
165     }
166     bs_write_ue( s, sps->i_num_ref_frames );
167     bs_write( s, 1, sps->b_gaps_in_frame_num_value_allowed );
168     bs_write_ue( s, sps->i_mb_width - 1 );
169     bs_write_ue( s, sps->i_mb_height - 1);
170     bs_write( s, 1, sps->b_frame_mbs_only );
171     if( !sps->b_frame_mbs_only )
172     {
173         bs_write( s, 1, sps->b_mb_adaptive_frame_field );
174     }
175     bs_write( s, 1, sps->b_direct8x8_inference );
176
177     bs_write( s, 1, sps->b_crop );
178     if( sps->b_crop )
179     {
180         bs_write_ue( s, sps->crop.i_left );
181         bs_write_ue( s, sps->crop.i_right );
182         bs_write_ue( s, sps->crop.i_top );
183         bs_write_ue( s, sps->crop.i_bottom );
184     }
185
186     bs_write( s, 1, sps->b_vui );
187     if( sps->b_vui )
188     {
189         bs_write1( s, sps->vui.b_aspect_ratio_info_present );
190         if( sps->vui.b_aspect_ratio_info_present )
191         {
192             int i;
193             static const struct { int w, h; int sar; } sar[] =
194             {
195                 { 1,   1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
196                 { 40, 33, 5 }, { 24, 11, 6 }, { 20, 11, 7 }, { 32, 11, 8 },
197                 { 80, 33, 9 }, { 18, 11, 10}, { 15, 11, 11}, { 64, 33, 12},
198                 { 160,99, 13}, { 0, 0, -1 }
199             };
200             for( i = 0; sar[i].sar != -1; i++ )
201             {
202                 if( sar[i].w == sps->vui.i_sar_width &&
203                     sar[i].h == sps->vui.i_sar_height )
204                     break;
205             }
206             if( sar[i].sar != -1 )
207             {
208                 bs_write( s, 8, sar[i].sar );
209             }
210             else
211             {
212                 bs_write( s, 8, 255);   /* aspect_ration_idc (extented) */
213                 bs_write( s, 16, sps->vui.i_sar_width );
214                 bs_write( s, 16, sps->vui.i_sar_height );
215             }
216         }
217
218         bs_write1( s, 0 );      /* overscan_info_present_flag */
219
220         bs_write1( s, 0 );      /* video_signal_type_present_flag */
221 #if 0
222         bs_write( s, 3, 5 );    /* unspecified video format */
223         bs_write1( s, 1 );      /* video full range flag */
224         bs_write1( s, 0 );      /* colour description present flag */
225 #endif
226         bs_write1( s, 0 );      /* chroma_loc_info_present_flag */
227
228         bs_write1( s, sps->vui.b_timing_info_present );
229         if( sps->vui.b_timing_info_present )
230         {
231             bs_write( s, 32, sps->vui.i_num_units_in_tick );
232             bs_write( s, 32, sps->vui.i_time_scale );
233             bs_write1( s, sps->vui.b_fixed_frame_rate );
234         }
235
236         bs_write1( s, 0 );      /* nal_hrd_parameters_present_flag */
237         bs_write1( s, 0 );      /* vcl_hrd_parameters_present_flag */
238         bs_write1( s, 0 );      /* pic_struct_present_flag */
239         bs_write1( s, 0 );      /* bitstream_restriction_flag */
240     }
241
242     bs_rbsp_trailing( s );
243 }
244
245 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
246 {
247     pps->i_id = i_id;
248     pps->i_sps_id = sps->i_id;
249     pps->b_cabac = param->b_cabac;
250
251     pps->b_pic_order = 0;
252     pps->i_num_slice_groups = 1;
253
254     if( pps->i_num_slice_groups > 1 )
255     {
256         int i;
257
258         pps->i_slice_group_map_type = 0;
259         if( pps->i_slice_group_map_type == 0 )
260         {
261             for( i = 0; i < pps->i_num_slice_groups; i++ )
262             {
263                 pps->i_run_length[i] = 1;
264             }
265         }
266         else if( pps->i_slice_group_map_type == 2 )
267         {
268             for( i = 0; i < pps->i_num_slice_groups; i++ )
269             {
270                 pps->i_top_left[i] = 0;
271                 pps->i_bottom_right[i] = 0;
272             }
273         }
274         else if( pps->i_slice_group_map_type >= 3 &&
275                  pps->i_slice_group_map_type <= 5 )
276         {
277             pps->b_slice_group_change_direction = 0;
278             pps->i_slice_group_change_rate = 0;
279         }
280         else if( pps->i_slice_group_map_type == 6 )
281         {
282             pps->i_pic_size_in_map_units = 1;
283             for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
284             {
285                 pps->i_slice_group_id[i] = 0;
286             }
287         }
288     }
289     pps->i_num_ref_idx_l0_active = 1;
290     pps->i_num_ref_idx_l1_active = 1;
291
292     pps->b_weighted_pred = 0;
293     pps->b_weighted_bipred = 0;
294
295     pps->i_pic_init_qp = 26;
296     pps->i_pic_init_qs = 26;
297
298     pps->i_chroma_qp_index_offset = 0;
299 #if 0
300     if( !param->b_deblocking_filter )
301     {
302         pps->b_deblocking_filter_control = 1;
303     }
304     else
305     {
306         pps->b_deblocking_filter_control = 1;
307     }
308 #endif
309     pps->b_deblocking_filter_control = 1;
310     pps->b_constrained_intra_pred = 0;
311     pps->b_redundant_pic_cnt = 0;
312 }
313
314 void x264_pps_write( bs_t *s, x264_pps_t *pps )
315 {
316     bs_write_ue( s, pps->i_id );
317     bs_write_ue( s, pps->i_sps_id );
318
319     bs_write( s, 1, pps->b_cabac );
320     bs_write( s, 1, pps->b_pic_order );
321     bs_write_ue( s, pps->i_num_slice_groups - 1 );
322
323     if( pps->i_num_slice_groups > 1 )
324     {
325         int i;
326
327         bs_write_ue( s, pps->i_slice_group_map_type );
328         if( pps->i_slice_group_map_type == 0 )
329         {
330             for( i = 0; i < pps->i_num_slice_groups; i++ )
331             {
332                 bs_write_ue( s, pps->i_run_length[i] - 1 );
333             }
334         }
335         else if( pps->i_slice_group_map_type == 2 )
336         {
337             for( i = 0; i < pps->i_num_slice_groups; i++ )
338             {
339                 bs_write_ue( s, pps->i_top_left[i] );
340                 bs_write_ue( s, pps->i_bottom_right[i] );
341             }
342         }
343         else if( pps->i_slice_group_map_type >= 3 &&
344                  pps->i_slice_group_map_type <= 5 )
345         {
346             bs_write( s, 1, pps->b_slice_group_change_direction );
347             bs_write_ue( s, pps->b_slice_group_change_direction - 1 );
348         }
349         else if( pps->i_slice_group_map_type == 6 )
350         {
351             bs_write_ue( s, pps->i_pic_size_in_map_units - 1 );
352             for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
353             {
354                 /* FIXME */
355                 /* bs_write( s, ceil( log2( pps->i_pic_size_in_map_units +1 ) ),
356                  *              pps->i_slice_group_id[i] );
357                  */
358             }
359         }
360     }
361
362     bs_write_ue( s, pps->i_num_ref_idx_l0_active - 1 );
363     bs_write_ue( s, pps->i_num_ref_idx_l1_active - 1 );
364     bs_write( s, 1, pps->b_weighted_pred );
365     bs_write( s, 2, pps->b_weighted_bipred );
366
367     bs_write_se( s, pps->i_pic_init_qp - 26 );
368     bs_write_se( s, pps->i_pic_init_qs - 26 );
369     bs_write_se( s, pps->i_chroma_qp_index_offset );
370
371     bs_write( s, 1, pps->b_deblocking_filter_control );
372     bs_write( s, 1, pps->b_constrained_intra_pred );
373     bs_write( s, 1, pps->b_redundant_pic_cnt );
374
375     bs_rbsp_trailing( s );
376 }
377