bfi.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Brute Force & Ignorance (BFI) video decoder
  3. * Copyright (c) 2008 Sisir Koppaka
  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/bfi.c
  23. * @brief Brute Force & Ignorance (.bfi) video decoder
  24. * @author Sisir Koppaka ( sisir.koppaka at gmail dot com )
  25. * @sa http://wiki.multimedia.cx/index.php?title=BFI
  26. */
  27. #include "libavutil/common.h"
  28. #include "avcodec.h"
  29. #include "bytestream.h"
  30. typedef struct BFIContext {
  31. AVCodecContext *avctx;
  32. AVFrame frame;
  33. uint8_t *dst;
  34. } BFIContext;
  35. static av_cold int bfi_decode_init(AVCodecContext * avctx)
  36. {
  37. BFIContext *bfi = avctx->priv_data;
  38. avctx->pix_fmt = PIX_FMT_PAL8;
  39. bfi->dst = av_mallocz(avctx->width * avctx->height);
  40. return 0;
  41. }
  42. static int bfi_decode_frame(AVCodecContext * avctx, void *data,
  43. int *data_size, const uint8_t * buf,
  44. int buf_size)
  45. {
  46. BFIContext *bfi = avctx->priv_data;
  47. uint8_t *dst = bfi->dst;
  48. uint8_t *src, *dst_offset, colour1, colour2;
  49. uint8_t *frame_end = bfi->dst + avctx->width * avctx->height;
  50. uint32_t *pal;
  51. int i, j, height = avctx->height;
  52. if (bfi->frame.data[0])
  53. avctx->release_buffer(avctx, &bfi->frame);
  54. bfi->frame.reference = 1;
  55. if (avctx->get_buffer(avctx, &bfi->frame) < 0) {
  56. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  57. return -1;
  58. }
  59. /* Set frame parameters and palette, if necessary */
  60. if (!avctx->frame_number) {
  61. bfi->frame.pict_type = FF_I_TYPE;
  62. bfi->frame.key_frame = 1;
  63. /* Setting the palette */
  64. if(avctx->extradata_size>768) {
  65. av_log(NULL, AV_LOG_ERROR, "Palette is too large.\n");
  66. return -1;
  67. }
  68. pal = (uint32_t *) bfi->frame.data[1];
  69. for (i = 0; i < avctx->extradata_size / 3; i++) {
  70. int shift = 16;
  71. *pal = 0;
  72. for (j = 0; j < 3; j++, shift -= 8)
  73. *pal +=
  74. ((avctx->extradata[i * 3 + j] << 2) |
  75. (avctx->extradata[i * 3 + j] >> 4)) << shift;
  76. pal++;
  77. }
  78. bfi->frame.palette_has_changed = 1;
  79. } else {
  80. bfi->frame.pict_type = FF_P_TYPE;
  81. bfi->frame.key_frame = 0;
  82. }
  83. buf += 4; //Unpacked size, not required.
  84. while (dst != frame_end) {
  85. static const uint8_t lentab[4]={0,2,0,1};
  86. unsigned int byte = *buf++, av_uninit(offset);
  87. unsigned int code = byte >> 6;
  88. unsigned int length = byte & ~0xC0;
  89. /* Get length and offset(if required) */
  90. if (length == 0) {
  91. if (code == 1) {
  92. length = bytestream_get_byte(&buf);
  93. offset = bytestream_get_le16(&buf);
  94. } else {
  95. length = bytestream_get_le16(&buf);
  96. if (code == 2 && length == 0)
  97. break;
  98. }
  99. } else {
  100. if (code == 1)
  101. offset = bytestream_get_byte(&buf);
  102. }
  103. /* Do boundary check */
  104. if (dst + (length<<lentab[code]) > frame_end)
  105. break;
  106. switch (code) {
  107. case 0: //Normal Chain
  108. bytestream_get_buffer(&buf, dst, length);
  109. dst += length;
  110. break;
  111. case 1: //Back Chain
  112. dst_offset = dst - offset;
  113. length *= 4; //Convert dwords to bytes.
  114. if (dst_offset < bfi->dst)
  115. break;
  116. while (length--)
  117. *dst++ = *dst_offset++;
  118. break;
  119. case 2: //Skip Chain
  120. dst += length;
  121. break;
  122. case 3: //Fill Chain
  123. colour1 = bytestream_get_byte(&buf);
  124. colour2 = bytestream_get_byte(&buf);
  125. while (length--) {
  126. *dst++ = colour1;
  127. *dst++ = colour2;
  128. }
  129. break;
  130. }
  131. }
  132. src = bfi->dst;
  133. dst = bfi->frame.data[0];
  134. while (height--) {
  135. memcpy(dst, src, avctx->width);
  136. src += avctx->width;
  137. dst += bfi->frame.linesize[0];
  138. }
  139. *data_size = sizeof(AVFrame);
  140. *(AVFrame *) data = bfi->frame;
  141. return buf_size;
  142. }
  143. static av_cold int bfi_decode_close(AVCodecContext * avctx)
  144. {
  145. BFIContext *bfi = avctx->priv_data;
  146. if (bfi->frame.data[0])
  147. avctx->release_buffer(avctx, &bfi->frame);
  148. av_free(bfi->dst);
  149. return 0;
  150. }
  151. AVCodec bfi_decoder = {
  152. .name = "bfi",
  153. .type = CODEC_TYPE_VIDEO,
  154. .id = CODEC_ID_BFI,
  155. .priv_data_size = sizeof(BFIContext),
  156. .init = bfi_decode_init,
  157. .close = bfi_decode_close,
  158. .decode = bfi_decode_frame,
  159. .long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
  160. };