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.

198 lines
5.9KB

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