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.

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