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