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