Recognizes if input is ogg or not.
[xiph/unicode.git] / w3d / tarkin_dec.c
blob3920da167925a338c74e8499e8b613e1b370c6c9
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include "w3dtypes.h"
5 #include "mem.h"
6 #include "tarkin.h"
7 #include "pnm.h"
8 #include <ogg/ogg.h>
10 #define CHUNK_SIZE 4096
12 int main (int argc, char **argv)
14 char *fname = "stream.ogg";
15 char ofname [11];
16 uint32_t frame = 0;
17 uint8_t *rgb;
18 int fd;
19 TarkinStream *tarkin_stream;
20 int nread;
21 int nheader = 0;
22 ogg_sync_state oy;
23 ogg_stream_state os;
24 ogg_page og;
25 ogg_packet op;
26 TarkinInfo ti;
27 TarkinComment tc;
28 TarkinTime date;
30 char *buffer;
32 TarkinVideoLayerDesc *layer;
34 if (argc == 2) {
35 fname = argv[1];
36 } else if (argc != 1) {
37 printf ("\n usage: %s <tarkin_stream>\n\n", argv[0]);
38 exit (-1);
41 if ((fd = open (fname, O_RDONLY|O_BINARY)) < 0) {
42 printf ("error opening '%s'\n", fname);
43 exit (-1);
46 tarkin_stream = tarkin_stream_new (fd);
47 ogg_sync_init(&oy);
48 ogg_stream_init(&os,1);
49 tarkin_info_init(&ti);
50 tarkin_comment_init(&tc);
51 while(1){
52 buffer = ogg_sync_buffer(&oy, CHUNK_SIZE);
53 if((nread = read(fd, buffer, CHUNK_SIZE))>0)
54 ogg_sync_wrote(&oy, nread);
55 else{
56 ogg_sync_wrote(&oy,0);
58 if(ogg_sync_pageout(&oy,&og)){
59 ogg_stream_pagein(&os,&og);
60 while(ogg_stream_packetout(&os,&op)){
61 if(op.e_o_s)
62 break;
63 if(nheader<3){ /* 3 first packets to headerin */
64 tarkin_synthesis_headerin(&ti, &tc, &op);
65 if(nheader == 2){
66 tarkin_synthesis_init(tarkin_stream, &ti);
68 nheader++;
69 } else {
70 tarkin_synthesis_packetin(tarkin_stream, &op);
71 while(tarkin_synthesis_frameout(tarkin_stream, &rgb, 0, &date)==0){
72 layer = &tarkin_stream->layer->desc;
73 snprintf(ofname, 11, layer->format == TARKIN_GRAYSCALE ? "out%03d.pgm" : "out%03d.ppm", frame);
74 printf ("write '%s' %dx%d\n", ofname, layer->width, layer->height);
75 write_pnm (ofname, rgb, layer->width, layer->height);
76 tarkin_synthesis_freeframe(tarkin_stream, rgb);
78 frame ++;
83 if(nread==0)
84 break;
86 tarkin_stream_destroy (tarkin_stream);
87 close (fd);
89 return 0;