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