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.

983 lines
31KB

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