1283
|
1 /*
|
|
2 * AVISynth support for ffmpeg system
|
|
3 * Copyright (c) 2006 DivX, Inc.
|
|
4 *
|
|
5 * This library is free software; you can redistribute it and/or
|
|
6 * modify it under the terms of the GNU Lesser General Public
|
|
7 * License as published by the Free Software Foundation; either
|
|
8 * version 2 of the License, or (at your option) any later version.
|
|
9 *
|
|
10 * This library is distributed in the hope that it will be useful,
|
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 * Lesser General Public License for more details.
|
|
14 *
|
|
15 * You should have received a copy of the GNU Lesser General Public
|
|
16 * License along with this library; if not, write to the Free Software
|
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
18 */
|
|
19
|
|
20 #include "avformat.h"
|
|
21 #include "riff.h"
|
|
22
|
|
23 #include <windows.h>
|
|
24 #include <vfw.h>
|
|
25
|
|
26 typedef struct {
|
|
27 PAVISTREAM handle;
|
|
28 AVISTREAMINFO info;
|
|
29 DWORD read;
|
|
30 LONG chunck_size;
|
|
31 LONG chunck_samples;
|
|
32 } AVISynthStream;
|
|
33
|
|
34 typedef struct {
|
|
35 PAVIFILE file;
|
|
36 AVISynthStream *streams;
|
|
37 int nb_streams;
|
|
38 int next_stream;
|
|
39 } AVISynthContext;
|
|
40
|
|
41 static int avisynth_read_header(AVFormatContext *s, AVFormatParameters *ap)
|
|
42 {
|
|
43 AVISynthContext *avs = s->priv_data;
|
|
44 HRESULT res;
|
|
45 AVIFILEINFO info;
|
|
46 DWORD id;
|
|
47 AVStream *st;
|
|
48 AVISynthStream *stream;
|
|
49
|
|
50 AVIFileInit();
|
|
51
|
|
52 res = AVIFileOpen(&avs->file, s->filename, OF_READ|OF_SHARE_DENY_WRITE, NULL);
|
|
53 if (res != S_OK)
|
|
54 {
|
|
55 av_log(s, AV_LOG_ERROR, "AVIFileOpen failed with error %ld", res);
|
|
56 AVIFileExit();
|
|
57 return -1;
|
|
58 }
|
|
59
|
|
60 res = AVIFileInfo(avs->file, &info, sizeof(info));
|
|
61 if (res != S_OK)
|
|
62 {
|
|
63 av_log(s, AV_LOG_ERROR, "AVIFileInfo failed with error %ld", res);
|
|
64 AVIFileExit();
|
|
65 return -1;
|
|
66 }
|
|
67
|
|
68 avs->streams = av_mallocz(info.dwStreams * sizeof(AVISynthStream));
|
|
69
|
|
70 for (id=0; id<info.dwStreams; id++)
|
|
71 {
|
|
72 stream = &avs->streams[id];
|
|
73 stream->read = 0;
|
|
74 if (AVIFileGetStream(avs->file, &stream->handle, 0, id) == S_OK)
|
|
75 {
|
|
76 if (AVIStreamInfo(stream->handle, &stream->info, sizeof(stream->info)) == S_OK)
|
|
77 {
|
|
78 if (stream->info.fccType == streamtypeAUDIO)
|
|
79 {
|
|
80 WAVEFORMATEX wvfmt;
|
|
81 LONG struct_size = sizeof(WAVEFORMATEX);
|
|
82 if (AVIStreamReadFormat(stream->handle, 0, &wvfmt, &struct_size) != S_OK)
|
|
83 continue;
|
|
84
|
|
85 st = av_new_stream(s, id);
|
|
86 st->codec->codec_type = CODEC_TYPE_AUDIO;
|
|
87
|
|
88 st->codec->block_align = wvfmt.nBlockAlign;
|
|
89 st->codec->channels = wvfmt.nChannels;
|
|
90 st->codec->sample_rate = wvfmt.nSamplesPerSec;
|
|
91 st->codec->bit_rate = wvfmt.nAvgBytesPerSec * 8;
|
|
92 st->codec->bits_per_sample = wvfmt.wBitsPerSample;
|
|
93
|
|
94 stream->chunck_samples = wvfmt.nSamplesPerSec * (uint64_t)info.dwScale / (uint64_t)info.dwRate;
|
|
95 stream->chunck_size = stream->chunck_samples * wvfmt.nChannels * wvfmt.wBitsPerSample / 8;
|
|
96
|
1346
|
97 st->codec->codec_tag = wvfmt.wFormatTag;
|
1283
|
98 st->codec->codec_id = wav_codec_get_id(wvfmt.wFormatTag, st->codec->bits_per_sample);
|
|
99 }
|
|
100 else if (stream->info.fccType == streamtypeVIDEO)
|
|
101 {
|
|
102 BITMAPINFO imgfmt;
|
|
103 LONG struct_size = sizeof(BITMAPINFO);
|
|
104
|
|
105 stream->chunck_size = stream->info.dwSampleSize;
|
|
106 stream->chunck_samples = 1;
|
|
107
|
|
108 if (AVIStreamReadFormat(stream->handle, 0, &imgfmt, &struct_size) != S_OK)
|
|
109 continue;
|
|
110
|
|
111 st = av_new_stream(s, id);
|
|
112 st->codec->codec_type = CODEC_TYPE_VIDEO;
|
|
113 st->r_frame_rate.num = stream->info.dwRate;
|
|
114 st->r_frame_rate.den = stream->info.dwScale;
|
|
115
|
|
116 st->codec->width = imgfmt.bmiHeader.biWidth;
|
|
117 st->codec->height = imgfmt.bmiHeader.biHeight;
|
|
118
|
1346
|
119 st->codec->bits_per_sample = imgfmt.bmiHeader.biBitCount;
|
1283
|
120 st->codec->bit_rate = (uint64_t)stream->info.dwSampleSize * (uint64_t)stream->info.dwRate * 8 / (uint64_t)stream->info.dwScale;
|
1346
|
121 st->codec->codec_tag = imgfmt.bmiHeader.biCompression;
|
|
122 st->codec->codec_id = codec_get_id(codec_bmp_tags, imgfmt.bmiHeader.biCompression);
|
1283
|
123
|
|
124 st->duration = stream->info.dwLength;
|
|
125 }
|
|
126 else
|
|
127 {
|
|
128 AVIStreamRelease(stream->handle);
|
|
129 continue;
|
|
130 }
|
|
131
|
|
132 avs->nb_streams++;
|
|
133
|
|
134 st->codec->stream_codec_tag = stream->info.fccHandler;
|
|
135
|
|
136 av_set_pts_info(st, 64, info.dwScale, info.dwRate);
|
|
137 st->start_time = stream->info.dwStart;
|
|
138 }
|
|
139 }
|
|
140 }
|
|
141
|
|
142 return 0;
|
|
143 }
|
|
144
|
|
145 static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
146 {
|
|
147 AVISynthContext *avs = s->priv_data;
|
|
148 HRESULT res;
|
|
149 AVISynthStream *stream;
|
|
150 int stream_id = avs->next_stream;
|
|
151 LONG read_size;
|
|
152
|
|
153 // handle interleaving manually...
|
|
154 stream = &avs->streams[stream_id];
|
|
155
|
|
156 if (stream->read >= stream->info.dwLength)
|
|
157 return AVERROR_IO;
|
|
158
|
|
159 if (av_new_packet(pkt, stream->chunck_size))
|
|
160 return AVERROR_IO;
|
|
161 pkt->stream_index = stream_id;
|
|
162 pkt->pts = avs->streams[stream_id].read / avs->streams[stream_id].chunck_samples;
|
|
163
|
|
164 res = AVIStreamRead(stream->handle, stream->read, stream->chunck_samples, pkt->data, stream->chunck_size, &read_size, NULL);
|
|
165
|
|
166 pkt->pts = stream->read;
|
|
167 pkt->size = read_size;
|
|
168
|
|
169 stream->read += stream->chunck_samples;
|
|
170
|
|
171 // prepare for the next stream to read
|
|
172 do {
|
|
173 avs->next_stream = (avs->next_stream+1) % avs->nb_streams;
|
|
174 } while (avs->next_stream != stream_id && s->streams[avs->next_stream]->discard >= AVDISCARD_ALL);
|
|
175
|
|
176 return (res == S_OK) ? pkt->size : -1;
|
|
177 }
|
|
178
|
|
179 static int avisynth_read_close(AVFormatContext *s)
|
|
180 {
|
|
181 AVISynthContext *avs = s->priv_data;
|
|
182 int i;
|
|
183
|
|
184 for (i=0;i<avs->nb_streams;i++)
|
|
185 {
|
|
186 AVIStreamRelease(avs->streams[i].handle);
|
|
187 }
|
|
188
|
|
189 av_free(avs->streams);
|
|
190 AVIFileRelease(avs->file);
|
|
191 AVIFileExit();
|
|
192 return 0;
|
|
193 }
|
|
194
|
|
195 static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
|
|
196 {
|
|
197 AVISynthContext *avs = s->priv_data;
|
|
198 int stream_id;
|
|
199
|
|
200 for (stream_id = 0; stream_id < avs->nb_streams; stream_id++)
|
|
201 {
|
|
202 avs->streams[stream_id].read = pts * avs->streams[stream_id].chunck_samples;
|
|
203 }
|
|
204
|
|
205 return 0;
|
|
206 }
|
|
207
|
|
208 AVInputFormat avisynth_demuxer = {
|
|
209 "avs",
|
|
210 "AVISynth",
|
|
211 sizeof(AVISynthContext),
|
|
212 NULL,
|
|
213 avisynth_read_header,
|
|
214 avisynth_read_packet,
|
|
215 avisynth_read_close,
|
|
216 avisynth_read_seek,
|
|
217 NULL,
|
|
218 0,
|
|
219 "avs",
|
|
220 };
|