Remove the (long deprecated) proto_tree_add_*_hidden() functions
[obnox/wireshark/wip.git] / wiretap / mpeg.c
index 186a7922de4ee370ece0acf64f62e17955898587..fcecacea18d12fd3fb16f3d14614a75eb504c13f 100644 (file)
 #endif
 
 #include "mpeg.h"
-#include "mpeg-audio.h"
+#include "wsutil/mpeg-audio.h"
 
 #include "wtap-int.h"
 #include "buffer.h"
 #include "file_wrappers.h"
 #include <errno.h>
-#include <math.h>
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
@@ -54,15 +53,15 @@ mpeg_resync(wtap *wth, int *err, gchar **err_info _U_)
 {
        gint64 offset = file_tell(wth->fh);
        size_t count = 0;
-       int sync = file_getc(wth->fh);
+       int byte = file_getc(wth->fh);
 
-       while (sync != EOF) {
-               if (sync == 0xff && count > 0) {
-                       sync = file_getc(wth->fh);
-                       if (sync != EOF && (sync & 0xe0) == 0xe0)
+       while (byte != EOF) {
+               if (byte == 0xff && count > 0) {
+                       byte = file_getc(wth->fh);
+                       if (byte != EOF && (byte & 0xe0) == 0xe0)
                                break;
                } else
-                       sync = file_getc(wth->fh);
+                       byte = file_getc(wth->fh);
                count++;
        }
        file_seek(wth->fh, offset, SEEK_SET, err);
@@ -106,6 +105,8 @@ mpeg_read_rec_data(FILE_T fh, guchar *pd, int length, int *err)
        return TRUE;
 }
 
+#define SCRHZ 27000000
+
 static gboolean 
 mpeg_read(wtap *wth, int *err, gchar **err_info _U_,
                gint64 *data_offset)
@@ -120,7 +121,6 @@ mpeg_read(wtap *wth, int *err, gchar **err_info _U_,
        if (PES_VALID(n)) {
                gint64 offset = file_tell(wth->fh);
                guint8 stream;
-               int bytes_read;
 
                if (offset == -1)
                        return -1;
@@ -138,10 +138,6 @@ mpeg_read(wtap *wth, int *err, gchar **err_info _U_,
                        guint32 pack0;
                        guint64 pack;
                        guint8 stuffing;
-                       guint32 scr;
-                       guint16 scr_ext;
-                       double t;
-                       double secs;
 
                        bytes_read = file_read(&pack1, 1, sizeof pack1, wth->fh);
                        if (bytes_read != sizeof pack1) {
@@ -172,15 +168,20 @@ mpeg_read(wtap *wth, int *err, gchar **err_info _U_,
                                        stuffing &= 0x07;
                                        packet_size = 14 + stuffing;
 
-                                       scr = (guint32)
-                                               ((pack >> 59 & 0x0007) << 30 |
-                                               (pack >> 43 & 0x7fff) << 15 |
-                                                (pack >> 27 & 0x7fff) << 0);
-                                       scr_ext = (guint16)(pack >> 17 & 0x1ff);
-                                       t = wth->capture.mpeg->t0 + scr / 90e3 + scr_ext / 27e6;
-
-                                       wth->capture.mpeg->now.nsecs = (int)(modf(t, &secs) * 1e9);
-                                       wth->capture.mpeg->now.secs = (time_t)secs;
+                                       {
+                                               guint64 bytes = pack >> 16;
+                                               guint64 ts_val =
+                                                       (bytes >> 43 & 0x0007) << 30 |
+                                                       (bytes >> 27 & 0x7fff) << 15 |
+                                                       (bytes >> 11 & 0x7fff) << 0;
+                                               unsigned ext = (unsigned)((bytes >> 1) & 0x1ff);
+                                               guint64 cr = 300 * ts_val + ext;
+                                               unsigned rem = (unsigned)(cr % SCRHZ);
+                                               wth->capture.mpeg->now.secs
+                                                       = wth->capture.mpeg->t0 + (time_t)(cr / SCRHZ);
+                                               wth->capture.mpeg->now.nsecs
+                                                       = (int)(G_GINT64_CONSTANT(1000000000) * rem / SCRHZ);
+                                       }
                                        ts = wth->capture.mpeg->now;
                                        break;
                                default:
@@ -247,14 +248,15 @@ mpeg_close(wtap *wth)
        g_free(wth->capture.mpeg);
 }
 
-/* XXX We probably need more magic to open more types */
 struct _mpeg_magic {
        size_t len;
        const gchar* match;
 } magic[] = {
-       {3,"TAG"},
-       {3,"ID3"},
-       {0,NULL}
+       { 3, "TAG" }, /* ID3v1 */
+       { 3, "ID3" }, /* ID3v2 */
+       { 3, "\0\0\1" }, /* MPEG PES */
+       { 2, "\xff\xfb" }, /* MP3, taken from http://en.wikipedia.org/wiki/MP3#File_structure */
+       { 0, NULL }
 };
 
 int 
@@ -273,8 +275,8 @@ mpeg_open(wtap *wth, int *err, gchar **err_info _U_)
                return 0;
        }
 
-       for (m=magic;m->match;m++) {
-               if (memcmp(magic_buf,m->match,m->len) == 0)
+       for (m=magic; m->match; m++) {
+               if (memcmp(magic_buf, m->match, m->len) == 0)
                        goto good_magic;
        }
        
@@ -293,10 +295,10 @@ good_magic:
        wth->subtype_close = mpeg_close;
        wth->snapshot_length = 0;
 
-       wth->capture.mpeg = g_malloc(sizeof(libpcap_t));
+       wth->capture.mpeg = g_malloc(sizeof(mpeg_t));
        wth->capture.mpeg->now.secs = time(NULL);
        wth->capture.mpeg->now.nsecs = 0;
-       wth->capture.mpeg->t0 = (double) wth->capture.mpeg->now.secs;
+       wth->capture.mpeg->t0 = wth->capture.mpeg->now.secs;
 
        return 1;
 }