00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avformat.h"
00023 #include "riff.h"
00024
00025 #include <windows.h>
00026 #include <vfw.h>
00027
00028 typedef struct {
00029 PAVISTREAM handle;
00030 AVISTREAMINFO info;
00031 DWORD read;
00032 LONG chunck_size;
00033 LONG chunck_samples;
00034 } AVISynthStream;
00035
00036 typedef struct {
00037 PAVIFILE file;
00038 AVISynthStream *streams;
00039 int nb_streams;
00040 int next_stream;
00041 } AVISynthContext;
00042
00043 static int avisynth_read_header(AVFormatContext *s, AVFormatParameters *ap)
00044 {
00045 AVISynthContext *avs = s->priv_data;
00046 HRESULT res;
00047 AVIFILEINFO info;
00048 DWORD id;
00049 AVStream *st;
00050 AVISynthStream *stream;
00051
00052 AVIFileInit();
00053
00054 res = AVIFileOpen(&avs->file, s->filename, OF_READ|OF_SHARE_DENY_WRITE, NULL);
00055 if (res != S_OK)
00056 {
00057 av_log(s, AV_LOG_ERROR, "AVIFileOpen failed with error %ld", res);
00058 AVIFileExit();
00059 return -1;
00060 }
00061
00062 res = AVIFileInfo(avs->file, &info, sizeof(info));
00063 if (res != S_OK)
00064 {
00065 av_log(s, AV_LOG_ERROR, "AVIFileInfo failed with error %ld", res);
00066 AVIFileExit();
00067 return -1;
00068 }
00069
00070 avs->streams = av_mallocz(info.dwStreams * sizeof(AVISynthStream));
00071
00072 for (id=0; id<info.dwStreams; id++)
00073 {
00074 stream = &avs->streams[id];
00075 stream->read = 0;
00076 if (AVIFileGetStream(avs->file, &stream->handle, 0, id) == S_OK)
00077 {
00078 if (AVIStreamInfo(stream->handle, &stream->info, sizeof(stream->info)) == S_OK)
00079 {
00080 if (stream->info.fccType == streamtypeAUDIO)
00081 {
00082 WAVEFORMATEX wvfmt;
00083 LONG struct_size = sizeof(WAVEFORMATEX);
00084 if (AVIStreamReadFormat(stream->handle, 0, &wvfmt, &struct_size) != S_OK)
00085 continue;
00086
00087 st = av_new_stream(s, id);
00088 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00089
00090 st->codec->block_align = wvfmt.nBlockAlign;
00091 st->codec->channels = wvfmt.nChannels;
00092 st->codec->sample_rate = wvfmt.nSamplesPerSec;
00093 st->codec->bit_rate = wvfmt.nAvgBytesPerSec * 8;
00094 st->codec->bits_per_coded_sample = wvfmt.wBitsPerSample;
00095
00096 stream->chunck_samples = wvfmt.nSamplesPerSec * (uint64_t)info.dwScale / (uint64_t)info.dwRate;
00097 stream->chunck_size = stream->chunck_samples * wvfmt.nChannels * wvfmt.wBitsPerSample / 8;
00098
00099 st->codec->codec_tag = wvfmt.wFormatTag;
00100 st->codec->codec_id = ff_wav_codec_get_id(wvfmt.wFormatTag, st->codec->bits_per_coded_sample);
00101 }
00102 else if (stream->info.fccType == streamtypeVIDEO)
00103 {
00104 BITMAPINFO imgfmt;
00105 LONG struct_size = sizeof(BITMAPINFO);
00106
00107 stream->chunck_size = stream->info.dwSampleSize;
00108 stream->chunck_samples = 1;
00109
00110 if (AVIStreamReadFormat(stream->handle, 0, &imgfmt, &struct_size) != S_OK)
00111 continue;
00112
00113 st = av_new_stream(s, id);
00114 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00115 st->r_frame_rate.num = stream->info.dwRate;
00116 st->r_frame_rate.den = stream->info.dwScale;
00117
00118 st->codec->width = imgfmt.bmiHeader.biWidth;
00119 st->codec->height = imgfmt.bmiHeader.biHeight;
00120
00121 st->codec->bits_per_coded_sample = imgfmt.bmiHeader.biBitCount;
00122 st->codec->bit_rate = (uint64_t)stream->info.dwSampleSize * (uint64_t)stream->info.dwRate * 8 / (uint64_t)stream->info.dwScale;
00123 st->codec->codec_tag = imgfmt.bmiHeader.biCompression;
00124 st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, imgfmt.bmiHeader.biCompression);
00125 if (st->codec->codec_id == CODEC_ID_RAWVIDEO && imgfmt.bmiHeader.biCompression== BI_RGB) {
00126 st->codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
00127 if (st->codec->extradata) {
00128 st->codec->extradata_size = 9;
00129 memcpy(st->codec->extradata, "BottomUp", 9);
00130 }
00131 }
00132
00133
00134 st->duration = stream->info.dwLength;
00135 }
00136 else
00137 {
00138 AVIStreamRelease(stream->handle);
00139 continue;
00140 }
00141
00142 avs->nb_streams++;
00143
00144 st->codec->stream_codec_tag = stream->info.fccHandler;
00145
00146 av_set_pts_info(st, 64, info.dwScale, info.dwRate);
00147 st->start_time = stream->info.dwStart;
00148 }
00149 }
00150 }
00151
00152 return 0;
00153 }
00154
00155 static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
00156 {
00157 AVISynthContext *avs = s->priv_data;
00158 HRESULT res;
00159 AVISynthStream *stream;
00160 int stream_id = avs->next_stream;
00161 LONG read_size;
00162
00163
00164 stream = &avs->streams[stream_id];
00165
00166 if (stream->read >= stream->info.dwLength)
00167 return AVERROR(EIO);
00168
00169 if (av_new_packet(pkt, stream->chunck_size))
00170 return AVERROR(EIO);
00171 pkt->stream_index = stream_id;
00172 pkt->pts = avs->streams[stream_id].read / avs->streams[stream_id].chunck_samples;
00173
00174 res = AVIStreamRead(stream->handle, stream->read, stream->chunck_samples, pkt->data, stream->chunck_size, &read_size, NULL);
00175
00176 pkt->size = read_size;
00177
00178 stream->read += stream->chunck_samples;
00179
00180
00181 do {
00182 avs->next_stream = (avs->next_stream+1) % avs->nb_streams;
00183 } while (avs->next_stream != stream_id && s->streams[avs->next_stream]->discard >= AVDISCARD_ALL);
00184
00185 return (res == S_OK) ? pkt->size : -1;
00186 }
00187
00188 static int avisynth_read_close(AVFormatContext *s)
00189 {
00190 AVISynthContext *avs = s->priv_data;
00191 int i;
00192
00193 for (i=0;i<avs->nb_streams;i++)
00194 {
00195 AVIStreamRelease(avs->streams[i].handle);
00196 }
00197
00198 av_free(avs->streams);
00199 AVIFileRelease(avs->file);
00200 AVIFileExit();
00201 return 0;
00202 }
00203
00204 static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
00205 {
00206 AVISynthContext *avs = s->priv_data;
00207 int stream_id;
00208
00209 for (stream_id = 0; stream_id < avs->nb_streams; stream_id++)
00210 {
00211 avs->streams[stream_id].read = pts * avs->streams[stream_id].chunck_samples;
00212 }
00213
00214 return 0;
00215 }
00216
00217 AVInputFormat ff_avisynth_demuxer = {
00218 "avs",
00219 NULL_IF_CONFIG_SMALL("AVISynth"),
00220 sizeof(AVISynthContext),
00221 NULL,
00222 avisynth_read_header,
00223 avisynth_read_packet,
00224 avisynth_read_close,
00225 avisynth_read_seek,
00226 NULL,
00227 0,
00228 "avs",
00229 };