FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/eatgq.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 133 174 76.4%
Functions: 9 9 100.0%
Branches: 49 85 57.6%

Line Branch Exec Source
1 /*
2 * Electronic Arts TGQ Video Decoder
3 * Copyright (c) 2007-2008 Peter Ross <pross@xvid.org>
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 St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Electronic Arts TGQ Video Decoder
25 * @author Peter Ross <pross@xvid.org>
26 *
27 * Technical details here:
28 * http://wiki.multimedia.cx/index.php?title=Electronic_Arts_TGQ
29 */
30
31 #define BITSTREAM_READER_LE
32
33 #include "libavutil/mem_internal.h"
34
35 #include "aandcttab.h"
36 #include "avcodec.h"
37 #include "bytestream.h"
38 #include "codec_internal.h"
39 #include "copy_block.h"
40 #include "decode.h"
41 #include "eaidct.h"
42 #include "get_bits.h"
43
44 typedef struct TgqContext {
45 AVCodecContext *avctx;
46 AVFrame *last_frame;
47 int width, height;
48 int qtable[64];
49 DECLARE_ALIGNED(16, int16_t, block)[6][64];
50 } TgqContext;
51
52 2 static av_cold int tgq_decode_init(AVCodecContext *avctx)
53 {
54 2 TgqContext *s = avctx->priv_data;
55 2 s->avctx = avctx;
56 2 avctx->framerate = (AVRational){ 15, 1 };
57 2 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
58 2 s->last_frame = av_frame_alloc();
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->last_frame)
60 return AVERROR(ENOMEM);
61 2 return 0;
62 }
63
64 101826 static int tgq_decode_block(TgqContext *s, int16_t block[64], GetBitContext *gb)
65 {
66 101826 const uint8_t *scantable = ff_zigzag_direct;
67 int i, j, value;
68 101826 block[0] = get_sbits(gb, 8) * s->qtable[0];
69
2/2
✓ Branch 0 taken 1463527 times.
✓ Branch 1 taken 101826 times.
1667179 for (i = 1; i < 64;) {
70
6/7
✗ Branch 1 not taken.
✓ Branch 2 taken 152397 times.
✓ Branch 3 taken 218259 times.
✓ Branch 4 taken 148778 times.
✓ Branch 5 taken 333783 times.
✓ Branch 6 taken 339292 times.
✓ Branch 7 taken 271018 times.
1463527 switch (show_bits(gb, 3)) {
71 152397 case 4:
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 152397 times.
152397 if (i >= 63)
73 return AVERROR_INVALIDDATA;
74 152397 block[scantable[i++]] = 0;
75 370656 case 0:
76 370656 block[scantable[i++]] = 0;
77 370656 skip_bits(gb, 3);
78 370656 break;
79 148778 case 5:
80 case 1:
81 148778 skip_bits(gb, 2);
82 148778 value = get_bits(gb, 6);
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 148778 times.
148778 if (value > 64 - i)
84 return AVERROR_INVALIDDATA;
85
2/2
✓ Branch 0 taken 4947892 times.
✓ Branch 1 taken 148778 times.
5096670 for (j = 0; j < value; j++)
86 4947892 block[scantable[i++]] = 0;
87 148778 break;
88 333783 case 6:
89 333783 skip_bits(gb, 3);
90 333783 block[scantable[i]] = -s->qtable[scantable[i]];
91 333783 i++;
92 333783 break;
93 339292 case 2:
94 339292 skip_bits(gb, 3);
95 339292 block[scantable[i]] = s->qtable[scantable[i]];
96 339292 i++;
97 339292 break;
98 271018 case 7: // 111b
99 case 3: // 011b
100 271018 skip_bits(gb, 2);
101
2/2
✓ Branch 1 taken 1440 times.
✓ Branch 2 taken 269578 times.
271018 if (show_bits(gb, 6) == 0x3F) {
102 1440 skip_bits(gb, 6);
103 1440 block[scantable[i]] = get_sbits(gb, 8) * s->qtable[scantable[i]];
104 } else {
105 269578 block[scantable[i]] = get_sbits(gb, 6) * s->qtable[scantable[i]];
106 }
107 271018 i++;
108 271018 break;
109 }
110 }
111 101826 block[0] += 128 << 4;
112 101826 return 0;
113 }
114
115 16971 static void tgq_idct_put_mb(TgqContext *s, int16_t (*block)[64], AVFrame *frame,
116 int mb_x, int mb_y)
117 {
118 16971 ptrdiff_t linesize = frame->linesize[0];
119 16971 uint8_t *dest_y = frame->data[0] + (mb_y * 16 * linesize) + mb_x * 16;
120 16971 uint8_t *dest_cb = frame->data[1] + (mb_y * 8 * frame->linesize[1]) + mb_x * 8;
121 16971 uint8_t *dest_cr = frame->data[2] + (mb_y * 8 * frame->linesize[2]) + mb_x * 8;
122
123 16971 ff_ea_idct_put_c(dest_y , linesize, block[0]);
124 16971 ff_ea_idct_put_c(dest_y + 8, linesize, block[1]);
125 16971 ff_ea_idct_put_c(dest_y + 8 * linesize , linesize, block[2]);
126 16971 ff_ea_idct_put_c(dest_y + 8 * linesize + 8, linesize, block[3]);
127
1/2
✓ Branch 0 taken 16971 times.
✗ Branch 1 not taken.
16971 if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
128 16971 ff_ea_idct_put_c(dest_cb, frame->linesize[1], block[4]);
129 16971 ff_ea_idct_put_c(dest_cr, frame->linesize[2], block[5]);
130 }
131 16971 }
132
133 50508 static inline void tgq_dconly(TgqContext *s, unsigned char *dst,
134 ptrdiff_t dst_stride, int dc)
135 {
136 50508 int level = av_clip_uint8((dc*s->qtable[0] + 2056) >> 4);
137 int j;
138
2/2
✓ Branch 0 taken 404064 times.
✓ Branch 1 taken 50508 times.
454572 for (j = 0; j < 8; j++)
139 404064 memset(dst + j * dst_stride, level, 8);
140 50508 }
141
142 8418 static void tgq_idct_put_mb_dconly(TgqContext *s, AVFrame *frame,
143 int mb_x, int mb_y, const int8_t *dc)
144 {
145 8418 ptrdiff_t linesize = frame->linesize[0];
146 8418 uint8_t *dest_y = frame->data[0] + (mb_y * 16 * linesize) + mb_x * 16;
147 8418 uint8_t *dest_cb = frame->data[1] + (mb_y * 8 * frame->linesize[1]) + mb_x * 8;
148 8418 uint8_t *dest_cr = frame->data[2] + (mb_y * 8 * frame->linesize[2]) + mb_x * 8;
149 8418 tgq_dconly(s, dest_y, linesize, dc[0]);
150 8418 tgq_dconly(s, dest_y + 8, linesize, dc[1]);
151 8418 tgq_dconly(s, dest_y + 8 * linesize, linesize, dc[2]);
152 8418 tgq_dconly(s, dest_y + 8 * linesize + 8, linesize, dc[3]);
153
1/2
✓ Branch 0 taken 8418 times.
✗ Branch 1 not taken.
8418 if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
154 8418 tgq_dconly(s, dest_cb, frame->linesize[1], dc[4]);
155 8418 tgq_dconly(s, dest_cr, frame->linesize[2], dc[5]);
156 }
157 8418 }
158
159 25389 static int tgq_decode_mb(TgqContext *s, GetByteContext *gbyte,
160 AVFrame *frame, int mb_y, int mb_x)
161 {
162 int mode;
163 int i;
164 int8_t dc[6];
165
166 25389 mode = bytestream2_get_byte(gbyte);
167
2/2
✓ Branch 0 taken 16971 times.
✓ Branch 1 taken 8418 times.
25389 if (mode > 12) {
168 GetBitContext gb;
169
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16971 times.
16971 int ret = init_get_bits8(&gb, gbyte->buffer, FFMIN(bytestream2_get_bytes_left(gbyte), mode));
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16971 times.
16971 if (ret < 0)
171 return ret;
172
173
2/2
✓ Branch 0 taken 101826 times.
✓ Branch 1 taken 16971 times.
118797 for (i = 0; i < 6; i++) {
174 101826 int ret = tgq_decode_block(s, s->block[i], &gb);
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101826 times.
101826 if (ret < 0)
176 return ret;
177 }
178 16971 tgq_idct_put_mb(s, s->block, frame, mb_x, mb_y);
179 16971 bytestream2_skip(gbyte, mode);
180 } else {
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8418 times.
8418 if (mode == 1) {
182 int x, y;
183 int mv = bytestream2_get_byte(gbyte);
184 int mv_x = mv >> 4;
185 int mv_y = mv & 0x0F;
186 if (!s->last_frame->data[0]) {
187 av_log(s->avctx, AV_LOG_ERROR, "missing reference frame\n");
188 return -1;
189 }
190 if (mv_x >= 8) mv_x -= 16;
191 if (mv_y >= 8) mv_y -= 16;
192 x = mb_x * 16 - mv_x;
193 y = mb_y * 16 - mv_y;
194 if (x < 0 || x + 16 > s->width || y < 0 || y + 16 > s->height) {
195 av_log(s->avctx, AV_LOG_ERROR, "invalid motion vector\n");
196 return -1;
197 }
198 copy_block16(frame->data[0] + (mb_y * 16 * frame->linesize[0]) + mb_x * 16,
199 s->last_frame->data[0] + y * s->last_frame->linesize[0] + x,
200 frame->linesize[0], s->last_frame->linesize[0], 16);
201 for (int p = 1; p < 3; p++)
202 copy_block8(frame->data[p] + (mb_y * 8 * frame->linesize[p]) + mb_x * 8,
203 s->last_frame->data[p] + (y >> 1) * s->last_frame->linesize[p] + (x >> 1),
204 frame->linesize[p], s->last_frame->linesize[p], 8);
205 frame->flags &= ~AV_FRAME_FLAG_KEY;
206 return 0;
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8418 times.
8418 } else if (mode == 3) {
208 memset(dc, bytestream2_get_byte(gbyte), 4);
209 dc[4] = bytestream2_get_byte(gbyte);
210 dc[5] = bytestream2_get_byte(gbyte);
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8418 times.
8418 } else if (mode == 6) {
212 bytestream2_get_buffer(gbyte, dc, 6);
213
1/2
✓ Branch 0 taken 8418 times.
✗ Branch 1 not taken.
8418 } else if (mode == 12) {
214
2/2
✓ Branch 0 taken 50508 times.
✓ Branch 1 taken 8418 times.
58926 for (i = 0; i < 6; i++) {
215 50508 dc[i] = bytestream2_get_byte(gbyte);
216 50508 bytestream2_skip(gbyte, 1);
217 }
218 } else {
219 av_log(s->avctx, AV_LOG_ERROR, "unsupported mb mode %i\n", mode);
220 return -1;
221 }
222 8418 tgq_idct_put_mb_dconly(s, frame, mb_x, mb_y, dc);
223 }
224 25389 return 0;
225 }
226
227 279 static void tgq_calculate_qtable(TgqContext *s, int quant)
228 {
229 int i, j;
230 279 const int a = (14 * (100 - quant)) / 100 + 1;
231 279 const int b = (11 * (100 - quant)) / 100 + 4;
232
2/2
✓ Branch 0 taken 2232 times.
✓ Branch 1 taken 279 times.
2511 for (j = 0; j < 8; j++)
233
2/2
✓ Branch 0 taken 17856 times.
✓ Branch 1 taken 2232 times.
20088 for (i = 0; i < 8; i++)
234 17856 s->qtable[j * 8 + i] = ((a * (j + i) / (7 + 7) + b) *
235 17856 ff_inv_aanscales[j * 8 + i]) >> (14 - 4);
236 279 }
237
238 279 static int tgq_decode_frame(AVCodecContext *avctx, AVFrame *frame,
239 int *got_frame, AVPacket *avpkt)
240 {
241 279 const uint8_t *buf = avpkt->data;
242 279 int buf_size = avpkt->size;
243 279 TgqContext *s = avctx->priv_data;
244 GetByteContext gbyte;
245 int x, y, ret;
246 int big_endian;
247
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
279 if (buf_size < 16) {
249 av_log(avctx, AV_LOG_WARNING, "truncated header\n");
250 return AVERROR_INVALIDDATA;
251 }
252 279 big_endian = AV_RL32(&buf[4]) > 0x000FFFFF;
253 279 bytestream2_init(&gbyte, buf + 8, buf_size - 8);
254
1/2
✓ Branch 0 taken 279 times.
✗ Branch 1 not taken.
279 if (big_endian) {
255 279 s->width = bytestream2_get_be16u(&gbyte);
256 279 s->height = bytestream2_get_be16u(&gbyte);
257 } else {
258 s->width = bytestream2_get_le16u(&gbyte);
259 s->height = bytestream2_get_le16u(&gbyte);
260 }
261
262
3/4
✓ Branch 0 taken 278 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 278 times.
279 if (s->avctx->width != s->width || s->avctx->height != s->height) {
263 1 av_frame_unref(s->last_frame);
264 1 ret = ff_set_dimensions(s->avctx, s->width, s->height);
265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
266 return ret;
267 }
268
269 279 tgq_calculate_qtable(s, bytestream2_get_byteu(&gbyte));
270 279 bytestream2_skipu(&gbyte, 3);
271
272
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 279 times.
279 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
273 return ret;
274
275 279 frame->flags |= AV_FRAME_FLAG_KEY;
276
2/2
✓ Branch 0 taken 1953 times.
✓ Branch 1 taken 279 times.
2232 for (y = 0; y < FFALIGN(avctx->height, 16) >> 4; y++)
277
2/2
✓ Branch 0 taken 25389 times.
✓ Branch 1 taken 1953 times.
27342 for (x = 0; x < FFALIGN(avctx->width, 16) >> 4; x++)
278
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25389 times.
25389 if (tgq_decode_mb(s, &gbyte, frame, y, x) < 0)
279 return AVERROR_INVALIDDATA;
280
281
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 279 times.
279 if ((ret = av_frame_replace(s->last_frame, frame)) < 0)
282 return ret;
283
284 279 *got_frame = 1;
285
286 279 return avpkt->size;
287 }
288
289 2 static av_cold int tgq_decode_close(AVCodecContext *avctx)
290 {
291 2 TgqContext *s = avctx->priv_data;
292 2 av_frame_free(&s->last_frame);
293 2 return 0;
294 }
295
296 const FFCodec ff_eatgq_decoder = {
297 .p.name = "eatgq",
298 CODEC_LONG_NAME("Electronic Arts TGQ video"),
299 .p.type = AVMEDIA_TYPE_VIDEO,
300 .p.id = AV_CODEC_ID_TGQ,
301 .priv_data_size = sizeof(TgqContext),
302 .init = tgq_decode_init,
303 .close = tgq_decode_close,
304 FF_CODEC_DECODE_CB(tgq_decode_frame),
305 .p.capabilities = AV_CODEC_CAP_DR1,
306 };
307