]> git.sesse.net Git - x264/blob - encoder/set.c
4f94bad9c0bd4b6c5e7982adf7480d99b1bd56ac
[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/common.h"
37 #ifndef _MSC_VER
38 #include "config.h"
39 #endif
40
41 void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
42 {
43     sps->i_id = i_id;
44
45     if( param->analyse.b_transform_8x8 )
46         sps->i_profile_idc  = PROFILE_HIGH;
47     else if( param->b_cabac || param->i_bframe > 0 )
48         sps->i_profile_idc  = PROFILE_MAIN;
49     else
50         sps->i_profile_idc  = PROFILE_BASELINE;
51     sps->i_level_idc = param->i_level_idc;
52
53     sps->b_constraint_set0  = 0;
54     sps->b_constraint_set1  = 0;
55     sps->b_constraint_set2  = 0;
56
57     sps->i_log2_max_frame_num = 4;  /* at least 4 */
58     while( (1 << sps->i_log2_max_frame_num) <= param->i_keyint_max )
59     {
60         sps->i_log2_max_frame_num++;
61     }
62     sps->i_log2_max_frame_num++;    /* just in case */
63
64     sps->i_poc_type = 0;
65     if( sps->i_poc_type == 0 )
66     {
67         sps->i_log2_max_poc_lsb = sps->i_log2_max_frame_num + 1;    /* max poc = 2*frame_num */
68     }
69     else if( sps->i_poc_type == 1 )
70     {
71         int i;
72
73         /* FIXME */
74         sps->b_delta_pic_order_always_zero = 1;
75         sps->i_offset_for_non_ref_pic = 0;
76         sps->i_offset_for_top_to_bottom_field = 0;
77         sps->i_num_ref_frames_in_poc_cycle = 0;
78
79         for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
80         {
81             sps->i_offset_for_ref_frame[i] = 0;
82         }
83     }
84
85     sps->vui.i_num_reorder_frames = param->b_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
86     sps->i_num_ref_frames = X264_MIN(16, param->i_frame_reference + sps->vui.i_num_reorder_frames);
87     sps->vui.i_max_dec_frame_buffering = sps->i_num_ref_frames + 1;
88
89     sps->b_gaps_in_frame_num_value_allowed = 0;
90     sps->i_mb_width = ( param->i_width + 15 ) / 16;
91     sps->i_mb_height= ( param->i_height + 15 )/ 16;
92     sps->b_frame_mbs_only = 1;
93     sps->b_mb_adaptive_frame_field = 0;
94     sps->b_direct8x8_inference = 0;
95     if( sps->b_frame_mbs_only == 0 ||
96         !(param->analyse.inter & X264_ANALYSE_PSUB8x8) )
97     {
98         sps->b_direct8x8_inference = 1;
99     }
100
101     if( param->i_width % 16 != 0 || param->i_height % 16 != 0 )
102     {
103         sps->b_crop = 1;
104         sps->crop.i_left    = 0;
105         sps->crop.i_right   = ( 16 - param->i_width % 16)/2;
106         sps->crop.i_top     = 0;
107         sps->crop.i_bottom  = ( 16 - param->i_height % 16)/2;
108     }
109     else
110     {
111         sps->b_crop = 0;
112         sps->crop.i_left    = 0;
113         sps->crop.i_right   = 0;
114         sps->crop.i_top     = 0;
115         sps->crop.i_bottom  = 0;
116     }
117
118     sps->b_vui = 0;
119     sps->vui.b_aspect_ratio_info_present = 0;
120
121     if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
122     {
123         sps->vui.b_aspect_ratio_info_present = 1;
124         sps->vui.i_sar_width = param->vui.i_sar_width;
125         sps->vui.i_sar_height= param->vui.i_sar_height;
126     }
127     sps->b_vui |= sps->vui.b_aspect_ratio_info_present;
128
129     if( param->i_fps_num > 0 && param->i_fps_den > 0)
130     {
131         sps->vui.b_timing_info_present = 1;
132         /* The standard is confusing me, but this seems to work best
133            with other encoders */
134         sps->vui.i_num_units_in_tick = param->i_fps_den;
135         sps->vui.i_time_scale = param->i_fps_num;
136         sps->vui.b_fixed_frame_rate = 1;
137     }
138     sps->b_vui |= sps->vui.b_timing_info_present;
139
140     sps->vui.b_bitstream_restriction = param->i_bframe > 0;
141     if( sps->vui.b_bitstream_restriction )
142     {
143         sps->vui.b_motion_vectors_over_pic_boundaries = 1;
144         sps->vui.i_max_bytes_per_pic_denom = 0;
145         sps->vui.i_max_bits_per_mb_denom = 0;
146         sps->vui.i_log2_max_mv_length_horizontal =
147         sps->vui.i_log2_max_mv_length_vertical = (int)(log(param->analyse.i_mv_range*4-1)/log(2)) + 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
166     if( sps->i_profile_idc >= PROFILE_HIGH )
167     {
168         bs_write_ue( s, 1 ); // chroma_format_idc = 4:2:0
169         bs_write_ue( s, 0 ); // bit_depth_luma_minus8
170         bs_write_ue( s, 0 ); // bit_depth_chroma_minus8
171         bs_write( s, 1, 0 ); // qpprime_y_zero_transform_bypass_flag
172         bs_write( s, 1, 0 ); // seq_scaling_matrix_present_flag
173     }
174
175     bs_write_ue( s, sps->i_log2_max_frame_num - 4 );
176     bs_write_ue( s, sps->i_poc_type );
177     if( sps->i_poc_type == 0 )
178     {
179         bs_write_ue( s, sps->i_log2_max_poc_lsb - 4 );
180     }
181     else if( sps->i_poc_type == 1 )
182     {
183         int i;
184
185         bs_write( s, 1, sps->b_delta_pic_order_always_zero );
186         bs_write_se( s, sps->i_offset_for_non_ref_pic );
187         bs_write_se( s, sps->i_offset_for_top_to_bottom_field );
188         bs_write_ue( s, sps->i_num_ref_frames_in_poc_cycle );
189
190         for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
191         {
192             bs_write_se( s, sps->i_offset_for_ref_frame[i] );
193         }
194     }
195     bs_write_ue( s, sps->i_num_ref_frames );
196     bs_write( s, 1, sps->b_gaps_in_frame_num_value_allowed );
197     bs_write_ue( s, sps->i_mb_width - 1 );
198     bs_write_ue( s, sps->i_mb_height - 1);
199     bs_write( s, 1, sps->b_frame_mbs_only );
200     if( !sps->b_frame_mbs_only )
201     {
202         bs_write( s, 1, sps->b_mb_adaptive_frame_field );
203     }
204     bs_write( s, 1, sps->b_direct8x8_inference );
205
206     bs_write( s, 1, sps->b_crop );
207     if( sps->b_crop )
208     {
209         bs_write_ue( s, sps->crop.i_left );
210         bs_write_ue( s, sps->crop.i_right );
211         bs_write_ue( s, sps->crop.i_top );
212         bs_write_ue( s, sps->crop.i_bottom );
213     }
214
215     bs_write( s, 1, sps->b_vui );
216     if( sps->b_vui )
217     {
218         bs_write1( s, sps->vui.b_aspect_ratio_info_present );
219         if( sps->vui.b_aspect_ratio_info_present )
220         {
221             int i;
222             static const struct { int w, h; int sar; } sar[] =
223             {
224                 { 1,   1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
225                 { 40, 33, 5 }, { 24, 11, 6 }, { 20, 11, 7 }, { 32, 11, 8 },
226                 { 80, 33, 9 }, { 18, 11, 10}, { 15, 11, 11}, { 64, 33, 12},
227                 { 160,99, 13}, { 0, 0, -1 }
228             };
229             for( i = 0; sar[i].sar != -1; i++ )
230             {
231                 if( sar[i].w == sps->vui.i_sar_width &&
232                     sar[i].h == sps->vui.i_sar_height )
233                     break;
234             }
235             if( sar[i].sar != -1 )
236             {
237                 bs_write( s, 8, sar[i].sar );
238             }
239             else
240             {
241                 bs_write( s, 8, 255);   /* aspect_ratio_idc (extented) */
242                 bs_write( s, 16, sps->vui.i_sar_width );
243                 bs_write( s, 16, sps->vui.i_sar_height );
244             }
245         }
246
247         bs_write1( s, 0 );      /* overscan_info_present_flag */
248
249         bs_write1( s, 0 );      /* video_signal_type_present_flag */
250 #if 0
251         bs_write( s, 3, 5 );    /* unspecified video format */
252         bs_write1( s, 1 );      /* video full range flag */
253         bs_write1( s, 0 );      /* colour description present flag */
254 #endif
255         bs_write1( s, 0 );      /* chroma_loc_info_present_flag */
256
257         bs_write1( s, sps->vui.b_timing_info_present );
258         if( sps->vui.b_timing_info_present )
259         {
260             bs_write( s, 32, sps->vui.i_num_units_in_tick );
261             bs_write( s, 32, sps->vui.i_time_scale );
262             bs_write1( s, sps->vui.b_fixed_frame_rate );
263         }
264
265         bs_write1( s, 0 );      /* nal_hrd_parameters_present_flag */
266         bs_write1( s, 0 );      /* vcl_hrd_parameters_present_flag */
267         bs_write1( s, 0 );      /* pic_struct_present_flag */
268         bs_write1( s, sps->vui.b_bitstream_restriction );
269         if( sps->vui.b_bitstream_restriction )
270         {
271             bs_write1( s, sps->vui.b_motion_vectors_over_pic_boundaries );
272             bs_write_ue( s, sps->vui.i_max_bytes_per_pic_denom );
273             bs_write_ue( s, sps->vui.i_max_bits_per_mb_denom );
274             bs_write_ue( s, sps->vui.i_log2_max_mv_length_horizontal );
275             bs_write_ue( s, sps->vui.i_log2_max_mv_length_vertical );
276             bs_write_ue( s, sps->vui.i_num_reorder_frames );
277             bs_write_ue( s, sps->vui.i_max_dec_frame_buffering );
278         }
279     }
280
281     bs_rbsp_trailing( s );
282 }
283
284 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
285 {
286     pps->i_id = i_id;
287     pps->i_sps_id = sps->i_id;
288     pps->b_cabac = param->b_cabac;
289
290     pps->b_pic_order = 0;
291     pps->i_num_slice_groups = 1;
292
293     if( pps->i_num_slice_groups > 1 )
294     {
295         int i;
296
297         pps->i_slice_group_map_type = 0;
298         if( pps->i_slice_group_map_type == 0 )
299         {
300             for( i = 0; i < pps->i_num_slice_groups; i++ )
301             {
302                 pps->i_run_length[i] = 1;
303             }
304         }
305         else if( pps->i_slice_group_map_type == 2 )
306         {
307             for( i = 0; i < pps->i_num_slice_groups; i++ )
308             {
309                 pps->i_top_left[i] = 0;
310                 pps->i_bottom_right[i] = 0;
311             }
312         }
313         else if( pps->i_slice_group_map_type >= 3 &&
314                  pps->i_slice_group_map_type <= 5 )
315         {
316             pps->b_slice_group_change_direction = 0;
317             pps->i_slice_group_change_rate = 0;
318         }
319         else if( pps->i_slice_group_map_type == 6 )
320         {
321             pps->i_pic_size_in_map_units = 1;
322             for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
323             {
324                 pps->i_slice_group_id[i] = 0;
325             }
326         }
327     }
328     pps->i_num_ref_idx_l0_active = 1;
329     pps->i_num_ref_idx_l1_active = 1;
330
331     pps->b_weighted_pred = 0;
332     pps->b_weighted_bipred = param->analyse.b_weighted_bipred ? 2 : 0;
333
334     pps->i_pic_init_qp = 26;
335     pps->i_pic_init_qs = 26;
336
337     pps->i_chroma_qp_index_offset = param->analyse.i_chroma_qp_offset;
338     pps->b_deblocking_filter_control = 1;
339     pps->b_constrained_intra_pred = 0;
340     pps->b_redundant_pic_cnt = 0;
341
342     pps->b_transform_8x8_mode = param->analyse.b_transform_8x8 ? 1 : 0;
343 }
344
345 void x264_pps_write( bs_t *s, x264_pps_t *pps )
346 {
347     bs_write_ue( s, pps->i_id );
348     bs_write_ue( s, pps->i_sps_id );
349
350     bs_write( s, 1, pps->b_cabac );
351     bs_write( s, 1, pps->b_pic_order );
352     bs_write_ue( s, pps->i_num_slice_groups - 1 );
353
354     if( pps->i_num_slice_groups > 1 )
355     {
356         int i;
357
358         bs_write_ue( s, pps->i_slice_group_map_type );
359         if( pps->i_slice_group_map_type == 0 )
360         {
361             for( i = 0; i < pps->i_num_slice_groups; i++ )
362             {
363                 bs_write_ue( s, pps->i_run_length[i] - 1 );
364             }
365         }
366         else if( pps->i_slice_group_map_type == 2 )
367         {
368             for( i = 0; i < pps->i_num_slice_groups; i++ )
369             {
370                 bs_write_ue( s, pps->i_top_left[i] );
371                 bs_write_ue( s, pps->i_bottom_right[i] );
372             }
373         }
374         else if( pps->i_slice_group_map_type >= 3 &&
375                  pps->i_slice_group_map_type <= 5 )
376         {
377             bs_write( s, 1, pps->b_slice_group_change_direction );
378             bs_write_ue( s, pps->b_slice_group_change_direction - 1 );
379         }
380         else if( pps->i_slice_group_map_type == 6 )
381         {
382             bs_write_ue( s, pps->i_pic_size_in_map_units - 1 );
383             for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
384             {
385                 /* FIXME */
386                 /* bs_write( s, ceil( log2( pps->i_pic_size_in_map_units +1 ) ),
387                  *              pps->i_slice_group_id[i] );
388                  */
389             }
390         }
391     }
392
393     bs_write_ue( s, pps->i_num_ref_idx_l0_active - 1 );
394     bs_write_ue( s, pps->i_num_ref_idx_l1_active - 1 );
395     bs_write( s, 1, pps->b_weighted_pred );
396     bs_write( s, 2, pps->b_weighted_bipred );
397
398     bs_write_se( s, pps->i_pic_init_qp - 26 );
399     bs_write_se( s, pps->i_pic_init_qs - 26 );
400     bs_write_se( s, pps->i_chroma_qp_index_offset );
401
402     bs_write( s, 1, pps->b_deblocking_filter_control );
403     bs_write( s, 1, pps->b_constrained_intra_pred );
404     bs_write( s, 1, pps->b_redundant_pic_cnt );
405
406     if( pps->b_transform_8x8_mode )
407     {
408         bs_write( s, 1, pps->b_transform_8x8_mode );
409         bs_write( s, 1, 0 ); // pic_scaling_matrix_present_flag
410         bs_write_se( s, 0 ); // second_chroma_qp_index_offset
411     }
412
413     bs_rbsp_trailing( s );
414 }
415
416 void x264_sei_version_write( bs_t *s )
417 {
418     int i;
419     // random ID number generated according to ISO-11578
420     const uint8_t uuid[16] = {
421         0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7,
422         0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef
423     };
424     char version[256];
425     int length;
426     sprintf( version, "x264 - core %d%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html",
427              X264_BUILD, X264_VERSION );
428     length = strlen(version)+1+16;
429
430     bs_write( s, 8, 0x5 ); // payload_type = user_data_unregistered
431     while( length > 255 )
432         bs_write( s, 8, 255 ), length -= 255;
433     bs_write( s, 8, length ); // payload_size
434
435     for( i = 0; i < 16; i++ )
436         bs_write( s, 8, uuid[i] );
437     for( i = 0; i < length-16; i++ )
438         bs_write( s, 8, version[i] );
439
440     bs_rbsp_trailing( s );
441 }