fix usage of "if(tree) {" to display the right things, even if no coloring rule is set
[obnox/wireshark/wip.git] / epan / dissectors / packet-h261.c
1 /* packet-h261.c
2  *
3  * Routines for ITU-T Recommendation H.261 dissection
4  *
5  * $Id$
6  *
7  * Copyright 2000, Philips Electronics N.V.
8  * Andreas Sikkema <h323@ramdyne.nl>
9  *
10  * Ethereal - Network traffic analyzer
11  * By Gerald Combs <gerald@ethereal.com>
12  * Copyright 1998 Gerald Combs
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 /*
30  * This dissector tries to dissect the H.261 protocol according to Annex C
31  * of ITU-T Recommendation H.225.0 (02/98)
32  */
33
34
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <glib.h>
40 #include <epan/packet.h>
41
42 #include <stdio.h>
43 #include <string.h>
44
45 #include "rtp_pt.h"
46
47 /* H.261 header fields             */
48 static int proto_h261          = -1;
49 static int hf_h261_sbit        = -1;
50 static int hf_h261_ebit        = -1;
51 static int hf_h261_ibit        = -1;
52 static int hf_h261_vbit        = -1;
53 static int hf_h261_gobn        = -1;
54 static int hf_h261_mbap        = -1;
55 static int hf_h261_quant       = -1;
56 static int hf_h261_hmvd        = -1; /* Mislabeled in a figure in section C.3.1 as HMDV */
57 static int hf_h261_vmvd        = -1;
58 static int hf_h261_data        = -1;
59
60 /* H.261 fields defining a sub tree */
61 static gint ett_h261           = -1;
62
63 static void
64 dissect_h261( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree )
65 {
66         proto_item *ti            = NULL;
67         proto_tree *h261_tree     = NULL;
68         unsigned int offset       = 0;
69
70         if ( check_col( pinfo->cinfo, COL_PROTOCOL ) )   {
71                 col_set_str( pinfo->cinfo, COL_PROTOCOL, "H.261" );
72         }
73
74         if ( check_col( pinfo->cinfo, COL_INFO) ) {
75                 col_set_str( pinfo->cinfo, COL_INFO, "H.261 message");
76         }
77
78         if ( tree ) {
79                 ti = proto_tree_add_item( tree, proto_h261, tvb, offset, -1, FALSE );
80                 h261_tree = proto_item_add_subtree( ti, ett_h261 );
81                 /* SBIT 1st octet, 3 bits */
82                 proto_tree_add_uint( h261_tree, hf_h261_sbit, tvb, offset, 1, tvb_get_guint8( tvb, offset ) >> 5 );
83                 /* EBIT 1st octet, 3 bits */
84                 proto_tree_add_uint( h261_tree, hf_h261_ebit, tvb, offset, 1, ( tvb_get_guint8( tvb, offset )  << 3 ) >> 5 );
85                 /* I flag, 1 bit */
86                 proto_tree_add_boolean( h261_tree, hf_h261_ibit, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 2 );
87                 /* V flag, 1 bit */
88                 proto_tree_add_boolean( h261_tree, hf_h261_vbit, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 1 );
89                 offset++;
90
91                 /* GOBN 2nd octet, 4 bits */
92                 proto_tree_add_uint( h261_tree, hf_h261_gobn, tvb, offset, 1, tvb_get_guint8( tvb, offset ) >> 4 );
93                 /* MBAP 2nd octet, 4 bits, 3rd octet 1 bit */
94                 proto_tree_add_uint( h261_tree, hf_h261_mbap, tvb, offset, 1,
95                     ( tvb_get_guint8( tvb, offset ) & 15 )
96                     + ( tvb_get_guint8( tvb, offset + 1 ) >> 7 ) );
97                 offset++;
98
99                 /* QUANT 3rd octet, 5 bits (starting at bit 2!) */
100                 proto_tree_add_uint( h261_tree, hf_h261_quant, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 124 );
101
102                 /* HMVD 3rd octet 2 bits, 4th octet 3 bits */
103                 proto_tree_add_uint( h261_tree, hf_h261_hmvd, tvb, offset, 2,
104                     ( ( tvb_get_guint8( tvb, offset ) & 0x03 ) << 3 )
105                      + ( tvb_get_guint8( tvb, offset+1 ) >> 5 ) );
106                 offset++;
107
108                 /* VMVD 4th octet, last 5 bits */
109                 proto_tree_add_uint( h261_tree, hf_h261_vmvd, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 31 );
110                 offset++;
111
112                 /* The rest of the packet is the H.261 stream */
113                 proto_tree_add_item( h261_tree, hf_h261_data, tvb, offset, -1, FALSE );
114         }
115 }
116
117 void
118 proto_register_h261(void)
119 {
120         static hf_register_info hf[] =
121         {
122                 {
123                         &hf_h261_sbit,
124                         {
125                                 "Start bit position",
126                                 "h261.sbit",
127                                 FT_UINT8,
128                                 BASE_DEC,
129                                 NULL,
130                                 0x0,
131                                 "", HFILL
132                         }
133                 },
134                 {
135                         &hf_h261_ebit,
136                         {
137                                 "End bit position",
138                                 "h261.ebit",
139                                 FT_UINT8,
140                                 BASE_DEC,
141                                 NULL,
142                                 0x0,
143                                 "", HFILL
144                         }
145                 },
146                 {
147                         &hf_h261_ibit,
148                         {
149                                 "Intra frame encoded data flag",
150                                 "h261.i",
151                                 FT_BOOLEAN,
152                                 BASE_NONE,
153                                 NULL,
154                                 0x0,
155                                 "", HFILL
156                         }
157                 },
158                 {
159                         &hf_h261_vbit,
160                         {
161                                 "Motion vector flag",
162                                 "h261.v",
163                                 FT_BOOLEAN,
164                                 BASE_NONE,
165                                 NULL,
166                                 0x0,
167                                 "", HFILL
168                         }
169                 },
170                 {
171                         &hf_h261_gobn,
172                         {
173                                 "GOB Number",
174                                 "h261.gobn",
175                                 FT_UINT8,
176                                 BASE_DEC,
177                                 NULL,
178                                 0x0,
179                                 "", HFILL
180                         }
181                 },
182                 {
183                         &hf_h261_mbap,
184                         {
185                                 "Macroblock address predictor",
186                                 "h261.mbap",
187                                 FT_UINT8,
188                                 BASE_DEC,
189                                 NULL,
190                                 0x0,
191                                 "", HFILL
192                         }
193                 },
194                 {
195                         &hf_h261_quant,
196                         {
197                                 "Quantizer",
198                                 "h261.quant",
199                                 FT_UINT8,
200                                 BASE_DEC,
201                                 NULL,
202                                 0x0,
203                                 "", HFILL
204                         }
205                 },
206                 {
207                         &hf_h261_hmvd,
208                         {
209                                 "Horizontal motion vector data",
210                                 "h261.hmvd",
211                                 FT_UINT8,
212                                 BASE_DEC,
213                                 NULL,
214                                 0x0,
215                                 "", HFILL
216                         }
217                 },
218                 {
219                         &hf_h261_vmvd,
220                         {
221                                 "Vertical motion vector data",
222                                 "h261.vmvd",
223                                 FT_UINT8,
224                                 BASE_DEC,
225                                 NULL,
226                                 0x0,
227                                 "", HFILL
228                         }
229                 },
230                 {
231                         &hf_h261_data,
232                         {
233                                 "H.261 stream",
234                                 "h261.stream",
235                                 FT_BYTES,
236                                 BASE_NONE,
237                                 NULL,
238                                 0x0,
239                                 "", HFILL
240                         }
241                 },
242 };
243
244         static gint *ett[] =
245         {
246                 &ett_h261,
247         };
248
249
250         proto_h261 = proto_register_protocol("ITU-T Recommendation H.261",
251             "H.261", "h261");
252         proto_register_field_array(proto_h261, hf, array_length(hf));
253         proto_register_subtree_array(ett, array_length(ett));
254 }
255
256 void
257 proto_reg_handoff_h261(void)
258 {
259         dissector_handle_t h261_handle;
260
261         h261_handle = create_dissector_handle(dissect_h261, proto_h261);
262         dissector_add("rtp.pt", PT_H261, h261_handle);
263 }