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.

265 lines
8.7KB

  1. /*
  2. * Electronic Arts TGQ Video Decoder
  3. * Copyright (c) 2007-2008 Peter Ross <pross@xvid.org>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. AVFrame frame;
  40. int width, height;
  41. ScanTable scantable;
  42. int qtable[64];
  43. DECLARE_ALIGNED(16, int16_t, block)[6][64];
  44. GetByteContext gb;
  45. } TgqContext;
  46. static av_cold int tgq_decode_init(AVCodecContext *avctx)
  47. {
  48. TgqContext *s = avctx->priv_data;
  49. uint8_t idct_permutation[64];
  50. s->avctx = avctx;
  51. ff_init_scantable_permutation(idct_permutation, FF_NO_IDCT_PERM);
  52. ff_init_scantable(idct_permutation, &s->scantable, ff_zigzag_direct);
  53. avctx->time_base = (AVRational){1, 15};
  54. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  55. return 0;
  56. }
  57. static void tgq_decode_block(TgqContext *s, int16_t block[64], GetBitContext *gb)
  58. {
  59. uint8_t *perm = s->scantable.permutated;
  60. int i, j, value;
  61. block[0] = get_sbits(gb, 8) * s->qtable[0];
  62. for (i = 1; i < 64;) {
  63. switch (show_bits(gb, 3)) {
  64. case 4:
  65. block[perm[i++]] = 0;
  66. case 0:
  67. block[perm[i++]] = 0;
  68. skip_bits(gb, 3);
  69. break;
  70. case 5:
  71. case 1:
  72. skip_bits(gb, 2);
  73. value = get_bits(gb, 6);
  74. for (j = 0; j < value; j++)
  75. block[perm[i++]] = 0;
  76. break;
  77. case 6:
  78. skip_bits(gb, 3);
  79. block[perm[i]] = -s->qtable[perm[i]];
  80. i++;
  81. break;
  82. case 2:
  83. skip_bits(gb, 3);
  84. block[perm[i]] = s->qtable[perm[i]];
  85. i++;
  86. break;
  87. case 7: // 111b
  88. case 3: // 011b
  89. skip_bits(gb, 2);
  90. if (show_bits(gb, 6) == 0x3F) {
  91. skip_bits(gb, 6);
  92. block[perm[i]] = get_sbits(gb, 8) * s->qtable[perm[i]];
  93. } else {
  94. block[perm[i]] = get_sbits(gb, 6) * s->qtable[perm[i]];
  95. }
  96. i++;
  97. break;
  98. }
  99. }
  100. block[0] += 128 << 4;
  101. }
  102. static void tgq_idct_put_mb(TgqContext *s, int16_t (*block)[64],
  103. int mb_x, int mb_y)
  104. {
  105. int linesize = s->frame.linesize[0];
  106. uint8_t *dest_y = s->frame.data[0] + (mb_y * 16 * linesize) + mb_x * 16;
  107. uint8_t *dest_cb = s->frame.data[1] + (mb_y * 8 * s->frame.linesize[1]) + mb_x * 8;
  108. uint8_t *dest_cr = s->frame.data[2] + (mb_y * 8 * s->frame.linesize[2]) + mb_x * 8;
  109. ff_ea_idct_put_c(dest_y , linesize, block[0]);
  110. ff_ea_idct_put_c(dest_y + 8, linesize, block[1]);
  111. ff_ea_idct_put_c(dest_y + 8 * linesize , linesize, block[2]);
  112. ff_ea_idct_put_c(dest_y + 8 * linesize + 8, linesize, block[3]);
  113. if (!(s->avctx->flags & CODEC_FLAG_GRAY)) {
  114. ff_ea_idct_put_c(dest_cb, s->frame.linesize[1], block[4]);
  115. ff_ea_idct_put_c(dest_cr, s->frame.linesize[2], block[5]);
  116. }
  117. }
  118. static inline void tgq_dconly(TgqContext *s, unsigned char *dst,
  119. int dst_stride, int dc)
  120. {
  121. int level = av_clip_uint8((dc*s->qtable[0] + 2056) >> 4);
  122. int j;
  123. for (j = 0; j < 8; j++)
  124. memset(dst + j * dst_stride, level, 8);
  125. }
  126. static void tgq_idct_put_mb_dconly(TgqContext *s, int mb_x, int mb_y, const int8_t *dc)
  127. {
  128. int linesize = s->frame.linesize[0];
  129. uint8_t *dest_y = s->frame.data[0] + (mb_y * 16 * linesize) + mb_x * 16;
  130. uint8_t *dest_cb = s->frame.data[1] + (mb_y * 8 * s->frame.linesize[1]) + mb_x * 8;
  131. uint8_t *dest_cr = s->frame.data[2] + (mb_y * 8 * s->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, s->frame.linesize[1], dc[4]);
  138. tgq_dconly(s, dest_cr, s->frame.linesize[2], dc[5]);
  139. }
  140. }
  141. static void tgq_decode_mb(TgqContext *s, 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(s->gb.buffer_end - s->gb.buffer, 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, 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. }
  169. tgq_idct_put_mb_dconly(s, mb_x, mb_y, dc);
  170. }
  171. }
  172. static void tgq_calculate_qtable(TgqContext *s, int quant)
  173. {
  174. int i, j;
  175. const int a = (14 * (100 - quant)) / 100 + 1;
  176. const int b = (11 * (100 - quant)) / 100 + 4;
  177. for (j = 0; j < 8; j++)
  178. for (i = 0; i < 8; i++)
  179. s->qtable[j * 8 + i] = ((a * (j + i) / (7 + 7) + b) *
  180. ff_inv_aanscales[j * 8 + i]) >> (14 - 4);
  181. }
  182. static int tgq_decode_frame(AVCodecContext *avctx,
  183. void *data, int *got_frame,
  184. AVPacket *avpkt)
  185. {
  186. const uint8_t *buf = avpkt->data;
  187. int buf_size = avpkt->size;
  188. TgqContext *s = avctx->priv_data;
  189. int x, y, ret;
  190. int big_endian = AV_RL32(&buf[4]) > 0x000FFFFF;
  191. if (buf_size < 16) {
  192. av_log(avctx, AV_LOG_WARNING, "truncated header\n");
  193. return AVERROR_INVALIDDATA;
  194. }
  195. bytestream2_init(&s->gb, buf + 8, buf_size - 8);
  196. if (big_endian) {
  197. s->width = bytestream2_get_be16u(&s->gb);
  198. s->height = bytestream2_get_be16u(&s->gb);
  199. } else {
  200. s->width = bytestream2_get_le16u(&s->gb);
  201. s->height = bytestream2_get_le16u(&s->gb);
  202. }
  203. if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
  204. avcodec_set_dimensions(s->avctx, s->width, s->height);
  205. if (s->frame.data[0])
  206. avctx->release_buffer(avctx, &s->frame);
  207. }
  208. tgq_calculate_qtable(s, bytestream2_get_byteu(&s->gb));
  209. bytestream2_skip(&s->gb, 3);
  210. if (!s->frame.data[0]) {
  211. s->frame.key_frame = 1;
  212. s->frame.pict_type = AV_PICTURE_TYPE_I;
  213. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
  214. if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
  215. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  216. return ret;
  217. }
  218. }
  219. for (y = 0; y < FFALIGN(avctx->height, 16) >> 4; y++)
  220. for (x = 0; x < FFALIGN(avctx->width, 16) >> 4; x++)
  221. tgq_decode_mb(s, y, x);
  222. *got_frame = 1;
  223. *(AVFrame*)data = s->frame;
  224. return avpkt->size;
  225. }
  226. static av_cold int tgq_decode_end(AVCodecContext *avctx)
  227. {
  228. TgqContext *s = avctx->priv_data;
  229. if (s->frame.data[0])
  230. s->avctx->release_buffer(avctx, &s->frame);
  231. return 0;
  232. }
  233. AVCodec ff_eatgq_decoder = {
  234. .name = "eatgq",
  235. .type = AVMEDIA_TYPE_VIDEO,
  236. .id = AV_CODEC_ID_TGQ,
  237. .priv_data_size = sizeof(TgqContext),
  238. .init = tgq_decode_init,
  239. .close = tgq_decode_end,
  240. .decode = tgq_decode_frame,
  241. .capabilities = CODEC_CAP_DR1,
  242. .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGQ video"),
  243. };