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.

191 lines
5.9KB

  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 = AV_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. GetByteContext g;
  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. bytestream2_init(&g, avpkt->data, buf_size);
  61. /* Set frame parameters and palette, if necessary */
  62. if (!avctx->frame_number) {
  63. bfi->frame.pict_type = AV_PICTURE_TYPE_I;
  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 += ((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. 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 -1;
  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 -1;
  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 = bfi->frame.data[0];
  144. while (height--) {
  145. memcpy(dst, src, avctx->width);
  146. src += avctx->width;
  147. dst += bfi->frame.linesize[0];
  148. }
  149. *data_size = sizeof(AVFrame);
  150. *(AVFrame *)data = bfi->frame;
  151. return buf_size;
  152. }
  153. static av_cold int bfi_decode_close(AVCodecContext *avctx)
  154. {
  155. BFIContext *bfi = avctx->priv_data;
  156. if (bfi->frame.data[0])
  157. avctx->release_buffer(avctx, &bfi->frame);
  158. av_free(bfi->dst);
  159. return 0;
  160. }
  161. AVCodec ff_bfi_decoder = {
  162. .name = "bfi",
  163. .type = AVMEDIA_TYPE_VIDEO,
  164. .id = AV_CODEC_ID_BFI,
  165. .priv_data_size = sizeof(BFIContext),
  166. .init = bfi_decode_init,
  167. .close = bfi_decode_close,
  168. .decode = bfi_decode_frame,
  169. .capabilities = CODEC_CAP_DR1,
  170. .long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
  171. };