bitstream.c
上传用户:sun1608
上传日期:2007-02-02
资源大小:6116k
文件大小:21k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1.  /******************************************************************************
  2.   *                                                                            *
  3.   *  This file is part of XviD, a free MPEG-4 video encoder/decoder            *
  4.   *                                                                            *
  5.   *  XviD is an implementation of a part of one or more MPEG-4 Video tools     *
  6.   *  as specified in ISO/IEC 14496-2 standard.  Those intending to use this    *
  7.   *  software module in hardware or software products are advised that its     *
  8.   *  use may infringe existing patents or copyrights, and any such use         *
  9.   *  would be at such party's own risk.  The original developer of this        *
  10.   *  software module and his/her company, and subsequent editors and their     *
  11.   *  companies, will have no liability for use of this software or             *
  12.   *  modifications or derivatives thereof.                                     *
  13.   *                                                                            *
  14.   *  XviD is free software; you can redistribute it and/or modify it           *
  15.   *  under the terms of the GNU General Public License as published by         *
  16.   *  the Free Software Foundation; either version 2 of the License, or         *
  17.   *  (at your option) any later version.                                       *
  18.   *                                                                            *
  19.   *  XviD is distributed in the hope that it will be useful, but               *
  20.   *  WITHOUT ANY WARRANTY; without even the implied warranty of                *
  21.   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
  22.   *  GNU General Public License for more details.                              *
  23.   *                                                                            *
  24.   *  You should have received a copy of the GNU General Public License         *
  25.   *  along with this program; if not, write to the Free Software               *
  26.   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *
  27.   *                                                                            *
  28.   ******************************************************************************/
  29.  /******************************************************************************
  30.   *                                                                            *
  31.   *  bitstream.c                                                               *
  32.   *                                                                            *
  33.   *  Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au>                    *
  34.   *                                                                            *
  35.   *  For more information visit the XviD homepage: http://www.xvid.org         *
  36.   *                                                                            *
  37.   ******************************************************************************/
  38.  /******************************************************************************
  39.   *    *
  40.   *  Revision history:                                                         *
  41.   *                                                                            *
  42.   *  26.03.2002 interlacing support
  43.   *  03.03.2002 qmatrix writing    *
  44.   *  03.03.2002 merged BITREADER and BITWRITER    *
  45.   *  30.02.2002 intra_dc_threshold support    *
  46.   *  04.12.2001 support for additional headers    *
  47.   *  16.12.2001 inital version                                               *
  48.   *    *
  49.   ******************************************************************************/
  50. #include "bitstream.h"
  51. #include "zigzag.h"
  52. #include "../quant/quant_matrix.h"
  53. static int __inline log2bin(int value)
  54. {
  55. int n = 0;
  56. while (value)
  57. {
  58. value >>= 1;
  59. n++;
  60. }
  61. return n;
  62. }
  63. static const uint32_t intra_dc_threshold_table[] =
  64. {
  65. 32, /* never use */
  66. 13,
  67. 15,
  68. 17,
  69. 19,
  70. 21,
  71. 23,
  72. 1,
  73. };
  74. void bs_get_matrix(Bitstream * bs, uint8_t * matrix) 
  75. int i = 0; 
  76.     int last, value = 0; 
  77.     
  78.     do 
  79. last = value; 
  80.         value = BitstreamGetBits(bs, 8); 
  81.         matrix[ scan_tables[0][i++] ]  = value; 
  82.     } 
  83.     while (value != 0 && i < 64); 
  84.     
  85. while (i < 64) 
  86. matrix[ scan_tables[0][i++] ]  = last; 
  87. /*
  88. decode headers
  89. returns coding_type, or -1 if error
  90. */
  91. int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, 
  92.  uint32_t * quant, uint32_t * fcode, uint32_t * intra_dc_threshold,
  93.  int findvol)
  94. {
  95. uint32_t vol_ver_id;
  96. uint32_t time_inc_resolution;
  97. uint32_t coding_type;
  98. uint32_t start_code;
  99. do
  100. {
  101. BitstreamByteAlign(bs);
  102. start_code = BitstreamShowBits(bs, 32);
  103. if (start_code == VISOBJSEQ_START_CODE)
  104. {
  105. // DEBUG("visual_object_sequence");
  106. BitstreamSkip(bs, 32); // visual_object_sequence_start_code
  107. BitstreamSkip(bs, 8); // profile_and_level_indication
  108. }
  109. else if (start_code == VISOBJSEQ_STOP_CODE)
  110. {
  111. BitstreamSkip(bs, 32); // visual_object_sequence_stop_code
  112. }
  113. else if (start_code == VISOBJ_START_CODE)
  114. {
  115. // DEBUG("visual_object");
  116. BitstreamSkip(bs,32); // visual_object_start_code
  117. if (BitstreamGetBit(bs)) // is_visual_object_identified
  118. {
  119. vol_ver_id = BitstreamGetBits(bs,4); // visual_object_ver_id
  120. BitstreamSkip(bs, 3); // visual_object_priority
  121. }
  122. else
  123. {
  124. vol_ver_id = 1;
  125. }
  126. if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) // visual_object_type
  127. {
  128. DEBUG("visual_object_type != video");
  129. return -1;
  130. }
  131. BitstreamSkip(bs, 4);
  132. // video_signal_type
  133. if (BitstreamGetBit(bs)) // video_signal_type
  134. {
  135. DEBUG("+ video_signal_type");
  136. BitstreamSkip(bs, 3); // video_format
  137. BitstreamSkip(bs, 1); // video_range
  138. if (BitstreamGetBit(bs)) // color_description
  139. {
  140. DEBUG("+ color_description");
  141. BitstreamSkip(bs, 8); // color_primaries
  142. BitstreamSkip(bs, 8); // transfer_characteristics
  143. BitstreamSkip(bs, 8); // matrix_coefficients
  144. }
  145. }
  146. }
  147. else if ((start_code & ~0x1f) == VIDOBJ_START_CODE)
  148. {
  149. BitstreamSkip(bs, 32); // video_object_start_code
  150. else if ((start_code & ~0xf) == VIDOBJLAY_START_CODE)
  151. {
  152. // DEBUG("video_object_layer");
  153. BitstreamSkip(bs, 32); // video_object_layer_start_code
  154. BitstreamSkip(bs, 1); // random_accessible_vol
  155. // video_object_type_indication
  156. if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE &&
  157. BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE &&
  158. BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN &&
  159. BitstreamShowBits(bs, 8) != 0) // BUGGY DIVX
  160. {
  161. DEBUG1("video_object_type_indication not supported", BitstreamShowBits(bs, 8));
  162. return -1;
  163. }
  164. BitstreamSkip(bs, 8);
  165. if (BitstreamGetBit(bs)) // is_object_layer_identifier
  166. {
  167. DEBUG("+ is_object_layer_identifier");
  168. vol_ver_id = BitstreamGetBits(bs,4); // video_object_layer_verid
  169. BitstreamSkip(bs, 3); // video_object_layer_priority
  170. }
  171. else
  172. {
  173. vol_ver_id = 1;
  174. }
  175. //DEBUGI("vol_ver_id", vol_ver_id);
  176. if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR) // aspect_ratio_info
  177. {
  178. DEBUG("+ aspect_ratio_info");
  179. BitstreamSkip(bs, 8); // par_width
  180. BitstreamSkip(bs, 8); // par_height
  181. }
  182. if (BitstreamGetBit(bs)) // vol_control_parameters
  183. {
  184. DEBUG("+ vol_control_parameters");
  185. BitstreamSkip(bs, 2); // chroma_format
  186. BitstreamSkip(bs, 1); // low_delay
  187. if (BitstreamGetBit(bs)) // vbv_parameters
  188. {
  189. DEBUG("+ vbv_parameters");
  190. BitstreamSkip(bs, 15); // first_half_bitrate
  191. READ_MARKER();
  192. BitstreamSkip(bs, 15); // latter_half_bitrate
  193. READ_MARKER();
  194. BitstreamSkip(bs, 15); // first_half_vbv_buffer_size
  195. READ_MARKER();
  196. BitstreamSkip(bs, 3); // latter_half_vbv_buffer_size
  197. BitstreamSkip(bs, 11); // first_half_vbv_occupancy
  198. READ_MARKER();
  199. BitstreamSkip(bs, 15); // latter_half_vbv_occupancy
  200. READ_MARKER();
  201. }
  202. }
  203. dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape
  204. // DEBUG1("shape", dec->shape);
  205. if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1)
  206. {
  207. BitstreamSkip(bs, 4); // video_object_layer_shape_extension
  208. }
  209. READ_MARKER();
  210. time_inc_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution
  211. time_inc_resolution--;
  212. if (time_inc_resolution > 0)
  213. {
  214. dec->time_inc_bits = log2bin(time_inc_resolution);
  215. }
  216. else
  217. {
  218. // dec->time_inc_bits = 0;
  219. // for "old" xvid compatibility, set time_inc_bits = 1
  220. dec->time_inc_bits = 1;
  221. }
  222. READ_MARKER();
  223. if (BitstreamGetBit(bs)) // fixed_vop_rate
  224. {
  225. BitstreamSkip(bs, dec->time_inc_bits); // fixed_vop_time_increment
  226. }
  227. if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)
  228. {
  229. if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR)
  230. {
  231. uint32_t width, height;
  232. READ_MARKER();
  233. width = BitstreamGetBits(bs, 13); // video_object_layer_width
  234. //DEBUGI("width", width);
  235. READ_MARKER();
  236. height = BitstreamGetBits(bs, 13); // video_object_layer_height
  237. //DEBUGI("height", height);
  238. READ_MARKER();
  239. if (findvol == 0) {
  240.   if (width != dec->width || height != dec->height)
  241.     {
  242.       DEBUG("FATAL: video dimension discrepancy ***");
  243.       DEBUG2("bitstream width/height", width, height);
  244.       DEBUG2("param width/height", dec->width, dec->height);
  245.       return -1;
  246.     }
  247. } else {
  248.   dec->width = width;
  249.   dec->height = height;
  250. }
  251. }
  252. if ((dec->interlacing = BitstreamGetBit(bs)))
  253. {
  254. DEBUG("vol: interlacing");
  255. }
  256. if (!BitstreamGetBit(bs)) // obmc_disable
  257. {
  258. DEBUG("IGNORED/TODO: !obmc_disable");
  259. // TODO
  260. // fucking divx4.02 has this enabled
  261. }
  262. if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2)))  // sprite_enable
  263. {
  264. DEBUG("sprite_enable; not supported");
  265. return -1;
  266. }
  267. if (vol_ver_id != 1 && dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)
  268. {
  269. BitstreamSkip(bs, 1); // sadct_disable
  270. }
  271. if (BitstreamGetBit(bs)) // not_8_bit
  272. {
  273. DEBUG("+ not_8_bit [IGNORED/TODO]");
  274. dec->quant_bits = BitstreamGetBits(bs, 4); // quant_precision
  275. BitstreamSkip(bs, 4); // bits_per_pixel
  276. }
  277. else
  278. {
  279. dec->quant_bits = 5;
  280. }
  281. if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE)
  282. {
  283. BitstreamSkip(bs, 1); // no_gray_quant_update
  284. BitstreamSkip(bs, 1); // composition_method
  285. BitstreamSkip(bs, 1); // linear_composition
  286. }
  287. dec->quant_type = BitstreamGetBit(bs); // quant_type
  288. // DEBUG1("**** quant_type", dec->quant_type);
  289. if (dec->quant_type)
  290. {
  291. if (BitstreamGetBit(bs)) // load_intra_quant_mat
  292. {
  293. uint8_t matrix[64];
  294. bs_get_matrix(bs, matrix);
  295. set_intra_matrix(matrix);
  296. }
  297. else
  298. set_intra_matrix(get_default_intra_matrix());
  299. if (BitstreamGetBit(bs)) // load_inter_quant_mat
  300. {
  301. uint8_t matrix[64]; 
  302. bs_get_matrix(bs, matrix);
  303. set_inter_matrix(matrix);
  304. }
  305. else
  306. set_inter_matrix(get_default_inter_matrix());
  307. if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE)
  308. {
  309. // TODO
  310. DEBUG("TODO: grayscale matrix stuff");
  311. return -1;
  312. }
  313. }
  314. if (vol_ver_id != 1)
  315. {
  316. dec->quarterpel = BitstreamGetBit(bs); // quarter_sampe
  317. if (dec->quarterpel)
  318. {
  319. DEBUG("IGNORED/TODO: quarter_sample");
  320. }
  321. }
  322. else
  323. {
  324. dec->quarterpel = 0;
  325. }
  326. if (!BitstreamGetBit(bs)) // complexity_estimation_disable
  327. {
  328. DEBUG("TODO: complexity_estimation header");
  329. // TODO
  330. return -1;
  331. }
  332. if (!BitstreamGetBit(bs)) // resync_marker_disable
  333. {
  334. DEBUG("IGNORED/TODO: !resync_marker_disable");
  335. // TODO
  336. }
  337. if (BitstreamGetBit(bs)) // data_partitioned
  338. {
  339. DEBUG("+ data_partitioned");
  340. BitstreamSkip(bs, 1); // reversible_vlc
  341. }
  342. if (vol_ver_id != 1)
  343. {
  344. if (BitstreamGetBit(bs)) // newpred_enable
  345. {
  346. DEBUG("+ newpred_enable");
  347. BitstreamSkip(bs, 2); // requested_upstream_message_type
  348. BitstreamSkip(bs, 1); // newpred_segment_type
  349. }
  350. if (BitstreamGetBit(bs)) // reduced_resolution_vop_enable
  351. {
  352. DEBUG("TODO: reduced_resolution_vop_enable");
  353. // TODO
  354. return -1;
  355. }
  356. }
  357. if (BitstreamGetBit(bs)) // scalability
  358. {
  359. // TODO
  360. DEBUG("TODO: scalability");
  361. return -1;
  362. }
  363. }
  364. else // dec->shape == BINARY_ONLY
  365. {
  366. if (vol_ver_id != 1)
  367. {
  368. if (BitstreamGetBit(bs)) // scalability
  369. {
  370. // TODO
  371. DEBUG("TODO: scalability");
  372. return -1;
  373. }
  374. }
  375. BitstreamSkip(bs, 1); // resync_marker_disable
  376. }
  377. if (findvol != 0) return 1;
  378. }
  379. else if (start_code == GRPOFVOP_START_CODE)
  380. {
  381. // DEBUG("group_of_vop");
  382. BitstreamSkip(bs, 32);
  383. {
  384. int hours, minutes, seconds;
  385. hours = BitstreamGetBits(bs, 5);
  386. minutes = BitstreamGetBits(bs, 6);
  387. READ_MARKER();
  388. seconds = BitstreamGetBits(bs, 6);
  389. // DEBUG3("hms", hours, minutes, seconds);
  390. }
  391. BitstreamSkip(bs, 1); // closed_gov
  392. BitstreamSkip(bs, 1); // broken_link
  393. }
  394. else if (start_code == VOP_START_CODE)
  395. {
  396.   if (findvol != 0) return -1;
  397. // DEBUG("vop_start_code");
  398. BitstreamSkip(bs, 32); // vop_start_code
  399. coding_type = BitstreamGetBits(bs, 2); // vop_coding_type
  400. //DEBUG1("coding_type", coding_type);
  401. while (BitstreamGetBit(bs) != 0) ; // time_base
  402. READ_MARKER();
  403.  
  404. //DEBUG1("time_inc_bits", dec->time_inc_bits);
  405. //DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits));
  406. if (dec->time_inc_bits)
  407. {
  408. BitstreamSkip(bs, dec->time_inc_bits); // vop_time_increment
  409. }
  410. READ_MARKER();
  411. if (!BitstreamGetBit(bs)) // vop_coded
  412. {
  413. return N_VOP;
  414. }
  415. /* if (newpred_enable)
  416. {
  417. }
  418. */
  419. if (coding_type != I_VOP)
  420. {
  421. *rounding = BitstreamGetBit(bs); // rounding_type
  422. //DEBUG1("rounding", *rounding);
  423. }
  424. /* if (reduced_resolution_enable)
  425. {
  426. }
  427. */
  428. if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR)
  429. {
  430. uint32_t width, height;
  431. uint32_t horiz_mc_ref, vert_mc_ref;
  432. width = BitstreamGetBits(bs, 13);
  433. READ_MARKER();
  434. height = BitstreamGetBits(bs, 13);
  435. READ_MARKER();
  436. horiz_mc_ref = BitstreamGetBits(bs, 13);
  437. READ_MARKER();
  438. vert_mc_ref = BitstreamGetBits(bs, 13);
  439. READ_MARKER();
  440. // DEBUG2("vop_width/height", width, height);
  441. // DEBUG2("ref             ", horiz_mc_ref, vert_mc_ref);
  442. BitstreamSkip(bs, 1); // change_conv_ratio_disable
  443. if (BitstreamGetBit(bs)) // vop_constant_alpha
  444. {
  445. BitstreamSkip(bs, 8); // vop_constant_alpha_value
  446. }
  447. }
  448. if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY)
  449. {
  450. // intra_dc_vlc_threshold
  451. *intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ];
  452. if (dec->interlacing)
  453. {
  454. if ((dec->top_field_first = BitstreamGetBit(bs)))
  455. {
  456. DEBUG("vop: top_field_first");
  457. }
  458. if ((dec->alternate_vertical_scan = BitstreamGetBit(bs)))
  459. {
  460. DEBUG("vop: alternate_vertical_scan");
  461. }
  462. }
  463. }
  464. *quant = BitstreamGetBits(bs, dec->quant_bits); // vop_quant
  465. //DEBUG1("quant", *quant);
  466. if (coding_type != I_VOP)
  467. {
  468. *fcode = BitstreamGetBits(bs, 3); // fcode_forward
  469. }
  470. if (coding_type == B_VOP)
  471. {
  472. // *fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward
  473. }
  474. return coding_type;
  475. }
  476. else if (start_code == USERDATA_START_CODE)
  477. {
  478. // DEBUG("user_data");
  479. BitstreamSkip(bs, 32); // user_data_start_code
  480. }
  481. else  // start_code == ?
  482. {
  483. if (BitstreamShowBits(bs, 24) == 0x000001)
  484. {
  485. DEBUG1("*** WARNING: unknown start_code", BitstreamShowBits(bs, 32));
  486. }
  487. BitstreamSkip(bs, 8);
  488. }
  489. }
  490. while ((BitstreamPos(bs) >> 3) < bs->length);
  491. DEBUG("*** WARNING: no vop_start_code found");
  492. if (findvol != 0) return 0;
  493. return -1; /* ignore it */
  494. }
  495. /* write custom quant matrix */
  496. static void bs_put_matrix(Bitstream * bs, const int16_t *matrix)
  497. {
  498. int i, j;
  499. const int last = matrix[scan_tables[0][63]];
  500. for (j = 63; j >= 0 && matrix[scan_tables[0][j - 1]] == last; j--) ;
  501. for (i = 0; i <= j; i++)
  502. {
  503. BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8);
  504. }
  505. if (j < 63)
  506. {
  507. BitstreamPutBits(bs, 0, 8);
  508. }
  509. }
  510. #ifdef MPEG4IP
  511. /*
  512. write vosh header
  513. */
  514. void BitstreamWriteVoshHeader(Bitstream * const bs)
  515. {
  516.     BitstreamPad(bs);
  517. BitstreamPutBits(bs, VISOBJSEQ_START_CODE, 32);
  518. BitstreamPutBits(bs, 3, 8); // Simple Profile @ L3
  519. // video object_start_code
  520. BitstreamPutBits(bs, VISOBJ_START_CODE, 32);
  521. // no verid, priority, or signal type
  522.     BitstreamPutBits(bs, 0x08, 8);
  523. // video object_start_code & vo_id
  524. BitstreamPutBits(bs, VO_START_CODE, 27);
  525.     BitstreamPutBits(bs, 0, 5);
  526. }
  527. #endif
  528. /*
  529. write vol header
  530. */
  531. void BitstreamWriteVolHeader(Bitstream * const bs,
  532. const MBParam * pParam)
  533. {
  534.     BitstreamPad(bs);
  535. #ifndef MPEG4IP
  536. // video object_start_code & vo_id
  537. BitstreamPutBits(bs, VO_START_CODE, 27);
  538.     BitstreamPutBits(bs, 0, 5);
  539. #endif
  540. // video_object_layer_start_code & vol_id
  541. BitstreamPutBits(bs, VOL_START_CODE, 28);
  542. BitstreamPutBits(bs, 0, 4);
  543. BitstreamPutBit(bs, 0); // random_accessible_vol
  544. #ifdef MPEG4IP
  545. BitstreamPutBits(bs, 1, 8); // video_object_type_indication
  546. // 1 == simple profile
  547. BitstreamPutBit(bs, 1); // is_object_layer_identified 
  548. BitstreamPutBits(bs, 2, 4); // visual object layer ver id = 2 
  549. BitstreamPutBits(bs, 1, 3); // visual object layer priority = 1 
  550. #else
  551. BitstreamPutBits(bs, 0, 8); // video_object_type_indication
  552. BitstreamPutBit(bs, 0); // is_object_layer_identified (0=not given)
  553. #endif
  554. BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1)
  555. BitstreamPutBit(bs, 0); // vol_control_parameters (0=not given)
  556. BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular)
  557. WRITE_MARKER();
  558.     
  559. /* time_increment_resolution; ignored by current decore versions
  560. eg. 2fps res=2 inc=1
  561. 25fps res=25 inc=1
  562. 29.97fps res=30000 inc=1001
  563. */
  564. #ifdef MPEG4IP
  565. BitstreamPutBits(bs, pParam->fbase, 16);
  566. #else
  567. BitstreamPutBits(bs, 2, 16);
  568. #endif
  569. WRITE_MARKER();
  570. // fixed_vop_rate
  571. // MPEG4IP - above comment is incorrect, 0 signals variable rate
  572. BitstreamPutBit(bs, 0);
  573. // fixed_time_increment: value=nth_of_sec, nbits = log2(resolution)
  574. // BitstreamPutBits(bs, 0, 15);
  575. WRITE_MARKER();
  576. BitstreamPutBits(bs, pParam->width, 13); // width
  577. WRITE_MARKER();
  578. BitstreamPutBits(bs, pParam->height, 13); // height
  579. WRITE_MARKER();
  580. BitstreamPutBit(bs, pParam->global_flags & XVID_INTERLACING); // interlace
  581. BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation)
  582. #ifdef MPEG4IP
  583. BitstreamPutBits(bs, 0, 2); // sprite_usage
  584. #else
  585. BitstreamPutBit(bs, 0); // sprite_enable
  586. #endif
  587. BitstreamPutBit(bs, 0); // not_in_bit
  588. // quant_type   0=h.263  1=mpeg4(quantizer tables)
  589. BitstreamPutBit(bs, pParam->quant_type);
  590. if (pParam->quant_type)
  591. {
  592. BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat
  593. if (get_intra_matrix_status())
  594. {
  595. bs_put_matrix(bs, get_intra_matrix());
  596. }
  597. BitstreamPutBit(bs, get_inter_matrix_status()); // load_inter_quant_mat
  598. if (get_inter_matrix_status())
  599. {
  600. bs_put_matrix(bs, get_inter_matrix());
  601. }
  602. }
  603. #ifdef MPEG4IP
  604. BitstreamPutBit(bs, 0); // quarter pixel
  605. #endif
  606. BitstreamPutBit(bs, 1); // complexity_estimation_disable
  607. BitstreamPutBit(bs, 1); // resync_marker_disable
  608. BitstreamPutBit(bs, 0); // data_partitioned
  609. #ifdef MPEG4IP
  610. BitstreamPutBit(bs, 0); // newpred
  611. BitstreamPutBit(bs, 0); // reduced resolution vop
  612. #endif
  613. BitstreamPutBit(bs, 0); // scalability
  614. }
  615. /*
  616.   write vop header
  617.   NOTE: doesnt handle bother with time_base & time_inc
  618.   time_base = n seconds since last resync (eg. last iframe)
  619.   time_inc = nth of a second since last resync
  620.   (decoder uses these values to determine precise time since last resync)
  621. */
  622. void BitstreamWriteVopHeader(Bitstream * const bs,
  623. const MBParam * pParam)
  624. {
  625.     BitstreamPad(bs);
  626.     BitstreamPutBits(bs, VOP_START_CODE, 32);
  627.     BitstreamPutBits(bs, pParam->coding_type, 2);
  628.     
  629. // time_base = 0  write n x PutBit(1), PutBit(0)
  630. BitstreamPutBits(bs, 0, 1);
  631. WRITE_MARKER();
  632. // time_increment: value=nth_of_sec, nbits = log2(resolution)
  633. #ifdef MPEG4IP
  634. BitstreamPutBits(bs, pParam->fincr, pParam->time_inc_bits);
  635. #else
  636. BitstreamPutBits(bs, 1, 1);
  637. #endif
  638. WRITE_MARKER();
  639. BitstreamPutBits(bs, 1, 1); // vop_coded
  640. if (pParam->coding_type != I_VOP)
  641. BitstreamPutBits(bs, pParam->rounding_type, 1);
  642.     
  643. BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold
  644. if (pParam->global_flags & XVID_INTERLACING)
  645. {
  646. BitstreamPutBit(bs, 1); // top field first
  647. BitstreamPutBit(bs, 0); // alternate vertical scan
  648. }
  649.   BitstreamPutBits(bs, pParam->quant, 5); // quantizer
  650. if (pParam->coding_type != I_VOP)
  651. BitstreamPutBits(bs, pParam->fixed_code, 3); // fixed_code = [1,4]
  652. }