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  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", 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", 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;
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;
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 
137  st->duration = stream->info.dwLength;
138  }
139  else
140  {
141  AVIStreamRelease(stream->handle);
142  continue;
143  }
144 
145  avs->nb_streams++;
146 
147  st->codec->stream_codec_tag = stream->info.fccHandler;
148 
149  avpriv_set_pts_info(st, 64, info.dwScale, info.dwRate);
150  st->start_time = stream->info.dwStart;
151  }
152  }
153  }
154 
155  return 0;
156 }
157 
159 {
160  AVISynthContext *avs = s->priv_data;
161  HRESULT res;
162  AVISynthStream *stream;
163  int stream_id = avs->next_stream;
164  LONG read_size;
165 
166  // handle interleaving manually...
167  stream = &avs->streams[stream_id];
168 
169  if (stream->read >= stream->info.dwLength)
170  return AVERROR(EIO);
171 
172  if (av_new_packet(pkt, stream->chunck_size))
173  return AVERROR(EIO);
174  pkt->stream_index = stream_id;
175  pkt->pts = avs->streams[stream_id].read / avs->streams[stream_id].chunck_samples;
176 
177  res = AVIStreamRead(stream->handle, stream->read, stream->chunck_samples, pkt->data, stream->chunck_size, &read_size, NULL);
178 
179  pkt->pts = stream->read;
180  pkt->size = read_size;
181 
182  stream->read += stream->chunck_samples;
183 
184  // prepare for the next stream to read
185  do {
186  avs->next_stream = (avs->next_stream+1) % avs->nb_streams;
187  } while (avs->next_stream != stream_id && s->streams[avs->next_stream]->discard >= AVDISCARD_ALL);
188 
189  return (res == S_OK) ? pkt->size : -1;
190 }
191 
193 {
194  AVISynthContext *avs = s->priv_data;
195  int i;
196 
197  for (i=0;i<avs->nb_streams;i++)
198  {
199  AVIStreamRelease(avs->streams[i].handle);
200  }
201 
202  av_free(avs->streams);
203  AVIFileRelease(avs->file);
204  AVIFileExit();
205  return 0;
206 }
207 
208 static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
209 {
210  AVISynthContext *avs = s->priv_data;
211  int stream_id;
212 
213  for (stream_id = 0; stream_id < avs->nb_streams; stream_id++)
214  {
215  avs->streams[stream_id].read = pts * avs->streams[stream_id].chunck_samples;
216  }
217 
218  return 0;
219 }
220 
222  .name = "avs",
223  .long_name = NULL_IF_CONFIG_SMALL("AVISynth"),
224  .priv_data_size = sizeof(AVISynthContext),
229  .extensions = "avs",
230 };
unsigned int stream_codec_tag
fourcc from the AVI stream header (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + ...
Definition: avcodec.h:1373
DWORD read
Definition: avisynth.c:32
static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
Definition: avisynth.c:208
enum AVCodecID id
Definition: mxfenc.c:85
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:2126
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:3283
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:916
discard all
Definition: avcodec.h:536
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2141
LONG chunck_samples
Definition: avisynth.c:34
Format I/O context.
Definition: avformat.h:828
int id
Format-specific stream ID.
Definition: avformat.h:629
AVStream ** streams
Definition: avformat.h:876
uint8_t * data
Definition: avcodec.h:915
static int flags
Definition: log.c:42
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2704
static int avisynth_read_header(AVFormatContext *s)
Definition: avisynth.c:44
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:56
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2736
AVInputFormat ff_avisynth_demuxer
Definition: avisynth.c:221
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:704
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:641
static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: avisynth.c:158
AVISTREAMINFO info
Definition: avisynth.c:31
AVISynthStream * streams
Definition: avisynth.c:39
PAVISTREAM handle
Definition: avisynth.c:30
int bit_rate
the average bitrate
Definition: avcodec.h:1404
char filename[1024]
input or output filename
Definition: avformat.h:878
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:31
int width
picture width / height.
Definition: avcodec.h:1508
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:536
Stream structure.
Definition: avformat.h:622
NULL
Definition: eval.c:52
enum AVMediaType codec_type
Definition: avcodec.h:1347
enum AVCodecID codec_id
Definition: avcodec.h:1350
int sample_rate
samples per second
Definition: avcodec.h:2104
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1365
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:684
Main libavformat public API header.
enum AVCodecID ff_wav_codec_get_id(unsigned int tag, int bps)
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:677
LONG chunck_size
Definition: avisynth.c:33
int den
denominator
Definition: rational.h:45
static int avisynth_read_close(AVFormatContext *s)
Definition: avisynth.c:192
int channels
number of audio channels
Definition: avcodec.h:2105
void * priv_data
Format private data.
Definition: avformat.h:848
PAVIFILE file
Definition: avisynth.c:38
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:455
int stream_index
Definition: avcodec.h:917
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:690
This structure stores compressed data.
Definition: avcodec.h:898
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:158
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:908