OSDN Git Service

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