FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ulti.c
Date: 2021-09-26 18:22:30
Exec Total Coverage
Lines: 201 257 78.2%
Branches: 66 87 75.9%

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