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.

165 lines
4.2KB

  1. /*
  2. * AVS video decoder.
  3. * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.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 Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. #include "get_bits.h"
  23. typedef struct {
  24. AVFrame picture;
  25. } AvsContext;
  26. typedef enum {
  27. AVS_VIDEO = 0x01,
  28. AVS_AUDIO = 0x02,
  29. AVS_PALETTE = 0x03,
  30. AVS_GAME_DATA = 0x04,
  31. } AvsBlockType;
  32. typedef enum {
  33. AVS_I_FRAME = 0x00,
  34. AVS_P_FRAME_3X3 = 0x01,
  35. AVS_P_FRAME_2X2 = 0x02,
  36. AVS_P_FRAME_2X3 = 0x03,
  37. } AvsVideoSubType;
  38. static int
  39. avs_decode_frame(AVCodecContext * avctx,
  40. void *data, int *data_size, AVPacket *avpkt)
  41. {
  42. const uint8_t *buf = avpkt->data;
  43. int buf_size = avpkt->size;
  44. AvsContext *const avs = avctx->priv_data;
  45. AVFrame *picture = data;
  46. AVFrame *const p = (AVFrame *) & avs->picture;
  47. const uint8_t *table, *vect;
  48. uint8_t *out;
  49. int i, j, x, y, stride, vect_w = 3, vect_h = 3;
  50. AvsVideoSubType sub_type;
  51. AvsBlockType type;
  52. GetBitContext change_map;
  53. if (avctx->reget_buffer(avctx, p)) {
  54. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  55. return -1;
  56. }
  57. p->reference = 1;
  58. p->pict_type = FF_P_TYPE;
  59. p->key_frame = 0;
  60. out = avs->picture.data[0];
  61. stride = avs->picture.linesize[0];
  62. sub_type = buf[0];
  63. type = buf[1];
  64. buf += 4;
  65. if (type == AVS_PALETTE) {
  66. int first, last;
  67. uint32_t *pal = (uint32_t *) avs->picture.data[1];
  68. first = AV_RL16(buf);
  69. last = first + AV_RL16(buf + 2);
  70. buf += 4;
  71. for (i=first; i<last; i++, buf+=3)
  72. pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
  73. sub_type = buf[0];
  74. type = buf[1];
  75. buf += 4;
  76. }
  77. if (type != AVS_VIDEO)
  78. return -1;
  79. switch (sub_type) {
  80. case AVS_I_FRAME:
  81. p->pict_type = FF_I_TYPE;
  82. p->key_frame = 1;
  83. case AVS_P_FRAME_3X3:
  84. vect_w = 3;
  85. vect_h = 3;
  86. break;
  87. case AVS_P_FRAME_2X2:
  88. vect_w = 2;
  89. vect_h = 2;
  90. break;
  91. case AVS_P_FRAME_2X3:
  92. vect_w = 2;
  93. vect_h = 3;
  94. break;
  95. default:
  96. return -1;
  97. }
  98. table = buf + (256 * vect_w * vect_h);
  99. if (sub_type != AVS_I_FRAME) {
  100. int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
  101. init_get_bits(&change_map, table, map_size);
  102. table += map_size;
  103. }
  104. for (y=0; y<198; y+=vect_h) {
  105. for (x=0; x<318; x+=vect_w) {
  106. if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {
  107. vect = &buf[*table++ * (vect_w * vect_h)];
  108. for (j=0; j<vect_w; j++) {
  109. out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
  110. out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
  111. if (vect_h == 3)
  112. out[(y + 2) * stride + x + j] =
  113. vect[(2 * vect_w) + j];
  114. }
  115. }
  116. }
  117. if (sub_type != AVS_I_FRAME)
  118. align_get_bits(&change_map);
  119. }
  120. *picture = *(AVFrame *) & avs->picture;
  121. *data_size = sizeof(AVPicture);
  122. return buf_size;
  123. }
  124. static av_cold int avs_decode_init(AVCodecContext * avctx)
  125. {
  126. avctx->pix_fmt = PIX_FMT_PAL8;
  127. return 0;
  128. }
  129. AVCodec avs_decoder = {
  130. "avs",
  131. CODEC_TYPE_VIDEO,
  132. CODEC_ID_AVS,
  133. sizeof(AvsContext),
  134. avs_decode_init,
  135. NULL,
  136. NULL,
  137. avs_decode_frame,
  138. CODEC_CAP_DR1,
  139. .long_name = NULL_IF_CONFIG_SMALL("AVS (Audio Video Standard) video"),
  140. };