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.

1017 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. if (size & 1) {
  587. get_byte(pb);
  588. size++;
  589. }
  590. }
  591. return size;
  592. }
  593. memset(d, -1, sizeof(int)*8);
  594. for(i=sync=url_ftell(pb); !url_feof(pb); i++) {
  595. int j;
  596. if (i >= avi->movi_end) {
  597. if (avi->is_odml) {
  598. url_fskip(pb, avi->riff_end - i);
  599. avi->riff_end = avi->movi_end = url_fsize(pb);
  600. } else
  601. break;
  602. }
  603. for(j=0; j<7; j++)
  604. d[j]= d[j+1];
  605. d[7]= get_byte(pb);
  606. size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
  607. if( d[2] >= '0' && d[2] <= '9'
  608. && d[3] >= '0' && d[3] <= '9'){
  609. n= (d[2] - '0') * 10 + (d[3] - '0');
  610. }else{
  611. n= 100; //invalid stream id
  612. }
  613. //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);
  614. if(i + size > avi->movi_end || d[0]<0)
  615. continue;
  616. //parse ix##
  617. if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
  618. //parse JUNK
  619. ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')){
  620. url_fskip(pb, size);
  621. //av_log(NULL, AV_LOG_DEBUG, "SKIP\n");
  622. goto resync;
  623. }
  624. if( d[0] >= '0' && d[0] <= '9'
  625. && d[1] >= '0' && d[1] <= '9'){
  626. n= (d[0] - '0') * 10 + (d[1] - '0');
  627. }else{
  628. n= 100; //invalid stream id
  629. }
  630. //parse ##dc/##wb
  631. if(n < s->nb_streams){
  632. AVStream *st;
  633. AVIStream *ast;
  634. st = s->streams[n];
  635. ast = st->priv_data;
  636. if( (st->discard >= AVDISCARD_DEFAULT && size==0)
  637. /*|| (st->discard >= AVDISCARD_NONKEY && !(pkt->flags & PKT_FLAG_KEY))*/ //FIXME needs a little reordering
  638. || st->discard >= AVDISCARD_ALL){
  639. if(ast->sample_size) ast->frame_offset += pkt->size;
  640. else ast->frame_offset++;
  641. url_fskip(pb, size);
  642. goto resync;
  643. }
  644. if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
  645. d[2]*256+d[3] == ast->prefix /*||
  646. (d[2] == 'd' && d[3] == 'c') ||
  647. (d[2] == 'w' && d[3] == 'b')*/) {
  648. //av_log(NULL, AV_LOG_DEBUG, "OK\n");
  649. if(d[2]*256+d[3] == ast->prefix)
  650. ast->prefix_count++;
  651. else{
  652. ast->prefix= d[2]*256+d[3];
  653. ast->prefix_count= 0;
  654. }
  655. avi->stream_index= n;
  656. ast->packet_size= size + 8;
  657. ast->remaining= size;
  658. goto resync;
  659. }
  660. }
  661. /* palette changed chunk */
  662. if ( d[0] >= '0' && d[0] <= '9'
  663. && d[1] >= '0' && d[1] <= '9'
  664. && ((d[2] == 'p' && d[3] == 'c'))
  665. && n < s->nb_streams && i + size <= avi->movi_end) {
  666. AVStream *st;
  667. int first, clr, flags, k, p;
  668. st = s->streams[n];
  669. first = get_byte(pb);
  670. clr = get_byte(pb);
  671. if(!clr) /* all 256 colors used */
  672. clr = 256;
  673. flags = get_le16(pb);
  674. p = 4;
  675. for (k = first; k < clr + first; k++) {
  676. int r, g, b;
  677. r = get_byte(pb);
  678. g = get_byte(pb);
  679. b = get_byte(pb);
  680. get_byte(pb);
  681. st->codec->palctrl->palette[k] = b + (g << 8) + (r << 16);
  682. }
  683. st->codec->palctrl->palette_changed = 1;
  684. goto resync;
  685. }
  686. }
  687. return -1;
  688. }
  689. /* XXX: we make the implicit supposition that the position are sorted
  690. for each stream */
  691. static int avi_read_idx1(AVFormatContext *s, int size)
  692. {
  693. AVIContext *avi = s->priv_data;
  694. ByteIOContext *pb = &s->pb;
  695. int nb_index_entries, i;
  696. AVStream *st;
  697. AVIStream *ast;
  698. unsigned int index, tag, flags, pos, len;
  699. unsigned last_pos= -1;
  700. nb_index_entries = size / 16;
  701. if (nb_index_entries <= 0)
  702. return -1;
  703. /* read the entries and sort them in each stream component */
  704. for(i = 0; i < nb_index_entries; i++) {
  705. tag = get_le32(pb);
  706. flags = get_le32(pb);
  707. pos = get_le32(pb);
  708. len = get_le32(pb);
  709. #if defined(DEBUG_SEEK)
  710. av_log(NULL, AV_LOG_DEBUG, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
  711. i, tag, flags, pos, len);
  712. #endif
  713. if(i==0 && pos > avi->movi_list)
  714. avi->movi_list= 0; //FIXME better check
  715. pos += avi->movi_list;
  716. index = ((tag & 0xff) - '0') * 10;
  717. index += ((tag >> 8) & 0xff) - '0';
  718. if (index >= s->nb_streams)
  719. continue;
  720. st = s->streams[index];
  721. ast = st->priv_data;
  722. #if defined(DEBUG_SEEK)
  723. av_log(NULL, AV_LOG_DEBUG, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
  724. #endif
  725. if(last_pos == pos)
  726. avi->non_interleaved= 1;
  727. else
  728. av_add_index_entry(st, pos, ast->cum_len / FFMAX(1, ast->sample_size), len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
  729. if(ast->sample_size)
  730. ast->cum_len += len;
  731. else
  732. ast->cum_len ++;
  733. last_pos= pos;
  734. }
  735. return 0;
  736. }
  737. static int guess_ni_flag(AVFormatContext *s){
  738. int i;
  739. int64_t last_start=0;
  740. int64_t first_end= INT64_MAX;
  741. for(i=0; i<s->nb_streams; i++){
  742. AVStream *st = s->streams[i];
  743. int n= st->nb_index_entries;
  744. if(n <= 0)
  745. continue;
  746. if(st->index_entries[0].pos > last_start)
  747. last_start= st->index_entries[0].pos;
  748. if(st->index_entries[n-1].pos < first_end)
  749. first_end= st->index_entries[n-1].pos;
  750. }
  751. return last_start > first_end;
  752. }
  753. static int avi_load_index(AVFormatContext *s)
  754. {
  755. AVIContext *avi = s->priv_data;
  756. ByteIOContext *pb = &s->pb;
  757. uint32_t tag, size;
  758. offset_t pos= url_ftell(pb);
  759. url_fseek(pb, avi->movi_end, SEEK_SET);
  760. #ifdef DEBUG_SEEK
  761. printf("movi_end=0x%"PRIx64"\n", avi->movi_end);
  762. #endif
  763. for(;;) {
  764. if (url_feof(pb))
  765. break;
  766. tag = get_le32(pb);
  767. size = get_le32(pb);
  768. #ifdef DEBUG_SEEK
  769. printf("tag=%c%c%c%c size=0x%x\n",
  770. tag & 0xff,
  771. (tag >> 8) & 0xff,
  772. (tag >> 16) & 0xff,
  773. (tag >> 24) & 0xff,
  774. size);
  775. #endif
  776. switch(tag) {
  777. case MKTAG('i', 'd', 'x', '1'):
  778. if (avi_read_idx1(s, size) < 0)
  779. goto skip;
  780. else
  781. goto the_end;
  782. break;
  783. default:
  784. skip:
  785. size += (size & 1);
  786. url_fskip(pb, size);
  787. break;
  788. }
  789. }
  790. the_end:
  791. url_fseek(pb, pos, SEEK_SET);
  792. return 0;
  793. }
  794. static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  795. {
  796. AVIContext *avi = s->priv_data;
  797. AVStream *st;
  798. int i, index;
  799. int64_t pos;
  800. if (!avi->index_loaded) {
  801. /* we only load the index on demand */
  802. avi_load_index(s);
  803. avi->index_loaded = 1;
  804. }
  805. assert(stream_index>= 0);
  806. st = s->streams[stream_index];
  807. index= av_index_search_timestamp(st, timestamp, flags);
  808. if(index<0)
  809. return -1;
  810. /* find the position */
  811. pos = st->index_entries[index].pos;
  812. timestamp = st->index_entries[index].timestamp;
  813. // av_log(NULL, AV_LOG_DEBUG, "XX %"PRId64" %d %"PRId64"\n", timestamp, index, st->index_entries[index].timestamp);
  814. if (ENABLE_DV_DEMUXER && avi->dv_demux) {
  815. /* One and only one real stream for DV in AVI, and it has video */
  816. /* offsets. Calling with other stream indices should have failed */
  817. /* the av_index_search_timestamp call above. */
  818. assert(stream_index == 0);
  819. /* Feed the DV video stream version of the timestamp to the */
  820. /* DV demux so it can synth correct timestamps */
  821. dv_offset_reset(avi->dv_demux, timestamp);
  822. url_fseek(&s->pb, pos, SEEK_SET);
  823. avi->stream_index= -1;
  824. return 0;
  825. }
  826. for(i = 0; i < s->nb_streams; i++) {
  827. AVStream *st2 = s->streams[i];
  828. AVIStream *ast2 = st2->priv_data;
  829. ast2->packet_size=
  830. ast2->remaining= 0;
  831. if (st2->nb_index_entries <= 0)
  832. continue;
  833. // assert(st2->codec->block_align);
  834. assert(st2->time_base.den == ast2->rate);
  835. assert(st2->time_base.num == ast2->scale);
  836. index = av_index_search_timestamp(
  837. st2,
  838. av_rescale(timestamp, st2->time_base.den*(int64_t)st->time_base.num, st->time_base.den * (int64_t)st2->time_base.num),
  839. flags | AVSEEK_FLAG_BACKWARD);
  840. if(index<0)
  841. index=0;
  842. if(!avi->non_interleaved){
  843. while(index>0 && st2->index_entries[index].pos > pos)
  844. index--;
  845. while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos)
  846. index++;
  847. }
  848. // av_log(NULL, AV_LOG_DEBUG, "%"PRId64" %d %"PRId64"\n", timestamp, index, st2->index_entries[index].timestamp);
  849. /* extract the current frame number */
  850. ast2->frame_offset = st2->index_entries[index].timestamp;
  851. if(ast2->sample_size)
  852. ast2->frame_offset *=ast2->sample_size;
  853. }
  854. /* do the seek */
  855. url_fseek(&s->pb, pos, SEEK_SET);
  856. avi->stream_index= -1;
  857. return 0;
  858. }
  859. static int avi_read_close(AVFormatContext *s)
  860. {
  861. int i;
  862. AVIContext *avi = s->priv_data;
  863. for(i=0;i<s->nb_streams;i++) {
  864. AVStream *st = s->streams[i];
  865. AVIStream *ast = st->priv_data;
  866. av_free(ast);
  867. av_free(st->codec->palctrl);
  868. }
  869. if (avi->dv_demux)
  870. av_free(avi->dv_demux);
  871. return 0;
  872. }
  873. static int avi_probe(AVProbeData *p)
  874. {
  875. /* check file header */
  876. if (p->buf_size <= 32)
  877. return 0;
  878. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  879. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  880. p->buf[8] == 'A' && p->buf[9] == 'V' &&
  881. p->buf[10] == 'I' && p->buf[11] == ' ')
  882. return AVPROBE_SCORE_MAX;
  883. else
  884. return 0;
  885. }
  886. AVInputFormat avi_demuxer = {
  887. "avi",
  888. "avi format",
  889. sizeof(AVIContext),
  890. avi_probe,
  891. avi_read_header,
  892. avi_read_packet,
  893. avi_read_close,
  894. avi_read_seek,
  895. };