1 /*
2  * ISO File Format parsing library
3  *
4  * gstisoff.h
5  *
6  * Copyright (C) 2015 Samsung Electronics. All rights reserved.
7  *   Author: Thiago Santos <thiagoss@osg.samsung.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library (COPYING); if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24 
25 #ifndef __GST_ISOFF_H__
26 #define __GST_ISOFF_H__
27 
28 #include <gst/gst.h>
29 #include <gst/base/base.h>
30 
31 G_BEGIN_DECLS
32 
33 #ifndef GST_ISOFF_API
34 # ifdef BUILDING_GST_ISOFF
35 #  define GST_ISOFF_API GST_API_EXPORT         /* from config.h */
36 # else
37 #  define GST_ISOFF_API GST_API_IMPORT
38 # endif
39 #endif
40 
41 typedef enum {
42   GST_ISOFF_PARSER_OK,
43   GST_ISOFF_PARSER_DONE,
44   GST_ISOFF_PARSER_UNEXPECTED,
45   GST_ISOFF_PARSER_ERROR
46 } GstIsoffParserResult;
47 
48 GST_ISOFF_API
49 gboolean gst_isoff_parse_box_header (GstByteReader * reader, guint32 * type, guint8 extended_type[16], guint * header_size, guint64 * size);
50 
51 #define GST_ISOFF_FOURCC_UUID GST_MAKE_FOURCC('u','u','i','d')
52 #define GST_ISOFF_FOURCC_MOOF GST_MAKE_FOURCC('m','o','o','f')
53 #define GST_ISOFF_FOURCC_MFHD GST_MAKE_FOURCC('m','f','h','d')
54 #define GST_ISOFF_FOURCC_TFHD GST_MAKE_FOURCC('t','f','h','d')
55 #define GST_ISOFF_FOURCC_TRUN GST_MAKE_FOURCC('t','r','u','n')
56 #define GST_ISOFF_FOURCC_TRAF GST_MAKE_FOURCC('t','r','a','f')
57 #define GST_ISOFF_FOURCC_TFDT GST_MAKE_FOURCC('t','f','d','t')
58 #define GST_ISOFF_FOURCC_MDAT GST_MAKE_FOURCC('m','d','a','t')
59 #define GST_ISOFF_FOURCC_MOOV GST_MAKE_FOURCC('m','o','o','v')
60 #define GST_ISOFF_FOURCC_TRAK GST_MAKE_FOURCC('t','r','a','k')
61 #define GST_ISOFF_FOURCC_TKHD GST_MAKE_FOURCC('t','k','h','d')
62 #define GST_ISOFF_FOURCC_MDIA GST_MAKE_FOURCC('m','d','i','a')
63 #define GST_ISOFF_FOURCC_MDHD GST_MAKE_FOURCC('m','d','h','d')
64 #define GST_ISOFF_FOURCC_HDLR GST_MAKE_FOURCC('h','d','l','r')
65 #define GST_ISOFF_FOURCC_SIDX GST_MAKE_FOURCC('s','i','d','x')
66 
67 /* handler type */
68 #define GST_ISOFF_FOURCC_SOUN GST_MAKE_FOURCC('s','o','u','n')
69 #define GST_ISOFF_FOURCC_VIDE GST_MAKE_FOURCC('v','i','d','e')
70 
71 #define GST_ISOFF_SAMPLE_FLAGS_IS_LEADING(flags)                   (((flags) >> 26) & 0x03)
72 #define GST_ISOFF_SAMPLE_FLAGS_SAMPLE_DEPENDS_ON(flags)            (((flags) >> 24) & 0x03)
73 #define GST_ISOFF_SAMPLE_FLAGS_SAMPLE_IS_DEPENDED_ON(flags)        (((flags) >> 22) & 0x03)
74 #define GST_ISOFF_SAMPLE_FLAGS_SAMPLE_HAS_REDUNDANCY(flags)        (((flags) >> 20) & 0x03)
75 #define GST_ISOFF_SAMPLE_FLAGS_SAMPLE_PADDING_VALUE(flags)         (((flags) >> 17) & 0x07)
76 #define GST_ISOFF_SAMPLE_FLAGS_SAMPLE_IS_NON_SYNC_SAMPLE(flags)    (((flags) >> 16) & 0x01)
77 #define GST_ISOFF_SAMPLE_FLAGS_SAMPLE_DEGRADATION_PRIORITY(flags)  (((flags) >>  0) & 0x0f)
78 
79 /* Smooth-Streaming specific boxes */
80 typedef struct _GstTfxdBox
81 {
82   guint8 version;
83   guint32 flags;
84 
85   guint64 time;
86   guint64 duration;
87 } GstTfxdBox;
88 
89 typedef struct _GstTfrfBoxEntry
90 {
91   guint64 time;
92   guint64 duration;
93 } GstTfrfBoxEntry;
94 
95 typedef struct _GstTfrfBox
96 {
97   guint8 version;
98   guint32 flags;
99 
100   gint entries_count;
101   GArray *entries;
102 } GstTfrfBox;
103 
104 /* Common boxes */
105 typedef struct _GstMfhdBox
106 {
107   guint32 sequence_number;
108 } GstMfhdBox;
109 
110 typedef enum
111 {
112   GST_TFHD_FLAGS_BASE_DATA_OFFSET_PRESENT         = 0x000001,
113   GST_TFHD_FLAGS_SAMPLE_DESCRIPTION_INDEX_PRESENT = 0x000002,
114   GST_TFHD_FLAGS_DEFAULT_SAMPLE_DURATION_PRESENT  = 0x000008,
115   GST_TFHD_FLAGS_DEFAULT_SAMPLE_SIZE_PRESENT      = 0x000010,
116   GST_TFHD_FLAGS_DEFAULT_SAMPLE_FLAGS_PRESENT     = 0x000020,
117   GST_TFHD_FLAGS_DURATION_IS_EMPTY                = 0x010000,
118   GST_TFHD_FLAGS_DEFAULT_BASE_IS_MOOF             = 0x020000
119 } GstTfhdFlags;
120 
121 typedef struct _GstTfhdBox
122 {
123   guint8 version;
124   GstTfhdFlags flags;
125 
126   guint32 track_id;
127 
128   /* optional */
129   guint64 base_data_offset;
130   guint32 sample_description_index;
131   guint32 default_sample_duration;
132   guint32 default_sample_size;
133   guint32 default_sample_flags;
134 } GstTfhdBox;
135 
136 typedef enum
137 {
138   GST_TRUN_FLAGS_DATA_OFFSET_PRESENT                     = 0x000001,
139   GST_TRUN_FLAGS_FIRST_SAMPLE_FLAGS_PRESENT              = 0x000004,
140   GST_TRUN_FLAGS_SAMPLE_DURATION_PRESENT                 = 0x000100,
141   GST_TRUN_FLAGS_SAMPLE_SIZE_PRESENT                     = 0x000200,
142   GST_TRUN_FLAGS_SAMPLE_FLAGS_PRESENT                    = 0x000400,
143   GST_TRUN_FLAGS_SAMPLE_COMPOSITION_TIME_OFFSETS_PRESENT = 0x000800
144 } GstTrunFlags;
145 
146 typedef struct _GstTrunBox
147 {
148   guint8 version;
149   GstTrunFlags flags;
150 
151   guint32 sample_count;
152 
153   /* optional */
154   gint32 data_offset;
155   guint32 first_sample_flags;
156   GArray *samples;
157 } GstTrunBox;
158 
159 typedef struct _GstTrunSample
160 {
161   guint32 sample_duration;
162   guint32 sample_size;
163   guint32 sample_flags;
164 
165   union {
166     guint32 u; /* version 0 */
167     gint32  s; /* others */
168   } sample_composition_time_offset;
169 } GstTrunSample;
170 
171 typedef struct _GstTdftBox
172 {
173   guint64 decode_time;
174 } GstTfdtBox;
175 
176 typedef struct _GstTrafBox
177 {
178   GstTfhdBox tfhd;
179   GstTfdtBox tfdt;
180   GArray *trun;
181 
182   /* smooth-streaming specific */
183   GstTfrfBox *tfrf;
184   GstTfxdBox *tfxd;
185 } GstTrafBox;
186 
187 typedef struct _GstMoofBox
188 {
189   GstMfhdBox mfhd;
190   GArray *traf;
191 } GstMoofBox;
192 
193 GST_ISOFF_API
194 GstMoofBox * gst_isoff_moof_box_parse (GstByteReader *reader);
195 
196 GST_ISOFF_API
197 void gst_isoff_moof_box_free (GstMoofBox *moof);
198 
199 typedef struct _GstTkhdBox
200 {
201   guint32 track_id;
202 } GstTkhdBox;
203 
204 typedef struct _GstMdhdBox
205 {
206   guint32 timescale;
207 } GstMdhdBox;
208 
209 typedef struct _GstHdlrBox
210 {
211   guint32 handler_type;
212 } GstHdlrBox;
213 
214 typedef struct _GstMdiaBox
215 {
216   GstMdhdBox mdhd;
217   GstHdlrBox hdlr;
218 } GstMdiaBox;
219 
220 typedef struct _GstTrakBox
221 {
222   GstTkhdBox tkhd;
223   GstMdiaBox mdia;
224 } GstTrakBox;
225 
226 typedef struct _GstMoovBox
227 {
228   GArray *trak;
229 } GstMoovBox;
230 
231 GST_ISOFF_API
232 GstMoovBox * gst_isoff_moov_box_parse (GstByteReader *reader);
233 
234 GST_ISOFF_API
235 void gst_isoff_moov_box_free (GstMoovBox *moov);
236 
237 typedef struct _GstSidxBoxEntry
238 {
239   gboolean ref_type;
240   guint32 size;
241   GstClockTime duration;
242   gboolean starts_with_sap;
243   guint8 sap_type;
244   guint32 sap_delta_time;
245 
246   guint64 offset;
247   GstClockTime pts;
248 } GstSidxBoxEntry;
249 
250 typedef struct _GstSidxBox
251 {
252   guint8 version;
253   guint32 flags;
254 
255   guint32 ref_id;
256   guint32 timescale;
257   guint64 earliest_pts;
258   guint64 first_offset;
259 
260   gint entry_index;
261   gint entries_count;
262 
263   GstSidxBoxEntry *entries;
264 } GstSidxBox;
265 
266 typedef enum _GstSidxParserStatus
267 {
268   GST_ISOFF_SIDX_PARSER_INIT,
269   GST_ISOFF_SIDX_PARSER_HEADER,
270   GST_ISOFF_SIDX_PARSER_DATA,
271   GST_ISOFF_SIDX_PARSER_FINISHED
272 } GstSidxParserStatus;
273 
274 typedef struct _GstSidxParser
275 {
276   GstSidxParserStatus status;
277 
278   guint64 size;
279   guint64 cumulative_entry_size;
280   guint64 cumulative_pts;
281 
282   GstSidxBox sidx;
283 } GstSidxParser;
284 
285 GST_ISOFF_API
286 void gst_isoff_sidx_parser_init (GstSidxParser * parser);
287 
288 GST_ISOFF_API
289 void gst_isoff_sidx_parser_clear (GstSidxParser * parser);
290 
291 GST_ISOFF_API
292 GstIsoffParserResult gst_isoff_sidx_parser_parse (GstSidxParser * parser, GstByteReader * reader, guint * consumed);
293 
294 GST_ISOFF_API
295 GstIsoffParserResult gst_isoff_sidx_parser_add_buffer (GstSidxParser * parser, GstBuffer * buf, guint * consumed);
296 
297 G_END_DECLS
298 
299 #endif /* __GST_ISOFF_H__ */
300