Debian Codegraph @

lib/libv4lconvert/mr97310a.c

raw
1 /*
2 * MR97310A decoder
3 *
4 * Copyright (C) 2004-2009 Theodore Kilgore <[email protected]>
5 *
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.
10 *
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.
15 *
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., 51 Franklin Street - Suite 500,
19 * Boston, MA 02111-1307, USA.
20 */
21
22 #include <unistd.h>
23 #include "libv4lconvert-priv.h"
24 #include "libv4lsyscall-priv.h"
25
26 #define CLIP(x) ((x) < 0 ? 0 : ((x) > 0xff) ? 0xff : (x))
27
28 #define MIN_CLOCKDIV_CID V4L2_CID_PRIVATE_BASE
29
30 /* FIXME not threadsafe */
31 static int decoder_initialized;
32
33 static struct {
34 unsigned char is_abs;
35 unsigned char len;
36 signed char val;
37 } table[256];
38
39 static void init_mr97310a_decoder(void)
40 {
41 int i;
42 int is_abs, val, len;
43
44 for (i = 0; i < 256; ++i) {
45 is_abs = 0;
46 val = 0;
47 len = 0;
48 if ((i & 0x80) == 0) {
49 /* code 0 */
50 val = 0;
51 len = 1;
52 } else if ((i & 0xe0) == 0xc0) {
53 /* code 110 */
54 val = -3;
55 len = 3;
56 } else if ((i & 0xe0) == 0xa0) {
57 /* code 101 */
58 val = 3;
59 len = 3;
60 } else if ((i & 0xf0) == 0x80) {
61 /* code 1000 */
62 val = 8;
63 len = 4;
64 } else if ((i & 0xf0) == 0x90) {
65 /* code 1001 */
66 val = -8;
67 len = 4;
68 } else if ((i & 0xf0) == 0xf0) {
69 /* code 1111 */
70 val = -20;
71 len = 4;
72 } else if ((i & 0xf8) == 0xe0) {
73 /* code 11100 */
74 val = 20;
75 len = 5;
76 } else if ((i & 0xf8) == 0xe8) {
77 /* code 11101xxxxx */
78 is_abs = 1;
79 val = 0; /* value is calculated later */
80 len = 5;
81 }
82 table[i].is_abs = is_abs;
83 table[i].val = val;
84 table[i].len = len;
85 }
86 decoder_initialized = 1;
87 }
88
89 static inline unsigned char get_byte(const unsigned char *inp,
90 unsigned int bitpos)
91 {
92 const unsigned char *addr;
93
94 addr = inp + (bitpos >> 3);
95 return (addr[0] << (bitpos & 7)) | (addr[1] >> (8 - (bitpos & 7)));
96 }
97
98 int v4lconvert_decode_mr97310a(struct v4lconvert_data *data,
99 const unsigned char *inp, int src_size,
100 unsigned char *outp, int width, int height)
101 {
102 int row, col;
103 int val;
104 int bitpos;
105 unsigned char code;
106 unsigned char lp, tp, tlp, trp;
107 struct v4l2_control min_clockdiv = { .id = MIN_CLOCKDIV_CID };
108
109 if (!decoder_initialized)
110 init_mr97310a_decoder();
111
112 /* remove the header */
113 inp += 12;
114
115 bitpos = 0;
116
117 /* main decoding loop */
118 for (row = 0; row < height; ++row) {
119 col = 0;
120
121 /* first two pixels in first two rows are stored as raw 8-bit */
122 if (row < 2) {
123 code = get_byte(inp, bitpos);
124 bitpos += 8;
125 *outp++ = code;
126
127 code = get_byte(inp, bitpos);
128 bitpos += 8;
129 *outp++ = code;
130
131 col += 2;
132 }
133
134 while (col < width) {
135 /* get bitcode */
136 code = get_byte(inp, bitpos);
137 /* update bit position */
138 bitpos += table[code].len;
139
140 /* calculate pixel value */
141 if (table[code].is_abs) {
142 /* get 5 more bits and use them as absolute value */
143 code = get_byte(inp, bitpos);
144 val = (code & 0xf8);
145 bitpos += 5;
146
147 } else {
148 /* value is relative to top or left pixel */
149 val = table[code].val;
150 lp = outp[-2];
151 if (row > 1) {
152 tlp = outp[-2 * width - 2];
153 tp = outp[-2 * width];
154 trp = outp[-2 * width + 2];
155 }
156 if (row < 2) {
157 /* top row: relative to left pixel */
158 val += lp;
159 } else if (col < 2) {
160 /* left column: relative to top pixel */
161 /* initial estimate */
162 val += (tp + trp) / 2;
163 } else if (col > width - 3) {
164 /* left column: relative to top pixel */
165 val += (tp + lp + tlp + 1) / 3;
166 /* main area: weighted average of tlp, trp,
167 * lp, and tp */
168 } else {
169 tlp >>= 1;
170 trp >>= 1;
171 /* initial estimate for predictor */
172 val += (lp + tp + tlp + trp + 1) / 3;
173 }
174 }
175 /* store pixel */
176 *outp++ = CLIP(val);
177 ++col;
178 }
179
180 /* src_size - 12 because of 12 byte footer */
181 if (((bitpos - 1) / 8) >= (src_size - 12)) {
182 data->frames_dropped++;
183 if (data->frames_dropped == 3) {
184 /* Tell the driver to go slower as
185 the compression engine is not able to
186 compress the image enough, we may
187 fail to do this because older
188 drivers don't support this */
189 SYS_IOCTL(data->fd, VIDIOC_G_CTRL,
190 &min_clockdiv);
191 min_clockdiv.value++;
192 SYS_IOCTL(data->fd, VIDIOC_S_CTRL,
193 &min_clockdiv);
194 /* We return success here, because if we
195 return failure for too many frames in a row
196 libv4l2 will return an error to the
197 application and some applications abort
198 on the first error received. */
199 data->frames_dropped = 0;
200 return 0;
201 }
202 V4LCONVERT_ERR("incomplete mr97310a frame\n");
203 return -1;
204 }
205 }
206
207 data->frames_dropped = 0;
208 return 0;
209 }