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.

744 lines
23KB

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