diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-04-08 17:59:15 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-04-08 17:59:15 -0400 |
commit | c8f6735fce3d7ba5aa8019775d26b6999ed5a344 (patch) | |
tree | b303e7d53a0bd365f23490d06bebc8d04228bbbe | |
parent | 6b5037e8c094c99bb5d6b07edb830a93bdddd110 (diff) |
derp, use a separate encoder for each video stream
-rw-r--r-- | src/freenect-server.c | 54 |
1 files changed, 31 insertions, 23 deletions
diff --git a/src/freenect-server.c b/src/freenect-server.c index 7a4cebb..a36701c 100644 --- a/src/freenect-server.c +++ b/src/freenect-server.c @@ -40,40 +40,47 @@ FILE *video_stream = NULL; FILE *accel_stream = NULL; freenect_context *ctx; -struct jpeg_error_mgr jpeg_encoder_err; -struct jpeg_compress_struct jpeg_encoder; -unsigned char *jpeg_encoder_buf; -unsigned long jpeg_encoder_len; +struct mpjpg_encoder { + struct jpeg_error_mgr jpeg_encoder_err; + struct jpeg_compress_struct jpeg_encoder; + unsigned char *jpeg_buf; + unsigned long jpeg_len; +}; -void jpeg_init() { - jpeg_encoder.err = jpeg_std_error(&jpeg_encoder_err); - jpeg_create_compress(&jpeg_encoder); - jpeg_encoder.image_width = 640; - jpeg_encoder.image_height = 480; - jpeg_encoder.input_components = 3; - jpeg_encoder.in_color_space = JCS_RGB; - jpeg_set_defaults(&jpeg_encoder); - jpeg_mem_dest(&jpeg_encoder, &jpeg_encoder_buf, &jpeg_encoder_len); +struct mpjpg_encoder depth_encoder; +struct mpjpg_encoder video_encoder; + +void mpjpg_init(struct mpjpg_encoder *e) { + e->jpeg_encoder.err = jpeg_std_error(&e->jpeg_encoder_err); + jpeg_create_compress(&e->jpeg_encoder); + e->jpeg_encoder.image_width = 640; + e->jpeg_encoder.image_height = 480; + e->jpeg_encoder.input_components = 3; + e->jpeg_encoder.in_color_space = JCS_RGB; + jpeg_set_defaults(&e->jpeg_encoder); + e->jpeg_buf = NULL; + e->jpeg_len = 0; + jpeg_mem_dest(&e->jpeg_encoder, &e->jpeg_buf, &e->jpeg_len); } -void write_imageframe(FILE *stream, JSAMPLE *data) { +void write_imageframe(FILE *stream, struct mpjpg_encoder *e, JSAMPLE *data) { JSAMPLE *row_pointers[480]; for (size_t i = 0; i < 480; i++) row_pointers[i] = &data[i*640*3]; - jpeg_start_compress(&jpeg_encoder, TRUE); - while (jpeg_encoder.next_scanline < jpeg_encoder.image_height) - jpeg_write_scanlines(&jpeg_encoder, &row_pointers[jpeg_encoder.next_scanline], 480-jpeg_encoder.next_scanline); - jpeg_finish_compress(&jpeg_encoder); + jpeg_start_compress(&e->jpeg_encoder, TRUE); + while (e->jpeg_encoder.next_scanline < e->jpeg_encoder.image_height) + jpeg_write_scanlines(&e->jpeg_encoder, &row_pointers[e->jpeg_encoder.next_scanline], 480-e->jpeg_encoder.next_scanline); + jpeg_finish_compress(&e->jpeg_encoder); if (fprintf(stream, "--ffserver\r\n" "Content-type: image/jpeg\r\n" "Content-length: %lu\r\n" "\r\n", - jpeg_encoder_len) < 1) + e->jpeg_len) < 1) error(EXIT_FAILURE, ferror(stream), "writing header"); - if (fwrite(jpeg_encoder_buf, jpeg_encoder_len, 1, stream) < 1) + if (fwrite(e->jpeg_buf, e->jpeg_len, 1, stream) < 1) error(EXIT_FAILURE, ferror(stream), "writing body"); if (fwrite("\r\n", 2, 1, stream) < 1) error(EXIT_FAILURE, ferror(stream), "writing footer"); @@ -130,7 +137,7 @@ void handle_depth(freenect_device *dev, void *depth, uint32_t timestamp UNUSED) for (size_t i = 0; i < 640*480; i++) depth_rgb[i*3+0] = depth_rgb[i*3+1] = depth_rgb[i*3+2] = (uint8_t)(depth_grey[i]*8.0/11.0); /* write the image */ - write_imageframe(depth_stream, depth_rgb); + write_imageframe(depth_stream, &depth_encoder, depth_rgb); } void handle_video(freenect_device *dev, void *rgb, uint32_t timestamp UNUSED) { @@ -138,7 +145,7 @@ void handle_video(freenect_device *dev, void *rgb, uint32_t timestamp UNUSED) { if (size != 640*480*3) error(EXIT_FAILURE, 0, "handle_video: expected 640*480*3 byte frame, but got %"PRId32, size); /* write the image */ - write_imageframe(video_stream, rgb); + write_imageframe(video_stream, &video_encoder, rgb); } void print_mode(const char *name, freenect_frame_mode mode) { @@ -240,7 +247,8 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - jpeg_init(); + mpjpg_init(&depth_encoder); + mpjpg_init(&video_encoder); res = freenect_init(&ctx, 0); if (res) { |