static void test_compress(
    FILE* outFp,
    FILE* inpFp,
    size_t messageMaxBytes,
    size_t ringBufferBytes)
{
    LZ4_stream_t* const lz4Stream = LZ4_createStream();
    const size_t cmpBufBytes = LZ4_COMPRESSBOUND(messageMaxBytes);
    char* const cmpBuf = (char*) malloc(cmpBufBytes);
    char* const inpBuf = (char*) malloc(ringBufferBytes);
    int inpOffset = 0;

    for ( ; ; )
    {
        char* const inpPtr = &inpBuf[inpOffset];

#if 0
        // Read random length data to the ring buffer.
        const int randomLength = (rand() % messageMaxBytes) + 1;
        const int inpBytes = (int) read_bin(inpFp, inpPtr, randomLength);
        if (0 == inpBytes) break;
#else
        // Read line to the ring buffer.
        int inpBytes = 0;
        if (!fgets(inpPtr, (int) messageMaxBytes, inpFp))
            break;
        inpBytes = (int) strlen(inpPtr);
#endif

        {
            const int cmpBytes = LZ4_compress_fast_continue(
                lz4Stream, inpPtr, cmpBuf, inpBytes, cmpBufBytes, 1);
            if (cmpBytes <= 0) break;
            write_uint16(outFp, (uint16_t) cmpBytes);
            write_bin(outFp, cmpBuf, cmpBytes);

            // Add and wraparound the ringbuffer offset
            inpOffset += inpBytes;
            if ((size_t)inpOffset >= ringBufferBytes - messageMaxBytes) inpOffset = 0;
        }
    }
    write_uint16(outFp, 0);

    free(inpBuf);
    free(cmpBuf);
    LZ4_freeStream(lz4Stream);
}
void save_thread_func(void * lpParam){
	char line_buffer[128];
	char * cmp_frame_buff ;
 	IplImage * dummy_image ;
	int i = 0 ;
	unsigned long timestamp ;
	FILE *seqFile;
	cmp_frame_buff = malloc(LZ4_MESSAGE_MAX_BYTES);
	if(cmp_frame_buff == NULL) printf("Cannot allocate sequence output file");
	sprintf(path, "%s/sequence.lz4", path_base);
    	seqFile = fopen(path, "wb");

	//start LZ4 compression
	LZ4_stream_t* const lz4Stream = LZ4_createStream();
    	const size_t cmpBufBytes = LZ4_COMPRESSBOUND(LZ4_MESSAGE_MAX_BYTES);
    	char* const cmpBuf = (char*) malloc(cmpBufBytes);

	dummy_image = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 1);
	printf("Start Save ! \n");
	while(thread_alive || my_frame_buffer.nb_frames_availables > 0){
		if(my_frame_buffer.nb_frames_availables > 0){
			if(pop_frame(dummy_image, &timestamp, &my_frame_buffer) >= 0){
				memcpy(cmp_frame_buff, &timestamp, sizeof(unsigned long));
				memcpy((cmp_frame_buff + sizeof(unsigned long)), dummy_image->imageData, 640*480);
				const int cmpBytes = LZ4_compress_continue(lz4Stream, cmp_frame_buff, cmpBuf, MESSAGE_MAX_BYTES);
				if (cmpBytes <= 0){
				 printf("Compression failed \n");
				 break;
				}
				fwrite(cmpBuf, 1, cmpBytes, seqFile);
                                i ++ ;
        		}
		}
		usleep(WRITE_DELAY_US);
	}
	free(cmp_frame_buff);
	LZ4_freeStream(lz4Stream);
	fclose(seqFile);
	printf("End Save \n");
}
Esempio n. 3
0
int lz4_encode(Lz4EncoderContext *lz4, int height, int stride, uint8_t *io_ptr,
               unsigned int num_io_bytes, int top_down, uint8_t format)
{
    Lz4Encoder *enc = (Lz4Encoder *)lz4;
    uint8_t *lines;
    int num_lines = 0;
    int total_lines = 0;
    int in_size, enc_size, out_size, already_copied;
    uint8_t *in_buf, *compressed_lines;
    uint8_t *out_buf = io_ptr;
    LZ4_stream_t *stream = LZ4_createStream();

    // Encode direction and format
    *(out_buf++) = top_down ? 1 : 0;
    *(out_buf++) = format;
    num_io_bytes -= 2;
    out_size = 2;

    do {
        num_lines = enc->usr->more_lines(enc->usr, &lines);
        if (num_lines <= 0) {
            spice_error("more lines failed");
            LZ4_freeStream(stream);
            return 0;
        }
        in_buf = lines;
        in_size = stride * num_lines;
        lines += in_size;
        int bound_size = LZ4_compressBound(in_size);
        compressed_lines = (uint8_t *) malloc(bound_size + 4);
#ifdef HAVE_LZ4_COMPRESS_FAST_CONTINUE
        enc_size = LZ4_compress_fast_continue(stream, (const char *) in_buf,
                                              (char *) compressed_lines + 4, in_size,
                                              bound_size, 1);
#else
        enc_size = LZ4_compress_continue(stream, (const char *) in_buf,
                                         (char *) compressed_lines + 4, in_size);
#endif
        if (enc_size <= 0) {
            spice_error("compress failed!");
            free(compressed_lines);
            LZ4_freeStream(stream);
            return 0;
        }
        *((uint32_t *)compressed_lines) = htonl(enc_size);

        out_size += enc_size += 4;
        already_copied = 0;
        while (num_io_bytes < enc_size) {
            memcpy(out_buf, compressed_lines + already_copied, num_io_bytes);
            already_copied += num_io_bytes;
            enc_size -= num_io_bytes;
            num_io_bytes = enc->usr->more_space(enc->usr, &io_ptr);
            if (num_io_bytes <= 0) {
                spice_error("more space failed");
                free(compressed_lines);
                LZ4_freeStream(stream);
                return 0;
            }
            out_buf = io_ptr;
        }
        memcpy(out_buf, compressed_lines + already_copied, enc_size);
        out_buf += enc_size;
        num_io_bytes -= enc_size;

        free(compressed_lines);
        total_lines += num_lines;
    } while (total_lines < height);

    LZ4_freeStream(stream);
    if (total_lines != height) {
        spice_error("too many lines\n");
        out_size = 0;
    }

    return out_size;
}