]> git.sesse.net Git - ffmpeg/blob - libavformat/avisynth.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / avisynth.c
1 /*
2  * AVISynth support
3  * Copyright (c) 2006 DivX, Inc.
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
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
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "avformat.h"
23 #include "riff.h"
24
25 #include <windows.h>
26 #include <vfw.h>
27
28 typedef struct {
29   PAVISTREAM handle;
30   AVISTREAMINFO info;
31   DWORD read;
32   LONG chunck_size;
33   LONG chunck_samples;
34 } AVISynthStream;
35
36 typedef struct {
37   PAVIFILE file;
38   AVISynthStream *streams;
39   int nb_streams;
40   int next_stream;
41 } AVISynthContext;
42
43 static int avisynth_read_header(AVFormatContext *s, AVFormatParameters *ap)
44 {
45   AVISynthContext *avs = s->priv_data;
46   HRESULT res;
47   AVIFILEINFO info;
48   DWORD id;
49   AVStream *st;
50   AVISynthStream *stream;
51
52   AVIFileInit();
53
54   res = AVIFileOpen(&avs->file, s->filename, OF_READ|OF_SHARE_DENY_WRITE, NULL);
55   if (res != S_OK)
56     {
57       av_log(s, AV_LOG_ERROR, "AVIFileOpen failed with error %ld", res);
58       AVIFileExit();
59       return -1;
60     }
61
62   res = AVIFileInfo(avs->file, &info, sizeof(info));
63   if (res != S_OK)
64     {
65       av_log(s, AV_LOG_ERROR, "AVIFileInfo failed with error %ld", res);
66       AVIFileExit();
67       return -1;
68     }
69
70   avs->streams = av_mallocz(info.dwStreams * sizeof(AVISynthStream));
71
72   for (id=0; id<info.dwStreams; id++)
73     {
74       stream = &avs->streams[id];
75       stream->read = 0;
76       if (AVIFileGetStream(avs->file, &stream->handle, 0, id) == S_OK)
77         {
78           if (AVIStreamInfo(stream->handle, &stream->info, sizeof(stream->info)) == S_OK)
79             {
80               if (stream->info.fccType == streamtypeAUDIO)
81                 {
82                   WAVEFORMATEX wvfmt;
83                   LONG struct_size = sizeof(WAVEFORMATEX);
84                   if (AVIStreamReadFormat(stream->handle, 0, &wvfmt, &struct_size) != S_OK)
85                     continue;
86
87                   st = avformat_new_stream(s, NULL);
88                   st->id = id;
89                   st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
90
91                   st->codec->block_align = wvfmt.nBlockAlign;
92                   st->codec->channels = wvfmt.nChannels;
93                   st->codec->sample_rate = wvfmt.nSamplesPerSec;
94                   st->codec->bit_rate = wvfmt.nAvgBytesPerSec * 8;
95                   st->codec->bits_per_coded_sample = wvfmt.wBitsPerSample;
96
97                   stream->chunck_samples = wvfmt.nSamplesPerSec * (uint64_t)info.dwScale / (uint64_t)info.dwRate;
98                   stream->chunck_size = stream->chunck_samples * wvfmt.nChannels * wvfmt.wBitsPerSample / 8;
99
100                   st->codec->codec_tag = wvfmt.wFormatTag;
101                   st->codec->codec_id = ff_wav_codec_get_id(wvfmt.wFormatTag, st->codec->bits_per_coded_sample);
102                 }
103               else if (stream->info.fccType == streamtypeVIDEO)
104                 {
105                   BITMAPINFO imgfmt;
106                   LONG struct_size = sizeof(BITMAPINFO);
107
108                   stream->chunck_size = stream->info.dwSampleSize;
109                   stream->chunck_samples = 1;
110
111                   if (AVIStreamReadFormat(stream->handle, 0, &imgfmt, &struct_size) != S_OK)
112                     continue;
113
114                   st = avformat_new_stream(s, NULL);
115                   st->id = id;
116                   st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
117                   st->r_frame_rate.num = stream->info.dwRate;
118                   st->r_frame_rate.den = stream->info.dwScale;
119
120                   st->codec->width = imgfmt.bmiHeader.biWidth;
121                   st->codec->height = imgfmt.bmiHeader.biHeight;
122
123                   st->codec->bits_per_coded_sample = imgfmt.bmiHeader.biBitCount;
124                   st->codec->bit_rate = (uint64_t)stream->info.dwSampleSize * (uint64_t)stream->info.dwRate * 8 / (uint64_t)stream->info.dwScale;
125                   st->codec->codec_tag = imgfmt.bmiHeader.biCompression;
126                   st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, imgfmt.bmiHeader.biCompression);
127                   if (st->codec->codec_id == CODEC_ID_RAWVIDEO && imgfmt.bmiHeader.biCompression== BI_RGB) {
128                     st->codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
129                     if (st->codec->extradata) {
130                       st->codec->extradata_size = 9;
131                       memcpy(st->codec->extradata, "BottomUp", 9);
132                     }
133                   }
134
135
136                   st->duration = stream->info.dwLength;
137                 }
138               else
139                 {
140                   AVIStreamRelease(stream->handle);
141                   continue;
142                 }
143
144               avs->nb_streams++;
145
146               st->codec->stream_codec_tag = stream->info.fccHandler;
147
148               av_set_pts_info(st, 64, info.dwScale, info.dwRate);
149               st->start_time = stream->info.dwStart;
150             }
151         }
152     }
153
154   return 0;
155 }
156
157 static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
158 {
159   AVISynthContext *avs = s->priv_data;
160   HRESULT res;
161   AVISynthStream *stream;
162   int stream_id = avs->next_stream;
163   LONG read_size;
164
165   // handle interleaving manually...
166   stream = &avs->streams[stream_id];
167
168   if (stream->read >= stream->info.dwLength)
169     return AVERROR(EIO);
170
171   if (av_new_packet(pkt, stream->chunck_size))
172     return AVERROR(EIO);
173   pkt->stream_index = stream_id;
174   pkt->pts = avs->streams[stream_id].read / avs->streams[stream_id].chunck_samples;
175
176   res = AVIStreamRead(stream->handle, stream->read, stream->chunck_samples, pkt->data, stream->chunck_size, &read_size, NULL);
177
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 {
192   AVISynthContext *avs = s->priv_data;
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 {
208   AVISynthContext *avs = s->priv_data;
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
219 AVInputFormat ff_avisynth_demuxer = {
220     .name           = "avs",
221     .long_name      = NULL_IF_CONFIG_SMALL("AVISynth"),
222     .priv_data_size = sizeof(AVISynthContext),
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",
228 };