Mercurial > libavformat.hg
annotate avienc.c @ 4313:bdb984bfa3ed libavformat
delay header writing and parse mpeg2 frame
author | bcoudurier |
---|---|
date | Sat, 31 Jan 2009 06:32:12 +0000 |
parents | 77e0c7511d41 |
children | 1b6a4571f7bf |
rev | line source |
---|---|
0 | 1 /* |
1415
3b00fb8ef8e4
replace coder/decoder file description in libavformat by muxer/demuxer
aurel
parents:
1358
diff
changeset
|
2 * AVI muxer |
4251
77e0c7511d41
cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents:
4250
diff
changeset
|
3 * Copyright (c) 2000 Fabrice Bellard |
0 | 4 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
5 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
6 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
0 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
0 | 11 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
0 | 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 | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
894
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 20 */ |
21 #include "avformat.h" | |
22 #include "avi.h" | |
1172
6a5e58d2114b
move common stuff from avienc.c and wav.c to new file riff.c
mru
parents:
1169
diff
changeset
|
23 #include "riff.h" |
0 | 24 |
25 /* | |
885 | 26 * TODO: |
0 | 27 * - fill all fields if non streamed (nb_frames for example) |
28 */ | |
29 | |
4206 | 30 #if CONFIG_AVI_MUXER |
119 | 31 typedef struct AVIIentry { |
32 unsigned int flags, pos, len; | |
33 } AVIIentry; | |
34 | |
35 #define AVI_INDEX_CLUSTER_SIZE 16384 | |
36 | |
0 | 37 typedef struct AVIIndex { |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3872
diff
changeset
|
38 int64_t indx_start; |
119 | 39 int entry; |
40 int ents_allocated; | |
41 AVIIentry** cluster; | |
0 | 42 } AVIIndex; |
43 | |
44 typedef struct { | |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3872
diff
changeset
|
45 int64_t riff_start, movi_list, odml_list; |
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3872
diff
changeset
|
46 int64_t frames_hdr_all, frames_hdr_strm[MAX_STREAMS]; |
0 | 47 int audio_strm_length[MAX_STREAMS]; |
119 | 48 int riff_id; |
479 | 49 int packet_count[MAX_STREAMS]; |
119 | 50 |
51 AVIIndex indexes[MAX_STREAMS]; | |
0 | 52 } AVIContext; |
53 | |
885 | 54 static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id) |
119 | 55 { |
56 int cl = ent_id / AVI_INDEX_CLUSTER_SIZE; | |
57 int id = ent_id % AVI_INDEX_CLUSTER_SIZE; | |
58 return &idx->cluster[cl][id]; | |
59 } | |
60 | |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3872
diff
changeset
|
61 static int64_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb, |
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3872
diff
changeset
|
62 const char* riff_tag, const char* list_tag) |
102
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
63 { |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3872
diff
changeset
|
64 int64_t loff; |
119 | 65 int i; |
885 | 66 |
119 | 67 avi->riff_id++; |
68 for (i=0; i<MAX_STREAMS; i++) | |
69 avi->indexes[i].entry = 0; | |
885 | 70 |
102
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
71 avi->riff_start = start_tag(pb, "RIFF"); |
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
72 put_tag(pb, riff_tag); |
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
73 loff = start_tag(pb, "LIST"); |
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
74 put_tag(pb, list_tag); |
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
75 return loff; |
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
76 } |
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
77 |
1332 | 78 static char* avi_stream2fourcc(char* tag, int index, enum CodecType type) |
119 | 79 { |
80 tag[0] = '0'; | |
81 tag[1] = '0' + index; | |
82 if (type == CODEC_TYPE_VIDEO) { | |
83 tag[2] = 'd'; | |
84 tag[3] = 'c'; | |
85 } else { | |
86 tag[2] = 'w'; | |
87 tag[3] = 'b'; | |
88 } | |
89 tag[4] = '\0'; | |
90 return tag; | |
91 } | |
92 | |
1256 | 93 static void avi_write_info_tag(ByteIOContext *pb, const char *tag, const char *str) |
94 { | |
95 int len = strlen(str); | |
96 if (len > 0) { | |
97 len++; | |
98 put_tag(pb, tag); | |
99 put_le32(pb, len); | |
100 put_strz(pb, str); | |
101 if (len & 1) | |
102 put_byte(pb, 0); | |
103 } | |
104 } | |
105 | |
4149 | 106 static void avi_write_info_tag2(AVFormatContext *s, const char *fourcc, const char *key1, const char *key2) |
107 { | |
4250
2fc899894f5e
replace AV_METADATA_IGNORE_CASE flag by a new AV_METADATA_MATCH_CASE flag
aurel
parents:
4206
diff
changeset
|
108 AVMetadataTag *tag= av_metadata_get(s->metadata, key1, NULL, 0); |
4149 | 109 if(!tag && key2) |
4250
2fc899894f5e
replace AV_METADATA_IGNORE_CASE flag by a new AV_METADATA_MATCH_CASE flag
aurel
parents:
4206
diff
changeset
|
110 tag= av_metadata_get(s->metadata, key2, NULL, 0); |
4149 | 111 if(tag) |
112 avi_write_info_tag(s->pb, fourcc, tag->value); | |
113 } | |
114 | |
1267 | 115 static int avi_write_counters(AVFormatContext* s, int riff_id) |
116 { | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2227
diff
changeset
|
117 ByteIOContext *pb = s->pb; |
1267 | 118 AVIContext *avi = s->priv_data; |
119 int n, au_byterate, au_ssize, au_scale, nb_frames = 0; | |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3872
diff
changeset
|
120 int64_t file_size; |
1267 | 121 AVCodecContext* stream; |
122 | |
123 file_size = url_ftell(pb); | |
124 for(n = 0; n < s->nb_streams; n++) { | |
125 assert(avi->frames_hdr_strm[n]); | |
126 stream = s->streams[n]->codec; | |
127 url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET); | |
128 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale); | |
129 if(au_ssize == 0) { | |
130 put_le32(pb, avi->packet_count[n]); | |
131 } else { | |
132 put_le32(pb, avi->audio_strm_length[n] / au_ssize); | |
133 } | |
134 if(stream->codec_type == CODEC_TYPE_VIDEO) | |
135 nb_frames = FFMAX(nb_frames, avi->packet_count[n]); | |
136 } | |
137 if(riff_id == 1) { | |
138 assert(avi->frames_hdr_all); | |
139 url_fseek(pb, avi->frames_hdr_all, SEEK_SET); | |
140 put_le32(pb, nb_frames); | |
141 } | |
142 url_fseek(pb, file_size, SEEK_SET); | |
143 | |
144 return 0; | |
145 } | |
146 | |
0 | 147 static int avi_write_header(AVFormatContext *s) |
148 { | |
149 AVIContext *avi = s->priv_data; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2227
diff
changeset
|
150 ByteIOContext *pb = s->pb; |
0 | 151 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale; |
152 AVCodecContext *stream, *video_enc; | |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3872
diff
changeset
|
153 int64_t list1, list2, strh, strf; |
0 | 154 |
155 /* header list */ | |
119 | 156 avi->riff_id = 0; |
102
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
157 list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl"); |
0 | 158 |
159 /* avi header */ | |
160 put_tag(pb, "avih"); | |
161 put_le32(pb, 14 * 4); | |
162 bitrate = 0; | |
163 | |
164 video_enc = NULL; | |
165 for(n=0;n<s->nb_streams;n++) { | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
796
diff
changeset
|
166 stream = s->streams[n]->codec; |
0 | 167 bitrate += stream->bit_rate; |
168 if (stream->codec_type == CODEC_TYPE_VIDEO) | |
169 video_enc = stream; | |
170 } | |
885 | 171 |
0 | 172 nb_frames = 0; |
173 | |
36
1188ad85857a
audio only avi patch by (Andriy Rysin <arysin at bcsii dot net>)
michaelni
parents:
15
diff
changeset
|
174 if(video_enc){ |
1556 | 175 put_le32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den)); |
36
1188ad85857a
audio only avi patch by (Andriy Rysin <arysin at bcsii dot net>)
michaelni
parents:
15
diff
changeset
|
176 } else { |
887 | 177 put_le32(pb, 0); |
36
1188ad85857a
audio only avi patch by (Andriy Rysin <arysin at bcsii dot net>)
michaelni
parents:
15
diff
changeset
|
178 } |
0 | 179 put_le32(pb, bitrate / 8); /* XXX: not quite exact */ |
180 put_le32(pb, 0); /* padding */ | |
873
7af647503b67
Support for streaming: dont write indexes and dont signal HAS_INDEX in header. Also set filesize to max in this case.
alex
parents:
863
diff
changeset
|
181 if (url_is_streamed(pb)) |
887 | 182 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */ |
873
7af647503b67
Support for streaming: dont write indexes and dont signal HAS_INDEX in header. Also set filesize to max in this case.
alex
parents:
863
diff
changeset
|
183 else |
887 | 184 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */ |
0 | 185 avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */ |
186 put_le32(pb, nb_frames); /* nb frames, filled later */ | |
187 put_le32(pb, 0); /* initial frame */ | |
188 put_le32(pb, s->nb_streams); /* nb streams */ | |
189 put_le32(pb, 1024 * 1024); /* suggested buffer size */ | |
885 | 190 if(video_enc){ |
992 | 191 put_le32(pb, video_enc->width); |
192 put_le32(pb, video_enc->height); | |
36
1188ad85857a
audio only avi patch by (Andriy Rysin <arysin at bcsii dot net>)
michaelni
parents:
15
diff
changeset
|
193 } else { |
887 | 194 put_le32(pb, 0); |
195 put_le32(pb, 0); | |
885 | 196 } |
0 | 197 put_le32(pb, 0); /* reserved */ |
198 put_le32(pb, 0); /* reserved */ | |
199 put_le32(pb, 0); /* reserved */ | |
200 put_le32(pb, 0); /* reserved */ | |
885 | 201 |
0 | 202 /* stream list */ |
203 for(i=0;i<n;i++) { | |
204 list2 = start_tag(pb, "LIST"); | |
205 put_tag(pb, "strl"); | |
885 | 206 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
796
diff
changeset
|
207 stream = s->streams[i]->codec; |
0 | 208 |
209 /* stream generic header */ | |
210 strh = start_tag(pb, "strh"); | |
211 switch(stream->codec_type) { | |
781 | 212 case CODEC_TYPE_VIDEO: put_tag(pb, "vids"); break; |
213 case CODEC_TYPE_AUDIO: put_tag(pb, "auds"); break; | |
214 // case CODEC_TYPE_TEXT : put_tag(pb, "txts"); break; | |
215 case CODEC_TYPE_DATA : put_tag(pb, "dats"); break; | |
216 } | |
217 if(stream->codec_type == CODEC_TYPE_VIDEO) | |
89
8e3cf4e9fc5a
rawvideo patch by (Fred Rothganger <rothgang at uiuc dot edu>)
michaelni
parents:
85
diff
changeset
|
218 put_le32(pb, stream->codec_tag); |
781 | 219 else |
220 put_le32(pb, 1); | |
221 put_le32(pb, 0); /* flags */ | |
222 put_le16(pb, 0); /* priority */ | |
223 put_le16(pb, 0); /* language */ | |
224 put_le32(pb, 0); /* initial frame */ | |
225 | |
226 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale); | |
75
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
65
diff
changeset
|
227 |
781 | 228 put_le32(pb, au_scale); /* scale */ |
229 put_le32(pb, au_byterate); /* rate */ | |
230 av_set_pts_info(s->streams[i], 64, au_scale, au_byterate); | |
231 | |
232 put_le32(pb, 0); /* start */ | |
233 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */ | |
887 | 234 if (url_is_streamed(pb)) |
235 put_le32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */ | |
236 else | |
237 put_le32(pb, 0); /* length, XXX: filled later */ | |
885 | 238 |
781 | 239 /* suggested buffer size */ //FIXME set at the end to largest chunk |
240 if(stream->codec_type == CODEC_TYPE_VIDEO) | |
885 | 241 put_le32(pb, 1024 * 1024); |
781 | 242 else if(stream->codec_type == CODEC_TYPE_AUDIO) |
885 | 243 put_le32(pb, 12 * 1024); |
781 | 244 else |
885 | 245 put_le32(pb, 0); |
781 | 246 put_le32(pb, -1); /* quality */ |
247 put_le32(pb, au_ssize); /* sample size */ | |
248 put_le32(pb, 0); | |
249 put_le16(pb, stream->width); | |
250 put_le16(pb, stream->height); | |
0 | 251 end_tag(pb, strh); |
252 | |
781 | 253 if(stream->codec_type != CODEC_TYPE_DATA){ |
0 | 254 strf = start_tag(pb, "strf"); |
255 switch(stream->codec_type) { | |
256 case CODEC_TYPE_VIDEO: | |
887 | 257 put_bmp_header(pb, stream, codec_bmp_tags, 0); |
0 | 258 break; |
259 case CODEC_TYPE_AUDIO: | |
260 if (put_wav_header(pb, stream) < 0) { | |
261 return -1; | |
262 } | |
263 break; | |
264 default: | |
537 | 265 return -1; |
0 | 266 } |
267 end_tag(pb, strf); | |
781 | 268 } |
885 | 269 |
887 | 270 if (!url_is_streamed(pb)) { |
271 unsigned char tag[5]; | |
272 int j; | |
885 | 273 |
274 /* Starting to lay out AVI OpenDML master index. | |
887 | 275 * We want to make it JUNK entry for now, since we'd |
276 * like to get away without making AVI an OpenDML one | |
277 * for compatibility reasons. | |
278 */ | |
279 avi->indexes[i].entry = avi->indexes[i].ents_allocated = 0; | |
280 avi->indexes[i].indx_start = start_tag(pb, "JUNK"); | |
281 put_le16(pb, 4); /* wLongsPerEntry */ | |
282 put_byte(pb, 0); /* bIndexSubType (0 == frame index) */ | |
283 put_byte(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */ | |
284 put_le32(pb, 0); /* nEntriesInUse (will fill out later on) */ | |
285 put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type)); | |
286 /* dwChunkId */ | |
287 put_le64(pb, 0); /* dwReserved[3] | |
288 put_le32(pb, 0); Must be 0. */ | |
289 for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++) | |
290 put_le64(pb, 0); | |
291 end_tag(pb, avi->indexes[i].indx_start); | |
292 } | |
885 | 293 |
3100 | 294 if( stream->codec_type == CODEC_TYPE_VIDEO |
3759
27537074f2a9
convert every muxer/demuxer to write/read sample_aspect_ratio from/to
aurel
parents:
3424
diff
changeset
|
295 && s->streams[i]->sample_aspect_ratio.num>0 |
27537074f2a9
convert every muxer/demuxer to write/read sample_aspect_ratio from/to
aurel
parents:
3424
diff
changeset
|
296 && s->streams[i]->sample_aspect_ratio.den>0){ |
3100 | 297 int vprp= start_tag(pb, "vprp"); |
3759
27537074f2a9
convert every muxer/demuxer to write/read sample_aspect_ratio from/to
aurel
parents:
3424
diff
changeset
|
298 AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio, |
3100 | 299 (AVRational){stream->width, stream->height}); |
300 int num, den; | |
301 av_reduce(&num, &den, dar.num, dar.den, 0xFFFF); | |
302 | |
303 put_le32(pb, 0); //video format = unknown | |
304 put_le32(pb, 0); //video standard= unknown | |
305 put_le32(pb, lrintf(1.0/av_q2d(stream->time_base))); | |
306 put_le32(pb, stream->width ); | |
307 put_le32(pb, stream->height); | |
3177 | 308 put_le16(pb, den); |
3100 | 309 put_le16(pb, num); |
310 put_le32(pb, stream->width ); | |
311 put_le32(pb, stream->height); | |
312 put_le32(pb, 1); //progressive FIXME | |
313 | |
314 put_le32(pb, stream->height); | |
315 put_le32(pb, stream->width ); | |
316 put_le32(pb, stream->height); | |
317 put_le32(pb, stream->width ); | |
318 put_le32(pb, 0); | |
319 put_le32(pb, 0); | |
320 | |
321 put_le32(pb, 0); | |
322 put_le32(pb, 0); | |
323 end_tag(pb, vprp); | |
324 } | |
325 | |
0 | 326 end_tag(pb, list2); |
327 } | |
885 | 328 |
119 | 329 if (!url_is_streamed(pb)) { |
330 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */ | |
331 avi->odml_list = start_tag(pb, "JUNK"); | |
332 put_tag(pb, "odml"); | |
333 put_tag(pb, "dmlh"); | |
334 put_le32(pb, 248); | |
335 for (i = 0; i < 248; i+= 4) | |
336 put_le32(pb, 0); | |
337 end_tag(pb, avi->odml_list); | |
338 } | |
0 | 339 |
340 end_tag(pb, list1); | |
885 | 341 |
1256 | 342 list2 = start_tag(pb, "LIST"); |
343 put_tag(pb, "INFO"); | |
4149 | 344 avi_write_info_tag2(s, "INAM", "Title", NULL); |
345 avi_write_info_tag2(s, "IART", "Artist", "Author"); | |
346 avi_write_info_tag2(s, "ICOP", "Copyright", NULL); | |
347 avi_write_info_tag2(s, "ICMT", "Comment", NULL); | |
348 avi_write_info_tag2(s, "IPRD", "Album", NULL); | |
349 avi_write_info_tag2(s, "IGNR", "Genre", NULL); | |
350 avi_write_info_tag2(s, "IPRT", "Track", NULL); | |
1256 | 351 if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) |
352 avi_write_info_tag(pb, "ISFT", LIBAVFORMAT_IDENT); | |
353 end_tag(pb, list2); | |
354 | |
355 /* some padding for easier tag editing */ | |
356 list2 = start_tag(pb, "JUNK"); | |
357 for (i = 0; i < 1016; i += 4) | |
358 put_le32(pb, 0); | |
359 end_tag(pb, list2); | |
360 | |
0 | 361 avi->movi_list = start_tag(pb, "LIST"); |
362 put_tag(pb, "movi"); | |
363 | |
364 put_flush_packet(pb); | |
365 | |
366 return 0; | |
367 } | |
368 | |
119 | 369 static int avi_write_ix(AVFormatContext *s) |
370 { | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2227
diff
changeset
|
371 ByteIOContext *pb = s->pb; |
119 | 372 AVIContext *avi = s->priv_data; |
1332 | 373 char tag[5]; |
374 char ix_tag[] = "ix00"; | |
119 | 375 int i, j; |
885 | 376 |
976 | 377 assert(!url_is_streamed(pb)); |
873
7af647503b67
Support for streaming: dont write indexes and dont signal HAS_INDEX in header. Also set filesize to max in this case.
alex
parents:
863
diff
changeset
|
378 |
119 | 379 if (avi->riff_id > AVI_MASTER_INDEX_SIZE) |
380 return -1; | |
885 | 381 |
119 | 382 for (i=0;i<s->nb_streams;i++) { |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3872
diff
changeset
|
383 int64_t ix, pos; |
885 | 384 |
887 | 385 avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type); |
386 ix_tag[3] = '0' + i; | |
885 | 387 |
887 | 388 /* Writing AVI OpenDML leaf index chunk */ |
389 ix = url_ftell(pb); | |
390 put_tag(pb, &ix_tag[0]); /* ix?? */ | |
391 put_le32(pb, avi->indexes[i].entry * 8 + 24); | |
392 /* chunk size */ | |
119 | 393 put_le16(pb, 2); /* wLongsPerEntry */ |
887 | 394 put_byte(pb, 0); /* bIndexSubType (0 == frame index) */ |
395 put_byte(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */ | |
396 put_le32(pb, avi->indexes[i].entry); | |
397 /* nEntriesInUse */ | |
398 put_tag(pb, &tag[0]); /* dwChunkId */ | |
399 put_le64(pb, avi->movi_list);/* qwBaseOffset */ | |
400 put_le32(pb, 0); /* dwReserved_3 (must be 0) */ | |
119 | 401 |
402 for (j=0; j<avi->indexes[i].entry; j++) { | |
403 AVIIentry* ie = avi_get_ientry(&avi->indexes[i], j); | |
887 | 404 put_le32(pb, ie->pos + 8); |
405 put_le32(pb, ((uint32_t)ie->len & ~0x80000000) | | |
406 (ie->flags & 0x10 ? 0 : 0x80000000)); | |
119 | 407 } |
887 | 408 put_flush_packet(pb); |
119 | 409 pos = url_ftell(pb); |
885 | 410 |
887 | 411 /* Updating one entry in the AVI OpenDML master index */ |
412 url_fseek(pb, avi->indexes[i].indx_start - 8, SEEK_SET); | |
413 put_tag(pb, "indx"); /* enabling this entry */ | |
414 url_fskip(pb, 8); | |
415 put_le32(pb, avi->riff_id); /* nEntriesInUse */ | |
416 url_fskip(pb, 16*avi->riff_id); | |
417 put_le64(pb, ix); /* qwOffset */ | |
418 put_le32(pb, pos - ix); /* dwSize */ | |
419 put_le32(pb, avi->indexes[i].entry); /* dwDuration */ | |
119 | 420 |
887 | 421 url_fseek(pb, pos, SEEK_SET); |
119 | 422 } |
423 return 0; | |
424 } | |
425 | |
426 static int avi_write_idx1(AVFormatContext *s) | |
102
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
427 { |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2227
diff
changeset
|
428 ByteIOContext *pb = s->pb; |
102
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
429 AVIContext *avi = s->priv_data; |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3872
diff
changeset
|
430 int64_t idx_chunk; |
1267 | 431 int i; |
1332 | 432 char tag[5]; |
102
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
433 |
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
434 if (!url_is_streamed(pb)) { |
887 | 435 AVIIentry* ie = 0, *tie; |
436 int entry[MAX_STREAMS]; | |
437 int empty, stream_id = -1; | |
102
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
438 |
887 | 439 idx_chunk = start_tag(pb, "idx1"); |
440 memset(&entry[0], 0, sizeof(entry)); | |
441 do { | |
442 empty = 1; | |
443 for (i=0; i<s->nb_streams; i++) { | |
444 if (avi->indexes[i].entry <= entry[i]) | |
445 continue; | |
885 | 446 |
887 | 447 tie = avi_get_ientry(&avi->indexes[i], entry[i]); |
448 if (empty || tie->pos < ie->pos) { | |
449 ie = tie; | |
450 stream_id = i; | |
451 } | |
452 empty = 0; | |
453 } | |
454 if (!empty) { | |
455 avi_stream2fourcc(&tag[0], stream_id, | |
456 s->streams[stream_id]->codec->codec_type); | |
457 put_tag(pb, &tag[0]); | |
458 put_le32(pb, ie->flags); | |
119 | 459 put_le32(pb, ie->pos); |
460 put_le32(pb, ie->len); | |
887 | 461 entry[stream_id]++; |
462 } | |
463 } while (!empty); | |
464 end_tag(pb, idx_chunk); | |
102
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
465 |
1267 | 466 avi_write_counters(s, avi->riff_id); |
102
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
467 } |
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
468 return 0; |
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
469 } |
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
470 |
468 | 471 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt) |
0 | 472 { |
473 AVIContext *avi = s->priv_data; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2227
diff
changeset
|
474 ByteIOContext *pb = s->pb; |
0 | 475 unsigned char tag[5]; |
468 | 476 unsigned int flags=0; |
477 const int stream_index= pkt->stream_index; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
796
diff
changeset
|
478 AVCodecContext *enc= s->streams[stream_index]->codec; |
468 | 479 int size= pkt->size; |
102
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
480 |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1415
diff
changeset
|
481 // av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index); |
724 | 482 while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avi->packet_count[stream_index]){ |
479 | 483 AVPacket empty_packet; |
484 | |
485 av_init_packet(&empty_packet); | |
486 empty_packet.size= 0; | |
487 empty_packet.data= NULL; | |
488 empty_packet.stream_index= stream_index; | |
489 avi_write_packet(s, &empty_packet); | |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1415
diff
changeset
|
490 // av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avi->packet_count[stream_index]); |
479 | 491 } |
492 avi->packet_count[stream_index]++; | |
493 | |
873
7af647503b67
Support for streaming: dont write indexes and dont signal HAS_INDEX in header. Also set filesize to max in this case.
alex
parents:
863
diff
changeset
|
494 // Make sure to put an OpenDML chunk when the file size exceeds the limits |
885 | 495 if (!url_is_streamed(pb) && |
887 | 496 (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) { |
885 | 497 |
119 | 498 avi_write_ix(s); |
499 end_tag(pb, avi->movi_list); | |
885 | 500 |
887 | 501 if (avi->riff_id == 1) |
502 avi_write_idx1(s); | |
119 | 503 |
887 | 504 end_tag(pb, avi->riff_start); |
505 avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi"); | |
102
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
506 } |
885 | 507 |
119 | 508 avi_stream2fourcc(&tag[0], stream_index, enc->codec_type); |
468 | 509 if(pkt->flags&PKT_FLAG_KEY) |
510 flags = 0x10; | |
119 | 511 if (enc->codec_type == CODEC_TYPE_AUDIO) { |
0 | 512 avi->audio_strm_length[stream_index] += size; |
468 | 513 } |
0 | 514 |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2227
diff
changeset
|
515 if (!url_is_streamed(s->pb)) { |
119 | 516 AVIIndex* idx = &avi->indexes[stream_index]; |
887 | 517 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE; |
518 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE; | |
119 | 519 if (idx->ents_allocated <= idx->entry) { |
887 | 520 idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*)); |
521 if (!idx->cluster) | |
522 return -1; | |
119 | 523 idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry)); |
887 | 524 if (!idx->cluster[cl]) |
525 return -1; | |
526 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE; | |
527 } | |
885 | 528 |
887 | 529 idx->cluster[cl][id].flags = flags; |
119 | 530 idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list; |
531 idx->cluster[cl][id].len = size; | |
887 | 532 idx->entry++; |
0 | 533 } |
885 | 534 |
0 | 535 put_buffer(pb, tag, 4); |
536 put_le32(pb, size); | |
468 | 537 put_buffer(pb, pkt->data, size); |
0 | 538 if (size & 1) |
539 put_byte(pb, 0); | |
540 | |
541 put_flush_packet(pb); | |
542 return 0; | |
543 } | |
544 | |
545 static int avi_write_trailer(AVFormatContext *s) | |
546 { | |
102
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
547 AVIContext *avi = s->priv_data; |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2227
diff
changeset
|
548 ByteIOContext *pb = s->pb; |
102
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
549 int res = 0; |
119 | 550 int i, j, n, nb_frames; |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3872
diff
changeset
|
551 int64_t file_size; |
0 | 552 |
1676 | 553 if (!url_is_streamed(pb)){ |
554 if (avi->riff_id == 1) { | |
555 end_tag(pb, avi->movi_list); | |
556 res = avi_write_idx1(s); | |
557 end_tag(pb, avi->riff_start); | |
558 } else { | |
559 avi_write_ix(s); | |
560 end_tag(pb, avi->movi_list); | |
561 end_tag(pb, avi->riff_start); | |
119 | 562 |
1676 | 563 file_size = url_ftell(pb); |
564 url_fseek(pb, avi->odml_list - 8, SEEK_SET); | |
565 put_tag(pb, "LIST"); /* Making this AVI OpenDML one */ | |
566 url_fskip(pb, 16); | |
0 | 567 |
1676 | 568 for (n=nb_frames=0;n<s->nb_streams;n++) { |
569 AVCodecContext *stream = s->streams[n]->codec; | |
570 if (stream->codec_type == CODEC_TYPE_VIDEO) { | |
571 if (nb_frames < avi->packet_count[n]) | |
572 nb_frames = avi->packet_count[n]; | |
573 } else { | |
574 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) { | |
575 nb_frames += avi->packet_count[n]; | |
576 } | |
0 | 577 } |
578 } | |
1676 | 579 put_le32(pb, nb_frames); |
580 url_fseek(pb, file_size, SEEK_SET); | |
581 | |
582 avi_write_counters(s, avi->riff_id); | |
102
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
583 } |
873
7af647503b67
Support for streaming: dont write indexes and dont signal HAS_INDEX in header. Also set filesize to max in this case.
alex
parents:
863
diff
changeset
|
584 } |
119 | 585 put_flush_packet(pb); |
102
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
586 |
119 | 587 for (i=0; i<MAX_STREAMS; i++) { |
887 | 588 for (j=0; j<avi->indexes[i].ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++) |
119 | 589 av_free(avi->indexes[i].cluster[j]); |
887 | 590 av_free(avi->indexes[i].cluster); |
591 avi->indexes[i].cluster = NULL; | |
592 avi->indexes[i].ents_allocated = avi->indexes[i].entry = 0; | |
119 | 593 } |
885 | 594 |
102
c48108fe538e
AVI > 2Gb (OpenDML) generation patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
94
diff
changeset
|
595 return res; |
0 | 596 } |
597 | |
1169 | 598 AVOutputFormat avi_muxer = { |
0 | 599 "avi", |
3424
7a0230981402
Make long_names in lavf/lavdev optional depending on CONFIG_SMALL.
diego
parents:
3177
diff
changeset
|
600 NULL_IF_CONFIG_SMALL("AVI format"), |
0 | 601 "video/x-msvideo", |
602 "avi", | |
603 sizeof(AVIContext), | |
604 CODEC_ID_MP2, | |
106
4da5d55b3690
we really shouldnt use M$* as default codec -> use MPEG4 as default
michaelni
parents:
105
diff
changeset
|
605 CODEC_ID_MPEG4, |
0 | 606 avi_write_header, |
607 avi_write_packet, | |
608 avi_write_trailer, | |
3766
f062deeedb8d
Change codec_tag type from const struct AVCodecTag ** to const struct AVCodecTag * const *
reimar
parents:
3759
diff
changeset
|
609 .codec_tag= (const AVCodecTag* const []){codec_bmp_tags, codec_wav_tags, 0}, |
0 | 610 }; |
1169 | 611 #endif //CONFIG_AVI_MUXER |