cb187cb191dea67aeecb265f7ecedf0316471a6f
[obnox/wireshark/wip.git] / epan / dissectors / packet-h263.c
1 /* packet-h263.c
2  *
3  * Routines for ITU-T Recommendation H.263 dissection
4  *
5  * Copyright 2003 Niklas \99gren <niklas.ogren@7l.se>
6  * Seven Levels Consultants AB
7  *
8  * $Id$
9  *
10  * Wireshark - Network traffic analyzer
11  * By Gerald Combs <gerald@wireshark.org>
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  * http://www.ietf.org/rfc/rfc4629.txt?number=4629
35  * and http://www.itu.int/rec/T-REC-H.263/en
36  */
37
38
39 #ifdef HAVE_CONFIG_H
40 # include "config.h"
41 #endif
42
43 #include <glib.h>
44 #include <epan/packet.h>
45
46 #include <stdio.h>
47 #include <string.h>
48
49 #include <epan/emem.h>
50 #include <epan/rtp_pt.h>
51 #include <epan/iax2_codec_type.h>
52
53 static void dissect_h263_data( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree );
54
55 /* H.263 header fields             */
56 static int proto_h263                   = -1;
57 static int proto_h263P                  = -1;
58 static int proto_h263_data              = -1;
59
60 /* Mode A header */
61 static int hf_h263_ftype = -1;
62 static int hf_h263_pbframes = -1;
63 static int hf_h263_sbit = -1;
64 static int hf_h263_ebit = -1;
65 static int hf_h263_srcformat = -1;
66 static int hf_h263_picture_coding_type = -1;    
67 static int hf_h263_unrestricted_motion_vector = -1;
68 static int hf_h263_syntax_based_arithmetic = -1;
69 static int hf_h263_advanced_prediction = -1;
70 static int hf_h263_r = -1;
71 static int hf_h263_rr = -1;
72 static int hf_h263_dbq = -1;
73 static int hf_h263_trb = -1;
74 static int hf_h263_tr = -1;
75 /* Additional fields for Mode B or C header */
76 static int hf_h263_quant = -1;
77 static int hf_h263_gobn = -1;
78 static int hf_h263_mba = -1;
79 static int hf_h263_hmv1 = -1;
80 static int hf_h263_vmv1 = -1;
81 static int hf_h263_hmv2 = -1;
82 static int hf_h263_vmv2 = -1;
83 /* Fields for the data section */
84 static int hf_h263_psc = -1;
85 static int hf_h263_gbsc = -1;
86 static int hf_h263_TR =-1;
87 static int hf_h263_split_screen_indicator = -1;
88 static int hf_h263_document_camera_indicator = -1;
89 static int hf_h263_full_picture_freeze_release = -1;
90 static int hf_h263_source_format = -1;
91 static int hf_h263_payload_picture_coding_type = -1;
92 static int hf_h263_opt_unres_motion_vector_mode = -1;
93 static int hf_h263_syntax_based_arithmetic_coding_mode = -1;
94 static int hf_h263_optional_advanced_prediction_mode = -1;
95 static int hf_h263_PB_frames_mode = -1;
96 static int hf_h263_data                 = -1;
97 static int hf_h263_payload              = -1;
98 static int hf_h263_GN                   = -1;
99 static int hf_h263_UFEP                 = -1;
100 static int hf_h263_opptype              = -1;
101 static int hf_h263_pquant               = -1;
102 static int hf_h263_cpm                  = -1;
103 static int hf_h263_psbi                 = -1;
104 static int hf_h263_picture_type_code = -1;
105 static int hf_h263_ext_source_format = -1;
106 static int hf_h263_custom_pcf   = -1;
107 static int hf_h263_pei                  = -1;
108 static int hf_h263_psupp                = -1;
109
110 /* H.263 RFC 4629 fields */
111 static int hf_h263P_payload = -1;
112 static int hf_h263P_rr = -1;
113 static int hf_h263P_pbit = -1;
114 static int hf_h263P_vbit = -1;
115 static int hf_h263P_plen = -1;
116 static int hf_h263P_pebit = -1;
117 static int hf_h263P_tid = -1;
118 static int hf_h263P_trun = -1;
119 static int hf_h263P_s = -1;
120 static int hf_h263P_extra_hdr = -1;
121 static int hf_h263P_PSC = -1;
122 static int hf_h263P_TR = -1;
123
124 /* Source format types */
125 #define H263_SRCFORMAT_FORB             0  /* forbidden */
126 #define H263_SRCFORMAT_SQCIF    1
127 #define H263_SRCFORMAT_QCIF             2
128 #define H263_SRCFORMAT_CIF              3
129 #define H263_SRCFORMAT_4CIF             4
130 #define H263_SRCFORMAT_16CIF    5
131 #define H263_PLUSPTYPE                  7
132
133 static const value_string srcformat_vals[] =
134 {
135   { H263_SRCFORMAT_FORB,                "forbidden" },
136   { H263_SRCFORMAT_SQCIF,               "sub-QCIF 128x96" },
137   { H263_SRCFORMAT_QCIF,                "QCIF 176x144" },
138   { H263_SRCFORMAT_CIF,                 "CIF 352x288" },
139   { H263_SRCFORMAT_4CIF,                "4CIF 704x576" },
140   { H263_SRCFORMAT_16CIF,               "16CIF 1408x1152" },
141   { 6,                                                  "Reserved",},
142   { H263_PLUSPTYPE,                             "extended PTYPE" },
143   { 0,          NULL },
144 };
145
146 /*
147  * If UFEP is "001", then the following bits are present in PLUSPTYPE:
148  *  Bits 1-3 Source Format, "000" reserved, "001" sub-QCIF, "010" QCIF, "011" CIF,
149  * "100" 4CIF, "101" 16CIF, "110" custom source format, "111" reserved;
150  */
151 static const value_string ext_srcformat_vals[] =
152 {
153   { 0,                                                  "reserved" },
154   { H263_SRCFORMAT_SQCIF,               "sub-QCIF 128x96" },
155   { H263_SRCFORMAT_QCIF,                "QCIF 176x144" },
156   { H263_SRCFORMAT_CIF,                 "CIF 352x288" },
157   { H263_SRCFORMAT_4CIF,                "4CIF 704x576" },
158   { H263_SRCFORMAT_16CIF,               "16CIF 1408x1152" },
159   { 6,                                                  "Custom source format",},
160   { 7,                                                  "Reserved" },
161   { 0,          NULL },
162 };
163
164 static const value_string h263_ufep_vals[] =
165 {
166   { 0,          "Only MPPTYPE included" },
167   { 1,          "All extended PTYPE fields are included" },
168   { 0,          NULL },
169 };
170
171 static const true_false_string on_off_flg = {
172   "On",
173   "Off"
174 };
175 static const true_false_string picture_coding_type_flg = {
176   "INTER (P-picture)",
177   "INTRA (I-picture)"
178 };
179
180 static const value_string picture_coding_type_vals[] =
181 {
182   { 0,          "I-Frame" },
183   { 1,          "P-frame" },
184   { 0,          NULL },
185 };
186
187 static const true_false_string PB_frames_mode_flg = {
188   "PB-frame",
189   "Normal I- or P-picture"
190 };
191
192 static const true_false_string cpm_flg = {
193   "On",
194   "Off"
195 };
196
197 static const true_false_string custom_pcf_flg = {
198   "Custom PCF",
199   "CIF PCF"
200 };
201
202 /*  Bits 1-3 Picture Type Code:*/
203 static const value_string picture_type_code_vals[] =
204 {
205   { 0,          "I-picture (INTRA)" },
206   { 1,          "P-picture (INTER)" },
207   { 2,          "Improved PB-frame (see Annex M)" },
208   { 3,          "B-picture (see Annex O)" },
209   { 4,          "EI-picture (see Annex O)" },
210   { 5,          "EP-picture (see Annex O)" },
211   { 6,          "Reserved" },
212   { 7,          "Reserved" },
213   { 0,          NULL },
214 };
215
216 /* H.263 fields defining a sub tree */
217 static gint ett_h263                    = -1;
218 static gint ett_h263_payload    = -1;
219 static gint ett_h263_optype             = -1;
220
221 /* H.263-1998 fields defining a sub tree */
222 static gint ett_h263P                   = -1;
223 static gint ett_h263P_extra_hdr = -1;
224 static gint ett_h263P_payload   = -1;
225 static gint ett_h263P_data = -1;
226
227 static void
228 dissect_h263( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree )
229 {
230         proto_item *ti                                  = NULL;
231         proto_tree *h263_tree                   = NULL;
232         unsigned int offset                             = 0;
233         unsigned int h263_version               = 0;
234         tvbuff_t *next_tvb;
235
236         h263_version = (tvb_get_guint8( tvb, offset ) & 0xc0 ) >> 6;
237
238         if ( check_col( pinfo->cinfo, COL_PROTOCOL ) )   {
239                 col_set_str( pinfo->cinfo, COL_PROTOCOL, "H.263 " );
240         }
241
242         if( h263_version == 0x00) {
243           if ( check_col( pinfo->cinfo, COL_INFO) ) {
244             col_append_str( pinfo->cinfo, COL_INFO, "MODE A ");
245           }
246         }
247         else if( h263_version == 0x02) {
248           if ( check_col( pinfo->cinfo, COL_INFO) ) {
249             col_append_str( pinfo->cinfo, COL_INFO, "MODE B ");
250           }
251         }
252         else if( h263_version == 0x03) {
253           if ( check_col( pinfo->cinfo, COL_INFO) ) {
254             col_append_str( pinfo->cinfo, COL_INFO, "MODE C ");
255           }
256         }
257
258         if ( tree ) {
259           ti = proto_tree_add_item( tree, proto_h263, tvb, offset, -1, FALSE );
260           h263_tree = proto_item_add_subtree( ti, ett_h263 );
261
262           /* FBIT 1st octet, 1 bit */
263           proto_tree_add_boolean( h263_tree, hf_h263_ftype, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x80 );
264           /* PBIT 1st octet, 1 bit */
265           proto_tree_add_boolean( h263_tree, hf_h263_pbframes, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x40 );
266           /* SBIT 1st octet, 3 bits */
267           proto_tree_add_uint( h263_tree, hf_h263_sbit, tvb, offset, 1, ( tvb_get_guint8( tvb, offset ) & 0x38 ) >> 3 );
268           /* EBIT 1st octet, 3 bits */
269           proto_tree_add_uint( h263_tree, hf_h263_ebit, tvb, offset, 1, tvb_get_guint8( tvb, offset )  & 0x7 );
270
271           offset++;
272
273           /* SRC 2nd octet, 3 bits */
274           proto_tree_add_uint( h263_tree, hf_h263_srcformat, tvb, offset, 1, tvb_get_guint8( tvb, offset ) >> 5 );
275
276           if(h263_version == 0x00) { /* MODE A */
277             /* I flag, 1 bit */
278             proto_tree_add_boolean( h263_tree, hf_h263_picture_coding_type, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x10 );
279             /* U flag, 1 bit */
280             proto_tree_add_boolean( h263_tree, hf_h263_unrestricted_motion_vector, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x08 );
281             /* S flag, 1 bit */
282             proto_tree_add_boolean( h263_tree, hf_h263_syntax_based_arithmetic, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x04 );
283             /* A flag, 1 bit */
284             proto_tree_add_boolean( h263_tree, hf_h263_advanced_prediction, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x02 );
285
286             /* Reserved 2nd octect, 1 bit + 3rd octect 3 bits */
287             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 ) );
288
289             offset++;
290
291             /* DBQ 3 octect, 2 bits */
292             proto_tree_add_uint( h263_tree, hf_h263_dbq, tvb, offset, 1, ( tvb_get_guint8( tvb, offset ) & 0x18 ) >> 3 );
293             /* TRB 3 octect, 3 bits */
294             proto_tree_add_uint( h263_tree, hf_h263_trb, tvb, offset, 1, ( tvb_get_guint8( tvb, offset ) & 0x07 ) );
295
296             offset++;
297             
298             /* TR 4 octect, 8 bits */
299             proto_tree_add_uint( h263_tree, hf_h263_tr, tvb, offset, 1, tvb_get_guint8( tvb, offset ) );
300             
301             offset++;
302
303           } else { /* MODE B or MODE C */
304
305             /* QUANT 2 octect, 5 bits */
306             proto_tree_add_uint( h263_tree, hf_h263_quant, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x1f );
307
308             offset++;
309
310             /* GOBN 3 octect, 5 bits */
311             proto_tree_add_uint( h263_tree, hf_h263_gobn, tvb, offset, 1, ( tvb_get_guint8( tvb, offset ) & 0xf8 ) >> 3);
312             /* MBA 3 octect, 3 bits + 4 octect 6 bits */
313             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 ) );
314             
315             offset++;
316
317             /* Reserved 4th octect, 2 bits */
318             proto_tree_add_uint( h263_tree, hf_h263_r, tvb, offset, 1, ( tvb_get_guint8( tvb, offset ) & 0x3 ) );
319
320             offset++;
321
322             /* I flag, 1 bit */
323             proto_tree_add_boolean( h263_tree, hf_h263_picture_coding_type, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x80 );
324             /* U flag, 1 bit */
325             proto_tree_add_boolean( h263_tree, hf_h263_unrestricted_motion_vector, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x40 );
326             /* S flag, 1 bit */
327             proto_tree_add_boolean( h263_tree, hf_h263_syntax_based_arithmetic, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x20 );
328             /* A flag, 1 bit */
329             proto_tree_add_boolean( h263_tree, hf_h263_advanced_prediction, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x10 );
330
331             /* HMV1 5th octect, 4 bits + 6th octect 3 bits*/
332             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) );
333
334             offset++;
335             
336             /* VMV1 6th octect, 5 bits + 7th octect 2 bits*/
337             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) );
338             
339             offset++;
340
341             /* HMV2 7th octect, 6 bits + 8th octect 1 bit*/
342             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) );
343             
344             offset++;
345
346             /* VMV2 8th octect, 7 bits*/
347             proto_tree_add_uint( h263_tree, hf_h263_vmv2, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x7f );
348                   
349             offset++;
350
351             if(h263_version == 0x03) { /* MODE C */
352               /* Reserved 9th to 11th octect, 8 + 8 + 3 bits */
353               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 ) );
354
355               offset+=2;
356
357               /* DBQ 11th octect, 2 bits */
358               proto_tree_add_uint( h263_tree, hf_h263_dbq, tvb, offset, 1, ( tvb_get_guint8( tvb, offset ) & 0x18 ) >>3 );
359               /* TRB 11th octect, 3 bits */
360               proto_tree_add_uint( h263_tree, hf_h263_trb, tvb, offset, 1, tvb_get_guint8( tvb, offset ) & 0x07 );
361               
362               offset++;
363               
364               /* TR 12th octect, 8 bits */
365               proto_tree_add_uint( h263_tree, hf_h263_tr, tvb, offset, 1, tvb_get_guint8( tvb, offset ) );
366               
367               offset++;
368             } /* end mode c */
369           } /* end not mode a */
370
371           /* The rest of the packet is the H.263 stream */
372           next_tvb = tvb_new_subset( tvb, offset, tvb_length(tvb) - offset, tvb_reported_length(tvb) - offset);
373           dissect_h263_data( next_tvb, pinfo, h263_tree );
374
375         }
376 }
377
378 #define cVALS(x) (const value_string*)(x)
379
380 static proto_item *
381 h263_proto_tree_add_bits(proto_tree *tree, int hf_index, tvbuff_t *tvb, gint bit_offset, gint no_of_bits, guint32 *return_value)
382 {
383         gint offset;
384         guint length;
385         guint bit_length;
386         char *str;
387         header_field_info *hf_field;
388         guint32 value = 0;
389         int bit;
390         guint32 mask = 0, tmp;
391         gboolean is_bytealigned = FALSE;
392         guint8 mask8    = 0xff;
393         guint16 mask16  = 0xffff;
394         guint32 mask24  = 0xffffff;
395         guint32 mask32  = 0xffffffff;
396         guint8 shift;
397         int i;
398
399         if((bit_offset&0x7)==0)
400                 is_bytealigned = TRUE;
401
402         hf_field = proto_registrar_get_nth(hf_index);
403
404         offset = bit_offset>>3;
405         bit_length = ((bit_offset&0x7)+no_of_bits);
406         length = bit_length >>3;
407         if((bit_length&0x7)!=0)
408                 length = length +1;
409
410         if (no_of_bits < 2){
411                 /* Single bit */
412                 mask8 = mask8 >>(bit_offset&0x7);
413                 value = tvb_get_guint8(tvb,offset) & mask8;
414                 mask = 0x80;
415                 shift = 8-((bit_offset + no_of_bits)&0x7);
416                 if (shift<8){
417                         value = value >> shift;
418                         mask = mask >> shift;
419                 }
420         }else if(no_of_bits < 9){
421                 /* One or 2 bytes */
422                 if(length == 1){
423                         /* Spans 1 byte */
424                         mask8 = mask8>>(bit_offset&0x7);
425                         value = tvb_get_guint8(tvb,offset)&mask8;
426                         mask = 0x80;
427                 }else{
428                         /* Spans 2 bytes */     
429                         mask16 = mask16>>(bit_offset&0x7);
430                         value = tvb_get_ntohs(tvb,offset) & mask16;
431                         mask = 0x8000;
432                 }
433                 shift = 8-((bit_offset + no_of_bits)&0x7);
434                 if (shift<8){
435                         value = value >> shift;
436                         mask = mask >> shift;
437                 }
438                 
439         }else if (no_of_bits < 17){
440                 /* 2 or 3 bytes */
441                 if(length == 2){
442                         /* Spans 2 bytes */
443                         mask16 = mask16>>(bit_offset&0x7);
444                         value = tvb_get_ntohs(tvb,offset) & mask16;
445                         mask = 0x8000;
446                 }else{
447                         /* Spans 3 bytes */     
448                         mask24 = mask24>>(bit_offset&0x7);
449                         value = tvb_get_ntoh24(tvb,offset) & mask24;
450                         mask = 0x800000;
451                 }
452                 shift = 8-((bit_offset + no_of_bits)&0x7);
453                 if (shift<8){
454                         value = value >> shift;
455                         mask = mask >> shift;
456                 }
457
458         }else if (no_of_bits < 25){
459                 /* 3 or 4 bytes */
460                 if(length == 3){
461                         /* Spans 3 bytes */
462                         mask24 = mask24>>(bit_offset&0x7);
463                         value = tvb_get_ntoh24(tvb,offset) & mask24;
464                         mask = 0x800000;
465                 }else{
466                         /* Spans 4 bytes */     
467                         mask32 = mask32>>(bit_offset&0x7);
468                         value = tvb_get_ntohl(tvb,offset) & mask32;
469                         mask = 0x80000000;
470                 }
471                 shift = 8-((bit_offset + no_of_bits)&0x7);
472                 if (shift<8){
473                         value = value >> shift;
474                         mask = mask >> shift;
475                 }
476
477         }else if (no_of_bits < 33){
478                 /* 4 or 5 bytes */
479                 if(length == 4){
480                         /* Spans 4 bytes */     
481                         mask32 = mask32>>(bit_offset&0x7);
482                         value = tvb_get_ntohl(tvb,offset) & mask32;
483                         mask = 0x80000000;
484                 }else{
485                         /* Spans 5 bytes
486                          * Does not handle unaligned bits over 24
487                          */
488                         DISSECTOR_ASSERT_NOT_REACHED();
489                 }
490                 shift = 8-((bit_offset + no_of_bits)&0x7);
491                 if (shift<8){
492                         value = value >> shift;
493                         mask = mask >> shift;
494                 }
495
496         }else{
497                 DISSECTOR_ASSERT_NOT_REACHED();
498         }
499
500         /* prepare the string */
501         str=ep_alloc(256);
502         str[0]='\0';
503         for(bit=0;bit<((int)(bit_offset&0x07));bit++){
504                 if(bit&&(!(bit%4))){
505                         strcat(str, " ");
506                 }
507                 strcat(str,".");
508                 mask = mask>>1;
509         }
510         /* read the bits for the int */
511         for(i=0;i<no_of_bits;i++){
512                 if(bit&&(!(bit%4))){
513                         strcat(str, " ");
514                 }
515                 if(bit&&(!(bit%8))){
516                         strcat(str, " ");
517                 }
518                 bit++;
519                 tmp = value & mask;
520                 if(tmp != 0){
521                         strcat(str, "1");
522                 } else {
523                         strcat(str, "0");
524                 }
525                 mask = mask>>1;
526         }
527         for(;bit%8;bit++){
528                 if(bit&&(!(bit%4))){
529                         strcat(str, " ");
530                 }
531                 strcat(str,".");
532         }
533
534         if(return_value){
535                 *return_value=value;
536         }
537         if(hf_index == -1)
538                 return NULL;
539
540         strcat(str," = ");
541         strcat(str,hf_field->name);
542
543         if (hf_field->type == FT_BOOLEAN){
544                 /* Boolean field */
545                 if (hf_field->strings) {
546                         const true_false_string         *tfstring = &tfs_true_false;
547                         tfstring = (const struct true_false_string*) hf_field->strings;
548
549                         return proto_tree_add_boolean_format(tree, hf_index, tvb, offset, length, value,
550                                 "%s: %s",
551                                 str,
552                                 value ? tfstring->true_string : tfstring->false_string);
553                 }else{
554                         return proto_tree_add_boolean_format(tree, hf_index, tvb, offset, length, value,
555                                 "%s: %u",
556                                 str,
557                                 value);
558                 }
559         }
560         /* 1 - 32 bits field */
561
562         if (hf_field->strings) {
563                 return proto_tree_add_uint_format(tree, hf_index, tvb, offset, length, value,
564                       "%s: %s",
565                                           str,
566                                           val_to_str(value, cVALS(hf_field->strings), "Unknown"));
567         }
568         switch(hf_field->display){
569         case BASE_DEC:
570                 return proto_tree_add_uint_format(tree, hf_index, tvb, offset, length, value,
571                          "%s: %u",
572                                           str,
573                                           value);
574                 break;
575         case BASE_HEX:
576                 return proto_tree_add_uint_format(tree, hf_index, tvb, offset, length, value,
577                          "%s: 0x%x",
578                                           str,
579                                           value);
580                 break;
581         default:
582                 DISSECTOR_ASSERT_NOT_REACHED();
583                 return NULL;
584                 ;
585         }
586
587 }
588
589 /*
590  * 5.3 Macroblock layer
591 static int
592 dissect_h263_macroblock_layer( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gint offset)
593 {
594
595 }
596  */
597
598
599 static int
600 dissect_h263_group_of_blocks_layer( tvbuff_t *tvb, proto_tree *tree, gint offset, gboolean is_rfc4626)
601 {
602
603         unsigned int offset_in_bits             = offset << 3;
604
605         if(is_rfc4626){
606                 /* GBSC 1xxx xxxx */
607                 h263_proto_tree_add_bits(tree, hf_h263_gbsc, tvb, offset_in_bits, 1, NULL);
608                 offset_in_bits++;
609         }else{
610                 /* Group of Block Start Code (GBSC) (17 bits) 
611                  * A word of 17 bits. Its value is 0000 0000 0000 0000 1.
612                  */
613                 h263_proto_tree_add_bits(tree, hf_h263_gbsc, tvb, offset_in_bits, 17, NULL);
614                 offset_in_bits = offset_in_bits +17;
615         }
616         /* 
617          * Group Number (GN) (5 bits)
618          */
619         h263_proto_tree_add_bits(tree, hf_h263_GN, tvb, offset_in_bits, 5, NULL);
620         offset_in_bits = offset_in_bits +5;
621         /* 5.2.4 GOB Sub-Bitstream Indicator (GSBI) (2 bits)
622          * A fixed length codeword of 2 bits that is only present if CPM is "1" in the picture header.
623          */
624         /* 
625          * 5.2.5 GOB Frame ID (GFID) (2 bits)
626          */
627         /*
628          * 5.2.6 Quantizer Information (GQUANT) (5 bits)
629          */
630         /*
631          * 5.3 Macroblock layer
632          */
633
634         return offset_in_bits>>3;
635 }
636
637
638 /*
639  * Length is used for the "Extra header" otherwise set to -1.
640  */
641 static int
642 dissect_h263_picture_layer( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gint offset, gint length _U_, gboolean is_rfc4626)
643 {
644         proto_tree *h263_opptype_tree   = NULL;
645         proto_item *opptype_item                = NULL;
646         unsigned int offset_in_bits             = offset << 3;
647         guint32 source_format;
648         guint32 ufep;
649         guint32 picture_coding_type;
650         guint32 PB_frames_mode = 0;
651         guint32 custom_pcf = 0;
652         guint32 picture_type_code =0;
653         guint32 cpm;
654         guint32 pei;
655
656         if(is_rfc4626){
657                 /* PC 1000 00xx */ 
658                 h263_proto_tree_add_bits(tree, hf_h263_psc, tvb, offset_in_bits, 6, NULL);
659                 offset_in_bits = offset_in_bits +6;
660
661         }else{
662         /* Check for PSC, PSC is a word of 22 bits. 
663          * Its value is 0000 0000 0000 0000' 1000 00xx xxxx xxxx.
664          */
665                 h263_proto_tree_add_bits(tree, hf_h263_psc, tvb, offset_in_bits, 22, NULL);
666                 offset_in_bits = offset_in_bits +22;
667
668         }
669         h263_proto_tree_add_bits(tree, hf_h263_TR, tvb, offset_in_bits, 8, NULL);
670         offset_in_bits = offset_in_bits +8;
671         /*
672          * Bit 1: Always "1", in order to avoid start code emulation. 
673          * Bit 2: Always "0", for distinction with Recommendation H.261.
674          */
675         offset_in_bits = offset_in_bits +2;
676         /* Bit 3: Split screen indicator, "0" off, "1" on. */
677         h263_proto_tree_add_bits( tree, hf_h263_split_screen_indicator, tvb, offset_in_bits, 1, NULL);
678         offset_in_bits++;
679         /* Bit 4: Document camera indicator, */
680         h263_proto_tree_add_bits( tree, hf_h263_document_camera_indicator, tvb, offset_in_bits, 1, NULL);
681         offset_in_bits++;
682         /* Bit 5: Full Picture Freeze Release, "0" off, "1" on. */
683         h263_proto_tree_add_bits( tree, hf_h263_full_picture_freeze_release, tvb, offset_in_bits, 1, NULL);
684         offset_in_bits++;
685         /* Bits 6-8: Source Format, "000" forbidden, "001" sub-QCIF, "010" QCIF, "011" CIF,
686          * "100" 4CIF, "101" 16CIF, "110" reserved, "111" extended PTYPE.
687          */
688         h263_proto_tree_add_bits( tree, hf_h263_source_format, tvb, offset_in_bits, 3 ,&source_format);
689         offset_in_bits = offset_in_bits +3;
690         if (source_format != H263_PLUSPTYPE){
691                 /* Not extended PTYPE */
692                 /* Bit 9: Picture Coding Type, "0" INTRA (I-picture), "1" INTER (P-picture). */
693                 h263_proto_tree_add_bits( tree, hf_h263_payload_picture_coding_type, tvb, offset_in_bits, 1, &picture_coding_type);
694                 if ( check_col( pinfo->cinfo, COL_INFO) )
695                         col_append_fstr(pinfo->cinfo, COL_INFO, val_to_str(picture_coding_type, picture_coding_type_vals, "Unknown (%u)"));
696                 offset_in_bits++;
697                 /* Bit 10: Optional Unrestricted Motion Vector mode (see Annex D), "0" off, "1" on. */
698                 h263_proto_tree_add_bits( tree, hf_h263_opt_unres_motion_vector_mode, tvb, offset_in_bits, 1, NULL);
699                 offset_in_bits++;
700                 /* Bit 11: Optional Syntax-based Arithmetic Coding mode (see Annex E), "0" off, "1" on.*/
701                 h263_proto_tree_add_bits( tree, hf_h263_syntax_based_arithmetic_coding_mode, tvb, offset_in_bits, 1, NULL);
702                 offset_in_bits++;
703                 /* Bit 12: Optional Advanced Prediction mode (see Annex F), "0" off, "1" on.*/
704                 h263_proto_tree_add_bits( tree, hf_h263_optional_advanced_prediction_mode, tvb, offset_in_bits, 1, NULL);
705                 offset_in_bits++;
706                 /* Bit 13: Optional PB-frames mode (see Annex G), "0" normal I- or P-picture, "1" PB-frame.*/
707                 h263_proto_tree_add_bits( tree, hf_h263_PB_frames_mode, tvb, offset_in_bits, 1, &PB_frames_mode);
708                 offset_in_bits++;
709         }else{
710                 /* Extended PTYPE 
711                  * Update Full Extended PTYPE (UFEP) (3 bits)
712                  */
713                 /* .... ..xx x... .... */
714                 h263_proto_tree_add_bits( tree, hf_h263_UFEP, tvb, offset_in_bits, 3, &ufep);
715                 offset_in_bits = offset_in_bits +3;
716                 if(ufep==1){
717                         /* The Optional Part of PLUSPTYPE (OPPTYPE) (18 bits) 
718                          */
719                          /*  .xxx xxxx  xxxx xxxx  xxx. .... */
720                         opptype_item = h263_proto_tree_add_bits( tree, hf_h263_opptype, tvb, offset_in_bits, 18, NULL);
721                         h263_opptype_tree = proto_item_add_subtree( opptype_item, ett_h263_optype );
722                         /*
723                          * If UFEP is "001", then the following bits are present in PLUSPTYPE:
724                          *  Bits 1-3 Source Format, "000" reserved, "001" sub-QCIF, "010" QCIF, "011" CIF,
725                          * "100" 4CIF, "101" 16CIF, "110" custom source format, "111" reserved;
726                          */
727                         h263_proto_tree_add_bits( h263_opptype_tree, hf_h263_ext_source_format, tvb, offset_in_bits, 3, NULL);
728                         offset_in_bits = offset_in_bits +3;
729                         
730                         /*
731                          *  Bit 4 Optional Custom PCF, "0" CIF PCF, "1" custom PCF;
732                          */
733                         h263_proto_tree_add_bits( h263_opptype_tree, hf_h263_custom_pcf, tvb, offset_in_bits, 3, &custom_pcf);
734                         offset_in_bits++;
735                         /*
736                          *  Bit 5 Optional Unrestricted Motion Vector (UMV) mode (see Annex D), "0" off, "1" on;
737                          */
738                         offset_in_bits++;
739                         /*
740                          *  Bit 6 Optional Syntax-based Arithmetic Coding (SAC) mode (see Annex E), "0" off, "1" on;
741                          */
742                         offset_in_bits++;
743                         /*
744                          *  Bit 7 Optional Advanced Prediction (AP) mode (see Annex F), "0" off, "1" on;
745                          */
746                         offset_in_bits++;
747                         /*
748                          *  Bit 8 Optional Advanced INTRA Coding (AIC) mode (see Annex I), "0" off, "1" on;
749                          */
750                         offset_in_bits++;
751                         /*
752                          *  Bit 9 Optional Deblocking Filter (DF) mode (see Annex J), "0" off, "1" on;
753                          */
754                         offset_in_bits++;
755                         /*
756                          *  Bit 10 Optional Slice Structured (SS) mode (see Annex K), "0" off, "1" on;
757                          */
758                         offset_in_bits++;
759                         /*
760                          *  Bit 11 Optional Reference Picture Selection (RPS) mode (see Annex N), "0" off, "1" on;
761                          */
762                         offset_in_bits++;
763                         /*
764                          *  Bit 12 Optional Independent Segment Decoding (ISD) mode (see Annex R), "0" off,"1" on;
765                          */
766                         offset_in_bits++;
767                         /*
768                          *  Bit 13 Optional Alternative INTER VLC (AIV) mode (see Annex S), "0" off, "1" on;
769                          */
770                         offset_in_bits++;
771                         /*
772                          *  Bit 14 Optional Modified Quantization (MQ) mode (see Annex T), "0" off, "1" on;
773                          */
774                         offset_in_bits++;
775                         /*
776                          *  Bit 15 Equal to "1" to prevent start code emulation;
777                          */
778                         offset_in_bits++;
779                         /*
780                          *  Bit 16 Reserved, shall be equal to "0";
781                          */
782                         offset_in_bits++;
783                         /*
784                          *  Bit 17 Reserved, shall be equal to "0";
785                          */
786                         offset_in_bits++;
787                         /*
788                          *  Bit 18 Reserved, shall be equal to "0".
789                          */
790                         offset_in_bits++;
791                 }
792                 /*
793                  * 5.1.4.3 The mandatory part of PLUSPTYPE when PLUSPTYPE present (MPPTYPE) (9 bits)
794                  * Regardless of the value of UFEP, the following 9 bits are also present in PLUSPTYPE:
795                  * - Bits 1-3 Picture Type Code:
796                  * "000" I-picture (INTRA);
797                  * "001" P-picture (INTER);
798                  * "010" Improved PB-frame (see Annex M);
799                  * "011" B-picture (see Annex O);
800                  * "100" EI-picture (see Annex O);
801                  * "101" EP-picture (see Annex O);
802                  * "110" Reserved;
803                  * "111" Reserved;
804                  */
805                 h263_proto_tree_add_bits( tree, hf_h263_picture_type_code, tvb, offset_in_bits, 3, &picture_type_code);
806                 offset_in_bits = offset_in_bits +3;
807                 /*
808                  *  Bit 4 Optional Reference Picture Resampling (RPR) mode (see Annex P), "0" off, "1" on;
809                  */
810                 offset_in_bits++;
811                 /*
812                  *  Bit 5 Optional Reduced-Resolution Update (RRU) mode (see Annex Q), "0" off, "1" on;
813                  */
814                 offset_in_bits++;
815                 /*
816                  *  Bit 6 Rounding Type (RTYPE) (see 6.1.2);
817                  */
818                 offset_in_bits++;
819                 /*
820                  *  Bit 7 Reserved, shall be equal to "0";
821                  */
822                 offset_in_bits++;
823                 /*
824                  *  Bit 8 Reserved, shall be equal to "0";
825                  */
826                 offset_in_bits++;
827                 /*
828                  *  Bit 9 Equal to "1" to prevent start code emulation.
829                  */
830                 offset_in_bits++;
831                 /* The picture header location of CPM (1 bit) and PSBI (2 bits)
832                  * the picture header depends on whether or not PLUSPTYPE is present 
833                  * (see 5.1.20 and 5.1.21). If PLUSPTYPE is present, then CPM follows
834                  * immediately after PLUSPTYPE in the picture header.
835                  */
836                 h263_proto_tree_add_bits( tree, hf_h263_cpm, tvb, offset_in_bits, 1, &cpm);
837                 offset_in_bits++;
838                 /* 5.1.21 Picture Sub-Bitstream Indicator (PSBI) (2 bits)
839                  * only present if Continuous Presence Multipoint and Video
840                  * Multiplex mode is indicated by CPM.
841                  */
842                 if(cpm==1){
843                         h263_proto_tree_add_bits( tree, hf_h263_psbi, tvb, offset_in_bits, 2, NULL);
844                         offset_in_bits = offset_in_bits +2;
845                 }
846                 /* TODO Add the rest of the fields */
847                 /* 5.1.5 Custom Picture Format (CPFMT) (23 bits)
848                  * present only if the use of a custom picture format is
849                  * signalled in PLUSPTYPE and UFEP is '001'. When present, CPFMT consists of:
850                  * Bits 1-4 Pixel Aspect Ratio Code: A 4-bit index to the PAR value in Table 5. For
851                  * extended PAR, the exact pixel aspect ratio shall be specified in EPAR
852                  * (see 5.1.6);
853                  * Bits 5-13 Picture Width Indication: Range [0, ... , 511]; Number of pixels per
854                  * line = (PWI + 1) * 4;
855                  * Bit 14 Equal to "1" to prevent start code emulation;
856                  * Bits 15-23 Picture Height Indication: Range [1, ... , 288]; Number of lines = PHI * 4.
857                  */
858                 /* 5.1.6 Extended Pixel Aspect Ratio (EPAR) (16 bits)
859                  * A fixed length codeword of 16 bits that is present only if CPFMT is present and extended PAR is
860                  * indicated therein. When present, EPAR consists of:
861                  *  Bits 1-8 PAR Width: "0" is forbidden. The natural binary representation of the PAR
862                  * width;
863                  *  Bits 9-16 PAR Height: "0" is forbidden. The natural binary representation of the PAR
864                  * height.
865                  */
866                 /* 5.1.7 Custom Picture Clock Frequency Code (CPCFC) (8 bits)
867                  * A fixed length codeword of 8 bits that is present only if PLUSPTYPE is present and UFEP is 001
868                  * and a custom picture clock frequency is signalled in PLUSPTYPE. When present, CPCFC consists of:
869                  * Bit 1 Clock Conversion Code: "0" indicates a clock conversion factor of 1000 and
870                  * "1" indicates 1001;
871                  * Bits 2-8 Clock Divisor: "0" is forbidden. The natural binary representation of the value
872                  * of the clock divisor.
873                  */
874                 /* 5.1.8 Extended Temporal Reference (ETR) (2 bits)
875                  * A fixed length codeword of 2 bits which is present only if a custom picture clock frequency is in
876                  * use (regardless of the value of UFEP). It is the two MSBs of the 10-bit number defined in 5.1.2.
877                  */
878                 /* 5.1.9 Unlimited Unrestricted Motion Vectors Indicator (UUI) (Variable length)
879                  * A variable length codeword of 1 or 2 bits that is present only if the optional Unrestricted Motion
880                  * Vector mode is indicated in PLUSPTYPE and UFEP is 001. When UUI is present it indicates the
881                  * effective limitation of the range of the motion vectors being used.
882                  *  UUI = "1" The motion vector range is limited according to Tables D.1 and D.2.
883                  *  UUI = "01" The motion vector range is not limited except by the picture size.
884                  */
885                 /*
886                  *  5.1.10 Slice Structured Submode bits (SSS) (2 bits)
887                  *  A fixed length codeword of 2 bits which is present only if the optional Slice Structured mode
888                  *  (see Annex K) is indicated in PLUSPTYPE and UFEP is 001. If the Slice Structured mode is in use
889                  *  but UFEP is not 001, the last values sent for SSS shall remain in effect.
890                  *  - Bit 1 Rectangular Slices, "0" indicates free-running slices, "1" indicates rectangular
891                  *  slices;
892                  *  - Bit 2 Arbitrary Slice Ordering, "0" indicates sequential order, "1" indicates arbitrary
893                  *  order.
894                  *  5.1.11 Enhancement Layer Number (ELNUM) (4 bits)
895                  *  A fixed length codeword of 4 bits which is present only if the optional Temporal, SNR, and Spatial
896                  *  Scalability mode is in use (regardless of the value of UFEP). The particular enhancement layer is
897                  *  identified by an enhancement layer number, ELNUM. Picture correspondence between layers is
898                  *  achieved via the temporal reference. Picture size is either indicated within each enhancement layer
899                  *  using the existing source format fields or is inferred by the relationship to the reference layer. The
900                  *  first enhancement layer above the base layer is designated as Enhancement Layer Number 2, and
901                  *  the base layer has number 1.
902                  *  5.1.12 Reference Layer Number (RLNUM) (4 bits)
903                  *  A fixed length codeword of 4 bits which is present only if the optional Temporal, SNR, and Spatial
904                  *  Scalability mode is in use (see Annex O) and UFEP is 001. The layer number for the pictures used
905                  *  as reference anchors is identified by a Reference Layer Number (RLNUM). Time correspondence
906                  *  between layers is achieved via the temporal reference.
907                  *  Note that for B-pictures in an enhancement layer having temporally surrounding EI- or EP-pictures
908                  *  which are present in the same enhancement layer, RLNUM shall be equal to ELNUM
909                  *  (see Annex O).
910                  *  5.1.13 Reference Picture Selection Mode Flags (RPSMF) (3 bits)
911                  *  A fixed length codeword of 3 bits that is present only if the Reference Picture Selection mode is in
912                  *  use and UFEP is 001. When present, RPSMF indicates which type of back-channel messages are
913                  *  needed by the encoder. If the Reference Picture Selection mode is in use but RPSMF is not present,
914                  *  the last value of RPSMF that was sent shall remain in effect.
915                  *  - 100: neither ACK nor NACK signals needed;
916                  *  - 101: need ACK signals to be returned;
917                  *  - 110: need NACK signals to be returned;
918                  *  - 111: need both ACK and NACK signals to be returned;
919                  *  - 000-011: Reserved.
920                  *  5.1.14 Temporal Reference for Prediction Indication (TRPI) (1 bit)
921                  *  A fixed length codeword of 1 bit that is present only if the optional Reference Picture Selection
922                  *  mode is in use (regardless of the value of UFEP). When present, TRPI indicates the presence of the
923                  *  following TRP field:
924                  *  - 0: TRP field is not present;
925                  *  - 1: TRP field is present.
926                  *  TRPI shall be 0 whenever the picture header indicates an I- or EI-picture.
927                  *  5.1.15 Temporal Reference for Prediction (TRP) (10 bits)
928                  *  When present (as indicated in TRPI), TRP indicates the Temporal Reference which is used for
929                  *  prediction of the encoding, except for in the case of B-pictures. For B-pictures, the picture having
930                  *  the temporal reference TRP is used for the prediction in the forward direction. (Prediction in the
931                  *  reverse-temporal direction always uses the immediately temporally subsequent picture.) TRP is a
932                  *  ten-bit number. If a custom picture clock frequency was not in use for the reference picture, the two
933                  *  MSBs of TRP are zero and the LSBs contain the eight-bit TR found in the picture header of the
934                  *  reference picture. If a custom picture clock frequency was in use for the reference picture, TRP is a
935                  *  ten-bit number consisting of the concatenation of ETR and TR from the reference picture header.
936                  *  When TRP is not present, the most recent temporally previous anchor picture shall be used for
937                  *  prediction, as when not in the Reference Picture Selection mode. TRP is valid until the next PSC,
938                  *  GSC, or SSC.
939                  *  5.1.16 Back-Channel message Indication (BCI) (Variable length)
940                  *  A variable length field of one or two bits that is present only if the optional Reference Picture
941                  *  Selection mode is in use. When set to "1", this signals the presence of the following optional video
942                  *  Back-Channel Message (BCM) field. "01" indicates the absence or the end of the video backchannel
943                  *  message field. Combinations of BCM and BCI may not be present, and may be repeated
944                  *  when present. BCI shall be set to "01" if the videomux submode of the optional Reference Picture
945                  *  Selection mode is not in use.
946                  *  5.1.17 Back-Channel Message (BCM) (Variable length)
947                  *  The Back-Channel message with syntax as specified in N.4.2, which is present only if the preceding
948                  *  BCI field is present and is set to "1".
949                  *  5.1.18 Reference Picture Resampling Parameters (RPRP) (Variable length)
950                  *  A variable length field that is present only if the optional Reference Picture Resampling mode bit is
951                  *  set in PLUSPTYPE. This field carries the parameters of the Reference Picture Resampling mode
952                  *  (see Annex P). Note that the Reference Picture Resampling mode can also be invoked implicitly by
953                  *  the occurrence of a picture header for an INTER coded picture having a picture size which differs
954                  *  from that of the previous encoded picture, in which case the RPRP field is not present and the
955                  *  Reference Picture Resampling mode bit is not set.
956                  */
957         }
958         /* 5.1.19 Quantizer Information (PQUANT) (5 bits) */
959         h263_proto_tree_add_bits( tree, hf_h263_pquant, tvb, offset_in_bits, 5, NULL);
960         offset_in_bits = offset_in_bits +5;
961         if (source_format != H263_PLUSPTYPE){
962                 h263_proto_tree_add_bits( tree, hf_h263_cpm, tvb, offset_in_bits, 1, &cpm);
963                 offset_in_bits++;
964                 /* 5.1.21 Picture Sub-Bitstream Indicator (PSBI) (2 bits)
965                  * only present if Continuous Presence Multipoint and Video
966                  * Multiplex mode is indicated by CPM.
967                  */
968                 if(cpm==1){
969                         h263_proto_tree_add_bits( tree, hf_h263_psbi, tvb, offset_in_bits, 2, NULL);
970                         offset_in_bits = offset_in_bits +2;
971                 }
972         }
973         /* 5.1.22 Temporal Reference for B-pictures in PB-frames (TRB) (3/5 bits)
974          * TRB is present if PTYPE or PLUSPTYPE indicates "PB-frame" or "Improved PB-frame"
975          * It is 3 bits long for standard CIF picture clock frequency and is
976          * extended to 5 bits when a custom picture clock frequency is in use.
977          */
978         if((PB_frames_mode == 1)||(picture_type_code == 2 )){
979                 if(custom_pcf == 0){
980                         h263_proto_tree_add_bits( tree, hf_h263_trb, tvb, offset_in_bits, 3, NULL);
981                         offset_in_bits = offset_in_bits +3;
982                 }else{
983                         h263_proto_tree_add_bits( tree, hf_h263_trb, tvb, offset_in_bits, 5, NULL);
984                         offset_in_bits = offset_in_bits +5;
985                 }
986         }
987         /* 5.1.23 Quantization information for B-pictures in PB-frames (DBQUANT) (2 bits)
988          * DBQUANT is present if PTYPE or PLUSPTYPE indicates "PB-frame" or "Improved PB-frame"
989          */
990         if((PB_frames_mode == 1)||(picture_type_code == 2 )){
991                 offset_in_bits = offset_in_bits +2;
992         }
993         /* 5.1.24 Extra Insertion Information (PEI) (1 bit)
994          * A bit which when set to "1" signals the presence of the following optional data field.
995          */
996         h263_proto_tree_add_bits( tree, hf_h263_pei, tvb, offset_in_bits, 1, &pei);
997         while(pei==1)
998         {
999                 /*5.1.25 Supplemental Enhancement Information (PSUPP) (0/8/16 ... bits) 
1000                  * If PEI is set to "1", then 9 bits follow consisting of 8 bits of data (PSUPP) and then another PEI bit
1001                  * to indicate if a further 9 bits follow and so on. Encoders shall use PSUPP as specified in Annex L.
1002                  */
1003                 h263_proto_tree_add_bits( tree, hf_h263_psupp, tvb, offset_in_bits, 8, NULL);
1004                 offset_in_bits = offset_in_bits +8;
1005                 h263_proto_tree_add_bits( tree, hf_h263_pei, tvb, offset_in_bits, 1, &pei);
1006
1007         }
1008         /* For the first GOB in each picture (with number 0), no GOB header shall be transmitted.
1009          * For all other GOBs, the GOB header may be empty, depending on the encoder strategy.
1010          */
1011
1012         /*
1013          * 5.3 Macroblock layer
1014          * dissect_h263_macroblock_layer( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gint offset)
1015          */
1016
1017         return offset_in_bits>>3;
1018
1019 }
1020
1021 /* RFC 4629 */
1022 static void
1023 dissect_h263P( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree )
1024 {
1025         proto_item *ti                                  = NULL;
1026         proto_item *data_item                   = NULL;
1027         proto_item *extra_hdr_item              = NULL;
1028         proto_tree *h263P_tree                  = NULL;
1029         proto_tree *h263P_extr_hdr_tree = NULL;
1030         proto_tree *h263P_data_tree             = NULL;
1031         unsigned int offset                             = 0;
1032         unsigned int start_offset               = 0;
1033         guint16 data16, plen;
1034         guint8 startcode;
1035
1036         /*
1037         tvbuff_t *next_tvb;
1038         */
1039
1040         if ( check_col( pinfo->cinfo, COL_PROTOCOL ) )   {
1041                 col_set_str( pinfo->cinfo, COL_PROTOCOL, "H.263 RFC4629 " );
1042         }
1043
1044         if ( tree ) {
1045           ti = proto_tree_add_item( tree, proto_h263P, tvb, offset, -1, FALSE );
1046           h263P_tree = proto_item_add_subtree( ti, ett_h263P );
1047           /* Add it as hidden to make a filter of h263 possible here as well */
1048           proto_tree_add_item_hidden( tree, proto_h263, tvb, offset, -1, FALSE );
1049
1050           data16 = tvb_get_ntohs(tvb,offset);
1051           proto_tree_add_item( h263P_tree, hf_h263P_rr, tvb, offset, 2, FALSE );
1052           proto_tree_add_item( h263P_tree, hf_h263P_pbit, tvb, offset, 2, FALSE );
1053           proto_tree_add_item( h263P_tree, hf_h263P_vbit, tvb, offset, 2, FALSE );
1054           proto_tree_add_item( h263P_tree, hf_h263P_plen, tvb, offset, 2, FALSE );
1055           proto_tree_add_item( h263P_tree, hf_h263P_pebit, tvb, offset, 2, FALSE );
1056           offset = offset +2;
1057           /*
1058            *   V: 1 bit
1059            *
1060            *      Indicates the presence of an 8-bit field containing information
1061            *      for Video Redundancy Coding (VRC), which follows immediately after
1062            *      the initial 16 bits of the payload header, if present.  For syntax
1063            *      and semantics of that 8-bit VRC field, see Section 5.2.
1064            */
1065
1066           if ((data16&0x0200)==0x0200){
1067                   /* V bit = 1 
1068                    *   The format of the VRC header extension is as follows:
1069                    *
1070                    *         0 1 2 3 4 5 6 7
1071                    *        +-+-+-+-+-+-+-+-+
1072                    *        | TID | Trun  |S|
1073                    *        +-+-+-+-+-+-+-+-+
1074                    *
1075                    *   TID: 3 bits
1076                    *
1077                    *   Thread ID.  Up to 7 threads are allowed.  Each frame of H.263+ VRC
1078                    *   data will use as reference information only sync frames or frames
1079                    *   within the same thread.  By convention, thread 0 is expected to be
1080                    *   the "canonical" thread, which is the thread from which the sync frame
1081                    *   should ideally be used.  In the case of corruption or loss of the
1082                    *   thread 0 representation, a representation of the sync frame with a
1083                    *   higher thread number can be used by the decoder.  Lower thread
1084                    *   numbers are expected to contain representations of the sync frames
1085                    *   equal to or better than higher thread numbers in the absence of data
1086                    *   corruption or loss.  See [Vredun] for a detailed discussion of VRC.
1087                    *
1088                    *   Trun: 4 bits
1089                    *
1090                    *   Monotonically increasing (modulo 16) 4-bit number counting the packet
1091                    *   number within each thread.
1092                    *
1093                    *   S: 1 bit
1094                    *
1095                    *   A bit that indicates that the packet content is for a sync frame.  
1096                    *   :
1097                    */
1098                   proto_tree_add_item( h263P_tree, hf_h263P_tid, tvb, offset, 1, FALSE );
1099                   proto_tree_add_item( h263P_tree, hf_h263P_trun, tvb, offset, 1, FALSE );
1100                   proto_tree_add_item( h263P_tree, hf_h263P_s, tvb, offset, 1, FALSE );
1101                   offset++;
1102           }
1103
1104           /* Length, in bytes, of the extra picture header. */
1105           plen = (data16 & 0x01f8) >> 3;
1106           if (plen != 0){
1107                   start_offset = offset;
1108                   extra_hdr_item = proto_tree_add_item( h263P_tree, hf_h263P_extra_hdr, tvb, offset, plen, FALSE );
1109                   h263P_extr_hdr_tree = proto_item_add_subtree( extra_hdr_item, ett_h263P_extra_hdr );
1110                   
1111                   dissect_h263_picture_layer( tvb, pinfo, h263P_extr_hdr_tree, offset, plen, TRUE);
1112
1113                   offset = start_offset + plen;         
1114           }
1115           if ((data16&0x0400)!=0){
1116                   /* P bit = 1 */
1117                   data_item = proto_tree_add_item( h263P_tree, hf_h263P_payload, tvb, offset, -1, FALSE );
1118                   h263P_data_tree = proto_item_add_subtree( data_item, ett_h263P_data );
1119                   /* Startc code holds bit 17 -23 of the codeword */
1120                   startcode = tvb_get_guint8(tvb,offset)&0xfe;
1121                   if (startcode & 0x80){
1122                           /* All picture, slice, and EOSBS start codes
1123                            * shall be byte aligned, and GOB and EOS start codes may be byte aligned.
1124                            */
1125                           switch(startcode){
1126                           case 0xf8:
1127                                   /* End Of Sub-Bitstream code (EOSBS) 
1128                                    * EOSBS codes shall be byte aligned
1129                                    * ( 1111 100. )
1130                                    */
1131                                   break;
1132                           case 0x80:
1133                           case 0x82:
1134                                   /* Picture Start Code (PSC)
1135                                    * ( 1000 00x.)
1136                                    */
1137                                   if(check_col( pinfo->cinfo, COL_INFO))
1138                                           col_append_str( pinfo->cinfo, COL_INFO, "(PSC) ");
1139                                   offset = dissect_h263_picture_layer( tvb, pinfo, h263P_data_tree, offset, -1, TRUE);
1140                                   break;
1141                           case 0xfc:
1142                           case 0xfe:
1143                                   /* End Of Sequence (EOS)
1144                                    * ( 1111 11x. )
1145                                    */
1146                           default:
1147                                   /* Group of Block Start Code (GBSC) or
1148                                    * Slice Start Code (SSC)
1149                                    */
1150                                   if ( check_col( pinfo->cinfo, COL_INFO) )
1151                                           col_append_str( pinfo->cinfo, COL_INFO, "(GBSC) ");
1152                                   dissect_h263_group_of_blocks_layer( tvb, h263P_data_tree, offset,TRUE);
1153                                   break;
1154                           }
1155                   }else{
1156                           /* Error */
1157                   }
1158                   return;
1159           }
1160           proto_tree_add_item( h263P_tree, hf_h263P_payload, tvb, offset, -1, FALSE );
1161         }
1162 }
1163 /*
1164         5.1.1 Picture Start Code (PSC) (22 bits)
1165         PSC is a word of 22 bits. Its value is 0000 0000 0000 0000 1 00000. All picture start codes shall be
1166         byte aligned.
1167         ( 1000 00xx)
1168
1169         End Of Sequence (EOS) (22 bits)
1170         A codeword of 22 bits. Its value is 0000 0000 0000 0000 1 11111.
1171         ( 1111 11xx )
1172
1173         Group of Block Start Code (GBSC) (17 bits)
1174         A word of 17 bits. Its value is 0000 0000 0000 0000 1.
1175         ( 1xxx xxxx )
1176
1177         End Of Sub-Bitstream code (EOSBS) (23 bits)
1178         The EOSBS code is a codeword of 23 bits. Its value is 0000 0000 0000 0000 1 11110 0.
1179         ( 1111 100x )
1180
1181         Slice Start Code (SSC) (17 bits)
1182         A word of 17 bits. Its value is 0000 0000 0000 0000 1.
1183         ( 1xxx xxxx )
1184   */
1185 static void dissect_h263_data( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree )
1186 {
1187         guint offset = 0;
1188         proto_item *h263_payload_item   = NULL;
1189         proto_tree *h263_payload_tree   = NULL;
1190         guint32 data;
1191         guint8 startcode;
1192
1193         if ( check_col( pinfo->cinfo, COL_INFO) ) {
1194           col_append_str( pinfo->cinfo, COL_INFO, "H263 payload ");
1195         }
1196
1197         if( tree ) {
1198           h263_payload_item = proto_tree_add_item( tree, hf_h263_payload, tvb, offset, -1, FALSE );
1199           h263_payload_tree = proto_item_add_subtree( h263_payload_item, ett_h263_payload );
1200         }
1201
1202         /* Check for PSC, PSC is a word of 22 bits. Its value is 0000 0000 0000 0000' 1000 00xx xxxx xxxx. */
1203         data = tvb_get_ntohl(tvb, offset);
1204         
1205         if (( data & 0xffff8000) == 0x00008000 ) { 
1206                 /* Start Code found
1207                  *
1208                  * Startc code holds bit 17 -23 of the codeword
1209                  */
1210                 startcode = tvb_get_guint8(tvb,offset+2)&0xfe;
1211                 if (startcode & 0x80){
1212                         switch(startcode){
1213                         case 0xf8:
1214                                 /* End Of Sub-Bitstream code (EOSBS) 
1215                                  * ( 1111 100. )
1216                                  */
1217                                 break;
1218                         case 0x80:
1219                         case 0x82:
1220                                 /* Picture Start Code (PSC)
1221                                  * ( 1000 00x.)
1222                                  */
1223                                 if(check_col( pinfo->cinfo, COL_INFO))
1224                                         col_append_str( pinfo->cinfo, COL_INFO, "(PSC) ");
1225                                 offset = dissect_h263_picture_layer( tvb, pinfo, h263_payload_tree, offset, -1, FALSE);
1226                                 break;
1227                         case 0xfc:
1228                         case 0xfe:
1229                                 /* End Of Sequence (EOS)
1230                                  * ( 1111 11x. )
1231                                  */
1232                         default:
1233                                 /* Group of Block Start Code (GBSC) or
1234                                  * Slice Start Code (SSC)
1235                                  */
1236                                 if ( check_col( pinfo->cinfo, COL_INFO) )
1237                                         col_append_str( pinfo->cinfo, COL_INFO, "(GBSC) ");
1238                                 offset = dissect_h263_group_of_blocks_layer( tvb, h263_payload_tree, offset,FALSE);
1239                                 break;
1240                         }
1241                 }else{
1242                         /* Error */
1243                 }
1244         }
1245         if( tree )
1246                 proto_tree_add_item( h263_payload_tree, hf_h263_data, tvb, offset, -1, FALSE );
1247 }
1248
1249 void
1250 proto_register_h263(void)
1251 {
1252         static hf_register_info hf[] =
1253         {
1254                 {
1255                         &hf_h263_ftype,
1256                         {
1257                                 "F",
1258                                 "h263.ftype",
1259                                 FT_BOOLEAN,
1260                                 BASE_NONE,
1261                                 NULL,
1262                                 0x0,
1263                                 "Indicates the mode of the payload header (MODE A or B/C)", HFILL
1264                         }
1265                 },
1266                 {
1267                         &hf_h263_pbframes,
1268                         {
1269                                 "p/b frame",
1270                                 "h263.pbframes",
1271                                 FT_BOOLEAN,
1272                                 BASE_NONE,
1273                                 NULL,
1274                                 0x0,
1275                                 "Optional PB-frames mode as defined by H.263 (MODE C)", HFILL
1276                         }
1277                 },
1278                 {
1279                         &hf_h263_sbit,
1280                         {
1281                                 "Start bit position",
1282                                 "h263.sbit",
1283                                 FT_UINT8,
1284                                 BASE_DEC,
1285                                 NULL,
1286                                 0x0,
1287                                 "Start bit position specifies number of most significant bits that shall be ignored in the first data byte.", HFILL
1288                         }
1289                 },
1290                 {
1291                         &hf_h263_ebit,
1292                         {
1293                                 "End bit position",
1294                                 "h263.ebit",
1295                                 FT_UINT8,
1296                                 BASE_DEC,
1297                                 NULL,
1298                                 0x0,
1299                                 "End bit position specifies number of least significant bits that shall be ignored in the last data byte.", HFILL
1300                         }
1301                 },
1302                 {
1303                         &hf_h263_srcformat,
1304                         {
1305                                 "SRC format",
1306                                 "h263.srcformat",
1307                                 FT_UINT8,
1308                                 BASE_DEC,
1309                                 VALS(srcformat_vals),
1310                                 0x0,
1311                                 "Source format specifies the resolution of the current picture.", HFILL
1312                         }
1313                 },
1314                 {
1315                         &hf_h263_picture_coding_type,
1316                         {
1317                                 "Inter-coded frame",
1318                                 "h263.picture_coding_type",
1319                                 FT_BOOLEAN,
1320                                 BASE_NONE,
1321                                 NULL,
1322                                 0x0,
1323                                 "Picture coding type, intra-coded (false) or inter-coded (true)", HFILL
1324                         }
1325                 },
1326                 {
1327                         &hf_h263_unrestricted_motion_vector,
1328                         {
1329                                 "Motion vector",
1330                                 "h263.unrestricted_motion_vector",
1331                                 FT_BOOLEAN,
1332                                 BASE_NONE,
1333                                 NULL,
1334                                 0x0,
1335                                 "Unrestricted Motion Vector option for current picture", HFILL
1336                         }
1337                 },
1338                 {
1339                         &hf_h263_syntax_based_arithmetic,
1340                         {
1341                                 "Syntax-based arithmetic coding",
1342                                 "h263.syntax_based_arithmetic",
1343                                 FT_BOOLEAN,
1344                                 BASE_NONE,
1345                                 NULL,
1346                                 0x0,
1347                                 "Syntax-based Arithmetic Coding option for current picture", HFILL
1348                         }
1349                 },
1350                 {
1351                         &hf_h263_advanced_prediction,
1352                         {
1353                                 "Advanced prediction option",
1354                                 "h263.advanced_prediction",
1355                                 FT_BOOLEAN,
1356                                 BASE_NONE,
1357                                 NULL,
1358                                 0x0,
1359                                 "Advanced Prediction option for current picture", HFILL
1360                         }
1361                 },
1362                 {
1363                         &hf_h263_dbq,
1364                         {
1365                                 "Differential quantization parameter",
1366                                 "h263.dbq",
1367                                 FT_UINT8,
1368                                 BASE_DEC,
1369                                 NULL,
1370                                 0x0,
1371                                 "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
1372                         }
1373                 },
1374                 {
1375                         &hf_h263_trb,
1376                         {
1377                                 "Temporal Reference for B frames",
1378                                 "h263.trb",
1379                                 FT_UINT8,
1380                                 BASE_DEC,
1381                                 NULL,
1382                                 0x0,
1383                                 "Temporal Reference for the B frame as defined by H.263", HFILL
1384                         }
1385                 },
1386                 {
1387                         &hf_h263_tr,
1388                         {
1389                                 "Temporal Reference for P frames",
1390                                 "h263.tr",
1391                                 FT_UINT8,
1392                                 BASE_DEC,
1393                                 NULL,
1394                                 0x0,
1395                                 "Temporal Reference for the P frame as defined by H.263", HFILL
1396                         }
1397                 },
1398                 {
1399                         &hf_h263_quant,
1400                         {
1401                                 "Quantizer",
1402                                 "h263.quant",
1403                                 FT_UINT8,
1404                                 BASE_DEC,
1405                                 NULL,
1406                                 0x0,
1407                                 "Quantization value for the first MB coded at the starting of the packet.", HFILL
1408                         }
1409                 },
1410                 {
1411                         &hf_h263_gobn,
1412                         {
1413                                 "GOB Number",
1414                                 "h263.gobn",
1415                                 FT_UINT8,
1416                                 BASE_DEC,
1417                                 NULL,
1418                                 0x0,
1419                                 "GOB number in effect at the start of the packet.", HFILL
1420                         }
1421                 },
1422                 {
1423                         &hf_h263_mba,
1424                         {
1425                                 "Macroblock address",
1426                                 "h263.mba",
1427                                 FT_UINT16,
1428                                 BASE_DEC,
1429                                 NULL,
1430                                 0x0,
1431                                 "The address within the GOB of the first MB in the packet, counting from zero in scan order.", HFILL
1432                         }
1433                 },
1434                 {
1435                         &hf_h263_hmv1,
1436                         {
1437                                 "Horizontal motion vector 1",
1438                                 "h263.hmv1",
1439                                 FT_UINT8,
1440                                 BASE_DEC,
1441                                 NULL,
1442                                 0x0,
1443                                 "Horizontal motion vector predictor for the first MB in this packet ", HFILL
1444                         }
1445                 },
1446                 {
1447                         &hf_h263_vmv1,
1448                         {
1449                                 "Vertical motion vector 1",
1450                                 "h263.vmv1",
1451                                 FT_UINT8,
1452                                 BASE_DEC,
1453                                 NULL,
1454                                 0x0,
1455                                 "Vertical motion vector predictor for the first MB in this packet ", HFILL
1456                         }
1457                 },
1458                 {
1459                         &hf_h263_hmv2,
1460                         {
1461                                 "Horizontal motion vector 2",
1462                                 "h263.hmv2",
1463                                 FT_UINT8,
1464                                 BASE_DEC,
1465                                 NULL,
1466                                 0x0,
1467                                 "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
1468                         }
1469                 },
1470                 {
1471                         &hf_h263_vmv2,
1472                         {
1473                                 "Vertical motion vector 2",
1474                                 "h263.vmv2",
1475                                 FT_UINT8,
1476                                 BASE_DEC,
1477                                 NULL,
1478                                 0x0,
1479                                 "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
1480                         }
1481                 },
1482                 {
1483                         &hf_h263_r,
1484                         {
1485                                 "Reserved field",
1486                                 "h263.r",
1487                                 FT_UINT8,
1488                                 BASE_DEC,
1489                                 NULL,
1490                                 0x0,
1491                                 "Reserved field that houls contain zeroes", HFILL
1492                         }
1493                 },
1494                 {
1495                         &hf_h263_rr,
1496                         {
1497                                 "Reserved field 2",
1498                                 "h263.rr",
1499                                 FT_UINT16,
1500                                 BASE_DEC,
1501                                 NULL,
1502                                 0x0,
1503                                 "Reserved field that should contain zeroes", HFILL
1504                         }
1505                 },
1506                 {
1507                         &hf_h263_payload,
1508                         {
1509                                 "H.263 payload",
1510                                 "h263.payload",
1511                                 FT_NONE,
1512                                 BASE_NONE,
1513                                 NULL,
1514                                 0x0,
1515                                 "The actual H.263 data", HFILL
1516                         }
1517                 },
1518                 {
1519                         &hf_h263_data,
1520                         {
1521                                 "H.263 stream",
1522                                 "h263.stream",
1523                                 FT_BYTES,
1524                                 BASE_NONE,
1525                                 NULL,
1526                                 0x0,
1527                                 "The H.263 stream including its Picture, GOB or Macro block start code.", HFILL
1528                         }
1529                 },
1530                 {
1531                         &hf_h263_psc,
1532                         {
1533                                 "H.263 Picture start Code",
1534                                 "h263.psc",
1535                                 FT_UINT32,
1536                                 BASE_HEX,
1537                                 NULL,
1538                                 0xfffffc00,
1539                                 "Picture start Code, PSC", HFILL
1540                         }
1541                 },
1542                 { &hf_h263_gbsc,
1543                         {
1544                                 "H.263 Group of Block Start Code",
1545                                 "h263.gbsc",
1546                                 FT_UINT32,
1547                                 BASE_HEX,
1548                                 NULL,
1549                                 0xffff8000,
1550                                 "Group of Block Start Code", HFILL
1551                         }
1552                 },
1553                 {
1554                         &hf_h263_TR,
1555                         {
1556                                 "H.263 Temporal Reference",
1557                                 "h263.tr2",
1558                                 FT_UINT32,
1559                                 BASE_DEC,
1560                                 NULL,
1561                                 0x000003fc,
1562                                 "Temporal Reference, TR", HFILL
1563                         }
1564                 },
1565                 {
1566                         &hf_h263_split_screen_indicator,
1567                         {
1568                                 "H.263 Split screen indicator",
1569                                 "h263.split_screen_indicator",
1570                                 FT_BOOLEAN,
1571                                 8,
1572                                 TFS(&on_off_flg),
1573                                 0x80,
1574                                 "Split screen indicator", HFILL
1575                         }
1576                 },
1577                 {
1578                         &hf_h263_document_camera_indicator,
1579                         {
1580                                 "H.263 Document camera indicator",
1581                                 "h263.document_camera_indicator",
1582                                 FT_BOOLEAN,
1583                                 8,
1584                                 TFS(&on_off_flg),
1585                                 0x40,
1586                                 "Document camera indicator", HFILL
1587                         }
1588                 },
1589                 {
1590                         &hf_h263_full_picture_freeze_release,
1591                         {
1592                                 "H.263 Full Picture Freeze Release",
1593                                 "h263.split_screen_indicator",
1594                                 FT_BOOLEAN,
1595                                 8,
1596                                 TFS(&on_off_flg),
1597                                 0x20,
1598                                 "Full Picture Freeze Release", HFILL
1599                         }
1600                 },
1601                 {
1602                         &hf_h263_source_format,
1603                         {
1604                                 "H.263 Source Format",
1605                                 "h263.source_format",
1606                                 FT_UINT8,
1607                                 BASE_HEX,
1608                                 VALS(srcformat_vals),
1609                                 0x1c,
1610                                 "Source Format", HFILL
1611                         }
1612                 },
1613                 {
1614                         &hf_h263_ext_source_format,
1615                         {
1616                                 "H.263 Source Format",
1617                                 "h263.ext_source_format",
1618                                 FT_UINT8,
1619                                 BASE_HEX,
1620                                 VALS(ext_srcformat_vals),
1621                                 0x0,
1622                                 "Source Format", HFILL
1623                         }
1624                 },
1625                 {
1626                         &hf_h263_UFEP,
1627                         {
1628                                 "H.263 Update Full Extended PTYPE",
1629                                 "h263.ufep",
1630                                 FT_UINT16,
1631                                 BASE_DEC,
1632                                 VALS(h263_ufep_vals),
1633                                 0x0380,
1634                                 "Update Full Extended PTYPE", HFILL
1635                         }
1636                 },
1637                 {
1638                         &hf_h263_opptype,
1639                         {
1640                                 "H.263 Optional Part of PLUSPTYPE",
1641                                 "h263.opptype",
1642                                 FT_UINT24,
1643                                 BASE_DEC,
1644                                 NULL,
1645                                 0x7fffe0,
1646                                 "Optional Part of PLUSPTYPE", HFILL
1647                         }
1648                 },
1649                 {
1650                         &hf_h263_payload_picture_coding_type,
1651                         {
1652                                 "H.263 Picture Coding Type",
1653                                 "h263.picture_coding_type",
1654                                 FT_BOOLEAN,
1655                                 8,
1656                                 TFS(&picture_coding_type_flg),
1657                                 0x02,
1658                                 "Picture Coding Typet", HFILL
1659                         }
1660                 },
1661                 {
1662                         &hf_h263_opt_unres_motion_vector_mode,
1663                         {
1664                                 "H.263 Optional Unrestricted Motion Vector mode",
1665                                 "h263.opt_unres_motion_vector_mode",
1666                                 FT_BOOLEAN,
1667                                 8,
1668                                 TFS(&on_off_flg),
1669                                 0x01,
1670                                 "Optional Unrestricted Motion Vector mode", HFILL
1671                         }
1672                 },
1673                 {
1674                         &hf_h263_syntax_based_arithmetic_coding_mode,
1675                         {
1676                                 "H.263 Optional Syntax-based Arithmetic Coding mode",
1677                                 "h263.syntax_based_arithmetic_coding_mode",
1678                                 FT_BOOLEAN,
1679                                 8,
1680                                 TFS(&on_off_flg),
1681                                 0x80,
1682                                 "Optional Syntax-based Arithmetic Coding mode", HFILL
1683                         }
1684                 },
1685                 {
1686                         &hf_h263_optional_advanced_prediction_mode,
1687                         {
1688                                 "H.263 Optional Advanced Prediction mode",
1689                                 "h263.optional_advanced_prediction_mode",
1690                                 FT_BOOLEAN,
1691                                 8,
1692                                 TFS(&on_off_flg),
1693                                 0x40,
1694                                 "Optional Advanced Prediction mode", HFILL
1695                         }
1696                 },
1697                 {
1698                         &hf_h263_PB_frames_mode,
1699                         {
1700                                 "H.263 Optional PB-frames mode",
1701                                 "h263.PB_frames_mode",
1702                                 FT_BOOLEAN,
1703                                 8,
1704                                 TFS(&PB_frames_mode_flg),
1705                                 0x20,
1706                                 "Optional PB-frames mode", HFILL
1707                         }
1708                 },
1709                 {
1710                         &hf_h263_GN,
1711                         {
1712                                 "H.263 Group Number",
1713                                 "h263.gn",
1714                                 FT_UINT32,
1715                                 BASE_DEC,
1716                                 NULL,
1717                                 0x00007c00,
1718                                 "Group Number, GN", HFILL
1719                         }
1720                 },
1721                 {
1722                         &hf_h263_pquant,
1723                         {
1724                                 "H.263 Quantizer Information (PQUANT)",
1725                                 "h263.pquant",
1726                                 FT_UINT32,
1727                                 BASE_DEC,
1728                                 NULL,
1729                                 0x0,
1730                                 "Quantizer Information (PQUANT)", HFILL
1731                         }
1732                 },
1733                 {
1734                         &hf_h263_cpm,
1735                         {
1736                                 "H.263 Continuous Presence Multipoint and Video Multiplex (CPM)",
1737                                 "h263.cpm",
1738                                 FT_BOOLEAN,
1739                                 8,
1740                                 TFS(&cpm_flg),
1741                                 0x0,
1742                                 "Continuous Presence Multipoint and Video Multiplex (CPM)", HFILL
1743                         }
1744                 },
1745                 {
1746                         &hf_h263_psbi,
1747                         {
1748                                 "H.263 Picture Sub-Bitstream Indicator (PSBI)",
1749                                 "h263.psi",
1750                                 FT_UINT32,
1751                                 BASE_DEC,
1752                                 NULL,
1753                                 0x0,
1754                                 "Picture Sub-Bitstream Indicator (PSBI)", HFILL
1755                         }
1756                 },
1757                 {
1758                         &hf_h263_picture_type_code,
1759                         {
1760                                 "H.263 Picture Type Code",
1761                                 "h263.psi",
1762                                 FT_UINT32,
1763                                 BASE_DEC,
1764                                 VALS(picture_type_code_vals),
1765                                 0x0,
1766                                 "Picture Type Code", HFILL
1767                         }
1768                 },
1769                 {
1770                         &hf_h263_custom_pcf,
1771                         {
1772                                 "H.263 Custom PCF",
1773                                 "h263.custom_pcf",
1774                                 FT_BOOLEAN,
1775                                 8,
1776                                 TFS(&custom_pcf_flg),
1777                                 0x0,
1778                                 "Custom PCF", HFILL
1779                         }
1780                 },
1781                 {
1782                         &hf_h263_pei,
1783                         {
1784                                 "H.263 Extra Insertion Information (PEI)",
1785                                 "h263.pei",
1786                                 FT_BOOLEAN,
1787                                 8,
1788                                 NULL,
1789                                 0x0,
1790                                 "Extra Insertion Information (PEI)", HFILL
1791                         }
1792                 },
1793                 {
1794                         &hf_h263_psupp,
1795                         {
1796                                 "H.263 Supplemental Enhancement Information (PSUPP)",
1797                                 "h263.psupp",
1798                                 FT_UINT32,
1799                                 BASE_DEC,
1800                                 NULL,
1801                                 0x0,
1802                                 "Supplemental Enhancement Information (PSUPP)", HFILL
1803                         }
1804                 },
1805 };
1806
1807         static gint *ett[] =
1808         {
1809                 &ett_h263,
1810                 &ett_h263_payload,
1811                 &ett_h263_optype,
1812         };
1813
1814
1815         proto_h263 = proto_register_protocol("ITU-T Recommendation H.263 RTP Payload header (RFC2190)",
1816             "H.263", "h263");
1817         proto_h263_data = proto_register_protocol("ITU-T Recommendation H.263",
1818             "H.263 data", "h263data");
1819         proto_register_field_array(proto_h263, hf, array_length(hf));
1820         proto_register_subtree_array(ett, array_length(ett));
1821         register_dissector("h263", dissect_h263, proto_h263);
1822         register_dissector("h263data", dissect_h263_data, proto_h263_data);
1823 }
1824
1825 void
1826 proto_register_h263P(void)
1827 {
1828         static hf_register_info hf[] =
1829         {
1830                 {
1831                         &hf_h263P_payload,
1832                         {
1833                                 "H.263 RFC4629 payload",
1834                                 "h263P.payload",
1835                                 FT_NONE,
1836                                 BASE_NONE,
1837                                 NULL,
1838                                 0x0,
1839                                 "The actual H.263 RFC4629 data", HFILL
1840                         }
1841                 },
1842                 {
1843                         &hf_h263P_rr,
1844                         {
1845                                 "Reserved",
1846                                 "h263P.rr",
1847                                 FT_UINT16,
1848                                 BASE_DEC,
1849                                 NULL,
1850                                 0xf800,
1851                                 "Reserved SHALL be zero", HFILL
1852                         }
1853                 },
1854                 {
1855                         &hf_h263P_pbit,
1856                         {
1857                                 "P",
1858                                 "h263P.p",
1859                                 FT_BOOLEAN,
1860                                 16,
1861                                 NULL,
1862                                 0x0400,
1863                                 "Indicates (GOB/Slice) start or (EOS or EOSBS)", HFILL
1864                         }
1865                 },
1866                 {
1867                         &hf_h263P_vbit,
1868                         {
1869                                 "V",
1870                                 "h263P.v",
1871                                 FT_BOOLEAN,
1872                                 16,
1873                                 NULL,
1874                                 0x0200,
1875                                 "presence of Video Redundancy Coding (VRC) field", HFILL
1876                         }
1877                 },
1878                 {
1879                         &hf_h263P_plen,
1880                         {
1881                                 "PLEN",
1882                                 "h263P.plen",
1883                                 FT_UINT16,
1884                                 BASE_DEC,
1885                                 NULL,
1886                                 0x01f8,
1887                                 "Length, in bytes, of the extra picture header", HFILL
1888                         }
1889                 },
1890                 {
1891                         &hf_h263P_pebit,
1892                         {
1893                                 "PEBIT",
1894                                 "h263P.pebit",
1895                                 FT_UINT16,
1896                                 BASE_DEC,
1897                                 NULL,
1898                                 0x0003,
1899                                 "number of bits that shall be ignored in the last byte of the picture header", HFILL
1900                         }
1901                 },
1902
1903
1904                 {
1905                         &hf_h263P_tid,
1906                         {
1907                                 "Thread ID",
1908                                 "h263P.tid",
1909                                 FT_UINT8,
1910                                 BASE_DEC,
1911                                 NULL,
1912                                 0xe0,
1913                                 "Thread ID", HFILL
1914                         }
1915                 },
1916                 {
1917                         &hf_h263P_trun,
1918                         {
1919                                 "Trun",
1920                                 "h263P.trun",
1921                                 FT_UINT8,
1922                                 BASE_DEC,
1923                                 NULL,
1924                                 0x1e,
1925                                 "Monotonically increasing (modulo 16) 4-bit number counting the packet number within each thread", HFILL
1926                         }
1927                 },
1928                 {
1929                         &hf_h263P_s,
1930                         {
1931                                 "S",
1932                                 "h263P.s",
1933                                 FT_UINT8,
1934                                 BASE_DEC,
1935                                 NULL,
1936                                 0x01,
1937                                 "Indicates that the packet content is for a sync frame", HFILL
1938                         }
1939                 },
1940                 {
1941                         &hf_h263P_extra_hdr,
1942                         {
1943                                 "Extra picture header",
1944                                 "h263P.extra_hdr",
1945                                 FT_BYTES,
1946                                 BASE_NONE,
1947                                 NULL,
1948                                 0x0,
1949                                 "Extra picture header", HFILL
1950                         }
1951                 },
1952                 {
1953                         &hf_h263P_PSC,
1954                         {
1955                                 "H.263 PSC",
1956                                 "h263P.PSC",
1957                                 FT_UINT16,
1958                                 BASE_HEX,
1959                                 NULL,
1960                                 0xfc00,
1961                                 "Picture Start Code(PSC)", HFILL
1962                         }
1963                 },
1964                 {
1965                         &hf_h263P_TR,
1966                         {
1967                                 "H.263 Temporal Reference",
1968                                 "h263P.tr",
1969                                 FT_UINT16,
1970                                 BASE_HEX,
1971                                 NULL,
1972                                 0x03fc,
1973                                 "Temporal Reference, TR", HFILL
1974                         }
1975                 },
1976
1977         };
1978
1979         static gint *ett[] =
1980         {
1981                 &ett_h263P,
1982                 &ett_h263P_extra_hdr,
1983                 &ett_h263P_payload,
1984                 &ett_h263P_data,
1985         };
1986
1987
1988         proto_h263P = proto_register_protocol("ITU-T Recommendation H.263 RTP Payload header (RFC4629)",
1989             "H263P", "h263p");
1990
1991         proto_register_field_array(proto_h263P, hf, array_length(hf));
1992         proto_register_subtree_array(ett, array_length(ett));
1993
1994         register_dissector("h263P", dissect_h263P, proto_h263P);
1995
1996 }
1997
1998 void
1999 proto_reg_handoff_h263(void)
2000 {
2001         dissector_handle_t h263_handle;
2002
2003         h263_handle = find_dissector("h263");
2004         dissector_add("rtp.pt", PT_H263, h263_handle);
2005         dissector_add("iax2.codec", AST_FORMAT_H263, h263_handle);
2006 }
2007
2008 void
2009 proto_reg_handoff_h263P(void)
2010 {
2011         dissector_handle_t h263P_handle;
2012
2013         h263P_handle = find_dissector("h263P");
2014         dissector_add_string("rtp_dyn_payload_type","H263-1998", h263P_handle);
2015         dissector_add_string("rtp_dyn_payload_type","H263-2000", h263P_handle);
2016 }