PageRenderTime 18ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Play/ps.js

http://github.com/mbebenita/Broadway
JavaScript | 234 lines | 195 code | 20 blank | 19 comment | 37 complexity | 7eec353789c8f8685896378b500f84bc MD5 | raw file
Possible License(s): BSD-3-Clause
  1. 'use strict';
  2. /**
  3. * Represents a Sequence Parameter Set (SPS)
  4. *
  5. * Clause 7.4.2.2
  6. */
  7. var SPS = (function() {
  8. function constructor() {};
  9. constructor.prototype.decode = function (stream) {
  10. traceln("| + Sequence Parameter Set");
  11. this.profile_idc = stream.readBits(8);
  12. traceln("| | profile_idc: " + this.profile_idc);
  13. this.constrained_set0_flag = stream.readBit();
  14. traceln("| | constrained_set0_flag: " + this.constrained_set0_flag);
  15. this.constrained_set1_flag = stream.readBit();
  16. traceln("| | constrained_set1_flag: " + this.constrained_set1_flag);
  17. this.constrained_set2_flag = stream.readBit();
  18. traceln("| | constrained_set2_flag: " + this.constrained_set2_flag);
  19. assert(stream.readBits(5) == 0);
  20. this.level_idc = stream.readBits(8);
  21. traceln("| | level_idc: " + this.level_idc);
  22. assertRange(this.level_idc, 0, 51);
  23. assert(mapLev2Idx[this.level_idc] != 255);
  24. this.seq_parameter_set_id = stream.uev();
  25. traceln("| | seq_parameter_set_id: " + this.seq_parameter_set_id);
  26. assertRange(this.seq_parameter_set_id, 0, 31);
  27. this.log2_max_frame_num_minus4 = stream.uev();
  28. traceln("| | log2_max_frame_num_minus4: " + this.log2_max_frame_num_minus4);
  29. assertRange(this.log2_max_frame_num_minus4, 0, 12);
  30. this.pic_order_cnt_type = stream.uev();
  31. traceln("| | pic_order_cnt_type: " + this.pic_order_cnt_type);
  32. if (this.pic_order_cnt_type == 0) {
  33. this.log2_max_pic_order_cnt_lsb_minus4 = stream.uev();
  34. traceln("| | log2_max_pic_order_cnt_lsb_minus4: " + this.log2_max_pic_order_cnt_lsb_minus4);
  35. } else if (this.pic_order_cnt_type == 1) {
  36. this.delta_pic_order_always_zero_flag = stream.readBit();
  37. traceln("| | delta_pic_order_always_zero_flag: " + this.delta_pic_order_always_zero_flag);
  38. this.offset_for_non_ref_pic = stream.sev32();
  39. traceln("| | offset_for_non_ref_pic: " + this.offset_for_non_ref_pic);
  40. this.offset_for_top_to_bottom_field = stream.sev32();
  41. traceln("| | offset_for_top_to_bottom_field: " + this.offset_for_top_to_bottom_field);
  42. this.num_ref_frames_in_pic_order_cnt_cycle = stream.uev();
  43. traceln("| | num_ref_frames_in_pic_order_cnt_cycle: " + this.num_ref_frames_in_pic_order_cnt_cycle);
  44. this.offset_for_ref_frame = [];
  45. for (var i = 0; i < this.num_ref_frames_in_pic_order_cnt_cycle; i++) {
  46. offset_for_ref_frame[i] = stream.sev32();
  47. traceln("| | offset_for_ref_frame[ " + i + "]: " + this.offset_for_ref_frame[i]);
  48. }
  49. }
  50. this.num_ref_frames = stream.uev();
  51. traceln("| | num_ref_frames: " + this.num_ref_frames);
  52. assertRange(this.num_ref_frames, 0, 16);
  53. this.gaps_in_frame_num_value_allowed_flag = stream.readBit();
  54. traceln("| | gaps_in_frame_num_value_allowed_flag: " + this.gaps_in_frame_num_value_allowed_flag);
  55. this.pic_width_in_mbs_minus1 = stream.uev();
  56. traceln("| | pic_width_in_mbs_minus1: " + this.pic_width_in_mbs_minus1);
  57. this.pic_height_in_map_units_minus1 = stream.uev();
  58. traceln("| | pic_height_in_map_units_minus1: " + this.pic_height_in_map_units_minus1);
  59. this.frame_mbs_only_flag = stream.readBit();
  60. traceln("| | frame_mbs_only_flag: " + this.frame_mbs_only_flag);
  61. this.mb_adaptive_frame_field_flag = 0;
  62. if (!this.frame_mbs_only_flag) {
  63. this.mb_adaptive_frame_field_flag = stream.readBit();
  64. traceln("| | mb_adaptive_frame_field_flag: " + this.mb_adaptive_frame_field_flag);
  65. }
  66. this.direct_8x8_inference_flag = stream.readBit();
  67. traceln("| | direct_8x8_inference_flag: " + this.direct_8x8_inference_flag);
  68. this.frame_cropping_flag = stream.readBit();
  69. traceln("| | frame_cropping_flag: " + this.frame_cropping_flag);
  70. this.frame_crop_left_offset = 0;
  71. this.frame_crop_right_offset = 0;
  72. this.frame_crop_top_offset = 0;
  73. this.frame_crop_bottom_offset = 0;
  74. if (this.frame_cropping_flag) {
  75. this.frame_crop_left_offset = stream.uev();
  76. traceln("| | frame_crop_left_offset: " + this.frame_crop_left_offset);
  77. this.frame_crop_right_offset = stream.uev();
  78. traceln("| | frame_crop_right_offset: " + this.frame_crop_right_offset);
  79. this.frame_crop_top_offset = stream.uev();
  80. traceln("| | frame_crop_top_offset: " + this.frame_crop_top_offset);
  81. this.frame_crop_bottom_offset = stream.uev();
  82. traceln("| | frame_crop_bottom_offset: " + this.frame_crop_bottom_offset);
  83. }
  84. this.vui_parameters_present_flag = stream.readBit();
  85. traceln("| | vui_parameters_present_flag: " + this.vui_parameters_present_flag);
  86. if (this.vui_parameters_present_flag) {
  87. unexpected();
  88. }
  89. decoder.SequenceParameterSets[this.seq_parameter_set_id] = this;
  90. };
  91. constructor.prototype.toString = function () {
  92. return "SPS: " + getProperties(this, true);
  93. };
  94. return constructor;
  95. })();
  96. /**
  97. * Represents a Picture Parameter Set (PPS)
  98. *
  99. * Clause 7.4.2.2
  100. *
  101. * Book 5.5, Parameter sets remain inactive, until they are activated when referenced in slice headers. Slice
  102. * headers activate PPSs which in turn activate SPSs.
  103. */
  104. var PPS = (function() {
  105. function constructor() { }
  106. constructor.prototype.decode = function (stream) {
  107. traceln("| + Picture Parameter Set");
  108. this.pic_parameter_set_id = stream.uev();
  109. traceln("| | pic_parameter_set_id: " + this.pic_parameter_set_id);
  110. assertRange(this.pic_parameter_set_id, 0, 255);
  111. /* Register Picture Parameter Set */
  112. decoder.PictureParameterSets[this.pic_parameter_set_id] = this;
  113. this.seq_parameter_set_id = stream.uev();
  114. traceln("| | seq_parameter_set_id: " + this.seq_parameter_set_id);
  115. assertRange(this.seq_parameter_set_id, 0, 31);
  116. this.entropy_coding_mode_flag = stream.readBit();
  117. traceln("| | entropy_coding_mode_flag: " + this.entropy_coding_mode_flag);
  118. /* Only CAVLC (entropy_coding_mode_flag == false) is supported. */
  119. if (this.entropy_coding_mode_flag) {
  120. unexpected();
  121. }
  122. this.pic_order_present_flag = stream.readBit();
  123. traceln("| | pic_order_present_flag: " + this.pic_order_present_flag);
  124. this.num_slice_groups_minus1 = stream.uev();
  125. traceln("| | num_slice_groups_minus1: " + this.num_slice_groups_minus1);
  126. if (this.num_slice_groups_minus1 > MAX_NUM_SLICE_GROUP - 1) {
  127. unexpected();
  128. }
  129. this.slice_group_change_rate_minus1 = 0;
  130. if (this.num_slice_groups_minus1 > 0) {
  131. this.slice_group_map_type = stream.uev();
  132. traceln("| | slice_group_map_type: " + this.slice_group_map_type);
  133. if (this.slice_group_map_type == 0) {
  134. this.run_length_minus1 = [];
  135. for (var i = 0; i <= this.num_slice_groups_minus1; i++) {
  136. this.run_length_minus1[i] = stream.uev();
  137. traceln("| | run_length_minus1[" + i + "]: " + this.run_length_minus1[i]);
  138. }
  139. } else if (this.slice_group_map_type == 2) {
  140. this.top_left = [];
  141. this.bottom_right = [];
  142. for ( var i = 0; i < this.num_slice_groups_minus1; i++) {
  143. this.top_left[i] = stream.uev();
  144. traceln("| | top_left[" + i + "]: " + this.top_left[i]);
  145. this.bottom_right[i] = stream.uev();
  146. traceln("| | bottom_right[" + i + "]: " + this.bottom_right[i]);
  147. }
  148. } else if (this.slice_group_map_type == 3
  149. || this.slice_group_map_type == 4
  150. || this.slice_group_map_type == 5) {
  151. this.slice_group_change_direction_flag = stream.readBit();
  152. traceln("| | slice_group_change_direction_flag: " + this.slice_group_change_direction_flag);
  153. this.slice_group_change_rate_minus1 = stream.uev();
  154. traceln("| | slice_group_change_rate_minus1: " + this.slice_group_change_rate_minus1);
  155. } else if (this.slice_group_map_type == 6) {
  156. this.pic_size_in_map_units_minus1 = stream.uev();
  157. traceln("| | pic_size_in_map_units_minus1: " + this.pic_size_in_map_units_minus1);
  158. /* ceil(log2(num_slice_groups_minus1+1)) bits */
  159. var numBits = 0;
  160. var i = this.num_slice_groups_minus1;
  161. while (i > 0) {
  162. numBits++;
  163. i >>>= 1;
  164. }
  165. var sps = decoder.SequenceParameterSets[this.seq_parameter_set_id];
  166. if (sps == null) {
  167. unexpected();
  168. }
  169. var picWidthInMbs = sps.pic_width_in_mbs_minus1 + 1;
  170. var picHeightInMapUnits = sps.pic_height_in_map_units_minus1 + 1;
  171. var picSizeInMapUnits = picWidthInMbs * picHeightInMapUnits;
  172. /* information has to be consistent with the seq_param */
  173. if (this.pic_size_in_map_units_minus1 != picSizeInMapUnits - 1) {
  174. unexpected();
  175. }
  176. this.slice_group_id = [];
  177. for (i = 0; i < picSizeInMapUnits; i++) {
  178. this.slice_group_id[i] = stream.readBits(numBits);
  179. traceln("| | slice_group_id[" + i + "]: " + this.slice_group_id[i]);
  180. }
  181. }
  182. }
  183. /* Number of reference pictures in listX. */
  184. this.num_ref_idx_l0_active_minus1 = stream.uev();
  185. traceln("| | num_ref_idx_l0_active_minus1: " + this.num_ref_idx_l0_active_minus1);
  186. assertRange(this.num_ref_idx_l0_active_minus1, 0, 31);
  187. this.num_ref_idx_l1_active_minus1 = stream.uev();
  188. traceln("| | num_ref_idx_l1_active_minus1: " + this.num_ref_idx_l1_active_minus1);
  189. assertRange(this.num_ref_idx_l1_active_minus1, 0, 31);
  190. this.weighted_pred_flag = stream.readBit();
  191. traceln("| | weighted_pred_flag: " + this.weighted_pred_flag);
  192. this.weighted_bipred_idc = stream.readBits(2);
  193. traceln("| | weighted_bipred_idc: " + this.weighted_bipred_idc);
  194. assertRange(this.weighted_bipred_idc, 0, 3);
  195. this.pic_init_qp_minus26 = stream.sev();
  196. traceln("| | pic_init_qp_minus26: " + this.pic_init_qp_minus26);
  197. assertRange(this.pic_init_qp_minus26, -26, 25);
  198. this.pic_init_qs_minus26 = stream.sev();
  199. traceln("| | pic_init_qs_minus26: " + this.pic_init_qs_minus26);
  200. assertRange(this.pic_init_qs_minus26, -26, 25);
  201. this.chroma_qp_index_offset = stream.sev();
  202. traceln("| | chroma_qp_index_offset: " + this.chroma_qp_index_offset);
  203. assertRange(this.chroma_qp_index_offset, -12, 12);
  204. this.pic_parameter_set_id = stream.readBits(3);
  205. // traceln("| | pic_parameter_set_id: " + this.pic_parameter_set_id);
  206. this.deblocking_filter_control_present_flag = this.pic_parameter_set_id >> 2;
  207. this.constrained_intra_pred_flag = (this.pic_parameter_set_id >> 1) & 1;
  208. this.redundant_pic_cnt_present_flag = this.pic_parameter_set_id & 1;
  209. };
  210. constructor.prototype.toString = function () {
  211. return "PPS: " + getProperties(this, true);
  212. };
  213. return constructor;
  214. })();