| 1 |
/* We would like to embed this inside libv4l, but we cannot as I've failed |
| 2 |
to contact Mark W. McClelland to get permission to relicense this, |
| 3 |
so this lives in an external (GPL licensed) helper */ |
| 4 |
|
| 5 |
/* OV511 Decompression Support Module |
| 6 |
* |
| 7 |
* Copyright (c) 1999-2003 Mark W. McClelland. All rights reserved. |
| 8 |
* http://alpha.dyndns.org/ov511/ |
| 9 |
* |
| 10 |
* Original decompression code Copyright 1998-2000 OmniVision Technologies |
| 11 |
* |
| 12 |
* This program is free software; you can redistribute it and/or modify it |
| 13 |
* under the terms of the GNU General Public License as published by the |
| 14 |
* Free Software Foundation; version 2 of the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <limits.h> |
| 18 |
#include <string.h> |
| 19 |
#include <unistd.h> |
| 20 |
#include "helper-funcs.h" |
| 21 |
|
| 22 |
/****************************************************************************** |
| 23 |
* Decompression Functions |
| 24 |
******************************************************************************/ |
| 25 |
|
| 26 |
static void DecompressYHI(unsigned char *pIn, |
| 27 |
unsigned char *pOut, |
| 28 |
int *iIn, /* in/out */ |
| 29 |
int *iOut, /* in/out */ |
| 30 |
const int w, |
| 31 |
const int YUVFlag) |
| 32 |
{ |
| 33 |
short ZigZag[64]; |
| 34 |
int temp[64]; |
| 35 |
int Zcnt_Flag = 0; |
| 36 |
int Num8_Flag = 0; |
| 37 |
int in_pos = *iIn; |
| 38 |
int out_pos = *iOut; |
| 39 |
int tmp, tmp1, tmp2, tmp3; |
| 40 |
unsigned char header, ZTable[64]; |
| 41 |
short tmpl, tmph, half_byte, idx, count; |
| 42 |
unsigned long ZigZag_length = 0, ZT_length, i, j; |
| 43 |
short DeZigZag[64]; |
| 44 |
|
| 45 |
const short a = 11584; |
| 46 |
const short b = 16068; |
| 47 |
const short c = 15136; |
| 48 |
const short d = 13624; |
| 49 |
const short e = 9104; |
| 50 |
const short f = 6270; |
| 51 |
const short g = 3196; |
| 52 |
|
| 53 |
int out_idx; |
| 54 |
|
| 55 |
/* Take off every 'Zig' */ |
| 56 |
for (i = 0; i < 64; i++) |
| 57 |
ZigZag[i] = 0; |
| 58 |
|
| 59 |
/***************************** |
| 60 |
* Read in the Y header byte * |
| 61 |
*****************************/ |
| 62 |
|
| 63 |
header = pIn[in_pos]; |
| 64 |
in_pos++; |
| 65 |
|
| 66 |
ZigZag_length = header & 0x3f; |
| 67 |
ZigZag_length = ZigZag_length + 1; |
| 68 |
|
| 69 |
Num8_Flag = header & 0x40; |
| 70 |
Zcnt_Flag = header & 0x80; |
| 71 |
|
| 72 |
/************************* |
| 73 |
* Read in the Y content * |
| 74 |
*************************/ |
| 75 |
|
| 76 |
if (Zcnt_Flag == 0) { /* Without Zero Table read contents directly */ |
| 77 |
/* Read in ZigZag[0] */ |
| 78 |
ZigZag[0] = pIn[in_pos++]; |
| 79 |
tmpl = pIn[in_pos++]; |
| 80 |
tmph = tmpl << 8; |
| 81 |
ZigZag[0] = ZigZag[0] | tmph; |
| 82 |
ZigZag[0] = ZigZag[0] << 4; |
| 83 |
ZigZag[0] = ZigZag[0] >> 4; |
| 84 |
|
| 85 |
if (Num8_Flag) { /* 8 Bits */ |
| 86 |
for (i = 1; i < ZigZag_length; i++) { |
| 87 |
ZigZag[i] = pIn[in_pos++]; |
| 88 |
ZigZag[i] = ZigZag[i] << 8; |
| 89 |
ZigZag[i] = ZigZag[i] >> 8; |
| 90 |
} |
| 91 |
} else { /* 12 bits and has no Zero Table */ |
| 92 |
idx = 1; |
| 93 |
half_byte = 0; |
| 94 |
for (i = 1; i < ZigZag_length; i++) { |
| 95 |
if (half_byte == 0) { |
| 96 |
ZigZag[i] = pIn[in_pos++]; |
| 97 |
tmpl = pIn[in_pos++]; |
| 98 |
tmph = tmpl << 8; |
| 99 |
tmph = tmph & 0x0f00; |
| 100 |
ZigZag[i] = ZigZag[i] | tmph; |
| 101 |
ZigZag[i] = ZigZag[i] << 4; |
| 102 |
ZigZag[i] = ZigZag[i] >> 4; |
| 103 |
half_byte = 1; |
| 104 |
} else { |
| 105 |
ZigZag[i] = pIn[in_pos++]; |
| 106 |
ZigZag[i] = ZigZag[i] << 8; |
| 107 |
tmpl = tmpl & 0x00f0; |
| 108 |
ZigZag[i] = ZigZag[i] | tmpl; |
| 109 |
ZigZag[i] = ZigZag[i] >> 4; |
| 110 |
half_byte = 0; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} else { /* Has Zero Table */ |
| 115 |
/* Calculate Z-Table length */ |
| 116 |
ZT_length = ZigZag_length / 8; |
| 117 |
tmp = ZigZag_length % 8; |
| 118 |
|
| 119 |
if (tmp > 0) |
| 120 |
ZT_length = ZT_length + 1; |
| 121 |
|
| 122 |
/* Read in Zero Table */ |
| 123 |
for (j = 0; j < ZT_length; j++) |
| 124 |
ZTable[j] = pIn[in_pos++]; |
| 125 |
|
| 126 |
/* Read in ZigZag[0] */ |
| 127 |
ZigZag[0] = pIn[in_pos++]; |
| 128 |
tmpl = pIn[in_pos++]; |
| 129 |
tmph = tmpl << 8; |
| 130 |
ZigZag[0] = ZigZag[0] | tmph; |
| 131 |
ZigZag[0] = ZigZag[0] << 4; |
| 132 |
ZigZag[0] = ZigZag[0] >> 4; |
| 133 |
|
| 134 |
/* Decode ZigZag */ |
| 135 |
idx = 0; |
| 136 |
ZTable[idx] = ZTable[idx] << 1; |
| 137 |
count = 7; |
| 138 |
|
| 139 |
if (Num8_Flag) { /* 8 Bits and has zero table */ |
| 140 |
for (i = 1; i < ZigZag_length; i++) { |
| 141 |
if ((ZTable[idx] & 0x80)) { |
| 142 |
ZigZag[i] = pIn[in_pos++]; |
| 143 |
ZigZag[i] = ZigZag[i] << 8; |
| 144 |
ZigZag[i] = ZigZag[i] >> 8; |
| 145 |
} |
| 146 |
|
| 147 |
ZTable[idx] = ZTable[idx]<<1; |
| 148 |
count--; |
| 149 |
if (count == 0) { |
| 150 |
count = 8; |
| 151 |
idx++; |
| 152 |
} |
| 153 |
} |
| 154 |
} else { /* 12 bits and has Zero Table */ |
| 155 |
half_byte = 0; |
| 156 |
for (i = 1; i < ZigZag_length; i++) { |
| 157 |
if (ZTable[idx] & 0x80) { |
| 158 |
if (half_byte == 0) { |
| 159 |
ZigZag[i] = pIn[in_pos++]; |
| 160 |
tmpl = pIn[in_pos++]; |
| 161 |
tmph = tmpl << 8; |
| 162 |
tmph = tmph & 0x0f00; |
| 163 |
ZigZag[i] = ZigZag[i] | tmph; |
| 164 |
ZigZag[i] = ZigZag[i] << 4; |
| 165 |
ZigZag[i] = ZigZag[i] >> 4; |
| 166 |
half_byte = 1; |
| 167 |
} else { |
| 168 |
ZigZag[i] = pIn[in_pos++]; |
| 169 |
ZigZag[i] = ZigZag[i] << 8; |
| 170 |
tmpl = tmpl & 0x00f0; |
| 171 |
ZigZag[i] = ZigZag[i] | tmpl; |
| 172 |
ZigZag[i] = ZigZag[i] >> 4; |
| 173 |
half_byte = 0; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
ZTable[idx] = ZTable[idx] << 1; |
| 178 |
count--; |
| 179 |
if (count == 0) { |
| 180 |
count = 8; |
| 181 |
idx++; |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/************* |
| 188 |
* De-ZigZag * |
| 189 |
*************/ |
| 190 |
|
| 191 |
for (j = 0; j < 64; j++) |
| 192 |
DeZigZag[j] = 0; |
| 193 |
|
| 194 |
if (YUVFlag == 1) { |
| 195 |
DeZigZag[0] = ZigZag[0]; |
| 196 |
DeZigZag[1] = ZigZag[1] << 1; |
| 197 |
DeZigZag[2] = ZigZag[5] << 1; |
| 198 |
DeZigZag[3] = ZigZag[6] << 2; |
| 199 |
|
| 200 |
DeZigZag[8] = ZigZag[2] << 1; |
| 201 |
DeZigZag[9] = ZigZag[4] << 1; |
| 202 |
DeZigZag[10] = ZigZag[7] << 1; |
| 203 |
DeZigZag[11] = ZigZag[13] << 2; |
| 204 |
|
| 205 |
DeZigZag[16] = ZigZag[3] << 1; |
| 206 |
DeZigZag[17] = ZigZag[8] << 1; |
| 207 |
DeZigZag[18] = ZigZag[12] << 2; |
| 208 |
DeZigZag[19] = ZigZag[17] << 2; |
| 209 |
|
| 210 |
DeZigZag[24] = ZigZag[9] << 2; |
| 211 |
DeZigZag[25] = ZigZag[11] << 2; |
| 212 |
DeZigZag[26] = ZigZag[18] << 2; |
| 213 |
DeZigZag[27] = ZigZag[24] << 3; |
| 214 |
} else { |
| 215 |
DeZigZag[0] = ZigZag[0]; |
| 216 |
DeZigZag[1] = ZigZag[1] << 2; |
| 217 |
DeZigZag[2] = ZigZag[5] << 2; |
| 218 |
DeZigZag[3] = ZigZag[6] << 3; |
| 219 |
|
| 220 |
DeZigZag[8] = ZigZag[2] << 2; |
| 221 |
DeZigZag[9] = ZigZag[4] << 2; |
| 222 |
DeZigZag[10] = ZigZag[7] << 2; |
| 223 |
DeZigZag[11] = ZigZag[13] << 4; |
| 224 |
|
| 225 |
DeZigZag[16] = ZigZag[3] << 2; |
| 226 |
DeZigZag[17] = ZigZag[8] << 2; |
| 227 |
DeZigZag[18] = ZigZag[12] << 3; |
| 228 |
DeZigZag[19] = ZigZag[17] << 4; |
| 229 |
|
| 230 |
DeZigZag[24] = ZigZag[9] << 3; |
| 231 |
DeZigZag[25] = ZigZag[11] << 4; |
| 232 |
DeZigZag[26] = ZigZag[18] << 4; |
| 233 |
DeZigZag[27] = ZigZag[24] << 4; |
| 234 |
} |
| 235 |
|
| 236 |
/***************** |
| 237 |
**** IDCT 1D **** |
| 238 |
*****************/ |
| 239 |
|
| 240 |
#define IDCT_1D(c0, c1, c2, c3, in) \ |
| 241 |
do { \ |
| 242 |
tmp1 = ((c0) * DeZigZag[in]) + ((c2) * DeZigZag[(in) + 2]); \ |
| 243 |
tmp2 = (c1) * DeZigZag[(in) + 1]; \ |
| 244 |
tmp3 = (c3) * DeZigZag[(in) + 3]; \ |
| 245 |
} while (0) |
| 246 |
|
| 247 |
#define COMPOSE_1(out1, out2) \ |
| 248 |
do { \ |
| 249 |
tmp = tmp1 + tmp2 + tmp3; \ |
| 250 |
temp[out1] = tmp >> 15; \ |
| 251 |
tmp = tmp1 - tmp2 - tmp3; \ |
| 252 |
temp[out2] = tmp >> 15; \ |
| 253 |
} while (0) |
| 254 |
|
| 255 |
#define COMPOSE_2(out1, out2) \ |
| 256 |
do { \ |
| 257 |
tmp = tmp1 + tmp2 - tmp3; \ |
| 258 |
temp[out1] = tmp >> 15; \ |
| 259 |
tmp = tmp1 - tmp2 + tmp3; \ |
| 260 |
temp[out2] = tmp >> 15; \ |
| 261 |
} while (0) |
| 262 |
|
| 263 |
/* j = 0 */ |
| 264 |
IDCT_1D(a, b, c, d, 0); COMPOSE_1(0, 56); |
| 265 |
IDCT_1D(a, b, c, d, 8); COMPOSE_1(1, 57); |
| 266 |
IDCT_1D(a, b, c, d, 16); COMPOSE_1(2, 58); |
| 267 |
IDCT_1D(a, b, c, d, 24); COMPOSE_1(3, 59); |
| 268 |
|
| 269 |
/* j = 1 */ |
| 270 |
IDCT_1D(a, d, f, g, 0); COMPOSE_2(8, 48); |
| 271 |
IDCT_1D(a, d, f, g, 8); COMPOSE_2(9, 49); |
| 272 |
IDCT_1D(a, d, f, g, 16); COMPOSE_2(10, 50); |
| 273 |
IDCT_1D(a, d, f, g, 24); COMPOSE_2(11, 51); |
| 274 |
|
| 275 |
/* j = 2 */ |
| 276 |
IDCT_1D(a, e, -f, b, 0); COMPOSE_2(16, 40); |
| 277 |
IDCT_1D(a, e, -f, b, 8); COMPOSE_2(17, 41); |
| 278 |
IDCT_1D(a, e, -f, b, 16); COMPOSE_2(18, 42); |
| 279 |
IDCT_1D(a, e, -f, b, 24); COMPOSE_2(19, 43); |
| 280 |
|
| 281 |
/* j = 3 */ |
| 282 |
IDCT_1D(a, g, -c, e, 0); COMPOSE_2(24, 32); |
| 283 |
IDCT_1D(a, g, -c, e, 8); COMPOSE_2(25, 33); |
| 284 |
IDCT_1D(a, g, -c, e, 16); COMPOSE_2(26, 34); |
| 285 |
IDCT_1D(a, g, -c, e, 24); COMPOSE_2(27, 35); |
| 286 |
|
| 287 |
#undef IDCT_1D |
| 288 |
#undef COMPOSE_1 |
| 289 |
#undef COMPOSE_2 |
| 290 |
|
| 291 |
/***************** |
| 292 |
**** IDCT 2D **** |
| 293 |
*****************/ |
| 294 |
|
| 295 |
#define IDCT_2D(c0, c1, c2, c3, in) \ |
| 296 |
do { \ |
| 297 |
tmp = temp[in] * (c0) + temp[(in) + 1] * (c1) \ |
| 298 |
+ temp[(in) + 2] * (c2) + temp[(in) + 3] * (c3); \ |
| 299 |
} while (0) |
| 300 |
|
| 301 |
#define STORE(i) \ |
| 302 |
do { \ |
| 303 |
tmp = tmp >> 15; \ |
| 304 |
tmp = tmp + 128; \ |
| 305 |
if (tmp > 255) \ |
| 306 |
tmp = 255; \ |
| 307 |
if (tmp < 0) \ |
| 308 |
tmp = 0; \ |
| 309 |
pOut[i] = (unsigned char)tmp; \ |
| 310 |
} while (0) |
| 311 |
|
| 312 |
#define IDCT_2D_ROW(in) \ |
| 313 |
do { \ |
| 314 |
IDCT_2D(a, b, c, d, in); STORE(0 + out_idx); \ |
| 315 |
IDCT_2D(a, d, f, -g, in); STORE(1 + out_idx); \ |
| 316 |
IDCT_2D(a, e, -f, -b, in); STORE(2 + out_idx); \ |
| 317 |
IDCT_2D(a, g, -c, -e, in); STORE(3 + out_idx); \ |
| 318 |
IDCT_2D(a, -g, -c, e, in); STORE(4 + out_idx); \ |
| 319 |
IDCT_2D(a, -e, -f, b, in); STORE(5 + out_idx); \ |
| 320 |
IDCT_2D(a, -d, f, g, in); STORE(6 + out_idx); \ |
| 321 |
IDCT_2D(a, -b, c, -d, in); STORE(7 + out_idx); \ |
| 322 |
} while (0) |
| 323 |
|
| 324 |
|
| 325 |
#define IDCT_2D_FAST(c0, c1, c2, c3, in) \ |
| 326 |
do { \ |
| 327 |
tmp1 = ((c0) * temp[in]) + ((c2) * temp[(in) + 2]); \ |
| 328 |
tmp2 = (c1) * temp[(in) + 1]; \ |
| 329 |
tmp3 = (c3) * temp[(in) + 3]; \ |
| 330 |
} while (0) |
| 331 |
|
| 332 |
#define STORE_FAST_1(out1, out2) \ |
| 333 |
do { \ |
| 334 |
tmp = tmp1 + tmp2 + tmp3; \ |
| 335 |
STORE((out1) + out_idx); \ |
| 336 |
tmp = tmp1 - tmp2 - tmp3; \ |
| 337 |
STORE((out2) + out_idx); \ |
| 338 |
} while (0) |
| 339 |
|
| 340 |
#define STORE_FAST_2(out1, out2) \ |
| 341 |
do { \ |
| 342 |
tmp = tmp1 + tmp2 - tmp3; \ |
| 343 |
STORE((out1) + out_idx); \ |
| 344 |
tmp = tmp1 - tmp2 + tmp3; \ |
| 345 |
STORE((out2) + out_idx); \ |
| 346 |
} while (0) |
| 347 |
|
| 348 |
#define IDCT_2D_FAST_ROW(in) \ |
| 349 |
do { \ |
| 350 |
IDCT_2D_FAST(a, b, c, d, in); STORE_FAST_1(0, 7); \ |
| 351 |
IDCT_2D_FAST(a, d, f, g, in); STORE_FAST_2(1, 6); \ |
| 352 |
IDCT_2D_FAST(a, e, -f, b, in); STORE_FAST_2(2, 5); \ |
| 353 |
IDCT_2D_FAST(a, g, -c, e, in); STORE_FAST_2(3, 4); \ |
| 354 |
} while (0) |
| 355 |
|
| 356 |
out_idx = out_pos; |
| 357 |
|
| 358 |
IDCT_2D_ROW(0); out_idx += w; |
| 359 |
IDCT_2D_ROW(8); out_idx += w; |
| 360 |
IDCT_2D_ROW(16); out_idx += w; |
| 361 |
IDCT_2D_ROW(24); out_idx += w; |
| 362 |
IDCT_2D_ROW(32); out_idx += w; |
| 363 |
IDCT_2D_ROW(40); out_idx += w; |
| 364 |
IDCT_2D_FAST_ROW(48); out_idx += w; |
| 365 |
IDCT_2D_FAST_ROW(56); |
| 366 |
|
| 367 |
*iIn = in_pos; |
| 368 |
*iOut = out_pos + 8; |
| 369 |
} |
| 370 |
|
| 371 |
#define DECOMP_Y() DecompressYHI(pIn, pY, &iIn, &iY, w, 1) |
| 372 |
#define DECOMP_U() DecompressYHI(pIn, pU, &iIn, &iU, w / 2, 2) |
| 373 |
#define DECOMP_V() DecompressYHI(pIn, pV, &iIn, &iV, w / 2, 2) |
| 374 |
|
| 375 |
#if 0 |
| 376 |
static inline int |
| 377 |
Decompress400HiNoMMX(unsigned char *pIn, |
| 378 |
unsigned char *pOut, |
| 379 |
const int w, |
| 380 |
const int h, |
| 381 |
const int inSize) |
| 382 |
{ |
| 383 |
unsigned char *pY = pOut; |
| 384 |
int x, y, iIn, iY; |
| 385 |
|
| 386 |
iIn = 0; |
| 387 |
for (y = 0; y < h; y += 8) { |
| 388 |
iY = w * y; |
| 389 |
|
| 390 |
for (x = 0; x < w; x += 8) |
| 391 |
DECOMP_Y(); |
| 392 |
} |
| 393 |
|
| 394 |
return 0; |
| 395 |
} |
| 396 |
#endif |
| 397 |
|
| 398 |
static inline int |
| 399 |
Decompress420HiNoMMX(unsigned char *pIn, |
| 400 |
unsigned char *pOut, |
| 401 |
const int w, |
| 402 |
const int h, |
| 403 |
const int inSize) |
| 404 |
{ |
| 405 |
unsigned char *pY = pOut; |
| 406 |
unsigned char *pU = pY + w * h; |
| 407 |
unsigned char *pV = pU + w * h / 4; |
| 408 |
int xY, xUV, iY, iU, iV, iIn, count; |
| 409 |
const int nBlocks = (w * h) / (32 * 8); |
| 410 |
|
| 411 |
iIn = 0; |
| 412 |
iY = iU = iV = 0; |
| 413 |
xY = xUV = 0; |
| 414 |
|
| 415 |
for (count = 0; count < nBlocks; count++) { |
| 416 |
DECOMP_U(); |
| 417 |
DECOMP_V(); xUV += 16; |
| 418 |
if (xUV >= w) { |
| 419 |
iU += (w * 7) / 2; |
| 420 |
iV += (w * 7) / 2; |
| 421 |
xUV = 0; |
| 422 |
} |
| 423 |
|
| 424 |
DECOMP_Y(); xY += 8; |
| 425 |
DECOMP_Y(); xY += 8; |
| 426 |
if (xY >= w) { |
| 427 |
iY += w * 7; |
| 428 |
xY = 0; |
| 429 |
} |
| 430 |
DECOMP_Y(); xY += 8; |
| 431 |
DECOMP_Y(); xY += 8; |
| 432 |
if (xY >= w) { |
| 433 |
iY += w * 7; |
| 434 |
xY = 0; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
return 0; |
| 439 |
} |
| 440 |
|
| 441 |
/* Copies a 64-byte segment at pIn to an 8x8 block at pOut. The width of the |
| 442 |
* image at pOut is specified by w. |
| 443 |
*/ |
| 444 |
static inline void |
| 445 |
make_8x8(unsigned char *pIn, unsigned char *pOut, int w) |
| 446 |
{ |
| 447 |
unsigned char *pOut1 = pOut; |
| 448 |
int x, y; |
| 449 |
|
| 450 |
for (y = 0; y < 8; y++) { |
| 451 |
pOut1 = pOut; |
| 452 |
for (x = 0; x < 8; x++) |
| 453 |
*pOut1++ = *pIn++; |
| 454 |
pOut += w; |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
#if 0 |
| 459 |
/* |
| 460 |
* For RAW BW (YUV 4:0:0) images, data show up in 256 byte segments. |
| 461 |
* The segments represent 4 squares of 8x8 pixels as follows: |
| 462 |
* |
| 463 |
* 0 1 ... 7 64 65 ... 71 ... 192 193 ... 199 |
| 464 |
* 8 9 ... 15 72 73 ... 79 200 201 ... 207 |
| 465 |
* ... ... ... |
| 466 |
* 56 57 ... 63 120 121 ... 127 248 249 ... 255 |
| 467 |
* |
| 468 |
*/ |
| 469 |
static void |
| 470 |
yuv400raw_to_yuv400p(struct ov511_frame *frame, |
| 471 |
unsigned char *pIn0, unsigned char *pOut0) |
| 472 |
{ |
| 473 |
int x, y; |
| 474 |
unsigned char *pIn, *pOut, *pOutLine; |
| 475 |
|
| 476 |
/* Copy Y */ |
| 477 |
pIn = pIn0; |
| 478 |
pOutLine = pOut0; |
| 479 |
for (y = 0; y < frame->rawheight - 1; y += 8) { |
| 480 |
pOut = pOutLine; |
| 481 |
for (x = 0; x < frame->rawwidth - 1; x += 8) { |
| 482 |
make_8x8(pIn, pOut, frame->rawwidth); |
| 483 |
pIn += 64; |
| 484 |
pOut += 8; |
| 485 |
} |
| 486 |
pOutLine += 8 * frame->rawwidth; |
| 487 |
} |
| 488 |
} |
| 489 |
#endif |
| 490 |
|
| 491 |
/* |
| 492 |
* For YUV 4:2:0 images, the data show up in 384 byte segments. |
| 493 |
* The first 64 bytes of each segment are U, the next 64 are V. The U and |
| 494 |
* V are arranged as follows: |
| 495 |
* |
| 496 |
* 0 1 ... 7 |
| 497 |
* 8 9 ... 15 |
| 498 |
* ... |
| 499 |
* 56 57 ... 63 |
| 500 |
* |
| 501 |
* U and V are shipped at half resolution (1 U,V sample -> one 2x2 block). |
| 502 |
* |
| 503 |
* The next 256 bytes are full resolution Y data and represent 4 squares |
| 504 |
* of 8x8 pixels as follows: |
| 505 |
* |
| 506 |
* 0 1 ... 7 64 65 ... 71 ... 192 193 ... 199 |
| 507 |
* 8 9 ... 15 72 73 ... 79 200 201 ... 207 |
| 508 |
* ... ... ... |
| 509 |
* 56 57 ... 63 120 121 ... 127 ... 248 249 ... 255 |
| 510 |
* |
| 511 |
* Note that the U and V data in one segment represent a 16 x 16 pixel |
| 512 |
* area, but the Y data represent a 32 x 8 pixel area. If the width is not an |
| 513 |
* even multiple of 32, the extra 8x8 blocks within a 32x8 block belong to the |
| 514 |
* next horizontal stripe. |
| 515 |
* |
| 516 |
* If dumppix module param is set, _parse_data just dumps the incoming segments, |
| 517 |
* verbatim, in order, into the frame. When used with vidcat -f ppm -s 640x480 |
| 518 |
* this puts the data on the standard output and can be analyzed with the |
| 519 |
* parseppm.c utility I wrote. That's a much faster way for figuring out how |
| 520 |
* these data are scrambled. |
| 521 |
*/ |
| 522 |
|
| 523 |
/* Converts from raw, uncompressed segments at pIn0 to a YUV420P frame at pOut0. |
| 524 |
* |
| 525 |
* FIXME: Currently only handles width and height that are multiples of 16 |
| 526 |
*/ |
| 527 |
static void |
| 528 |
yuv420raw_to_yuv420p(unsigned char *pIn0, unsigned char *pOut0, |
| 529 |
int width, int height) |
| 530 |
{ |
| 531 |
int k, x, y; |
| 532 |
unsigned char *pIn, *pOut, *pOutLine; |
| 533 |
const unsigned int a = width * height; |
| 534 |
const unsigned int w = width / 2; |
| 535 |
|
| 536 |
/* Copy U and V */ |
| 537 |
pIn = pIn0; |
| 538 |
pOutLine = pOut0 + a; |
| 539 |
for (y = 0; y < height - 1; y += 16) { |
| 540 |
pOut = pOutLine; |
| 541 |
for (x = 0; x < width - 1; x += 16) { |
| 542 |
make_8x8(pIn, pOut, w); |
| 543 |
make_8x8(pIn + 64, pOut + a / 4, w); |
| 544 |
pIn += 384; |
| 545 |
pOut += 8; |
| 546 |
} |
| 547 |
pOutLine += 8 * w; |
| 548 |
} |
| 549 |
|
| 550 |
/* Copy Y */ |
| 551 |
pIn = pIn0 + 128; |
| 552 |
pOutLine = pOut0; |
| 553 |
k = 0; |
| 554 |
for (y = 0; y < height - 1; y += 8) { |
| 555 |
pOut = pOutLine; |
| 556 |
for (x = 0; x < width - 1; x += 8) { |
| 557 |
make_8x8(pIn, pOut, width); |
| 558 |
pIn += 64; |
| 559 |
pOut += 8; |
| 560 |
if ((++k) > 3) { |
| 561 |
k = 0; |
| 562 |
pIn += 128; |
| 563 |
} |
| 564 |
} |
| 565 |
pOutLine += 8 * width; |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
/* Remove all 0 blocks from input */ |
| 571 |
static void remove0blocks(unsigned char *pIn, int *inSize) |
| 572 |
{ |
| 573 |
long long *in = (long long *)pIn; |
| 574 |
long long *out = (long long *)pIn; |
| 575 |
int i, j; |
| 576 |
|
| 577 |
for (i = 0; i < *inSize; i += 32, in += 4) { |
| 578 |
int all_zero = 1; |
| 579 |
for (j = 0; j < 4; j++) |
| 580 |
if (in[j]) { |
| 581 |
all_zero = 0; |
| 582 |
break; |
| 583 |
} |
| 584 |
|
| 585 |
/* Skip 32 byte blocks of all 0 */ |
| 586 |
if (all_zero) |
| 587 |
continue; |
| 588 |
|
| 589 |
for (j = 0; j < 4; j++) |
| 590 |
*out++ = in[j]; |
| 591 |
} |
| 592 |
|
| 593 |
*inSize -= (in - out) * 8; |
| 594 |
} |
| 595 |
|
| 596 |
static int v4lconvert_ov511_to_yuv420(unsigned char *src, unsigned char *dest, |
| 597 |
int w, int h, int yvu, int src_size) |
| 598 |
{ |
| 599 |
int rc = 0; |
| 600 |
|
| 601 |
src_size -= 11; /* Remove footer */ |
| 602 |
|
| 603 |
remove0blocks(src, &src_size); |
| 604 |
|
| 605 |
/* Compressed ? */ |
| 606 |
if (src[8] & 0x40) |
| 607 |
rc = Decompress420HiNoMMX(src + 9, dest, w, h, src_size); |
| 608 |
else |
| 609 |
yuv420raw_to_yuv420p(src + 9, dest, w, h); |
| 610 |
|
| 611 |
return rc; |
| 612 |
} |
| 613 |
|
| 614 |
int main(int argc, char *argv[]) |
| 615 |
{ |
| 616 |
int width, height, yvu, src_size, dest_size; |
| 617 |
unsigned char src_buf[500000]; |
| 618 |
unsigned char dest_buf[500000]; |
| 619 |
|
| 620 |
while (1) { |
| 621 |
if (v4lconvert_helper_read(STDIN_FILENO, &width, sizeof(int), argv[0])) |
| 622 |
return 1; /* Erm, no way to recover without loosing sync with libv4l */ |
| 623 |
|
| 624 |
if (v4lconvert_helper_read(STDIN_FILENO, &height, sizeof(int), argv[0])) |
| 625 |
return 1; /* Erm, no way to recover without loosing sync with libv4l */ |
| 626 |
|
| 627 |
if (v4lconvert_helper_read(STDIN_FILENO, &yvu, sizeof(int), argv[0])) |
| 628 |
return 1; /* Erm, no way to recover without loosing sync with libv4l */ |
| 629 |
|
| 630 |
if (v4lconvert_helper_read(STDIN_FILENO, &src_size, sizeof(int), argv[0])) |
| 631 |
return 1; /* Erm, no way to recover without loosing sync with libv4l */ |
| 632 |
|
| 633 |
if (src_size > sizeof(src_buf)) { |
| 634 |
fprintf(stderr, "%s: error: src_buf too small, need: %d\n", |
| 635 |
argv[0], src_size); |
| 636 |
return 2; |
| 637 |
} |
| 638 |
|
| 639 |
if (v4lconvert_helper_read(STDIN_FILENO, src_buf, src_size, argv[0])) |
| 640 |
return 1; /* Erm, no way to recover without loosing sync with libv4l */ |
| 641 |
|
| 642 |
|
| 643 |
dest_size = width * height * 3 / 2; |
| 644 |
if (width <= 0 || width > SHRT_MAX || height <= 0 || height > SHRT_MAX) { |
| 645 |
fprintf(stderr, "%s: error: width or height out of bounds\n", |
| 646 |
argv[0]); |
| 647 |
dest_size = -1; |
| 648 |
} else if (dest_size > sizeof(dest_buf)) { |
| 649 |
fprintf(stderr, "%s: error: dest_buf too small, need: %d\n", |
| 650 |
argv[0], dest_size); |
| 651 |
dest_size = -1; |
| 652 |
} else if (v4lconvert_ov511_to_yuv420(src_buf, dest_buf, width, height, |
| 653 |
yvu, src_size)) |
| 654 |
dest_size = -1; |
| 655 |
|
| 656 |
if (v4lconvert_helper_write(STDOUT_FILENO, &dest_size, sizeof(int), |
| 657 |
argv[0])) |
| 658 |
return 1; /* Erm, no way to recover without loosing sync with libv4l */ |
| 659 |
|
| 660 |
if (dest_size == -1) |
| 661 |
continue; |
| 662 |
|
| 663 |
if (v4lconvert_helper_write(STDOUT_FILENO, dest_buf, dest_size, argv[0])) |
| 664 |
return 1; /* Erm, no way to recover without loosing sync with libv4l */ |
| 665 |
} |
| 666 |
} |