1
0

parset.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*!
  2. ************************************************************************
  3. * \file
  4. * parset.c
  5. * \brief
  6. * Parameter Sets
  7. * \author
  8. * Main contributors (see contributors.h for copyright, address and affiliation details)
  9. * - Stephan Wenger <[email protected]>
  10. *
  11. ***********************************************************************
  12. */
  13. #include "global.h"
  14. #include "image.h"
  15. #include "parsetcommon.h"
  16. #include "parset.h"
  17. #include "nalu.h"
  18. #include "memalloc.h"
  19. #include "fmo.h"
  20. #include "cabac.h"
  21. #include "vlc.h"
  22. #include "mbuffer.h"
  23. #include "erc_api.h"
  24. #if TRACE
  25. #define SYMTRACESTRING(s) strncpy(sym->tracestring,s,TRACESTRING_SIZE)
  26. #else
  27. #define SYMTRACESTRING(s) // do nothing
  28. #endif
  29. extern void init_frext(VideoParameters *p_Vid);
  30. // syntax for scaling list matrix values
  31. void Scaling_List(int *scalingList, int sizeOfScalingList, Boolean *UseDefaultScalingMatrix, Bitstream *s)
  32. {
  33. int j, scanj;
  34. int delta_scale, lastScale, nextScale;
  35. lastScale = 8;
  36. nextScale = 8;
  37. for(j=0; j<sizeOfScalingList; j++)
  38. {
  39. scanj = (sizeOfScalingList==16) ? ZZ_SCAN[j]:ZZ_SCAN8[j];
  40. if(nextScale!=0)
  41. {
  42. delta_scale = se_v ( " : delta_sl " , s);
  43. nextScale = (lastScale + delta_scale + 256) % 256;
  44. *UseDefaultScalingMatrix = (Boolean) (scanj==0 && nextScale==0);
  45. }
  46. scalingList[scanj] = (nextScale==0) ? lastScale:nextScale;
  47. lastScale = scalingList[scanj];
  48. }
  49. }
  50. // fill sps with content of p
  51. static void InterpretSPS (VideoParameters *p_Vid, DataPartition *p, seq_parameter_set_rbsp_t *sps)
  52. {
  53. unsigned i;
  54. unsigned n_ScalingList;
  55. int reserved_zero;
  56. Bitstream *s = p->bitstream;
  57. assert (p != NULL);
  58. assert (p->bitstream != NULL);
  59. assert (p->bitstream->streamBuffer != 0);
  60. assert (sps != NULL);
  61. sps->profile_idc = u_v (8, "SPS: profile_idc" , s);
  62. if ((sps->profile_idc!=BASELINE ) &&
  63. (sps->profile_idc!=MAIN ) &&
  64. (sps->profile_idc!=EXTENDED ) &&
  65. (sps->profile_idc!=FREXT_HP ) &&
  66. (sps->profile_idc!=FREXT_Hi10P ) &&
  67. (sps->profile_idc!=FREXT_Hi422 ) &&
  68. (sps->profile_idc!=FREXT_Hi444 ) &&
  69. (sps->profile_idc!=FREXT_CAVLC444 ))
  70. {
  71. printf("Invalid Profile IDC (%d) encountered. \n", sps->profile_idc);
  72. return;
  73. }
  74. sps->constrained_set0_flag = u_1 ( "SPS: constrained_set0_flag" , s);
  75. sps->constrained_set1_flag = u_1 ( "SPS: constrained_set1_flag" , s);
  76. sps->constrained_set2_flag = u_1 ( "SPS: constrained_set2_flag" , s);
  77. sps->constrained_set3_flag = u_1 ( "SPS: constrained_set3_flag" , s);
  78. reserved_zero = u_v (4, "SPS: reserved_zero_4bits" , s);
  79. assert (reserved_zero==0);
  80. sps->level_idc = u_v (8, "SPS: level_idc" , s);
  81. sps->seq_parameter_set_id = ue_v ("SPS: seq_parameter_set_id" , s);
  82. // Fidelity Range Extensions stuff
  83. sps->chroma_format_idc = 1;
  84. sps->bit_depth_luma_minus8 = 0;
  85. sps->bit_depth_chroma_minus8 = 0;
  86. p_Vid->lossless_qpprime_flag = 0;
  87. sps->separate_colour_plane_flag = 0;
  88. if((IS_FREXT_PROFILE(sps->profile_idc)))/*==FREXT_HP ) ||
  89. (sps->profile_idc==FREXT_Hi10P) ||
  90. (sps->profile_idc==FREXT_Hi422) ||
  91. (sps->profile_idc==FREXT_Hi444) ||
  92. (sps->profile_idc==FREXT_CAVLC444))*/
  93. {
  94. sps->chroma_format_idc = ue_v ("SPS: chroma_format_idc" , s);
  95. if(sps->chroma_format_idc == YUV444)
  96. {
  97. sps->separate_colour_plane_flag = u_1 ("SPS: separate_colour_plane_flag" , s);
  98. }
  99. sps->bit_depth_luma_minus8 = ue_v ("SPS: bit_depth_luma_minus8" , s);
  100. sps->bit_depth_chroma_minus8 = ue_v ("SPS: bit_depth_chroma_minus8" , s);
  101. p_Vid->lossless_qpprime_flag = u_1 ("SPS: lossless_qpprime_y_zero_flag" , s);
  102. sps->seq_scaling_matrix_present_flag = u_1 ( "SPS: seq_scaling_matrix_present_flag" , s);
  103. if(sps->seq_scaling_matrix_present_flag)
  104. {
  105. n_ScalingList = (sps->chroma_format_idc != YUV444) ? 8 : 12;
  106. for(i=0; i<n_ScalingList; i++)
  107. {
  108. sps->seq_scaling_list_present_flag[i] = u_1 ( "SPS: seq_scaling_list_present_flag" , s);
  109. if(sps->seq_scaling_list_present_flag[i])
  110. {
  111. if(i<6)
  112. Scaling_List(sps->ScalingList4x4[i], 16, &sps->UseDefaultScalingMatrix4x4Flag[i], s);
  113. else
  114. Scaling_List(sps->ScalingList8x8[i-6], 64, &sps->UseDefaultScalingMatrix8x8Flag[i-6], s);
  115. }
  116. }
  117. }
  118. }
  119. sps->log2_max_frame_num_minus4 = ue_v ("SPS: log2_max_frame_num_minus4" , s);
  120. sps->pic_order_cnt_type = ue_v ("SPS: pic_order_cnt_type" , s);
  121. if (sps->pic_order_cnt_type == 0)
  122. sps->log2_max_pic_order_cnt_lsb_minus4 = ue_v ("SPS: log2_max_pic_order_cnt_lsb_minus4" , s);
  123. else if (sps->pic_order_cnt_type == 1)
  124. {
  125. sps->delta_pic_order_always_zero_flag = u_1 ("SPS: delta_pic_order_always_zero_flag" , s);
  126. sps->offset_for_non_ref_pic = se_v ("SPS: offset_for_non_ref_pic" , s);
  127. sps->offset_for_top_to_bottom_field = se_v ("SPS: offset_for_top_to_bottom_field" , s);
  128. sps->num_ref_frames_in_pic_order_cnt_cycle = ue_v ("SPS: num_ref_frames_in_pic_order_cnt_cycle" , s);
  129. for(i=0; i<sps->num_ref_frames_in_pic_order_cnt_cycle; i++)
  130. sps->offset_for_ref_frame[i] = se_v ("SPS: offset_for_ref_frame[i]" , s);
  131. }
  132. sps->num_ref_frames = ue_v ("SPS: num_ref_frames" , s);
  133. sps->gaps_in_frame_num_value_allowed_flag = u_1 ("SPS: gaps_in_frame_num_value_allowed_flag" , s);
  134. sps->pic_width_in_mbs_minus1 = ue_v ("SPS: pic_width_in_mbs_minus1" , s);
  135. sps->pic_height_in_map_units_minus1 = ue_v ("SPS: pic_height_in_map_units_minus1" , s);
  136. sps->frame_mbs_only_flag = u_1 ("SPS: frame_mbs_only_flag" , s);
  137. if (!sps->frame_mbs_only_flag)
  138. {
  139. sps->mb_adaptive_frame_field_flag = u_1 ("SPS: mb_adaptive_frame_field_flag" , s);
  140. }
  141. sps->direct_8x8_inference_flag = u_1 ("SPS: direct_8x8_inference_flag" , s);
  142. sps->frame_cropping_flag = u_1 ("SPS: frame_cropping_flag" , s);
  143. if (sps->frame_cropping_flag)
  144. {
  145. sps->frame_cropping_rect_left_offset = ue_v ("SPS: frame_cropping_rect_left_offset" , s);
  146. sps->frame_cropping_rect_right_offset = ue_v ("SPS: frame_cropping_rect_right_offset" , s);
  147. sps->frame_cropping_rect_top_offset = ue_v ("SPS: frame_cropping_rect_top_offset" , s);
  148. sps->frame_cropping_rect_bottom_offset = ue_v ("SPS: frame_cropping_rect_bottom_offset" , s);
  149. }
  150. sps->vui_parameters_present_flag = (Boolean) u_1 ("SPS: vui_parameters_present_flag" , s);
  151. InitVUI(sps);
  152. ReadVUI(p, sps);
  153. sps->Valid = TRUE;
  154. }
  155. void InitVUI(seq_parameter_set_rbsp_t *sps)
  156. {
  157. sps->vui_seq_parameters.matrix_coefficients = 2;
  158. }
  159. int ReadVUI(DataPartition *p, seq_parameter_set_rbsp_t *sps)
  160. {
  161. Bitstream *s = p->bitstream;
  162. if (sps->vui_parameters_present_flag)
  163. {
  164. sps->vui_seq_parameters.aspect_ratio_info_present_flag = u_1 ("VUI: aspect_ratio_info_present_flag" , s);
  165. if (sps->vui_seq_parameters.aspect_ratio_info_present_flag)
  166. {
  167. sps->vui_seq_parameters.aspect_ratio_idc = u_v ( 8, "VUI: aspect_ratio_idc" , s);
  168. if (255==sps->vui_seq_parameters.aspect_ratio_idc)
  169. {
  170. sps->vui_seq_parameters.sar_width = (unsigned short) u_v (16, "VUI: sar_width" , s);
  171. sps->vui_seq_parameters.sar_height = (unsigned short) u_v (16, "VUI: sar_height" , s);
  172. }
  173. }
  174. sps->vui_seq_parameters.overscan_info_present_flag = u_1 ("VUI: overscan_info_present_flag" , s);
  175. if (sps->vui_seq_parameters.overscan_info_present_flag)
  176. {
  177. sps->vui_seq_parameters.overscan_appropriate_flag = u_1 ("VUI: overscan_appropriate_flag" , s);
  178. }
  179. sps->vui_seq_parameters.video_signal_type_present_flag = u_1 ("VUI: video_signal_type_present_flag" , s);
  180. if (sps->vui_seq_parameters.video_signal_type_present_flag)
  181. {
  182. sps->vui_seq_parameters.video_format = u_v ( 3,"VUI: video_format" , s);
  183. sps->vui_seq_parameters.video_full_range_flag = u_1 ( "VUI: video_full_range_flag" , s);
  184. sps->vui_seq_parameters.colour_description_present_flag = u_1 ( "VUI: color_description_present_flag" , s);
  185. if(sps->vui_seq_parameters.colour_description_present_flag)
  186. {
  187. sps->vui_seq_parameters.colour_primaries = u_v ( 8,"VUI: colour_primaries" , s);
  188. sps->vui_seq_parameters.transfer_characteristics = u_v ( 8,"VUI: transfer_characteristics" , s);
  189. sps->vui_seq_parameters.matrix_coefficients = u_v ( 8,"VUI: matrix_coefficients" , s);
  190. }
  191. }
  192. sps->vui_seq_parameters.chroma_location_info_present_flag = u_1 ( "VUI: chroma_loc_info_present_flag" , s);
  193. if(sps->vui_seq_parameters.chroma_location_info_present_flag)
  194. {
  195. sps->vui_seq_parameters.chroma_sample_loc_type_top_field = ue_v ( "VUI: chroma_sample_loc_type_top_field" , s);
  196. sps->vui_seq_parameters.chroma_sample_loc_type_bottom_field = ue_v ( "VUI: chroma_sample_loc_type_bottom_field" , s);
  197. }
  198. sps->vui_seq_parameters.timing_info_present_flag = u_1 ("VUI: timing_info_present_flag" , s);
  199. if (sps->vui_seq_parameters.timing_info_present_flag)
  200. {
  201. sps->vui_seq_parameters.num_units_in_tick = u_v (32,"VUI: num_units_in_tick" , s);
  202. sps->vui_seq_parameters.time_scale = u_v (32,"VUI: time_scale" , s);
  203. sps->vui_seq_parameters.fixed_frame_rate_flag = u_1 ( "VUI: fixed_frame_rate_flag" , s);
  204. }
  205. sps->vui_seq_parameters.nal_hrd_parameters_present_flag = u_1 ("VUI: nal_hrd_parameters_present_flag" , s);
  206. if (sps->vui_seq_parameters.nal_hrd_parameters_present_flag)
  207. {
  208. ReadHRDParameters(p, &(sps->vui_seq_parameters.nal_hrd_parameters));
  209. }
  210. sps->vui_seq_parameters.vcl_hrd_parameters_present_flag = u_1 ("VUI: vcl_hrd_parameters_present_flag" , s);
  211. if (sps->vui_seq_parameters.vcl_hrd_parameters_present_flag)
  212. {
  213. ReadHRDParameters(p, &(sps->vui_seq_parameters.vcl_hrd_parameters));
  214. }
  215. if (sps->vui_seq_parameters.nal_hrd_parameters_present_flag || sps->vui_seq_parameters.vcl_hrd_parameters_present_flag)
  216. {
  217. sps->vui_seq_parameters.low_delay_hrd_flag = u_1 ("VUI: low_delay_hrd_flag" , s);
  218. }
  219. sps->vui_seq_parameters.pic_struct_present_flag = u_1 ("VUI: pic_struct_present_flag " , s);
  220. sps->vui_seq_parameters.bitstream_restriction_flag = u_1 ("VUI: bitstream_restriction_flag" , s);
  221. if (sps->vui_seq_parameters.bitstream_restriction_flag)
  222. {
  223. sps->vui_seq_parameters.motion_vectors_over_pic_boundaries_flag = u_1 ("VUI: motion_vectors_over_pic_boundaries_flag", s);
  224. sps->vui_seq_parameters.max_bytes_per_pic_denom = ue_v ("VUI: max_bytes_per_pic_denom" , s);
  225. sps->vui_seq_parameters.max_bits_per_mb_denom = ue_v ("VUI: max_bits_per_mb_denom" , s);
  226. sps->vui_seq_parameters.log2_max_mv_length_horizontal = ue_v ("VUI: log2_max_mv_length_horizontal" , s);
  227. sps->vui_seq_parameters.log2_max_mv_length_vertical = ue_v ("VUI: log2_max_mv_length_vertical" , s);
  228. sps->vui_seq_parameters.num_reorder_frames = ue_v ("VUI: num_reorder_frames" , s);
  229. sps->vui_seq_parameters.max_dec_frame_buffering = ue_v ("VUI: max_dec_frame_buffering" , s);
  230. }
  231. }
  232. return 0;
  233. }
  234. int ReadHRDParameters(DataPartition *p, hrd_parameters_t *hrd)
  235. {
  236. Bitstream *s = p->bitstream;
  237. unsigned int SchedSelIdx;
  238. hrd->cpb_cnt_minus1 = ue_v ( "VUI: cpb_cnt_minus1" , s);
  239. hrd->bit_rate_scale = u_v ( 4,"VUI: bit_rate_scale" , s);
  240. hrd->cpb_size_scale = u_v ( 4,"VUI: cpb_size_scale" , s);
  241. for( SchedSelIdx = 0; SchedSelIdx <= hrd->cpb_cnt_minus1; SchedSelIdx++ )
  242. {
  243. hrd->bit_rate_value_minus1[ SchedSelIdx ] = ue_v ( "VUI: bit_rate_value_minus1" , s);
  244. hrd->cpb_size_value_minus1[ SchedSelIdx ] = ue_v ( "VUI: cpb_size_value_minus1" , s);
  245. hrd->cbr_flag[ SchedSelIdx ] = u_1 ( "VUI: cbr_flag" , s);
  246. }
  247. hrd->initial_cpb_removal_delay_length_minus1 = u_v ( 5,"VUI: initial_cpb_removal_delay_length_minus1" , s);
  248. hrd->cpb_removal_delay_length_minus1 = u_v ( 5,"VUI: cpb_removal_delay_length_minus1" , s);
  249. hrd->dpb_output_delay_length_minus1 = u_v ( 5,"VUI: dpb_output_delay_length_minus1" , s);
  250. hrd->time_offset_length = u_v ( 5,"VUI: time_offset_length" , s);
  251. return 0;
  252. }
  253. static void InterpretPPS (VideoParameters *p_Vid, DataPartition *p, pic_parameter_set_rbsp_t *pps)
  254. {
  255. unsigned i;
  256. unsigned n_ScalingList;
  257. int chroma_format_idc;
  258. int NumberBitsPerSliceGroupId;
  259. Bitstream *s = p->bitstream;
  260. assert (p != NULL);
  261. assert (p->bitstream != NULL);
  262. assert (p->bitstream->streamBuffer != 0);
  263. assert (pps != NULL);
  264. pps->pic_parameter_set_id = ue_v ("PPS: pic_parameter_set_id" , s);
  265. pps->seq_parameter_set_id = ue_v ("PPS: seq_parameter_set_id" , s);
  266. pps->entropy_coding_mode_flag = u_1 ("PPS: entropy_coding_mode_flag" , s);
  267. //! Note: as per JVT-F078 the following bit is unconditional. If F078 is not accepted, then
  268. //! one has to fetch the correct SPS to check whether the bit is present (hopefully there is
  269. //! no consistency problem :-(
  270. //! The current encoder code handles this in the same way. When you change this, don't forget
  271. //! the encoder! StW, 12/8/02
  272. pps->bottom_field_pic_order_in_frame_present_flag = u_1 ("PPS: bottom_field_pic_order_in_frame_present_flag" , s);
  273. pps->num_slice_groups_minus1 = ue_v ("PPS: num_slice_groups_minus1" , s);
  274. // FMO stuff begins here
  275. if (pps->num_slice_groups_minus1 > 0)
  276. {
  277. pps->slice_group_map_type = ue_v ("PPS: slice_group_map_type" , s);
  278. if (pps->slice_group_map_type == 0)
  279. {
  280. for (i=0; i<=pps->num_slice_groups_minus1; i++)
  281. pps->run_length_minus1 [i] = ue_v ("PPS: run_length_minus1 [i]" , s);
  282. }
  283. else if (pps->slice_group_map_type == 2)
  284. {
  285. for (i=0; i<pps->num_slice_groups_minus1; i++)
  286. {
  287. //! JVT-F078: avoid reference of SPS by using ue(v) instead of u(v)
  288. pps->top_left [i] = ue_v ("PPS: top_left [i]" , s);
  289. pps->bottom_right [i] = ue_v ("PPS: bottom_right [i]" , s);
  290. }
  291. }
  292. else if (pps->slice_group_map_type == 3 ||
  293. pps->slice_group_map_type == 4 ||
  294. pps->slice_group_map_type == 5)
  295. {
  296. pps->slice_group_change_direction_flag = u_1 ("PPS: slice_group_change_direction_flag" , s);
  297. pps->slice_group_change_rate_minus1 = ue_v ("PPS: slice_group_change_rate_minus1" , s);
  298. }
  299. else if (pps->slice_group_map_type == 6)
  300. {
  301. if (pps->num_slice_groups_minus1+1 >4)
  302. NumberBitsPerSliceGroupId = 3;
  303. else if (pps->num_slice_groups_minus1+1 > 2)
  304. NumberBitsPerSliceGroupId = 2;
  305. else
  306. NumberBitsPerSliceGroupId = 1;
  307. pps->pic_size_in_map_units_minus1 = ue_v ("PPS: pic_size_in_map_units_minus1" , s);
  308. if ((pps->slice_group_id = calloc (pps->pic_size_in_map_units_minus1+1, 1)) == NULL)
  309. no_mem_exit ("InterpretPPS: slice_group_id");
  310. for (i=0; i<=pps->pic_size_in_map_units_minus1; i++)
  311. pps->slice_group_id[i] = (byte) u_v (NumberBitsPerSliceGroupId, "slice_group_id[i]", s);
  312. }
  313. }
  314. // End of FMO stuff
  315. pps->num_ref_idx_l0_active_minus1 = ue_v ("PPS: num_ref_idx_l0_active_minus1" , s);
  316. pps->num_ref_idx_l1_active_minus1 = ue_v ("PPS: num_ref_idx_l1_active_minus1" , s);
  317. pps->weighted_pred_flag = u_1 ("PPS: weighted_pred_flag" , s);
  318. pps->weighted_bipred_idc = u_v ( 2, "PPS: weighted_bipred_idc" , s);
  319. pps->pic_init_qp_minus26 = se_v ("PPS: pic_init_qp_minus26" , s);
  320. pps->pic_init_qs_minus26 = se_v ("PPS: pic_init_qs_minus26" , s);
  321. pps->chroma_qp_index_offset = se_v ("PPS: chroma_qp_index_offset" , s);
  322. pps->deblocking_filter_control_present_flag = u_1 ("PPS: deblocking_filter_control_present_flag" , s);
  323. pps->constrained_intra_pred_flag = u_1 ("PPS: constrained_intra_pred_flag" , s);
  324. pps->redundant_pic_cnt_present_flag = u_1 ("PPS: redundant_pic_cnt_present_flag" , s);
  325. if(more_rbsp_data(s->streamBuffer, s->frame_bitoffset,s->bitstream_length)) // more_data_in_rbsp()
  326. {
  327. //Fidelity Range Extensions Stuff
  328. pps->transform_8x8_mode_flag = u_1 ("PPS: transform_8x8_mode_flag" , s);
  329. pps->pic_scaling_matrix_present_flag = u_1 ("PPS: pic_scaling_matrix_present_flag" , s);
  330. if(pps->pic_scaling_matrix_present_flag)
  331. {
  332. chroma_format_idc = p_Vid->SeqParSet[pps->seq_parameter_set_id].chroma_format_idc;
  333. n_ScalingList = 6 + ((chroma_format_idc != YUV444) ? 2 : 6) * pps->transform_8x8_mode_flag;
  334. for(i=0; i<n_ScalingList; i++)
  335. {
  336. pps->pic_scaling_list_present_flag[i]= u_1 ("PPS: pic_scaling_list_present_flag" , s);
  337. if(pps->pic_scaling_list_present_flag[i])
  338. {
  339. if(i<6)
  340. Scaling_List(pps->ScalingList4x4[i], 16, &pps->UseDefaultScalingMatrix4x4Flag[i], s);
  341. else
  342. Scaling_List(pps->ScalingList8x8[i-6], 64, &pps->UseDefaultScalingMatrix8x8Flag[i-6], s);
  343. }
  344. }
  345. }
  346. pps->second_chroma_qp_index_offset = se_v ("PPS: second_chroma_qp_index_offset" , s);
  347. }
  348. else
  349. {
  350. pps->second_chroma_qp_index_offset = pps->chroma_qp_index_offset;
  351. }
  352. pps->Valid = TRUE;
  353. }
  354. void PPSConsistencyCheck (pic_parameter_set_rbsp_t *pps)
  355. {
  356. printf ("Consistency checking a picture parset, to be implemented\n");
  357. // if (pps->seq_parameter_set_id invalid then do something)
  358. }
  359. void SPSConsistencyCheck (seq_parameter_set_rbsp_t *sps)
  360. {
  361. printf ("Consistency checking a sequence parset, to be implemented\n");
  362. }
  363. void MakePPSavailable (VideoParameters *p_Vid, int id, pic_parameter_set_rbsp_t *pps)
  364. {
  365. assert (pps->Valid == TRUE);
  366. if (p_Vid->PicParSet[id].Valid == TRUE && p_Vid->PicParSet[id].slice_group_id != NULL)
  367. free (p_Vid->PicParSet[id].slice_group_id);
  368. memcpy (&p_Vid->PicParSet[id], pps, sizeof (pic_parameter_set_rbsp_t));
  369. // we can simply use the memory provided with the pps. the PPS is destroyed after this function
  370. // call and will not try to free if pps->slice_group_id == NULL
  371. p_Vid->PicParSet[id].slice_group_id = pps->slice_group_id;
  372. pps->slice_group_id = NULL;
  373. }
  374. void CleanUpPPS(VideoParameters *p_Vid)
  375. {
  376. int i;
  377. for (i=0; i<MAXPPS; i++)
  378. {
  379. if (p_Vid->PicParSet[i].Valid == TRUE && p_Vid->PicParSet[i].slice_group_id != NULL)
  380. free (p_Vid->PicParSet[i].slice_group_id);
  381. p_Vid->PicParSet[i].Valid = FALSE;
  382. }
  383. }
  384. void MakeSPSavailable (VideoParameters *p_Vid, int id, seq_parameter_set_rbsp_t *sps)
  385. {
  386. assert (sps->Valid == TRUE);
  387. memcpy (&p_Vid->SeqParSet[id], sps, sizeof (seq_parameter_set_rbsp_t));
  388. }
  389. void ProcessSPS_Memory(VideoParameters *p_Vid, const void *buffer, size_t bufferlen)
  390. {
  391. }
  392. void ProcessSPS(VideoParameters *p_Vid, NALU_t *nalu)
  393. {
  394. DataPartition *dp = AllocPartition(1);
  395. seq_parameter_set_rbsp_t *sps = AllocSPS();
  396. //memcpy (dp->bitstream->streamBuffer, buffer, bufferlen);
  397. dp->bitstream->streamBuffer = &nalu->buf[1];
  398. dp->bitstream->code_len = dp->bitstream->bitstream_length = RBSPtoSODB (dp->bitstream->streamBuffer, nalu->len-1);
  399. dp->bitstream->read_len = dp->bitstream->frame_bitoffset = 0;
  400. InterpretSPS (p_Vid, dp, sps);
  401. if (sps->Valid)
  402. {
  403. if (p_Vid->active_sps)
  404. {
  405. if (sps->seq_parameter_set_id == p_Vid->active_sps->seq_parameter_set_id)
  406. {
  407. if (!sps_is_equal(sps, p_Vid->active_sps))
  408. {
  409. if (p_Vid->dec_picture)
  410. {
  411. // this may only happen on slice loss
  412. exit_picture(p_Vid, &p_Vid->dec_picture);
  413. }
  414. p_Vid->active_sps=NULL;
  415. }
  416. }
  417. }
  418. // SPSConsistencyCheck (pps);
  419. MakeSPSavailable (p_Vid, sps->seq_parameter_set_id, sps);
  420. p_Vid->profile_idc = sps->profile_idc;
  421. p_Vid->separate_colour_plane_flag = sps->separate_colour_plane_flag;
  422. if( p_Vid->separate_colour_plane_flag )
  423. {
  424. p_Vid->ChromaArrayType = 0;
  425. }
  426. else
  427. {
  428. p_Vid->ChromaArrayType = sps->chroma_format_idc;
  429. }
  430. }
  431. FreePartition (dp, 1);
  432. FreeSPS (sps);
  433. }
  434. void ProcessPPS (VideoParameters *p_Vid, NALU_t *nalu)
  435. {
  436. DataPartition *dp = AllocPartition(1);
  437. pic_parameter_set_rbsp_t *pps = AllocPPS();
  438. //memcpy (dp->bitstream->streamBuffer, &nalu->buf[1], nalu->len-1);
  439. dp->bitstream->streamBuffer = &nalu->buf[1];
  440. dp->bitstream->code_len = dp->bitstream->bitstream_length = RBSPtoSODB (dp->bitstream->streamBuffer, nalu->len-1);
  441. dp->bitstream->read_len = dp->bitstream->frame_bitoffset = 0;
  442. InterpretPPS (p_Vid, dp, pps);
  443. // PPSConsistencyCheck (pps);
  444. if (p_Vid->active_pps)
  445. {
  446. if (pps->pic_parameter_set_id == p_Vid->active_pps->pic_parameter_set_id)
  447. {
  448. if (!pps_is_equal(pps, p_Vid->active_pps))
  449. {
  450. if (p_Vid->dec_picture)
  451. {
  452. // this may only happen on slice loss
  453. exit_picture(p_Vid, &p_Vid->dec_picture);
  454. }
  455. p_Vid->active_pps = NULL;
  456. }
  457. }
  458. }
  459. MakePPSavailable (p_Vid, pps->pic_parameter_set_id, pps);
  460. FreePartition (dp, 1);
  461. FreePPS (pps);
  462. }
  463. /*!
  464. ************************************************************************
  465. * \brief
  466. * Updates images max values
  467. *
  468. ************************************************************************
  469. */
  470. static void updateMaxValue(FrameFormat *format)
  471. {
  472. format->max_value[0] = (1 << format->bit_depth[0]) - 1;
  473. format->max_value_sq[0] = format->max_value[0] * format->max_value[0];
  474. format->max_value[1] = (1 << format->bit_depth[1]) - 1;
  475. format->max_value_sq[1] = format->max_value[1] * format->max_value[1];
  476. format->max_value[2] = (1 << format->bit_depth[2]) - 1;
  477. format->max_value_sq[2] = format->max_value[2] * format->max_value[2];
  478. }
  479. /*!
  480. ************************************************************************
  481. * \brief
  482. * Reset format information
  483. *
  484. ************************************************************************
  485. */
  486. static void reset_format_info(seq_parameter_set_rbsp_t *sps, VideoParameters *p_Vid, FrameFormat *output)
  487. {
  488. InputParameters *p_Inp = p_Vid->p_Inp;
  489. static const int SubWidthC [4]= { 1, 2, 2, 1};
  490. static const int SubHeightC [4]= { 1, 2, 1, 1};
  491. int crop_left, crop_right;
  492. int crop_top, crop_bottom;
  493. // cropping for luma
  494. if (sps->frame_cropping_flag)
  495. {
  496. crop_left = SubWidthC [sps->chroma_format_idc] * sps->frame_cropping_rect_left_offset;
  497. crop_right = SubWidthC [sps->chroma_format_idc] * sps->frame_cropping_rect_right_offset;
  498. crop_top = SubHeightC[sps->chroma_format_idc] * ( 2 - sps->frame_mbs_only_flag ) * sps->frame_cropping_rect_top_offset;
  499. crop_bottom = SubHeightC[sps->chroma_format_idc] * ( 2 - sps->frame_mbs_only_flag ) * sps->frame_cropping_rect_bottom_offset;
  500. }
  501. else
  502. {
  503. crop_left = crop_right = crop_top = crop_bottom = 0;
  504. }
  505. output->width_crop = p_Vid->width - crop_left - crop_right;
  506. output->height_crop = p_Vid->height - crop_top - crop_bottom;
  507. output->width = p_Vid->width;
  508. output->height = p_Vid->height;
  509. output->width_cr = p_Vid->width_cr;
  510. output->height_cr = p_Vid->height_cr;
  511. // output size (excluding padding)
  512. output->size_cmp[0] = output->width * output->height;
  513. output->size_cmp[1] = output->width_cr * output->height_cr;
  514. output->size_cmp[2] = output->size_cmp[1];
  515. output->size = output->size_cmp[0] + output->size_cmp[1] + output->size_cmp[2];
  516. output->mb_width = output->width / MB_BLOCK_SIZE;
  517. output->mb_height = output->height / MB_BLOCK_SIZE;
  518. output->bit_depth[0] = p_Vid->bitdepth_luma;
  519. output->bit_depth[1] = p_Vid->bitdepth_chroma;
  520. output->bit_depth[2] = p_Vid->bitdepth_chroma;
  521. output->yuv_format = (ColorFormat) sps->chroma_format_idc;
  522. updateMaxValue(output);
  523. }
  524. /*!
  525. ************************************************************************
  526. * \brief
  527. * Activate Sequence Parameter Sets
  528. *
  529. ************************************************************************
  530. */
  531. void activate_sps (VideoParameters *p_Vid, seq_parameter_set_rbsp_t *sps)
  532. {
  533. InputParameters *p_Inp = p_Vid->p_Inp;
  534. if (p_Vid->active_sps != sps)
  535. {
  536. if (p_Vid->dec_picture)
  537. {
  538. // this may only happen on slice loss
  539. exit_picture(p_Vid, &p_Vid->dec_picture);
  540. }
  541. p_Vid->active_sps = sps;
  542. p_Vid->bitdepth_chroma = 0;
  543. p_Vid->width_cr = 0;
  544. p_Vid->height_cr = 0;
  545. // maximum vertical motion vector range in luma quarter pixel units
  546. if (p_Vid->active_sps->level_idc <= 10)
  547. {
  548. p_Vid->max_vmv_r = 64 * 4;
  549. }
  550. else if (p_Vid->active_sps->level_idc <= 20)
  551. {
  552. p_Vid->max_vmv_r = 128 * 4;
  553. }
  554. else if (p_Vid->active_sps->level_idc <= 30)
  555. {
  556. p_Vid->max_vmv_r = 256 * 4;
  557. }
  558. else
  559. {
  560. p_Vid->max_vmv_r = 512 * 4; // 512 pixels in quarter pixels
  561. }
  562. // Fidelity Range Extensions stuff (part 1)
  563. p_Vid->bitdepth_luma = (short) (sps->bit_depth_luma_minus8 + 8);
  564. p_Vid->bitdepth_scale[0] = 1 << sps->bit_depth_luma_minus8;
  565. if (sps->chroma_format_idc != YUV400)
  566. {
  567. p_Vid->bitdepth_chroma = (short) (sps->bit_depth_chroma_minus8 + 8);
  568. p_Vid->bitdepth_scale[1] = 1 << sps->bit_depth_chroma_minus8;
  569. }
  570. p_Vid->MaxFrameNum = 1<<(sps->log2_max_frame_num_minus4+4);
  571. p_Vid->PicWidthInMbs = (sps->pic_width_in_mbs_minus1 +1);
  572. p_Vid->PicHeightInMapUnits = (sps->pic_height_in_map_units_minus1 +1);
  573. p_Vid->FrameHeightInMbs = ( 2 - sps->frame_mbs_only_flag ) * p_Vid->PicHeightInMapUnits;
  574. p_Vid->FrameSizeInMbs = p_Vid->PicWidthInMbs * p_Vid->FrameHeightInMbs;
  575. p_Vid->yuv_format=sps->chroma_format_idc;
  576. p_Vid->width = p_Vid->PicWidthInMbs * MB_BLOCK_SIZE;
  577. p_Vid->height = p_Vid->FrameHeightInMbs * MB_BLOCK_SIZE;
  578. if (sps->chroma_format_idc == YUV420)
  579. {
  580. p_Vid->width_cr = (p_Vid->width >> 1);
  581. p_Vid->height_cr = (p_Vid->height >> 1);
  582. }
  583. else if (sps->chroma_format_idc == YUV422)
  584. {
  585. p_Vid->width_cr = (p_Vid->width >> 1);
  586. p_Vid->height_cr = p_Vid->height;
  587. }
  588. else if (sps->chroma_format_idc == YUV444)
  589. {
  590. //YUV444
  591. p_Vid->width_cr = p_Vid->width;
  592. p_Vid->height_cr = p_Vid->height;
  593. }
  594. init_frext(p_Vid);
  595. init_global_buffers(p_Vid);
  596. if (!p_Vid->no_output_of_prior_pics_flag)
  597. {
  598. flush_dpb(p_Vid);
  599. }
  600. init_dpb(p_Vid);
  601. ercInit(p_Vid, p_Vid->width, p_Vid->height, 1);
  602. image_cache_set_dimensions(&p_Vid->image_cache[0], p_Vid->width, p_Vid->height);
  603. image_cache_set_dimensions(&p_Vid->image_cache[1], p_Vid->width_cr, p_Vid->height_cr);
  604. motion_cache_set_dimensions(&p_Vid->motion_cache, p_Vid->width / BLOCK_SIZE, p_Vid->height / BLOCK_SIZE);
  605. }
  606. reset_format_info(sps, p_Vid, &p_Inp->output);
  607. }
  608. void activate_pps(VideoParameters *p_Vid, pic_parameter_set_rbsp_t *pps)
  609. {
  610. if (p_Vid->active_pps != pps)
  611. {
  612. if (p_Vid->dec_picture)
  613. {
  614. // this may only happen on slice loss
  615. exit_picture(p_Vid, &p_Vid->dec_picture);
  616. }
  617. p_Vid->active_pps = pps;
  618. // Fidelity Range Extensions stuff (part 2)
  619. p_Vid->Transform8x8Mode = pps->transform_8x8_mode_flag;
  620. }
  621. }
  622. void UseParameterSet (Slice *currSlice, int PicParsetId)
  623. {
  624. VideoParameters *p_Vid = currSlice->p_Vid;
  625. seq_parameter_set_rbsp_t *sps = &p_Vid->SeqParSet[p_Vid->PicParSet[PicParsetId].seq_parameter_set_id];
  626. pic_parameter_set_rbsp_t *pps = &p_Vid->PicParSet[PicParsetId];
  627. if (p_Vid->PicParSet[PicParsetId].Valid != TRUE)
  628. printf ("Trying to use an invalid (uninitialized) Picture Parameter Set with ID %d, expect the unexpected...\n", PicParsetId);
  629. if (p_Vid->SeqParSet[p_Vid->PicParSet[PicParsetId].seq_parameter_set_id].Valid != TRUE)
  630. printf ("PicParset %d references an invalid (uninitialized) Sequence Parameter Set with ID %d, expect the unexpected...\n", PicParsetId, (int) p_Vid->PicParSet[PicParsetId].seq_parameter_set_id);
  631. sps = &p_Vid->SeqParSet[p_Vid->PicParSet[PicParsetId].seq_parameter_set_id];
  632. // In theory, and with a well-designed software, the lines above
  633. // are everything necessary. In practice, we need to patch many values
  634. // in p_Vid-> (but no more in p_Inp-> -- these have been taken care of)
  635. // Sequence Parameter Set Stuff first
  636. // printf ("Using Picture Parameter set %d and associated Sequence Parameter Set %d\n", PicParsetId, p_Vid->PicParSet[PicParsetId].seq_parameter_set_id);
  637. if ((int) sps->pic_order_cnt_type < 0 || sps->pic_order_cnt_type > 2) // != 1
  638. {
  639. printf ("invalid sps->pic_order_cnt_type = %d\n", (int) sps->pic_order_cnt_type);
  640. error ("pic_order_cnt_type != 1", -1000);
  641. }
  642. if (sps->pic_order_cnt_type == 1)
  643. {
  644. if(sps->num_ref_frames_in_pic_order_cnt_cycle >= MAXnum_ref_frames_in_pic_order_cnt_cycle)
  645. {
  646. error("num_ref_frames_in_pic_order_cnt_cycle too large",-1011);
  647. }
  648. }
  649. activate_sps(p_Vid, sps);
  650. activate_pps(p_Vid, pps);
  651. // currSlice->dp_mode is set by read_new_slice (NALU first byte available there)
  652. if (pps->entropy_coding_mode_flag == CAVLC)
  653. {
  654. currSlice->nal_startcode_follows = uvlc_startcode_follows;
  655. }
  656. else
  657. {
  658. currSlice->nal_startcode_follows = cabac_startcode_follows;
  659. }
  660. }