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.

319 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 libavcodec/eamad.c
  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. {
  116. OPEN_READER(re, &s->gb);
  117. /* now quantify & encode AC coefficients */
  118. for (;;) {
  119. UPDATE_CACHE(re, &s->gb);
  120. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  121. if (level == 127) {
  122. break;
  123. } else if (level != 0) {
  124. i += run;
  125. j = scantable[i];
  126. level = (level*quant_matrix[j]) >> 4;
  127. level = (level-1)|1;
  128. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  129. LAST_SKIP_BITS(re, &s->gb, 1);
  130. } else {
  131. /* escape */
  132. UPDATE_CACHE(re, &s->gb);
  133. level = SHOW_SBITS(re, &s->gb, 10); SKIP_BITS(re, &s->gb, 10);
  134. UPDATE_CACHE(re, &s->gb);
  135. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  136. i += run;
  137. j = scantable[i];
  138. if (level < 0) {
  139. level = -level;
  140. level = (level*quant_matrix[j]) >> 4;
  141. level = (level-1)|1;
  142. level = -level;
  143. } else {
  144. level = (level*quant_matrix[j]) >> 4;
  145. level = (level-1)|1;
  146. }
  147. }
  148. if (i > 63) {
  149. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  150. return;
  151. }
  152. block[j] = level;
  153. }
  154. CLOSE_READER(re, &s->gb);
  155. }
  156. }
  157. static int decode_motion(GetBitContext *gb)
  158. {
  159. int value = 0;
  160. if (get_bits1(gb)) {
  161. if (get_bits1(gb))
  162. value = -17;
  163. value += get_bits(gb, 4) + 1;
  164. }
  165. return value;
  166. }
  167. static void decode_mb(MadContext *t, int inter)
  168. {
  169. MpegEncContext *s = &t->s;
  170. int mv_map = 0;
  171. int mv_x, mv_y;
  172. int j;
  173. if (inter) {
  174. int v = decode210(&s->gb);
  175. if (v < 2) {
  176. mv_map = v ? get_bits(&s->gb, 6) : 63;
  177. mv_x = decode_motion(&s->gb);
  178. mv_y = decode_motion(&s->gb);
  179. } else {
  180. mv_map = 0;
  181. }
  182. }
  183. for (j=0; j<6; j++) {
  184. if (mv_map & (1<<j)) { // mv_x and mv_y are guarded by mv_map
  185. int add = 2*decode_motion(&s->gb);
  186. comp_block(t, s->mb_x, s->mb_y, j, mv_x, mv_y, add);
  187. } else {
  188. s->dsp.clear_block(t->block);
  189. decode_block_intra(t, t->block);
  190. idct_put(t, t->block, s->mb_x, s->mb_y, j);
  191. }
  192. }
  193. }
  194. static void calc_intra_matrix(MadContext *t, int qscale)
  195. {
  196. MpegEncContext *s = &t->s;
  197. int i;
  198. if (s->avctx->idct_algo == FF_IDCT_EA) {
  199. s->intra_matrix[0] = (ff_inv_aanscales[0]*ff_mpeg1_default_intra_matrix[0]) >> 11;
  200. for (i=1; i<64; i++)
  201. s->intra_matrix[i] = (ff_inv_aanscales[i]*ff_mpeg1_default_intra_matrix[i]*qscale + 32) >> 10;
  202. } else {
  203. s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
  204. for (i=1; i<64; i++)
  205. s->intra_matrix[i] = (ff_mpeg1_default_intra_matrix[i]*qscale) << 1;
  206. }
  207. }
  208. static int decode_frame(AVCodecContext *avctx,
  209. void *data, int *data_size,
  210. AVPacket *avpkt)
  211. {
  212. const uint8_t *buf = avpkt->data;
  213. int buf_size = avpkt->size;
  214. const uint8_t *buf_end = buf+buf_size;
  215. MadContext *t = avctx->priv_data;
  216. MpegEncContext *s = &t->s;
  217. int chunk_type;
  218. int inter;
  219. if (buf_size < 17) {
  220. av_log(avctx, AV_LOG_ERROR, "Input buffer too small\n");
  221. *data_size = 0;
  222. return -1;
  223. }
  224. chunk_type = AV_RL32(&buf[0]);
  225. inter = (chunk_type == MADm_TAG || chunk_type == MADe_TAG);
  226. buf += 8;
  227. av_reduce(&avctx->time_base.num, &avctx->time_base.den,
  228. AV_RL16(&buf[6]), 1000, 1<<30);
  229. s->width = AV_RL16(&buf[8]);
  230. s->height = AV_RL16(&buf[10]);
  231. calc_intra_matrix(t, buf[13]);
  232. buf += 16;
  233. if (avctx->width != s->width || avctx->height != s->height) {
  234. if (avcodec_check_dimensions(avctx, s->width, s->height) < 0)
  235. return -1;
  236. avcodec_set_dimensions(avctx, s->width, s->height);
  237. if (t->frame.data[0])
  238. avctx->release_buffer(avctx, &t->frame);
  239. }
  240. t->frame.reference = 1;
  241. if (!t->frame.data[0]) {
  242. if (avctx->get_buffer(avctx, &t->frame) < 0) {
  243. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  244. return -1;
  245. }
  246. }
  247. av_fast_malloc(&t->bitstream_buf, &t->bitstream_buf_size, (buf_end-buf) + FF_INPUT_BUFFER_PADDING_SIZE);
  248. if (!t->bitstream_buf)
  249. return AVERROR(ENOMEM);
  250. bswap16_buf(t->bitstream_buf, (const uint16_t*)buf, (buf_end-buf)/2);
  251. init_get_bits(&s->gb, t->bitstream_buf, 8*(buf_end-buf));
  252. for (s->mb_y=0; s->mb_y < (avctx->height+15)/16; s->mb_y++)
  253. for (s->mb_x=0; s->mb_x < (avctx->width +15)/16; s->mb_x++)
  254. decode_mb(t, inter);
  255. *data_size = sizeof(AVFrame);
  256. *(AVFrame*)data = t->frame;
  257. if (chunk_type != MADe_TAG)
  258. FFSWAP(AVFrame, t->frame, t->last_frame);
  259. return buf_size;
  260. }
  261. static av_cold int decode_end(AVCodecContext *avctx)
  262. {
  263. MadContext *t = avctx->priv_data;
  264. if (t->frame.data[0])
  265. avctx->release_buffer(avctx, &t->frame);
  266. if (t->last_frame.data[0])
  267. avctx->release_buffer(avctx, &t->last_frame);
  268. av_free(t->bitstream_buf);
  269. return 0;
  270. }
  271. AVCodec eamad_decoder = {
  272. "eamad",
  273. CODEC_TYPE_VIDEO,
  274. CODEC_ID_MAD,
  275. sizeof(MadContext),
  276. decode_init,
  277. NULL,
  278. decode_end,
  279. decode_frame,
  280. CODEC_CAP_DR1,
  281. .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Madcow Video")
  282. };