FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/eatgq.c
Date: 2026-04-30 08:04:40
Exec Total Coverage
Lines: 133 175 76.0%
Functions: 9 9 100.0%
Branches: 49 87 56.3%

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