You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

829 lines
26KB

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