tiertexseqv.c 6.2 KB

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