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.

830 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_new_packet(pkt, size);
  384. get_buffer(pb, pkt->data, size);
  385. if (avi->dv_demux) {
  386. dstr = pkt->destruct;
  387. size = dv_produce_packet(avi->dv_demux, pkt,
  388. pkt->data, pkt->size);
  389. pkt->destruct = dstr;
  390. pkt->flags |= PKT_FLAG_KEY;
  391. } else {
  392. /* XXX: how to handle B frames in avi ? */
  393. pkt->dts = ast->frame_offset;
  394. // pkt->dts += ast->start;
  395. if(ast->sample_size)
  396. pkt->dts /= ast->sample_size;
  397. //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);
  398. pkt->stream_index = avi->stream_index;
  399. if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
  400. if(st->index_entries){
  401. AVIndexEntry *e;
  402. int index;
  403. index= av_index_search_timestamp(st, pkt->dts, 0);
  404. e= &st->index_entries[index];
  405. if(index >= 0 && e->timestamp == ast->frame_offset){
  406. if (e->flags & AVINDEX_KEYFRAME)
  407. pkt->flags |= PKT_FLAG_KEY;
  408. }
  409. } else {
  410. /* if no index, better to say that all frames
  411. are key frames */
  412. pkt->flags |= PKT_FLAG_KEY;
  413. }
  414. } else {
  415. pkt->flags |= PKT_FLAG_KEY;
  416. }
  417. if(ast->sample_size)
  418. ast->frame_offset += pkt->size;
  419. else
  420. ast->frame_offset++;
  421. }
  422. ast->remaining -= size;
  423. if(!ast->remaining){
  424. avi->stream_index= -1;
  425. ast->packet_size= 0;
  426. if (size & 1) {
  427. get_byte(pb);
  428. size++;
  429. }
  430. }
  431. return size;
  432. }
  433. memset(d, -1, sizeof(int)*8);
  434. for(i=sync=url_ftell(pb); !url_feof(pb); i++) {
  435. int j;
  436. if (i >= avi->movi_end) {
  437. if (avi->is_odml) {
  438. url_fskip(pb, avi->riff_end - i);
  439. avi->riff_end = avi->movi_end = url_fsize(pb);
  440. } else
  441. break;
  442. }
  443. for(j=0; j<7; j++)
  444. d[j]= d[j+1];
  445. d[7]= get_byte(pb);
  446. size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
  447. if( d[2] >= '0' && d[2] <= '9'
  448. && d[3] >= '0' && d[3] <= '9'){
  449. n= (d[2] - '0') * 10 + (d[3] - '0');
  450. }else{
  451. n= 100; //invalid stream id
  452. }
  453. //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);
  454. if(i + size > avi->movi_end || d[0]<0)
  455. continue;
  456. //parse ix##
  457. if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
  458. //parse JUNK
  459. ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')){
  460. url_fskip(pb, size);
  461. //av_log(NULL, AV_LOG_DEBUG, "SKIP\n");
  462. goto resync;
  463. }
  464. if( d[0] >= '0' && d[0] <= '9'
  465. && d[1] >= '0' && d[1] <= '9'){
  466. n= (d[0] - '0') * 10 + (d[1] - '0');
  467. }else{
  468. n= 100; //invalid stream id
  469. }
  470. //parse ##dc/##wb
  471. if(n < s->nb_streams){
  472. AVStream *st;
  473. AVIStream *ast;
  474. st = s->streams[n];
  475. ast = st->priv_data;
  476. if( (st->discard >= AVDISCARD_DEFAULT && size==0)
  477. /*|| (st->discard >= AVDISCARD_NONKEY && !(pkt->flags & PKT_FLAG_KEY))*/ //FIXME needs a little reordering
  478. || st->discard >= AVDISCARD_ALL){
  479. if(ast->sample_size) ast->frame_offset += pkt->size;
  480. else ast->frame_offset++;
  481. url_fskip(pb, size);
  482. goto resync;
  483. }
  484. if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
  485. d[2]*256+d[3] == ast->prefix /*||
  486. (d[2] == 'd' && d[3] == 'c') ||
  487. (d[2] == 'w' && d[3] == 'b')*/) {
  488. //av_log(NULL, AV_LOG_DEBUG, "OK\n");
  489. if(d[2]*256+d[3] == ast->prefix)
  490. ast->prefix_count++;
  491. else{
  492. ast->prefix= d[2]*256+d[3];
  493. ast->prefix_count= 0;
  494. }
  495. avi->stream_index= n;
  496. ast->packet_size= size + 8;
  497. ast->remaining= size;
  498. goto resync;
  499. }
  500. }
  501. /* palette changed chunk */
  502. if ( d[0] >= '0' && d[0] <= '9'
  503. && d[1] >= '0' && d[1] <= '9'
  504. && ((d[2] == 'p' && d[3] == 'c'))
  505. && n < s->nb_streams && i + size <= avi->movi_end) {
  506. AVStream *st;
  507. int first, clr, flags, k, p;
  508. st = s->streams[n];
  509. first = get_byte(pb);
  510. clr = get_byte(pb);
  511. if(!clr) /* all 256 colors used */
  512. clr = 256;
  513. flags = get_le16(pb);
  514. p = 4;
  515. for (k = first; k < clr + first; k++) {
  516. int r, g, b;
  517. r = get_byte(pb);
  518. g = get_byte(pb);
  519. b = get_byte(pb);
  520. get_byte(pb);
  521. st->codec.palctrl->palette[k] = b + (g << 8) + (r << 16);
  522. }
  523. st->codec.palctrl->palette_changed = 1;
  524. goto resync;
  525. }
  526. }
  527. return -1;
  528. }
  529. /* XXX: we make the implicit supposition that the position are sorted
  530. for each stream */
  531. static int avi_read_idx1(AVFormatContext *s, int size)
  532. {
  533. AVIContext *avi = s->priv_data;
  534. ByteIOContext *pb = &s->pb;
  535. int nb_index_entries, i;
  536. AVStream *st;
  537. AVIStream *ast;
  538. unsigned int index, tag, flags, pos, len;
  539. unsigned last_pos= -1;
  540. nb_index_entries = size / 16;
  541. if (nb_index_entries <= 0)
  542. return -1;
  543. /* read the entries and sort them in each stream component */
  544. for(i = 0; i < nb_index_entries; i++) {
  545. tag = get_le32(pb);
  546. flags = get_le32(pb);
  547. pos = get_le32(pb);
  548. len = get_le32(pb);
  549. #if defined(DEBUG_SEEK)
  550. av_log(NULL, AV_LOG_DEBUG, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
  551. i, tag, flags, pos, len);
  552. #endif
  553. if(i==0 && pos > avi->movi_list)
  554. avi->movi_list= 0; //FIXME better check
  555. index = ((tag & 0xff) - '0') * 10;
  556. index += ((tag >> 8) & 0xff) - '0';
  557. if (index >= s->nb_streams)
  558. continue;
  559. st = s->streams[index];
  560. ast = st->priv_data;
  561. #if defined(DEBUG_SEEK)
  562. av_log(NULL, AV_LOG_DEBUG, "%d cum_len=%d\n", len, ast->cum_len);
  563. #endif
  564. if(last_pos == pos)
  565. avi->non_interleaved= 1;
  566. else
  567. av_add_index_entry(st, pos, ast->cum_len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
  568. if(ast->sample_size)
  569. ast->cum_len += len / ast->sample_size;
  570. else
  571. ast->cum_len ++;
  572. last_pos= pos;
  573. }
  574. return 0;
  575. }
  576. static int guess_ni_flag(AVFormatContext *s){
  577. AVIContext *avi = s->priv_data;
  578. int i;
  579. int64_t last_start=0;
  580. int64_t first_end= INT64_MAX;
  581. for(i=0; i<s->nb_streams; i++){
  582. AVStream *st = s->streams[i];
  583. AVIStream *ast = st->priv_data;
  584. int n= st->nb_index_entries;
  585. if(n <= 0)
  586. continue;
  587. if(st->index_entries[0].pos > last_start)
  588. last_start= st->index_entries[0].pos;
  589. if(st->index_entries[n-1].pos < first_end)
  590. first_end= st->index_entries[n-1].pos;
  591. }
  592. return last_start > first_end;
  593. }
  594. static int avi_load_index(AVFormatContext *s)
  595. {
  596. AVIContext *avi = s->priv_data;
  597. ByteIOContext *pb = &s->pb;
  598. uint32_t tag, size;
  599. offset_t pos= url_ftell(pb);
  600. url_fseek(pb, avi->movi_end, SEEK_SET);
  601. #ifdef DEBUG_SEEK
  602. printf("movi_end=0x%llx\n", avi->movi_end);
  603. #endif
  604. for(;;) {
  605. if (url_feof(pb))
  606. break;
  607. tag = get_le32(pb);
  608. size = get_le32(pb);
  609. #ifdef DEBUG_SEEK
  610. printf("tag=%c%c%c%c size=0x%x\n",
  611. tag & 0xff,
  612. (tag >> 8) & 0xff,
  613. (tag >> 16) & 0xff,
  614. (tag >> 24) & 0xff,
  615. size);
  616. #endif
  617. switch(tag) {
  618. case MKTAG('i', 'd', 'x', '1'):
  619. if (avi_read_idx1(s, size) < 0)
  620. goto skip;
  621. else
  622. goto the_end;
  623. break;
  624. default:
  625. skip:
  626. size += (size & 1);
  627. url_fskip(pb, size);
  628. break;
  629. }
  630. }
  631. the_end:
  632. avi->non_interleaved |= guess_ni_flag(s);
  633. url_fseek(pb, pos, SEEK_SET);
  634. return 0;
  635. }
  636. static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  637. {
  638. AVIContext *avi = s->priv_data;
  639. AVStream *st;
  640. int i, index;
  641. int64_t pos;
  642. if (!avi->index_loaded) {
  643. /* we only load the index on demand */
  644. avi_load_index(s);
  645. avi->index_loaded = 1;
  646. }
  647. assert(stream_index>= 0);
  648. st = s->streams[stream_index];
  649. index= av_index_search_timestamp(st, timestamp, flags);
  650. if(index<0)
  651. return -1;
  652. /* find the position */
  653. pos = st->index_entries[index].pos;
  654. timestamp = st->index_entries[index].timestamp;
  655. // av_log(NULL, AV_LOG_DEBUG, "XX %Ld %d %Ld\n", timestamp, index, st->index_entries[index].timestamp);
  656. for(i = 0; i < s->nb_streams; i++) {
  657. AVStream *st2 = s->streams[i];
  658. AVIStream *ast2 = st2->priv_data;
  659. ast2->packet_size=
  660. ast2->remaining= 0;
  661. if (st2->nb_index_entries <= 0)
  662. continue;
  663. // assert(st2->codec.block_align);
  664. assert(st2->time_base.den == ast2->rate);
  665. assert(st2->time_base.num == ast2->scale);
  666. index = av_index_search_timestamp(
  667. st2,
  668. av_rescale(timestamp, st2->time_base.den*(int64_t)st->time_base.num, st->time_base.den * (int64_t)st2->time_base.num),
  669. flags | AVSEEK_FLAG_BACKWARD);
  670. if(index<0)
  671. index=0;
  672. if(!avi->non_interleaved){
  673. while(index>0 && st2->index_entries[index].pos > pos)
  674. index--;
  675. while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos)
  676. index++;
  677. }
  678. // av_log(NULL, AV_LOG_DEBUG, "%Ld %d %Ld\n", timestamp, index, st2->index_entries[index].timestamp);
  679. /* extract the current frame number */
  680. ast2->frame_offset = st2->index_entries[index].timestamp;
  681. if(ast2->sample_size)
  682. ast2->frame_offset *=ast2->sample_size;
  683. }
  684. if (avi->dv_demux)
  685. dv_flush_audio_packets(avi->dv_demux);
  686. /* do the seek */
  687. pos += avi->movi_list;
  688. url_fseek(&s->pb, pos, SEEK_SET);
  689. avi->stream_index= -1;
  690. return 0;
  691. }
  692. static int avi_read_close(AVFormatContext *s)
  693. {
  694. int i;
  695. AVIContext *avi = s->priv_data;
  696. for(i=0;i<s->nb_streams;i++) {
  697. AVStream *st = s->streams[i];
  698. AVIStream *ast = st->priv_data;
  699. av_free(ast);
  700. av_free(st->codec.extradata);
  701. av_free(st->codec.palctrl);
  702. }
  703. if (avi->dv_demux)
  704. av_free(avi->dv_demux);
  705. return 0;
  706. }
  707. static int avi_probe(AVProbeData *p)
  708. {
  709. /* check file header */
  710. if (p->buf_size <= 32)
  711. return 0;
  712. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  713. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  714. p->buf[8] == 'A' && p->buf[9] == 'V' &&
  715. p->buf[10] == 'I' && p->buf[11] == ' ')
  716. return AVPROBE_SCORE_MAX;
  717. else
  718. return 0;
  719. }
  720. static AVInputFormat avi_iformat = {
  721. "avi",
  722. "avi format",
  723. sizeof(AVIContext),
  724. avi_probe,
  725. avi_read_header,
  726. avi_read_packet,
  727. avi_read_close,
  728. avi_read_seek,
  729. };
  730. int avidec_init(void)
  731. {
  732. av_register_input_format(&avi_iformat);
  733. return 0;
  734. }