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.

1012 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', 0x19))
  75. av_log(NULL, AV_LOG_INFO, "file has been generated with a totally broken muxer\n");
  76. else
  77. if (tag != MKTAG('A', 'V', 'I', ' ') && tag != MKTAG('A', 'V', 'I', 'X'))
  78. return -1;
  79. return 0;
  80. }
  81. static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
  82. AVIContext *avi = s->priv_data;
  83. ByteIOContext *pb = &s->pb;
  84. int longs_pre_entry= get_le16(pb);
  85. int index_sub_type = get_byte(pb);
  86. int index_type = get_byte(pb);
  87. int entries_in_use = get_le32(pb);
  88. int chunk_id = get_le32(pb);
  89. int64_t base = get_le64(pb);
  90. int stream_id= 10*((chunk_id&0xFF) - '0') + (((chunk_id>>8)&0xFF) - '0');
  91. AVStream *st;
  92. AVIStream *ast;
  93. int i;
  94. int64_t last_pos= -1;
  95. int64_t filesize= url_fsize(&s->pb);
  96. #ifdef DEBUG_SEEK
  97. av_log(s, AV_LOG_ERROR, "longs_pre_entry:%d index_type:%d entries_in_use:%d chunk_id:%X base:%16"PRIX64"\n",
  98. longs_pre_entry,index_type, entries_in_use, chunk_id, base);
  99. #endif
  100. if(stream_id > s->nb_streams || stream_id < 0)
  101. return -1;
  102. st= s->streams[stream_id];
  103. ast = st->priv_data;
  104. if(index_sub_type)
  105. return -1;
  106. get_le32(pb);
  107. if(index_type && longs_pre_entry != 2)
  108. return -1;
  109. if(index_type>1)
  110. return -1;
  111. if(filesize > 0 && base >= filesize){
  112. av_log(s, AV_LOG_ERROR, "ODML index invalid\n");
  113. if(base>>32 == (base & 0xFFFFFFFF) && (base & 0xFFFFFFFF) < filesize && filesize <= 0xFFFFFFFF)
  114. base &= 0xFFFFFFFF;
  115. else
  116. return -1;
  117. }
  118. for(i=0; i<entries_in_use; i++){
  119. if(index_type){
  120. int64_t pos= get_le32(pb) + base - 8;
  121. int len = get_le32(pb);
  122. int key= len >= 0;
  123. len &= 0x7FFFFFFF;
  124. #ifdef DEBUG_SEEK
  125. av_log(s, AV_LOG_ERROR, "pos:%"PRId64", len:%X\n", pos, len);
  126. #endif
  127. if(last_pos == pos || pos == base - 8)
  128. avi->non_interleaved= 1;
  129. else
  130. av_add_index_entry(st, pos, ast->cum_len / FFMAX(1, ast->sample_size), len, 0, key ? AVINDEX_KEYFRAME : 0);
  131. if(ast->sample_size)
  132. ast->cum_len += len;
  133. else
  134. ast->cum_len ++;
  135. last_pos= pos;
  136. }else{
  137. int64_t offset, pos;
  138. int duration;
  139. offset = get_le64(pb);
  140. get_le32(pb); /* size */
  141. duration = get_le32(pb);
  142. pos = url_ftell(pb);
  143. url_fseek(pb, offset+8, SEEK_SET);
  144. read_braindead_odml_indx(s, frame_num);
  145. frame_num += duration;
  146. url_fseek(pb, pos, SEEK_SET);
  147. }
  148. }
  149. avi->index_loaded=1;
  150. return 0;
  151. }
  152. static void clean_index(AVFormatContext *s){
  153. int i;
  154. int64_t j;
  155. for(i=0; i<s->nb_streams; i++){
  156. AVStream *st = s->streams[i];
  157. AVIStream *ast = st->priv_data;
  158. int n= st->nb_index_entries;
  159. int max= ast->sample_size;
  160. int64_t pos, size, ts;
  161. if(n != 1 || ast->sample_size==0)
  162. continue;
  163. while(max < 1024) max+=max;
  164. pos= st->index_entries[0].pos;
  165. size= st->index_entries[0].size;
  166. ts= st->index_entries[0].timestamp;
  167. for(j=0; j<size; j+=max){
  168. av_add_index_entry(st, pos+j, ts + j/ast->sample_size, FFMIN(max, size-j), 0, AVINDEX_KEYFRAME);
  169. }
  170. }
  171. }
  172. static int avi_read_tag(ByteIOContext *pb, char *buf, int maxlen, unsigned int size)
  173. {
  174. offset_t i = url_ftell(pb);
  175. size += (size & 1);
  176. get_strz(pb, buf, maxlen);
  177. url_fseek(pb, i+size, SEEK_SET);
  178. return 0;
  179. }
  180. static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
  181. {
  182. AVIContext *avi = s->priv_data;
  183. ByteIOContext *pb = &s->pb;
  184. uint32_t tag, tag1, handler;
  185. int codec_type, stream_index, frame_period, bit_rate;
  186. unsigned int size, nb_frames;
  187. int i;
  188. AVStream *st;
  189. AVIStream *ast = NULL;
  190. char str_track[4];
  191. avi->stream_index= -1;
  192. if (get_riff(avi, pb) < 0)
  193. return -1;
  194. /* first list tag */
  195. stream_index = -1;
  196. codec_type = -1;
  197. frame_period = 0;
  198. for(;;) {
  199. if (url_feof(pb))
  200. goto fail;
  201. tag = get_le32(pb);
  202. size = get_le32(pb);
  203. #ifdef DEBUG
  204. print_tag("tag", tag, size);
  205. #endif
  206. switch(tag) {
  207. case MKTAG('L', 'I', 'S', 'T'):
  208. /* ignored, except when start of video packets */
  209. tag1 = get_le32(pb);
  210. #ifdef DEBUG
  211. print_tag("list", tag1, 0);
  212. #endif
  213. if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
  214. avi->movi_list = url_ftell(pb) - 4;
  215. if(size) avi->movi_end = avi->movi_list + size + (size & 1);
  216. else avi->movi_end = url_fsize(pb);
  217. #ifdef DEBUG
  218. printf("movi end=%"PRIx64"\n", avi->movi_end);
  219. #endif
  220. goto end_of_header;
  221. }
  222. break;
  223. case MKTAG('d', 'm', 'l', 'h'):
  224. avi->is_odml = 1;
  225. url_fskip(pb, size + (size & 1));
  226. break;
  227. case MKTAG('a', 'v', 'i', 'h'):
  228. /* avi header */
  229. /* using frame_period is bad idea */
  230. frame_period = get_le32(pb);
  231. bit_rate = get_le32(pb) * 8;
  232. get_le32(pb);
  233. avi->non_interleaved |= get_le32(pb) & AVIF_MUSTUSEINDEX;
  234. url_fskip(pb, 2 * 4);
  235. get_le32(pb);
  236. url_fskip(pb, size - 7 * 4);
  237. break;
  238. case MKTAG('s', 't', 'r', 'h'):
  239. /* stream header */
  240. tag1 = get_le32(pb);
  241. handler = get_le32(pb); /* codec tag */
  242. if(tag1 == MKTAG('p', 'a', 'd', 's')){
  243. url_fskip(pb, size - 8);
  244. break;
  245. }else{
  246. stream_index++;
  247. st = av_new_stream(s, stream_index);
  248. if (!st)
  249. goto fail;
  250. ast = av_mallocz(sizeof(AVIStream));
  251. if (!ast)
  252. goto fail;
  253. st->priv_data = ast;
  254. }
  255. #ifdef DEBUG
  256. print_tag("strh", tag1, -1);
  257. #endif
  258. if(tag1 == MKTAG('i', 'a', 'v', 's') || tag1 == MKTAG('i', 'v', 'a', 's')){
  259. int64_t dv_dur;
  260. /*
  261. * After some consideration -- I don't think we
  262. * have to support anything but DV in a type1 AVIs.
  263. */
  264. if (s->nb_streams != 1)
  265. goto fail;
  266. if (handler != MKTAG('d', 'v', 's', 'd') &&
  267. handler != MKTAG('d', 'v', 'h', 'd') &&
  268. handler != MKTAG('d', 'v', 's', 'l'))
  269. goto fail;
  270. ast = s->streams[0]->priv_data;
  271. av_freep(&s->streams[0]->codec->extradata);
  272. av_freep(&s->streams[0]);
  273. s->nb_streams = 0;
  274. if (ENABLE_DV_DEMUXER) {
  275. avi->dv_demux = dv_init_demux(s);
  276. if (!avi->dv_demux)
  277. goto fail;
  278. }
  279. s->streams[0]->priv_data = ast;
  280. url_fskip(pb, 3 * 4);
  281. ast->scale = get_le32(pb);
  282. ast->rate = get_le32(pb);
  283. url_fskip(pb, 4); /* start time */
  284. dv_dur = get_le32(pb);
  285. if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) {
  286. dv_dur *= AV_TIME_BASE;
  287. s->duration = av_rescale(dv_dur, ast->scale, ast->rate);
  288. }
  289. /*
  290. * else, leave duration alone; timing estimation in utils.c
  291. * will make a guess based on bit rate.
  292. */
  293. stream_index = s->nb_streams - 1;
  294. url_fskip(pb, size - 9*4);
  295. break;
  296. }
  297. assert(stream_index < s->nb_streams);
  298. st->codec->stream_codec_tag= handler;
  299. get_le32(pb); /* flags */
  300. get_le16(pb); /* priority */
  301. get_le16(pb); /* language */
  302. get_le32(pb); /* initial frame */
  303. ast->scale = get_le32(pb);
  304. ast->rate = get_le32(pb);
  305. if(ast->scale && ast->rate){
  306. }else if(frame_period){
  307. ast->rate = 1000000;
  308. ast->scale = frame_period;
  309. }else{
  310. ast->rate = 25;
  311. ast->scale = 1;
  312. }
  313. av_set_pts_info(st, 64, ast->scale, ast->rate);
  314. ast->cum_len=get_le32(pb); /* start */
  315. nb_frames = get_le32(pb);
  316. st->start_time = 0;
  317. st->duration = nb_frames;
  318. get_le32(pb); /* buffer size */
  319. get_le32(pb); /* quality */
  320. ast->sample_size = get_le32(pb); /* sample ssize */
  321. ast->cum_len *= FFMAX(1, ast->sample_size);
  322. // av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d\n", ast->rate, ast->scale, ast->start, ast->sample_size);
  323. switch(tag1) {
  324. case MKTAG('v', 'i', 'd', 's'):
  325. codec_type = CODEC_TYPE_VIDEO;
  326. ast->sample_size = 0;
  327. break;
  328. case MKTAG('a', 'u', 'd', 's'):
  329. codec_type = CODEC_TYPE_AUDIO;
  330. break;
  331. case MKTAG('t', 'x', 't', 's'):
  332. //FIXME
  333. codec_type = CODEC_TYPE_DATA; //CODEC_TYPE_SUB ? FIXME
  334. break;
  335. default:
  336. av_log(s, AV_LOG_ERROR, "unknown stream type %X\n", tag1);
  337. goto fail;
  338. }
  339. ast->frame_offset= ast->cum_len;
  340. url_fskip(pb, size - 12 * 4);
  341. break;
  342. case MKTAG('s', 't', 'r', 'f'):
  343. /* stream header */
  344. if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) {
  345. url_fskip(pb, size);
  346. } else {
  347. st = s->streams[stream_index];
  348. switch(codec_type) {
  349. case CODEC_TYPE_VIDEO:
  350. get_le32(pb); /* size */
  351. st->codec->width = get_le32(pb);
  352. st->codec->height = get_le32(pb);
  353. get_le16(pb); /* panes */
  354. st->codec->bits_per_sample= get_le16(pb); /* depth */
  355. tag1 = get_le32(pb);
  356. get_le32(pb); /* ImageSize */
  357. get_le32(pb); /* XPelsPerMeter */
  358. get_le32(pb); /* YPelsPerMeter */
  359. get_le32(pb); /* ClrUsed */
  360. get_le32(pb); /* ClrImportant */
  361. if(size > 10*4 && size<(1<<30)){
  362. st->codec->extradata_size= size - 10*4;
  363. st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  364. get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
  365. }
  366. if(st->codec->extradata_size & 1) //FIXME check if the encoder really did this correctly
  367. get_byte(pb);
  368. /* Extract palette from extradata if bpp <= 8 */
  369. /* This code assumes that extradata contains only palette */
  370. /* This is true for all paletted codecs implemented in ffmpeg */
  371. if (st->codec->extradata_size && (st->codec->bits_per_sample <= 8)) {
  372. st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
  373. #ifdef WORDS_BIGENDIAN
  374. for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
  375. st->codec->palctrl->palette[i] = bswap_32(((uint32_t*)st->codec->extradata)[i]);
  376. #else
  377. memcpy(st->codec->palctrl->palette, st->codec->extradata,
  378. FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
  379. #endif
  380. st->codec->palctrl->palette_changed = 1;
  381. }
  382. #ifdef DEBUG
  383. print_tag("video", tag1, 0);
  384. #endif
  385. st->codec->codec_type = CODEC_TYPE_VIDEO;
  386. st->codec->codec_tag = tag1;
  387. st->codec->codec_id = codec_get_id(codec_bmp_tags, tag1);
  388. 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
  389. // url_fskip(pb, size - 5 * 4);
  390. break;
  391. case CODEC_TYPE_AUDIO:
  392. get_wav_header(pb, st->codec, size);
  393. if(ast->sample_size && st->codec->block_align && ast->sample_size % st->codec->block_align)
  394. av_log(s, AV_LOG_DEBUG, "invalid sample size or block align detected\n");
  395. if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
  396. url_fskip(pb, 1);
  397. /* Force parsing as several audio frames can be in
  398. * one packet. */
  399. st->need_parsing = 1;
  400. /* ADTS header is in extradata, AAC without header must be stored as exact frames, parser not needed and it will fail */
  401. if (st->codec->codec_id == CODEC_ID_AAC && st->codec->extradata_size)
  402. st->need_parsing = 0;
  403. /* AVI files with Xan DPCM audio (wrongly) declare PCM
  404. * audio in the header but have Axan as stream_code_tag. */
  405. if (st->codec->stream_codec_tag == ff_get_fourcc("Axan")){
  406. st->codec->codec_id = CODEC_ID_XAN_DPCM;
  407. st->codec->codec_tag = 0;
  408. }
  409. break;
  410. default:
  411. st->codec->codec_type = CODEC_TYPE_DATA;
  412. st->codec->codec_id= CODEC_ID_NONE;
  413. st->codec->codec_tag= 0;
  414. url_fskip(pb, size);
  415. break;
  416. }
  417. }
  418. break;
  419. case MKTAG('i', 'n', 'd', 'x'):
  420. i= url_ftell(pb);
  421. if(!url_is_streamed(pb) && !(s->flags & AVFMT_FLAG_IGNIDX)){
  422. read_braindead_odml_indx(s, 0);
  423. }
  424. url_fseek(pb, i+size, SEEK_SET);
  425. break;
  426. case MKTAG('I', 'N', 'A', 'M'):
  427. avi_read_tag(pb, s->title, sizeof(s->title), size);
  428. break;
  429. case MKTAG('I', 'A', 'R', 'T'):
  430. avi_read_tag(pb, s->author, sizeof(s->author), size);
  431. break;
  432. case MKTAG('I', 'C', 'O', 'P'):
  433. avi_read_tag(pb, s->copyright, sizeof(s->copyright), size);
  434. break;
  435. case MKTAG('I', 'C', 'M', 'T'):
  436. avi_read_tag(pb, s->comment, sizeof(s->comment), size);
  437. break;
  438. case MKTAG('I', 'G', 'N', 'R'):
  439. avi_read_tag(pb, s->genre, sizeof(s->genre), size);
  440. break;
  441. case MKTAG('I', 'P', 'R', 'D'):
  442. avi_read_tag(pb, s->album, sizeof(s->album), size);
  443. break;
  444. case MKTAG('I', 'P', 'R', 'T'):
  445. avi_read_tag(pb, str_track, sizeof(str_track), size);
  446. sscanf(str_track, "%d", &s->track);
  447. break;
  448. default:
  449. /* skip tag */
  450. size += (size & 1);
  451. url_fskip(pb, size);
  452. break;
  453. }
  454. }
  455. end_of_header:
  456. /* check stream number */
  457. if (stream_index != s->nb_streams - 1) {
  458. fail:
  459. for(i=0;i<s->nb_streams;i++) {
  460. av_freep(&s->streams[i]->codec->extradata);
  461. av_freep(&s->streams[i]);
  462. }
  463. return -1;
  464. }
  465. if(!avi->index_loaded && !url_is_streamed(pb))
  466. avi_load_index(s);
  467. avi->index_loaded = 1;
  468. avi->non_interleaved |= guess_ni_flag(s);
  469. if(avi->non_interleaved)
  470. clean_index(s);
  471. return 0;
  472. }
  473. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  474. {
  475. AVIContext *avi = s->priv_data;
  476. ByteIOContext *pb = &s->pb;
  477. int n, d[8], size;
  478. offset_t i, sync;
  479. void* dstr;
  480. if (ENABLE_DV_DEMUXER && avi->dv_demux) {
  481. size = dv_get_packet(avi->dv_demux, pkt);
  482. if (size >= 0)
  483. return size;
  484. }
  485. if(avi->non_interleaved){
  486. int best_stream_index = 0;
  487. AVStream *best_st= NULL;
  488. AVIStream *best_ast;
  489. int64_t best_ts= INT64_MAX;
  490. int i;
  491. for(i=0; i<s->nb_streams; i++){
  492. AVStream *st = s->streams[i];
  493. AVIStream *ast = st->priv_data;
  494. int64_t ts= ast->frame_offset;
  495. if(ast->sample_size)
  496. ts /= ast->sample_size;
  497. ts= av_rescale(ts, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den);
  498. // av_log(NULL, AV_LOG_DEBUG, "%"PRId64" %d/%d %"PRId64"\n", ts, st->time_base.num, st->time_base.den, ast->frame_offset);
  499. if(ts < best_ts){
  500. best_ts= ts;
  501. best_st= st;
  502. best_stream_index= i;
  503. }
  504. }
  505. best_ast = best_st->priv_data;
  506. 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
  507. if(best_ast->remaining)
  508. i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
  509. else
  510. i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
  511. // av_log(NULL, AV_LOG_DEBUG, "%d\n", i);
  512. if(i>=0){
  513. int64_t pos= best_st->index_entries[i].pos;
  514. pos += best_ast->packet_size - best_ast->remaining;
  515. url_fseek(&s->pb, pos + 8, SEEK_SET);
  516. // av_log(NULL, AV_LOG_DEBUG, "pos=%"PRId64"\n", pos);
  517. assert(best_ast->remaining <= best_ast->packet_size);
  518. avi->stream_index= best_stream_index;
  519. if(!best_ast->remaining)
  520. best_ast->packet_size=
  521. best_ast->remaining= best_st->index_entries[i].size;
  522. }
  523. }
  524. resync:
  525. if(avi->stream_index >= 0){
  526. AVStream *st= s->streams[ avi->stream_index ];
  527. AVIStream *ast= st->priv_data;
  528. int size;
  529. if(ast->sample_size <= 1) // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
  530. size= INT_MAX;
  531. else if(ast->sample_size < 32)
  532. size= 64*ast->sample_size;
  533. else
  534. size= ast->sample_size;
  535. if(size > ast->remaining)
  536. size= ast->remaining;
  537. av_get_packet(pb, pkt, size);
  538. if (ENABLE_DV_DEMUXER && avi->dv_demux) {
  539. dstr = pkt->destruct;
  540. size = dv_produce_packet(avi->dv_demux, pkt,
  541. pkt->data, pkt->size);
  542. pkt->destruct = dstr;
  543. pkt->flags |= PKT_FLAG_KEY;
  544. } else {
  545. /* XXX: how to handle B frames in avi ? */
  546. pkt->dts = ast->frame_offset;
  547. // pkt->dts += ast->start;
  548. if(ast->sample_size)
  549. pkt->dts /= ast->sample_size;
  550. //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);
  551. pkt->stream_index = avi->stream_index;
  552. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  553. AVIndexEntry *e;
  554. int index;
  555. assert(st->index_entries);
  556. index= av_index_search_timestamp(st, pkt->dts, 0);
  557. e= &st->index_entries[index];
  558. if(index >= 0 && e->timestamp == ast->frame_offset){
  559. if (e->flags & AVINDEX_KEYFRAME)
  560. pkt->flags |= PKT_FLAG_KEY;
  561. }
  562. } else {
  563. pkt->flags |= PKT_FLAG_KEY;
  564. }
  565. if(ast->sample_size)
  566. ast->frame_offset += pkt->size;
  567. else
  568. ast->frame_offset++;
  569. }
  570. ast->remaining -= size;
  571. if(!ast->remaining){
  572. avi->stream_index= -1;
  573. ast->packet_size= 0;
  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 %"PRId64" %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. {
  643. uint64_t pos= url_ftell(pb) - 8;
  644. if(!st->index_entries || !st->nb_index_entries || st->index_entries[st->nb_index_entries - 1].pos < pos){
  645. av_add_index_entry(st, pos, ast->frame_offset / FFMAX(1, ast->sample_size), size, 0, AVINDEX_KEYFRAME);
  646. }
  647. }
  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. if (ENABLE_DV_DEMUXER && avi->dv_demux) {
  805. /* One and only one real stream for DV in AVI, and it has video */
  806. /* offsets. Calling with other stream indices should have failed */
  807. /* the av_index_search_timestamp call above. */
  808. assert(stream_index == 0);
  809. /* Feed the DV video stream version of the timestamp to the */
  810. /* DV demux so it can synth correct timestamps */
  811. dv_offset_reset(avi->dv_demux, timestamp);
  812. url_fseek(&s->pb, pos, SEEK_SET);
  813. avi->stream_index= -1;
  814. return 0;
  815. }
  816. for(i = 0; i < s->nb_streams; i++) {
  817. AVStream *st2 = s->streams[i];
  818. AVIStream *ast2 = st2->priv_data;
  819. ast2->packet_size=
  820. ast2->remaining= 0;
  821. if (st2->nb_index_entries <= 0)
  822. continue;
  823. // assert(st2->codec->block_align);
  824. assert(st2->time_base.den == ast2->rate);
  825. assert(st2->time_base.num == ast2->scale);
  826. index = av_index_search_timestamp(
  827. st2,
  828. av_rescale(timestamp, st2->time_base.den*(int64_t)st->time_base.num, st->time_base.den * (int64_t)st2->time_base.num),
  829. flags | AVSEEK_FLAG_BACKWARD);
  830. if(index<0)
  831. index=0;
  832. if(!avi->non_interleaved){
  833. while(index>0 && st2->index_entries[index].pos > pos)
  834. index--;
  835. while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos)
  836. index++;
  837. }
  838. // av_log(NULL, AV_LOG_DEBUG, "%"PRId64" %d %"PRId64"\n", timestamp, index, st2->index_entries[index].timestamp);
  839. /* extract the current frame number */
  840. ast2->frame_offset = st2->index_entries[index].timestamp;
  841. if(ast2->sample_size)
  842. ast2->frame_offset *=ast2->sample_size;
  843. }
  844. /* do the seek */
  845. url_fseek(&s->pb, pos, SEEK_SET);
  846. avi->stream_index= -1;
  847. return 0;
  848. }
  849. static int avi_read_close(AVFormatContext *s)
  850. {
  851. int i;
  852. AVIContext *avi = s->priv_data;
  853. for(i=0;i<s->nb_streams;i++) {
  854. AVStream *st = s->streams[i];
  855. AVIStream *ast = st->priv_data;
  856. av_free(ast);
  857. av_free(st->codec->palctrl);
  858. }
  859. if (avi->dv_demux)
  860. av_free(avi->dv_demux);
  861. return 0;
  862. }
  863. static int avi_probe(AVProbeData *p)
  864. {
  865. /* check file header */
  866. if (p->buf_size <= 32)
  867. return 0;
  868. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  869. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  870. p->buf[8] == 'A' && p->buf[9] == 'V' &&
  871. p->buf[10] == 'I' && (p->buf[11] == ' ' || p->buf[11] == 0x19))
  872. return AVPROBE_SCORE_MAX;
  873. else
  874. return 0;
  875. }
  876. AVInputFormat avi_demuxer = {
  877. "avi",
  878. "avi format",
  879. sizeof(AVIContext),
  880. avi_probe,
  881. avi_read_header,
  882. avi_read_packet,
  883. avi_read_close,
  884. avi_read_seek,
  885. };