-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smjpeg_encode.c
382 lines (341 loc) · 11.6 KB
/
smjpeg_encode.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* This file takes a stream of raw audio and a series of jpeg images
and constructs a motion jpeg animation from it.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <string.h>
#include <jpeglib.h>
#include "adpcm.h"
#include "smjpeg_file.h"
/* Default audio encoding parameters */
#define DEFAULT_AUDIO_ENCODING AUDIO_ENCODING_ADPCM
#define DEFAULT_AUDIO_RATE 22050
#define DEFAULT_AUDIO_BITS 16
#define DEFAULT_AUDIO_CHANNELS 1
#define DEFAULT_AUDIO_FRAME 512 /* Encode 512 samples per frame */
/* Default video encoding parameters */
#define DEFAULT_VIDEO_ENCODING VIDEO_ENCODING_JPEG
#define DEFAULT_VIDEO_FPS 15.0
#define DEFAULT_JPEG_PREFIX "frame."
#define DEFAULT_AUDIO_INPUT "audio.raw"
#define DEFAULT_OUTPUT_FILE "output.mjpg"
typedef unsigned char Uint8;
typedef unsigned short Uint16;
typedef unsigned int Uint32;
/* Open a JPEG file and get the image width and height */
int get_jpeg_dimensions(const char *file, Uint16 *w, Uint16 *h)
{
FILE *input;
int status;
status = -1;
input = fopen(file, "rb");
if ( input ) {
struct jpeg_error_mgr errmgr;
struct jpeg_decompress_struct cinfo;
cinfo.err = jpeg_std_error(&errmgr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, input);
jpeg_read_header(&cinfo, TRUE);
cinfo.out_color_space = JCS_RGB;
jpeg_calc_output_dimensions(&cinfo);
*w = cinfo.output_width;
*h = cinfo.output_height;
jpeg_destroy_decompress(&cinfo);
status = 0;
}
return(status);
}
int WriteAudioChunk(FILE *input, double timestamp, Uint32 size,
const char *encoding, FILE *output, Uint8 channels, void* data)
{
Uint8 buffer[BUFSIZ];
int len;
int i;
//fprintf(stderr, "A");
fwrite(AUDIO_DATA_MAGIC, 4, 1, output);
WRITE32((Uint32)timestamp, output);
if ( MAGIC_EQUALS(encoding, AUDIO_ENCODING_ADPCM) )
{
struct adpcm_state *state = (struct adpcm_state*) data;
WRITE32((channels*4)+(size/4), output);
for (i = 0; i < channels; i++)
{
WRITE16(state[i].valprev, output);
WRITE8(state[i].index, output);
WRITE8(0, output);
}
} else {
WRITE32(size, output);
}
while ( size > 0 ) {
if ( size < BUFSIZ ) {
len = fread(buffer, 1, size, input);
} else {
len = fread(buffer, 1, BUFSIZ, input);
}
if ( len < 0 ) {
if ( ferror(input) ) {
fprintf(stderr, "Error reading input data\n");
}
return(-1);
}
size -= len;
if ( MAGIC_EQUALS(encoding, AUDIO_ENCODING_ADPCM) ) {
Uint8 encoded[BUFSIZ];
SMJPEG_adpcm_coder((short *)buffer, encoded, len/2, channels, (struct adpcm_state*) data);
fwrite(encoded, len/4, 1, output);
} else {
fwrite(buffer, len, 1, output);
}
}
return(0);
}
int WriteVideoChunk(FILE *input, double timestamp, Uint32 size,
const char *encoding, FILE *output)
{
Uint8 buffer[BUFSIZ];
int len;
//fprintf(stderr, "V");
fwrite(VIDEO_DATA_MAGIC, 4, 1, output);
WRITE32((Uint32)timestamp, output);
WRITE32(size, output);
while ( size > 0 ) {
if ( size < BUFSIZ ) {
len = fread(buffer, 1, size, input);
} else {
len = fread(buffer, 1, BUFSIZ, input);
}
if ( len < 0 ) {
if ( ferror(input) ) {
fprintf(stderr, "Error reading input data\n");
}
return(-1);
}
size -= len;
fwrite(buffer, len, 1, output);
}
return(0);
}
void Usage(const char *argv0)
{
printf("SMJPEG " VERSION " encoder, Loki Entertainment Software and Fat N Soft\n");
printf("Usage: %s [-r fps] [-c channels] [-n input] [-1]\n", argv0);
printf("If no FPS is given it will calculate it based on the nubmer of video frames and length of audio\n");
}
int main(int argc, char *argv[])
{
const Uint8 smjpeg_magic[] = { '\0', '\n', 'S','M','J','P','E','G' };
Uint16 audio_rate;
Uint8 audio_bits;
Uint8 audio_channels;
char audio_encoding[5];
double video_fps;
Uint32 video_nframes;
Uint16 video_width;
Uint16 video_height;
char video_encoding[5];
int index;
struct stat sb;
char jpegprefix[PATH_MAX], jpegfile[PATH_MAX];
char audiofile[PATH_MAX];
char outputfile[PATH_MAX];
FILE *jpeginput, *audioinput, *output;
Uint32 audio_left;
Uint32 audio_framesize;
Uint32 video_framesize;
double ms_per_audio_frame;
double ms_per_video_frame;
double audio_time, video_time;
int status;
int fps_set;
void *audio_data;
char* input_names[256];
/* First, set default encoding parameters */
audio_rate = DEFAULT_AUDIO_RATE;
audio_bits = DEFAULT_AUDIO_BITS;
audio_channels = DEFAULT_AUDIO_CHANNELS;
strcpy(audio_encoding, DEFAULT_AUDIO_ENCODING);
video_fps = DEFAULT_VIDEO_FPS;
video_nframes = 0;
video_width = 0;
video_height = 0;
strcpy(video_encoding, DEFAULT_VIDEO_ENCODING);
strcpy(jpegprefix, DEFAULT_JPEG_PREFIX);
strcpy(audiofile, DEFAULT_AUDIO_INPUT);
strcpy(outputfile, DEFAULT_OUTPUT_FILE);
fps_set = 0;
strcpy(input_names, "%d.jpg");
/* Process command-line options */
for ( index=1; argv[index]; ++index ) {
if ( (strcmp(argv[index], "-h") == 0) ||
(strcmp(argv[index], "--help") == 0) ) {
Usage(argv[0]);
exit(0);
}
if ( (strcmp(argv[index], "-r") == 0) && argv[index+1] ) {
++index;
video_fps = atoi(argv[index]);
fps_set = 1;
}
if ( (strcmp(argv[index], "-c") == 0) && argv[index+1] ) {
++index;
audio_channels = atoi(argv[index]);
}
if ( (strcmp(argv[index], "-n") == 0) && argv[index+1] )
{
index++;
strcpy(input_names, argv[index]);
}
}
/* Count the number of jpeg frames */
index = 1;
do {
sprintf(jpegfile, input_names, index++);
fprintf(stderr, "%s", jpegfile);
} while ( access(jpegfile, R_OK) == 0 );
/* Double check that we have some frames */
video_nframes = index - 2;
if ( video_nframes == 0 ) {
fprintf(stderr, "Warning: no video stream - audio only\n");
}
/* Get the width and height of the output movie */
if ( video_nframes > 0 ) {
sprintf(jpegfile, input_names, 1);
get_jpeg_dimensions(jpegfile, &video_width, &video_height);
}
/* Check to see if there is any audio input */
stat(audiofile, &sb);
audio_left = sb.st_size;
audioinput = fopen(audiofile, "rb");
if ( audioinput == NULL ) {
fprintf(stderr, "Warning: no audio stream - video only\n");
}
else {
if (fps_set == 0)
video_fps = ((double)(video_nframes)/(((double)audio_left)/(audio_rate*(audio_bits/8)*audio_channels)));
}
/* Check to make sure we have at least audio or video */
if ( ! video_nframes && ! audioinput ) {
fprintf(stderr, "No audio or video input - aborting!\n");
exit(1);
}
/* Open the output file */
output = fopen(outputfile, "wb");
if ( output == NULL ) {
fprintf(stderr, "Unable to write output to %s\n", outputfile);
exit(2);
}
/* Okay, now we're ready to rock! */
if ( video_nframes ) {
printf("Encoding %d %dx%d frames of %s encoded video at %2.2f FPS\n",
video_nframes, video_width, video_height, video_encoding, video_fps);
}
if ( audioinput ) {
printf("- Multiplexing %d-bit %s audio stream at %d Hz\n",
audio_bits,(audio_channels == 1)?"mono":"stereo", audio_rate);
}
/* Write the main header */
fwrite(smjpeg_magic, sizeof(smjpeg_magic), 1, output);
WRITE32(SMJPEG_FORMAT_VERSION, output);
WRITE32((Uint32)(((double)video_nframes/video_fps)*1000.0), output);
/* Write the audio header */
if ( audioinput ) {
fwrite(AUDIO_HEADER_MAGIC, 4, 1, output);
WRITE32(8, output);
WRITE16(audio_rate, output);
WRITE8(audio_bits, output);
WRITE8(audio_channels, output);
fwrite(audio_encoding, 4, 1, output);
}
/* Write the video header */
if ( video_nframes ) {
fwrite(VIDEO_HEADER_MAGIC, 4, 1, output);
WRITE32(12, output);
WRITE32(video_nframes, output);
WRITE16(video_width, output);
WRITE16(video_height, output);
fwrite(video_encoding, 4, 1, output);
}
/* Write the end of header marker */
fwrite(HEADER_END_MAGIC, 4, 1, output);
/* Create audio data for encoder */
if (MAGIC_EQUALS(audio_encoding, AUDIO_ENCODING_ADPCM))
{
audio_data = malloc(sizeof(struct adpcm_state) * audio_channels);
if (audio_data == NULL)
{
fprintf(stderr, "Out of memory\n");
exit(2);
}
memset(audio_data, 0, sizeof(struct adpcm_state) * audio_channels);
}
else
{
audio_data = NULL;
}
/* Multiplex the audio and video data */
audio_framesize = DEFAULT_AUDIO_FRAME * (audio_bits / 8) * audio_channels;
audio_time = 0.0;
video_time = 0.0;
ms_per_audio_frame = (1000.0 * DEFAULT_AUDIO_FRAME) / audio_rate;
ms_per_video_frame = 1000.0 / video_fps;
for ( index=1; index <= video_nframes; ++index ) {
/* Encode audio for this frame and one frame ahead */
while ( audioinput && (audio_left > 0) &&
(audio_time < (video_time+2*ms_per_video_frame)) ) {
if ( audio_framesize > audio_left ) {
audio_framesize = audio_left;
}
WriteAudioChunk(audioinput, audio_time, audio_framesize,
audio_encoding, output, audio_channels,
audio_data);
audio_left -= audio_framesize;
audio_time += ms_per_audio_frame;
printf("A"); fflush(stdout);
}
/* Encode the video for this frame */
sprintf(jpegfile, input_names, index);
stat(jpegfile, &sb);
video_framesize = sb.st_size;
jpeginput = fopen(jpegfile, "rb");
if ( jpeginput ) {
WriteVideoChunk(jpeginput, video_time, video_framesize,
video_encoding, output);
video_time += ms_per_video_frame;
} else {
fprintf(stderr, "Couldn't open %s: %s\n", jpegfile,
strerror(errno));
abort();
}
fclose(jpeginput);
printf("V"); fflush(stdout);
}
/* Finish writing any audio data that's left */
while ( audioinput && (audio_left > 0) ) {
if ( audio_framesize > audio_left ) {
audio_framesize = audio_left;
}
WriteAudioChunk(audioinput, audio_time, audio_framesize,
audio_encoding, output, audio_channels,
audio_data);
audio_left -= audio_framesize;
audio_time += ms_per_audio_frame;
printf("A"); fflush(stdout);
}
/* Write the end of data marker */
fwrite(DATA_END_MAGIC, 4, 1, output);
/* We're done! */
printf("\n");
if ( ferror(output) || (fclose(output) == EOF) ) {
fprintf(stderr, "Error while writing to output!\n");
status=6;
} else {
printf("Encoding successfully completed.\n");
status=0;
}
exit(status);
}