Mercurial > mplayer.hg
annotate libmpdemux/muxer_avi.c @ 12138:eb3ad04675e1
MPL2 subreader, patch by Cezary Jackiewicz (cjackiewicz@poczta.onet.pl)
author | alex |
---|---|
date | Tue, 06 Apr 2004 11:52:31 +0000 |
parents | d030debb4442 |
children | ca5dc9c2cb51 |
rev | line source |
---|---|
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
1
diff
changeset
|
1 #include <stdio.h> |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
1
diff
changeset
|
2 #include <stdlib.h> |
2529 | 3 #include <string.h> |
8585 | 4 #include <inttypes.h> |
8591 | 5 #include <unistd.h> |
12036 | 6 #include <limits.h> |
2529 | 7 |
2555
66837325b929
config.h cleanup, few things added to steram/demuxer headers
arpi
parents:
2529
diff
changeset
|
8 #include "config.h" |
7149 | 9 #include "../version.h" |
2555
66837325b929
config.h cleanup, few things added to steram/demuxer headers
arpi
parents:
2529
diff
changeset
|
10 |
12036 | 11 #include "stream.h" |
12 #include "demuxer.h" | |
13 #include "stheader.h" | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
1
diff
changeset
|
14 |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
1
diff
changeset
|
15 #include "wine/mmreg.h" |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
1
diff
changeset
|
16 #include "wine/avifmt.h" |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
1
diff
changeset
|
17 #include "wine/vfw.h" |
6918 | 18 #include "bswap.h" |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
1
diff
changeset
|
19 |
8585 | 20 #include "muxer.h" |
6918 | 21 #include "aviheader.h" |
12036 | 22 #include "mp_msg.h" |
1 | 23 |
7145
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
24 extern char *info_name; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
25 extern char *info_artist; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
26 extern char *info_genre; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
27 extern char *info_subject; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
28 extern char *info_copyright; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
29 extern char *info_sourceform; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
30 extern char *info_comment; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
31 |
12036 | 32 /* #define ODML_CHUNKLEN 0x02000000 */ /* for testing purposes */ |
33 #define ODML_CHUNKLEN 0x40000000 | |
34 #define ODML_NOTKEYFRAME 0x80000000U | |
35 #define MOVIALIGN 0x00001000 | |
36 | |
12061 | 37 float avi_aspect_override = -1.0; |
12051 | 38 |
12036 | 39 struct avi_odmlidx_entry { |
40 uint64_t ofs; | |
41 uint32_t len; | |
42 uint32_t flags; | |
43 }; | |
44 | |
45 struct avi_odmlsuperidx_entry { | |
46 uint64_t ofs; | |
47 uint32_t len; | |
48 uint32_t duration; | |
49 }; | |
50 | |
51 struct avi_stream_info { | |
52 int idxsize; | |
53 int idxpos; | |
54 int superidxpos; | |
55 int superidxsize; | |
56 struct avi_odmlidx_entry *idx; | |
57 struct avi_odmlsuperidx_entry *superidx; | |
58 }; | |
59 | |
12061 | 60 static unsigned int avi_aspect(muxer_stream_t *vstream) |
61 { | |
62 int x,y; | |
63 float aspect = vstream->aspect; | |
64 | |
65 if (avi_aspect_override > 0.0) { | |
66 aspect = avi_aspect_override; | |
67 } | |
68 | |
69 if (aspect <= 0.0) return 0; | |
70 | |
71 if (aspect > 15.99/9.0 && aspect < 16.01/9.0) { | |
72 return MAKE_AVI_ASPECT(16, 9); | |
73 } | |
74 if (aspect > 3.99/3.0 && aspect < 4.01/3.0) { | |
75 return MAKE_AVI_ASPECT(4, 3); | |
76 } | |
77 | |
78 if (aspect >= 1.0) { | |
79 x = 16384; | |
80 y = (float)x / aspect; | |
81 } else { | |
82 y = 16384; | |
83 x = (float)y * aspect; | |
84 } | |
85 | |
86 return MAKE_AVI_ASPECT(x, y); | |
87 } | |
88 | |
8585 | 89 static muxer_stream_t* avifile_new_stream(muxer_t *muxer,int type){ |
12036 | 90 struct avi_stream_info *si; |
8585 | 91 muxer_stream_t* s; |
9007
12fc55eb3373
Cleanup of the muxer API, func parameters muxer & muxer_f eliminated.
arpi
parents:
8591
diff
changeset
|
92 if (!muxer) return NULL; |
8585 | 93 if(muxer->avih.dwStreams>=MUXER_MAX_STREAMS){ |
12036 | 94 mp_msg(MSGT_MUXER, MSGL_ERR, "Too many streams! increase MUXER_MAX_STREAMS !\n"); |
2529 | 95 return NULL; |
96 } | |
8585 | 97 s=malloc(sizeof(muxer_stream_t)); |
98 memset(s,0,sizeof(muxer_stream_t)); | |
2529 | 99 if(!s) return NULL; // no mem!? |
100 muxer->streams[muxer->avih.dwStreams]=s; | |
101 s->type=type; | |
102 s->id=muxer->avih.dwStreams; | |
103 s->timer=0.0; | |
2652 | 104 s->size=0; |
9007
12fc55eb3373
Cleanup of the muxer API, func parameters muxer & muxer_f eliminated.
arpi
parents:
8591
diff
changeset
|
105 s->muxer=muxer; |
12036 | 106 s->priv=si=malloc(sizeof(struct avi_stream_info)); |
107 memset(si,0,sizeof(struct avi_stream_info)); | |
108 si->idxsize=256; | |
109 si->idx=malloc(sizeof(struct avi_odmlidx_entry)*si->idxsize); | |
110 | |
2529 | 111 switch(type){ |
8585 | 112 case MUXER_TYPE_VIDEO: |
2529 | 113 s->ckid=mmioFOURCC(('0'+s->id/10),('0'+(s->id%10)),'d','c'); |
114 s->h.fccType=streamtypeVIDEO; | |
115 if(!muxer->def_v) muxer->def_v=s; | |
116 break; | |
8585 | 117 case MUXER_TYPE_AUDIO: |
2529 | 118 s->ckid=mmioFOURCC(('0'+s->id/10),('0'+(s->id%10)),'w','b'); |
119 s->h.fccType=streamtypeAUDIO; | |
120 break; | |
121 default: | |
12036 | 122 mp_msg(MSGT_MUXER, MSGL_WARN, "Warning! unknown stream type: %d\n",type); |
2529 | 123 return NULL; |
124 } | |
125 muxer->avih.dwStreams++; | |
126 return s; | |
127 } | |
1 | 128 |
2529 | 129 static void write_avi_chunk(FILE *f,unsigned int id,int len,void* data){ |
6918 | 130 int le_len = le2me_32(len); |
131 int le_id = le2me_32(id); | |
132 fwrite(&le_id,4,1,f); | |
133 fwrite(&le_len,4,1,f); | |
134 | |
1 | 135 if(len>0){ |
136 if(data){ | |
137 // DATA | |
138 fwrite(data,len,1,f); | |
139 if(len&1){ // padding | |
140 unsigned char zerobyte=0; | |
141 fwrite(&zerobyte,1,1,f); | |
142 } | |
143 } else { | |
144 // JUNK | |
145 char *avi_junk_data="[= MPlayer junk data! =]"; | |
146 if(len&1) ++len; // padding | |
147 while(len>0){ | |
148 int l=strlen(avi_junk_data); | |
149 if(l>len) l=len; | |
150 fwrite(avi_junk_data,l,1,f); | |
151 len-=l; | |
152 } | |
153 } | |
154 } | |
2529 | 155 } |
156 | |
12036 | 157 static void write_avi_list(FILE *f,unsigned int id,int len); |
158 static void avifile_write_index(muxer_t *muxer); | |
159 | |
160 static void avifile_odml_new_riff(muxer_t *muxer) | |
161 { | |
162 FILE *f = muxer->file; | |
163 uint32_t riff[3]; | |
164 | |
165 /* Pad to ODML_CHUNKLEN */ | |
166 write_avi_chunk(f,ckidAVIPADDING,ODML_CHUNKLEN - (ftello(f)%ODML_CHUNKLEN) - 8,NULL); | |
167 | |
168 /* RIFF/AVIX chunk */ | |
169 riff[0]=le2me_32(mmioFOURCC('R','I','F','F')); | |
170 riff[1]=0; | |
171 riff[2]=le2me_32(mmioFOURCC('A','V','I','X')); | |
172 fwrite(riff,12,1,f); | |
173 | |
174 write_avi_list(f,listtypeAVIMOVIE,0); | |
175 } | |
176 | |
9007
12fc55eb3373
Cleanup of the muxer API, func parameters muxer & muxer_f eliminated.
arpi
parents:
8591
diff
changeset
|
177 static void avifile_write_chunk(muxer_stream_t *s,size_t len,unsigned int flags){ |
12036 | 178 off_t pos; |
179 struct avi_stream_info *si = s->priv; | |
9007
12fc55eb3373
Cleanup of the muxer API, func parameters muxer & muxer_f eliminated.
arpi
parents:
8591
diff
changeset
|
180 muxer_t *muxer=s->muxer; |
12036 | 181 int isodml = muxer->file_end > ODML_CHUNKLEN ? 1 : 0; |
2529 | 182 |
12036 | 183 if (!isodml) { |
184 // add to the traditional index: | |
185 if(muxer->idx_pos>=muxer->idx_size){ | |
186 muxer->idx_size+=256; // 4kB | |
187 muxer->idx=realloc(muxer->idx,16*muxer->idx_size); | |
188 } | |
189 muxer->idx[muxer->idx_pos].ckid=s->ckid; | |
190 muxer->idx[muxer->idx_pos].dwFlags=flags; // keyframe? | |
191 muxer->idx[muxer->idx_pos].dwChunkOffset=ftello(muxer->file)-(muxer->movi_start-4); | |
192 muxer->idx[muxer->idx_pos].dwChunkLength=len; | |
193 ++muxer->idx_pos; | |
194 } | |
195 | |
196 // add to odml index | |
197 if(si->idxpos>=si->idxsize){ | |
198 si->idxsize+=256; | |
199 si->idx=realloc(si->idx,sizeof(*si->idx)*si->idxsize); | |
2529 | 200 } |
12036 | 201 si->idx[si->idxpos].flags=(flags&AVIIF_KEYFRAME)?0:ODML_NOTKEYFRAME; |
202 si->idx[si->idxpos].ofs=ftello(muxer->file); | |
203 si->idx[si->idxpos].len=len; | |
204 ++si->idxpos; | |
205 | |
206 pos = muxer->file_end; | |
207 if (pos < ODML_CHUNKLEN && | |
208 pos + 16*muxer->idx_pos + len + 8 > ODML_CHUNKLEN) { | |
209 | |
210 avifile_write_index(muxer); | |
211 avifile_odml_new_riff(muxer); | |
212 | |
213 pos = muxer->file_end = ftello(muxer->file); | |
214 } | |
215 if (pos % ODML_CHUNKLEN + len + 8 > ODML_CHUNKLEN) { | |
216 avifile_odml_new_riff(muxer); | |
217 muxer->file_end = ftello(muxer->file); | |
218 } | |
2529 | 219 |
220 // write out the chunk: | |
9007
12fc55eb3373
Cleanup of the muxer API, func parameters muxer & muxer_f eliminated.
arpi
parents:
8591
diff
changeset
|
221 write_avi_chunk(muxer->file,s->ckid,len,s->buffer); /* unsigned char */ |
6918 | 222 |
2529 | 223 // alter counters: |
12036 | 224 if (len > s->h.dwSuggestedBufferSize){ |
225 s->h.dwSuggestedBufferSize = len; | |
226 } | |
2529 | 227 if(s->h.dwSampleSize){ |
228 // CBR | |
229 s->h.dwLength+=len/s->h.dwSampleSize; | |
12036 | 230 if(len%s->h.dwSampleSize) mp_msg(MSGT_MUXER, MSGL_WARN, "Warning! len isn't divisable by samplesize!\n"); |
2529 | 231 } else { |
232 // VBR | |
233 s->h.dwLength++; | |
234 } | |
235 s->timer=(double)s->h.dwLength*s->h.dwScale/s->h.dwRate; | |
2652 | 236 s->size+=len; |
8585 | 237 if((unsigned int)len>s->h.dwSuggestedBufferSize) s->h.dwSuggestedBufferSize=len; |
1 | 238 |
12062 | 239 muxer->file_end += len + len&1 + 8; |
1 | 240 } |
241 | |
2529 | 242 static void write_avi_list(FILE *f,unsigned int id,int len){ |
1 | 243 unsigned int list_id=FOURCC_LIST; |
6918 | 244 int le_len; |
245 int le_id; | |
1 | 246 len+=4; // list fix |
6918 | 247 list_id = le2me_32(list_id); |
248 le_len = le2me_32(len); | |
249 le_id = le2me_32(id); | |
1 | 250 fwrite(&list_id,4,1,f); |
6918 | 251 fwrite(&le_len,4,1,f); |
252 fwrite(&le_id,4,1,f); | |
1 | 253 } |
254 | |
11374 | 255 #define WFSIZE(wf) (sizeof(WAVEFORMATEX)+(wf)->cbSize) |
2635
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2555
diff
changeset
|
256 |
9007
12fc55eb3373
Cleanup of the muxer API, func parameters muxer & muxer_f eliminated.
arpi
parents:
8591
diff
changeset
|
257 static void avifile_write_header(muxer_t *muxer){ |
8585 | 258 uint32_t riff[3]; |
12036 | 259 unsigned int dmlh[1]; |
8585 | 260 unsigned int i; |
2529 | 261 unsigned int hdrsize; |
8585 | 262 muxer_info_t info[16]; |
12036 | 263 FILE *f = muxer->file; |
264 VideoPropHeader vprp; | |
12061 | 265 uint32_t aspect = avi_aspect(muxer->def_v); |
12036 | 266 off_t pos; |
267 int isodml = muxer->file_end > ODML_CHUNKLEN ? 1 : 0; | |
268 | |
12061 | 269 if (aspect == 0) { |
270 mp_msg(MSGT_MUXER, MSGL_INFO, "ODML: Aspect information not (yet?) available or unspecified, not writing vprp header.\n"); | |
271 } else { | |
272 mp_msg(MSGT_MUXER, MSGL_INFO, "ODML: vprp aspect is %d:%d.\n", aspect >> 16, aspect & 0xffff); | |
273 } | |
274 | |
12036 | 275 if (isodml) { |
276 for (pos = 0; pos < muxer->file_end; pos += ODML_CHUNKLEN) { | |
277 unsigned int rifflen, movilen; | |
278 | |
279 /* fixup RIFF length */ | |
280 if (muxer->file_end - pos > ODML_CHUNKLEN) { | |
281 rifflen = le2me_32(ODML_CHUNKLEN - 8); | |
282 movilen = le2me_32(ODML_CHUNKLEN - 20); | |
283 } else { | |
284 rifflen = le2me_32(muxer->file_end - pos - 8); | |
285 movilen = le2me_32(muxer->file_end - pos - 20); | |
286 } | |
287 fseeko(f, pos + 4, SEEK_SET); | |
288 fwrite(&rifflen,4,1,f); | |
7145
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
289 |
12036 | 290 /* fixup movi length */ |
291 if (pos > 0) { | |
292 fseeko(f, pos + 16, SEEK_SET); | |
293 fwrite(&movilen,4,1,f); | |
294 } | |
295 } | |
296 | |
297 fseeko(f, 12, SEEK_SET); | |
298 } else { | |
299 // RIFF header: | |
300 riff[0]=mmioFOURCC('R','I','F','F'); | |
301 riff[1]=muxer->file_end-2*sizeof(unsigned int); // filesize | |
302 riff[2]=formtypeAVI; // 'AVI ' | |
303 riff[0]=le2me_32(riff[0]); | |
304 riff[1]=le2me_32(riff[1]); | |
305 riff[2]=le2me_32(riff[2]); | |
306 fwrite(&riff,12,1,f); | |
307 } | |
308 | |
2529 | 309 // update AVI header: |
310 if(muxer->def_v){ | |
12036 | 311 int i; |
2529 | 312 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
|
313 // muxer->avih.dwMaxBytesPerSec=1000000; // dummy!!!!! FIXME |
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2555
diff
changeset
|
314 // muxer->avih.dwPaddingGranularity=2; // ??? |
2529 | 315 muxer->avih.dwFlags|=AVIF_ISINTERLEAVED|AVIF_TRUSTCKTYPE; |
12036 | 316 muxer->avih.dwTotalFrames=0; |
317 for (i=0; i<muxer->idx_pos; i++) { | |
318 if (muxer->idx[i].ckid == muxer->def_v->ckid) | |
319 muxer->avih.dwTotalFrames++; | |
320 } | |
2635
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2555
diff
changeset
|
321 // muxer->avih.dwSuggestedBufferSize=muxer->def_v->h.dwSuggestedBufferSize; |
2529 | 322 muxer->avih.dwWidth=muxer->def_v->bih->biWidth; |
323 muxer->avih.dwHeight=muxer->def_v->bih->biHeight; | |
324 } | |
325 | |
1 | 326 // AVI header: |
2529 | 327 hdrsize=sizeof(muxer->avih)+8; |
12036 | 328 if (isodml) hdrsize+=sizeof(dmlh)+20; // dmlh |
2529 | 329 // calc total header size: |
330 for(i=0;i<muxer->avih.dwStreams;i++){ | |
12036 | 331 muxer_stream_t *s = muxer->streams[i]; |
332 struct avi_stream_info *si = s->priv; | |
333 | |
2529 | 334 hdrsize+=12; // LIST |
335 hdrsize+=sizeof(muxer->streams[i]->h)+8; // strh | |
336 switch(muxer->streams[i]->type){ | |
8585 | 337 case MUXER_TYPE_VIDEO: |
2529 | 338 hdrsize+=muxer->streams[i]->bih->biSize+8; // strf |
12061 | 339 if (aspect != 0) { |
12051 | 340 hdrsize+=8+4*(9+8*1); // vprp |
341 } | |
2529 | 342 break; |
8585 | 343 case MUXER_TYPE_AUDIO: |
2635
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2555
diff
changeset
|
344 hdrsize+=WFSIZE(muxer->streams[i]->wf)+8; // strf |
2529 | 345 break; |
346 } | |
12036 | 347 if (isodml && si && si->superidx && si->superidxsize) { |
348 hdrsize += 32 + 16*si->superidxsize; //indx | |
349 } | |
2529 | 350 } |
351 write_avi_list(f,listtypeAVIHEADER,hdrsize); | |
6918 | 352 |
353 le2me_MainAVIHeader(&muxer->avih); | |
354 write_avi_chunk(f,ckidAVIMAINHDR,sizeof(muxer->avih),&muxer->avih); /* MainAVIHeader */ | |
355 le2me_MainAVIHeader(&muxer->avih); | |
2529 | 356 |
357 // stream headers: | |
358 for(i=0;i<muxer->avih.dwStreams;i++){ | |
12036 | 359 muxer_stream_t *s = muxer->streams[i]; |
360 struct avi_stream_info *si = s->priv; | |
361 unsigned int idxhdr[8]; | |
362 int j,n; | |
363 | |
364 hdrsize=sizeof(s->h)+8; // strh | |
365 if (si && si->superidx && si->superidxsize) { | |
366 hdrsize += 32 + 16*si->superidxsize; //indx | |
367 } | |
368 switch(s->type){ | |
8585 | 369 case MUXER_TYPE_VIDEO: |
12036 | 370 hdrsize+=s->bih->biSize+8; // strf |
371 s->h.fccHandler = s->bih->biCompression; | |
372 s->h.rcFrame.right = s->bih->biWidth; | |
373 s->h.rcFrame.bottom = s->bih->biHeight; | |
12061 | 374 if (aspect != 0) { |
12051 | 375 // fill out vprp info |
376 memset(&vprp, 0, sizeof(vprp)); | |
377 vprp.dwVerticalRefreshRate = (s->h.dwRate+s->h.dwScale-1)/s->h.dwScale; | |
378 vprp.dwHTotalInT = muxer->avih.dwWidth; | |
379 vprp.dwVTotalInLines = muxer->avih.dwHeight; | |
12061 | 380 vprp.dwFrameAspectRatio = aspect; |
12051 | 381 vprp.dwFrameWidthInPixels = muxer->avih.dwWidth; |
382 vprp.dwFrameHeightInLines = muxer->avih.dwHeight; | |
383 vprp.nbFieldPerFrame = 1; | |
384 vprp.FieldInfo[0].CompressedBMHeight = muxer->avih.dwHeight; | |
385 vprp.FieldInfo[0].CompressedBMWidth = muxer->avih.dwWidth; | |
386 vprp.FieldInfo[0].ValidBMHeight = muxer->avih.dwHeight; | |
387 vprp.FieldInfo[0].ValidBMWidth = muxer->avih.dwWidth; | |
388 hdrsize+=8+4*(9+8*1); // vprp | |
389 } | |
2529 | 390 break; |
8585 | 391 case MUXER_TYPE_AUDIO: |
12036 | 392 hdrsize+=WFSIZE(s->wf)+8; // strf |
393 s->h.fccHandler = s->wf->wFormatTag; | |
2529 | 394 break; |
395 } | |
12036 | 396 |
2529 | 397 write_avi_list(f,listtypeSTREAMHEADER,hdrsize); |
12036 | 398 le2me_AVIStreamHeader(&s->h); |
399 write_avi_chunk(f,ckidSTREAMHEADER,sizeof(s->h),&s->h); /* AVISTreamHeader */ // strh | |
400 le2me_AVIStreamHeader(&s->h); | |
6918 | 401 |
12036 | 402 switch(s->type){ |
8585 | 403 case MUXER_TYPE_VIDEO: |
6918 | 404 { |
12036 | 405 int biSize=s->bih->biSize; |
406 le2me_BITMAPINFOHEADER(s->bih); | |
407 write_avi_chunk(f,ckidSTREAMFORMAT,biSize,s->bih); /* BITMAPINFOHEADER */ | |
12053 | 408 le2me_BITMAPINFOHEADER(s->bih); |
409 | |
12061 | 410 if (aspect != 0) { |
12053 | 411 int fields = vprp.nbFieldPerFrame; |
12051 | 412 le2me_VideoPropHeader(&vprp); |
413 le2me_VIDEO_FIELD_DESC(&vprp.FieldInfo[0]); | |
414 le2me_VIDEO_FIELD_DESC(&vprp.FieldInfo[1]); | |
415 write_avi_chunk(f,mmioFOURCC('v','p','r','p'), | |
12053 | 416 sizeof(VideoPropHeader) - |
417 sizeof(VIDEO_FIELD_DESC)*(2-fields), | |
418 &vprp); /* Video Properties Header */ | |
12051 | 419 } |
6918 | 420 } |
2529 | 421 break; |
8585 | 422 case MUXER_TYPE_AUDIO: |
6918 | 423 { |
12036 | 424 int wfsize = WFSIZE(s->wf); |
425 le2me_WAVEFORMATEX(s->wf); | |
426 write_avi_chunk(f,ckidSTREAMFORMAT,wfsize,s->wf); /* WAVEFORMATEX */ | |
427 le2me_WAVEFORMATEX(s->wf); | |
6918 | 428 } |
2529 | 429 break; |
430 } | |
12036 | 431 if (isodml && si && si->superidx && si->superidxsize) { |
432 n = si->superidxsize; | |
433 | |
434 idxhdr[0] = le2me_32(mmioFOURCC('i', 'n', 'd', 'x')); | |
435 idxhdr[1] = le2me_32(24 + 16*n); | |
436 idxhdr[2] = le2me_32(0x00000004); | |
437 idxhdr[3] = le2me_32(si->superidxpos); | |
438 idxhdr[4] = le2me_32(s->ckid); | |
439 idxhdr[5] = 0; | |
440 idxhdr[6] = 0; | |
441 idxhdr[7] = 0; | |
442 | |
443 fwrite(idxhdr,sizeof(idxhdr),1,f); | |
444 for (j=0; j<n; j++) { | |
445 struct avi_odmlsuperidx_entry *entry = &si->superidx[j]; | |
446 unsigned int data[4]; | |
447 data[0] = le2me_32(entry->ofs); | |
448 data[1] = le2me_32(entry->ofs >> 32); | |
449 data[2] = le2me_32(entry->len); | |
450 data[3] = le2me_32(entry->duration); | |
451 fwrite(data,sizeof(data),1,f); | |
452 } | |
453 } | |
454 } | |
455 | |
456 // ODML | |
457 if (isodml) { | |
458 memset(dmlh, 0, sizeof(dmlh)); | |
459 dmlh[0] = le2me_32(muxer->avih.dwTotalFrames); | |
460 write_avi_list(f,mmioFOURCC('o','d','m','l'),sizeof(dmlh)+8); | |
461 write_avi_chunk(f,mmioFOURCC('d','m','l','h'),sizeof(dmlh),dmlh); | |
2529 | 462 } |
463 | |
7145
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
464 // ============= INFO =============== |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
465 // always include software info |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
466 info[0].id=mmioFOURCC('I','S','F','T'); // Software: |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
467 info[0].text="MEncoder " VERSION; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
468 // include any optional strings |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
469 i=1; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
470 if(info_name!=NULL){ |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
471 info[i].id=mmioFOURCC('I','N','A','M'); // Name: |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
472 info[i++].text=info_name; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
473 } |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
474 if(info_artist!=NULL){ |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
475 info[i].id=mmioFOURCC('I','A','R','T'); // Artist: |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
476 info[i++].text=info_artist; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
477 } |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
478 if(info_genre!=NULL){ |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
479 info[i].id=mmioFOURCC('I','G','N','R'); // Genre: |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
480 info[i++].text=info_genre; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
481 } |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
482 if(info_subject!=NULL){ |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
483 info[i].id=mmioFOURCC('I','S','B','J'); // Subject: |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
484 info[i++].text=info_subject; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
485 } |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
486 if(info_copyright!=NULL){ |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
487 info[i].id=mmioFOURCC('I','C','O','P'); // Copyright: |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
488 info[i++].text=info_copyright; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
489 } |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
490 if(info_sourceform!=NULL){ |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
491 info[i].id=mmioFOURCC('I','S','R','F'); // Source Form: |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
492 info[i++].text=info_sourceform; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
493 } |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
494 if(info_comment!=NULL){ |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
495 info[i].id=mmioFOURCC('I','C','M','T'); // Comment: |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
496 info[i++].text=info_comment; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
497 } |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
498 info[i].id=0; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
499 |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
500 hdrsize=0; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
501 // calc info size: |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
502 for(i=0;info[i].id!=0;i++) if(info[i].text){ |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
503 size_t sz=strlen(info[i].text)+1; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
504 hdrsize+=sz+8+sz%2; |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
505 } |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
506 // write infos: |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
507 if (hdrsize!=0){ |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
508 write_avi_list(f,mmioFOURCC('I','N','F','O'),hdrsize); |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
509 for(i=0;info[i].id!=0;i++) if(info[i].text){ |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
510 write_avi_chunk(f,info[i].id,strlen(info[i].text)+1,info[i].text); |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
511 } |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
512 } |
3854945aefbb
new mencoder option -info, to store copyright, title, encoder version etc in AVI
arpi
parents:
7144
diff
changeset
|
513 |
2529 | 514 // JUNK: |
12036 | 515 write_avi_chunk(f,ckidAVIPADDING,MOVIALIGN-(ftello(f)%MOVIALIGN)-8,NULL); /* junk */ |
516 if (!isodml) { | |
517 // 'movi' header: | |
518 write_avi_list(f,listtypeAVIMOVIE,muxer->movi_end-ftello(f)-12); | |
519 } else { | |
520 if (ftello(f) != MOVIALIGN) { | |
521 mp_msg(MSGT_MUXER, MSGL_ERR, "Opendml superindex is too big for reserved space!\n"); | |
522 mp_msg(MSGT_MUXER, MSGL_ERR, "Expected filepos %d, real filepos %d, missing space %d\n", MOVIALIGN, ftell(muxer->file), ftell(muxer->file)-MOVIALIGN); | |
12037 | 523 mp_msg(MSGT_MUXER, MSGL_ERR, "Try increasing MOVIALIGN in libmpdemux/muxer_avi.c\n"); |
12036 | 524 } |
525 write_avi_list(f,listtypeAVIMOVIE,muxer->movi_end-ftello(f)-12); | |
526 } | |
527 muxer->movi_start=ftello(muxer->file); | |
528 } | |
529 | |
530 static void avifile_odml_write_index(muxer_t *muxer){ | |
531 muxer_stream_t* s; | |
532 struct avi_stream_info *si; | |
533 int i; | |
534 | |
535 for (i=0; i<muxer->avih.dwStreams; i++) { | |
536 int j,k,n,idxpos,len,last,entries_per_subidx; | |
537 unsigned int idxhdr[8]; | |
538 s = muxer->streams[i]; | |
539 si = s->priv; | |
540 | |
541 /* | |
542 * According to Avery Lee MSMP wants the subidx chunks to have the same size. | |
543 * | |
544 * So this code figures out how many entries we can put into | |
545 * an ix?? chunk, so that each ix?? chunk has the same size and the offsets | |
546 * don't overflow (Using ODML_CHUNKLEN for that is a bit more restrictive | |
547 * than it has to be though). | |
548 */ | |
549 | |
550 len = 0; | |
551 n = 0; | |
552 entries_per_subidx = INT_MAX; | |
553 do { | |
554 off_t start = si->idx[0].ofs; | |
555 last = entries_per_subidx; | |
556 for (j=0; j<si->idxpos; j++) { | |
557 len = si->idx[j].ofs - start; | |
558 if(len >= ODML_CHUNKLEN || n >= entries_per_subidx) { | |
559 if (entries_per_subidx > n) { | |
560 entries_per_subidx = n; | |
561 } | |
562 start = si->idx[j].ofs; | |
563 len = 0; | |
564 n = 0; | |
565 } | |
566 n++; | |
567 } | |
568 } while (last != entries_per_subidx); | |
569 | |
570 si->superidxpos = (si->idxpos+entries_per_subidx-1) / entries_per_subidx; | |
571 | |
572 mp_msg(MSGT_MUXER, MSGL_V, "ODML: Stream %d: Using %d entries per subidx, %d entries in superidx\n", | |
573 i, entries_per_subidx, si->superidxpos); | |
574 | |
575 si->superidxsize = si->superidxpos; | |
576 si->superidx = malloc(sizeof(*si->superidx) * si->superidxsize); | |
577 memset(si->superidx, 0, sizeof(*si->superidx) * si->superidxsize); | |
578 | |
579 idxpos = 0; | |
580 for (j=0; j<si->superidxpos; j++) { | |
581 off_t start = si->idx[idxpos].ofs; | |
582 int duration; | |
583 | |
584 duration = 0; | |
585 for (k=0; k<entries_per_subidx && idxpos+k<si->idxpos; k++) { | |
586 duration += s->h.dwSampleSize ? si->idx[idxpos+k].len/s->h.dwSampleSize : 1; | |
587 } | |
588 | |
589 idxhdr[0] = le2me_32((s->ckid << 16) | mmioFOURCC('i', 'x', 0, 0)); | |
590 idxhdr[1] = le2me_32(24 + 8*k); | |
591 idxhdr[2] = le2me_32(0x01000002); | |
592 idxhdr[3] = le2me_32(k); | |
593 idxhdr[4] = le2me_32(s->ckid); | |
594 idxhdr[5] = le2me_32(start + 8); | |
595 idxhdr[6] = le2me_32((start + 8)>> 32); | |
596 idxhdr[7] = 0; /* unused */ | |
597 | |
598 si->superidx[j].len = 32 + 8*k; | |
599 si->superidx[j].ofs = ftello(muxer->file); | |
600 si->superidx[j].duration = duration; | |
601 | |
602 fwrite(idxhdr,sizeof(idxhdr),1,muxer->file); | |
603 for (k=0; k<entries_per_subidx && idxpos<si->idxpos; k++) { | |
604 unsigned int entry[2]; | |
605 entry[0] = le2me_32(si->idx[idxpos].ofs - start); | |
606 entry[1] = le2me_32(si->idx[idxpos].len | si->idx[idxpos].flags); | |
607 idxpos++; | |
608 fwrite(entry,sizeof(entry),1,muxer->file); | |
609 } | |
610 } | |
611 } | |
612 muxer->file_end=ftello(muxer->file); | |
1 | 613 } |
614 | |
9007
12fc55eb3373
Cleanup of the muxer API, func parameters muxer & muxer_f eliminated.
arpi
parents:
8591
diff
changeset
|
615 static void avifile_write_index(muxer_t *muxer){ |
12036 | 616 |
617 if(muxer->file_end > ODML_CHUNKLEN && | |
618 muxer->idx && muxer->idx_pos>0) { | |
619 avifile_odml_write_index(muxer); | |
620 return; | |
621 } | |
622 | |
623 muxer->movi_end=ftello(muxer->file); | |
2529 | 624 if(muxer->idx && muxer->idx_pos>0){ |
6918 | 625 int i; |
2529 | 626 // fixup index entries: |
627 // for(i=0;i<muxer->idx_pos;i++) muxer->idx[i].dwChunkOffset-=muxer->movi_start-4; | |
628 // write index chunk: | |
6918 | 629 for (i=0; i<muxer->idx_pos; i++) le2me_AVIINDEXENTRY((&muxer->idx[i])); |
9007
12fc55eb3373
Cleanup of the muxer API, func parameters muxer & muxer_f eliminated.
arpi
parents:
8591
diff
changeset
|
630 write_avi_chunk(muxer->file,ckidAVINEWINDEX,16*muxer->idx_pos,muxer->idx); /* AVIINDEXENTRY */ |
6918 | 631 for (i=0; i<muxer->idx_pos; i++) le2me_AVIINDEXENTRY((&muxer->idx[i])); |
2529 | 632 muxer->avih.dwFlags|=AVIF_HASINDEX; |
633 } | |
12036 | 634 muxer->file_end=ftello(muxer->file); |
1 | 635 } |
636 | |
8585 | 637 void muxer_init_muxer_avi(muxer_t *muxer){ |
638 muxer->cont_new_stream = &avifile_new_stream; | |
639 muxer->cont_write_chunk = &avifile_write_chunk; | |
640 muxer->cont_write_header = &avifile_write_header; | |
641 muxer->cont_write_index = &avifile_write_index; | |
642 } |