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.

234 lines
6.3KB

  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 libavcodec/tiertexseqv.c
  23. * Tiertex Limited SEQ video decoder
  24. */
  25. #include "avcodec.h"
  26. #define ALT_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, unsigned char *dst, int dst_size)
  33. {
  34. int i, len, sz;
  35. GetBitContext gb;
  36. int code_table[64];
  37. /* get the rle codes (at most 64 bytes) */
  38. init_get_bits(&gb, src, 64 * 8);
  39. for (i = 0, sz = 0; i < 64 && sz < dst_size; i++) {
  40. code_table[i] = get_sbits(&gb, 4);
  41. sz += FFABS(code_table[i]);
  42. }
  43. src += (get_bits_count(&gb) + 7) / 8;
  44. /* do the rle unpacking */
  45. for (i = 0; i < 64 && dst_size > 0; i++) {
  46. len = code_table[i];
  47. if (len < 0) {
  48. len = -len;
  49. memset(dst, *src++, FFMIN(len, dst_size));
  50. } else {
  51. memcpy(dst, src, FFMIN(len, dst_size));
  52. src += len;
  53. }
  54. dst += len;
  55. dst_size -= len;
  56. }
  57. return src;
  58. }
  59. static const unsigned char *seq_decode_op1(SeqVideoContext *seq, const unsigned char *src, unsigned char *dst)
  60. {
  61. const unsigned char *color_table;
  62. int b, i, len, bits;
  63. GetBitContext gb;
  64. unsigned char block[8 * 8];
  65. len = *src++;
  66. if (len & 0x80) {
  67. switch (len & 3) {
  68. case 1:
  69. src = seq_unpack_rle_block(src, block, sizeof(block));
  70. for (b = 0; b < 8; b++) {
  71. memcpy(dst, &block[b * 8], 8);
  72. dst += seq->frame.linesize[0];
  73. }
  74. break;
  75. case 2:
  76. src = seq_unpack_rle_block(src, block, sizeof(block));
  77. for (i = 0; i < 8; i++) {
  78. for (b = 0; b < 8; b++)
  79. dst[b * seq->frame.linesize[0]] = block[i * 8 + b];
  80. ++dst;
  81. }
  82. break;
  83. }
  84. } else {
  85. color_table = src;
  86. src += len;
  87. bits = ff_log2_tab[len - 1] + 1;
  88. init_get_bits(&gb, src, bits * 8 * 8); src += bits * 8;
  89. for (b = 0; b < 8; b++) {
  90. for (i = 0; i < 8; i++)
  91. dst[i] = color_table[get_bits(&gb, bits)];
  92. dst += seq->frame.linesize[0];
  93. }
  94. }
  95. return src;
  96. }
  97. static const unsigned char *seq_decode_op2(SeqVideoContext *seq, const unsigned char *src, unsigned char *dst)
  98. {
  99. int i;
  100. for (i = 0; i < 8; i++) {
  101. memcpy(dst, src, 8);
  102. src += 8;
  103. dst += seq->frame.linesize[0];
  104. }
  105. return src;
  106. }
  107. static const unsigned char *seq_decode_op3(SeqVideoContext *seq, const unsigned char *src, unsigned char *dst)
  108. {
  109. int pos, offset;
  110. do {
  111. pos = *src++;
  112. offset = ((pos >> 3) & 7) * seq->frame.linesize[0] + (pos & 7);
  113. dst[offset] = *src++;
  114. } while (!(pos & 0x80));
  115. return src;
  116. }
  117. static void seqvideo_decode(SeqVideoContext *seq, const unsigned char *data, int data_size)
  118. {
  119. GetBitContext gb;
  120. int flags, i, j, x, y, op;
  121. unsigned char c[3];
  122. unsigned char *dst;
  123. uint32_t *palette;
  124. flags = *data++;
  125. if (flags & 1) {
  126. palette = (uint32_t *)seq->frame.data[1];
  127. for (i = 0; i < 256; i++) {
  128. for (j = 0; j < 3; j++, data++)
  129. c[j] = (*data << 2) | (*data >> 4);
  130. palette[i] = AV_RB24(c);
  131. }
  132. seq->frame.palette_has_changed = 1;
  133. }
  134. if (flags & 2) {
  135. init_get_bits(&gb, data, 128 * 8); data += 128;
  136. for (y = 0; y < 128; y += 8)
  137. for (x = 0; x < 256; x += 8) {
  138. dst = &seq->frame.data[0][y * seq->frame.linesize[0] + x];
  139. op = get_bits(&gb, 2);
  140. switch (op) {
  141. case 1:
  142. data = seq_decode_op1(seq, data, dst);
  143. break;
  144. case 2:
  145. data = seq_decode_op2(seq, data, dst);
  146. break;
  147. case 3:
  148. data = seq_decode_op3(seq, data, dst);
  149. break;
  150. }
  151. }
  152. }
  153. }
  154. static av_cold int seqvideo_decode_init(AVCodecContext *avctx)
  155. {
  156. SeqVideoContext *seq = avctx->priv_data;
  157. seq->avctx = avctx;
  158. avctx->pix_fmt = PIX_FMT_PAL8;
  159. seq->frame.data[0] = NULL;
  160. return 0;
  161. }
  162. static int seqvideo_decode_frame(AVCodecContext *avctx,
  163. void *data, int *data_size,
  164. AVPacket *avpkt)
  165. {
  166. const uint8_t *buf = avpkt->data;
  167. int buf_size = avpkt->size;
  168. SeqVideoContext *seq = avctx->priv_data;
  169. seq->frame.reference = 1;
  170. seq->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  171. if (avctx->reget_buffer(avctx, &seq->frame)) {
  172. av_log(seq->avctx, AV_LOG_ERROR, "tiertexseqvideo: reget_buffer() failed\n");
  173. return -1;
  174. }
  175. seqvideo_decode(seq, buf, buf_size);
  176. *data_size = sizeof(AVFrame);
  177. *(AVFrame *)data = seq->frame;
  178. return buf_size;
  179. }
  180. static av_cold int seqvideo_decode_end(AVCodecContext *avctx)
  181. {
  182. SeqVideoContext *seq = avctx->priv_data;
  183. if (seq->frame.data[0])
  184. avctx->release_buffer(avctx, &seq->frame);
  185. return 0;
  186. }
  187. AVCodec tiertexseqvideo_decoder = {
  188. "tiertexseqvideo",
  189. CODEC_TYPE_VIDEO,
  190. CODEC_ID_TIERTEXSEQVIDEO,
  191. sizeof(SeqVideoContext),
  192. seqvideo_decode_init,
  193. NULL,
  194. seqvideo_decode_end,
  195. seqvideo_decode_frame,
  196. CODEC_CAP_DR1,
  197. .long_name = NULL_IF_CONFIG_SMALL("Tiertex Limited SEQ video"),
  198. };