decode.c
上传用户:aoeyumen
上传日期:2007-01-06
资源大小:3329k
文件大小:9k
源码类别:

DVD

开发平台:

Unix_Linux

  1. /* 
  2.  *    decode.c
  3.  *
  4.  * Copyright (C) Aaron Holtzman - May 1999
  5.  *
  6.  *  This file is part of ac3dec, a free Dolby AC-3 stream decoder.
  7.  *
  8.  *  ac3dec is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  ac3dec is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  *
  23.  */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif 
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <errno.h>
  30. #include <string.h>
  31. #include "ac3.h"
  32. #include "decode.h"
  33. #include "bitstream.h"
  34. #include "imdct.h"
  35. #include "exponent.h"
  36. #include "mantissa.h"
  37. #include "bit_allocate.h"
  38. #include "uncouple.h"
  39. #include "parse.h"
  40. #include "output.h"
  41. #include "crc.h"
  42. #include "rematrix.h"
  43. #include "sys/time.h"
  44. #include "debug.h"
  45. static void decode_find_sync(bitstream_t *bs);
  46. static void decode_print_banner(void);
  47. static stream_coeffs_t stream_coeffs;
  48. static stream_samples_t stream_samples;
  49. static audblk_t audblk;
  50. static bsi_t bsi;
  51. static syncinfo_t syncinfo;
  52. static uint_32 frame_count = 0;
  53. prefs_t global_prefs = {0,0};
  54. #ifdef DO_NIST
  55. Synchronization *s;
  56. AudioStream *inputstream;
  57. int decode_ac3(AudioStream* inp, Synchronization* syn)
  58. #else
  59. int main(int argc,char *argv[])
  60. #endif
  61. {
  62. int i;
  63. bitstream_t *bs;
  64. FILE *in_file;
  65. int done_banner = 0;
  66. #ifdef DO_NIST
  67.         s = syn;
  68.         inputstream = inp;
  69. #else
  70. /* If we get an argument then use it as a filename... otherwise use
  71.  * stdin */
  72. if(argc > 1)
  73. {
  74. in_file = fopen(argv[1],"r");
  75. if(!in_file)
  76. {
  77. fprintf(stderr,"%s - Couldn't open file %sn",strerror(errno),argv[1]);
  78. exit(1);
  79. }
  80. }
  81. else
  82. in_file = stdin;
  83. bs = bitstream_open(in_file);
  84. #endif
  85. imdct_init();
  86. decode_sanity_check_init();
  87. output_open();
  88. while(1)
  89. {
  90. decode_find_sync(bs);
  91. parse_syncinfo(&syncinfo,bs);
  92. parse_bsi(&bsi,bs);
  93. if(!done_banner)
  94. {
  95. decode_print_banner();
  96. done_banner = 1;
  97. }
  98. for(i=0; i < 6; i++)
  99. {
  100. /* Extract most of the audblk info from the bitstream
  101.  * (minus the mantissas */
  102. parse_audblk(&bsi,&audblk,bs);
  103. decode_sanity_check();
  104. /* Take the differential exponent data and turn it into
  105.  * absolute exponents */
  106. exponent_unpack(&bsi,&audblk,&stream_coeffs); 
  107. decode_sanity_check();
  108. /* Figure out how many bits per mantissa */
  109. bit_allocate(syncinfo.fscod,&bsi,&audblk);
  110. decode_sanity_check();
  111. /* Extract the mantissas from the data stream */
  112. mantissa_unpack(&bsi,&audblk,bs);
  113. decode_sanity_check();
  114. /* Uncouple the coupling channel if it exists and
  115.  * convert the mantissa and exponents to IEEE floating
  116.  * point format */
  117. uncouple(&bsi,&audblk,&stream_coeffs);
  118. decode_sanity_check();
  119. if(bsi.acmod == 0x2)
  120. rematrix(&audblk,&stream_coeffs);
  121. #if 0
  122. /* Perform dynamic range compensation */
  123. dynamic_range(&bsi,&audblk,&stream_coeffs); 
  124. #endif
  125. /* Convert the frequency data into time samples */
  126. imdct(&bsi,&audblk,&stream_coeffs,&stream_samples);
  127. decode_sanity_check();
  128. /* Send the samples to the output device */
  129. output_play(&bsi,&stream_samples);
  130. }
  131. #ifdef DO_NIST
  132.                 if (s) {
  133. /*                  output_flush(); */
  134.                   s->wait(2);
  135.                 }
  136. #endif
  137. parse_auxdata(&syncinfo,bs);
  138. if(!crc_validate())
  139. {
  140. dprintf("(crc) CRC check failedn");
  141. }
  142. else
  143. {
  144. dprintf("(crc) CRC check passedn");
  145. }
  146. decode_sanity_check();
  147. #ifndef DO_NIST
  148. if(bitstream_done(bs))
  149. break;
  150. #endif
  151. }
  152. printf("End of streamn");
  153. #ifndef DO_NIST
  154. bitstream_close(bs);
  155. #endif
  156. output_close();
  157. return 0;
  158. }
  159. static 
  160. void decode_find_sync(bitstream_t *bs)
  161. {
  162. uint_16 sync_word;
  163. uint_32 i = 0;
  164. #ifdef DO_NIST
  165.         inputstream->get_header_ac3();
  166. #else
  167. sync_word = bitstream_get(bs,16);
  168. /* Make sure we sync'ed */
  169. while(1)
  170. {
  171. if(sync_word == 0x0b77)
  172. break;
  173. sync_word <<= 8;
  174. sync_word |= bitstream_get(bs,8);
  175. i++;
  176. }
  177. dprintf("(sync) %ld bits skipped to synchronizen",i);
  178. dprintf("(sync) begin frame %ldn",frame_count);
  179. frame_count++;
  180. bs->total_bits_read = 16;
  181. #endif
  182. crc_init();
  183. }
  184. void decode_sanity_check_init(void)
  185. {
  186. syncinfo.magic = DECODE_MAGIC_NUMBER;
  187. bsi.magic = DECODE_MAGIC_NUMBER;
  188. audblk.magic1 = DECODE_MAGIC_NUMBER;
  189. audblk.magic2 = DECODE_MAGIC_NUMBER;
  190. audblk.magic3 = DECODE_MAGIC_NUMBER;
  191. }
  192. void decode_sanity_check(void)
  193. {
  194. int i;
  195. #ifdef DO_NIST
  196.  return;
  197. #endif
  198. if(syncinfo.magic != DECODE_MAGIC_NUMBER)
  199. fprintf(stderr,"n** Sanity check failed -- syncinfo magic number **");
  200. if(bsi.magic != DECODE_MAGIC_NUMBER)
  201. fprintf(stderr,"n** Sanity check failed -- bsi magic number **");
  202. if(audblk.magic1 != DECODE_MAGIC_NUMBER)
  203. fprintf(stderr,"n** Sanity check failed -- audblk magic number 1 **"); 
  204. if(audblk.magic2 != DECODE_MAGIC_NUMBER)
  205. fprintf(stderr,"n** Sanity check failed -- audblk magic number 2 **"); 
  206. if(audblk.magic3 != DECODE_MAGIC_NUMBER)
  207. fprintf(stderr,"n** Sanity check failed -- audblk magic number 3 **"); 
  208. for(i = 0;i < 5 ; i++)
  209. {
  210. if (audblk.fbw_exp[i][255] !=0 || audblk.fbw_exp[i][254] !=0 || 
  211. audblk.fbw_exp[i][253] !=0)
  212. fprintf(stderr,"n** Sanity check failed -- fbw_exp out of bounds **"); 
  213. if (audblk.fbw_bap[i][255] !=0 || audblk.fbw_bap[i][254] !=0 || 
  214. audblk.fbw_bap[i][253] !=0)
  215. fprintf(stderr,"n** Sanity check failed -- fbw_bap out of bounds **"); 
  216. if (audblk.chmant[i][255] !=0 || audblk.chmant[i][254] !=0 || 
  217. audblk.chmant[i][253] !=0)
  218. fprintf(stderr,"n** Sanity check failed -- chmant out of bounds **"); 
  219. }
  220. if (audblk.cpl_exp[255] !=0 || audblk.cpl_exp[254] !=0 || 
  221. audblk.cpl_exp[253] !=0)
  222. fprintf(stderr,"n** Sanity check failed -- cpl_exp out of bounds **"); 
  223. if (audblk.cpl_bap[255] !=0 || audblk.cpl_bap[254] !=0 || 
  224. audblk.cpl_bap[253] !=0)
  225. fprintf(stderr,"n** Sanity check failed -- cpl_bap out of bounds **"); 
  226. if (audblk.cplmant[255] !=0 || audblk.cplmant[254] !=0 || 
  227. audblk.cplmant[253] !=0)
  228. fprintf(stderr,"n** Sanity check failed -- cpl_mant out of bounds **"); 
  229. if ((audblk.cplinu == 1) && (audblk.cplbegf > (audblk.cplendf+2)))
  230. fprintf(stderr,"n** Sanity check failed -- cpl params inconsistent **"); 
  231. for(i=0; i < bsi.nfchans; i++)
  232. {
  233. if((audblk.chincpl[i] == 0) && (audblk.chbwcod[i] > 60))
  234. fprintf(stderr,"n** Sanity check failed -- chbwcod too big **"); 
  235. }
  236. return;
  237. }
  238. const char *const language[256] = {
  239. "unknown", "Albanian", "Breton", "Catalan", "Croatian", "Welsh", "Czech",
  240. "Danish", "German", "English", "Spanish", "Esperanto", "Estonian", "Basque",
  241. "Faroese", "French", "Frisian", "Irish", "Gaelic", "Galician", "Icelandic",
  242. "Italian", "Lappish", "Latin", "Latvian", "Luxembourgian", "Lithuanian",
  243. "Faroese", "French", "Frisian", "Irish", "Gaelic", "Galician", "Icelandic",
  244. "Italian", "Lappish", "Latin", "Latvian", "Luxembourgian", "Lithuanian",
  245. "Hungarian", "Maltese", "Dutch", "Norwegian", "Occitan", "Polish","Portugese",
  246. "Romanian", "Romansh", "Serbian", "Slovak", "Slovene", "Finnish", "Swedish",
  247. "Turkish", "Flemish", "Walloon", "0x2c", "0x2d", "0x2e", "0x2f", "0x30",
  248. "0x31", "0x32", "0x33", "0x34", "0x35", "0x36", "0x37", "0x38", "0x39","0x3a", 
  249. "0x3b", "0x3c", "0x3d", "0x3e", "0x3f", "background", "0x41", "0x42", "0x43",
  250. "0x44", "Zulu", "Vietnamese", "Uzbek", "Urdu", "Ukrainian", "Thai", "Telugu",
  251. "Tatar", "Tamil", "Tadzhik", "Swahili", "Sranan Tongo", "Somali", "Sinhalese",
  252. "Shona", "Serbo-Croat", "Ruthenian", "Russian", "Quechua", "Pustu", "Punjabi",
  253. "Persian", "Papamiento", "Oriya", "Nepali", "Ndebele", "Marathi", "Moldavian",
  254. "Malaysian", "Malagasay", "Macedonian", "Laotian", "Korean", "Khmer", "Kazakh",
  255. "Kannada", "Japanese", "Indonesian", "Hindi", "Hebrew", "Hausa", "Gurani",
  256. "Gujurati", "Greek", "Georgian", "Fulani", "Dari", "Churash", "Chinese",
  257. "Burmese", "Bulgarian", "Bengali", "Belorussian", "Bambora", "Azerbijani",
  258. "Assamese", "Armenian", "Arabic", "Amharic"
  259. };
  260.  
  261. void decode_print_banner(void)
  262. {
  263. printf(PACKAGE"-"VERSION" (C) 1999 Aaron Holtzman (aholtzma@ess.engr.uvic.ca)n");
  264. printf("%d.%d Mode ",bsi.nfchans,bsi.lfeon);
  265. if (bsi.langcode)
  266.         printf("%s ", language[bsi.langcod]);
  267. switch (syncinfo.fscod)
  268. {
  269. case 2:
  270. printf("32 KHz   ");
  271. break;
  272. case 1:
  273. printf("44.1 KHz ");
  274. break;
  275. case 0:
  276. printf("48 KHz   ");
  277. break;
  278. default:
  279. printf("Invalid sampling rate ");
  280. break;
  281. }
  282. printf("%4d kbps ",syncinfo.bit_rate);
  283. switch(bsi.bsmod)
  284. {
  285. case 0:
  286. printf("Complete Main Audio Service");
  287. break;
  288. case 1:
  289. printf("Music and Effects Audio Service");
  290. case 2:
  291. printf("Visually Impaired Audio Service");
  292. break;
  293. case 3:
  294. printf("Hearing Impaired Audio Service");
  295. break;
  296. case 4:
  297. printf("Dialogue Audio Service");
  298. break;
  299. case 5:
  300. printf("Commentary Audio Service");
  301. break;
  302. case 6:
  303. printf("Emergency Audio Service");
  304. break;
  305. case 7:
  306. printf("Voice Over Audio Service");
  307. break;
  308. }
  309. printf("n");
  310. }