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