Fix Dead Store (Dead assignement/Dead increment) warning found by Clang
[metze/wireshark/wip.git] / epan / dissectors / file-mp4.c
1 /* file-mp4.c
2  * routines for dissection of MP4 files
3  * Copyright 2013-2014, Martin Kaiser <martin@kaiser.cx>
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 /* this dissector is based on
25  * ISO/IEC 14496-12 (ISO base media file format) and
26  * ISO/IEC 14496-14 (MP4 file format)
27  *
28  * at the moment, it dissects the basic box structure and the payload of
29  * some simple boxes */
30
31
32 #include "config.h"
33
34 #include <math.h>
35 #include <glib.h>
36 #include <epan/expert.h>
37 #include <epan/packet.h>
38
39 #define MAKE_TYPE_VAL(a, b, c, d)   ((a)<<24 | (b)<<16 | (c)<<8 | (d))
40
41 void proto_register_mp4(void);
42 void proto_reg_handoff_mp4(void);
43
44 static gint dissect_mp4_box(guint32 parent_box_type _U_,
45         tvbuff_t *tvb, gint offset, packet_info *pinfo, proto_tree *tree);
46
47 static int proto_mp4 = -1;
48
49 static gint ett_mp4 = -1;
50 static gint ett_mp4_box = -1;
51
52 static int hf_mp4_box_size = -1;
53 static int hf_mp4_box_type_str = -1;
54 static int hf_mp4_box_largesize = -1;
55 static int hf_mp4_full_box_ver = -1;
56 static int hf_mp4_full_box_flags = -1;
57 static int hf_mp4_ftyp_brand = -1;
58 static int hf_mp4_ftyp_ver = -1;
59 static int hf_mp4_ftyp_add_brand = -1;
60 static int hf_mp4_mfhd_seq_num = -1;
61 static int hf_mp4_tkhd_creat_time = -1;
62 static int hf_mp4_tkhd_mod_time = -1;
63 static int hf_mp4_tkhd_track_id = -1;
64 static int hf_mp4_tkhd_duration = -1;
65 static int hf_mp4_tkhd_width = -1;
66 static int hf_mp4_tkhd_height = -1;
67 static int hf_mp4_hdlr_type = -1;
68 static int hf_mp4_hdlr_name = -1;
69 static int hf_mp4_dref_entry_cnt = -1;
70 static int hf_mp4_stsd_entry_cnt = -1;
71
72 static expert_field ei_mp4_box_too_large = EI_INIT;
73
74 /* a box must at least have a 32bit len field and a 32bit type */
75 #define MIN_BOX_SIZE 8
76 /* an extended box has the first length field set to 1 */
77 #define BOX_SIZE_EXTENDED 1
78
79 /* the box type is stored as four text characters
80    it is in network byte order and contains only printable characters
81    for our internal handling, we convert this to a 32bit value */
82
83 #define BOX_TYPE_NONE  0x0 /* used for parent_box_type of a top-level box */
84 #define BOX_TYPE_FTYP  MAKE_TYPE_VAL('f', 't', 'y', 'p')
85 #define BOX_TYPE_MOOV  MAKE_TYPE_VAL('m', 'o', 'o', 'v')
86 #define BOX_TYPE_MVHD  MAKE_TYPE_VAL('m', 'v', 'h', 'd')
87 #define BOX_TYPE_TRAK  MAKE_TYPE_VAL('t', 'r', 'a', 'k')
88 #define BOX_TYPE_TKHD  MAKE_TYPE_VAL('t', 'k', 'h', 'd')
89 #define BOX_TYPE_MDIA  MAKE_TYPE_VAL('m', 'd', 'i', 'a')
90 #define BOX_TYPE_MDHD  MAKE_TYPE_VAL('m', 'd', 'h', 'd')
91 #define BOX_TYPE_HDLR  MAKE_TYPE_VAL('h', 'd', 'l', 'r')
92 #define BOX_TYPE_MINF  MAKE_TYPE_VAL('m', 'i', 'n', 'f')
93 #define BOX_TYPE_VMHD  MAKE_TYPE_VAL('v', 'm', 'h', 'd')
94 #define BOX_TYPE_SMHD  MAKE_TYPE_VAL('s', 'm', 'h', 'd')
95 #define BOX_TYPE_DINF  MAKE_TYPE_VAL('d', 'i', 'n', 'f')
96 #define BOX_TYPE_DREF  MAKE_TYPE_VAL('d', 'r', 'e', 'f')
97 #define BOX_TYPE_STBL  MAKE_TYPE_VAL('s', 't', 'b', 'l')
98 #define BOX_TYPE_STTS  MAKE_TYPE_VAL('s', 't', 't', 's')
99 #define BOX_TYPE_CTTS  MAKE_TYPE_VAL('c', 't', 't', 's')
100 #define BOX_TYPE_STSD  MAKE_TYPE_VAL('s', 't', 's', 'd')
101 #define BOX_TYPE_STSZ  MAKE_TYPE_VAL('s', 't', 's', 'z')
102 #define BOX_TYPE_STSC  MAKE_TYPE_VAL('s', 't', 's', 'c')
103 #define BOX_TYPE_STCO  MAKE_TYPE_VAL('s', 't', 'c', 'o')
104 #define BOX_TYPE_STSS  MAKE_TYPE_VAL('s', 't', 's', 's')
105 #define BOX_TYPE_MVEX  MAKE_TYPE_VAL('m', 'v', 'e', 'x')
106 #define BOX_TYPE_MOOF  MAKE_TYPE_VAL('m', 'o', 'o', 'f')
107 #define BOX_TYPE_MEHD  MAKE_TYPE_VAL('m', 'e', 'h', 'd')
108 #define BOX_TYPE_TREX  MAKE_TYPE_VAL('t', 'r', 'e', 'x')
109 #define BOX_TYPE_MFHD  MAKE_TYPE_VAL('m', 'f', 'h', 'd')
110 #define BOX_TYPE_TRAF  MAKE_TYPE_VAL('t', 'r', 'a', 'f')
111 #define BOX_TYPE_TFHD  MAKE_TYPE_VAL('t', 'f', 'h', 'd')
112 #define BOX_TYPE_TRUN  MAKE_TYPE_VAL('t', 'r', 'u', 'n')
113 #define BOX_TYPE_MDAT  MAKE_TYPE_VAL('m', 'd', 'a', 't')
114 #define BOX_TYPE_UDTA  MAKE_TYPE_VAL('u', 'd', 't', 'a')
115 /* the box name is url + <space>, all names must be 4 characters long */
116 #define BOX_TYPE_URL_  MAKE_TYPE_VAL('u', 'r', 'l', ' ')
117
118 /* the location for this URL box is the same as in the upper-level movie box */
119 #define ENTRY_FLAG_MOVIE 0x000001
120
121 static const value_string box_types[] = {
122     { BOX_TYPE_FTYP, "File Type Box" },
123     { BOX_TYPE_MOOV, "Movie Box" },
124     { BOX_TYPE_MVHD, "Movie Header Box" },
125     { BOX_TYPE_TRAK, "Track Box" },
126     { BOX_TYPE_TKHD, "Track Header Box" },
127     { BOX_TYPE_MDIA, "Media Box" },
128     { BOX_TYPE_MDHD, "Media Header Box" },
129     { BOX_TYPE_HDLR, "Handler Reference Box" },
130     { BOX_TYPE_MINF, "Media Information Box" },
131     { BOX_TYPE_VMHD, "Video Media Header Box" },
132     { BOX_TYPE_SMHD, "Sound Media Header Box" },
133     { BOX_TYPE_DINF, "Data Information Box" },
134     { BOX_TYPE_DREF, "Data Reference Box" },
135     { BOX_TYPE_STBL, "Sample to Group Box" },
136     { BOX_TYPE_STTS, "Decoding Time To Sample Box" },
137     { BOX_TYPE_CTTS, "Composition Time To Sample Box" },
138     { BOX_TYPE_STSD, "Sample Description Box" },
139     { BOX_TYPE_STSZ, "Sample Size Box" },
140     { BOX_TYPE_STSC, "Sample To Chunk Box" },
141     { BOX_TYPE_STCO, "Chunk Offset Box" },
142     { BOX_TYPE_STSS, "Sync Sample Table" },
143     { BOX_TYPE_MVEX, "Movie Extends Box" },
144     { BOX_TYPE_MOOF, "Movie Fragment Box" },
145     { BOX_TYPE_MEHD, "Movie Extends Header Box" },
146     { BOX_TYPE_TREX, "Track Extends Box" },
147     { BOX_TYPE_MFHD, "Movie Fragment Header Box" },
148     { BOX_TYPE_TRAF, "Track Fragment Box" },
149     { BOX_TYPE_TFHD, "Track Fragment Header Box" },
150     { BOX_TYPE_TRUN, "Track Fragment Run Box" },
151     { BOX_TYPE_MDAT, "Media Data Box" },
152     { BOX_TYPE_UDTA, "User Data Box" },
153     { BOX_TYPE_URL_, "URL Box" },
154     { 0, NULL }
155 };
156
157 /* convert a decimal number x into a double 0.x (e.g. 123 becomes 0.123) */
158 static inline double
159 make_fract(guint x)
160 {
161     if (x==0)
162         return 0.0;
163
164     return (double)(x / exp(log(10.0)*(1+floor(log((double)x)/log(10.0)))));
165 }
166
167 static gint
168 dissect_mp4_mvhd_body(tvbuff_t *tvb, gint offset, gint len _U_,
169         packet_info *pinfo _U_, proto_tree *tree)
170 {
171     gint offset_start;
172
173     offset_start = offset;
174     proto_tree_add_item(tree, hf_mp4_full_box_ver,
175             tvb, offset, 1, ENC_BIG_ENDIAN);
176     offset += 1;
177     proto_tree_add_item(tree, hf_mp4_full_box_flags,
178             tvb, offset, 3, ENC_BIG_ENDIAN);
179     offset += 3;
180
181     return offset-offset_start;
182 }
183
184 static gint
185 dissect_mp4_mfhd_body(tvbuff_t *tvb, gint offset, gint len _U_,
186         packet_info *pinfo _U_, proto_tree *tree)
187 {
188     gint offset_start;
189
190     offset_start = offset;
191     proto_tree_add_item(tree, hf_mp4_full_box_ver,
192             tvb, offset, 1, ENC_BIG_ENDIAN);
193     offset += 1;
194     proto_tree_add_item(tree, hf_mp4_full_box_flags,
195             tvb, offset, 3, ENC_BIG_ENDIAN);
196     offset += 3;
197
198     proto_tree_add_item(tree, hf_mp4_mfhd_seq_num,
199             tvb, offset, 4, ENC_BIG_ENDIAN);
200     offset += 4;
201
202     return offset-offset_start;
203 }
204
205
206 static gint
207 dissect_mp4_tkhd_body(tvbuff_t *tvb, gint offset, gint len _U_,
208         packet_info *pinfo _U_, proto_tree *tree)
209 {
210     gint     offset_start;
211     guint8   version;
212     guint8   time_len;
213     double   width, height;
214     guint16  fract_dec;
215
216     offset_start = offset;
217
218     version = tvb_get_guint8(tvb, offset);
219     proto_tree_add_item(tree, hf_mp4_full_box_ver,
220             tvb, offset, 1, ENC_BIG_ENDIAN);
221     offset += 1;
222     proto_tree_add_item(tree, hf_mp4_full_box_flags,
223             tvb, offset, 3, ENC_BIG_ENDIAN);
224     offset += 3;
225
226     time_len = (version==0) ? 4 : 8;
227     proto_tree_add_item(tree, hf_mp4_tkhd_creat_time,
228             tvb, offset, time_len, ENC_BIG_ENDIAN);
229     offset += time_len;
230     proto_tree_add_item(tree, hf_mp4_tkhd_mod_time,
231             tvb, offset, time_len, ENC_BIG_ENDIAN);
232     offset += time_len;
233
234     proto_tree_add_item(tree, hf_mp4_tkhd_track_id,
235             tvb, offset, 4, ENC_BIG_ENDIAN);
236     offset += 4;
237
238     offset += 4;   /* 32bit reserved */
239
240     proto_tree_add_item(tree, hf_mp4_tkhd_duration,
241             tvb, offset, time_len, ENC_BIG_ENDIAN);
242     offset += time_len;
243
244     offset += 2*4; /* 2*32bit reserved */
245     offset += 2;   /* 16bit layer */
246     offset += 2;   /* 16bit alternate_group */
247     offset += 2;   /* 16bit volume */
248     offset += 2;   /* 16bit reserved */
249     offset += 9*4; /* 9*32bit matrix */
250
251     width = tvb_get_ntohs(tvb, offset);
252     fract_dec = tvb_get_ntohs(tvb, offset+2);
253     width += make_fract(fract_dec);
254     proto_tree_add_double_format_value(tree, hf_mp4_tkhd_width,
255             tvb, offset, 4, width, "%f", width);
256     offset += 4;
257
258     height = tvb_get_ntohs(tvb, offset);
259     fract_dec = tvb_get_ntohs(tvb, offset+2);
260     height += make_fract(fract_dec);
261     proto_tree_add_double_format_value(tree, hf_mp4_tkhd_height,
262             tvb, offset, 4, height, "%f", height);
263     offset += 4;
264
265     return offset-offset_start;
266 }
267
268
269 static gint
270 dissect_mp4_ftyp_body(tvbuff_t *tvb, gint offset, gint len,
271         packet_info *pinfo _U_, proto_tree *tree)
272 {
273     gint offset_start;
274
275     offset_start = offset;
276     proto_tree_add_item(tree, hf_mp4_ftyp_brand,
277             tvb, offset, 4, ENC_ASCII|ENC_NA);
278     offset += 4;
279     proto_tree_add_item(tree, hf_mp4_ftyp_ver,
280             tvb, offset, 4, ENC_BIG_ENDIAN);
281     offset += 4;
282
283     while ((offset-offset_start) < len) {
284         proto_tree_add_item(tree, hf_mp4_ftyp_add_brand,
285                 tvb, offset, 4, ENC_ASCII|ENC_NA);
286         offset += 4;
287     }
288
289     return offset - offset_start;
290 }
291
292
293 static gint
294 dissect_mp4_hdlr_body(tvbuff_t *tvb, gint offset, gint len _U_,
295         packet_info *pinfo _U_, proto_tree *tree)
296 {
297     gint   offset_start;
298     guint  hdlr_name_len;
299
300     offset_start = offset;
301
302     proto_tree_add_item(tree, hf_mp4_full_box_ver,
303             tvb, offset, 1, ENC_BIG_ENDIAN);
304     /* XXX - put up an expert info if version!=0 */
305     offset += 1;
306     proto_tree_add_item(tree, hf_mp4_full_box_flags,
307             tvb, offset, 3, ENC_BIG_ENDIAN);
308     offset += 3;
309
310     offset += 4;   /* four reserved 0 bytes */
311
312     proto_tree_add_item(tree, hf_mp4_hdlr_type,
313             tvb, offset, 4, ENC_ASCII|ENC_NA);
314     offset += 4;
315
316     offset += 12;   /* 3x32bit reserved */
317
318     /* name is a 0-terminated UTF-8 string, len includes the final 0 */
319     hdlr_name_len = tvb_strsize(tvb, offset);
320     proto_tree_add_item(tree, hf_mp4_hdlr_name,
321             tvb, offset, hdlr_name_len, ENC_UTF_8|ENC_NA);
322     offset += hdlr_name_len;
323
324     return offset-offset_start;
325 }
326
327
328 static gint
329 dissect_mp4_dref_body(tvbuff_t *tvb, gint offset, gint len _U_,
330         packet_info *pinfo, proto_tree *tree)
331 {
332     gint     offset_start;
333     guint32  entry_cnt, i;
334     gint     ret;
335
336     offset_start = offset;
337
338     proto_tree_add_item(tree, hf_mp4_full_box_ver,
339             tvb, offset, 1, ENC_BIG_ENDIAN);
340     /* XXX - put up an expert info if version!=0 */
341     offset += 1;
342     proto_tree_add_item(tree, hf_mp4_full_box_flags,
343             tvb, offset, 3, ENC_BIG_ENDIAN);
344     offset += 3;
345
346     entry_cnt = tvb_get_ntohl(tvb, offset);
347     proto_tree_add_item(tree, hf_mp4_dref_entry_cnt,
348             tvb, offset, 4, ENC_BIG_ENDIAN);
349     offset += 4;
350
351     for(i=0; i<entry_cnt; i++) {
352         ret = dissect_mp4_box(BOX_TYPE_DREF, tvb, offset, pinfo, tree);
353         if (ret<=0)
354             break;
355
356         offset += ret;
357     }
358
359     return offset-offset_start;
360 }
361
362
363 static gint
364 dissect_mp4_url_body(tvbuff_t *tvb, gint offset, gint len,
365         packet_info *pinfo _U_, proto_tree *tree)
366 {
367     guint32  flags;
368
369     proto_tree_add_item(tree, hf_mp4_full_box_ver,
370             tvb, offset, 1, ENC_BIG_ENDIAN);
371     /* XXX - put up an expert info if version!=0 */
372     offset += 1;
373
374     flags = tvb_get_ntoh24(tvb, offset);
375     proto_tree_add_item(tree, hf_mp4_full_box_flags,
376             tvb, offset, 3, ENC_BIG_ENDIAN);
377     /*offset += 3;*/
378
379     if (flags&ENTRY_FLAG_MOVIE) {
380         proto_tree_add_text(tree, tvb, 0, 0,
381                 "Media data location is defined in the movie box");
382     }
383     else {
384         /* XXX - dissect location string */
385     }
386
387     return len;
388 }
389
390
391 static gint
392 dissect_mp4_stsd_body(tvbuff_t *tvb, gint offset, gint len,
393         packet_info *pinfo _U_, proto_tree *tree)
394 {
395     guint32  entry_cnt, i;
396     gint     ret;
397
398     proto_tree_add_item(tree, hf_mp4_full_box_ver,
399             tvb, offset, 1, ENC_BIG_ENDIAN);
400     /* XXX - put up an expert info if version!=0 */
401     offset += 1;
402     proto_tree_add_item(tree, hf_mp4_full_box_flags,
403             tvb, offset, 3, ENC_BIG_ENDIAN);
404     offset += 3;
405
406     entry_cnt = tvb_get_ntohl(tvb, offset);
407     proto_tree_add_item(tree, hf_mp4_dref_entry_cnt,
408             tvb, offset, 4, ENC_BIG_ENDIAN);
409     offset += 4;
410
411     for(i=0; i<entry_cnt; i++) {
412         /* a sample entry has the same format as an mp4 box
413            we call dissect_mp4_box() to dissect it
414            alternatively, we could parse it ourselves, we'd then have to
415            handle the extended lengths etc */
416
417         /* XXX - dissect the content of each Sample Entry,
418            this depends on the handler_type, we could add an optional
419            void *data parameter to dissect_mp4_box() and handle sample
420            entry boxes based on parent box and data parameter */
421         ret = dissect_mp4_box(BOX_TYPE_STSD, tvb, offset, pinfo, tree);
422         if (ret<=0)
423             break;
424
425         offset += ret;
426     }
427
428     return len;
429 }
430
431  
432 /* dissect a box, return its (standard or extended) length or 0 for error */
433 static gint
434 dissect_mp4_box(guint32 parent_box_type _U_,
435         tvbuff_t *tvb, gint offset, packet_info *pinfo, proto_tree *tree)
436 {
437     gint        offset_start;
438     guint64     box_size;
439     guint32     box_type;
440     guint8     *box_type_str;
441     proto_item *type_pi, *size_pi, *ext_size_pi = NULL;
442     proto_tree *box_tree;
443     gint        ret;
444     gint        body_size;
445
446
447     offset_start = offset;
448
449     /* the following mechanisms are not supported for now
450        - size==0, indicating that the box extends to the end of the file
451        - extended box types */
452
453     box_size = (guint64)tvb_get_ntohl(tvb, offset);
454     if ((box_size != BOX_SIZE_EXTENDED) && (box_size < MIN_BOX_SIZE))
455         return -1;
456
457     box_type = tvb_get_ntohl(tvb, offset+4);
458     box_type_str = tvb_get_string_enc(wmem_packet_scope(), tvb,
459             offset+4, 4, ENC_ASCII|ENC_NA);
460
461     box_tree = proto_tree_add_subtree_format(tree, tvb, offset, -1, ett_mp4_box, &type_pi, "%s (%s)",
462             val_to_str_const(box_type, box_types, "unknown"), box_type_str);
463
464     size_pi = proto_tree_add_item(box_tree, hf_mp4_box_size,
465             tvb, offset, 4, ENC_BIG_ENDIAN);
466     if (box_size == BOX_SIZE_EXTENDED)
467         proto_item_append_text(size_pi, " (actual size is in largesize)");
468
469     offset += 4;
470     proto_tree_add_item(box_tree, hf_mp4_box_type_str,
471             tvb, offset, 4, ENC_ASCII|ENC_NA);
472     offset += 4;
473
474     if (box_size == BOX_SIZE_EXTENDED) {
475         box_size = tvb_get_ntoh64(tvb, offset);
476         ext_size_pi = proto_tree_add_item(box_tree, hf_mp4_box_largesize,
477                 tvb, offset, 8, ENC_BIG_ENDIAN);
478         offset += 8;
479     }
480
481     if (box_size > G_MAXINT) {
482         /* this should be ok for ext_size_pi==NULL */
483         expert_add_info(pinfo, ext_size_pi, &ei_mp4_box_too_large);
484         return -1;
485     }
486     proto_item_set_len(type_pi, (gint)box_size);
487     body_size = (gint)box_size - (offset-offset_start);
488
489     /* we do not dissect full box version and flags here
490        these two components are required by the function dissecting the body
491        some fields of the body depend on the version and flags */
492
493     /* XXX - check parent box if supplied */
494     switch (box_type) {
495         case BOX_TYPE_FTYP:
496             dissect_mp4_ftyp_body(tvb, offset, body_size, pinfo, box_tree);
497             break;
498         case BOX_TYPE_MVHD:
499             dissect_mp4_mvhd_body(tvb, offset, body_size, pinfo, box_tree);
500             break;
501         case BOX_TYPE_MFHD:
502             dissect_mp4_mfhd_body(tvb, offset, body_size, pinfo, box_tree);
503             break;
504         case BOX_TYPE_TKHD:
505             dissect_mp4_tkhd_body(tvb, offset, body_size, pinfo, box_tree);
506             break;
507         case BOX_TYPE_HDLR:
508             dissect_mp4_hdlr_body(tvb, offset, body_size, pinfo, box_tree);
509             break;
510         case BOX_TYPE_DREF:
511             dissect_mp4_dref_body(tvb, offset, body_size, pinfo, box_tree);
512             break;
513         case BOX_TYPE_URL_:
514             dissect_mp4_url_body(tvb, offset, body_size, pinfo, box_tree);
515             break;
516         case BOX_TYPE_STSD:
517             dissect_mp4_stsd_body(tvb, offset, body_size, pinfo, box_tree);
518             break;
519         case BOX_TYPE_MOOV:
520         case BOX_TYPE_MOOF:
521         case BOX_TYPE_STBL:
522         case BOX_TYPE_MDIA:
523         case BOX_TYPE_TRAK:
524         case BOX_TYPE_TRAF:
525         case BOX_TYPE_MINF:
526         case BOX_TYPE_MVEX:
527         case BOX_TYPE_DINF:
528         case BOX_TYPE_UDTA:
529             while (offset-offset_start < (gint)box_size) {
530                 ret = dissect_mp4_box(box_type, tvb, offset, pinfo, box_tree);
531                 if (ret <= 0)
532                     break;
533                 offset += ret;
534             }
535             break;
536         default:
537             break;
538     }
539
540     return (gint)box_size;
541 }
542
543
544 static int
545 dissect_mp4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
546 {
547     gint        offset = 0;
548     guint32     box_type;
549     proto_item *pi;
550     proto_tree *mp4_tree;
551     gint        ret;
552
553     /* to make sure that we have an mp4 file, we check that it starts with
554         a box of a known type
555        please note that we do not allow the first box to be an extended box
556        this detection should be safe as long as the dissector is only called for
557         the video/mp4 mime type
558        when we read mp4 files directly, we might need stricter checks here */
559     if (tvb_reported_length(tvb) < MIN_BOX_SIZE)
560         return 0;
561     box_type = tvb_get_ntohl(tvb, 4);
562     if (try_val_to_str(box_type, box_types) == NULL)
563         return 0;
564
565     col_set_str(pinfo->cinfo, COL_PROTOCOL, "MP4");
566     col_clear(pinfo->cinfo, COL_INFO);
567
568     pi = proto_tree_add_protocol_format(tree, proto_mp4,
569             tvb, 0, (gint)tvb_reported_length(tvb), "MP4");
570     mp4_tree = proto_item_add_subtree(pi, ett_mp4);
571
572     while (tvb_reported_length_remaining(tvb, offset) > 0) {
573         ret = dissect_mp4_box(BOX_TYPE_NONE, tvb, offset, pinfo, mp4_tree);
574         if (ret <= 0)
575             break;
576         offset += ret;
577     }
578
579     return offset;
580 }
581
582 void
583 proto_register_mp4(void)
584 {
585     static hf_register_info hf[] = {
586         { &hf_mp4_box_size,
587             { "Box size", "mp4.box.size", FT_UINT32, BASE_DEC,
588                 NULL, 0, NULL, HFILL } },
589         { &hf_mp4_box_type_str,
590             { "Box type", "mp4.box.type_str", FT_STRING, BASE_NONE,
591                 NULL, 0, NULL, HFILL } },
592         { &hf_mp4_box_largesize,
593             { "Box size (largesize)", "mp4.box.largesize", FT_UINT64, BASE_DEC,
594                 NULL, 0, NULL, HFILL } },
595         { &hf_mp4_full_box_ver,
596             { "Box version", "mp4.full_box.version", FT_UINT8, BASE_DEC,
597                 NULL, 0, NULL, HFILL } },
598         { &hf_mp4_full_box_flags,
599             { "Flags", "mp4.full_box.flags", FT_UINT24, BASE_HEX,
600                 NULL, 0, NULL, HFILL } },
601         { &hf_mp4_ftyp_brand,
602             { "Brand", "mp4.ftyp.brand", FT_STRING, BASE_NONE,
603                 NULL, 0, NULL, HFILL } },
604         { &hf_mp4_ftyp_ver,
605             { "Version", "mp4.ftyp.version", FT_UINT32, BASE_DEC,
606                 NULL, 0, NULL, HFILL } },
607         { &hf_mp4_ftyp_add_brand,
608             { "Additional brand", "mp4.ftyp.additional_brand", FT_STRING,
609                 BASE_NONE, NULL, 0, NULL, HFILL } },
610         { &hf_mp4_mfhd_seq_num,
611             { "Sequence number", "mp4.mfhd.sequence_number", FT_UINT32,
612                 BASE_DEC, NULL, 0, NULL, HFILL } },
613         { &hf_mp4_tkhd_creat_time,
614             { "Creation time", "mp4.tkhd.creation_time", FT_UINT64,
615                 BASE_DEC, NULL, 0, NULL, HFILL } },
616         { &hf_mp4_tkhd_mod_time,
617             { "Modification time", "mp4.tkhd.modification_time", FT_UINT64,
618                 BASE_DEC, NULL, 0, NULL, HFILL } },
619         { &hf_mp4_tkhd_track_id,
620             { "Track ID", "mp4.tkhd.track_id", FT_UINT32,
621                 BASE_DEC, NULL, 0, NULL, HFILL } },
622         { &hf_mp4_tkhd_duration,
623             { "Duration", "mp4.tkhd.duration", FT_UINT64,
624                 BASE_DEC, NULL, 0, NULL, HFILL } },
625         { &hf_mp4_tkhd_width,
626             { "Width", "mp4.tkhd.width", FT_DOUBLE,
627                 BASE_NONE, NULL, 0, NULL, HFILL } },
628         { &hf_mp4_tkhd_height,
629             { "Height", "mp4.tkhd.height", FT_DOUBLE,
630                 BASE_NONE, NULL, 0, NULL, HFILL } },
631         { &hf_mp4_hdlr_type,
632             { "Handler type", "mp4.hdlr.type", FT_STRING,
633                 BASE_NONE, NULL, 0, NULL, HFILL } },
634         { &hf_mp4_hdlr_name,
635             { "Handler name", "mp4.hdlr.name", FT_STRINGZ,
636                 BASE_NONE, NULL, 0, NULL, HFILL } },
637         { &hf_mp4_dref_entry_cnt,
638             { "Number of entries", "mp4.dref.entry_count", FT_UINT32,
639                 BASE_DEC, NULL, 0, NULL, HFILL } },
640         { &hf_mp4_stsd_entry_cnt,
641             { "Number of entries", "mp4.stsd.entry_count", FT_UINT32,
642                 BASE_DEC, NULL, 0, NULL, HFILL } }
643     };
644
645     static gint *ett[] = {
646         &ett_mp4,
647         &ett_mp4_box
648     };
649
650     static ei_register_info ei[] = {
651         { &ei_mp4_box_too_large,
652             { "mp4.box_too_large", PI_PROTOCOL, PI_WARN,
653                 "box size too large, dissection of this box is not supported", EXPFILL }}
654     };
655
656     expert_module_t *expert_mp4;
657
658     proto_mp4 = proto_register_protocol("MP4 / ISOBMFF file format", "mp4", "mp4");
659
660     proto_register_field_array(proto_mp4, hf, array_length(hf));
661     proto_register_subtree_array(ett, array_length(ett));
662     expert_mp4 = expert_register_protocol(proto_mp4);
663     expert_register_field_array(expert_mp4, ei, array_length(ei));
664 }
665
666 void
667 proto_reg_handoff_mp4(void)
668 {
669     dissector_handle_t mp4_handle = new_create_dissector_handle(dissect_mp4, proto_mp4);
670     dissector_add_string("media_type", "video/mp4", mp4_handle);
671 }
672
673
674 /*
675  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
676  *
677  * Local variables:
678  * c-basic-offset: 4
679  * tab-width: 8
680  * indent-tabs-mode: nil
681  * End:
682  *
683  * vi: set shiftwidth=4 tabstop=8 expandtab:
684  * :indentSize=4:tabSize=8:noTabs=true:
685  */