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.

988 lines
31KB

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