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.

348 lines
10KB

  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 ALT_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, av_uninit(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. unsigned char *frame0_end = s->last_frame.data[0] + s->avctx->width*s->last_frame.linesize[0];
  123. int num_mvs;
  124. int num_blocks_raw;
  125. int num_blocks_packed;
  126. int vector_bits;
  127. int i,j,x,y;
  128. GetBitContext gb;
  129. int mvbits;
  130. const unsigned char *blocks_raw;
  131. if(buf+12>buf_end)
  132. return -1;
  133. num_mvs = AV_RL16(&buf[0]);
  134. num_blocks_raw = AV_RL16(&buf[2]);
  135. num_blocks_packed = AV_RL16(&buf[4]);
  136. vector_bits = AV_RL16(&buf[6]);
  137. buf += 12;
  138. /* allocate codebook buffers as neccessary */
  139. if (num_mvs > s->num_mvs) {
  140. s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
  141. s->num_mvs = num_mvs;
  142. }
  143. if (num_blocks_packed > s->num_blocks_packed) {
  144. s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16*sizeof(unsigned char));
  145. s->num_blocks_packed = num_blocks_packed;
  146. }
  147. /* read motion vectors */
  148. mvbits = (num_mvs*2*10+31) & ~31;
  149. if (buf+(mvbits>>3)+16*num_blocks_raw+8*num_blocks_packed>buf_end)
  150. return -1;
  151. init_get_bits(&gb, buf, mvbits);
  152. for (i=0; i<num_mvs; i++) {
  153. s->mv_codebook[i][0] = get_sbits(&gb, 10);
  154. s->mv_codebook[i][1] = get_sbits(&gb, 10);
  155. }
  156. buf += mvbits>>3;
  157. /* note ptr to uncompressed blocks */
  158. blocks_raw = buf;
  159. buf += num_blocks_raw*16;
  160. /* read compressed blocks */
  161. init_get_bits(&gb, buf, (buf_end-buf)<<3);
  162. for (i=0; i<num_blocks_packed; i++) {
  163. int tmp[4];
  164. for(j=0; j<4; j++)
  165. tmp[j] = get_bits(&gb, 8);
  166. for(j=0; j<16; j++)
  167. s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
  168. }
  169. if (get_bits_left(&gb) < vector_bits *
  170. (s->avctx->height/4) * (s->avctx->width/4))
  171. return -1;
  172. /* read vectors and build frame */
  173. for(y=0; y<s->avctx->height/4; y++)
  174. for(x=0; x<s->avctx->width/4; x++) {
  175. unsigned int vector = get_bits(&gb, vector_bits);
  176. const unsigned char *src;
  177. int src_stride;
  178. if (vector < num_mvs) {
  179. src = s->last_frame.data[0] +
  180. (y*4 + s->mv_codebook[vector][1])*s->last_frame.linesize[0] +
  181. x*4 + s->mv_codebook[vector][0];
  182. src_stride = s->last_frame.linesize[0];
  183. if (src+3*src_stride+3>=frame0_end)
  184. continue;
  185. }else{
  186. int offset = vector - num_mvs;
  187. if (offset<num_blocks_raw)
  188. src = blocks_raw + 16*offset;
  189. else if (offset-num_blocks_raw<num_blocks_packed)
  190. src = s->block_codebook[offset-num_blocks_raw];
  191. else
  192. continue;
  193. src_stride = 4;
  194. }
  195. for(j=0; j<4; j++)
  196. for(i=0; i<4; i++)
  197. s->frame.data[0][ (y*4+j)*s->frame.linesize[0] + (x*4+i) ] =
  198. src[j*src_stride + i];
  199. }
  200. return 0;
  201. }
  202. /** release AVFrame buffers if allocated */
  203. static void cond_release_buffer(AVFrame *pic)
  204. {
  205. if (pic->data[0]) {
  206. av_freep(&pic->data[0]);
  207. av_free(pic->data[1]);
  208. }
  209. }
  210. static int tgv_decode_frame(AVCodecContext *avctx,
  211. void *data, int *data_size,
  212. AVPacket *avpkt)
  213. {
  214. const uint8_t *buf = avpkt->data;
  215. int buf_size = avpkt->size;
  216. TgvContext *s = avctx->priv_data;
  217. const uint8_t *buf_end = buf + buf_size;
  218. int chunk_type;
  219. chunk_type = AV_RL32(&buf[0]);
  220. buf += EA_PREAMBLE_SIZE;
  221. if (chunk_type==kVGT_TAG) {
  222. int pal_count, i;
  223. if(buf+12>buf_end) {
  224. av_log(avctx, AV_LOG_WARNING, "truncated header\n");
  225. return -1;
  226. }
  227. s->width = AV_RL16(&buf[0]);
  228. s->height = AV_RL16(&buf[2]);
  229. if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
  230. avcodec_set_dimensions(s->avctx, s->width, s->height);
  231. cond_release_buffer(&s->frame);
  232. cond_release_buffer(&s->last_frame);
  233. }
  234. pal_count = AV_RL16(&buf[6]);
  235. buf += 12;
  236. for(i=0; i<pal_count && i<AVPALETTE_COUNT && buf+2<buf_end; i++) {
  237. s->palette[i] = AV_RB24(buf);
  238. buf += 3;
  239. }
  240. }
  241. if (av_image_check_size(s->width, s->height, 0, avctx))
  242. return -1;
  243. /* shuffle */
  244. FFSWAP(AVFrame, s->frame, s->last_frame);
  245. if (!s->frame.data[0]) {
  246. s->frame.reference = 1;
  247. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
  248. s->frame.linesize[0] = s->width;
  249. /* allocate additional 12 bytes to accomodate av_memcpy_backptr() OUTBUF_PADDED optimisation */
  250. s->frame.data[0] = av_malloc(s->width*s->height + 12);
  251. if (!s->frame.data[0])
  252. return AVERROR(ENOMEM);
  253. s->frame.data[1] = av_malloc(AVPALETTE_SIZE);
  254. if (!s->frame.data[1]) {
  255. av_freep(&s->frame.data[0]);
  256. return AVERROR(ENOMEM);
  257. }
  258. }
  259. memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
  260. if(chunk_type==kVGT_TAG) {
  261. s->frame.key_frame = 1;
  262. s->frame.pict_type = FF_I_TYPE;
  263. if (unpack(buf, buf_end, s->frame.data[0], s->avctx->width, s->avctx->height)<0) {
  264. av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
  265. return -1;
  266. }
  267. }else{
  268. if (!s->last_frame.data[0]) {
  269. av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
  270. return buf_size;
  271. }
  272. s->frame.key_frame = 0;
  273. s->frame.pict_type = FF_P_TYPE;
  274. if (tgv_decode_inter(s, buf, buf_end)<0) {
  275. av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
  276. return -1;
  277. }
  278. }
  279. *data_size = sizeof(AVFrame);
  280. *(AVFrame*)data = s->frame;
  281. return buf_size;
  282. }
  283. static av_cold int tgv_decode_end(AVCodecContext *avctx)
  284. {
  285. TgvContext *s = avctx->priv_data;
  286. cond_release_buffer(&s->frame);
  287. cond_release_buffer(&s->last_frame);
  288. av_free(s->mv_codebook);
  289. av_free(s->block_codebook);
  290. return 0;
  291. }
  292. AVCodec ff_eatgv_decoder = {
  293. "eatgv",
  294. AVMEDIA_TYPE_VIDEO,
  295. CODEC_ID_TGV,
  296. sizeof(TgvContext),
  297. tgv_decode_init,
  298. NULL,
  299. tgv_decode_end,
  300. tgv_decode_frame,
  301. .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
  302. };