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.

255 lines
8.4KB

  1. /*
  2. * Electronic Arts TGQ Video Decoder
  3. * Copyright (c) 2007-2008 Peter Ross <pross@xvid.org>
  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 TGQ Video Decoder
  24. * @author Peter Ross <pross@xvid.org>
  25. *
  26. * Technical details here:
  27. * http://wiki.multimedia.cx/index.php?title=Electronic_Arts_TGQ
  28. */
  29. #include "avcodec.h"
  30. #define BITSTREAM_READER_LE
  31. #include "get_bits.h"
  32. #include "bytestream.h"
  33. #include "dsputil.h"
  34. #include "aandcttab.h"
  35. #include "eaidct.h"
  36. #include "internal.h"
  37. typedef struct TgqContext {
  38. AVCodecContext *avctx;
  39. int width, height;
  40. ScanTable scantable;
  41. int qtable[64];
  42. DECLARE_ALIGNED(16, int16_t, block)[6][64];
  43. GetByteContext gb;
  44. } TgqContext;
  45. static av_cold int tgq_decode_init(AVCodecContext *avctx)
  46. {
  47. TgqContext *s = avctx->priv_data;
  48. uint8_t idct_permutation[64];
  49. s->avctx = avctx;
  50. ff_init_scantable_permutation(idct_permutation, FF_NO_IDCT_PERM);
  51. ff_init_scantable(idct_permutation, &s->scantable, ff_zigzag_direct);
  52. avctx->time_base = (AVRational){1, 15};
  53. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  54. return 0;
  55. }
  56. static void tgq_decode_block(TgqContext *s, int16_t block[64], GetBitContext *gb)
  57. {
  58. uint8_t *perm = s->scantable.permutated;
  59. int i, j, value;
  60. block[0] = get_sbits(gb, 8) * s->qtable[0];
  61. for (i = 1; i < 64;) {
  62. switch (show_bits(gb, 3)) {
  63. case 4:
  64. block[perm[i++]] = 0;
  65. case 0:
  66. block[perm[i++]] = 0;
  67. skip_bits(gb, 3);
  68. break;
  69. case 5:
  70. case 1:
  71. skip_bits(gb, 2);
  72. value = get_bits(gb, 6);
  73. for (j = 0; j < value; j++)
  74. block[perm[i++]] = 0;
  75. break;
  76. case 6:
  77. skip_bits(gb, 3);
  78. block[perm[i]] = -s->qtable[perm[i]];
  79. i++;
  80. break;
  81. case 2:
  82. skip_bits(gb, 3);
  83. block[perm[i]] = s->qtable[perm[i]];
  84. i++;
  85. break;
  86. case 7: // 111b
  87. case 3: // 011b
  88. skip_bits(gb, 2);
  89. if (show_bits(gb, 6) == 0x3F) {
  90. skip_bits(gb, 6);
  91. block[perm[i]] = get_sbits(gb, 8) * s->qtable[perm[i]];
  92. } else {
  93. block[perm[i]] = get_sbits(gb, 6) * s->qtable[perm[i]];
  94. }
  95. i++;
  96. break;
  97. }
  98. }
  99. block[0] += 128 << 4;
  100. }
  101. static void tgq_idct_put_mb(TgqContext *s, int16_t (*block)[64], AVFrame *frame,
  102. int mb_x, int mb_y)
  103. {
  104. int linesize = frame->linesize[0];
  105. uint8_t *dest_y = frame->data[0] + (mb_y * 16 * linesize) + mb_x * 16;
  106. uint8_t *dest_cb = frame->data[1] + (mb_y * 8 * frame->linesize[1]) + mb_x * 8;
  107. uint8_t *dest_cr = frame->data[2] + (mb_y * 8 * frame->linesize[2]) + mb_x * 8;
  108. ff_ea_idct_put_c(dest_y , linesize, block[0]);
  109. ff_ea_idct_put_c(dest_y + 8, linesize, block[1]);
  110. ff_ea_idct_put_c(dest_y + 8 * linesize , linesize, block[2]);
  111. ff_ea_idct_put_c(dest_y + 8 * linesize + 8, linesize, block[3]);
  112. if (!(s->avctx->flags & CODEC_FLAG_GRAY)) {
  113. ff_ea_idct_put_c(dest_cb, frame->linesize[1], block[4]);
  114. ff_ea_idct_put_c(dest_cr, frame->linesize[2], block[5]);
  115. }
  116. }
  117. static inline void tgq_dconly(TgqContext *s, unsigned char *dst,
  118. int dst_stride, int dc)
  119. {
  120. int level = av_clip_uint8((dc*s->qtable[0] + 2056) >> 4);
  121. int j;
  122. for (j = 0; j < 8; j++)
  123. memset(dst + j * dst_stride, level, 8);
  124. }
  125. static void tgq_idct_put_mb_dconly(TgqContext *s, AVFrame *frame,
  126. int mb_x, int mb_y, const int8_t *dc)
  127. {
  128. int linesize = frame->linesize[0];
  129. uint8_t *dest_y = frame->data[0] + (mb_y * 16 * linesize) + mb_x * 16;
  130. uint8_t *dest_cb = frame->data[1] + (mb_y * 8 * frame->linesize[1]) + mb_x * 8;
  131. uint8_t *dest_cr = frame->data[2] + (mb_y * 8 * frame->linesize[2]) + mb_x * 8;
  132. tgq_dconly(s, dest_y, linesize, dc[0]);
  133. tgq_dconly(s, dest_y + 8, linesize, dc[1]);
  134. tgq_dconly(s, dest_y + 8 * linesize, linesize, dc[2]);
  135. tgq_dconly(s, dest_y + 8 * linesize + 8, linesize, dc[3]);
  136. if (!(s->avctx->flags & CODEC_FLAG_GRAY)) {
  137. tgq_dconly(s, dest_cb, frame->linesize[1], dc[4]);
  138. tgq_dconly(s, dest_cr, frame->linesize[2], dc[5]);
  139. }
  140. }
  141. static int tgq_decode_mb(TgqContext *s, AVFrame *frame, int mb_y, int mb_x)
  142. {
  143. int mode;
  144. int i;
  145. int8_t dc[6];
  146. mode = bytestream2_get_byte(&s->gb);
  147. if (mode > 12) {
  148. GetBitContext gb;
  149. init_get_bits(&gb, s->gb.buffer, FFMIN(bytestream2_get_bytes_left(&s->gb), mode) * 8);
  150. for (i = 0; i < 6; i++)
  151. tgq_decode_block(s, s->block[i], &gb);
  152. tgq_idct_put_mb(s, s->block, frame, mb_x, mb_y);
  153. bytestream2_skip(&s->gb, mode);
  154. } else {
  155. if (mode == 3) {
  156. memset(dc, bytestream2_get_byte(&s->gb), 4);
  157. dc[4] = bytestream2_get_byte(&s->gb);
  158. dc[5] = bytestream2_get_byte(&s->gb);
  159. } else if (mode == 6) {
  160. bytestream2_get_buffer(&s->gb, dc, 6);
  161. } else if (mode == 12) {
  162. for (i = 0; i < 6; i++) {
  163. dc[i] = bytestream2_get_byte(&s->gb);
  164. bytestream2_skip(&s->gb, 1);
  165. }
  166. } else {
  167. av_log(s->avctx, AV_LOG_ERROR, "unsupported mb mode %i\n", mode);
  168. return -1;
  169. }
  170. tgq_idct_put_mb_dconly(s, frame, mb_x, mb_y, dc);
  171. }
  172. return 0;
  173. }
  174. static void tgq_calculate_qtable(TgqContext *s, int quant)
  175. {
  176. int i, j;
  177. const int a = (14 * (100 - quant)) / 100 + 1;
  178. const int b = (11 * (100 - quant)) / 100 + 4;
  179. for (j = 0; j < 8; j++)
  180. for (i = 0; i < 8; i++)
  181. s->qtable[j * 8 + i] = ((a * (j + i) / (7 + 7) + b) *
  182. ff_inv_aanscales[j * 8 + i]) >> (14 - 4);
  183. }
  184. static int tgq_decode_frame(AVCodecContext *avctx,
  185. void *data, int *got_frame,
  186. AVPacket *avpkt)
  187. {
  188. const uint8_t *buf = avpkt->data;
  189. int buf_size = avpkt->size;
  190. TgqContext *s = avctx->priv_data;
  191. AVFrame *frame = data;
  192. int x, y, ret;
  193. int big_endian;
  194. if (buf_size < 16) {
  195. av_log(avctx, AV_LOG_WARNING, "truncated header\n");
  196. return AVERROR_INVALIDDATA;
  197. }
  198. big_endian = AV_RL32(&buf[4]) > 0x000FFFFF;
  199. bytestream2_init(&s->gb, buf + 8, buf_size - 8);
  200. if (big_endian) {
  201. s->width = bytestream2_get_be16u(&s->gb);
  202. s->height = bytestream2_get_be16u(&s->gb);
  203. } else {
  204. s->width = bytestream2_get_le16u(&s->gb);
  205. s->height = bytestream2_get_le16u(&s->gb);
  206. }
  207. if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
  208. avcodec_set_dimensions(s->avctx, s->width, s->height);
  209. }
  210. tgq_calculate_qtable(s, bytestream2_get_byteu(&s->gb));
  211. bytestream2_skip(&s->gb, 3);
  212. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  213. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  214. return ret;
  215. }
  216. frame->key_frame = 1;
  217. frame->pict_type = AV_PICTURE_TYPE_I;
  218. for (y = 0; y < FFALIGN(avctx->height, 16) >> 4; y++)
  219. for (x = 0; x < FFALIGN(avctx->width, 16) >> 4; x++)
  220. if (tgq_decode_mb(s, frame, y, x) < 0)
  221. return AVERROR_INVALIDDATA;
  222. *got_frame = 1;
  223. return avpkt->size;
  224. }
  225. AVCodec ff_eatgq_decoder = {
  226. .name = "eatgq",
  227. .type = AVMEDIA_TYPE_VIDEO,
  228. .id = AV_CODEC_ID_TGQ,
  229. .priv_data_size = sizeof(TgqContext),
  230. .init = tgq_decode_init,
  231. .decode = tgq_decode_frame,
  232. .capabilities = CODEC_CAP_DR1,
  233. .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGQ video"),
  234. };