2001-02-24 20:28:24 +00:00
|
|
|
|
2001-04-23 03:42:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-10-29 00:55:15 +00:00
|
|
|
#include <string.h>
|
2002-04-12 10:40:38 +00:00
|
|
|
#include <sys/types.h>
|
2001-10-29 00:55:15 +00:00
|
|
|
|
2001-10-30 17:03:11 +00:00
|
|
|
#include "config.h"
|
2002-08-29 21:37:45 +00:00
|
|
|
#include "../version.h"
|
2001-10-30 17:03:11 +00:00
|
|
|
|
2001-10-29 00:55:15 +00:00
|
|
|
//#include "stream.h"
|
|
|
|
//#include "demuxer.h"
|
|
|
|
//#include "stheader.h"
|
2001-04-23 03:42:17 +00:00
|
|
|
|
|
|
|
#include "wine/mmreg.h"
|
|
|
|
#include "wine/avifmt.h"
|
|
|
|
#include "wine/vfw.h"
|
2002-08-05 11:26:26 +00:00
|
|
|
#include "bswap.h"
|
2001-04-23 03:42:17 +00:00
|
|
|
|
2001-10-29 00:55:15 +00:00
|
|
|
#include "aviwrite.h"
|
2002-08-05 11:26:26 +00:00
|
|
|
#include "aviheader.h"
|
2001-04-23 03:42:17 +00:00
|
|
|
|
2002-08-29 20:50:49 +00:00
|
|
|
extern char *info_name;
|
|
|
|
extern char *info_artist;
|
|
|
|
extern char *info_genre;
|
|
|
|
extern char *info_subject;
|
|
|
|
extern char *info_copyright;
|
|
|
|
extern char *info_sourceform;
|
|
|
|
extern char *info_comment;
|
|
|
|
|
2001-10-29 00:55:15 +00:00
|
|
|
aviwrite_stream_t* aviwrite_new_stream(aviwrite_t *muxer,int type){
|
|
|
|
aviwrite_stream_t* s;
|
|
|
|
if(muxer->avih.dwStreams>=AVIWRITE_MAX_STREAMS){
|
|
|
|
printf("Too many streams! increase AVIWRITE_MAX_STREAMS !\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
s=malloc(sizeof(aviwrite_stream_t));
|
|
|
|
memset(s,0,sizeof(aviwrite_stream_t));
|
|
|
|
if(!s) return NULL; // no mem!?
|
|
|
|
muxer->streams[muxer->avih.dwStreams]=s;
|
|
|
|
s->type=type;
|
|
|
|
s->id=muxer->avih.dwStreams;
|
|
|
|
s->timer=0.0;
|
2001-11-03 20:57:13 +00:00
|
|
|
s->size=0;
|
2001-10-29 00:55:15 +00:00
|
|
|
switch(type){
|
|
|
|
case AVIWRITE_TYPE_VIDEO:
|
|
|
|
s->ckid=mmioFOURCC(('0'+s->id/10),('0'+(s->id%10)),'d','c');
|
|
|
|
s->h.fccType=streamtypeVIDEO;
|
|
|
|
if(!muxer->def_v) muxer->def_v=s;
|
|
|
|
break;
|
|
|
|
case AVIWRITE_TYPE_AUDIO:
|
|
|
|
s->ckid=mmioFOURCC(('0'+s->id/10),('0'+(s->id%10)),'w','b');
|
|
|
|
s->h.fccType=streamtypeAUDIO;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("WarninG! unknown stream type: %d\n",type);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
muxer->avih.dwStreams++;
|
|
|
|
return s;
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-10-29 00:55:15 +00:00
|
|
|
aviwrite_t* aviwrite_new_muxer(){
|
|
|
|
aviwrite_t* muxer=malloc(sizeof(aviwrite_t));
|
|
|
|
memset(muxer,0,sizeof(aviwrite_t));
|
|
|
|
return muxer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_avi_chunk(FILE *f,unsigned int id,int len,void* data){
|
2002-08-05 11:26:26 +00:00
|
|
|
int le_len = le2me_32(len);
|
|
|
|
int le_id = le2me_32(id);
|
|
|
|
fwrite(&le_id,4,1,f);
|
|
|
|
fwrite(&le_len,4,1,f);
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
if(len>0){
|
|
|
|
if(data){
|
|
|
|
// DATA
|
|
|
|
fwrite(data,len,1,f);
|
|
|
|
if(len&1){ // padding
|
|
|
|
unsigned char zerobyte=0;
|
|
|
|
fwrite(&zerobyte,1,1,f);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// JUNK
|
|
|
|
char *avi_junk_data="[= MPlayer junk data! =]";
|
|
|
|
if(len&1) ++len; // padding
|
|
|
|
while(len>0){
|
|
|
|
int l=strlen(avi_junk_data);
|
|
|
|
if(l>len) l=len;
|
|
|
|
fwrite(avi_junk_data,l,1,f);
|
|
|
|
len-=l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-29 00:55:15 +00:00
|
|
|
void aviwrite_write_chunk(aviwrite_t *muxer,aviwrite_stream_t *s, FILE *f,int len,unsigned int flags){
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-10-29 00:55:15 +00:00
|
|
|
// add to the index:
|
|
|
|
if(muxer->idx_pos>=muxer->idx_size){
|
|
|
|
muxer->idx_size+=256; // 4kB
|
|
|
|
muxer->idx=realloc(muxer->idx,16*muxer->idx_size);
|
|
|
|
}
|
|
|
|
muxer->idx[muxer->idx_pos].ckid=s->ckid;
|
|
|
|
muxer->idx[muxer->idx_pos].dwFlags=flags; // keyframe?
|
|
|
|
muxer->idx[muxer->idx_pos].dwChunkOffset=ftell(f)-(muxer->movi_start-4);
|
|
|
|
muxer->idx[muxer->idx_pos].dwChunkLength=len;
|
|
|
|
++muxer->idx_pos;
|
|
|
|
|
|
|
|
// write out the chunk:
|
2002-08-05 11:26:26 +00:00
|
|
|
write_avi_chunk(f,s->ckid,len,s->buffer); /* unsigned char */
|
|
|
|
|
2001-10-29 00:55:15 +00:00
|
|
|
// alter counters:
|
|
|
|
if(s->h.dwSampleSize){
|
|
|
|
// CBR
|
|
|
|
s->h.dwLength+=len/s->h.dwSampleSize;
|
|
|
|
if(len%s->h.dwSampleSize) printf("Warning! len isn't divisable by samplesize!\n");
|
|
|
|
} else {
|
|
|
|
// VBR
|
|
|
|
s->h.dwLength++;
|
|
|
|
}
|
|
|
|
s->timer=(double)s->h.dwLength*s->h.dwScale/s->h.dwRate;
|
2001-11-03 20:57:13 +00:00
|
|
|
s->size+=len;
|
2001-10-29 00:55:15 +00:00
|
|
|
if(len>s->h.dwSuggestedBufferSize) s->h.dwSuggestedBufferSize=len;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_avi_list(FILE *f,unsigned int id,int len){
|
2001-02-24 20:28:24 +00:00
|
|
|
unsigned int list_id=FOURCC_LIST;
|
2002-08-05 11:26:26 +00:00
|
|
|
int le_len;
|
|
|
|
int le_id;
|
2001-02-24 20:28:24 +00:00
|
|
|
len+=4; // list fix
|
2002-08-05 11:26:26 +00:00
|
|
|
list_id = le2me_32(list_id);
|
|
|
|
le_len = le2me_32(len);
|
|
|
|
le_id = le2me_32(id);
|
2001-02-24 20:28:24 +00:00
|
|
|
fwrite(&list_id,4,1,f);
|
2002-08-05 11:26:26 +00:00
|
|
|
fwrite(&le_len,4,1,f);
|
|
|
|
fwrite(&le_id,4,1,f);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2001-11-02 17:43:05 +00:00
|
|
|
// muxer->streams[i]->wf->cbSize
|
|
|
|
#define WFSIZE(wf) (sizeof(WAVEFORMATEX)+(((wf)->cbSize)?((wf)->cbSize-2):0))
|
|
|
|
|
2001-10-29 00:55:15 +00:00
|
|
|
void aviwrite_write_header(aviwrite_t *muxer,FILE *f){
|
2001-02-24 20:28:24 +00:00
|
|
|
unsigned int riff[3];
|
2001-10-29 00:55:15 +00:00
|
|
|
int i;
|
|
|
|
unsigned int hdrsize;
|
2002-08-29 20:50:49 +00:00
|
|
|
aviwrite_info_t info[16];
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
// RIFF header:
|
2002-08-05 11:26:26 +00:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
/* FIXME: updating the header on big-endian causes the video
|
|
|
|
* to be unreadable ("AVI_NI: No video stream found!").
|
|
|
|
* Just don't update it (no seeking, not playable with WMP,
|
|
|
|
* but better than nothing)
|
|
|
|
*/
|
|
|
|
if(muxer->file_end != 0)
|
|
|
|
return;
|
|
|
|
#endif
|
2001-02-24 20:28:24 +00:00
|
|
|
riff[0]=mmioFOURCC('R','I','F','F');
|
2002-08-29 20:21:59 +00:00
|
|
|
riff[1]=muxer->file_end-2*sizeof(unsigned int); // filesize
|
2001-02-24 20:28:24 +00:00
|
|
|
riff[2]=formtypeAVI; // 'AVI '
|
2002-08-05 11:26:26 +00:00
|
|
|
riff[0]=le2me_32(riff[0]);
|
|
|
|
riff[1]=le2me_32(riff[1]);
|
|
|
|
riff[2]=le2me_32(riff[2]);
|
2001-02-24 20:28:24 +00:00
|
|
|
fwrite(&riff,12,1,f);
|
2001-10-29 00:55:15 +00:00
|
|
|
// update AVI header:
|
|
|
|
if(muxer->def_v){
|
|
|
|
muxer->avih.dwMicroSecPerFrame=1000000.0*muxer->def_v->h.dwScale/muxer->def_v->h.dwRate;
|
2001-11-02 17:43:05 +00:00
|
|
|
// muxer->avih.dwMaxBytesPerSec=1000000; // dummy!!!!! FIXME
|
|
|
|
// muxer->avih.dwPaddingGranularity=2; // ???
|
2001-10-29 00:55:15 +00:00
|
|
|
muxer->avih.dwFlags|=AVIF_ISINTERLEAVED|AVIF_TRUSTCKTYPE;
|
|
|
|
muxer->avih.dwTotalFrames=muxer->def_v->h.dwLength;
|
2001-11-02 17:43:05 +00:00
|
|
|
// muxer->avih.dwSuggestedBufferSize=muxer->def_v->h.dwSuggestedBufferSize;
|
2001-10-29 00:55:15 +00:00
|
|
|
muxer->avih.dwWidth=muxer->def_v->bih->biWidth;
|
|
|
|
muxer->avih.dwHeight=muxer->def_v->bih->biHeight;
|
|
|
|
}
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
// AVI header:
|
2001-10-29 00:55:15 +00:00
|
|
|
hdrsize=sizeof(muxer->avih)+8;
|
|
|
|
// calc total header size:
|
|
|
|
for(i=0;i<muxer->avih.dwStreams;i++){
|
|
|
|
hdrsize+=12; // LIST
|
|
|
|
hdrsize+=sizeof(muxer->streams[i]->h)+8; // strh
|
|
|
|
switch(muxer->streams[i]->type){
|
|
|
|
case AVIWRITE_TYPE_VIDEO:
|
|
|
|
hdrsize+=muxer->streams[i]->bih->biSize+8; // strf
|
|
|
|
break;
|
|
|
|
case AVIWRITE_TYPE_AUDIO:
|
2001-11-02 17:43:05 +00:00
|
|
|
hdrsize+=WFSIZE(muxer->streams[i]->wf)+8; // strf
|
2001-10-29 00:55:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write_avi_list(f,listtypeAVIHEADER,hdrsize);
|
2002-08-05 11:26:26 +00:00
|
|
|
|
|
|
|
le2me_MainAVIHeader(&muxer->avih);
|
|
|
|
write_avi_chunk(f,ckidAVIMAINHDR,sizeof(muxer->avih),&muxer->avih); /* MainAVIHeader */
|
|
|
|
le2me_MainAVIHeader(&muxer->avih);
|
2001-10-29 00:55:15 +00:00
|
|
|
|
|
|
|
// stream headers:
|
|
|
|
for(i=0;i<muxer->avih.dwStreams;i++){
|
|
|
|
hdrsize=sizeof(muxer->streams[i]->h)+8; // strh
|
|
|
|
switch(muxer->streams[i]->type){
|
|
|
|
case AVIWRITE_TYPE_VIDEO:
|
|
|
|
hdrsize+=muxer->streams[i]->bih->biSize+8; // strf
|
|
|
|
break;
|
|
|
|
case AVIWRITE_TYPE_AUDIO:
|
2001-11-02 17:43:05 +00:00
|
|
|
hdrsize+=WFSIZE(muxer->streams[i]->wf)+8; // strf
|
2001-10-29 00:55:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
write_avi_list(f,listtypeSTREAMHEADER,hdrsize);
|
2002-08-05 11:26:26 +00:00
|
|
|
le2me_AVIStreamHeader(&muxer->streams[i]->h);
|
|
|
|
write_avi_chunk(f,ckidSTREAMHEADER,sizeof(muxer->streams[i]->h),&muxer->streams[i]->h); /* AVISTreamHeader */ // strh
|
|
|
|
le2me_AVIStreamHeader(&muxer->streams[i]->h);
|
|
|
|
|
2001-10-29 00:55:15 +00:00
|
|
|
switch(muxer->streams[i]->type){
|
|
|
|
case AVIWRITE_TYPE_VIDEO:
|
2002-08-05 11:26:26 +00:00
|
|
|
{
|
|
|
|
int biSize=muxer->streams[i]->bih->biSize;
|
|
|
|
le2me_BITMAPINFOHEADER(muxer->streams[i]->bih);
|
|
|
|
write_avi_chunk(f,ckidSTREAMFORMAT,biSize,muxer->streams[i]->bih); /* BITMAPINFOHEADER */
|
|
|
|
le2me_BITMAPINFOHEADER(muxer->streams[i]->bih);
|
|
|
|
}
|
2001-10-29 00:55:15 +00:00
|
|
|
break;
|
|
|
|
case AVIWRITE_TYPE_AUDIO:
|
2002-08-05 11:26:26 +00:00
|
|
|
{
|
|
|
|
int wfsize = WFSIZE(muxer->streams[i]->wf);
|
|
|
|
le2me_WAVEFORMATEX(muxer->streams[i]->wf);
|
|
|
|
write_avi_chunk(f,ckidSTREAMFORMAT,wfsize,muxer->streams[i]->wf); /* WAVEFORMATEX */
|
|
|
|
le2me_WAVEFORMATEX(muxer->streams[i]->wf);
|
|
|
|
}
|
2001-10-29 00:55:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-29 20:50:49 +00:00
|
|
|
// ============= INFO ===============
|
|
|
|
// always include software info
|
|
|
|
info[0].id=mmioFOURCC('I','S','F','T'); // Software:
|
|
|
|
info[0].text="MEncoder " VERSION;
|
|
|
|
// include any optional strings
|
|
|
|
i=1;
|
|
|
|
if(info_name!=NULL){
|
|
|
|
info[i].id=mmioFOURCC('I','N','A','M'); // Name:
|
|
|
|
info[i++].text=info_name;
|
|
|
|
}
|
|
|
|
if(info_artist!=NULL){
|
|
|
|
info[i].id=mmioFOURCC('I','A','R','T'); // Artist:
|
|
|
|
info[i++].text=info_artist;
|
|
|
|
}
|
|
|
|
if(info_genre!=NULL){
|
|
|
|
info[i].id=mmioFOURCC('I','G','N','R'); // Genre:
|
|
|
|
info[i++].text=info_genre;
|
|
|
|
}
|
|
|
|
if(info_subject!=NULL){
|
|
|
|
info[i].id=mmioFOURCC('I','S','B','J'); // Subject:
|
|
|
|
info[i++].text=info_subject;
|
|
|
|
}
|
|
|
|
if(info_copyright!=NULL){
|
|
|
|
info[i].id=mmioFOURCC('I','C','O','P'); // Copyright:
|
|
|
|
info[i++].text=info_copyright;
|
|
|
|
}
|
|
|
|
if(info_sourceform!=NULL){
|
|
|
|
info[i].id=mmioFOURCC('I','S','R','F'); // Source Form:
|
|
|
|
info[i++].text=info_sourceform;
|
|
|
|
}
|
|
|
|
if(info_comment!=NULL){
|
|
|
|
info[i].id=mmioFOURCC('I','C','M','T'); // Comment:
|
|
|
|
info[i++].text=info_comment;
|
|
|
|
}
|
|
|
|
info[i].id=0;
|
|
|
|
|
|
|
|
hdrsize=0;
|
|
|
|
// calc info size:
|
|
|
|
for(i=0;info[i].id!=0;i++) if(info[i].text){
|
|
|
|
size_t sz=strlen(info[i].text)+1;
|
|
|
|
hdrsize+=sz+8+sz%2;
|
|
|
|
}
|
|
|
|
// write infos:
|
|
|
|
if (hdrsize!=0){
|
|
|
|
write_avi_list(f,mmioFOURCC('I','N','F','O'),hdrsize);
|
|
|
|
for(i=0;info[i].id!=0;i++) if(info[i].text){
|
|
|
|
write_avi_chunk(f,info[i].id,strlen(info[i].text)+1,info[i].text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-29 00:55:15 +00:00
|
|
|
// JUNK:
|
2002-08-05 11:26:26 +00:00
|
|
|
write_avi_chunk(f,ckidAVIPADDING,2048-(ftell(f)&2047)-8,NULL); /* junk */
|
2001-02-24 20:28:24 +00:00
|
|
|
// 'movi' header:
|
2001-10-29 00:55:15 +00:00
|
|
|
write_avi_list(f,listtypeAVIMOVIE,muxer->movi_end-ftell(f)-12);
|
|
|
|
muxer->movi_start=ftell(f);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-29 00:55:15 +00:00
|
|
|
void aviwrite_write_index(aviwrite_t *muxer,FILE *f){
|
|
|
|
muxer->movi_end=ftell(f);
|
|
|
|
if(muxer->idx && muxer->idx_pos>0){
|
2002-08-05 11:26:26 +00:00
|
|
|
int i;
|
2001-10-29 00:55:15 +00:00
|
|
|
// fixup index entries:
|
|
|
|
// for(i=0;i<muxer->idx_pos;i++) muxer->idx[i].dwChunkOffset-=muxer->movi_start-4;
|
|
|
|
// write index chunk:
|
2002-08-05 11:26:26 +00:00
|
|
|
for (i=0; i<muxer->idx_pos; i++) le2me_AVIINDEXENTRY((&muxer->idx[i]));
|
|
|
|
write_avi_chunk(f,ckidAVINEWINDEX,16*muxer->idx_pos,muxer->idx); /* AVIINDEXENTRY */
|
|
|
|
for (i=0; i<muxer->idx_pos; i++) le2me_AVIINDEXENTRY((&muxer->idx[i]));
|
2001-10-29 00:55:15 +00:00
|
|
|
muxer->avih.dwFlags|=AVIF_HASINDEX;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2001-10-29 00:55:15 +00:00
|
|
|
muxer->file_end=ftell(f);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|