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