You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

356 lines
11KB

  1. /*
  2. * Electronic Arts TGV Video Decoder
  3. * Copyright (c) 2007-2008 Peter Ross
  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 St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Electronic Arts TGV Video Decoder
  24. * by Peter Ross (pross@xvid.org)
  25. *
  26. * Technical details here:
  27. * http://wiki.multimedia.cx/index.php?title=Electronic_Arts_TGV
  28. */
  29. #include "avcodec.h"
  30. #define BITSTREAM_READER_LE
  31. #include "get_bits.h"
  32. #include "internal.h"
  33. #include "libavutil/imgutils.h"
  34. #include "libavutil/mem.h"
  35. #define EA_PREAMBLE_SIZE 8
  36. #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
  37. typedef struct TgvContext {
  38. AVCodecContext *avctx;
  39. AVFrame *last_frame;
  40. uint8_t *frame_buffer;
  41. int width,height;
  42. uint32_t palette[AVPALETTE_COUNT];
  43. int (*mv_codebook)[2];
  44. uint8_t (*block_codebook)[16];
  45. int num_mvs; ///< current length of mv_codebook
  46. int num_blocks_packed; ///< current length of block_codebook
  47. } TgvContext;
  48. static av_cold int tgv_decode_init(AVCodecContext *avctx)
  49. {
  50. TgvContext *s = avctx->priv_data;
  51. s->avctx = avctx;
  52. avctx->time_base = (AVRational){1, 15};
  53. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  54. s->last_frame = av_frame_alloc();
  55. if (!s->last_frame)
  56. return AVERROR(ENOMEM);
  57. return 0;
  58. }
  59. /**
  60. * Unpack buffer
  61. * @return 0 on success, -1 on critical buffer underflow
  62. */
  63. static int unpack(const uint8_t *src, const uint8_t *src_end,
  64. uint8_t *dst, int width, int height)
  65. {
  66. uint8_t *dst_end = dst + width*height;
  67. int size, size1, size2, offset, run;
  68. uint8_t *dst_start = dst;
  69. if (src[0] & 0x01)
  70. src += 5;
  71. else
  72. src += 2;
  73. if (src + 3 > src_end)
  74. return AVERROR_INVALIDDATA;
  75. size = AV_RB24(src);
  76. src += 3;
  77. while (size > 0 && src < src_end) {
  78. /* determine size1 and size2 */
  79. size1 = (src[0] & 3);
  80. if (src[0] & 0x80) { // 1
  81. if (src[0] & 0x40 ) { // 11
  82. if (src[0] & 0x20) { // 111
  83. if (src[0] < 0xFC) // !(111111)
  84. size1 = (((src[0] & 31) + 1) << 2);
  85. src++;
  86. size2 = 0;
  87. } else { // 110
  88. offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
  89. size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
  90. src += 4;
  91. }
  92. } else { // 10
  93. size1 = ((src[1] & 0xC0) >> 6);
  94. offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
  95. size2 = (src[0] & 0x3F) + 4;
  96. src += 3;
  97. }
  98. } else { // 0
  99. offset = ((src[0] & 0x60) << 3) + src[1] + 1;
  100. size2 = ((src[0] & 0x1C) >> 2) + 3;
  101. src += 2;
  102. }
  103. /* fetch strip from src */
  104. if (size1 > src_end - src)
  105. break;
  106. if (size1 > 0) {
  107. size -= size1;
  108. run = FFMIN(size1, dst_end - dst);
  109. memcpy(dst, src, run);
  110. dst += run;
  111. src += run;
  112. }
  113. if (size2 > 0) {
  114. if (dst - dst_start < offset)
  115. return 0;
  116. size -= size2;
  117. run = FFMIN(size2, dst_end - dst);
  118. av_memcpy_backptr(dst, offset, run);
  119. dst += run;
  120. }
  121. }
  122. return 0;
  123. }
  124. /**
  125. * Decode inter-frame
  126. * @return 0 on success, -1 on critical buffer underflow
  127. */
  128. static int tgv_decode_inter(TgvContext *s, AVFrame *frame,
  129. const uint8_t *buf, const uint8_t *buf_end)
  130. {
  131. int num_mvs;
  132. int num_blocks_raw;
  133. int num_blocks_packed;
  134. int vector_bits;
  135. int i,j,x,y;
  136. GetBitContext gb;
  137. int mvbits;
  138. const uint8_t *blocks_raw;
  139. if (buf + 12 > buf_end)
  140. return AVERROR_INVALIDDATA;
  141. num_mvs = AV_RL16(&buf[0]);
  142. num_blocks_raw = AV_RL16(&buf[2]);
  143. num_blocks_packed = AV_RL16(&buf[4]);
  144. vector_bits = AV_RL16(&buf[6]);
  145. buf += 12;
  146. if (vector_bits > MIN_CACHE_BITS || !vector_bits) {
  147. av_log(s->avctx, AV_LOG_ERROR,
  148. "Invalid value for motion vector bits: %d\n", vector_bits);
  149. return AVERROR_INVALIDDATA;
  150. }
  151. /* allocate codebook buffers as necessary */
  152. if (num_mvs > s->num_mvs) {
  153. s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
  154. s->num_mvs = num_mvs;
  155. }
  156. if (num_blocks_packed > s->num_blocks_packed) {
  157. s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16);
  158. s->num_blocks_packed = num_blocks_packed;
  159. }
  160. /* read motion vectors */
  161. mvbits = (num_mvs * 2 * 10 + 31) & ~31;
  162. if (buf + (mvbits >> 3) + 16 * num_blocks_raw + 8 * num_blocks_packed > buf_end)
  163. return AVERROR_INVALIDDATA;
  164. init_get_bits(&gb, buf, mvbits);
  165. for (i = 0; i < num_mvs; i++) {
  166. s->mv_codebook[i][0] = get_sbits(&gb, 10);
  167. s->mv_codebook[i][1] = get_sbits(&gb, 10);
  168. }
  169. buf += mvbits >> 3;
  170. /* note ptr to uncompressed blocks */
  171. blocks_raw = buf;
  172. buf += num_blocks_raw * 16;
  173. /* read compressed blocks */
  174. init_get_bits(&gb, buf, (buf_end - buf) << 3);
  175. for (i = 0; i < num_blocks_packed; i++) {
  176. int tmp[4];
  177. for (j = 0; j < 4; j++)
  178. tmp[j] = get_bits(&gb, 8);
  179. for (j = 0; j < 16; j++)
  180. s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
  181. }
  182. if (get_bits_left(&gb) < vector_bits *
  183. (s->avctx->height / 4) * (s->avctx->width / 4))
  184. return AVERROR_INVALIDDATA;
  185. /* read vectors and build frame */
  186. for (y = 0; y < s->avctx->height / 4; y++)
  187. for (x = 0; x < s->avctx->width / 4; x++) {
  188. unsigned int vector = get_bits(&gb, vector_bits);
  189. const uint8_t *src;
  190. int src_stride;
  191. if (vector < num_mvs) {
  192. int mx = x * 4 + s->mv_codebook[vector][0];
  193. int my = y * 4 + s->mv_codebook[vector][1];
  194. if (mx < 0 || mx + 4 > s->avctx->width ||
  195. my < 0 || my + 4 > s->avctx->height)
  196. continue;
  197. src = s->last_frame->data[0] + mx + my * s->last_frame->linesize[0];
  198. src_stride = s->last_frame->linesize[0];
  199. } else {
  200. int offset = vector - num_mvs;
  201. if (offset < num_blocks_raw)
  202. src = blocks_raw + 16*offset;
  203. else if (offset - num_blocks_raw < num_blocks_packed)
  204. src = s->block_codebook[offset - num_blocks_raw];
  205. else
  206. continue;
  207. src_stride = 4;
  208. }
  209. for (j = 0; j < 4; j++)
  210. for (i = 0; i < 4; i++)
  211. frame->data[0][(y * 4 + j) * frame->linesize[0] + (x * 4 + i)] =
  212. src[j * src_stride + i];
  213. }
  214. return 0;
  215. }
  216. static int tgv_decode_frame(AVCodecContext *avctx,
  217. void *data, int *got_frame,
  218. AVPacket *avpkt)
  219. {
  220. const uint8_t *buf = avpkt->data;
  221. int buf_size = avpkt->size;
  222. TgvContext *s = avctx->priv_data;
  223. const uint8_t *buf_end = buf + buf_size;
  224. AVFrame *frame = data;
  225. int chunk_type, ret;
  226. chunk_type = AV_RL32(&buf[0]);
  227. buf += EA_PREAMBLE_SIZE;
  228. if (chunk_type == kVGT_TAG) {
  229. int pal_count, i;
  230. if (buf + 12 > buf_end) {
  231. av_log(avctx, AV_LOG_WARNING, "truncated header\n");
  232. return AVERROR_INVALIDDATA;
  233. }
  234. s->width = AV_RL16(&buf[0]);
  235. s->height = AV_RL16(&buf[2]);
  236. if (s->avctx->width != s->width || s->avctx->height != s->height) {
  237. av_freep(&s->frame_buffer);
  238. av_frame_unref(s->last_frame);
  239. if ((ret = ff_set_dimensions(s->avctx, s->width, s->height)) < 0)
  240. return ret;
  241. }
  242. pal_count = AV_RL16(&buf[6]);
  243. buf += 12;
  244. for (i = 0; i < pal_count && i < AVPALETTE_COUNT && buf + 2 < buf_end; i++) {
  245. s->palette[i] = AV_RB24(buf);
  246. buf += 3;
  247. }
  248. }
  249. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
  250. return ret;
  251. memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
  252. if (chunk_type == kVGT_TAG) {
  253. int y;
  254. frame->key_frame = 1;
  255. frame->pict_type = AV_PICTURE_TYPE_I;
  256. if (!s->frame_buffer &&
  257. !(s->frame_buffer = av_malloc(s->width * s->height)))
  258. return AVERROR(ENOMEM);
  259. if (unpack(buf, buf_end, s->frame_buffer, s->avctx->width, s->avctx->height) < 0) {
  260. av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
  261. return AVERROR_INVALIDDATA;
  262. }
  263. for (y = 0; y < s->height; y++)
  264. memcpy(frame->data[0] + y * frame->linesize[0],
  265. s->frame_buffer + y * s->width,
  266. s->width);
  267. } else {
  268. if (!s->last_frame->data[0]) {
  269. av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
  270. return buf_size;
  271. }
  272. frame->key_frame = 0;
  273. frame->pict_type = AV_PICTURE_TYPE_P;
  274. if (tgv_decode_inter(s, frame, buf, buf_end) < 0) {
  275. av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
  276. return AVERROR_INVALIDDATA;
  277. }
  278. }
  279. av_frame_unref(s->last_frame);
  280. if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
  281. return ret;
  282. *got_frame = 1;
  283. return buf_size;
  284. }
  285. static av_cold int tgv_decode_end(AVCodecContext *avctx)
  286. {
  287. TgvContext *s = avctx->priv_data;
  288. av_frame_free(&s->last_frame);
  289. av_freep(&s->frame_buffer);
  290. av_free(s->mv_codebook);
  291. av_free(s->block_codebook);
  292. return 0;
  293. }
  294. AVCodec ff_eatgv_decoder = {
  295. .name = "eatgv",
  296. .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
  297. .type = AVMEDIA_TYPE_VIDEO,
  298. .id = AV_CODEC_ID_TGV,
  299. .priv_data_size = sizeof(TgvContext),
  300. .init = tgv_decode_init,
  301. .close = tgv_decode_end,
  302. .decode = tgv_decode_frame,
  303. .capabilities = CODEC_CAP_DR1,
  304. };