pass the context when popping blocks
[swfdec.git] / tools / dump.c
blobeb56c4b63f231ac2bd4097b6d7260e64a2660de9
1 /* Swfdec
2 * Copyright (C) 2003-2006 David Schleef <ds@schleef.org>
3 * 2005-2006 Eric Anholt <eric@anholt.net>
4 * 2006-2007 Benjamin Otte <otte@gnome.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
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 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <stdio.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <sys/stat.h>
30 #include <glib.h>
31 #include <glib-object.h>
32 #include <swfdec/swfdec.h>
33 #include <swfdec/swfdec_audio_decoder.h>
34 #include <swfdec/swfdec_button.h>
35 #include <swfdec/swfdec_text_field.h>
36 #include <swfdec/swfdec_font.h>
37 #include <swfdec/swfdec_image.h>
38 #include <swfdec/swfdec_movie.h>
39 #include <swfdec/swfdec_player_internal.h>
40 #include <swfdec/swfdec_sprite.h>
41 #include <swfdec/swfdec_shape.h>
42 #include <swfdec/swfdec_sound.h>
43 #include <swfdec/swfdec_swf_decoder.h>
44 #include <swfdec/swfdec_resource.h>
45 #include <swfdec/swfdec_tag.h>
46 #include <swfdec/swfdec_text.h>
48 static gboolean verbose = FALSE;
50 static const char *
51 get_audio_format_name (guint codec)
53 switch (codec) {
54 case SWFDEC_AUDIO_CODEC_ADPCM:
55 return "ADPCM";
56 case SWFDEC_AUDIO_CODEC_MP3:
57 return "MP3";
58 case SWFDEC_AUDIO_CODEC_UNCOMPRESSED:
59 return "uncompressed";
60 case SWFDEC_AUDIO_CODEC_NELLYMOSER:
61 return "Nellymoser";
62 default:
63 return "Unknown";
67 static void
68 dump_sound (SwfdecSound *sound)
70 g_print (" codec: %s\n", get_audio_format_name (sound->codec));
71 if (verbose) {
72 g_print (" format: %s\n", swfdec_audio_format_to_string (sound->format));
73 g_print (" samples: %u (%gs)\n", sound->n_samples,
74 (double) sound->n_samples / swfdec_audio_format_get_rate (sound->format));
78 static void
79 dump_sprite (SwfdecSwfDecoder *dec, SwfdecSprite *s)
81 if (!verbose) {
82 g_print (" %u frames\n", s->n_frames);
83 } else {
84 guint i, j, tag;
85 SwfdecBuffer *buffer;
87 j = 0;
88 for (i = 0; ; i++) {
89 if (!swfdec_sprite_get_action (s, i, &tag, &buffer))
90 break;
91 switch (tag) {
92 case SWFDEC_TAG_DOACTION:
93 g_print (" %4u script\n", j);
94 break;
95 case SWFDEC_TAG_PLACEOBJECT2:
96 case SWFDEC_TAG_PLACEOBJECT3:
98 SwfdecBits bits;
99 gboolean has_char, is_move;
100 guint depth;
102 swfdec_bits_init (&bits, buffer);
103 swfdec_bits_getbits (&bits, 6);
104 has_char = swfdec_bits_getbit (&bits);
105 is_move = swfdec_bits_getbit (&bits);
106 if (tag == SWFDEC_TAG_PLACEOBJECT3)
107 swfdec_bits_get_u8 (&bits);
108 depth = swfdec_bits_get_u16 (&bits);
109 g_print (" %4u %5u %s", j, depth, is_move ? "move" : "place");
110 if (has_char) {
111 SwfdecCharacter *c;
112 c = swfdec_swf_decoder_get_character (dec, swfdec_bits_get_u16 (&bits));
113 if (c)
114 g_print (" %s %u", G_OBJECT_TYPE_NAME (c), c->id);
116 g_print ("\n");
118 break;
119 case SWFDEC_TAG_REMOVEOBJECT:
120 case SWFDEC_TAG_REMOVEOBJECT2:
122 SwfdecBits bits;
123 swfdec_bits_init (&bits, buffer);
124 if (tag == SWFDEC_TAG_REMOVEOBJECT)
125 swfdec_bits_get_u16 (&bits);
126 g_print (" %4u %5u remove\n", j, swfdec_bits_get_u16 (&bits));
128 break;
129 case SWFDEC_TAG_SHOWFRAME:
130 j++;
131 break;
132 case SWFDEC_TAG_STARTSOUND:
133 /* FIXME add info about what sound etc */
134 g_print (" %4u start sound\n", j);
135 break;
136 case SWFDEC_TAG_EXPORTASSETS:
137 g_print (" %4u export\n", j);
138 break;
139 case SWFDEC_TAG_DOINITACTION:
140 g_print (" %4u init action\n", j);
141 break;
142 case SWFDEC_TAG_SETBACKGROUNDCOLOR:
143 g_print (" %4u background color\n", j);
144 break;
145 case SWFDEC_TAG_SOUNDSTREAMHEAD:
146 /* FIXME */
147 g_print (" %4u sound stream\n", j);
148 break;
149 case SWFDEC_TAG_SOUNDSTREAMHEAD2:
150 case SWFDEC_TAG_SOUNDSTREAMBLOCK:
151 break;
152 default:
153 g_assert_not_reached ();
159 static void
160 dump_path (cairo_path_t *path)
162 int i;
163 cairo_path_data_t *data = path->data;
164 const char *name;
166 for (i = 0; i < path->num_data; i++) {
167 name = NULL;
168 switch (data[i].header.type) {
169 case CAIRO_PATH_CURVE_TO:
170 g_print (" curve %g %g (%g %g . %g %g)\n",
171 data[i + 3].point.x, data[i + 3].point.y,
172 data[i + 1].point.x, data[i + 1].point.y,
173 data[i + 2].point.x, data[i + 2].point.y);
174 i += 3;
175 break;
176 case CAIRO_PATH_LINE_TO:
177 name = "line ";
178 case CAIRO_PATH_MOVE_TO:
179 if (!name)
180 name = "move ";
181 i++;
182 g_print (" %s %g %g\n", name, data[i].point.x, data[i].point.y);
183 break;
184 case CAIRO_PATH_CLOSE_PATH:
185 g_print (" close\n");
186 break;
187 default:
188 g_assert_not_reached ();
189 break;
194 static void
195 dump_shape (SwfdecShape *shape)
197 GSList *walk;
199 for (walk = shape->draws; walk; walk = walk->next) {
200 if (SWFDEC_IS_PATTERN (walk->data)) {
201 SwfdecPattern *pattern = walk->data;
202 char *str = swfdec_pattern_to_string (pattern);
203 g_print ("%s\n", str);
204 g_free (str);
205 if (verbose) {
206 g_print (" %g %g %g %g %g %g\n",
207 pattern->start_transform.xx, pattern->start_transform.xy,
208 pattern->start_transform.yx, pattern->start_transform.yy,
209 pattern->start_transform.x0, pattern->start_transform.y0);
211 } else if (SWFDEC_IS_STROKE (walk->data)) {
212 SwfdecStroke *line = walk->data;
213 g_print ("line (width %u, color #%08X)\n", line->start_width, line->start_color);
214 } else {
215 g_print ("not filled\n");
217 if (verbose) {
218 dump_path (&SWFDEC_DRAW (walk->data)->path);
223 static void
224 dump_text_field (SwfdecTextField *text)
226 g_print (" %s\n", text->input ? text->input : "");
227 if (verbose) {
228 if (text->variable)
229 g_print (" variable %s\n", text->variable);
230 else
231 g_print (" no variable\n");
235 static void
236 dump_text (SwfdecText *text)
238 guint i;
239 gunichar2 uni[text->glyphs->len];
240 char *s;
242 for (i = 0; i < text->glyphs->len; i++) {
243 SwfdecTextGlyph *glyph = &g_array_index (text->glyphs, SwfdecTextGlyph, i);
244 uni[i] = g_array_index (glyph->font->glyphs, SwfdecFontEntry, glyph->glyph).value;
245 if (uni[i] == 0)
246 goto fallback;
248 s = g_utf16_to_utf8 (uni, text->glyphs->len, NULL, NULL, NULL);
249 if (s == NULL)
250 goto fallback;
251 g_print (" text: %s\n", s);
252 g_free (s);
253 return;
255 fallback:
256 g_print (" %u characters\n", text->glyphs->len);
259 static void
260 dump_font (SwfdecFont *font)
262 unsigned int i;
263 if (font->name)
264 g_print (" %s\n", font->name);
265 g_print (" %u characters\n", font->glyphs->len);
266 if (verbose) {
267 for (i = 0; i < font->glyphs->len; i++) {
268 gunichar2 c = g_array_index (font->glyphs, SwfdecFontEntry, i).value;
269 char *s;
270 if (c == 0 || (s = g_utf16_to_utf8 (&c, 1, NULL, NULL, NULL)) == NULL) {
271 g_print (" ");
272 } else {
273 g_print ("%s ", s);
274 g_free (s);
277 g_print ("\n");
281 static void
282 dump_button (SwfdecButton *button)
286 static const char *
287 get_image_type_name (SwfdecImageType type)
289 switch (type) {
290 case SWFDEC_IMAGE_TYPE_JPEG:
291 return "JPEG with global table";
292 case SWFDEC_IMAGE_TYPE_JPEG2:
293 return "JPEG";
294 case SWFDEC_IMAGE_TYPE_JPEG3:
295 return "JPEG with alpha";
296 case SWFDEC_IMAGE_TYPE_LOSSLESS:
297 return "lossless";
298 case SWFDEC_IMAGE_TYPE_LOSSLESS2:
299 return "lossless with alpha";
300 case SWFDEC_IMAGE_TYPE_PNG:
301 return "PNG";
302 case SWFDEC_IMAGE_TYPE_UNKNOWN:
303 default:
304 g_assert_not_reached ();
305 return "Unknown";
309 static void
310 dump_image (SwfdecImage *image)
312 cairo_surface_destroy (swfdec_image_create_surface (image, NULL));
313 g_print (" %s %u x %u\n", get_image_type_name (image->type),
314 image->width, image->height);
317 static void
318 dump_object (gpointer value, gpointer dec)
320 SwfdecCharacter *c = value;
322 g_print ("%d: %s\n", c->id, G_OBJECT_TYPE_NAME (c));
323 if (verbose && SWFDEC_IS_GRAPHIC (c)) {
324 SwfdecGraphic *graphic = SWFDEC_GRAPHIC (c);
325 g_print (" extents: %g %g %g %g\n", graphic->extents.x0, graphic->extents.y0,
326 graphic->extents.x1, graphic->extents.y1);
328 if (SWFDEC_IS_IMAGE (c)) {
329 dump_image (SWFDEC_IMAGE (c));
331 if (SWFDEC_IS_SPRITE (c)) {
332 dump_sprite (dec, SWFDEC_SPRITE (c));
334 if (SWFDEC_IS_SHAPE(c)) {
335 dump_shape(SWFDEC_SHAPE(c));
337 if (SWFDEC_IS_TEXT (c)) {
338 dump_text (SWFDEC_TEXT (c));
340 if (SWFDEC_IS_TEXT_FIELD (c)) {
341 dump_text_field (SWFDEC_TEXT_FIELD (c));
343 if (SWFDEC_IS_FONT (c)) {
344 dump_font (SWFDEC_FONT (c));
346 if (SWFDEC_IS_BUTTON (c)) {
347 dump_button (SWFDEC_BUTTON (c));
349 if (SWFDEC_IS_SOUND (c)) {
350 dump_sound (SWFDEC_SOUND (c));
354 static void
355 enqueue (gpointer key, gpointer value, gpointer listp)
357 GList **list = listp;
359 *list = g_list_prepend (*list, value);
362 static int
363 sort_by_id (gconstpointer a, gconstpointer b)
365 if (SWFDEC_CHARACTER (a)->id < SWFDEC_CHARACTER (b)->id)
366 return -1;
367 return 1;
371 main (int argc, char *argv[])
373 SwfdecSwfDecoder *s;
374 SwfdecPlayer *player;
375 GError *error = NULL;
376 GOptionEntry options[] = {
377 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "bew verbose", NULL },
378 { NULL }
380 GOptionContext *ctx;
381 GList *list = NULL;
382 SwfdecURL *url;
384 ctx = g_option_context_new ("");
385 g_option_context_add_main_entries (ctx, options, "options");
386 g_option_context_parse (ctx, &argc, &argv, &error);
387 g_option_context_free (ctx);
388 if (error) {
389 g_printerr ("Error parsing command line arguments: %s\n", error->message);
390 g_error_free (error);
391 return 1;
394 swfdec_init();
396 if(argc < 2){
397 g_print ("usage: %s [OPTIONS] file\n", argv[0]);
398 return 0;
401 player = swfdec_player_new (NULL);
402 url = swfdec_url_new_from_input (argv[1]);
403 swfdec_player_set_url (player, url);
404 swfdec_url_free (url);
405 /* FIXME: HACK! */
406 swfdec_player_advance (player, 0);
407 if (!swfdec_player_is_initialized (player)) {
408 g_printerr ("File \"%s\" is not a SWF file\n", argv[1]);
409 g_object_unref (player);
410 player = NULL;
411 return 1;
413 s = (SwfdecSwfDecoder *) SWFDEC_MOVIE (player->priv->roots->data)->resource->decoder;
414 /* FIXME: can happen after a _root.loadMovie() call */
415 if (!SWFDEC_IS_SWF_DECODER (s)) {
416 g_printerr ("Movie already unloaded from \"%s\"\n", argv[1]);
417 g_object_unref (player);
418 player = NULL;
419 return 1;
422 g_print ("file:\n");
423 g_print (" version: %d\n", s->version);
424 g_print (" rate : %g fps\n", SWFDEC_DECODER (s)->rate / 256.0);
425 g_print (" size : %ux%u pixels\n", SWFDEC_DECODER (s)->width, SWFDEC_DECODER (s)->height);
426 g_print ("objects:\n");
427 g_hash_table_foreach (s->characters, enqueue, &list);
428 list = g_list_sort (list, sort_by_id);
429 g_list_foreach (list, dump_object, s);
430 g_list_free (list);
432 g_print ("main sprite:\n");
433 dump_sprite (s, s->main_sprite);
434 g_object_unref (player);
435 s = NULL;
436 player = NULL;
438 return 0;