bfi.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Brute Force & Ignorance (BFI) video decoder
  3. * Copyright (c) 2008 Sisir Koppaka
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. * @brief Brute Force & Ignorance (.bfi) video decoder
  24. * @author Sisir Koppaka ( sisir.koppaka at gmail dot com )
  25. * @see 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, AVPacket *avpkt)
  44. {
  45. const uint8_t *buf = avpkt->data, *buf_end = avpkt->data + avpkt->size;
  46. int buf_size = avpkt->size;
  47. BFIContext *bfi = avctx->priv_data;
  48. uint8_t *dst = bfi->dst;
  49. uint8_t *src, *dst_offset, colour1, colour2;
  50. uint8_t *frame_end = bfi->dst + avctx->width * avctx->height;
  51. uint32_t *pal;
  52. int i, j, height = avctx->height;
  53. if (bfi->frame.data[0])
  54. avctx->release_buffer(avctx, &bfi->frame);
  55. bfi->frame.reference = 1;
  56. if (avctx->get_buffer(avctx, &bfi->frame) < 0) {
  57. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  58. return -1;
  59. }
  60. /* Set frame parameters and palette, if necessary */
  61. if (!avctx->frame_number) {
  62. bfi->frame.pict_type = AV_PICTURE_TYPE_I;
  63. bfi->frame.key_frame = 1;
  64. /* Setting the palette */
  65. if(avctx->extradata_size>768) {
  66. av_log(NULL, AV_LOG_ERROR, "Palette is too large.\n");
  67. return -1;
  68. }
  69. pal = (uint32_t *) bfi->frame.data[1];
  70. for (i = 0; i < avctx->extradata_size / 3; i++) {
  71. int shift = 16;
  72. *pal = 0;
  73. for (j = 0; j < 3; j++, shift -= 8)
  74. *pal +=
  75. ((avctx->extradata[i * 3 + j] << 2) |
  76. (avctx->extradata[i * 3 + j] >> 4)) << shift;
  77. pal++;
  78. }
  79. bfi->frame.palette_has_changed = 1;
  80. } else {
  81. bfi->frame.pict_type = AV_PICTURE_TYPE_P;
  82. bfi->frame.key_frame = 0;
  83. }
  84. buf += 4; //Unpacked size, not required.
  85. while (dst != frame_end) {
  86. static const uint8_t lentab[4]={0,2,0,1};
  87. unsigned int byte = *buf++, av_uninit(offset);
  88. unsigned int code = byte >> 6;
  89. unsigned int length = byte & ~0xC0;
  90. if (buf >= buf_end) {
  91. av_log(avctx, AV_LOG_ERROR, "Input resolution larger than actual frame.\n");
  92. return -1;
  93. }
  94. /* Get length and offset(if required) */
  95. if (length == 0) {
  96. if (code == 1) {
  97. length = bytestream_get_byte(&buf);
  98. offset = bytestream_get_le16(&buf);
  99. } else {
  100. length = bytestream_get_le16(&buf);
  101. if (code == 2 && length == 0)
  102. break;
  103. }
  104. } else {
  105. if (code == 1)
  106. offset = bytestream_get_byte(&buf);
  107. }
  108. /* Do boundary check */
  109. if (dst + (length<<lentab[code]) > frame_end)
  110. break;
  111. switch (code) {
  112. case 0: //Normal Chain
  113. if (length >= buf_end - buf) {
  114. av_log(avctx, AV_LOG_ERROR, "Frame larger than buffer.\n");
  115. return -1;
  116. }
  117. bytestream_get_buffer(&buf, dst, length);
  118. dst += length;
  119. break;
  120. case 1: //Back Chain
  121. dst_offset = dst - offset;
  122. length *= 4; //Convert dwords to bytes.
  123. if (dst_offset < bfi->dst)
  124. break;
  125. while (length--)
  126. *dst++ = *dst_offset++;
  127. break;
  128. case 2: //Skip Chain
  129. dst += length;
  130. break;
  131. case 3: //Fill Chain
  132. colour1 = bytestream_get_byte(&buf);
  133. colour2 = bytestream_get_byte(&buf);
  134. while (length--) {
  135. *dst++ = colour1;
  136. *dst++ = colour2;
  137. }
  138. break;
  139. }
  140. }
  141. src = bfi->dst;
  142. dst = bfi->frame.data[0];
  143. while (height--) {
  144. memcpy(dst, src, avctx->width);
  145. src += avctx->width;
  146. dst += bfi->frame.linesize[0];
  147. }
  148. *data_size = sizeof(AVFrame);
  149. *(AVFrame *) data = bfi->frame;
  150. return buf_size;
  151. }
  152. static av_cold int bfi_decode_close(AVCodecContext * avctx)
  153. {
  154. BFIContext *bfi = avctx->priv_data;
  155. if (bfi->frame.data[0])
  156. avctx->release_buffer(avctx, &bfi->frame);
  157. av_free(bfi->dst);
  158. return 0;
  159. }
  160. AVCodec ff_bfi_decoder = {
  161. .name = "bfi",
  162. .type = AVMEDIA_TYPE_VIDEO,
  163. .id = CODEC_ID_BFI,
  164. .priv_data_size = sizeof(BFIContext),
  165. .init = bfi_decode_init,
  166. .close = bfi_decode_close,
  167. .decode = bfi_decode_frame,
  168. .capabilities = CODEC_CAP_DR1,
  169. .long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
  170. };