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.

978 lines
31KB

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