2 * IBM Ultimotion Video Decoder
3 * Copyright (C) 2004 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
24 * IBM Ultimotion Video Decoder.
28 #include "bytestream.h"
29 #include "codec_internal.h"
34 typedef struct UltimotionDecodeContext {
35 AVCodecContext *avctx;
36 int width, height, blocks;
38 const uint8_t *ulti_codebook;
40 } UltimotionDecodeContext;
42 static av_cold int ulti_decode_init(AVCodecContext *avctx)
44 UltimotionDecodeContext *s = avctx->priv_data;
47 s->width = avctx->width;
48 s->height = avctx->height;
49 s->blocks = (s->width / 8) * (s->height / 8);
51 return AVERROR_INVALIDDATA;
52 avctx->pix_fmt = AV_PIX_FMT_YUV410P;
53 s->ulti_codebook = ulti_codebook;
55 s->frame = av_frame_alloc();
57 return AVERROR(ENOMEM);
62 static av_cold int ulti_decode_end(AVCodecContext *avctx)
64 UltimotionDecodeContext *s = avctx->priv_data;
66 av_frame_free(&s->frame);
71 static const int block_coords[8] = // 4x4 block coords in 8x8 superblock
72 { 0, 0, 0, 4, 4, 4, 4, 0};
74 static const int angle_by_index[4] = { 0, 2, 6, 12};
76 /* Lookup tables for luma and chroma - used by ulti_convert_yuv() */
77 static const uint8_t ulti_lumas[64] =
78 { 0x10, 0x13, 0x17, 0x1A, 0x1E, 0x21, 0x25, 0x28,
79 0x2C, 0x2F, 0x33, 0x36, 0x3A, 0x3D, 0x41, 0x44,
80 0x48, 0x4B, 0x4F, 0x52, 0x56, 0x59, 0x5C, 0x60,
81 0x63, 0x67, 0x6A, 0x6E, 0x71, 0x75, 0x78, 0x7C,
82 0x7F, 0x83, 0x86, 0x8A, 0x8D, 0x91, 0x94, 0x98,
83 0x9B, 0x9F, 0xA2, 0xA5, 0xA9, 0xAC, 0xB0, 0xB3,
84 0xB7, 0xBA, 0xBE, 0xC1, 0xC5, 0xC8, 0xCC, 0xCF,
85 0xD3, 0xD6, 0xDA, 0xDD, 0xE1, 0xE4, 0xE8, 0xEB};
87 static const uint8_t ulti_chromas[16] =
88 { 0x60, 0x67, 0x6D, 0x73, 0x7A, 0x80, 0x86, 0x8D,
89 0x93, 0x99, 0xA0, 0xA6, 0xAC, 0xB3, 0xB9, 0xC0};
91 /* convert Ultimotion YUV block (sixteen 6-bit Y samples and
92 two 4-bit chroma samples) into standard YUV and put it into frame */
93 static void ulti_convert_yuv(AVFrame *frame, int x, int y,
94 uint8_t *luma,int chroma)
96 uint8_t *y_plane, *cr_plane, *cb_plane;
99 y_plane = frame->data[0] + x + y * frame->linesize[0];
100 cr_plane = frame->data[1] + (x / 4) + (y / 4) * frame->linesize[1];
101 cb_plane = frame->data[2] + (x / 4) + (y / 4) * frame->linesize[2];
103 cr_plane[0] = ulti_chromas[chroma >> 4];
105 cb_plane[0] = ulti_chromas[chroma & 0xF];
108 for(i = 0; i < 16; i++){
109 y_plane[i & 3] = ulti_lumas[luma[i]];
110 if((i & 3) == 3) { //next row
111 y_plane += frame->linesize[0];
116 /* generate block like in MS Video1 */
117 static void ulti_pattern(AVFrame *frame, int x, int y,
118 int f0, int f1, int Y0, int Y1, int chroma)
122 for(mask = 0x80, i = 0; mask; mask >>= 1, i++) {
129 for(mask = 0x80, i = 8; mask; mask >>= 1, i++) {
136 ulti_convert_yuv(frame, x, y, Luma, chroma);
139 /* fill block with some gradient */
140 static void ulti_grad(AVFrame *frame, int x, int y, uint8_t *Y, int chroma, int angle)
143 if(angle & 8) { //reverse order
155 Luma[0] = Y[0]; Luma[1] = Y[1]; Luma[2] = Y[2]; Luma[3] = Y[3];
156 Luma[4] = Y[0]; Luma[5] = Y[1]; Luma[6] = Y[2]; Luma[7] = Y[3];
157 Luma[8] = Y[0]; Luma[9] = Y[1]; Luma[10] = Y[2]; Luma[11] = Y[3];
158 Luma[12] = Y[0]; Luma[13] = Y[1]; Luma[14] = Y[2]; Luma[15] = Y[3];
161 Luma[0] = Y[1]; Luma[1] = Y[2]; Luma[2] = Y[3]; 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[0]; Luma[14] = Y[1]; Luma[15] = Y[2];
167 Luma[0] = Y[1]; Luma[1] = Y[2]; Luma[2] = Y[3]; Luma[3] = Y[3];
168 Luma[4] = Y[1]; Luma[5] = Y[2]; Luma[6] = Y[2]; Luma[7] = Y[3];
169 Luma[8] = Y[0]; Luma[9] = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[2];
170 Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[1]; Luma[15] = Y[2];
173 Luma[0] = Y[2]; Luma[1] = Y[3]; 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[0]; Luma[15] = Y[1];
179 Luma[0] = Y[3]; Luma[1] = Y[3]; Luma[2] = Y[3]; Luma[3] = Y[3];
180 Luma[4] = Y[2]; Luma[5] = Y[2]; Luma[6] = Y[2]; Luma[7] = Y[2];
181 Luma[8] = Y[1]; Luma[9] = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[1];
182 Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[0]; Luma[15] = Y[0];
185 Luma[0] = Y[3]; Luma[1] = Y[3]; Luma[2] = Y[3]; Luma[3] = Y[2];
186 Luma[4] = Y[3]; Luma[5] = Y[2]; Luma[6] = Y[2]; Luma[7] = Y[1];
187 Luma[8] = Y[2]; Luma[9] = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[0];
188 Luma[12] = Y[1]; Luma[13] = Y[0]; Luma[14] = Y[0]; Luma[15] = Y[0];
191 Luma[0] = Y[3]; Luma[1] = Y[3]; Luma[2] = Y[2]; Luma[3] = Y[2];
192 Luma[4] = Y[3]; Luma[5] = Y[2]; Luma[6] = Y[1]; Luma[7] = Y[1];
193 Luma[8] = Y[2]; Luma[9] = Y[2]; Luma[10] = Y[1]; Luma[11] = Y[0];
194 Luma[12] = Y[1]; Luma[13] = Y[1]; Luma[14] = Y[0]; Luma[15] = Y[0];
197 Luma[0] = Y[3]; Luma[1] = Y[3]; Luma[2] = Y[2]; Luma[3] = Y[1];
198 Luma[4] = Y[3]; Luma[5] = Y[2]; Luma[6] = Y[1]; Luma[7] = Y[0];
199 Luma[8] = Y[3]; Luma[9] = Y[2]; Luma[10] = Y[1]; Luma[11] = Y[0];
200 Luma[12] = Y[2]; Luma[13] = Y[1]; Luma[14] = Y[0]; Luma[15] = Y[0];
203 Luma[0] = Y[0]; Luma[1] = Y[0]; Luma[2] = Y[1]; Luma[3] = Y[1];
204 Luma[4] = Y[0]; Luma[5] = Y[0]; Luma[6] = Y[1]; Luma[7] = Y[1];
205 Luma[8] = Y[2]; Luma[9] = Y[2]; Luma[10] = Y[3]; Luma[11] = Y[3];
206 Luma[12] = Y[2]; Luma[13] = Y[2]; Luma[14] = Y[3]; Luma[15] = Y[3];
210 ulti_convert_yuv(frame, x, y, Luma, chroma);
213 static int ulti_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
214 int *got_frame, AVPacket *avpkt)
216 const uint8_t *buf = avpkt->data;
217 int buf_size = avpkt->size;
218 UltimotionDecodeContext *s=avctx->priv_data;
229 if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
232 bytestream2_init(&s->gb, buf, buf_size);
236 if(blocks >= s->blocks || y >= s->height)
237 break;//all blocks decoded
239 if (bytestream2_get_bytes_left(&s->gb) < 1)
241 idx = bytestream2_get_byteu(&s->gb);
242 if((idx & 0xF8) == 0x70) {
244 case 0x70: //change modifier
245 modifier = bytestream2_get_byte(&s->gb);
247 av_log(avctx, AV_LOG_INFO, "warning: modifier must be 0 or 1, got %i\n", modifier);
249 case 0x71: // set uniq flag
252 case 0x72: //toggle mode
255 case 0x73: //end-of-frame
258 case 0x74: //skip some blocks
259 skip = bytestream2_get_byte(&s->gb);
260 if ((blocks + skip) >= s->blocks)
264 while(x >= s->width) {
270 av_log(avctx, AV_LOG_INFO, "warning: unknown escape 0x%02X\n", idx);
272 } else { //handle one block
276 uint8_t Y[4]; // luma samples of block
277 int tx = 0, ty = 0; //coords of subblock
286 chroma = bytestream2_get_byte(&s->gb);
289 for (i = 0; i < 4; i++) { // for every subblock
290 code = (idx >> (6 - i*2)) & 3; //extract 2 bits
291 if(!code) //skip subblock
294 chroma = bytestream2_get_byte(&s->gb);
296 tx = x + block_coords[i * 2];
297 ty = y + block_coords[(i * 2) + 1];
300 tmp = bytestream2_get_byte(&s->gb);
302 angle = angle_by_index[(tmp >> 6) & 0x3];
319 if (modifier) { // unpack four luma samples
320 tmp = bytestream2_get_be24(&s->gb);
322 Y[0] = (tmp >> 18) & 0x3F;
323 Y[1] = (tmp >> 12) & 0x3F;
324 Y[2] = (tmp >> 6) & 0x3F;
327 } else { // retrieve luma samples from codebook
328 tmp = bytestream2_get_be16(&s->gb);
330 angle = (tmp >> 12) & 0xF;
333 Y[0] = s->ulti_codebook[tmp];
334 Y[1] = s->ulti_codebook[tmp + 1];
335 Y[2] = s->ulti_codebook[tmp + 2];
336 Y[3] = s->ulti_codebook[tmp + 3];
341 if (modifier) { // all 16 luma samples
344 if (bytestream2_get_bytes_left(&s->gb) < 12)
346 tmp = bytestream2_get_be24u(&s->gb);
347 Luma[0] = (tmp >> 18) & 0x3F;
348 Luma[1] = (tmp >> 12) & 0x3F;
349 Luma[2] = (tmp >> 6) & 0x3F;
350 Luma[3] = tmp & 0x3F;
352 tmp = bytestream2_get_be24u(&s->gb);
353 Luma[4] = (tmp >> 18) & 0x3F;
354 Luma[5] = (tmp >> 12) & 0x3F;
355 Luma[6] = (tmp >> 6) & 0x3F;
356 Luma[7] = tmp & 0x3F;
358 tmp = bytestream2_get_be24u(&s->gb);
359 Luma[8] = (tmp >> 18) & 0x3F;
360 Luma[9] = (tmp >> 12) & 0x3F;
361 Luma[10] = (tmp >> 6) & 0x3F;
362 Luma[11] = tmp & 0x3F;
364 tmp = bytestream2_get_be24u(&s->gb);
365 Luma[12] = (tmp >> 18) & 0x3F;
366 Luma[13] = (tmp >> 12) & 0x3F;
367 Luma[14] = (tmp >> 6) & 0x3F;
368 Luma[15] = tmp & 0x3F;
370 ulti_convert_yuv(s->frame, tx, ty, Luma, chroma);
372 if (bytestream2_get_bytes_left(&s->gb) < 4)
374 tmp = bytestream2_get_byteu(&s->gb);
376 angle = (tmp >> 4) & 0x7;
377 tmp = (tmp << 8) + bytestream2_get_byteu(&s->gb);
378 Y[0] = (tmp >> 6) & 0x3F;
380 Y[2] = bytestream2_get_byteu(&s->gb) & 0x3F;
381 Y[3] = bytestream2_get_byteu(&s->gb) & 0x3F;
382 ulti_grad(s->frame, tx, ty, Y, chroma, angle); //draw block
383 } else { // some patterns
385 int f1 = bytestream2_get_byteu(&s->gb);
386 Y[0] = bytestream2_get_byteu(&s->gb) & 0x3F;
387 Y[1] = bytestream2_get_byteu(&s->gb) & 0x3F;
388 ulti_pattern(s->frame, tx, ty, f0, f1, Y[0], Y[1], chroma);
394 ulti_grad(s->frame, tx, ty, Y, chroma, angle); // draw block
406 if ((ret = av_frame_ref(rframe, s->frame)) < 0)
412 av_log(avctx, AV_LOG_ERROR,
413 "Insufficient data\n");
414 return AVERROR_INVALIDDATA;
417 const FFCodec ff_ulti_decoder = {
418 .p.name = "ultimotion",
419 CODEC_LONG_NAME("IBM UltiMotion"),
420 .p.type = AVMEDIA_TYPE_VIDEO,
421 .p.id = AV_CODEC_ID_ULTI,
422 .priv_data_size = sizeof(UltimotionDecodeContext),
423 .init = ulti_decode_init,
424 .close = ulti_decode_end,
425 FF_CODEC_DECODE_CB(ulti_decode_frame),
426 .p.capabilities = AV_CODEC_CAP_DR1,