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