| @@ -36,12 +36,14 @@ · 35 unmodified lines |
| 36 | 36 | #include "avcodec.h" |
| 37 | 37 | #include "bytestream.h" |
| 38 | 38 | #include "codec_internal.h" |
| 39 | +#include "copy_block.h" |
| 39 | 40 | #include "decode.h" |
| 40 | 41 | #include "eaidct.h" |
| 41 | 42 | #include "get_bits.h" |
| 42 | 43 | |
| 43 | 44 | typedef struct TgqContext { |
| 44 | 45 | AVCodecContext *avctx; |
| 46 | + AVFrame *last_frame; |
| 45 | 47 | int width, height; |
| 46 | 48 | int qtable[64]; |
| 47 | 49 | DECLARE_ALIGNED(16, int16_t, block)[6][64]; |
| @@ -53,6 +55,9 @@ static av_cold int tgq_decode_init(AVCodecContext *avctx) · 5 unmodified lines |
| 53 | 55 | s->avctx = avctx; |
| 54 | 56 | avctx->framerate = (AVRational){ 15, 1 }; |
| 55 | 57 | avctx->pix_fmt = AV_PIX_FMT_YUV420P; |
| 58 | + s->last_frame = av_frame_alloc(); |
| 59 | + if (!s->last_frame) |
| 60 | + return AVERROR(ENOMEM); |
| 56 | 61 | return 0; |
| 57 | 62 | } |
| 58 | 63 | |
| @@ -173,7 +178,33 @@ static int tgq_decode_mb(TgqContext *s, GetByteContext *gbyte, · 114 unmodified lines |
| 173 | 178 | tgq_idct_put_mb(s, s->block, frame, mb_x, mb_y); |
| 174 | 179 | bytestream2_skip(gbyte, mode); |
| 175 | 180 | } else { |
| 176 | | − if (mode == 3) { |
| 181 | + 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 | + } else if (mode == 3) { |
| 177 | 208 | memset(dc, bytestream2_get_byte(gbyte), 4); |
| 178 | 209 | dc[4] = bytestream2_get_byte(gbyte); |
| 179 | 210 | dc[5] = bytestream2_get_byte(gbyte); |
| @@ -228,9 +259,12 @@ static int tgq_decode_frame(AVCodecContext *avctx, AVFrame *frame, · 48 unmodified lines |
| 228 | 259 | s->height = bytestream2_get_le16u(&gbyte); |
| 229 | 260 | } |
| 230 | 261 | |
| 231 | | − ret = ff_set_dimensions(s->avctx, s->width, s->height); |
| 232 | | − if (ret < 0) |
| 233 | | − return ret; |
| 262 | + if (s->avctx->width != s->width || s->avctx->height != s->height) { |
| 263 | + av_frame_unref(s->last_frame); |
| 264 | + ret = ff_set_dimensions(s->avctx, s->width, s->height); |
| 265 | + if (ret < 0) |
| 266 | + return ret; |
| 267 | + } |
| 234 | 268 | |
| 235 | 269 | tgq_calculate_qtable(s, bytestream2_get_byteu(&gbyte)); |
| 236 | 270 | bytestream2_skipu(&gbyte, 3); |
| @@ -238,16 +272,27 @@ static int tgq_decode_frame(AVCodecContext *avctx, AVFrame *frame, · 1 unmodified line |
| 238 | 272 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
| 239 | 273 | return ret; |
| 240 | 274 | |
| 275 | + frame->flags |= AV_FRAME_FLAG_KEY; |
| 241 | 276 | for (y = 0; y < FFALIGN(avctx->height, 16) >> 4; y++) |
| 242 | 277 | for (x = 0; x < FFALIGN(avctx->width, 16) >> 4; x++) |
| 243 | 278 | if (tgq_decode_mb(s, &gbyte, frame, y, x) < 0) |
| 244 | 279 | return AVERROR_INVALIDDATA; |
| 245 | 280 | |
| 281 | + if ((ret = av_frame_replace(s->last_frame, frame)) < 0) |
| 282 | + return ret; |
| 283 | + |
| 246 | 284 | *got_frame = 1; |
| 247 | 285 | |
| 248 | 286 | return avpkt->size; |
| 249 | 287 | } |
| 250 | 288 | |
| 289 | +static av_cold int tgq_decode_close(AVCodecContext *avctx) |
| 290 | +{ |
| 291 | + TgqContext *s = avctx->priv_data; |
| 292 | + av_frame_free(&s->last_frame); |
| 293 | + return 0; |
| 294 | +} |
| 295 | + |
| 251 | 296 | const FFCodec ff_eatgq_decoder = { |
| 252 | 297 | .p.name = "eatgq", |
| 253 | 298 | CODEC_LONG_NAME("Electronic Arts TGQ video"), |
| @@ -255,6 +300,7 @@ const FFCodec ff_eatgq_decoder = { · 1 unmodified line |
| 255 | 300 | .p.id = AV_CODEC_ID_TGQ, |
| 256 | 301 | .priv_data_size = sizeof(TgqContext), |
| 257 | 302 | .init = tgq_decode_init, |
| 303 | + .close = tgq_decode_close, |
| 258 | 304 | FF_CODEC_DECODE_CB(tgq_decode_frame), |
| 259 | 305 | .p.capabilities = AV_CODEC_CAP_DR1, |
| 260 | 306 | }; |