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.

990 lines
32KB

  1. /*
  2. * AVI demuxer
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "avi.h"
  23. #include "dv.h"
  24. #include "riff.h"
  25. #undef NDEBUG
  26. #include <assert.h>
  27. //#define DEBUG
  28. //#define DEBUG_SEEK
  29. typedef struct AVIStream {
  30. int64_t frame_offset; /* current frame (video) or byte (audio) counter
  31. (used to compute the pts) */
  32. int remaining;
  33. int packet_size;
  34. int scale;
  35. int rate;
  36. int sample_size; /* size of one sample (or packet) (in the rate/scale sense) in bytes */
  37. int64_t cum_len; /* temporary storage (used during seek) */
  38. int prefix; ///< normally 'd'<<8 + 'c' or 'w'<<8 + 'b'
  39. int prefix_count;
  40. } AVIStream;
  41. typedef struct {
  42. int64_t riff_end;
  43. int64_t movi_end;
  44. offset_t movi_list;
  45. int index_loaded;
  46. int is_odml;
  47. int non_interleaved;
  48. int stream_index;
  49. DVDemuxContext* dv_demux;
  50. } AVIContext;
  51. static int avi_load_index(AVFormatContext *s);
  52. static int guess_ni_flag(AVFormatContext *s);
  53. #ifdef DEBUG
  54. static void print_tag(const char *str, unsigned int tag, int size)
  55. {
  56. printf("%s: tag=%c%c%c%c size=0x%x\n",
  57. str, tag & 0xff,
  58. (tag >> 8) & 0xff,
  59. (tag >> 16) & 0xff,
  60. (tag >> 24) & 0xff,
  61. size);
  62. }
  63. #endif
  64. static int get_riff(AVIContext *avi, ByteIOContext *pb)
  65. {
  66. uint32_t tag;
  67. /* check RIFF header */
  68. tag = get_le32(pb);
  69. if (tag != MKTAG('R', 'I', 'F', 'F'))
  70. return -1;
  71. avi->riff_end = get_le32(pb); /* RIFF chunk size */
  72. avi->riff_end += url_ftell(pb); /* RIFF chunk end */
  73. tag = get_le32(pb);
  74. if (tag != MKTAG('A', 'V', 'I', ' ') && tag != MKTAG('A', 'V', 'I', 'X'))
  75. return -1;
  76. return 0;
  77. }
  78. static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
  79. AVIContext *avi = s->priv_data;
  80. ByteIOContext *pb = &s->pb;
  81. int longs_pre_entry= get_le16(pb);
  82. int index_sub_type = get_byte(pb);
  83. int index_type = get_byte(pb);
  84. int entries_in_use = get_le32(pb);
  85. int chunk_id = get_le32(pb);
  86. int64_t base = get_le64(pb);
  87. int stream_id= 10*((chunk_id&0xFF) - '0') + (((chunk_id>>8)&0xFF) - '0');
  88. AVStream *st;
  89. AVIStream *ast;
  90. int i;
  91. int64_t last_pos= -1;
  92. int64_t filesize= url_fsize(&s->pb);
  93. #ifdef DEBUG_SEEK
  94. av_log(s, AV_LOG_ERROR, "longs_pre_entry:%d index_type:%d entries_in_use:%d chunk_id:%X base:%16"PRIX64"\n",
  95. longs_pre_entry,index_type, entries_in_use, chunk_id, base);
  96. #endif
  97. if(stream_id > s->nb_streams || stream_id < 0)
  98. return -1;
  99. st= s->streams[stream_id];
  100. ast = st->priv_data;
  101. if(index_sub_type)
  102. return -1;
  103. get_le32(pb);
  104. if(index_type && longs_pre_entry != 2)
  105. return -1;
  106. if(index_type>1)
  107. return -1;
  108. if(filesize > 0 && base >= filesize){
  109. av_log(s, AV_LOG_ERROR, "ODML index invalid\n");
  110. if(base>>32 == (base & 0xFFFFFFFF) && (base & 0xFFFFFFFF) < filesize && filesize <= 0xFFFFFFFF)
  111. base &= 0xFFFFFFFF;
  112. else
  113. return -1;
  114. }
  115. for(i=0; i<entries_in_use; i++){
  116. if(index_type){
  117. int64_t pos= get_le32(pb) + base - 8;
  118. int len = get_le32(pb);
  119. int key= len >= 0;
  120. len &= 0x7FFFFFFF;
  121. #ifdef DEBUG_SEEK
  122. av_log(s, AV_LOG_ERROR, "pos:%"PRId64", len:%X\n", pos, len);
  123. #endif
  124. if(last_pos == pos || pos == base - 8)
  125. avi->non_interleaved= 1;
  126. else
  127. av_add_index_entry(st, pos, ast->cum_len / FFMAX(1, ast->sample_size), len, 0, key ? AVINDEX_KEYFRAME : 0);
  128. if(ast->sample_size)
  129. ast->cum_len += len;
  130. else
  131. ast->cum_len ++;
  132. last_pos= pos;
  133. }else{
  134. int64_t offset, pos;
  135. int duration;
  136. offset = get_le64(pb);
  137. get_le32(pb); /* size */
  138. duration = get_le32(pb);
  139. pos = url_ftell(pb);
  140. url_fseek(pb, offset+8, SEEK_SET);
  141. read_braindead_odml_indx(s, frame_num);
  142. frame_num += duration;
  143. url_fseek(pb, pos, SEEK_SET);
  144. }
  145. }
  146. avi->index_loaded=1;
  147. return 0;
  148. }
  149. static void clean_index(AVFormatContext *s){
  150. int i;
  151. int64_t j;
  152. for(i=0; i<s->nb_streams; i++){
  153. AVStream *st = s->streams[i];
  154. AVIStream *ast = st->priv_data;
  155. int n= st->nb_index_entries;
  156. int max= ast->sample_size;
  157. int64_t pos, size, ts;
  158. if(n != 1 || ast->sample_size==0)
  159. continue;
  160. while(max < 1024) max+=max;
  161. pos= st->index_entries[0].pos;
  162. size= st->index_entries[0].size;
  163. ts= st->index_entries[0].timestamp;
  164. for(j=0; j<size; j+=max){
  165. av_add_index_entry(st, pos+j, ts + j/ast->sample_size, FFMIN(max, size-j), 0, AVINDEX_KEYFRAME);
  166. }
  167. }
  168. }
  169. static int avi_read_tag(ByteIOContext *pb, char *buf, int maxlen, unsigned int size)
  170. {
  171. offset_t i = url_ftell(pb);
  172. size += (size & 1);
  173. get_strz(pb, buf, maxlen);
  174. url_fseek(pb, i+size, SEEK_SET);
  175. return 0;
  176. }
  177. static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
  178. {
  179. AVIContext *avi = s->priv_data;
  180. ByteIOContext *pb = &s->pb;
  181. uint32_t tag, tag1, handler;
  182. int codec_type, stream_index, frame_period, bit_rate;
  183. unsigned int size, nb_frames;
  184. int i, n;
  185. AVStream *st;
  186. AVIStream *ast = NULL;
  187. 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. /*
  255. * After some consideration -- I don't think we
  256. * have to support anything but DV in a type1 AVIs.
  257. */
  258. if (s->nb_streams != 1)
  259. goto fail;
  260. if (handler != MKTAG('d', 'v', 's', 'd') &&
  261. handler != MKTAG('d', 'v', 'h', 'd') &&
  262. handler != MKTAG('d', 'v', 's', 'l'))
  263. goto fail;
  264. ast = s->streams[0]->priv_data;
  265. av_freep(&s->streams[0]->codec->extradata);
  266. av_freep(&s->streams[0]);
  267. s->nb_streams = 0;
  268. if (ENABLE_DV_DEMUXER) {
  269. avi->dv_demux = dv_init_demux(s);
  270. if (!avi->dv_demux)
  271. goto fail;
  272. }
  273. s->streams[0]->priv_data = ast;
  274. url_fskip(pb, 3 * 4);
  275. ast->scale = get_le32(pb);
  276. ast->rate = get_le32(pb);
  277. stream_index = s->nb_streams - 1;
  278. url_fskip(pb, size - 7*4);
  279. break;
  280. }
  281. if (stream_index >= s->nb_streams) {
  282. url_fskip(pb, size - 8);
  283. /* ignore padding stream */
  284. if (tag1 == MKTAG('p', 'a', 'd', 's'))
  285. stream_index--;
  286. break;
  287. }
  288. st = s->streams[stream_index];
  289. ast = st->priv_data;
  290. st->codec->stream_codec_tag= handler;
  291. get_le32(pb); /* flags */
  292. get_le16(pb); /* priority */
  293. get_le16(pb); /* language */
  294. get_le32(pb); /* initial frame */
  295. ast->scale = get_le32(pb);
  296. ast->rate = get_le32(pb);
  297. if(ast->scale && ast->rate){
  298. }else if(frame_period){
  299. ast->rate = 1000000;
  300. ast->scale = frame_period;
  301. }else{
  302. ast->rate = 25;
  303. ast->scale = 1;
  304. }
  305. av_set_pts_info(st, 64, ast->scale, ast->rate);
  306. ast->cum_len=get_le32(pb); /* start */
  307. nb_frames = get_le32(pb);
  308. st->start_time = 0;
  309. st->duration = nb_frames;
  310. get_le32(pb); /* buffer size */
  311. get_le32(pb); /* quality */
  312. ast->sample_size = get_le32(pb); /* sample ssize */
  313. ast->cum_len *= FFMAX(1, ast->sample_size);
  314. // av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d\n", ast->rate, ast->scale, ast->start, ast->sample_size);
  315. switch(tag1) {
  316. case MKTAG('v', 'i', 'd', 's'):
  317. codec_type = CODEC_TYPE_VIDEO;
  318. ast->sample_size = 0;
  319. break;
  320. case MKTAG('a', 'u', 'd', 's'):
  321. codec_type = CODEC_TYPE_AUDIO;
  322. break;
  323. case MKTAG('t', 'x', 't', 's'):
  324. //FIXME
  325. codec_type = CODEC_TYPE_DATA; //CODEC_TYPE_SUB ? FIXME
  326. break;
  327. case MKTAG('p', 'a', 'd', 's'):
  328. codec_type = CODEC_TYPE_UNKNOWN;
  329. stream_index--;
  330. break;
  331. default:
  332. av_log(s, AV_LOG_ERROR, "unknown stream type %X\n", tag1);
  333. goto fail;
  334. }
  335. ast->frame_offset= ast->cum_len;
  336. url_fskip(pb, size - 12 * 4);
  337. break;
  338. case MKTAG('s', 't', 'r', 'f'):
  339. /* stream header */
  340. if (stream_index >= s->nb_streams || avi->dv_demux) {
  341. url_fskip(pb, size);
  342. } else {
  343. st = s->streams[stream_index];
  344. switch(codec_type) {
  345. case CODEC_TYPE_VIDEO:
  346. get_le32(pb); /* size */
  347. st->codec->width = get_le32(pb);
  348. st->codec->height = get_le32(pb);
  349. get_le16(pb); /* panes */
  350. st->codec->bits_per_sample= get_le16(pb); /* depth */
  351. tag1 = get_le32(pb);
  352. get_le32(pb); /* ImageSize */
  353. get_le32(pb); /* XPelsPerMeter */
  354. get_le32(pb); /* YPelsPerMeter */
  355. get_le32(pb); /* ClrUsed */
  356. get_le32(pb); /* ClrImportant */
  357. if(size > 10*4 && size<(1<<30)){
  358. st->codec->extradata_size= size - 10*4;
  359. st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  360. get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
  361. }
  362. if(st->codec->extradata_size & 1) //FIXME check if the encoder really did this correctly
  363. get_byte(pb);
  364. /* Extract palette from extradata if bpp <= 8 */
  365. /* This code assumes that extradata contains only palette */
  366. /* This is true for all paletted codecs implemented in ffmpeg */
  367. if (st->codec->extradata_size && (st->codec->bits_per_sample <= 8)) {
  368. st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
  369. #ifdef WORDS_BIGENDIAN
  370. for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
  371. st->codec->palctrl->palette[i] = bswap_32(((uint32_t*)st->codec->extradata)[i]);
  372. #else
  373. memcpy(st->codec->palctrl->palette, st->codec->extradata,
  374. FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
  375. #endif
  376. st->codec->palctrl->palette_changed = 1;
  377. }
  378. #ifdef DEBUG
  379. print_tag("video", tag1, 0);
  380. #endif
  381. st->codec->codec_type = CODEC_TYPE_VIDEO;
  382. st->codec->codec_tag = tag1;
  383. st->codec->codec_id = codec_get_id(codec_bmp_tags, tag1);
  384. st->need_parsing = 2; //only parse headers dont do slower repacketization, this is needed to get the pict type which is needed for generating correct pts
  385. // url_fskip(pb, size - 5 * 4);
  386. break;
  387. case CODEC_TYPE_AUDIO:
  388. get_wav_header(pb, st->codec, size);
  389. if(ast->sample_size && st->codec->block_align && ast->sample_size % st->codec->block_align)
  390. av_log(s, AV_LOG_DEBUG, "invalid sample size or block align detected\n");
  391. if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
  392. url_fskip(pb, 1);
  393. /* Force parsing as several audio frames can be in
  394. * one packet. */
  395. st->need_parsing = 1;
  396. /* ADTS header is in extradata, AAC without header must be stored as exact frames, parser not needed and it will fail */
  397. if (st->codec->codec_id == CODEC_ID_AAC && st->codec->extradata_size)
  398. st->need_parsing = 0;
  399. /* AVI files with Xan DPCM audio (wrongly) declare PCM
  400. * audio in the header but have Axan as stream_code_tag. */
  401. if (st->codec->stream_codec_tag == ff_get_fourcc("Axan")){
  402. st->codec->codec_id = CODEC_ID_XAN_DPCM;
  403. st->codec->codec_tag = 0;
  404. }
  405. break;
  406. default:
  407. st->codec->codec_type = CODEC_TYPE_DATA;
  408. st->codec->codec_id= CODEC_ID_NONE;
  409. st->codec->codec_tag= 0;
  410. url_fskip(pb, size);
  411. break;
  412. }
  413. }
  414. break;
  415. case MKTAG('i', 'n', 'd', 'x'):
  416. i= url_ftell(pb);
  417. if(!url_is_streamed(pb) && !(s->flags & AVFMT_FLAG_IGNIDX)){
  418. read_braindead_odml_indx(s, 0);
  419. }
  420. url_fseek(pb, i+size, SEEK_SET);
  421. break;
  422. case MKTAG('I', 'N', 'A', 'M'):
  423. avi_read_tag(pb, s->title, sizeof(s->title), size);
  424. break;
  425. case MKTAG('I', 'A', 'R', 'T'):
  426. avi_read_tag(pb, s->author, sizeof(s->author), size);
  427. break;
  428. case MKTAG('I', 'C', 'O', 'P'):
  429. avi_read_tag(pb, s->copyright, sizeof(s->copyright), size);
  430. break;
  431. case MKTAG('I', 'C', 'M', 'T'):
  432. avi_read_tag(pb, s->comment, sizeof(s->comment), size);
  433. break;
  434. case MKTAG('I', 'G', 'N', 'R'):
  435. avi_read_tag(pb, s->genre, sizeof(s->genre), size);
  436. break;
  437. case MKTAG('I', 'P', 'R', 'D'):
  438. avi_read_tag(pb, s->album, sizeof(s->album), size);
  439. break;
  440. case MKTAG('I', 'P', 'R', 'T'):
  441. avi_read_tag(pb, str_track, sizeof(str_track), size);
  442. sscanf(str_track, "%d", &s->track);
  443. break;
  444. default:
  445. /* skip tag */
  446. size += (size & 1);
  447. url_fskip(pb, size);
  448. break;
  449. }
  450. }
  451. end_of_header:
  452. /* check stream number */
  453. if (stream_index != s->nb_streams - 1) {
  454. fail:
  455. for(i=0;i<s->nb_streams;i++) {
  456. av_freep(&s->streams[i]->codec->extradata);
  457. av_freep(&s->streams[i]);
  458. }
  459. return -1;
  460. }
  461. if(!avi->index_loaded && !url_is_streamed(pb))
  462. avi_load_index(s);
  463. avi->index_loaded = 1;
  464. avi->non_interleaved |= guess_ni_flag(s);
  465. if(avi->non_interleaved)
  466. clean_index(s);
  467. return 0;
  468. }
  469. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  470. {
  471. AVIContext *avi = s->priv_data;
  472. ByteIOContext *pb = &s->pb;
  473. int n, d[8], size;
  474. offset_t i, sync;
  475. void* dstr;
  476. if (ENABLE_DV_DEMUXER && avi->dv_demux) {
  477. size = dv_get_packet(avi->dv_demux, pkt);
  478. if (size >= 0)
  479. return size;
  480. }
  481. if(avi->non_interleaved){
  482. int best_stream_index = 0;
  483. AVStream *best_st= NULL;
  484. AVIStream *best_ast;
  485. int64_t best_ts= INT64_MAX;
  486. int i;
  487. for(i=0; i<s->nb_streams; i++){
  488. AVStream *st = s->streams[i];
  489. AVIStream *ast = st->priv_data;
  490. int64_t ts= ast->frame_offset;
  491. if(ast->sample_size)
  492. ts /= ast->sample_size;
  493. ts= av_rescale(ts, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den);
  494. // av_log(NULL, AV_LOG_DEBUG, "%"PRId64" %d/%d %"PRId64"\n", ts, st->time_base.num, st->time_base.den, ast->frame_offset);
  495. if(ts < best_ts){
  496. best_ts= ts;
  497. best_st= st;
  498. best_stream_index= i;
  499. }
  500. }
  501. best_ast = best_st->priv_data;
  502. 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
  503. if(best_ast->remaining)
  504. i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
  505. else
  506. i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
  507. // av_log(NULL, AV_LOG_DEBUG, "%d\n", i);
  508. if(i>=0){
  509. int64_t pos= best_st->index_entries[i].pos;
  510. pos += best_ast->packet_size - best_ast->remaining;
  511. url_fseek(&s->pb, pos + 8, SEEK_SET);
  512. // av_log(NULL, AV_LOG_DEBUG, "pos=%"PRId64"\n", pos);
  513. assert(best_ast->remaining <= best_ast->packet_size);
  514. avi->stream_index= best_stream_index;
  515. if(!best_ast->remaining)
  516. best_ast->packet_size=
  517. best_ast->remaining= best_st->index_entries[i].size;
  518. }
  519. }
  520. resync:
  521. if(avi->stream_index >= 0){
  522. AVStream *st= s->streams[ avi->stream_index ];
  523. AVIStream *ast= st->priv_data;
  524. int size;
  525. if(ast->sample_size <= 1) // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
  526. size= INT_MAX;
  527. else if(ast->sample_size < 32)
  528. size= 64*ast->sample_size;
  529. else
  530. size= ast->sample_size;
  531. if(size > ast->remaining)
  532. size= ast->remaining;
  533. av_get_packet(pb, pkt, size);
  534. if (ENABLE_DV_DEMUXER && avi->dv_demux) {
  535. dstr = pkt->destruct;
  536. size = dv_produce_packet(avi->dv_demux, pkt,
  537. pkt->data, pkt->size);
  538. pkt->destruct = dstr;
  539. pkt->flags |= PKT_FLAG_KEY;
  540. } else {
  541. /* XXX: how to handle B frames in avi ? */
  542. pkt->dts = ast->frame_offset;
  543. // pkt->dts += ast->start;
  544. if(ast->sample_size)
  545. pkt->dts /= ast->sample_size;
  546. //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);
  547. pkt->stream_index = avi->stream_index;
  548. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  549. if(st->index_entries){
  550. AVIndexEntry *e;
  551. int index;
  552. index= av_index_search_timestamp(st, pkt->dts, 0);
  553. e= &st->index_entries[index];
  554. if(index >= 0 && e->timestamp == ast->frame_offset){
  555. if (e->flags & AVINDEX_KEYFRAME)
  556. pkt->flags |= PKT_FLAG_KEY;
  557. }
  558. } else {
  559. /* if no index, better to say that all frames
  560. are key frames */
  561. pkt->flags |= PKT_FLAG_KEY;
  562. }
  563. } else {
  564. pkt->flags |= PKT_FLAG_KEY;
  565. }
  566. if(ast->sample_size)
  567. ast->frame_offset += pkt->size;
  568. else
  569. ast->frame_offset++;
  570. }
  571. ast->remaining -= size;
  572. if(!ast->remaining){
  573. avi->stream_index= -1;
  574. ast->packet_size= 0;
  575. if (size & 1) {
  576. get_byte(pb);
  577. size++;
  578. }
  579. }
  580. return size;
  581. }
  582. memset(d, -1, sizeof(int)*8);
  583. for(i=sync=url_ftell(pb); !url_feof(pb); i++) {
  584. int j;
  585. if (i >= avi->movi_end) {
  586. if (avi->is_odml) {
  587. url_fskip(pb, avi->riff_end - i);
  588. avi->riff_end = avi->movi_end = url_fsize(pb);
  589. } else
  590. break;
  591. }
  592. for(j=0; j<7; j++)
  593. d[j]= d[j+1];
  594. d[7]= get_byte(pb);
  595. size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
  596. if( d[2] >= '0' && d[2] <= '9'
  597. && d[3] >= '0' && d[3] <= '9'){
  598. n= (d[2] - '0') * 10 + (d[3] - '0');
  599. }else{
  600. n= 100; //invalid stream id
  601. }
  602. //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);
  603. if(i + size > avi->movi_end || d[0]<0)
  604. continue;
  605. //parse ix##
  606. if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
  607. //parse JUNK
  608. ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')){
  609. url_fskip(pb, size);
  610. //av_log(NULL, AV_LOG_DEBUG, "SKIP\n");
  611. goto resync;
  612. }
  613. if( d[0] >= '0' && d[0] <= '9'
  614. && d[1] >= '0' && d[1] <= '9'){
  615. n= (d[0] - '0') * 10 + (d[1] - '0');
  616. }else{
  617. n= 100; //invalid stream id
  618. }
  619. //parse ##dc/##wb
  620. if(n < s->nb_streams){
  621. AVStream *st;
  622. AVIStream *ast;
  623. st = s->streams[n];
  624. ast = st->priv_data;
  625. if( (st->discard >= AVDISCARD_DEFAULT && size==0)
  626. /*|| (st->discard >= AVDISCARD_NONKEY && !(pkt->flags & PKT_FLAG_KEY))*/ //FIXME needs a little reordering
  627. || st->discard >= AVDISCARD_ALL){
  628. if(ast->sample_size) ast->frame_offset += pkt->size;
  629. else ast->frame_offset++;
  630. url_fskip(pb, size);
  631. goto resync;
  632. }
  633. if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
  634. d[2]*256+d[3] == ast->prefix /*||
  635. (d[2] == 'd' && d[3] == 'c') ||
  636. (d[2] == 'w' && d[3] == 'b')*/) {
  637. //av_log(NULL, AV_LOG_DEBUG, "OK\n");
  638. if(d[2]*256+d[3] == ast->prefix)
  639. ast->prefix_count++;
  640. else{
  641. ast->prefix= d[2]*256+d[3];
  642. ast->prefix_count= 0;
  643. }
  644. avi->stream_index= n;
  645. ast->packet_size= size + 8;
  646. ast->remaining= size;
  647. goto resync;
  648. }
  649. }
  650. /* palette changed chunk */
  651. if ( d[0] >= '0' && d[0] <= '9'
  652. && d[1] >= '0' && d[1] <= '9'
  653. && ((d[2] == 'p' && d[3] == 'c'))
  654. && n < s->nb_streams && i + size <= avi->movi_end) {
  655. AVStream *st;
  656. int first, clr, flags, k, p;
  657. st = s->streams[n];
  658. first = get_byte(pb);
  659. clr = get_byte(pb);
  660. if(!clr) /* all 256 colors used */
  661. clr = 256;
  662. flags = get_le16(pb);
  663. p = 4;
  664. for (k = first; k < clr + first; k++) {
  665. int r, g, b;
  666. r = get_byte(pb);
  667. g = get_byte(pb);
  668. b = get_byte(pb);
  669. get_byte(pb);
  670. st->codec->palctrl->palette[k] = b + (g << 8) + (r << 16);
  671. }
  672. st->codec->palctrl->palette_changed = 1;
  673. goto resync;
  674. }
  675. }
  676. return -1;
  677. }
  678. /* XXX: we make the implicit supposition that the position are sorted
  679. for each stream */
  680. static int avi_read_idx1(AVFormatContext *s, int size)
  681. {
  682. AVIContext *avi = s->priv_data;
  683. ByteIOContext *pb = &s->pb;
  684. int nb_index_entries, i;
  685. AVStream *st;
  686. AVIStream *ast;
  687. unsigned int index, tag, flags, pos, len;
  688. unsigned last_pos= -1;
  689. nb_index_entries = size / 16;
  690. if (nb_index_entries <= 0)
  691. return -1;
  692. /* read the entries and sort them in each stream component */
  693. for(i = 0; i < nb_index_entries; i++) {
  694. tag = get_le32(pb);
  695. flags = get_le32(pb);
  696. pos = get_le32(pb);
  697. len = get_le32(pb);
  698. #if defined(DEBUG_SEEK)
  699. av_log(NULL, AV_LOG_DEBUG, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
  700. i, tag, flags, pos, len);
  701. #endif
  702. if(i==0 && pos > avi->movi_list)
  703. avi->movi_list= 0; //FIXME better check
  704. pos += avi->movi_list;
  705. index = ((tag & 0xff) - '0') * 10;
  706. index += ((tag >> 8) & 0xff) - '0';
  707. if (index >= s->nb_streams)
  708. continue;
  709. st = s->streams[index];
  710. ast = st->priv_data;
  711. #if defined(DEBUG_SEEK)
  712. av_log(NULL, AV_LOG_DEBUG, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
  713. #endif
  714. if(last_pos == pos)
  715. avi->non_interleaved= 1;
  716. else
  717. av_add_index_entry(st, pos, ast->cum_len / FFMAX(1, ast->sample_size), len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
  718. if(ast->sample_size)
  719. ast->cum_len += len;
  720. else
  721. ast->cum_len ++;
  722. last_pos= pos;
  723. }
  724. return 0;
  725. }
  726. static int guess_ni_flag(AVFormatContext *s){
  727. int i;
  728. int64_t last_start=0;
  729. int64_t first_end= INT64_MAX;
  730. for(i=0; i<s->nb_streams; i++){
  731. AVStream *st = s->streams[i];
  732. int n= st->nb_index_entries;
  733. if(n <= 0)
  734. continue;
  735. if(st->index_entries[0].pos > last_start)
  736. last_start= st->index_entries[0].pos;
  737. if(st->index_entries[n-1].pos < first_end)
  738. first_end= st->index_entries[n-1].pos;
  739. }
  740. return last_start > first_end;
  741. }
  742. static int avi_load_index(AVFormatContext *s)
  743. {
  744. AVIContext *avi = s->priv_data;
  745. ByteIOContext *pb = &s->pb;
  746. uint32_t tag, size;
  747. offset_t pos= url_ftell(pb);
  748. url_fseek(pb, avi->movi_end, SEEK_SET);
  749. #ifdef DEBUG_SEEK
  750. printf("movi_end=0x%"PRIx64"\n", avi->movi_end);
  751. #endif
  752. for(;;) {
  753. if (url_feof(pb))
  754. break;
  755. tag = get_le32(pb);
  756. size = get_le32(pb);
  757. #ifdef DEBUG_SEEK
  758. printf("tag=%c%c%c%c size=0x%x\n",
  759. tag & 0xff,
  760. (tag >> 8) & 0xff,
  761. (tag >> 16) & 0xff,
  762. (tag >> 24) & 0xff,
  763. size);
  764. #endif
  765. switch(tag) {
  766. case MKTAG('i', 'd', 'x', '1'):
  767. if (avi_read_idx1(s, size) < 0)
  768. goto skip;
  769. else
  770. goto the_end;
  771. break;
  772. default:
  773. skip:
  774. size += (size & 1);
  775. url_fskip(pb, size);
  776. break;
  777. }
  778. }
  779. the_end:
  780. url_fseek(pb, pos, SEEK_SET);
  781. return 0;
  782. }
  783. static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  784. {
  785. AVIContext *avi = s->priv_data;
  786. AVStream *st;
  787. int i, index;
  788. int64_t pos;
  789. if (!avi->index_loaded) {
  790. /* we only load the index on demand */
  791. avi_load_index(s);
  792. avi->index_loaded = 1;
  793. }
  794. assert(stream_index>= 0);
  795. st = s->streams[stream_index];
  796. index= av_index_search_timestamp(st, timestamp, flags);
  797. if(index<0)
  798. return -1;
  799. /* find the position */
  800. pos = st->index_entries[index].pos;
  801. timestamp = st->index_entries[index].timestamp;
  802. // av_log(NULL, AV_LOG_DEBUG, "XX %"PRId64" %d %"PRId64"\n", timestamp, index, st->index_entries[index].timestamp);
  803. for(i = 0; i < s->nb_streams; i++) {
  804. AVStream *st2 = s->streams[i];
  805. AVIStream *ast2 = st2->priv_data;
  806. ast2->packet_size=
  807. ast2->remaining= 0;
  808. if (st2->nb_index_entries <= 0)
  809. continue;
  810. // assert(st2->codec->block_align);
  811. assert(st2->time_base.den == ast2->rate);
  812. assert(st2->time_base.num == ast2->scale);
  813. index = av_index_search_timestamp(
  814. st2,
  815. av_rescale(timestamp, st2->time_base.den*(int64_t)st->time_base.num, st->time_base.den * (int64_t)st2->time_base.num),
  816. flags | AVSEEK_FLAG_BACKWARD);
  817. if(index<0)
  818. index=0;
  819. if(!avi->non_interleaved){
  820. while(index>0 && st2->index_entries[index].pos > pos)
  821. index--;
  822. while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos)
  823. index++;
  824. }
  825. // av_log(NULL, AV_LOG_DEBUG, "%"PRId64" %d %"PRId64"\n", timestamp, index, st2->index_entries[index].timestamp);
  826. /* extract the current frame number */
  827. ast2->frame_offset = st2->index_entries[index].timestamp;
  828. if(ast2->sample_size)
  829. ast2->frame_offset *=ast2->sample_size;
  830. }
  831. if (ENABLE_DV_DEMUXER && avi->dv_demux)
  832. dv_flush_audio_packets(avi->dv_demux);
  833. /* do the seek */
  834. url_fseek(&s->pb, pos, SEEK_SET);
  835. avi->stream_index= -1;
  836. return 0;
  837. }
  838. static int avi_read_close(AVFormatContext *s)
  839. {
  840. int i;
  841. AVIContext *avi = s->priv_data;
  842. for(i=0;i<s->nb_streams;i++) {
  843. AVStream *st = s->streams[i];
  844. AVIStream *ast = st->priv_data;
  845. av_free(ast);
  846. av_free(st->codec->palctrl);
  847. }
  848. if (avi->dv_demux)
  849. av_free(avi->dv_demux);
  850. return 0;
  851. }
  852. static int avi_probe(AVProbeData *p)
  853. {
  854. /* check file header */
  855. if (p->buf_size <= 32)
  856. return 0;
  857. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  858. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  859. p->buf[8] == 'A' && p->buf[9] == 'V' &&
  860. p->buf[10] == 'I' && p->buf[11] == ' ')
  861. return AVPROBE_SCORE_MAX;
  862. else
  863. return 0;
  864. }
  865. AVInputFormat avi_demuxer = {
  866. "avi",
  867. "avi format",
  868. sizeof(AVIContext),
  869. avi_probe,
  870. avi_read_header,
  871. avi_read_packet,
  872. avi_read_close,
  873. avi_read_seek,
  874. };