Mercurial > mplayer.hg
annotate libmpdemux/aviwrite.c @ 5214:dd79075bbd98
added a BMP file demuxer...yeah, that's right, a static image BMP file
demuxer; what, is that so weird?...:)
author | melanson |
---|---|
date | Wed, 20 Mar 2002 05:15:53 +0000 |
parents | fd279f14b9ab |
children | 8cd761968f35 |
rev | line source |
---|---|
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
1
diff
changeset
|
1 |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
1
diff
changeset
|
2 #include <stdio.h> |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
1
diff
changeset
|
3 #include <stdlib.h> |
2529 | 4 #include <string.h> |
5 | |
2555
66837325b929
config.h cleanup, few things added to steram/demuxer headers
arpi
parents:
2529
diff
changeset
|
6 #include "config.h" |
66837325b929
config.h cleanup, few things added to steram/demuxer headers
arpi
parents:
2529
diff
changeset
|
7 |
2529 | 8 //#include "stream.h" |
9 //#include "demuxer.h" | |
10 //#include "stheader.h" | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
1
diff
changeset
|
11 |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
1
diff
changeset
|
12 #include "wine/mmreg.h" |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
1
diff
changeset
|
13 #include "wine/avifmt.h" |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
1
diff
changeset
|
14 #include "wine/vfw.h" |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
1
diff
changeset
|
15 |
2529 | 16 #include "aviwrite.h" |
1 | 17 |
2529 | 18 aviwrite_stream_t* aviwrite_new_stream(aviwrite_t *muxer,int type){ |
19 aviwrite_stream_t* s; | |
20 if(muxer->avih.dwStreams>=AVIWRITE_MAX_STREAMS){ | |
21 printf("Too many streams! increase AVIWRITE_MAX_STREAMS !\n"); | |
22 return NULL; | |
23 } | |
24 s=malloc(sizeof(aviwrite_stream_t)); | |
25 memset(s,0,sizeof(aviwrite_stream_t)); | |
26 if(!s) return NULL; // no mem!? | |
27 muxer->streams[muxer->avih.dwStreams]=s; | |
28 s->type=type; | |
29 s->id=muxer->avih.dwStreams; | |
30 s->timer=0.0; | |
2652 | 31 s->size=0; |
2529 | 32 switch(type){ |
33 case AVIWRITE_TYPE_VIDEO: | |
34 s->ckid=mmioFOURCC(('0'+s->id/10),('0'+(s->id%10)),'d','c'); | |
35 s->h.fccType=streamtypeVIDEO; | |
36 if(!muxer->def_v) muxer->def_v=s; | |
37 break; | |
38 case AVIWRITE_TYPE_AUDIO: | |
39 s->ckid=mmioFOURCC(('0'+s->id/10),('0'+(s->id%10)),'w','b'); | |
40 s->h.fccType=streamtypeAUDIO; | |
41 break; | |
42 default: | |
43 printf("WarninG! unknown stream type: %d\n",type); | |
44 return NULL; | |
45 } | |
46 muxer->avih.dwStreams++; | |
47 return s; | |
48 } | |
1 | 49 |
2529 | 50 aviwrite_t* aviwrite_new_muxer(){ |
51 aviwrite_t* muxer=malloc(sizeof(aviwrite_t)); | |
52 memset(muxer,0,sizeof(aviwrite_t)); | |
53 return muxer; | |
54 } | |
55 | |
56 static void write_avi_chunk(FILE *f,unsigned int id,int len,void* data){ | |
1 | 57 fwrite(&id,4,1,f); |
58 fwrite(&len,4,1,f); | |
59 if(len>0){ | |
60 if(data){ | |
61 // DATA | |
62 fwrite(data,len,1,f); | |
63 if(len&1){ // padding | |
64 unsigned char zerobyte=0; | |
65 fwrite(&zerobyte,1,1,f); | |
66 } | |
67 } else { | |
68 // JUNK | |
69 char *avi_junk_data="[= MPlayer junk data! =]"; | |
70 if(len&1) ++len; // padding | |
71 while(len>0){ | |
72 int l=strlen(avi_junk_data); | |
73 if(l>len) l=len; | |
74 fwrite(avi_junk_data,l,1,f); | |
75 len-=l; | |
76 } | |
77 } | |
78 } | |
2529 | 79 } |
80 | |
81 void aviwrite_write_chunk(aviwrite_t *muxer,aviwrite_stream_t *s, FILE *f,int len,unsigned int flags){ | |
82 | |
83 // add to the index: | |
84 if(muxer->idx_pos>=muxer->idx_size){ | |
85 muxer->idx_size+=256; // 4kB | |
86 muxer->idx=realloc(muxer->idx,16*muxer->idx_size); | |
87 } | |
88 muxer->idx[muxer->idx_pos].ckid=s->ckid; | |
89 muxer->idx[muxer->idx_pos].dwFlags=flags; // keyframe? | |
90 muxer->idx[muxer->idx_pos].dwChunkOffset=ftell(f)-(muxer->movi_start-4); | |
91 muxer->idx[muxer->idx_pos].dwChunkLength=len; | |
92 ++muxer->idx_pos; | |
93 | |
94 // write out the chunk: | |
95 write_avi_chunk(f,s->ckid,len,s->buffer); | |
96 | |
97 // alter counters: | |
98 if(s->h.dwSampleSize){ | |
99 // CBR | |
100 s->h.dwLength+=len/s->h.dwSampleSize; | |
101 if(len%s->h.dwSampleSize) printf("Warning! len isn't divisable by samplesize!\n"); | |
102 } else { | |
103 // VBR | |
104 s->h.dwLength++; | |
105 } | |
106 s->timer=(double)s->h.dwLength*s->h.dwScale/s->h.dwRate; | |
2652 | 107 s->size+=len; |
2529 | 108 if(len>s->h.dwSuggestedBufferSize) s->h.dwSuggestedBufferSize=len; |
1 | 109 |
110 } | |
111 | |
2529 | 112 static void write_avi_list(FILE *f,unsigned int id,int len){ |
1 | 113 unsigned int list_id=FOURCC_LIST; |
114 len+=4; // list fix | |
115 fwrite(&list_id,4,1,f); | |
116 fwrite(&len,4,1,f); | |
117 fwrite(&id,4,1,f); | |
118 } | |
119 | |
2635
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2555
diff
changeset
|
120 // muxer->streams[i]->wf->cbSize |
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2555
diff
changeset
|
121 #define WFSIZE(wf) (sizeof(WAVEFORMATEX)+(((wf)->cbSize)?((wf)->cbSize-2):0)) |
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2555
diff
changeset
|
122 |
2529 | 123 void aviwrite_write_header(aviwrite_t *muxer,FILE *f){ |
1 | 124 unsigned int riff[3]; |
2529 | 125 int i; |
126 unsigned int hdrsize; | |
1 | 127 // RIFF header: |
128 riff[0]=mmioFOURCC('R','I','F','F'); | |
2529 | 129 riff[1]=muxer->file_end; // filesize |
1 | 130 riff[2]=formtypeAVI; // 'AVI ' |
131 fwrite(&riff,12,1,f); | |
2529 | 132 |
133 // update AVI header: | |
134 if(muxer->def_v){ | |
135 muxer->avih.dwMicroSecPerFrame=1000000.0*muxer->def_v->h.dwScale/muxer->def_v->h.dwRate; | |
2635
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2555
diff
changeset
|
136 // muxer->avih.dwMaxBytesPerSec=1000000; // dummy!!!!! FIXME |
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2555
diff
changeset
|
137 // muxer->avih.dwPaddingGranularity=2; // ??? |
2529 | 138 muxer->avih.dwFlags|=AVIF_ISINTERLEAVED|AVIF_TRUSTCKTYPE; |
139 muxer->avih.dwTotalFrames=muxer->def_v->h.dwLength; | |
2635
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2555
diff
changeset
|
140 // muxer->avih.dwSuggestedBufferSize=muxer->def_v->h.dwSuggestedBufferSize; |
2529 | 141 muxer->avih.dwWidth=muxer->def_v->bih->biWidth; |
142 muxer->avih.dwHeight=muxer->def_v->bih->biHeight; | |
143 } | |
144 | |
1 | 145 // AVI header: |
2529 | 146 hdrsize=sizeof(muxer->avih)+8; |
147 // calc total header size: | |
148 for(i=0;i<muxer->avih.dwStreams;i++){ | |
149 hdrsize+=12; // LIST | |
150 hdrsize+=sizeof(muxer->streams[i]->h)+8; // strh | |
151 switch(muxer->streams[i]->type){ | |
152 case AVIWRITE_TYPE_VIDEO: | |
153 hdrsize+=muxer->streams[i]->bih->biSize+8; // strf | |
154 break; | |
155 case AVIWRITE_TYPE_AUDIO: | |
2635
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2555
diff
changeset
|
156 hdrsize+=WFSIZE(muxer->streams[i]->wf)+8; // strf |
2529 | 157 break; |
158 } | |
159 } | |
160 write_avi_list(f,listtypeAVIHEADER,hdrsize); | |
161 write_avi_chunk(f,ckidAVIMAINHDR,sizeof(muxer->avih),&muxer->avih); | |
162 | |
163 // stream headers: | |
164 for(i=0;i<muxer->avih.dwStreams;i++){ | |
165 hdrsize=sizeof(muxer->streams[i]->h)+8; // strh | |
166 switch(muxer->streams[i]->type){ | |
167 case AVIWRITE_TYPE_VIDEO: | |
168 hdrsize+=muxer->streams[i]->bih->biSize+8; // strf | |
169 break; | |
170 case AVIWRITE_TYPE_AUDIO: | |
2635
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2555
diff
changeset
|
171 hdrsize+=WFSIZE(muxer->streams[i]->wf)+8; // strf |
2529 | 172 break; |
173 } | |
174 write_avi_list(f,listtypeSTREAMHEADER,hdrsize); | |
175 write_avi_chunk(f,ckidSTREAMHEADER,sizeof(muxer->streams[i]->h),&muxer->streams[i]->h); // strh | |
176 switch(muxer->streams[i]->type){ | |
177 case AVIWRITE_TYPE_VIDEO: | |
178 write_avi_chunk(f,ckidSTREAMFORMAT,muxer->streams[i]->bih->biSize,muxer->streams[i]->bih); | |
179 break; | |
180 case AVIWRITE_TYPE_AUDIO: | |
2635
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2555
diff
changeset
|
181 write_avi_chunk(f,ckidSTREAMFORMAT,WFSIZE(muxer->streams[i]->wf),muxer->streams[i]->wf); |
2529 | 182 break; |
183 } | |
184 } | |
185 | |
186 // JUNK: | |
1 | 187 write_avi_chunk(f,ckidAVIPADDING,2048-(ftell(f)&2047)-8,NULL); |
188 // 'movi' header: | |
2529 | 189 write_avi_list(f,listtypeAVIMOVIE,muxer->movi_end-ftell(f)-12); |
190 muxer->movi_start=ftell(f); | |
1 | 191 } |
192 | |
2529 | 193 void aviwrite_write_index(aviwrite_t *muxer,FILE *f){ |
194 muxer->movi_end=ftell(f); | |
195 if(muxer->idx && muxer->idx_pos>0){ | |
196 // fixup index entries: | |
197 // int i; | |
198 // for(i=0;i<muxer->idx_pos;i++) muxer->idx[i].dwChunkOffset-=muxer->movi_start-4; | |
199 // write index chunk: | |
200 write_avi_chunk(f,ckidAVINEWINDEX,16*muxer->idx_pos,muxer->idx); | |
201 muxer->avih.dwFlags|=AVIF_HASINDEX; | |
202 } | |
203 muxer->file_end=ftell(f); | |
1 | 204 } |
205 |