Don't do fcn calls in arg of g_?to??(); Macro may very well eval args multiple times.
[obnox/wireshark/wip.git] / epan / dissectors / packet-h264.c
1 /* packet-h264.c
2  * Routines for H.264 dissection
3  * Copyright 2007 - 2009, Anders Broman <anders.broman[at]ericsson.com>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  *
25  * References:
26  * http://www.ietf.org/rfc/rfc3984.txt?number=3984
27  * http://www.itu.int/rec/T-REC-H.264/en
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <string.h>
35
36 #include <glib.h>
37
38 #include <epan/packet.h>
39 #include <epan/asn1.h>
40 #include <epan/strutil.h>
41
42 #include <epan/prefs.h>
43
44
45 /* Initialize the protocol and registered fields */
46 static int proto_h264                                                           = -1;
47 static int hf_h264_type                                                         = -1;
48 static int hf_h264_nal_f_bit                                            = -1;
49 static int hf_h264_nal_nri                                                      = -1;
50 static int hf_h264_start_bit                                            = -1;
51 static int hf_h264_forbidden_bit                                        = -1;
52 static int hf_h264_end_bit                                                      = -1;
53 static int hf_h264_profile                                                      = -1;
54 static int hf_h264_profile_idc                                          = -1;
55 static int hf_h264_rbsp_stop_bit                                        = -1;
56 static int hf_h264_rbsp_trailing_bits                           = -1;
57 static int hf_h264_constraint_set0_flag                         = -1;
58 static int hf_h264_constraint_set1_flag                         = -1;
59 static int hf_h264_constraint_set2_flag                         = -1;
60 static int hf_h264_constraint_set3_flag                         = -1;
61 static int hf_h264_reserved_zero_4bits                          = -1;
62 static int hf_h264_level_idc                                            = -1;
63 static int hf_h264_nal_unit                                                     = -1;
64 static int hf_h264_forbidden_zero_bit                           = -1;
65 static int hf_h264_nal_ref_idc                                          = -1;
66 static int hf_h264_nal_unit_type                                        = -1;
67 static int hf_h264_seq_parameter_set_id                         = -1;
68 static int hf_h264_chroma_format_idc                            = -1;
69 static int hf_h264_residual_colour_transform_flag       = -1;
70 static int hf_h264_bit_depth_luma_minus8                        = -1;
71 static int hf_h264_bit_depth_chroma_minus8                      = -1;
72 static int hf_h264_qpprime_y_zero_transform_bypass_flag = -1;
73 static int hf_h264_seq_scaling_matrix_present_flag      = -1;
74 static int hf_h264_log2_max_frame_num_minus4            = -1;
75 static int hf_h264_pic_order_cnt_type                           = -1;
76 static int hf_h264_log2_max_pic_order_cnt_lsb_minus4 = -1;
77 static int hf_h264_delta_pic_order_always_zero_flag = -1;
78 static int hf_h264_offset_for_non_ref_pic                       = -1;
79 static int hf_h264_offset_for_top_to_bottom_field       = -1;
80 static int hf_h264_num_ref_frames_in_pic_order_cnt_cycle = -1;
81 static int hf_h264_offset_for_ref_frame                         = -1;
82 static int hf_h264_num_ref_frames                                       = -1;
83 static int hf_h264_gaps_in_frame_num_value_allowed_flag = -1;
84 static int hf_h264_pic_width_in_mbs_minus1                      = -1;
85 static int hf_h264_pic_height_in_map_units_minus1       = -1;
86 static int hf_h264_frame_mbs_only_flag                          = -1;
87 static int hf_h264_mb_adaptive_frame_field_flag         = -1;
88 static int hf_h264_direct_8x8_inference_flag            = -1;
89 static int hf_h264_frame_cropping_flag                          = -1;
90 static int hf_h264_frame_crop_left_offset                       = -1;
91 static int hf_h264_frame_crop_right_offset                      = -1;
92 static int hf_h264_frame_crop_top_offset                        = -1;
93 static int hf_h264_frame_crop_bottom_offset                     = -1;
94 static int hf_h264_vui_parameters_present_flag          = -1;
95 static int hf_h264_pic_parameter_set_id                         = -1;
96 static int hf_h264_entropy_coding_mode_flag                     = -1;
97 static int hf_h264_pic_order_present_flag                       = -1;
98 static int hf_h264_num_slice_groups_minus1                      = -1;
99 static int hf_h264_slice_group_map_type                         = -1;
100 static int hf_h264_num_ref_idx_l0_active_minus1         = -1;
101 static int hf_h264_num_ref_idx_l1_active_minus1         = -1;
102 static int hf_h264_weighted_pred_flag                           = -1;
103 static int hf_h264_weighted_bipred_idc                          = -1;
104 static int hf_h264_pic_init_qp_minus26                          = -1;
105 static int hf_h264_pic_init_qs_minus26                          = -1;
106 static int hf_h264_chroma_qp_index_offset                       = -1;
107 static int hf_h264_deblocking_filter_control_present_flag = -1;
108 static int hf_h264_constrained_intra_pred_flag          = -1;
109 static int hf_h264_redundant_pic_cnt_present_flag       = -1;
110 static int hf_h264_transform_8x8_mode_flag                      = -1;
111 static int hf_h264_pic_scaling_matrix_present_flag      = -1;
112 static int hf_h264_second_chroma_qp_index_offset        = -1;
113 static int hf_h264_par_profile                                          = -1;
114 static int hf_h264_par_profile_b                                        = -1;
115 static int hf_h264_par_profile_m                                        = -1;
116 static int hf_h264_par_profile_e                                        = -1;
117 static int hf_h264_par_profile_h                                        = -1;
118 static int hf_h264_par_profile_h10                                      = -1;
119 static int hf_h264_par_profile_h4_2_2                           = -1;
120 static int hf_h264_par_profile_h4_4_4                           = -1;
121 static int hf_h264_par_add_mode_sup                                     = -1;
122 static int hf_h264_par_AdditionalModesSupported         = -1;
123 static int hf_h264_par_add_mode_sup_rcdo                        = -1;
124 static int hf_h264_par_ProfileIOP                                       = -1;
125 static int hf_h264_par_constraint_set0_flag                     = -1;
126 static int hf_h264_par_constraint_set1_flag                     = -1;
127 static int hf_h264_par_constraint_set2_flag                     = -1;
128
129 /* VUI parameters */
130 static int hf_h264_aspect_ratio_info_present_flag       = -1;
131 static int hf_h264_aspect_ratio_idc                                     = -1;
132 static int hf_h264_sar_width                                            = -1;
133 static int hf_h264_sar_height                                           = -1;
134 static int hf_h264_overscan_info_present_flag           = -1;
135 static int hf_h264_overscan_appropriate_flag            = -1;
136 static int hf_h264_video_signal_type_present_flag       = -1;
137 static int hf_h264_video_format                                         = -1;
138 static int hf_h264_video_full_range_flag                        = -1;
139 static int hf_h264_colour_description_present_flag      = -1;
140 static int hf_h264_colour_primaries                                     = -1;
141 static int hf_h264_transfer_characteristics                     = -1;
142 static int hf_h264_matrix_coefficients                          = -1;
143 static int hf_h264_chroma_loc_info_present_flag         = -1;
144 static int hf_h264_chroma_sample_loc_type_top_field = -1;
145 static int hf_h264_chroma_sample_loc_type_bottom_field = -1;
146 static int hf_h264_timing_info_present_flag                     = -1;
147 static int hf_h264_num_units_in_tick                            = -1;
148 static int hf_h264_time_scale                                           = -1;
149 static int hf_h264_fixed_frame_rate_flag                        = -1;
150 static int hf_h264_nal_hrd_parameters_present_flag      = -1;
151 static int hf_h264_vcl_hrd_parameters_present_flag      = -1;
152 static int hf_h264_low_delay_hrd_flag                           = -1;
153 static int hf_h264_pic_struct_present_flag                      = -1;
154 static int hf_h264_bitstream_restriction_flag           = -1;
155 static int hf_h264_motion_vectors_over_pic_boundaries_flag = -1;
156 static int hf_h264_max_bytes_per_pic_denom                      = -1;
157 static int hf_h264_max_bits_per_mb_denom                        = -1;
158 static int hf_h264_log2_max_mv_length_horizontal        = -1;
159 static int hf_h264_log2_max_mv_length_vertical          = -1;
160 static int hf_h264_num_reorder_frames                           = -1;
161 static int hf_h264_max_dec_frame_buffering                      = -1;
162 static int hf_h264_cpb_cnt_minus1                                       = -1;
163 static int hf_h264_bit_rate_scale                                       = -1;
164 static int hf_h264_cpb_size_scale                                       = -1;
165 static int hf_h264_bit_rate_value_minus1                        = -1;
166 static int hf_h264_cpb_size_value_minus1                        = -1;
167 static int hf_h264_cbr_flag                                                     = -1;
168 static int hf_h264_initial_cpb_removal_delay_length_minus1 = -1;
169 static int hf_h264_cpb_removal_delay_length_minus1      = -1;
170 static int hf_h264_dpb_output_delay_length_minus11      = -1;
171 static int hf_h264_time_offset_length                           = -1;
172
173 static int hf_h264_first_mb_in_slice                            = -1;
174 static int hf_h264_slice_type                                           = -1;
175 static int hf_h264_slice_id                                                     = -1;
176 static int hf_h264_payloadsize                                          = -1;
177 static int hf_h264_payloadtype                                                  = -1;
178 static int hf_h264_frame_num                                            = -1;
179
180 /* Initialize the subtree pointers */
181 static int ett_h264 = -1;
182 static int ett_h264_profile = -1;
183 static int ett_h264_nal = -1;
184 static int ett_h264_fua = -1;
185 static int ett_h264_stream = -1;
186 static int ett_h264_nal_unit = -1;
187 static int ett_h264_par_profile = -1;
188 static int ett_h264_par_AdditionalModesSupported = -1;
189 static int ett_h264_par_ProfileIOP                               = -1;
190
191 /* The dynamic payload type range which will be dissected as H.264 */
192
193 #define RTP_PT_DEFAULT_RANGE "0"
194 static range_t *temp_dynamic_payload_type_range = NULL;
195
196 static dissector_handle_t h264_handle;
197
198 /* syntax tables in subclause 7.3 is equal to
199  * ue(v), me(v), se(v), or te(v).
200  */
201 typedef enum {
202     H264_UE_V = 0,
203     H264_ME_V = 1,
204     H264_SE_V = 2,
205     H264_TE_V = 3
206 } h264_golomb_descriptors;
207
208
209 static const true_false_string h264_f_bit_vals = {
210   "Bit errors or other syntax violations",
211   "No bit errors or other syntax violations"
212 };
213 static const true_false_string h264_start_bit_vals = {
214   "the first packet of FU-A picture",
215   "Not the first packet of FU-A picture"
216 };
217 static const true_false_string h264_end_bit_vals = {
218   "the last packet of FU-A picture",
219   "Not the last packet of FU-A picture"
220 };
221 static const true_false_string h264_forbidden_bit_vals = {
222   "Forbidden Bit of FU-A",
223   "Not Forbidden Bit of FU-A"
224 };
225
226 #define H264_SEQ_PAR_SET                7
227 #define H264_PIC_PAR_SET                8
228
229 static const value_string h264_type_values[] = {
230         { 0,    "Undefined" },
231         { 1,    "NAL unit - Coded slice of a non-IDR picture" },        /* Single NAL unit packet per H.264 */
232         { 2,    "NAL unit - Coded slice data partition A" },
233         { 3,    "NAL unit - Coded slice data partition B" },
234         { 4,    "NAL unit - Coded slice data partition C" },
235         { 5,    "NAL unit - Coded slice of an IDR picture" },
236         { 6,    "NAL unit - Supplemental enhancement information (SEI)" },
237         { H264_SEQ_PAR_SET,     "NAL unit - Sequence parameter set" },                          /* 7 */
238         { H264_PIC_PAR_SET,     "NAL unit - Picture parameter set" },                           /* 8 */
239         { 9,    "NAL unit - Access unit delimiter" },
240         { 10,   "NAL unit - End of sequence" },
241         { 11,   "NAL unit - End of stream" },
242         { 12,   "NAL unit - Filler data" },
243         { 13,   "NAL unit - Sequence parameter set extension" },
244         { 14,   "NAL unit - Reserved" },
245         { 15,   "NAL unit - Reserved" },
246         { 16,   "NAL unit - Reserved" },
247         { 17,   "NAL unit - Reserved" },
248         { 18,   "NAL unit - Reserved" },
249         { 19,   "NAL unit - Coded slice of an auxiliary coded picture without partitioning" },
250         { 20,   "NAL unit - Reserved" },
251         { 21,   "NAL unit - Reserved" },
252         { 22,   "NAL unit - Reserved" },
253         { 23,   "NAL unit - Reserved" },
254         { 24,   "STAP-A" },             /* Single-time aggregation packet */
255         { 25,   "STAP-B" },             /* Single-time aggregation packet */
256         { 26,   "MTAP16" },             /* Multi-time aggregation packet */
257         { 27,   "MTAP24" },             /* Multi-time aggregation packet */
258         { 28,   "FU-A" },               /* Fragmentation unit */
259         { 29,   "FU-B" },               /* Fragmentation unit */
260         { 30,   "undefined" },
261         { 31,   "undefined" },
262         { 0,    NULL }
263 };
264
265
266 static const value_string h264_profile_idc_values[] = {
267         { 66,   "Baseline profile" },
268         { 77,   "Main profile" },
269         { 88,   "Extended profile" },
270         { 100,  "High profile" },
271         { 110,  "High 10 profile" },
272         { 122,  "High 4:2:2 profile" },
273         { 144,  "High 4:4:4 profile" },
274         { 0,    NULL }
275 };
276
277 static const value_string h264_level_bitrate_values[] = {
278         { 10,   "64kb/s" },
279         { 11,   "192kb/s" },
280         { 12,   "384kb/s" },
281         { 13,   "2 Mb/s" },
282         { 20,   "2 Mb/s" },
283         { 21,   "4 Mb/s" },
284         { 22,   "4 Mb/s" },
285         { 30,   "10 Mb/s" },
286         { 31,   "14 Mb/s" },
287         { 32,   "20 Mb/s" },
288         { 40,   "20 Mb/s" },
289         { 41,   "50 Mb/s" },
290         { 42,   "50 Mb/s" },
291         { 50,   "135 Mb/s" },
292         { 51,   "240 Mb/s" },
293         { 0,    NULL }
294 };
295
296 static const value_string h264_nal_unit_type_vals[] = {
297         { 0,    "Unspecified" },
298         { 1,    "Coded slice of a non-IDR picture" },
299         { 2,    "Coded slice data partition A" },
300         { 3,    "Coded slice data partition B" },
301         { 4,    "Coded slice data partition C" },
302         { 5,    "Coded slice of an IDR picture" },
303         { 6,    "Supplemental enhancement information (SEI)" },
304         { 7,    "Sequence parameter set" },
305         { 8,    "Picture parameter set" },
306         { 9,    "Access unit delimiter" },
307         { 10,   "End of sequence" },
308         { 11,   "End of stream" },
309         { 12,   "Filler data" },
310         { 13,   "Sequence parameter set extension" },
311         { 14,   "Reserved" },
312         { 15,   "Reserved" },
313         { 16,   "Reserved" },
314         { 17,   "Reserved" },
315         { 18,   "Reserved" },
316         { 19,   "Coded slice of an auxiliary coded picture without partitioning" },
317         { 20,   "Reserved" },
318         { 21,   "Reserved" },
319         { 22,   "Reserved" },
320         { 23,   "Reserved" },
321         { 24,   "Unspecified" },
322         { 25,   "Unspecified" },
323         { 26,   "Unspecified" },
324         { 27,   "Unspecified" },
325         { 28,   "FU-A" },
326         { 29,   "Unspecified" },
327         { 30,   "Unspecified" },
328         { 31,   "Unspecified" },
329         { 0,    NULL }
330 };
331
332 static const value_string h264_slice_group_map_type_vals[] = {
333         { 0,    "Interleaved slice groups" },
334         { 1,    "Dispersed slice group mapping" },
335         { 2,    "One or more foreground slice groups and a leftover slice group" },
336         { 3,    "Changing slice groups" },
337         { 4,    "Changing slice groups" },
338         { 5,    "Changing slice groups" },
339         { 6,    "Explicit assignment of a slice group to each slice group map unit" },
340         { 0,    NULL }
341 };
342
343 /* Table 7-6 Name association to slice_type */
344 static const value_string h264_slice_type_vals[] = {
345         { 0,    "P (P slice)" },
346         { 1,    "B (B slice)" },
347         { 2,    "I (I slice)" },
348         { 3,    "SP (SP slice)" },
349         { 4,    "SI (SI slice)" },
350         { 5,    "P (P slice)" },
351         { 6,    "B (B slice)" },
352         { 7,    "I (I slice)" },
353         { 8,    "SP (SP slice)" },
354         { 9,    "SI (SI slice)" },
355         { 0,    NULL }
356 };
357 /* byte_aligned( ) is specified as follows.
358  * - If the current position in the bitstream is on a byte boundary, i.e.,
359  *       the next bit in the bitstream is the first bit in a byte,
360  *       the return value of byte_aligned( ) is equal to TRUE.
361  * - Otherwise, the return value of byte_aligned( ) is equal to FALSE.
362  */
363 static gboolean
364 h264_byte_aligned(int bit_offset)
365 {
366         if(bit_offset&0x3)
367                 return FALSE;
368
369         return TRUE;
370 }
371 /* Expect a tvb and a bit offset into the tvb
372  * returns the valu and bit_offset
373  */
374 #define cVALS(x) (const value_string*)(x)
375
376 guint32
377 dissect_h264_exp_golomb_code(proto_tree *tree, int hf_index, tvbuff_t *tvb, gint *start_bit_offset, h264_golomb_descriptors descriptor)
378 /*(tvbuff_t *tvb, gint *start_bit_offset) */
379 {
380         gint            leading_zero_bits, bit_offset, start_offset;
381         guint32         codenum, mask, value, tmp;
382         gint32          se_value=0;
383         gint            b;
384         char *str;
385         int bit;
386         int i;
387         header_field_info *hf_field = NULL;
388
389         start_offset = *start_bit_offset>>3;
390
391         if(hf_index > -1)
392                 hf_field = proto_registrar_get_nth(hf_index);
393
394         bit_offset = *start_bit_offset;
395
396         /* prepare the string */
397         str=ep_alloc(256);
398         str[0]='\0';
399         for(bit=0;bit<((int)(bit_offset&0x07));bit++){
400                 if(bit&&(!(bit%4))){
401                         g_strlcat(str, " ", 256);
402                 }
403                 g_strlcat(str,".", 256);
404         }
405
406
407         leading_zero_bits = -1;
408         for( b = 0; !b; leading_zero_bits++ ){
409                 if(bit&&(!(bit%4))){
410                         g_strlcat(str, " ", 256);
411                 }
412                 if(bit&&(!(bit%8))){
413                         g_strlcat(str, " ", 256);
414                 }
415                 b = tvb_get_bits8(tvb, bit_offset, 1);
416                 if(b != 0){
417                         g_strlcat(str, "1", 256);
418                 } else {
419                         g_strlcat(str, "0", 256);
420                 }
421                 bit++;
422                 bit_offset++;
423         }
424
425         if(leading_zero_bits==0){
426                 codenum = 0;
427                 *start_bit_offset = bit_offset;
428                 for(;bit%8;bit++){
429                         if(bit&&(!(bit%4))){
430                                 g_strlcat(str, " ", 256);
431                         }
432                 g_strlcat(str,".", 256);
433                 }
434                 if(hf_field){
435                         g_strlcat(str," = ", 256);
436                         g_strlcat(str,hf_field->name, 256);
437                         switch (descriptor){
438                         case H264_SE_V:
439                                 /* if the syntax element is coded as se(v),
440                                  * the value of the syntax element is derived by invoking the
441                                  * mapping process for signed Exp-Golomb codes as specified in
442                                  * subclause 9.1.1 with codeNum as the input.
443                                  */
444                                 if(hf_field->type==FT_INT32){
445                                         if (hf_field->strings) {
446                                                 proto_tree_add_int_format(tree, hf_index, tvb, start_offset, 1, codenum,
447                                                           "%s: %s (%d)",
448                                                           str,
449                                                           val_to_str(codenum, cVALS(hf_field->strings), "Unknown "),
450                                                           codenum);
451                                         }else{
452                                                 switch(hf_field->display){
453                                                         case BASE_DEC:
454                                                                 proto_tree_add_int_format(tree, hf_index, tvb, start_offset, 1, codenum,
455                                                                  "%s: %d",
456                                                                           str,
457                                                                           codenum);
458                                                                 break;
459                                                         default:
460                                                                 DISSECTOR_ASSERT_NOT_REACHED();
461                                                                 break;
462                                                 }
463                                         }
464                                 }
465                                 return codenum;
466                         default:
467                                 break;
468                         }
469                         if(hf_field->type==FT_UINT32){
470                                 if (hf_field->strings) {
471                                         proto_tree_add_uint_format(tree, hf_index, tvb, start_offset, 1, codenum,
472                                                   "%s: %s (%u)",
473                                                   str,
474                                                   val_to_str(codenum, cVALS(hf_field->strings), "Unknown "),
475                                                   codenum);
476                                 }else{
477                                         switch(hf_field->display){
478                                                 case BASE_DEC:
479                                                         proto_tree_add_uint_format(tree, hf_index, tvb, start_offset, 1, codenum,
480                                                          "%s: %u",
481                                                                   str,
482                                                                   codenum);
483                                                         break;
484                                                 case BASE_HEX:
485                                                         proto_tree_add_uint_format(tree, hf_index, tvb, start_offset, 1, codenum,
486                                                      "%s: 0x%x",
487                                                                   str,
488                                                                   codenum);
489                                                         break;
490                                                 default:
491                                                         DISSECTOR_ASSERT_NOT_REACHED();
492                                                         break;
493                                         }
494                                 }
495                         }else{
496                                 /* Only allow guint32 */
497                                 DISSECTOR_ASSERT_NOT_REACHED();
498                         }
499                 }
500                 return codenum;
501         }
502
503         /*
504         Syntax elements coded as ue(v), me(v), or se(v) are Exp-Golomb-coded. Syntax elements coded as te(v) are truncated
505         Exp-Golomb-coded. The parsing process for these syntax elements begins with reading the bits starting at the current
506         location in the bitstream up to and including the first non-zero bit, and counting the number of leading bits that are
507         equal to 0. This process is specified as follows:
508         leadingZeroBits = -1;
509         for( b = 0; !b; leadingZeroBits++ )
510         b = read_bits( 1 )
511         The variable codeNum is then assigned as follows:
512         codeNum = 2leadingZeroBits - 1 + read_bits( leadingZeroBits )
513         where the value returned from read_bits( leadingZeroBits ) is interpreted as a binary representation of an unsigned
514         integer with most significant bit written first.
515         */
516         codenum = 1;
517         codenum = codenum << leading_zero_bits;
518         mask = codenum>>1;
519         if (leading_zero_bits > 32)
520                 DISSECTOR_ASSERT_NOT_REACHED();
521         else if (leading_zero_bits > 16)
522                 value = tvb_get_bits32(tvb, bit_offset,leading_zero_bits, ENC_BIG_ENDIAN);
523         else if (leading_zero_bits > 8)
524                 value = tvb_get_bits16(tvb, bit_offset,leading_zero_bits, ENC_BIG_ENDIAN);
525         else
526                 value = tvb_get_bits8(tvb, bit_offset,leading_zero_bits );
527         codenum = (codenum-1) + value;
528         bit_offset = bit_offset + leading_zero_bits;
529
530         /* read the bits for the int */
531         for(i=0;i<leading_zero_bits;i++){
532                 if(bit&&(!(bit%4))){
533                         g_strlcat(str, " ", 256);
534                 }
535                 if(bit&&(!(bit%8))){
536                         g_strlcat(str, " ", 256);
537                 }
538                 bit++;
539                 tmp = value & mask;
540                 if(tmp != 0){
541                         g_strlcat(str, "1", 256);
542                 } else {
543                         g_strlcat(str, "0", 256);
544                 }
545                 mask = mask>>1;
546         }
547         for(;bit%8;bit++){
548                 if(bit&&(!(bit%4))){
549                         g_strlcat(str, " ", 256);
550                 }
551                 g_strlcat(str,".", 256);
552         }
553
554         switch (descriptor){
555         case H264_SE_V:
556                 /* if the syntax element is coded as se(v),
557                  * the value of the syntax element is derived by invoking the
558                  * mapping process for signed Exp-Golomb codes as specified in
559                  * subclause 9.1.1 with codeNum as the input.
560                  *              k+1
561                  * (-1)    Ceil( k/2 )
562                  */
563                 se_value = (codenum + 1) >> 1;
564                 if (!(se_value & 1)){
565                         se_value =  - se_value;
566                 }
567                 break;
568         default:
569                 break;
570         }
571
572         if(hf_field){
573                 g_strlcat(str," = ", 256);
574                 g_strlcat(str,hf_field->name, 256);
575                 switch (descriptor){
576                 case H264_SE_V:
577                         g_strlcat(str,"(se(v))", 256);
578                         /* if the syntax element is coded as se(v),
579                          * the value of the syntax element is derived by invoking the
580                          * mapping process for signed Exp-Golomb codes as specified in
581                          * subclause 9.1.1 with codeNum as the input.
582                          */
583                         break;
584                 default:
585                 break;
586                 }
587                 if((hf_field->type==FT_UINT32)&&(descriptor==H264_UE_V)){
588                         if (hf_field->strings) {
589                                 proto_tree_add_uint_format(tree, hf_index, tvb, start_offset, 1, codenum,
590                                                   "%s: %s (%u)",
591                                                   str,
592                                                   val_to_str(codenum, cVALS(hf_field->strings), "Unknown "),
593                                                   codenum);
594                         }else{
595                                 switch(hf_field->display){
596                                         case BASE_DEC:
597                                                 proto_tree_add_uint_format(tree, hf_index, tvb, start_offset, 1, codenum,
598                                                  "%s: %u",
599                                                           str,
600                                                           codenum);
601                                                 break;
602                                         case BASE_HEX:
603                                                 proto_tree_add_uint_format(tree, hf_index, tvb, start_offset, 1, codenum,
604                                              "%s: 0x%x",
605                                                           str,
606                                                           codenum);
607                                                 break;
608                                         default:
609                                                 DISSECTOR_ASSERT_NOT_REACHED();
610                                                 break;
611                                 }
612                         }
613                 }else if((hf_field->type==FT_INT32)&&(descriptor==H264_SE_V)){
614                         if (hf_field->strings) {
615                                 proto_tree_add_int_format(tree, hf_index, tvb, start_offset, 1, codenum,
616                                                   "%s: %s (%d)",
617                                                   str,
618                                                   val_to_str(codenum, cVALS(hf_field->strings), "Unknown "),
619                                                   se_value);
620                         }else{
621                                 switch(hf_field->display){
622                                         case BASE_DEC:
623                                                 proto_tree_add_int_format(tree, hf_index, tvb, start_offset, 1, codenum,
624                                                  "%s: %d",
625                                                           str,
626                                                           se_value);
627                                                 break;
628                                         default:
629                                                 DISSECTOR_ASSERT_NOT_REACHED();
630                                                 break;
631                                 }
632                         }
633                         *start_bit_offset = bit_offset;
634                         return se_value;
635
636                 }else{
637                         /* Only allow guint32 */
638                         DISSECTOR_ASSERT_NOT_REACHED();
639                 }
640         }
641
642         *start_bit_offset = bit_offset;
643         return codenum;
644
645 }
646
647 /* This function is adapted to parsing NAL units from SDP data where the
648  * base64 coding may add extra padding
649  */
650
651 static gboolean
652 more_rbsp_data(proto_tree *tree _U_, tvbuff_t *tvb, packet_info *pinfo _U_, gint bit_offset)
653 {
654
655         int offset;
656         int remaining_length;
657         int last_one_bit;
658         guint8 b = 0;
659
660         /* XXX might not be the best way of doing things but:
661          * Serch from the end of the tvb for the first '1' bit
662          * assuming that its's the RTBSP stop bit
663          */
664
665         /* Set offset to the byte we are treating */
666         offset = bit_offset>>3;
667         remaining_length = tvb_length_remaining(tvb,offset);
668         /* If there is more then 2 bytes left there *should* be more data */
669         if(remaining_length>2){
670                 return TRUE;
671         }
672         /* Start from last bit */
673         last_one_bit = (tvb_length(tvb) << 3);
674
675         for( b = 0; !b; ){
676                 last_one_bit--;
677                 b = tvb_get_bits8(tvb, last_one_bit, 1);
678         }
679
680         if( last_one_bit == bit_offset){
681                 return FALSE;
682         }
683
684         return TRUE;
685 }
686
687 static int
688 dissect_h264_rbsp_trailing_bits(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo _U_, gint bit_offset)
689 {
690         gint remaining_bits=0;
691
692         proto_tree_add_bits_item(tree, hf_h264_rbsp_stop_bit, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
693         bit_offset++;
694
695         if((bit_offset&0x7)!=0){
696                 remaining_bits = 8 - (bit_offset&0x7);
697                 proto_tree_add_bits_item(tree, hf_h264_rbsp_trailing_bits, tvb, bit_offset, remaining_bits, ENC_BIG_ENDIAN);
698         }
699
700         return bit_offset+remaining_bits;
701 }
702
703 /*
704  * 7.3.3 Slice header syntax
705  * slice_header( )
706  * XXX Just parse a few bytes
707  */
708 static int
709 dissect_h264_slice_header(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo _U_, gint bit_offset)
710 {
711         /* first_mb_in_slice 2 ue(v) */
712         dissect_h264_exp_golomb_code(tree, hf_h264_first_mb_in_slice, tvb, &bit_offset, H264_UE_V);
713
714         /* slice_type 2 ue(v) */
715         dissect_h264_exp_golomb_code(tree, hf_h264_slice_type, tvb, &bit_offset, H264_UE_V);
716
717         /* pic_parameter_set_id 2 ue(v) */
718         dissect_h264_exp_golomb_code(tree, hf_h264_pic_parameter_set_id, tvb, &bit_offset, H264_UE_V);
719
720         /* frame_num 2 u(v) */
721         /*
722          * represented by log2_max_frame_num_minus4 + 4 bits in
723          * the bitstream
724          * proto_tree_add_bits_item(tree, hf_h264_frame_num, tvb, bit_offset, 4, ENC_BIG_ENDIAN);
725          */
726
727
728         return bit_offset;
729 }
730
731
732 /* 7.3.2.1.1 Scaling list syntax
733  *
734  * scaling_list( scalingList, sizeOfScalingList, useDefaultScalingMatrixFlag )
735  */
736 /*
737 static int
738 dissect_h264_scaling_list(proto_tree *tree, tvbuff_t *tvb, gint bit_offset, int hf_index_scalinglist,
739                                                   guint8 sizeOfScalingList, int hf_index_usedefaultscalingmatrixflag)
740 {
741
742         guint8 lastScale = 8;
743         guint8 nextScale = 8:
744         guint8 delta_scale;
745
746         for( j = 0; j < sizeOfScalingList; j++ ) {
747           if( nextScale != 0 ) {
748                   / delta_scale 0 | 1 se(v) /
749                   delta_scale = dissect_h264_exp_golomb_code(tree, hf_h264_delta_scale, tvb, &bit_offset);
750                   nextScale = ( lastScale + delta_scale + 256 ) % 256;
751                   useDefaultScalingMatrixFlag = ( j == 0 && nextScale == 0 );
752         }
753         scalingList[ j ] = ( nextScale == 0 ) ? lastScale : nextScale
754         lastScale = scalingList[ j ]
755         }
756 }
757 */
758 /* E.1.2 HRD parameters syntax */
759 static int
760 dissect_h264_hrd_parameters(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo _U_, gint bit_offset)
761 {
762         guint8 cpb_cnt_minus1;
763         int SchedSelIdx;
764
765
766         /* cpb_cnt_minus1 0 ue(v) */
767         cpb_cnt_minus1 = dissect_h264_exp_golomb_code(tree, hf_h264_cpb_cnt_minus1, tvb, &bit_offset, H264_UE_V);
768
769         /* bit_rate_scale 0 u(4) */
770         proto_tree_add_bits_item(tree, hf_h264_bit_rate_scale, tvb, bit_offset, 4, ENC_BIG_ENDIAN);
771         bit_offset = bit_offset + 4;
772
773         /* cpb_size_scale 0 u(4) */
774         proto_tree_add_bits_item(tree, hf_h264_cpb_size_scale, tvb, bit_offset, 4, ENC_BIG_ENDIAN);
775         bit_offset = bit_offset + 4;
776         /* for( SchedSelIdx = 0; SchedSelIdx <= cpb_cnt_minus1; SchedSelIdx++ ) { */
777         for( SchedSelIdx = 0; SchedSelIdx <= cpb_cnt_minus1; SchedSelIdx++ ) {
778
779                 /* bit_rate_value_minus1[ SchedSelIdx ] 0 ue(v) */
780                 dissect_h264_exp_golomb_code(tree, hf_h264_bit_rate_value_minus1, tvb, &bit_offset, H264_UE_V);
781
782                 /* cpb_size_value_minus1[ SchedSelIdx ] 0 ue(v)*/
783                 dissect_h264_exp_golomb_code(tree, hf_h264_cpb_size_value_minus1, tvb, &bit_offset, H264_UE_V);
784
785                 /* cbr_flag[ SchedSelIdx ] 0 u(1) */
786                 proto_tree_add_bits_item(tree, hf_h264_cbr_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
787                 bit_offset++;
788         }
789         /* initial_cpb_removal_delay_length_minus1 0 u(5) */
790         proto_tree_add_bits_item(tree, hf_h264_initial_cpb_removal_delay_length_minus1, tvb, bit_offset, 5, ENC_BIG_ENDIAN);
791         bit_offset = bit_offset + 5;
792
793         /* cpb_removal_delay_length_minus1 0 u(5) */
794         proto_tree_add_bits_item(tree, hf_h264_cpb_removal_delay_length_minus1, tvb, bit_offset, 5, ENC_BIG_ENDIAN);
795         bit_offset = bit_offset + 5;
796
797         /* dpb_output_delay_length_minus1 0 u(5) */
798         proto_tree_add_bits_item(tree, hf_h264_dpb_output_delay_length_minus11, tvb, bit_offset, 5, ENC_BIG_ENDIAN);
799         bit_offset = bit_offset + 5;
800
801         /* time_offset_length 0 u(5) */
802         proto_tree_add_bits_item(tree, hf_h264_time_offset_length, tvb, bit_offset, 5, ENC_BIG_ENDIAN);
803         bit_offset = bit_offset + 5;
804
805         return bit_offset;
806 }
807
808 #define EXTENDED_SAR 255
809
810 /* E.1.1 VUI parameters syntax */
811
812 /* Table E-2 - Meaning of video_format */
813 static const value_string h264_video_format_vals[] = {
814         { 22,   "reserved_sei_message)" },
815         { 0,    "Component" },
816         { 1,    "PAL" },
817         { 2,    "NTSC" },
818         { 3,    "SECAM" },
819         { 4,    "MAC" },
820         { 5,    "Unspecified video format" },
821         { 6,    "Reserved" },
822         { 7,    "Reserved" },
823         { 0,    NULL }
824 };
825 static int
826 dissect_h264_vui_parameters(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo, gint bit_offset)
827 {
828
829         guint8 aspect_ratio_info_present_flag, aspect_ratio_idc, overscan_info_present_flag;
830         guint8 video_signal_type_present_flag, colour_description_present_flag, chroma_loc_info_present_flag;
831         guint8 timing_info_present_flag, nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
832         guint8 bitstream_restriction_flag;
833
834         /* vui_parameters( ) {
835          * aspect_ratio_info_present_flag 0 u(1)
836          */
837         aspect_ratio_info_present_flag = tvb_get_bits8(tvb, bit_offset, 1);
838         proto_tree_add_bits_item(tree, hf_h264_aspect_ratio_info_present_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
839         bit_offset++;
840
841         if( aspect_ratio_info_present_flag ) {
842                 /* aspect_ratio_idc 0 u(8) */
843                 aspect_ratio_idc = tvb_get_bits8(tvb, bit_offset, 8);
844                 proto_tree_add_bits_item(tree, hf_h264_aspect_ratio_idc, tvb, bit_offset, 8, ENC_BIG_ENDIAN);
845                 bit_offset = bit_offset + 8;
846
847                 if( aspect_ratio_idc == EXTENDED_SAR ) {
848                         /* sar_width 0 u(16) */
849                         proto_tree_add_bits_item(tree, hf_h264_sar_width, tvb, bit_offset, 16, ENC_BIG_ENDIAN);
850                         bit_offset = bit_offset + 16;
851
852                         /* sar_height 0 u(16) */
853                         proto_tree_add_bits_item(tree, hf_h264_sar_height, tvb, bit_offset, 16, ENC_BIG_ENDIAN);
854                         bit_offset = bit_offset + 16;
855                 }
856         }
857         /* overscan_info_present_flag 0 u(1) */
858         overscan_info_present_flag = tvb_get_bits8(tvb, bit_offset, 1);
859         proto_tree_add_bits_item(tree, hf_h264_overscan_info_present_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
860         bit_offset++;
861
862         if( overscan_info_present_flag ){
863                 /* overscan_appropriate_flag 0 u(1) */
864                 proto_tree_add_bits_item(tree, hf_h264_overscan_appropriate_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
865                 bit_offset++;
866         }
867
868         /* video_signal_type_present_flag 0 u(1) */
869         video_signal_type_present_flag = tvb_get_bits8(tvb, bit_offset, 1);
870         proto_tree_add_bits_item(tree, hf_h264_video_signal_type_present_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
871         bit_offset++;
872
873         if( video_signal_type_present_flag ) {
874                 /* video_format 0 u(3) > */
875                 proto_tree_add_bits_item(tree, hf_h264_video_format, tvb, bit_offset, 3, ENC_BIG_ENDIAN);
876                 bit_offset = bit_offset + 3;
877
878                 /* video_full_range_flag 0 u(1)*/
879                 proto_tree_add_bits_item(tree, hf_h264_video_full_range_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
880                 bit_offset++;
881
882                 /* colour_description_present_flag 0 u(1) */
883                 colour_description_present_flag = tvb_get_bits8(tvb, bit_offset, 1);
884                 proto_tree_add_bits_item(tree, hf_h264_colour_description_present_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
885                 bit_offset++;
886
887                 if( colour_description_present_flag ) {
888                         /* colour_primaries 0 u(8) */
889                         proto_tree_add_bits_item(tree, hf_h264_colour_primaries, tvb, bit_offset, 8, ENC_BIG_ENDIAN);
890                         bit_offset = bit_offset + 8;
891
892                         /* transfer_characteristics 0 u(8) */
893                         proto_tree_add_bits_item(tree, hf_h264_transfer_characteristics, tvb, bit_offset, 8, ENC_BIG_ENDIAN);
894                         bit_offset = bit_offset + 8;
895
896                         /* matrix_coefficients 0 u(8)*/
897                         proto_tree_add_bits_item(tree, hf_h264_matrix_coefficients, tvb, bit_offset, 8, ENC_BIG_ENDIAN);
898                         bit_offset = bit_offset + 8;
899                 }
900         }
901
902         /* chroma_loc_info_present_flag 0 u(1) */
903         chroma_loc_info_present_flag = tvb_get_bits8(tvb, bit_offset, 1);
904         proto_tree_add_bits_item(tree, hf_h264_chroma_loc_info_present_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
905         bit_offset++;
906
907         if( chroma_loc_info_present_flag ) {
908                 /* chroma_sample_loc_type_top_field 0 ue(v) */
909                 dissect_h264_exp_golomb_code(tree, hf_h264_chroma_sample_loc_type_top_field, tvb, &bit_offset, H264_UE_V);
910
911                 /* chroma_sample_loc_type_bottom_field 0 ue(v) */
912                 dissect_h264_exp_golomb_code(tree, hf_h264_chroma_sample_loc_type_bottom_field, tvb, &bit_offset, H264_UE_V);
913         }
914
915         /* timing_info_present_flag 0 u(1) */
916         timing_info_present_flag = tvb_get_bits8(tvb, bit_offset, 1);
917         proto_tree_add_bits_item(tree, hf_h264_timing_info_present_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
918         bit_offset++;
919
920         if( timing_info_present_flag ) {
921                 /* num_units_in_tick 0 u(32) */
922                 proto_tree_add_bits_item(tree, hf_h264_num_units_in_tick, tvb, bit_offset, 32, ENC_BIG_ENDIAN);
923                 bit_offset = bit_offset + 32;
924
925                 /* time_scale 0 u(32) */
926                 proto_tree_add_bits_item(tree, hf_h264_time_scale, tvb, bit_offset, 32, ENC_BIG_ENDIAN);
927                 bit_offset = bit_offset + 32;
928
929                 /* fixed_frame_rate_flag 0 u(1) */
930                 proto_tree_add_bits_item(tree, hf_h264_fixed_frame_rate_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
931                 bit_offset++;
932         }
933         /* nal_hrd_parameters_present_flag 0 u(1) */
934         nal_hrd_parameters_present_flag = tvb_get_bits8(tvb, bit_offset, 1);
935         proto_tree_add_bits_item(tree, hf_h264_nal_hrd_parameters_present_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
936         bit_offset++;
937
938         if( nal_hrd_parameters_present_flag ){
939                 /* hrd_parameters( ) */
940                 bit_offset = dissect_h264_hrd_parameters(tree, tvb, pinfo, bit_offset);
941         }
942
943         /* vcl_hrd_parameters_present_flag 0 u(1) */
944         vcl_hrd_parameters_present_flag = tvb_get_bits8(tvb, bit_offset, 1);
945         proto_tree_add_bits_item(tree, hf_h264_vcl_hrd_parameters_present_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
946         bit_offset++;
947
948         if( vcl_hrd_parameters_present_flag ){
949                 /* hrd_parameters( ) */
950                 bit_offset = dissect_h264_hrd_parameters(tree, tvb, pinfo, bit_offset);
951         }
952         if( nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag ){
953                 /* low_delay_hrd_flag 0 u(1) */
954                 proto_tree_add_bits_item(tree, hf_h264_low_delay_hrd_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
955                 bit_offset++;
956         }
957         /* pic_struct_present_flag 0 u(1) */
958         proto_tree_add_bits_item(tree, hf_h264_pic_struct_present_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
959         bit_offset++;
960
961         /* bitstream_restriction_flag 0 u(1) */
962         bitstream_restriction_flag = tvb_get_bits8(tvb, bit_offset, 1);
963         proto_tree_add_bits_item(tree, hf_h264_bitstream_restriction_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
964         bit_offset++;
965
966         if( bitstream_restriction_flag ) {
967                 /* motion_vectors_over_pic_boundaries_flag 0 u(1) */
968                 proto_tree_add_bits_item(tree, hf_h264_motion_vectors_over_pic_boundaries_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
969                 bit_offset++;
970
971                 /* max_bytes_per_pic_denom 0 ue(v) */
972                 dissect_h264_exp_golomb_code(tree, hf_h264_max_bytes_per_pic_denom, tvb, &bit_offset, H264_UE_V);
973
974                 /* max_bits_per_mb_denom 0 ue(v) */
975                 dissect_h264_exp_golomb_code(tree, hf_h264_max_bits_per_mb_denom, tvb, &bit_offset, H264_UE_V);
976
977                 /* log2_max_mv_length_horizontal 0 ue(v) */
978                 dissect_h264_exp_golomb_code(tree, hf_h264_log2_max_mv_length_horizontal, tvb, &bit_offset, H264_UE_V);
979
980                 /* log2_max_mv_length_vertical 0 ue(v) */
981                 dissect_h264_exp_golomb_code(tree, hf_h264_log2_max_mv_length_vertical, tvb, &bit_offset, H264_UE_V);
982
983                 /* num_reorder_frames 0 ue(v) */
984                 dissect_h264_exp_golomb_code(tree, hf_h264_num_reorder_frames, tvb, &bit_offset, H264_UE_V);
985
986                 /* max_dec_frame_buffering 0 ue(v) */
987                 dissect_h264_exp_golomb_code(tree, hf_h264_max_dec_frame_buffering, tvb, &bit_offset, H264_UE_V);
988         }
989
990         return bit_offset;
991 }
992
993
994 /* Used To dissect SDP parameter (H.264)profile */
995 void
996 dissect_h264_profile(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
997 {
998         proto_item *item, *level_item;
999         proto_tree *h264_profile_tree;
1000         gint    offset = 0;
1001         guint8  constraint_set3_flag;
1002         guint32 level_idc;
1003
1004         item = proto_tree_add_item(tree, hf_h264_profile, tvb, offset, -1, ENC_NA);
1005         h264_profile_tree = proto_item_add_subtree(item, ett_h264_profile);
1006
1007         proto_tree_add_item(h264_profile_tree, hf_h264_profile_idc, tvb, offset, 1, ENC_BIG_ENDIAN);
1008         offset++;
1009
1010         constraint_set3_flag = (tvb_get_guint8(tvb,offset)&0x10)>>4;
1011         proto_tree_add_item(h264_profile_tree, hf_h264_constraint_set0_flag, tvb, offset, 1, ENC_BIG_ENDIAN);
1012         proto_tree_add_item(h264_profile_tree, hf_h264_constraint_set1_flag, tvb, offset, 1, ENC_BIG_ENDIAN);
1013         proto_tree_add_item(h264_profile_tree, hf_h264_constraint_set2_flag, tvb, offset, 1, ENC_BIG_ENDIAN);
1014         proto_tree_add_item(h264_profile_tree, hf_h264_constraint_set3_flag, tvb, offset, 1, ENC_BIG_ENDIAN);
1015         proto_tree_add_item(h264_profile_tree, hf_h264_reserved_zero_4bits, tvb, offset, 1, ENC_BIG_ENDIAN);
1016         offset++;
1017
1018         /* A level to which the bitstream conforms shall be indicated by the syntax element level_idc as follows.
1019          *      - If level_idc is equal to 11 and constraint_set3_flag is equal to 1, the indicated level is level 1b.
1020          *      - Otherwise (level_idc is not equal to 11 or constraint_set3_flag is not equal to 1), level_idc shall
1021          *    be set equal to a value of ten times the level number specified in Table A-1 and constraint_set3_flag
1022          *    shall be set equal to 0.
1023          */
1024
1025         level_idc = tvb_get_guint8(tvb,offset);
1026         level_item = proto_tree_add_item(h264_profile_tree, hf_h264_level_idc, tvb, offset, 1, ENC_BIG_ENDIAN);
1027         if((level_idc==11)&&(constraint_set3_flag==1)){
1028                 proto_item_append_text(level_item," [Level 1b (128kb/s)]");
1029         }else{
1030                 proto_item_append_text(level_item," [Level %.1f %s]",((double)level_idc/10),val_to_str(level_idc, h264_level_bitrate_values, "Unknown "));
1031         }
1032
1033 }
1034
1035 /*
1036  * 7.3.2.8 Slice layer without partitioning RBSP syntax
1037  * slice_layer_without_partitioning_rbsp( )
1038  */
1039
1040 static void
1041 dissect_h264_slice_layer_without_partitioning_rbsp(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo, gint offset)
1042 {
1043         gint bit_offset;
1044
1045         bit_offset = offset <<3;
1046
1047         /* slice_header( ) 2 */
1048         bit_offset = dissect_h264_slice_header(tree, tvb, pinfo, bit_offset);
1049         proto_tree_add_text(tree, tvb, bit_offset>>3, -1, "[Not decoded yet]");
1050         return;
1051         /* slice_data( ) * all categories of slice_data( ) syntax * 2 | 3 | 4 */
1052         /* rbsp_slice_trailing_bits( ) */
1053
1054 }
1055
1056 /*
1057  * 7.3.2.9.1 Slice data partition A RBSP syntax
1058  * slice_data_partition_a_layer_rbsp( )
1059  */
1060 static void
1061 dissect_h264_slice_data_partition_a_layer_rbsp(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo _U_, gint offset)
1062 {
1063         gint bit_offset;
1064
1065         bit_offset = offset <<3;
1066
1067         /* slice_header( ) 2 */
1068         bit_offset = dissect_h264_slice_header(tree, tvb, pinfo, bit_offset);
1069
1070         /* slice_id All ue(v) */
1071         dissect_h264_exp_golomb_code(tree, hf_h264_slice_id, tvb, &bit_offset, H264_UE_V);
1072         proto_tree_add_text(tree, tvb, bit_offset>>3, -1, "[Not decoded yet]");
1073         return;
1074         /* slice_data( ) * only category 2 parts of slice_data( ) syntax * 2*/
1075         /* rbsp_slice_trailing_bits( )*/
1076
1077 }
1078
1079 /*
1080  * 7.3.2.9.2 Slice data partition B RBSP syntax
1081  * slice_data_partition_b_layer_rbsp(
1082  */
1083 static void
1084 dissect_h264_slice_data_partition_b_layer_rbsp(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo _U_, gint offset)
1085 {
1086         gint bit_offset;
1087
1088         bit_offset = offset <<3;
1089
1090         /* slice_id All ue(v) */
1091         dissect_h264_exp_golomb_code(tree, hf_h264_slice_id, tvb, &bit_offset, H264_UE_V);
1092         /* if( redundant_pic_cnt_present_flag ) */
1093         /* redundant_pic_cnt All ue(v) */
1094         /* slice_data( ) * only category 3 parts of slice_data( ) syntax * 3 */
1095         /* rbsp_slice_trailing_bits( ) 3 */
1096         proto_tree_add_text(tree, tvb, bit_offset>>3, -1, "[Not decoded yet]");
1097
1098 }
1099
1100 /*
1101  * 7.3.2.9.3 Slice data partition C RBSP syntax
1102  * slice_data_partition_c_layer_rbsp( )
1103  */
1104 static void
1105 dissect_h264_slice_data_partition_c_layer_rbsp(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo _U_, gint offset)
1106 {
1107         gint bit_offset;
1108
1109         bit_offset = offset <<3;
1110
1111         /* slice_id All ue(v) */
1112         dissect_h264_exp_golomb_code(tree, hf_h264_slice_id, tvb, &bit_offset, H264_UE_V);
1113         /* if( redundant_pic_cnt_present_flag ) */
1114         /* redundant_pic_cnt All ue(v) */
1115         /* slice_data( ) * only category 4 parts of slice_data( ) syntax * 4 */
1116         /* rbsp_slice_trailing_bits( ) 4 */
1117         proto_tree_add_text(tree, tvb, bit_offset>>3, -1, "[Not decoded yet]");
1118
1119 }
1120 /* D.1.6 User data unregistered SEI message syntax */
1121
1122 static int
1123 h264_user_data_unregistered(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo _U_, gint bit_offset, guint32 payloadSize)
1124 {
1125         /* user_data_unregistered( payloadSize ) { C Descriptor */
1126         /* uuid_iso_iec_11578 5 u(128)
1127          * uuid_iso_iec_11578 shall have a value specified as a UUID
1128          * according to the procedures of ISO/IEC 11578:1996 Annex A.
1129          */
1130         proto_tree_add_text(tree, tvb, bit_offset>>3, 16, "uuid_iso_iec_1157");
1131         bit_offset+=128;
1132         /*      for( i = 16; i < payloadSize; i++ )
1133          *      user_data_payload_byte 5 b(8)
1134          */
1135         proto_tree_add_text(tree, tvb, bit_offset>>3, payloadSize-16, "user_data_payload");
1136         bit_offset+=(payloadSize-16)<<3;
1137
1138                 return bit_offset;
1139 }
1140 /* D.1 SEI payload syntax */
1141 static const value_string h264_sei_payload_vals[] = {
1142         { 0,    "buffering_period" },
1143         { 1,    "pic_timing" },
1144         { 2,    "pan_scan_rect" },
1145         { 3,    "filler_payload" },
1146         { 4,    "user_data_registered_itu_t_t35" },
1147         { 5,    "user_data_unregistered" },
1148         { 6,    "recovery_point" },
1149         { 7,    "dec_ref_pic_marking_repetition" },
1150         { 8,    "spare_pic" },
1151         { 9,    "scene_inf)" },
1152         { 10,   "sub_seq_info)" },
1153         { 11,   "sub_seq_layer_characteristics" },
1154         { 12,   "sub_seq_characteristics" },
1155         { 13,   "full_frame_freeze_release" },
1156         { 14,   "full_frame_freeze_release" },
1157         { 15,   "full_frame_snapshot" },
1158         { 16,   "progressive_refinement_segment_start" },
1159         { 17,   "progressive_refinement_segment_end" },
1160         { 18,   "motion_constrained_slice_group_set" },
1161         { 19,   "film_grain_characteristics)" },
1162         { 20,   "deblocking_filter_display_preference)" },
1163         { 21,   "stereo_video_info)" },
1164         { 22,   "reserved_sei_message)" },
1165         { 0,    NULL }
1166 };
1167
1168 static int
1169 h264_sei_payload(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo _U_, gint bit_offset, guint32 payloadType, guint32 payloadSize)
1170 {
1171         /* sei_payload( payloadType, payloadSize ) { C Descriptor */
1172         if( payloadType == 0 ){
1173                 /* buffering_period( payloadSize ) 5 */
1174                 bit_offset = bit_offset +(payloadSize<<3);
1175 #if 0
1176         }else if( payloadType == 1 ){
1177                 /* pic_timing( payloadSize ) 5 */
1178         }else if( payloadType == 2 ){
1179                 /* pan_scan_rect( payloadSize ) 5 */
1180         }else if( payloadType == 3 ){
1181                 /* filler_payload( payloadSize ) 5 */
1182         }else if( payloadType == 4 ){
1183                 /* user_data_registered_itu_t_t35( payloadSize ) 5 */
1184 #endif
1185         }else if( payloadType == 5 ){
1186                 /* user_data_unregistered( payloadSize ) 5 */
1187                 bit_offset = h264_user_data_unregistered( tree, tvb, pinfo, bit_offset, payloadSize);
1188         }else if( payloadType == 6 ){
1189                 /* recovery_point( payloadSize ) 5 */
1190                 bit_offset = bit_offset +(payloadSize<<3);
1191         }else if( payloadType == 7 ){
1192                 /* dec_ref_pic_marking_repetition( payloadSize ) 5 */
1193                 bit_offset = bit_offset +(payloadSize<<3);
1194         }
1195 #if 0
1196 else if( payloadType == 8 )
1197 spare_pic( payloadSize ) 5
1198 else if( payloadType == 9 )
1199 scene_info( payloadSize ) 5
1200 else if( payloadType == 10 )
1201 sub_seq_info( payloadSize ) 5
1202 else if( payloadType == 11 )
1203 sub_seq_layer_characteristics( payloadSize ) 5
1204 else if( payloadType == 12 )
1205 sub_seq_characteristics( payloadSize ) 5
1206 else if( payloadType == 13 )
1207 full_frame_freeze( payloadSize ) 5
1208 else if( payloadType == 14 )
1209 full_frame_freeze_release( payloadSize ) 5
1210 else if( payloadType == 15 )
1211 full_frame_snapshot( payloadSize ) 5
1212 else if( payloadType == 16 )
1213 progressive_refinement_segment_start( payloadSize ) 5
1214 else if( payloadType == 17 )
1215 progressive_refinement_segment_end( payloadSize ) 5
1216 else if( payloadType == 18 )
1217 motion_constrained_slice_group_set( payloadSize ) 5
1218 else if( payloadType == 19 )
1219 film_grain_characteristics( payloadSize ) 5
1220 else if( payloadType == 20 )
1221 deblocking_filter_display_preference( payloadSize ) 5
1222 else if( payloadType == 21 )
1223 stereo_video_info( payloadSize ) 5
1224 else
1225 reserved_sei_message( payloadSize ) 5
1226         return bit_offset;
1227 #endif
1228         if( !h264_byte_aligned(bit_offset)){
1229                 /* bit_equal_to_one / * equal to 1 * / 5 f(1) */
1230                 /* TODO:Display the filler and, error if not 1 ?? */
1231                 bit_offset++;
1232                 while( !h264_byte_aligned(bit_offset)){
1233                         /* bit_equal_to_zero / * equal to 0 * / 5 f(1) */
1234                         /* TODO:Display the filler and, error if not 0 ?? */
1235                         bit_offset++;
1236                 }
1237         }
1238         return bit_offset;
1239 }
1240
1241 /*
1242  * 7.3.2.3.1 Supplemental enhancement information message syntax
1243  */
1244 static gint
1245 dissect_h264_sei_message(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo, gint bit_offset)
1246 {
1247         /* sei_message( ) { C Descriptor */
1248         guint32 payloadType = 0, payloadSize;
1249         gint start_bit_offset, length;
1250
1251         start_bit_offset = bit_offset;
1252
1253         /* while( next_bits( 8 ) == 0xFF ) { */
1254         while( tvb_get_bits8(tvb, bit_offset, 8) == 0xFF ) {
1255                 /* ff_byte / * equal to 0xFF * / 5 f(8) */
1256                 payloadType += 255;
1257                 bit_offset+=8;
1258         }
1259         /* last_payload_type_byte 5 u(8) */
1260         payloadType += tvb_get_bits8(tvb, bit_offset, 8);
1261         bit_offset+=8;
1262         length = (bit_offset - start_bit_offset)>>3;
1263
1264         proto_tree_add_uint(tree, hf_h264_payloadtype, tvb, start_bit_offset>>3, length, payloadType);
1265
1266         payloadSize = 0;
1267         start_bit_offset = bit_offset;
1268         /* while( next_bits( 8 ) == 0xFF ) { */
1269         while( tvb_get_bits8(tvb, bit_offset, 8) == 0xFF ) {
1270                 /* ff_byte / * equal to 0xFF * / 5 f(8) */
1271                 payloadSize += 255;
1272                 bit_offset+=8;
1273         }
1274         /* last_payload_size_byte 5 u(8) */
1275         /* payloadSize += last_payload_size_byte */
1276         payloadSize += tvb_get_bits8(tvb, bit_offset, 8);
1277         bit_offset+=8;
1278         length = (bit_offset - start_bit_offset)>>3;
1279         proto_tree_add_uint(tree, hf_h264_payloadsize, tvb, start_bit_offset>>3, length, payloadSize);
1280
1281         /*sei_payload( payloadType, payloadSize ) 5 */
1282         bit_offset = h264_sei_payload( tree, tvb, pinfo, bit_offset, payloadType, payloadSize);
1283         return bit_offset;
1284 }
1285 /*
1286  * 7.3.2.3 Supplemental enhancement information RBSP syntax
1287  * sei_rbsp( )
1288  */
1289 static int
1290 dissect_h264_sei_rbsp(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo _U_, gint offset)
1291 {
1292         gint bit_offset;
1293
1294         bit_offset = offset <<3;
1295         /* do */
1296         /* sei_message( ) 5*/
1297         bit_offset = dissect_h264_sei_message( tree, tvb, pinfo, bit_offset);
1298
1299         /* while( more_rbsp_data( ) )
1300          * If there is more data in an RBSP before rbsp_trailing_bits( ),
1301          * the return value of more_rbsp_data( ) is equal to TRUE.
1302          */
1303         /* rbsp_trailing_bits( ) 5 */
1304         bit_offset = dissect_h264_rbsp_trailing_bits(tree, tvb, pinfo, bit_offset);
1305
1306         return bit_offset;
1307 }
1308
1309 /* Ref 7.3.2.1 Sequence parameter set RBSP syntax */
1310 static int
1311 dissect_h264_seq_parameter_set_rbsp(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo, gint offset)
1312 {
1313         proto_item *level_item;
1314         gint bit_offset;
1315         guint8  constraint_set3_flag;
1316         guint32 level_idc;
1317
1318         gint i;
1319         guint8 profile_idc, chroma_format_idc, frame_mbs_only_flag, frame_cropping_flag;
1320         guint8 pic_order_cnt_type, vui_parameters_present_flag, num_ref_frames_in_pic_order_cnt_cycle;
1321         guint8 seq_scaling_matrix_present_flag; /* seq_scaling_list_present_flag */
1322
1323         /* profile_idc 0 u(8) */
1324         profile_idc = tvb_get_guint8(tvb,offset);
1325         proto_tree_add_item(tree, hf_h264_profile_idc, tvb, offset, 1, ENC_BIG_ENDIAN);
1326         offset++;
1327
1328         constraint_set3_flag = (tvb_get_guint8(tvb,offset)&0x10)>>4;
1329         /* constraint_set0_flag 0 u(1) */
1330         proto_tree_add_item(tree, hf_h264_constraint_set0_flag, tvb, offset, 1, ENC_BIG_ENDIAN);
1331
1332         /* constraint_set1_flag 0 u(1) */
1333         proto_tree_add_item(tree, hf_h264_constraint_set1_flag, tvb, offset, 1, ENC_BIG_ENDIAN);
1334
1335         /* constraint_set2_flag 0 u(1) */
1336         proto_tree_add_item(tree, hf_h264_constraint_set2_flag, tvb, offset, 1, ENC_BIG_ENDIAN);
1337
1338         /* constraint_set3_flag 0 u(1) */
1339         proto_tree_add_item(tree, hf_h264_constraint_set3_flag, tvb, offset, 1, ENC_BIG_ENDIAN);
1340
1341         /* reserved_zero_4bits  equal to 0  0 u(4)*/
1342         proto_tree_add_item(tree, hf_h264_reserved_zero_4bits, tvb, offset, 1, ENC_BIG_ENDIAN);
1343         offset++;
1344
1345         /* level_idc 0 u(8) */
1346         level_idc = tvb_get_guint8(tvb,offset);
1347         level_item = proto_tree_add_item(tree, hf_h264_level_idc, tvb, offset, 1, ENC_BIG_ENDIAN);
1348         if((level_idc==11)&&(constraint_set3_flag==1)){
1349                 proto_item_append_text(level_item,"[Level 1b]");
1350         }else{
1351                 proto_item_append_text(level_item," [Level %.1f %s]",((double)level_idc/10),val_to_str(level_idc, h264_level_bitrate_values, "Unknown "));
1352         }
1353         offset++;
1354         /* seq_parameter_set_id 0 ue(v)
1355          * ue(v): unsigned integer Exp-Golomb-coded syntax element with the left bit first.
1356          * The parsing process for this descriptor is specified in subclause 9.1.
1357          */
1358         bit_offset = offset<<3;
1359         dissect_h264_exp_golomb_code(tree, hf_h264_seq_parameter_set_id, tvb, &bit_offset, H264_UE_V);
1360
1361
1362         if( profile_idc == 100 || profile_idc == 110 ||
1363                 profile_idc == 122 || profile_idc == 144 ) {
1364
1365                 /* chroma_format_idc 0 ue(v) */
1366                 chroma_format_idc = dissect_h264_exp_golomb_code(tree, hf_h264_chroma_format_idc, tvb, &bit_offset, H264_UE_V);
1367                 if( chroma_format_idc == 3 ){
1368                         /* residual_colour_transform_flag 0 u(1) */
1369                         proto_tree_add_bits_item(tree, hf_h264_residual_colour_transform_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1370                         bit_offset++;
1371                 }
1372
1373                 /* bit_depth_luma_minus8 0 ue(v) */
1374                 dissect_h264_exp_golomb_code(tree, hf_h264_bit_depth_luma_minus8, tvb, &bit_offset, H264_UE_V);
1375
1376                 /* bit_depth_chroma_minus8 0 ue(v) */
1377                 dissect_h264_exp_golomb_code(tree, hf_h264_bit_depth_chroma_minus8, tvb, &bit_offset, H264_UE_V);
1378
1379                 /* qpprime_y_zero_transform_bypass_flag 0 u(1) */
1380                 proto_tree_add_bits_item(tree, hf_h264_qpprime_y_zero_transform_bypass_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1381                 bit_offset++;
1382
1383                 /* seq_scaling_matrix_present_flag 0 u(1) */
1384                 seq_scaling_matrix_present_flag = tvb_get_bits8(tvb, bit_offset, 1);
1385                 proto_tree_add_bits_item(tree, hf_h264_seq_scaling_matrix_present_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1386                 bit_offset++;
1387
1388                 if( seq_scaling_matrix_present_flag ){
1389                         /*
1390                         for( i = 0; i < 8; i++ ) {
1391                                 / seq_scaling_list_present_flag[ i ] 0 u(1) /
1392                                 seq_scaling_list_present_flag = tvb_get_bits8(tvb, bit_offset, 1);
1393                                 bit_offset++;
1394                                 if( seq_scaling_list_present_flag){
1395                                         if( i < 6 )
1396                                                 scaling_list( ScalingList4x4[ i ], 16,UseDefaultScalingMatrix4x4Flag[ i ])0
1397                                                 dissect_h264_scaling_list()
1398                                         else
1399                                                 scaling_list( ScalingList8x8[ i - 6 ], 64,UseDefaultScalingMatrix8x8Flag[ i - 6 ] )0
1400                                 }
1401                         }
1402                         */
1403                         proto_tree_add_text(tree, tvb, offset, -1, "[Not decoded yet]");
1404                         return -1;
1405                 }
1406
1407         }
1408
1409         /* log2_max_frame_num_minus4 0 ue(v) */
1410         dissect_h264_exp_golomb_code(tree, hf_h264_log2_max_frame_num_minus4, tvb, &bit_offset, H264_UE_V);
1411
1412         /* pic_order_cnt_type 0 ue(v) */
1413         pic_order_cnt_type = dissect_h264_exp_golomb_code(tree,hf_h264_pic_order_cnt_type, tvb, &bit_offset, H264_UE_V);
1414
1415         if(pic_order_cnt_type == 0){
1416                 /* log2_max_pic_order_cnt_lsb_minus4 0 ue(v) */
1417                 dissect_h264_exp_golomb_code(tree, hf_h264_log2_max_pic_order_cnt_lsb_minus4, tvb, &bit_offset, H264_UE_V);
1418         }else if(pic_order_cnt_type == 1) {
1419                 /* delta_pic_order_always_zero_flag 0 u(1) */
1420                 proto_tree_add_bits_item(tree, hf_h264_delta_pic_order_always_zero_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1421                 bit_offset++;
1422
1423                 /* offset_for_non_ref_pic 0 se(v) */
1424                 dissect_h264_exp_golomb_code(tree, hf_h264_offset_for_non_ref_pic, tvb, &bit_offset, H264_SE_V);
1425
1426                 /* offset_for_top_to_bottom_field 0 se(v) */
1427                 dissect_h264_exp_golomb_code(tree, hf_h264_offset_for_top_to_bottom_field, tvb, &bit_offset, H264_SE_V);
1428
1429                 /* num_ref_frames_in_pic_order_cnt_cycle 0 ue(v) */
1430                 num_ref_frames_in_pic_order_cnt_cycle = dissect_h264_exp_golomb_code(tree, hf_h264_num_ref_frames_in_pic_order_cnt_cycle, tvb, &bit_offset, H264_UE_V);
1431                 for( i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++ ){
1432                         /*offset_for_ref_frame[ i ] 0 se(v)*/
1433                         dissect_h264_exp_golomb_code(tree, hf_h264_offset_for_ref_frame, tvb, &bit_offset, H264_SE_V);
1434                 }
1435         }
1436         /* num_ref_frames 0 ue(v) */
1437         dissect_h264_exp_golomb_code(tree, hf_h264_num_ref_frames, tvb, &bit_offset, H264_UE_V);
1438
1439         /*      gaps_in_frame_num_value_allowed_flag 0 u(1) */
1440         proto_tree_add_bits_item(tree, hf_h264_gaps_in_frame_num_value_allowed_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1441         bit_offset++;
1442
1443         /*      pic_width_in_mbs_minus1 0 ue(v) */
1444         dissect_h264_exp_golomb_code(tree, hf_h264_pic_width_in_mbs_minus1, tvb, &bit_offset, H264_UE_V);
1445
1446         /* pic_height_in_map_units_minus1 0 ue(v) */
1447         dissect_h264_exp_golomb_code(tree, hf_h264_pic_height_in_map_units_minus1, tvb, &bit_offset, H264_UE_V);
1448
1449         /* frame_mbs_only_flag 0 u(1) */
1450         frame_mbs_only_flag = tvb_get_bits8(tvb, bit_offset, 1);
1451         proto_tree_add_bits_item(tree, hf_h264_frame_mbs_only_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1452         bit_offset++;
1453         if( !frame_mbs_only_flag ){
1454                 /* mb_adaptive_frame_field_flag 0 u(1) */
1455                 proto_tree_add_bits_item(tree, hf_h264_mb_adaptive_frame_field_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1456                 bit_offset++;
1457         }
1458
1459         /* direct_8x8_inference_flag 0 u(1) */
1460         proto_tree_add_bits_item(tree, hf_h264_direct_8x8_inference_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1461         bit_offset++;
1462
1463         /* frame_cropping_flag 0 u(1) */
1464         frame_cropping_flag = tvb_get_bits8(tvb, bit_offset, 1);
1465         proto_tree_add_bits_item(tree, hf_h264_frame_cropping_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1466         bit_offset++;
1467
1468         if(frame_cropping_flag) {
1469                 /* frame_crop_left_offset 0 ue(v) */
1470                 dissect_h264_exp_golomb_code(tree, hf_h264_frame_crop_left_offset, tvb, &bit_offset, H264_UE_V);
1471                 dissect_h264_exp_golomb_code(tree,hf_h264_frame_crop_right_offset, tvb, &bit_offset, H264_UE_V);
1472                 dissect_h264_exp_golomb_code(tree, hf_h264_frame_crop_top_offset, tvb, &bit_offset, H264_UE_V);
1473                 dissect_h264_exp_golomb_code(tree, hf_h264_frame_crop_bottom_offset, tvb, &bit_offset, H264_UE_V);
1474
1475         }
1476
1477         /*      vui_parameters_present_flag 0 u(1) */
1478         vui_parameters_present_flag = tvb_get_bits8(tvb, bit_offset, 1);
1479         proto_tree_add_bits_item(tree, hf_h264_vui_parameters_present_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1480         bit_offset++;
1481         if(vui_parameters_present_flag){
1482                 bit_offset = dissect_h264_vui_parameters(tree, tvb, pinfo, bit_offset);
1483         }
1484
1485         /*      rbsp_trailing_bits( ) 0 */
1486         bit_offset = dissect_h264_rbsp_trailing_bits(tree, tvb, pinfo, bit_offset);
1487
1488         offset = bit_offset>>3;
1489
1490         return offset;
1491 }
1492
1493 /* 7.3.2.2 Picture parameter set RBSP syntax */
1494
1495 static void
1496 dissect_h264_pic_parameter_set_rbsp(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo, gint offset)
1497 {
1498
1499         gint bit_offset;
1500         guint32 num_slice_groups_minus1, pic_scaling_matrix_present_flag;
1501
1502         bit_offset = offset<<3;
1503
1504         /* pic_parameter_set_id 1 ue(v) */
1505         dissect_h264_exp_golomb_code(tree, hf_h264_pic_parameter_set_id, tvb, &bit_offset, H264_UE_V);
1506
1507         /* seq_parameter_set_id 1 ue(v) */
1508         dissect_h264_exp_golomb_code(tree, hf_h264_seq_parameter_set_id, tvb, &bit_offset, H264_UE_V);
1509
1510         /* entropy_coding_mode_flag 1 u(1) */
1511         proto_tree_add_bits_item(tree, hf_h264_entropy_coding_mode_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1512         bit_offset++;
1513
1514         /* pic_order_present_flag 1 u(1)*/
1515         proto_tree_add_bits_item(tree, hf_h264_pic_order_present_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1516         bit_offset++;
1517
1518         /* num_slice_groups_minus1 1 ue(v)*/
1519         num_slice_groups_minus1 = dissect_h264_exp_golomb_code(tree, hf_h264_num_slice_groups_minus1, tvb, &bit_offset, H264_UE_V);
1520         if( num_slice_groups_minus1 > 0 ) {
1521                 /* slice_group_map_type 1 ue(v)*/
1522                 dissect_h264_exp_golomb_code(tree, hf_h264_slice_group_map_type, tvb, &bit_offset, H264_UE_V);
1523         /*      slice_group_map_type = dissect_h264_exp_golomb_code(tree, hf_h264_slice_group_map_type, tvb, &bit_offset, H264_UE_V);*/
1524         /* if( slice_group_map_type == 0 )*/
1525         /* for( iGroup = 0; iGroup <= num_slice_groups_minus1; iGroup++ )*/
1526         /* run_length_minus1[ iGroup ] 1 ue(v)*/
1527         /* else if( slice_group_map_type == 2 )*/
1528         /* for( iGroup = 0; iGroup < num_slice_groups_minus1; iGroup++ ) {*/
1529         /* top_left[ iGroup ] 1 ue(v)*/
1530         /* bottom_right[ iGroup ] 1 ue(v)*/
1531         /* }*/
1532         /* else if( slice_group_map_type == 3 ||*/
1533         /* slice_group_map_type == 4 ||*/
1534         /* slice_group_map_type == 5 ) {*/
1535         /* slice_group_change_direction_flag 1 u(1)*/
1536         /* slice_group_change_rate_minus1 1 ue(v)*/
1537         /* } else if( slice_group_map_type == 6 ) {*/
1538         /* pic_size_in_map_units_minus1 1 ue(v)*/
1539         /* for( i = 0; i <= pic_size_in_map_units_minus1; i++ )*/
1540         /* slice_group_id[ i ] 1 u(v)*/
1541         /* }*/
1542         /* }*/
1543                 proto_tree_add_text(tree, tvb, bit_offset>>3, -1, "[Not decoded yet]");
1544                 return;
1545         }
1546         /* num_ref_idx_l0_active_minus1 1 ue(v)*/
1547         dissect_h264_exp_golomb_code(tree, hf_h264_num_ref_idx_l0_active_minus1, tvb, &bit_offset, H264_UE_V);
1548
1549         /* num_ref_idx_l1_active_minus1 1 ue(v)*/
1550         dissect_h264_exp_golomb_code(tree, hf_h264_num_ref_idx_l1_active_minus1, tvb, &bit_offset, H264_UE_V);
1551
1552         /* weighted_pred_flag 1 u(1)*/
1553         proto_tree_add_bits_item(tree, hf_h264_weighted_pred_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1554         bit_offset++;
1555
1556         /* weighted_bipred_idc 1 u(2)*/
1557         proto_tree_add_bits_item(tree, hf_h264_weighted_bipred_idc, tvb, bit_offset, 2, ENC_BIG_ENDIAN);
1558         bit_offset= bit_offset+2;
1559
1560         /* pic_init_qp_minus26  * relative to 26 * 1 se(v)*/
1561         dissect_h264_exp_golomb_code(tree, hf_h264_pic_init_qp_minus26, tvb, &bit_offset, H264_SE_V);
1562
1563         /* pic_init_qs_minus26  * relative to 26 *  1 se(v)*/
1564         dissect_h264_exp_golomb_code(tree, hf_h264_pic_init_qs_minus26, tvb, &bit_offset, H264_SE_V);
1565
1566         /* chroma_qp_index_offset 1 se(v)*/
1567         dissect_h264_exp_golomb_code(tree, hf_h264_chroma_qp_index_offset, tvb, &bit_offset, H264_SE_V);
1568
1569         /* deblocking_filter_control_present_flag 1 u(1)*/
1570         proto_tree_add_bits_item(tree, hf_h264_deblocking_filter_control_present_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1571         bit_offset++;
1572
1573         /* constrained_intra_pred_flag 1 u(1)*/
1574         proto_tree_add_bits_item(tree, hf_h264_constrained_intra_pred_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1575         bit_offset++;
1576
1577         /* redundant_pic_cnt_present_flag 1 u(1)*/
1578         proto_tree_add_bits_item(tree, hf_h264_redundant_pic_cnt_present_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1579         bit_offset++;
1580
1581         if( more_rbsp_data(tree, tvb, pinfo, bit_offset)){
1582                 /* transform_8x8_mode_flag 1 u(1)*/
1583                 proto_tree_add_bits_item(tree, hf_h264_transform_8x8_mode_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1584                 bit_offset++;
1585
1586                 /* pic_scaling_matrix_present_flag 1 u(1)*/
1587                 pic_scaling_matrix_present_flag = tvb_get_bits8(tvb, bit_offset, 1);
1588                 proto_tree_add_bits_item(tree, hf_h264_pic_scaling_matrix_present_flag, tvb, bit_offset, 1, ENC_BIG_ENDIAN);
1589                 bit_offset++;
1590
1591                 if( pic_scaling_matrix_present_flag ){
1592                         proto_tree_add_text(tree, tvb, bit_offset>>3, -1, "[Not decoded yet]");
1593                         return;
1594                         /* for( i = 0; i < 6 + 2* transform_8x8_mode_flag; i++ ) {*/
1595                                 /* pic_scaling_list_present_flag[ i ] 1 u(1)*/
1596                                 /* if( pic_scaling_list_present_flag[ i ] )*/
1597                                 /* if( i < 6 )*/
1598                                         /* scaling_list( ScalingList4x4[ i ], 16, UseDefaultScalingMatrix4x4Flag[ i ] )*/
1599                                 /* else*/
1600                                         /* scaling_list( ScalingList8x8[ i - 6 ], 64, UseDefaultScalingMatrix8x8Flag[ i - 6 ] )*/
1601                         /* }*/
1602                         }
1603
1604                 /* second_chroma_qp_index_offset 1 se(v)*/
1605                 dissect_h264_exp_golomb_code(tree, hf_h264_second_chroma_qp_index_offset, tvb, &bit_offset, H264_SE_V);
1606         }
1607         bit_offset = dissect_h264_rbsp_trailing_bits(tree, tvb, pinfo, bit_offset);
1608
1609 }
1610
1611 /*
1612  * 7.3.2.4 Access unit delimiter RBSP syntax
1613  * access_unit_delimiter_rbsp( )
1614  */
1615 static void
1616 dissect_h264_access_unit_delimiter_rbsp(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo _U_, gint offset)
1617 {
1618         /* primary_pic_type 6 u(3) */
1619         /* rbsp_trailing_bits( ) 6 */
1620         proto_tree_add_text(tree, tvb, offset, -1, "[Not decoded yet]");
1621
1622 }
1623
1624 /*
1625  * 7.3.2.5 End of sequence RBSP syntax
1626  * end_of_seq_rbsp( ) {}
1627  */
1628 static void
1629 dissect_h264_end_of_seq_rbsp(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo _U_, gint offset)
1630 {
1631         proto_tree_add_text(tree, tvb, offset, -1, "[Not decoded yet]");
1632
1633 }
1634
1635 /*
1636  * 7.3.2.6 End of stream RBSP syntax
1637  * end_of_stream_rbsp( ) {}
1638  */
1639 static void
1640 dissect_h264_end_of_stream_rbsp(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo _U_, gint offset)
1641 {
1642         proto_tree_add_text(tree, tvb, offset, -1, "[Not decoded yet]");
1643
1644 }
1645
1646 /*
1647  * 7.3.2.7 Filler data RBSP syntax
1648  * filler_data_rbsp( )
1649  */
1650 static void
1651 dissect_h264_filler_data_rbsp(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo _U_, gint offset)
1652 {
1653         /* while( next_bits( 8 ) == 0xFF ) */
1654         /* ff_byte * equal to 0xFF * 9 f(8) */
1655         /* rbsp_trailing_bits( ) 9 */
1656         proto_tree_add_text(tree, tvb, offset, -1, "[Not decoded yet]");
1657
1658 }
1659
1660 /*
1661  * 7.3.2.1.2 Sequence parameter set extension RBSP syntax
1662  * seq_parameter_set_extension_rbsp( )
1663  */
1664 static void
1665 dissect_h264_seq_parameter_set_extension_rbsp(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo _U_, gint offset)
1666 {
1667         /* seq_parameter_set_id 10 ue(v) */
1668         /* aux_format_idc 10 ue(v) */
1669         /* if( aux_format_idc != 0 ) { */
1670         /* bit_depth_aux_minus8 10 ue(v) */
1671         /* alpha_incr_flag 10 u(1) */
1672         /* alpha_opaque_value 10 u(v) */
1673         /* alpha_transparent_value 10 u(v) */
1674         /* } */
1675         /* additional_extension_flag 10 u(1) */
1676         /* rbsp_trailing_bits() 10 */
1677         proto_tree_add_text(tree, tvb, offset, -1, "[Not decoded yet]");
1678
1679 }
1680
1681
1682 /*
1683  * Dissect NAL unit as recived in sprop-parameter-sets of SDP
1684  * or "DecoderConfiguration parameter in H.245
1685  */
1686 void
1687 dissect_h264_nal_unit(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
1688 {
1689         proto_item *item;
1690         proto_tree *h264_nal_tree;
1691         gint    offset = 0;
1692         guint8 nal_unit_type;
1693         guint32 dword;
1694         item = proto_tree_add_item(tree, hf_h264_nal_unit, tvb, offset, -1, ENC_NA);
1695         h264_nal_tree = proto_item_add_subtree(item, ett_h264_nal_unit);
1696
1697 startover:
1698         /* In decoder configuration start code may be pressent
1699          * B.1.1 Byte stream NAL unit syntax
1700          */
1701         dword = tvb_get_bits32(tvb, offset<<3, 32, ENC_BIG_ENDIAN);
1702         if(dword==1){
1703                 /* zero_byte + start_code_prefix_one_3bytes */
1704                 offset+=4;
1705         }else if((dword >> 8)== 1){
1706                 /* start_code_prefix_one_3bytes */
1707                 offset+= 3;
1708         }
1709         /* Ref: 7.3.1 NAL unit syntax */
1710         nal_unit_type = tvb_get_guint8(tvb,offset) & 0x1f;
1711
1712         /* forbidden_zero_bit All f(1) */
1713         proto_tree_add_item(h264_nal_tree, hf_h264_forbidden_zero_bit, tvb, offset, 1, ENC_BIG_ENDIAN);
1714         /* nal_ref_idc All u(2) */
1715         proto_tree_add_item(h264_nal_tree, hf_h264_nal_ref_idc, tvb, offset, 1, ENC_BIG_ENDIAN);
1716         /* nal_unit_type All u(5) */
1717         proto_tree_add_item(h264_nal_tree, hf_h264_nal_unit_type, tvb, offset, 1, ENC_BIG_ENDIAN);
1718         offset++;
1719
1720         switch(nal_unit_type){
1721         case 0: /* Unspecified */
1722                 proto_tree_add_text(h264_nal_tree, tvb, offset, -1, "Unspecified NAL unit type");
1723                 break;
1724         case 1: /* Coded slice of a non-IDR picture */
1725                 dissect_h264_slice_layer_without_partitioning_rbsp(h264_nal_tree, tvb, pinfo, offset);
1726                 break;
1727         case 2: /* Coded slice data partition A */
1728                 dissect_h264_slice_data_partition_a_layer_rbsp(h264_nal_tree, tvb, pinfo, offset);
1729                 break;
1730         case 3: /* Coded slice data partition B */
1731                 dissect_h264_slice_data_partition_b_layer_rbsp(h264_nal_tree, tvb, pinfo, offset);
1732                 break;
1733         case 4: /* Coded slice data partition C */
1734                 dissect_h264_slice_data_partition_c_layer_rbsp(h264_nal_tree, tvb, pinfo, offset);
1735                 break;
1736         case 5: /* Coded slice of an IDR picture */
1737                 dissect_h264_slice_layer_without_partitioning_rbsp(h264_nal_tree, tvb, pinfo, offset);
1738                 break;
1739         case 6: /* Supplemental enhancement information (SEI) */
1740                 dissect_h264_sei_rbsp(h264_nal_tree, tvb, pinfo, offset);
1741                 break;
1742         case H264_SEQ_PAR_SET:  /* 7 Sequence parameter set*/
1743                 offset = dissect_h264_seq_parameter_set_rbsp(h264_nal_tree, tvb, pinfo, offset);
1744                 /* A bit ugly */
1745                 if(tvb_length_remaining(tvb,offset) > 0){
1746                         /* In this case length = offset as we start from zero */
1747                         proto_item_set_len(item, offset/*Length */);
1748                         item = proto_tree_add_item(tree, hf_h264_nal_unit, tvb, offset, -1, ENC_NA);
1749                         h264_nal_tree = proto_item_add_subtree(item, ett_h264_nal_unit);
1750                         goto startover;
1751                 }
1752                 break;
1753         case H264_PIC_PAR_SET:  /* 8 Picture parameter set */
1754                 dissect_h264_pic_parameter_set_rbsp(h264_nal_tree, tvb, pinfo, offset);
1755                 break;
1756         case 9: /* Access unit delimiter */
1757                 dissect_h264_access_unit_delimiter_rbsp(h264_nal_tree, tvb, pinfo, offset);
1758                 break;
1759         case 10:        /* End of sequence */
1760                 dissect_h264_end_of_seq_rbsp(h264_nal_tree, tvb, pinfo, offset);
1761                 break;
1762         case 11:        /* End of stream */
1763                 dissect_h264_end_of_stream_rbsp(h264_nal_tree, tvb, pinfo, offset);
1764                 break;
1765         case 12:        /* Filler data */
1766                 dissect_h264_filler_data_rbsp(h264_nal_tree, tvb, pinfo, offset);
1767                 break;
1768         case 13:        /* Sequence parameter set extension */
1769                 dissect_h264_seq_parameter_set_extension_rbsp(h264_nal_tree, tvb, pinfo, offset);
1770                 break;
1771         case 14:        /* Reserved */
1772         case 15:        /* Reserved */
1773         case 16:        /* Reserved */
1774         case 17:        /* Reserved */
1775         case 18:        /* Reserved */
1776                 proto_tree_add_text(h264_nal_tree, tvb, offset, -1, "Reserved NAL unit type");
1777                 break;
1778         case 19:        /* Coded slice of an auxiliary coded picture without partitioning */
1779                 dissect_h264_slice_layer_without_partitioning_rbsp(tree, tvb, pinfo, offset);
1780                 break;
1781         case 28:
1782                 dissect_h264_slice_layer_without_partitioning_rbsp(tree, tvb, pinfo, offset);
1783                 break;
1784         default:
1785                 /* 24..31 Unspecified */
1786                 proto_tree_add_text(h264_nal_tree, tvb, offset, -1, "Unspecified NAL unit type");
1787                 break;
1788         }
1789
1790 }
1791 /* Code to actually dissect the packets */
1792 static void
1793 dissect_h264(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1794 {
1795         int offset = 0;
1796         proto_item *item, *ti, *stream_item, *fua_item;
1797         proto_tree *h264_tree, *h264_nal_tree, *stream_tree, *fua_tree;
1798         guint8 type;
1799
1800
1801 /* Make entries in Protocol column and Info column on summary display */
1802         col_set_str(pinfo->cinfo, COL_PROTOCOL, "H264");
1803         if (tree) {
1804
1805                 item = proto_tree_add_item(tree, proto_h264, tvb, 0, -1, ENC_NA);
1806                 h264_tree = proto_item_add_subtree(item, ett_h264);
1807
1808                 type = tvb_get_guint8(tvb,offset)&0x1f;
1809 /* if the type is 28, it would be draw another title */
1810                 if(type == 28)
1811                         ti = proto_tree_add_text(h264_tree, tvb, offset, 1, "FU identifier");
1812                 else
1813                         ti = proto_tree_add_text(h264_tree, tvb, offset, 1, "NAL unit header or first byte of the payload");
1814                 h264_nal_tree = proto_item_add_subtree(ti, ett_h264_nal);
1815
1816                 /* +---------------+
1817                  * |0|1|2|3|4|5|6|7|
1818                  * +-+-+-+-+-+-+-+-+
1819                  * |F|NRI|  Type   |
1820                  * +---------------+
1821                  */
1822
1823                 /* F: 1 bit
1824                  * forbidden_zero_bit.  A value of 0 indicates that the NAL unit type
1825                  * octet and payload should not contain bit errors or other syntax
1826                  * violations.  A value of 1 indicates that the NAL unit type octet
1827                  * and payload may contain bit errors or other syntax violations.
1828                  */
1829                 proto_tree_add_item(h264_nal_tree, hf_h264_nal_f_bit, tvb, offset, 1, ENC_BIG_ENDIAN);
1830                 proto_tree_add_item(h264_nal_tree, hf_h264_nal_nri, tvb, offset, 1, ENC_BIG_ENDIAN);
1831                 col_append_fstr(pinfo->cinfo, COL_INFO, " %s",
1832                         val_to_str(type, h264_type_values, "Unknown Type (%u)"));
1833
1834                 proto_tree_add_item(h264_nal_tree, hf_h264_type, tvb, offset, 1, ENC_BIG_ENDIAN);
1835                 offset++;
1836                 if (type == 28){
1837                         fua_item = proto_tree_add_text(h264_tree, tvb, offset, 1, "FU Header");
1838                         fua_tree = proto_item_add_subtree(fua_item, ett_h264_fua);
1839                         proto_tree_add_item(fua_tree, hf_h264_start_bit, tvb, offset, 1, ENC_BIG_ENDIAN);
1840                         proto_tree_add_item(fua_tree, hf_h264_end_bit, tvb, offset, 1, ENC_BIG_ENDIAN);
1841                         proto_tree_add_item(fua_tree, hf_h264_forbidden_bit, tvb, offset, 1, ENC_BIG_ENDIAN);
1842                         proto_tree_add_item(fua_tree, hf_h264_nal_unit_type, tvb, offset, 1, ENC_BIG_ENDIAN);
1843                         if ( (tvb_get_guint8(tvb,offset)&0x80) == 0x80 ){
1844                                 type = tvb_get_guint8(tvb,offset)&0x1f;
1845                                 offset++;
1846                         }
1847                         else
1848                                 return;
1849                 }
1850                 stream_item =proto_tree_add_text(h264_tree, tvb, offset, -1, "H264 bitstream");
1851                 stream_tree = proto_item_add_subtree(stream_item, ett_h264_stream);
1852                 switch(type){
1853                 case 1:                         /* 1 Coded slice of a non-IDR picture */
1854                         dissect_h264_slice_layer_without_partitioning_rbsp(stream_tree, tvb, pinfo, offset);
1855                         break;
1856                 case 3: /* Coded slice data partition B */
1857                         dissect_h264_slice_data_partition_b_layer_rbsp(h264_nal_tree, tvb, pinfo, offset);
1858                         break;
1859                 case 4: /* Coded slice data partition C */
1860                         dissect_h264_slice_data_partition_c_layer_rbsp(h264_nal_tree, tvb, pinfo, offset);
1861                         break;
1862                 case 5: /* Coded slice of an IDR picture */
1863                         dissect_h264_slice_layer_without_partitioning_rbsp(stream_tree, tvb, pinfo, offset);
1864                         break;
1865                 case 6: /* Supplemental enhancement information (SEI) */
1866                         dissect_h264_sei_rbsp(stream_tree, tvb, pinfo, offset);
1867                         break;
1868                 case H264_SEQ_PAR_SET:  /* 7 Sequence parameter set*/
1869                         dissect_h264_seq_parameter_set_rbsp(stream_tree, tvb, pinfo, offset);
1870                         break;
1871                 case H264_PIC_PAR_SET:  /* 8 Picture parameter set */
1872                         dissect_h264_pic_parameter_set_rbsp(stream_tree, tvb, pinfo, offset);
1873                         break;
1874                 case 19:        /* Coded slice of an auxiliary coded picture without partitioning */
1875                         dissect_h264_slice_layer_without_partitioning_rbsp(stream_tree, tvb, pinfo, offset);
1876                         break;
1877                 default:
1878                         break;
1879                 }
1880         }/* if tree */
1881
1882 }
1883
1884
1885 /* Capability */
1886
1887 static const int *profile_fields[] = {
1888   &hf_h264_par_profile_b,
1889   &hf_h264_par_profile_m,
1890   &hf_h264_par_profile_e,
1891   &hf_h264_par_profile_h,
1892   &hf_h264_par_profile_h10,
1893   &hf_h264_par_profile_h4_2_2,
1894   &hf_h264_par_profile_h4_4_4,
1895   NULL
1896 };
1897
1898 static int
1899 dissect_h264_par_profile(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
1900 {
1901   int offset = 0;
1902
1903   proto_tree_add_bitmask(tree, tvb, offset,
1904                          hf_h264_par_profile, ett_h264_par_profile,
1905                          profile_fields, ENC_BIG_ENDIAN);
1906   offset += 1;
1907   return offset;
1908 }
1909
1910 static const int *AdditionalModesSupported_fields[] = {
1911   &hf_h264_par_add_mode_sup_rcdo,
1912   NULL
1913 };
1914 static int
1915 dissect_h264_par_AdditionalModesSupported(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
1916 {
1917   int offset = 0;
1918
1919   proto_tree_add_bitmask(tree, tvb, offset,
1920                          hf_h264_par_AdditionalModesSupported, ett_h264_par_AdditionalModesSupported,
1921                          AdditionalModesSupported_fields, ENC_BIG_ENDIAN);
1922   offset += 1;
1923   return offset;
1924 }
1925
1926
1927 static const int *ProfileIOP_fields[] = {
1928   &hf_h264_par_constraint_set0_flag,
1929   &hf_h264_par_constraint_set1_flag,
1930   &hf_h264_par_constraint_set2_flag,
1931   NULL
1932 };
1933
1934
1935 static int
1936 dissect_h264_ProfileIOP(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
1937 {
1938   int offset = 0;
1939
1940   proto_tree_add_bitmask(tree, tvb, offset,
1941                          hf_h264_par_ProfileIOP, ett_h264_par_ProfileIOP,
1942                          ProfileIOP_fields, ENC_BIG_ENDIAN);
1943   offset += 1;
1944   return offset;
1945 }
1946
1947 static const value_string h264_par_level_values[] = {
1948   { 15,         "1" },
1949   { 19,         "1b" },
1950   { 22,         "1.1" },
1951   { 29,         "1.2" },
1952   { 36,         "1.3" },
1953   { 43,         "2" },
1954   { 50,         "2.1" },
1955   { 57,         "2.2" },
1956   { 64,         "3" },
1957   { 71,         "3.1" },
1958   { 78,         "3.2" },
1959   { 85,         "4" },
1960   { 92,         "4.1" },
1961   { 99,         "4.2" },
1962   { 106,        "5" },
1963   { 113 ,       "5.1" },
1964   { 0,  NULL }
1965 };
1966
1967 static int
1968 dissect_h264_par_level(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree _U_)
1969 {
1970   int offset = 0;
1971   guint16 lvl;
1972   const gchar *p = NULL;
1973   asn1_ctx_t *actx;
1974
1975   actx = get_asn1_ctx(pinfo->private_data);
1976   DISSECTOR_ASSERT(actx);
1977
1978   lvl = tvb_get_ntohs(tvb, offset);
1979   p = match_strval(lvl, VALS(h264_par_level_values));
1980   if (p) {
1981     proto_item_append_text(actx->created_item, " - Level %s", p);
1982   }
1983   offset += 2;
1984   return offset;
1985 }
1986 static int
1987 dissect_h264_par_DecoderConfigurationInformation(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree _U_)
1988 {
1989   asn1_ctx_t *actx;
1990
1991   actx = get_asn1_ctx(pinfo->private_data);
1992   DISSECTOR_ASSERT(actx);
1993
1994   dissect_h264_nal_unit(tvb, pinfo, tree);
1995
1996   return tvb_length(tvb);
1997 }
1998
1999 typedef struct _h264_capability_t {
2000   const gchar *id;
2001   const gchar *name;
2002   new_dissector_t content_pdu;
2003 } h264_capability_t;
2004
2005 static h264_capability_t h264_capability_tab[] = {
2006   /* ITU-T H.241 (05/2006), 8.3 H.264 capabilities */
2007   { "GenericCapability/0.0.8.241.0.0.1", "ITU-T Rec. H.241 H.264 Video Capabilities", NULL },
2008   { "GenericCapability/0.0.8.241.0.0.1/collapsing/41", "Profile", dissect_h264_par_profile },
2009   { "GenericCapability/0.0.8.241.0.0.1/collapsing/42", "Level", dissect_h264_par_level },
2010   { "GenericCapability/0.0.8.241.0.0.1/collapsing/3" , "CustomMaxMBPS", NULL },
2011   { "GenericCapability/0.0.8.241.0.0.1/collapsing/4" , "CustomMaxFS", NULL },
2012   { "GenericCapability/0.0.8.241.0.0.1/collapsing/5" , "CustomMaxDPB", NULL },
2013   { "GenericCapability/0.0.8.241.0.0.1/collapsing/6" , "CustomMaxBRandCPB", NULL },
2014   { "GenericCapability/0.0.8.241.0.0.1/collapsing/7" , "MaxStaticMBPS", NULL },
2015   { "GenericCapability/0.0.8.241.0.0.1/collapsing/8" , "max-rcmd-nal-unit-size", NULL },
2016   { "GenericCapability/0.0.8.241.0.0.1/collapsing/9" , "max-nal-unit-size", NULL },
2017   { "GenericCapability/0.0.8.241.0.0.1/collapsing/10", "SampleAspectRatiosSupported", NULL },
2018   { "GenericCapability/0.0.8.241.0.0.1/collapsing/11", "AdditionalModesSupported", dissect_h264_par_AdditionalModesSupported },
2019   { "GenericCapability/0.0.8.241.0.0.1/collapsing/12", "AdditionalDisplayCapabilities", NULL },
2020   /* TS 26.111  H.264 */
2021   { "GenericCapability/0.0.8.241.0.0.1/nonCollapsing/43" , "DecoderConfigurationInformation", dissect_h264_par_DecoderConfigurationInformation },
2022   { "GenericCapability/0.0.8.241.0.0.1/collapsing/44" , "AcceptRedundantSlices", NULL },
2023   { "GenericCapability/0.0.8.241.0.0.1/collapsing/45" , "NalAlignedMode", NULL },
2024   { "GenericCapability/0.0.8.241.0.0.1/collapsing/46" , "ProfileIOP", dissect_h264_ProfileIOP },
2025   { NULL, NULL, NULL },
2026 };
2027
2028 static h264_capability_t *find_cap(const gchar *id) {
2029   h264_capability_t *ftr = NULL;
2030   h264_capability_t *f;
2031
2032   for (f=h264_capability_tab; f->id; f++) {
2033     if (!strcmp(id, f->id)) { ftr = f; break; }
2034   }
2035   return ftr;
2036 }
2037
2038 static void
2039 dissect_h264_name(tvbuff_t *tvb _U_, packet_info *pinfo, proto_tree *tree)
2040 {
2041   asn1_ctx_t *actx;
2042   h264_capability_t *ftr = NULL;
2043
2044   actx = get_asn1_ctx(pinfo->private_data);
2045   DISSECTOR_ASSERT(actx);
2046   if (tree) {
2047     ftr = find_cap(pinfo->match_string);
2048     if (ftr) {
2049       proto_item_append_text(actx->created_item, " - %s", ftr->name);
2050       proto_item_append_text(proto_item_get_parent(proto_tree_get_parent(tree)), ": %s", ftr->name);
2051     } else {
2052       proto_item_append_text(actx->created_item, " - unknown(%s)", pinfo->match_string);
2053     }
2054   }
2055 }
2056
2057
2058 static void range_delete_h264_rtp_pt_callback(guint32 rtp_pt) {
2059   if (rtp_pt >= 96 && rtp_pt <= 127)
2060      dissector_delete_uint("rtp.pt", rtp_pt, h264_handle);
2061 }
2062
2063 static void range_add_h264_rtp_pt_callback(guint32 rtp_pt) {
2064   if (rtp_pt >= 96 && rtp_pt <= 127)
2065      dissector_add_uint("rtp.pt", rtp_pt, h264_handle);
2066 }
2067
2068
2069
2070 /* Register the protocol with Wireshark */
2071 /* If this dissector uses sub-dissector registration add a registration routine.
2072    This format is required because a script is used to find these routines and
2073    create the code that calls these routines.
2074 */
2075 void
2076 proto_reg_handoff_h264(void)
2077 {
2078
2079         static range_t *dynamic_payload_type_range = NULL;
2080         static gboolean h264_prefs_initialized = FALSE;
2081
2082         if (!h264_prefs_initialized) {
2083                 dissector_handle_t h264_name_handle;
2084                 h264_capability_t *ftr;
2085
2086                 h264_handle = find_dissector("h264");
2087                 dissector_add_string("rtp_dyn_payload_type","H264", h264_handle);
2088
2089                 h264_name_handle = create_dissector_handle(dissect_h264_name, proto_h264);
2090                 for (ftr=h264_capability_tab; ftr->id; ftr++) {
2091                     if (ftr->name)
2092                                 dissector_add_string("h245.gef.name", ftr->id, h264_name_handle);
2093                         if (ftr->content_pdu)
2094                                 dissector_add_string("h245.gef.content", ftr->id, new_create_dissector_handle(ftr->content_pdu, proto_h264));
2095                 }
2096                 h264_prefs_initialized = TRUE;
2097         } else {
2098                 range_foreach(dynamic_payload_type_range, range_delete_h264_rtp_pt_callback);
2099                 g_free(dynamic_payload_type_range);
2100         }
2101
2102         dynamic_payload_type_range = range_copy(temp_dynamic_payload_type_range);
2103         range_foreach(dynamic_payload_type_range, range_add_h264_rtp_pt_callback);
2104 }
2105
2106 /* this format is required because a script is used to build the C function
2107    that calls all the protocol registration.
2108 */
2109
2110 void
2111 proto_register_h264(void)
2112 {
2113
2114         module_t *h264_module;
2115
2116 /* Setup list of header fields  See Section 1.6.1 for details*/
2117         static hf_register_info hf[] = {
2118                 { &hf_h264_nal_f_bit,
2119                         { "F bit",           "h264.f",
2120                         FT_BOOLEAN, 8, TFS(&h264_f_bit_vals), 0x80,
2121                         NULL, HFILL }
2122                 },
2123                 { &hf_h264_nal_nri,
2124                         { "Nal_ref_idc (NRI)",           "h264.nal_nri",
2125                         FT_UINT8, BASE_DEC, NULL, 0x60,
2126                         NULL, HFILL }
2127                 },
2128                 { &hf_h264_type,
2129                         { "Type",           "h264.nal_unit_hdr",
2130                         FT_UINT8, BASE_DEC, VALS(h264_type_values), 0x1f,
2131                         NULL, HFILL }
2132                 },
2133                 { &hf_h264_start_bit,
2134                         { "Start bit", "h264.start.bit",
2135                         FT_BOOLEAN, 8, TFS(&h264_start_bit_vals), 0x80,
2136                         NULL, HFILL }
2137                 },
2138                 { &hf_h264_end_bit,
2139                         { "End bit", "h264.end.bit",
2140                         FT_BOOLEAN, 8, TFS(&h264_end_bit_vals), 0x40,
2141                         NULL, HFILL }
2142                 },
2143                 { &hf_h264_forbidden_bit,
2144                         { "Forbidden bit", "h264.forbidden.bit",
2145                         FT_UINT8, BASE_DEC, NULL, 0x20,
2146                         NULL, HFILL }
2147                 },
2148                 { &hf_h264_profile,
2149                         { "Profile",           "h264.profile",
2150                         FT_BYTES, BASE_NONE, NULL, 0x0,
2151                         NULL, HFILL }
2152                 },
2153                 { &hf_h264_profile_idc,
2154                         { "Profile_idc",           "h264.profile_idc",
2155                         FT_UINT8, BASE_DEC, VALS(h264_profile_idc_values), 0xff,
2156                         NULL, HFILL }
2157                 },
2158                 { &hf_h264_rbsp_stop_bit,
2159                         { "rbsp_stop_bit",           "h264.rbsp_stop_bit",
2160                         FT_UINT8, BASE_DEC, NULL, 0x0,
2161                         NULL, HFILL }
2162                 },
2163                 { &hf_h264_rbsp_trailing_bits,
2164                         { "rbsp_trailing_bits",           "h264.rbsp_trailing_bits",
2165                         FT_UINT8, BASE_DEC, NULL, 0x0,
2166                         NULL, HFILL }
2167                 },
2168                 { &hf_h264_constraint_set0_flag,
2169                         { "Constraint_set0_flag",           "h264.constraint_set0_flag",
2170                         FT_UINT8, BASE_DEC, NULL, 0x80,
2171                         NULL, HFILL }
2172                 },
2173                 { &hf_h264_constraint_set1_flag,
2174                         { "Constraint_set1_flag",           "h264.constraint_set1_flag",
2175                         FT_UINT8, BASE_DEC, NULL, 0x40,
2176                         NULL, HFILL }
2177                 },
2178                 { &hf_h264_constraint_set2_flag,
2179                         { "Constraint_set2_flag",           "h264.constraint_set2_flag",
2180                         FT_UINT8, BASE_DEC, NULL, 0x20,
2181                         NULL, HFILL }
2182                 },
2183                 { &hf_h264_constraint_set3_flag,
2184                         { "Constraint_set3_flag",           "h264.constraint_set3_flag",
2185                         FT_UINT8, BASE_DEC, NULL, 0x10,
2186                         NULL, HFILL }
2187                 },
2188                 { &hf_h264_reserved_zero_4bits,
2189                         { "Reserved_zero_4bits",           "h264.reserved_zero_4bits",
2190                         FT_UINT8, BASE_DEC, NULL, 0x0f,
2191                         NULL, HFILL }
2192                 },
2193                 { &hf_h264_level_idc,
2194                         { "Level_id",           "h264.level_id",
2195                         FT_UINT8, BASE_DEC, NULL, 0xff,
2196                         NULL, HFILL }
2197                 },
2198                 { &hf_h264_nal_unit,
2199                         { "NAL unit",           "h264.nal_unit",
2200                         FT_BYTES, BASE_NONE, NULL, 0x0,
2201                         NULL, HFILL }
2202                 },
2203                 { &hf_h264_forbidden_zero_bit,
2204                         { "Forbidden_zero_bit",           "h264.forbidden_zero_bit",
2205                         FT_UINT8, BASE_DEC, NULL, 0x80,
2206                         NULL, HFILL }
2207                 },
2208                 { &hf_h264_nal_ref_idc,
2209                         { "Nal_ref_idc",           "h264.nal_ref_idc",
2210                         FT_UINT8, BASE_DEC, NULL, 0x60,
2211                         NULL, HFILL }
2212                 },
2213                 {&hf_h264_nal_unit_type,
2214                         { "Nal_unit_type",           "h264.nal_unit_type",
2215                         FT_UINT8, BASE_DEC, VALS(h264_nal_unit_type_vals), 0x1f,
2216                         NULL, HFILL }
2217                 },
2218                 { &hf_h264_seq_parameter_set_id,
2219                         { "seq_parameter_set_id",           "h264.seq_parameter_set_id",
2220                         FT_UINT32, BASE_DEC, NULL, 0x0,
2221                         NULL, HFILL }
2222                 },
2223                 { &hf_h264_chroma_format_idc,
2224                         { "chroma_format_id",           "h264.chroma_format_id",
2225                         FT_UINT32, BASE_DEC, NULL, 0x0,
2226                         NULL, HFILL }
2227                 },
2228                 { &hf_h264_residual_colour_transform_flag,
2229                         { "residual_colour_transform_flag",           "h264.residual_colour_transform_flag",
2230                         FT_UINT8, BASE_DEC, NULL, 0x0,
2231                         NULL, HFILL }
2232                 },
2233                 { &hf_h264_bit_depth_luma_minus8,
2234                         { "bit_depth_luma_minus8",           "h264.bit_depth_luma_minus8",
2235                         FT_UINT32, BASE_DEC, NULL, 0x0,
2236                         NULL, HFILL }
2237                 },
2238                 { &hf_h264_bit_depth_chroma_minus8,
2239                         { "bit_depth_chroma_minus8",           "h264.bit_depth_chroma_minus8",
2240                         FT_UINT32, BASE_DEC, NULL, 0x0,
2241                         NULL, HFILL }
2242                 },
2243                 { &hf_h264_qpprime_y_zero_transform_bypass_flag,
2244                         { "qpprime_y_zero_transform_bypass_flag",           "h264.qpprime_y_zero_transform_bypass_flag",
2245                         FT_UINT32, BASE_DEC, NULL, 0x0,
2246                         NULL, HFILL }
2247                 },
2248                 { &hf_h264_seq_scaling_matrix_present_flag,
2249                         { "seq_scaling_matrix_present_flag",           "h264.seq_scaling_matrix_present_flag",
2250                         FT_UINT32, BASE_DEC, NULL, 0x0,
2251                         NULL, HFILL }
2252                 },
2253                 { &hf_h264_log2_max_frame_num_minus4,
2254                         { "log2_max_frame_num_minus4",           "h264.log2_max_frame_num_minus4",
2255                         FT_UINT32, BASE_DEC, NULL, 0x0,
2256                         NULL, HFILL }
2257                 },
2258                 { &hf_h264_pic_order_cnt_type,
2259                         { "pic_order_cnt_type",           "h264.pic_order_cnt_type",
2260                         FT_UINT32, BASE_DEC, NULL, 0x0,
2261                         NULL, HFILL }
2262                 },
2263                 { &hf_h264_log2_max_pic_order_cnt_lsb_minus4,
2264                         { "log2_max_pic_order_cnt_lsb_minus4",           "h264.log2_max_pic_order_cnt_lsb_minus4",
2265                         FT_UINT32, BASE_DEC, NULL, 0x0,
2266                         NULL, HFILL }
2267                 },
2268                 { &hf_h264_delta_pic_order_always_zero_flag,
2269                         { "delta_pic_order_always_zero_flag",           "h264.delta_pic_order_always_zero_flag",
2270                         FT_UINT8, BASE_DEC, NULL, 0x0,
2271                         NULL, HFILL }
2272                 },
2273                 { &hf_h264_offset_for_non_ref_pic,
2274                         { "offset_for_non_ref_pic",           "h264.offset_for_non_ref_pic",
2275                         FT_INT32, BASE_DEC, NULL, 0x0,
2276                         NULL, HFILL }
2277                 },
2278                 { &hf_h264_offset_for_top_to_bottom_field,
2279                         { "offset_for_top_to_bottom_field",           "h264.offset_for_top_to_bottom_field",
2280                         FT_INT32, BASE_DEC, NULL, 0x0,
2281                         NULL, HFILL }
2282                 },
2283                 { &hf_h264_num_ref_frames_in_pic_order_cnt_cycle,
2284                         { "num_ref_frames_in_pic_order_cnt_cycle",           "h264.num_ref_frames_in_pic_order_cnt_cycle",
2285                         FT_UINT32, BASE_DEC, NULL, 0x0,
2286                         NULL, HFILL }
2287                 },
2288                 { &hf_h264_offset_for_ref_frame,
2289                         { "offset_for_ref_frame",           "h264.offset_for_ref_frame",
2290                         FT_INT32, BASE_DEC, NULL, 0x0,
2291                         NULL, HFILL }
2292                 },
2293                 { &hf_h264_num_ref_frames,
2294                         { "num_ref_frames",           "h264.num_ref_frames",
2295                         FT_UINT32, BASE_DEC, NULL, 0x0,
2296                         NULL, HFILL }
2297                 },
2298                 { &hf_h264_gaps_in_frame_num_value_allowed_flag,
2299                         { "gaps_in_frame_num_value_allowed_flag",           "h264.gaps_in_frame_num_value_allowed_flag",
2300                         FT_UINT8, BASE_DEC, NULL, 0x0,
2301                         NULL, HFILL }
2302                 },
2303                 { &hf_h264_pic_width_in_mbs_minus1,
2304                         { "pic_width_in_mbs_minus1",           "h264.pic_width_in_mbs_minus1",
2305                         FT_UINT32, BASE_DEC, NULL, 0x0,
2306                         NULL, HFILL }
2307                 },
2308                 { &hf_h264_pic_height_in_map_units_minus1,
2309                         { "pic_height_in_map_units_minus1",           "h264.pic_height_in_map_units_minus1",
2310                         FT_UINT32, BASE_DEC, NULL, 0x0,
2311                         NULL, HFILL }
2312                 },
2313                 { &hf_h264_frame_mbs_only_flag,
2314                         { "frame_mbs_only_flag",           "h264.frame_mbs_only_flag",
2315                         FT_UINT8, BASE_DEC, NULL, 0x0,
2316                         NULL, HFILL }
2317                 },
2318                 { &hf_h264_mb_adaptive_frame_field_flag,
2319                         { "mb_adaptive_frame_field_flag",           "h264.mb_adaptive_frame_field_flag",
2320                         FT_UINT8, BASE_DEC, NULL, 0x0,
2321                         NULL, HFILL }
2322                 },
2323                 { &hf_h264_direct_8x8_inference_flag,
2324                         { "direct_8x8_inference_flag",           "h264.direct_8x8_inference_flag",
2325                         FT_UINT8, BASE_DEC, NULL, 0x0,
2326                         NULL, HFILL }
2327                 },
2328                 { &hf_h264_frame_cropping_flag,
2329                         { "frame_cropping_flag",           "h264.frame_cropping_flag",
2330                         FT_UINT8, BASE_DEC, NULL, 0x0,
2331                         NULL, HFILL }
2332                 },
2333                 { &hf_h264_frame_crop_left_offset,
2334                         { "frame_crop_left_offset",           "h264.frame_crop_left_offset",
2335                         FT_UINT32, BASE_DEC, NULL, 0x0,
2336                         NULL, HFILL }
2337                 },
2338                 { &hf_h264_frame_crop_right_offset,
2339                         { "frame_crop_left_offset",           "h264.frame_crop_right_offset",
2340                         FT_UINT32, BASE_DEC, NULL, 0x0,
2341                         NULL, HFILL }
2342                 },
2343                 { &hf_h264_frame_crop_top_offset,
2344                         { "frame_crop_top_offset",           "h264.frame_crop_top_offset",
2345                         FT_UINT32, BASE_DEC, NULL, 0x0,
2346                         NULL, HFILL }
2347                 },
2348                 { &hf_h264_frame_crop_bottom_offset,
2349                         { "frame_crop_bottom_offset",           "h264.frame_crop_bottom_offset",
2350                         FT_UINT32, BASE_DEC, NULL, 0x0,
2351                         NULL, HFILL }
2352                 },
2353                 { &hf_h264_vui_parameters_present_flag,
2354                         { "vui_parameters_present_flag",           "h264.vui_parameters_present_flag",
2355                         FT_UINT8, BASE_DEC, NULL, 0x0,
2356                         NULL, HFILL }
2357                 },
2358                 { &hf_h264_pic_parameter_set_id,
2359                         { "pic_parameter_set_id",           "h264.pic_parameter_set_id",
2360                         FT_UINT32, BASE_DEC, NULL, 0x0,
2361                         NULL, HFILL }
2362                 },
2363                 { &hf_h264_entropy_coding_mode_flag,
2364                         { "entropy_coding_mode_flag",           "h264.entropy_coding_mode_flag",
2365                         FT_UINT8, BASE_DEC, NULL, 0x0,
2366                         NULL, HFILL }
2367                 },
2368                 { &hf_h264_pic_order_present_flag,
2369                         { "pic_order_present_flag",           "h264.pic_order_present_flag",
2370                         FT_UINT8, BASE_DEC, NULL, 0x0,
2371                         NULL, HFILL }
2372                 },
2373                 { &hf_h264_num_slice_groups_minus1,
2374                         { "num_slice_groups_minus1",           "h264.num_slice_groups_minus1",
2375                         FT_UINT32, BASE_DEC, NULL, 0x0,
2376                         NULL, HFILL }
2377                 },
2378                 { &hf_h264_slice_group_map_type,
2379                         { "slice_group_map_type",           "h264.slice_group_map_type",
2380                         FT_UINT32, BASE_DEC, VALS(h264_slice_group_map_type_vals), 0x0,
2381                         NULL, HFILL }
2382                 },
2383                 { &hf_h264_num_ref_idx_l0_active_minus1,
2384                         { "num_ref_idx_l0_active_minus1",           "h264.num_ref_idx_l0_active_minus1",
2385                         FT_UINT32, BASE_DEC, NULL, 0x0,
2386                         NULL, HFILL }
2387                 },
2388                 { &hf_h264_num_ref_idx_l1_active_minus1,
2389                         { "num_ref_idx_l1_active_minus1",           "h264.num_ref_idx_l1_active_minus1",
2390                         FT_UINT32, BASE_DEC, NULL, 0x0,
2391                         NULL, HFILL }
2392                 },
2393                 { &hf_h264_weighted_pred_flag,
2394                         { "weighted_pred_flag",           "h264.weighted_pred_flag",
2395                         FT_UINT8, BASE_DEC, NULL, 0x0,
2396                         NULL, HFILL }
2397                 },
2398                 { &hf_h264_weighted_bipred_idc,
2399                         { "weighted_bipred_idc",           "h264.weighted_bipred_idc",
2400                         FT_UINT8, BASE_DEC, NULL, 0x0,
2401                         NULL, HFILL }
2402                 },
2403                 { &hf_h264_pic_init_qp_minus26,
2404                         { "pic_init_qp_minus26",           "h264.pic_init_qp_minus26",
2405                         FT_INT32, BASE_DEC, NULL, 0x0,
2406                         NULL, HFILL }
2407                 },
2408                 { &hf_h264_pic_init_qs_minus26,
2409                         { "pic_init_qs_minus26",           "h264.pic_init_qs_minus26",
2410                         FT_INT32, BASE_DEC, NULL, 0x0,
2411                         NULL, HFILL }
2412                 },
2413                 { &hf_h264_chroma_qp_index_offset,
2414                         { "chroma_qp_index_offset",           "h264.chroma_qp_index_offset",
2415                         FT_INT32, BASE_DEC, NULL, 0x0,
2416                         NULL, HFILL }
2417                 },
2418                 { &hf_h264_deblocking_filter_control_present_flag,
2419                         { "deblocking_filter_control_present_flag",           "h264.deblocking_filter_control_present_flag",
2420                         FT_UINT8, BASE_DEC, NULL, 0x0,
2421                         NULL, HFILL }
2422                 },
2423                 { &hf_h264_constrained_intra_pred_flag,
2424                         { "constrained_intra_pred_flag",           "h264.constrained_intra_pred_flag",
2425                         FT_UINT8, BASE_DEC, NULL, 0x0,
2426                         NULL, HFILL }
2427                 },
2428                 { &hf_h264_redundant_pic_cnt_present_flag,
2429                         { "redundant_pic_cnt_present_flag",           "h264.redundant_pic_cnt_present_flag",
2430                         FT_UINT8, BASE_DEC, NULL, 0x0,
2431                         NULL, HFILL }
2432                 },
2433                 { &hf_h264_transform_8x8_mode_flag,
2434                         { "transform_8x8_mode_flag",           "h264.transform_8x8_mode_flag",
2435                         FT_UINT8, BASE_DEC, NULL, 0x0,
2436                         NULL, HFILL }
2437                 },
2438                 { &hf_h264_pic_scaling_matrix_present_flag,
2439                         { "pic_scaling_matrix_present_flag",           "h264.pic_scaling_matrix_present_flag",
2440                         FT_UINT8, BASE_DEC, NULL, 0x0,
2441                         NULL, HFILL }
2442                 },
2443                 { &hf_h264_second_chroma_qp_index_offset,
2444                         { "second_chroma_qp_index_offset",           "h264.second_chroma_qp_index_offset",
2445                         FT_INT32, BASE_DEC, NULL, 0x0,
2446                         NULL, HFILL }
2447                 },
2448
2449                 { &hf_h264_aspect_ratio_info_present_flag,
2450                         { "aspect_ratio_info_present_flag",           "h264.aspect_ratio_info_present_flag",
2451                         FT_UINT8, BASE_DEC, NULL, 0x0,
2452                         NULL, HFILL }
2453                 },
2454                 { &hf_h264_aspect_ratio_idc,
2455                         { "aspect_ratio_idc",           "h264.aspect_ratio_idc",
2456                         FT_UINT8, BASE_DEC, NULL, 0x0,
2457                         NULL, HFILL }
2458                 },
2459                 { &hf_h264_sar_width,
2460                         { "sar_width",           "h264.sar_width",
2461                         FT_UINT16, BASE_DEC, NULL, 0x0,
2462                         NULL, HFILL }
2463                 },
2464                 { &hf_h264_sar_height,
2465                         { "sar_height",           "h264.sar_height",
2466                         FT_UINT16, BASE_DEC, NULL, 0x0,
2467                         NULL, HFILL }
2468                 },
2469                 { &hf_h264_overscan_info_present_flag,
2470                         { "overscan_info_present_flag",           "h264.overscan_info_present_flag",
2471                         FT_UINT8, BASE_DEC, NULL, 0x0,
2472                         NULL, HFILL }
2473                 },
2474                 { &hf_h264_overscan_appropriate_flag,
2475                         { "overscan_appropriate_flag",           "h264.overscan_appropriate_flag",
2476                         FT_UINT8, BASE_DEC, NULL, 0x0,
2477                         NULL, HFILL }
2478                 },
2479                 { &hf_h264_video_signal_type_present_flag,
2480                         { "video_signal_type_present_flag",           "h264.video_signal_type_present_flag",
2481                         FT_UINT8, BASE_DEC, NULL, 0x0,
2482                         NULL, HFILL }
2483                 },
2484                 { &hf_h264_video_format,
2485                         { "video_format",           "h264.video_format",
2486                         FT_UINT8, BASE_DEC, VALS(h264_video_format_vals), 0x0,
2487                         NULL, HFILL }
2488                 },
2489                 { &hf_h264_video_full_range_flag,
2490                         { "video_full_range_flag",           "h264.video_full_range_flag",
2491                         FT_UINT8, BASE_DEC, NULL, 0x0,
2492                         NULL, HFILL }
2493                 },
2494                 { &hf_h264_colour_description_present_flag,
2495                         { "colour_description_present_flag",           "h264.colour_description_present_flag",
2496                         FT_UINT8, BASE_DEC, NULL, 0x0,
2497                         NULL, HFILL }
2498                 },
2499                 { &hf_h264_colour_primaries,
2500                         { "colour_primaries",           "h264.colour_primaries",
2501                         FT_UINT8, BASE_DEC, NULL, 0x0,
2502                         NULL, HFILL }
2503                 },
2504                 { &hf_h264_transfer_characteristics,
2505                         { "transfer_characteristics",           "h264.transfer_characteristics",
2506                         FT_UINT8, BASE_DEC, NULL, 0x0,
2507                         NULL, HFILL }
2508                 },
2509                 { &hf_h264_matrix_coefficients,
2510                         { "matrix_coefficients",           "h264.matrix_coefficients",
2511                         FT_UINT8, BASE_DEC, NULL, 0x0,
2512                         NULL, HFILL }
2513                 },
2514                 { &hf_h264_chroma_loc_info_present_flag,
2515                         { "chroma_loc_info_present_flag",           "h264.chroma_loc_info_present_flag",
2516                         FT_UINT8, BASE_DEC, NULL, 0x0,
2517                         NULL, HFILL }
2518                 },
2519                 { &hf_h264_chroma_sample_loc_type_top_field,
2520                         { "chroma_sample_loc_type_top_field",           "h264.chroma_sample_loc_type_top_field",
2521                         FT_UINT32, BASE_DEC, NULL, 0x0,
2522                         NULL, HFILL }
2523                 },
2524                 { &hf_h264_chroma_sample_loc_type_bottom_field,
2525                         { "chroma_sample_loc_type_bottom_field",           "h264.chroma_sample_loc_type_bottom_field",
2526                         FT_UINT32, BASE_DEC, NULL, 0x0,
2527                         NULL, HFILL }
2528                 },
2529                 { &hf_h264_timing_info_present_flag,
2530                         { "timing_info_present_flag",           "h264.timing_info_present_flag",
2531                         FT_UINT8, BASE_DEC, NULL, 0x0,
2532                         NULL, HFILL }
2533                 },
2534                 { &hf_h264_num_units_in_tick,
2535                         { "num_units_in_tick",           "h264.num_units_in_tick",
2536                         FT_UINT32, BASE_DEC, NULL, 0x0,
2537                         NULL, HFILL }
2538                 },
2539                 { &hf_h264_time_scale,
2540                         { "time_scale",           "h264.time_scale",
2541                         FT_UINT32, BASE_DEC, NULL, 0x0,
2542                         NULL, HFILL }
2543                 },
2544                 { &hf_h264_fixed_frame_rate_flag,
2545                         { "fixed_frame_rate_flag",           "h264.fixed_frame_rate_flag",
2546                         FT_UINT8, BASE_DEC, NULL, 0x0,
2547                         NULL, HFILL }
2548                 },
2549                 { &hf_h264_nal_hrd_parameters_present_flag,
2550                         { "nal_hrd_parameters_present_flag",           "h264.nal_hrd_parameters_present_flag",
2551                         FT_UINT8, BASE_DEC, NULL, 0x0,
2552                         NULL, HFILL }
2553                 },
2554                 { &hf_h264_vcl_hrd_parameters_present_flag,
2555                         { "vcl_hrd_parameters_present_flag",           "h264.vcl_hrd_parameters_present_flag",
2556                         FT_UINT8, BASE_DEC, NULL, 0x0,
2557                         NULL, HFILL }
2558                 },
2559                 { &hf_h264_low_delay_hrd_flag,
2560                         { "low_delay_hrd_flag",           "h264.low_delay_hrd_flag",
2561                         FT_UINT8, BASE_DEC, NULL, 0x0,
2562                         NULL, HFILL }
2563                 },
2564                 { &hf_h264_pic_struct_present_flag,
2565                         { "pic_struct_present_flag",           "h264.pic_struct_present_flag",
2566                         FT_UINT8, BASE_DEC, NULL, 0x0,
2567                         NULL, HFILL }
2568                 },
2569                 { &hf_h264_bitstream_restriction_flag,
2570                         { "bitstream_restriction_flag",           "h264.bitstream_restriction_flag",
2571                         FT_UINT8, BASE_DEC, NULL, 0x0,
2572                         NULL, HFILL }
2573                 },
2574                 { &hf_h264_motion_vectors_over_pic_boundaries_flag,
2575                         { "motion_vectors_over_pic_boundaries_flag",           "h264.motion_vectors_over_pic_boundaries_flag",
2576                         FT_UINT32, BASE_DEC, NULL, 0x0,
2577                         NULL, HFILL }
2578                 },
2579                 { &hf_h264_max_bytes_per_pic_denom,
2580                         { "max_bytes_per_pic_denom",           "h264.max_bytes_per_pic_denom",
2581                         FT_UINT32, BASE_DEC, NULL, 0x0,
2582                         NULL, HFILL }
2583                 },
2584                 { &hf_h264_max_bits_per_mb_denom,
2585                         { "max_bits_per_mb_denom",           "h264.max_bits_per_mb_denom",
2586                         FT_UINT32, BASE_DEC, NULL, 0x0,
2587                         NULL, HFILL }
2588                 },
2589                 { &hf_h264_log2_max_mv_length_horizontal,
2590                         { "max_mv_length_horizontal",           "h264.max_mv_length_horizontal",
2591                         FT_UINT32, BASE_DEC, NULL, 0x0,
2592                         NULL, HFILL }
2593                 },
2594                 { &hf_h264_log2_max_mv_length_vertical,
2595                         { "log2_max_mv_length_vertical",           "h264.log2_max_mv_length_vertical",
2596                         FT_UINT32, BASE_DEC, NULL, 0x0,
2597                         NULL, HFILL }
2598                 },
2599                 { &hf_h264_num_reorder_frames,
2600                         { "num_reorder_frames",           "h264.num_reorder_frames",
2601                         FT_UINT32, BASE_DEC, NULL, 0x0,
2602                         NULL, HFILL }
2603                 },
2604                 { &hf_h264_max_dec_frame_buffering,
2605                         { "max_dec_frame_buffering",           "h264.max_dec_frame_buffering",
2606                         FT_UINT32, BASE_DEC, NULL, 0x0,
2607                         NULL, HFILL }
2608                 },
2609                 { &hf_h264_cpb_cnt_minus1,
2610                         { "cpb_cnt_minus1",           "h264.cpb_cnt_minus1",
2611                         FT_UINT32, BASE_DEC, NULL, 0x0,
2612                         NULL, HFILL }
2613                 },
2614                 { &hf_h264_bit_rate_scale,
2615                         { "bit_rate_scale",           "h264.bit_rate_scale",
2616                         FT_UINT8, BASE_DEC, NULL, 0x0,
2617                         NULL, HFILL }
2618                 },
2619                 { &hf_h264_cpb_size_scale,
2620                         { "cpb_size_scale",           "h264.cpb_size_scale",
2621                         FT_UINT8, BASE_DEC, NULL, 0x0,
2622                         NULL, HFILL }
2623                 },
2624                 { &hf_h264_bit_rate_value_minus1,
2625                         { "bit_rate_value_minus1",           "h264.bit_rate_value_minus1",
2626                         FT_UINT32, BASE_DEC, NULL, 0x0,
2627                         NULL, HFILL }
2628                 },
2629                 { &hf_h264_cpb_size_value_minus1,
2630                         { "cpb_size_value_minus1",           "h264.cpb_size_value_minus1",
2631                         FT_UINT32, BASE_DEC, NULL, 0x0,
2632                         NULL, HFILL }
2633                 },
2634                 { &hf_h264_cbr_flag,
2635                         { "cbr_flag",           "h264.cbr_flag",
2636                         FT_UINT8, BASE_DEC, NULL, 0x0,
2637                         NULL, HFILL }
2638                 },
2639                 { &hf_h264_initial_cpb_removal_delay_length_minus1,
2640                         { "initial_cpb_removal_delay_length_minus1",           "h264.initial_cpb_removal_delay_length_minus1",
2641                         FT_UINT8, BASE_DEC, NULL, 0x0,
2642                         NULL, HFILL }
2643                 },
2644                 { &hf_h264_cpb_removal_delay_length_minus1,
2645                         { "cpb_removal_delay_length_minus1",           "h264.cpb_removal_delay_length_minus1",
2646                         FT_UINT8, BASE_DEC, NULL, 0x0,
2647                         NULL, HFILL }
2648                 },
2649                 { &hf_h264_dpb_output_delay_length_minus11,
2650                         { "dpb_output_delay_length_minus11",           "h264.dpb_output_delay_length_minus11",
2651                         FT_UINT8, BASE_DEC, NULL, 0x0,
2652                         NULL, HFILL }
2653                 },
2654                 { &hf_h264_time_offset_length,
2655                         { "time_offset_length",           "h264.time_offset_length",
2656                         FT_UINT8, BASE_DEC, NULL, 0x0,
2657                         NULL, HFILL }
2658                 },
2659                 { &hf_h264_first_mb_in_slice,
2660                         { "first_mb_in_slice",           "h264.first_mb_in_slice",
2661                         FT_UINT32, BASE_DEC, NULL, 0x0,
2662                         NULL, HFILL }
2663                 },
2664                 { &hf_h264_slice_type,
2665                         { "slice_type",           "h264.slice_type",
2666                         FT_UINT32, BASE_DEC, VALS(h264_slice_type_vals), 0x0,
2667                         NULL, HFILL }
2668                 },
2669                 { &hf_h264_slice_id,
2670                         { "slice_id",           "h264.slice_id",
2671                         FT_UINT32, BASE_DEC, NULL, 0x0,
2672                         NULL, HFILL }
2673                 },
2674                 { &hf_h264_payloadsize,
2675                         { "PayloadSize",           "h264.payloadsize",
2676                         FT_UINT32, BASE_DEC, NULL, 0x0,
2677                         NULL, HFILL }
2678                 },
2679                 { &hf_h264_payloadtype,
2680                         { "payloadType",           "h264.payloadtype",
2681                         FT_UINT32, BASE_DEC, VALS(h264_sei_payload_vals), 0x0,
2682                         NULL, HFILL }
2683                 },
2684                 { &hf_h264_frame_num,
2685                         { "frame_num",           "h264.frame_num",
2686                         FT_UINT8, BASE_DEC, NULL, 0x0,
2687                         NULL, HFILL }
2688                 },
2689                 { &hf_h264_par_profile,
2690                         { "Profile", "h264.profile",
2691                         FT_UINT8, BASE_HEX, NULL, 0x00,
2692                         NULL, HFILL}},
2693                 { &hf_h264_par_profile_b,
2694                         { "Baseline Profile", "h264.profile.base",
2695                         FT_BOOLEAN, 8, NULL, 0x40,
2696                         NULL, HFILL}},
2697                 { &hf_h264_par_profile_m,
2698                         { "Main Profile", "h264.profile.main",
2699                         FT_BOOLEAN, 8, NULL, 0x20,
2700                         NULL, HFILL}},
2701                 { &hf_h264_par_profile_e,
2702                         { "Extended Profile.", "h264.profile.ext",
2703                         FT_BOOLEAN, 8, NULL, 0x10,
2704                         NULL, HFILL}},
2705                 { &hf_h264_par_profile_h,
2706                         { "High Profile", "h264.profile.high",
2707                         FT_BOOLEAN, 8, NULL, 0x08,
2708                         NULL, HFILL}},
2709                 { &hf_h264_par_profile_h10,
2710                         { "High 10 Profile", "h264.profile.high10",
2711                         FT_BOOLEAN, 8, NULL, 0x04,
2712                         NULL, HFILL}},
2713                 { &hf_h264_par_profile_h4_2_2,
2714                         { "High 4:2:2 Profile", "h264.profile.high4_2_2",
2715                         FT_BOOLEAN, 8, NULL, 0x02,
2716                         NULL, HFILL}},
2717                 { &hf_h264_par_profile_h4_4_4,
2718                         { "High 4:4:4 Profile", "h264.profile.high4_4_4",
2719                         FT_BOOLEAN, 8, NULL, 0x01,
2720                         NULL, HFILL}},
2721                 { &hf_h264_par_AdditionalModesSupported,
2722                         { "AdditionalModesSupported", "h264.AdditionalModesSupported",
2723                         FT_UINT8, BASE_HEX, NULL, 0x00,
2724                         NULL, HFILL}},
2725                 { &hf_h264_par_add_mode_sup,
2726                         { "Additional Modes Supported", "h264.add_mode_sup",
2727                         FT_UINT8, BASE_HEX, NULL, 0x00,
2728                         NULL, HFILL}},
2729                 { &hf_h264_par_add_mode_sup_rcdo,
2730                         { "Reduced Complexity Decoding Operation (RCDO) support", "h264.add_mode_sup.rcdo",
2731                         FT_BOOLEAN, 8, NULL, 0x40,
2732                         NULL, HFILL}},
2733                 { &hf_h264_par_ProfileIOP,
2734                         { "ProfileIOP", "h264.ProfileIOP",
2735                         FT_UINT8, BASE_HEX, NULL, 0x00,
2736                         NULL, HFILL}},
2737                 { &hf_h264_par_constraint_set0_flag,
2738                         { "constraint_set0_flag", "h264.par.constraint_set0_flag",
2739                         FT_BOOLEAN, 8, NULL, 0x80,
2740                         NULL, HFILL}},
2741                 { &hf_h264_par_constraint_set1_flag,
2742                         { "constraint_set1_flag", "h264.par.constraint_set1_flag",
2743                         FT_BOOLEAN, 8, NULL, 0x40,
2744                         NULL, HFILL}},
2745                 { &hf_h264_par_constraint_set2_flag,
2746                         { "constraint_set2_flag", "h264.par.constraint_set2_flag",
2747                         FT_BOOLEAN, 8, NULL, 0x20,
2748                         NULL, HFILL}},
2749         };
2750
2751 /* Setup protocol subtree array */
2752         static gint *ett[] = {
2753                 &ett_h264,
2754                 &ett_h264_profile,
2755                 &ett_h264_nal,
2756                 &ett_h264_fua,
2757                 &ett_h264_stream,
2758                 &ett_h264_nal_unit,
2759                 &ett_h264_par_profile,
2760                 &ett_h264_par_AdditionalModesSupported,
2761                 &ett_h264_par_ProfileIOP,
2762         };
2763
2764 /* Register the protocol name and description */
2765         proto_h264 = proto_register_protocol("H.264","H264", "h264");
2766
2767 /* Required function calls to register the header fields and subtrees used */
2768         proto_register_field_array(proto_h264, hf, array_length(hf));
2769         proto_register_subtree_array(ett, array_length(ett));
2770         /* Register a configuration option for port */
2771
2772
2773         h264_module = prefs_register_protocol(proto_h264, proto_reg_handoff_h264);
2774
2775
2776         prefs_register_range_preference(h264_module, "dynamic.payload.type",
2777                                                     "H264 dynamic payload types",
2778                                                     "Dynamic payload types which will be interpreted as H264"
2779                                                     "; Values must be in the range 96 - 127",
2780                                                     &temp_dynamic_payload_type_range, 127);
2781
2782         register_dissector("h264", dissect_h264, proto_h264);
2783 }
2784
2785