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.

189 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. * @see http://wiki.multimedia.cx/index.php?title=BFI
  26. */
  27. #include "libavutil/common.h"
  28. #include "avcodec.h"
  29. #include "bytestream.h"
  30. #include "internal.h"
  31. typedef struct BFIContext {
  32. AVCodecContext *avctx;
  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 = AV_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 *got_frame, AVPacket *avpkt)
  45. {
  46. AVFrame *frame = data;
  47. GetByteContext g;
  48. int buf_size = avpkt->size;
  49. BFIContext *bfi = avctx->priv_data;
  50. uint8_t *dst = bfi->dst;
  51. uint8_t *src, *dst_offset, colour1, colour2;
  52. uint8_t *frame_end = bfi->dst + avctx->width * avctx->height;
  53. uint32_t *pal;
  54. int i, j, ret, height = avctx->height;
  55. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  56. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  57. return ret;
  58. }
  59. bytestream2_init(&g, avpkt->data, buf_size);
  60. /* Set frame parameters and palette, if necessary */
  61. if (!avctx->frame_number) {
  62. frame->pict_type = AV_PICTURE_TYPE_I;
  63. 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 AVERROR_INVALIDDATA;
  68. }
  69. pal = (uint32_t *)frame->data[1];
  70. for (i = 0; i < avctx->extradata_size / 3; i++) {
  71. int shift = 16;
  72. *pal = 0xFFU << 24;
  73. for (j = 0; j < 3; j++, shift -= 8)
  74. *pal += ((avctx->extradata[i * 3 + j] << 2) |
  75. (avctx->extradata[i * 3 + j] >> 4)) << shift;
  76. pal++;
  77. }
  78. memcpy(bfi->pal, frame->data[1], sizeof(bfi->pal));
  79. frame->palette_has_changed = 1;
  80. } else {
  81. frame->pict_type = AV_PICTURE_TYPE_P;
  82. frame->key_frame = 0;
  83. frame->palette_has_changed = 0;
  84. memcpy(frame->data[1], bfi->pal, sizeof(bfi->pal));
  85. }
  86. bytestream2_skip(&g, 4); // Unpacked size, not required.
  87. while (dst != frame_end) {
  88. static const uint8_t lentab[4] = { 0, 2, 0, 1 };
  89. unsigned int byte = bytestream2_get_byte(&g), av_uninit(offset);
  90. unsigned int code = byte >> 6;
  91. unsigned int length = byte & ~0xC0;
  92. if (!bytestream2_get_bytes_left(&g)) {
  93. av_log(avctx, AV_LOG_ERROR,
  94. "Input resolution larger than actual frame.\n");
  95. return AVERROR_INVALIDDATA;
  96. }
  97. /* Get length and offset (if required) */
  98. if (length == 0) {
  99. if (code == 1) {
  100. length = bytestream2_get_byte(&g);
  101. offset = bytestream2_get_le16(&g);
  102. } else {
  103. length = bytestream2_get_le16(&g);
  104. if (code == 2 && length == 0)
  105. break;
  106. }
  107. } else {
  108. if (code == 1)
  109. offset = bytestream2_get_byte(&g);
  110. }
  111. /* Do boundary check */
  112. if (dst + (length << lentab[code]) > frame_end)
  113. break;
  114. switch (code) {
  115. case 0: // normal chain
  116. if (length >= bytestream2_get_bytes_left(&g)) {
  117. av_log(avctx, AV_LOG_ERROR, "Frame larger than buffer.\n");
  118. return AVERROR_INVALIDDATA;
  119. }
  120. bytestream2_get_buffer(&g, dst, length);
  121. dst += length;
  122. break;
  123. case 1: // back chain
  124. dst_offset = dst - offset;
  125. length *= 4; // Convert dwords to bytes.
  126. if (dst_offset < bfi->dst)
  127. break;
  128. while (length--)
  129. *dst++ = *dst_offset++;
  130. break;
  131. case 2: // skip chain
  132. dst += length;
  133. break;
  134. case 3: // fill chain
  135. colour1 = bytestream2_get_byte(&g);
  136. colour2 = bytestream2_get_byte(&g);
  137. while (length--) {
  138. *dst++ = colour1;
  139. *dst++ = colour2;
  140. }
  141. break;
  142. }
  143. }
  144. src = bfi->dst;
  145. dst = frame->data[0];
  146. while (height--) {
  147. memcpy(dst, src, avctx->width);
  148. src += avctx->width;
  149. dst += frame->linesize[0];
  150. }
  151. *got_frame = 1;
  152. return buf_size;
  153. }
  154. static av_cold int bfi_decode_close(AVCodecContext *avctx)
  155. {
  156. BFIContext *bfi = avctx->priv_data;
  157. av_free(bfi->dst);
  158. return 0;
  159. }
  160. AVCodec ff_bfi_decoder = {
  161. .name = "bfi",
  162. .type = AVMEDIA_TYPE_VIDEO,
  163. .id = AV_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. };