avs.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "bitstream.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, const uint8_t * buf, int buf_size)
  41. {
  42. AvsContext *const avs = avctx->priv_data;
  43. AVFrame *picture = data;
  44. AVFrame *const p = (AVFrame *) & avs->picture;
  45. const uint8_t *table, *vect;
  46. uint8_t *out;
  47. int i, j, x, y, stride, vect_w = 3, vect_h = 3;
  48. AvsVideoSubType sub_type;
  49. AvsBlockType type;
  50. GetBitContext change_map;
  51. if (avctx->reget_buffer(avctx, p)) {
  52. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  53. return -1;
  54. }
  55. p->reference = 1;
  56. p->pict_type = FF_P_TYPE;
  57. p->key_frame = 0;
  58. out = avs->picture.data[0];
  59. stride = avs->picture.linesize[0];
  60. sub_type = buf[0];
  61. type = buf[1];
  62. buf += 4;
  63. if (type == AVS_PALETTE) {
  64. int first, last;
  65. uint32_t *pal = (uint32_t *) avs->picture.data[1];
  66. first = AV_RL16(buf);
  67. last = first + AV_RL16(buf + 2);
  68. buf += 4;
  69. for (i=first; i<last; i++, buf+=3)
  70. pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
  71. sub_type = buf[0];
  72. type = buf[1];
  73. buf += 4;
  74. }
  75. if (type != AVS_VIDEO)
  76. return -1;
  77. switch (sub_type) {
  78. case AVS_I_FRAME:
  79. p->pict_type = FF_I_TYPE;
  80. p->key_frame = 1;
  81. case AVS_P_FRAME_3X3:
  82. vect_w = 3;
  83. vect_h = 3;
  84. break;
  85. case AVS_P_FRAME_2X2:
  86. vect_w = 2;
  87. vect_h = 2;
  88. break;
  89. case AVS_P_FRAME_2X3:
  90. vect_w = 2;
  91. vect_h = 3;
  92. break;
  93. default:
  94. return -1;
  95. }
  96. table = buf + (256 * vect_w * vect_h);
  97. if (sub_type != AVS_I_FRAME) {
  98. int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
  99. init_get_bits(&change_map, table, map_size);
  100. table += map_size;
  101. }
  102. for (y=0; y<198; y+=vect_h) {
  103. for (x=0; x<318; x+=vect_w) {
  104. if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {
  105. vect = &buf[*table++ * (vect_w * vect_h)];
  106. for (j=0; j<vect_w; j++) {
  107. out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
  108. out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
  109. if (vect_h == 3)
  110. out[(y + 2) * stride + x + j] =
  111. vect[(2 * vect_w) + j];
  112. }
  113. }
  114. }
  115. if (sub_type != AVS_I_FRAME)
  116. align_get_bits(&change_map);
  117. }
  118. *picture = *(AVFrame *) & avs->picture;
  119. *data_size = sizeof(AVPicture);
  120. return buf_size;
  121. }
  122. static av_cold int avs_decode_init(AVCodecContext * avctx)
  123. {
  124. avctx->pix_fmt = PIX_FMT_PAL8;
  125. avcodec_set_dimensions(avctx, 318, 198);
  126. return 0;
  127. }
  128. AVCodec avs_decoder = {
  129. "avs",
  130. CODEC_TYPE_VIDEO,
  131. CODEC_ID_AVS,
  132. sizeof(AvsContext),
  133. avs_decode_init,
  134. NULL,
  135. NULL,
  136. avs_decode_frame,
  137. CODEC_CAP_DR1,
  138. .long_name = NULL_IF_CONFIG_SMALL("AVS (Audio Video Standard) video"),
  139. };