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.

985 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:%16LX\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:%Ld, 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=%Lx\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. /* force parsing as several audio frames can be in
  397. one packet */
  398. if (xan_video)
  399. st->codec->codec_id = CODEC_ID_XAN_DPCM;
  400. break;
  401. default:
  402. st->codec->codec_type = CODEC_TYPE_DATA;
  403. st->codec->codec_id= CODEC_ID_NONE;
  404. st->codec->codec_tag= 0;
  405. url_fskip(pb, size);
  406. break;
  407. }
  408. }
  409. break;
  410. case MKTAG('i', 'n', 'd', 'x'):
  411. i= url_ftell(pb);
  412. if(!url_is_streamed(pb) && !(s->flags & AVFMT_FLAG_IGNIDX)){
  413. read_braindead_odml_indx(s, 0);
  414. }
  415. url_fseek(pb, i+size, SEEK_SET);
  416. break;
  417. case MKTAG('I', 'N', 'A', 'M'):
  418. avi_read_tag(pb, s->title, sizeof(s->title), size);
  419. break;
  420. case MKTAG('I', 'A', 'R', 'T'):
  421. avi_read_tag(pb, s->author, sizeof(s->author), size);
  422. break;
  423. case MKTAG('I', 'C', 'O', 'P'):
  424. avi_read_tag(pb, s->copyright, sizeof(s->copyright), size);
  425. break;
  426. case MKTAG('I', 'C', 'M', 'T'):
  427. avi_read_tag(pb, s->comment, sizeof(s->comment), size);
  428. break;
  429. case MKTAG('I', 'G', 'N', 'R'):
  430. avi_read_tag(pb, s->genre, sizeof(s->genre), size);
  431. break;
  432. case MKTAG('I', 'P', 'R', 'D'):
  433. avi_read_tag(pb, s->album, sizeof(s->album), size);
  434. break;
  435. case MKTAG('I', 'P', 'R', 'T'):
  436. avi_read_tag(pb, str_track, sizeof(str_track), size);
  437. sscanf(str_track, "%d", &s->track);
  438. break;
  439. default:
  440. /* skip tag */
  441. size += (size & 1);
  442. url_fskip(pb, size);
  443. break;
  444. }
  445. }
  446. end_of_header:
  447. /* check stream number */
  448. if (stream_index != s->nb_streams - 1) {
  449. fail:
  450. for(i=0;i<s->nb_streams;i++) {
  451. av_freep(&s->streams[i]->codec->extradata);
  452. av_freep(&s->streams[i]);
  453. }
  454. return -1;
  455. }
  456. if(!avi->index_loaded && !url_is_streamed(pb))
  457. avi_load_index(s);
  458. avi->index_loaded = 1;
  459. avi->non_interleaved |= guess_ni_flag(s);
  460. if(avi->non_interleaved)
  461. clean_index(s);
  462. return 0;
  463. }
  464. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  465. {
  466. AVIContext *avi = s->priv_data;
  467. ByteIOContext *pb = &s->pb;
  468. int n, d[8], size;
  469. offset_t i, sync;
  470. void* dstr;
  471. if (avi->dv_demux) {
  472. size = dv_get_packet(avi->dv_demux, pkt);
  473. if (size >= 0)
  474. return size;
  475. }
  476. if(avi->non_interleaved){
  477. int best_stream_index = 0;
  478. AVStream *best_st= NULL;
  479. AVIStream *best_ast;
  480. int64_t best_ts= INT64_MAX;
  481. int i;
  482. for(i=0; i<s->nb_streams; i++){
  483. AVStream *st = s->streams[i];
  484. AVIStream *ast = st->priv_data;
  485. int64_t ts= ast->frame_offset;
  486. if(ast->sample_size)
  487. ts /= ast->sample_size;
  488. ts= av_rescale(ts, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den);
  489. // av_log(NULL, AV_LOG_DEBUG, "%Ld %d/%d %Ld\n", ts, st->time_base.num, st->time_base.den, ast->frame_offset);
  490. if(ts < best_ts){
  491. best_ts= ts;
  492. best_st= st;
  493. best_stream_index= i;
  494. }
  495. }
  496. best_ast = best_st->priv_data;
  497. 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
  498. if(best_ast->remaining)
  499. i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
  500. else
  501. i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
  502. // av_log(NULL, AV_LOG_DEBUG, "%d\n", i);
  503. if(i>=0){
  504. int64_t pos= best_st->index_entries[i].pos;
  505. pos += best_ast->packet_size - best_ast->remaining;
  506. url_fseek(&s->pb, pos + 8, SEEK_SET);
  507. // av_log(NULL, AV_LOG_DEBUG, "pos=%Ld\n", pos);
  508. assert(best_ast->remaining <= best_ast->packet_size);
  509. avi->stream_index= best_stream_index;
  510. if(!best_ast->remaining)
  511. best_ast->packet_size=
  512. best_ast->remaining= best_st->index_entries[i].size;
  513. }
  514. }
  515. resync:
  516. if(avi->stream_index >= 0){
  517. AVStream *st= s->streams[ avi->stream_index ];
  518. AVIStream *ast= st->priv_data;
  519. int size;
  520. if(ast->sample_size <= 1) // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
  521. size= INT_MAX;
  522. else if(ast->sample_size < 32)
  523. size= 64*ast->sample_size;
  524. else
  525. size= ast->sample_size;
  526. if(size > ast->remaining)
  527. size= ast->remaining;
  528. av_get_packet(pb, pkt, size);
  529. if (avi->dv_demux) {
  530. dstr = pkt->destruct;
  531. size = dv_produce_packet(avi->dv_demux, pkt,
  532. pkt->data, pkt->size);
  533. pkt->destruct = dstr;
  534. pkt->flags |= PKT_FLAG_KEY;
  535. } else {
  536. /* XXX: how to handle B frames in avi ? */
  537. pkt->dts = ast->frame_offset;
  538. // pkt->dts += ast->start;
  539. if(ast->sample_size)
  540. pkt->dts /= ast->sample_size;
  541. //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);
  542. pkt->stream_index = avi->stream_index;
  543. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  544. if(st->index_entries){
  545. AVIndexEntry *e;
  546. int index;
  547. index= av_index_search_timestamp(st, pkt->dts, 0);
  548. e= &st->index_entries[index];
  549. if(index >= 0 && e->timestamp == ast->frame_offset){
  550. if (e->flags & AVINDEX_KEYFRAME)
  551. pkt->flags |= PKT_FLAG_KEY;
  552. }
  553. } else {
  554. /* if no index, better to say that all frames
  555. are key frames */
  556. pkt->flags |= PKT_FLAG_KEY;
  557. }
  558. } else {
  559. pkt->flags |= PKT_FLAG_KEY;
  560. }
  561. if(ast->sample_size)
  562. ast->frame_offset += pkt->size;
  563. else
  564. ast->frame_offset++;
  565. }
  566. ast->remaining -= size;
  567. if(!ast->remaining){
  568. avi->stream_index= -1;
  569. ast->packet_size= 0;
  570. if (size & 1) {
  571. get_byte(pb);
  572. size++;
  573. }
  574. }
  575. return size;
  576. }
  577. memset(d, -1, sizeof(int)*8);
  578. for(i=sync=url_ftell(pb); !url_feof(pb); i++) {
  579. int j;
  580. if (i >= avi->movi_end) {
  581. if (avi->is_odml) {
  582. url_fskip(pb, avi->riff_end - i);
  583. avi->riff_end = avi->movi_end = url_fsize(pb);
  584. } else
  585. break;
  586. }
  587. for(j=0; j<7; j++)
  588. d[j]= d[j+1];
  589. d[7]= get_byte(pb);
  590. size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
  591. if( d[2] >= '0' && d[2] <= '9'
  592. && d[3] >= '0' && d[3] <= '9'){
  593. n= (d[2] - '0') * 10 + (d[3] - '0');
  594. }else{
  595. n= 100; //invalid stream id
  596. }
  597. //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);
  598. if(i + size > avi->movi_end || d[0]<0)
  599. continue;
  600. //parse ix##
  601. if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
  602. //parse JUNK
  603. ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')){
  604. url_fskip(pb, size);
  605. //av_log(NULL, AV_LOG_DEBUG, "SKIP\n");
  606. goto resync;
  607. }
  608. if( d[0] >= '0' && d[0] <= '9'
  609. && d[1] >= '0' && d[1] <= '9'){
  610. n= (d[0] - '0') * 10 + (d[1] - '0');
  611. }else{
  612. n= 100; //invalid stream id
  613. }
  614. //parse ##dc/##wb
  615. if(n < s->nb_streams){
  616. AVStream *st;
  617. AVIStream *ast;
  618. st = s->streams[n];
  619. ast = st->priv_data;
  620. if( (st->discard >= AVDISCARD_DEFAULT && size==0)
  621. /*|| (st->discard >= AVDISCARD_NONKEY && !(pkt->flags & PKT_FLAG_KEY))*/ //FIXME needs a little reordering
  622. || st->discard >= AVDISCARD_ALL){
  623. if(ast->sample_size) ast->frame_offset += pkt->size;
  624. else ast->frame_offset++;
  625. url_fskip(pb, size);
  626. goto resync;
  627. }
  628. if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
  629. d[2]*256+d[3] == ast->prefix /*||
  630. (d[2] == 'd' && d[3] == 'c') ||
  631. (d[2] == 'w' && d[3] == 'b')*/) {
  632. //av_log(NULL, AV_LOG_DEBUG, "OK\n");
  633. if(d[2]*256+d[3] == ast->prefix)
  634. ast->prefix_count++;
  635. else{
  636. ast->prefix= d[2]*256+d[3];
  637. ast->prefix_count= 0;
  638. }
  639. avi->stream_index= n;
  640. ast->packet_size= size + 8;
  641. ast->remaining= size;
  642. goto resync;
  643. }
  644. }
  645. /* palette changed chunk */
  646. if ( d[0] >= '0' && d[0] <= '9'
  647. && d[1] >= '0' && d[1] <= '9'
  648. && ((d[2] == 'p' && d[3] == 'c'))
  649. && n < s->nb_streams && i + size <= avi->movi_end) {
  650. AVStream *st;
  651. int first, clr, flags, k, p;
  652. st = s->streams[n];
  653. first = get_byte(pb);
  654. clr = get_byte(pb);
  655. if(!clr) /* all 256 colors used */
  656. clr = 256;
  657. flags = get_le16(pb);
  658. p = 4;
  659. for (k = first; k < clr + first; k++) {
  660. int r, g, b;
  661. r = get_byte(pb);
  662. g = get_byte(pb);
  663. b = get_byte(pb);
  664. get_byte(pb);
  665. st->codec->palctrl->palette[k] = b + (g << 8) + (r << 16);
  666. }
  667. st->codec->palctrl->palette_changed = 1;
  668. goto resync;
  669. }
  670. }
  671. return -1;
  672. }
  673. /* XXX: we make the implicit supposition that the position are sorted
  674. for each stream */
  675. static int avi_read_idx1(AVFormatContext *s, int size)
  676. {
  677. AVIContext *avi = s->priv_data;
  678. ByteIOContext *pb = &s->pb;
  679. int nb_index_entries, i;
  680. AVStream *st;
  681. AVIStream *ast;
  682. unsigned int index, tag, flags, pos, len;
  683. unsigned last_pos= -1;
  684. nb_index_entries = size / 16;
  685. if (nb_index_entries <= 0)
  686. return -1;
  687. /* read the entries and sort them in each stream component */
  688. for(i = 0; i < nb_index_entries; i++) {
  689. tag = get_le32(pb);
  690. flags = get_le32(pb);
  691. pos = get_le32(pb);
  692. len = get_le32(pb);
  693. #if defined(DEBUG_SEEK)
  694. av_log(NULL, AV_LOG_DEBUG, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
  695. i, tag, flags, pos, len);
  696. #endif
  697. if(i==0 && pos > avi->movi_list)
  698. avi->movi_list= 0; //FIXME better check
  699. pos += avi->movi_list;
  700. index = ((tag & 0xff) - '0') * 10;
  701. index += ((tag >> 8) & 0xff) - '0';
  702. if (index >= s->nb_streams)
  703. continue;
  704. st = s->streams[index];
  705. ast = st->priv_data;
  706. #if defined(DEBUG_SEEK)
  707. av_log(NULL, AV_LOG_DEBUG, "%d cum_len=%Ld\n", len, ast->cum_len);
  708. #endif
  709. if(last_pos == pos)
  710. avi->non_interleaved= 1;
  711. else
  712. av_add_index_entry(st, pos, ast->cum_len, len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
  713. if(ast->sample_size)
  714. ast->cum_len += len / ast->sample_size;
  715. else
  716. ast->cum_len ++;
  717. last_pos= pos;
  718. }
  719. return 0;
  720. }
  721. static int guess_ni_flag(AVFormatContext *s){
  722. int i;
  723. int64_t last_start=0;
  724. int64_t first_end= INT64_MAX;
  725. for(i=0; i<s->nb_streams; i++){
  726. AVStream *st = s->streams[i];
  727. int n= st->nb_index_entries;
  728. if(n <= 0)
  729. continue;
  730. if(st->index_entries[0].pos > last_start)
  731. last_start= st->index_entries[0].pos;
  732. if(st->index_entries[n-1].pos < first_end)
  733. first_end= st->index_entries[n-1].pos;
  734. }
  735. return last_start > first_end;
  736. }
  737. static int avi_load_index(AVFormatContext *s)
  738. {
  739. AVIContext *avi = s->priv_data;
  740. ByteIOContext *pb = &s->pb;
  741. uint32_t tag, size;
  742. offset_t pos= url_ftell(pb);
  743. url_fseek(pb, avi->movi_end, SEEK_SET);
  744. #ifdef DEBUG_SEEK
  745. printf("movi_end=0x%llx\n", avi->movi_end);
  746. #endif
  747. for(;;) {
  748. if (url_feof(pb))
  749. break;
  750. tag = get_le32(pb);
  751. size = get_le32(pb);
  752. #ifdef DEBUG_SEEK
  753. printf("tag=%c%c%c%c size=0x%x\n",
  754. tag & 0xff,
  755. (tag >> 8) & 0xff,
  756. (tag >> 16) & 0xff,
  757. (tag >> 24) & 0xff,
  758. size);
  759. #endif
  760. switch(tag) {
  761. case MKTAG('i', 'd', 'x', '1'):
  762. if (avi_read_idx1(s, size) < 0)
  763. goto skip;
  764. else
  765. goto the_end;
  766. break;
  767. default:
  768. skip:
  769. size += (size & 1);
  770. url_fskip(pb, size);
  771. break;
  772. }
  773. }
  774. the_end:
  775. url_fseek(pb, pos, SEEK_SET);
  776. return 0;
  777. }
  778. static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  779. {
  780. AVIContext *avi = s->priv_data;
  781. AVStream *st;
  782. int i, index;
  783. int64_t pos;
  784. if (!avi->index_loaded) {
  785. /* we only load the index on demand */
  786. avi_load_index(s);
  787. avi->index_loaded = 1;
  788. }
  789. assert(stream_index>= 0);
  790. st = s->streams[stream_index];
  791. index= av_index_search_timestamp(st, timestamp, flags);
  792. if(index<0)
  793. return -1;
  794. /* find the position */
  795. pos = st->index_entries[index].pos;
  796. timestamp = st->index_entries[index].timestamp;
  797. // av_log(NULL, AV_LOG_DEBUG, "XX %Ld %d %Ld\n", timestamp, index, st->index_entries[index].timestamp);
  798. for(i = 0; i < s->nb_streams; i++) {
  799. AVStream *st2 = s->streams[i];
  800. AVIStream *ast2 = st2->priv_data;
  801. ast2->packet_size=
  802. ast2->remaining= 0;
  803. if (st2->nb_index_entries <= 0)
  804. continue;
  805. // assert(st2->codec->block_align);
  806. assert(st2->time_base.den == ast2->rate);
  807. assert(st2->time_base.num == ast2->scale);
  808. index = av_index_search_timestamp(
  809. st2,
  810. av_rescale(timestamp, st2->time_base.den*(int64_t)st->time_base.num, st->time_base.den * (int64_t)st2->time_base.num),
  811. flags | AVSEEK_FLAG_BACKWARD);
  812. if(index<0)
  813. index=0;
  814. if(!avi->non_interleaved){
  815. while(index>0 && st2->index_entries[index].pos > pos)
  816. index--;
  817. while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos)
  818. index++;
  819. }
  820. // av_log(NULL, AV_LOG_DEBUG, "%Ld %d %Ld\n", timestamp, index, st2->index_entries[index].timestamp);
  821. /* extract the current frame number */
  822. ast2->frame_offset = st2->index_entries[index].timestamp;
  823. if(ast2->sample_size)
  824. ast2->frame_offset *=ast2->sample_size;
  825. }
  826. if (avi->dv_demux)
  827. dv_flush_audio_packets(avi->dv_demux);
  828. /* do the seek */
  829. url_fseek(&s->pb, pos, SEEK_SET);
  830. avi->stream_index= -1;
  831. return 0;
  832. }
  833. static int avi_read_close(AVFormatContext *s)
  834. {
  835. int i;
  836. AVIContext *avi = s->priv_data;
  837. for(i=0;i<s->nb_streams;i++) {
  838. AVStream *st = s->streams[i];
  839. AVIStream *ast = st->priv_data;
  840. av_free(ast);
  841. av_free(st->codec->palctrl);
  842. }
  843. if (avi->dv_demux)
  844. av_free(avi->dv_demux);
  845. return 0;
  846. }
  847. static int avi_probe(AVProbeData *p)
  848. {
  849. /* check file header */
  850. if (p->buf_size <= 32)
  851. return 0;
  852. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  853. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  854. p->buf[8] == 'A' && p->buf[9] == 'V' &&
  855. p->buf[10] == 'I' && p->buf[11] == ' ')
  856. return AVPROBE_SCORE_MAX;
  857. else
  858. return 0;
  859. }
  860. AVInputFormat avi_demuxer = {
  861. "avi",
  862. "avi format",
  863. sizeof(AVIContext),
  864. avi_probe,
  865. avi_read_header,
  866. avi_read_packet,
  867. avi_read_close,
  868. avi_read_seek,
  869. };