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.

939 lines
30KB

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