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.

934 lines
29KB

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