summaryrefslogtreecommitdiffstats
path: root/debian/transcode/transcode-1.1.7/import/decode_lzo.c
blob: d9641cb8a7622815edc91ba292f812f4d01c9a9e (plain)
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
/*
 *  decode_lzo.c
 *
 *  Copyright (C) Tilmann Bitterberg - 2003
 *
 *  This file is part of transcode, a video stream processing tool
 *
 *  transcode is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  transcode is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "transcode.h"
#include "libtc/libtc.h"
#include "tcinfo.h"

#include "ioaux.h"
#include "avilib/avilib.h"
#include "tc.h"

#ifdef HAVE_LZO

#include "libtc/tc_lzo.h"

#define MOD_NAME    "decode_lzo"

#define BUFFER_SIZE SIZE_RGB_FRAME<<1

static int r;
static lzo_byte *out;
static lzo_byte *inbuf;
static lzo_byte *wrkmem;
static lzo_uint out_len;


inline static void str2long(unsigned char *bb, long *bytes)
{
    *bytes = (bb[0]<<24) | (bb[1]<<16) | (bb[2]<<8) | (bb[3]);
}

void decode_lzo(decode_t *decode)
{
    long bytes;
    ssize_t ss;
    tc_lzo_header_t h;

    /*
     * Step 1: initialize the LZO library
     */

    if (lzo_init() != LZO_E_OK) {
      tc_log_error(__FILE__, "lzo_init() failed");
      goto decoder_error;
    }

    wrkmem = (lzo_bytep) lzo_malloc(LZO1X_1_MEM_COMPRESS);
    out = (lzo_bytep) lzo_malloc(BUFFER_SIZE);
    inbuf = (lzo_bytep) lzo_malloc(BUFFER_SIZE);

    if (wrkmem == NULL || out == NULL) {
      tc_log_error(__FILE__, "out of memory");
      goto decoder_error;
    }

    r = 0;
    out_len = 0;
    verbose = decode->verbose;

    for (;;) {
	if ( (ss=tc_pread (decode->fd_in, (uint8_t *)&h, sizeof(h))) != sizeof(h)) {
	    //tc_log_msg(__FILE__, "failed to read frame size: EOF. expected (%ld) got (%d)", 4L, ss);
	    goto decoder_out;
	}

	// check magic
	if (h.magic != TC_CODEC_LZO2) {
	    tc_log_error(__FILE__, "Wrong stream magic: expected (0x%x) got (0x%x)", TC_CODEC_LZO2, h.magic);
	    goto decoder_error;
	}

	//str2long(bb, &bytes);
	bytes = h.size;

	if (verbose & TC_DEBUG)
	    tc_log_msg(__FILE__, "got bytes (%ld)", bytes);
	if ( (ss=tc_pread (decode->fd_in, inbuf, bytes))!=bytes) {
	    tc_log_error(__FILE__, "failed to read frame: expected (%ld) got (%lu)", bytes, (unsigned long)ss);
	    goto decoder_error;
	}

	if (h.flags & TC_LZO_NOT_COMPRESSIBLE) {
	  ac_memcpy(out, inbuf, bytes);
	  out_len = bytes;
	  r = LZO_E_OK;
	} else {
	  r = lzo1x_decompress(inbuf, bytes, out, &out_len, wrkmem);
	}

	if (r == LZO_E_OK) {
	    if(verbose & TC_DEBUG)
		tc_log_msg(__FILE__, "decompressed %lu bytes into %lu bytes",
			   (long) bytes, (long) out_len);
	} else {

	    /* this should NEVER happen */
	    tc_log_error(__FILE__, "internal error - decompression failed: %d", r);
	    goto decoder_error;
	}

	if ( (ss = tc_pwrite (decode->fd_out, out, out_len)) != out_len) {
	    tc_log_error(__FILE__, "failed to write frame: expected (%ld) wrote (%lu)", bytes, (unsigned long)ss);
	    goto decoder_error;
	}
    }

decoder_out:
    import_exit(0);

decoder_error:
    import_exit(1);

}

#else /* HAVE_LZO */

void decode_lzo(decode_t *decode)
{
    tc_log_error(__FILE__, "No support for LZO configured -- exiting");
    import_exit(1);
}


#endif /* HAVE_LZO */