]> git.sesse.net Git - x264/blob - encoder/set.c
checkasm: check 8x8dct, mc average, quant, and SSE2.
[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 static void scaling_list_write( bs_t *s, x264_pps_t *pps, int idx )
42 {
43     const int len = idx<4 ? 16 : 64;
44     const int *zigzag = idx<4 ? x264_zigzag_scan4 : x264_zigzag_scan8;
45     const uint8_t *list = pps->scaling_list[idx];
46     const uint8_t *def_list = (idx==CQM_4IC) ? pps->scaling_list[CQM_4IY]
47                             : (idx==CQM_4PC) ? pps->scaling_list[CQM_4PY]
48                             : x264_cqm_jvt[idx];
49     int j;
50     if( memcmp( list, def_list, len ) )
51     {
52         bs_write( s, 1, 1 ); // scaling_list_present_flag
53         for( j = 0; j < len; j++ )
54             bs_write_se( s, list[zigzag[j]] - (j>0 ? list[zigzag[j-1]] : 8) ); // delta
55     }
56     else
57         bs_write( s, 1, 0 ); // scaling_list_present_flag
58 }
59
60 void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
61 {
62     sps->i_id = i_id;
63
64     sps->b_qpprime_y_zero_transform_bypass = !param->rc.b_cbr && param->rc.i_qp_constant == 0;
65     if( sps->b_qpprime_y_zero_transform_bypass )
66         sps->i_profile_idc  = PROFILE_HIGH444;
67     else if( param->analyse.b_transform_8x8 || param->i_cqm_preset != X264_CQM_FLAT )
68         sps->i_profile_idc  = PROFILE_HIGH;
69     else if( param->b_cabac || param->i_bframe > 0 )
70         sps->i_profile_idc  = PROFILE_MAIN;
71     else
72         sps->i_profile_idc  = PROFILE_BASELINE;
73     sps->i_level_idc = param->i_level_idc;
74
75     sps->b_constraint_set0  = 0;
76     sps->b_constraint_set1  = 0;
77     sps->b_constraint_set2  = 0;
78
79     sps->i_log2_max_frame_num = 4;  /* at least 4 */
80     while( (1 << sps->i_log2_max_frame_num) <= param->i_keyint_max )
81     {
82         sps->i_log2_max_frame_num++;
83     }
84     sps->i_log2_max_frame_num++;    /* just in case */
85
86     sps->i_poc_type = 0;
87     if( sps->i_poc_type == 0 )
88     {
89         sps->i_log2_max_poc_lsb = sps->i_log2_max_frame_num + 1;    /* max poc = 2*frame_num */
90     }
91     else if( sps->i_poc_type == 1 )
92     {
93         int i;
94
95         /* FIXME */
96         sps->b_delta_pic_order_always_zero = 1;
97         sps->i_offset_for_non_ref_pic = 0;
98         sps->i_offset_for_top_to_bottom_field = 0;
99         sps->i_num_ref_frames_in_poc_cycle = 0;
100
101         for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
102         {
103             sps->i_offset_for_ref_frame[i] = 0;
104         }
105     }
106
107     sps->vui.i_num_reorder_frames = param->b_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
108     /* extra slot with pyramid so that we don't have to override the
109      * order of forgetting old pictures */
110     sps->vui.i_max_dec_frame_buffering =
111     sps->i_num_ref_frames = X264_MIN(16, param->i_frame_reference + sps->vui.i_num_reorder_frames + param->b_bframe_pyramid);
112
113     sps->b_gaps_in_frame_num_value_allowed = 0;
114     sps->i_mb_width = ( param->i_width + 15 ) / 16;
115     sps->i_mb_height= ( param->i_height + 15 )/ 16;
116     sps->b_frame_mbs_only = 1;
117     sps->b_mb_adaptive_frame_field = 0;
118     sps->b_direct8x8_inference = 0;
119     if( sps->b_frame_mbs_only == 0 ||
120         !(param->analyse.inter & X264_ANALYSE_PSUB8x8) )
121     {
122         sps->b_direct8x8_inference = 1;
123     }
124
125     sps->crop.i_left   = 0;
126     sps->crop.i_top    = 0;
127     sps->crop.i_right  = (- param->i_width)  & 15;
128     sps->crop.i_bottom = (- param->i_height) & 15;
129     sps->b_crop = sps->crop.i_left  || sps->crop.i_top ||
130                   sps->crop.i_right || sps->crop.i_bottom;
131
132     sps->b_vui = 0;
133     sps->vui.b_aspect_ratio_info_present = 0;
134
135     if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
136     {
137         sps->vui.b_aspect_ratio_info_present = 1;
138         sps->vui.i_sar_width = param->vui.i_sar_width;
139         sps->vui.i_sar_height= param->vui.i_sar_height;
140     }
141     sps->b_vui |= sps->vui.b_aspect_ratio_info_present;
142     
143     sps->vui.b_overscan_info_present = ( param->vui.i_overscan ? 1 : 0 );
144     if( sps->vui.b_overscan_info_present )
145         sps->vui.b_overscan_info = ( param->vui.i_overscan == 2 ? 1 : 0 );
146     sps->b_vui |= sps->vui.b_overscan_info_present;
147     
148     sps->vui.b_signal_type_present = 0;
149     sps->vui.i_vidformat = ( param->vui.i_vidformat <= 5 ? param->vui.i_vidformat : 5 );
150     sps->vui.b_fullrange = ( param->vui.b_fullrange ? 1 : 0 );
151     sps->vui.b_color_description_present = 0;
152
153     sps->vui.i_colorprim = ( param->vui.i_colorprim <=  9 ? param->vui.i_colorprim : 2 );
154     sps->vui.i_transfer  = ( param->vui.i_transfer  <= 11 ? param->vui.i_transfer  : 2 );
155     sps->vui.i_colmatrix = ( param->vui.i_colmatrix <=  9 ? param->vui.i_colmatrix : 2 );
156     if( sps->vui.i_colorprim != 2 ||
157         sps->vui.i_transfer  != 2 ||
158         sps->vui.i_colmatrix != 2 )
159     {
160         sps->vui.b_color_description_present = 1;
161     }
162
163     if( sps->vui.i_vidformat != 5 ||
164         sps->vui.b_fullrange ||
165         sps->vui.b_color_description_present )
166     {
167         sps->vui.b_signal_type_present = 1;
168     }
169     sps->b_vui |= sps->vui.b_signal_type_present;
170     
171     /* FIXME: not sufficient for interlaced video */
172     sps->vui.b_chroma_loc_info_present = ( param->vui.i_chroma_loc ? 1 : 0 );
173     if( sps->vui.b_chroma_loc_info_present )
174     {
175         sps->vui.i_chroma_loc_top = param->vui.i_chroma_loc;
176         sps->vui.i_chroma_loc_bottom = param->vui.i_chroma_loc;
177     }
178     sps->b_vui |= sps->vui.b_chroma_loc_info_present;
179
180     sps->vui.b_timing_info_present = 0;
181     if( param->i_fps_num > 0 && param->i_fps_den > 0)
182     {
183         sps->vui.b_timing_info_present = 1;
184         /* The standard is confusing me, but this seems to work best
185            with other encoders */
186         sps->vui.i_num_units_in_tick = param->i_fps_den;
187         sps->vui.i_time_scale = param->i_fps_num;
188         sps->vui.b_fixed_frame_rate = 1;
189     }
190     sps->b_vui |= sps->vui.b_timing_info_present;
191
192     sps->vui.b_bitstream_restriction = param->i_bframe > 0;
193     if( sps->vui.b_bitstream_restriction )
194     {
195         sps->vui.b_motion_vectors_over_pic_boundaries = 1;
196         sps->vui.i_max_bytes_per_pic_denom = 0;
197         sps->vui.i_max_bits_per_mb_denom = 0;
198         sps->vui.i_log2_max_mv_length_horizontal =
199         sps->vui.i_log2_max_mv_length_vertical = (int)(log(param->analyse.i_mv_range*4-1)/log(2)) + 1;
200     }
201     sps->b_vui |= sps->vui.b_bitstream_restriction;
202 }
203
204
205 void x264_sps_write( bs_t *s, x264_sps_t *sps )
206 {
207     bs_write( s, 8, sps->i_profile_idc );
208     bs_write( s, 1, sps->b_constraint_set0 );
209     bs_write( s, 1, sps->b_constraint_set1 );
210     bs_write( s, 1, sps->b_constraint_set2 );
211
212     bs_write( s, 5, 0 );    /* reserved */
213
214     bs_write( s, 8, sps->i_level_idc );
215
216     bs_write_ue( s, sps->i_id );
217
218     if( sps->i_profile_idc >= PROFILE_HIGH )
219     {
220         bs_write_ue( s, 1 ); // chroma_format_idc = 4:2:0
221         bs_write_ue( s, 0 ); // bit_depth_luma_minus8
222         bs_write_ue( s, 0 ); // bit_depth_chroma_minus8
223         bs_write( s, 1, sps->b_qpprime_y_zero_transform_bypass );
224         bs_write( s, 1, 0 ); // seq_scaling_matrix_present_flag
225     }
226
227     bs_write_ue( s, sps->i_log2_max_frame_num - 4 );
228     bs_write_ue( s, sps->i_poc_type );
229     if( sps->i_poc_type == 0 )
230     {
231         bs_write_ue( s, sps->i_log2_max_poc_lsb - 4 );
232     }
233     else if( sps->i_poc_type == 1 )
234     {
235         int i;
236
237         bs_write( s, 1, sps->b_delta_pic_order_always_zero );
238         bs_write_se( s, sps->i_offset_for_non_ref_pic );
239         bs_write_se( s, sps->i_offset_for_top_to_bottom_field );
240         bs_write_ue( s, sps->i_num_ref_frames_in_poc_cycle );
241
242         for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
243         {
244             bs_write_se( s, sps->i_offset_for_ref_frame[i] );
245         }
246     }
247     bs_write_ue( s, sps->i_num_ref_frames );
248     bs_write( s, 1, sps->b_gaps_in_frame_num_value_allowed );
249     bs_write_ue( s, sps->i_mb_width - 1 );
250     bs_write_ue( s, sps->i_mb_height - 1);
251     bs_write( s, 1, sps->b_frame_mbs_only );
252     if( !sps->b_frame_mbs_only )
253     {
254         bs_write( s, 1, sps->b_mb_adaptive_frame_field );
255     }
256     bs_write( s, 1, sps->b_direct8x8_inference );
257
258     bs_write( s, 1, sps->b_crop );
259     if( sps->b_crop )
260     {
261         bs_write_ue( s, sps->crop.i_left   / 2 );
262         bs_write_ue( s, sps->crop.i_right  / 2 );
263         bs_write_ue( s, sps->crop.i_top    / 2 );
264         bs_write_ue( s, sps->crop.i_bottom / 2 );
265     }
266
267     bs_write( s, 1, sps->b_vui );
268     if( sps->b_vui )
269     {
270         bs_write1( s, sps->vui.b_aspect_ratio_info_present );
271         if( sps->vui.b_aspect_ratio_info_present )
272         {
273             int i;
274             static const struct { int w, h; int sar; } sar[] =
275             {
276                 { 1,   1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
277                 { 40, 33, 5 }, { 24, 11, 6 }, { 20, 11, 7 }, { 32, 11, 8 },
278                 { 80, 33, 9 }, { 18, 11, 10}, { 15, 11, 11}, { 64, 33, 12},
279                 { 160,99, 13}, { 0, 0, -1 }
280             };
281             for( i = 0; sar[i].sar != -1; i++ )
282             {
283                 if( sar[i].w == sps->vui.i_sar_width &&
284                     sar[i].h == sps->vui.i_sar_height )
285                     break;
286             }
287             if( sar[i].sar != -1 )
288             {
289                 bs_write( s, 8, sar[i].sar );
290             }
291             else
292             {
293                 bs_write( s, 8, 255);   /* aspect_ratio_idc (extented) */
294                 bs_write( s, 16, sps->vui.i_sar_width );
295                 bs_write( s, 16, sps->vui.i_sar_height );
296             }
297         }
298
299         bs_write1( s, sps->vui.b_overscan_info_present );
300         if( sps->vui.b_overscan_info_present )
301             bs_write1( s, sps->vui.b_overscan_info );
302
303         bs_write1( s, sps->vui.b_signal_type_present );
304         if( sps->vui.b_signal_type_present )
305         {
306             bs_write( s, 3, sps->vui.i_vidformat );
307             bs_write1( s, sps->vui.b_fullrange );
308             bs_write1( s, sps->vui.b_color_description_present );
309             if( sps->vui.b_color_description_present )
310             {
311                 bs_write( s, 8, sps->vui.i_colorprim );
312                 bs_write( s, 8, sps->vui.i_transfer );
313                 bs_write( s, 8, sps->vui.i_colmatrix );
314             }
315         }
316
317         bs_write1( s, sps->vui.b_chroma_loc_info_present );
318         if( sps->vui.b_chroma_loc_info_present )
319         {
320             bs_write_ue( s, sps->vui.i_chroma_loc_top );
321             bs_write_ue( s, sps->vui.i_chroma_loc_bottom );
322         }
323
324         bs_write1( s, sps->vui.b_timing_info_present );
325         if( sps->vui.b_timing_info_present )
326         {
327             bs_write( s, 32, sps->vui.i_num_units_in_tick );
328             bs_write( s, 32, sps->vui.i_time_scale );
329             bs_write1( s, sps->vui.b_fixed_frame_rate );
330         }
331
332         bs_write1( s, 0 );      /* nal_hrd_parameters_present_flag */
333         bs_write1( s, 0 );      /* vcl_hrd_parameters_present_flag */
334         bs_write1( s, 0 );      /* pic_struct_present_flag */
335         bs_write1( s, sps->vui.b_bitstream_restriction );
336         if( sps->vui.b_bitstream_restriction )
337         {
338             bs_write1( s, sps->vui.b_motion_vectors_over_pic_boundaries );
339             bs_write_ue( s, sps->vui.i_max_bytes_per_pic_denom );
340             bs_write_ue( s, sps->vui.i_max_bits_per_mb_denom );
341             bs_write_ue( s, sps->vui.i_log2_max_mv_length_horizontal );
342             bs_write_ue( s, sps->vui.i_log2_max_mv_length_vertical );
343             bs_write_ue( s, sps->vui.i_num_reorder_frames );
344             bs_write_ue( s, sps->vui.i_max_dec_frame_buffering );
345         }
346     }
347
348     bs_rbsp_trailing( s );
349 }
350
351 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
352 {
353     int i, j;
354
355     pps->i_id = i_id;
356     pps->i_sps_id = sps->i_id;
357     pps->b_cabac = param->b_cabac;
358
359     pps->b_pic_order = 0;
360     pps->i_num_slice_groups = 1;
361
362     pps->i_num_ref_idx_l0_active = 1;
363     pps->i_num_ref_idx_l1_active = 1;
364
365     pps->b_weighted_pred = 0;
366     pps->b_weighted_bipred = param->analyse.b_weighted_bipred ? 2 : 0;
367
368     pps->i_pic_init_qp = 26;
369     pps->i_pic_init_qs = 26;
370
371     pps->i_chroma_qp_index_offset = param->analyse.i_chroma_qp_offset;
372     pps->b_deblocking_filter_control = 1;
373     pps->b_constrained_intra_pred = 0;
374     pps->b_redundant_pic_cnt = 0;
375
376     pps->b_transform_8x8_mode = param->analyse.b_transform_8x8 ? 1 : 0;
377
378     pps->i_cqm_preset = param->i_cqm_preset;
379     switch( pps->i_cqm_preset )
380     {
381     case X264_CQM_FLAT:
382         for( i = 0; i < 6; i++ )
383             pps->scaling_list[i] = x264_cqm_flat16;
384         break;
385     case X264_CQM_JVT:
386         for( i = 0; i < 6; i++ )
387             pps->scaling_list[i] = x264_cqm_jvt[i];
388         break;
389     case X264_CQM_CUSTOM:
390         pps->scaling_list[CQM_4IY] = param->cqm_4iy;
391         pps->scaling_list[CQM_4IC] = param->cqm_4ic;
392         pps->scaling_list[CQM_4PY] = param->cqm_4py;
393         pps->scaling_list[CQM_4PC] = param->cqm_4pc;
394         pps->scaling_list[CQM_8IY+4] = param->cqm_8iy;
395         pps->scaling_list[CQM_8PY+4] = param->cqm_8py;
396         for( i = 0; i < 6; i++ )
397             for( j = 0; j < (i<4?16:64); j++ )
398                 if( pps->scaling_list[i][j] == 0 )
399                     pps->scaling_list[i] = x264_cqm_jvt[i];
400         break;
401     }
402 }
403
404 void x264_pps_write( bs_t *s, x264_pps_t *pps )
405 {
406     bs_write_ue( s, pps->i_id );
407     bs_write_ue( s, pps->i_sps_id );
408
409     bs_write( s, 1, pps->b_cabac );
410     bs_write( s, 1, pps->b_pic_order );
411     bs_write_ue( s, pps->i_num_slice_groups - 1 );
412
413     bs_write_ue( s, pps->i_num_ref_idx_l0_active - 1 );
414     bs_write_ue( s, pps->i_num_ref_idx_l1_active - 1 );
415     bs_write( s, 1, pps->b_weighted_pred );
416     bs_write( s, 2, pps->b_weighted_bipred );
417
418     bs_write_se( s, pps->i_pic_init_qp - 26 );
419     bs_write_se( s, pps->i_pic_init_qs - 26 );
420     bs_write_se( s, pps->i_chroma_qp_index_offset );
421
422     bs_write( s, 1, pps->b_deblocking_filter_control );
423     bs_write( s, 1, pps->b_constrained_intra_pred );
424     bs_write( s, 1, pps->b_redundant_pic_cnt );
425
426     if( pps->b_transform_8x8_mode || pps->i_cqm_preset != X264_CQM_FLAT )
427     {
428         bs_write( s, 1, pps->b_transform_8x8_mode );
429         bs_write( s, 1, (pps->i_cqm_preset != X264_CQM_FLAT) );
430         if( pps->i_cqm_preset != X264_CQM_FLAT )
431         {
432             scaling_list_write( s, pps, CQM_4IY );
433             scaling_list_write( s, pps, CQM_4IC );
434             bs_write( s, 1, 0 ); // Cr = Cb
435             scaling_list_write( s, pps, CQM_4PY );
436             scaling_list_write( s, pps, CQM_4PC );
437             bs_write( s, 1, 0 ); // Cr = Cb
438             if( pps->b_transform_8x8_mode )
439             {
440                 scaling_list_write( s, pps, CQM_8IY+4 );
441                 scaling_list_write( s, pps, CQM_8PY+4 );
442             }
443         }
444         bs_write_se( s, pps->i_chroma_qp_index_offset );
445     }
446
447     bs_rbsp_trailing( s );
448 }
449
450 void x264_sei_version_write( bs_t *s )
451 {
452     int i;
453     // random ID number generated according to ISO-11578
454     const uint8_t uuid[16] = {
455         0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7,
456         0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef
457     };
458     char version[256];
459     int length;
460     sprintf( version, "x264 - core %d%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html",
461              X264_BUILD, X264_VERSION );
462     length = strlen(version)+1+16;
463
464     bs_write( s, 8, 0x5 ); // payload_type = user_data_unregistered
465     while( length > 255 )
466         bs_write( s, 8, 255 ), length -= 255;
467     bs_write( s, 8, length ); // payload_size
468
469     for( i = 0; i < 16; i++ )
470         bs_write( s, 8, uuid[i] );
471     for( i = 0; i < length-16; i++ )
472         bs_write( s, 8, version[i] );
473
474     bs_rbsp_trailing( s );
475 }