mp4_header.c
上传用户:tuheem
上传日期:2007-05-01
资源大小:21889k
文件大小:8k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. #include <stdlib.h>
  2. #include <math.h>
  3. #include "mp4_vars.h"
  4. #include "getbits.h"
  5. #include "debug.h"
  6. #include "mp4_header.h"
  7. /**
  8.  *
  9. **/
  10. void next_start_code();
  11. /***/
  12. //得到视频对象层头
  13. int getvolhdr()
  14. {
  15. if (showbits(27) == VO_START_CODE)
  16. {
  17. getbits(27); 
  18. getbits(5); 
  19. if (getbits(28) != VOL_START_CODE)
  20. {
  21. exit(101);
  22. }
  23. mp4_state->hdr.ident = getbits(4); 
  24. mp4_state->hdr.random_accessible_vol = getbits(1);
  25. mp4_state->hdr.type_indication = getbits(8); 
  26. mp4_state->hdr.is_object_layer_identifier = getbits(1);
  27. if (mp4_state->hdr.is_object_layer_identifier) {
  28. mp4_state->hdr.visual_object_layer_verid = getbits(4);
  29. mp4_state->hdr.visual_object_layer_priority = getbits(3);
  30. else {
  31. mp4_state->hdr.visual_object_layer_verid = 1;
  32. mp4_state->hdr.visual_object_layer_priority = 1;
  33. }
  34. mp4_state->hdr.aspect_ratio_info = getbits(4);
  35. mp4_state->hdr.vol_control_parameters = getbits(1);
  36. if (mp4_state->hdr.vol_control_parameters) {
  37. mp4_state->hdr.chroma_format = getbits(2);
  38. mp4_state->hdr.low_delay = getbits(1);
  39. mp4_state->hdr.vbv_parameters = getbits(1);
  40. if (mp4_state->hdr.vbv_parameters) {
  41. mp4_state->hdr.first_half_bit_rate = getbits(15);
  42. getbits1(); 
  43. mp4_state->hdr.latter_half_bit_rate = getbits(15);
  44. getbits1(); 
  45. mp4_state->hdr.first_half_vbv_buffer_size = getbits(15);
  46. getbits1(); 
  47. mp4_state->hdr.latter_half_vbv_buffer_size = getbits(3);
  48. mp4_state->hdr.first_half_vbv_occupancy = getbits(11);
  49. getbits1(); 
  50. mp4_state->hdr.latter_half_vbv_occupancy = getbits(15);
  51. getbits1(); 
  52. }
  53. }
  54. mp4_state->hdr.shape = getbits(2);
  55. getbits1(); 
  56. mp4_state->hdr.time_increment_resolution = getbits(16);
  57. getbits1(); 
  58. mp4_state->hdr.fixed_vop_rate = getbits(1);
  59. if (mp4_state->hdr.fixed_vop_rate) {
  60. int bits = (int) ceil(log((double)mp4_state->hdr.time_increment_resolution)/log(2.0));
  61. if (bits < 1) 
  62. bits = 1;
  63. mp4_state->hdr.fixed_vop_time_increment = getbits(bits);
  64. }
  65. if (mp4_state->hdr.shape != BINARY_SHAPE_ONLY)  
  66. {
  67. if(mp4_state->hdr.shape == 0)
  68. {
  69. getbits1(); 
  70. mp4_state->hdr.width = getbits(13);
  71. getbits1(); 
  72. mp4_state->hdr.height = getbits(13);
  73. getbits1(); 
  74. }
  75. mp4_state->hdr.interlaced = getbits(1);
  76. mp4_state->hdr.obmc_disable = getbits(1);
  77. if (mp4_state->hdr.visual_object_layer_verid == 1) {
  78. mp4_state->hdr.sprite_usage = getbits(1);
  79. else {
  80. mp4_state->hdr.sprite_usage = getbits(2);
  81. }
  82. mp4_state->hdr.not_8_bit = getbits(1);
  83. if (mp4_state->hdr.not_8_bit) 
  84. {
  85. mp4_state->hdr.quant_precision = getbits(4);
  86. mp4_state->hdr.bits_per_pixel = getbits(4);
  87. }
  88. else 
  89. {
  90. mp4_state->hdr.quant_precision = 5;
  91. mp4_state->hdr.bits_per_pixel = 8;
  92. }
  93. if (mp4_state->hdr.shape == GRAY_SCALE) {
  94. exit(102);
  95. }
  96. mp4_state->hdr.quant_type = getbits(1); 
  97. if (mp4_state->hdr.quant_type) 
  98. {
  99. mp4_state->hdr.load_intra_quant_matrix = getbits(1);
  100. if (mp4_state->hdr.load_intra_quant_matrix) {
  101. unsigned int val;
  102. int i, k = 0;
  103. do {
  104. k++;
  105. val = getbits(8);
  106. mp4_tables->intra_quant_matrix[mp4_tables->zig_zag_scan[k]] = val;
  107. } while ((k < 64) && (val != 0));
  108. for (i = k; i < 64; i++) {
  109. mp4_tables->intra_quant_matrix[mp4_tables->zig_zag_scan[i]] =
  110. mp4_tables->intra_quant_matrix[mp4_tables->zig_zag_scan[k-1]];
  111. }
  112. }
  113. mp4_state->hdr.load_nonintra_quant_matrix = getbits(1);
  114. if (mp4_state->hdr.load_nonintra_quant_matrix) {
  115. unsigned int val;
  116. int i, k = 0;
  117. do {
  118. k++;
  119. val = getbits(8);
  120. mp4_tables->nonintra_quant_matrix[mp4_tables->zig_zag_scan[k]] = val;
  121. } while ((k < 64) && (val != 0));
  122. for (i = k; i < 64; i++) {
  123. mp4_tables->nonintra_quant_matrix[mp4_tables->zig_zag_scan[i]] =
  124. mp4_tables->nonintra_quant_matrix[mp4_tables->zig_zag_scan[k-1]];
  125. }
  126. }
  127. }
  128. if (mp4_state->hdr.visual_object_layer_verid/*ident*/ != 1) {
  129. mp4_state->hdr.quarter_pixel = getbits(1);
  130. } else {
  131. mp4_state->hdr.quarter_pixel = 0;
  132. }
  133. mp4_state->hdr.complexity_estimation_disable = getbits(1);
  134. mp4_state->hdr.error_res_disable = getbits(1);
  135. mp4_state->hdr.data_partitioning = getbits(1);
  136. if (mp4_state->hdr.data_partitioning) {
  137. exit(102);
  138. }   
  139. else {
  140. mp4_state->hdr.error_res_disable = 1;
  141. }
  142. mp4_state->hdr.intra_acdc_pred_disable = 0;
  143. mp4_state->hdr.scalability = getbits(1);
  144. if (mp4_state->hdr.scalability) {
  145. exit(103);
  146. }
  147. if (showbits(32) == USER_DATA_START_CODE) {
  148. exit(104);
  149. }
  150.     } 
  151. return 1;
  152.   }
  153.   
  154.   return 0; // // 没有视频对象启动代码
  155. }
  156. /***/
  157. // 得到图像组头
  158. int getgophdr()
  159. {
  160. if (nextbits(32) == GOP_START_CODE) 
  161. {
  162. getbits(32); 
  163. mp4_state->hdr.time_code = getbits(18);
  164. mp4_state->hdr.closed_gov = getbits(1);
  165. mp4_state->hdr.broken_link = getbits(1);
  166. }
  167. return 1;
  168. }
  169. /***/
  170. // 得到视频对象平面的头
  171. int getvophdr()
  172. {
  173. next_start_code();
  174. if(getbits(32) != (int) VOP_START_CODE)
  175.   {
  176. _Print("Vop start_code NOT foundn");
  177. return 0;
  178.   }
  179. mp4_state->hdr.prediction_type = getbits(2);
  180. while (getbits(1) == 1) // 临时的时间起点
  181.   {
  182. mp4_state->hdr.time_base++;
  183.   }
  184. getbits1(); 
  185. {
  186. int bits = (int) ceil(log(mp4_state->hdr.time_increment_resolution)/log(2.0));
  187. if (bits < 1) bits = 1;
  188. mp4_state->hdr.time_inc = getbits(bits); 
  189. }
  190. getbits1(); 
  191. mp4_state->hdr.vop_coded = getbits(1);
  192. if (mp4_state->hdr.vop_coded == 0) 
  193. {
  194. next_start_code();
  195. return 1;
  196. }  
  197. if ((mp4_state->hdr.shape != BINARY_SHAPE_ONLY) &&
  198. (mp4_state->hdr.prediction_type == P_VOP)) 
  199. {
  200. mp4_state->hdr.rounding_type = getbits(1);
  201. } else {
  202. mp4_state->hdr.rounding_type = 0;
  203. }
  204. if (mp4_state->hdr.shape != RECTANGULAR)
  205. {
  206. if (! (mp4_state->hdr.sprite_usage == STATIC_SPRITE && 
  207. mp4_state->hdr.prediction_type==I_VOP) )
  208. {
  209. mp4_state->hdr.width = getbits(13);
  210. getbits1();
  211. mp4_state->hdr.height = getbits(13);
  212. getbits1();
  213. mp4_state->hdr.hor_spat_ref = getbits(13);
  214. getbits1();
  215. mp4_state->hdr.ver_spat_ref = getbits(13);
  216. getbits1(); 
  217. }
  218. mp4_state->hdr.change_CR_disable = getbits(1);
  219. mp4_state->hdr.constant_alpha = getbits(1);
  220. if (mp4_state->hdr.constant_alpha) {
  221. mp4_state->hdr.constant_alpha_value = getbits(8);
  222. }
  223.   }
  224. if (! (mp4_state->hdr.complexity_estimation_disable)) {
  225. exit(108);
  226. }
  227. if (mp4_state->hdr.shape != BINARY_SHAPE_ONLY)  
  228.   { 
  229. mp4_state->hdr.intra_dc_vlc_thr = getbits(3);
  230. if (mp4_state->hdr.interlaced) {
  231. exit(109);
  232. }
  233.   }
  234. if (mp4_state->hdr.shape != BINARY_SHAPE_ONLY) 
  235.   { 
  236. mp4_state->hdr.quantizer = getbits(mp4_state->hdr.quant_precision); // vop quant
  237. if (mp4_state->hdr.prediction_type != I_VOP) 
  238. {
  239. mp4_state->hdr.fcode_for = getbits(3); 
  240. }
  241. if (! mp4_state->hdr.scalability) {
  242. if (mp4_state->hdr.shape && mp4_state->hdr.prediction_type!=I_VOP)
  243. // 视频对象平面编码类型
  244. mp4_state->hdr.shape_coding_type = getbits(1); 
  245. }
  246. return 1;
  247. }
  248. /***/
  249. int __inline nextbits(int nbit)
  250. {
  251. return showbits(nbit);
  252. }
  253. int __inline bytealigned(int nbit) 
  254. {
  255. return (((ld->bitcnt + nbit) % 8) == 0);
  256. }
  257. /***/
  258. void __inline next_start_code()
  259. {
  260. if (mp4_state->juice_flag)
  261. {
  262. if (! bytealigned(0))
  263. {
  264. getbits(1);
  265. while (! bytealigned(0)) {
  266. flushbits(1);
  267. }
  268. }
  269. }
  270. else
  271. {
  272. getbits(1);
  273. while (! bytealigned(0)) {
  274. flushbits(1);
  275. }
  276. }
  277. }
  278. /***/
  279. int __inline nextbits_bytealigned(int nbit)
  280. {
  281. int code;
  282. int skipcnt = 0;
  283. if (bytealigned(skipcnt))
  284. {
  285. if (showbits(8) == 127) {
  286. skipcnt += 8;
  287. }
  288. }
  289. else
  290. {
  291. while (! bytealigned(skipcnt)) {
  292. skipcnt += 1;
  293. }
  294. }
  295. code = showbits(nbit + skipcnt);
  296. return ((code << skipcnt) >> skipcnt);
  297. }