FFmpeg  2.1.1
bethsoftvideo.c
Go to the documentation of this file.
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 /**
23  * @file
24  * @brief Bethesda Softworks VID Video Decoder
25  * @author Nicholas Tung [ntung (at. ntung com] (2007-03)
26  * @see http://wiki.multimedia.cx/index.php?title=Bethsoft_VID
27  * @see http://www.svatopluk.com/andux/docs/dfvid.html
28  */
29 
30 #include "libavutil/common.h"
31 #include "avcodec.h"
32 #include "bethsoftvideo.h"
33 #include "bytestream.h"
34 #include "internal.h"
35 
36 typedef struct BethsoftvidContext {
40 
42 {
43  BethsoftvidContext *vid = avctx->priv_data;
45  avctx->pix_fmt = AV_PIX_FMT_PAL8;
46  return 0;
47 }
48 
50 {
51  uint32_t *palette = (uint32_t *)ctx->frame.data[1];
52  int a;
53 
54  if (bytestream2_get_bytes_left(&ctx->g) < 256*3)
55  return AVERROR_INVALIDDATA;
56 
57  for(a = 0; a < 256; a++){
58  palette[a] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->g) * 4;
59  palette[a] |= palette[a] >> 6 & 0x30303;
60  }
61  ctx->frame.palette_has_changed = 1;
62  return 0;
63 }
64 
66  void *data, int *got_frame,
67  AVPacket *avpkt)
68 {
69  BethsoftvidContext * vid = avctx->priv_data;
70  char block_type;
71  uint8_t * dst;
72  uint8_t * frame_end;
73  int remaining = avctx->width; // number of bytes remaining on a line
74  int wrap_to_next_line;
75  int code, ret;
76  int yoffset;
77 
78  if ((ret = ff_reget_buffer(avctx, &vid->frame)) < 0)
79  return ret;
80  wrap_to_next_line = vid->frame.linesize[0] - avctx->width;
81 
82  if (avpkt->side_data_elems > 0 &&
83  avpkt->side_data[0].type == AV_PKT_DATA_PALETTE) {
84  bytestream2_init(&vid->g, avpkt->side_data[0].data,
85  avpkt->side_data[0].size);
86  if ((ret = set_palette(vid)) < 0)
87  return ret;
88  }
89 
90  bytestream2_init(&vid->g, avpkt->data, avpkt->size);
91  dst = vid->frame.data[0];
92  frame_end = vid->frame.data[0] + vid->frame.linesize[0] * avctx->height;
93 
94  switch(block_type = bytestream2_get_byte(&vid->g)){
95  case PALETTE_BLOCK: {
96  *got_frame = 0;
97  if ((ret = set_palette(vid)) < 0) {
98  av_log(avctx, AV_LOG_ERROR, "error reading palette\n");
99  return ret;
100  }
101  return bytestream2_tell(&vid->g);
102  }
103  case VIDEO_YOFF_P_FRAME:
104  yoffset = bytestream2_get_le16(&vid->g);
105  if(yoffset >= avctx->height)
106  return AVERROR_INVALIDDATA;
107  dst += vid->frame.linesize[0] * yoffset;
108  }
109 
110  // main code
111  while((code = bytestream2_get_byte(&vid->g))){
112  int length = code & 0x7f;
113 
114  // copy any bytes starting at the current position, and ending at the frame width
115  while(length > remaining){
116  if(code < 0x80)
117  bytestream2_get_buffer(&vid->g, dst, remaining);
118  else if(block_type == VIDEO_I_FRAME)
119  memset(dst, bytestream2_peek_byte(&vid->g), remaining);
120  length -= remaining; // decrement the number of bytes to be copied
121  dst += remaining + wrap_to_next_line; // skip over extra bytes at end of frame
122  remaining = avctx->width;
123  if(dst == frame_end)
124  goto end;
125  }
126 
127  // copy any remaining bytes after / if line overflows
128  if(code < 0x80)
129  bytestream2_get_buffer(&vid->g, dst, length);
130  else if(block_type == VIDEO_I_FRAME)
131  memset(dst, bytestream2_get_byte(&vid->g), length);
132  remaining -= length;
133  dst += length;
134  }
135  end:
136 
137  if ((ret = av_frame_ref(data, &vid->frame)) < 0)
138  return ret;
139 
140  *got_frame = 1;
141 
142  return avpkt->size;
143 }
144 
146 {
147  BethsoftvidContext * vid = avctx->priv_data;
148  av_frame_unref(&vid->frame);
149  return 0;
150 }
151 
153  .name = "bethsoftvid",
154  .long_name = NULL_IF_CONFIG_SMALL("Bethesda VID video"),
155  .type = AVMEDIA_TYPE_VIDEO,
157  .priv_data_size = sizeof(BethsoftvidContext),
161  .capabilities = CODEC_CAP_DR1,
162 };
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1064
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:131
AVCodec.
Definition: avcodec.h:2922
#define av_cold
Definition: avcodec.h:653
AVCodec ff_bethsoftvid_decoder
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
Definition: avfilter.c:965
uint8_t
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
const char * name
Name of the codec implementation.
Definition: avcodec.h:2929
int av_frame_ref(AVFrame *dst, AVFrame *src)
Setup a new reference to the data described by a given frame.
Definition: frame.c:247
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:742
const char data[16]
Definition: mxf.c:68
struct AVPacket::@25 * side_data
Additional packet data that can be provided by the container.
#define U(x)
Definition: vp56_arith.h:37
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:261
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:152
int side_data_elems
Definition: avcodec.h:1079
static int bethsoftvid_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: bethsoftvideo.c:65
Libavcodec external API header.
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: utils.c:986
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:351
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
static int set_palette(BethsoftvidContext *ctx)
Definition: bethsoftvideo.c:49
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:186
static av_cold int bethsoftvid_decode_end(AVCodecContext *avctx)
static av_cold int bethsoftvid_decode_init(AVCodecContext *avctx)
Definition: bethsoftvideo.c:41
main external API structure.
Definition: avcodec.h:1146
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:538
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:1046
uint8_t * data
Definition: avcodec.h:1063
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:303
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
void * priv_data
Definition: avcodec.h:1182
int palette
Definition: v4l.c:60
common internal api header.
#define AVERROR_INVALIDDATA
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
enum AVPacketSideDataType type
Definition: avcodec.h:1077
const char int length
Definition: avisynth_c.h:668
8 bit with PIX_FMT_RGB32 palette
Definition: avcodec.h:4545
This structure stores compressed data.
Definition: avcodec.h:1040
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
GetByteContext g
Definition: bethsoftvideo.c:38