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.

1017 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(size > 10*4 && size<(1<<30)){
  362. st->codec->extradata_size= size - 10*4;
  363. st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  364. get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
  365. }
  366. if(st->codec->extradata_size & 1) //FIXME check if the encoder really did this correctly
  367. get_byte(pb);
  368. /* Extract palette from extradata if bpp <= 8 */
  369. /* This code assumes that extradata contains only palette */
  370. /* This is true for all paletted codecs implemented in ffmpeg */
  371. if (st->codec->extradata_size && (st->codec->bits_per_sample <= 8)) {
  372. st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
  373. #ifdef WORDS_BIGENDIAN
  374. for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
  375. st->codec->palctrl->palette[i] = bswap_32(((uint32_t*)st->codec->extradata)[i]);
  376. #else
  377. memcpy(st->codec->palctrl->palette, st->codec->extradata,
  378. FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
  379. #endif
  380. st->codec->palctrl->palette_changed = 1;
  381. }
  382. #ifdef DEBUG
  383. print_tag("video", tag1, 0);
  384. #endif
  385. st->codec->codec_type = CODEC_TYPE_VIDEO;
  386. st->codec->codec_tag = tag1;
  387. st->codec->codec_id = codec_get_id(codec_bmp_tags, tag1);
  388. st->need_parsing = AVSTREAM_PARSE_HEADERS; // this is needed to get the pict type which is needed for generating correct pts
  389. // url_fskip(pb, size - 5 * 4);
  390. break;
  391. case CODEC_TYPE_AUDIO:
  392. get_wav_header(pb, st->codec, size);
  393. if(ast->sample_size && st->codec->block_align && ast->sample_size % st->codec->block_align)
  394. av_log(s, AV_LOG_DEBUG, "invalid sample size or block align detected\n");
  395. if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
  396. url_fskip(pb, 1);
  397. /* Force parsing as several audio frames can be in
  398. * one packet and timestamps refer to packet start*/
  399. st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
  400. /* ADTS header is in extradata, AAC without header must be stored as exact frames, parser not needed and it will fail */
  401. if (st->codec->codec_id == CODEC_ID_AAC && st->codec->extradata_size)
  402. st->need_parsing = AVSTREAM_PARSE_NONE;
  403. /* AVI files with Xan DPCM audio (wrongly) declare PCM
  404. * audio in the header but have Axan as stream_code_tag. */
  405. if (st->codec->stream_codec_tag == ff_get_fourcc("Axan")){
  406. st->codec->codec_id = CODEC_ID_XAN_DPCM;
  407. st->codec->codec_tag = 0;
  408. }
  409. break;
  410. default:
  411. st->codec->codec_type = CODEC_TYPE_DATA;
  412. st->codec->codec_id= CODEC_ID_NONE;
  413. st->codec->codec_tag= 0;
  414. url_fskip(pb, size);
  415. break;
  416. }
  417. }
  418. break;
  419. case MKTAG('i', 'n', 'd', 'x'):
  420. i= url_ftell(pb);
  421. if(!url_is_streamed(pb) && !(s->flags & AVFMT_FLAG_IGNIDX)){
  422. read_braindead_odml_indx(s, 0);
  423. }
  424. url_fseek(pb, i+size, SEEK_SET);
  425. break;
  426. case MKTAG('I', 'N', 'A', 'M'):
  427. avi_read_tag(pb, s->title, sizeof(s->title), size);
  428. break;
  429. case MKTAG('I', 'A', 'R', 'T'):
  430. avi_read_tag(pb, s->author, sizeof(s->author), size);
  431. break;
  432. case MKTAG('I', 'C', 'O', 'P'):
  433. avi_read_tag(pb, s->copyright, sizeof(s->copyright), size);
  434. break;
  435. case MKTAG('I', 'C', 'M', 'T'):
  436. avi_read_tag(pb, s->comment, sizeof(s->comment), size);
  437. break;
  438. case MKTAG('I', 'G', 'N', 'R'):
  439. avi_read_tag(pb, s->genre, sizeof(s->genre), size);
  440. break;
  441. case MKTAG('I', 'P', 'R', 'D'):
  442. avi_read_tag(pb, s->album, sizeof(s->album), size);
  443. break;
  444. case MKTAG('I', 'P', 'R', 'T'):
  445. avi_read_tag(pb, str_track, sizeof(str_track), size);
  446. sscanf(str_track, "%d", &s->track);
  447. break;
  448. default:
  449. if(size > 1000000){
  450. av_log(s, AV_LOG_ERROR, "well something went wrong during header parsing, "
  451. "ill ignore it and try to continue anyway\n");
  452. avi->movi_list = url_ftell(pb) - 4;
  453. avi->movi_end = url_fsize(pb);
  454. goto end_of_header;
  455. }
  456. /* skip tag */
  457. size += (size & 1);
  458. url_fskip(pb, size);
  459. break;
  460. }
  461. }
  462. end_of_header:
  463. /* check stream number */
  464. if (stream_index != s->nb_streams - 1) {
  465. fail:
  466. for(i=0;i<s->nb_streams;i++) {
  467. av_freep(&s->streams[i]->codec->extradata);
  468. av_freep(&s->streams[i]);
  469. }
  470. return -1;
  471. }
  472. if(!avi->index_loaded && !url_is_streamed(pb))
  473. avi_load_index(s);
  474. avi->index_loaded = 1;
  475. avi->non_interleaved |= guess_ni_flag(s);
  476. if(avi->non_interleaved)
  477. clean_index(s);
  478. return 0;
  479. }
  480. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  481. {
  482. AVIContext *avi = s->priv_data;
  483. ByteIOContext *pb = &s->pb;
  484. int n, d[8], size;
  485. offset_t i, sync;
  486. void* dstr;
  487. if (ENABLE_DV_DEMUXER && avi->dv_demux) {
  488. size = dv_get_packet(avi->dv_demux, pkt);
  489. if (size >= 0)
  490. return size;
  491. }
  492. if(avi->non_interleaved){
  493. int best_stream_index = 0;
  494. AVStream *best_st= NULL;
  495. AVIStream *best_ast;
  496. int64_t best_ts= INT64_MAX;
  497. int i;
  498. for(i=0; i<s->nb_streams; i++){
  499. AVStream *st = s->streams[i];
  500. AVIStream *ast = st->priv_data;
  501. int64_t ts= ast->frame_offset;
  502. if(ast->sample_size)
  503. ts /= ast->sample_size;
  504. ts= av_rescale(ts, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den);
  505. // av_log(NULL, AV_LOG_DEBUG, "%"PRId64" %d/%d %"PRId64"\n", ts, st->time_base.num, st->time_base.den, ast->frame_offset);
  506. if(ts < best_ts){
  507. best_ts= ts;
  508. best_st= st;
  509. best_stream_index= i;
  510. }
  511. }
  512. best_ast = best_st->priv_data;
  513. 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
  514. if(best_ast->remaining)
  515. i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
  516. else
  517. i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
  518. // av_log(NULL, AV_LOG_DEBUG, "%d\n", i);
  519. if(i>=0){
  520. int64_t pos= best_st->index_entries[i].pos;
  521. pos += best_ast->packet_size - best_ast->remaining;
  522. url_fseek(&s->pb, pos + 8, SEEK_SET);
  523. // av_log(NULL, AV_LOG_DEBUG, "pos=%"PRId64"\n", pos);
  524. assert(best_ast->remaining <= best_ast->packet_size);
  525. avi->stream_index= best_stream_index;
  526. if(!best_ast->remaining)
  527. best_ast->packet_size=
  528. best_ast->remaining= best_st->index_entries[i].size;
  529. }
  530. }
  531. resync:
  532. if(avi->stream_index >= 0){
  533. AVStream *st= s->streams[ avi->stream_index ];
  534. AVIStream *ast= st->priv_data;
  535. int size;
  536. if(ast->sample_size <= 1) // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
  537. size= INT_MAX;
  538. else if(ast->sample_size < 32)
  539. size= 64*ast->sample_size;
  540. else
  541. size= ast->sample_size;
  542. if(size > ast->remaining)
  543. size= ast->remaining;
  544. av_get_packet(pb, pkt, size);
  545. if (ENABLE_DV_DEMUXER && avi->dv_demux) {
  546. dstr = pkt->destruct;
  547. size = dv_produce_packet(avi->dv_demux, pkt,
  548. pkt->data, pkt->size);
  549. pkt->destruct = dstr;
  550. pkt->flags |= PKT_FLAG_KEY;
  551. } else {
  552. /* XXX: how to handle B frames in avi ? */
  553. pkt->dts = ast->frame_offset;
  554. // pkt->dts += ast->start;
  555. if(ast->sample_size)
  556. pkt->dts /= ast->sample_size;
  557. //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);
  558. pkt->stream_index = avi->stream_index;
  559. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  560. AVIndexEntry *e;
  561. int index;
  562. assert(st->index_entries);
  563. index= av_index_search_timestamp(st, pkt->dts, 0);
  564. e= &st->index_entries[index];
  565. if(index >= 0 && e->timestamp == ast->frame_offset){
  566. if (e->flags & AVINDEX_KEYFRAME)
  567. pkt->flags |= PKT_FLAG_KEY;
  568. }
  569. } else {
  570. pkt->flags |= PKT_FLAG_KEY;
  571. }
  572. if(ast->sample_size)
  573. ast->frame_offset += pkt->size;
  574. else
  575. ast->frame_offset++;
  576. }
  577. ast->remaining -= size;
  578. if(!ast->remaining){
  579. avi->stream_index= -1;
  580. ast->packet_size= 0;
  581. }
  582. return size;
  583. }
  584. memset(d, -1, sizeof(int)*8);
  585. for(i=sync=url_ftell(pb); !url_feof(pb); i++) {
  586. int j;
  587. if (i >= avi->movi_end) {
  588. if (avi->is_odml) {
  589. url_fskip(pb, avi->riff_end - i);
  590. avi->riff_end = avi->movi_end = url_fsize(pb);
  591. } else
  592. break;
  593. }
  594. for(j=0; j<7; j++)
  595. d[j]= d[j+1];
  596. d[7]= get_byte(pb);
  597. size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
  598. if( d[2] >= '0' && d[2] <= '9'
  599. && d[3] >= '0' && d[3] <= '9'){
  600. n= (d[2] - '0') * 10 + (d[3] - '0');
  601. }else{
  602. n= 100; //invalid stream id
  603. }
  604. //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);
  605. if(i + size > avi->movi_end || d[0]<0)
  606. continue;
  607. //parse ix##
  608. if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
  609. //parse JUNK
  610. ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')){
  611. url_fskip(pb, size);
  612. //av_log(NULL, AV_LOG_DEBUG, "SKIP\n");
  613. goto resync;
  614. }
  615. if( d[0] >= '0' && d[0] <= '9'
  616. && d[1] >= '0' && d[1] <= '9'){
  617. n= (d[0] - '0') * 10 + (d[1] - '0');
  618. }else{
  619. n= 100; //invalid stream id
  620. }
  621. //parse ##dc/##wb
  622. if(n < s->nb_streams){
  623. AVStream *st;
  624. AVIStream *ast;
  625. st = s->streams[n];
  626. ast = st->priv_data;
  627. if( (st->discard >= AVDISCARD_DEFAULT && size==0)
  628. /*|| (st->discard >= AVDISCARD_NONKEY && !(pkt->flags & PKT_FLAG_KEY))*/ //FIXME needs a little reordering
  629. || st->discard >= AVDISCARD_ALL){
  630. if(ast->sample_size) ast->frame_offset += pkt->size;
  631. else ast->frame_offset++;
  632. url_fskip(pb, size);
  633. goto resync;
  634. }
  635. if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
  636. d[2]*256+d[3] == ast->prefix /*||
  637. (d[2] == 'd' && d[3] == 'c') ||
  638. (d[2] == 'w' && d[3] == 'b')*/) {
  639. //av_log(NULL, AV_LOG_DEBUG, "OK\n");
  640. if(d[2]*256+d[3] == ast->prefix)
  641. ast->prefix_count++;
  642. else{
  643. ast->prefix= d[2]*256+d[3];
  644. ast->prefix_count= 0;
  645. }
  646. avi->stream_index= n;
  647. ast->packet_size= size + 8;
  648. ast->remaining= size;
  649. {
  650. uint64_t pos= url_ftell(pb) - 8;
  651. if(!st->index_entries || !st->nb_index_entries || st->index_entries[st->nb_index_entries - 1].pos < pos){
  652. av_add_index_entry(st, pos, ast->frame_offset / FFMAX(1, ast->sample_size), size, 0, AVINDEX_KEYFRAME);
  653. }
  654. }
  655. goto resync;
  656. }
  657. }
  658. /* palette changed chunk */
  659. if ( d[0] >= '0' && d[0] <= '9'
  660. && d[1] >= '0' && d[1] <= '9'
  661. && ((d[2] == 'p' && d[3] == 'c'))
  662. && n < s->nb_streams && i + size <= avi->movi_end) {
  663. AVStream *st;
  664. int first, clr, flags, k, p;
  665. st = s->streams[n];
  666. first = get_byte(pb);
  667. clr = get_byte(pb);
  668. if(!clr) /* all 256 colors used */
  669. clr = 256;
  670. flags = get_le16(pb);
  671. p = 4;
  672. for (k = first; k < clr + first; k++) {
  673. int r, g, b;
  674. r = get_byte(pb);
  675. g = get_byte(pb);
  676. b = get_byte(pb);
  677. get_byte(pb);
  678. st->codec->palctrl->palette[k] = b + (g << 8) + (r << 16);
  679. }
  680. st->codec->palctrl->palette_changed = 1;
  681. goto resync;
  682. }
  683. }
  684. return -1;
  685. }
  686. /* XXX: we make the implicit supposition that the position are sorted
  687. for each stream */
  688. static int avi_read_idx1(AVFormatContext *s, int size)
  689. {
  690. AVIContext *avi = s->priv_data;
  691. ByteIOContext *pb = &s->pb;
  692. int nb_index_entries, i;
  693. AVStream *st;
  694. AVIStream *ast;
  695. unsigned int index, tag, flags, pos, len;
  696. unsigned last_pos= -1;
  697. nb_index_entries = size / 16;
  698. if (nb_index_entries <= 0)
  699. return -1;
  700. /* read the entries and sort them in each stream component */
  701. for(i = 0; i < nb_index_entries; i++) {
  702. tag = get_le32(pb);
  703. flags = get_le32(pb);
  704. pos = get_le32(pb);
  705. len = get_le32(pb);
  706. #if defined(DEBUG_SEEK)
  707. av_log(NULL, AV_LOG_DEBUG, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
  708. i, tag, flags, pos, len);
  709. #endif
  710. if(i==0 && pos > avi->movi_list)
  711. avi->movi_list= 0; //FIXME better check
  712. pos += avi->movi_list;
  713. index = ((tag & 0xff) - '0') * 10;
  714. index += ((tag >> 8) & 0xff) - '0';
  715. if (index >= s->nb_streams)
  716. continue;
  717. st = s->streams[index];
  718. ast = st->priv_data;
  719. #if defined(DEBUG_SEEK)
  720. av_log(NULL, AV_LOG_DEBUG, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
  721. #endif
  722. if(last_pos == pos)
  723. avi->non_interleaved= 1;
  724. else
  725. av_add_index_entry(st, pos, ast->cum_len / FFMAX(1, ast->sample_size), len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
  726. if(ast->sample_size)
  727. ast->cum_len += len;
  728. else
  729. ast->cum_len ++;
  730. last_pos= pos;
  731. }
  732. return 0;
  733. }
  734. static int guess_ni_flag(AVFormatContext *s){
  735. int i;
  736. int64_t last_start=0;
  737. int64_t first_end= INT64_MAX;
  738. for(i=0; i<s->nb_streams; i++){
  739. AVStream *st = s->streams[i];
  740. int n= st->nb_index_entries;
  741. if(n <= 0)
  742. continue;
  743. if(st->index_entries[0].pos > last_start)
  744. last_start= st->index_entries[0].pos;
  745. if(st->index_entries[n-1].pos < first_end)
  746. first_end= st->index_entries[n-1].pos;
  747. }
  748. return last_start > first_end;
  749. }
  750. static int avi_load_index(AVFormatContext *s)
  751. {
  752. AVIContext *avi = s->priv_data;
  753. ByteIOContext *pb = &s->pb;
  754. uint32_t tag, size;
  755. offset_t pos= url_ftell(pb);
  756. url_fseek(pb, avi->movi_end, SEEK_SET);
  757. #ifdef DEBUG_SEEK
  758. printf("movi_end=0x%"PRIx64"\n", avi->movi_end);
  759. #endif
  760. for(;;) {
  761. if (url_feof(pb))
  762. break;
  763. tag = get_le32(pb);
  764. size = get_le32(pb);
  765. #ifdef DEBUG_SEEK
  766. printf("tag=%c%c%c%c size=0x%x\n",
  767. tag & 0xff,
  768. (tag >> 8) & 0xff,
  769. (tag >> 16) & 0xff,
  770. (tag >> 24) & 0xff,
  771. size);
  772. #endif
  773. switch(tag) {
  774. case MKTAG('i', 'd', 'x', '1'):
  775. if (avi_read_idx1(s, size) < 0)
  776. goto skip;
  777. else
  778. goto the_end;
  779. break;
  780. default:
  781. skip:
  782. size += (size & 1);
  783. url_fskip(pb, size);
  784. break;
  785. }
  786. }
  787. the_end:
  788. url_fseek(pb, pos, SEEK_SET);
  789. return 0;
  790. }
  791. static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  792. {
  793. AVIContext *avi = s->priv_data;
  794. AVStream *st;
  795. int i, index;
  796. int64_t pos;
  797. if (!avi->index_loaded) {
  798. /* we only load the index on demand */
  799. avi_load_index(s);
  800. avi->index_loaded = 1;
  801. }
  802. assert(stream_index>= 0);
  803. st = s->streams[stream_index];
  804. index= av_index_search_timestamp(st, timestamp, flags);
  805. if(index<0)
  806. return -1;
  807. /* find the position */
  808. pos = st->index_entries[index].pos;
  809. timestamp = st->index_entries[index].timestamp;
  810. // av_log(NULL, AV_LOG_DEBUG, "XX %"PRId64" %d %"PRId64"\n", timestamp, index, st->index_entries[index].timestamp);
  811. if (ENABLE_DV_DEMUXER && avi->dv_demux) {
  812. /* One and only one real stream for DV in AVI, and it has video */
  813. /* offsets. Calling with other stream indices should have failed */
  814. /* the av_index_search_timestamp call above. */
  815. assert(stream_index == 0);
  816. /* Feed the DV video stream version of the timestamp to the */
  817. /* DV demux so it can synth correct timestamps */
  818. dv_offset_reset(avi->dv_demux, timestamp);
  819. url_fseek(&s->pb, pos, SEEK_SET);
  820. avi->stream_index= -1;
  821. return 0;
  822. }
  823. for(i = 0; i < s->nb_streams; i++) {
  824. AVStream *st2 = s->streams[i];
  825. AVIStream *ast2 = st2->priv_data;
  826. ast2->packet_size=
  827. ast2->remaining= 0;
  828. if (st2->nb_index_entries <= 0)
  829. continue;
  830. // assert(st2->codec->block_align);
  831. assert(st2->time_base.den == ast2->rate);
  832. assert(st2->time_base.num == ast2->scale);
  833. index = av_index_search_timestamp(
  834. st2,
  835. av_rescale(timestamp, st2->time_base.den*(int64_t)st->time_base.num, st->time_base.den * (int64_t)st2->time_base.num),
  836. flags | AVSEEK_FLAG_BACKWARD);
  837. if(index<0)
  838. index=0;
  839. if(!avi->non_interleaved){
  840. while(index>0 && st2->index_entries[index].pos > pos)
  841. index--;
  842. while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos)
  843. index++;
  844. }
  845. // av_log(NULL, AV_LOG_DEBUG, "%"PRId64" %d %"PRId64"\n", timestamp, index, st2->index_entries[index].timestamp);
  846. /* extract the current frame number */
  847. ast2->frame_offset = st2->index_entries[index].timestamp;
  848. if(ast2->sample_size)
  849. ast2->frame_offset *=ast2->sample_size;
  850. }
  851. /* do the seek */
  852. url_fseek(&s->pb, pos, SEEK_SET);
  853. avi->stream_index= -1;
  854. return 0;
  855. }
  856. static int avi_read_close(AVFormatContext *s)
  857. {
  858. int i;
  859. AVIContext *avi = s->priv_data;
  860. for(i=0;i<s->nb_streams;i++) {
  861. AVStream *st = s->streams[i];
  862. AVIStream *ast = st->priv_data;
  863. av_free(ast);
  864. av_free(st->codec->palctrl);
  865. }
  866. if (avi->dv_demux)
  867. av_free(avi->dv_demux);
  868. return 0;
  869. }
  870. static int avi_probe(AVProbeData *p)
  871. {
  872. /* check file header */
  873. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  874. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  875. p->buf[8] == 'A' && p->buf[9] == 'V' &&
  876. p->buf[10] == 'I' && (p->buf[11] == ' ' || p->buf[11] == 0x19))
  877. return AVPROBE_SCORE_MAX;
  878. else
  879. return 0;
  880. }
  881. AVInputFormat avi_demuxer = {
  882. "avi",
  883. "avi format",
  884. sizeof(AVIContext),
  885. avi_probe,
  886. avi_read_header,
  887. avi_read_packet,
  888. avi_read_close,
  889. avi_read_seek,
  890. };