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.

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