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.

991 lines
32KB

  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 / FFMAX(1, ast->sample_size), len, 0, key ? AVINDEX_KEYFRAME : 0);
  128. if(ast->sample_size)
  129. ast->cum_len += len;
  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. if (ENABLE_DV_DEMUXER) {
  270. avi->dv_demux = dv_init_demux(s);
  271. if (!avi->dv_demux)
  272. goto fail;
  273. }
  274. s->streams[0]->priv_data = ast;
  275. url_fskip(pb, 3 * 4);
  276. ast->scale = get_le32(pb);
  277. ast->rate = get_le32(pb);
  278. stream_index = s->nb_streams - 1;
  279. url_fskip(pb, size - 7*4);
  280. break;
  281. }
  282. if (stream_index >= s->nb_streams) {
  283. url_fskip(pb, size - 8);
  284. /* ignore padding stream */
  285. if (tag1 == MKTAG('p', 'a', 'd', 's'))
  286. stream_index--;
  287. break;
  288. }
  289. st = s->streams[stream_index];
  290. ast = st->priv_data;
  291. st->codec->stream_codec_tag= handler;
  292. get_le32(pb); /* flags */
  293. get_le16(pb); /* priority */
  294. get_le16(pb); /* language */
  295. get_le32(pb); /* initial frame */
  296. ast->scale = get_le32(pb);
  297. ast->rate = get_le32(pb);
  298. if(ast->scale && ast->rate){
  299. }else if(frame_period){
  300. ast->rate = 1000000;
  301. ast->scale = frame_period;
  302. }else{
  303. ast->rate = 25;
  304. ast->scale = 1;
  305. }
  306. av_set_pts_info(st, 64, ast->scale, ast->rate);
  307. ast->cum_len=get_le32(pb); /* start */
  308. nb_frames = get_le32(pb);
  309. st->start_time = 0;
  310. st->duration = nb_frames;
  311. get_le32(pb); /* buffer size */
  312. get_le32(pb); /* quality */
  313. ast->sample_size = get_le32(pb); /* sample ssize */
  314. ast->cum_len *= FFMAX(1, ast->sample_size);
  315. // av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d\n", ast->rate, ast->scale, ast->start, ast->sample_size);
  316. switch(tag1) {
  317. case MKTAG('v', 'i', 'd', 's'):
  318. codec_type = CODEC_TYPE_VIDEO;
  319. ast->sample_size = 0;
  320. break;
  321. case MKTAG('a', 'u', 'd', 's'):
  322. codec_type = CODEC_TYPE_AUDIO;
  323. break;
  324. case MKTAG('t', 'x', 't', 's'):
  325. //FIXME
  326. codec_type = CODEC_TYPE_DATA; //CODEC_TYPE_SUB ? FIXME
  327. break;
  328. case MKTAG('p', 'a', 'd', 's'):
  329. codec_type = CODEC_TYPE_UNKNOWN;
  330. stream_index--;
  331. break;
  332. default:
  333. av_log(s, AV_LOG_ERROR, "unknown stream type %X\n", tag1);
  334. goto fail;
  335. }
  336. ast->frame_offset= ast->cum_len;
  337. url_fskip(pb, size - 12 * 4);
  338. break;
  339. case MKTAG('s', 't', 'r', 'f'):
  340. /* stream header */
  341. if (stream_index >= s->nb_streams || avi->dv_demux) {
  342. url_fskip(pb, size);
  343. } else {
  344. st = s->streams[stream_index];
  345. switch(codec_type) {
  346. case CODEC_TYPE_VIDEO:
  347. get_le32(pb); /* size */
  348. st->codec->width = get_le32(pb);
  349. st->codec->height = get_le32(pb);
  350. get_le16(pb); /* panes */
  351. st->codec->bits_per_sample= get_le16(pb); /* depth */
  352. tag1 = get_le32(pb);
  353. get_le32(pb); /* ImageSize */
  354. get_le32(pb); /* XPelsPerMeter */
  355. get_le32(pb); /* YPelsPerMeter */
  356. get_le32(pb); /* ClrUsed */
  357. get_le32(pb); /* ClrImportant */
  358. if(size > 10*4 && size<(1<<30)){
  359. st->codec->extradata_size= size - 10*4;
  360. st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  361. get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
  362. }
  363. if(st->codec->extradata_size & 1) //FIXME check if the encoder really did this correctly
  364. get_byte(pb);
  365. /* Extract palette from extradata if bpp <= 8 */
  366. /* This code assumes that extradata contains only palette */
  367. /* This is true for all paletted codecs implemented in ffmpeg */
  368. if (st->codec->extradata_size && (st->codec->bits_per_sample <= 8)) {
  369. st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
  370. #ifdef WORDS_BIGENDIAN
  371. for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
  372. st->codec->palctrl->palette[i] = bswap_32(((uint32_t*)st->codec->extradata)[i]);
  373. #else
  374. memcpy(st->codec->palctrl->palette, st->codec->extradata,
  375. FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
  376. #endif
  377. st->codec->palctrl->palette_changed = 1;
  378. }
  379. #ifdef DEBUG
  380. print_tag("video", tag1, 0);
  381. #endif
  382. st->codec->codec_type = CODEC_TYPE_VIDEO;
  383. st->codec->codec_tag = tag1;
  384. st->codec->codec_id = codec_get_id(codec_bmp_tags, tag1);
  385. if (st->codec->codec_id == CODEC_ID_XAN_WC4)
  386. xan_video = 1;
  387. 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
  388. // url_fskip(pb, size - 5 * 4);
  389. break;
  390. case CODEC_TYPE_AUDIO:
  391. get_wav_header(pb, st->codec, size);
  392. if(ast->sample_size && st->codec->block_align && ast->sample_size % st->codec->block_align)
  393. av_log(s, AV_LOG_DEBUG, "invalid sample size or block align detected\n");
  394. if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
  395. url_fskip(pb, 1);
  396. /* special case time: To support Xan DPCM, hardcode
  397. * the format if Xxan is the video codec */
  398. st->need_parsing = 1;
  399. /* ADTS header is in extradata, AAC without header must be stored as exact frames, parser not needed and it will fail */
  400. if (st->codec->codec_id == CODEC_ID_AAC && st->codec->extradata_size)
  401. st->need_parsing = 0;
  402. /* force parsing as several audio frames can be in
  403. one packet */
  404. if (xan_video)
  405. st->codec->codec_id = CODEC_ID_XAN_DPCM;
  406. break;
  407. default:
  408. st->codec->codec_type = CODEC_TYPE_DATA;
  409. st->codec->codec_id= CODEC_ID_NONE;
  410. st->codec->codec_tag= 0;
  411. url_fskip(pb, size);
  412. break;
  413. }
  414. }
  415. break;
  416. case MKTAG('i', 'n', 'd', 'x'):
  417. i= url_ftell(pb);
  418. if(!url_is_streamed(pb) && !(s->flags & AVFMT_FLAG_IGNIDX)){
  419. read_braindead_odml_indx(s, 0);
  420. }
  421. url_fseek(pb, i+size, SEEK_SET);
  422. break;
  423. case MKTAG('I', 'N', 'A', 'M'):
  424. avi_read_tag(pb, s->title, sizeof(s->title), size);
  425. break;
  426. case MKTAG('I', 'A', 'R', 'T'):
  427. avi_read_tag(pb, s->author, sizeof(s->author), size);
  428. break;
  429. case MKTAG('I', 'C', 'O', 'P'):
  430. avi_read_tag(pb, s->copyright, sizeof(s->copyright), size);
  431. break;
  432. case MKTAG('I', 'C', 'M', 'T'):
  433. avi_read_tag(pb, s->comment, sizeof(s->comment), size);
  434. break;
  435. case MKTAG('I', 'G', 'N', 'R'):
  436. avi_read_tag(pb, s->genre, sizeof(s->genre), size);
  437. break;
  438. case MKTAG('I', 'P', 'R', 'D'):
  439. avi_read_tag(pb, s->album, sizeof(s->album), size);
  440. break;
  441. case MKTAG('I', 'P', 'R', 'T'):
  442. avi_read_tag(pb, str_track, sizeof(str_track), size);
  443. sscanf(str_track, "%d", &s->track);
  444. break;
  445. default:
  446. /* skip tag */
  447. size += (size & 1);
  448. url_fskip(pb, size);
  449. break;
  450. }
  451. }
  452. end_of_header:
  453. /* check stream number */
  454. if (stream_index != s->nb_streams - 1) {
  455. fail:
  456. for(i=0;i<s->nb_streams;i++) {
  457. av_freep(&s->streams[i]->codec->extradata);
  458. av_freep(&s->streams[i]);
  459. }
  460. return -1;
  461. }
  462. if(!avi->index_loaded && !url_is_streamed(pb))
  463. avi_load_index(s);
  464. avi->index_loaded = 1;
  465. avi->non_interleaved |= guess_ni_flag(s);
  466. if(avi->non_interleaved)
  467. clean_index(s);
  468. return 0;
  469. }
  470. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  471. {
  472. AVIContext *avi = s->priv_data;
  473. ByteIOContext *pb = &s->pb;
  474. int n, d[8], size;
  475. offset_t i, sync;
  476. void* dstr;
  477. if (ENABLE_DV_DEMUXER && avi->dv_demux) {
  478. size = dv_get_packet(avi->dv_demux, pkt);
  479. if (size >= 0)
  480. return size;
  481. }
  482. if(avi->non_interleaved){
  483. int best_stream_index = 0;
  484. AVStream *best_st= NULL;
  485. AVIStream *best_ast;
  486. int64_t best_ts= INT64_MAX;
  487. int i;
  488. for(i=0; i<s->nb_streams; i++){
  489. AVStream *st = s->streams[i];
  490. AVIStream *ast = st->priv_data;
  491. int64_t ts= ast->frame_offset;
  492. if(ast->sample_size)
  493. ts /= ast->sample_size;
  494. ts= av_rescale(ts, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den);
  495. // av_log(NULL, AV_LOG_DEBUG, "%"PRId64" %d/%d %"PRId64"\n", ts, st->time_base.num, st->time_base.den, ast->frame_offset);
  496. if(ts < best_ts){
  497. best_ts= ts;
  498. best_st= st;
  499. best_stream_index= i;
  500. }
  501. }
  502. best_ast = best_st->priv_data;
  503. 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
  504. if(best_ast->remaining)
  505. i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
  506. else
  507. i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
  508. // av_log(NULL, AV_LOG_DEBUG, "%d\n", i);
  509. if(i>=0){
  510. int64_t pos= best_st->index_entries[i].pos;
  511. pos += best_ast->packet_size - best_ast->remaining;
  512. url_fseek(&s->pb, pos + 8, SEEK_SET);
  513. // av_log(NULL, AV_LOG_DEBUG, "pos=%"PRId64"\n", pos);
  514. assert(best_ast->remaining <= best_ast->packet_size);
  515. avi->stream_index= best_stream_index;
  516. if(!best_ast->remaining)
  517. best_ast->packet_size=
  518. best_ast->remaining= best_st->index_entries[i].size;
  519. }
  520. }
  521. resync:
  522. if(avi->stream_index >= 0){
  523. AVStream *st= s->streams[ avi->stream_index ];
  524. AVIStream *ast= st->priv_data;
  525. int size;
  526. if(ast->sample_size <= 1) // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
  527. size= INT_MAX;
  528. else if(ast->sample_size < 32)
  529. size= 64*ast->sample_size;
  530. else
  531. size= ast->sample_size;
  532. if(size > ast->remaining)
  533. size= ast->remaining;
  534. av_get_packet(pb, pkt, size);
  535. if (ENABLE_DV_DEMUXER && avi->dv_demux) {
  536. dstr = pkt->destruct;
  537. size = dv_produce_packet(avi->dv_demux, pkt,
  538. pkt->data, pkt->size);
  539. pkt->destruct = dstr;
  540. pkt->flags |= PKT_FLAG_KEY;
  541. } else {
  542. /* XXX: how to handle B frames in avi ? */
  543. pkt->dts = ast->frame_offset;
  544. // pkt->dts += ast->start;
  545. if(ast->sample_size)
  546. pkt->dts /= ast->sample_size;
  547. //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);
  548. pkt->stream_index = avi->stream_index;
  549. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  550. if(st->index_entries){
  551. AVIndexEntry *e;
  552. int index;
  553. index= av_index_search_timestamp(st, pkt->dts, 0);
  554. e= &st->index_entries[index];
  555. if(index >= 0 && e->timestamp == ast->frame_offset){
  556. if (e->flags & AVINDEX_KEYFRAME)
  557. pkt->flags |= PKT_FLAG_KEY;
  558. }
  559. } else {
  560. /* if no index, better to say that all frames
  561. are key frames */
  562. pkt->flags |= PKT_FLAG_KEY;
  563. }
  564. } else {
  565. pkt->flags |= PKT_FLAG_KEY;
  566. }
  567. if(ast->sample_size)
  568. ast->frame_offset += pkt->size;
  569. else
  570. ast->frame_offset++;
  571. }
  572. ast->remaining -= size;
  573. if(!ast->remaining){
  574. avi->stream_index= -1;
  575. ast->packet_size= 0;
  576. if (size & 1) {
  577. get_byte(pb);
  578. size++;
  579. }
  580. }
  581. return size;
  582. }
  583. memset(d, -1, sizeof(int)*8);
  584. for(i=sync=url_ftell(pb); !url_feof(pb); i++) {
  585. int j;
  586. if (i >= avi->movi_end) {
  587. if (avi->is_odml) {
  588. url_fskip(pb, avi->riff_end - i);
  589. avi->riff_end = avi->movi_end = url_fsize(pb);
  590. } else
  591. break;
  592. }
  593. for(j=0; j<7; j++)
  594. d[j]= d[j+1];
  595. d[7]= get_byte(pb);
  596. size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
  597. if( d[2] >= '0' && d[2] <= '9'
  598. && d[3] >= '0' && d[3] <= '9'){
  599. n= (d[2] - '0') * 10 + (d[3] - '0');
  600. }else{
  601. n= 100; //invalid stream id
  602. }
  603. //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);
  604. if(i + size > avi->movi_end || d[0]<0)
  605. continue;
  606. //parse ix##
  607. if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
  608. //parse JUNK
  609. ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')){
  610. url_fskip(pb, size);
  611. //av_log(NULL, AV_LOG_DEBUG, "SKIP\n");
  612. goto resync;
  613. }
  614. if( d[0] >= '0' && d[0] <= '9'
  615. && d[1] >= '0' && d[1] <= '9'){
  616. n= (d[0] - '0') * 10 + (d[1] - '0');
  617. }else{
  618. n= 100; //invalid stream id
  619. }
  620. //parse ##dc/##wb
  621. if(n < s->nb_streams){
  622. AVStream *st;
  623. AVIStream *ast;
  624. st = s->streams[n];
  625. ast = st->priv_data;
  626. if( (st->discard >= AVDISCARD_DEFAULT && size==0)
  627. /*|| (st->discard >= AVDISCARD_NONKEY && !(pkt->flags & PKT_FLAG_KEY))*/ //FIXME needs a little reordering
  628. || st->discard >= AVDISCARD_ALL){
  629. if(ast->sample_size) ast->frame_offset += pkt->size;
  630. else ast->frame_offset++;
  631. url_fskip(pb, size);
  632. goto resync;
  633. }
  634. if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
  635. d[2]*256+d[3] == ast->prefix /*||
  636. (d[2] == 'd' && d[3] == 'c') ||
  637. (d[2] == 'w' && d[3] == 'b')*/) {
  638. //av_log(NULL, AV_LOG_DEBUG, "OK\n");
  639. if(d[2]*256+d[3] == ast->prefix)
  640. ast->prefix_count++;
  641. else{
  642. ast->prefix= d[2]*256+d[3];
  643. ast->prefix_count= 0;
  644. }
  645. avi->stream_index= n;
  646. ast->packet_size= size + 8;
  647. ast->remaining= size;
  648. goto resync;
  649. }
  650. }
  651. /* palette changed chunk */
  652. if ( d[0] >= '0' && d[0] <= '9'
  653. && d[1] >= '0' && d[1] <= '9'
  654. && ((d[2] == 'p' && d[3] == 'c'))
  655. && n < s->nb_streams && i + size <= avi->movi_end) {
  656. AVStream *st;
  657. int first, clr, flags, k, p;
  658. st = s->streams[n];
  659. first = get_byte(pb);
  660. clr = get_byte(pb);
  661. if(!clr) /* all 256 colors used */
  662. clr = 256;
  663. flags = get_le16(pb);
  664. p = 4;
  665. for (k = first; k < clr + first; k++) {
  666. int r, g, b;
  667. r = get_byte(pb);
  668. g = get_byte(pb);
  669. b = get_byte(pb);
  670. get_byte(pb);
  671. st->codec->palctrl->palette[k] = b + (g << 8) + (r << 16);
  672. }
  673. st->codec->palctrl->palette_changed = 1;
  674. goto resync;
  675. }
  676. }
  677. return -1;
  678. }
  679. /* XXX: we make the implicit supposition that the position are sorted
  680. for each stream */
  681. static int avi_read_idx1(AVFormatContext *s, int size)
  682. {
  683. AVIContext *avi = s->priv_data;
  684. ByteIOContext *pb = &s->pb;
  685. int nb_index_entries, i;
  686. AVStream *st;
  687. AVIStream *ast;
  688. unsigned int index, tag, flags, pos, len;
  689. unsigned last_pos= -1;
  690. nb_index_entries = size / 16;
  691. if (nb_index_entries <= 0)
  692. return -1;
  693. /* read the entries and sort them in each stream component */
  694. for(i = 0; i < nb_index_entries; i++) {
  695. tag = get_le32(pb);
  696. flags = get_le32(pb);
  697. pos = get_le32(pb);
  698. len = get_le32(pb);
  699. #if defined(DEBUG_SEEK)
  700. av_log(NULL, AV_LOG_DEBUG, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
  701. i, tag, flags, pos, len);
  702. #endif
  703. if(i==0 && pos > avi->movi_list)
  704. avi->movi_list= 0; //FIXME better check
  705. pos += avi->movi_list;
  706. index = ((tag & 0xff) - '0') * 10;
  707. index += ((tag >> 8) & 0xff) - '0';
  708. if (index >= s->nb_streams)
  709. continue;
  710. st = s->streams[index];
  711. ast = st->priv_data;
  712. #if defined(DEBUG_SEEK)
  713. av_log(NULL, AV_LOG_DEBUG, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
  714. #endif
  715. if(last_pos == pos)
  716. avi->non_interleaved= 1;
  717. else
  718. av_add_index_entry(st, pos, ast->cum_len / FFMAX(1, ast->sample_size), len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
  719. if(ast->sample_size)
  720. ast->cum_len += len;
  721. else
  722. ast->cum_len ++;
  723. last_pos= pos;
  724. }
  725. return 0;
  726. }
  727. static int guess_ni_flag(AVFormatContext *s){
  728. int i;
  729. int64_t last_start=0;
  730. int64_t first_end= INT64_MAX;
  731. for(i=0; i<s->nb_streams; i++){
  732. AVStream *st = s->streams[i];
  733. int n= st->nb_index_entries;
  734. if(n <= 0)
  735. continue;
  736. if(st->index_entries[0].pos > last_start)
  737. last_start= st->index_entries[0].pos;
  738. if(st->index_entries[n-1].pos < first_end)
  739. first_end= st->index_entries[n-1].pos;
  740. }
  741. return last_start > first_end;
  742. }
  743. static int avi_load_index(AVFormatContext *s)
  744. {
  745. AVIContext *avi = s->priv_data;
  746. ByteIOContext *pb = &s->pb;
  747. uint32_t tag, size;
  748. offset_t pos= url_ftell(pb);
  749. url_fseek(pb, avi->movi_end, SEEK_SET);
  750. #ifdef DEBUG_SEEK
  751. printf("movi_end=0x%"PRIx64"\n", avi->movi_end);
  752. #endif
  753. for(;;) {
  754. if (url_feof(pb))
  755. break;
  756. tag = get_le32(pb);
  757. size = get_le32(pb);
  758. #ifdef DEBUG_SEEK
  759. printf("tag=%c%c%c%c size=0x%x\n",
  760. tag & 0xff,
  761. (tag >> 8) & 0xff,
  762. (tag >> 16) & 0xff,
  763. (tag >> 24) & 0xff,
  764. size);
  765. #endif
  766. switch(tag) {
  767. case MKTAG('i', 'd', 'x', '1'):
  768. if (avi_read_idx1(s, size) < 0)
  769. goto skip;
  770. else
  771. goto the_end;
  772. break;
  773. default:
  774. skip:
  775. size += (size & 1);
  776. url_fskip(pb, size);
  777. break;
  778. }
  779. }
  780. the_end:
  781. url_fseek(pb, pos, SEEK_SET);
  782. return 0;
  783. }
  784. static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  785. {
  786. AVIContext *avi = s->priv_data;
  787. AVStream *st;
  788. int i, index;
  789. int64_t pos;
  790. if (!avi->index_loaded) {
  791. /* we only load the index on demand */
  792. avi_load_index(s);
  793. avi->index_loaded = 1;
  794. }
  795. assert(stream_index>= 0);
  796. st = s->streams[stream_index];
  797. index= av_index_search_timestamp(st, timestamp, flags);
  798. if(index<0)
  799. return -1;
  800. /* find the position */
  801. pos = st->index_entries[index].pos;
  802. timestamp = st->index_entries[index].timestamp;
  803. // av_log(NULL, AV_LOG_DEBUG, "XX %"PRId64" %d %"PRId64"\n", timestamp, index, st->index_entries[index].timestamp);
  804. for(i = 0; i < s->nb_streams; i++) {
  805. AVStream *st2 = s->streams[i];
  806. AVIStream *ast2 = st2->priv_data;
  807. ast2->packet_size=
  808. ast2->remaining= 0;
  809. if (st2->nb_index_entries <= 0)
  810. continue;
  811. // assert(st2->codec->block_align);
  812. assert(st2->time_base.den == ast2->rate);
  813. assert(st2->time_base.num == ast2->scale);
  814. index = av_index_search_timestamp(
  815. st2,
  816. av_rescale(timestamp, st2->time_base.den*(int64_t)st->time_base.num, st->time_base.den * (int64_t)st2->time_base.num),
  817. flags | AVSEEK_FLAG_BACKWARD);
  818. if(index<0)
  819. index=0;
  820. if(!avi->non_interleaved){
  821. while(index>0 && st2->index_entries[index].pos > pos)
  822. index--;
  823. while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos)
  824. index++;
  825. }
  826. // av_log(NULL, AV_LOG_DEBUG, "%"PRId64" %d %"PRId64"\n", timestamp, index, st2->index_entries[index].timestamp);
  827. /* extract the current frame number */
  828. ast2->frame_offset = st2->index_entries[index].timestamp;
  829. if(ast2->sample_size)
  830. ast2->frame_offset *=ast2->sample_size;
  831. }
  832. if (ENABLE_DV_DEMUXER && avi->dv_demux)
  833. dv_flush_audio_packets(avi->dv_demux);
  834. /* do the seek */
  835. url_fseek(&s->pb, pos, SEEK_SET);
  836. avi->stream_index= -1;
  837. return 0;
  838. }
  839. static int avi_read_close(AVFormatContext *s)
  840. {
  841. int i;
  842. AVIContext *avi = s->priv_data;
  843. for(i=0;i<s->nb_streams;i++) {
  844. AVStream *st = s->streams[i];
  845. AVIStream *ast = st->priv_data;
  846. av_free(ast);
  847. av_free(st->codec->palctrl);
  848. }
  849. if (avi->dv_demux)
  850. av_free(avi->dv_demux);
  851. return 0;
  852. }
  853. static int avi_probe(AVProbeData *p)
  854. {
  855. /* check file header */
  856. if (p->buf_size <= 32)
  857. return 0;
  858. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  859. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  860. p->buf[8] == 'A' && p->buf[9] == 'V' &&
  861. p->buf[10] == 'I' && p->buf[11] == ' ')
  862. return AVPROBE_SCORE_MAX;
  863. else
  864. return 0;
  865. }
  866. AVInputFormat avi_demuxer = {
  867. "avi",
  868. "avi format",
  869. sizeof(AVIContext),
  870. avi_probe,
  871. avi_read_header,
  872. avi_read_packet,
  873. avi_read_close,
  874. avi_read_seek,
  875. };