Mercurial > libavformat.hg
annotate avidec.c @ 472:fd31daf8df85 libavformat
10l
author | michael |
---|---|
date | Sun, 30 May 2004 01:04:50 +0000 |
parents | 8b7a8dccde70 |
children | d33ce0cfc81c |
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 | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
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 |
23 //#define DEBUG | |
311 | 24 //#define DEBUG_SEEK |
0 | 25 |
311 | 26 typedef struct AVIIndexEntry { |
27 unsigned int flags; | |
28 unsigned int pos; | |
29 unsigned int cum_len; /* sum of all lengths before this packet */ | |
30 } AVIIndexEntry; | |
31 | |
32 typedef struct AVIStream { | |
33 AVIIndexEntry *index_entries; | |
34 int nb_index_entries; | |
35 int index_entries_allocated_size; | |
36 int frame_offset; /* current frame (video) or byte (audio) counter | |
37 (used to compute the pts) */ | |
38 int scale; | |
39 int rate; | |
40 int sample_size; /* audio only data */ | |
457 | 41 int start; |
311 | 42 |
43 int new_frame_offset; /* temporary storage (used during seek) */ | |
44 int cum_len; /* temporary storage (used during seek) */ | |
45 } AVIStream; | |
0 | 46 |
47 typedef struct { | |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
48 int64_t riff_end; |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
49 int64_t movi_end; |
0 | 50 offset_t movi_list; |
311 | 51 int index_loaded; |
296
252946de6d3f
* DV demuxer is now capable of decoding auxilary audio stream. So,
romansh
parents:
262
diff
changeset
|
52 DVDemuxContext* dv_demux; |
0 | 53 } AVIContext; |
54 | |
469
6a4cc19e8d9b
exporting keyframe flags, fixes keyframe stuff with streamcopy
michael
parents:
466
diff
changeset
|
55 static int avi_load_index(AVFormatContext *s); |
6a4cc19e8d9b
exporting keyframe flags, fixes keyframe stuff with streamcopy
michael
parents:
466
diff
changeset
|
56 |
0 | 57 #ifdef DEBUG |
58 static void print_tag(const char *str, unsigned int tag, int size) | |
59 { | |
60 printf("%s: tag=%c%c%c%c size=0x%x\n", | |
61 str, tag & 0xff, | |
62 (tag >> 8) & 0xff, | |
63 (tag >> 16) & 0xff, | |
64 (tag >> 24) & 0xff, | |
65 size); | |
66 } | |
67 #endif | |
68 | |
92
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
69 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
|
70 { |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
71 uint32_t tag; |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
72 /* check RIFF header */ |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
73 tag = get_le32(pb); |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
74 |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
75 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
|
76 return -1; |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
77 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
|
78 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
|
79 tag = get_le32(pb); |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
80 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
|
81 return -1; |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
82 |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
83 return 0; |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
84 } |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
85 |
0 | 86 static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap) |
87 { | |
88 AVIContext *avi = s->priv_data; | |
89 ByteIOContext *pb = &s->pb; | |
98
cae6ddfadf51
AVI type 1 support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
92
diff
changeset
|
90 uint32_t tag, tag1, handler; |
75
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
91 int codec_type, stream_index, frame_period, bit_rate, scale, rate; |
188
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
92 unsigned int size, nb_frames; |
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
93 int i, n; |
0 | 94 AVStream *st; |
311 | 95 AVIStream *ast; |
227 | 96 int xan_video = 0; /* hack to support Xan A/V */ |
0 | 97 |
92
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
98 if (get_riff(avi, pb) < 0) |
0 | 99 return -1; |
100 | |
101 /* first list tag */ | |
102 stream_index = -1; | |
103 codec_type = -1; | |
104 frame_period = 0; | |
105 for(;;) { | |
106 if (url_feof(pb)) | |
107 goto fail; | |
108 tag = get_le32(pb); | |
109 size = get_le32(pb); | |
110 #ifdef DEBUG | |
111 print_tag("tag", tag, size); | |
112 #endif | |
113 | |
114 switch(tag) { | |
115 case MKTAG('L', 'I', 'S', 'T'): | |
116 /* ignored, except when start of video packets */ | |
117 tag1 = get_le32(pb); | |
118 #ifdef DEBUG | |
119 print_tag("list", tag1, 0); | |
120 #endif | |
121 if (tag1 == MKTAG('m', 'o', 'v', 'i')) { | |
311 | 122 avi->movi_list = url_ftell(pb) - 4; |
123 avi->movi_end = avi->movi_list + size; | |
0 | 124 #ifdef DEBUG |
125 printf("movi end=%Lx\n", avi->movi_end); | |
126 #endif | |
127 goto end_of_header; | |
128 } | |
129 break; | |
130 case MKTAG('a', 'v', 'i', 'h'): | |
131 /* avi header */ | |
132 /* using frame_period is bad idea */ | |
133 frame_period = get_le32(pb); | |
134 bit_rate = get_le32(pb) * 8; | |
135 url_fskip(pb, 4 * 4); | |
188
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
136 n = get_le32(pb); |
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
137 for(i=0;i<n;i++) { |
311 | 138 AVIStream *ast; |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
139 st = av_new_stream(s, i); |
0 | 140 if (!st) |
141 goto fail; | |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
457
diff
changeset
|
142 |
311 | 143 ast = av_mallocz(sizeof(AVIStream)); |
144 if (!ast) | |
145 goto fail; | |
146 st->priv_data = ast; | |
0 | 147 } |
148 url_fskip(pb, size - 7 * 4); | |
149 break; | |
150 case MKTAG('s', 't', 'r', 'h'): | |
151 /* stream header */ | |
152 stream_index++; | |
153 tag1 = get_le32(pb); | |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
154 handler = get_le32(pb); /* codec tag */ |
471 | 155 #ifdef DEBUG |
156 print_tag("strh", tag1, -1); | |
157 #endif | |
0 | 158 switch(tag1) { |
98
cae6ddfadf51
AVI type 1 support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
92
diff
changeset
|
159 case MKTAG('i', 'a', 'v', 's'): |
cae6ddfadf51
AVI type 1 support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
92
diff
changeset
|
160 case MKTAG('i', 'v', 'a', 's'): |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
161 /* |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
162 * After some consideration -- I don't think we |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
163 * have to support anything but DV in a type1 AVIs. |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
164 */ |
98
cae6ddfadf51
AVI type 1 support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
92
diff
changeset
|
165 if (s->nb_streams != 1) |
cae6ddfadf51
AVI type 1 support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
92
diff
changeset
|
166 goto fail; |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
167 |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
168 if (handler != MKTAG('d', 'v', 's', 'd') && |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
169 handler != MKTAG('d', 'v', 'h', 'd') && |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
170 handler != MKTAG('d', 'v', 's', 'l')) |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
171 goto fail; |
296
252946de6d3f
* DV demuxer is now capable of decoding auxilary audio stream. So,
romansh
parents:
262
diff
changeset
|
172 |
252946de6d3f
* DV demuxer is now capable of decoding auxilary audio stream. So,
romansh
parents:
262
diff
changeset
|
173 av_freep(&s->streams[0]->codec.extradata); |
252946de6d3f
* DV demuxer is now capable of decoding auxilary audio stream. So,
romansh
parents:
262
diff
changeset
|
174 av_freep(&s->streams[0]); |
252946de6d3f
* DV demuxer is now capable of decoding auxilary audio stream. So,
romansh
parents:
262
diff
changeset
|
175 s->nb_streams = 0; |
252946de6d3f
* DV demuxer is now capable of decoding auxilary audio stream. So,
romansh
parents:
262
diff
changeset
|
176 avi->dv_demux = dv_init_demux(s); |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
177 if (!avi->dv_demux) |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
178 goto fail; |
296
252946de6d3f
* DV demuxer is now capable of decoding auxilary audio stream. So,
romansh
parents:
262
diff
changeset
|
179 stream_index = s->nb_streams - 1; |
252946de6d3f
* DV demuxer is now capable of decoding auxilary audio stream. So,
romansh
parents:
262
diff
changeset
|
180 url_fskip(pb, size - 8); |
252946de6d3f
* DV demuxer is now capable of decoding auxilary audio stream. So,
romansh
parents:
262
diff
changeset
|
181 break; |
98
cae6ddfadf51
AVI type 1 support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
92
diff
changeset
|
182 case MKTAG('v', 'i', 'd', 's'): |
0 | 183 codec_type = CODEC_TYPE_VIDEO; |
75
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
184 |
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
185 if (stream_index >= s->nb_streams) { |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
186 url_fskip(pb, size - 8); |
75
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
187 break; |
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
188 } |
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
189 |
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
190 st = s->streams[stream_index]; |
311 | 191 ast = st->priv_data; |
339 | 192 st->codec.stream_codec_tag= handler; |
311 | 193 |
0 | 194 get_le32(pb); /* flags */ |
195 get_le16(pb); /* priority */ | |
196 get_le16(pb); /* language */ | |
197 get_le32(pb); /* XXX: initial frame ? */ | |
98
cae6ddfadf51
AVI type 1 support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
92
diff
changeset
|
198 scale = get_le32(pb); /* scale */ |
cae6ddfadf51
AVI type 1 support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
92
diff
changeset
|
199 rate = get_le32(pb); /* rate */ |
75
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
200 |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
84
diff
changeset
|
201 if(scale && rate){ |
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
84
diff
changeset
|
202 }else if(frame_period){ |
311 | 203 rate = 1000000; |
204 scale = frame_period; | |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
84
diff
changeset
|
205 }else{ |
311 | 206 rate = 25; |
207 scale = 1; | |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
84
diff
changeset
|
208 } |
311 | 209 ast->rate = rate; |
210 ast->scale = scale; | |
463
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
211 av_set_pts_info(st, 64, scale, rate); |
311 | 212 st->codec.frame_rate = rate; |
213 st->codec.frame_rate_base = scale; | |
188
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
214 get_le32(pb); /* start */ |
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
215 nb_frames = get_le32(pb); |
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
216 st->start_time = 0; |
465 | 217 st->duration = av_rescale(nb_frames, |
218 st->codec.frame_rate_base * AV_TIME_BASE, | |
219 st->codec.frame_rate); | |
188
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
220 url_fskip(pb, size - 9 * 4); |
0 | 221 break; |
222 case MKTAG('a', 'u', 'd', 's'): | |
188
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
223 { |
311 | 224 unsigned int length; |
188
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
225 |
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
226 codec_type = CODEC_TYPE_AUDIO; |
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
227 |
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
228 if (stream_index >= s->nb_streams) { |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
229 url_fskip(pb, size - 8); |
188
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
230 break; |
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
231 } |
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
232 st = s->streams[stream_index]; |
311 | 233 ast = st->priv_data; |
234 | |
188
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
235 get_le32(pb); /* flags */ |
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
236 get_le16(pb); /* priority */ |
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
237 get_le16(pb); /* language */ |
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
238 get_le32(pb); /* initial frame */ |
311 | 239 ast->scale = get_le32(pb); /* scale */ |
240 ast->rate = get_le32(pb); | |
463
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
241 av_set_pts_info(st, 64, ast->scale, ast->rate); |
457 | 242 ast->start= get_le32(pb); /* start */ |
188
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
243 length = get_le32(pb); /* length, in samples or bytes */ |
311 | 244 get_le32(pb); /* buffer size */ |
245 get_le32(pb); /* quality */ | |
246 ast->sample_size = get_le32(pb); /* sample ssize */ | |
457 | 247 //av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d\n", ast->scale, ast->rate, ast->sample_size, ast->start); |
188
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
248 st->start_time = 0; |
465 | 249 if (ast->rate != 0) |
250 st->duration = (int64_t)length * AV_TIME_BASE / ast->rate; | |
311 | 251 url_fskip(pb, size - 12 * 4); |
188
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
149
diff
changeset
|
252 } |
73
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
253 break; |
471 | 254 case MKTAG('t', 'x', 't', 's'): |
255 //FIXME | |
256 codec_type = CODEC_TYPE_DATA; //CODEC_TYPE_SUB ? FIXME | |
257 url_fskip(pb, size - 8); | |
258 break; | |
73
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
259 default: |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
260 goto fail; |
0 | 261 } |
262 break; | |
263 case MKTAG('s', 't', 'r', 'f'): | |
264 /* stream header */ | |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
265 if (stream_index >= s->nb_streams || avi->dv_demux) { |
0 | 266 url_fskip(pb, size); |
267 } else { | |
268 st = s->streams[stream_index]; | |
269 switch(codec_type) { | |
270 case CODEC_TYPE_VIDEO: | |
271 get_le32(pb); /* size */ | |
272 st->codec.width = get_le32(pb); | |
273 st->codec.height = get_le32(pb); | |
274 get_le16(pb); /* panes */ | |
75
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
275 st->codec.bits_per_sample= get_le16(pb); /* depth */ |
0 | 276 tag1 = get_le32(pb); |
75
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
277 get_le32(pb); /* ImageSize */ |
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
278 get_le32(pb); /* XPelsPerMeter */ |
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
279 get_le32(pb); /* YPelsPerMeter */ |
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
280 get_le32(pb); /* ClrUsed */ |
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
281 get_le32(pb); /* ClrImportant */ |
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
282 |
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
283 st->codec.extradata_size= size - 10*4; |
110 | 284 st->codec.extradata= av_malloc(st->codec.extradata_size); |
75
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
285 get_buffer(pb, st->codec.extradata, st->codec.extradata_size); |
76 | 286 |
287 if(st->codec.extradata_size & 1) //FIXME check if the encoder really did this correctly | |
288 get_byte(pb); | |
75
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
289 |
297
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
290 /* Extract palette from extradata if bpp <= 8 */ |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
291 /* This code assumes that extradata contains only palette */ |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
292 /* This is true for all paletted codecs implemented in ffmpeg */ |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
293 if (st->codec.extradata_size && (st->codec.bits_per_sample <= 8)) { |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
294 st->codec.palctrl = av_mallocz(sizeof(AVPaletteControl)); |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
295 #ifdef WORDS_BIGENDIAN |
300
6ee1b02f9b2a
* fixes for broken builds on Solaris, OS2 and all bingendian
romansh
parents:
297
diff
changeset
|
296 for (i = 0; i < FFMIN(st->codec.extradata_size, AVPALETTE_SIZE)/4; i++) |
6ee1b02f9b2a
* fixes for broken builds on Solaris, OS2 and all bingendian
romansh
parents:
297
diff
changeset
|
297 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
|
298 #else |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
299 memcpy(st->codec.palctrl->palette, st->codec.extradata, |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
300 FFMIN(st->codec.extradata_size, AVPALETTE_SIZE)); |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
301 #endif |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
302 st->codec.palctrl->palette_changed = 1; |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
303 } |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
304 |
0 | 305 #ifdef DEBUG |
306 print_tag("video", tag1, 0); | |
307 #endif | |
308 st->codec.codec_type = CODEC_TYPE_VIDEO; | |
309 st->codec.codec_tag = tag1; | |
310 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1); | |
227 | 311 if (st->codec.codec_id == CODEC_ID_XAN_WC4) |
312 xan_video = 1; | |
75
78bec272ce3a
read BITMAPINFOHEADER extra stuff (huffyuv decoding fixed)
michaelni
parents:
73
diff
changeset
|
313 // url_fskip(pb, size - 5 * 4); |
0 | 314 break; |
73
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
315 case CODEC_TYPE_AUDIO: |
84
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
82
diff
changeset
|
316 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
|
317 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
|
318 url_fskip(pb, 1); |
227 | 319 /* special case time: To support Xan DPCM, hardcode |
320 * the format if Xxan is the video codec */ | |
311 | 321 st->need_parsing = 1; |
322 /* force parsing as several audio frames can be in | |
323 one packet */ | |
227 | 324 if (xan_video) |
325 st->codec.codec_id = CODEC_ID_XAN_DPCM; | |
0 | 326 break; |
327 default: | |
472 | 328 st->codec.codec_type = CODEC_TYPE_DATA; |
329 st->codec.codec_id= CODEC_ID_NONE; | |
330 st->codec.codec_tag= 0; | |
0 | 331 url_fskip(pb, size); |
332 break; | |
333 } | |
334 } | |
335 break; | |
336 default: | |
337 /* skip tag */ | |
338 size += (size & 1); | |
339 url_fskip(pb, size); | |
340 break; | |
341 } | |
342 } | |
343 end_of_header: | |
344 /* check stream number */ | |
345 if (stream_index != s->nb_streams - 1) { | |
346 fail: | |
347 for(i=0;i<s->nb_streams;i++) { | |
80 | 348 av_freep(&s->streams[i]->codec.extradata); |
0 | 349 av_freep(&s->streams[i]); |
350 } | |
351 return -1; | |
352 } | |
353 | |
469
6a4cc19e8d9b
exporting keyframe flags, fixes keyframe stuff with streamcopy
michael
parents:
466
diff
changeset
|
354 assert(!avi->index_loaded); |
6a4cc19e8d9b
exporting keyframe flags, fixes keyframe stuff with streamcopy
michael
parents:
466
diff
changeset
|
355 avi_load_index(s); |
6a4cc19e8d9b
exporting keyframe flags, fixes keyframe stuff with streamcopy
michael
parents:
466
diff
changeset
|
356 avi->index_loaded = 1; |
6a4cc19e8d9b
exporting keyframe flags, fixes keyframe stuff with streamcopy
michael
parents:
466
diff
changeset
|
357 |
0 | 358 return 0; |
359 } | |
360 | |
361 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt) | |
362 { | |
363 AVIContext *avi = s->priv_data; | |
364 ByteIOContext *pb = &s->pb; | |
82 | 365 int n, d[8], size, i; |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
366 void* dstr; |
98
cae6ddfadf51
AVI type 1 support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
92
diff
changeset
|
367 |
82 | 368 memset(d, -1, sizeof(int)*8); |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
369 |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
370 if (avi->dv_demux) { |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
371 size = dv_get_packet(avi->dv_demux, pkt); |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
372 if (size >= 0) |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
373 return size; |
149 | 374 } |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
375 |
92
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
376 for(i=url_ftell(pb); !url_feof(pb); i++) { |
82 | 377 int j; |
378 | |
92
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
379 if (i >= avi->movi_end) { /* Let's see if it's an OpenDML AVI */ |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
380 uint32_t tag, size, tag2; |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
381 url_fskip(pb, avi->riff_end - url_ftell(pb)); |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
382 if (get_riff(avi, pb) < 0) |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
383 return -1; |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
384 |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
385 tag = get_le32(pb); |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
386 size = get_le32(pb); |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
387 tag2 = get_le32(pb); |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
388 if (tag == MKTAG('L','I','S','T') && tag2 == MKTAG('m','o','v','i')) |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
389 avi->movi_end = url_ftell(pb) + size - 4; |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
390 else |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
391 return -1; |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
392 } |
5a4b5f03d13e
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
91
diff
changeset
|
393 |
82 | 394 for(j=0; j<7; j++) |
395 d[j]= d[j+1]; | |
396 d[7]= get_byte(pb); | |
397 | |
398 size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24); | |
399 | |
400 //parse ix## | |
401 n= (d[2] - '0') * 10 + (d[3] - '0'); | |
402 if( d[2] >= '0' && d[2] <= '9' | |
403 && d[3] >= '0' && d[3] <= '9' | |
404 && d[0] == 'i' && d[1] == 'x' | |
405 && n < s->nb_streams | |
406 && i + size <= avi->movi_end){ | |
407 | |
408 url_fskip(pb, size); | |
409 } | |
410 | |
411 //parse ##dc/##wb | |
412 n= (d[0] - '0') * 10 + (d[1] - '0'); | |
413 if( d[0] >= '0' && d[0] <= '9' | |
414 && d[1] >= '0' && d[1] <= '9' | |
98
cae6ddfadf51
AVI type 1 support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
92
diff
changeset
|
415 && ((d[2] == 'd' && d[3] == 'c') || |
cae6ddfadf51
AVI type 1 support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
92
diff
changeset
|
416 (d[2] == 'w' && d[3] == 'b') || |
cae6ddfadf51
AVI type 1 support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
92
diff
changeset
|
417 (d[2] == 'd' && d[3] == 'b') || |
cae6ddfadf51
AVI type 1 support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
92
diff
changeset
|
418 (d[2] == '_' && d[3] == '_')) |
82 | 419 && n < s->nb_streams |
98
cae6ddfadf51
AVI type 1 support patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
92
diff
changeset
|
420 && i + size <= avi->movi_end) { |
82 | 421 |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
422 av_new_packet(pkt, size); |
149 | 423 get_buffer(pb, pkt->data, size); |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
424 if (size & 1) { |
149 | 425 get_byte(pb); |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
426 size++; |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
427 } |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
428 |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
429 if (avi->dv_demux) { |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
430 dstr = pkt->destruct; |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
431 size = dv_produce_packet(avi->dv_demux, pkt, |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
432 pkt->data, pkt->size); |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
433 pkt->destruct = dstr; |
311 | 434 pkt->flags |= PKT_FLAG_KEY; |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
435 } else { |
311 | 436 AVStream *st; |
437 AVIStream *ast; | |
438 st = s->streams[n]; | |
439 ast = st->priv_data; | |
471 | 440 |
311 | 441 /* XXX: how to handle B frames in avi ? */ |
469
6a4cc19e8d9b
exporting keyframe flags, fixes keyframe stuff with streamcopy
michael
parents:
466
diff
changeset
|
442 pkt->dts = ast->frame_offset; |
6a4cc19e8d9b
exporting keyframe flags, fixes keyframe stuff with streamcopy
michael
parents:
466
diff
changeset
|
443 // pkt->dts += ast->start; |
457 | 444 if(ast->sample_size) |
469
6a4cc19e8d9b
exporting keyframe flags, fixes keyframe stuff with streamcopy
michael
parents:
466
diff
changeset
|
445 pkt->dts /= ast->sample_size; |
471 | 446 //av_log(NULL, AV_LOG_DEBUG, "dts:%Ld offset:%d %d/%d %d st:%d size:%d\n", pkt->dts, ast->frame_offset, ast->scale, ast->rate, AV_TIME_BASE, n, size); |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
447 pkt->stream_index = n; |
311 | 448 /* FIXME: We really should read index for that */ |
449 if (st->codec.codec_type == CODEC_TYPE_VIDEO) { | |
450 if (ast->frame_offset < ast->nb_index_entries) { | |
451 if (ast->index_entries[ast->frame_offset].flags & AVIIF_INDEX) | |
452 pkt->flags |= PKT_FLAG_KEY; | |
453 } else { | |
454 /* if no index, better to say that all frames | |
455 are key frames */ | |
456 pkt->flags |= PKT_FLAG_KEY; | |
457 } | |
458 } else { | |
459 pkt->flags |= PKT_FLAG_KEY; | |
460 } | |
457 | 461 if(ast->sample_size) |
462 ast->frame_offset += pkt->size; | |
463 else | |
464 ast->frame_offset++; | |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
465 } |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
466 return size; |
82 | 467 } |
468 } | |
469 return -1; | |
0 | 470 } |
471 | |
311 | 472 /* XXX: we make the implicit supposition that the position are sorted |
473 for each stream */ | |
474 static int avi_read_idx1(AVFormatContext *s, int size) | |
475 { | |
476 ByteIOContext *pb = &s->pb; | |
477 int nb_index_entries, i; | |
478 AVStream *st; | |
479 AVIStream *ast; | |
480 AVIIndexEntry *ie, *entries; | |
481 unsigned int index, tag, flags, pos, len; | |
482 | |
483 nb_index_entries = size / 16; | |
484 if (nb_index_entries <= 0) | |
485 return -1; | |
486 | |
487 /* read the entries and sort them in each stream component */ | |
488 for(i = 0; i < nb_index_entries; i++) { | |
489 tag = get_le32(pb); | |
490 flags = get_le32(pb); | |
491 pos = get_le32(pb); | |
492 len = get_le32(pb); | |
493 #if defined(DEBUG_SEEK) && 0 | |
494 printf("%d: tag=0x%x flags=0x%x pos=0x%x len=%d\n", | |
495 i, tag, flags, pos, len); | |
496 #endif | |
497 index = ((tag & 0xff) - '0') * 10; | |
498 index += ((tag >> 8) & 0xff) - '0'; | |
499 if (index >= s->nb_streams) | |
500 continue; | |
501 st = s->streams[index]; | |
502 ast = st->priv_data; | |
503 | |
504 entries = av_fast_realloc(ast->index_entries, | |
505 &ast->index_entries_allocated_size, | |
506 (ast->nb_index_entries + 1) * | |
507 sizeof(AVIIndexEntry)); | |
508 if (entries) { | |
509 ast->index_entries = entries; | |
510 ie = &entries[ast->nb_index_entries++]; | |
511 ie->flags = flags; | |
512 ie->pos = pos; | |
513 ie->cum_len = ast->cum_len; | |
514 ast->cum_len += len; | |
515 } | |
516 } | |
517 return 0; | |
518 } | |
519 | |
520 static int avi_load_index(AVFormatContext *s) | |
521 { | |
522 AVIContext *avi = s->priv_data; | |
523 ByteIOContext *pb = &s->pb; | |
524 uint32_t tag, size; | |
343 | 525 offset_t pos= url_ftell(pb); |
526 | |
311 | 527 url_fseek(pb, avi->movi_end, SEEK_SET); |
528 #ifdef DEBUG_SEEK | |
529 printf("movi_end=0x%llx\n", avi->movi_end); | |
530 #endif | |
531 for(;;) { | |
532 if (url_feof(pb)) | |
533 break; | |
534 tag = get_le32(pb); | |
535 size = get_le32(pb); | |
536 #ifdef DEBUG_SEEK | |
537 printf("tag=%c%c%c%c size=0x%x\n", | |
538 tag & 0xff, | |
539 (tag >> 8) & 0xff, | |
540 (tag >> 16) & 0xff, | |
541 (tag >> 24) & 0xff, | |
542 size); | |
543 #endif | |
544 switch(tag) { | |
545 case MKTAG('i', 'd', 'x', '1'): | |
546 if (avi_read_idx1(s, size) < 0) | |
547 goto skip; | |
548 else | |
549 goto the_end; | |
550 break; | |
551 default: | |
552 skip: | |
553 size += (size & 1); | |
554 url_fskip(pb, size); | |
555 break; | |
556 } | |
557 } | |
558 the_end: | |
343 | 559 url_fseek(pb, pos, SEEK_SET); |
311 | 560 return 0; |
561 } | |
562 | |
563 /* return the index entry whose position is immediately >= 'wanted_pos' */ | |
564 static int locate_frame_in_index(AVIIndexEntry *entries, | |
565 int nb_entries, int wanted_pos) | |
566 { | |
567 int a, b, m, pos; | |
568 | |
569 a = 0; | |
570 b = nb_entries - 1; | |
571 while (a <= b) { | |
572 m = (a + b) >> 1; | |
573 pos = entries[m].pos; | |
574 if (pos == wanted_pos) | |
575 goto found; | |
576 else if (pos > wanted_pos) { | |
577 b = m - 1; | |
578 } else { | |
579 a = m + 1; | |
580 } | |
581 } | |
582 m = a; | |
583 if (m > 0) | |
584 m--; | |
585 found: | |
586 return m; | |
587 } | |
588 | |
589 static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp) | |
590 { | |
591 AVIContext *avi = s->priv_data; | |
592 AVStream *st; | |
593 AVIStream *ast; | |
594 int frame_number, i; | |
595 int64_t pos; | |
596 | |
597 if (!avi->index_loaded) { | |
598 /* we only load the index on demand */ | |
599 avi_load_index(s); | |
600 avi->index_loaded = 1; | |
601 } | |
602 if (stream_index < 0) { | |
603 for(i = 0; i < s->nb_streams; i++) { | |
604 st = s->streams[i]; | |
605 if (st->codec.codec_type == CODEC_TYPE_VIDEO) | |
606 goto found; | |
607 } | |
608 return -1; | |
609 found: | |
610 stream_index = i; | |
611 } | |
612 | |
613 st = s->streams[stream_index]; | |
614 if (st->codec.codec_type != CODEC_TYPE_VIDEO) | |
615 return -1; | |
616 ast = st->priv_data; | |
617 /* compute the frame number */ | |
466 | 618 frame_number = timestamp; |
311 | 619 #ifdef DEBUG_SEEK |
620 printf("timestamp=%0.3f nb_indexes=%d frame_number=%d\n", | |
621 (double)timestamp / AV_TIME_BASE, | |
622 ast->nb_index_entries, frame_number); | |
623 #endif | |
624 /* find a closest key frame before */ | |
625 if (frame_number >= ast->nb_index_entries) | |
626 return -1; | |
627 while (frame_number >= 0 && | |
628 !(ast->index_entries[frame_number].flags & AVIIF_INDEX)) | |
629 frame_number--; | |
630 if (frame_number < 0) | |
631 return -1; | |
632 ast->new_frame_offset = frame_number; | |
633 | |
634 /* find the position */ | |
635 pos = ast->index_entries[frame_number].pos; | |
636 | |
637 #ifdef DEBUG_SEEK | |
638 printf("key_frame_number=%d pos=0x%llx\n", | |
639 frame_number, pos); | |
640 #endif | |
641 | |
642 /* update the frame counters for all the other stream by looking | |
643 at the positions just after the one found */ | |
644 for(i = 0; i < s->nb_streams; i++) { | |
645 int j; | |
646 if (i != stream_index) { | |
647 st = s->streams[i]; | |
648 ast = st->priv_data; | |
649 if (ast->nb_index_entries <= 0) | |
650 return -1; | |
651 j = locate_frame_in_index(ast->index_entries, | |
652 ast->nb_index_entries, | |
653 pos); | |
654 /* get next frame */ | |
655 if ((j + 1) < ast->nb_index_entries) | |
656 j++; | |
657 /* extract the current frame number */ | |
457 | 658 if (ast->sample_size==0) |
311 | 659 ast->new_frame_offset = j; |
660 else | |
661 ast->new_frame_offset = ast->index_entries[j].cum_len; | |
662 } | |
663 } | |
664 | |
665 /* everything is OK now. We can update the frame offsets */ | |
666 for(i = 0; i < s->nb_streams; i++) { | |
667 st = s->streams[i]; | |
668 ast = st->priv_data; | |
669 ast->frame_offset = ast->new_frame_offset; | |
670 #ifdef DEBUG_SEEK | |
671 printf("%d: frame_offset=%d\n", i, | |
672 ast->frame_offset); | |
673 #endif | |
674 } | |
675 /* do the seek */ | |
676 pos += avi->movi_list; | |
677 url_fseek(&s->pb, pos, SEEK_SET); | |
678 return 0; | |
679 } | |
680 | |
0 | 681 static int avi_read_close(AVFormatContext *s) |
682 { | |
110 | 683 int i; |
684 AVIContext *avi = s->priv_data; | |
685 | |
686 for(i=0;i<s->nb_streams;i++) { | |
687 AVStream *st = s->streams[i]; | |
311 | 688 AVIStream *ast = st->priv_data; |
340 | 689 if(ast){ |
690 av_free(ast->index_entries); | |
691 av_free(ast); | |
692 } | |
110 | 693 av_free(st->codec.extradata); |
297
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
296
diff
changeset
|
694 av_free(st->codec.palctrl); |
110 | 695 } |
696 | |
262
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
697 if (avi->dv_demux) |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
698 av_free(avi->dv_demux); |
f174d9c00bce
* DV handling was streamlined for both muxing/demuxing and
romansh
parents:
227
diff
changeset
|
699 |
0 | 700 return 0; |
701 } | |
702 | |
703 static int avi_probe(AVProbeData *p) | |
704 { | |
705 /* check file header */ | |
706 if (p->buf_size <= 32) | |
707 return 0; | |
708 if (p->buf[0] == 'R' && p->buf[1] == 'I' && | |
709 p->buf[2] == 'F' && p->buf[3] == 'F' && | |
710 p->buf[8] == 'A' && p->buf[9] == 'V' && | |
711 p->buf[10] == 'I' && p->buf[11] == ' ') | |
712 return AVPROBE_SCORE_MAX; | |
713 else | |
714 return 0; | |
715 } | |
716 | |
717 static AVInputFormat avi_iformat = { | |
718 "avi", | |
719 "avi format", | |
720 sizeof(AVIContext), | |
721 avi_probe, | |
722 avi_read_header, | |
723 avi_read_packet, | |
724 avi_read_close, | |
311 | 725 avi_read_seek, |
0 | 726 }; |
727 | |
728 int avidec_init(void) | |
729 { | |
730 av_register_input_format(&avi_iformat); | |
731 return 0; | |
732 } |