summaryrefslogtreecommitdiffstats
path: root/gst/qtdemux/qtatomparser.h
blob: 4bf5409f260db2c6454bfa2577ceade76cd8e75a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* GStreamer QuickTime atom parser
 * Copyright (C) 2009 Tim-Philipp Müller <tim centricular net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifndef QT_ATOM_PARSER_H
#define QT_ATOM_PARSER_H

#include <gst/base/gstbytereader.h>

/* our inlined version of GstByteReader */

typedef GstByteReader QtAtomParser;

static inline void
qt_atom_parser_init (QtAtomParser * parser, const guint8 * data, guint size)
{
  gst_byte_reader_init (parser, data, size);
}

static inline guint
qt_atom_parser_get_remaining (QtAtomParser * parser)
{
  return parser->size - parser->byte;
}

static inline gboolean
qt_atom_parser_has_remaining (QtAtomParser * parser, guint64 min_remaining)
{
  return G_LIKELY (parser->size >= min_remaining) &&
      G_LIKELY ((parser->size - min_remaining) >= parser->byte);
}

static inline gboolean
qt_atom_parser_has_chunks (QtAtomParser * parser, guint32 n_chunks,
    guint32 chunk_size)
{
  /* assumption: n_chunks and chunk_size are 32-bit, we cast to 64-bit here
   * to avoid overflows, to handle e.g. (guint32)-1 * size correctly */
  return qt_atom_parser_has_remaining (parser, (guint64) n_chunks * chunk_size);
}

static inline gboolean
qt_atom_parser_skip (QtAtomParser * parser, guint nbytes)
{
  if (G_UNLIKELY (qt_atom_parser_get_remaining (parser) < nbytes))
    return FALSE;

  parser->byte += nbytes;
  return TRUE;
}

static inline void
qt_atom_parser_skip_unchecked (QtAtomParser * parser, guint nbytes)
{
  parser->byte += nbytes;
}

#ifndef GST_READ_UINT8_BE
#define GST_READ_UINT8_BE GST_READ_UINT8
#endif

#define _QT_ATOM_PARSER_GET_PEEK_BITS(bits,typebits) \
static inline gboolean \
qt_atom_parser_get_uint##bits (QtAtomParser * parser, guint##typebits * val)  \
{ \
  if (G_UNLIKELY (qt_atom_parser_get_remaining (parser) < (bits / 8))) \
    return FALSE; \
  *val = GST_READ_UINT##bits##_BE (parser->data + parser->byte);  \
  parser->byte += bits / 8; \
  return TRUE; \
} \
\
static inline gboolean \
qt_atom_parser_peek_uint##bits (QtAtomParser * parser, guint##typebits * val)  \
{ \
  if (G_UNLIKELY (qt_atom_parser_get_remaining (parser) < (bits / 8))) \
    return FALSE; \
  *val = GST_READ_UINT##bits##_BE (parser->data + parser->byte);  \
  return TRUE; \
} \
\
static inline guint##typebits \
qt_atom_parser_get_uint##bits##_unchecked (QtAtomParser * parser)  \
{ \
  guint##typebits val = GST_READ_UINT##bits##_BE (parser->data + parser->byte);  \
  parser->byte += bits / 8; \
  return val; \
} \
\
static inline guint##typebits \
qt_atom_parser_peek_uint##bits##_unchecked (QtAtomParser * parser)  \
{ \
  return GST_READ_UINT##bits##_BE (parser->data + parser->byte);  \
}

_QT_ATOM_PARSER_GET_PEEK_BITS(8,8);
_QT_ATOM_PARSER_GET_PEEK_BITS(16,16);
_QT_ATOM_PARSER_GET_PEEK_BITS(24,32);
_QT_ATOM_PARSER_GET_PEEK_BITS(32,32);
_QT_ATOM_PARSER_GET_PEEK_BITS(64,64);

static inline gboolean
qt_atom_parser_peek_sub (QtAtomParser * parser, guint offset, guint size,
    QtAtomParser * sub)
{
  *sub = *parser;

  if (G_UNLIKELY (!qt_atom_parser_skip (sub, offset)))
    return FALSE;

  return (qt_atom_parser_get_remaining (sub) >= size);
}

static inline gboolean
qt_atom_parser_skipn_and_get_uint32 (QtAtomParser * parser,
    guint bytes_to_skip, guint32 * val)
{
  if (G_UNLIKELY (qt_atom_parser_get_remaining (parser) < (bytes_to_skip + 4)))
    return FALSE;

  qt_atom_parser_skip_unchecked (parser, bytes_to_skip);
  *val = qt_atom_parser_get_uint32_unchecked (parser);
  return TRUE;
}

/* off_size must be either 4 or 8 */
static inline gboolean
qt_atom_parser_get_offset (QtAtomParser * parser, guint off_size, guint64 * val)
{
  if (G_UNLIKELY (qt_atom_parser_get_remaining (parser) < off_size))
    return FALSE;

  if (off_size == sizeof (guint64)) {
    *val = qt_atom_parser_get_uint64_unchecked (parser);
  } else {
    *val = qt_atom_parser_get_uint32_unchecked (parser);
  }
  return TRUE;
}

/* off_size must be either 4 or 8 */
static inline guint64
qt_atom_parser_get_offset_unchecked (QtAtomParser * parser, guint off_size)
{
  if (off_size == sizeof (guint64)) {
    return qt_atom_parser_get_uint64_unchecked (parser);
  } else {
    return qt_atom_parser_get_uint32_unchecked (parser);
  }
}

static inline guint8 *
qt_atom_parser_peek_bytes_unchecked (QtAtomParser * parser)
{
  return (guint8 *) parser->data + parser->byte;
}

static inline gboolean
qt_atom_parser_get_fourcc (QtAtomParser * parser, guint32 * fourcc)
{
  guint32 f_be;

  if (G_UNLIKELY (qt_atom_parser_get_remaining (parser) < 4))
    return FALSE;

  f_be = qt_atom_parser_get_uint32_unchecked (parser);
  *fourcc = GUINT32_SWAP_LE_BE (f_be);
  return TRUE;
}

static inline guint32
qt_atom_parser_get_fourcc_unchecked (QtAtomParser * parser)
{
  guint32 fourcc;

  fourcc = qt_atom_parser_get_uint32_unchecked (parser);
  return GUINT32_SWAP_LE_BE (fourcc);
}

#endif /* QT_ATOM_PARSER_H */