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.

320 lines
9.8KB

  1. /*
  2. * Electronic Arts Madcow Video Decoder
  3. * Copyright (c) 2007-2009 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 Madcow Video Decoder
  24. * by Peter Ross <pross@xvid.org>
  25. *
  26. * Technical details here:
  27. * http://wiki.multimedia.cx/index.php?title=Electronic_Arts_MAD
  28. */
  29. #include "avcodec.h"
  30. #include "get_bits.h"
  31. #include "dsputil.h"
  32. #include "aandcttab.h"
  33. #include "mpeg12.h"
  34. #include "mpeg12data.h"
  35. #define EA_PREAMBLE_SIZE 8
  36. #define MADk_TAG MKTAG('M', 'A', 'D', 'k') /* MAD i-frame */
  37. #define MADm_TAG MKTAG('M', 'A', 'D', 'm') /* MAD p-frame */
  38. #define MADe_TAG MKTAG('M', 'A', 'D', 'e') /* MAD lqp-frame */
  39. typedef struct MadContext {
  40. MpegEncContext s;
  41. AVFrame frame;
  42. AVFrame last_frame;
  43. void *bitstream_buf;
  44. unsigned int bitstream_buf_size;
  45. DECLARE_ALIGNED(16, DCTELEM, block)[64];
  46. } MadContext;
  47. static void bswap16_buf(uint16_t *dst, const uint16_t *src, int count)
  48. {
  49. int i;
  50. for (i=0; i<count; i++)
  51. dst[i] = bswap_16(src[i]);
  52. }
  53. static av_cold int decode_init(AVCodecContext *avctx)
  54. {
  55. MadContext *t = avctx->priv_data;
  56. MpegEncContext *s = &t->s;
  57. s->avctx = avctx;
  58. avctx->pix_fmt = PIX_FMT_YUV420P;
  59. if (avctx->idct_algo == FF_IDCT_AUTO)
  60. avctx->idct_algo = FF_IDCT_EA;
  61. dsputil_init(&s->dsp, avctx);
  62. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  63. ff_mpeg12_init_vlcs();
  64. return 0;
  65. }
  66. static inline void comp(unsigned char *dst, int dst_stride,
  67. unsigned char *src, int src_stride, int add)
  68. {
  69. int j, i;
  70. for (j=0; j<8; j++)
  71. for (i=0; i<8; i++)
  72. dst[j*dst_stride + i] = av_clip_uint8(src[j*src_stride + i] + add);
  73. }
  74. static inline void comp_block(MadContext *t, int mb_x, int mb_y,
  75. int j, int mv_x, int mv_y, int add)
  76. {
  77. MpegEncContext *s = &t->s;
  78. if (j < 4) {
  79. comp(t->frame.data[0] + (mb_y*16 + ((j&2)<<2))*t->frame.linesize[0] + mb_x*16 + ((j&1)<<3),
  80. t->frame.linesize[0],
  81. t->last_frame.data[0] + (mb_y*16 + ((j&2)<<2) + mv_y)*t->last_frame.linesize[0] + mb_x*16 + ((j&1)<<3) + mv_x,
  82. t->last_frame.linesize[0], add);
  83. } else if (!(s->avctx->flags & CODEC_FLAG_GRAY)) {
  84. int index = j - 3;
  85. comp(t->frame.data[index] + (mb_y*8)*t->frame.linesize[index] + mb_x * 8,
  86. t->frame.linesize[index],
  87. t->last_frame.data[index] + (mb_y * 8 + (mv_y/2))*t->last_frame.linesize[index] + mb_x * 8 + (mv_x/2),
  88. t->last_frame.linesize[index], add);
  89. }
  90. }
  91. static inline void idct_put(MadContext *t, DCTELEM *block, int mb_x, int mb_y, int j)
  92. {
  93. MpegEncContext *s = &t->s;
  94. if (j < 4) {
  95. s->dsp.idct_put(
  96. t->frame.data[0] + (mb_y*16 + ((j&2)<<2))*t->frame.linesize[0] + mb_x*16 + ((j&1)<<3),
  97. t->frame.linesize[0], block);
  98. } else if (!(s->avctx->flags & CODEC_FLAG_GRAY)) {
  99. int index = j - 3;
  100. s->dsp.idct_put(
  101. t->frame.data[index] + (mb_y*8)*t->frame.linesize[index] + mb_x*8,
  102. t->frame.linesize[index], block);
  103. }
  104. }
  105. static inline void decode_block_intra(MadContext * t, DCTELEM * block)
  106. {
  107. MpegEncContext *s = &t->s;
  108. int level, i, j, run;
  109. RLTable *rl = &ff_rl_mpeg1;
  110. const uint8_t *scantable = s->intra_scantable.permutated;
  111. int16_t *quant_matrix = s->intra_matrix;
  112. block[0] = (128 + get_sbits(&s->gb, 8)) * quant_matrix[0];
  113. /* The RL decoder is derived from mpeg1_decode_block_intra;
  114. Escaped level and run values a decoded differently */
  115. i = 0;
  116. {
  117. OPEN_READER(re, &s->gb);
  118. /* now quantify & encode AC coefficients */
  119. for (;;) {
  120. UPDATE_CACHE(re, &s->gb);
  121. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  122. if (level == 127) {
  123. break;
  124. } else if (level != 0) {
  125. i += run;
  126. j = scantable[i];
  127. level = (level*quant_matrix[j]) >> 4;
  128. level = (level-1)|1;
  129. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  130. LAST_SKIP_BITS(re, &s->gb, 1);
  131. } else {
  132. /* escape */
  133. UPDATE_CACHE(re, &s->gb);
  134. level = SHOW_SBITS(re, &s->gb, 10); SKIP_BITS(re, &s->gb, 10);
  135. UPDATE_CACHE(re, &s->gb);
  136. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  137. i += run;
  138. j = scantable[i];
  139. if (level < 0) {
  140. level = -level;
  141. level = (level*quant_matrix[j]) >> 4;
  142. level = (level-1)|1;
  143. level = -level;
  144. } else {
  145. level = (level*quant_matrix[j]) >> 4;
  146. level = (level-1)|1;
  147. }
  148. }
  149. if (i > 63) {
  150. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  151. return;
  152. }
  153. block[j] = level;
  154. }
  155. CLOSE_READER(re, &s->gb);
  156. }
  157. }
  158. static int decode_motion(GetBitContext *gb)
  159. {
  160. int value = 0;
  161. if (get_bits1(gb)) {
  162. if (get_bits1(gb))
  163. value = -17;
  164. value += get_bits(gb, 4) + 1;
  165. }
  166. return value;
  167. }
  168. static void decode_mb(MadContext *t, int inter)
  169. {
  170. MpegEncContext *s = &t->s;
  171. int mv_map = 0;
  172. int mv_x, mv_y;
  173. int j;
  174. if (inter) {
  175. int v = decode210(&s->gb);
  176. if (v < 2) {
  177. mv_map = v ? get_bits(&s->gb, 6) : 63;
  178. mv_x = decode_motion(&s->gb);
  179. mv_y = decode_motion(&s->gb);
  180. } else {
  181. mv_map = 0;
  182. }
  183. }
  184. for (j=0; j<6; j++) {
  185. if (mv_map & (1<<j)) { // mv_x and mv_y are guarded by mv_map
  186. int add = 2*decode_motion(&s->gb);
  187. comp_block(t, s->mb_x, s->mb_y, j, mv_x, mv_y, add);
  188. } else {
  189. s->dsp.clear_block(t->block);
  190. decode_block_intra(t, t->block);
  191. idct_put(t, t->block, s->mb_x, s->mb_y, j);
  192. }
  193. }
  194. }
  195. static void calc_intra_matrix(MadContext *t, int qscale)
  196. {
  197. MpegEncContext *s = &t->s;
  198. int i;
  199. if (s->avctx->idct_algo == FF_IDCT_EA) {
  200. s->intra_matrix[0] = (ff_inv_aanscales[0]*ff_mpeg1_default_intra_matrix[0]) >> 11;
  201. for (i=1; i<64; i++)
  202. s->intra_matrix[i] = (ff_inv_aanscales[i]*ff_mpeg1_default_intra_matrix[i]*qscale + 32) >> 10;
  203. } else {
  204. s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
  205. for (i=1; i<64; i++)
  206. s->intra_matrix[i] = (ff_mpeg1_default_intra_matrix[i]*qscale) << 1;
  207. }
  208. }
  209. static int decode_frame(AVCodecContext *avctx,
  210. void *data, int *data_size,
  211. AVPacket *avpkt)
  212. {
  213. const uint8_t *buf = avpkt->data;
  214. int buf_size = avpkt->size;
  215. const uint8_t *buf_end = buf+buf_size;
  216. MadContext *t = avctx->priv_data;
  217. MpegEncContext *s = &t->s;
  218. int chunk_type;
  219. int inter;
  220. if (buf_size < 17) {
  221. av_log(avctx, AV_LOG_ERROR, "Input buffer too small\n");
  222. *data_size = 0;
  223. return -1;
  224. }
  225. chunk_type = AV_RL32(&buf[0]);
  226. inter = (chunk_type == MADm_TAG || chunk_type == MADe_TAG);
  227. buf += 8;
  228. av_reduce(&avctx->time_base.num, &avctx->time_base.den,
  229. AV_RL16(&buf[6]), 1000, 1<<30);
  230. s->width = AV_RL16(&buf[8]);
  231. s->height = AV_RL16(&buf[10]);
  232. calc_intra_matrix(t, buf[13]);
  233. buf += 16;
  234. if (avctx->width != s->width || avctx->height != s->height) {
  235. if (avcodec_check_dimensions(avctx, s->width, s->height) < 0)
  236. return -1;
  237. avcodec_set_dimensions(avctx, s->width, s->height);
  238. if (t->frame.data[0])
  239. avctx->release_buffer(avctx, &t->frame);
  240. }
  241. t->frame.reference = 1;
  242. if (!t->frame.data[0]) {
  243. if (avctx->get_buffer(avctx, &t->frame) < 0) {
  244. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  245. return -1;
  246. }
  247. }
  248. av_fast_malloc(&t->bitstream_buf, &t->bitstream_buf_size, (buf_end-buf) + FF_INPUT_BUFFER_PADDING_SIZE);
  249. if (!t->bitstream_buf)
  250. return AVERROR(ENOMEM);
  251. bswap16_buf(t->bitstream_buf, (const uint16_t*)buf, (buf_end-buf)/2);
  252. init_get_bits(&s->gb, t->bitstream_buf, 8*(buf_end-buf));
  253. for (s->mb_y=0; s->mb_y < (avctx->height+15)/16; s->mb_y++)
  254. for (s->mb_x=0; s->mb_x < (avctx->width +15)/16; s->mb_x++)
  255. decode_mb(t, inter);
  256. *data_size = sizeof(AVFrame);
  257. *(AVFrame*)data = t->frame;
  258. if (chunk_type != MADe_TAG)
  259. FFSWAP(AVFrame, t->frame, t->last_frame);
  260. return buf_size;
  261. }
  262. static av_cold int decode_end(AVCodecContext *avctx)
  263. {
  264. MadContext *t = avctx->priv_data;
  265. if (t->frame.data[0])
  266. avctx->release_buffer(avctx, &t->frame);
  267. if (t->last_frame.data[0])
  268. avctx->release_buffer(avctx, &t->last_frame);
  269. av_free(t->bitstream_buf);
  270. return 0;
  271. }
  272. AVCodec eamad_decoder = {
  273. "eamad",
  274. AVMEDIA_TYPE_VIDEO,
  275. CODEC_ID_MAD,
  276. sizeof(MadContext),
  277. decode_init,
  278. NULL,
  279. decode_end,
  280. decode_frame,
  281. CODEC_CAP_DR1,
  282. .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Madcow Video")
  283. };