avidec.c 48 KB

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