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.

355 lines
10KB

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