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 if (st->codec->codec_id == CODEC_ID_RAWVIDEO && imgfmt.bmiHeader.biCompression== BI_RGB) {
00129 st->codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
00130 if (st->codec->extradata) {
00131 st->codec->extradata_size = 9;
00132 memcpy(st->codec->extradata, "BottomUp", 9);
00133 }
00134 }
00135
00136
00137 st->duration = stream->info.dwLength;
00138 }
00139 else
00140 {
00141 AVIStreamRelease(stream->handle);
00142 continue;
00143 }
00144
00145 avs->nb_streams++;
00146
00147 st->codec->stream_codec_tag = stream->info.fccHandler;
00148
00149 avpriv_set_pts_info(st, 64, info.dwScale, info.dwRate);
00150 st->start_time = stream->info.dwStart;
00151 }
00152 }
00153 }
00154
00155 return 0;
00156 }
00157
00158 static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
00159 {
00160 AVISynthContext *avs = s->priv_data;
00161 HRESULT res;
00162 AVISynthStream *stream;
00163 int stream_id = avs->next_stream;
00164 LONG read_size;
00165
00166
00167 stream = &avs->streams[stream_id];
00168
00169 if (stream->read >= stream->info.dwLength)
00170 return AVERROR(EIO);
00171
00172 if (av_new_packet(pkt, stream->chunck_size))
00173 return AVERROR(EIO);
00174 pkt->stream_index = stream_id;
00175 pkt->pts = avs->streams[stream_id].read / avs->streams[stream_id].chunck_samples;
00176
00177 res = AVIStreamRead(stream->handle, stream->read, stream->chunck_samples, pkt->data, stream->chunck_size, &read_size, NULL);
00178
00179 pkt->size = read_size;
00180
00181 stream->read += stream->chunck_samples;
00182
00183
00184 do {
00185 avs->next_stream = (avs->next_stream+1) % avs->nb_streams;
00186 } while (avs->next_stream != stream_id && s->streams[avs->next_stream]->discard >= AVDISCARD_ALL);
00187
00188 return (res == S_OK) ? pkt->size : -1;
00189 }
00190
00191 static int avisynth_read_close(AVFormatContext *s)
00192 {
00193 AVISynthContext *avs = s->priv_data;
00194 int i;
00195
00196 for (i=0;i<avs->nb_streams;i++)
00197 {
00198 AVIStreamRelease(avs->streams[i].handle);
00199 }
00200
00201 av_free(avs->streams);
00202 AVIFileRelease(avs->file);
00203 AVIFileExit();
00204 return 0;
00205 }
00206
00207 static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
00208 {
00209 AVISynthContext *avs = s->priv_data;
00210 int stream_id;
00211
00212 for (stream_id = 0; stream_id < avs->nb_streams; stream_id++)
00213 {
00214 avs->streams[stream_id].read = pts * avs->streams[stream_id].chunck_samples;
00215 }
00216
00217 return 0;
00218 }
00219
00220 AVInputFormat ff_avisynth_demuxer = {
00221 .name = "avs",
00222 .long_name = NULL_IF_CONFIG_SMALL("AVISynth"),
00223 .priv_data_size = sizeof(AVISynthContext),
00224 .read_header = avisynth_read_header,
00225 .read_packet = avisynth_read_packet,
00226 .read_close = avisynth_read_close,
00227 .read_seek = avisynth_read_seek,
00228 .extensions = "avs",
00229 };