Libav
ulti.c
Go to the documentation of this file.
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 
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 {
44 
46 {
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;
55 
56  s->frame = av_frame_alloc();
57  if (!s->frame)
58  return AVERROR(ENOMEM);
59 
60  return 0;
61 }
62 
65 
66  av_frame_free(&s->frame);
67 
68  return 0;
69 }
70 
71 static const int block_coords[8] = // 4x4 block coords in 8x8 superblock
72  { 0, 0, 0, 4, 4, 4, 4, 0};
73 
74 static const int angle_by_index[4] = { 0, 2, 6, 12};
75 
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};
86 
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};
90 
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)
95 {
96  uint8_t *y_plane, *cr_plane, *cb_plane;
97  int i;
98 
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];
102 
103  cr_plane[0] = ulti_chromas[chroma >> 4];
104 
105  cb_plane[0] = ulti_chromas[chroma & 0xF];
106 
107 
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];
112  }
113  }
114 }
115 
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)
119 {
120  uint8_t Luma[16];
121  int mask, i;
122  for(mask = 0x80, i = 0; mask; mask >>= 1, i++) {
123  if(f0 & mask)
124  Luma[i] = Y1;
125  else
126  Luma[i] = Y0;
127  }
128 
129  for(mask = 0x80, i = 8; mask; mask >>= 1, i++) {
130  if(f1 & mask)
131  Luma[i] = Y1;
132  else
133  Luma[i] = Y0;
134  }
135 
136  ulti_convert_yuv(frame, x, y, Luma, chroma);
137 }
138 
139 /* fill block with some gradient */
140 static void ulti_grad(AVFrame *frame, int x, int y, uint8_t *Y, int chroma, int angle)
141 {
142  uint8_t Luma[16];
143  if(angle & 8) { //reverse order
144  int t;
145  angle &= 0x7;
146  t = Y[0];
147  Y[0] = Y[3];
148  Y[3] = t;
149  t = Y[1];
150  Y[1] = Y[2];
151  Y[2] = t;
152  }
153  switch(angle){
154  case 0:
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];
159  break;
160  case 1:
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];
165  break;
166  case 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];
171  break;
172  case 3:
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];
177  break;
178  case 4:
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];
183  break;
184  case 5:
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];
189  break;
190  case 6:
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];
195  break;
196  case 7:
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];
201  break;
202  default:
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];
207  break;
208  }
209 
210  ulti_convert_yuv(frame, x, y, Luma, chroma);
211 }
212 
214  void *data, int *got_frame,
215  AVPacket *avpkt)
216 {
217  const uint8_t *buf = avpkt->data;
218  int buf_size = avpkt->size;
220  int modifier = 0;
221  int uniq = 0;
222  int mode = 0;
223  int blocks = 0;
224  int done = 0;
225  int x = 0, y = 0;
226  int i, ret;
227  int skip;
228  int tmp;
229 
230  if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) {
231  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
232  return ret;
233  }
234 
235  bytestream2_init(&s->gb, buf, buf_size);
236 
237  while(!done) {
238  int idx;
239  if(blocks >= s->blocks || y >= s->height)
240  break;//all blocks decoded
241 
242  if (bytestream2_get_bytes_left(&s->gb) < 1)
243  goto err;
244  idx = bytestream2_get_byteu(&s->gb);
245  if((idx & 0xF8) == 0x70) {
246  switch(idx) {
247  case 0x70: //change modifier
248  modifier = bytestream2_get_byte(&s->gb);
249  if(modifier>1)
250  av_log(avctx, AV_LOG_INFO, "warning: modifier must be 0 or 1, got %i\n", modifier);
251  break;
252  case 0x71: // set uniq flag
253  uniq = 1;
254  break;
255  case 0x72: //toggle mode
256  mode = !mode;
257  break;
258  case 0x73: //end-of-frame
259  done = 1;
260  break;
261  case 0x74: //skip some blocks
262  skip = bytestream2_get_byte(&s->gb);
263  if ((blocks + skip) >= s->blocks)
264  break;
265  blocks += skip;
266  x += skip * 8;
267  while(x >= s->width) {
268  x -= s->width;
269  y += 8;
270  }
271  break;
272  default:
273  av_log(avctx, AV_LOG_INFO, "warning: unknown escape 0x%02X\n", idx);
274  }
275  } else { //handle one block
276  int code;
277  int cf;
278  int angle = 0;
279  uint8_t Y[4]; // luma samples of block
280  int tx = 0, ty = 0; //coords of subblock
281  int chroma = 0;
282  if (mode || uniq) {
283  uniq = 0;
284  cf = 1;
285  chroma = 0;
286  } else {
287  cf = 0;
288  if (idx) {
289  chroma = bytestream2_get_byte(&s->gb);
290  }
291  }
292  for (i = 0; i < 4; i++) { // for every subblock
293  code = (idx >> (6 - i*2)) & 3; //extract 2 bits
294  if(!code) //skip subblock
295  continue;
296  if(cf) {
297  chroma = bytestream2_get_byte(&s->gb);
298  }
299  tx = x + block_coords[i * 2];
300  ty = y + block_coords[(i * 2) + 1];
301  switch(code) {
302  case 1:
303  tmp = bytestream2_get_byte(&s->gb);
304 
305  angle = angle_by_index[(tmp >> 6) & 0x3];
306 
307  Y[0] = tmp & 0x3F;
308  Y[1] = Y[0];
309 
310  if (angle) {
311  Y[2] = Y[0]+1;
312  if (Y[2] > 0x3F)
313  Y[2] = 0x3F;
314  Y[3] = Y[2];
315  } else {
316  Y[2] = Y[0];
317  Y[3] = Y[0];
318  }
319  break;
320 
321  case 2:
322  if (modifier) { // unpack four luma samples
323  tmp = bytestream2_get_be24(&s->gb);
324 
325  Y[0] = (tmp >> 18) & 0x3F;
326  Y[1] = (tmp >> 12) & 0x3F;
327  Y[2] = (tmp >> 6) & 0x3F;
328  Y[3] = tmp & 0x3F;
329  angle = 16;
330  } else { // retrieve luma samples from codebook
331  tmp = bytestream2_get_be16(&s->gb);
332 
333  angle = (tmp >> 12) & 0xF;
334  tmp &= 0xFFF;
335  tmp <<= 2;
336  Y[0] = s->ulti_codebook[tmp];
337  Y[1] = s->ulti_codebook[tmp + 1];
338  Y[2] = s->ulti_codebook[tmp + 2];
339  Y[3] = s->ulti_codebook[tmp + 3];
340  }
341  break;
342 
343  case 3:
344  if (modifier) { // all 16 luma samples
345  uint8_t Luma[16];
346 
347  if (bytestream2_get_bytes_left(&s->gb) < 12)
348  goto err;
349  tmp = bytestream2_get_be24u(&s->gb);
350  Luma[0] = (tmp >> 18) & 0x3F;
351  Luma[1] = (tmp >> 12) & 0x3F;
352  Luma[2] = (tmp >> 6) & 0x3F;
353  Luma[3] = tmp & 0x3F;
354 
355  tmp = bytestream2_get_be24u(&s->gb);
356  Luma[4] = (tmp >> 18) & 0x3F;
357  Luma[5] = (tmp >> 12) & 0x3F;
358  Luma[6] = (tmp >> 6) & 0x3F;
359  Luma[7] = tmp & 0x3F;
360 
361  tmp = bytestream2_get_be24u(&s->gb);
362  Luma[8] = (tmp >> 18) & 0x3F;
363  Luma[9] = (tmp >> 12) & 0x3F;
364  Luma[10] = (tmp >> 6) & 0x3F;
365  Luma[11] = tmp & 0x3F;
366 
367  tmp = bytestream2_get_be24u(&s->gb);
368  Luma[12] = (tmp >> 18) & 0x3F;
369  Luma[13] = (tmp >> 12) & 0x3F;
370  Luma[14] = (tmp >> 6) & 0x3F;
371  Luma[15] = tmp & 0x3F;
372 
373  ulti_convert_yuv(s->frame, tx, ty, Luma, chroma);
374  } else {
375  if (bytestream2_get_bytes_left(&s->gb) < 4)
376  goto err;
377  tmp = bytestream2_get_byteu(&s->gb);
378  if(tmp & 0x80) {
379  angle = (tmp >> 4) & 0x7;
380  tmp = (tmp << 8) + bytestream2_get_byteu(&s->gb);
381  Y[0] = (tmp >> 6) & 0x3F;
382  Y[1] = tmp & 0x3F;
383  Y[2] = bytestream2_get_byteu(&s->gb) & 0x3F;
384  Y[3] = bytestream2_get_byteu(&s->gb) & 0x3F;
385  ulti_grad(s->frame, tx, ty, Y, chroma, angle); //draw block
386  } else { // some patterns
387  int f0, f1;
388  f0 = bytestream2_get_byteu(&s->gb);
389  f1 = tmp;
390  Y[0] = bytestream2_get_byteu(&s->gb) & 0x3F;
391  Y[1] = bytestream2_get_byteu(&s->gb) & 0x3F;
392  ulti_pattern(s->frame, tx, ty, f1, f0, Y[0], Y[1], chroma);
393  }
394  }
395  break;
396  }
397  if(code != 3)
398  ulti_grad(s->frame, tx, ty, Y, chroma, angle); // draw block
399  }
400  blocks++;
401  x += 8;
402  if(x >= s->width) {
403  x = 0;
404  y += 8;
405  }
406  }
407  }
408 
409  *got_frame = 1;
410  if ((ret = av_frame_ref(data, s->frame)) < 0)
411  return ret;
412 
413  return buf_size;
414 
415 err:
416  av_log(avctx, AV_LOG_ERROR,
417  "Insufficient data\n");
418  return AVERROR_INVALIDDATA;
419 }
420 
422  .name = "ultimotion",
423  .long_name = NULL_IF_CONFIG_SMALL("IBM UltiMotion"),
424  .type = AVMEDIA_TYPE_VIDEO,
425  .id = AV_CODEC_ID_ULTI,
426  .priv_data_size = sizeof(UltimotionDecodeContext),
430  .capabilities = CODEC_CAP_DR1,
431 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
static av_cold int ulti_decode_end(AVCodecContext *avctx)
Definition: ulti.c:63
int size
Definition: avcodec.h:974
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1254
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
AVCodec.
Definition: avcodec.h:2796
AVCodec ff_ulti_decoder
Definition: ulti.c:421
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:57
AVFrame * frame
Definition: ulti.c:40
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:188
GetByteContext gb
Definition: ulti.c:42
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
Definition: vf_drawbox.c:37
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
static const uint16_t mask[17]
Definition: lzw.c:38
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:69
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:149
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2803
static const unsigned char ulti_codebook[16384]
Definition: ulti_cb.h:25
AVCodecContext * avctx
Definition: ulti.c:38
static const int angle_by_index[4]
Definition: ulti.c:74
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: utils.c:808
int width
picture width / height.
Definition: avcodec.h:1224
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
static int ulti_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: ulti.c:213
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
static av_cold int ulti_decode_init(AVCodecContext *avctx)
Definition: ulti.c:45
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:71
const uint8_t * ulti_codebook
Definition: ulti.c:41
static void ulti_convert_yuv(AVFrame *frame, int x, int y, uint8_t *luma, int chroma)
Definition: ulti.c:93
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
common internal api header.
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1092
static void ulti_grad(AVFrame *frame, int x, int y, uint8_t *Y, int chroma, int angle)
Definition: ulti.c:140
static void ulti_pattern(AVFrame *frame, int x, int y, int f0, int f1, int Y0, int Y1, int chroma)
Definition: ulti.c:117
static const uint8_t ulti_chromas[16]
Definition: ulti.c:87
static const int block_coords[8]
Definition: ulti.c:71
This structure stores compressed data.
Definition: avcodec.h:950
static const uint8_t ulti_lumas[64]
Definition: ulti.c:77