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.

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