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.

1335 lines
44KB

  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. //#define DEBUG
  22. //#define DEBUG_SEEK
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/bswap.h"
  25. #include "libavcodec/bytestream.h"
  26. #include "avformat.h"
  27. #include "avi.h"
  28. #include "dv.h"
  29. #include "riff.h"
  30. #undef NDEBUG
  31. #include <assert.h>
  32. typedef struct AVIStream {
  33. int64_t frame_offset; /* current frame (video) or byte (audio) counter
  34. (used to compute the pts) */
  35. int remaining;
  36. int packet_size;
  37. int scale;
  38. int rate;
  39. int sample_size; /* size of one sample (or packet) (in the rate/scale sense) in bytes */
  40. int64_t cum_len; /* temporary storage (used during seek) */
  41. int prefix; ///< normally 'd'<<8 + 'c' or 'w'<<8 + 'b'
  42. int prefix_count;
  43. uint32_t pal[256];
  44. int has_pal;
  45. int dshow_block_align; ///< block align variable used to emulate bugs in the MS dshow demuxer
  46. AVFormatContext *sub_ctx;
  47. AVPacket sub_pkt;
  48. uint8_t *sub_buffer;
  49. } AVIStream;
  50. typedef struct {
  51. int64_t riff_end;
  52. int64_t movi_end;
  53. int64_t fsize;
  54. int64_t movi_list;
  55. int64_t last_pkt_pos;
  56. int index_loaded;
  57. int is_odml;
  58. int non_interleaved;
  59. int stream_index;
  60. DVDemuxContext* dv_demux;
  61. int odml_depth;
  62. #define MAX_ODML_DEPTH 1000
  63. } AVIContext;
  64. static const char avi_headers[][8] = {
  65. { 'R', 'I', 'F', 'F', 'A', 'V', 'I', ' ' },
  66. { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 'X' },
  67. { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 0x19},
  68. { 'O', 'N', '2', ' ', 'O', 'N', '2', 'f' },
  69. { 'R', 'I', 'F', 'F', 'A', 'M', 'V', ' ' },
  70. { 0 }
  71. };
  72. static int avi_load_index(AVFormatContext *s);
  73. static int guess_ni_flag(AVFormatContext *s);
  74. #ifdef DEBUG
  75. static void print_tag(const char *str, unsigned int tag, int size)
  76. {
  77. dprintf(NULL, "%s: tag=%c%c%c%c size=0x%x\n",
  78. str, tag & 0xff,
  79. (tag >> 8) & 0xff,
  80. (tag >> 16) & 0xff,
  81. (tag >> 24) & 0xff,
  82. size);
  83. }
  84. #endif
  85. static inline int get_duration(AVIStream *ast, int len){
  86. if(ast->sample_size){
  87. return len;
  88. }else if (ast->dshow_block_align){
  89. return (len + ast->dshow_block_align - 1)/ast->dshow_block_align;
  90. }else
  91. return 1;
  92. }
  93. static int get_riff(AVFormatContext *s, ByteIOContext *pb)
  94. {
  95. AVIContext *avi = s->priv_data;
  96. char header[8];
  97. int i;
  98. /* check RIFF header */
  99. get_buffer(pb, header, 4);
  100. avi->riff_end = get_le32(pb); /* RIFF chunk size */
  101. avi->riff_end += url_ftell(pb); /* RIFF chunk end */
  102. get_buffer(pb, header+4, 4);
  103. for(i=0; avi_headers[i][0]; i++)
  104. if(!memcmp(header, avi_headers[i], 8))
  105. break;
  106. if(!avi_headers[i][0])
  107. return -1;
  108. if(header[7] == 0x19)
  109. av_log(s, AV_LOG_INFO, "This file has been generated by a totally broken muxer.\n");
  110. return 0;
  111. }
  112. static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
  113. AVIContext *avi = s->priv_data;
  114. ByteIOContext *pb = s->pb;
  115. int longs_pre_entry= get_le16(pb);
  116. int index_sub_type = get_byte(pb);
  117. int index_type = get_byte(pb);
  118. int entries_in_use = get_le32(pb);
  119. int chunk_id = get_le32(pb);
  120. int64_t base = get_le64(pb);
  121. int stream_id= 10*((chunk_id&0xFF) - '0') + (((chunk_id>>8)&0xFF) - '0');
  122. AVStream *st;
  123. AVIStream *ast;
  124. int i;
  125. int64_t last_pos= -1;
  126. int64_t filesize= url_fsize(s->pb);
  127. #ifdef DEBUG_SEEK
  128. av_log(s, AV_LOG_ERROR, "longs_pre_entry:%d index_type:%d entries_in_use:%d chunk_id:%X base:%16"PRIX64"\n",
  129. longs_pre_entry,index_type, entries_in_use, chunk_id, base);
  130. #endif
  131. if(stream_id >= s->nb_streams || stream_id < 0)
  132. return -1;
  133. st= s->streams[stream_id];
  134. ast = st->priv_data;
  135. if(index_sub_type)
  136. return -1;
  137. get_le32(pb);
  138. if(index_type && longs_pre_entry != 2)
  139. return -1;
  140. if(index_type>1)
  141. return -1;
  142. if(filesize > 0 && base >= filesize){
  143. av_log(s, AV_LOG_ERROR, "ODML index invalid\n");
  144. if(base>>32 == (base & 0xFFFFFFFF) && (base & 0xFFFFFFFF) < filesize && filesize <= 0xFFFFFFFF)
  145. base &= 0xFFFFFFFF;
  146. else
  147. return -1;
  148. }
  149. for(i=0; i<entries_in_use; i++){
  150. if(index_type){
  151. int64_t pos= get_le32(pb) + base - 8;
  152. int len = get_le32(pb);
  153. int key= len >= 0;
  154. len &= 0x7FFFFFFF;
  155. #ifdef DEBUG_SEEK
  156. av_log(s, AV_LOG_ERROR, "pos:%"PRId64", len:%X\n", pos, len);
  157. #endif
  158. if(url_feof(pb))
  159. return -1;
  160. if(last_pos == pos || pos == base - 8)
  161. avi->non_interleaved= 1;
  162. if(last_pos != pos && (len || !ast->sample_size))
  163. av_add_index_entry(st, pos, ast->cum_len, len, 0, key ? AVINDEX_KEYFRAME : 0);
  164. ast->cum_len += get_duration(ast, len);
  165. last_pos= pos;
  166. }else{
  167. int64_t offset, pos;
  168. int duration;
  169. offset = get_le64(pb);
  170. get_le32(pb); /* size */
  171. duration = get_le32(pb);
  172. if(url_feof(pb))
  173. return -1;
  174. pos = url_ftell(pb);
  175. if(avi->odml_depth > MAX_ODML_DEPTH){
  176. av_log(s, AV_LOG_ERROR, "Too deeply nested ODML indexes\n");
  177. return -1;
  178. }
  179. url_fseek(pb, offset+8, SEEK_SET);
  180. avi->odml_depth++;
  181. read_braindead_odml_indx(s, frame_num);
  182. avi->odml_depth--;
  183. frame_num += duration;
  184. url_fseek(pb, pos, SEEK_SET);
  185. }
  186. }
  187. avi->index_loaded=1;
  188. return 0;
  189. }
  190. static void clean_index(AVFormatContext *s){
  191. int i;
  192. int64_t j;
  193. for(i=0; i<s->nb_streams; i++){
  194. AVStream *st = s->streams[i];
  195. AVIStream *ast = st->priv_data;
  196. int n= st->nb_index_entries;
  197. int max= ast->sample_size;
  198. int64_t pos, size, ts;
  199. if(n != 1 || ast->sample_size==0)
  200. continue;
  201. while(max < 1024) max+=max;
  202. pos= st->index_entries[0].pos;
  203. size= st->index_entries[0].size;
  204. ts= st->index_entries[0].timestamp;
  205. for(j=0; j<size; j+=max){
  206. av_add_index_entry(st, pos+j, ts+j, FFMIN(max, size-j), 0, AVINDEX_KEYFRAME);
  207. }
  208. }
  209. }
  210. static int avi_read_tag(AVFormatContext *s, AVStream *st, uint32_t tag, uint32_t size)
  211. {
  212. ByteIOContext *pb = s->pb;
  213. char key[5] = {0}, *value;
  214. size += (size & 1);
  215. if (size == UINT_MAX)
  216. return -1;
  217. value = av_malloc(size+1);
  218. if (!value)
  219. return -1;
  220. get_buffer(pb, value, size);
  221. value[size]=0;
  222. AV_WL32(key, tag);
  223. if(st)
  224. return av_metadata_set2(&st->metadata, key, value,
  225. AV_METADATA_DONT_STRDUP_VAL);
  226. else
  227. return av_metadata_set2(&s->metadata, key, value,
  228. AV_METADATA_DONT_STRDUP_VAL);
  229. }
  230. static void avi_read_info(AVFormatContext *s, uint64_t end)
  231. {
  232. while (url_ftell(s->pb) < end) {
  233. uint32_t tag = get_le32(s->pb);
  234. uint32_t size = get_le32(s->pb);
  235. avi_read_tag(s, NULL, tag, size);
  236. }
  237. }
  238. static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
  239. {
  240. AVIContext *avi = s->priv_data;
  241. ByteIOContext *pb = s->pb;
  242. unsigned int tag, tag1, handler;
  243. int codec_type, stream_index, frame_period, bit_rate;
  244. unsigned int size;
  245. int i;
  246. AVStream *st;
  247. AVIStream *ast = NULL;
  248. int avih_width=0, avih_height=0;
  249. int amv_file_format=0;
  250. uint64_t list_end = 0;
  251. avi->stream_index= -1;
  252. if (get_riff(s, pb) < 0)
  253. return -1;
  254. avi->fsize = url_fsize(pb);
  255. if(avi->fsize<=0)
  256. avi->fsize= avi->riff_end == 8 ? INT64_MAX : avi->riff_end;
  257. /* first list tag */
  258. stream_index = -1;
  259. codec_type = -1;
  260. frame_period = 0;
  261. for(;;) {
  262. if (url_feof(pb))
  263. goto fail;
  264. tag = get_le32(pb);
  265. size = get_le32(pb);
  266. #ifdef DEBUG
  267. print_tag("tag", tag, size);
  268. #endif
  269. switch(tag) {
  270. case MKTAG('L', 'I', 'S', 'T'):
  271. list_end = url_ftell(pb) + size;
  272. /* Ignored, except at start of video packets. */
  273. tag1 = get_le32(pb);
  274. #ifdef DEBUG
  275. print_tag("list", tag1, 0);
  276. #endif
  277. if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
  278. avi->movi_list = url_ftell(pb) - 4;
  279. if(size) avi->movi_end = avi->movi_list + size + (size & 1);
  280. else avi->movi_end = url_fsize(pb);
  281. dprintf(NULL, "movi end=%"PRIx64"\n", avi->movi_end);
  282. goto end_of_header;
  283. }
  284. else if (tag1 == MKTAG('I', 'N', 'F', 'O'))
  285. avi_read_info(s, list_end);
  286. break;
  287. case MKTAG('d', 'm', 'l', 'h'):
  288. avi->is_odml = 1;
  289. url_fskip(pb, size + (size & 1));
  290. break;
  291. case MKTAG('a', 'm', 'v', 'h'):
  292. amv_file_format=1;
  293. case MKTAG('a', 'v', 'i', 'h'):
  294. /* AVI header */
  295. /* using frame_period is bad idea */
  296. frame_period = get_le32(pb);
  297. bit_rate = get_le32(pb) * 8;
  298. get_le32(pb);
  299. avi->non_interleaved |= get_le32(pb) & AVIF_MUSTUSEINDEX;
  300. url_fskip(pb, 2 * 4);
  301. get_le32(pb);
  302. get_le32(pb);
  303. avih_width=get_le32(pb);
  304. avih_height=get_le32(pb);
  305. url_fskip(pb, size - 10 * 4);
  306. break;
  307. case MKTAG('s', 't', 'r', 'h'):
  308. /* stream header */
  309. tag1 = get_le32(pb);
  310. handler = get_le32(pb); /* codec tag */
  311. if(tag1 == MKTAG('p', 'a', 'd', 's')){
  312. url_fskip(pb, size - 8);
  313. break;
  314. }else{
  315. stream_index++;
  316. st = av_new_stream(s, stream_index);
  317. if (!st)
  318. goto fail;
  319. ast = av_mallocz(sizeof(AVIStream));
  320. if (!ast)
  321. goto fail;
  322. st->priv_data = ast;
  323. }
  324. if(amv_file_format)
  325. tag1 = stream_index ? MKTAG('a','u','d','s') : MKTAG('v','i','d','s');
  326. #ifdef DEBUG
  327. print_tag("strh", tag1, -1);
  328. #endif
  329. if(tag1 == MKTAG('i', 'a', 'v', 's') || tag1 == MKTAG('i', 'v', 'a', 's')){
  330. int64_t dv_dur;
  331. /*
  332. * After some consideration -- I don't think we
  333. * have to support anything but DV in type1 AVIs.
  334. */
  335. if (s->nb_streams != 1)
  336. goto fail;
  337. if (handler != MKTAG('d', 'v', 's', 'd') &&
  338. handler != MKTAG('d', 'v', 'h', 'd') &&
  339. handler != MKTAG('d', 'v', 's', 'l'))
  340. goto fail;
  341. ast = s->streams[0]->priv_data;
  342. av_freep(&s->streams[0]->codec->extradata);
  343. av_freep(&s->streams[0]->codec);
  344. av_freep(&s->streams[0]);
  345. s->nb_streams = 0;
  346. if (CONFIG_DV_DEMUXER) {
  347. avi->dv_demux = dv_init_demux(s);
  348. if (!avi->dv_demux)
  349. goto fail;
  350. }
  351. s->streams[0]->priv_data = ast;
  352. url_fskip(pb, 3 * 4);
  353. ast->scale = get_le32(pb);
  354. ast->rate = get_le32(pb);
  355. url_fskip(pb, 4); /* start time */
  356. dv_dur = get_le32(pb);
  357. if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) {
  358. dv_dur *= AV_TIME_BASE;
  359. s->duration = av_rescale(dv_dur, ast->scale, ast->rate);
  360. }
  361. /*
  362. * else, leave duration alone; timing estimation in utils.c
  363. * will make a guess based on bitrate.
  364. */
  365. stream_index = s->nb_streams - 1;
  366. url_fskip(pb, size - 9*4);
  367. break;
  368. }
  369. assert(stream_index < s->nb_streams);
  370. st->codec->stream_codec_tag= handler;
  371. get_le32(pb); /* flags */
  372. get_le16(pb); /* priority */
  373. get_le16(pb); /* language */
  374. get_le32(pb); /* initial frame */
  375. ast->scale = get_le32(pb);
  376. ast->rate = get_le32(pb);
  377. if(!(ast->scale && ast->rate)){
  378. av_log(s, AV_LOG_WARNING, "scale/rate is %u/%u which is invalid. (This file has been generated by broken software.)\n", ast->scale, ast->rate);
  379. if(frame_period){
  380. ast->rate = 1000000;
  381. ast->scale = frame_period;
  382. }else{
  383. ast->rate = 25;
  384. ast->scale = 1;
  385. }
  386. }
  387. av_set_pts_info(st, 64, ast->scale, ast->rate);
  388. ast->cum_len=get_le32(pb); /* start */
  389. st->nb_frames = get_le32(pb);
  390. st->start_time = 0;
  391. get_le32(pb); /* buffer size */
  392. get_le32(pb); /* quality */
  393. ast->sample_size = get_le32(pb); /* sample ssize */
  394. ast->cum_len *= FFMAX(1, ast->sample_size);
  395. // av_log(s, AV_LOG_DEBUG, "%d %d %d %d\n", ast->rate, ast->scale, ast->start, ast->sample_size);
  396. switch(tag1) {
  397. case MKTAG('v', 'i', 'd', 's'):
  398. codec_type = AVMEDIA_TYPE_VIDEO;
  399. ast->sample_size = 0;
  400. break;
  401. case MKTAG('a', 'u', 'd', 's'):
  402. codec_type = AVMEDIA_TYPE_AUDIO;
  403. break;
  404. case MKTAG('t', 'x', 't', 's'):
  405. codec_type = AVMEDIA_TYPE_SUBTITLE;
  406. break;
  407. case MKTAG('d', 'a', 't', 's'):
  408. codec_type = AVMEDIA_TYPE_DATA;
  409. break;
  410. default:
  411. av_log(s, AV_LOG_ERROR, "unknown stream type %X\n", tag1);
  412. goto fail;
  413. }
  414. if(ast->sample_size == 0)
  415. st->duration = st->nb_frames;
  416. ast->frame_offset= ast->cum_len;
  417. url_fskip(pb, size - 12 * 4);
  418. break;
  419. case MKTAG('s', 't', 'r', 'f'):
  420. /* stream header */
  421. if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) {
  422. url_fskip(pb, size);
  423. } else {
  424. uint64_t cur_pos = url_ftell(pb);
  425. if (cur_pos < list_end)
  426. size = FFMIN(size, list_end - cur_pos);
  427. st = s->streams[stream_index];
  428. switch(codec_type) {
  429. case AVMEDIA_TYPE_VIDEO:
  430. if(amv_file_format){
  431. st->codec->width=avih_width;
  432. st->codec->height=avih_height;
  433. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  434. st->codec->codec_id = CODEC_ID_AMV;
  435. url_fskip(pb, size);
  436. break;
  437. }
  438. get_le32(pb); /* size */
  439. st->codec->width = get_le32(pb);
  440. st->codec->height = (int32_t)get_le32(pb);
  441. get_le16(pb); /* panes */
  442. st->codec->bits_per_coded_sample= get_le16(pb); /* depth */
  443. tag1 = get_le32(pb);
  444. get_le32(pb); /* ImageSize */
  445. get_le32(pb); /* XPelsPerMeter */
  446. get_le32(pb); /* YPelsPerMeter */
  447. get_le32(pb); /* ClrUsed */
  448. get_le32(pb); /* ClrImportant */
  449. if (tag1 == MKTAG('D', 'X', 'S', 'B') || tag1 == MKTAG('D','X','S','A')) {
  450. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  451. st->codec->codec_tag = tag1;
  452. st->codec->codec_id = CODEC_ID_XSUB;
  453. break;
  454. }
  455. if(size > 10*4 && size<(1<<30)){
  456. st->codec->extradata_size= size - 10*4;
  457. st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  458. if (!st->codec->extradata) {
  459. st->codec->extradata_size= 0;
  460. return AVERROR(ENOMEM);
  461. }
  462. get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
  463. }
  464. if(st->codec->extradata_size & 1) //FIXME check if the encoder really did this correctly
  465. get_byte(pb);
  466. /* Extract palette from extradata if bpp <= 8. */
  467. /* This code assumes that extradata contains only palette. */
  468. /* This is true for all paletted codecs implemented in FFmpeg. */
  469. if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
  470. st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
  471. #if HAVE_BIGENDIAN
  472. for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
  473. st->codec->palctrl->palette[i] = av_bswap32(((uint32_t*)st->codec->extradata)[i]);
  474. #else
  475. memcpy(st->codec->palctrl->palette, st->codec->extradata,
  476. FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
  477. #endif
  478. st->codec->palctrl->palette_changed = 1;
  479. }
  480. #ifdef DEBUG
  481. print_tag("video", tag1, 0);
  482. #endif
  483. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  484. st->codec->codec_tag = tag1;
  485. st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
  486. st->need_parsing = AVSTREAM_PARSE_HEADERS; // This is needed to get the pict type which is necessary for generating correct pts.
  487. // Support "Resolution 1:1" for Avid AVI Codec
  488. if(tag1 == MKTAG('A', 'V', 'R', 'n') &&
  489. st->codec->extradata_size >= 31 &&
  490. !memcmp(&st->codec->extradata[28], "1:1", 3))
  491. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  492. if(st->codec->codec_tag==0 && st->codec->height > 0 && st->codec->extradata_size < 1U<<30){
  493. st->codec->extradata_size+= 9;
  494. st->codec->extradata= av_realloc(st->codec->extradata, st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  495. if(st->codec->extradata)
  496. memcpy(st->codec->extradata + st->codec->extradata_size - 9, "BottomUp", 9);
  497. }
  498. st->codec->height= FFABS(st->codec->height);
  499. // url_fskip(pb, size - 5 * 4);
  500. break;
  501. case AVMEDIA_TYPE_AUDIO:
  502. ff_get_wav_header(pb, st->codec, size);
  503. ast->dshow_block_align= st->codec->block_align;
  504. if(ast->sample_size && st->codec->block_align && ast->sample_size != st->codec->block_align){
  505. av_log(s, AV_LOG_WARNING, "sample size (%d) != block align (%d)\n", ast->sample_size, st->codec->block_align);
  506. ast->sample_size= st->codec->block_align;
  507. }
  508. if (size&1) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
  509. url_fskip(pb, 1);
  510. /* Force parsing as several audio frames can be in
  511. * one packet and timestamps refer to packet start. */
  512. st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
  513. /* ADTS header is in extradata, AAC without header must be
  514. * stored as exact frames. Parser not needed and it will
  515. * fail. */
  516. if (st->codec->codec_id == CODEC_ID_AAC && st->codec->extradata_size)
  517. st->need_parsing = AVSTREAM_PARSE_NONE;
  518. /* AVI files with Xan DPCM audio (wrongly) declare PCM
  519. * audio in the header but have Axan as stream_code_tag. */
  520. if (st->codec->stream_codec_tag == AV_RL32("Axan")){
  521. st->codec->codec_id = CODEC_ID_XAN_DPCM;
  522. st->codec->codec_tag = 0;
  523. }
  524. if (amv_file_format){
  525. st->codec->codec_id = CODEC_ID_ADPCM_IMA_AMV;
  526. ast->dshow_block_align = 0;
  527. }
  528. break;
  529. case AVMEDIA_TYPE_SUBTITLE:
  530. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  531. st->codec->codec_id = CODEC_ID_PROBE;
  532. break;
  533. default:
  534. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  535. st->codec->codec_id= CODEC_ID_NONE;
  536. st->codec->codec_tag= 0;
  537. url_fskip(pb, size);
  538. break;
  539. }
  540. }
  541. break;
  542. case MKTAG('i', 'n', 'd', 'x'):
  543. i= url_ftell(pb);
  544. if(!url_is_streamed(pb) && !(s->flags & AVFMT_FLAG_IGNIDX)){
  545. read_braindead_odml_indx(s, 0);
  546. }
  547. url_fseek(pb, i+size, SEEK_SET);
  548. break;
  549. case MKTAG('v', 'p', 'r', 'p'):
  550. if(stream_index < (unsigned)s->nb_streams && size > 9*4){
  551. AVRational active, active_aspect;
  552. st = s->streams[stream_index];
  553. get_le32(pb);
  554. get_le32(pb);
  555. get_le32(pb);
  556. get_le32(pb);
  557. get_le32(pb);
  558. active_aspect.den= get_le16(pb);
  559. active_aspect.num= get_le16(pb);
  560. active.num = get_le32(pb);
  561. active.den = get_le32(pb);
  562. get_le32(pb); //nbFieldsPerFrame
  563. if(active_aspect.num && active_aspect.den && active.num && active.den){
  564. st->sample_aspect_ratio= av_div_q(active_aspect, active);
  565. //av_log(s, AV_LOG_ERROR, "vprp %d/%d %d/%d\n", active_aspect.num, active_aspect.den, active.num, active.den);
  566. }
  567. size -= 9*4;
  568. }
  569. url_fseek(pb, size, SEEK_CUR);
  570. break;
  571. case MKTAG('s', 't', 'r', 'n'):
  572. if(s->nb_streams){
  573. avi_read_tag(s, s->streams[s->nb_streams-1], tag, size);
  574. break;
  575. }
  576. default:
  577. if(size > 1000000){
  578. av_log(s, AV_LOG_ERROR, "Something went wrong during header parsing, "
  579. "I will ignore it and try to continue anyway.\n");
  580. avi->movi_list = url_ftell(pb) - 4;
  581. avi->movi_end = url_fsize(pb);
  582. goto end_of_header;
  583. }
  584. /* skip tag */
  585. size += (size & 1);
  586. url_fskip(pb, size);
  587. break;
  588. }
  589. }
  590. end_of_header:
  591. /* check stream number */
  592. if (stream_index != s->nb_streams - 1) {
  593. fail:
  594. return -1;
  595. }
  596. if(!avi->index_loaded && !url_is_streamed(pb))
  597. avi_load_index(s);
  598. avi->index_loaded = 1;
  599. avi->non_interleaved |= guess_ni_flag(s);
  600. for(i=0; i<s->nb_streams; i++){
  601. AVStream *st = s->streams[i];
  602. if(st->nb_index_entries)
  603. break;
  604. }
  605. if(i==s->nb_streams && avi->non_interleaved) {
  606. av_log(s, AV_LOG_WARNING, "non-interleaved AVI without index, switching to interleaved\n");
  607. avi->non_interleaved=0;
  608. }
  609. if(avi->non_interleaved) {
  610. av_log(s, AV_LOG_INFO, "non-interleaved AVI\n");
  611. clean_index(s);
  612. }
  613. return 0;
  614. }
  615. static int read_gab2_sub(AVStream *st, AVPacket *pkt) {
  616. if (!strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data+5) == 2) {
  617. uint8_t desc[256], *d = desc;
  618. uint8_t *end, *ptr = pkt->data+7;
  619. unsigned int size, desc_len = bytestream_get_le32(&ptr);
  620. int score = AVPROBE_SCORE_MAX / 2;
  621. AVIStream *ast = st->priv_data;
  622. AVInputFormat *sub_demuxer;
  623. AVRational time_base;
  624. ByteIOContext *pb;
  625. AVProbeData pd;
  626. if (desc_len > FFMAX(pkt->size-17, 0))
  627. return 0;
  628. end = ptr + desc_len;
  629. while (ptr < end-1) {
  630. uint8_t tmp;
  631. uint32_t ch;
  632. GET_UTF16(ch, ptr < end-1 ? bytestream_get_le16(&ptr) : 0, break;);
  633. PUT_UTF8(ch, tmp, if(d-desc < sizeof(desc)-1) *d++ = tmp;);
  634. }
  635. *d = 0;
  636. if (*desc)
  637. av_metadata_set2(&st->metadata, "title", desc, 0);
  638. ptr = end + 2;
  639. size = bytestream_get_le32(&ptr);
  640. size = FFMIN(size, pkt->size+pkt->data-ptr);
  641. pd = (AVProbeData) { .buf = ptr, .buf_size = size };
  642. if (!(sub_demuxer = av_probe_input_format2(&pd, 1, &score)))
  643. return 0;
  644. pb = av_alloc_put_byte(ptr, size, 0, NULL, NULL, NULL, NULL);
  645. if (!av_open_input_stream(&ast->sub_ctx, pb, "", sub_demuxer, NULL)) {
  646. av_read_packet(ast->sub_ctx, &ast->sub_pkt);
  647. *st->codec = *ast->sub_ctx->streams[0]->codec;
  648. ast->sub_ctx->streams[0]->codec->extradata = NULL;
  649. time_base = ast->sub_ctx->streams[0]->time_base;
  650. av_set_pts_info(st, 64, time_base.num, time_base.den);
  651. }
  652. ast->sub_buffer = pkt->data;
  653. memset(pkt, 0, sizeof(*pkt));
  654. return 1;
  655. }
  656. return 0;
  657. }
  658. static AVStream *get_subtitle_pkt(AVFormatContext *s, AVStream *next_st,
  659. AVPacket *pkt)
  660. {
  661. AVIStream *ast, *next_ast = next_st->priv_data;
  662. int64_t ts, next_ts, ts_min = INT64_MAX;
  663. AVStream *st, *sub_st = NULL;
  664. int i;
  665. next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
  666. AV_TIME_BASE_Q);
  667. for (i=0; i<s->nb_streams; i++) {
  668. st = s->streams[i];
  669. ast = st->priv_data;
  670. if (st->discard < AVDISCARD_ALL && ast->sub_pkt.data) {
  671. ts = av_rescale_q(ast->sub_pkt.dts, st->time_base, AV_TIME_BASE_Q);
  672. if (ts <= next_ts && ts < ts_min) {
  673. ts_min = ts;
  674. sub_st = st;
  675. }
  676. }
  677. }
  678. if (sub_st) {
  679. ast = sub_st->priv_data;
  680. *pkt = ast->sub_pkt;
  681. pkt->stream_index = sub_st->index;
  682. if (av_read_packet(ast->sub_ctx, &ast->sub_pkt) < 0)
  683. ast->sub_pkt.data = NULL;
  684. }
  685. return sub_st;
  686. }
  687. static int get_stream_idx(int *d){
  688. if( d[0] >= '0' && d[0] <= '9'
  689. && d[1] >= '0' && d[1] <= '9'){
  690. return (d[0] - '0') * 10 + (d[1] - '0');
  691. }else{
  692. return 100; //invalid stream ID
  693. }
  694. }
  695. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  696. {
  697. AVIContext *avi = s->priv_data;
  698. ByteIOContext *pb = s->pb;
  699. int n, d[8];
  700. unsigned int size;
  701. int64_t i, sync;
  702. void* dstr;
  703. if (CONFIG_DV_DEMUXER && avi->dv_demux) {
  704. int size = dv_get_packet(avi->dv_demux, pkt);
  705. if (size >= 0)
  706. return size;
  707. }
  708. if(avi->non_interleaved){
  709. int best_stream_index = 0;
  710. AVStream *best_st= NULL;
  711. AVIStream *best_ast;
  712. int64_t best_ts= INT64_MAX;
  713. int i;
  714. for(i=0; i<s->nb_streams; i++){
  715. AVStream *st = s->streams[i];
  716. AVIStream *ast = st->priv_data;
  717. int64_t ts= ast->frame_offset;
  718. int64_t last_ts;
  719. if(!st->nb_index_entries)
  720. continue;
  721. last_ts = st->index_entries[st->nb_index_entries - 1].timestamp;
  722. if(!ast->remaining && ts > last_ts)
  723. continue;
  724. ts = av_rescale_q(ts, st->time_base, (AVRational){FFMAX(1, ast->sample_size), AV_TIME_BASE});
  725. // av_log(s, AV_LOG_DEBUG, "%"PRId64" %d/%d %"PRId64"\n", ts, st->time_base.num, st->time_base.den, ast->frame_offset);
  726. if(ts < best_ts){
  727. best_ts= ts;
  728. best_st= st;
  729. best_stream_index= i;
  730. }
  731. }
  732. if(!best_st)
  733. return -1;
  734. best_ast = best_st->priv_data;
  735. best_ts = av_rescale_q(best_ts, (AVRational){FFMAX(1, best_ast->sample_size), AV_TIME_BASE}, best_st->time_base);
  736. if(best_ast->remaining)
  737. i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
  738. else{
  739. i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
  740. if(i>=0)
  741. best_ast->frame_offset= best_st->index_entries[i].timestamp;
  742. }
  743. // av_log(s, AV_LOG_DEBUG, "%d\n", i);
  744. if(i>=0){
  745. int64_t pos= best_st->index_entries[i].pos;
  746. pos += best_ast->packet_size - best_ast->remaining;
  747. url_fseek(s->pb, pos + 8, SEEK_SET);
  748. // av_log(s, AV_LOG_DEBUG, "pos=%"PRId64"\n", pos);
  749. assert(best_ast->remaining <= best_ast->packet_size);
  750. avi->stream_index= best_stream_index;
  751. if(!best_ast->remaining)
  752. best_ast->packet_size=
  753. best_ast->remaining= best_st->index_entries[i].size;
  754. }
  755. }
  756. resync:
  757. if(avi->stream_index >= 0){
  758. AVStream *st= s->streams[ avi->stream_index ];
  759. AVIStream *ast= st->priv_data;
  760. int size, err;
  761. if(get_subtitle_pkt(s, st, pkt))
  762. return 0;
  763. if(ast->sample_size <= 1) // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
  764. size= INT_MAX;
  765. else if(ast->sample_size < 32)
  766. // arbitrary multiplier to avoid tiny packets for raw PCM data
  767. size= 1024*ast->sample_size;
  768. else
  769. size= ast->sample_size;
  770. if(size > ast->remaining)
  771. size= ast->remaining;
  772. avi->last_pkt_pos= url_ftell(pb);
  773. err= av_get_packet(pb, pkt, size);
  774. if(err<0)
  775. return err;
  776. if(ast->has_pal && pkt->data && pkt->size<(unsigned)INT_MAX/2){
  777. void *ptr= av_realloc(pkt->data, pkt->size + 4*256 + FF_INPUT_BUFFER_PADDING_SIZE);
  778. if(ptr){
  779. ast->has_pal=0;
  780. pkt->size += 4*256;
  781. pkt->data= ptr;
  782. memcpy(pkt->data + pkt->size - 4*256, ast->pal, 4*256);
  783. }else
  784. av_log(s, AV_LOG_ERROR, "Failed to append palette\n");
  785. }
  786. if (CONFIG_DV_DEMUXER && avi->dv_demux) {
  787. dstr = pkt->destruct;
  788. size = dv_produce_packet(avi->dv_demux, pkt,
  789. pkt->data, pkt->size);
  790. pkt->destruct = dstr;
  791. pkt->flags |= AV_PKT_FLAG_KEY;
  792. if (size < 0)
  793. av_free_packet(pkt);
  794. } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
  795. && !st->codec->codec_tag && read_gab2_sub(st, pkt)) {
  796. ast->frame_offset++;
  797. avi->stream_index = -1;
  798. ast->remaining = 0;
  799. goto resync;
  800. } else {
  801. /* XXX: How to handle B-frames in AVI? */
  802. pkt->dts = ast->frame_offset;
  803. // pkt->dts += ast->start;
  804. if(ast->sample_size)
  805. pkt->dts /= ast->sample_size;
  806. //av_log(s, 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);
  807. pkt->stream_index = avi->stream_index;
  808. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  809. AVIndexEntry *e;
  810. int index;
  811. assert(st->index_entries);
  812. index= av_index_search_timestamp(st, ast->frame_offset, 0);
  813. e= &st->index_entries[index];
  814. if(index >= 0 && e->timestamp == ast->frame_offset){
  815. if (e->flags & AVINDEX_KEYFRAME)
  816. pkt->flags |= AV_PKT_FLAG_KEY;
  817. }
  818. } else {
  819. pkt->flags |= AV_PKT_FLAG_KEY;
  820. }
  821. ast->frame_offset += get_duration(ast, pkt->size);
  822. }
  823. ast->remaining -= size;
  824. if(!ast->remaining){
  825. avi->stream_index= -1;
  826. ast->packet_size= 0;
  827. }
  828. return size;
  829. }
  830. memset(d, -1, sizeof(int)*8);
  831. for(i=sync=url_ftell(pb); !url_feof(pb); i++) {
  832. int j;
  833. for(j=0; j<7; j++)
  834. d[j]= d[j+1];
  835. d[7]= get_byte(pb);
  836. size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
  837. n= get_stream_idx(d+2);
  838. //av_log(s, 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);
  839. if(i + (uint64_t)size > avi->fsize || d[0]<0)
  840. continue;
  841. //parse ix##
  842. if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
  843. //parse JUNK
  844. ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')
  845. ||(d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1')){
  846. url_fskip(pb, size);
  847. //av_log(s, AV_LOG_DEBUG, "SKIP\n");
  848. goto resync;
  849. }
  850. //parse stray LIST
  851. if(d[0] == 'L' && d[1] == 'I' && d[2] == 'S' && d[3] == 'T'){
  852. url_fskip(pb, 4);
  853. goto resync;
  854. }
  855. n= get_stream_idx(d);
  856. if(!((i-avi->last_pkt_pos)&1) && get_stream_idx(d+1) < s->nb_streams)
  857. continue;
  858. //detect ##ix chunk and skip
  859. if(d[2] == 'i' && d[3] == 'x' && n < s->nb_streams){
  860. url_fskip(pb, size);
  861. goto resync;
  862. }
  863. //parse ##dc/##wb
  864. if(n < s->nb_streams){
  865. AVStream *st;
  866. AVIStream *ast;
  867. st = s->streams[n];
  868. ast = st->priv_data;
  869. if(s->nb_streams>=2){
  870. AVStream *st1 = s->streams[1];
  871. AVIStream *ast1= st1->priv_data;
  872. //workaround for broken small-file-bug402.avi
  873. if( d[2] == 'w' && d[3] == 'b'
  874. && n==0
  875. && st ->codec->codec_type == AVMEDIA_TYPE_VIDEO
  876. && st1->codec->codec_type == AVMEDIA_TYPE_AUDIO
  877. && ast->prefix == 'd'*256+'c'
  878. && (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count)
  879. ){
  880. n=1;
  881. st = st1;
  882. ast = ast1;
  883. av_log(s, AV_LOG_WARNING, "Invalid stream + prefix combination, assuming audio.\n");
  884. }
  885. }
  886. if( (st->discard >= AVDISCARD_DEFAULT && size==0)
  887. /*|| (st->discard >= AVDISCARD_NONKEY && !(pkt->flags & AV_PKT_FLAG_KEY))*/ //FIXME needs a little reordering
  888. || st->discard >= AVDISCARD_ALL){
  889. ast->frame_offset += get_duration(ast, size);
  890. url_fskip(pb, size);
  891. goto resync;
  892. }
  893. if (d[2] == 'p' && d[3] == 'c' && size<=4*256+4) {
  894. int k = get_byte(pb);
  895. int last = (k + get_byte(pb) - 1) & 0xFF;
  896. get_le16(pb); //flags
  897. for (; k <= last; k++)
  898. ast->pal[k] = get_be32(pb)>>8;// b + (g << 8) + (r << 16);
  899. ast->has_pal= 1;
  900. goto resync;
  901. } else if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
  902. d[2]*256+d[3] == ast->prefix /*||
  903. (d[2] == 'd' && d[3] == 'c') ||
  904. (d[2] == 'w' && d[3] == 'b')*/) {
  905. //av_log(s, AV_LOG_DEBUG, "OK\n");
  906. if(d[2]*256+d[3] == ast->prefix)
  907. ast->prefix_count++;
  908. else{
  909. ast->prefix= d[2]*256+d[3];
  910. ast->prefix_count= 0;
  911. }
  912. avi->stream_index= n;
  913. ast->packet_size= size + 8;
  914. ast->remaining= size;
  915. if(size || !ast->sample_size){
  916. uint64_t pos= url_ftell(pb) - 8;
  917. if(!st->index_entries || !st->nb_index_entries || st->index_entries[st->nb_index_entries - 1].pos < pos){
  918. av_add_index_entry(st, pos, ast->frame_offset, size, 0, AVINDEX_KEYFRAME);
  919. }
  920. }
  921. goto resync;
  922. }
  923. }
  924. }
  925. return AVERROR_EOF;
  926. }
  927. /* XXX: We make the implicit supposition that the positions are sorted
  928. for each stream. */
  929. static int avi_read_idx1(AVFormatContext *s, int size)
  930. {
  931. AVIContext *avi = s->priv_data;
  932. ByteIOContext *pb = s->pb;
  933. int nb_index_entries, i;
  934. AVStream *st;
  935. AVIStream *ast;
  936. unsigned int index, tag, flags, pos, len;
  937. unsigned last_pos= -1;
  938. nb_index_entries = size / 16;
  939. if (nb_index_entries <= 0)
  940. return -1;
  941. /* Read the entries and sort them in each stream component. */
  942. for(i = 0; i < nb_index_entries; i++) {
  943. tag = get_le32(pb);
  944. flags = get_le32(pb);
  945. pos = get_le32(pb);
  946. len = get_le32(pb);
  947. #if defined(DEBUG_SEEK)
  948. av_log(s, AV_LOG_DEBUG, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
  949. i, tag, flags, pos, len);
  950. #endif
  951. if(i==0 && pos > avi->movi_list)
  952. avi->movi_list= 0; //FIXME better check
  953. pos += avi->movi_list;
  954. index = ((tag & 0xff) - '0') * 10;
  955. index += ((tag >> 8) & 0xff) - '0';
  956. if (index >= s->nb_streams)
  957. continue;
  958. st = s->streams[index];
  959. ast = st->priv_data;
  960. #if defined(DEBUG_SEEK)
  961. av_log(s, AV_LOG_DEBUG, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
  962. #endif
  963. if(url_feof(pb))
  964. return -1;
  965. if(last_pos == pos)
  966. avi->non_interleaved= 1;
  967. else if(len || !ast->sample_size)
  968. av_add_index_entry(st, pos, ast->cum_len, len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
  969. ast->cum_len += get_duration(ast, len);
  970. last_pos= pos;
  971. }
  972. return 0;
  973. }
  974. static int guess_ni_flag(AVFormatContext *s){
  975. int i;
  976. int64_t last_start=0;
  977. int64_t first_end= INT64_MAX;
  978. int64_t oldpos= url_ftell(s->pb);
  979. for(i=0; i<s->nb_streams; i++){
  980. AVStream *st = s->streams[i];
  981. int n= st->nb_index_entries;
  982. unsigned int size;
  983. if(n <= 0)
  984. continue;
  985. if(n >= 2){
  986. int64_t pos= st->index_entries[0].pos;
  987. url_fseek(s->pb, pos + 4, SEEK_SET);
  988. size= get_le32(s->pb);
  989. if(pos + size > st->index_entries[1].pos)
  990. last_start= INT64_MAX;
  991. }
  992. if(st->index_entries[0].pos > last_start)
  993. last_start= st->index_entries[0].pos;
  994. if(st->index_entries[n-1].pos < first_end)
  995. first_end= st->index_entries[n-1].pos;
  996. }
  997. url_fseek(s->pb, oldpos, SEEK_SET);
  998. return last_start > first_end;
  999. }
  1000. static int avi_load_index(AVFormatContext *s)
  1001. {
  1002. AVIContext *avi = s->priv_data;
  1003. ByteIOContext *pb = s->pb;
  1004. uint32_t tag, size;
  1005. int64_t pos= url_ftell(pb);
  1006. int ret = -1;
  1007. if (url_fseek(pb, avi->movi_end, SEEK_SET) < 0)
  1008. goto the_end; // maybe truncated file
  1009. #ifdef DEBUG_SEEK
  1010. printf("movi_end=0x%"PRIx64"\n", avi->movi_end);
  1011. #endif
  1012. for(;;) {
  1013. if (url_feof(pb))
  1014. break;
  1015. tag = get_le32(pb);
  1016. size = get_le32(pb);
  1017. #ifdef DEBUG_SEEK
  1018. printf("tag=%c%c%c%c size=0x%x\n",
  1019. tag & 0xff,
  1020. (tag >> 8) & 0xff,
  1021. (tag >> 16) & 0xff,
  1022. (tag >> 24) & 0xff,
  1023. size);
  1024. #endif
  1025. switch(tag) {
  1026. case MKTAG('i', 'd', 'x', '1'):
  1027. if (avi_read_idx1(s, size) < 0)
  1028. goto skip;
  1029. ret = 0;
  1030. goto the_end;
  1031. break;
  1032. default:
  1033. skip:
  1034. size += (size & 1);
  1035. if (url_fseek(pb, size, SEEK_CUR) < 0)
  1036. goto the_end; // something is wrong here
  1037. break;
  1038. }
  1039. }
  1040. the_end:
  1041. url_fseek(pb, pos, SEEK_SET);
  1042. return ret;
  1043. }
  1044. static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
  1045. {
  1046. AVIStream *ast2 = st2->priv_data;
  1047. int64_t ts2 = av_rescale_q(timestamp, st->time_base, st2->time_base);
  1048. av_free_packet(&ast2->sub_pkt);
  1049. if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 ||
  1050. avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0)
  1051. av_read_packet(ast2->sub_ctx, &ast2->sub_pkt);
  1052. }
  1053. static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  1054. {
  1055. AVIContext *avi = s->priv_data;
  1056. AVStream *st;
  1057. int i, index;
  1058. int64_t pos;
  1059. AVIStream *ast;
  1060. if (!avi->index_loaded) {
  1061. /* we only load the index on demand */
  1062. avi_load_index(s);
  1063. avi->index_loaded = 1;
  1064. }
  1065. assert(stream_index>= 0);
  1066. st = s->streams[stream_index];
  1067. ast= st->priv_data;
  1068. index= av_index_search_timestamp(st, timestamp * FFMAX(ast->sample_size, 1), flags);
  1069. if(index<0)
  1070. return -1;
  1071. /* find the position */
  1072. pos = st->index_entries[index].pos;
  1073. timestamp = st->index_entries[index].timestamp / FFMAX(ast->sample_size, 1);
  1074. // av_log(s, AV_LOG_DEBUG, "XX %"PRId64" %d %"PRId64"\n", timestamp, index, st->index_entries[index].timestamp);
  1075. if (CONFIG_DV_DEMUXER && avi->dv_demux) {
  1076. /* One and only one real stream for DV in AVI, and it has video */
  1077. /* offsets. Calling with other stream indexes should have failed */
  1078. /* the av_index_search_timestamp call above. */
  1079. assert(stream_index == 0);
  1080. /* Feed the DV video stream version of the timestamp to the */
  1081. /* DV demux so it can synthesize correct timestamps. */
  1082. dv_offset_reset(avi->dv_demux, timestamp);
  1083. url_fseek(s->pb, pos, SEEK_SET);
  1084. avi->stream_index= -1;
  1085. return 0;
  1086. }
  1087. for(i = 0; i < s->nb_streams; i++) {
  1088. AVStream *st2 = s->streams[i];
  1089. AVIStream *ast2 = st2->priv_data;
  1090. ast2->packet_size=
  1091. ast2->remaining= 0;
  1092. if (ast2->sub_ctx) {
  1093. seek_subtitle(st, st2, timestamp);
  1094. continue;
  1095. }
  1096. if (st2->nb_index_entries <= 0)
  1097. continue;
  1098. // assert(st2->codec->block_align);
  1099. assert((int64_t)st2->time_base.num*ast2->rate == (int64_t)st2->time_base.den*ast2->scale);
  1100. index = av_index_search_timestamp(
  1101. st2,
  1102. av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
  1103. flags | AVSEEK_FLAG_BACKWARD);
  1104. if(index<0)
  1105. index=0;
  1106. if(!avi->non_interleaved){
  1107. while(index>0 && st2->index_entries[index].pos > pos)
  1108. index--;
  1109. while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos)
  1110. index++;
  1111. }
  1112. // av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %"PRId64"\n", timestamp, index, st2->index_entries[index].timestamp);
  1113. /* extract the current frame number */
  1114. ast2->frame_offset = st2->index_entries[index].timestamp;
  1115. }
  1116. /* do the seek */
  1117. url_fseek(s->pb, pos, SEEK_SET);
  1118. avi->stream_index= -1;
  1119. return 0;
  1120. }
  1121. static int avi_read_close(AVFormatContext *s)
  1122. {
  1123. int i;
  1124. AVIContext *avi = s->priv_data;
  1125. for(i=0;i<s->nb_streams;i++) {
  1126. AVStream *st = s->streams[i];
  1127. AVIStream *ast = st->priv_data;
  1128. av_free(st->codec->palctrl);
  1129. if (ast->sub_ctx) {
  1130. av_freep(&ast->sub_ctx->pb);
  1131. av_close_input_stream(ast->sub_ctx);
  1132. }
  1133. av_free(ast->sub_buffer);
  1134. av_free_packet(&ast->sub_pkt);
  1135. }
  1136. if (avi->dv_demux)
  1137. av_free(avi->dv_demux);
  1138. return 0;
  1139. }
  1140. static int avi_probe(AVProbeData *p)
  1141. {
  1142. int i;
  1143. /* check file header */
  1144. for(i=0; avi_headers[i][0]; i++)
  1145. if(!memcmp(p->buf , avi_headers[i] , 4) &&
  1146. !memcmp(p->buf+8, avi_headers[i]+4, 4))
  1147. return AVPROBE_SCORE_MAX;
  1148. return 0;
  1149. }
  1150. AVInputFormat avi_demuxer = {
  1151. "avi",
  1152. NULL_IF_CONFIG_SMALL("AVI format"),
  1153. sizeof(AVIContext),
  1154. avi_probe,
  1155. avi_read_header,
  1156. avi_read_packet,
  1157. avi_read_close,
  1158. avi_read_seek,
  1159. .metadata_conv = ff_avi_metadata_conv,
  1160. };