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.

1384 lines
46KB

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