bethsoftvideo.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Bethesda VID video decoder
  3. * Copyright (C) 2007 Nicholas Tung
  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/bethsoftvideo.c
  23. * @brief Bethesda Softworks VID Video Decoder
  24. * @author Nicholas Tung [ntung (at. ntung com] (2007-03)
  25. * @sa http://wiki.multimedia.cx/index.php?title=Bethsoft_VID
  26. * @sa http://www.svatopluk.com/andux/docs/dfvid.html
  27. */
  28. #include "libavutil/common.h"
  29. #include "dsputil.h"
  30. #include "bethsoftvideo.h"
  31. #include "bytestream.h"
  32. typedef struct BethsoftvidContext {
  33. AVFrame frame;
  34. } BethsoftvidContext;
  35. static av_cold int bethsoftvid_decode_init(AVCodecContext *avctx)
  36. {
  37. BethsoftvidContext *vid = avctx->priv_data;
  38. vid->frame.reference = 1;
  39. vid->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
  40. FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  41. avctx->pix_fmt = PIX_FMT_PAL8;
  42. return 0;
  43. }
  44. static void set_palette(AVFrame * frame, const uint8_t * palette_buffer)
  45. {
  46. uint32_t * palette = (uint32_t *)frame->data[1];
  47. int a;
  48. for(a = 0; a < 256; a++){
  49. palette[a] = AV_RB24(&palette_buffer[a * 3]) * 4;
  50. }
  51. frame->palette_has_changed = 1;
  52. }
  53. static int bethsoftvid_decode_frame(AVCodecContext *avctx,
  54. void *data, int *data_size,
  55. const uint8_t *buf, int buf_size)
  56. {
  57. BethsoftvidContext * vid = avctx->priv_data;
  58. char block_type;
  59. uint8_t * dst;
  60. uint8_t * frame_end;
  61. int remaining = avctx->width; // number of bytes remaining on a line
  62. const int wrap_to_next_line = vid->frame.linesize[0] - avctx->width;
  63. int code;
  64. int yoffset;
  65. if (avctx->reget_buffer(avctx, &vid->frame)) {
  66. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  67. return -1;
  68. }
  69. dst = vid->frame.data[0];
  70. frame_end = vid->frame.data[0] + vid->frame.linesize[0] * avctx->height;
  71. switch(block_type = *buf++){
  72. case PALETTE_BLOCK:
  73. set_palette(&vid->frame, buf);
  74. return 0;
  75. case VIDEO_YOFF_P_FRAME:
  76. yoffset = bytestream_get_le16(&buf);
  77. if(yoffset >= avctx->height)
  78. return -1;
  79. dst += vid->frame.linesize[0] * yoffset;
  80. }
  81. // main code
  82. while((code = *buf++)){
  83. int length = code & 0x7f;
  84. // copy any bytes starting at the current position, and ending at the frame width
  85. while(length > remaining){
  86. if(code < 0x80)
  87. bytestream_get_buffer(&buf, dst, remaining);
  88. else if(block_type == VIDEO_I_FRAME)
  89. memset(dst, buf[0], remaining);
  90. length -= remaining; // decrement the number of bytes to be copied
  91. dst += remaining + wrap_to_next_line; // skip over extra bytes at end of frame
  92. remaining = avctx->width;
  93. if(dst == frame_end)
  94. goto end;
  95. }
  96. // copy any remaining bytes after / if line overflows
  97. if(code < 0x80)
  98. bytestream_get_buffer(&buf, dst, length);
  99. else if(block_type == VIDEO_I_FRAME)
  100. memset(dst, *buf++, length);
  101. remaining -= length;
  102. dst += length;
  103. }
  104. end:
  105. *data_size = sizeof(AVFrame);
  106. *(AVFrame*)data = vid->frame;
  107. return buf_size;
  108. }
  109. static av_cold int bethsoftvid_decode_end(AVCodecContext *avctx)
  110. {
  111. BethsoftvidContext * vid = avctx->priv_data;
  112. if(vid->frame.data[0])
  113. avctx->release_buffer(avctx, &vid->frame);
  114. return 0;
  115. }
  116. AVCodec bethsoftvid_decoder = {
  117. .name = "bethsoftvid",
  118. .type = CODEC_TYPE_VIDEO,
  119. .id = CODEC_ID_BETHSOFTVID,
  120. .priv_data_size = sizeof(BethsoftvidContext),
  121. .init = bethsoftvid_decode_init,
  122. .close = bethsoftvid_decode_end,
  123. .decode = bethsoftvid_decode_frame,
  124. .long_name = NULL_IF_CONFIG_SMALL("Bethesda VID video"),
  125. };