1 /* GStreamer QuickTime atom parser
2 * Copyright (C) 2009 Tim-Philipp Müller <tim centricular net>
3 * Copyright (C) <2009> STEricsson <benjamin.gaignard@stericsson.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 #ifndef QT_ATOM_PARSER_H
22 #define QT_ATOM_PARSER_H
24 #include <gst/base/gstbytereader.h>
26 /* our inlined version of GstByteReader */
28 static inline gboolean
29 qt_atom_parser_has_remaining (GstByteReader * parser, guint64 bytes_needed)
31 return G_LIKELY (parser->size >= bytes_needed) &&
32 G_LIKELY ((parser->size - bytes_needed) >= parser->byte);
35 static inline gboolean
36 qt_atom_parser_has_chunks (GstByteReader * parser, guint32 n_chunks,
39 /* assumption: n_chunks and chunk_size are 32-bit, we cast to 64-bit here
40 * to avoid overflows, to handle e.g. (guint32)-1 * size correctly */
41 return qt_atom_parser_has_remaining (parser, (guint64) n_chunks * chunk_size);
44 static inline gboolean
45 qt_atom_parser_peek_sub (GstByteReader * parser, guint offset, guint size,
50 if (G_UNLIKELY (!gst_byte_reader_skip (sub, offset)))
53 return (gst_byte_reader_get_remaining (sub) >= size);
56 static inline gboolean
57 qt_atom_parser_skipn_and_get_uint32 (GstByteReader * parser,
58 guint bytes_to_skip, guint32 * val)
60 if (G_UNLIKELY (gst_byte_reader_get_remaining (parser) < (bytes_to_skip + 4)))
63 gst_byte_reader_skip_unchecked (parser, bytes_to_skip);
64 *val = gst_byte_reader_get_uint32_be_unchecked (parser);
68 /* off_size must be either 4 or 8 */
69 static inline gboolean
70 qt_atom_parser_get_offset (GstByteReader * parser, guint off_size,
73 if (G_UNLIKELY (gst_byte_reader_get_remaining (parser) < off_size))
76 if (off_size == sizeof (guint64)) {
77 *val = gst_byte_reader_get_uint64_be_unchecked (parser);
79 *val = gst_byte_reader_get_uint32_be_unchecked (parser);
84 /* off_size must be either 4 or 8 */
86 qt_atom_parser_get_offset_unchecked (GstByteReader * parser, guint off_size)
88 if (off_size == sizeof (guint64)) {
89 return gst_byte_reader_get_uint64_be_unchecked (parser);
91 return gst_byte_reader_get_uint32_be_unchecked (parser);
95 /* size must be from 1 to 4 */
97 qt_atom_parser_get_uint_with_size_unchecked (GstByteReader * parser,
102 return gst_byte_reader_get_uint8_unchecked (parser);
104 return gst_byte_reader_get_uint16_be_unchecked (parser);
106 return gst_byte_reader_get_uint24_be_unchecked (parser);
108 return gst_byte_reader_get_uint32_be_unchecked (parser);
110 g_assert_not_reached ();
111 gst_byte_reader_skip_unchecked (parser, size);
117 static inline gboolean
118 qt_atom_parser_get_fourcc (GstByteReader * parser, guint32 * fourcc)
122 if (G_UNLIKELY (gst_byte_reader_get_remaining (parser) < 4))
125 f_be = gst_byte_reader_get_uint32_be_unchecked (parser);
126 *fourcc = GUINT32_SWAP_LE_BE (f_be);
130 static inline guint32
131 qt_atom_parser_get_fourcc_unchecked (GstByteReader * parser)
135 fourcc = gst_byte_reader_get_uint32_be_unchecked (parser);
136 return GUINT32_SWAP_LE_BE (fourcc);
139 #endif /* QT_ATOM_PARSER_H */