* Prefer col_append_str instead of col_append_fstr for constant strings
[metze/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 Ogren <niklas.ogren@7l.se>
6  * Seven Levels Consultants AB
7  *
8  * Copyright 2008 Richard van der Hoff, MX Telecom
9  * <richardv@mxtelecom.com>
10  *
11  * $Id$
12  *
13  * Wireshark - Network traffic analyzer
14  * By Gerald Combs <gerald@wireshark.org>
15  * Copyright 1998 Gerald Combs
16  *
17  * Copied structure from packet-h261.c
18  *
19  * This program is free software; you can redistribute it and/or
20  * modify it under the terms of the GNU General Public License
21  * as published by the Free Software Foundation; either version 2
22  * of the License, or (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program; if not, write to the Free Software
31  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
32  */
33
34
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <glib.h>
40 #include <epan/packet.h>
41
42 #include <stdio.h>
43 #include <string.h>
44
45 #include "packet-h263.h"
46
47 static int proto_h263_data              = -1;
48
49 /* Fields for the data section */
50 static int hf_h263_psc = -1;
51 static int hf_h263_gbsc = -1;
52 static int hf_h263_TR =-1;
53 static int hf_h263_split_screen_indicator = -1;
54 static int hf_h263_document_camera_indicator = -1;
55 static int hf_h263_full_picture_freeze_release = -1;
56 static int hf_h263_source_format = -1;
57 static int hf_h263_payload_picture_coding_type = -1;
58 static int hf_h263_opt_unres_motion_vector_mode = -1;
59 static int hf_h263_syntax_based_arithmetic_coding_mode = -1;
60 static int hf_h263_optional_advanced_prediction_mode = -1;
61 static int hf_h263_PB_frames_mode = -1;
62 static int hf_h263_data                 = -1;
63 static int hf_h263_GN                   = -1;
64 static int hf_h263_UFEP                 = -1;
65 static int hf_h263_opptype              = -1;
66 static int hf_h263_pquant               = -1;
67 static int hf_h263_cpm                  = -1;
68 static int hf_h263_psbi                 = -1;
69 static int hf_h263_picture_type_code = -1;
70 static int hf_h263_ext_source_format = -1;
71 static int hf_h263_custom_pcf   = -1;
72 static int hf_h263_pei                  = -1;
73 static int hf_h263_psupp                = -1;
74 static int hf_h263_trb = -1;
75 static int hf_h263_not_dissected = -1;
76
77 /* H.263 fields defining a sub tree */
78 static gint ett_h263_payload    = -1;
79 static gint ett_h263_optype             = -1;
80
81
82 /* Source format types */
83 #define H263_SRCFORMAT_FORB             0  /* forbidden */
84 #define H263_SRCFORMAT_SQCIF    1
85 #define H263_SRCFORMAT_QCIF             2
86 #define H263_SRCFORMAT_CIF              3
87 #define H263_SRCFORMAT_4CIF             4
88 #define H263_SRCFORMAT_16CIF    5
89 #define H263_PLUSPTYPE                  7
90
91 const value_string h263_srcformat_vals[] =
92 {
93   { H263_SRCFORMAT_FORB,                "forbidden" },
94   { H263_SRCFORMAT_SQCIF,               "sub-QCIF 128x96" },
95   { H263_SRCFORMAT_QCIF,                "QCIF 176x144" },
96   { H263_SRCFORMAT_CIF,                 "CIF 352x288" },
97   { H263_SRCFORMAT_4CIF,                "4CIF 704x576" },
98   { H263_SRCFORMAT_16CIF,               "16CIF 1408x1152" },
99   { 6,                                                  "Reserved",},
100   { H263_PLUSPTYPE,                             "extended PTYPE" },
101   { 0,          NULL },
102 };
103
104 /*
105  * If UFEP is "001", then the following bits are present in PLUSPTYPE:
106  *  Bits 1-3 Source Format, "000" reserved, "001" sub-QCIF, "010" QCIF, "011" CIF,
107  * "100" 4CIF, "101" 16CIF, "110" custom source format, "111" reserved;
108  */
109 static const value_string ext_srcformat_vals[] =
110 {
111   { 0,                                                  "reserved" },
112   { H263_SRCFORMAT_SQCIF,               "sub-QCIF 128x96" },
113   { H263_SRCFORMAT_QCIF,                "QCIF 176x144" },
114   { H263_SRCFORMAT_CIF,                 "CIF 352x288" },
115   { H263_SRCFORMAT_4CIF,                "4CIF 704x576" },
116   { H263_SRCFORMAT_16CIF,               "16CIF 1408x1152" },
117   { 6,                                                  "Custom source format",},
118   { 7,                                                  "Reserved" },
119   { 0,          NULL },
120 };
121
122 static const value_string h263_ufep_vals[] =
123 {
124   { 0,          "Only MPPTYPE included" },
125   { 1,          "All extended PTYPE fields are included" },
126   { 0,          NULL },
127 };
128
129 static const true_false_string on_off_flg = {
130   "On",
131   "Off"
132 };
133 static const true_false_string picture_coding_type_flg = {
134   "INTER (P-picture)",
135   "INTRA (I-picture)"
136 };
137
138 static const value_string picture_coding_type_vals[] =
139 {
140   { 0,          "I-Frame" },
141   { 1,          "P-frame" },
142   { 0,          NULL },
143 };
144
145 static const true_false_string PB_frames_mode_flg = {
146   "PB-frame",
147   "Normal I- or P-picture"
148 };
149
150 static const true_false_string cpm_flg = {
151   "On",
152   "Off"
153 };
154
155 static const true_false_string custom_pcf_flg = {
156   "Custom PCF",
157   "CIF PCF"
158 };
159
160 /*  Bits 1-3 Picture Type Code:*/
161 static const value_string picture_type_code_vals[] =
162 {
163   { 0,          "I-picture (INTRA)" },
164   { 1,          "P-picture (INTER)" },
165   { 2,          "Improved PB-frame (see Annex M)" },
166   { 3,          "B-picture (see Annex O)" },
167   { 4,          "EI-picture (see Annex O)" },
168   { 5,          "EP-picture (see Annex O)" },
169   { 6,          "Reserved" },
170   { 7,          "Reserved" },
171   { 0,          NULL },
172 };
173
174
175 /*
176  * 5.3 Macroblock layer
177 static int
178 dissect_h263_macroblock_layer( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gint offset)
179 {
180
181 }
182  */
183
184
185 int
186 dissect_h263_group_of_blocks_layer( tvbuff_t *tvb, proto_tree *tree, gint offset, gboolean is_rfc4626)
187 {
188
189         unsigned int offset_in_bits             = offset << 3;
190
191         if(is_rfc4626){
192                 /* GBSC 1xxx xxxx */
193                 proto_tree_add_bits_item(tree, hf_h263_gbsc, tvb, offset_in_bits, 1, FALSE);
194                 offset_in_bits++;
195         }else{
196                 /* Group of Block Start Code (GBSC) (17 bits) 
197                  * A word of 17 bits. Its value is 0000 0000 0000 0000 1.
198                  */
199                 proto_tree_add_bits_item(tree, hf_h263_gbsc, tvb, offset_in_bits, 17, FALSE);
200                 offset_in_bits = offset_in_bits +17;
201         }
202         /* 
203          * Group Number (GN) (5 bits)
204          */
205         proto_tree_add_bits_item(tree, hf_h263_GN, tvb, offset_in_bits, 5, FALSE);
206         offset_in_bits = offset_in_bits +5;
207         /* 5.2.4 GOB Sub-Bitstream Indicator (GSBI) (2 bits)
208          * A fixed length codeword of 2 bits that is only present if CPM is "1" in the picture header.
209          */
210         /* 
211          * 5.2.5 GOB Frame ID (GFID) (2 bits)
212          */
213         /*
214          * 5.2.6 Quantizer Information (GQUANT) (5 bits)
215          */
216         /*
217          * 5.3 Macroblock layer
218          */
219
220         return offset_in_bits>>3;
221 }
222
223
224 /*
225  * Length is used for the "Extra header" otherwise set to -1.
226  */
227 int
228 dissect_h263_picture_layer( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gint offset, gint length _U_, gboolean is_rfc4626)
229 {
230         proto_tree *h263_opptype_tree   = NULL;
231         proto_item *opptype_item                = NULL;
232         unsigned int offset_in_bits             = offset << 3;
233         unsigned int saved_bit_offset;
234         guint64 source_format;
235         guint64 ufep;
236         guint64 picture_coding_type;
237         guint64 PB_frames_mode = 0;
238         guint64 custom_pcf = 0;
239         guint64 picture_type_code =0;
240         guint64 cpm;
241         guint64 pei;
242
243         if(is_rfc4626){
244                 /* PC 1000 00xx */ 
245                 proto_tree_add_bits_item(tree, hf_h263_psc, tvb, offset_in_bits, 6, FALSE);
246                 offset_in_bits = offset_in_bits +6;
247
248         }else{
249         /* Check for PSC, PSC is a word of 22 bits. 
250          * Its value is 0000 0000 0000 0000' 1000 00xx xxxx xxxx.
251          */
252                 proto_tree_add_bits_item(tree, hf_h263_psc, tvb, offset_in_bits, 22, FALSE);
253                 offset_in_bits = offset_in_bits +22;
254
255         }
256         proto_tree_add_bits_item(tree, hf_h263_TR, tvb, offset_in_bits, 8, FALSE);
257         offset_in_bits = offset_in_bits +8;
258         /*
259          * Bit 1: Always "1", in order to avoid start code emulation. 
260          * Bit 2: Always "0", for distinction with Recommendation H.261.
261          */
262         offset_in_bits = offset_in_bits +2;
263         /* Bit 3: Split screen indicator, "0" off, "1" on. */
264         proto_tree_add_bits_item( tree, hf_h263_split_screen_indicator, tvb, offset_in_bits, 1, FALSE);
265         offset_in_bits++;
266         /* Bit 4: Document camera indicator, */
267         proto_tree_add_bits_item( tree, hf_h263_document_camera_indicator, tvb, offset_in_bits, 1, FALSE);
268         offset_in_bits++;
269         /* Bit 5: Full Picture Freeze Release, "0" off, "1" on. */
270         proto_tree_add_bits_item( tree, hf_h263_full_picture_freeze_release, tvb, offset_in_bits, 1, FALSE);
271         offset_in_bits++;
272         /* Bits 6-8: Source Format, "000" forbidden, "001" sub-QCIF, "010" QCIF, "011" CIF,
273          * "100" 4CIF, "101" 16CIF, "110" reserved, "111" extended PTYPE.
274          */
275         proto_tree_add_bits_ret_val( tree, hf_h263_source_format, tvb, offset_in_bits, 3 ,&source_format, FALSE);
276         offset_in_bits = offset_in_bits +3;
277         if (source_format != H263_PLUSPTYPE){
278                 /* Not extended PTYPE */
279                 /* Bit 9: Picture Coding Type, "0" INTRA (I-picture), "1" INTER (P-picture). */
280                 proto_tree_add_bits_ret_val( tree, hf_h263_payload_picture_coding_type, tvb, offset_in_bits, 1, &picture_coding_type, FALSE);
281                 if ( check_col( pinfo->cinfo, COL_INFO) )
282                         col_append_str(pinfo->cinfo, COL_INFO, val_to_str((guint32)picture_coding_type, picture_coding_type_vals, "Unknown (%u)"));
283                 offset_in_bits++;
284                 /* Bit 10: Optional Unrestricted Motion Vector mode (see Annex D), "0" off, "1" on. */
285                 proto_tree_add_bits_item( tree, hf_h263_opt_unres_motion_vector_mode, tvb, offset_in_bits, 1, FALSE);
286                 offset_in_bits++;
287                 /* Bit 11: Optional Syntax-based Arithmetic Coding mode (see Annex E), "0" off, "1" on.*/
288                 proto_tree_add_bits_item( tree, hf_h263_syntax_based_arithmetic_coding_mode, tvb, offset_in_bits, 1, FALSE);
289                 offset_in_bits++;
290                 /* Bit 12: Optional Advanced Prediction mode (see Annex F), "0" off, "1" on.*/
291                 proto_tree_add_bits_item( tree, hf_h263_optional_advanced_prediction_mode, tvb, offset_in_bits, 1, FALSE);
292                 offset_in_bits++;
293                 /* Bit 13: Optional PB-frames mode (see Annex G), "0" normal I- or P-picture, "1" PB-frame.*/
294                 proto_tree_add_bits_ret_val( tree, hf_h263_PB_frames_mode, tvb, offset_in_bits, 1, &PB_frames_mode, FALSE);
295                 offset_in_bits++;
296         }else{
297                 /* Extended PTYPE 
298                  * Update Full Extended PTYPE (UFEP) (3 bits)
299                  */
300                 /* .... ..xx x... .... */
301                 proto_tree_add_bits_ret_val( tree, hf_h263_UFEP, tvb, offset_in_bits, 3, &ufep, FALSE);
302                 offset_in_bits = offset_in_bits +3;
303                 if(ufep==1){
304                         /* The Optional Part of PLUSPTYPE (OPPTYPE) (18 bits) 
305                          */
306                          /*  .xxx xxxx  xxxx xxxx  xxx. .... */
307                         opptype_item = proto_tree_add_bits_item( tree, hf_h263_opptype, tvb, offset_in_bits, 18, FALSE);
308                         h263_opptype_tree = proto_item_add_subtree( opptype_item, ett_h263_optype );
309                         /*
310                          * If UFEP is "001", then the following bits are present in PLUSPTYPE:
311                          *  Bits 1-3 Source Format, "000" reserved, "001" sub-QCIF, "010" QCIF, "011" CIF,
312                          * "100" 4CIF, "101" 16CIF, "110" custom source format, "111" reserved;
313                          */
314                         proto_tree_add_bits_item( h263_opptype_tree, hf_h263_ext_source_format, tvb, offset_in_bits, 3, FALSE);
315                         offset_in_bits+=3;
316                         
317                         /*
318                          *  Bit 4 Optional Custom PCF, "0" CIF PCF, "1" custom PCF;
319                          */
320                         proto_tree_add_bits_ret_val( h263_opptype_tree, hf_h263_custom_pcf, tvb, offset_in_bits, 1, &custom_pcf, FALSE);
321                         offset_in_bits++;
322                         saved_bit_offset=offset_in_bits;
323                         /*
324                          *  Bit 5 Optional Unrestricted Motion Vector (UMV) mode (see Annex D), "0" off, "1" on;
325                          */
326                         offset_in_bits++;
327                         /*
328                          *  Bit 6 Optional Syntax-based Arithmetic Coding (SAC) mode (see Annex E), "0" off, "1" on;
329                          */
330                         offset_in_bits++;
331                         /*
332                          *  Bit 7 Optional Advanced Prediction (AP) mode (see Annex F), "0" off, "1" on;
333                          */
334                         offset_in_bits++;
335                         /*
336                          *  Bit 8 Optional Advanced INTRA Coding (AIC) mode (see Annex I), "0" off, "1" on;
337                          */
338                         offset_in_bits++;
339                         /*
340                          *  Bit 9 Optional Deblocking Filter (DF) mode (see Annex J), "0" off, "1" on;
341                          */
342                         offset_in_bits++;
343                         /*
344                          *  Bit 10 Optional Slice Structured (SS) mode (see Annex K), "0" off, "1" on;
345                          */
346                         offset_in_bits++;
347                         /*
348                          *  Bit 11 Optional Reference Picture Selection (RPS) mode (see Annex N), "0" off, "1" on;
349                          */
350                         offset_in_bits++;
351                         /*
352                          *  Bit 12 Optional Independent Segment Decoding (ISD) mode (see Annex R), "0" off,"1" on;
353                          */
354                         offset_in_bits++;
355                         /*
356                          *  Bit 13 Optional Alternative INTER VLC (AIV) mode (see Annex S), "0" off, "1" on;
357                          */
358                         offset_in_bits++;
359                         /*
360                          *  Bit 14 Optional Modified Quantization (MQ) mode (see Annex T), "0" off, "1" on;
361                          */
362                         offset_in_bits++;
363                         /*
364                          *  Bit 15 Equal to "1" to prevent start code emulation;
365                          */
366                         offset_in_bits++;
367                         /*
368                          *  Bit 16 Reserved, shall be equal to "0";
369                          */
370                         offset_in_bits++;
371                         /*
372                          *  Bit 17 Reserved, shall be equal to "0";
373                          */
374                         offset_in_bits++;
375                         /*
376                          *  Bit 18 Reserved, shall be equal to "0".
377                          */
378                         offset_in_bits++;
379                         proto_tree_add_bits_item( h263_opptype_tree, hf_h263_not_dissected, tvb, saved_bit_offset, offset_in_bits-saved_bit_offset, FALSE);
380                         
381                 }
382                 /*
383                  * 5.1.4.3 The mandatory part of PLUSPTYPE when PLUSPTYPE present (MPPTYPE) (9 bits)
384                  * Regardless of the value of UFEP, the following 9 bits are also present in PLUSPTYPE:
385                  * - Bits 1-3 Picture Type Code:
386                  * "000" I-picture (INTRA);
387                  * "001" P-picture (INTER);
388                  * "010" Improved PB-frame (see Annex M);
389                  * "011" B-picture (see Annex O);
390                  * "100" EI-picture (see Annex O);
391                  * "101" EP-picture (see Annex O);
392                  * "110" Reserved;
393                  * "111" Reserved;
394                  */
395                 proto_tree_add_bits_ret_val( tree, hf_h263_picture_type_code, tvb, offset_in_bits, 3, &picture_type_code, FALSE);
396                 offset_in_bits+=3;
397                 saved_bit_offset=offset_in_bits;
398                 /*
399                  *  Bit 4 Optional Reference Picture Resampling (RPR) mode (see Annex P), "0" off, "1" on;
400                  */
401                 offset_in_bits++;
402                 /*
403                  *  Bit 5 Optional Reduced-Resolution Update (RRU) mode (see Annex Q), "0" off, "1" on;
404                  */
405                 offset_in_bits++;
406                 /*
407                  *  Bit 6 Rounding Type (RTYPE) (see 6.1.2);
408                  */
409                 offset_in_bits++;
410                 /*
411                  *  Bit 7 Reserved, shall be equal to "0";
412                  */
413                 offset_in_bits++;
414                 /*
415                  *  Bit 8 Reserved, shall be equal to "0";
416                  */
417                 offset_in_bits++;
418                 /*
419                  *  Bit 9 Equal to "1" to prevent start code emulation.
420                  */
421                 offset_in_bits++;
422                 proto_tree_add_bits_item( tree, hf_h263_not_dissected, tvb, saved_bit_offset, offset_in_bits-saved_bit_offset, FALSE);
423                 /* The picture header location of CPM (1 bit) and PSBI (2 bits)
424                  * the picture header depends on whether or not PLUSPTYPE is present 
425                  * (see 5.1.20 and 5.1.21). If PLUSPTYPE is present, then CPM follows
426                  * immediately after PLUSPTYPE in the picture header.
427                  */
428                 proto_tree_add_bits_ret_val( tree, hf_h263_cpm, tvb, offset_in_bits, 1, &cpm, FALSE);
429                 offset_in_bits++;
430                 /* 5.1.21 Picture Sub-Bitstream Indicator (PSBI) (2 bits)
431                  * only present if Continuous Presence Multipoint and Video
432                  * Multiplex mode is indicated by CPM.
433                  */
434                 if(cpm==1){
435                         proto_tree_add_bits_item( tree, hf_h263_psbi, tvb, offset_in_bits, 2, FALSE);
436                         offset_in_bits+=2;
437                 }
438                 return offset_in_bits>>3;
439                 /* TODO Add the rest of the fields */
440                 /* 5.1.5 Custom Picture Format (CPFMT) (23 bits)
441                  * present only if the use of a custom picture format is
442                  * signalled in PLUSPTYPE and UFEP is '001'. When present, CPFMT consists of:
443                  * Bits 1-4 Pixel Aspect Ratio Code: A 4-bit index to the PAR value in Table 5. For
444                  * extended PAR, the exact pixel aspect ratio shall be specified in EPAR
445                  * (see 5.1.6);
446                  * Bits 5-13 Picture Width Indication: Range [0, ... , 511]; Number of pixels per
447                  * line = (PWI + 1) * 4;
448                  * Bit 14 Equal to "1" to prevent start code emulation;
449                  * Bits 15-23 Picture Height Indication: Range [1, ... , 288]; Number of lines = PHI * 4.
450                  */
451                 /* 5.1.6 Extended Pixel Aspect Ratio (EPAR) (16 bits)
452                  * A fixed length codeword of 16 bits that is present only if CPFMT is present and extended PAR is
453                  * indicated therein. When present, EPAR consists of:
454                  *  Bits 1-8 PAR Width: "0" is forbidden. The natural binary representation of the PAR
455                  * width;
456                  *  Bits 9-16 PAR Height: "0" is forbidden. The natural binary representation of the PAR
457                  * height.
458                  */
459                 /* 5.1.7 Custom Picture Clock Frequency Code (CPCFC) (8 bits)
460                  * A fixed length codeword of 8 bits that is present only if PLUSPTYPE is present and UFEP is 001
461                  * and a custom picture clock frequency is signalled in PLUSPTYPE. When present, CPCFC consists of:
462                  * Bit 1 Clock Conversion Code: "0" indicates a clock conversion factor of 1000 and
463                  * "1" indicates 1001;
464                  * Bits 2-8 Clock Divisor: "0" is forbidden. The natural binary representation of the value
465                  * of the clock divisor.
466                  */
467                 /* 5.1.8 Extended Temporal Reference (ETR) (2 bits)
468                  * A fixed length codeword of 2 bits which is present only if a custom picture clock frequency is in
469                  * use (regardless of the value of UFEP). It is the two MSBs of the 10-bit number defined in 5.1.2.
470                  */
471                 /* 5.1.9 Unlimited Unrestricted Motion Vectors Indicator (UUI) (Variable length)
472                  * A variable length codeword of 1 or 2 bits that is present only if the optional Unrestricted Motion
473                  * Vector mode is indicated in PLUSPTYPE and UFEP is 001. When UUI is present it indicates the
474                  * effective limitation of the range of the motion vectors being used.
475                  *  UUI = "1" The motion vector range is limited according to Tables D.1 and D.2.
476                  *  UUI = "01" The motion vector range is not limited except by the picture size.
477                  */
478                 /*
479                  *  5.1.10 Slice Structured Submode bits (SSS) (2 bits)
480                  *  A fixed length codeword of 2 bits which is present only if the optional Slice Structured mode
481                  *  (see Annex K) is indicated in PLUSPTYPE and UFEP is 001. If the Slice Structured mode is in use
482                  *  but UFEP is not 001, the last values sent for SSS shall remain in effect.
483                  *  - Bit 1 Rectangular Slices, "0" indicates free-running slices, "1" indicates rectangular
484                  *  slices;
485                  *  - Bit 2 Arbitrary Slice Ordering, "0" indicates sequential order, "1" indicates arbitrary
486                  *  order.
487                  *  5.1.11 Enhancement Layer Number (ELNUM) (4 bits)
488                  *  A fixed length codeword of 4 bits which is present only if the optional Temporal, SNR, and Spatial
489                  *  Scalability mode is in use (regardless of the value of UFEP). The particular enhancement layer is
490                  *  identified by an enhancement layer number, ELNUM. Picture correspondence between layers is
491                  *  achieved via the temporal reference. Picture size is either indicated within each enhancement layer
492                  *  using the existing source format fields or is inferred by the relationship to the reference layer. The
493                  *  first enhancement layer above the base layer is designated as Enhancement Layer Number 2, and
494                  *  the base layer has number 1.
495                  *  5.1.12 Reference Layer Number (RLNUM) (4 bits)
496                  *  A fixed length codeword of 4 bits which is present only if the optional Temporal, SNR, and Spatial
497                  *  Scalability mode is in use (see Annex O) and UFEP is 001. The layer number for the pictures used
498                  *  as reference anchors is identified by a Reference Layer Number (RLNUM). Time correspondence
499                  *  between layers is achieved via the temporal reference.
500                  *  Note that for B-pictures in an enhancement layer having temporally surrounding EI- or EP-pictures
501                  *  which are present in the same enhancement layer, RLNUM shall be equal to ELNUM
502                  *  (see Annex O).
503                  *  5.1.13 Reference Picture Selection Mode Flags (RPSMF) (3 bits)
504                  *  A fixed length codeword of 3 bits that is present only if the Reference Picture Selection mode is in
505                  *  use and UFEP is 001. When present, RPSMF indicates which type of back-channel messages are
506                  *  needed by the encoder. If the Reference Picture Selection mode is in use but RPSMF is not present,
507                  *  the last value of RPSMF that was sent shall remain in effect.
508                  *  - 100: neither ACK nor NACK signals needed;
509                  *  - 101: need ACK signals to be returned;
510                  *  - 110: need NACK signals to be returned;
511                  *  - 111: need both ACK and NACK signals to be returned;
512                  *  - 000-011: Reserved.
513                  *  5.1.14 Temporal Reference for Prediction Indication (TRPI) (1 bit)
514                  *  A fixed length codeword of 1 bit that is present only if the optional Reference Picture Selection
515                  *  mode is in use (regardless of the value of UFEP). When present, TRPI indicates the presence of the
516                  *  following TRP field:
517                  *  - 0: TRP field is not present;
518                  *  - 1: TRP field is present.
519                  *  TRPI shall be 0 whenever the picture header indicates an I- or EI-picture.
520                  *  5.1.15 Temporal Reference for Prediction (TRP) (10 bits)
521                  *  When present (as indicated in TRPI), TRP indicates the Temporal Reference which is used for
522                  *  prediction of the encoding, except for in the case of B-pictures. For B-pictures, the picture having
523                  *  the temporal reference TRP is used for the prediction in the forward direction. (Prediction in the
524                  *  reverse-temporal direction always uses the immediately temporally subsequent picture.) TRP is a
525                  *  ten-bit number. If a custom picture clock frequency was not in use for the reference picture, the two
526                  *  MSBs of TRP are zero and the LSBs contain the eight-bit TR found in the picture header of the
527                  *  reference picture. If a custom picture clock frequency was in use for the reference picture, TRP is a
528                  *  ten-bit number consisting of the concatenation of ETR and TR from the reference picture header.
529                  *  When TRP is not present, the most recent temporally previous anchor picture shall be used for
530                  *  prediction, as when not in the Reference Picture Selection mode. TRP is valid until the next PSC,
531                  *  GSC, or SSC.
532                  *  5.1.16 Back-Channel message Indication (BCI) (Variable length)
533                  *  A variable length field of one or two bits that is present only if the optional Reference Picture
534                  *  Selection mode is in use. When set to "1", this signals the presence of the following optional video
535                  *  Back-Channel Message (BCM) field. "01" indicates the absence or the end of the video backchannel
536                  *  message field. Combinations of BCM and BCI may not be present, and may be repeated
537                  *  when present. BCI shall be set to "01" if the videomux submode of the optional Reference Picture
538                  *  Selection mode is not in use.
539                  *  5.1.17 Back-Channel Message (BCM) (Variable length)
540                  *  The Back-Channel message with syntax as specified in N.4.2, which is present only if the preceding
541                  *  BCI field is present and is set to "1".
542                  *  5.1.18 Reference Picture Resampling Parameters (RPRP) (Variable length)
543                  *  A variable length field that is present only if the optional Reference Picture Resampling mode bit is
544                  *  set in PLUSPTYPE. This field carries the parameters of the Reference Picture Resampling mode
545                  *  (see Annex P). Note that the Reference Picture Resampling mode can also be invoked implicitly by
546                  *  the occurrence of a picture header for an INTER coded picture having a picture size which differs
547                  *  from that of the previous encoded picture, in which case the RPRP field is not present and the
548                  *  Reference Picture Resampling mode bit is not set.
549                  */
550         }
551         /* 5.1.19 Quantizer Information (PQUANT) (5 bits) */
552         proto_tree_add_bits_item( tree, hf_h263_pquant, tvb, offset_in_bits, 5, FALSE);
553         offset_in_bits = offset_in_bits +5;
554         if (source_format != H263_PLUSPTYPE){
555                 proto_tree_add_bits_ret_val( tree, hf_h263_cpm, tvb, offset_in_bits, 1, &cpm, FALSE);
556                 offset_in_bits++;
557                 /* 5.1.21 Picture Sub-Bitstream Indicator (PSBI) (2 bits)
558                  * only present if Continuous Presence Multipoint and Video
559                  * Multiplex mode is indicated by CPM.
560                  */
561                 if(cpm==1){
562                         proto_tree_add_bits_item( tree, hf_h263_psbi, tvb, offset_in_bits, 2, FALSE);
563                         offset_in_bits = offset_in_bits +2;
564                 }
565         }
566         /* 5.1.22 Temporal Reference for B-pictures in PB-frames (TRB) (3/5 bits)
567          * TRB is present if PTYPE or PLUSPTYPE indicates "PB-frame" or "Improved PB-frame"
568          * It is 3 bits long for standard CIF picture clock frequency and is
569          * extended to 5 bits when a custom picture clock frequency is in use.
570          */
571         if((PB_frames_mode == 1)||(picture_type_code == 2 )){
572                 if(custom_pcf == 0){
573                         proto_tree_add_bits_item( tree, hf_h263_trb, tvb, offset_in_bits, 3, FALSE);
574                         offset_in_bits = offset_in_bits +3;
575                 }else{
576                         proto_tree_add_bits_item( tree, hf_h263_trb, tvb, offset_in_bits, 5, FALSE);
577                         offset_in_bits = offset_in_bits +5;
578                 }
579         }
580         /* 5.1.23 Quantization information for B-pictures in PB-frames (DBQUANT) (2 bits)
581          * DBQUANT is present if PTYPE or PLUSPTYPE indicates "PB-frame" or "Improved PB-frame"
582          */
583         if((PB_frames_mode == 1)||(picture_type_code == 2 )){
584                 offset_in_bits = offset_in_bits +2;
585         }
586         /* 5.1.24 Extra Insertion Information (PEI) (1 bit)
587          * A bit which when set to "1" signals the presence of the following optional data field.
588          */
589         proto_tree_add_bits_ret_val( tree, hf_h263_pei, tvb, offset_in_bits, 1, &pei, FALSE);
590         offset_in_bits++;
591         while(pei==1)
592         {
593                 /*5.1.25 Supplemental Enhancement Information (PSUPP) (0/8/16 ... bits) 
594                  * If PEI is set to "1", then 9 bits follow consisting of 8 bits of data (PSUPP) and then another PEI bit
595                  * to indicate if a further 9 bits follow and so on. Encoders shall use PSUPP as specified in Annex L.
596                  */
597                 proto_tree_add_bits_item( tree, hf_h263_psupp, tvb, offset_in_bits, 8, FALSE);
598                 offset_in_bits+=8;
599                 proto_tree_add_bits_ret_val( tree, hf_h263_pei, tvb, offset_in_bits, 1, &pei, FALSE);
600                 offset_in_bits++;
601         }
602         /* For the first GOB in each picture (with number 0), no GOB header shall be transmitted.
603          * For all other GOBs, the GOB header may be empty, depending on the encoder strategy.
604          */
605
606         /*
607          * 5.3 Macroblock layer
608          * dissect_h263_macroblock_layer( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gint offset)
609          */
610
611         return offset_in_bits>>3;
612
613 }
614
615 /*
616         5.1.1 Picture Start Code (PSC) (22 bits)
617         PSC is a word of 22 bits. Its value is 0000 0000 0000 0000 1 00000. All picture start codes shall be
618         byte aligned.
619         ( 1000 00xx)
620
621         End Of Sequence (EOS) (22 bits)
622         A codeword of 22 bits. Its value is 0000 0000 0000 0000 1 11111.
623         ( 1111 11xx )
624
625         Group of Block Start Code (GBSC) (17 bits)
626         A word of 17 bits. Its value is 0000 0000 0000 0000 1.
627         ( 1xxx xxxx )
628
629         End Of Sub-Bitstream code (EOSBS) (23 bits)
630         The EOSBS code is a codeword of 23 bits. Its value is 0000 0000 0000 0000 1 11110 0.
631         ( 1111 100x )
632
633         Slice Start Code (SSC) (17 bits)
634         A word of 17 bits. Its value is 0000 0000 0000 0000 1.
635         ( 1xxx xxxx )
636   */
637 static void dissect_h263_data( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree )
638 {
639         guint offset = 0;
640         proto_item *h263_payload_item   = NULL;
641         proto_tree *h263_payload_tree   = NULL;
642         guint32 data;
643         guint8 startcode;
644
645         col_append_str( pinfo->cinfo, COL_INFO, "H263 payload ");
646
647         if( tree ) {
648           h263_payload_item = proto_tree_add_item( tree, proto_h263_data, tvb, offset, -1, FALSE );
649           h263_payload_tree = proto_item_add_subtree( h263_payload_item, ett_h263_payload );
650         }
651
652         /* Check for PSC, PSC is a word of 22 bits. Its value is 0000 0000 0000 0000' 1000 00xx xxxx xxxx. */
653         data = tvb_get_ntohl(tvb, offset);
654         
655         if (( data & 0xffff8000) == 0x00008000 ) { 
656                 /* Start Code found
657                  *
658                  * Startc code holds bit 17 -23 of the codeword
659                  */
660                 startcode = tvb_get_guint8(tvb,offset+2)&0xfe;
661                 if (startcode & 0x80){
662                         switch(startcode){
663                         case 0xf8:
664                                 /* End Of Sub-Bitstream code (EOSBS) 
665                                  * ( 1111 100. )
666                                  */
667                                 break;
668                         case 0x80:
669                         case 0x82:
670                                 /* Picture Start Code (PSC)
671                                  * ( 1000 00x.)
672                                  */
673                                 col_append_str( pinfo->cinfo, COL_INFO, "(PSC) ");
674                                 offset = dissect_h263_picture_layer( tvb, pinfo, h263_payload_tree, offset, -1, FALSE);
675                                 break;
676                         case 0xfc:
677                         case 0xfe:
678                                 /* End Of Sequence (EOS)
679                                  * ( 1111 11x. )
680                                  */
681                         default:
682                                 /* Group of Block Start Code (GBSC) or
683                                  * Slice Start Code (SSC)
684                                  */
685                                 col_append_str( pinfo->cinfo, COL_INFO, "(GBSC) ");
686                                 offset = dissect_h263_group_of_blocks_layer( tvb, h263_payload_tree, offset,FALSE);
687                                 break;
688                         }
689                 }else{
690                         /* Error */
691                 }
692         }
693         if( tree )
694                 proto_tree_add_item( h263_payload_tree, hf_h263_data, tvb, offset, -1, FALSE );
695 }
696
697 void
698 proto_register_h263_data(void)
699 {
700         static hf_register_info hf[] =
701         {
702                 {
703                         &hf_h263_psc,
704                         {
705                                 "H.263 Picture start Code",
706                                 "h263.psc",
707                                 FT_UINT32,
708                                 BASE_HEX,
709                                 NULL,
710                                 0x0,
711                                 "Picture start Code, PSC", HFILL
712                         }
713                 },
714                 { &hf_h263_gbsc,
715                         {
716                                 "H.263 Group of Block Start Code",
717                                 "h263.gbsc",
718                                 FT_UINT32,
719                                 BASE_HEX,
720                                 NULL,
721                                 0x0,
722                                 "Group of Block Start Code", HFILL
723                         }
724                 },
725                 {
726                         &hf_h263_TR,
727                         {
728                                 "H.263 Temporal Reference",
729                                 "h263.tr2",
730                                 FT_UINT32,
731                                 BASE_DEC,
732                                 NULL,
733                                 0x0,
734                                 "Temporal Reference, TR", HFILL
735                         }
736                 },
737                 {
738                         &hf_h263_trb,
739                         {
740                                 "Temporal Reference for B frames",
741                                 "h263.trb",
742                                 FT_UINT8,
743                                 BASE_DEC,
744                                 NULL,
745                                 0x0,
746                                 "Temporal Reference for the B frame as defined by H.263", HFILL
747                         }
748                 },
749                 {
750                         &hf_h263_split_screen_indicator,
751                         {
752                                 "H.263 Split screen indicator",
753                                 "h263.split_screen_indicator",
754                                 FT_BOOLEAN,
755                                 BASE_NONE,
756                                 TFS(&on_off_flg),
757                                 0x0,
758                                 "Split screen indicator", HFILL
759                         }
760                 },
761                 {
762                         &hf_h263_document_camera_indicator,
763                         {
764                                 "H.263 Document camera indicator",
765                                 "h263.document_camera_indicator",
766                                 FT_BOOLEAN,
767                                 BASE_NONE,
768                                 TFS(&on_off_flg),
769                                 0x0,
770                                 "Document camera indicator", HFILL
771                         }
772                 },
773                 {
774                         &hf_h263_full_picture_freeze_release,
775                         {
776                                 "H.263 Full Picture Freeze Release",
777                                 "h263.split_screen_indicator",
778                                 FT_BOOLEAN,
779                                 BASE_NONE,
780                                 TFS(&on_off_flg),
781                                 0x0,
782                                 "Full Picture Freeze Release", HFILL
783                         }
784                 },
785                 {
786                         &hf_h263_source_format,
787                         {
788                                 "H.263 Source Format",
789                                 "h263.source_format",
790                                 FT_UINT8,
791                                 BASE_HEX,
792                                 VALS(h263_srcformat_vals),
793                                 0x0,
794                                 "Source Format", HFILL
795                         }
796                 },
797                 {
798                         &hf_h263_ext_source_format,
799                         {
800                                 "H.263 Source Format",
801                                 "h263.ext_source_format",
802                                 FT_UINT8,
803                                 BASE_HEX,
804                                 VALS(ext_srcformat_vals),
805                                 0x0,
806                                 "Source Format", HFILL
807                         }
808                 },
809                 {
810                         &hf_h263_UFEP,
811                         {
812                                 "H.263 Update Full Extended PTYPE",
813                                 "h263.ufep",
814                                 FT_UINT16,
815                                 BASE_DEC,
816                                 VALS(h263_ufep_vals),
817                                 0x0,
818                                 "Update Full Extended PTYPE", HFILL
819                         }
820                 },
821                 {
822                         &hf_h263_opptype,
823                         {
824                                 "H.263 Optional Part of PLUSPTYPE",
825                                 "h263.opptype",
826                                 FT_UINT24,
827                                 BASE_DEC,
828                                 NULL,
829                                 0x0,
830                                 "Optional Part of PLUSPTYPE", HFILL
831                         }
832                 },
833                 {
834                         &hf_h263_payload_picture_coding_type,
835                         {
836                                 "H.263 Picture Coding Type",
837                                 "h263.picture_coding_type",
838                                 FT_BOOLEAN,
839                                 BASE_NONE,
840                                 TFS(&picture_coding_type_flg),
841                                 0x0,
842                                 "Picture Coding Type", HFILL
843                         }
844                 },
845                 {
846                         &hf_h263_opt_unres_motion_vector_mode,
847                         {
848                                 "H.263 Optional Unrestricted Motion Vector mode",
849                                 "h263.opt_unres_motion_vector_mode",
850                                 FT_BOOLEAN,
851                                 BASE_NONE,
852                                 TFS(&on_off_flg),
853                                 0x0,
854                                 "Optional Unrestricted Motion Vector mode", HFILL
855                         }
856                 },
857                 {
858                         &hf_h263_syntax_based_arithmetic_coding_mode,
859                         {
860                                 "H.263 Optional Syntax-based Arithmetic Coding mode",
861                                 "h263.syntax_based_arithmetic_coding_mode",
862                                 FT_BOOLEAN,
863                                 BASE_NONE,
864                                 TFS(&on_off_flg),
865                                 0x0,
866                                 "Optional Syntax-based Arithmetic Coding mode", HFILL
867                         }
868                 },
869                 {
870                         &hf_h263_optional_advanced_prediction_mode,
871                         {
872                                 "H.263 Optional Advanced Prediction mode",
873                                 "h263.optional_advanced_prediction_mode",
874                                 FT_BOOLEAN,
875                                 BASE_NONE,
876                                 TFS(&on_off_flg),
877                                 0x0,
878                                 "Optional Advanced Prediction mode", HFILL
879                         }
880                 },
881                 {
882                         &hf_h263_PB_frames_mode,
883                         {
884                                 "H.263 Optional PB-frames mode",
885                                 "h263.PB_frames_mode",
886                                 FT_BOOLEAN,
887                                 BASE_NONE,
888                                 TFS(&PB_frames_mode_flg),
889                                 0x0,
890                                 "Optional PB-frames mode", HFILL
891                         }
892                 },
893                 {
894                         &hf_h263_GN,
895                         {
896                                 "H.263 Group Number",
897                                 "h263.gn",
898                                 FT_UINT32,
899                                 BASE_DEC,
900                                 NULL,
901                                 0x0,
902                                 "Group Number, GN", HFILL
903                         }
904                 },
905                 {
906                         &hf_h263_pquant,
907                         {
908                                 "H.263 Quantizer Information (PQUANT)",
909                                 "h263.pquant",
910                                 FT_UINT32,
911                                 BASE_DEC,
912                                 NULL,
913                                 0x0,
914                                 "Quantizer Information (PQUANT)", HFILL
915                         }
916                 },
917                 {
918                         &hf_h263_cpm,
919                         {
920                                 "H.263 Continuous Presence Multipoint and Video Multiplex (CPM)",
921                                 "h263.cpm",
922                                 FT_BOOLEAN,
923                                 BASE_NONE,
924                                 TFS(&cpm_flg),
925                                 0x0,
926                                 "Continuous Presence Multipoint and Video Multiplex (CPM)", HFILL
927                         }
928                 },
929                 {
930                         &hf_h263_psbi,
931                         {
932                                 "H.263 Picture Sub-Bitstream Indicator (PSBI)",
933                                 "h263.psbi",
934                                 FT_UINT32,
935                                 BASE_DEC,
936                                 NULL,
937                                 0x0,
938                                 "Picture Sub-Bitstream Indicator (PSBI)", HFILL
939                         }
940                 },
941                 {
942                         &hf_h263_picture_type_code,
943                         {
944                                 "H.263 Picture Type Code",
945                                 "h263.psi",
946                                 FT_UINT32,
947                                 BASE_DEC,
948                                 VALS(picture_type_code_vals),
949                                 0x0,
950                                 "Picture Type Code", HFILL
951                         }
952                 },
953                 {
954                         &hf_h263_custom_pcf,
955                         {
956                                 "H.263 Custom PCF",
957                                 "h263.custom_pcf",
958                                 FT_BOOLEAN,
959                                 BASE_NONE,
960                                 TFS(&custom_pcf_flg),
961                                 0x0,
962                                 "Custom PCF", HFILL
963                         }
964                 },
965                 {
966                         &hf_h263_pei,
967                         {
968                                 "H.263 Extra Insertion Information (PEI)",
969                                 "h263.pei",
970                                 FT_BOOLEAN,
971                                 BASE_NONE,
972                                 NULL,
973                                 0x0,
974                                 "Extra Insertion Information (PEI)", HFILL
975                         }
976                 },
977                 {
978                         &hf_h263_psupp,
979                         {
980                                 "H.263 Supplemental Enhancement Information (PSUPP)",
981                                 "h263.psupp",
982                                 FT_UINT32,
983                                 BASE_DEC,
984                                 NULL,
985                                 0x0,
986                                 "Supplemental Enhancement Information (PSUPP)", HFILL
987                         }
988                 },
989                 {
990                         &hf_h263_data,
991                         {
992                                 "H.263 stream",
993                                 "h263.stream",
994                                 FT_BYTES,
995                                 BASE_NONE,
996                                 NULL,
997                                 0x0,
998                                 "The H.263 stream including its Picture, GOB or Macro block start code.", HFILL
999                         }
1000                 },
1001                 {
1002                         &hf_h263_not_dissected,
1003                         {
1004                                 "H.263 Bits currently not dissected",
1005                                 "h263.not_dis",
1006                                 FT_UINT32,
1007                                 BASE_DEC,
1008                                 NULL,
1009                                 0x0,
1010                                 "These bits are not dissected(yet), displayed for clarity", HFILL
1011                         }
1012                 },
1013 };
1014
1015         static gint *ett[] =
1016         {
1017             &ett_h263_payload,
1018             &ett_h263_optype,
1019         };
1020
1021         proto_register_subtree_array(ett, array_length(ett));
1022
1023         proto_h263_data = proto_register_protocol("ITU-T Recommendation H.263",
1024             "H.263", "h263");
1025         proto_register_field_array(proto_h263_data, hf, array_length(hf));
1026         register_dissector("h263data", dissect_h263_data, proto_h263_data);
1027 }