4
* Copyright (C) 2004-2009 Theodore Kilgore <kilgota@auburn.edu>
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the
18
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
* Boston, MA 02111-1307, USA.
23
#include "libv4lconvert-priv.h"
24
#include "libv4lsyscall-priv.h"
26
#define CLIP(x) ((x)<0?0:((x)>0xff)?0xff:(x))
28
#define MIN_CLOCKDIV_CID V4L2_CID_PRIVATE_BASE
30
/* FIXME not threadsafe */
31
static int decoder_initialized = 0;
39
static void init_mr97310a_decoder(void)
44
for (i = 0; i < 256; ++i) {
48
if ((i & 0x80) == 0) {
52
} else if ((i & 0xe0) == 0xc0) {
56
} else if ((i & 0xe0) == 0xa0) {
60
} else if ((i & 0xf0) == 0x80) {
64
} else if ((i & 0xf0) == 0x90) {
68
} else if ((i & 0xf0) == 0xf0) {
72
} else if ((i & 0xf8) == 0xe0) {
76
} else if ((i & 0xf8) == 0xe8) {
79
val = 0; /* value is calculated later */
82
table[i].is_abs = is_abs;
86
decoder_initialized = 1;
89
static inline unsigned char get_byte(const unsigned char *inp,
92
const unsigned char *addr;
93
addr = inp + (bitpos >> 3);
94
return (addr[0] << (bitpos & 7)) | (addr[1] >> (8 - (bitpos & 7)));
97
int v4lconvert_decode_mr97310a(struct v4lconvert_data *data,
98
const unsigned char *inp, int src_size,
99
unsigned char *outp, int width, int height)
105
unsigned char lp, tp, tlp, trp;
106
struct v4l2_control min_clockdiv = { .id = MIN_CLOCKDIV_CID };
108
if (!decoder_initialized)
109
init_mr97310a_decoder();
111
/* remove the header */
116
/* main decoding loop */
117
for (row = 0; row < height; ++row) {
120
/* first two pixels in first two rows are stored as raw 8-bit */
122
code = get_byte(inp, bitpos);
126
code = get_byte(inp, bitpos);
133
while (col < width) {
135
code = get_byte(inp, bitpos);
136
/* update bit position */
137
bitpos += table[code].len;
139
/* calculate pixel value */
140
if (table[code].is_abs) {
141
/* get 5 more bits and use them as absolute value */
142
code = get_byte(inp, bitpos);
147
/* value is relative to top or left pixel */
148
val = table[code].val;
151
tlp = outp[-2*width-2];
153
trp = outp[-2*width+2];
156
/* top row: relative to left pixel */
158
} else if (col < 2) {
159
/* left column: relative to top pixel */
160
/* initial estimate */
162
} else if (col > width - 3) {
163
/* left column: relative to top pixel */
164
val += (tp + lp + tlp + 1)/3;
165
/* main area: weighted average of tlp, trp,
170
/* initial estimate for predictor */
171
val += (lp + tp + tlp + trp + 1)/3;
179
/* src_size - 12 because of 12 byte footer */
180
if (((bitpos - 1) / 8) >= (src_size - 12)) {
181
data->frames_dropped++;
182
if (data->frames_dropped == 3) {
183
/* Tell the driver to go slower as
184
the compression engine is not able to
185
compress the image enough, we may
186
fail to do this because older
187
drivers don't support this */
188
SYS_IOCTL(data->fd, VIDIOC_G_CTRL,
190
min_clockdiv.value++;
191
SYS_IOCTL(data->fd, VIDIOC_S_CTRL,
193
/* We return success here, because if we
194
return failure for too many frames in a row
195
libv4l2 will return an error to the
196
application and some applications abort
197
on the first error received. */
198
data->frames_dropped = 0;
201
V4LCONVERT_ERR("incomplete mr97310a frame\n");
206
data->frames_dropped = 0;