Mercurial > libavformat.hg
annotate avidec.c @ 983:558381bf97d2 libavformat
support seeking in RenderAvi.avi (audio stream == single huge chunk)
author | michael |
---|---|
date | Wed, 01 Mar 2006 15:42:07 +0000 |
parents | c21f07d85158 |
children | b644fe79f7a2 |
rev | line source |
---|---|
0 | 1 /* |
2 * AVI decoder. | |
3 * Copyright (c) 2001 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:
887
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" | |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
21 #include "dv.h" |
0 | 22 |
700
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
23 #undef NDEBUG |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
24 #include <assert.h> |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
25 |
0 | 26 //#define DEBUG |
311 | 27 //#define DEBUG_SEEK |
0 | 28 |
311 | 29 typedef struct AVIStream { |
701 | 30 int64_t frame_offset; /* current frame (video) or byte (audio) counter |
311 | 31 (used to compute the pts) */ |
701 | 32 int remaining; |
33 int packet_size; | |
34 | |
311 | 35 int scale; |
885 | 36 int rate; |
982 | 37 int sample_size; /* size of one sample (or packet) (in the rate/scale sense) in bytes */ |
457 | 38 int start; |
885 | 39 |
977 | 40 int64_t cum_len; /* temporary storage (used during seek) */ |
885 | 41 |
519 | 42 int prefix; ///< normally 'd'<<8 + 'c' or 'w'<<8 + 'b' |
43 int prefix_count; | |
311 | 44 } AVIStream; |
0 | 45 |
46 typedef struct { | |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
47 int64_t riff_end; |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
48 int64_t movi_end; |
0 | 49 offset_t movi_list; |
311 | 50 int index_loaded; |
510 | 51 int is_odml; |
701 | 52 int non_interleaved; |
53 int stream_index; | |
296
252946de6d3f
* DV demuxer is now capable of decoding auxilary audio stream. So,
romansh
parents:
262
diff
changeset
|
54 DVDemuxContext* dv_demux; |
0 | 55 } AVIContext; |
56 | |
469
6a4cc19e8d9b
exporting keyframe flags, fixes keyframe stuff with streamcopy
michael
parents:
466
diff
changeset
|
57 static int avi_load_index(AVFormatContext *s); |
979 | 58 static int guess_ni_flag(AVFormatContext *s); |
469
6a4cc19e8d9b
exporting keyframe flags, fixes keyframe stuff with streamcopy
michael
parents:
466
diff
changeset
|
59 |
0 | 60 #ifdef DEBUG |
61 static void print_tag(const char *str, unsigned int tag, int size) | |
62 { | |
63 printf("%s: tag=%c%c%c%c size=0x%x\n", | |
64 str, tag & 0xff, | |
65 (tag >> 8) & 0xff, | |
66 (tag >> 16) & 0xff, | |
67 (tag >> 24) & 0xff, | |
68 size); | |
69 } | |
70 #endif | |
71 | |
92
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
72 static int get_riff(AVIContext *avi, ByteIOContext *pb) |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
73 { |
885 | 74 uint32_t tag; |
92
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
75 /* check RIFF header */ |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
76 tag = get_le32(pb); |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
77 |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
78 if (tag != MKTAG('R', 'I', 'F', 'F')) |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
79 return -1; |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
80 avi->riff_end = get_le32(pb); /* RIFF chunk size */ |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
81 avi->riff_end += url_ftell(pb); /* RIFF chunk end */ |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
82 tag = get_le32(pb); |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
83 if (tag != MKTAG('A', 'V', 'I', ' ') && tag != MKTAG('A', 'V', 'I', 'X')) |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
84 return -1; |
885 | 85 |
92
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
86 return 0; |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
87 } |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
88 |
977 | 89 static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){ |
90 ByteIOContext *pb = &s->pb; | |
91 int longs_pre_entry= get_le16(pb); | |
92 int index_sub_type = get_byte(pb); | |
93 int index_type = get_byte(pb); | |
94 int entries_in_use = get_le32(pb); | |
95 int chunk_id = get_le32(pb); | |
96 int64_t base = get_le64(pb); | |
97 int stream_id= 10*((chunk_id&0xFF) - '0') + (((chunk_id>>8)&0xFF) - '0'); | |
98 AVStream *st; | |
99 AVIStream *ast; | |
100 int i; | |
101 | |
102 // av_log(s, AV_LOG_ERROR, "longs_pre_entry:%d index_type:%d entries_in_use:%d chunk_id:%X base:%Ld\n", | |
103 // longs_pre_entry,index_type, entries_in_use, chunk_id, base); | |
104 | |
105 if(stream_id > s->nb_streams || stream_id < 0) | |
106 return -1; | |
107 st= s->streams[stream_id]; | |
108 ast = st->priv_data; | |
109 | |
110 if(index_sub_type) | |
111 return -1; | |
112 | |
113 get_le32(pb); | |
114 | |
115 if(index_type && longs_pre_entry != 2) | |
116 return -1; | |
117 if(index_type>1) | |
118 return -1; | |
119 | |
120 for(i=0; i<entries_in_use; i++){ | |
121 if(index_type){ | |
979 | 122 int64_t pos= get_le32(pb) + base - 8; |
977 | 123 int len = get_le32(pb); |
979 | 124 int key= len >= 0; |
125 len &= 0x7FFFFFFF; | |
977 | 126 |
979 | 127 //av_log(s, AV_LOG_ERROR, "pos:%Ld, len:%X\n", pos, len); |
128 av_add_index_entry(st, pos, ast->cum_len, len, 0, key ? AVINDEX_KEYFRAME : 0); | |
977 | 129 |
130 if(ast->sample_size) | |
131 ast->cum_len += len / ast->sample_size; | |
132 else | |
133 ast->cum_len ++; | |
134 }else{ | |
135 int64_t offset= get_le64(pb); | |
136 int size = get_le32(pb); | |
137 int duration = get_le32(pb); | |
138 int64_t pos= url_ftell(pb); | |
139 | |
140 url_fseek(pb, offset+8, SEEK_SET); | |
141 read_braindead_odml_indx(s, frame_num); | |
142 frame_num += duration; | |
143 | |
144 url_fseek(pb, pos, SEEK_SET); | |
145 } | |
146 } | |
147 return 0; | |
148 } | |
149 | |
983
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
150 static void clean_index(AVFormatContext *s){ |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
151 int i, j; |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
152 |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
153 for(i=0; i<s->nb_streams; i++){ |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
154 AVStream *st = s->streams[i]; |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
155 AVIStream *ast = st->priv_data; |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
156 int n= st->nb_index_entries; |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
157 int max= ast->sample_size; |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
158 int64_t pos, size, ts; |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
159 |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
160 if(n != 1 || ast->sample_size==0) |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
161 continue; |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
162 |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
163 while(max < 1024) max+=max; |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
164 |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
165 pos= st->index_entries[0].pos; |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
166 size= st->index_entries[0].size; |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
167 ts= st->index_entries[0].timestamp; |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
168 |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
169 for(j=0; j<size; j+=max){ |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
170 av_add_index_entry(st, pos+j, ts + j/ast->sample_size, FFMIN(max, size-j), 0, AVINDEX_KEYFRAME); |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
171 } |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
172 } |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
173 } |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
174 |
0 | 175 static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap) |
176 { | |
177 AVIContext *avi = s->priv_data; | |
178 ByteIOContext *pb = &s->pb; | |
98
cae6ddfadf51
AVI type 1 support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
92
diff
changeset
|
179 uint32_t tag, tag1, handler; |
703 | 180 int codec_type, stream_index, frame_period, bit_rate; |
188
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
181 unsigned int size, nb_frames; |
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
182 int i, n; |
0 | 183 AVStream *st; |
311 | 184 AVIStream *ast; |
227 | 185 int xan_video = 0; /* hack to support Xan A/V */ |
0 | 186 |
701 | 187 avi->stream_index= -1; |
885 | 188 |
92
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
189 if (get_riff(avi, pb) < 0) |
0 | 190 return -1; |
191 | |
192 /* first list tag */ | |
193 stream_index = -1; | |
194 codec_type = -1; | |
195 frame_period = 0; | |
196 for(;;) { | |
197 if (url_feof(pb)) | |
198 goto fail; | |
199 tag = get_le32(pb); | |
200 size = get_le32(pb); | |
201 #ifdef DEBUG | |
202 print_tag("tag", tag, size); | |
203 #endif | |
204 | |
205 switch(tag) { | |
206 case MKTAG('L', 'I', 'S', 'T'): | |
207 /* ignored, except when start of video packets */ | |
208 tag1 = get_le32(pb); | |
209 #ifdef DEBUG | |
210 print_tag("list", tag1, 0); | |
211 #endif | |
212 if (tag1 == MKTAG('m', 'o', 'v', 'i')) { | |
311 | 213 avi->movi_list = url_ftell(pb) - 4; |
495 | 214 if(size) avi->movi_end = avi->movi_list + size; |
764
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
743
diff
changeset
|
215 else avi->movi_end = url_fsize(pb); |
0 | 216 #ifdef DEBUG |
217 printf("movi end=%Lx\n", avi->movi_end); | |
218 #endif | |
219 goto end_of_header; | |
220 } | |
221 break; | |
510 | 222 case MKTAG('d', 'm', 'l', 'h'): |
887 | 223 avi->is_odml = 1; |
224 url_fskip(pb, size + (size & 1)); | |
225 break; | |
0 | 226 case MKTAG('a', 'v', 'i', 'h'): |
887 | 227 /* avi header */ |
0 | 228 /* using frame_period is bad idea */ |
229 frame_period = get_le32(pb); | |
230 bit_rate = get_le32(pb) * 8; | |
981
55519fa957c3
fix demuxing of XviD_with_3_AAC-HE_audio_streams.avi
michael
parents:
980
diff
changeset
|
231 get_le32(pb); |
55519fa957c3
fix demuxing of XviD_with_3_AAC-HE_audio_streams.avi
michael
parents:
980
diff
changeset
|
232 avi->non_interleaved |= get_le32(pb) & AVIF_MUSTUSEINDEX; |
55519fa957c3
fix demuxing of XviD_with_3_AAC-HE_audio_streams.avi
michael
parents:
980
diff
changeset
|
233 |
55519fa957c3
fix demuxing of XviD_with_3_AAC-HE_audio_streams.avi
michael
parents:
980
diff
changeset
|
234 url_fskip(pb, 2 * 4); |
188
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
235 n = get_le32(pb); |
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
236 for(i=0;i<n;i++) { |
311 | 237 AVIStream *ast; |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
238 st = av_new_stream(s, i); |
0 | 239 if (!st) |
240 goto fail; | |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
457
diff
changeset
|
241 |
311 | 242 ast = av_mallocz(sizeof(AVIStream)); |
243 if (!ast) | |
244 goto fail; | |
245 st->priv_data = ast; | |
887 | 246 } |
0 | 247 url_fskip(pb, size - 7 * 4); |
248 break; | |
249 case MKTAG('s', 't', 'r', 'h'): | |
250 /* stream header */ | |
251 stream_index++; | |
252 tag1 = get_le32(pb); | |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
253 handler = get_le32(pb); /* codec tag */ |
471 | 254 #ifdef DEBUG |
255 print_tag("strh", tag1, -1); | |
256 #endif | |
703 | 257 if(tag1 == MKTAG('i', 'a', 'v', 's') || tag1 == MKTAG('i', 'v', 'a', 's')){ |
885 | 258 /* |
887 | 259 * After some consideration -- I don't think we |
260 * have to support anything but DV in a type1 AVIs. | |
261 */ | |
262 if (s->nb_streams != 1) | |
263 goto fail; | |
885 | 264 |
887 | 265 if (handler != MKTAG('d', 'v', 's', 'd') && |
266 handler != MKTAG('d', 'v', 'h', 'd') && | |
267 handler != MKTAG('d', 'v', 's', 'l')) | |
268 goto fail; | |
885 | 269 |
887 | 270 ast = s->streams[0]->priv_data; |
271 av_freep(&s->streams[0]->codec->extradata); | |
272 av_freep(&s->streams[0]); | |
273 s->nb_streams = 0; | |
274 avi->dv_demux = dv_init_demux(s); | |
275 if (!avi->dv_demux) | |
276 goto fail; | |
277 s->streams[0]->priv_data = ast; | |
278 url_fskip(pb, 3 * 4); | |
279 ast->scale = get_le32(pb); | |
280 ast->rate = get_le32(pb); | |
281 stream_index = s->nb_streams - 1; | |
282 url_fskip(pb, size - 7*4); | |
703 | 283 break; |
284 } | |
285 | |
286 if (stream_index >= s->nb_streams) { | |
287 url_fskip(pb, size - 8); | |
288 break; | |
885 | 289 } |
703 | 290 st = s->streams[stream_index]; |
291 ast = st->priv_data; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
292 st->codec->stream_codec_tag= handler; |
703 | 293 |
294 get_le32(pb); /* flags */ | |
295 get_le16(pb); /* priority */ | |
296 get_le16(pb); /* language */ | |
297 get_le32(pb); /* initial frame */ | |
298 ast->scale = get_le32(pb); | |
299 ast->rate = get_le32(pb); | |
300 if(ast->scale && ast->rate){ | |
301 }else if(frame_period){ | |
302 ast->rate = 1000000; | |
303 ast->scale = frame_period; | |
304 }else{ | |
305 ast->rate = 25; | |
306 ast->scale = 1; | |
307 } | |
308 av_set_pts_info(st, 64, ast->scale, ast->rate); | |
885 | 309 |
703 | 310 ast->start= get_le32(pb); /* start */ |
311 nb_frames = get_le32(pb); | |
312 | |
313 st->start_time = 0; | |
743 | 314 st->duration = nb_frames; |
703 | 315 get_le32(pb); /* buffer size */ |
316 get_le32(pb); /* quality */ | |
317 ast->sample_size = get_le32(pb); /* sample ssize */ | |
318 // av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d\n", ast->rate, ast->scale, ast->start, ast->sample_size); | |
319 | |
320 switch(tag1) { | |
887 | 321 case MKTAG('v', 'i', 'd', 's'): |
0 | 322 codec_type = CODEC_TYPE_VIDEO; |
75
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
323 |
703 | 324 ast->sample_size = 0; |
0 | 325 break; |
326 case MKTAG('a', 'u', 'd', 's'): | |
703 | 327 codec_type = CODEC_TYPE_AUDIO; |
73
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
328 break; |
471 | 329 case MKTAG('t', 'x', 't', 's'): |
885 | 330 //FIXME |
471 | 331 codec_type = CODEC_TYPE_DATA; //CODEC_TYPE_SUB ? FIXME |
332 break; | |
534 | 333 case MKTAG('p', 'a', 'd', 's'): |
334 codec_type = CODEC_TYPE_UNKNOWN; | |
335 stream_index--; | |
336 break; | |
73
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
337 default: |
534 | 338 av_log(s, AV_LOG_ERROR, "unknown stream type %X\n", tag1); |
73
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
339 goto fail; |
0 | 340 } |
703 | 341 url_fskip(pb, size - 12 * 4); |
0 | 342 break; |
343 case MKTAG('s', 't', 'r', 'f'): | |
344 /* stream header */ | |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
345 if (stream_index >= s->nb_streams || avi->dv_demux) { |
0 | 346 url_fskip(pb, size); |
347 } else { | |
348 st = s->streams[stream_index]; | |
349 switch(codec_type) { | |
350 case CODEC_TYPE_VIDEO: | |
351 get_le32(pb); /* size */ | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
352 st->codec->width = get_le32(pb); |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
353 st->codec->height = get_le32(pb); |
0 | 354 get_le16(pb); /* panes */ |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
355 st->codec->bits_per_sample= get_le16(pb); /* depth */ |
0 | 356 tag1 = get_le32(pb); |
75
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
357 get_le32(pb); /* ImageSize */ |
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
358 get_le32(pb); /* XPelsPerMeter */ |
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
359 get_le32(pb); /* YPelsPerMeter */ |
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
360 get_le32(pb); /* ClrUsed */ |
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
361 get_le32(pb); /* ClrImportant */ |
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
362 |
639 | 363 if(size > 10*4 && size<(1<<30)){ |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
364 st->codec->extradata_size= size - 10*4; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
365 st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
366 get_buffer(pb, st->codec->extradata, st->codec->extradata_size); |
639 | 367 } |
885 | 368 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
369 if(st->codec->extradata_size & 1) //FIXME check if the encoder really did this correctly |
76 | 370 get_byte(pb); |
75
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
371 |
297
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
372 /* Extract palette from extradata if bpp <= 8 */ |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
373 /* This code assumes that extradata contains only palette */ |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
374 /* This is true for all paletted codecs implemented in ffmpeg */ |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
375 if (st->codec->extradata_size && (st->codec->bits_per_sample <= 8)) { |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
376 st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl)); |
297
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
377 #ifdef WORDS_BIGENDIAN |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
378 for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++) |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
379 st->codec->palctrl->palette[i] = bswap_32(((uint32_t*)st->codec->extradata)[i]); |
297
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
380 #else |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
381 memcpy(st->codec->palctrl->palette, st->codec->extradata, |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
382 FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)); |
297
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
383 #endif |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
384 st->codec->palctrl->palette_changed = 1; |
297
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
385 } |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
386 |
0 | 387 #ifdef DEBUG |
388 print_tag("video", tag1, 0); | |
389 #endif | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
390 st->codec->codec_type = CODEC_TYPE_VIDEO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
391 st->codec->codec_tag = tag1; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
392 st->codec->codec_id = codec_get_id(codec_bmp_tags, tag1); |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
393 if (st->codec->codec_id == CODEC_ID_XAN_WC4) |
227 | 394 xan_video = 1; |
842 | 395 st->need_parsing = 2; //only parse headers dont do slower repacketization, this is needed to get the pict type which is needed for generating correct pts |
75
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
396 // url_fskip(pb, size - 5 * 4); |
0 | 397 break; |
73
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
398 case CODEC_TYPE_AUDIO: |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
399 get_wav_header(pb, st->codec, size); |
13
8a5285a0ca2f
Fix for odd strf tag in Stargate SG-1 - 3x18 - Shades of Grey.avi
mmu_man
parents:
5
diff
changeset
|
400 if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */ |
8a5285a0ca2f
Fix for odd strf tag in Stargate SG-1 - 3x18 - Shades of Grey.avi
mmu_man
parents:
5
diff
changeset
|
401 url_fskip(pb, 1); |
227 | 402 /* special case time: To support Xan DPCM, hardcode |
403 * the format if Xxan is the video codec */ | |
311 | 404 st->need_parsing = 1; |
405 /* force parsing as several audio frames can be in | |
406 one packet */ | |
227 | 407 if (xan_video) |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
408 st->codec->codec_id = CODEC_ID_XAN_DPCM; |
0 | 409 break; |
410 default: | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
411 st->codec->codec_type = CODEC_TYPE_DATA; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
412 st->codec->codec_id= CODEC_ID_NONE; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
413 st->codec->codec_tag= 0; |
0 | 414 url_fskip(pb, size); |
415 break; | |
416 } | |
417 } | |
418 break; | |
977 | 419 case MKTAG('i', 'n', 'd', 'x'): |
420 i= url_ftell(pb); | |
421 read_braindead_odml_indx(s, 0); | |
422 avi->index_loaded=1; | |
423 url_fseek(pb, i+size, SEEK_SET); | |
424 break; | |
0 | 425 default: |
426 /* skip tag */ | |
427 size += (size & 1); | |
428 url_fskip(pb, size); | |
429 break; | |
430 } | |
431 } | |
432 end_of_header: | |
433 /* check stream number */ | |
434 if (stream_index != s->nb_streams - 1) { | |
435 fail: | |
436 for(i=0;i<s->nb_streams;i++) { | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
437 av_freep(&s->streams[i]->codec->extradata); |
0 | 438 av_freep(&s->streams[i]); |
439 } | |
440 return -1; | |
441 } | |
442 | |
977 | 443 if(!avi->index_loaded) |
444 avi_load_index(s); | |
469
6a4cc19e8d9b
exporting keyframe flags, fixes keyframe stuff with streamcopy
michael
parents:
466
diff
changeset
|
445 avi->index_loaded = 1; |
979 | 446 avi->non_interleaved |= guess_ni_flag(s); |
983
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
447 if(avi->non_interleaved) |
558381bf97d2
support seeking in RenderAvi.avi (audio stream == single huge chunk)
michael
parents:
982
diff
changeset
|
448 clean_index(s); |
885 | 449 |
0 | 450 return 0; |
451 } | |
452 | |
453 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt) | |
454 { | |
455 AVIContext *avi = s->priv_data; | |
456 ByteIOContext *pb = &s->pb; | |
510 | 457 int n, d[8], size; |
569 | 458 offset_t i, sync; |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
459 void* dstr; |
885 | 460 |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
461 if (avi->dv_demux) { |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
462 size = dv_get_packet(avi->dv_demux, pkt); |
887 | 463 if (size >= 0) |
464 return size; | |
149 | 465 } |
885 | 466 |
701 | 467 if(avi->non_interleaved){ |
851
62e66722e9bb
Kill some compiler warnings. Compiled code verified identical after changes.
mru
parents:
842
diff
changeset
|
468 int best_stream_index = 0; |
701 | 469 AVStream *best_st= NULL; |
470 AVIStream *best_ast; | |
471 int64_t best_ts= INT64_MAX; | |
472 int i; | |
885 | 473 |
701 | 474 for(i=0; i<s->nb_streams; i++){ |
475 AVStream *st = s->streams[i]; | |
476 AVIStream *ast = st->priv_data; | |
477 int64_t ts= ast->frame_offset; | |
569 | 478 |
701 | 479 if(ast->sample_size) |
480 ts /= ast->sample_size; | |
481 ts= av_rescale(ts, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den); | |
482 | |
483 // av_log(NULL, AV_LOG_DEBUG, "%Ld %d/%d %Ld\n", ts, st->time_base.num, st->time_base.den, ast->frame_offset); | |
484 if(ts < best_ts){ | |
485 best_ts= ts; | |
486 best_st= st; | |
487 best_stream_index= i; | |
488 } | |
489 } | |
490 best_ast = best_st->priv_data; | |
491 best_ts= av_rescale(best_ts, best_st->time_base.den, AV_TIME_BASE * (int64_t)best_st->time_base.num); //FIXME a little ugly | |
492 if(best_ast->remaining) | |
493 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD); | |
494 else | |
495 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY); | |
496 | |
497 // av_log(NULL, AV_LOG_DEBUG, "%d\n", i); | |
498 if(i>=0){ | |
499 int64_t pos= best_st->index_entries[i].pos; | |
980 | 500 pos += best_ast->packet_size - best_ast->remaining; |
981
55519fa957c3
fix demuxing of XviD_with_3_AAC-HE_audio_streams.avi
michael
parents:
980
diff
changeset
|
501 url_fseek(&s->pb, pos + 8, SEEK_SET); |
701 | 502 // av_log(NULL, AV_LOG_DEBUG, "pos=%Ld\n", pos); |
885 | 503 |
982 | 504 assert(best_ast->remaining <= best_ast->packet_size); |
505 | |
981
55519fa957c3
fix demuxing of XviD_with_3_AAC-HE_audio_streams.avi
michael
parents:
980
diff
changeset
|
506 avi->stream_index= best_stream_index; |
55519fa957c3
fix demuxing of XviD_with_3_AAC-HE_audio_streams.avi
michael
parents:
980
diff
changeset
|
507 if(!best_ast->remaining) |
982 | 508 best_ast->packet_size= |
981
55519fa957c3
fix demuxing of XviD_with_3_AAC-HE_audio_streams.avi
michael
parents:
980
diff
changeset
|
509 best_ast->remaining= best_st->index_entries[i].size; |
701 | 510 } |
511 } | |
885 | 512 |
569 | 513 resync: |
701 | 514 if(avi->stream_index >= 0){ |
515 AVStream *st= s->streams[ avi->stream_index ]; | |
516 AVIStream *ast= st->priv_data; | |
517 int size; | |
885 | 518 |
701 | 519 if(ast->sample_size == 0) |
520 size= INT_MAX; | |
885 | 521 else if(ast->sample_size < 32) |
701 | 522 size= 64*ast->sample_size; |
523 else | |
524 size= ast->sample_size; | |
525 | |
526 if(size > ast->remaining) | |
527 size= ast->remaining; | |
775 | 528 av_get_packet(pb, pkt, size); |
885 | 529 |
701 | 530 if (avi->dv_demux) { |
531 dstr = pkt->destruct; | |
532 size = dv_produce_packet(avi->dv_demux, pkt, | |
533 pkt->data, pkt->size); | |
534 pkt->destruct = dstr; | |
535 pkt->flags |= PKT_FLAG_KEY; | |
536 } else { | |
537 /* XXX: how to handle B frames in avi ? */ | |
538 pkt->dts = ast->frame_offset; | |
539 // pkt->dts += ast->start; | |
540 if(ast->sample_size) | |
541 pkt->dts /= ast->sample_size; | |
982 | 542 //av_log(NULL, AV_LOG_DEBUG, "dts:%Ld offset:%Ld %d/%d smpl_siz:%d base:%d st:%d size:%d\n", pkt->dts, ast->frame_offset, ast->scale, ast->rate, ast->sample_size, AV_TIME_BASE, avi->stream_index, size); |
701 | 543 pkt->stream_index = avi->stream_index; |
544 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
545 if (st->codec->codec_type == CODEC_TYPE_VIDEO) { |
701 | 546 if(st->index_entries){ |
547 AVIndexEntry *e; | |
548 int index; | |
549 | |
550 index= av_index_search_timestamp(st, pkt->dts, 0); | |
551 e= &st->index_entries[index]; | |
885 | 552 |
718 | 553 if(index >= 0 && e->timestamp == ast->frame_offset){ |
701 | 554 if (e->flags & AVINDEX_KEYFRAME) |
555 pkt->flags |= PKT_FLAG_KEY; | |
556 } | |
557 } else { | |
558 /* if no index, better to say that all frames | |
559 are key frames */ | |
560 pkt->flags |= PKT_FLAG_KEY; | |
561 } | |
562 } else { | |
885 | 563 pkt->flags |= PKT_FLAG_KEY; |
701 | 564 } |
565 if(ast->sample_size) | |
566 ast->frame_offset += pkt->size; | |
567 else | |
568 ast->frame_offset++; | |
569 } | |
570 ast->remaining -= size; | |
571 if(!ast->remaining){ | |
572 avi->stream_index= -1; | |
573 ast->packet_size= 0; | |
574 if (size & 1) { | |
575 get_byte(pb); | |
576 size++; | |
577 } | |
578 } | |
579 | |
580 return size; | |
581 } | |
582 | |
569 | 583 memset(d, -1, sizeof(int)*8); |
584 for(i=sync=url_ftell(pb); !url_feof(pb); i++) { | |
82 | 585 int j; |
586 | |
887 | 587 if (i >= avi->movi_end) { |
588 if (avi->is_odml) { | |
589 url_fskip(pb, avi->riff_end - i); | |
590 avi->riff_end = avi->movi_end = url_fsize(pb); | |
591 } else | |
592 break; | |
593 } | |
92
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
594 |
82 | 595 for(j=0; j<7; j++) |
596 d[j]= d[j+1]; | |
597 d[7]= get_byte(pb); | |
885 | 598 |
82 | 599 size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24); |
885 | 600 |
519 | 601 if( d[2] >= '0' && d[2] <= '9' |
602 && d[3] >= '0' && d[3] <= '9'){ | |
603 n= (d[2] - '0') * 10 + (d[3] - '0'); | |
604 }else{ | |
605 n= 100; //invalid stream id | |
606 } | |
607 //av_log(NULL, AV_LOG_DEBUG, "%X %X %X %X %X %X %X %X %lld %d %d\n", d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n); | |
608 if(i + size > avi->movi_end || d[0]<0) | |
609 continue; | |
885 | 610 |
82 | 611 //parse ix## |
519 | 612 if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams) |
887 | 613 //parse JUNK |
519 | 614 ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')){ |
82 | 615 url_fskip(pb, size); |
519 | 616 //av_log(NULL, AV_LOG_DEBUG, "SKIP\n"); |
569 | 617 goto resync; |
82 | 618 } |
510 | 619 |
519 | 620 if( d[0] >= '0' && d[0] <= '9' |
621 && d[1] >= '0' && d[1] <= '9'){ | |
622 n= (d[0] - '0') * 10 + (d[1] - '0'); | |
623 }else{ | |
624 n= 100; //invalid stream id | |
510 | 625 } |
885 | 626 |
82 | 627 //parse ##dc/##wb |
705 | 628 if(n < s->nb_streams){ |
519 | 629 AVStream *st; |
630 AVIStream *ast; | |
631 st = s->streams[n]; | |
632 ast = st->priv_data; | |
885 | 633 |
708 | 634 if( (st->discard >= AVDISCARD_DEFAULT && size==0) |
635 /*|| (st->discard >= AVDISCARD_NONKEY && !(pkt->flags & PKT_FLAG_KEY))*/ //FIXME needs a little reordering | |
636 || st->discard >= AVDISCARD_ALL){ | |
637 if(ast->sample_size) ast->frame_offset += pkt->size; | |
638 else ast->frame_offset++; | |
650 | 639 url_fskip(pb, size); |
640 goto resync; | |
641 } | |
519 | 642 |
885 | 643 if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) || |
519 | 644 d[2]*256+d[3] == ast->prefix /*|| |
885 | 645 (d[2] == 'd' && d[3] == 'c') || |
887 | 646 (d[2] == 'w' && d[3] == 'b')*/) { |
519 | 647 |
648 //av_log(NULL, AV_LOG_DEBUG, "OK\n"); | |
649 if(d[2]*256+d[3] == ast->prefix) | |
650 ast->prefix_count++; | |
651 else{ | |
652 ast->prefix= d[2]*256+d[3]; | |
653 ast->prefix_count= 0; | |
654 } | |
655 | |
701 | 656 avi->stream_index= n; |
657 ast->packet_size= size + 8; | |
658 ast->remaining= size; | |
659 goto resync; | |
519 | 660 } |
82 | 661 } |
520
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
662 /* palette changed chunk */ |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
663 if ( d[0] >= '0' && d[0] <= '9' |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
664 && d[1] >= '0' && d[1] <= '9' |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
665 && ((d[2] == 'p' && d[3] == 'c')) |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
666 && n < s->nb_streams && i + size <= avi->movi_end) { |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
667 |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
668 AVStream *st; |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
669 int first, clr, flags, k, p; |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
670 |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
671 st = s->streams[n]; |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
672 |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
673 first = get_byte(pb); |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
674 clr = get_byte(pb); |
887 | 675 if(!clr) /* all 256 colors used */ |
676 clr = 256; | |
520
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
677 flags = get_le16(pb); |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
678 p = 4; |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
679 for (k = first; k < clr + first; k++) { |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
680 int r, g, b; |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
681 r = get_byte(pb); |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
682 g = get_byte(pb); |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
683 b = get_byte(pb); |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
684 get_byte(pb); |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
685 st->codec->palctrl->palette[k] = b + (g << 8) + (r << 16); |
520
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
686 } |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
687 st->codec->palctrl->palette_changed = 1; |
569 | 688 goto resync; |
520
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
689 } |
5d96fe8f6560
added support for the elusive AVI palette change chunk, courtesy of
melanson
parents:
519
diff
changeset
|
690 |
82 | 691 } |
519 | 692 |
82 | 693 return -1; |
0 | 694 } |
695 | |
311 | 696 /* XXX: we make the implicit supposition that the position are sorted |
697 for each stream */ | |
698 static int avi_read_idx1(AVFormatContext *s, int size) | |
699 { | |
701 | 700 AVIContext *avi = s->priv_data; |
311 | 701 ByteIOContext *pb = &s->pb; |
702 int nb_index_entries, i; | |
703 AVStream *st; | |
704 AVIStream *ast; | |
705 unsigned int index, tag, flags, pos, len; | |
701 | 706 unsigned last_pos= -1; |
885 | 707 |
311 | 708 nb_index_entries = size / 16; |
709 if (nb_index_entries <= 0) | |
710 return -1; | |
711 | |
712 /* read the entries and sort them in each stream component */ | |
713 for(i = 0; i < nb_index_entries; i++) { | |
714 tag = get_le32(pb); | |
715 flags = get_le32(pb); | |
716 pos = get_le32(pb); | |
717 len = get_le32(pb); | |
700
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
718 #if defined(DEBUG_SEEK) |
885 | 719 av_log(NULL, AV_LOG_DEBUG, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/", |
311 | 720 i, tag, flags, pos, len); |
721 #endif | |
701 | 722 if(i==0 && pos > avi->movi_list) |
723 avi->movi_list= 0; //FIXME better check | |
980 | 724 pos += avi->movi_list; |
701 | 725 |
311 | 726 index = ((tag & 0xff) - '0') * 10; |
727 index += ((tag >> 8) & 0xff) - '0'; | |
728 if (index >= s->nb_streams) | |
729 continue; | |
730 st = s->streams[index]; | |
731 ast = st->priv_data; | |
885 | 732 |
700
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
733 #if defined(DEBUG_SEEK) |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
734 av_log(NULL, AV_LOG_DEBUG, "%d cum_len=%d\n", len, ast->cum_len); |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
735 #endif |
705 | 736 if(last_pos == pos) |
737 avi->non_interleaved= 1; | |
738 else | |
979 | 739 av_add_index_entry(st, pos, ast->cum_len, len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0); |
701 | 740 if(ast->sample_size) |
741 ast->cum_len += len / ast->sample_size; | |
742 else | |
743 ast->cum_len ++; | |
744 last_pos= pos; | |
311 | 745 } |
746 return 0; | |
747 } | |
748 | |
701 | 749 static int guess_ni_flag(AVFormatContext *s){ |
750 int i; | |
751 int64_t last_start=0; | |
752 int64_t first_end= INT64_MAX; | |
885 | 753 |
701 | 754 for(i=0; i<s->nb_streams; i++){ |
755 AVStream *st = s->streams[i]; | |
756 int n= st->nb_index_entries; | |
757 | |
758 if(n <= 0) | |
759 continue; | |
760 | |
761 if(st->index_entries[0].pos > last_start) | |
762 last_start= st->index_entries[0].pos; | |
763 if(st->index_entries[n-1].pos < first_end) | |
764 first_end= st->index_entries[n-1].pos; | |
765 } | |
766 return last_start > first_end; | |
767 } | |
768 | |
311 | 769 static int avi_load_index(AVFormatContext *s) |
770 { | |
771 AVIContext *avi = s->priv_data; | |
772 ByteIOContext *pb = &s->pb; | |
773 uint32_t tag, size; | |
343 | 774 offset_t pos= url_ftell(pb); |
885 | 775 |
311 | 776 url_fseek(pb, avi->movi_end, SEEK_SET); |
777 #ifdef DEBUG_SEEK | |
778 printf("movi_end=0x%llx\n", avi->movi_end); | |
779 #endif | |
780 for(;;) { | |
781 if (url_feof(pb)) | |
782 break; | |
783 tag = get_le32(pb); | |
784 size = get_le32(pb); | |
785 #ifdef DEBUG_SEEK | |
786 printf("tag=%c%c%c%c size=0x%x\n", | |
787 tag & 0xff, | |
788 (tag >> 8) & 0xff, | |
789 (tag >> 16) & 0xff, | |
790 (tag >> 24) & 0xff, | |
791 size); | |
792 #endif | |
793 switch(tag) { | |
794 case MKTAG('i', 'd', 'x', '1'): | |
795 if (avi_read_idx1(s, size) < 0) | |
796 goto skip; | |
797 else | |
798 goto the_end; | |
799 break; | |
800 default: | |
801 skip: | |
802 size += (size & 1); | |
803 url_fskip(pb, size); | |
804 break; | |
805 } | |
806 } | |
807 the_end: | |
343 | 808 url_fseek(pb, pos, SEEK_SET); |
311 | 809 return 0; |
810 } | |
811 | |
558 | 812 static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) |
311 | 813 { |
814 AVIContext *avi = s->priv_data; | |
815 AVStream *st; | |
700
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
816 int i, index; |
311 | 817 int64_t pos; |
818 | |
819 if (!avi->index_loaded) { | |
820 /* we only load the index on demand */ | |
821 avi_load_index(s); | |
822 avi->index_loaded = 1; | |
823 } | |
700
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
824 assert(stream_index>= 0); |
311 | 825 |
826 st = s->streams[stream_index]; | |
700
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
827 index= av_index_search_timestamp(st, timestamp, flags); |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
828 if(index<0) |
311 | 829 return -1; |
885 | 830 |
311 | 831 /* find the position */ |
700
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
832 pos = st->index_entries[index].pos; |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
833 timestamp = st->index_entries[index].timestamp; |
311 | 834 |
700
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
835 // av_log(NULL, AV_LOG_DEBUG, "XX %Ld %d %Ld\n", timestamp, index, st->index_entries[index].timestamp); |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
836 |
311 | 837 for(i = 0; i < s->nb_streams; i++) { |
700
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
838 AVStream *st2 = s->streams[i]; |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
839 AVIStream *ast2 = st2->priv_data; |
701 | 840 |
841 ast2->packet_size= | |
842 ast2->remaining= 0; | |
843 | |
700
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
844 if (st2->nb_index_entries <= 0) |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
845 continue; |
885 | 846 |
700
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
847 // assert(st2->codec.block_align); |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
848 assert(st2->time_base.den == ast2->rate); |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
849 assert(st2->time_base.num == ast2->scale); |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
850 index = av_index_search_timestamp( |
885 | 851 st2, |
700
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
852 av_rescale(timestamp, st2->time_base.den*(int64_t)st->time_base.num, st->time_base.den * (int64_t)st2->time_base.num), |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
853 flags | AVSEEK_FLAG_BACKWARD); |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
854 if(index<0) |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
855 index=0; |
885 | 856 |
701 | 857 if(!avi->non_interleaved){ |
858 while(index>0 && st2->index_entries[index].pos > pos) | |
859 index--; | |
860 while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos) | |
861 index++; | |
862 } | |
863 | |
700
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
864 // av_log(NULL, AV_LOG_DEBUG, "%Ld %d %Ld\n", timestamp, index, st2->index_entries[index].timestamp); |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
865 /* extract the current frame number */ |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
866 ast2->frame_offset = st2->index_entries[index].timestamp; |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
867 if(ast2->sample_size) |
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
868 ast2->frame_offset *=ast2->sample_size; |
311 | 869 } |
700
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
870 |
562
bf3231dd1d7c
* static allocation for audio packets. This will make it a little bit
romansh
parents:
558
diff
changeset
|
871 if (avi->dv_demux) |
bf3231dd1d7c
* static allocation for audio packets. This will make it a little bit
romansh
parents:
558
diff
changeset
|
872 dv_flush_audio_packets(avi->dv_demux); |
311 | 873 /* do the seek */ |
874 url_fseek(&s->pb, pos, SEEK_SET); | |
701 | 875 avi->stream_index= -1; |
311 | 876 return 0; |
877 } | |
878 | |
0 | 879 static int avi_read_close(AVFormatContext *s) |
880 { | |
110 | 881 int i; |
882 AVIContext *avi = s->priv_data; | |
883 | |
884 for(i=0;i<s->nb_streams;i++) { | |
885 AVStream *st = s->streams[i]; | |
311 | 886 AVIStream *ast = st->priv_data; |
700
a5e6e0e61e24
use libavformats index system instead of the half duplicated mess in avidec.c
michael
parents:
673
diff
changeset
|
887 av_free(ast); |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
888 av_free(st->codec->extradata); |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
889 av_free(st->codec->palctrl); |
110 | 890 } |
891 | |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
892 if (avi->dv_demux) |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
893 av_free(avi->dv_demux); |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
894 |
0 | 895 return 0; |
896 } | |
897 | |
898 static int avi_probe(AVProbeData *p) | |
899 { | |
900 /* check file header */ | |
901 if (p->buf_size <= 32) | |
902 return 0; | |
903 if (p->buf[0] == 'R' && p->buf[1] == 'I' && | |
904 p->buf[2] == 'F' && p->buf[3] == 'F' && | |
905 p->buf[8] == 'A' && p->buf[9] == 'V' && | |
906 p->buf[10] == 'I' && p->buf[11] == ' ') | |
907 return AVPROBE_SCORE_MAX; | |
908 else | |
909 return 0; | |
910 } | |
911 | |
912 static AVInputFormat avi_iformat = { | |
913 "avi", | |
914 "avi format", | |
915 sizeof(AVIContext), | |
916 avi_probe, | |
917 avi_read_header, | |
918 avi_read_packet, | |
919 avi_read_close, | |
311 | 920 avi_read_seek, |
0 | 921 }; |
922 | |
923 int avidec_init(void) | |
924 { | |
925 av_register_input_format(&avi_iformat); | |
926 return 0; | |
927 } |