]> git.sesse.net Git - x264/blob - encoder/set.c
* all: added a x264_param_t.analyse.b_psnr
[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 "../core/bs.h"
36 #include "../core/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     {
89         sps->b_direct8x8_inference = 1;
90     }
91
92     if( param->i_width % 16 != 0 || param->i_height % 16 != 0 )
93     {
94         sps->b_crop = 1;
95         sps->crop.i_left    = 0;
96         sps->crop.i_right   = ( 16 - param->i_width % 16)/2;
97         sps->crop.i_top     = 0;
98         sps->crop.i_bottom  = ( 16 - param->i_height % 16)/2;
99     }
100     else
101     {
102         sps->b_crop = 0;
103         sps->crop.i_left    = 0;
104         sps->crop.i_right   = 0;
105         sps->crop.i_top     = 0;
106         sps->crop.i_bottom  = 0;
107     }
108
109     sps->b_vui = 0;
110
111     if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
112     {
113         int w = param->vui.i_sar_width;
114         int h = param->vui.i_sar_height;
115         int a = w, b = h;
116
117         while( b != 0 )
118         {
119             int t = a;
120
121             a = b;
122             b = t % b;
123         }
124
125         w /= a;
126         h /= a;
127         while( w > 65535 || h > 65535 )
128         {
129             w /= 2;
130             h /= 2;
131         }
132
133         if( w == 0 || h == 0 )
134         {
135             fprintf( stderr, "x264: cannot create valid sample aspect ratio\n" );
136             sps->vui.b_aspect_ratio_info_present = 0;
137         }
138         else if( w == h )
139         {
140             fprintf( stderr, "x264: no need for a SAR\n" );
141             sps->vui.b_aspect_ratio_info_present = 0;
142         }
143         else
144         {
145             fprintf( stderr, "x264: using SAR=%d/%d\n", w, h );
146             sps->vui.b_aspect_ratio_info_present = 1;
147             sps->vui.i_sar_width = w;
148             sps->vui.i_sar_height= h;
149         }
150     }
151     sps->b_vui |= sps->vui.b_aspect_ratio_info_present;
152
153     if( param->i_fps_num > 0 && param->i_fps_den > 0)
154     {
155         sps->vui.b_timing_info_present = 1;
156         /* The standard is confusing me, but this seems to work best
157            with other encoders */
158         sps->vui.i_num_units_in_tick = param->i_fps_den;
159         sps->vui.i_time_scale = param->i_fps_num;
160         sps->vui.b_fixed_frame_rate = 1;
161     }
162     sps->b_vui |= sps->vui.b_timing_info_present;
163 }
164
165
166 void x264_sps_write( bs_t *s, x264_sps_t *sps )
167 {
168     bs_write( s, 8, sps->i_profile_idc );
169     bs_write( s, 1, sps->b_constraint_set0 );
170     bs_write( s, 1, sps->b_constraint_set1 );
171     bs_write( s, 1, sps->b_constraint_set2 );
172
173     bs_write( s, 5, 0 );    /* reserved */
174
175     bs_write( s, 8, sps->i_level_idc );
176
177     bs_write_ue( s, sps->i_id );
178     bs_write_ue( s, sps->i_log2_max_frame_num - 4 );
179     bs_write_ue( s, sps->i_poc_type );
180     if( sps->i_poc_type == 0 )
181     {
182         bs_write_ue( s, sps->i_log2_max_poc_lsb - 4 );
183     }
184     else if( sps->i_poc_type == 1 )
185     {
186         int i;
187
188         bs_write( s, 1, sps->b_delta_pic_order_always_zero );
189         bs_write_se( s, sps->i_offset_for_non_ref_pic );
190         bs_write_se( s, sps->i_offset_for_top_to_bottom_field );
191         bs_write_ue( s, sps->i_num_ref_frames_in_poc_cycle );
192
193         for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
194         {
195             bs_write_se( s, sps->i_offset_for_ref_frame[i] );
196         }
197     }
198     bs_write_ue( s, sps->i_num_ref_frames );
199     bs_write( s, 1, sps->b_gaps_in_frame_num_value_allowed );
200     bs_write_ue( s, sps->i_mb_width - 1 );
201     bs_write_ue( s, sps->i_mb_height - 1);
202     bs_write( s, 1, sps->b_frame_mbs_only );
203     if( !sps->b_frame_mbs_only )
204     {
205         bs_write( s, 1, sps->b_mb_adaptive_frame_field );
206     }
207     bs_write( s, 1, sps->b_direct8x8_inference );
208
209     bs_write( s, 1, sps->b_crop );
210     if( sps->b_crop )
211     {
212         bs_write_ue( s, sps->crop.i_left );
213         bs_write_ue( s, sps->crop.i_right );
214         bs_write_ue( s, sps->crop.i_top );
215         bs_write_ue( s, sps->crop.i_bottom );
216     }
217
218     bs_write( s, 1, sps->b_vui );
219     if( sps->b_vui )
220     {
221         bs_write1( s, sps->vui.b_aspect_ratio_info_present );
222         if( sps->vui.b_aspect_ratio_info_present )
223         {
224             int i;
225             static const struct { int w, h; int sar; } sar[] =
226             {
227                 { 1,   1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
228                 { 40, 33, 5 }, { 24, 11, 6 }, { 20, 11, 7 }, { 32, 11, 8 },
229                 { 80, 33, 9 }, { 18, 11, 10}, { 15, 11, 11}, { 64, 33, 12},
230                 { 160,99, 13}, { 0, 0, -1 }
231             };
232             for( i = 0; sar[i].sar != -1; i++ )
233             {
234                 if( sar[i].w == sps->vui.i_sar_width &&
235                     sar[i].h == sps->vui.i_sar_height )
236                     break;
237             }
238             if( sar[i].sar != -1 )
239             {
240                 bs_write( s, 8, sar[i].sar );
241             }
242             else
243             {
244                 bs_write( s, 8, 255);   /* aspect_ration_idc (extented) */
245                 bs_write( s, 16, sps->vui.i_sar_width );
246                 bs_write( s, 16, sps->vui.i_sar_height );
247             }
248         }
249
250         bs_write1( s, 0 );      /* overscan_info_present_flag */
251
252         bs_write1( s, 0 );      /* video_signal_type_present_flag */
253 #if 0
254         bs_write( s, 3, 5 );    /* unspecified video format */
255         bs_write1( s, 1 );      /* video full range flag */
256         bs_write1( s, 0 );      /* colour description present flag */
257 #endif
258         bs_write1( s, 0 );      /* chroma_loc_info_present_flag */
259
260         bs_write1( s, sps->vui.b_timing_info_present );
261         if( sps->vui.b_timing_info_present )
262         {
263             bs_write( s, 32, sps->vui.i_num_units_in_tick );
264             bs_write( s, 32, sps->vui.i_time_scale );
265             bs_write1( s, sps->vui.b_fixed_frame_rate );
266         }
267
268         bs_write1( s, 0 );      /* nal_hrd_parameters_present_flag */
269         bs_write1( s, 0 );      /* vcl_hrd_parameters_present_flag */
270         bs_write1( s, 0 );      /* pic_struct_present_flag */
271         bs_write1( s, 0 );      /* bitstream_restriction_flag */
272     }
273
274     bs_rbsp_trailing( s );
275 }
276
277 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
278 {
279     pps->i_id = i_id;
280     pps->i_sps_id = sps->i_id;
281     pps->b_cabac = param->b_cabac;
282
283     pps->b_pic_order = 0;
284     pps->i_num_slice_groups = 1;
285
286     if( pps->i_num_slice_groups > 1 )
287     {
288         int i;
289
290         pps->i_slice_group_map_type = 0;
291         if( pps->i_slice_group_map_type == 0 )
292         {
293             for( i = 0; i < pps->i_num_slice_groups; i++ )
294             {
295                 pps->i_run_length[i] = 1;
296             }
297         }
298         else if( pps->i_slice_group_map_type == 2 )
299         {
300             for( i = 0; i < pps->i_num_slice_groups; i++ )
301             {
302                 pps->i_top_left[i] = 0;
303                 pps->i_bottom_right[i] = 0;
304             }
305         }
306         else if( pps->i_slice_group_map_type >= 3 &&
307                  pps->i_slice_group_map_type <= 5 )
308         {
309             pps->b_slice_group_change_direction = 0;
310             pps->i_slice_group_change_rate = 0;
311         }
312         else if( pps->i_slice_group_map_type == 6 )
313         {
314             pps->i_pic_size_in_map_units = 1;
315             for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
316             {
317                 pps->i_slice_group_id[i] = 0;
318             }
319         }
320     }
321     pps->i_num_ref_idx_l0_active = 1;
322     pps->i_num_ref_idx_l1_active = 1;
323
324     pps->b_weighted_pred = 0;
325     pps->b_weighted_bipred = 0;
326
327     pps->i_pic_init_qp = 26;
328     pps->i_pic_init_qs = 26;
329
330     pps->i_chroma_qp_index_offset = 0;
331 #if 0
332     if( !param->b_deblocking_filter )
333     {
334         pps->b_deblocking_filter_control = 1;
335     }
336     else
337     {
338         pps->b_deblocking_filter_control = 1;
339     }
340 #endif
341     pps->b_deblocking_filter_control = 1;
342     pps->b_constrained_intra_pred = 0;
343     pps->b_redundant_pic_cnt = 0;
344 }
345
346 void x264_pps_write( bs_t *s, x264_pps_t *pps )
347 {
348     bs_write_ue( s, pps->i_id );
349     bs_write_ue( s, pps->i_sps_id );
350
351     bs_write( s, 1, pps->b_cabac );
352     bs_write( s, 1, pps->b_pic_order );
353     bs_write_ue( s, pps->i_num_slice_groups - 1 );
354
355     if( pps->i_num_slice_groups > 1 )
356     {
357         int i;
358
359         bs_write_ue( s, pps->i_slice_group_map_type );
360         if( pps->i_slice_group_map_type == 0 )
361         {
362             for( i = 0; i < pps->i_num_slice_groups; i++ )
363             {
364                 bs_write_ue( s, pps->i_run_length[i] - 1 );
365             }
366         }
367         else if( pps->i_slice_group_map_type == 2 )
368         {
369             for( i = 0; i < pps->i_num_slice_groups; i++ )
370             {
371                 bs_write_ue( s, pps->i_top_left[i] );
372                 bs_write_ue( s, pps->i_bottom_right[i] );
373             }
374         }
375         else if( pps->i_slice_group_map_type >= 3 &&
376                  pps->i_slice_group_map_type <= 5 )
377         {
378             bs_write( s, 1, pps->b_slice_group_change_direction );
379             bs_write_ue( s, pps->b_slice_group_change_direction - 1 );
380         }
381         else if( pps->i_slice_group_map_type == 6 )
382         {
383             bs_write_ue( s, pps->i_pic_size_in_map_units - 1 );
384             for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
385             {
386                 /* FIXME */
387                 /* bs_write( s, ceil( log2( pps->i_pic_size_in_map_units +1 ) ),
388                  *              pps->i_slice_group_id[i] );
389                  */
390             }
391         }
392     }
393
394     bs_write_ue( s, pps->i_num_ref_idx_l0_active - 1 );
395     bs_write_ue( s, pps->i_num_ref_idx_l1_active - 1 );
396     bs_write( s, 1, pps->b_weighted_pred );
397     bs_write( s, 2, pps->b_weighted_bipred );
398
399     bs_write_se( s, pps->i_pic_init_qp - 26 );
400     bs_write_se( s, pps->i_pic_init_qs - 26 );
401     bs_write_se( s, pps->i_chroma_qp_index_offset );
402
403     bs_write( s, 1, pps->b_deblocking_filter_control );
404     bs_write( s, 1, pps->b_constrained_intra_pred );
405     bs_write( s, 1, pps->b_redundant_pic_cnt );
406
407     bs_rbsp_trailing( s );
408 }
409