H264SequenceParameterSet.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /*------------------------------------------------------------------------------
  2. -- --
  3. -- This software is confidential and proprietary and may be used --
  4. -- only as expressly authorized by a licensing agreement from --
  5. -- --
  6. -- Hantro Products Oy. --
  7. -- --
  8. -- (C) COPYRIGHT 2006 HANTRO PRODUCTS OY --
  9. -- ALL RIGHTS RESERVED --
  10. -- --
  11. -- The entire notice above must be reproduced --
  12. -- on all copies and should not be removed. --
  13. -- --
  14. --------------------------------------------------------------------------------
  15. --
  16. -- Abstract : H264 Sequence Parameter Set
  17. --
  18. ------------------------------------------------------------------------------*/
  19. /*------------------------------------------------------------------------------
  20. 1. Include headers
  21. ------------------------------------------------------------------------------*/
  22. #include "H264SequenceParameterSet.h"
  23. #include "H264NalUnit.h"
  24. /*------------------------------------------------------------------------------
  25. 2. External compiler flags
  26. --------------------------------------------------------------------------------
  27. --------------------------------------------------------------------------------
  28. 3. Module defines
  29. ------------------------------------------------------------------------------*/
  30. #define MAX_LEVEL_INDEX (sizeof(H264LevelIdc)/sizeof(*H264LevelIdc))
  31. /* max MV length is 512 horizontal and 128 vertical in quarter pixel resolution */
  32. #define LOG2_MAX_MV_LENGTH_HOR 9
  33. #define LOG2_MAX_MV_LENGTH_VER 7
  34. #define EXTENDED_SAR 255
  35. const u32 H264LevelIdc[15] =
  36. { 10, 11, 12, 13, 20, 21, 22, 30, 31, 32, 40, 41, 42, 50, 51 };
  37. const u32 H264MaxCPBS[15] =
  38. { 210000, 600000, 1200000, 2400000, 2400000, 4800000, 4800000, 12000000,
  39. 16800000, 24000000, 30000000, 75000000, 75000000, 162000000, 288000000
  40. };
  41. /* Level 51 MaxFS in standard is 36864, increased to enable max resolution */
  42. const u32 H264MaxFS[15] = { 99, 396, 396, 396, 396, 792, 1620, 1620,
  43. 3600, 5120, 8192, 8192, 8192, 22080, 65025
  44. };
  45. /* sqrt(8*maxFrameSize) is maximum width and height of specific level */
  46. const u32 H264SqrtMaxFS8[15] =
  47. { 28, 56, 56, 56, 56, 79, 113, 113, 169, 202, 256, 256, 256, 420, 543 };
  48. const u32 H264MaxMBPS[15] =
  49. { 1485, 3000, 6000, 11880, 11880, 19800, 20250, 40500,
  50. 108000, 216000, 245760, 245760, 491520, 589824, 983040
  51. };
  52. const u32 H264MaxBR[15] =
  53. { 76800, 230400, 460800, 921600, 2400000, 4800000, 4800000, 12000000,
  54. 16800000, 24000000, 24000000, 60000000, 60000000, 162000000, 288000000
  55. };
  56. /*------------------------------------------------------------------------------
  57. 4. Local function prototypes
  58. ------------------------------------------------------------------------------*/
  59. static void SeqParameterSetMvcExtension(stream_s * stream, sps_s * sps);
  60. static void WriteVui(stream_s * strm, vui_t * vui, i32 numRefFrames);
  61. static i32 GetAspectRatioIdc(i32 sarWidth, i32 sarHeight);
  62. static void UpdateVuiPresence(sps_s * sps);
  63. /*------------------------------------------------------------------------------
  64. H264SpsInit
  65. ------------------------------------------------------------------------------*/
  66. void H264SeqParameterSetInit(sps_s * sps)
  67. {
  68. sps->byteStream = ENCHW_YES;
  69. sps->profileIdc = 66; /* 66 = baseline, 77 = main, 100 = high, 118 = MVC */
  70. sps->constraintSet0 = ENCHW_YES;
  71. sps->constraintSet1 = ENCHW_YES;
  72. sps->constraintSet2 = ENCHW_YES;
  73. sps->constraintSet3 = ENCHW_NO;
  74. sps->levelIdc = 30;
  75. sps->seqParameterSetId = 0;
  76. sps->log2MaxFrameNumMinus4 = (16 - 4);
  77. sps->picOrderCntType = 2;
  78. sps->numRefFrames = 1;
  79. sps->gapsInFrameNumValueAllowed = ENCHW_NO;
  80. sps->picWidthInMbsMinus1 = 176 / 16 - 1;
  81. sps->picHeightInMapUnitsMinus1 = 144 / 16 - 1;
  82. sps->frameMbsOnly = ENCHW_YES;
  83. sps->direct8x8Inference = ENCHW_YES;
  84. sps->frameCropping = ENCHW_NO;
  85. sps->vuiParametersPresent = ENCHW_YES;
  86. sps->vui.bitStreamRestrictionFlag = 1;
  87. sps->vui.videoFullRange = 0;
  88. sps->vui.sarWidth = 0;
  89. sps->vui.sarHeight = 0;
  90. sps->frameCropLeftOffset = 0;
  91. sps->frameCropRightOffset = 0;
  92. sps->frameCropTopOffset = 0;
  93. sps->frameCropBottomOffset = 0;
  94. return;
  95. }
  96. /*------------------------------------------------------------------------------
  97. H264SeqParameterSet
  98. ------------------------------------------------------------------------------*/
  99. void H264SeqParameterSet(stream_s * stream, sps_s * sps, true_e nalHeader)
  100. {
  101. /* Nal unit syntax */
  102. if (nalHeader == ENCHW_YES)
  103. H264NalUnitHdr(stream, 1, SPSET, sps->byteStream);
  104. H264NalBits(stream, sps->profileIdc, 8);
  105. COMMENT("profile_idc");
  106. H264NalBits(stream, (i32) sps->constraintSet0, 1);
  107. COMMENT("constraint_set0_flag");
  108. H264NalBits(stream, (i32) sps->constraintSet1, 1);
  109. COMMENT("constraint_set1_flag");
  110. H264NalBits(stream, (i32) sps->constraintSet2, 1);
  111. COMMENT("constraint_set2_flag");
  112. H264NalBits(stream, (i32) sps->constraintSet3, 1);
  113. COMMENT("constraint_set3_flag");
  114. H264NalBits(stream, 0, 4);
  115. COMMENT("reserved_zero_4bits");
  116. H264NalBits(stream, sps->levelIdc, 8);
  117. COMMENT("level_idc");
  118. H264ExpGolombUnsigned(stream, sps->seqParameterSetId);
  119. COMMENT("seq_parameter_set_id");
  120. if (sps->profileIdc >= 100)
  121. {
  122. H264ExpGolombUnsigned(stream, 1);
  123. COMMENT("chroma_format_idc");
  124. H264ExpGolombUnsigned(stream, 0);
  125. COMMENT("bit_depth_luma_minus8");
  126. H264ExpGolombUnsigned(stream, 0);
  127. COMMENT("bit_depth_chroma_minus8");
  128. H264NalBits(stream, 0, 1);
  129. COMMENT("qpprime_y_zero_transform_bypass_flag");
  130. H264NalBits(stream, 0, 1);
  131. COMMENT("seq_scaling_matrix_present_flag");
  132. }
  133. H264ExpGolombUnsigned(stream, sps->log2MaxFrameNumMinus4);
  134. COMMENT("log2_max_frame_num_minus4");
  135. H264ExpGolombUnsigned(stream, sps->picOrderCntType);
  136. COMMENT("pic_order_cnt_type");
  137. H264ExpGolombUnsigned(stream, sps->numRefFrames);
  138. COMMENT("num_ref_frames");
  139. H264NalBits(stream, (i32) sps->gapsInFrameNumValueAllowed, 1);
  140. COMMENT("gaps_in_frame_num_value_allowed_flag");
  141. H264ExpGolombUnsigned(stream, sps->picWidthInMbsMinus1);
  142. COMMENT("pic_width_in_mbs_minus1");
  143. H264ExpGolombUnsigned(stream, sps->picHeightInMapUnitsMinus1);
  144. COMMENT("pic_height_in_map_units_minus1");
  145. H264NalBits(stream, (i32) sps->frameMbsOnly, 1);
  146. COMMENT("frame_mbs_only_flag");
  147. H264NalBits(stream, (i32) sps->direct8x8Inference, 1);
  148. COMMENT("direct_8x8_inference_flag");
  149. H264NalBits(stream, (i32) sps->frameCropping, 1);
  150. COMMENT("frame_cropping_flag");
  151. /* Frame cropping parameters */
  152. if (sps->frameCropping)
  153. {
  154. H264ExpGolombUnsigned(stream, sps->frameCropLeftOffset);
  155. COMMENT("frame_crop_left_offset");
  156. H264ExpGolombUnsigned(stream, sps->frameCropRightOffset);
  157. COMMENT("frame_crop_right_offset");
  158. H264ExpGolombUnsigned(stream, sps->frameCropTopOffset);
  159. COMMENT("frame_crop_top_offset");
  160. H264ExpGolombUnsigned(stream, sps->frameCropBottomOffset);
  161. COMMENT("frame_crop_bottom_offset");
  162. }
  163. UpdateVuiPresence(sps);
  164. #if 0
  165. /* Currently JMVC can't decode this */
  166. sps->vuiParametersPresent = ENCHW_NO;
  167. #endif
  168. H264NalBits(stream, (i32) sps->vuiParametersPresent, 1);
  169. COMMENT("vui_parameters_present_flag");
  170. if(sps->vuiParametersPresent == ENCHW_YES)
  171. WriteVui(stream, &sps->vui, sps->numRefFrames);
  172. if (nalHeader == ENCHW_YES)
  173. H264NalUnitTrailinBits(stream, sps->byteStream);
  174. }
  175. void UpdateVuiPresence(sps_s * sps)
  176. {
  177. if(sps->vui.nalHrdParametersPresentFlag == 0 &&
  178. sps->vui.timeScale == 0 &&
  179. sps->vui.pictStructPresentFlag == 0 &&
  180. sps->vui.sarWidth == 0 && sps->vui.videoFullRange == 0 &&
  181. sps->vui.bitStreamRestrictionFlag == 0)
  182. {
  183. sps->vuiParametersPresent = ENCHW_NO;
  184. }
  185. }
  186. /*------------------------------------------------------------------------------
  187. H264SubsetSeqParameterSet
  188. ------------------------------------------------------------------------------*/
  189. void H264SubsetSeqParameterSet(stream_s * stream, sps_s * sps)
  190. {
  191. /* Nal unit syntax */
  192. H264NalUnitHdr(stream, 1, SSPSET, sps->byteStream);
  193. sps->profileIdc = 128; /* Stereo High profile */
  194. H264SeqParameterSet(stream, sps, ENCHW_NO);
  195. H264NalBits(stream, 1, 1);
  196. COMMENT("bit_equal_to_one");
  197. SeqParameterSetMvcExtension(stream, sps);
  198. H264NalBits(stream, 0, 1);
  199. COMMENT("mvc_vui_parameters_present_flag");
  200. H264NalBits(stream, 0, 1);
  201. COMMENT("additional_extension2_flag");
  202. H264NalUnitTrailinBits(stream, sps->byteStream);
  203. }
  204. /*------------------------------------------------------------------------------
  205. H264SubsetSeqParameterSet
  206. ------------------------------------------------------------------------------*/
  207. static void SeqParameterSetMvcExtension(stream_s * stream, sps_s * sps)
  208. {
  209. H264ExpGolombUnsigned(stream, 1);
  210. COMMENT("num_views_minus_1");
  211. H264ExpGolombUnsigned(stream, 0);
  212. COMMENT("view_id[0]");
  213. H264ExpGolombUnsigned(stream, 1);
  214. COMMENT("view_id[1]");
  215. H264ExpGolombUnsigned(stream, 1);
  216. COMMENT("num_anchor_refs_l0[1]");
  217. H264ExpGolombUnsigned(stream, 0);
  218. COMMENT("anchor_ref_l0[1]");
  219. H264ExpGolombUnsigned(stream, 0);
  220. COMMENT("num_anchor_refs_l1[1]");
  221. H264ExpGolombUnsigned(stream, 1);
  222. COMMENT("num_non_anchor_refs_l0[1]");
  223. H264ExpGolombUnsigned(stream, 0);
  224. COMMENT("non_anchor_ref_l0[1]");
  225. H264ExpGolombUnsigned(stream, 0);
  226. COMMENT("num_non_anchor_refs_l1[1]");
  227. H264ExpGolombUnsigned(stream, 0);
  228. COMMENT("num_level_values_signalled_minus1");
  229. H264NalBits(stream, sps->levelIdc, 8);
  230. COMMENT("level_idc");
  231. H264ExpGolombUnsigned(stream, 0);
  232. COMMENT("num_applicable_ops_minus1");
  233. H264NalBits(stream, 0, 3);
  234. COMMENT("applicable_op_temporal_id");
  235. H264ExpGolombUnsigned(stream, 0);
  236. COMMENT("applicable_op_num_target_view_minus1");
  237. H264ExpGolombUnsigned(stream, 1);
  238. COMMENT("applicable_op_target_view_id");
  239. H264ExpGolombUnsigned(stream, 1);
  240. COMMENT("applicable_op_num_views_minus1");
  241. }
  242. /*------------------------------------------------------------------------------
  243. Function: WriteVui
  244. Functional description:
  245. Write VUI params into the stream
  246. Inputs:
  247. vui_t *vui pointer to VUI params structure
  248. u32 numRefFrames number of reference frames, used as
  249. max_dec_frame_buffering
  250. Outputs:
  251. stream_s * pointer to stream data
  252. ------------------------------------------------------------------------------*/
  253. static void WriteVui(stream_s * strm, vui_t * vui, i32 numRefFrames)
  254. {
  255. /* Variables */
  256. i32 sarIdc;
  257. /* Code */
  258. ASSERT(strm);
  259. ASSERT(vui);
  260. sarIdc = GetAspectRatioIdc(vui->sarWidth, vui->sarHeight);
  261. if(sarIdc == 0) /* unspecified sample aspect ratio -> not present */
  262. {
  263. H264NalBits(strm, 0, 1);
  264. COMMENT("aspect_ratio_info_present_flag");
  265. }
  266. else
  267. {
  268. H264NalBits(strm, 1, 1);
  269. COMMENT("aspect_ratio_info_present_flag");
  270. H264NalBits(strm, sarIdc, 8);
  271. COMMENT("aspect_ratio_idc");
  272. if(sarIdc == EXTENDED_SAR)
  273. {
  274. H264NalBits(strm, vui->sarWidth, 16);
  275. COMMENT("sar_width");
  276. H264NalBits(strm, vui->sarHeight, 16);
  277. COMMENT("sar_height");
  278. }
  279. }
  280. H264NalBits(strm, 0, 1);
  281. COMMENT("overscan_info_present_flag");
  282. if(vui->videoFullRange != 0)
  283. {
  284. H264NalBits(strm, 1, 1);
  285. COMMENT("video_signal_type_present_flag");
  286. H264NalBits(strm, 5, 3);
  287. COMMENT("unspecified video_format");
  288. H264NalBits(strm, 1, 1);
  289. COMMENT("video_full_range_flag");
  290. H264NalBits(strm, 0, 1);
  291. COMMENT("colour_description_present_flag");
  292. }
  293. else
  294. {
  295. H264NalBits(strm, 0, 1);
  296. COMMENT("video_signal_type_present_flag");
  297. }
  298. H264NalBits(strm, 0, 1);
  299. COMMENT("chroma_loc_info_present_flag");
  300. if(vui->timeScale != 0)
  301. {
  302. H264NalBits(strm, 1, 1);
  303. COMMENT("timing_info_present_flag");
  304. H264NalBits(strm, vui->numUnitsInTick >> 16, 16);
  305. COMMENT("num_units_in_tick msb");
  306. H264NalBits(strm, vui->numUnitsInTick & 0xFFFF, 16);
  307. COMMENT("num_units_in_tick lsb");
  308. H264NalBits(strm, vui->timeScale >> 16, 16);
  309. COMMENT("time_scale msb");
  310. H264NalBits(strm, vui->timeScale & 0xFFFF, 16);
  311. COMMENT("time_scale lsb");
  312. H264NalBits(strm, 0, 1);
  313. COMMENT("fixed_frame_rate_flag");
  314. }
  315. else
  316. {
  317. H264NalBits(strm, 0, 1);
  318. COMMENT("timing_info_present_flag");
  319. }
  320. H264NalBits(strm, (i32) vui->nalHrdParametersPresentFlag, 1);
  321. COMMENT("nal_hrd_parameters_present_flag");
  322. if(vui->nalHrdParametersPresentFlag == ENCHW_YES)
  323. {
  324. H264ExpGolombUnsigned(strm, 0);
  325. COMMENT("cpb_cnt_minus1");
  326. {
  327. u32 bit_rate_scale = 1;
  328. u32 cpb_size_scale = 1;
  329. u32 tmp, i = 0;
  330. tmp = vui->cpbSize;
  331. while (4095 < (tmp >> (4 + i++)));
  332. cpb_size_scale = i;
  333. i = 0;
  334. tmp = vui->bitRate;
  335. while (4095 < (tmp >> (6 + i++)));
  336. bit_rate_scale = i;
  337. H264NalBits(strm, bit_rate_scale, 4);
  338. COMMENT("bit_rate_scale");
  339. H264NalBits(strm, cpb_size_scale, 4);
  340. COMMENT("cpb_size_scale");
  341. tmp = vui->bitRate >> (6 + bit_rate_scale);
  342. H264ExpGolombUnsigned(strm, tmp - 1);
  343. vui->bitRate = tmp << (6 + bit_rate_scale);
  344. COMMENT("bit_rate_value_minus1");
  345. tmp = vui->cpbSize >> (4 + cpb_size_scale);
  346. H264ExpGolombUnsigned(strm, tmp - 1);
  347. vui->cpbSize = tmp << (4 + cpb_size_scale);
  348. COMMENT("cpb_size_value_minus1");
  349. }
  350. H264NalBits(strm, 0, 1);
  351. COMMENT("cbr_flag");
  352. H264NalBits(strm, vui->initialCpbRemovalDelayLength - 1, 5);
  353. COMMENT("initial_cpb_removal_delay_length_minus1");
  354. H264NalBits(strm, vui->cpbRemovalDelayLength - 1, 5);
  355. COMMENT("cpb_removal_delay_length_minus1");
  356. H264NalBits(strm, vui->dpbOutputDelayLength - 1, 5);
  357. COMMENT("dpb_output_delay_length_minus1");
  358. H264NalBits(strm, vui->timeOffsetLength, 5);
  359. COMMENT("time_offset_length");
  360. }
  361. H264NalBits(strm, 0, 1);
  362. COMMENT("vcl_hrd_parameters_present_flag");
  363. if(vui->nalHrdParametersPresentFlag == ENCHW_YES)
  364. {
  365. H264NalBits(strm, 0, 1);
  366. COMMENT("low_delay_hrd_flag");
  367. }
  368. H264NalBits(strm, (i32) vui->pictStructPresentFlag, 1);
  369. COMMENT("pic_struct_present_flag");
  370. H264NalBits(strm, (i32) vui->bitStreamRestrictionFlag, 1);
  371. COMMENT("bit_stream_restriction_flag");
  372. if(vui->bitStreamRestrictionFlag == ENCHW_YES)
  373. {
  374. H264NalBits(strm, 1, 1);
  375. COMMENT("motion_vectors_over_pic_boundaries");
  376. H264ExpGolombUnsigned(strm, 0);
  377. COMMENT("max_bytes_per_pic_denom");
  378. H264ExpGolombUnsigned(strm, 0);
  379. COMMENT("max_bits_per_mb_denom");
  380. H264ExpGolombUnsigned(strm, LOG2_MAX_MV_LENGTH_HOR);
  381. COMMENT("log2_mv_length_horizontal");
  382. H264ExpGolombUnsigned(strm, LOG2_MAX_MV_LENGTH_VER);
  383. COMMENT("log2_mv_length_vertical");
  384. H264ExpGolombUnsigned(strm, 0);
  385. COMMENT("num_reorder_frames");
  386. H264ExpGolombUnsigned(strm, numRefFrames);
  387. COMMENT("max_dec_frame_buffering");
  388. }
  389. }
  390. /*------------------------------------------------------------------------------
  391. Function: GetAspectRatioIdc
  392. Functional description:
  393. Inputs:
  394. u32 sarWidth sample aspect ratio width
  395. u32 sarHeight sample aspect ratio height
  396. Outputs:
  397. Returns:
  398. u32 acpectRatioIdc
  399. ------------------------------------------------------------------------------*/
  400. static i32 GetAspectRatioIdc(i32 sarWidth, i32 sarHeight)
  401. {
  402. i32 aspectRatioIdc;
  403. if(sarWidth == 0 || sarHeight == 0) /* unspecified */
  404. aspectRatioIdc = 0;
  405. else if(sarWidth == sarHeight) /* square, 1:1 */
  406. aspectRatioIdc = 1;
  407. else if(sarHeight == 11)
  408. {
  409. if(sarWidth == 12) /* 12:11 */
  410. aspectRatioIdc = 2;
  411. else if(sarWidth == 10) /* 10:11 */
  412. aspectRatioIdc = 3;
  413. else if(sarWidth == 16) /* 16:11 */
  414. aspectRatioIdc = 4;
  415. else if(sarWidth == 24) /* 24:11 */
  416. aspectRatioIdc = 6;
  417. else if(sarWidth == 20) /* 20:11 */
  418. aspectRatioIdc = 7;
  419. else if(sarWidth == 32) /* 32:11 */
  420. aspectRatioIdc = 8;
  421. else if(sarWidth == 18) /* 18:11 */
  422. aspectRatioIdc = 10;
  423. else if(sarWidth == 15) /* 15:11 */
  424. aspectRatioIdc = 11;
  425. else /* Extended_SAR */
  426. aspectRatioIdc = EXTENDED_SAR;
  427. }
  428. else if(sarHeight == 33)
  429. {
  430. if(sarWidth == 40) /* 40:33 */
  431. aspectRatioIdc = 5;
  432. else if(sarWidth == 80) /* 80:33 */
  433. aspectRatioIdc = 9;
  434. else if(sarWidth == 64) /* 64:33 */
  435. aspectRatioIdc = 12;
  436. else /* Extended_SAR */
  437. aspectRatioIdc = EXTENDED_SAR;
  438. }
  439. else if(sarWidth == 160 && sarHeight == 99) /* 160:99 */
  440. aspectRatioIdc = 13;
  441. else /* Extended_SAR */
  442. aspectRatioIdc = EXTENDED_SAR;
  443. return (aspectRatioIdc);
  444. }
  445. /*------------------------------------------------------------------------------
  446. Function: H264CheckLevel
  447. Functional description:
  448. Check whether levelIdc can accommodate the stream based on bit and
  449. frame rates set by the application
  450. Inputs:
  451. seqParamSet_t * pointer to SPS data structure
  452. u32 bitRate bit rate in bits per second
  453. u32 frameRateNum numerator of the frame rate
  454. u32 frameRateDenom denominator of the frame rate
  455. Outputs:
  456. seqParamSet_t * pointer to SPS data structure
  457. Returns:
  458. ENCHW_OK for success
  459. ENCHW_NOK for invalid params
  460. ------------------------------------------------------------------------------*/
  461. bool_e H264CheckLevel(sps_s * sps, i32 bitRate, i32 frameRateNum,
  462. i32 frameRateDenom)
  463. {
  464. /* Variables */
  465. i32 tmp, i;
  466. /* Code */
  467. ASSERT(sps);
  468. if(bitRate <= 0 || frameRateNum <= 0 || frameRateDenom <= 0)
  469. return (ENCHW_NOK);
  470. i = sps->levelIdx;
  471. tmp = (sps->picWidthInMbsMinus1 + 1) * (sps->picHeightInMapUnitsMinus1 + 1);
  472. if((u32) tmp > H264MaxFS[i] ||
  473. (u32) sps->picWidthInMbsMinus1 >= H264SqrtMaxFS8[i] ||
  474. (u32) sps->picHeightInMapUnitsMinus1 >= H264SqrtMaxFS8[i])
  475. return (ENCHW_NOK);
  476. tmp = frameRateNum * tmp / frameRateDenom;
  477. if(H264MaxMBPS[sps->levelIdx] < (u32) tmp)
  478. return (ENCHW_NOK);
  479. return (ENCHW_OK);
  480. }
  481. /*------------------------------------------------------------------------------
  482. Function: H264GetLevelIndex
  483. Functional description:
  484. function determines index to level tables
  485. level argument. If invalid level -> return INVALID_LEVEL
  486. Inputs:
  487. u32 levelIdc
  488. Outputs:
  489. Returns:
  490. u32 index
  491. INVALID_LEVEL
  492. ------------------------------------------------------------------------------*/
  493. u32 H264GetLevelIndex(u32 levelIdc)
  494. {
  495. u32 i;
  496. i = 0;
  497. while(H264LevelIdc[i] != levelIdc)
  498. {
  499. i++;
  500. if(i >= MAX_LEVEL_INDEX)
  501. return INVALID_LEVEL;
  502. }
  503. return (i);
  504. }
  505. /*------------------------------------------------------------------------------
  506. Function: H264SpsSetVui
  507. Functional description:
  508. Set VUI parameters in the SPS structure
  509. Inputs:
  510. u32 timeScale
  511. u32 numUnitsInTick
  512. bool zeroReorderFrames
  513. Outputs:
  514. seqParamSet_t * pointer to SPS structure
  515. ------------------------------------------------------------------------------*/
  516. void H264SpsSetVuiTimigInfo(sps_s * sps, u32 timeScale, u32 numUnitsInTick)
  517. {
  518. if(timeScale)
  519. sps->vuiParametersPresent = ENCHW_YES;
  520. sps->vui.timeScale = timeScale; /* used as timing_info_present_flag */
  521. sps->vui.numUnitsInTick = numUnitsInTick;
  522. }
  523. void H264SpsSetVuiVideoInfo(sps_s * sps, u32 videoFullRange)
  524. {
  525. if(videoFullRange)
  526. sps->vuiParametersPresent = ENCHW_YES;
  527. sps->vui.videoFullRange = videoFullRange; /* used as video_signal_type_present_flag */
  528. }
  529. void H264SpsSetVuiAspectRatio(sps_s * sps, u32 sampleAspectRatioWidth,
  530. u32 sampleAspectRatioHeight)
  531. {
  532. ASSERT(sampleAspectRatioWidth < (1 << 16));
  533. ASSERT(sampleAspectRatioHeight < (1 << 16));
  534. if(sampleAspectRatioWidth)
  535. sps->vuiParametersPresent = ENCHW_YES;
  536. sps->vui.sarWidth = sampleAspectRatioWidth; /* used as aspect_ratio_info_present_flag */
  537. sps->vui.sarHeight = sampleAspectRatioHeight;
  538. }
  539. /*------------------------------------------------------------------------------
  540. Function: H264SpsSetVuiHrd
  541. Functional description:
  542. Set VUI HRD parameters in the SPS structure
  543. Inputs:
  544. seqParamSet_t * pointer to SPS structure
  545. Outputs:
  546. seqParamSet_t * pointer to SPS structure
  547. ------------------------------------------------------------------------------*/
  548. void H264SpsSetVuiHrd(sps_s * sps, u32 present)
  549. {
  550. ASSERT(sps);
  551. sps->vui.nalHrdParametersPresentFlag = present;
  552. if(present)
  553. sps->vuiParametersPresent = ENCHW_YES;
  554. else
  555. {
  556. return;
  557. }
  558. ASSERT(sps->vui.timeScale && sps->vui.numUnitsInTick); /* set these first */
  559. sps->vui.initialCpbRemovalDelayLength = 24;
  560. sps->vui.cpbRemovalDelayLength = 24;
  561. sps->vui.dpbOutputDelayLength = 24;
  562. {
  563. u32 n = 1;
  564. while(sps->vui.numUnitsInTick > (1U << n))
  565. {
  566. n++;
  567. }
  568. sps->vui.timeOffsetLength = n;
  569. }
  570. }
  571. /*------------------------------------------------------------------------------
  572. Function: H264SpsSetVuiHrdBitRate
  573. Functional description:
  574. Set VUI HRD bit rate in the SPS structure
  575. Inputs:
  576. seqParamSet_t * pointer to SPS structure
  577. u32 bitRate
  578. Outputs:
  579. seqParamSet_t * pointer to SPS structure
  580. ------------------------------------------------------------------------------*/
  581. void H264SpsSetVuiHrdBitRate(sps_s * sps, u32 bitRate)
  582. {
  583. ASSERT(sps);
  584. sps->vui.bitRate = bitRate;
  585. }
  586. void H264SpsSetVuiHrdCpbSize(sps_s * sps, u32 cpbSize)
  587. {
  588. ASSERT(sps);
  589. sps->vui.cpbSize = cpbSize;
  590. }
  591. u32 H264SpsGetVuiHrdBitRate(sps_s * sps)
  592. {
  593. ASSERT(sps);
  594. return sps->vui.bitRate;
  595. }
  596. u32 H264SpsGetVuiHrdCpbSize(sps_s * sps)
  597. {
  598. ASSERT(sps);
  599. return sps->vui.cpbSize;
  600. }
  601. /*------------------------------------------------------------------------------
  602. Function name : H264EndOfSequence
  603. Description :
  604. Return type : void
  605. Argument : stream_s *stream
  606. Argument : sps_s *sps
  607. ------------------------------------------------------------------------------*/
  608. void H264EndOfSequence(stream_s * stream, sps_s * sps)
  609. {
  610. H264NalUnitHdr(stream, 0, ENDOFSEQUENCE, sps->byteStream);
  611. }
  612. /*------------------------------------------------------------------------------
  613. Function name : H264EndOfStream
  614. Description :
  615. Return type : void
  616. Argument : stream_s *stream
  617. Argument : sps_s *sps
  618. ------------------------------------------------------------------------------*/
  619. void H264EndOfStream(stream_s * stream, sps_s * sps)
  620. {
  621. H264NalUnitHdr(stream, 0, ENDOFSTREAM, sps->byteStream);
  622. }
  623. /*------------------------------------------------------------------------------
  624. Function name : H264SpsSetVuiPictStructPresentFlag
  625. Description : Signal presence of pic_struct in picture timing SEI
  626. Return type : void
  627. Argument : sps_s * sps
  628. Argument : u32 flag
  629. ------------------------------------------------------------------------------*/
  630. void H264SpsSetVuiPictStructPresentFlag(sps_s * sps, u32 flag)
  631. {
  632. sps->vui.pictStructPresentFlag = flag;
  633. }