]> git.sesse.net Git - ffmpeg/blob - libavcodec/ulti.c
Fix compilation when MMX is disabled.
[ffmpeg] / libavcodec / ulti.c
1 /*
2  * IBM Ultimotion Video Decoder
3  * Copyright (C) 2004 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 "avcodec.h"
34 #include "bytestream.h"
35
36 #include "ulti_cb.h"
37
38 typedef struct UltimotionDecodeContext {
39     AVCodecContext *avctx;
40     int width, height, blocks;
41     AVFrame frame;
42     const 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->coded_frame = (AVFrame*) &s->frame;
55     s->ulti_codebook = ulti_codebook;
56
57     return 0;
58 }
59
60 static int block_coords[8] = // 4x4 block coords in 8x8 superblock
61     { 0, 0, 0, 4, 4, 4, 4, 0};
62
63 static int angle_by_index[4] = { 0, 2, 6, 12};
64
65 /* Lookup tables for luma and chroma - used by ulti_convert_yuv() */
66 static uint8_t ulti_lumas[64] =
67     { 0x10, 0x13, 0x17, 0x1A, 0x1E, 0x21, 0x25, 0x28,
68       0x2C, 0x2F, 0x33, 0x36, 0x3A, 0x3D, 0x41, 0x44,
69       0x48, 0x4B, 0x4F, 0x52, 0x56, 0x59, 0x5C, 0x60,
70       0x63, 0x67, 0x6A, 0x6E, 0x71, 0x75, 0x78, 0x7C,
71       0x7F, 0x83, 0x86, 0x8A, 0x8D, 0x91, 0x94, 0x98,
72       0x9B, 0x9F, 0xA2, 0xA5, 0xA9, 0xAC, 0xB0, 0xB3,
73       0xB7, 0xBA, 0xBE, 0xC1, 0xC5, 0xC8, 0xCC, 0xCF,
74       0xD3, 0xD6, 0xDA, 0xDD, 0xE1, 0xE4, 0xE8, 0xEB};
75
76 static uint8_t ulti_chromas[16] =
77     { 0x60, 0x67, 0x6D, 0x73, 0x7A, 0x80, 0x86, 0x8D,
78       0x93, 0x99, 0xA0, 0xA6, 0xAC, 0xB3, 0xB9, 0xC0};
79
80 /* convert Ultimotion YUV block (sixteen 6-bit Y samples and
81  two 4-bit chroma samples) into standard YUV and put it into frame */
82 static void ulti_convert_yuv(AVFrame *frame, int x, int y,
83                              uint8_t *luma,int chroma)
84 {
85     uint8_t *y_plane, *cr_plane, *cb_plane;
86     int i;
87
88     y_plane = frame->data[0] + x + y * frame->linesize[0];
89     cr_plane = frame->data[1] + (x / 4) + (y / 4) * frame->linesize[1];
90     cb_plane = frame->data[2] + (x / 4) + (y / 4) * frame->linesize[2];
91
92     cr_plane[0] = ulti_chromas[chroma >> 4];
93
94     cb_plane[0] = ulti_chromas[chroma & 0xF];
95
96
97     for(i = 0; i < 16; i++){
98         y_plane[i & 3] = ulti_lumas[luma[i]];
99         if((i & 3) == 3) { //next row
100             y_plane += frame->linesize[0];
101         }
102     }
103 }
104
105 /* generate block like in MS Video1 */
106 static void ulti_pattern(AVFrame *frame, int x, int y,
107                          int f0, int f1, int Y0, int Y1, int chroma)
108 {
109     uint8_t Luma[16];
110     int mask, i;
111     for(mask = 0x80, i = 0; mask; mask >>= 1, i++) {
112         if(f0 & mask)
113             Luma[i] = Y1;
114         else
115             Luma[i] = Y0;
116     }
117
118     for(mask = 0x80, i = 8; mask; mask >>= 1, i++) {
119         if(f1 & mask)
120             Luma[i] = Y1;
121         else
122             Luma[i] = Y0;
123     }
124
125     ulti_convert_yuv(frame, x, y, Luma, chroma);
126 }
127
128 /* fill block with some gradient */
129 static void ulti_grad(AVFrame *frame, int x, int y, uint8_t *Y, int chroma, int angle)
130 {
131     uint8_t Luma[16];
132     if(angle & 8) { //reverse order
133         int t;
134         angle &= 0x7;
135         t = Y[0];
136         Y[0] = Y[3];
137         Y[3] = t;
138         t = Y[1];
139         Y[1] = Y[2];
140         Y[2] = t;
141     }
142     switch(angle){
143     case 0:
144         Luma[0]  = Y[0]; Luma[1]  = Y[1]; Luma[2]  = Y[2]; Luma[3]  = Y[3];
145         Luma[4]  = Y[0]; Luma[5]  = Y[1]; Luma[6]  = Y[2]; Luma[7]  = Y[3];
146         Luma[8]  = Y[0]; Luma[9]  = Y[1]; Luma[10] = Y[2]; Luma[11] = Y[3];
147         Luma[12] = Y[0]; Luma[13] = Y[1]; Luma[14] = Y[2]; Luma[15] = Y[3];
148         break;
149     case 1:
150         Luma[0]  = Y[1]; Luma[1]  = Y[2]; Luma[2]  = Y[3]; Luma[3]  = Y[3];
151         Luma[4]  = Y[0]; Luma[5]  = Y[1]; Luma[6]  = Y[2]; Luma[7]  = Y[3];
152         Luma[8]  = Y[0]; Luma[9]  = Y[1]; Luma[10] = Y[2]; Luma[11] = Y[3];
153         Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[1]; Luma[15] = Y[2];
154         break;
155     case 2:
156         Luma[0]  = Y[1]; Luma[1]  = Y[2]; Luma[2]  = Y[3]; Luma[3]  = Y[3];
157         Luma[4]  = Y[1]; Luma[5]  = Y[2]; Luma[6]  = Y[2]; Luma[7]  = Y[3];
158         Luma[8]  = Y[0]; Luma[9]  = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[2];
159         Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[1]; Luma[15] = Y[2];
160         break;
161     case 3:
162         Luma[0]  = Y[2]; Luma[1]  = Y[3]; Luma[2]  = Y[3]; Luma[3]  = Y[3];
163         Luma[4]  = Y[1]; Luma[5]  = Y[2]; Luma[6]  = Y[2]; Luma[7]  = Y[3];
164         Luma[8]  = Y[0]; Luma[9]  = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[2];
165         Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[0]; Luma[15] = Y[1];
166         break;
167     case 4:
168         Luma[0]  = Y[3]; Luma[1]  = Y[3]; Luma[2]  = Y[3]; Luma[3]  = Y[3];
169         Luma[4]  = Y[2]; Luma[5]  = Y[2]; Luma[6]  = Y[2]; Luma[7]  = Y[2];
170         Luma[8]  = Y[1]; Luma[9]  = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[1];
171         Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[0]; Luma[15] = Y[0];
172         break;
173     case 5:
174         Luma[0]  = Y[3]; Luma[1]  = Y[3]; Luma[2]  = Y[3]; Luma[3]  = Y[2];
175         Luma[4]  = Y[3]; Luma[5]  = Y[2]; Luma[6]  = Y[2]; Luma[7]  = Y[1];
176         Luma[8]  = Y[2]; Luma[9]  = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[0];
177         Luma[12] = Y[1]; Luma[13] = Y[0]; Luma[14] = Y[0]; Luma[15] = Y[0];
178         break;
179     case 6:
180         Luma[0]  = Y[3]; Luma[1]  = Y[3]; Luma[2]  = Y[2]; Luma[3]  = Y[2];
181         Luma[4]  = Y[3]; Luma[5]  = Y[2]; Luma[6]  = Y[1]; Luma[7]  = Y[1];
182         Luma[8]  = Y[2]; Luma[9]  = Y[2]; Luma[10] = Y[1]; Luma[11] = Y[0];
183         Luma[12] = Y[1]; Luma[13] = Y[1]; Luma[14] = Y[0]; Luma[15] = Y[0];
184         break;
185     case 7:
186         Luma[0]  = Y[3]; Luma[1]  = Y[3]; Luma[2]  = Y[2]; Luma[3]  = Y[1];
187         Luma[4]  = Y[3]; Luma[5]  = Y[2]; Luma[6]  = Y[1]; Luma[7]  = Y[0];
188         Luma[8]  = Y[3]; Luma[9]  = Y[2]; Luma[10] = Y[1]; Luma[11] = Y[0];
189         Luma[12] = Y[2]; Luma[13] = Y[1]; Luma[14] = Y[0]; Luma[15] = Y[0];
190         break;
191     default:
192         Luma[0]  = Y[0]; Luma[1]  = Y[0]; Luma[2]  = Y[1]; Luma[3]  = Y[1];
193         Luma[4]  = Y[0]; Luma[5]  = Y[0]; Luma[6]  = Y[1]; Luma[7]  = Y[1];
194         Luma[8]  = Y[2]; Luma[9]  = Y[2]; Luma[10] = Y[3]; Luma[11] = Y[3];
195         Luma[12] = Y[2]; Luma[13] = Y[2]; Luma[14] = Y[3]; Luma[15] = Y[3];
196         break;
197     }
198
199     ulti_convert_yuv(frame, x, y, Luma, chroma);
200 }
201
202 static int ulti_decode_frame(AVCodecContext *avctx,
203                              void *data, int *data_size,
204                              uint8_t *buf, int buf_size)
205 {
206     UltimotionDecodeContext *s=avctx->priv_data;
207     int modifier = 0;
208     int uniq = 0;
209     int mode = 0;
210     int blocks = 0;
211     int done = 0;
212     int x = 0, y = 0;
213     int i;
214     int skip;
215     int tmp;
216
217     if(s->frame.data[0])
218         avctx->release_buffer(avctx, &s->frame);
219
220     s->frame.reference = 1;
221     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
222     if(avctx->get_buffer(avctx, &s->frame) < 0) {
223         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
224         return -1;
225     }
226
227     while(!done) {
228         int idx;
229         if(blocks >= s->blocks || y >= s->height)
230             break;//all blocks decoded
231
232         idx = *buf++;
233         if((idx & 0xF8) == 0x70) {
234             switch(idx) {
235             case 0x70: //change modifier
236                 modifier = *buf++;
237                 if(modifier>1)
238                     av_log(avctx, AV_LOG_INFO, "warning: modifier must be 0 or 1, got %i\n", modifier);
239                 break;
240             case 0x71: // set uniq flag
241                 uniq = 1;
242                 break;
243             case 0x72: //toggle mode
244                 mode = !mode;
245                 break;
246             case 0x73: //end-of-frame
247                 done = 1;
248                 break;
249             case 0x74: //skip some blocks
250                 skip = *buf++;
251                 if ((blocks + skip) >= s->blocks)
252                     break;
253                 blocks += skip;
254                 x += skip * 8;
255                 while(x >= s->width) {
256                     x -= s->width;
257                     y += 8;
258                 }
259                 break;
260             default:
261                 av_log(avctx, AV_LOG_INFO, "warning: unknown escape 0x%02X\n", idx);
262             }
263         } else { //handle one block
264             int code;
265             int cf;
266             int angle = 0;
267             uint8_t Y[4]; // luma samples of block
268             int tx = 0, ty = 0; //coords of subblock
269             int chroma = 0;
270             if (mode || uniq) {
271                 uniq = 0;
272                 cf = 1;
273                 chroma = 0;
274             } else {
275                 cf = 0;
276                 if (idx)
277                     chroma = *buf++;
278             }
279             for (i = 0; i < 4; i++) { // for every subblock
280                 code = (idx >> (6 - i*2)) & 3; //extract 2 bits
281                 if(!code) //skip subblock
282                     continue;
283                 if(cf)
284                     chroma = *buf++;
285                 tx = x + block_coords[i * 2];
286                 ty = y + block_coords[(i * 2) + 1];
287                 switch(code) {
288                 case 1:
289                     tmp = *buf++;
290
291                     angle = angle_by_index[(tmp >> 6) & 0x3];
292
293                     Y[0] = tmp & 0x3F;
294                     Y[1] = Y[0];
295
296                     if (angle) {
297                         Y[2] = Y[0]+1;
298                         if (Y[2] > 0x3F)
299                             Y[2] = 0x3F;
300                         Y[3] = Y[2];
301                     } else {
302                         Y[2] = Y[0];
303                         Y[3] = Y[0];
304                     }
305                     break;
306
307                 case 2:
308                     if (modifier) { // unpack four luma samples
309                         tmp = bytestream_get_be24(&buf);
310
311                         Y[0] = (tmp >> 18) & 0x3F;
312                         Y[1] = (tmp >> 12) & 0x3F;
313                         Y[2] = (tmp >> 6) & 0x3F;
314                         Y[3] = tmp & 0x3F;
315                         angle = 16;
316                     } else { // retrieve luma samples from codebook
317                         tmp = bytestream_get_be16(&buf);
318
319                         angle = (tmp >> 12) & 0xF;
320                         tmp &= 0xFFF;
321                         tmp <<= 2;
322                         Y[0] = s->ulti_codebook[tmp];
323                         Y[1] = s->ulti_codebook[tmp + 1];
324                         Y[2] = s->ulti_codebook[tmp + 2];
325                         Y[3] = s->ulti_codebook[tmp + 3];
326                     }
327                     break;
328
329                 case 3:
330                     if (modifier) { // all 16 luma samples
331                         uint8_t Luma[16];
332
333                         tmp = bytestream_get_be24(&buf);
334                         Luma[0] = (tmp >> 18) & 0x3F;
335                         Luma[1] = (tmp >> 12) & 0x3F;
336                         Luma[2] = (tmp >> 6) & 0x3F;
337                         Luma[3] = tmp & 0x3F;
338
339                         tmp = bytestream_get_be24(&buf);
340                         Luma[4] = (tmp >> 18) & 0x3F;
341                         Luma[5] = (tmp >> 12) & 0x3F;
342                         Luma[6] = (tmp >> 6) & 0x3F;
343                         Luma[7] = tmp & 0x3F;
344
345                         tmp = bytestream_get_be24(&buf);
346                         Luma[8] = (tmp >> 18) & 0x3F;
347                         Luma[9] = (tmp >> 12) & 0x3F;
348                         Luma[10] = (tmp >> 6) & 0x3F;
349                         Luma[11] = tmp & 0x3F;
350
351                         tmp = bytestream_get_be24(&buf);
352                         Luma[12] = (tmp >> 18) & 0x3F;
353                         Luma[13] = (tmp >> 12) & 0x3F;
354                         Luma[14] = (tmp >> 6) & 0x3F;
355                         Luma[15] = tmp & 0x3F;
356
357                         ulti_convert_yuv(&s->frame, tx, ty, Luma, chroma);
358                     } else {
359                         tmp = *buf++;
360                         if(tmp & 0x80) {
361                             angle = (tmp >> 4) & 0x7;
362                             tmp = (tmp << 8) + *buf++;
363                             Y[0] = (tmp >> 6) & 0x3F;
364                             Y[1] = tmp & 0x3F;
365                             Y[2] = (*buf++) & 0x3F;
366                             Y[3] = (*buf++) & 0x3F;
367                             ulti_grad(&s->frame, tx, ty, Y, chroma, angle); //draw block
368                         } else { // some patterns
369                             int f0, f1;
370                             f0 = *buf++;
371                             f1 = tmp;
372                             Y[0] = (*buf++) & 0x3F;
373                             Y[1] = (*buf++) & 0x3F;
374                             ulti_pattern(&s->frame, tx, ty, f1, f0, Y[0], Y[1], chroma);
375                         }
376                     }
377                     break;
378                 }
379                 if(code != 3)
380                     ulti_grad(&s->frame, tx, ty, Y, chroma, angle); // draw block
381             }
382             blocks++;
383                 x += 8;
384             if(x >= s->width) {
385                 x = 0;
386                 y += 8;
387             }
388         }
389     }
390
391     *data_size=sizeof(AVFrame);
392     *(AVFrame*)data= s->frame;
393
394     return buf_size;
395 }
396
397 static int ulti_decode_end(AVCodecContext *avctx)
398 {
399 /*    UltimotionDecodeContext *s = avctx->priv_data;*/
400
401     return 0;
402 }
403
404 AVCodec ulti_decoder = {
405     "ultimotion",
406     CODEC_TYPE_VIDEO,
407     CODEC_ID_ULTI,
408     sizeof(UltimotionDecodeContext),
409     ulti_decode_init,
410     NULL,
411     ulti_decode_end,
412     ulti_decode_frame,
413     CODEC_CAP_DR1,
414     NULL
415 };
416