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.

274 lines
7.8KB

  1. /*
  2. * Tiertex Limited SEQ Video Decoder
  3. * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
  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. /**
  22. * @file
  23. * Tiertex Limited SEQ video decoder
  24. */
  25. #define BITSTREAM_READER_LE
  26. #include "avcodec.h"
  27. #include "bitstream.h"
  28. #include "internal.h"
  29. typedef struct SeqVideoContext {
  30. AVCodecContext *avctx;
  31. AVFrame *frame;
  32. } SeqVideoContext;
  33. static const unsigned char *seq_unpack_rle_block(const unsigned char *src,
  34. const unsigned char *src_end,
  35. unsigned char *dst, int dst_size)
  36. {
  37. int i, len, sz;
  38. BitstreamContext bc;
  39. int code_table[64];
  40. /* get the rle codes */
  41. bitstream_init8(&bc, src, src_end - src);
  42. for (i = 0, sz = 0; i < 64 && sz < dst_size; i++) {
  43. if (bitstream_bits_left(&bc) < 4)
  44. return NULL;
  45. code_table[i] = bitstream_read_signed(&bc, 4);
  46. sz += FFABS(code_table[i]);
  47. }
  48. src += (bitstream_tell(&bc) + 7) / 8;
  49. /* do the rle unpacking */
  50. for (i = 0; i < 64 && dst_size > 0; i++) {
  51. len = code_table[i];
  52. if (len < 0) {
  53. len = -len;
  54. if (src_end - src < 1)
  55. return NULL;
  56. memset(dst, *src++, FFMIN(len, dst_size));
  57. } else {
  58. if (src_end - src < len)
  59. return NULL;
  60. memcpy(dst, src, FFMIN(len, dst_size));
  61. src += len;
  62. }
  63. dst += len;
  64. dst_size -= len;
  65. }
  66. return src;
  67. }
  68. static const unsigned char *seq_decode_op1(SeqVideoContext *seq,
  69. const unsigned char *src,
  70. const unsigned char *src_end,
  71. unsigned char *dst)
  72. {
  73. const unsigned char *color_table;
  74. int b, i, len, bits;
  75. BitstreamContext bc;
  76. unsigned char block[8 * 8];
  77. if (src_end - src < 1)
  78. return NULL;
  79. len = *src++;
  80. if (len & 0x80) {
  81. switch (len & 3) {
  82. case 1:
  83. src = seq_unpack_rle_block(src, src_end, block, sizeof(block));
  84. for (b = 0; b < 8; b++) {
  85. memcpy(dst, &block[b * 8], 8);
  86. dst += seq->frame->linesize[0];
  87. }
  88. break;
  89. case 2:
  90. src = seq_unpack_rle_block(src, src_end, block, sizeof(block));
  91. for (i = 0; i < 8; i++) {
  92. for (b = 0; b < 8; b++)
  93. dst[b * seq->frame->linesize[0]] = block[i * 8 + b];
  94. ++dst;
  95. }
  96. break;
  97. }
  98. } else {
  99. if (len <= 0)
  100. return NULL;
  101. bits = ff_log2_tab[len - 1] + 1;
  102. if (src_end - src < len + 8 * bits)
  103. return NULL;
  104. color_table = src;
  105. src += len;
  106. bitstream_init8(&bc, src, bits * 8);
  107. src += bits * 8;
  108. for (b = 0; b < 8; b++) {
  109. for (i = 0; i < 8; i++)
  110. dst[i] = color_table[bitstream_read(&bc, bits)];
  111. dst += seq->frame->linesize[0];
  112. }
  113. }
  114. return src;
  115. }
  116. static const unsigned char *seq_decode_op2(SeqVideoContext *seq,
  117. const unsigned char *src,
  118. const unsigned char *src_end,
  119. unsigned char *dst)
  120. {
  121. int i;
  122. if (src_end - src < 8 * 8)
  123. return NULL;
  124. for (i = 0; i < 8; i++) {
  125. memcpy(dst, src, 8);
  126. src += 8;
  127. dst += seq->frame->linesize[0];
  128. }
  129. return src;
  130. }
  131. static const unsigned char *seq_decode_op3(SeqVideoContext *seq,
  132. const unsigned char *src,
  133. const unsigned char *src_end,
  134. unsigned char *dst)
  135. {
  136. int pos, offset;
  137. do {
  138. if (src_end - src < 2)
  139. return NULL;
  140. pos = *src++;
  141. offset = ((pos >> 3) & 7) * seq->frame->linesize[0] + (pos & 7);
  142. dst[offset] = *src++;
  143. } while (!(pos & 0x80));
  144. return src;
  145. }
  146. static int seqvideo_decode(SeqVideoContext *seq, const unsigned char *data, int data_size)
  147. {
  148. const unsigned char *data_end = data + data_size;
  149. BitstreamContext bc;
  150. int flags, i, j, x, y, op;
  151. unsigned char c[3];
  152. unsigned char *dst;
  153. uint32_t *palette;
  154. flags = *data++;
  155. if (flags & 1) {
  156. palette = (uint32_t *)seq->frame->data[1];
  157. if (data_end - data < 256 * 3)
  158. return AVERROR_INVALIDDATA;
  159. for (i = 0; i < 256; i++) {
  160. for (j = 0; j < 3; j++, data++)
  161. c[j] = (*data << 2) | (*data >> 4);
  162. palette[i] = AV_RB24(c);
  163. }
  164. seq->frame->palette_has_changed = 1;
  165. }
  166. if (flags & 2) {
  167. if (data_end - data < 128)
  168. return AVERROR_INVALIDDATA;
  169. bitstream_init8(&bc, data, 128);
  170. data += 128;
  171. for (y = 0; y < 128; y += 8)
  172. for (x = 0; x < 256; x += 8) {
  173. dst = &seq->frame->data[0][y * seq->frame->linesize[0] + x];
  174. op = bitstream_read(&bc, 2);
  175. switch (op) {
  176. case 1:
  177. data = seq_decode_op1(seq, data, data_end, dst);
  178. break;
  179. case 2:
  180. data = seq_decode_op2(seq, data, data_end, dst);
  181. break;
  182. case 3:
  183. data = seq_decode_op3(seq, data, data_end, dst);
  184. break;
  185. }
  186. if (!data)
  187. return AVERROR_INVALIDDATA;
  188. }
  189. }
  190. return 0;
  191. }
  192. static av_cold int seqvideo_decode_init(AVCodecContext *avctx)
  193. {
  194. SeqVideoContext *seq = avctx->priv_data;
  195. seq->avctx = avctx;
  196. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  197. seq->frame = av_frame_alloc();
  198. if (!seq->frame)
  199. return AVERROR(ENOMEM);
  200. return 0;
  201. }
  202. static int seqvideo_decode_frame(AVCodecContext *avctx,
  203. void *data, int *got_frame,
  204. AVPacket *avpkt)
  205. {
  206. const uint8_t *buf = avpkt->data;
  207. int buf_size = avpkt->size;
  208. int ret;
  209. SeqVideoContext *seq = avctx->priv_data;
  210. if ((ret = ff_reget_buffer(avctx, seq->frame)) < 0) {
  211. av_log(seq->avctx, AV_LOG_ERROR, "tiertexseqvideo: reget_buffer() failed\n");
  212. return ret;
  213. }
  214. if (seqvideo_decode(seq, buf, buf_size))
  215. return AVERROR_INVALIDDATA;
  216. if ((ret = av_frame_ref(data, seq->frame)) < 0)
  217. return ret;
  218. *got_frame = 1;
  219. return buf_size;
  220. }
  221. static av_cold int seqvideo_decode_end(AVCodecContext *avctx)
  222. {
  223. SeqVideoContext *seq = avctx->priv_data;
  224. av_frame_free(&seq->frame);
  225. return 0;
  226. }
  227. AVCodec ff_tiertexseqvideo_decoder = {
  228. .name = "tiertexseqvideo",
  229. .long_name = NULL_IF_CONFIG_SMALL("Tiertex Limited SEQ video"),
  230. .type = AVMEDIA_TYPE_VIDEO,
  231. .id = AV_CODEC_ID_TIERTEXSEQVIDEO,
  232. .priv_data_size = sizeof(SeqVideoContext),
  233. .init = seqvideo_decode_init,
  234. .close = seqvideo_decode_end,
  235. .decode = seqvideo_decode_frame,
  236. .capabilities = AV_CODEC_CAP_DR1,
  237. };