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.

832 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_filesize(url_fileno(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 = av_rescale(nb_frames, ast->scale*(int64_t)AV_TIME_BASE, ast->rate);
  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. st->codec.frame_rate = ast->rate;
  210. st->codec.frame_rate_base = ast->scale;
  211. break;
  212. case MKTAG('a', 'u', 'd', 's'):
  213. codec_type = CODEC_TYPE_AUDIO;
  214. break;
  215. case MKTAG('t', 'x', 't', 's'):
  216. //FIXME
  217. codec_type = CODEC_TYPE_DATA; //CODEC_TYPE_SUB ? FIXME
  218. break;
  219. case MKTAG('p', 'a', 'd', 's'):
  220. codec_type = CODEC_TYPE_UNKNOWN;
  221. stream_index--;
  222. break;
  223. default:
  224. av_log(s, AV_LOG_ERROR, "unknown stream type %X\n", tag1);
  225. goto fail;
  226. }
  227. url_fskip(pb, size - 12 * 4);
  228. break;
  229. case MKTAG('s', 't', 'r', 'f'):
  230. /* stream header */
  231. if (stream_index >= s->nb_streams || avi->dv_demux) {
  232. url_fskip(pb, size);
  233. } else {
  234. st = s->streams[stream_index];
  235. switch(codec_type) {
  236. case CODEC_TYPE_VIDEO:
  237. get_le32(pb); /* size */
  238. st->codec.width = get_le32(pb);
  239. st->codec.height = get_le32(pb);
  240. get_le16(pb); /* panes */
  241. st->codec.bits_per_sample= get_le16(pb); /* depth */
  242. tag1 = get_le32(pb);
  243. get_le32(pb); /* ImageSize */
  244. get_le32(pb); /* XPelsPerMeter */
  245. get_le32(pb); /* YPelsPerMeter */
  246. get_le32(pb); /* ClrUsed */
  247. get_le32(pb); /* ClrImportant */
  248. if(size > 10*4 && size<(1<<30)){
  249. st->codec.extradata_size= size - 10*4;
  250. st->codec.extradata= av_malloc(st->codec.extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  251. get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
  252. }
  253. if(st->codec.extradata_size & 1) //FIXME check if the encoder really did this correctly
  254. get_byte(pb);
  255. /* Extract palette from extradata if bpp <= 8 */
  256. /* This code assumes that extradata contains only palette */
  257. /* This is true for all paletted codecs implemented in ffmpeg */
  258. if (st->codec.extradata_size && (st->codec.bits_per_sample <= 8)) {
  259. st->codec.palctrl = av_mallocz(sizeof(AVPaletteControl));
  260. #ifdef WORDS_BIGENDIAN
  261. for (i = 0; i < FFMIN(st->codec.extradata_size, AVPALETTE_SIZE)/4; i++)
  262. st->codec.palctrl->palette[i] = bswap_32(((uint32_t*)st->codec.extradata)[i]);
  263. #else
  264. memcpy(st->codec.palctrl->palette, st->codec.extradata,
  265. FFMIN(st->codec.extradata_size, AVPALETTE_SIZE));
  266. #endif
  267. st->codec.palctrl->palette_changed = 1;
  268. }
  269. #ifdef DEBUG
  270. print_tag("video", tag1, 0);
  271. #endif
  272. st->codec.codec_type = CODEC_TYPE_VIDEO;
  273. st->codec.codec_tag = tag1;
  274. st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
  275. if (st->codec.codec_id == CODEC_ID_XAN_WC4)
  276. xan_video = 1;
  277. // url_fskip(pb, size - 5 * 4);
  278. break;
  279. case CODEC_TYPE_AUDIO:
  280. get_wav_header(pb, &st->codec, size);
  281. if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
  282. url_fskip(pb, 1);
  283. /* special case time: To support Xan DPCM, hardcode
  284. * the format if Xxan is the video codec */
  285. st->need_parsing = 1;
  286. /* force parsing as several audio frames can be in
  287. one packet */
  288. if (xan_video)
  289. st->codec.codec_id = CODEC_ID_XAN_DPCM;
  290. break;
  291. default:
  292. st->codec.codec_type = CODEC_TYPE_DATA;
  293. st->codec.codec_id= CODEC_ID_NONE;
  294. st->codec.codec_tag= 0;
  295. url_fskip(pb, size);
  296. break;
  297. }
  298. }
  299. break;
  300. default:
  301. /* skip tag */
  302. size += (size & 1);
  303. url_fskip(pb, size);
  304. break;
  305. }
  306. }
  307. end_of_header:
  308. /* check stream number */
  309. if (stream_index != s->nb_streams - 1) {
  310. fail:
  311. for(i=0;i<s->nb_streams;i++) {
  312. av_freep(&s->streams[i]->codec.extradata);
  313. av_freep(&s->streams[i]);
  314. }
  315. return -1;
  316. }
  317. assert(!avi->index_loaded);
  318. avi_load_index(s);
  319. avi->index_loaded = 1;
  320. return 0;
  321. }
  322. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  323. {
  324. AVIContext *avi = s->priv_data;
  325. ByteIOContext *pb = &s->pb;
  326. int n, d[8], size;
  327. offset_t i, sync;
  328. void* dstr;
  329. if (avi->dv_demux) {
  330. size = dv_get_packet(avi->dv_demux, pkt);
  331. if (size >= 0)
  332. return size;
  333. }
  334. if(avi->non_interleaved){
  335. int best_stream_index;
  336. AVStream *best_st= NULL;
  337. AVIStream *best_ast;
  338. int64_t best_ts= INT64_MAX;
  339. int i;
  340. for(i=0; i<s->nb_streams; i++){
  341. AVStream *st = s->streams[i];
  342. AVIStream *ast = st->priv_data;
  343. int64_t ts= ast->frame_offset;
  344. if(ast->sample_size)
  345. ts /= ast->sample_size;
  346. ts= av_rescale(ts, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den);
  347. // av_log(NULL, AV_LOG_DEBUG, "%Ld %d/%d %Ld\n", ts, st->time_base.num, st->time_base.den, ast->frame_offset);
  348. if(ts < best_ts){
  349. best_ts= ts;
  350. best_st= st;
  351. best_stream_index= i;
  352. }
  353. }
  354. best_ast = best_st->priv_data;
  355. 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
  356. if(best_ast->remaining)
  357. i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
  358. else
  359. i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
  360. // av_log(NULL, AV_LOG_DEBUG, "%d\n", i);
  361. if(i>=0){
  362. int64_t pos= best_st->index_entries[i].pos;
  363. pos += avi->movi_list + best_ast->packet_size - best_ast->remaining;
  364. url_fseek(&s->pb, pos, SEEK_SET);
  365. // av_log(NULL, AV_LOG_DEBUG, "pos=%Ld\n", pos);
  366. if(best_ast->remaining)
  367. avi->stream_index= best_stream_index;
  368. else
  369. avi->stream_index= -1;
  370. }
  371. }
  372. resync:
  373. if(avi->stream_index >= 0){
  374. AVStream *st= s->streams[ avi->stream_index ];
  375. AVIStream *ast= st->priv_data;
  376. int size;
  377. if(ast->sample_size == 0)
  378. size= INT_MAX;
  379. else if(ast->sample_size < 32)
  380. size= 64*ast->sample_size;
  381. else
  382. size= ast->sample_size;
  383. if(size > ast->remaining)
  384. size= ast->remaining;
  385. av_new_packet(pkt, size);
  386. get_buffer(pb, pkt->data, size);
  387. if (avi->dv_demux) {
  388. dstr = pkt->destruct;
  389. size = dv_produce_packet(avi->dv_demux, pkt,
  390. pkt->data, pkt->size);
  391. pkt->destruct = dstr;
  392. pkt->flags |= PKT_FLAG_KEY;
  393. } else {
  394. /* XXX: how to handle B frames in avi ? */
  395. pkt->dts = ast->frame_offset;
  396. // pkt->dts += ast->start;
  397. if(ast->sample_size)
  398. pkt->dts /= ast->sample_size;
  399. //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);
  400. pkt->stream_index = avi->stream_index;
  401. if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
  402. if(st->index_entries){
  403. AVIndexEntry *e;
  404. int index;
  405. index= av_index_search_timestamp(st, pkt->dts, 0);
  406. e= &st->index_entries[index];
  407. if(e->timestamp == ast->frame_offset){
  408. if (e->flags & AVINDEX_KEYFRAME)
  409. pkt->flags |= PKT_FLAG_KEY;
  410. }
  411. } else {
  412. /* if no index, better to say that all frames
  413. are key frames */
  414. pkt->flags |= PKT_FLAG_KEY;
  415. }
  416. } else {
  417. pkt->flags |= PKT_FLAG_KEY;
  418. }
  419. if(ast->sample_size)
  420. ast->frame_offset += pkt->size;
  421. else
  422. ast->frame_offset++;
  423. }
  424. ast->remaining -= size;
  425. if(!ast->remaining){
  426. avi->stream_index= -1;
  427. ast->packet_size= 0;
  428. if (size & 1) {
  429. get_byte(pb);
  430. size++;
  431. }
  432. }
  433. return size;
  434. }
  435. memset(d, -1, sizeof(int)*8);
  436. for(i=sync=url_ftell(pb); !url_feof(pb); i++) {
  437. int j;
  438. if (i >= avi->movi_end) {
  439. if (avi->is_odml) {
  440. url_fskip(pb, avi->riff_end - i);
  441. avi->riff_end = avi->movi_end = url_filesize(url_fileno(pb));
  442. } else
  443. break;
  444. }
  445. for(j=0; j<7; j++)
  446. d[j]= d[j+1];
  447. d[7]= get_byte(pb);
  448. size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
  449. if( d[2] >= '0' && d[2] <= '9'
  450. && d[3] >= '0' && d[3] <= '9'){
  451. n= (d[2] - '0') * 10 + (d[3] - '0');
  452. }else{
  453. n= 100; //invalid stream id
  454. }
  455. //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);
  456. if(i + size > avi->movi_end || d[0]<0)
  457. continue;
  458. //parse ix##
  459. if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
  460. //parse JUNK
  461. ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')){
  462. url_fskip(pb, size);
  463. //av_log(NULL, AV_LOG_DEBUG, "SKIP\n");
  464. goto resync;
  465. }
  466. if( d[0] >= '0' && d[0] <= '9'
  467. && d[1] >= '0' && d[1] <= '9'){
  468. n= (d[0] - '0') * 10 + (d[1] - '0');
  469. }else{
  470. n= 100; //invalid stream id
  471. }
  472. //parse ##dc/##wb
  473. if(n < s->nb_streams){
  474. AVStream *st;
  475. AVIStream *ast;
  476. st = s->streams[n];
  477. ast = st->priv_data;
  478. if( (st->discard >= AVDISCARD_DEFAULT && size==0)
  479. /*|| (st->discard >= AVDISCARD_NONKEY && !(pkt->flags & PKT_FLAG_KEY))*/ //FIXME needs a little reordering
  480. || st->discard >= AVDISCARD_ALL){
  481. if(ast->sample_size) ast->frame_offset += pkt->size;
  482. else ast->frame_offset++;
  483. url_fskip(pb, size);
  484. goto resync;
  485. }
  486. if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
  487. d[2]*256+d[3] == ast->prefix /*||
  488. (d[2] == 'd' && d[3] == 'c') ||
  489. (d[2] == 'w' && d[3] == 'b')*/) {
  490. //av_log(NULL, AV_LOG_DEBUG, "OK\n");
  491. if(d[2]*256+d[3] == ast->prefix)
  492. ast->prefix_count++;
  493. else{
  494. ast->prefix= d[2]*256+d[3];
  495. ast->prefix_count= 0;
  496. }
  497. avi->stream_index= n;
  498. ast->packet_size= size + 8;
  499. ast->remaining= size;
  500. goto resync;
  501. }
  502. }
  503. /* palette changed chunk */
  504. if ( d[0] >= '0' && d[0] <= '9'
  505. && d[1] >= '0' && d[1] <= '9'
  506. && ((d[2] == 'p' && d[3] == 'c'))
  507. && n < s->nb_streams && i + size <= avi->movi_end) {
  508. AVStream *st;
  509. int first, clr, flags, k, p;
  510. st = s->streams[n];
  511. first = get_byte(pb);
  512. clr = get_byte(pb);
  513. if(!clr) /* all 256 colors used */
  514. clr = 256;
  515. flags = get_le16(pb);
  516. p = 4;
  517. for (k = first; k < clr + first; k++) {
  518. int r, g, b;
  519. r = get_byte(pb);
  520. g = get_byte(pb);
  521. b = get_byte(pb);
  522. get_byte(pb);
  523. st->codec.palctrl->palette[k] = b + (g << 8) + (r << 16);
  524. }
  525. st->codec.palctrl->palette_changed = 1;
  526. goto resync;
  527. }
  528. }
  529. return -1;
  530. }
  531. /* XXX: we make the implicit supposition that the position are sorted
  532. for each stream */
  533. static int avi_read_idx1(AVFormatContext *s, int size)
  534. {
  535. AVIContext *avi = s->priv_data;
  536. ByteIOContext *pb = &s->pb;
  537. int nb_index_entries, i;
  538. AVStream *st;
  539. AVIStream *ast;
  540. unsigned int index, tag, flags, pos, len;
  541. unsigned last_pos= -1;
  542. nb_index_entries = size / 16;
  543. if (nb_index_entries <= 0)
  544. return -1;
  545. /* read the entries and sort them in each stream component */
  546. for(i = 0; i < nb_index_entries; i++) {
  547. tag = get_le32(pb);
  548. flags = get_le32(pb);
  549. pos = get_le32(pb);
  550. len = get_le32(pb);
  551. #if defined(DEBUG_SEEK)
  552. av_log(NULL, AV_LOG_DEBUG, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
  553. i, tag, flags, pos, len);
  554. #endif
  555. if(i==0 && pos > avi->movi_list)
  556. avi->movi_list= 0; //FIXME better check
  557. index = ((tag & 0xff) - '0') * 10;
  558. index += ((tag >> 8) & 0xff) - '0';
  559. if (index >= s->nb_streams)
  560. continue;
  561. st = s->streams[index];
  562. ast = st->priv_data;
  563. #if defined(DEBUG_SEEK)
  564. av_log(NULL, AV_LOG_DEBUG, "%d cum_len=%d\n", len, ast->cum_len);
  565. #endif
  566. if(last_pos == pos)
  567. avi->non_interleaved= 1;
  568. else
  569. av_add_index_entry(st, pos, ast->cum_len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
  570. if(ast->sample_size)
  571. ast->cum_len += len / ast->sample_size;
  572. else
  573. ast->cum_len ++;
  574. last_pos= pos;
  575. }
  576. return 0;
  577. }
  578. static int guess_ni_flag(AVFormatContext *s){
  579. AVIContext *avi = s->priv_data;
  580. int i;
  581. int64_t last_start=0;
  582. int64_t first_end= INT64_MAX;
  583. for(i=0; i<s->nb_streams; i++){
  584. AVStream *st = s->streams[i];
  585. AVIStream *ast = st->priv_data;
  586. int n= st->nb_index_entries;
  587. if(n <= 0)
  588. continue;
  589. if(st->index_entries[0].pos > last_start)
  590. last_start= st->index_entries[0].pos;
  591. if(st->index_entries[n-1].pos < first_end)
  592. first_end= st->index_entries[n-1].pos;
  593. }
  594. return last_start > first_end;
  595. }
  596. static int avi_load_index(AVFormatContext *s)
  597. {
  598. AVIContext *avi = s->priv_data;
  599. ByteIOContext *pb = &s->pb;
  600. uint32_t tag, size;
  601. offset_t pos= url_ftell(pb);
  602. url_fseek(pb, avi->movi_end, SEEK_SET);
  603. #ifdef DEBUG_SEEK
  604. printf("movi_end=0x%llx\n", avi->movi_end);
  605. #endif
  606. for(;;) {
  607. if (url_feof(pb))
  608. break;
  609. tag = get_le32(pb);
  610. size = get_le32(pb);
  611. #ifdef DEBUG_SEEK
  612. printf("tag=%c%c%c%c size=0x%x\n",
  613. tag & 0xff,
  614. (tag >> 8) & 0xff,
  615. (tag >> 16) & 0xff,
  616. (tag >> 24) & 0xff,
  617. size);
  618. #endif
  619. switch(tag) {
  620. case MKTAG('i', 'd', 'x', '1'):
  621. if (avi_read_idx1(s, size) < 0)
  622. goto skip;
  623. else
  624. goto the_end;
  625. break;
  626. default:
  627. skip:
  628. size += (size & 1);
  629. url_fskip(pb, size);
  630. break;
  631. }
  632. }
  633. the_end:
  634. avi->non_interleaved |= guess_ni_flag(s);
  635. url_fseek(pb, pos, SEEK_SET);
  636. return 0;
  637. }
  638. static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  639. {
  640. AVIContext *avi = s->priv_data;
  641. AVStream *st;
  642. int i, index;
  643. int64_t pos;
  644. if (!avi->index_loaded) {
  645. /* we only load the index on demand */
  646. avi_load_index(s);
  647. avi->index_loaded = 1;
  648. }
  649. assert(stream_index>= 0);
  650. st = s->streams[stream_index];
  651. index= av_index_search_timestamp(st, timestamp, flags);
  652. if(index<0)
  653. return -1;
  654. /* find the position */
  655. pos = st->index_entries[index].pos;
  656. timestamp = st->index_entries[index].timestamp;
  657. // av_log(NULL, AV_LOG_DEBUG, "XX %Ld %d %Ld\n", timestamp, index, st->index_entries[index].timestamp);
  658. for(i = 0; i < s->nb_streams; i++) {
  659. AVStream *st2 = s->streams[i];
  660. AVIStream *ast2 = st2->priv_data;
  661. ast2->packet_size=
  662. ast2->remaining= 0;
  663. if (st2->nb_index_entries <= 0)
  664. continue;
  665. // assert(st2->codec.block_align);
  666. assert(st2->time_base.den == ast2->rate);
  667. assert(st2->time_base.num == ast2->scale);
  668. index = av_index_search_timestamp(
  669. st2,
  670. av_rescale(timestamp, st2->time_base.den*(int64_t)st->time_base.num, st->time_base.den * (int64_t)st2->time_base.num),
  671. flags | AVSEEK_FLAG_BACKWARD);
  672. if(index<0)
  673. index=0;
  674. if(!avi->non_interleaved){
  675. while(index>0 && st2->index_entries[index].pos > pos)
  676. index--;
  677. while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos)
  678. index++;
  679. }
  680. // av_log(NULL, AV_LOG_DEBUG, "%Ld %d %Ld\n", timestamp, index, st2->index_entries[index].timestamp);
  681. /* extract the current frame number */
  682. ast2->frame_offset = st2->index_entries[index].timestamp;
  683. if(ast2->sample_size)
  684. ast2->frame_offset *=ast2->sample_size;
  685. }
  686. if (avi->dv_demux)
  687. dv_flush_audio_packets(avi->dv_demux);
  688. /* do the seek */
  689. pos += avi->movi_list;
  690. url_fseek(&s->pb, pos, SEEK_SET);
  691. avi->stream_index= -1;
  692. return 0;
  693. }
  694. static int avi_read_close(AVFormatContext *s)
  695. {
  696. int i;
  697. AVIContext *avi = s->priv_data;
  698. for(i=0;i<s->nb_streams;i++) {
  699. AVStream *st = s->streams[i];
  700. AVIStream *ast = st->priv_data;
  701. av_free(ast);
  702. av_free(st->codec.extradata);
  703. av_free(st->codec.palctrl);
  704. }
  705. if (avi->dv_demux)
  706. av_free(avi->dv_demux);
  707. return 0;
  708. }
  709. static int avi_probe(AVProbeData *p)
  710. {
  711. /* check file header */
  712. if (p->buf_size <= 32)
  713. return 0;
  714. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  715. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  716. p->buf[8] == 'A' && p->buf[9] == 'V' &&
  717. p->buf[10] == 'I' && p->buf[11] == ' ')
  718. return AVPROBE_SCORE_MAX;
  719. else
  720. return 0;
  721. }
  722. static AVInputFormat avi_iformat = {
  723. "avi",
  724. "avi format",
  725. sizeof(AVIContext),
  726. avi_probe,
  727. avi_read_header,
  728. avi_read_packet,
  729. avi_read_close,
  730. avi_read_seek,
  731. };
  732. int avidec_init(void)
  733. {
  734. av_register_input_format(&avi_iformat);
  735. return 0;
  736. }