As the gtk2 directory is no longer needed (GTK1 and 2 are using the same sources...
[obnox/wireshark/wip.git] / packet-h263.c
1 /* packet-h263.c
2  *
3  * Routines for ITU-T Recommendation H.263 dissection
4  *
5  * Copyright 2003 Niklas Ă–gren <niklas.ogren@7l.se>
6  * Seven Levels Consultants AB
7  *
8  * $Id$
9  *
10  * Ethereal - Network traffic analyzer
11  * By Gerald Combs <gerald@ethereal.com>
12  * Copyright 1998 Gerald Combs
13  *
14  * Copied structure from packet-h261.c
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
29  */
30
31 /*
32  * This dissector tries to dissect the H.263 protocol according to
33  * ITU-T Recommendations and RFC 2190
34  */
35
36
37 #ifdef HAVE_CONFIG_H
38 # include "config.h"
39 #endif
40
41 #include <glib.h>
42 #include <epan/packet.h>
43
44 #include <stdio.h>
45 #include <string.h>
46
47 #include "rtp_pt.h"
48 #include "iax2_codec_type.h"
49
50 /* H.263 header fields             */
51 static int proto_h263          = -1;
52
53 /* Mode A header */
54 static int hf_h263_ftype = -1;
55 static int hf_h263_pbframes = -1;
56 static int hf_h263_sbit = -1;
57 static int hf_h263_ebit = -1;
58 static int hf_h263_srcformat = -1;
59 static int hf_h263_picture_coding_type = -1;    
60 static int hf_h263_unrestricted_motion_vector = -1;
61 static int hf_h263_syntax_based_arithmetic = -1;
62 static int hf_h263_advanced_prediction = -1;
63 static int hf_h263_r = -1;
64 static int hf_h263_rr = -1;
65 static int hf_h263_dbq = -1;
66 static int hf_h263_trb = -1;
67 static int hf_h263_tr = -1;
68 /* Additional fields for Mode B or C header */
69 static int hf_h263_quant = -1;
70 static int hf_h263_gobn = -1;
71 static int hf_h263_mba = -1;
72 static int hf_h263_hmv1 = -1;
73 static int hf_h263_vmv1 = -1;
74 static int hf_h263_hmv2 = -1;
75 static int hf_h263_vmv2 = -1;
76
77 static int hf_h263_data        = -1;
78
79 /* Source format types */
80 #define SRCFORMAT_FORB   0  /* forbidden */
81 #define SRCFORMAT_SQCIF  1
82 #define SRCFORMAT_QCIF   2
83 #define SRCFORMAT_CIF    3
84 #define SRCFORMAT_4CIF   4
85 #define SRCFORMAT_16CIF  5
86
87 static const value_string srcformat_vals[] =
88 {
89   { SRCFORMAT_FORB,     "forbidden" },
90   { SRCFORMAT_SQCIF,    "sub-QCIF 128x96" },
91   { SRCFORMAT_QCIF,     "QCIF 176x144" },
92   { SRCFORMAT_CIF,      "CIF 352x288" },
93   { SRCFORMAT_4CIF,     "4CIF 704x576" },
94   { SRCFORMAT_16CIF,    "16CIF 1408x1152" },
95   { 0,          NULL },
96 };
97
98 /* H.263 fields defining a sub tree */
99 static gint ett_h263           = -1;
100
101 static void
102 dissect_h263( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree )
103 {
104         proto_item *ti            = NULL;
105         proto_tree *h263_tree     = NULL;
106         unsigned int offset       = 0;
107         unsigned int h263_version = 0;
108
109         h263_version = (tvb_get_guint8( tvb, offset ) & 0xc0 ) >> 6;
110
111         if ( check_col( pinfo->cinfo, COL_PROTOCOL ) )   {
112                 col_set_str( pinfo->cinfo, COL_PROTOCOL, "H.263" );
113         }
114
115         if( h263_version == 0x00) {
116           if ( check_col( pinfo->cinfo, COL_INFO) ) {
117             col_append_str( pinfo->cinfo, COL_INFO, " MODE A");
118           }
119         }
120         else if( h263_version == 0x02) {
121           if ( check_col( pinfo->cinfo, COL_INFO) ) {
122             col_append_str( pinfo->cinfo, COL_INFO, " MODE B");
123           }
124         }
125         else if( h263_version == 0x03) {
126           if ( check_col( pinfo->cinfo, COL_INFO) ) {
127             col_append_str( pinfo->cinfo, COL_INFO, " MODE C");
128           }
129         }
130
131         if ( tree ) {
132           ti = proto_tree_add_item( tree, proto_h263, tvb, offset, -1, FALSE );
133           h263_tree = proto_item_add_subtree( ti, ett_h263 );
134
135           /* FBIT 1st octet, 1 bit */
136           proto_tree_add_boolean( h263_tree, hf_h263_ftype, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x80 );
137           /* PBIT 1st octet, 1 bit */
138           proto_tree_add_boolean( h263_tree, hf_h263_pbframes, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x40 );
139           /* SBIT 1st octet, 3 bits */
140           proto_tree_add_uint( h263_tree, hf_h263_sbit, tvb, offset, 1, ( tvb_get_guint8( tvb, offset ) & 0x38 ) >> 3 );
141           /* EBIT 1st octet, 3 bits */
142           proto_tree_add_uint( h263_tree, hf_h263_ebit, tvb, offset, 1, tvb_get_guint8( tvb, offset )  & 0x7 );
143
144           offset++;
145
146           /* SRC 2nd octet, 3 bits */
147           proto_tree_add_uint( h263_tree, hf_h263_srcformat, tvb, offset, 1, tvb_get_guint8( tvb, offset ) >> 5 );
148
149           if(h263_version == 0x00) { /* MODE A */
150             /* I flag, 1 bit */
151             proto_tree_add_boolean( h263_tree, hf_h263_picture_coding_type, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x10 );
152             /* U flag, 1 bit */
153             proto_tree_add_boolean( h263_tree, hf_h263_unrestricted_motion_vector, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x08 );
154             /* S flag, 1 bit */
155             proto_tree_add_boolean( h263_tree, hf_h263_syntax_based_arithmetic, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x04 );
156             /* A flag, 1 bit */
157             proto_tree_add_boolean( h263_tree, hf_h263_advanced_prediction, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x02 );
158
159             /* Reserved 2nd octect, 1 bit + 3rd octect 3 bits */
160             proto_tree_add_uint( h263_tree, hf_h263_r, tvb, offset, 2, ( ( tvb_get_guint8( tvb, offset ) & 0x1 ) << 3 ) + ( ( tvb_get_guint8( tvb, offset + 1 ) & 0xe0 ) >> 5 ) );
161
162             offset++;
163
164             /* DBQ 3 octect, 2 bits */
165             proto_tree_add_uint( h263_tree, hf_h263_dbq, tvb, offset, 1, ( tvb_get_guint8( tvb, offset ) & 0x18 ) >> 3 );
166             /* TRB 3 octect, 3 bits */
167             proto_tree_add_uint( h263_tree, hf_h263_trb, tvb, offset, 1, ( tvb_get_guint8( tvb, offset ) & 0x07 ) );
168
169             offset++;
170             
171             /* TR 4 octect, 8 bits */
172             proto_tree_add_uint( h263_tree, hf_h263_tr, tvb, offset, 1, tvb_get_guint8( tvb, offset ) );
173             
174             offset++;
175
176           } else { /* MODE B or MODE C */
177
178             /* QUANT 2 octect, 5 bits */
179             proto_tree_add_uint( h263_tree, hf_h263_quant, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x1f );
180
181             offset++;
182
183             /* GOBN 3 octect, 5 bits */
184             proto_tree_add_uint( h263_tree, hf_h263_gobn, tvb, offset, 1, ( tvb_get_guint8( tvb, offset ) & 0xf8 ) >> 3);
185             /* MBA 3 octect, 3 bits + 4 octect 6 bits */
186             proto_tree_add_uint( h263_tree, hf_h263_mba, tvb, offset, 2, ( ( tvb_get_guint8( tvb, offset ) & 0x7 ) << 6 ) + ( ( tvb_get_guint8( tvb, offset + 1 ) & 0xfc ) >> 2 ) );
187             
188             offset++;
189
190             /* Reserved 4th octect, 2 bits */
191             proto_tree_add_uint( h263_tree, hf_h263_r, tvb, offset, 1, ( tvb_get_guint8( tvb, offset ) & 0x3 ) );
192
193             offset++;
194
195             /* I flag, 1 bit */
196             proto_tree_add_boolean( h263_tree, hf_h263_picture_coding_type, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x80 );
197             /* U flag, 1 bit */
198             proto_tree_add_boolean( h263_tree, hf_h263_unrestricted_motion_vector, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x40 );
199             /* S flag, 1 bit */
200             proto_tree_add_boolean( h263_tree, hf_h263_syntax_based_arithmetic, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x20 );
201             /* A flag, 1 bit */
202             proto_tree_add_boolean( h263_tree, hf_h263_advanced_prediction, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x10 );
203
204             /* HMV1 5th octect, 4 bits + 6th octect 3 bits*/
205             proto_tree_add_uint( h263_tree, hf_h263_hmv1, tvb, offset, 2,( ( tvb_get_guint8( tvb, offset ) & 0xf ) << 3 ) + ( ( tvb_get_guint8( tvb, offset+1 ) & 0xe0 ) >> 5) );
206
207             offset++;
208             
209             /* VMV1 6th octect, 5 bits + 7th octect 2 bits*/
210             proto_tree_add_uint( h263_tree, hf_h263_vmv1, tvb, offset, 2,( ( tvb_get_guint8( tvb, offset ) & 0x1f ) << 2 ) + ( ( tvb_get_guint8( tvb, offset+1 ) & 0xc0 ) >> 6) );
211             
212             offset++;
213
214             /* HMV2 7th octect, 6 bits + 8th octect 1 bit*/
215             proto_tree_add_uint( h263_tree, hf_h263_hmv2, tvb, offset, 2,( ( tvb_get_guint8( tvb, offset ) & 0x3f ) << 1 ) + ( ( tvb_get_guint8( tvb, offset+1 ) & 0xf0 ) >> 7) );
216             
217             offset++;
218
219             /* VMV2 8th octect, 7 bits*/
220             proto_tree_add_uint( h263_tree, hf_h263_vmv2, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x7f );
221                   
222             offset++;
223
224             if(h263_version == 0x03) { /* MODE C */
225               /* Reserved 9th to 11th octect, 8 + 8 + 3 bits */
226               proto_tree_add_uint( h263_tree, hf_h263_rr, tvb, offset, 3, ( tvb_get_guint8( tvb, offset ) << 11 ) + ( tvb_get_guint8( tvb, offset + 1 ) << 3 ) + ( ( tvb_get_guint8( tvb, offset + 2 ) & 0xe0 ) >> 5 ) );
227
228               offset+=2;
229
230               /* DBQ 11th octect, 2 bits */
231               proto_tree_add_uint( h263_tree, hf_h263_dbq, tvb, offset, 1, ( tvb_get_guint8( tvb, offset ) & 0x18 ) >>3 );
232               /* TRB 11th octect, 3 bits */
233               proto_tree_add_uint( h263_tree, hf_h263_trb, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x07 );
234               
235               offset++;
236               
237               /* TR 12th octect, 8 bits */
238               proto_tree_add_uint( h263_tree, hf_h263_tr, tvb, offset, 1, tvb_get_guint8( tvb, offset ) );
239               
240               offset++;
241             } /* end mode c */
242           } /* end not mode a */
243
244           /* The rest of the packet is the H.263 stream */
245           proto_tree_add_item( h263_tree, hf_h263_data, tvb, offset, -1, FALSE );
246         }
247 }
248
249 void
250 proto_register_h263(void)
251 {
252         static hf_register_info hf[] =
253         {
254                 {
255                         &hf_h263_ftype,
256                         {
257                                 "F",
258                                 "h263.sbit",
259                                 FT_BOOLEAN,
260                                 BASE_NONE,
261                                 NULL,
262                                 0x0,
263                                 "Indicates the mode of the payload header (MODE A or B/C)", HFILL
264                         }
265                 },
266                 {
267                         &hf_h263_pbframes,
268                         {
269                                 "p/b frame",
270                                 "h263.pbframes",
271                                 FT_BOOLEAN,
272                                 BASE_NONE,
273                                 NULL,
274                                 0x0,
275                                 "Optional PB-frames mode as defined by H.263 (MODE C)", HFILL
276                         }
277                 },
278                 {
279                         &hf_h263_sbit,
280                         {
281                                 "Start bit position",
282                                 "h263.sbit",
283                                 FT_UINT8,
284                                 BASE_DEC,
285                                 NULL,
286                                 0x0,
287                                 "Start bit position specifies number of most significant bits that shall be ignored in the first data byte.", HFILL
288                         }
289                 },
290                 {
291                         &hf_h263_ebit,
292                         {
293                                 "End bit position",
294                                 "h263.ebit",
295                                 FT_UINT8,
296                                 BASE_DEC,
297                                 NULL,
298                                 0x0,
299                                 "End bit position specifies number of least significant bits that shall be ignored in the last data byte.", HFILL
300                         }
301                 },
302                 {
303                         &hf_h263_srcformat,
304                         {
305                                 "SRC format",
306                                 "h263.srcformat",
307                                 FT_UINT8,
308                                 BASE_DEC,
309                                 VALS(srcformat_vals),
310                                 0x0,
311                                 "Source format specifies the resolution of the current picture.", HFILL
312                         }
313                 },
314                 {
315                         &hf_h263_picture_coding_type,
316                         {
317                                 "Inter-coded frame",
318                                 "h263.picture_coding_type",
319                                 FT_BOOLEAN,
320                                 BASE_NONE,
321                                 NULL,
322                                 0x0,
323                                 "Picture coding type, intra-coded (false) or inter-coded (true)", HFILL
324                         }
325                 },
326                 {
327                         &hf_h263_unrestricted_motion_vector,
328                         {
329                                 "Motion vector",
330                                 "h263.unrestricted_motion_vector",
331                                 FT_BOOLEAN,
332                                 BASE_NONE,
333                                 NULL,
334                                 0x0,
335                                 "Unrestricted Motion Vector option for current picture", HFILL
336                         }
337                 },
338                 {
339                         &hf_h263_syntax_based_arithmetic,
340                         {
341                                 "Syntax-based arithmetic coding",
342                                 "h263.syntax_based_arithmetic",
343                                 FT_BOOLEAN,
344                                 BASE_NONE,
345                                 NULL,
346                                 0x0,
347                                 "Syntax-based Arithmetic Coding option for current picture", HFILL
348                         }
349                 },
350                 {
351                         &hf_h263_advanced_prediction,
352                         {
353                                 "Advanced prediction option",
354                                 "h263.advanced_prediction",
355                                 FT_BOOLEAN,
356                                 BASE_NONE,
357                                 NULL,
358                                 0x0,
359                                 "Advanced Prediction option for current picture", HFILL
360                         }
361                 },
362                 {
363                         &hf_h263_dbq,
364                         {
365                                 "Differential quantization parameter",
366                                 "h263.dbq",
367                                 FT_UINT8,
368                                 BASE_DEC,
369                                 NULL,
370                                 0x0,
371                                 "Differential quantization parameter used to calculate quantizer for the B frame based on quantizer for the P frame, when PB-frames option is used.", HFILL
372                         }
373                 },
374                 {
375                         &hf_h263_trb,
376                         {
377                                 "Temporal Reference for B frames",
378                                 "h263.trb",
379                                 FT_UINT8,
380                                 BASE_DEC,
381                                 NULL,
382                                 0x0,
383                                 "Temporal Reference for the B frame as defined by H.263", HFILL
384                         }
385                 },
386                 {
387                         &hf_h263_tr,
388                         {
389                                 "Temporal Reference for P frames",
390                                 "h263.tr",
391                                 FT_UINT8,
392                                 BASE_DEC,
393                                 NULL,
394                                 0x0,
395                                 "Temporal Reference for the P frame as defined by H.263", HFILL
396                         }
397                 },
398                 {
399                         &hf_h263_quant,
400                         {
401                                 "Quantizer",
402                                 "h263.quant",
403                                 FT_UINT8,
404                                 BASE_DEC,
405                                 NULL,
406                                 0x0,
407                                 "Quantization value for the first MB coded at the starting of the packet.", HFILL
408                         }
409                 },
410                 {
411                         &hf_h263_gobn,
412                         {
413                                 "GOB Number",
414                                 "h263.gobn",
415                                 FT_UINT8,
416                                 BASE_DEC,
417                                 NULL,
418                                 0x0,
419                                 "GOB number in effect at the start of the packet.", HFILL
420                         }
421                 },
422                 {
423                         &hf_h263_mba,
424                         {
425                                 "Macroblock address",
426                                 "h263.mba",
427                                 FT_UINT16,
428                                 BASE_DEC,
429                                 NULL,
430                                 0x0,
431                                 "The address within the GOB of the first MB in the packet, counting from zero in scan order.", HFILL
432                         }
433                 },
434                 {
435                         &hf_h263_hmv1,
436                         {
437                                 "Horizontal motion vector 1",
438                                 "h263.hmv1",
439                                 FT_UINT8,
440                                 BASE_DEC,
441                                 NULL,
442                                 0x0,
443                                 "Horizontal motion vector predictor for the first MB in this packet ", HFILL
444                         }
445                 },
446                 {
447                         &hf_h263_vmv1,
448                         {
449                                 "Vertical motion vector 1",
450                                 "h263.vmv1",
451                                 FT_UINT8,
452                                 BASE_DEC,
453                                 NULL,
454                                 0x0,
455                                 "Vertical motion vector predictor for the first MB in this packet ", HFILL
456                         }
457                 },
458                 {
459                         &hf_h263_hmv2,
460                         {
461                                 "Horizontal motion vector 2",
462                                 "h263.hmv2",
463                                 FT_UINT8,
464                                 BASE_DEC,
465                                 NULL,
466                                 0x0,
467                                 "Horizontal motion vector predictor for block number 3 in the first MB in this packet when four motion vectors are used with the advanced prediction option.", HFILL
468                         }
469                 },
470                 {
471                         &hf_h263_vmv2,
472                         {
473                                 "Vertical motion vector 2",
474                                 "h263.vmv2",
475                                 FT_UINT8,
476                                 BASE_DEC,
477                                 NULL,
478                                 0x0,
479                                 "Vertical motion vector predictor for block number 3 in the first MB in this packet when four motion vectors are used with the advanced prediction option.", HFILL
480                         }
481                 },
482                 {
483                         &hf_h263_r,
484                         {
485                                 "Reserved field",
486                                 "h263.r",
487                                 FT_UINT8,
488                                 BASE_DEC,
489                                 NULL,
490                                 0x0,
491                                 "Reserved field that houls contain zeroes", HFILL
492                         }
493                 },
494                 {
495                         &hf_h263_rr,
496                         {
497                                 "Reserved field 2",
498                                 "h263.rr",
499                                 FT_UINT16,
500                                 BASE_DEC,
501                                 NULL,
502                                 0x0,
503                                 "Reserved field that should contain zeroes", HFILL
504                         }
505                 },
506                 {
507                         &hf_h263_data,
508                         {
509                                 "H.263 stream",
510                                 "h263.stream",
511                                 FT_BYTES,
512                                 BASE_NONE,
513                                 NULL,
514                                 0x0,
515                                 "The H.263 stream including its Picture, GOB or Macro block start code.", HFILL
516                         }
517                 },
518 };
519
520         static gint *ett[] =
521         {
522                 &ett_h263,
523         };
524
525
526         proto_h263 = proto_register_protocol("ITU-T Recommendation H.263 RTP Payload header (RFC2190)",
527             "H.263", "h263");
528         proto_register_field_array(proto_h263, hf, array_length(hf));
529         proto_register_subtree_array(ett, array_length(ett));
530         register_dissector("h263", dissect_h263, proto_h263);
531 }
532
533 void
534 proto_reg_handoff_h263(void)
535 {
536         dissector_handle_t h263_handle;
537
538         h263_handle = find_dissector("h263");
539         dissector_add("rtp.pt", PT_H263, h263_handle);
540         dissector_add("iax2.codec", AST_FORMAT_H263, h263_handle);
541 }