Add missing file from commit r6122 (AVISynth support)
[ffmpeg.git] / libavformat / avisynth.c
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
97                   st->codec->codec_id = wav_codec_get_id(wvfmt.wFormatTag, st->codec->bits_per_sample);
98                 }
99               else if (stream->info.fccType == streamtypeVIDEO)
100                 {
101                   BITMAPINFO imgfmt;
102                   LONG struct_size = sizeof(BITMAPINFO);
103
104                   stream->chunck_size = stream->info.dwSampleSize;
105                   stream->chunck_samples = 1;
106
107                   if (AVIStreamReadFormat(stream->handle, 0, &imgfmt, &struct_size) != S_OK)
108                     continue;
109
110                   st = av_new_stream(s, id);
111                   st->codec->codec_type = CODEC_TYPE_VIDEO;
112                   st->r_frame_rate.num = stream->info.dwRate;
113                   st->r_frame_rate.den = stream->info.dwScale;
114
115                   st->codec->width = imgfmt.bmiHeader.biWidth;
116                   st->codec->height = imgfmt.bmiHeader.biHeight;
117
118                   st->codec->bits_per_sample = stream->info.dwSampleSize * 8;
119                   st->codec->bit_rate = (uint64_t)stream->info.dwSampleSize * (uint64_t)stream->info.dwRate * 8 / (uint64_t)stream->info.dwScale;
120                   st->codec->codec_id = codec_get_id(codec_bmp_tags, stream->info.fccHandler);
121
122                   st->duration = stream->info.dwLength;
123                 }
124               else
125                 {
126                   AVIStreamRelease(stream->handle);
127                   continue;
128                 }
129
130               avs->nb_streams++;
131               st->codec->codec_tag = stream->info.fccHandler;
132
133               st->codec->stream_codec_tag = stream->info.fccHandler;
134
135               av_set_pts_info(st, 64, info.dwScale, info.dwRate);
136               st->start_time = stream->info.dwStart;
137             }
138         }
139     }
140
141   return 0;
142 }
143
144 static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
145 {
146   AVISynthContext *avs = s->priv_data;
147   HRESULT res;
148   AVISynthStream *stream;
149   int stream_id = avs->next_stream;
150   LONG read_size;
151
152   // handle interleaving manually...
153   stream = &avs->streams[stream_id];
154
155   if (stream->read >= stream->info.dwLength)
156     return AVERROR_IO;
157
158   if (av_new_packet(pkt, stream->chunck_size))
159     return AVERROR_IO;
160   pkt->stream_index = stream_id;
161   pkt->pts = avs->streams[stream_id].read / avs->streams[stream_id].chunck_samples;
162
163   res = AVIStreamRead(stream->handle, stream->read, stream->chunck_samples, pkt->data, stream->chunck_size, &read_size, NULL);
164
165   pkt->pts = stream->read;
166   pkt->size = read_size;
167
168   stream->read += stream->chunck_samples;
169
170   // prepare for the next stream to read
171   do {
172     avs->next_stream = (avs->next_stream+1) % avs->nb_streams;
173   } while (avs->next_stream != stream_id && s->streams[avs->next_stream]->discard >= AVDISCARD_ALL);
174
175   return (res == S_OK) ? pkt->size : -1;
176 }
177
178 static int avisynth_read_close(AVFormatContext *s)
179 {
180   AVISynthContext *avs = s->priv_data;
181   int i;
182
183   for (i=0;i<avs->nb_streams;i++)
184     {
185       AVIStreamRelease(avs->streams[i].handle);
186     }
187
188   av_free(avs->streams);
189   AVIFileRelease(avs->file);
190   AVIFileExit();
191   return 0;
192 }
193
194 static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
195 {
196   AVISynthContext *avs = s->priv_data;
197   int stream_id;
198
199   for (stream_id = 0; stream_id < avs->nb_streams; stream_id++)
200     {
201       avs->streams[stream_id].read = pts * avs->streams[stream_id].chunck_samples;
202     }
203
204   return 0;
205 }
206
207 AVInputFormat avisynth_demuxer = {
208   "avs",
209   "AVISynth",
210   sizeof(AVISynthContext),
211   NULL,
212   avisynth_read_header,
213   avisynth_read_packet,
214   avisynth_read_close,
215   avisynth_read_seek,
216   NULL,
217   0,
218   "avs",
219 };