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.

366 lines
11KB

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