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