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.

363 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 "libavutil/imgutils.h"
  30. #include "libavutil/mem.h"
  31. #define BITSTREAM_READER_LE
  32. #include "avcodec.h"
  33. #include "get_bits.h"
  34. #include "internal.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->framerate = (AVRational){ 15, 1 };
  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. int err = av_reallocp(&s->mv_codebook, num_mvs * 2 * sizeof(int));
  154. if (err < 0)
  155. return err;
  156. s->num_mvs = num_mvs;
  157. }
  158. if (num_blocks_packed > s->num_blocks_packed) {
  159. int err;
  160. if ((err = av_reallocp(&s->block_codebook, num_blocks_packed * 16)) < 0) {
  161. s->num_blocks_packed = 0;
  162. return err;
  163. }
  164. s->num_blocks_packed = num_blocks_packed;
  165. }
  166. /* read motion vectors */
  167. mvbits = (num_mvs * 2 * 10 + 31) & ~31;
  168. if (buf + (mvbits >> 3) + 16 * num_blocks_raw + 8 * num_blocks_packed > buf_end)
  169. return AVERROR_INVALIDDATA;
  170. init_get_bits(&gb, buf, mvbits);
  171. for (i = 0; i < num_mvs; i++) {
  172. s->mv_codebook[i][0] = get_sbits(&gb, 10);
  173. s->mv_codebook[i][1] = get_sbits(&gb, 10);
  174. }
  175. buf += mvbits >> 3;
  176. /* note ptr to uncompressed blocks */
  177. blocks_raw = buf;
  178. buf += num_blocks_raw * 16;
  179. /* read compressed blocks */
  180. init_get_bits(&gb, buf, (buf_end - buf) << 3);
  181. for (i = 0; i < num_blocks_packed; i++) {
  182. int tmp[4];
  183. for (j = 0; j < 4; j++)
  184. tmp[j] = get_bits(&gb, 8);
  185. for (j = 0; j < 16; j++)
  186. s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
  187. }
  188. if (get_bits_left(&gb) < vector_bits *
  189. (s->avctx->height / 4) * (s->avctx->width / 4))
  190. return AVERROR_INVALIDDATA;
  191. /* read vectors and build frame */
  192. for (y = 0; y < s->avctx->height / 4; y++)
  193. for (x = 0; x < s->avctx->width / 4; x++) {
  194. unsigned int vector = get_bits(&gb, vector_bits);
  195. const uint8_t *src;
  196. int src_stride;
  197. if (vector < num_mvs) {
  198. int mx = x * 4 + s->mv_codebook[vector][0];
  199. int my = y * 4 + s->mv_codebook[vector][1];
  200. if (mx < 0 || mx + 4 > s->avctx->width ||
  201. my < 0 || my + 4 > s->avctx->height)
  202. continue;
  203. src = s->last_frame->data[0] + mx + my * s->last_frame->linesize[0];
  204. src_stride = s->last_frame->linesize[0];
  205. } else {
  206. int offset = vector - num_mvs;
  207. if (offset < num_blocks_raw)
  208. src = blocks_raw + 16*offset;
  209. else if (offset - num_blocks_raw < num_blocks_packed)
  210. src = s->block_codebook[offset - num_blocks_raw];
  211. else
  212. continue;
  213. src_stride = 4;
  214. }
  215. for (j = 0; j < 4; j++)
  216. for (i = 0; i < 4; i++)
  217. frame->data[0][(y * 4 + j) * frame->linesize[0] + (x * 4 + i)] =
  218. src[j * src_stride + i];
  219. }
  220. return 0;
  221. }
  222. static int tgv_decode_frame(AVCodecContext *avctx,
  223. void *data, int *got_frame,
  224. AVPacket *avpkt)
  225. {
  226. const uint8_t *buf = avpkt->data;
  227. int buf_size = avpkt->size;
  228. TgvContext *s = avctx->priv_data;
  229. const uint8_t *buf_end = buf + buf_size;
  230. AVFrame *frame = data;
  231. int chunk_type, ret;
  232. chunk_type = AV_RL32(&buf[0]);
  233. buf += EA_PREAMBLE_SIZE;
  234. if (chunk_type == kVGT_TAG) {
  235. int pal_count, i;
  236. if (buf + 12 > buf_end) {
  237. av_log(avctx, AV_LOG_WARNING, "truncated header\n");
  238. return AVERROR_INVALIDDATA;
  239. }
  240. s->width = AV_RL16(&buf[0]);
  241. s->height = AV_RL16(&buf[2]);
  242. if (s->avctx->width != s->width || s->avctx->height != s->height) {
  243. av_freep(&s->frame_buffer);
  244. av_frame_unref(s->last_frame);
  245. if ((ret = ff_set_dimensions(s->avctx, s->width, s->height)) < 0)
  246. return ret;
  247. }
  248. pal_count = AV_RL16(&buf[6]);
  249. buf += 12;
  250. for (i = 0; i < pal_count && i < AVPALETTE_COUNT && buf + 2 < buf_end; i++) {
  251. s->palette[i] = AV_RB24(buf);
  252. buf += 3;
  253. }
  254. }
  255. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
  256. return ret;
  257. memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
  258. if (chunk_type == kVGT_TAG) {
  259. int y;
  260. frame->key_frame = 1;
  261. frame->pict_type = AV_PICTURE_TYPE_I;
  262. if (!s->frame_buffer &&
  263. !(s->frame_buffer = av_malloc(s->width * s->height)))
  264. return AVERROR(ENOMEM);
  265. if (unpack(buf, buf_end, s->frame_buffer, s->avctx->width, s->avctx->height) < 0) {
  266. av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
  267. return AVERROR_INVALIDDATA;
  268. }
  269. for (y = 0; y < s->height; y++)
  270. memcpy(frame->data[0] + y * frame->linesize[0],
  271. s->frame_buffer + y * s->width,
  272. s->width);
  273. } else {
  274. if (!s->last_frame->data[0]) {
  275. av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
  276. return buf_size;
  277. }
  278. frame->key_frame = 0;
  279. frame->pict_type = AV_PICTURE_TYPE_P;
  280. if (tgv_decode_inter(s, frame, buf, buf_end) < 0) {
  281. av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
  282. return AVERROR_INVALIDDATA;
  283. }
  284. }
  285. av_frame_unref(s->last_frame);
  286. if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
  287. return ret;
  288. *got_frame = 1;
  289. return buf_size;
  290. }
  291. static av_cold int tgv_decode_end(AVCodecContext *avctx)
  292. {
  293. TgvContext *s = avctx->priv_data;
  294. av_frame_free(&s->last_frame);
  295. av_freep(&s->frame_buffer);
  296. av_free(s->mv_codebook);
  297. av_free(s->block_codebook);
  298. return 0;
  299. }
  300. AVCodec ff_eatgv_decoder = {
  301. .name = "eatgv",
  302. .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
  303. .type = AVMEDIA_TYPE_VIDEO,
  304. .id = AV_CODEC_ID_TGV,
  305. .priv_data_size = sizeof(TgvContext),
  306. .init = tgv_decode_init,
  307. .close = tgv_decode_end,
  308. .decode = tgv_decode_frame,
  309. .capabilities = AV_CODEC_CAP_DR1,
  310. };