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.

896 lines
28KB

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