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