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.

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