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