]> git.sesse.net Git - x264/blob - encoder/set.c
lossless mode enabled at qp=0
[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     sps->b_qpprime_y_zero_transform_bypass = !param->rc.b_cbr && param->rc.i_qp_constant == 0;
46     if( sps->b_qpprime_y_zero_transform_bypass )
47         sps->i_profile_idc  = PROFILE_HIGH444;
48     else if( param->analyse.b_transform_8x8 )
49         sps->i_profile_idc  = PROFILE_HIGH;
50     else if( param->b_cabac || param->i_bframe > 0 )
51         sps->i_profile_idc  = PROFILE_MAIN;
52     else
53         sps->i_profile_idc  = PROFILE_BASELINE;
54     sps->i_level_idc = param->i_level_idc;
55
56     sps->b_constraint_set0  = 0;
57     sps->b_constraint_set1  = 0;
58     sps->b_constraint_set2  = 0;
59
60     sps->i_log2_max_frame_num = 4;  /* at least 4 */
61     while( (1 << sps->i_log2_max_frame_num) <= param->i_keyint_max )
62     {
63         sps->i_log2_max_frame_num++;
64     }
65     sps->i_log2_max_frame_num++;    /* just in case */
66
67     sps->i_poc_type = 0;
68     if( sps->i_poc_type == 0 )
69     {
70         sps->i_log2_max_poc_lsb = sps->i_log2_max_frame_num + 1;    /* max poc = 2*frame_num */
71     }
72     else if( sps->i_poc_type == 1 )
73     {
74         int i;
75
76         /* FIXME */
77         sps->b_delta_pic_order_always_zero = 1;
78         sps->i_offset_for_non_ref_pic = 0;
79         sps->i_offset_for_top_to_bottom_field = 0;
80         sps->i_num_ref_frames_in_poc_cycle = 0;
81
82         for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
83         {
84             sps->i_offset_for_ref_frame[i] = 0;
85         }
86     }
87
88     sps->vui.i_num_reorder_frames = param->b_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
89     sps->vui.i_max_dec_frame_buffering =
90     sps->i_num_ref_frames = X264_MIN(16, param->i_frame_reference + sps->vui.i_num_reorder_frames);
91
92     sps->b_gaps_in_frame_num_value_allowed = 0;
93     sps->i_mb_width = ( param->i_width + 15 ) / 16;
94     sps->i_mb_height= ( param->i_height + 15 )/ 16;
95     sps->b_frame_mbs_only = 1;
96     sps->b_mb_adaptive_frame_field = 0;
97     sps->b_direct8x8_inference = 0;
98     if( sps->b_frame_mbs_only == 0 ||
99         !(param->analyse.inter & X264_ANALYSE_PSUB8x8) )
100     {
101         sps->b_direct8x8_inference = 1;
102     }
103
104     if( param->i_width % 16 != 0 || param->i_height % 16 != 0 )
105     {
106         sps->b_crop = 1;
107         sps->crop.i_left    = 0;
108         sps->crop.i_right   = ( 16 - param->i_width % 16)/2;
109         sps->crop.i_top     = 0;
110         sps->crop.i_bottom  = ( 16 - param->i_height % 16)/2;
111     }
112     else
113     {
114         sps->b_crop = 0;
115         sps->crop.i_left    = 0;
116         sps->crop.i_right   = 0;
117         sps->crop.i_top     = 0;
118         sps->crop.i_bottom  = 0;
119     }
120
121     sps->b_vui = 0;
122     sps->vui.b_aspect_ratio_info_present = 0;
123
124     if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
125     {
126         sps->vui.b_aspect_ratio_info_present = 1;
127         sps->vui.i_sar_width = param->vui.i_sar_width;
128         sps->vui.i_sar_height= param->vui.i_sar_height;
129     }
130     sps->b_vui |= sps->vui.b_aspect_ratio_info_present;
131
132     if( param->i_fps_num > 0 && param->i_fps_den > 0)
133     {
134         sps->vui.b_timing_info_present = 1;
135         /* The standard is confusing me, but this seems to work best
136            with other encoders */
137         sps->vui.i_num_units_in_tick = param->i_fps_den;
138         sps->vui.i_time_scale = param->i_fps_num;
139         sps->vui.b_fixed_frame_rate = 1;
140     }
141     sps->b_vui |= sps->vui.b_timing_info_present;
142
143     sps->vui.b_bitstream_restriction = param->i_bframe > 0;
144     if( sps->vui.b_bitstream_restriction )
145     {
146         sps->vui.b_motion_vectors_over_pic_boundaries = 1;
147         sps->vui.i_max_bytes_per_pic_denom = 0;
148         sps->vui.i_max_bits_per_mb_denom = 0;
149         sps->vui.i_log2_max_mv_length_horizontal =
150         sps->vui.i_log2_max_mv_length_vertical = (int)(log(param->analyse.i_mv_range*4-1)/log(2)) + 1;
151     }
152     sps->b_vui |= sps->vui.b_bitstream_restriction;
153 }
154
155
156 void x264_sps_write( bs_t *s, x264_sps_t *sps )
157 {
158     bs_write( s, 8, sps->i_profile_idc );
159     bs_write( s, 1, sps->b_constraint_set0 );
160     bs_write( s, 1, sps->b_constraint_set1 );
161     bs_write( s, 1, sps->b_constraint_set2 );
162
163     bs_write( s, 5, 0 );    /* reserved */
164
165     bs_write( s, 8, sps->i_level_idc );
166
167     bs_write_ue( s, sps->i_id );
168
169     if( sps->i_profile_idc >= PROFILE_HIGH )
170     {
171         bs_write_ue( s, 1 ); // chroma_format_idc = 4:2:0
172         bs_write_ue( s, 0 ); // bit_depth_luma_minus8
173         bs_write_ue( s, 0 ); // bit_depth_chroma_minus8
174         bs_write( s, 1, sps->b_qpprime_y_zero_transform_bypass );
175         bs_write( s, 1, 0 ); // seq_scaling_matrix_present_flag
176     }
177
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_ratio_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, sps->vui.b_bitstream_restriction );
272         if( sps->vui.b_bitstream_restriction )
273         {
274             bs_write1( s, sps->vui.b_motion_vectors_over_pic_boundaries );
275             bs_write_ue( s, sps->vui.i_max_bytes_per_pic_denom );
276             bs_write_ue( s, sps->vui.i_max_bits_per_mb_denom );
277             bs_write_ue( s, sps->vui.i_log2_max_mv_length_horizontal );
278             bs_write_ue( s, sps->vui.i_log2_max_mv_length_vertical );
279             bs_write_ue( s, sps->vui.i_num_reorder_frames );
280             bs_write_ue( s, sps->vui.i_max_dec_frame_buffering );
281         }
282     }
283
284     bs_rbsp_trailing( s );
285 }
286
287 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
288 {
289     pps->i_id = i_id;
290     pps->i_sps_id = sps->i_id;
291     pps->b_cabac = param->b_cabac;
292
293     pps->b_pic_order = 0;
294     pps->i_num_slice_groups = 1;
295
296 #if 0
297     if( pps->i_num_slice_groups > 1 )
298     {
299         int i;
300
301         pps->i_slice_group_map_type = 0;
302         if( pps->i_slice_group_map_type == 0 )
303         {
304             for( i = 0; i < pps->i_num_slice_groups; i++ )
305             {
306                 pps->i_run_length[i] = 1;
307             }
308         }
309         else if( pps->i_slice_group_map_type == 2 )
310         {
311             for( i = 0; i < pps->i_num_slice_groups; i++ )
312             {
313                 pps->i_top_left[i] = 0;
314                 pps->i_bottom_right[i] = 0;
315             }
316         }
317         else if( pps->i_slice_group_map_type >= 3 &&
318                  pps->i_slice_group_map_type <= 5 )
319         {
320             pps->b_slice_group_change_direction = 0;
321             pps->i_slice_group_change_rate = 0;
322         }
323         else if( pps->i_slice_group_map_type == 6 )
324         {
325             pps->i_pic_size_in_map_units = 1;
326             for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
327             {
328                 pps->i_slice_group_id[i] = 0;
329             }
330         }
331     }
332 #endif
333     pps->i_num_ref_idx_l0_active = 1;
334     pps->i_num_ref_idx_l1_active = 1;
335
336     pps->b_weighted_pred = 0;
337     pps->b_weighted_bipred = param->analyse.b_weighted_bipred ? 2 : 0;
338
339     pps->i_pic_init_qp = 26;
340     pps->i_pic_init_qs = 26;
341
342     pps->i_chroma_qp_index_offset = param->analyse.i_chroma_qp_offset;
343     pps->b_deblocking_filter_control = 1;
344     pps->b_constrained_intra_pred = 0;
345     pps->b_redundant_pic_cnt = 0;
346
347     pps->b_transform_8x8_mode = param->analyse.b_transform_8x8 ? 1 : 0;
348 }
349
350 void x264_pps_write( bs_t *s, x264_pps_t *pps )
351 {
352     bs_write_ue( s, pps->i_id );
353     bs_write_ue( s, pps->i_sps_id );
354
355     bs_write( s, 1, pps->b_cabac );
356     bs_write( s, 1, pps->b_pic_order );
357     bs_write_ue( s, pps->i_num_slice_groups - 1 );
358
359 #if 0
360     if( pps->i_num_slice_groups > 1 )
361     {
362         int i;
363
364         bs_write_ue( s, pps->i_slice_group_map_type );
365         if( pps->i_slice_group_map_type == 0 )
366         {
367             for( i = 0; i < pps->i_num_slice_groups; i++ )
368             {
369                 bs_write_ue( s, pps->i_run_length[i] - 1 );
370             }
371         }
372         else if( pps->i_slice_group_map_type == 2 )
373         {
374             for( i = 0; i < pps->i_num_slice_groups; i++ )
375             {
376                 bs_write_ue( s, pps->i_top_left[i] );
377                 bs_write_ue( s, pps->i_bottom_right[i] );
378             }
379         }
380         else if( pps->i_slice_group_map_type >= 3 &&
381                  pps->i_slice_group_map_type <= 5 )
382         {
383             bs_write( s, 1, pps->b_slice_group_change_direction );
384             bs_write_ue( s, pps->b_slice_group_change_direction - 1 );
385         }
386         else if( pps->i_slice_group_map_type == 6 )
387         {
388             bs_write_ue( s, pps->i_pic_size_in_map_units - 1 );
389             for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
390             {
391                 /* FIXME */
392                 /* bs_write( s, ceil( log2( pps->i_pic_size_in_map_units +1 ) ),
393                  *              pps->i_slice_group_id[i] );
394                  */
395             }
396         }
397     }
398 #endif
399
400     bs_write_ue( s, pps->i_num_ref_idx_l0_active - 1 );
401     bs_write_ue( s, pps->i_num_ref_idx_l1_active - 1 );
402     bs_write( s, 1, pps->b_weighted_pred );
403     bs_write( s, 2, pps->b_weighted_bipred );
404
405     bs_write_se( s, pps->i_pic_init_qp - 26 );
406     bs_write_se( s, pps->i_pic_init_qs - 26 );
407     bs_write_se( s, pps->i_chroma_qp_index_offset );
408
409     bs_write( s, 1, pps->b_deblocking_filter_control );
410     bs_write( s, 1, pps->b_constrained_intra_pred );
411     bs_write( s, 1, pps->b_redundant_pic_cnt );
412
413     if( pps->b_transform_8x8_mode )
414     {
415         bs_write( s, 1, pps->b_transform_8x8_mode );
416         bs_write( s, 1, 0 ); // pic_scaling_matrix_present_flag
417         bs_write_se( s, 0 ); // second_chroma_qp_index_offset
418     }
419
420     bs_rbsp_trailing( s );
421 }
422
423 void x264_sei_version_write( bs_t *s )
424 {
425     int i;
426     // random ID number generated according to ISO-11578
427     const uint8_t uuid[16] = {
428         0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7,
429         0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef
430     };
431     char version[256];
432     int length;
433     sprintf( version, "x264 - core %d%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html",
434              X264_BUILD, X264_VERSION );
435     length = strlen(version)+1+16;
436
437     bs_write( s, 8, 0x5 ); // payload_type = user_data_unregistered
438     while( length > 255 )
439         bs_write( s, 8, 255 ), length -= 255;
440     bs_write( s, 8, length ); // payload_size
441
442     for( i = 0; i < 16; i++ )
443         bs_write( s, 8, uuid[i] );
444     for( i = 0; i < length-16; i++ )
445         bs_write( s, 8, version[i] );
446
447     bs_rbsp_trailing( s );
448 }