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.

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