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.

961 lines
30KB

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