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.

187 lines
5.8KB

  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. return ret;
  57. bytestream2_init(&g, avpkt->data, buf_size);
  58. /* Set frame parameters and palette, if necessary */
  59. if (!avctx->frame_number) {
  60. frame->pict_type = AV_PICTURE_TYPE_I;
  61. frame->key_frame = 1;
  62. /* Setting the palette */
  63. if (avctx->extradata_size > 768) {
  64. av_log(NULL, AV_LOG_ERROR, "Palette is too large.\n");
  65. return AVERROR_INVALIDDATA;
  66. }
  67. pal = (uint32_t *)frame->data[1];
  68. for (i = 0; i < avctx->extradata_size / 3; i++) {
  69. int shift = 16;
  70. *pal = 0xFFU << 24;
  71. for (j = 0; j < 3; j++, shift -= 8)
  72. *pal += ((avctx->extradata[i * 3 + j] << 2) |
  73. (avctx->extradata[i * 3 + j] >> 4)) << shift;
  74. pal++;
  75. }
  76. memcpy(bfi->pal, frame->data[1], sizeof(bfi->pal));
  77. frame->palette_has_changed = 1;
  78. } else {
  79. frame->pict_type = AV_PICTURE_TYPE_P;
  80. frame->key_frame = 0;
  81. frame->palette_has_changed = 0;
  82. memcpy(frame->data[1], bfi->pal, sizeof(bfi->pal));
  83. }
  84. bytestream2_skip(&g, 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 = bytestream2_get_byte(&g), av_uninit(offset);
  88. unsigned int code = byte >> 6;
  89. unsigned int length = byte & ~0xC0;
  90. if (!bytestream2_get_bytes_left(&g)) {
  91. av_log(avctx, AV_LOG_ERROR,
  92. "Input resolution larger than actual frame.\n");
  93. return AVERROR_INVALIDDATA;
  94. }
  95. /* Get length and offset (if required) */
  96. if (length == 0) {
  97. if (code == 1) {
  98. length = bytestream2_get_byte(&g);
  99. offset = bytestream2_get_le16(&g);
  100. } else {
  101. length = bytestream2_get_le16(&g);
  102. if (code == 2 && length == 0)
  103. break;
  104. }
  105. } else {
  106. if (code == 1)
  107. offset = bytestream2_get_byte(&g);
  108. }
  109. /* Do boundary check */
  110. if (dst + (length << lentab[code]) > frame_end)
  111. break;
  112. switch (code) {
  113. case 0: // normal chain
  114. if (length >= bytestream2_get_bytes_left(&g)) {
  115. av_log(avctx, AV_LOG_ERROR, "Frame larger than buffer.\n");
  116. return AVERROR_INVALIDDATA;
  117. }
  118. bytestream2_get_buffer(&g, dst, length);
  119. dst += length;
  120. break;
  121. case 1: // back chain
  122. dst_offset = dst - offset;
  123. length *= 4; // Convert dwords to bytes.
  124. if (dst_offset < bfi->dst)
  125. break;
  126. while (length--)
  127. *dst++ = *dst_offset++;
  128. break;
  129. case 2: // skip chain
  130. dst += length;
  131. break;
  132. case 3: // fill chain
  133. colour1 = bytestream2_get_byte(&g);
  134. colour2 = bytestream2_get_byte(&g);
  135. while (length--) {
  136. *dst++ = colour1;
  137. *dst++ = colour2;
  138. }
  139. break;
  140. }
  141. }
  142. src = bfi->dst;
  143. dst = frame->data[0];
  144. while (height--) {
  145. memcpy(dst, src, avctx->width);
  146. src += avctx->width;
  147. dst += frame->linesize[0];
  148. }
  149. *got_frame = 1;
  150. return buf_size;
  151. }
  152. static av_cold int bfi_decode_close(AVCodecContext *avctx)
  153. {
  154. BFIContext *bfi = avctx->priv_data;
  155. av_free(bfi->dst);
  156. return 0;
  157. }
  158. AVCodec ff_bfi_decoder = {
  159. .name = "bfi",
  160. .type = AVMEDIA_TYPE_VIDEO,
  161. .id = AV_CODEC_ID_BFI,
  162. .priv_data_size = sizeof(BFIContext),
  163. .init = bfi_decode_init,
  164. .close = bfi_decode_close,
  165. .decode = bfi_decode_frame,
  166. .capabilities = CODEC_CAP_DR1,
  167. .long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
  168. };