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>
4 *
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.
9 *
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.
14 *
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.
19 */
20
21 #ifndef QT_ATOM_PARSER_H
22 #define QT_ATOM_PARSER_H
23
24 #include <gst/base/gstbytereader.h>
25
26 /* our inlined version of GstByteReader */
27
28 static inline gboolean
qt_atom_parser_has_remaining(GstByteReader * parser,guint64 bytes_needed)29 qt_atom_parser_has_remaining (GstByteReader * parser, guint64 bytes_needed)
30 {
31 return G_LIKELY (parser->size >= bytes_needed) &&
32 G_LIKELY ((parser->size - bytes_needed) >= parser->byte);
33 }
34
35 static inline gboolean
qt_atom_parser_has_chunks(GstByteReader * parser,guint32 n_chunks,guint32 chunk_size)36 qt_atom_parser_has_chunks (GstByteReader * parser, guint32 n_chunks,
37 guint32 chunk_size)
38 {
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);
42 }
43
44 static inline gboolean
qt_atom_parser_peek_sub(GstByteReader * parser,guint offset,guint size,GstByteReader * sub)45 qt_atom_parser_peek_sub (GstByteReader * parser, guint offset, guint size,
46 GstByteReader * sub)
47 {
48 *sub = *parser;
49
50 if (G_UNLIKELY (!gst_byte_reader_skip (sub, offset)))
51 return FALSE;
52
53 return (gst_byte_reader_get_remaining (sub) >= size);
54 }
55
56 static inline gboolean
qt_atom_parser_skipn_and_get_uint32(GstByteReader * parser,guint bytes_to_skip,guint32 * val)57 qt_atom_parser_skipn_and_get_uint32 (GstByteReader * parser,
58 guint bytes_to_skip, guint32 * val)
59 {
60 if (G_UNLIKELY (gst_byte_reader_get_remaining (parser) < (bytes_to_skip + 4)))
61 return FALSE;
62
63 gst_byte_reader_skip_unchecked (parser, bytes_to_skip);
64 *val = gst_byte_reader_get_uint32_be_unchecked (parser);
65 return TRUE;
66 }
67
68 /* off_size must be either 4 or 8 */
69 static inline gboolean
qt_atom_parser_get_offset(GstByteReader * parser,guint off_size,guint64 * val)70 qt_atom_parser_get_offset (GstByteReader * parser, guint off_size,
71 guint64 * val)
72 {
73 if (G_UNLIKELY (gst_byte_reader_get_remaining (parser) < off_size))
74 return FALSE;
75
76 if (off_size == sizeof (guint64)) {
77 *val = gst_byte_reader_get_uint64_be_unchecked (parser);
78 } else {
79 *val = gst_byte_reader_get_uint32_be_unchecked (parser);
80 }
81 return TRUE;
82 }
83
84 /* off_size must be either 4 or 8 */
85 static inline guint64
qt_atom_parser_get_offset_unchecked(GstByteReader * parser,guint off_size)86 qt_atom_parser_get_offset_unchecked (GstByteReader * parser, guint off_size)
87 {
88 if (off_size == sizeof (guint64)) {
89 return gst_byte_reader_get_uint64_be_unchecked (parser);
90 } else {
91 return gst_byte_reader_get_uint32_be_unchecked (parser);
92 }
93 }
94
95 /* size must be from 1 to 4 */
96 static inline guint32
qt_atom_parser_get_uint_with_size_unchecked(GstByteReader * parser,guint size)97 qt_atom_parser_get_uint_with_size_unchecked (GstByteReader * parser,
98 guint size)
99 {
100 switch (size) {
101 case 1:
102 return gst_byte_reader_get_uint8_unchecked (parser);
103 case 2:
104 return gst_byte_reader_get_uint16_be_unchecked (parser);
105 case 3:
106 return gst_byte_reader_get_uint24_be_unchecked (parser);
107 case 4:
108 return gst_byte_reader_get_uint32_be_unchecked (parser);
109 default:
110 g_assert_not_reached ();
111 gst_byte_reader_skip_unchecked (parser, size);
112 break;
113 }
114 return 0;
115 }
116
117 static inline gboolean
qt_atom_parser_get_fourcc(GstByteReader * parser,guint32 * fourcc)118 qt_atom_parser_get_fourcc (GstByteReader * parser, guint32 * fourcc)
119 {
120 guint32 f_be;
121
122 if (G_UNLIKELY (gst_byte_reader_get_remaining (parser) < 4))
123 return FALSE;
124
125 f_be = gst_byte_reader_get_uint32_be_unchecked (parser);
126 *fourcc = GUINT32_SWAP_LE_BE (f_be);
127 return TRUE;
128 }
129
130 static inline guint32
qt_atom_parser_get_fourcc_unchecked(GstByteReader * parser)131 qt_atom_parser_get_fourcc_unchecked (GstByteReader * parser)
132 {
133 guint32 fourcc;
134
135 fourcc = gst_byte_reader_get_uint32_be_unchecked (parser);
136 return GUINT32_SWAP_LE_BE (fourcc);
137 }
138
139 #endif /* QT_ATOM_PARSER_H */
140