c93.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Interplay C93 video decoder
  3. * Copyright (c) 2007 Anssi Hannula <anssi.hannula@gmail.com>
  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. #include "avcodec.h"
  22. #include "bytestream.h"
  23. typedef struct {
  24. AVFrame pictures[2];
  25. int currentpic;
  26. } C93DecoderContext;
  27. typedef enum {
  28. C93_8X8_FROM_PREV = 0x02,
  29. C93_4X4_FROM_PREV = 0x06,
  30. C93_4X4_FROM_CURR = 0x07,
  31. C93_8X8_2COLOR = 0x08,
  32. C93_4X4_2COLOR = 0x0A,
  33. C93_4X4_4COLOR_GRP = 0x0B,
  34. C93_4X4_4COLOR = 0x0D,
  35. C93_NOOP = 0x0E,
  36. C93_8X8_INTRA = 0x0F,
  37. } C93BlockType;
  38. #define WIDTH 320
  39. #define HEIGHT 192
  40. #define C93_HAS_PALETTE 0x01
  41. #define C93_FIRST_FRAME 0x02
  42. static av_cold int decode_init(AVCodecContext *avctx)
  43. {
  44. avctx->pix_fmt = PIX_FMT_PAL8;
  45. return 0;
  46. }
  47. static av_cold int decode_end(AVCodecContext *avctx)
  48. {
  49. C93DecoderContext * const c93 = avctx->priv_data;
  50. if (c93->pictures[0].data[0])
  51. avctx->release_buffer(avctx, &c93->pictures[0]);
  52. if (c93->pictures[1].data[0])
  53. avctx->release_buffer(avctx, &c93->pictures[1]);
  54. return 0;
  55. }
  56. static inline int copy_block(AVCodecContext *avctx, uint8_t *to,
  57. uint8_t *from, int offset, int height, int stride)
  58. {
  59. int i;
  60. int width = height;
  61. int from_x = offset % WIDTH;
  62. int from_y = offset / WIDTH;
  63. int overflow = from_x + width - WIDTH;
  64. if (!from) {
  65. /* silently ignoring predictive blocks in first frame */
  66. return 0;
  67. }
  68. if (from_y + height > HEIGHT) {
  69. av_log(avctx, AV_LOG_ERROR, "invalid offset %d during C93 decoding\n",
  70. offset);
  71. return -1;
  72. }
  73. if (overflow > 0) {
  74. width -= overflow;
  75. for (i = 0; i < height; i++) {
  76. memcpy(&to[i*stride+width], &from[(from_y+i)*stride], overflow);
  77. }
  78. }
  79. for (i = 0; i < height; i++) {
  80. memcpy(&to[i*stride], &from[(from_y+i)*stride+from_x], width);
  81. }
  82. return 0;
  83. }
  84. static inline void draw_n_color(uint8_t *out, int stride, int width,
  85. int height, int bpp, uint8_t cols[4], uint8_t grps[4], uint32_t col)
  86. {
  87. int x, y;
  88. for (y = 0; y < height; y++) {
  89. if (grps)
  90. cols[0] = grps[3 * (y >> 1)];
  91. for (x = 0; x < width; x++) {
  92. if (grps)
  93. cols[1]= grps[(x >> 1) + 1];
  94. out[x + y*stride] = cols[col & ((1 << bpp) - 1)];
  95. col >>= bpp;
  96. }
  97. }
  98. }
  99. static int decode_frame(AVCodecContext *avctx, void *data,
  100. int *data_size, const uint8_t * buf, int buf_size)
  101. {
  102. C93DecoderContext * const c93 = avctx->priv_data;
  103. AVFrame * const newpic = &c93->pictures[c93->currentpic];
  104. AVFrame * const oldpic = &c93->pictures[c93->currentpic^1];
  105. AVFrame *picture = data;
  106. uint8_t *out;
  107. int stride, i, x, y, bt = 0;
  108. c93->currentpic ^= 1;
  109. newpic->reference = 1;
  110. newpic->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
  111. FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
  112. if (avctx->reget_buffer(avctx, newpic)) {
  113. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  114. return -1;
  115. }
  116. stride = newpic->linesize[0];
  117. if (buf[0] & C93_FIRST_FRAME) {
  118. newpic->pict_type = FF_I_TYPE;
  119. newpic->key_frame = 1;
  120. } else {
  121. newpic->pict_type = FF_P_TYPE;
  122. newpic->key_frame = 0;
  123. }
  124. if (*buf++ & C93_HAS_PALETTE) {
  125. uint32_t *palette = (uint32_t *) newpic->data[1];
  126. const uint8_t *palbuf = buf + buf_size - 768 - 1;
  127. for (i = 0; i < 256; i++) {
  128. palette[i] = bytestream_get_be24(&palbuf);
  129. }
  130. } else {
  131. if (oldpic->data[1])
  132. memcpy(newpic->data[1], oldpic->data[1], 256 * 4);
  133. }
  134. for (y = 0; y < HEIGHT; y += 8) {
  135. out = newpic->data[0] + y * stride;
  136. for (x = 0; x < WIDTH; x += 8) {
  137. uint8_t *copy_from = oldpic->data[0];
  138. unsigned int offset, j;
  139. uint8_t cols[4], grps[4];
  140. C93BlockType block_type;
  141. if (!bt)
  142. bt = *buf++;
  143. block_type= bt & 0x0F;
  144. switch (block_type) {
  145. case C93_8X8_FROM_PREV:
  146. offset = bytestream_get_le16(&buf);
  147. if (copy_block(avctx, out, copy_from, offset, 8, stride))
  148. return -1;
  149. break;
  150. case C93_4X4_FROM_CURR:
  151. copy_from = newpic->data[0];
  152. case C93_4X4_FROM_PREV:
  153. for (j = 0; j < 8; j += 4) {
  154. for (i = 0; i < 8; i += 4) {
  155. offset = bytestream_get_le16(&buf);
  156. if (copy_block(avctx, &out[j*stride+i],
  157. copy_from, offset, 4, stride))
  158. return -1;
  159. }
  160. }
  161. break;
  162. case C93_8X8_2COLOR:
  163. bytestream_get_buffer(&buf, cols, 2);
  164. for (i = 0; i < 8; i++) {
  165. draw_n_color(out + i*stride, stride, 8, 1, 1, cols,
  166. NULL, *buf++);
  167. }
  168. break;
  169. case C93_4X4_2COLOR:
  170. case C93_4X4_4COLOR:
  171. case C93_4X4_4COLOR_GRP:
  172. for (j = 0; j < 8; j += 4) {
  173. for (i = 0; i < 8; i += 4) {
  174. if (block_type == C93_4X4_2COLOR) {
  175. bytestream_get_buffer(&buf, cols, 2);
  176. draw_n_color(out + i + j*stride, stride, 4, 4,
  177. 1, cols, NULL, bytestream_get_le16(&buf));
  178. } else if (block_type == C93_4X4_4COLOR) {
  179. bytestream_get_buffer(&buf, cols, 4);
  180. draw_n_color(out + i + j*stride, stride, 4, 4,
  181. 2, cols, NULL, bytestream_get_le32(&buf));
  182. } else {
  183. bytestream_get_buffer(&buf, grps, 4);
  184. draw_n_color(out + i + j*stride, stride, 4, 4,
  185. 1, cols, grps, bytestream_get_le16(&buf));
  186. }
  187. }
  188. }
  189. break;
  190. case C93_NOOP:
  191. break;
  192. case C93_8X8_INTRA:
  193. for (j = 0; j < 8; j++)
  194. bytestream_get_buffer(&buf, out + j*stride, 8);
  195. break;
  196. default:
  197. av_log(avctx, AV_LOG_ERROR, "unexpected type %x at %dx%d\n",
  198. block_type, x, y);
  199. return -1;
  200. }
  201. bt >>= 4;
  202. out += 8;
  203. }
  204. }
  205. *picture = *newpic;
  206. *data_size = sizeof(AVFrame);
  207. return buf_size;
  208. }
  209. AVCodec c93_decoder = {
  210. "c93",
  211. CODEC_TYPE_VIDEO,
  212. CODEC_ID_C93,
  213. sizeof(C93DecoderContext),
  214. decode_init,
  215. NULL,
  216. decode_end,
  217. decode_frame,
  218. CODEC_CAP_DR1,
  219. .long_name = NULL_IF_CONFIG_SMALL("Interplay C93"),
  220. };