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