Huge thanks to our Platinum Members Endace and LiveAction,
and our Silver Member Veeam, for supporting the Wireshark Foundation and project.

Wireshark-dev: [Wireshark-dev] [PATCH] RTP JPEG Dissector

From: Erwin Rol <mailinglists@xxxxxxxxxxxx>
Date: Thu, 10 Aug 2006 10:18:05 +0200
Attached a dissector for JPEG images in RTP streams, AKA  RFC2435. 

- Erwin

Index: epan/dissectors/Makefile.common
===================================================================
--- epan/dissectors/Makefile.common	(revision 18860)
+++ epan/dissectors/Makefile.common	(working copy)
@@ -396,6 +396,7 @@
 	packet-iua.c	\
 	packet-iuup.c	\
 	packet-jabber.c	\
+	packet-jpeg.c \
 	packet-juniper.c \
 	packet-jxta.c	\
 	packet-k12.c \
Index: epan/dissectors/packet-jpeg.c
===================================================================
--- epan/dissectors/packet-jpeg.c	(revision 0)
+++ epan/dissectors/packet-jpeg.c	(revision 0)
@@ -0,0 +1,287 @@
+/* packet-jpeg.c
+ *
+ * Routines for RFC 2435 JPEG dissection
+ *
+ * $Id: $
+ *
+ * Copyright 2006
+ * Erwin Rol <erwin@xxxxxxxxxxxx>
+ * Copyright 2001,
+ * Francisco Javier Cabello Torres, <fjcabello@xxxxxxxxx>
+ *
+ * Ethereal - Network traffic analyzer
+ * By Gerald Combs <gerald@xxxxxxxxxxxx>
+ * Copyright 1998 Gerald Combs
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <glib.h>
+#include <epan/packet.h>
+
+#include <stdio.h>
+#include <string.h>
+
+#include <epan/rtp_pt.h>
+
+
+/* JPEG header fields             */
+static int hf_rtp_jpeg_main_hdr = -1;
+static int hf_rtp_jpeg_main_hdr_ts = -1;
+static int hf_rtp_jpeg_main_hdr_offs = -1;
+static int hf_rtp_jpeg_main_hdr_type = -1;
+static int hf_rtp_jpeg_main_hdr_q = -1;
+static int hf_rtp_jpeg_main_hdr_width = -1;
+static int hf_rtp_jpeg_main_hdr_height = -1;
+
+static int hf_rtp_jpeg_restart_hdr = -1;
+static int hf_rtp_jpeg_restart_hdr_interval = -1;
+static int hf_rtp_jpeg_restart_hdr_f = -1;
+static int hf_rtp_jpeg_restart_hdr_l = -1;
+static int hf_rtp_jpeg_restart_hdr_count = -1;
+
+static int hf_rtp_jpeg_qtable_hdr = -1;
+static int hf_rtp_jpeg_qtable_hdr_mbz = -1;
+static int hf_rtp_jpeg_qtable_hdr_prec = -1;
+static int hf_rtp_jpeg_qtable_hdr_length = -1;
+static int hf_rtp_jpeg_qtable_hdr_data = -1;
+
+static int hf_rtp_jpeg_payload = -1;
+
+static int proto_jpeg = -1;
+
+/* JPEG fields defining a sub tree */
+static gint ett_jpeg = -1;
+
+static void
+dissect_jpeg( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree )
+{
+	proto_item *ti = NULL;
+	proto_tree *jpeg_tree = NULL;
+	proto_tree *main_hdr_tree = NULL;
+	proto_tree *restart_hdr_tree = NULL;
+	proto_tree *qtable_hdr_tree = NULL;
+	guint16 len = 0;
+	guint8 type = 0;
+	guint8 q = 0;
+	gint h = 0;
+	gint w = 0;
+
+	unsigned int offset       = 0;
+
+	if ( check_col( pinfo->cinfo, COL_PROTOCOL ) ) {
+		col_set_str( pinfo->cinfo, COL_PROTOCOL, "JPEG" );
+	}
+
+	if ( check_col( pinfo->cinfo, COL_INFO) ) {
+		col_set_str( pinfo->cinfo, COL_INFO, "JPEG message");
+	}
+
+	if ( tree ) {
+		ti = proto_tree_add_item( tree, proto_jpeg, tvb, offset, -1, FALSE );
+		jpeg_tree = proto_item_add_subtree( ti, ett_jpeg );
+
+		ti = proto_tree_add_item(jpeg_tree, hf_rtp_jpeg_main_hdr, tvb, offset, 8, FALSE);
+		main_hdr_tree = proto_item_add_subtree(ti, ett_jpeg);
+
+		proto_tree_add_item(main_hdr_tree, hf_rtp_jpeg_main_hdr_ts, tvb, offset, 1, FALSE);
+		offset += 1;
+		proto_tree_add_item(main_hdr_tree, hf_rtp_jpeg_main_hdr_offs, tvb, offset, 3, FALSE);
+		offset += 3;
+		proto_tree_add_item(main_hdr_tree, hf_rtp_jpeg_main_hdr_type, tvb, offset, 1, FALSE);
+		type = tvb_get_guint8(tvb, offset);
+		offset += 1;
+		proto_tree_add_item(main_hdr_tree, hf_rtp_jpeg_main_hdr_q, tvb, offset, 1, FALSE);
+		q = tvb_get_guint8(tvb, offset);
+		offset += 1;
+		w = tvb_get_guint8(tvb, offset) * 8;
+		proto_tree_add_uint(main_hdr_tree, hf_rtp_jpeg_main_hdr_width, tvb, offset, 1, w);
+		offset += 1;
+		h = tvb_get_guint8(tvb, offset) * 8;
+		proto_tree_add_uint(main_hdr_tree, hf_rtp_jpeg_main_hdr_height, tvb, offset, 1, h);
+		offset += 1;
+
+		if (type >= 64 && type <= 127) {
+			ti = proto_tree_add_item(jpeg_tree, hf_rtp_jpeg_restart_hdr, tvb, offset, 4, FALSE);
+			restart_hdr_tree = proto_item_add_subtree(ti, ett_jpeg);
+			proto_tree_add_item(restart_hdr_tree, hf_rtp_jpeg_restart_hdr_interval, tvb, offset, 2, FALSE);
+			offset += 2;
+			proto_tree_add_item(restart_hdr_tree, hf_rtp_jpeg_restart_hdr_f, tvb, offset, 2, FALSE);
+			proto_tree_add_item(restart_hdr_tree, hf_rtp_jpeg_restart_hdr_l, tvb, offset, 2, FALSE);
+			proto_tree_add_item(restart_hdr_tree, hf_rtp_jpeg_restart_hdr_count, tvb, offset, 2, FALSE);
+			offset += 2;
+		}
+
+		if (q >= 128 && q <= 255) {
+			ti = proto_tree_add_item(jpeg_tree, hf_rtp_jpeg_qtable_hdr, tvb, offset, -1, FALSE);
+			qtable_hdr_tree = proto_item_add_subtree(ti, ett_jpeg);
+			proto_tree_add_item(main_hdr_tree, hf_rtp_jpeg_qtable_hdr_mbz, tvb, offset, 1, FALSE);
+			offset += 1;
+			proto_tree_add_item(main_hdr_tree, hf_rtp_jpeg_qtable_hdr_prec, tvb, offset, 1, FALSE);
+			offset += 1;
+			proto_tree_add_item(main_hdr_tree, hf_rtp_jpeg_qtable_hdr_length, tvb, offset, 2, FALSE);
+			len = tvb_get_letohs(tvb, offset);
+			offset += 2;
+			if (len > 0) {
+				proto_tree_add_item(main_hdr_tree, hf_rtp_jpeg_qtable_hdr_data, tvb, offset, len, FALSE);
+				offset += 1;
+			}
+			proto_item_set_len(ti, len + 4);
+		}
+
+		/* The rest of the packet is the JPEG data */
+		proto_tree_add_item( jpeg_tree, hf_rtp_jpeg_payload, tvb, offset, -1, FALSE );
+	}
+}
+
+void
+proto_register_jpeg(void)
+{
+
+	static hf_register_info hf[] =
+	{
+		{ &hf_rtp_jpeg_main_hdr, { 
+			"Main Header",
+			"jpeg.main_hdr",
+			FT_NONE, BASE_NONE, NULL, 0,
+			"", HFILL 
+		}},
+		{ &hf_rtp_jpeg_main_hdr_ts, { 
+			"Type Specific",
+			"jpeg.main_hdr.ts",
+			FT_UINT8, BASE_DEC, NULL, 0,
+			"", HFILL 
+		}},
+		{ &hf_rtp_jpeg_main_hdr_offs, { 
+			"Fragement Offset",
+			"jpeg.main_hdr.offset",
+			FT_UINT24, BASE_DEC, NULL, 0,
+			"", HFILL 
+		}},
+		{ &hf_rtp_jpeg_main_hdr_type, { 
+			"Type",
+			"jpeg.main_hdr.type",
+			FT_UINT8, BASE_DEC, NULL, 0,
+			"", HFILL 
+		}},
+		{ &hf_rtp_jpeg_main_hdr_q, { 
+			"Q",
+			"jpeg.main_hdr.q",
+			FT_UINT8, BASE_DEC, NULL, 0,
+			"", HFILL 
+		}},
+		{ &hf_rtp_jpeg_main_hdr_width, { 
+			"Width",
+			"jpeg.main_hdr.width",
+			FT_UINT8, BASE_DEC, NULL, 0,
+			"", HFILL 
+		}},
+		{ &hf_rtp_jpeg_main_hdr_height, { 
+			"Height",
+			"jpeg.main_hdr.height",
+			FT_UINT8, BASE_DEC, NULL, 0,
+			"", HFILL 
+		}},
+		{ &hf_rtp_jpeg_restart_hdr, { 
+			"Restart Marker Header",
+			"jpeg.restart_hdr",
+			FT_NONE, BASE_NONE, NULL, 0,
+			"", HFILL 
+		}},
+		{ &hf_rtp_jpeg_restart_hdr_interval, { 
+			"Restart Interval",
+			"jpeg.restart_hdr.interval",
+			FT_UINT16, BASE_DEC, NULL, 0,
+			"", HFILL 
+		}},
+		{ &hf_rtp_jpeg_restart_hdr_f, { 
+			"F",
+			"jpeg.restart_hdr.f",
+			FT_UINT16, BASE_DEC, NULL, 0x8000,
+			"", HFILL 
+		}},
+		{ &hf_rtp_jpeg_restart_hdr_l, { 
+			"L",
+			"jpeg.restart_hdr.l",
+			FT_UINT16, BASE_DEC, NULL, 0x4000,
+			"", HFILL 
+		}},
+		{ &hf_rtp_jpeg_restart_hdr_count, { 
+			"Restart Count",
+			"jpeg.restart_hdr.count",
+			FT_UINT16, BASE_DEC, NULL, 0x3FFF,
+			"", HFILL 
+		}},
+		{ &hf_rtp_jpeg_qtable_hdr, { 
+			"Quantization Table Header",
+			"jpeg.qtable_hdr",
+			FT_NONE, BASE_NONE, NULL, 0,
+			"", HFILL 
+		}},
+		{ &hf_rtp_jpeg_qtable_hdr_mbz, { 
+			"MBZ",
+			"jpeg.qtable_hdr.mbz",
+			FT_UINT8, BASE_DEC, NULL, 0,
+			"", HFILL 
+		}},
+		{ &hf_rtp_jpeg_qtable_hdr_prec, { 
+			"Precision",
+			"jpeg.qtable_hdr.precision",
+			FT_UINT8, BASE_DEC, NULL, 0,
+			"", HFILL 
+		}},
+		{ &hf_rtp_jpeg_qtable_hdr_length, { 
+			"Length",
+			"jpeg.qtable_hdr.length",
+			FT_UINT16, BASE_DEC, NULL, 0,
+			"", HFILL 
+		}},
+		{ &hf_rtp_jpeg_qtable_hdr_data, { 
+			"Quantization Table Data",
+			"jpeg.qtable_hdr.data",
+			FT_BYTES, BASE_DEC, NULL, 0,
+			"", HFILL 
+		}},
+		{ &hf_rtp_jpeg_payload, { 
+			"Payload",
+			"jpeg.payload",
+			FT_BYTES, BASE_DEC, NULL, 0,
+			"", HFILL 
+		}},
+	};
+
+	static gint *ett[] =
+	{
+		&ett_jpeg,
+	};
+
+
+	proto_jpeg = proto_register_protocol("RFC 2435 JPEG","JPEG","jpeg");
+	proto_register_field_array(proto_jpeg, hf, array_length(hf));
+	proto_register_subtree_array(ett, array_length(ett));
+}
+
+void
+proto_reg_handoff_jpeg(void)
+{
+	dissector_handle_t jpeg_handle;
+
+	jpeg_handle = create_dissector_handle(dissect_jpeg, proto_jpeg);
+	dissector_add("rtp.pt", PT_JPEG, jpeg_handle);
+}