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.

1022 lines
33KB

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