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.

975 lines
31KB

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