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