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 | ||
c16ddcc9 DB |
22 | #include <windows.h> |
23 | #include <vfw.h> | |
24 | ||
97b052e5 | 25 | #include "libavutil/internal.h" |
9dc5607b | 26 | #include "avformat.h" |
c3f9ebf7 | 27 | #include "internal.h" |
9dc5607b GP |
28 | #include "riff.h" |
29 | ||
9dc5607b | 30 | typedef struct { |
c16ddcc9 DB |
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 { | |
c16ddcc9 DB |
39 | PAVIFILE file; |
40 | AviSynthStream *streams; | |
41 | int nb_streams; | |
42 | int next_stream; | |
03039f4c | 43 | } AviSynthContext; |
9dc5607b | 44 | |
6e9651d1 | 45 | static int avisynth_read_header(AVFormatContext *s) |
9dc5607b | 46 | { |
c16ddcc9 DB |
47 | AviSynthContext *avs = s->priv_data; |
48 | HRESULT res; | |
49 | AVIFILEINFO info; | |
50 | DWORD id; | |
51 | AVStream *st; | |
52 | AviSynthStream *stream; | |
53 | wchar_t filename_wchar[1024] = { 0 }; | |
54 | char filename_char[1024] = { 0 }; | |
55 | ||
56 | AVIFileInit(); | |
57 | ||
58 | /* AviSynth cannot accept UTF-8 file names. */ | |
59 | MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wchar, 1024); | |
60 | WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wchar, -1, filename_char, | |
61 | 1024, NULL, NULL); | |
62 | res = AVIFileOpen(&avs->file, filename_char, | |
63 | OF_READ | OF_SHARE_DENY_WRITE, NULL); | |
64 | if (res != S_OK) { | |
65 | av_log(s, AV_LOG_ERROR, "AVIFileOpen failed with error %ld", res); | |
66 | AVIFileExit(); | |
67 | return -1; | |
9dc5607b GP |
68 | } |
69 | ||
c16ddcc9 DB |
70 | res = AVIFileInfo(avs->file, &info, sizeof(info)); |
71 | if (res != S_OK) { | |
72 | av_log(s, AV_LOG_ERROR, "AVIFileInfo failed with error %ld", res); | |
73 | AVIFileExit(); | |
74 | return -1; | |
9dc5607b GP |
75 | } |
76 | ||
c16ddcc9 DB |
77 | avs->streams = av_mallocz(info.dwStreams * sizeof(AviSynthStream)); |
78 | ||
79 | for (id = 0; id < info.dwStreams; id++) { | |
80 | stream = &avs->streams[id]; | |
81 | stream->read = 0; | |
82 | if (AVIFileGetStream(avs->file, &stream->handle, 0, id) == S_OK && | |
83 | AVIStreamInfo(stream->handle, &stream->info, | |
84 | sizeof(stream->info)) == S_OK) { | |
85 | if (stream->info.fccType == streamtypeAUDIO) { | |
86 | WAVEFORMATEX wvfmt; | |
87 | LONG struct_size = sizeof(WAVEFORMATEX); | |
88 | if (AVIStreamReadFormat(stream->handle, 0, | |
89 | &wvfmt, &struct_size) != S_OK) | |
9dc5607b GP |
90 | continue; |
91 | ||
c16ddcc9 DB |
92 | st = avformat_new_stream(s, NULL); |
93 | st->id = id; | |
94 | st->codec->codec_type = AVMEDIA_TYPE_AUDIO; | |
95 | ||
96 | st->codec->block_align = wvfmt.nBlockAlign; | |
97 | st->codec->channels = wvfmt.nChannels; | |
98 | st->codec->sample_rate = wvfmt.nSamplesPerSec; | |
99 | st->codec->bit_rate = wvfmt.nAvgBytesPerSec * 8; | |
100 | st->codec->bits_per_coded_sample = wvfmt.wBitsPerSample; | |
101 | ||
102 | stream->chunck_samples = wvfmt.nSamplesPerSec * | |
103 | (uint64_t)info.dwScale / | |
104 | (uint64_t)info.dwRate; | |
105 | stream->chunck_size = stream->chunck_samples * | |
106 | wvfmt.nChannels * | |
107 | wvfmt.wBitsPerSample / 8; | |
108 | ||
109 | st->codec->codec_tag = wvfmt.wFormatTag; | |
110 | st->codec->codec_id = | |
111 | ff_wav_codec_get_id(wvfmt.wFormatTag, | |
112 | st->codec->bits_per_coded_sample); | |
113 | } else if (stream->info.fccType == streamtypeVIDEO) { | |
114 | BITMAPINFO imgfmt; | |
115 | LONG struct_size = sizeof(BITMAPINFO); | |
116 | ||
117 | stream->chunck_size = stream->info.dwSampleSize; | |
118 | stream->chunck_samples = 1; | |
119 | ||
120 | if (AVIStreamReadFormat(stream->handle, 0, &imgfmt, | |
121 | &struct_size) != S_OK) | |
9dc5607b GP |
122 | continue; |
123 | ||
c16ddcc9 DB |
124 | st = avformat_new_stream(s, NULL); |
125 | st->id = id; | |
126 | st->codec->codec_type = AVMEDIA_TYPE_VIDEO; | |
127 | st->avg_frame_rate.num = stream->info.dwRate; | |
128 | st->avg_frame_rate.den = stream->info.dwScale; | |
129 | ||
130 | st->codec->width = imgfmt.bmiHeader.biWidth; | |
131 | st->codec->height = imgfmt.bmiHeader.biHeight; | |
132 | ||
133 | st->codec->bits_per_coded_sample = imgfmt.bmiHeader.biBitCount; | |
134 | st->codec->bit_rate = (uint64_t)stream->info.dwSampleSize * | |
135 | (uint64_t)stream->info.dwRate * 8 / | |
136 | (uint64_t)stream->info.dwScale; | |
137 | st->codec->codec_tag = imgfmt.bmiHeader.biCompression; | |
138 | st->codec->codec_id = | |
139 | ff_codec_get_id(ff_codec_bmp_tags, | |
140 | imgfmt.bmiHeader.biCompression); | |
141 | ||
142 | st->duration = stream->info.dwLength; | |
143 | } else { | |
144 | AVIStreamRelease(stream->handle); | |
145 | continue; | |
146 | } | |
9dc5607b | 147 | |
c16ddcc9 | 148 | avs->nb_streams++; |
9dc5607b | 149 | |
c16ddcc9 | 150 | st->codec->stream_codec_tag = stream->info.fccHandler; |
9dc5607b | 151 | |
c16ddcc9 DB |
152 | avpriv_set_pts_info(st, 64, info.dwScale, info.dwRate); |
153 | st->start_time = stream->info.dwStart; | |
9dc5607b GP |
154 | } |
155 | } | |
156 | ||
c16ddcc9 | 157 | return 0; |
9dc5607b GP |
158 | } |
159 | ||
160 | static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt) | |
161 | { | |
c16ddcc9 DB |
162 | AviSynthContext *avs = s->priv_data; |
163 | HRESULT res; | |
164 | AviSynthStream *stream; | |
165 | int stream_id = avs->next_stream; | |
166 | LONG read_size; | |
9dc5607b | 167 | |
c16ddcc9 DB |
168 | // handle interleaving manually... |
169 | stream = &avs->streams[stream_id]; | |
9dc5607b | 170 | |
c16ddcc9 DB |
171 | if (stream->read >= stream->info.dwLength) |
172 | return AVERROR(EIO); | |
9dc5607b | 173 | |
c16ddcc9 DB |
174 | if (av_new_packet(pkt, stream->chunck_size)) |
175 | return AVERROR(EIO); | |
176 | pkt->stream_index = stream_id; | |
177 | pkt->pts = avs->streams[stream_id].read / | |
178 | avs->streams[stream_id].chunck_samples; | |
9dc5607b | 179 | |
c16ddcc9 DB |
180 | res = AVIStreamRead(stream->handle, stream->read, stream->chunck_samples, |
181 | pkt->data, stream->chunck_size, &read_size, NULL); | |
9dc5607b | 182 | |
c16ddcc9 DB |
183 | pkt->pts = stream->read; |
184 | pkt->size = read_size; | |
9dc5607b | 185 | |
c16ddcc9 | 186 | stream->read += stream->chunck_samples; |
9dc5607b | 187 | |
c16ddcc9 DB |
188 | // prepare for the next stream to read |
189 | do | |
190 | avs->next_stream = (avs->next_stream + 1) % avs->nb_streams; | |
191 | while (avs->next_stream != stream_id && | |
192 | s->streams[avs->next_stream]->discard >= AVDISCARD_ALL); | |
9dc5607b | 193 | |
c16ddcc9 | 194 | return (res == S_OK) ? pkt->size : -1; |
9dc5607b GP |
195 | } |
196 | ||
197 | static int avisynth_read_close(AVFormatContext *s) | |
198 | { | |
c16ddcc9 DB |
199 | AviSynthContext *avs = s->priv_data; |
200 | int i; | |
9dc5607b | 201 | |
c16ddcc9 DB |
202 | for (i = 0; i < avs->nb_streams; i++) |
203 | AVIStreamRelease(avs->streams[i].handle); | |
9dc5607b | 204 | |
c16ddcc9 DB |
205 | av_free(avs->streams); |
206 | AVIFileRelease(avs->file); | |
207 | AVIFileExit(); | |
208 | return 0; | |
9dc5607b GP |
209 | } |
210 | ||
c16ddcc9 DB |
211 | static int avisynth_read_seek(AVFormatContext *s, int stream_index, |
212 | int64_t pts, int flags) | |
9dc5607b | 213 | { |
c16ddcc9 DB |
214 | AviSynthContext *avs = s->priv_data; |
215 | int stream_id; | |
9dc5607b | 216 | |
c16ddcc9 DB |
217 | for (stream_id = 0; stream_id < avs->nb_streams; stream_id++) |
218 | avs->streams[stream_id].read = | |
219 | pts * avs->streams[stream_id].chunck_samples; | |
9dc5607b | 220 | |
c16ddcc9 | 221 | return 0; |
9dc5607b GP |
222 | } |
223 | ||
c6610a21 | 224 | AVInputFormat ff_avisynth_demuxer = { |
0f5b0b41 | 225 | .name = "avisynth", |
03039f4c DB |
226 | .long_name = NULL_IF_CONFIG_SMALL("AviSynth"), |
227 | .priv_data_size = sizeof(AviSynthContext), | |
dfc2c4d9 AK |
228 | .read_header = avisynth_read_header, |
229 | .read_packet = avisynth_read_packet, | |
230 | .read_close = avisynth_read_close, | |
231 | .read_seek = avisynth_read_seek, | |
232 | .extensions = "avs", | |
9dc5607b | 233 | }; |