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.

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