-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
play_smjpeg.c
177 lines (161 loc) · 5.15 KB
/
play_smjpeg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* This file plays SMJPEG format Motion JPEG movies */
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
/* Define this if you actually want to play the file */
#define PLAY_SMJPEG
#include "smjpeg_decode.h"
void Usage(const char *argv0)
{
printf("SMJPEG " VERSION " decoder, Loki Entertainment Software and Fat N Soft\n");
printf("Usage: %s [-2] [-l] [-f] [-v] file.mjpg [file.mjpg ...]\n", argv0);
printf("-2 is double size video.\n");
printf("-l is loop video playback.\n");
printf("-f is fullscreen playback.\n");
printf("-v displays version.\n");
}
int main(int argc, char *argv[])
{
SMJPEG movie;
int i;
int doubleflag;
int loopflag;
int fullflag;
int bpp;
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Couldn't init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
doubleflag = 0;
loopflag = 0;
fullflag = 0;
bpp = 16;
for ( i=1; argv[i]; ++i ) {
if ( (strcmp(argv[i], "-h") == 0) ||
(strcmp(argv[i], "--help") == 0) ) {
Usage(argv[0]);
exit(0);
}
if ( strcmp(argv[i], "-2") == 0 ) {
doubleflag = !doubleflag;
continue;
}
if ( strcmp(argv[i], "-l") == 0 ) {
loopflag = !loopflag;
continue;
}
if ( strcmp(argv[i], "-f") == 0 ) {
fullflag = SDL_FULLSCREEN;
continue;
}
if ( strcmp(argv[i], "-bpp") == 0 ) {
i ++;
bpp = atoi(argv[i]);
continue;
}
if ( strcmp(argv[i], "-v") == 0 ) {
printf("SMJPEG " VERSION " decoder, Loki Entertainment Software and Fat N Soft\n");
continue;
}
/* Load and play the animation */
if ( SMJPEG_load(&movie, argv[i]) < 0 ) {
fprintf(stderr, "%s\n", movie.status.message);
continue;
}
/* Print out information about the file */
if ( movie.audio.enabled ) {
printf("Audio stream: %d bit %s audio at %d Hz\n",
movie.audio.bits,
(movie.audio.channels == 1) ? "mono" : "stereo",
movie.audio.rate);
}
if ( movie.video.enabled ) {
printf("Video stream: %d frames of %dx%d animation\n",
movie.video.frames, movie.video.width, movie.video.height);
}
#ifdef PLAY_SMJPEG
if ( movie.video.enabled ) {
SDL_Surface *screen;
int width, height;
width = movie.video.width;
height = movie.video.height;
if ( doubleflag ) {
width *= 2;
height *= 2;
}
screen = SDL_SetVideoMode(width, height, bpp, SDL_SWSURFACE|fullflag);
if ( screen == NULL ) {
fprintf(stderr, "Couldn't set %dx%d 16-bit video mode: %s\n",
SDL_GetError());
continue;
}
if (bpp == 24)
{
screen->format->Rmask = 0xFF;
screen->format->Gmask = 0xFF00;
screen->format->Bmask = 0xFF0000;
}
SMJPEG_double(&movie, doubleflag);
SMJPEG_target(&movie, NULL, 0, 0, screen, SDL_UpdateRect);
}
if ( movie.audio.enabled ) {
SDL_AudioSpec spec;
spec.freq = movie.audio.rate;
switch (movie.audio.bits) {
case 8:
spec.format = AUDIO_U8;
break;
case 16:
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
spec.format = AUDIO_S16LSB;
#else
spec.format = AUDIO_S16MSB;
#endif
break;
default:
/* Uh oh... */
fprintf(stderr, "Unknown audio format in SMJPEG\n");
spec.format = 0;
break;
}
spec.channels = movie.audio.channels;
spec.samples = 512;
spec.callback = SMJPEG_feedaudio;
spec.userdata = &movie;
if ( SDL_OpenAudio(&spec, NULL) < 0 ) {
movie.audio.enabled = 0;
} else {
SDL_PauseAudio(0);
}
}
do {
SMJPEG_start(&movie, 1);
while ( ! movie.at_end ) {
SDL_Event event;
SMJPEG_advance(&movie, 1, 1);
if ( SDL_PollEvent(&event) ) {
switch(event.type) {
case SDL_KEYDOWN:
case SDL_QUIT:
loopflag = 0;
SMJPEG_stop(&movie);
break;
}
}
}
SMJPEG_stop(&movie);
if ( loopflag ) {
SMJPEG_rewind(&movie);
}
} while ( loopflag );
if ( movie.audio.enabled ) {
SDL_CloseAudio();
}
SMJPEG_free(&movie);
#endif /* PLAY_SMJPEG */
}
}