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.

271 lines
7.9KB

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