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.

1013 lines
33KB

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