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.

252 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 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. #define BITSTREAM_READER_LE
  30. #include "aandcttab.h"
  31. #include "avcodec.h"
  32. #include "bitstream.h"
  33. #include "bytestream.h"
  34. #include "eaidct.h"
  35. #include "idctdsp.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_IDCT_PERM_NONE);
  51. ff_init_scantable(idct_permutation, &s->scantable, ff_zigzag_direct);
  52. avctx->framerate = (AVRational){ 15, 1 };
  53. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  54. return 0;
  55. }
  56. static void tgq_decode_block(TgqContext *s, int16_t block[64], BitstreamContext *bc)
  57. {
  58. uint8_t *perm = s->scantable.permutated;
  59. int i, j, value;
  60. block[0] = bitstream_read_signed(bc, 8) * s->qtable[0];
  61. for (i = 1; i < 64;) {
  62. switch (bitstream_peek(bc, 3)) {
  63. case 4:
  64. block[perm[i++]] = 0;
  65. case 0:
  66. block[perm[i++]] = 0;
  67. bitstream_skip(bc, 3);
  68. break;
  69. case 5:
  70. case 1:
  71. bitstream_skip(bc, 2);
  72. value = bitstream_read(bc, 6);
  73. for (j = 0; j < value; j++)
  74. block[perm[i++]] = 0;
  75. break;
  76. case 6:
  77. bitstream_skip(bc, 3);
  78. block[perm[i]] = -s->qtable[perm[i]];
  79. i++;
  80. break;
  81. case 2:
  82. bitstream_skip(bc, 3);
  83. block[perm[i]] = s->qtable[perm[i]];
  84. i++;
  85. break;
  86. case 7: // 111b
  87. case 3: // 011b
  88. bitstream_skip(bc, 2);
  89. if (bitstream_peek(bc, 6) == 0x3F) {
  90. bitstream_skip(bc, 6);
  91. block[perm[i]] = bitstream_read_signed(bc, 8) * s->qtable[perm[i]];
  92. } else {
  93. block[perm[i]] = bitstream_read_signed(bc, 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. ptrdiff_t 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 & AV_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. ptrdiff_t 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. ptrdiff_t 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 & AV_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 void 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. BitstreamContext bc;
  149. bitstream_init8(&bc, s->gb.buffer, FFMIN(s->gb.buffer_end - s->gb.buffer, mode));
  150. for (i = 0; i < 6; i++)
  151. tgq_decode_block(s, s->block[i], &bc);
  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. }
  169. tgq_idct_put_mb_dconly(s, frame, 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. AVFrame *frame = data;
  190. int x, y, ret;
  191. int big_endian = AV_RL32(&buf[4]) > 0x000FFFFF;
  192. if (buf_size < 16) {
  193. av_log(avctx, AV_LOG_WARNING, "truncated header\n");
  194. return AVERROR_INVALIDDATA;
  195. }
  196. bytestream2_init(&s->gb, buf + 8, buf_size - 8);
  197. if (big_endian) {
  198. s->width = bytestream2_get_be16u(&s->gb);
  199. s->height = bytestream2_get_be16u(&s->gb);
  200. } else {
  201. s->width = bytestream2_get_le16u(&s->gb);
  202. s->height = bytestream2_get_le16u(&s->gb);
  203. }
  204. ret = ff_set_dimensions(s->avctx, s->width, s->height);
  205. if (ret < 0)
  206. return ret;
  207. tgq_calculate_qtable(s, bytestream2_get_byteu(&s->gb));
  208. bytestream2_skip(&s->gb, 3);
  209. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  210. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  211. return ret;
  212. }
  213. frame->key_frame = 1;
  214. frame->pict_type = AV_PICTURE_TYPE_I;
  215. for (y = 0; y < FFALIGN(avctx->height, 16) >> 4; y++)
  216. for (x = 0; x < FFALIGN(avctx->width, 16) >> 4; x++)
  217. tgq_decode_mb(s, frame, y, x);
  218. *got_frame = 1;
  219. return avpkt->size;
  220. }
  221. AVCodec ff_eatgq_decoder = {
  222. .name = "eatgq",
  223. .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts TGQ video"),
  224. .type = AVMEDIA_TYPE_VIDEO,
  225. .id = AV_CODEC_ID_TGQ,
  226. .priv_data_size = sizeof(TgqContext),
  227. .init = tgq_decode_init,
  228. .decode = tgq_decode_frame,
  229. .capabilities = AV_CODEC_CAP_DR1,
  230. };