]> git.sesse.net Git - ffmpeg/blob - cws2fws.c
bring back h264 build
[ffmpeg] / cws2fws.c
1 /*
2  * cws2fws by Alex Beregszaszi
3  * Public domain.
4  *
5  * This utility converts compressed Macromedia Flash files to uncompressed ones.
6  */
7
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <zlib.h>
14
15 #ifdef DEBUG
16 #define dbgprintf printf
17 #else
18 #define dbgprintf
19 #endif
20
21 int main(int argc, char *argv[])
22 {
23     int fd_in, fd_out, comp_len, uncomp_len, i, last_out;
24     char buf_in[1024], buf_out[65536];
25     z_stream zstream;
26     struct stat statbuf;
27
28     if (argc < 3)
29     {
30         printf("Usage: %s <infile.swf> <outfile.swf>\n", argv[0]);
31         exit(1);
32     }
33
34     fd_in = open(argv[1], O_RDONLY);
35     if (fd_in < 0)
36     {
37         perror("Error while opening: ");
38         exit(1);
39     }
40
41     fd_out = open(argv[2], O_WRONLY|O_CREAT, 00644);
42     if (fd_out < 0)
43     {
44         perror("Error while opening: ");
45         close(fd_in);
46         exit(1);
47     }
48
49     if (read(fd_in, &buf_in, 8) != 8)
50     {
51         printf("Header error\n");
52         close(fd_in);
53         close(fd_out);
54         exit(1);
55     }
56
57     if (buf_in[0] != 'C' || buf_in[1] != 'W' || buf_in[2] != 'S')
58     {
59         printf("Not a compressed flash file\n");
60         exit(1);
61     }
62
63     fstat(fd_in, &statbuf);
64     comp_len = statbuf.st_size;
65     uncomp_len = buf_in[4] | (buf_in[5] << 8) | (buf_in[6] << 16) | (buf_in[7] << 24);
66
67     printf("Compressed size: %d Uncompressed size: %d\n", comp_len-4, uncomp_len-4);
68
69     // write out modified header
70     buf_in[0] = 'F';
71     write(fd_out, &buf_in, 8);
72
73     zstream.zalloc = NULL;
74     zstream.zfree = NULL;
75     zstream.opaque = NULL;
76     inflateInit(&zstream);
77
78     for (i = 0; i < comp_len-8;)
79     {
80         int ret, len = read(fd_in, &buf_in, 1024);
81
82         dbgprintf("read %d bytes\n", len);
83
84         last_out = zstream.total_out;
85
86         zstream.next_in = &buf_in[0];
87         zstream.avail_in = len;
88         zstream.next_out = &buf_out[0];
89         zstream.avail_out = 65536;
90
91         ret = inflate(&zstream, Z_SYNC_FLUSH);
92         if (ret != Z_STREAM_END && ret != Z_OK)
93         {
94             printf("Error while decompressing: %d\n", ret);
95             inflateEnd(&zstream);
96             exit(1);
97         }
98
99         dbgprintf("a_in: %d t_in: %d a_out: %d t_out: %d -- %d out\n",
100             zstream.avail_in, zstream.total_in, zstream.avail_out, zstream.total_out,
101             zstream.total_out-last_out);
102
103         write(fd_out, &buf_out, zstream.total_out-last_out);
104
105         i += len;
106
107         if (ret == Z_STREAM_END || ret == Z_BUF_ERROR)
108             break;
109     }
110
111     if (zstream.total_out != uncomp_len-8)
112     {
113         printf("Size mismatch (%d != %d), updating header...\n",
114             zstream.total_out, uncomp_len-8);
115
116         buf_in[0] = (zstream.total_out+8) & 0xff;
117         buf_in[1] = (zstream.total_out+8 >> 8) & 0xff;
118         buf_in[2] = (zstream.total_out+8 >> 16) & 0xff;
119         buf_in[3] = (zstream.total_out+8 >> 24) & 0xff;
120
121         lseek(fd_out, 4, SEEK_SET);
122         write(fd_out, &buf_in, 4);
123     }
124
125     inflateEnd(&zstream);
126     close(fd_in);
127     close(fd_out);
128     return 0;
129 }