eatgv.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 libavcodec/eatgv.c
  23. * Electronic Arts TGV Video Decoder
  24. * by Peter Ross (suxen_drol at hotmail dot com)
  25. *
  26. * Technical details here:
  27. * http://wiki.multimedia.cx/index.php?title=Electronic_Arts_TGV
  28. */
  29. #include "avcodec.h"
  30. #define ALT_BITSTREAM_READER_LE
  31. #include "bitstream.h"
  32. #include "libavutil/lzo.h"
  33. #define EA_PREAMBLE_SIZE 8
  34. #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
  35. typedef struct TgvContext {
  36. AVCodecContext *avctx;
  37. AVFrame frame;
  38. AVFrame last_frame;
  39. int width,height;
  40. unsigned int palette[AVPALETTE_COUNT];
  41. int (*mv_codebook)[2];
  42. unsigned char (*block_codebook)[16];
  43. int num_mvs; ///< current length of mv_codebook
  44. int num_blocks_packed; ///< current length of block_codebook
  45. } TgvContext;
  46. static av_cold int tgv_decode_init(AVCodecContext *avctx){
  47. TgvContext *s = avctx->priv_data;
  48. s->avctx = avctx;
  49. avctx->time_base = (AVRational){1, 15};
  50. avctx->pix_fmt = PIX_FMT_PAL8;
  51. return 0;
  52. }
  53. /**
  54. * Unpack buffer
  55. * @return 0 on success, -1 on critical buffer underflow
  56. */
  57. static int unpack(const uint8_t *src, const uint8_t *src_end, unsigned char *dst, int width, int height) {
  58. unsigned char *dst_end = dst + width*height;
  59. int size, size1, size2, av_uninit(offset), run;
  60. unsigned char *dst_start = dst;
  61. if (src[0] & 0x01)
  62. src += 5;
  63. else
  64. src += 2;
  65. if (src+3>src_end)
  66. return -1;
  67. size = AV_RB24(src);
  68. src += 3;
  69. while(size>0 && src<src_end) {
  70. /* determine size1 and size2 */
  71. size1 = (src[0] & 3);
  72. if ( src[0] & 0x80 ) { // 1
  73. if (src[0] & 0x40 ) { // 11
  74. if ( src[0] & 0x20 ) { // 111
  75. if ( src[0] < 0xFC ) // !(111111)
  76. size1 = (((src[0] & 31) + 1) << 2);
  77. src++;
  78. size2 = 0;
  79. } else { // 110
  80. offset = ((src[0] & 0x10) << 12) + AV_RB16(&src[1]) + 1;
  81. size2 = ((src[0] & 0xC) << 6) + src[3] + 5;
  82. src += 4;
  83. }
  84. } else { // 10
  85. size1 = ( ( src[1] & 0xC0) >> 6 );
  86. offset = (AV_RB16(&src[1]) & 0x3FFF) + 1;
  87. size2 = (src[0] & 0x3F) + 4;
  88. src += 3;
  89. }
  90. } else { // 0
  91. offset = ((src[0] & 0x60) << 3) + src[1] + 1;
  92. size2 = ((src[0] & 0x1C) >> 2) + 3;
  93. src += 2;
  94. }
  95. /* fetch strip from src */
  96. if (size1>src_end-src)
  97. break;
  98. if (size1>0) {
  99. size -= size1;
  100. run = FFMIN(size1, dst_end-dst);
  101. memcpy(dst, src, run);
  102. dst += run;
  103. src += run;
  104. }
  105. if (size2>0) {
  106. if (dst-dst_start<offset)
  107. return 0;
  108. size -= size2;
  109. run = FFMIN(size2, dst_end-dst);
  110. av_memcpy_backptr(dst, offset, run);
  111. dst += run;
  112. }
  113. }
  114. return 0;
  115. }
  116. /**
  117. * Decode inter-frame
  118. * @return 0 on success, -1 on critical buffer underflow
  119. */
  120. static int tgv_decode_inter(TgvContext * s, const uint8_t *buf, const uint8_t *buf_end){
  121. unsigned char *frame0_end = s->last_frame.data[0] + s->avctx->width*s->last_frame.linesize[0];
  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. /* allocate codebook buffers as neccessary */
  138. if (num_mvs > s->num_mvs) {
  139. s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
  140. s->num_mvs = num_mvs;
  141. }
  142. if (num_blocks_packed > s->num_blocks_packed) {
  143. s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16*sizeof(unsigned char));
  144. s->num_blocks_packed = num_blocks_packed;
  145. }
  146. /* read motion vectors */
  147. mvbits = (num_mvs*2*10+31) & ~31;
  148. if (buf+(mvbits>>3)+16*num_blocks_raw+8*num_blocks_packed>buf_end)
  149. return -1;
  150. init_get_bits(&gb, buf, mvbits);
  151. for (i=0; i<num_mvs; i++) {
  152. s->mv_codebook[i][0] = get_sbits(&gb, 10);
  153. s->mv_codebook[i][1] = get_sbits(&gb, 10);
  154. }
  155. buf += mvbits>>3;
  156. /* note ptr to uncompressed blocks */
  157. blocks_raw = buf;
  158. buf += num_blocks_raw*16;
  159. /* read compressed blocks */
  160. init_get_bits(&gb, buf, (buf_end-buf)<<3);
  161. for (i=0; i<num_blocks_packed; i++) {
  162. int tmp[4];
  163. for(j=0; j<4; j++)
  164. tmp[j] = get_bits(&gb, 8);
  165. for(j=0; j<16; j++)
  166. s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
  167. }
  168. /* read vectors and build frame */
  169. for(y=0; y<s->avctx->height/4; y++)
  170. for(x=0; x<s->avctx->width/4; x++) {
  171. unsigned int vector = get_bits(&gb, vector_bits);
  172. const unsigned char *src;
  173. int src_stride;
  174. if (vector < num_mvs) {
  175. src = s->last_frame.data[0] +
  176. (y*4 + s->mv_codebook[vector][1])*s->last_frame.linesize[0] +
  177. x*4 + s->mv_codebook[vector][0];
  178. src_stride = s->last_frame.linesize[0];
  179. if (src+3*src_stride+3>=frame0_end)
  180. continue;
  181. }else{
  182. int offset = vector - num_mvs;
  183. if (offset<num_blocks_raw)
  184. src = blocks_raw + 16*offset;
  185. else if (offset-num_blocks_raw<num_blocks_packed)
  186. src = s->block_codebook[offset-num_blocks_raw];
  187. else
  188. continue;
  189. src_stride = 4;
  190. }
  191. for(j=0; j<4; j++)
  192. for(i=0; i<4; i++)
  193. s->frame.data[0][ (y*4+j)*s->frame.linesize[0] + (x*4+i) ] =
  194. src[j*src_stride + i];
  195. }
  196. return 0;
  197. }
  198. /** release AVFrame buffers if allocated */
  199. static void cond_release_buffer(AVFrame *pic)
  200. {
  201. if (pic->data[0]) {
  202. av_freep(&pic->data[0]);
  203. av_free(pic->data[1]);
  204. }
  205. }
  206. static int tgv_decode_frame(AVCodecContext *avctx,
  207. void *data, int *data_size,
  208. const uint8_t *buf, int buf_size)
  209. {
  210. TgvContext *s = avctx->priv_data;
  211. const uint8_t *buf_end = buf + buf_size;
  212. int chunk_type;
  213. chunk_type = AV_RL32(&buf[0]);
  214. buf += EA_PREAMBLE_SIZE;
  215. if (chunk_type==kVGT_TAG) {
  216. int pal_count, i;
  217. if(buf+12>buf_end) {
  218. av_log(avctx, AV_LOG_WARNING, "truncated header\n");
  219. return -1;
  220. }
  221. s->width = AV_RL16(&buf[0]);
  222. s->height = AV_RL16(&buf[2]);
  223. if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
  224. avcodec_set_dimensions(s->avctx, s->width, s->height);
  225. cond_release_buffer(&s->frame);
  226. cond_release_buffer(&s->last_frame);
  227. }
  228. pal_count = AV_RL16(&buf[6]);
  229. buf += 12;
  230. for(i=0; i<pal_count && i<AVPALETTE_COUNT && buf+2<buf_end; i++) {
  231. s->palette[i] = AV_RB24(buf);
  232. buf += 3;
  233. }
  234. }
  235. if (avcodec_check_dimensions(avctx, s->width, s->height))
  236. return -1;
  237. /* shuffle */
  238. FFSWAP(AVFrame, s->frame, s->last_frame);
  239. if (!s->frame.data[0]) {
  240. s->frame.reference = 1;
  241. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
  242. s->frame.linesize[0] = s->width;
  243. /* allocate additional 12 bytes to accomodate av_memcpy_backptr() OUTBUF_PADDED optimisation */
  244. s->frame.data[0] = av_malloc(s->width*s->height + 12);
  245. if (!s->frame.data[0])
  246. return AVERROR_NOMEM;
  247. s->frame.data[1] = av_malloc(AVPALETTE_SIZE);
  248. if (!s->frame.data[1]) {
  249. av_freep(&s->frame.data[0]);
  250. return AVERROR_NOMEM;
  251. }
  252. }
  253. memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
  254. if(chunk_type==kVGT_TAG) {
  255. s->frame.key_frame = 1;
  256. s->frame.pict_type = FF_I_TYPE;
  257. if (unpack(buf, buf_end, s->frame.data[0], s->avctx->width, s->avctx->height)<0) {
  258. av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
  259. return -1;
  260. }
  261. }else{
  262. if (!s->last_frame.data[0]) {
  263. av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
  264. return buf_size;
  265. }
  266. s->frame.key_frame = 0;
  267. s->frame.pict_type = FF_P_TYPE;
  268. if (tgv_decode_inter(s, buf, buf_end)<0) {
  269. av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
  270. return -1;
  271. }
  272. }
  273. *data_size = sizeof(AVFrame);
  274. *(AVFrame*)data = s->frame;
  275. return buf_size;
  276. }
  277. static av_cold int tgv_decode_end(AVCodecContext *avctx)
  278. {
  279. TgvContext *s = avctx->priv_data;
  280. cond_release_buffer(&s->frame);
  281. cond_release_buffer(&s->last_frame);
  282. av_free(s->mv_codebook);
  283. av_free(s->block_codebook);
  284. return 0;
  285. }
  286. AVCodec eatgv_decoder = {
  287. "eatgv",
  288. CODEC_TYPE_VIDEO,
  289. CODEC_ID_TGV,
  290. sizeof(TgvContext),
  291. tgv_decode_init,
  292. NULL,
  293. tgv_decode_end,
  294. tgv_decode_frame,
  295. .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
  296. };