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.

828 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. st->need_parsing = 2; //only parse headers dont do slower repacketization, this is needed to get the pict type which is needed for generating correct pts
  276. // url_fskip(pb, size - 5 * 4);
  277. break;
  278. case CODEC_TYPE_AUDIO:
  279. get_wav_header(pb, st->codec, size);
  280. if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
  281. url_fskip(pb, 1);
  282. /* special case time: To support Xan DPCM, hardcode
  283. * the format if Xxan is the video codec */
  284. st->need_parsing = 1;
  285. /* force parsing as several audio frames can be in
  286. one packet */
  287. if (xan_video)
  288. st->codec->codec_id = CODEC_ID_XAN_DPCM;
  289. break;
  290. default:
  291. st->codec->codec_type = CODEC_TYPE_DATA;
  292. st->codec->codec_id= CODEC_ID_NONE;
  293. st->codec->codec_tag= 0;
  294. url_fskip(pb, size);
  295. break;
  296. }
  297. }
  298. break;
  299. default:
  300. /* skip tag */
  301. size += (size & 1);
  302. url_fskip(pb, size);
  303. break;
  304. }
  305. }
  306. end_of_header:
  307. /* check stream number */
  308. if (stream_index != s->nb_streams - 1) {
  309. fail:
  310. for(i=0;i<s->nb_streams;i++) {
  311. av_freep(&s->streams[i]->codec->extradata);
  312. av_freep(&s->streams[i]);
  313. }
  314. return -1;
  315. }
  316. assert(!avi->index_loaded);
  317. avi_load_index(s);
  318. avi->index_loaded = 1;
  319. return 0;
  320. }
  321. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  322. {
  323. AVIContext *avi = s->priv_data;
  324. ByteIOContext *pb = &s->pb;
  325. int n, d[8], size;
  326. offset_t i, sync;
  327. void* dstr;
  328. if (avi->dv_demux) {
  329. size = dv_get_packet(avi->dv_demux, pkt);
  330. if (size >= 0)
  331. return size;
  332. }
  333. if(avi->non_interleaved){
  334. int best_stream_index = 0;
  335. AVStream *best_st= NULL;
  336. AVIStream *best_ast;
  337. int64_t best_ts= INT64_MAX;
  338. int i;
  339. for(i=0; i<s->nb_streams; i++){
  340. AVStream *st = s->streams[i];
  341. AVIStream *ast = st->priv_data;
  342. int64_t ts= ast->frame_offset;
  343. if(ast->sample_size)
  344. ts /= ast->sample_size;
  345. ts= av_rescale(ts, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den);
  346. // av_log(NULL, AV_LOG_DEBUG, "%Ld %d/%d %Ld\n", ts, st->time_base.num, st->time_base.den, ast->frame_offset);
  347. if(ts < best_ts){
  348. best_ts= ts;
  349. best_st= st;
  350. best_stream_index= i;
  351. }
  352. }
  353. best_ast = best_st->priv_data;
  354. 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
  355. if(best_ast->remaining)
  356. i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
  357. else
  358. i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
  359. // av_log(NULL, AV_LOG_DEBUG, "%d\n", i);
  360. if(i>=0){
  361. int64_t pos= best_st->index_entries[i].pos;
  362. pos += avi->movi_list + best_ast->packet_size - best_ast->remaining;
  363. url_fseek(&s->pb, pos, SEEK_SET);
  364. // av_log(NULL, AV_LOG_DEBUG, "pos=%Ld\n", pos);
  365. if(best_ast->remaining)
  366. avi->stream_index= best_stream_index;
  367. else
  368. avi->stream_index= -1;
  369. }
  370. }
  371. resync:
  372. if(avi->stream_index >= 0){
  373. AVStream *st= s->streams[ avi->stream_index ];
  374. AVIStream *ast= st->priv_data;
  375. int size;
  376. if(ast->sample_size == 0)
  377. size= INT_MAX;
  378. else if(ast->sample_size < 32)
  379. size= 64*ast->sample_size;
  380. else
  381. size= ast->sample_size;
  382. if(size > ast->remaining)
  383. size= ast->remaining;
  384. av_get_packet(pb, pkt, 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. 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. int n= st->nb_index_entries;
  583. if(n <= 0)
  584. continue;
  585. if(st->index_entries[0].pos > last_start)
  586. last_start= st->index_entries[0].pos;
  587. if(st->index_entries[n-1].pos < first_end)
  588. first_end= st->index_entries[n-1].pos;
  589. }
  590. return last_start > first_end;
  591. }
  592. static int avi_load_index(AVFormatContext *s)
  593. {
  594. AVIContext *avi = s->priv_data;
  595. ByteIOContext *pb = &s->pb;
  596. uint32_t tag, size;
  597. offset_t pos= url_ftell(pb);
  598. url_fseek(pb, avi->movi_end, SEEK_SET);
  599. #ifdef DEBUG_SEEK
  600. printf("movi_end=0x%llx\n", avi->movi_end);
  601. #endif
  602. for(;;) {
  603. if (url_feof(pb))
  604. break;
  605. tag = get_le32(pb);
  606. size = get_le32(pb);
  607. #ifdef DEBUG_SEEK
  608. printf("tag=%c%c%c%c size=0x%x\n",
  609. tag & 0xff,
  610. (tag >> 8) & 0xff,
  611. (tag >> 16) & 0xff,
  612. (tag >> 24) & 0xff,
  613. size);
  614. #endif
  615. switch(tag) {
  616. case MKTAG('i', 'd', 'x', '1'):
  617. if (avi_read_idx1(s, size) < 0)
  618. goto skip;
  619. else
  620. goto the_end;
  621. break;
  622. default:
  623. skip:
  624. size += (size & 1);
  625. url_fskip(pb, size);
  626. break;
  627. }
  628. }
  629. the_end:
  630. avi->non_interleaved |= guess_ni_flag(s);
  631. url_fseek(pb, pos, SEEK_SET);
  632. return 0;
  633. }
  634. static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  635. {
  636. AVIContext *avi = s->priv_data;
  637. AVStream *st;
  638. int i, index;
  639. int64_t pos;
  640. if (!avi->index_loaded) {
  641. /* we only load the index on demand */
  642. avi_load_index(s);
  643. avi->index_loaded = 1;
  644. }
  645. assert(stream_index>= 0);
  646. st = s->streams[stream_index];
  647. index= av_index_search_timestamp(st, timestamp, flags);
  648. if(index<0)
  649. return -1;
  650. /* find the position */
  651. pos = st->index_entries[index].pos;
  652. timestamp = st->index_entries[index].timestamp;
  653. // av_log(NULL, AV_LOG_DEBUG, "XX %Ld %d %Ld\n", timestamp, index, st->index_entries[index].timestamp);
  654. for(i = 0; i < s->nb_streams; i++) {
  655. AVStream *st2 = s->streams[i];
  656. AVIStream *ast2 = st2->priv_data;
  657. ast2->packet_size=
  658. ast2->remaining= 0;
  659. if (st2->nb_index_entries <= 0)
  660. continue;
  661. // assert(st2->codec.block_align);
  662. assert(st2->time_base.den == ast2->rate);
  663. assert(st2->time_base.num == ast2->scale);
  664. index = av_index_search_timestamp(
  665. st2,
  666. av_rescale(timestamp, st2->time_base.den*(int64_t)st->time_base.num, st->time_base.den * (int64_t)st2->time_base.num),
  667. flags | AVSEEK_FLAG_BACKWARD);
  668. if(index<0)
  669. index=0;
  670. if(!avi->non_interleaved){
  671. while(index>0 && st2->index_entries[index].pos > pos)
  672. index--;
  673. while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos)
  674. index++;
  675. }
  676. // av_log(NULL, AV_LOG_DEBUG, "%Ld %d %Ld\n", timestamp, index, st2->index_entries[index].timestamp);
  677. /* extract the current frame number */
  678. ast2->frame_offset = st2->index_entries[index].timestamp;
  679. if(ast2->sample_size)
  680. ast2->frame_offset *=ast2->sample_size;
  681. }
  682. if (avi->dv_demux)
  683. dv_flush_audio_packets(avi->dv_demux);
  684. /* do the seek */
  685. pos += avi->movi_list;
  686. url_fseek(&s->pb, pos, SEEK_SET);
  687. avi->stream_index= -1;
  688. return 0;
  689. }
  690. static int avi_read_close(AVFormatContext *s)
  691. {
  692. int i;
  693. AVIContext *avi = s->priv_data;
  694. for(i=0;i<s->nb_streams;i++) {
  695. AVStream *st = s->streams[i];
  696. AVIStream *ast = st->priv_data;
  697. av_free(ast);
  698. av_free(st->codec->extradata);
  699. av_free(st->codec->palctrl);
  700. }
  701. if (avi->dv_demux)
  702. av_free(avi->dv_demux);
  703. return 0;
  704. }
  705. static int avi_probe(AVProbeData *p)
  706. {
  707. /* check file header */
  708. if (p->buf_size <= 32)
  709. return 0;
  710. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  711. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  712. p->buf[8] == 'A' && p->buf[9] == 'V' &&
  713. p->buf[10] == 'I' && p->buf[11] == ' ')
  714. return AVPROBE_SCORE_MAX;
  715. else
  716. return 0;
  717. }
  718. static AVInputFormat avi_iformat = {
  719. "avi",
  720. "avi format",
  721. sizeof(AVIContext),
  722. avi_probe,
  723. avi_read_header,
  724. avi_read_packet,
  725. avi_read_close,
  726. avi_read_seek,
  727. };
  728. int avidec_init(void)
  729. {
  730. av_register_input_format(&avi_iformat);
  731. return 0;
  732. }