📄 avidecaps.cpp
字号:
/***************************************************************************************
*This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* The GPL can be found at: http://www.gnu.org/copyleft/gpl.html *
* *
* *
****************************************************************************************
* Authors: *
* Marc Dukette *
**************************************************************************************/
#include "AviDecaps.h"
int CacheSize;
videoinfo thisvid;
typedef int (CALLBACK* pInputMediaRead)(char *data, unsigned int size);
typedef int (CALLBACK* pInputMediaSeek)(int size, unsigned int method);
typedef int (CALLBACK* pInputMediaOpen)(LPTSTR lpFilename, int mode, int type, int reserve, int maxsize);
typedef void (CALLBACK* pInputMediaClose)();
pInputMediaRead InputMediaRead;
pInputMediaSeek InputMediaSeek;
pInputMediaOpen InputMediaOpen;
pInputMediaClose InputMediaClose;
HMODULE hDLL;
/* Convert a string of 4
* or 2 bytes to a number,
* also working on big
* endian machines
*
*/
unsigned long str2ulong(char *str)
{
unsigned long result;
result = ( ((unsigned long) str[0] & 0xFF) | ( ((unsigned long)str[1] &0xFF) << 8) |
(((unsigned long) str[2] & 0xFF) << 16) | ( ((unsigned long) str[3] & 0xFF) << 24) );
return result;
}
static unsigned long str2ushort(char *str)
{
return ( str[0] | (str[1]<<8) );
}
/*
* Reads the first 12 bytes of
* the file, and returns TRUE
* if the file is an AVI.
*
*/
int AVIDecaps_IsAVI()
{
char data[24];
if( InputMediaRead(data, 12) != 12 ) {
return 0;
}
if( strncmp(data, "RIFF", 4) !=0 ||
strncmp(data + 8, "AVI ", 4) !=0 ) {
return 0;
}
return 1;
return 0;
}
/*
* Fill the class with info from headers
* and reconstruct an index if wanted.
*
*/
int AVIDecaps_FillHeader(int getIndex)
{
unsigned long i, n, rate;
long scale, idx_type;
char *hdrl_data;
long hdrl_len = 0;
long nvi=0, nai=0, ioff;
long tot;
int lasttag = 0;
int vids_strh_seen = 0;
int auds_strh_seen = 0;
int auds_strf_seen = 0;
int num_stream = 0;
char data[256];
int a_rate=0;
/* go through the AVI file and
* extract the header list,
* the start position of the 'movi'
* list and an optionally
* present idx1 tag
*
*/
hdrl_data = 0;
while(1)
{
if( InputMediaRead(data, 8) != 8 )
break;
/*
* we assume it's EOF
*
*/
n = str2ulong(data + 4);
n = PAD_EVEN(n);
if(strncmp(data, "LIST", 4) == 0)
{
if( InputMediaRead(data, 4) != 4 ) {
return 0;
}
n -= 4;
if(strncmp(data, "hdrl", 4) == 0)
{
hdrl_len = n;
if (hdrl_data) free(hdrl_data);
hdrl_data = (char *) malloc(n);
if(hdrl_data == 0) {
return 0;
}
if( InputMediaRead(hdrl_data, n) != n ) {
return 0;
}
}
else if(strncmp(data, "movi", 4) == 0)
{
thisvid.movi_start = InputMediaSeek(0, INPUT_SEEK_CUR);
InputMediaSeek(n, INPUT_SEEK_CUR);
}
else
InputMediaSeek(n, INPUT_SEEK_CUR);
}
else if(strncmp(data,"idx1",4) == 0)
{
thisvid.n_idx = thisvid.max_idx = n/16;
if (thisvid.idx) free(thisvid.idx);
thisvid.video_index = (video_index_entry *) malloc(100000*sizeof(video_index_entry));
if (!thisvid.video_index)
{
return 0;
}
thisvid.audio_index = (audio_index_entry *) malloc(100000*sizeof(audio_index_entry));
if (!thisvid.audio_index)
{
free(thisvid.video_index);
thisvid.video_index=0;
return 0;
}
tot=0;
unsigned long read;
ioff = thisvid.movi_start+4;
for (i=0;i<(thisvid.n_idx/1000)+1;i++)
{
char temp[16000];
int toread=16000;
int i2;
if (i==(thisvid.n_idx/1000))
toread=(thisvid.n_idx%1000)*16;
if( (read = InputMediaRead((char *) temp, toread)) != toread) {
break;
}
for (i2=0;i2<read/16;i2++)
{
if(strncmp(temp+(16*i2)+2, "db", 1) == 0)
{
if(nvi%100000==0)
{
void* ptr = realloc((void *)thisvid.video_index,
(nvi+100000)*sizeof(video_index_entry));
if (!ptr)
{
free(thisvid.video_index);
thisvid.video_index=0;
free(thisvid.audio_index);
thisvid.audio_index=0;
return 0;
}
thisvid.video_index = (video_index_entry*)ptr;
}
thisvid.video_index[nvi].flags = (BYTE) (str2ulong(temp+(16*i2) +4) & AVIIF_KEYFRAME)>1;
thisvid.video_index[nvi].pos = str2ulong(temp+(16*i2)+ 8)+ioff;
thisvid.video_index[nvi].len = str2ulong(temp+(16*i2)+12);
nvi++;
}
else if(strncmp(temp+(16*i2)+2, "wb", 2) == 0)
{
if(nai%100000==0)
{
void* ptr = realloc((void *)thisvid.audio_index,
(nai+100000)*sizeof(audio_index_entry));
if (!ptr)
{
free(thisvid.video_index);
thisvid.video_index=0;
free(thisvid.audio_index);
thisvid.audio_index=0;
return 0;
}
thisvid.audio_index = (audio_index_entry*)ptr;
}
thisvid.audio_index[nai].pos = str2ulong(temp+(16*i2)+ 8)+ioff;
thisvid.audio_index[nai].len = str2ulong(temp+(16*i2)+12);
thisvid.audio_index[nai].tot = tot;
tot += thisvid.audio_index[nai].len;
nai++;
}
}
}
void* ptr = realloc((void *)thisvid.video_index,
(nvi)*sizeof(video_index_entry));
thisvid.video_index = (video_index_entry*)ptr;
ptr = realloc((void *)thisvid.audio_index,
(nai)*sizeof(audio_index_entry));
thisvid.audio_index = (audio_index_entry*)ptr;
}
else {
InputMediaSeek(n, INPUT_SEEK_CUR);
}
}
/*
* interpret the header list
*
*/
for(i=0; i < hdrl_len; )
{
/*
* list tags are completly ignored
*
*/
if(strncmp(hdrl_data + i, "LIST", 4)==0)
{
i+= 12;
continue;
}
n = str2ulong(hdrl_data+i+4);
n = PAD_EVEN(n);
/*
* interpret the tag and its args
*
*/
if(strncmp(hdrl_data + i, "strh", 4)==0)
{
i += 8;
if(strncmp(hdrl_data + i, "vids", 4) == 0 && !vids_strh_seen)
{
memcpy(thisvid.compressor,hdrl_data+i+4,4);
thisvid.compressor[4] = 0;
scale = str2ulong(hdrl_data+i+20);
rate = str2ulong(hdrl_data+i+24);
if(scale!=0)
thisvid.fps = (double)rate/(double)scale;
thisvid.video_frames = str2ulong(hdrl_data+i+32);
thisvid.video_strn = num_stream;
vids_strh_seen = 1;
lasttag = 1;
}
else if (strncmp (hdrl_data + i, "auds", 4) == 0 && ! auds_strh_seen)
{
thisvid.audio_bytes = str2ulong(hdrl_data + i + 32); //*AVIDecaps_SampleSize();
thisvid.audio_strn = num_stream;
auds_strh_seen = 1;
lasttag = 2;
}
else
lasttag = 0;
num_stream++;
}
else if(strncmp(hdrl_data + i, "strf", 4)==0)
{
i += 8;
if(lasttag == 1)
{
thisvid.width = str2ulong(hdrl_data+i+4);
thisvid.height = str2ulong(hdrl_data+i+8);
}
else if(lasttag == 2)
{
WAVEFORMATEX wfe;
memcpy(&wfe,
hdrl_data + i,
sizeof(WAVEFORMATEX));
thisvid.a_fmt = str2ushort(hdrl_data + i );
}
lasttag = 0;
}
else
{
i += 8;
lasttag = 0;
}
i += n;
}
if (hdrl_data)
free(hdrl_data);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -