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.

185 lines
5.4KB

  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, AVPacket *avpkt)
  44. {
  45. const uint8_t *buf = avpkt->data;
  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 = FF_I_TYPE;
  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 = FF_P_TYPE;
  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. /* Get length and offset(if required) */
  91. if (length == 0) {
  92. if (code == 1) {
  93. length = bytestream_get_byte(&buf);
  94. offset = bytestream_get_le16(&buf);
  95. } else {
  96. length = bytestream_get_le16(&buf);
  97. if (code == 2 && length == 0)
  98. break;
  99. }
  100. } else {
  101. if (code == 1)
  102. offset = bytestream_get_byte(&buf);
  103. }
  104. /* Do boundary check */
  105. if (dst + (length<<lentab[code]) > frame_end)
  106. break;
  107. switch (code) {
  108. case 0: //Normal Chain
  109. bytestream_get_buffer(&buf, dst, length);
  110. dst += length;
  111. break;
  112. case 1: //Back Chain
  113. dst_offset = dst - offset;
  114. length *= 4; //Convert dwords to bytes.
  115. if (dst_offset < bfi->dst)
  116. break;
  117. while (length--)
  118. *dst++ = *dst_offset++;
  119. break;
  120. case 2: //Skip Chain
  121. dst += length;
  122. break;
  123. case 3: //Fill Chain
  124. colour1 = bytestream_get_byte(&buf);
  125. colour2 = bytestream_get_byte(&buf);
  126. while (length--) {
  127. *dst++ = colour1;
  128. *dst++ = colour2;
  129. }
  130. break;
  131. }
  132. }
  133. src = bfi->dst;
  134. dst = bfi->frame.data[0];
  135. while (height--) {
  136. memcpy(dst, src, avctx->width);
  137. src += avctx->width;
  138. dst += bfi->frame.linesize[0];
  139. }
  140. *data_size = sizeof(AVFrame);
  141. *(AVFrame *) data = bfi->frame;
  142. return buf_size;
  143. }
  144. static av_cold int bfi_decode_close(AVCodecContext * avctx)
  145. {
  146. BFIContext *bfi = avctx->priv_data;
  147. if (bfi->frame.data[0])
  148. avctx->release_buffer(avctx, &bfi->frame);
  149. av_free(bfi->dst);
  150. return 0;
  151. }
  152. AVCodec bfi_decoder = {
  153. .name = "bfi",
  154. .type = CODEC_TYPE_VIDEO,
  155. .id = CODEC_ID_BFI,
  156. .priv_data_size = sizeof(BFIContext),
  157. .init = bfi_decode_init,
  158. .close = bfi_decode_close,
  159. .decode = bfi_decode_frame,
  160. .capabilities = CODEC_CAP_DR1,
  161. .long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
  162. };