b2d55f7d332a2c3e1744b9feba4a6553afe74baf
[framework/multimedia/gst-plugins-ext0.10.git] / piffdemux / src / piffdemux_dump.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) 2009 Tim-Philipp Müller <tim centricular net>
4  * Copyright (C) <2009> STEricsson <benjamin.gaignard@stericsson.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "piffdemux_types.h"
23 #include "piffdemux_dump.h"
24
25 #include "piffatomparser.h"
26
27 #include <string.h>
28
29 #define GET_UINT8(data)   gst_byte_reader_get_uint8_unchecked(data)
30 #define GET_UINT16(data)  gst_byte_reader_get_uint16_be_unchecked(data)
31 #define GET_UINT32(data)  gst_byte_reader_get_uint32_be_unchecked(data)
32 #define GET_UINT64(data)  gst_byte_reader_get_uint64_be_unchecked(data)
33 #define GET_FP32(data)   (gst_byte_reader_get_uint32_be_unchecked(data)/65536.0)
34 #define GET_FP16(data)   (gst_byte_reader_get_uint16_be_unchecked(data)/256.0)
35 #define GET_FOURCC(data)  piff_atom_parser_get_fourcc_unchecked(data)
36
37 gboolean
38 piffdemux_dump_vmhd (GstPiffDemux * piffdemux, GstByteReader * data, int depth)
39 {
40   if (!piff_atom_parser_has_remaining (data, 4 + 4))
41     return FALSE;
42
43   GST_LOG ("%*s  version/flags: %08x", depth, "", GET_UINT32 (data));
44   GST_LOG ("%*s  mode/color:    %08x", depth, "", GET_UINT32 (data));
45   return TRUE;
46 }
47
48
49 gboolean
50 piffdemux_dump_mfro (GstPiffDemux * piffdemux, GstByteReader * data, int depth)
51 {
52   if (!piff_atom_parser_has_remaining (data, 4))
53     return FALSE;
54
55   GST_LOG ("%*s  size: %d", depth, "", GET_UINT32 (data));
56   return TRUE;
57 }
58
59 gboolean
60 piffdemux_dump_tfra (GstPiffDemux * piffdemux, GstByteReader * data, int depth)
61 {
62   guint64 time = 0, moof_offset = 0;
63   guint32 len = 0, num_entries = 0, ver_flags = 0, track_id = 0, i;
64   guint value_size, traf_size, trun_size, sample_size;
65
66   if (!gst_byte_reader_get_uint32_be (data, &ver_flags))
67     return FALSE;
68
69   GST_LOG ("%*s  version/flags: %08x", depth, "", ver_flags);
70
71   if (!gst_byte_reader_get_uint32_be (data, &track_id) ||
72       gst_byte_reader_get_uint32_be (data, &len) ||
73       gst_byte_reader_get_uint32_be (data, &num_entries))
74     return FALSE;
75
76   GST_LOG ("%*s  track ID:      %u", depth, "", track_id);
77   GST_LOG ("%*s  length:        0x%x", depth, "", len);
78   GST_LOG ("%*s  n entries:     %u", depth, "", num_entries);
79
80   value_size = ((ver_flags >> 24) == 1) ? sizeof (guint64) : sizeof (guint32);
81   sample_size = (len & 3) + 1;
82   trun_size = ((len & 12) >> 2) + 1;
83   traf_size = ((len & 48) >> 4) + 1;
84
85   if (!piff_atom_parser_has_chunks (data, num_entries,
86           value_size + value_size + traf_size + trun_size + sample_size))
87     return FALSE;
88
89   for (i = 0; i < num_entries; i++) {
90     piff_atom_parser_get_offset (data, value_size, &time);
91     piff_atom_parser_get_offset (data, value_size, &moof_offset);
92     GST_LOG ("%*s    time:          %" G_GUINT64_FORMAT, depth, "", time);
93     GST_LOG ("%*s    moof_offset:   %" G_GUINT64_FORMAT,
94         depth, "", moof_offset);
95     GST_LOG ("%*s    traf_number:   %u", depth, "",
96         piff_atom_parser_get_uint_with_size_unchecked (data, traf_size));
97     GST_LOG ("%*s    trun_number:   %u", depth, "",
98         piff_atom_parser_get_uint_with_size_unchecked (data, trun_size));
99     GST_LOG ("%*s    sample_number: %u", depth, "",
100         piff_atom_parser_get_uint_with_size_unchecked (data, sample_size));
101   }
102
103   return TRUE;
104 }
105
106 gboolean
107 piffdemux_dump_tfhd (GstPiffDemux * piffdemux, GstByteReader * data, int depth)
108 {
109   guint32 flags = 0, n = 0, track_id = 0;
110   guint64 base_data_offset = 0;
111
112   if (!gst_byte_reader_skip (data, 1) ||
113       !gst_byte_reader_get_uint24_be (data, &flags))
114     return FALSE;
115   GST_LOG ("%*s  flags: %08x", depth, "", flags);
116
117   if (!gst_byte_reader_get_uint32_be (data, &track_id))
118     return FALSE;
119   GST_LOG ("%*s  track_id: %u", depth, "", track_id);
120
121   if (flags & TF_BASE_DATA_OFFSET) {
122     if (!gst_byte_reader_get_uint64_be (data, &base_data_offset))
123       return FALSE;
124     GST_LOG ("%*s    base-data-offset: %" G_GUINT64_FORMAT,
125         depth, "", base_data_offset);
126   }
127
128   if (flags & TF_SAMPLE_DESCRIPTION_INDEX) {
129     if (!gst_byte_reader_get_uint32_be (data, &n))
130       return FALSE;
131     GST_LOG ("%*s    sample-description-index: %u", depth, "", n);
132   }
133
134   if (flags & TF_DEFAULT_SAMPLE_DURATION) {
135     if (!gst_byte_reader_get_uint32_be (data, &n))
136       return FALSE;
137     GST_LOG ("%*s    default-sample-duration:  %u", depth, "", n);
138   }
139
140   if (flags & TF_DEFAULT_SAMPLE_SIZE) {
141     if (!gst_byte_reader_get_uint32_be (data, &n))
142       return FALSE;
143     GST_LOG ("%*s    default-sample-size:  %u", depth, "", n);
144   }
145
146   if (flags & TF_DEFAULT_SAMPLE_FLAGS) {
147     if (!gst_byte_reader_get_uint32_be (data, &n))
148       return FALSE;
149     GST_LOG ("%*s    default-sample-flags:  %u", depth, "", n);
150   }
151
152   GST_LOG ("%*s    duration-is-empty:     %s", depth, "",
153       flags & TF_DURATION_IS_EMPTY ? "yes" : "no");
154
155   return TRUE;
156 }
157
158 gboolean
159 piffdemux_dump_trun (GstPiffDemux * piffdemux, GstByteReader * data, int depth)
160 {
161   guint32 flags = 0, samples_count = 0, data_offset = 0, first_sample_flags = 0;
162   guint32 sample_duration = 0, sample_size = 0, sample_flags =
163       0, composition_time_offsets = 0;
164   int i = 0;
165
166   if (!gst_byte_reader_skip (data, 1) ||
167       !gst_byte_reader_get_uint24_be (data, &flags))
168     return FALSE;
169
170   GST_LOG ("%*s  flags: %08x", depth, "", flags);
171
172   if (!gst_byte_reader_get_uint32_be (data, &samples_count))
173     return FALSE;
174   GST_LOG ("%*s  samples_count: %u", depth, "", samples_count);
175
176   if (flags & TR_DATA_OFFSET) {
177     if (!gst_byte_reader_get_uint32_be (data, &data_offset))
178       return FALSE;
179     GST_LOG ("%*s    data-offset: %u", depth, "", data_offset);
180   }
181
182   if (flags & TR_FIRST_SAMPLE_FLAGS) {
183     if (!gst_byte_reader_get_uint32_be (data, &first_sample_flags))
184       return FALSE;
185     GST_LOG ("%*s    first-sample-flags: %u", depth, "", first_sample_flags);
186   }
187
188   for (i = 0; i < samples_count; i++) {
189     if (flags & TR_SAMPLE_DURATION) {
190       if (!gst_byte_reader_get_uint32_be (data, &sample_duration))
191         return FALSE;
192       GST_LOG ("%*s    sample-duration:  %u", depth, "", sample_duration);
193     }
194
195     if (flags & TR_SAMPLE_SIZE) {
196       if (!gst_byte_reader_get_uint32_be (data, &sample_size))
197         return FALSE;
198       GST_LOG ("%*s    sample-size:  %u", depth, "", sample_size);
199     }
200
201     if (flags & TR_SAMPLE_FLAGS) {
202       if (!gst_byte_reader_get_uint32_be (data, &sample_flags))
203         return FALSE;
204       GST_LOG ("%*s    sample-flags:  %u", depth, "", sample_flags);
205     }
206
207     if (flags & TR_COMPOSITION_TIME_OFFSETS) {
208       if (!gst_byte_reader_get_uint32_be (data, &composition_time_offsets))
209         return FALSE;
210       GST_LOG ("%*s    composition_time_offsets:  %u", depth, "",
211           composition_time_offsets);
212     }
213   }
214
215   return TRUE;
216 }
217
218 gboolean
219 piffdemux_dump_trex (GstPiffDemux * piffdemux, GstByteReader * data, int depth)
220 {
221   if (!piff_atom_parser_has_remaining (data, 4 + 4 + 4 + 4 + 4 + 4))
222     return FALSE;
223
224   GST_LOG ("%*s  version/flags: %08x", depth, "", GET_UINT32 (data));
225   GST_LOG ("%*s  track ID:      %08x", depth, "", GET_UINT32 (data));
226   GST_LOG ("%*s  default sample desc. index: %08x", depth, "",
227       GET_UINT32 (data));
228   GST_LOG ("%*s  default sample duration:    %08x", depth, "",
229       GET_UINT32 (data));
230   GST_LOG ("%*s  default sample size:        %08x", depth, "",
231       GET_UINT32 (data));
232   GST_LOG ("%*s  default sample flags:       %08x", depth, "",
233       GET_UINT32 (data));
234
235   return TRUE;
236 }
237
238
239 gboolean
240 piffdemux_dump_sdtp (GstPiffDemux * piffdemux, GstByteReader * data, int depth)
241 {
242   guint32 version;
243   guint8 val;
244   guint i = 1;
245
246   version = GET_UINT32 (data);
247   GST_LOG ("%*s  version/flags: %08x", depth, "", version);
248
249   /* the sample_count is specified in the stsz or stz2 box.
250    * the information for a sample is stored in a single byte,
251    * so we read until there are no remaining bytes */
252   while (piff_atom_parser_has_remaining (data, 1)) {
253     val = GET_UINT8 (data);
254     GST_LOG ("%*s     sample number: %d", depth, "", i);
255     GST_LOG ("%*s     sample_depends_on: %d", depth, "",
256         ((guint16) (val)) & 0x3);
257     GST_LOG ("%*s     sample_is_depended_on: %d", depth, "",
258         ((guint16) (val >> 2)) & 0x3);
259     GST_LOG ("%*s     sample_has_redundancy: %d", depth, "",
260         ((guint16) (val >> 4)) & 0x3);
261     ++i;
262   }
263   return TRUE;
264 }
265
266 gboolean
267 piffdemux_dump_unknown (GstPiffDemux * piffdemux, GstByteReader * data, int depth)
268 {
269   int len;
270
271   len = gst_byte_reader_get_remaining (data);
272   GST_LOG ("%*s  length: %d", depth, "", len);
273
274   GST_MEMDUMP_OBJECT (piffdemux, "unknown atom data",
275       gst_byte_reader_peek_data_unchecked (data), len);
276   return TRUE;
277 }
278
279 static gboolean
280 piffdemux_node_dump_foreach (GNode * node, gpointer piffdemux)
281 {
282   GstByteReader parser;
283   guint8 *buffer = (guint8 *) node->data;       /* FIXME: move to byte reader */
284   guint32 node_length;
285   guint32 fourcc;
286   const PiffNodeType *type;
287   int depth;
288
289   node_length = GST_READ_UINT32_BE (buffer);
290   fourcc = GST_READ_UINT32_LE (buffer + 4);
291
292   g_warn_if_fail (node_length >= 8);
293
294   gst_byte_reader_init (&parser, buffer + 8, node_length - 8);
295
296   type = piffdemux_type_get (fourcc);
297
298   depth = (g_node_depth (node) - 1) * 2;
299   GST_LOG ("%*s'%" GST_FOURCC_FORMAT "', [%d], %s",
300       depth, "", GST_FOURCC_ARGS (fourcc), node_length, type->name);
301
302   if (type->dump) {
303     gboolean ret;
304
305     ret = type->dump (GST_PIFFDEMUX_CAST (piffdemux), &parser, depth);
306
307     if (!ret) {
308       GST_WARNING ("%*s  not enough data parsing atom %" GST_FOURCC_FORMAT,
309           depth, "", GST_FOURCC_ARGS (fourcc));
310     }
311   }
312
313   return FALSE;
314 }
315
316 gboolean
317 piffdemux_node_dump (GstPiffDemux * piffdemux, GNode * node)
318 {
319   if (__gst_debug_min < GST_LEVEL_LOG)
320     return TRUE;
321
322   g_node_traverse (node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
323       piffdemux_node_dump_foreach, piffdemux);
324   return TRUE;
325 }