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.

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