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.

191 lines
5.2KB

  1. /*
  2. * AVS video decoder.
  3. * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.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 Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. #include "bitstream.h"
  23. #include "internal.h"
  24. typedef struct AvsContext {
  25. AVFrame *frame;
  26. } AvsContext;
  27. typedef enum {
  28. AVS_VIDEO = 0x01,
  29. AVS_AUDIO = 0x02,
  30. AVS_PALETTE = 0x03,
  31. AVS_GAME_DATA = 0x04,
  32. } AvsBlockType;
  33. typedef enum {
  34. AVS_I_FRAME = 0x00,
  35. AVS_P_FRAME_3X3 = 0x01,
  36. AVS_P_FRAME_2X2 = 0x02,
  37. AVS_P_FRAME_2X3 = 0x03,
  38. } AvsVideoSubType;
  39. static int
  40. avs_decode_frame(AVCodecContext * avctx,
  41. void *data, int *got_frame, AVPacket *avpkt)
  42. {
  43. const uint8_t *buf = avpkt->data;
  44. const uint8_t *buf_end = avpkt->data + avpkt->size;
  45. int buf_size = avpkt->size;
  46. AvsContext *const avs = avctx->priv_data;
  47. AVFrame *picture = data;
  48. AVFrame *const p = avs->frame;
  49. const uint8_t *table, *vect;
  50. uint8_t *out;
  51. int i, j, x, y, stride, ret, vect_w = 3, vect_h = 3;
  52. AvsVideoSubType sub_type;
  53. AvsBlockType type;
  54. BitstreamContext change_map;
  55. if ((ret = ff_reget_buffer(avctx, p)) < 0) {
  56. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  57. return ret;
  58. }
  59. p->pict_type = AV_PICTURE_TYPE_P;
  60. p->key_frame = 0;
  61. out = p->data[0];
  62. stride = p->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 *) p->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. sub_type = buf[0];
  79. type = buf[1];
  80. buf += 4;
  81. }
  82. if (type != AVS_VIDEO)
  83. return AVERROR_INVALIDDATA;
  84. switch (sub_type) {
  85. case AVS_I_FRAME:
  86. p->pict_type = AV_PICTURE_TYPE_I;
  87. p->key_frame = 1;
  88. case AVS_P_FRAME_3X3:
  89. vect_w = 3;
  90. vect_h = 3;
  91. break;
  92. case AVS_P_FRAME_2X2:
  93. vect_w = 2;
  94. vect_h = 2;
  95. break;
  96. case AVS_P_FRAME_2X3:
  97. vect_w = 2;
  98. vect_h = 3;
  99. break;
  100. default:
  101. return AVERROR_INVALIDDATA;
  102. }
  103. if (buf_end - buf < 256 * vect_w * vect_h)
  104. return AVERROR_INVALIDDATA;
  105. table = buf + (256 * vect_w * vect_h);
  106. if (sub_type != AVS_I_FRAME) {
  107. int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
  108. if (buf_end - table < map_size)
  109. return AVERROR_INVALIDDATA;
  110. bitstream_init8(&change_map, table, map_size);
  111. table += map_size;
  112. }
  113. for (y=0; y<198; y+=vect_h) {
  114. for (x=0; x<318; x+=vect_w) {
  115. if (sub_type == AVS_I_FRAME || bitstream_read_bit(&change_map)) {
  116. if (buf_end - table < 1)
  117. return AVERROR_INVALIDDATA;
  118. vect = &buf[*table++ * (vect_w * vect_h)];
  119. for (j=0; j<vect_w; j++) {
  120. out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
  121. out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
  122. if (vect_h == 3)
  123. out[(y + 2) * stride + x + j] =
  124. vect[(2 * vect_w) + j];
  125. }
  126. }
  127. }
  128. if (sub_type != AVS_I_FRAME)
  129. bitstream_align(&change_map);
  130. }
  131. if ((ret = av_frame_ref(picture, p)) < 0)
  132. return ret;
  133. *got_frame = 1;
  134. return buf_size;
  135. }
  136. static av_cold int avs_decode_init(AVCodecContext * avctx)
  137. {
  138. AvsContext *s = avctx->priv_data;
  139. s->frame = av_frame_alloc();
  140. if (!s->frame)
  141. return AVERROR(ENOMEM);
  142. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  143. return ff_set_dimensions(avctx, 318, 198);
  144. }
  145. static av_cold int avs_decode_end(AVCodecContext *avctx)
  146. {
  147. AvsContext *s = avctx->priv_data;
  148. av_frame_free(&s->frame);
  149. return 0;
  150. }
  151. AVCodec ff_avs_decoder = {
  152. .name = "avs",
  153. .long_name = NULL_IF_CONFIG_SMALL("AVS (Audio Video Standard) video"),
  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 = AV_CODEC_CAP_DR1,
  161. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  162. };