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.

189 lines
5.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. const uint8_t *buf_end = avpkt->data + avpkt->size;
  44. int buf_size = avpkt->size;
  45. AvsContext *const avs = avctx->priv_data;
  46. AVFrame *picture = data;
  47. AVFrame *const p = &avs->picture;
  48. const uint8_t *table, *vect;
  49. uint8_t *out;
  50. int i, j, x, y, stride, vect_w = 3, vect_h = 3;
  51. AvsVideoSubType sub_type;
  52. AvsBlockType type;
  53. GetBitContext change_map;
  54. if (avctx->reget_buffer(avctx, p)) {
  55. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  56. return -1;
  57. }
  58. p->reference = 3;
  59. p->pict_type = AV_PICTURE_TYPE_P;
  60. p->key_frame = 0;
  61. out = avs->picture.data[0];
  62. stride = avs->picture.linesize[0];
  63. if (buf_end - buf < 4)
  64. return AVERROR_INVALIDDATA;
  65. sub_type = buf[0];
  66. type = buf[1];
  67. buf += 4;
  68. if (type == AVS_PALETTE) {
  69. int first, last;
  70. uint32_t *pal = (uint32_t *) avs->picture.data[1];
  71. first = AV_RL16(buf);
  72. last = first + AV_RL16(buf + 2);
  73. if (first >= 256 || last > 256 || buf_end - buf < 4 + 4 + 3 * (last - first))
  74. return AVERROR_INVALIDDATA;
  75. buf += 4;
  76. for (i=first; i<last; i++, buf+=3) {
  77. pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
  78. pal[i] |= 0xFF << 24 | (pal[i] >> 6) & 0x30303;
  79. }
  80. sub_type = buf[0];
  81. type = buf[1];
  82. buf += 4;
  83. }
  84. if (type != AVS_VIDEO)
  85. return -1;
  86. switch (sub_type) {
  87. case AVS_I_FRAME:
  88. p->pict_type = AV_PICTURE_TYPE_I;
  89. p->key_frame = 1;
  90. case AVS_P_FRAME_3X3:
  91. vect_w = 3;
  92. vect_h = 3;
  93. break;
  94. case AVS_P_FRAME_2X2:
  95. vect_w = 2;
  96. vect_h = 2;
  97. break;
  98. case AVS_P_FRAME_2X3:
  99. vect_w = 2;
  100. vect_h = 3;
  101. break;
  102. default:
  103. return -1;
  104. }
  105. if (buf_end - buf < 256 * vect_w * vect_h)
  106. return AVERROR_INVALIDDATA;
  107. table = buf + (256 * vect_w * vect_h);
  108. if (sub_type != AVS_I_FRAME) {
  109. int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
  110. if (buf_end - table < map_size)
  111. return AVERROR_INVALIDDATA;
  112. init_get_bits(&change_map, table, map_size * 8);
  113. table += map_size;
  114. }
  115. for (y=0; y<198; y+=vect_h) {
  116. for (x=0; x<318; x+=vect_w) {
  117. if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {
  118. if (buf_end - table < 1)
  119. return AVERROR_INVALIDDATA;
  120. vect = &buf[*table++ * (vect_w * vect_h)];
  121. for (j=0; j<vect_w; j++) {
  122. out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
  123. out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
  124. if (vect_h == 3)
  125. out[(y + 2) * stride + x + j] =
  126. vect[(2 * vect_w) + j];
  127. }
  128. }
  129. }
  130. if (sub_type != AVS_I_FRAME)
  131. align_get_bits(&change_map);
  132. }
  133. *picture = avs->picture;
  134. *data_size = sizeof(AVPicture);
  135. return buf_size;
  136. }
  137. static av_cold int avs_decode_init(AVCodecContext * avctx)
  138. {
  139. AvsContext *const avs = avctx->priv_data;
  140. avctx->pix_fmt = PIX_FMT_PAL8;
  141. avcodec_get_frame_defaults(&avs->picture);
  142. avcodec_set_dimensions(avctx, 318, 198);
  143. return 0;
  144. }
  145. static av_cold int avs_decode_end(AVCodecContext *avctx)
  146. {
  147. AvsContext *s = avctx->priv_data;
  148. if (s->picture.data[0])
  149. avctx->release_buffer(avctx, &s->picture);
  150. return 0;
  151. }
  152. AVCodec ff_avs_decoder = {
  153. .name = "avs",
  154. .type = AVMEDIA_TYPE_VIDEO,
  155. .id = AV_CODEC_ID_AVS,
  156. .priv_data_size = sizeof(AvsContext),
  157. .init = avs_decode_init,
  158. .decode = avs_decode_frame,
  159. .close = avs_decode_end,
  160. .capabilities = CODEC_CAP_DR1,
  161. .long_name = NULL_IF_CONFIG_SMALL("AVS (Audio Video Standard) video"),
  162. };