]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - mp4.c
mp4: Fix trivial coding style issues in read_mvhd().
[paraslash.git] / mp4.c
diff --git a/mp4.c b/mp4.c
index aab39a1e35d7e6fb74978936d90e6129eac32b69..345775b247d0cf417dff7bb5f513ab0e7a86773b 100644 (file)
--- a/mp4.c
+++ b/mp4.c
@@ -8,6 +8,7 @@
 #include <regex.h>
 
 #include "para.h"
+#include "portable_io.h"
 #include "string.h"
 #include "mp4.h"
 
@@ -30,34 +31,12 @@ static int32_t read_data(mp4ff_t * f, void *data, uint32_t size)
        return result;
 }
 
-/* parse atom header size */
-static int32_t atom_get_size(const int8_t * data)
-{
-       uint32_t result;
-       uint32_t a, b, c, d;
-
-       a = (uint8_t) data[0];
-       b = (uint8_t) data[1];
-       c = (uint8_t) data[2];
-       d = (uint8_t) data[3];
-
-       result = (a << 24) | (b << 16) | (c << 8) | d;
-       return (int32_t) result;
-}
-
 static uint64_t read_int64(mp4ff_t * f)
 {
        uint8_t data[8];
-       uint64_t result = 0;
-       int8_t i;
 
        read_data(f, data, 8);
-
-       for (i = 0; i < 8; i++) {
-               result |= ((uint64_t) data[i]) << ((7 - i) * 8);
-       }
-
-       return result;
+       return read_u64_be(data);
 }
 
 /* comnapre 2 atom names, returns 1 for equal, 0 for unequal */
@@ -72,87 +51,92 @@ static int32_t atom_compare(const int8_t a1, const int8_t b1,
                return 0;
 }
 
-#define TRACK_AUDIO 1
-#define TRACK_VIDEO   2
-#define TRACK_SYSTEM  3
-#define TRACK_UNKNOWN 0
-
-/* atoms with subatoms */
-#define ATOM_MOOV 1
-#define ATOM_TRAK 2
-#define ATOM_EDTS 3
-#define ATOM_MDIA 4
-#define ATOM_MINF 5
-#define ATOM_STBL 6
-#define ATOM_UDTA 7
-#define ATOM_ILST 8            /* iTunes Metadata list */
-#define ATOM_TITLE 9
-#define ATOM_ARTIST 10
-#define ATOM_WRITER 11
-#define ATOM_ALBUM 12
-#define ATOM_DATE 13
-#define ATOM_TOOL 14
-#define ATOM_COMMENT 15
-#define ATOM_GENRE1 16
-#define ATOM_TRACK 17
-#define ATOM_DISC 18
-#define ATOM_COMPILATION 19
-#define ATOM_GENRE2 20
-#define ATOM_TEMPO 21
-#define ATOM_COVER 22
-#define ATOM_DRMS 23
-#define ATOM_SINF 24
-#define ATOM_SCHI 25
-
-#define SUBATOMIC 128
-
-/* atoms without subatoms */
-#define ATOM_FTYP 129
-#define ATOM_MDAT 130
-#define ATOM_MVHD 131
-#define ATOM_TKHD 132
-#define ATOM_TREF 133
-#define ATOM_MDHD 134
-#define ATOM_VMHD 135
-#define ATOM_SMHD 136
-#define ATOM_HMHD 137
-#define ATOM_STSD 138
-#define ATOM_STTS 139
-#define ATOM_STSZ 140
-#define ATOM_STZ2 141
-#define ATOM_STCO 142
-#define ATOM_STSC 143
-#define ATOM_MP4A 144
-#define ATOM_MP4V 145
-#define ATOM_MP4S 146
-#define ATOM_ESDS 147
-#define ATOM_META 148          /* iTunes Metadata box */
-#define ATOM_NAME 149          /* iTunes Metadata name box */
-#define ATOM_DATA 150          /* iTunes Metadata data box */
-#define ATOM_CTTS 151
-#define ATOM_FRMA 152
-#define ATOM_IVIV 153
-#define ATOM_PRIV 154
-#define ATOM_USER 155
-#define ATOM_KEY  156
-#define ATOM_ALBUM_ARTIST      157
-#define ATOM_CONTENTGROUP   158
-#define ATOM_LYRICS         159
-#define ATOM_DESCRIPTION    160
-#define ATOM_NETWORK        161
-#define ATOM_SHOW           162
-#define ATOM_EPISODENAME    163
-#define ATOM_SORTTITLE      164
-#define ATOM_SORTALBUM      165
-#define ATOM_SORTARTIST     166
-#define ATOM_SORTALBUMARTIST    167
-#define ATOM_SORTWRITER     168
-#define ATOM_SORTSHOW       169
-#define ATOM_SEASON         170
-#define ATOM_EPISODE        171
-#define ATOM_PODCAST        172
-
-#define ATOM_UNKNOWN 255
+enum tracks {
+       TRACK_UNKNOWN = 0,
+       TRACK_AUDIO  = 1,
+       TRACK_VIDEO = 2,
+       TRACK_SYSTEM = 3
+};
+
+enum atoms {
+       /* atoms with subatoms */
+       ATOM_MOOV = 1,
+       ATOM_TRAK = 2,
+       ATOM_EDTS = 3,
+       ATOM_MDIA = 4,
+       ATOM_MINF = 5,
+       ATOM_STBL = 6,
+       ATOM_UDTA = 7,
+       ATOM_ILST = 8, /* iTunes Metadata list */
+       ATOM_TITLE = 9,
+       ATOM_ARTIST = 10,
+       ATOM_WRITER = 11,
+       ATOM_ALBUM = 12,
+       ATOM_DATE = 13,
+       ATOM_TOOL = 14,
+       ATOM_COMMENT = 15,
+       ATOM_GENRE1 = 16,
+       ATOM_TRACK = 17,
+       ATOM_DISC = 18,
+       ATOM_COMPILATION = 19,
+       ATOM_GENRE2 = 20,
+       ATOM_TEMPO = 21,
+       ATOM_COVER = 22,
+       ATOM_DRMS = 23,
+       ATOM_SINF = 24,
+       ATOM_SCHI = 25,
+
+       SUBATOMIC = 128,
+
+       /* atoms without subatoms */
+       ATOM_FTYP = 129,
+       ATOM_MDAT = 130,
+       ATOM_MVHD = 131,
+       ATOM_TKHD = 132,
+       ATOM_TREF = 133,
+       ATOM_MDHD = 134,
+       ATOM_VMHD = 135,
+       ATOM_SMHD = 136,
+       ATOM_HMHD = 137,
+       ATOM_STSD = 138,
+       ATOM_STTS = 139,
+       ATOM_STSZ = 140,
+       ATOM_STZ2 = 141,
+       ATOM_STCO = 142,
+       ATOM_STSC = 143,
+       ATOM_MP4A = 144,
+       ATOM_MP4V = 145,
+       ATOM_MP4S = 146,
+       ATOM_ESDS = 147,
+       ATOM_META = 148, /* iTunes Metadata box */
+       ATOM_NAME = 149, /* iTunes Metadata name box */
+       ATOM_DATA = 150, /* iTunes Metadata data box */
+       ATOM_CTTS = 151,
+       ATOM_FRMA = 152,
+       ATOM_IVIV = 153,
+       ATOM_PRIV = 154,
+       ATOM_USER = 155,
+       ATOM_KEY = 156,
+       ATOM_ALBUM_ARTIST = 157,
+       ATOM_CONTENTGROUP = 158,
+       ATOM_LYRICS = 159,
+       ATOM_DESCRIPTION = 160,
+       ATOM_NETWORK = 161,
+       ATOM_SHOW = 162,
+       ATOM_EPISODENAME = 163,
+       ATOM_SORTTITLE = 164,
+       ATOM_SORTALBUM = 165,
+       ATOM_SORTARTIST = 166,
+       ATOM_SORTALBUMARTIST = 167,
+       ATOM_SORTWRITER = 168,
+       ATOM_SORTSHOW = 169,
+       ATOM_SEASON = 170,
+       ATOM_EPISODE = 171,
+       ATOM_PODCAST = 172,
+
+       ATOM_UNKNOWN = 255
+};
+
 #define ATOM_FREE ATOM_UNKNOWN
 #define ATOM_SKIP ATOM_UNKNOWN
 
@@ -325,7 +309,7 @@ static uint64_t atom_read_header(mp4ff_t * f, uint8_t * atom_type,
        if (ret != 8)
                return 0;
 
-       size = atom_get_size(atom_header);
+       size = read_u32_be(atom_header);
        *header_size = 8;
 
        /* check for 64 bit atom size */
@@ -393,33 +377,18 @@ static uint8_t read_char(mp4ff_t * f)
 
 static uint32_t read_int24(mp4ff_t * f)
 {
-       uint32_t result;
-       uint32_t a, b, c;
        int8_t data[4];
 
        read_data(f, data, 3);
-       a = (uint8_t) data[0];
-       b = (uint8_t) data[1];
-       c = (uint8_t) data[2];
-
-       result = (a << 16) | (b << 8) | c;
-       return (uint32_t) result;
+       return read_u24_be(data);
 }
 
 static uint32_t read_int32(mp4ff_t * f)
 {
-       uint32_t result;
-       uint32_t a, b, c, d;
        int8_t data[4];
 
        read_data(f, data, 4);
-       a = (uint8_t) data[0];
-       b = (uint8_t) data[1];
-       c = (uint8_t) data[2];
-       d = (uint8_t) data[3];
-
-       result = (a << 24) | (b << 16) | (c << 8) | d;
-       return (uint32_t) result;
+       return read_u32_be(data);
 }
 
 static int32_t read_stsz(mp4ff_t * f)
@@ -546,16 +515,10 @@ static int32_t read_stco(mp4ff_t * f)
 
 static uint16_t read_int16(mp4ff_t * f)
 {
-       uint32_t result;
-       uint32_t a, b;
        int8_t data[2];
 
        read_data(f, data, 2);
-       a = (uint8_t) data[0];
-       b = (uint8_t) data[1];
-
-       result = (a << 8) | b;
-       return (uint16_t) result;
+       return read_u16_be(data);
 }
 
 static uint32_t read_mp4_descr_length(mp4ff_t * f)
@@ -707,28 +670,23 @@ static int32_t read_mvhd(mp4ff_t * f)
 
        read_char(f);   /* version */
        read_int24(f);  /* flags */
-       /* creation_time */ read_int32(f);
-       /* modification_time */ read_int32(f);
+       read_int32(f); /* creation_time */
+       read_int32(f); /* modification_time */
        f->time_scale = read_int32(f);
        f->duration = read_int32(f);
-                                                       /* preferred_rate */ read_int32(f);
-                                                       /*mp4ff_read_fixed32(f); */
-                                                       /* preferred_volume */ read_int16(f);
-                                                       /*mp4ff_read_fixed16(f); */
-       for (i = 0; i < 10; i++) {
-               /* reserved */ read_char(f);
-       }
-       for (i = 0; i < 9; i++) {
-               read_int32(f);  /* matrix */
-       }
-       /* preview_time */ read_int32(f);
-       /* preview_duration */ read_int32(f);
-       /* poster_time */ read_int32(f);
-       /* selection_time */ read_int32(f);
-       /* selection_duration */ read_int32(f);
-       /* current_time */ read_int32(f);
-       /* next_track_id */ read_int32(f);
-
+       read_int32(f); /* preferred_rate */
+       read_int16(f); /* preferred_volume */
+       for (i = 0; i < 10; i++)
+               read_char(f); /* reserved */
+       for (i = 0; i < 9; i++)
+               read_int32(f); /* matrix */
+       read_int32(f); /* preview_time */
+       read_int32(f); /* preview_duration */
+       read_int32(f); /* poster_time */
+       read_int32(f); /* selection_time */
+       read_int32(f); /* selection_duration */
+       read_int32(f); /* current_time */
+       read_int32(f); /* next_track_id */
        return 0;
 }
 
@@ -1584,14 +1542,16 @@ static unsigned membuffer_write_atom_name(membuffer * buf, const char *data)
 
 static unsigned membuffer_write_int16(membuffer * buf, uint16_t data)
 {
-       uint8_t temp[2] = { (uint8_t) (data >> 8), (uint8_t) data };
+       uint8_t temp[2];
+
+       write_u16_be(temp, data);
        return membuffer_write(buf, temp, 2);
 }
 
 static unsigned membuffer_write_int32(membuffer * buf, uint32_t data)
 {
-       uint8_t temp[4] = { (uint8_t) (data >> 24), (uint8_t) (data >> 16),
-               (uint8_t) (data >> 8), (uint8_t) data };
+       uint8_t temp[4];
+       write_u32_be(temp, data);
        return membuffer_write(buf, temp, 4);
 }
 
@@ -1935,18 +1895,7 @@ uint32_t * out_size)
 
 static uint32_t fix_byte_order_32(uint32_t src)
 {
-       uint32_t result;
-       uint32_t a, b, c, d;
-       int8_t data[4];
-
-       memcpy(data, &src, sizeof (src));
-       a = (uint8_t) data[0];
-       b = (uint8_t) data[1];
-       c = (uint8_t) data[2];
-       d = (uint8_t) data[3];
-
-       result = (a << 24) | (b << 16) | (c << 8) | d;
-       return (uint32_t) result;
+       return read_u32_be(&src);
 }
 
 static uint32_t modify_moov(mp4ff_t * f, const mp4ff_metadata_t * data,
@@ -2076,19 +2025,9 @@ static int32_t write_data(mp4ff_t * f, void *data, uint32_t size)
 
 static int32_t write_int32(mp4ff_t * f, const uint32_t data)
 {
-       uint32_t result;
-       uint32_t a, b, c, d;
        int8_t temp[4];
-
-       *(uint32_t *) temp = data;
-       a = (uint8_t) temp[0];
-       b = (uint8_t) temp[1];
-       c = (uint8_t) temp[2];
-       d = (uint8_t) temp[3];
-
-       result = (a << 24) | (b << 16) | (c << 8) | d;
-
-       return write_data(f, (uint8_t *) & result, sizeof (result));
+       write_u32_be(temp, data);
+       return write_data(f, temp, sizeof(temp));
 }
 
 static int32_t truncate_stream(mp4ff_t * f)