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.

159 lines
4.0KB

  1. /*
  2. * AVS video decoder.
  3. * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avcodec.h"
  20. #include "bitstream.h"
  21. typedef struct {
  22. AVFrame picture;
  23. } avs_context_t;
  24. typedef enum {
  25. AVS_VIDEO = 0x01,
  26. AVS_AUDIO = 0x02,
  27. AVS_PALETTE = 0x03,
  28. AVS_GAME_DATA = 0x04,
  29. } avs_block_type_t;
  30. typedef enum {
  31. AVS_I_FRAME = 0x00,
  32. AVS_P_FRAME_3X3 = 0x01,
  33. AVS_P_FRAME_2X2 = 0x02,
  34. AVS_P_FRAME_2X3 = 0x03,
  35. } avs_video_sub_type_t;
  36. static int
  37. avs_decode_frame(AVCodecContext * avctx,
  38. void *data, int *data_size, uint8_t * buf, int buf_size)
  39. {
  40. avs_context_t *const avs = avctx->priv_data;
  41. AVFrame *picture = data;
  42. AVFrame *const p = (AVFrame *) & avs->picture;
  43. uint8_t *table, *vect, *out;
  44. int i, j, x, y, stride, vect_w = 3, vect_h = 3;
  45. int sub_type;
  46. avs_block_type_t type;
  47. GetBitContext change_map;
  48. if (avctx->reget_buffer(avctx, p)) {
  49. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  50. return -1;
  51. }
  52. p->reference = 1;
  53. p->pict_type = FF_P_TYPE;
  54. p->key_frame = 0;
  55. out = avs->picture.data[0];
  56. stride = avs->picture.linesize[0];
  57. sub_type = buf[0];
  58. type = buf[1];
  59. buf += 4;
  60. if (type == AVS_PALETTE) {
  61. int first, last;
  62. uint32_t *pal = (uint32_t *) avs->picture.data[1];
  63. first = LE_16(buf);
  64. last = first + LE_16(buf + 2);
  65. buf += 4;
  66. for (i=first; i<last; i++, buf+=3)
  67. pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
  68. sub_type = buf[0];
  69. type = buf[1];
  70. buf += 4;
  71. }
  72. if (type != AVS_VIDEO)
  73. return -1;
  74. switch (sub_type) {
  75. case AVS_I_FRAME:
  76. p->pict_type = FF_I_TYPE;
  77. p->key_frame = 1;
  78. case AVS_P_FRAME_3X3:
  79. vect_w = 3;
  80. vect_h = 3;
  81. break;
  82. case AVS_P_FRAME_2X2:
  83. vect_w = 2;
  84. vect_h = 2;
  85. break;
  86. case AVS_P_FRAME_2X3:
  87. vect_w = 2;
  88. vect_h = 3;
  89. break;
  90. default:
  91. return -1;
  92. }
  93. table = buf + (256 * vect_w * vect_h);
  94. if (sub_type != AVS_I_FRAME) {
  95. int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
  96. init_get_bits(&change_map, table, map_size);
  97. table += map_size;
  98. }
  99. for (y=0; y<198; y+=vect_h) {
  100. for (x=0; x<318; x+=vect_w) {
  101. if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {
  102. vect = &buf[*table++ * (vect_w * vect_h)];
  103. for (j=0; j<vect_w; j++) {
  104. out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
  105. out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
  106. if (vect_h == 3)
  107. out[(y + 2) * stride + x + j] =
  108. vect[(2 * vect_w) + j];
  109. }
  110. }
  111. }
  112. if (sub_type != AVS_I_FRAME)
  113. align_get_bits(&change_map);
  114. }
  115. *picture = *(AVFrame *) & avs->picture;
  116. *data_size = sizeof(AVPicture);
  117. return buf_size;
  118. }
  119. static int avs_decode_init(AVCodecContext * avctx)
  120. {
  121. avctx->pix_fmt = PIX_FMT_PAL8;
  122. return 0;
  123. }
  124. AVCodec avs_decoder = {
  125. "avs",
  126. CODEC_TYPE_VIDEO,
  127. CODEC_ID_AVS,
  128. sizeof(avs_context_t),
  129. avs_decode_init,
  130. NULL,
  131. NULL,
  132. avs_decode_frame,
  133. CODEC_CAP_DR1,
  134. };