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.

272 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 "get_bits.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. GetBitContext gb;
  39. int code_table[64];
  40. /* get the rle codes */
  41. init_get_bits(&gb, src, (src_end - src) * 8);
  42. for (i = 0, sz = 0; i < 64 && sz < dst_size; i++) {
  43. if (get_bits_left(&gb) < 4)
  44. return NULL;
  45. code_table[i] = get_sbits(&gb, 4);
  46. sz += FFABS(code_table[i]);
  47. }
  48. src += (get_bits_count(&gb) + 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. GetBitContext gb;
  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. init_get_bits(&gb, src, bits * 8 * 8); src += bits * 8;
  107. for (b = 0; b < 8; b++) {
  108. for (i = 0; i < 8; i++)
  109. dst[i] = color_table[get_bits(&gb, bits)];
  110. dst += seq->frame->linesize[0];
  111. }
  112. }
  113. return src;
  114. }
  115. static const unsigned char *seq_decode_op2(SeqVideoContext *seq,
  116. const unsigned char *src,
  117. const unsigned char *src_end,
  118. unsigned char *dst)
  119. {
  120. int i;
  121. if (src_end - src < 8 * 8)
  122. return NULL;
  123. for (i = 0; i < 8; i++) {
  124. memcpy(dst, src, 8);
  125. src += 8;
  126. dst += seq->frame->linesize[0];
  127. }
  128. return src;
  129. }
  130. static const unsigned char *seq_decode_op3(SeqVideoContext *seq,
  131. const unsigned char *src,
  132. const unsigned char *src_end,
  133. unsigned char *dst)
  134. {
  135. int pos, offset;
  136. do {
  137. if (src_end - src < 2)
  138. return NULL;
  139. pos = *src++;
  140. offset = ((pos >> 3) & 7) * seq->frame->linesize[0] + (pos & 7);
  141. dst[offset] = *src++;
  142. } while (!(pos & 0x80));
  143. return src;
  144. }
  145. static int seqvideo_decode(SeqVideoContext *seq, const unsigned char *data, int data_size)
  146. {
  147. const unsigned char *data_end = data + data_size;
  148. GetBitContext gb;
  149. int flags, i, j, x, y, op;
  150. unsigned char c[3];
  151. unsigned char *dst;
  152. uint32_t *palette;
  153. flags = *data++;
  154. if (flags & 1) {
  155. palette = (uint32_t *)seq->frame->data[1];
  156. if (data_end - data < 256 * 3)
  157. return AVERROR_INVALIDDATA;
  158. for (i = 0; i < 256; i++) {
  159. for (j = 0; j < 3; j++, data++)
  160. c[j] = (*data << 2) | (*data >> 4);
  161. palette[i] = AV_RB24(c);
  162. }
  163. seq->frame->palette_has_changed = 1;
  164. }
  165. if (flags & 2) {
  166. if (data_end - data < 128)
  167. return AVERROR_INVALIDDATA;
  168. init_get_bits(&gb, data, 128 * 8); data += 128;
  169. for (y = 0; y < 128; y += 8)
  170. for (x = 0; x < 256; x += 8) {
  171. dst = &seq->frame->data[0][y * seq->frame->linesize[0] + x];
  172. op = get_bits(&gb, 2);
  173. switch (op) {
  174. case 1:
  175. data = seq_decode_op1(seq, data, data_end, dst);
  176. break;
  177. case 2:
  178. data = seq_decode_op2(seq, data, data_end, dst);
  179. break;
  180. case 3:
  181. data = seq_decode_op3(seq, data, data_end, dst);
  182. break;
  183. }
  184. if (!data)
  185. return AVERROR_INVALIDDATA;
  186. }
  187. }
  188. return 0;
  189. }
  190. static av_cold int seqvideo_decode_init(AVCodecContext *avctx)
  191. {
  192. SeqVideoContext *seq = avctx->priv_data;
  193. seq->avctx = avctx;
  194. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  195. seq->frame = av_frame_alloc();
  196. if (!seq->frame)
  197. return AVERROR(ENOMEM);
  198. return 0;
  199. }
  200. static int seqvideo_decode_frame(AVCodecContext *avctx,
  201. void *data, int *got_frame,
  202. AVPacket *avpkt)
  203. {
  204. const uint8_t *buf = avpkt->data;
  205. int buf_size = avpkt->size;
  206. int ret;
  207. SeqVideoContext *seq = avctx->priv_data;
  208. if ((ret = ff_reget_buffer(avctx, seq->frame)) < 0) {
  209. av_log(seq->avctx, AV_LOG_ERROR, "tiertexseqvideo: reget_buffer() failed\n");
  210. return ret;
  211. }
  212. if (seqvideo_decode(seq, buf, buf_size))
  213. return AVERROR_INVALIDDATA;
  214. if ((ret = av_frame_ref(data, seq->frame)) < 0)
  215. return ret;
  216. *got_frame = 1;
  217. return buf_size;
  218. }
  219. static av_cold int seqvideo_decode_end(AVCodecContext *avctx)
  220. {
  221. SeqVideoContext *seq = avctx->priv_data;
  222. av_frame_free(&seq->frame);
  223. return 0;
  224. }
  225. AVCodec ff_tiertexseqvideo_decoder = {
  226. .name = "tiertexseqvideo",
  227. .long_name = NULL_IF_CONFIG_SMALL("Tiertex Limited SEQ video"),
  228. .type = AVMEDIA_TYPE_VIDEO,
  229. .id = AV_CODEC_ID_TIERTEXSEQVIDEO,
  230. .priv_data_size = sizeof(SeqVideoContext),
  231. .init = seqvideo_decode_init,
  232. .close = seqvideo_decode_end,
  233. .decode = seqvideo_decode_frame,
  234. .capabilities = AV_CODEC_CAP_DR1,
  235. };