Add phase/magnitude objective output types
[xiph/unicode.git] / w3d / tarkin_sdl_player.c
blob8921fb4fe384be6464448c1e5a96ca5665568be9
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <stdint.h>
6 #include "mem.h"
7 #include "tarkin.h"
8 #include "pnm.h"
9 #include <ogg/ogg.h>
11 #include <SDL/SDL.h>
13 #define CHUNK_SIZE 4096
15 int main (int argc, char **argv)
17 char *fname = "stream.ogg";
18 uint32_t frame = 0;
19 uint8_t *rgb;
20 int fd;
21 TarkinStream *tarkin_stream;
22 int nread;
23 int nheader = 0;
24 ogg_sync_state oy;
25 ogg_stream_state os;
26 ogg_page og;
27 ogg_packet op;
28 TarkinInfo ti;
29 TarkinComment tc;
30 TarkinTime date;
31 SDL_Surface * vid_surface = NULL;
33 char *buffer;
35 TarkinVideoLayerDesc *layer;
37 if (argc == 2) {
38 fname = argv[1];
39 } else if (argc != 1) {
40 printf ("\n usage: %s <tarkin_stream>\n\n", argv[0]);
41 exit (-1);
44 if ((fd = open (fname, O_RDONLY)) < 0) {
45 printf ("error opening '%s'\n", fname);
46 exit (-1);
49 // init SDL stuff
50 if (SDL_Init(SDL_INIT_VIDEO)) {
51 atexit(SDL_Quit);
54 tarkin_stream = tarkin_stream_new (fd);
55 ogg_sync_init(&oy);
56 ogg_stream_init(&os,1);
57 tarkin_info_init(&ti);
58 tarkin_comment_init(&tc);
59 while(1){
60 buffer = ogg_sync_buffer(&oy, CHUNK_SIZE);
61 if((nread = read(fd, buffer, CHUNK_SIZE))>0)
62 ogg_sync_wrote(&oy, nread);
63 else{
64 ogg_sync_wrote(&oy,0);
66 if(ogg_sync_pageout(&oy,&og)){
67 ogg_stream_pagein(&os,&og);
68 while(ogg_stream_packetout(&os,&op)){
69 if(op.e_o_s)
70 break;
71 if(nheader<3){ /* 3 first packets to headerin */
72 tarkin_synthesis_headerin(&ti, &tc, &op);
73 if(nheader == 2){
74 tarkin_synthesis_init(tarkin_stream, &ti);
76 nheader++;
77 } else {
78 tarkin_synthesis_packetin(tarkin_stream, &op);
79 while(tarkin_synthesis_frameout(tarkin_stream, &rgb, 0, &date)==0){
80 layer = &tarkin_stream->layer->desc;
82 // if SDL_initialized != true, init sdl stuff
83 if (!vid_surface)
85 #ifdef DEBUG
86 printf("initializing sdl surface...");
87 #endif
88 if ((vid_surface = SDL_SetVideoMode(layer->width, layer->height, 24, SDL_SWSURFACE)) == NULL) {
89 tarkin_synthesis_freeframe(tarkin_stream, rgb);
90 tarkin_stream_destroy (tarkin_stream);
91 close (fd);
92 exit(1);
96 if (SDL_MUSTLOCK(vid_surface))
97 SDL_LockSurface(vid_surface);
99 memcpy(vid_surface->pixels, rgb, layer->width * layer->height * 3); //...
101 if (SDL_MUSTLOCK(vid_surface))
102 SDL_UnlockSurface(vid_surface);
104 SDL_UpdateRect(vid_surface, 0, 0, 0, 0);
106 sk : removed this stuff
107 snprintf(ofname, 11, layer->format == TARKIN_GRAYSCALE ? "out%03d.pgm" : "out%03d.ppm", frame);
108 printf ("write '%s' %dx%d\n", ofname, layer->width, layer->height);
109 write_pnm (ofname, rgb, layer->width, layer->height);
111 tarkin_synthesis_freeframe(tarkin_stream, rgb);
113 frame ++;
118 if(nread==0)
119 break;
121 tarkin_stream_destroy (tarkin_stream);
122 close (fd);
124 return 0;