annotate src/ffmpeg/libavformat/avidec.c @ 818:07107d476f32 trunk

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