avisynth.c
Go to the documentation of this file.
1 /*
2  * AVISynth support
3  * Copyright (c) 2006 DivX, Inc.
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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;
36 
37 typedef struct {
38  PAVIFILE file;
43 
45 {
46  AVISynthContext *avs = s->priv_data;
47  HRESULT res;
48  AVIFILEINFO info;
49  DWORD id;
50  AVStream *st;
51  AVISynthStream *stream;
52 
53  AVIFileInit();
54 
55  res = AVIFileOpen(&avs->file, s->filename, OF_READ|OF_SHARE_DENY_WRITE, NULL);
56  if (res != S_OK)
57  {
58  av_log(s, AV_LOG_ERROR, "AVIFileOpen failed with error %ld", res);
59  AVIFileExit();
60  return -1;
61  }
62 
63  res = AVIFileInfo(avs->file, &info, sizeof(info));
64  if (res != S_OK)
65  {
66  av_log(s, AV_LOG_ERROR, "AVIFileInfo failed with error %ld", res);
67  AVIFileExit();
68  return -1;
69  }
70 
71  avs->streams = av_mallocz(info.dwStreams * sizeof(AVISynthStream));
72 
73  for (id=0; id<info.dwStreams; id++)
74  {
75  stream = &avs->streams[id];
76  stream->read = 0;
77  if (AVIFileGetStream(avs->file, &stream->handle, 0, id) == S_OK)
78  {
79  if (AVIStreamInfo(stream->handle, &stream->info, sizeof(stream->info)) == S_OK)
80  {
81  if (stream->info.fccType == streamtypeAUDIO)
82  {
83  WAVEFORMATEX wvfmt;
84  LONG struct_size = sizeof(WAVEFORMATEX);
85  if (AVIStreamReadFormat(stream->handle, 0, &wvfmt, &struct_size) != S_OK)
86  continue;
87 
88  st = avformat_new_stream(s, NULL);
89  st->id = id;
91 
92  st->codec->block_align = wvfmt.nBlockAlign;
93  st->codec->channels = wvfmt.nChannels;
94  st->codec->sample_rate = wvfmt.nSamplesPerSec;
95  st->codec->bit_rate = wvfmt.nAvgBytesPerSec * 8;
96  st->codec->bits_per_coded_sample = wvfmt.wBitsPerSample;
97 
98  stream->chunck_samples = wvfmt.nSamplesPerSec * (uint64_t)info.dwScale / (uint64_t)info.dwRate;
99  stream->chunck_size = stream->chunck_samples * wvfmt.nChannels * wvfmt.wBitsPerSample / 8;
100 
101  st->codec->codec_tag = wvfmt.wFormatTag;
102  st->codec->codec_id = ff_wav_codec_get_id(wvfmt.wFormatTag, st->codec->bits_per_coded_sample);
103  }
104  else if (stream->info.fccType == streamtypeVIDEO)
105  {
106  BITMAPINFO imgfmt;
107  LONG struct_size = sizeof(BITMAPINFO);
108 
109  stream->chunck_size = stream->info.dwSampleSize;
110  stream->chunck_samples = 1;
111 
112  if (AVIStreamReadFormat(stream->handle, 0, &imgfmt, &struct_size) != S_OK)
113  continue;
114 
115  st = avformat_new_stream(s, NULL);
116  st->id = id;
118  st->r_frame_rate.num = stream->info.dwRate;
119  st->r_frame_rate.den = stream->info.dwScale;
120 
121  st->codec->width = imgfmt.bmiHeader.biWidth;
122  st->codec->height = imgfmt.bmiHeader.biHeight;
123 
124  st->codec->bits_per_coded_sample = imgfmt.bmiHeader.biBitCount;
125  st->codec->bit_rate = (uint64_t)stream->info.dwSampleSize * (uint64_t)stream->info.dwRate * 8 / (uint64_t)stream->info.dwScale;
126  st->codec->codec_tag = imgfmt.bmiHeader.biCompression;
127  st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, imgfmt.bmiHeader.biCompression);
128 
129  st->duration = stream->info.dwLength;
130  }
131  else
132  {
133  AVIStreamRelease(stream->handle);
134  continue;
135  }
136 
137  avs->nb_streams++;
138 
139  st->codec->stream_codec_tag = stream->info.fccHandler;
140 
141  avpriv_set_pts_info(st, 64, info.dwScale, info.dwRate);
142  st->start_time = stream->info.dwStart;
143  }
144  }
145  }
146 
147  return 0;
148 }
149 
151 {
152  AVISynthContext *avs = s->priv_data;
153  HRESULT res;
154  AVISynthStream *stream;
155  int stream_id = avs->next_stream;
156  LONG read_size;
157 
158  // handle interleaving manually...
159  stream = &avs->streams[stream_id];
160 
161  if (stream->read >= stream->info.dwLength)
162  return AVERROR(EIO);
163 
164  if (av_new_packet(pkt, stream->chunck_size))
165  return AVERROR(EIO);
166  pkt->stream_index = stream_id;
167  pkt->pts = avs->streams[stream_id].read / avs->streams[stream_id].chunck_samples;
168 
169  res = AVIStreamRead(stream->handle, stream->read, stream->chunck_samples, pkt->data, stream->chunck_size, &read_size, NULL);
170 
171  pkt->pts = stream->read;
172  pkt->size = read_size;
173 
174  stream->read += stream->chunck_samples;
175 
176  // prepare for the next stream to read
177  do {
178  avs->next_stream = (avs->next_stream+1) % avs->nb_streams;
179  } while (avs->next_stream != stream_id && s->streams[avs->next_stream]->discard >= AVDISCARD_ALL);
180 
181  return (res == S_OK) ? pkt->size : -1;
182 }
183 
185 {
186  AVISynthContext *avs = s->priv_data;
187  int i;
188 
189  for (i=0;i<avs->nb_streams;i++)
190  {
191  AVIStreamRelease(avs->streams[i].handle);
192  }
193 
194  av_free(avs->streams);
195  AVIFileRelease(avs->file);
196  AVIFileExit();
197  return 0;
198 }
199 
200 static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
201 {
202  AVISynthContext *avs = s->priv_data;
203  int stream_id;
204 
205  for (stream_id = 0; stream_id < avs->nb_streams; stream_id++)
206  {
207  avs->streams[stream_id].read = pts * avs->streams[stream_id].chunck_samples;
208  }
209 
210  return 0;
211 }
212 
214  .name = "avs",
215  .long_name = NULL_IF_CONFIG_SMALL("AVISynth"),
216  .priv_data_size = sizeof(AVISynthContext),
221  .extensions = "avs",
222 };