avi_demuxer.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "avi_demuxer.h"
  5. #include <stream.h>
  6. unsigned int Samples_per_sec;
  7. /**
  8. ** 读取4 个字节
  9. **/
  10. int read4bytes(void *stream)
  11. {
  12. DWORD val;
  13. int rdnum;
  14. rdnum = read_data(stream, &val, 4);
  15. if (rdnum != 4)
  16. {
  17. val = 0;
  18. }
  19. return val;
  20. }
  21. FOURCC check_junk(void *stream, FOURCC tag, FOURCC compare_tag)
  22. {
  23. int curtag;
  24. int junklen;
  25. if (tag != compare_tag)
  26. return tag;
  27. junklen = read4bytes(stream);
  28. read_skip(stream, junklen);
  29. curtag = read4bytes(stream);
  30. curtag = check_junk(stream, curtag, compare_tag);
  31. return curtag;
  32. }
  33. /**
  34. ** 分析视频文件头,获取必要信息
  35. **/
  36. int read_header(void *avict)
  37. {
  38. //amv len is set to 0 so we can skip zero
  39. //make it simple just do the amv case
  40. //riff len avi
  41. //list len hdrl
  42. //avih len mainAVIHeader
  43. //list len strl
  44. //strh len AVIStreamHeader
  45. //strf len BITMAPINFORHEADER or WAVEFORMATEX
  46. //riff len avi or riff 0 amv
  47. FOURCC tag;
  48. MainAVIHeader mheader;
  49. AVIStreamHeader sheader;
  50. int rdnum;
  51. //BITMAPINFOHEADER bmpinfo;
  52. //WAVEFORMATEX waveinfo;
  53. io_stream_t stream;
  54. avi_contex_t *ac;
  55. //int junklen;
  56. FOURCC contag;
  57. int strhlen;
  58. int strflen;
  59. int movi_len;
  60. if (avict == NULL)
  61. {
  62. printf("%s: %d\n", __FUNCTION__, __LINE__);
  63. return MEMERROR;
  64. }
  65. ac = (avi_contex_t *)avict;
  66. stream = ac->stream;
  67. read_skip(stream, 8); //riff len
  68. tag = read4bytes(stream);
  69. if ((tag != formtypeAVI)&&(tag != formtypeAMV))
  70. {
  71. printf("%s: tag: 0x%x\n", __FUNCTION__, tag);
  72. return UNKNOWNCON;
  73. }
  74. contag = tag;
  75. if (contag == formtypeAMV)
  76. {
  77. // 视频文件为amv 格式
  78. ac->amvformat = 1;
  79. }
  80. read_skip(stream, 12); //list len hdrl
  81. tag = read4bytes(stream);
  82. //if (tag != ckidAVIMAINHDR) //avih len mainAVIHeader
  83. // return UNKNOWNCON;
  84. read_skip(stream, 4);
  85. rdnum = read_data(stream, &mheader, sizeof(MainAVIHeader));
  86. if (rdnum != sizeof(MainAVIHeader))
  87. {
  88. printf("%s: %d\n", __FUNCTION__, __LINE__);
  89. return STREAMEND;
  90. }
  91. if (ac->amvformat)
  92. {
  93. // AMV Format
  94. int fps = 0;
  95. int timedur = 0;
  96. int timeval = mheader.dwReserved[3];
  97. // 帧率,每秒帧数
  98. fps = mheader.dwReserved[0]; //dwRate
  99. // 视频时长hh:mm:ss,转成秒
  100. timedur = (timeval&0xff) + ((timeval>>8)&0xff)*60 + ((timeval>>16)&0xff)*3600;
  101. // 视频总帧数
  102. ac->TotalFrames = timedur * fps;
  103. ac->dwScale = mheader.dwReserved[1];
  104. ac->dwRate = mheader.dwReserved[0];
  105. }
  106. else
  107. {
  108. // AVI Format
  109. ac->TotalFrames = mheader.dwTotalFrames;
  110. }
  111. ac->index_flag = mheader.dwFlags;
  112. ac->bmpheader.biWidth = mheader.dwWidth;
  113. ac->bmpheader.biHeight = mheader.dwHeight;
  114. do{
  115. tag = read4bytes(stream);
  116. tag = check_junk(stream, tag, ckidAVIPADDING);
  117. if (tag != LISTTAG)
  118. break;
  119. movi_len = read4bytes(stream);
  120. tag = read4bytes(stream);
  121. if (tag == listtypeAVIMOVIE)
  122. {
  123. ac->movitagpos = file_tell(stream) ;
  124. ac->index_offset = movi_len+ac->movitagpos - 4;
  125. //相对偏移起始位置
  126. break;
  127. }
  128. tag = read4bytes(stream);
  129. if (tag != ckidSTREAMHEADER) //strh len AVIStreamHeader
  130. {
  131. printf("%s: %d\n", __FUNCTION__, __LINE__);
  132. return UNKNOWNCON;
  133. }
  134. strhlen = read4bytes(stream);
  135. memset(&sheader, 0, sizeof(AVIStreamHeader));
  136. if (contag == formtypeAMV)
  137. {
  138. read_skip(stream, strhlen);
  139. }
  140. else
  141. {
  142. rdnum = read_data(stream, &sheader, sizeof(AVIStreamHeader));
  143. if (rdnum != sizeof(AVIStreamHeader))
  144. {
  145. printf("%s: %d\n", __FUNCTION__, __LINE__);
  146. return STREAMEND;
  147. }
  148. }
  149. tag = read4bytes(stream);
  150. if (tag != (ckidSTREAMFORMAT)) //strf len BITMAPINFORHEADER or WAVEFORMATEX
  151. {
  152. printf("%s: %d\n", __FUNCTION__, __LINE__);
  153. return UNKNOWNCON;
  154. }
  155. strflen = read4bytes(stream);
  156. if (contag == formtypeAMV)
  157. {
  158. if (strflen == 0x14)
  159. {
  160. rdnum = read_data(stream, &ac->amvwaveheader, sizeof(AMVAudioStreamFormat));
  161. if (rdnum != sizeof(AMVAudioStreamFormat))
  162. {
  163. printf("%s: %d\n", __FUNCTION__, __LINE__);
  164. return STREAMEND;
  165. }
  166. ac->dwSamples_per_sec = ac->amvwaveheader.dwSamples_per_sec;
  167. ac->wChannels = ac->amvwaveheader.wChannels;
  168. Samples_per_sec = ac->dwSamples_per_sec;
  169. }
  170. else
  171. {
  172. read_skip(stream, strflen);
  173. }
  174. }
  175. else
  176. {
  177. if (sheader.fccType == streamtypeVIDEO)
  178. {
  179. ac->dwScale = sheader.dwScale;
  180. ac->dwRate = sheader.dwRate;
  181. rdnum = read_data(stream, &ac->bmpheader, sizeof(BITMAPINFOHEADER));
  182. if (rdnum != sizeof(BITMAPINFOHEADER))
  183. {
  184. printf("%s: %d\n", __FUNCTION__, __LINE__);
  185. return STREAMEND;
  186. }
  187. }
  188. if (sheader.fccType == streamtypeAUDIO)
  189. {
  190. rdnum = read_data(stream, &ac->waveheader, sizeof(WAVEFORMATEX));
  191. if (rdnum != sizeof(WAVEFORMATEX))
  192. {
  193. printf("%s: %d\n", __FUNCTION__, __LINE__);
  194. return STREAMEND;
  195. }
  196. ac->dwSamples_per_sec = ac->waveheader.nSamplesPerSec;
  197. ac->wChannels = ac->waveheader.nChannels;
  198. Samples_per_sec = ac->dwSamples_per_sec;
  199. read_skip(stream, ac->waveheader.cbSize);
  200. }
  201. }
  202. }while(1);
  203. printf("%s: %d\n", __FUNCTION__, __LINE__);
  204. return NORMAL;
  205. }
  206. int get_es_chunk(void *avict, avi_packet_t *raw_packet)
  207. {
  208. FOURCC tag;
  209. //int rdnum;
  210. void *stream;
  211. avi_contex_t *ac;
  212. int chunklen;
  213. //int d[8];
  214. //int i,n;
  215. //int size;
  216. //unsigned char val;
  217. if ((avict == NULL)||(raw_packet == NULL))
  218. return MEMERROR;
  219. ac = (avi_contex_t *)avict;
  220. stream = ac->stream;
  221. tag = read4bytes(stream);
  222. if (tag == 0)
  223. return STREAMEND;
  224. //tag = check_junk(io, tag, ckidAVIPADDING);
  225. //get chunk type
  226. switch ((tag >> 16) & DATA_MASK)
  227. {
  228. case cktypeDIBbits:
  229. case cktypeDIBcompressed:
  230. raw_packet->es_type = streamtypeVIDEO;
  231. //printf("f pos: 0x%x\n", file_tell(io) - 4);
  232. break;
  233. case cktypeWAVEbytes:
  234. raw_packet->es_type = streamtypeAUDIO;
  235. break;
  236. default:
  237. raw_packet->es_type = tag;
  238. break;
  239. }
  240. //read chunk
  241. chunklen = read4bytes(stream);
  242. raw_packet->data_len = chunklen;
  243. raw_packet->pts = ((ac->framecounter * TIMESCALE * 10) / ac->framerate)/10;
  244. return NORMAL;
  245. }
  246. typedef struct{
  247. int frameth;
  248. DWORD pos;
  249. }frame_info_t;
  250. #define SCAN_BUF_LEN (MAX_PACKET_LEN-3)
  251. int search_es_chunk(void *avict, avi_packet_t *raw_packet)
  252. {
  253. FOURCC tag;
  254. //FOURCC Firsttag;
  255. //int rdnum;
  256. void *stream;
  257. avi_contex_t *ac;
  258. //int chunklen;
  259. //int d[8];
  260. //int i,n;
  261. int size;
  262. //unsigned char val;
  263. int frame_num;
  264. frame_info_t frame_arr[10];
  265. int offsettostart;
  266. unsigned char paddbytes[3];
  267. unsigned char *tmpbuf = 0;
  268. int end;
  269. unsigned char *ptr;
  270. int buf_remain;
  271. int flag_start;
  272. if ((avict == NULL)||(raw_packet == NULL))
  273. return MEMERROR;
  274. ac = (avi_contex_t *)avict;
  275. stream = ac->stream;
  276. flag_start = 0;
  277. frame_num = 0;
  278. paddbytes[0] = 0;
  279. paddbytes[1] = 0;
  280. paddbytes[2] = 0;
  281. //外部传入,就是av_buf里的缓冲区
  282. tmpbuf = raw_packet->data;
  283. GETMOREDATA:
  284. //ac->movitagpos;
  285. //offsettostart = file_tell(io);
  286. offsettostart = file_tell(stream);
  287. if (offsettostart <= ac->movitagpos)
  288. return STREAMSTART;
  289. offsettostart -= ac->movitagpos;
  290. if (offsettostart > SCAN_BUF_LEN)
  291. {
  292. file_seek_back(stream, -SCAN_BUF_LEN);
  293. read_data(stream, tmpbuf, SCAN_BUF_LEN);
  294. end = SCAN_BUF_LEN;
  295. file_seek_back(stream, -SCAN_BUF_LEN);
  296. }
  297. else
  298. {
  299. file_seek_back(stream, -offsettostart);
  300. read_data(stream, tmpbuf, offsettostart);
  301. end = offsettostart;
  302. flag_start = 1;
  303. file_seek_back(stream, -offsettostart);
  304. }
  305. //seek -(5k or less 5k)
  306. tmpbuf[end + 0] = paddbytes[0];
  307. tmpbuf[end + 1] = paddbytes[1];
  308. tmpbuf[end + 2] = paddbytes[2];
  309. ptr = tmpbuf;
  310. buf_remain = end + 3;
  311. while(1)
  312. {
  313. if (buf_remain <= 3){
  314. paddbytes[0] = tmpbuf[0];
  315. paddbytes[1] = tmpbuf[1];
  316. paddbytes[2] = tmpbuf[2];
  317. //reget buf;
  318. if (frame_num)
  319. goto SACN_END;
  320. if (flag_start)
  321. return STREAMSTART;
  322. goto GETMOREDATA;
  323. }
  324. if (*ptr != 0x30){ //30 is '0'
  325. ptr++;
  326. buf_remain--;
  327. continue;
  328. }
  329. if (buf_remain > 3)
  330. tag = ptr[0] + (ptr[1]<<8) + (ptr[2]<<16) + (ptr[3]<<24);
  331. else
  332. continue;
  333. switch (tag)
  334. {
  335. case 0x63643030:
  336. frame_arr[frame_num].pos = file_tell(stream) + (ptr - (unsigned char *)tmpbuf);
  337. if (frame_num >= 10)
  338. return NORMAL;
  339. frame_num++;
  340. if (buf_remain > 7){
  341. size = ptr[4] + (ptr[5]<<8) + (ptr[6]<<16) + (ptr[7]<<24);
  342. if ((buf_remain - 8) >= size){
  343. ptr += (8 + size);
  344. buf_remain -= (8 + size);
  345. }
  346. else
  347. buf_remain = 0;
  348. }
  349. else{
  350. ptr += 4;
  351. buf_remain -=4;
  352. }
  353. break;
  354. default:
  355. ptr++;
  356. buf_remain--;
  357. break;
  358. }
  359. }
  360. SACN_END:
  361. if (frame_num > 0)
  362. {
  363. int i;
  364. //get frameth
  365. for(i = 0; i < frame_num;i++)
  366. {
  367. frame_arr[i].frameth = ac->framecounter - (frame_num - i);
  368. //save to index
  369. }
  370. ac->framecounter = frame_arr[frame_num - 1].frameth;
  371. file_seek_set(stream, frame_arr[frame_num - 1].pos);
  372. //printf("b pos: 0x%x\n", frame_arr[frame_num - 1].pos);
  373. raw_packet->pts = ((ac->framecounter * TIMESCALE * 10) / ac->framerate )/ 10;
  374. }
  375. return NORMAL;
  376. }