00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #include "libavutil/channel_layout.h"
00031 #include "libavutil/intreadwrite.h"
00032 #include "avformat.h"
00033 #include "internal.h"
00034 #include "libavcodec/bethsoftvideo.h"
00035
00036 #define BVID_PALETTE_SIZE 3 * 256
00037
00038 #define DEFAULT_SAMPLE_RATE 11111
00039
00040 typedef struct BVID_DemuxContext
00041 {
00042 int nframes;
00043 int sample_rate;
00044 int width;
00045 int height;
00049 int bethsoft_global_delay;
00050 int video_index;
00051 int audio_index;
00052 uint8_t *palette;
00053
00054 int is_finished;
00055
00056 } BVID_DemuxContext;
00057
00058 static int vid_probe(AVProbeData *p)
00059 {
00060
00061 if (AV_RL32(p->buf) != MKTAG('V', 'I', 'D', 0))
00062 return 0;
00063
00064 return AVPROBE_SCORE_MAX;
00065 }
00066
00067 static int vid_read_header(AVFormatContext *s)
00068 {
00069 BVID_DemuxContext *vid = s->priv_data;
00070 AVIOContext *pb = s->pb;
00071
00072
00073
00074
00075
00076 avio_skip(pb, 5);
00077 vid->nframes = avio_rl16(pb);
00078 vid->width = avio_rl16(pb);
00079 vid->height = avio_rl16(pb);
00080 vid->bethsoft_global_delay = avio_rl16(pb);
00081 avio_rl16(pb);
00082
00083
00084 vid->video_index = -1;
00085 vid->audio_index = -1;
00086 vid->sample_rate = DEFAULT_SAMPLE_RATE;
00087 s->ctx_flags |= AVFMTCTX_NOHEADER;
00088
00089 return 0;
00090 }
00091
00092 #define BUFFER_PADDING_SIZE 1000
00093 static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt,
00094 uint8_t block_type, AVFormatContext *s)
00095 {
00096 uint8_t * vidbuf_start = NULL;
00097 int vidbuf_nbytes = 0;
00098 int code;
00099 int bytes_copied = 0;
00100 int position, duration, npixels;
00101 unsigned int vidbuf_capacity;
00102 int ret = 0;
00103 AVStream *st;
00104
00105 if (vid->video_index < 0) {
00106 st = avformat_new_stream(s, NULL);
00107 if (!st)
00108 return AVERROR(ENOMEM);
00109 vid->video_index = st->index;
00110 if (vid->audio_index < 0) {
00111 av_log_ask_for_sample(s, "No audio packet before first video "
00112 "packet. Using default video time base.\n");
00113 }
00114 avpriv_set_pts_info(st, 64, 185, vid->sample_rate);
00115 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00116 st->codec->codec_id = AV_CODEC_ID_BETHSOFTVID;
00117 st->codec->width = vid->width;
00118 st->codec->height = vid->height;
00119 }
00120 st = s->streams[vid->video_index];
00121 npixels = st->codec->width * st->codec->height;
00122
00123 vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
00124 if(!vidbuf_start)
00125 return AVERROR(ENOMEM);
00126
00127
00128 position = avio_tell(pb) - 1;
00129
00130 vidbuf_start[vidbuf_nbytes++] = block_type;
00131
00132
00133 duration = vid->bethsoft_global_delay + avio_rl16(pb);
00134
00135
00136 if(block_type == VIDEO_YOFF_P_FRAME){
00137 if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], 2) != 2) {
00138 ret = AVERROR(EIO);
00139 goto fail;
00140 }
00141 vidbuf_nbytes += 2;
00142 }
00143
00144 do{
00145 vidbuf_start = av_fast_realloc(vidbuf_start, &vidbuf_capacity, vidbuf_nbytes + BUFFER_PADDING_SIZE);
00146 if(!vidbuf_start)
00147 return AVERROR(ENOMEM);
00148
00149 code = avio_r8(pb);
00150 vidbuf_start[vidbuf_nbytes++] = code;
00151
00152 if(code >= 0x80){
00153 if(block_type == VIDEO_I_FRAME)
00154 vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
00155 } else if(code){
00156 if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], code) != code) {
00157 ret = AVERROR(EIO);
00158 goto fail;
00159 }
00160 vidbuf_nbytes += code;
00161 }
00162 bytes_copied += code & 0x7F;
00163 if(bytes_copied == npixels){
00164
00165 if(avio_r8(pb))
00166 avio_seek(pb, -1, SEEK_CUR);
00167 break;
00168 }
00169 if (bytes_copied > npixels) {
00170 ret = AVERROR_INVALIDDATA;
00171 goto fail;
00172 }
00173 } while(code);
00174
00175
00176 if ((ret = av_new_packet(pkt, vidbuf_nbytes)) < 0)
00177 goto fail;
00178 memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
00179 av_free(vidbuf_start);
00180
00181 pkt->pos = position;
00182 pkt->stream_index = vid->video_index;
00183 pkt->duration = duration;
00184 if (block_type == VIDEO_I_FRAME)
00185 pkt->flags |= AV_PKT_FLAG_KEY;
00186
00187
00188 if (vid->palette) {
00189 uint8_t *pdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
00190 BVID_PALETTE_SIZE);
00191 if (pdata)
00192 memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
00193 av_freep(&vid->palette);
00194 }
00195
00196 vid->nframes--;
00197 return 0;
00198 fail:
00199 av_free(vidbuf_start);
00200 return ret;
00201 }
00202
00203 static int vid_read_packet(AVFormatContext *s,
00204 AVPacket *pkt)
00205 {
00206 BVID_DemuxContext *vid = s->priv_data;
00207 AVIOContext *pb = s->pb;
00208 unsigned char block_type;
00209 int audio_length;
00210 int ret_value;
00211
00212 if(vid->is_finished || url_feof(pb))
00213 return AVERROR_EOF;
00214
00215 block_type = avio_r8(pb);
00216 switch(block_type){
00217 case PALETTE_BLOCK:
00218 if (vid->palette) {
00219 av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
00220 av_freep(&vid->palette);
00221 }
00222 vid->palette = av_malloc(BVID_PALETTE_SIZE);
00223 if (!vid->palette)
00224 return AVERROR(ENOMEM);
00225 if (avio_read(pb, vid->palette, BVID_PALETTE_SIZE) != BVID_PALETTE_SIZE) {
00226 av_freep(&vid->palette);
00227 return AVERROR(EIO);
00228 }
00229 return vid_read_packet(s, pkt);
00230
00231 case FIRST_AUDIO_BLOCK:
00232 avio_rl16(pb);
00233
00234 vid->sample_rate = 1000000 / (256 - avio_r8(pb));
00235 case AUDIO_BLOCK:
00236 if (vid->audio_index < 0) {
00237 AVStream *st = avformat_new_stream(s, NULL);
00238 if (!st)
00239 return AVERROR(ENOMEM);
00240 vid->audio_index = st->index;
00241 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00242 st->codec->codec_id = AV_CODEC_ID_PCM_U8;
00243 st->codec->channels = 1;
00244 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
00245 st->codec->bits_per_coded_sample = 8;
00246 st->codec->sample_rate = vid->sample_rate;
00247 st->codec->bit_rate = 8 * st->codec->sample_rate;
00248 st->start_time = 0;
00249 avpriv_set_pts_info(st, 64, 1, vid->sample_rate);
00250 }
00251 audio_length = avio_rl16(pb);
00252 if ((ret_value = av_get_packet(pb, pkt, audio_length)) != audio_length) {
00253 if (ret_value < 0)
00254 return ret_value;
00255 av_log(s, AV_LOG_ERROR, "incomplete audio block\n");
00256 return AVERROR(EIO);
00257 }
00258 pkt->stream_index = vid->audio_index;
00259 pkt->duration = audio_length;
00260 pkt->flags |= AV_PKT_FLAG_KEY;
00261 return 0;
00262
00263 case VIDEO_P_FRAME:
00264 case VIDEO_YOFF_P_FRAME:
00265 case VIDEO_I_FRAME:
00266 return read_frame(vid, pb, pkt, block_type, s);
00267
00268 case EOF_BLOCK:
00269 if(vid->nframes != 0)
00270 av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
00271 vid->is_finished = 1;
00272 return AVERROR(EIO);
00273 default:
00274 av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
00275 block_type, block_type, block_type);
00276 return AVERROR_INVALIDDATA;
00277 }
00278 }
00279
00280 static int vid_read_close(AVFormatContext *s)
00281 {
00282 BVID_DemuxContext *vid = s->priv_data;
00283 av_freep(&vid->palette);
00284 return 0;
00285 }
00286
00287 AVInputFormat ff_bethsoftvid_demuxer = {
00288 .name = "bethsoftvid",
00289 .long_name = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID"),
00290 .priv_data_size = sizeof(BVID_DemuxContext),
00291 .read_probe = vid_probe,
00292 .read_header = vid_read_header,
00293 .read_packet = vid_read_packet,
00294 .read_close = vid_read_close,
00295 };