ffmpeg / libavcodec / ulti.c @ d0a0bbd2
History  View  Annotate  Download (11.6 KB)
1 
/*


2 
*

3 
* Copyright (C) 2004 Konstantin Shishkov

4 
*

5 
* This library is free software; you can redistribute it and/or

6 
* modify it under the terms of the GNU Lesser General Public

7 
* License as published by the Free Software Foundation; either

8 
* version 2 of the License, or (at your option) any later version.

9 
*

10 
* This library is distributed in the hope that it will be useful,

11 
* but WITHOUT ANY WARRANTY; without even the implied warranty of

12 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

13 
* Lesser General Public License for more details.

14 
*

15 
* You should have received a copy of the GNU Lesser General Public

16 
* License along with this library; if not, write to the Free Software

17 
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA

18 
*

19 
* IBM Ultimotion Video Decoder

20 
*

21 
*/

22  
23 
/**

24 
* @file ulti.c

25 
* IBM Ultimotion Video Decoder.

26 
*/

27 

28 
#include <stdio.h> 
29 
#include <stdlib.h> 
30 
#include <string.h> 
31 
#include <unistd.h> 
32  
33 
#include "common.h" 
34 
#include "avcodec.h" 
35  
36 
#include "ulti_cb.h" 
37  
38 
typedef struct UltimotionDecodeContext { 
39 
AVCodecContext *avctx; 
40 
int width, height, blocks;

41 
AVFrame frame; 
42 
uint8_t *ulti_codebook; 
43 
} UltimotionDecodeContext; 
44  
45 
static int ulti_decode_init(AVCodecContext *avctx) 
46 
{ 
47 
UltimotionDecodeContext *s = avctx>priv_data; 
48  
49 
s>avctx = avctx; 
50 
s>width = avctx>width; 
51 
s>height = avctx>height; 
52 
s>blocks = (s>width / 8) * (s>height / 8); 
53 
avctx>pix_fmt = PIX_FMT_YUV410P; 
54 
avctx>has_b_frames = 0;

55 
avctx>coded_frame = (AVFrame*) &s>frame; 
56 
s>ulti_codebook = ulti_codebook; 
57  
58 
return 0; 
59 
} 
60  
61 
static int block_coords[8] = // 4x4 block coords in 8x8 superblock 
62 
{ 0, 0, 0, 4, 4, 4, 4, 0}; 
63  
64 
static int angle_by_index[4] = { 0, 2, 6, 12}; 
65  
66 
/* Lookup tables for luma and chroma  used by ulti_convert_yuv() */

67 
static uint8_t ulti_lumas[64] = 
68 
{ 0x10, 0x13, 0x17, 0x1A, 0x1E, 0x21, 0x25, 0x28, 
69 
0x2C, 0x2F, 0x33, 0x36, 0x3A, 0x3D, 0x41, 0x44, 
70 
0x48, 0x4B, 0x4F, 0x52, 0x56, 0x59, 0x5C, 0x60, 
71 
0x63, 0x67, 0x6A, 0x6E, 0x71, 0x75, 0x78, 0x7C, 
72 
0x7F, 0x83, 0x86, 0x8A, 0x8D, 0x91, 0x94, 0x98, 
73 
0x9B, 0x9F, 0xA2, 0xA5, 0xA9, 0xAC, 0xB0, 0xB3, 
74 
0xB7, 0xBA, 0xBE, 0xC1, 0xC5, 0xC8, 0xCC, 0xCF, 
75 
0xD3, 0xD6, 0xDA, 0xDD, 0xE1, 0xE4, 0xE8, 0xEB}; 
76 

77 
static uint8_t ulti_chromas[16] = 
78 
{ 0x60, 0x67, 0x6D, 0x73, 0x7A, 0x80, 0x86, 0x8D, 
79 
0x93, 0x99, 0xA0, 0xA6, 0xAC, 0xB3, 0xB9, 0xC0}; 
80 

81 
/* convert Ultimotion YUV block (sixteen 6bit Y samples and

82 
two 4bit chroma samples) into standard YUV and put it into frame */

83 
static void ulti_convert_yuv(AVFrame *frame, int x, int y, 
84 
uint8_t *luma,int chroma)

85 
{ 
86 
uint8_t *y_plane, *cr_plane, *cb_plane; 
87 
int i;

88 

89 
y_plane = frame>data[0] + x + y * frame>linesize[0]; 
90 
cr_plane = frame>data[1] + (x / 4) + (y / 4) * frame>linesize[1]; 
91 
cb_plane = frame>data[2] + (x / 4) + (y / 4) * frame>linesize[2]; 
92 

93 
cr_plane[0] = ulti_chromas[chroma >> 4]; 
94 

95 
cb_plane[0] = ulti_chromas[chroma & 0xF]; 
96  
97 

98 
for(i = 0; i < 16; i++){ 
99 
y_plane[i & 3] = ulti_lumas[luma[i]];

100 
if((i & 3) == 3) { //next row 
101 
y_plane += frame>linesize[0];

102 
} 
103 
} 
104 
} 
105  
106 
/* generate block like in MS Video1 */

107 
static void ulti_pattern(AVFrame *frame, int x, int y, 
108 
int f0, int f1, int Y0, int Y1, int chroma) 
109 
{ 
110 
uint8_t Luma[16];

111 
int mask, i;

112 
for(mask = 0x80, i = 0; mask; mask >>= 1, i++) { 
113 
if(f0 & mask)

114 
Luma[i] = Y1; 
115 
else

116 
Luma[i] = Y0; 
117 
} 
118 

119 
for(mask = 0x80, i = 8; mask; mask >>= 1, i++) { 
120 
if(f1 & mask)

121 
Luma[i] = Y1; 
122 
else

123 
Luma[i] = Y0; 
124 
} 
125 

126 
ulti_convert_yuv(frame, x, y, Luma, chroma); 
127 
} 
128  
129 
/* fill block with some gradient */

130 
static void ulti_grad(AVFrame *frame, int x, int y, uint8_t *Y, int chroma, int angle) 
131 
{ 
132 
uint8_t Luma[16];

133 
if(angle & 8) { //reverse order 
134 
int t;

135 
angle &= 0x7;

136 
t = Y[0];

137 
Y[0] = Y[3]; 
138 
Y[3] = t;

139 
t = Y[1];

140 
Y[1] = Y[2]; 
141 
Y[2] = t;

142 
} 
143 
switch(angle){

144 
case 0: 
145 
Luma[0] = Y[0]; Luma[1] = Y[1]; Luma[2] = Y[2]; Luma[3] = Y[3]; 
146 
Luma[4] = Y[0]; Luma[5] = Y[1]; Luma[6] = Y[2]; Luma[7] = Y[3]; 
147 
Luma[8] = Y[0]; Luma[9] = Y[1]; Luma[10] = Y[2]; Luma[11] = Y[3]; 
148 
Luma[12] = Y[0]; Luma[13] = Y[1]; Luma[14] = Y[2]; Luma[15] = Y[3]; 
149 
break;

150 
case 1: 
151 
Luma[0] = Y[1]; Luma[1] = Y[2]; Luma[2] = Y[3]; Luma[3] = Y[3]; 
152 
Luma[4] = Y[0]; Luma[5] = Y[1]; Luma[6] = Y[2]; Luma[7] = Y[3]; 
153 
Luma[8] = Y[0]; Luma[9] = Y[1]; Luma[10] = Y[2]; Luma[11] = Y[3]; 
154 
Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[1]; Luma[15] = Y[2]; 
155 
break;

156 
case 2: 
157 
Luma[0] = Y[1]; Luma[1] = Y[2]; Luma[2] = Y[3]; Luma[3] = Y[3]; 
158 
Luma[4] = Y[1]; Luma[5] = Y[2]; Luma[6] = Y[2]; Luma[7] = Y[3]; 
159 
Luma[8] = Y[0]; Luma[9] = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[2]; 
160 
Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[1]; Luma[15] = Y[2]; 
161 
break;

162 
case 3: 
163 
Luma[0] = Y[2]; Luma[1] = Y[3]; Luma[2] = Y[3]; Luma[3] = Y[3]; 
164 
Luma[4] = Y[1]; Luma[5] = Y[2]; Luma[6] = Y[2]; Luma[7] = Y[3]; 
165 
Luma[8] = Y[0]; Luma[9] = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[2]; 
166 
Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[0]; Luma[15] = Y[1]; 
167 
break;

168 
case 4: 
169 
Luma[0] = Y[3]; Luma[1] = Y[3]; Luma[2] = Y[3]; Luma[3] = Y[3]; 
170 
Luma[4] = Y[2]; Luma[5] = Y[2]; Luma[6] = Y[2]; Luma[7] = Y[2]; 
171 
Luma[8] = Y[1]; Luma[9] = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[1]; 
172 
Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[0]; Luma[15] = Y[0]; 
173 
break;

174 
case 5: 
175 
Luma[0] = Y[3]; Luma[1] = Y[3]; Luma[2] = Y[3]; Luma[3] = Y[2]; 
176 
Luma[4] = Y[3]; Luma[5] = Y[2]; Luma[6] = Y[2]; Luma[7] = Y[1]; 
177 
Luma[8] = Y[2]; Luma[9] = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[0]; 
178 
Luma[12] = Y[1]; Luma[13] = Y[0]; Luma[14] = Y[0]; Luma[15] = Y[0]; 
179 
break;

180 
case 6: 
181 
Luma[0] = Y[3]; Luma[1] = Y[3]; Luma[2] = Y[2]; Luma[3] = Y[2]; 
182 
Luma[4] = Y[3]; Luma[5] = Y[2]; Luma[6] = Y[1]; Luma[7] = Y[1]; 
183 
Luma[8] = Y[2]; Luma[9] = Y[2]; Luma[10] = Y[1]; Luma[11] = Y[0]; 
184 
Luma[12] = Y[1]; Luma[13] = Y[1]; Luma[14] = Y[0]; Luma[15] = Y[0]; 
185 
break;

186 
case 7: 
187 
Luma[0] = Y[3]; Luma[1] = Y[3]; Luma[2] = Y[2]; Luma[3] = Y[1]; 
188 
Luma[4] = Y[3]; Luma[5] = Y[2]; Luma[6] = Y[1]; Luma[7] = Y[0]; 
189 
Luma[8] = Y[3]; Luma[9] = Y[2]; Luma[10] = Y[1]; Luma[11] = Y[0]; 
190 
Luma[12] = Y[2]; Luma[13] = Y[1]; Luma[14] = Y[0]; Luma[15] = Y[0]; 
191 
break;

192 
default:

193 
Luma[0] = Y[0]; Luma[1] = Y[0]; Luma[2] = Y[1]; Luma[3] = Y[1]; 
194 
Luma[4] = Y[0]; Luma[5] = Y[0]; Luma[6] = Y[1]; Luma[7] = Y[1]; 
195 
Luma[8] = Y[2]; Luma[9] = Y[2]; Luma[10] = Y[3]; Luma[11] = Y[3]; 
196 
Luma[12] = Y[2]; Luma[13] = Y[2]; Luma[14] = Y[3]; Luma[15] = Y[3]; 
197 
break;

198 
} 
199 

200 
ulti_convert_yuv(frame, x, y, Luma, chroma); 
201 
} 
202  
203 
static int ulti_decode_frame(AVCodecContext *avctx, 
204 
void *data, int *data_size, 
205 
uint8_t *buf, int buf_size)

206 
{ 
207 
UltimotionDecodeContext *s=avctx>priv_data; 
208 
int modifier = 0; 
209 
int uniq = 0; 
210 
int mode = 0; 
211 
int blocks = 0; 
212 
int done = 0; 
213 
int x = 0, y = 0; 
214 
int i;

215 
int skip;

216 
int tmp;

217  
218 
if(s>frame.data[0]) 
219 
avctx>release_buffer(avctx, &s>frame); 
220  
221 
s>frame.reference = 1;

222 
s>frame.buffer_hints = FF_BUFFER_HINTS_VALID  FF_BUFFER_HINTS_PRESERVE  FF_BUFFER_HINTS_REUSABLE; 
223 
if(avctx>get_buffer(avctx, &s>frame) < 0) { 
224 
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");

225 
return 1; 
226 
} 
227 

228 
while(!done) {

229 
int idx;

230 
if(blocks >= s>blocks  y >= s>height)

231 
break;//all blocks decoded 
232 

233 
idx = *buf++; 
234 
if((idx & 0xF8) == 0x70) { 
235 
switch(idx) {

236 
case 0x70: //change modifier 
237 
modifier = *buf++; 
238 
if(modifier>1) 
239 
av_log(avctx, AV_LOG_INFO, "warning: modifier must be 0 or 1, got %i\n", modifier);

240 
break;

241 
case 0x71: // set uniq flag 
242 
uniq = 1;

243 
break;

244 
case 0x72: //toggle mode 
245 
mode = !mode; 
246 
break;

247 
case 0x73: //endofframe 
248 
done = 1;

249 
break;

250 
case 0x74: //skip some blocks 
251 
skip = *buf++; 
252 
if ((blocks + skip) >= s>blocks)

253 
break;

254 
blocks += skip; 
255 
x += skip * 8;

256 
while(x >= s>width) {

257 
x = s>width; 
258 
y += 8;

259 
} 
260 
break;

261 
default:

262 
av_log(avctx, AV_LOG_INFO, "warning: unknown escape 0x%02X\n", idx);

263 
} 
264 
} else { //handle one block 
265 
int code;

266 
int cf;

267 
int angle = 0; 
268 
uint8_t Y[4]; // luma samples of block 
269 
int tx = 0, ty = 0; //coords of subblock 
270 
int chroma = 0; 
271 
if (mode  uniq) {

272 
uniq = 0;

273 
cf = 1;

274 
chroma = 0;

275 
} else {

276 
cf = 0;

277 
if (idx)

278 
chroma = *buf++; 
279 
} 
280 
for (i = 0; i < 4; i++) { // for every subblock 
281 
code = (idx >> (6  i*2)) & 3; //extract 2 bits 
282 
if(!code) //skip subblock 
283 
continue;

284 
if(cf)

285 
chroma = *buf++; 
286 
tx = x + block_coords[i * 2];

287 
ty = y + block_coords[(i * 2) + 1]; 
288 
switch(code) {

289 
case 1: 
290 
tmp = *buf++; 
291 

292 
angle = angle_by_index[(tmp >> 6) & 0x3]; 
293 

294 
Y[0] = tmp & 0x3F; 
295 
Y[1] = Y[0]; 
296 

297 
if (angle) {

298 
Y[2] = Y[0]+1; 
299 
if (Y[2] > 0x3F) 
300 
Y[2] = 0x3F; 
301 
Y[3] = Y[2]; 
302 
} else {

303 
Y[2] = Y[0]; 
304 
Y[3] = Y[0]; 
305 
} 
306 
break;

307 

308 
case 2: 
309 
if (modifier) { // unpack four luma samples 
310 
tmp = (*buf++) << 16;

311 
tmp += (*buf++) << 8;

312 
tmp += *buf++; 
313 

314 
Y[0] = (tmp >> 18) & 0x3F; 
315 
Y[1] = (tmp >> 12) & 0x3F; 
316 
Y[2] = (tmp >> 6) & 0x3F; 
317 
Y[3] = tmp & 0x3F; 
318 
angle = 16;

319 
} else { // retrieve luma samples from codebook 
320 
tmp = (*buf++) << 8;

321 
tmp += (*buf++); 
322 

323 
angle = (tmp >> 12) & 0xF; 
324 
tmp &= 0xFFF;

325 
tmp <<= 2;

326 
Y[0] = s>ulti_codebook[tmp];

327 
Y[1] = s>ulti_codebook[tmp + 1]; 
328 
Y[2] = s>ulti_codebook[tmp + 2]; 
329 
Y[3] = s>ulti_codebook[tmp + 3]; 
330 
} 
331 
break;

332 

333 
case 3: 
334 
if (modifier) { // all 16 luma samples 
335 
uint8_t Luma[16];

336 

337 
tmp = (*buf++) << 16;

338 
tmp += (*buf++) << 8;

339 
tmp += *buf++; 
340 
Luma[0] = (tmp >> 18) & 0x3F; 
341 
Luma[1] = (tmp >> 12) & 0x3F; 
342 
Luma[2] = (tmp >> 6) & 0x3F; 
343 
Luma[3] = tmp & 0x3F; 
344 

345 
tmp = (*buf++) << 16;

346 
tmp += (*buf++) << 8;

347 
tmp += *buf++; 
348 
Luma[4] = (tmp >> 18) & 0x3F; 
349 
Luma[5] = (tmp >> 12) & 0x3F; 
350 
Luma[6] = (tmp >> 6) & 0x3F; 
351 
Luma[7] = tmp & 0x3F; 
352 

353 
tmp = (*buf++) << 16;

354 
tmp += (*buf++) << 8;

355 
tmp += *buf++; 
356 
Luma[8] = (tmp >> 18) & 0x3F; 
357 
Luma[9] = (tmp >> 12) & 0x3F; 
358 
Luma[10] = (tmp >> 6) & 0x3F; 
359 
Luma[11] = tmp & 0x3F; 
360 

361 
tmp = (*buf++) << 16;

362 
tmp += (*buf++) << 8;

363 
tmp += *buf++; 
364 
Luma[12] = (tmp >> 18) & 0x3F; 
365 
Luma[13] = (tmp >> 12) & 0x3F; 
366 
Luma[14] = (tmp >> 6) & 0x3F; 
367 
Luma[15] = tmp & 0x3F; 
368 

369 
ulti_convert_yuv(&s>frame, tx, ty, Luma, chroma); 
370 
} else {

371 
tmp = *buf++; 
372 
if(tmp & 0x80) { 
373 
angle = (tmp >> 4) & 0x7; 
374 
tmp = (tmp << 8) + *buf++;

375 
Y[0] = (tmp >> 6) & 0x3F; 
376 
Y[1] = tmp & 0x3F; 
377 
Y[2] = (*buf++) & 0x3F; 
378 
Y[3] = (*buf++) & 0x3F; 
379 
ulti_grad(&s>frame, tx, ty, Y, chroma, angle); //draw block

380 
} else { // some patterns 
381 
int f0, f1;

382 
f0 = *buf++; 
383 
f1 = tmp; 
384 
Y[0] = (*buf++) & 0x3F; 
385 
Y[1] = (*buf++) & 0x3F; 
386 
ulti_pattern(&s>frame, tx, ty, f1, f0, Y[0], Y[1], chroma); 
387 
} 
388 
} 
389 
break;

390 
} 
391 
if(code != 3) 
392 
ulti_grad(&s>frame, tx, ty, Y, chroma, angle); // draw block

393 
} 
394 
blocks++; 
395 
x += 8;

396 
if(x >= s>width) {

397 
x = 0;

398 
y += 8;

399 
} 
400 
} 
401 
} 
402 

403 
*data_size=sizeof(AVFrame);

404 
*(AVFrame*)data= s>frame; 
405  
406 
return buf_size;

407 
} 
408  
409 
static int ulti_decode_end(AVCodecContext *avctx) 
410 
{ 
411 
/* UltimotionDecodeContext *s = avctx>priv_data;*/

412  
413 
return 0; 
414 
} 
415  
416 
AVCodec ulti_decoder = { 
417 
"ultimotion",

418 
CODEC_TYPE_VIDEO, 
419 
CODEC_ID_ULTI, 
420 
sizeof(UltimotionDecodeContext),

421 
ulti_decode_init, 
422 
NULL,

423 
ulti_decode_end, 
424 
ulti_decode_frame, 
425 
CODEC_CAP_DR1, 
426 
NULL

427 
}; 
428 