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.

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