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