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.

359 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/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. 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. unsigned last_frame_size = s->avctx->height*s->last_frame.linesize[0];
  125. int num_mvs;
  126. int num_blocks_raw;
  127. int num_blocks_packed;
  128. int vector_bits;
  129. int i,j,x,y;
  130. GetBitContext gb;
  131. int mvbits;
  132. const unsigned char *blocks_raw;
  133. if(buf_end - buf < 12)
  134. return -1;
  135. num_mvs = AV_RL16(&buf[0]);
  136. num_blocks_raw = AV_RL16(&buf[2]);
  137. num_blocks_packed = AV_RL16(&buf[4]);
  138. vector_bits = AV_RL16(&buf[6]);
  139. buf += 12;
  140. if (vector_bits > MIN_CACHE_BITS || !vector_bits) {
  141. av_log(s->avctx, AV_LOG_ERROR, "vector_bits %d invalid\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. unsigned offset =
  186. (y*4 + s->mv_codebook[vector][1])*s->last_frame.linesize[0] +
  187. x*4 + s->mv_codebook[vector][0];
  188. src_stride = s->last_frame.linesize[0];
  189. if (offset >= last_frame_size - (3*src_stride+3))
  190. continue;
  191. src = s->last_frame.data[0] + offset;
  192. }else{
  193. int offset = vector - num_mvs;
  194. if (offset<num_blocks_raw)
  195. src = blocks_raw + 16*offset;
  196. else if (offset-num_blocks_raw<num_blocks_packed)
  197. src = s->block_codebook[offset-num_blocks_raw];
  198. else
  199. continue;
  200. src_stride = 4;
  201. }
  202. for(j=0; j<4; j++)
  203. for(i=0; i<4; i++)
  204. s->frame.data[0][ (y*4+j)*s->frame.linesize[0] + (x*4+i) ] =
  205. src[j*src_stride + i];
  206. }
  207. return 0;
  208. }
  209. /** release AVFrame buffers if allocated */
  210. static void cond_release_buffer(AVFrame *pic)
  211. {
  212. if (pic->data[0]) {
  213. av_freep(&pic->data[0]);
  214. av_free(pic->data[1]);
  215. }
  216. }
  217. static int tgv_decode_frame(AVCodecContext *avctx,
  218. void *data, int *data_size,
  219. AVPacket *avpkt)
  220. {
  221. const uint8_t *buf = avpkt->data;
  222. int buf_size = avpkt->size;
  223. TgvContext *s = avctx->priv_data;
  224. const uint8_t *buf_end = buf + buf_size;
  225. int chunk_type;
  226. if (buf_end - buf < EA_PREAMBLE_SIZE)
  227. return AVERROR_INVALIDDATA;
  228. chunk_type = AV_RL32(&buf[0]);
  229. buf += EA_PREAMBLE_SIZE;
  230. if (chunk_type==kVGT_TAG) {
  231. int pal_count, i;
  232. if(buf_end - buf < 12) {
  233. av_log(avctx, AV_LOG_WARNING, "truncated header\n");
  234. return -1;
  235. }
  236. s->width = AV_RL16(&buf[0]);
  237. s->height = AV_RL16(&buf[2]);
  238. if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
  239. avcodec_set_dimensions(s->avctx, s->width, s->height);
  240. cond_release_buffer(&s->frame);
  241. cond_release_buffer(&s->last_frame);
  242. }
  243. pal_count = AV_RL16(&buf[6]);
  244. buf += 12;
  245. for(i=0; i<pal_count && i<AVPALETTE_COUNT && buf_end - buf >= 3; i++) {
  246. s->palette[i] = 0xFF << 24 | AV_RB24(buf);
  247. buf += 3;
  248. }
  249. }
  250. if (av_image_check_size(s->width, s->height, 0, avctx))
  251. return -1;
  252. /* shuffle */
  253. FFSWAP(AVFrame, s->frame, s->last_frame);
  254. if (!s->frame.data[0]) {
  255. s->frame.reference = 3;
  256. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
  257. s->frame.linesize[0] = s->width;
  258. /* allocate additional 12 bytes to accommodate av_memcpy_backptr() OUTBUF_PADDED optimisation */
  259. s->frame.data[0] = av_malloc(s->width*s->height + 12);
  260. if (!s->frame.data[0])
  261. return AVERROR(ENOMEM);
  262. s->frame.data[1] = av_malloc(AVPALETTE_SIZE);
  263. if (!s->frame.data[1]) {
  264. av_freep(&s->frame.data[0]);
  265. return AVERROR(ENOMEM);
  266. }
  267. }
  268. memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
  269. if(chunk_type==kVGT_TAG) {
  270. s->frame.key_frame = 1;
  271. s->frame.pict_type = AV_PICTURE_TYPE_I;
  272. if (unpack(buf, buf_end, s->frame.data[0], s->avctx->width, s->avctx->height)<0) {
  273. av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
  274. return -1;
  275. }
  276. }else{
  277. if (!s->last_frame.data[0]) {
  278. av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
  279. return buf_size;
  280. }
  281. s->frame.key_frame = 0;
  282. s->frame.pict_type = AV_PICTURE_TYPE_P;
  283. if (tgv_decode_inter(s, buf, buf_end)<0) {
  284. av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
  285. return -1;
  286. }
  287. }
  288. *data_size = sizeof(AVFrame);
  289. *(AVFrame*)data = s->frame;
  290. return buf_size;
  291. }
  292. static av_cold int tgv_decode_end(AVCodecContext *avctx)
  293. {
  294. TgvContext *s = avctx->priv_data;
  295. cond_release_buffer(&s->frame);
  296. cond_release_buffer(&s->last_frame);
  297. av_free(s->mv_codebook);
  298. av_free(s->block_codebook);
  299. return 0;
  300. }
  301. AVCodec ff_eatgv_decoder = {
  302. .name = "eatgv",
  303. .type = AVMEDIA_TYPE_VIDEO,
  304. .id = CODEC_ID_TGV,
  305. .priv_data_size = sizeof(TgvContext),
  306. .init = tgv_decode_init,
  307. .close = tgv_decode_end,
  308. .decode = tgv_decode_frame,
  309. .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
  310. };