You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

360 lines
6.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "h264.h"
  2. #include <cstdio>
  3. #include <cstring>
  4. namespace mtx {
  5. namespace h264 {
  6. namespace {
  7. // Exp-Golomb bit reader over an RBSP (emulation prevention bytes removed)
  8. class BitReader
  9. {
  10. public:
  11. explicit BitReader(const Bytes & d) : m_d(d) {}
  12. bool bit(uint32_t & v)
  13. {
  14. if (m_pos >= m_d.size() * 8)
  15. {
  16. return false;
  17. }
  18. v = (m_d[m_pos >> 3] >> (7 - (m_pos & 7))) & 1;
  19. m_pos++;
  20. return true;
  21. }
  22. bool bits(int n, uint32_t & v)
  23. {
  24. v = 0;
  25. for (int i = 0; i < n; i++)
  26. {
  27. uint32_t b;
  28. if (! bit(b))
  29. {
  30. return false;
  31. }
  32. v = (v << 1) | b;
  33. }
  34. return true;
  35. }
  36. bool ue(uint32_t & v)
  37. {
  38. int tZeros = 0;
  39. uint32_t b = 0;
  40. while (true)
  41. {
  42. if (! bit(b))
  43. {
  44. return false;
  45. }
  46. if (b)
  47. {
  48. break;
  49. }
  50. if (++tZeros > 31)
  51. {
  52. return false;
  53. }
  54. }
  55. uint32_t tRest = 0;
  56. if (! bits(tZeros, tRest))
  57. {
  58. return false;
  59. }
  60. v = ((1u << tZeros) - 1) + tRest;
  61. return true;
  62. }
  63. bool se(int32_t & v)
  64. {
  65. uint32_t u;
  66. if (! ue(u))
  67. {
  68. return false;
  69. }
  70. v = (u & 1) ? (int32_t) ((u + 1) / 2) : -(int32_t) (u / 2);
  71. return true;
  72. }
  73. private:
  74. const Bytes & m_d;
  75. size_t m_pos = 0;
  76. };
  77. Bytes rbsp(const Bytes & nal)
  78. {
  79. Bytes tOut;
  80. tOut.reserve(nal.size());
  81. int tZeros = 0;
  82. for (size_t i = 0; i < nal.size(); i++)
  83. {
  84. if (tZeros >= 2 && nal[i] == 3)
  85. {
  86. tZeros = 0;
  87. continue;
  88. }
  89. tZeros = (nal[i] == 0) ? tZeros + 1 : 0;
  90. tOut.push_back(nal[i]);
  91. }
  92. return tOut;
  93. }
  94. bool skipScalingList(BitReader & r, int size)
  95. {
  96. int32_t tLast = 8, tNext = 8;
  97. for (int j = 0; j < size; j++)
  98. {
  99. if (tNext != 0)
  100. {
  101. int32_t tDelta;
  102. if (! r.se(tDelta))
  103. {
  104. return false;
  105. }
  106. tNext = (tLast + tDelta + 256) % 256;
  107. }
  108. tLast = (tNext == 0) ? tLast : tNext;
  109. }
  110. return true;
  111. }
  112. } // namespace
  113. bool parseSps(const Bytes & sps, SpsInfo & info)
  114. {
  115. if (sps.size() < 4 || naluType(sps) != NALU_SPS)
  116. {
  117. return false;
  118. }
  119. Bytes tR = rbsp(sps);
  120. BitReader r(tR);
  121. uint32_t v, tForbidden;
  122. r.bits(8, tForbidden); // NAL header
  123. uint32_t tProfile, tConstr, tLevel;
  124. if (! r.bits(8, tProfile) || ! r.bits(8, tConstr) || ! r.bits(8, tLevel) || ! r.ue(v))
  125. {
  126. return false;
  127. }
  128. info.profileIdc = (int) tProfile;
  129. info.constraints = (int) tConstr;
  130. info.levelIdc = (int) tLevel;
  131. uint32_t tChroma = 1;
  132. if (tProfile == 100 || tProfile == 110 || tProfile == 122 || tProfile == 244 || tProfile == 44 || tProfile == 83 || tProfile == 86 || tProfile == 118 ||
  133. tProfile == 128 || tProfile == 138 || tProfile == 139 || tProfile == 134 || tProfile == 135)
  134. {
  135. if (! r.ue(tChroma))
  136. {
  137. return false;
  138. }
  139. if (tChroma == 3)
  140. {
  141. r.bit(v); // separate_colour_plane_flag
  142. }
  143. r.ue(v); // bit_depth_luma
  144. r.ue(v); // bit_depth_chroma
  145. r.bit(v); // qpprime_y_zero_transform_bypass
  146. uint32_t tScaling;
  147. if (! r.bit(tScaling))
  148. {
  149. return false;
  150. }
  151. if (tScaling)
  152. {
  153. int tCount = (tChroma != 3) ? 8 : 12;
  154. for (int i = 0; i < tCount; i++)
  155. {
  156. uint32_t tPresent;
  157. if (! r.bit(tPresent))
  158. {
  159. return false;
  160. }
  161. if (tPresent && ! skipScalingList(r, i < 6 ? 16 : 64))
  162. {
  163. return false;
  164. }
  165. }
  166. }
  167. }
  168. r.ue(v); // log2_max_frame_num_minus4
  169. uint32_t tPocType;
  170. if (! r.ue(tPocType))
  171. {
  172. return false;
  173. }
  174. if (tPocType == 0)
  175. {
  176. r.ue(v);
  177. }
  178. else if (tPocType == 1)
  179. {
  180. int32_t s;
  181. r.bit(v);
  182. r.se(s);
  183. r.se(s);
  184. uint32_t tCycle;
  185. if (! r.ue(tCycle) || tCycle > 255)
  186. {
  187. return false;
  188. }
  189. for (uint32_t i = 0; i < tCycle; i++)
  190. {
  191. r.se(s);
  192. }
  193. }
  194. r.ue(v); // max_num_ref_frames
  195. r.bit(v); // gaps_in_frame_num_allowed
  196. uint32_t tWmbs, tHmbs, tFrameMbsOnly;
  197. if (! r.ue(tWmbs) || ! r.ue(tHmbs) || ! r.bit(tFrameMbsOnly))
  198. {
  199. return false;
  200. }
  201. if (! tFrameMbsOnly)
  202. {
  203. r.bit(v); // mb_adaptive_frame_field
  204. }
  205. r.bit(v); // direct_8x8_inference
  206. uint32_t tCrop, cl = 0, cr = 0, ct = 0, cb = 0;
  207. if (! r.bit(tCrop))
  208. {
  209. return false;
  210. }
  211. if (tCrop)
  212. {
  213. r.ue(cl);
  214. r.ue(cr);
  215. r.ue(ct);
  216. r.ue(cb);
  217. }
  218. int tCropX = (tChroma == 0 || tChroma == 3) ? 1 : 2;
  219. int tCropY = ((tChroma == 1) ? 2 : 1) * (2 - (int) tFrameMbsOnly);
  220. info.width = (int) (tWmbs + 1) * 16 - (int) (cl + cr) * tCropX;
  221. info.height = (int) (2 - tFrameMbsOnly) * (int) (tHmbs + 1) * 16 - (int) (ct + cb) * tCropY;
  222. return info.width > 0 && info.height > 0;
  223. }
  224. std::string profileLevelId(const Bytes & sps)
  225. {
  226. if (sps.size() < 4)
  227. {
  228. return "42e01f";
  229. }
  230. char tB[8];
  231. snprintf(tB, sizeof(tB), "%02x%02x%02x", sps[1], sps[2], sps[3]);
  232. return tB;
  233. }
  234. static int b64val(char c)
  235. {
  236. if (c >= 'A' && c <= 'Z')
  237. {
  238. return c - 'A';
  239. }
  240. if (c >= 'a' && c <= 'z')
  241. {
  242. return c - 'a' + 26;
  243. }
  244. if (c >= '0' && c <= '9')
  245. {
  246. return c - '0' + 52;
  247. }
  248. if (c == '+' || c == '-')
  249. {
  250. return 62;
  251. }
  252. if (c == '/' || c == '_')
  253. {
  254. return 63;
  255. }
  256. return -1;
  257. }
  258. std::vector<Bytes> decodeSpropParameterSets(const char * sprop)
  259. {
  260. std::vector<Bytes> tOut;
  261. if (sprop == nullptr)
  262. {
  263. return tOut;
  264. }
  265. Bytes tCur;
  266. uint32_t tAcc = 0;
  267. int tBits = 0;
  268. for (const char * p = sprop;; p++)
  269. {
  270. if (*p == ',' || *p == 0)
  271. {
  272. if (! tCur.empty())
  273. {
  274. tOut.push_back(tCur);
  275. }
  276. tCur.clear();
  277. tAcc = 0;
  278. tBits = 0;
  279. if (*p == 0)
  280. {
  281. break;
  282. }
  283. continue;
  284. }
  285. int v = b64val(*p);
  286. if (v < 0)
  287. {
  288. continue; // '=' padding, spaces
  289. }
  290. tAcc = (tAcc << 6) | (uint32_t) v;
  291. tBits += 6;
  292. if (tBits >= 8)
  293. {
  294. tBits -= 8;
  295. tCur.push_back((uint8_t) ((tAcc >> tBits) & 0xff));
  296. }
  297. }
  298. return tOut;
  299. }
  300. Bytes toAnnexB(const std::vector<Bytes> & nalus)
  301. {
  302. Bytes tOut;
  303. for (const Bytes & n : nalus)
  304. {
  305. static const uint8_t tSc[4] = {0, 0, 0, 1};
  306. tOut.insert(tOut.end(), tSc, tSc + 4);
  307. tOut.insert(tOut.end(), n.begin(), n.end());
  308. }
  309. return tOut;
  310. }
  311. Bytes toAvcc(const std::vector<Bytes> & nalus)
  312. {
  313. Bytes tOut;
  314. for (const Bytes & n : nalus)
  315. {
  316. uint32_t s = (uint32_t) n.size();
  317. tOut.push_back((uint8_t) (s >> 24));
  318. tOut.push_back((uint8_t) (s >> 16));
  319. tOut.push_back((uint8_t) (s >> 8));
  320. tOut.push_back((uint8_t) s);
  321. tOut.insert(tOut.end(), n.begin(), n.end());
  322. }
  323. return tOut;
  324. }
  325. Bytes buildAvcC(const Bytes & sps, const Bytes & pps)
  326. {
  327. Bytes r;
  328. r.push_back(1);
  329. r.push_back(sps.size() > 1 ? sps[1] : 0x42);
  330. r.push_back(sps.size() > 2 ? sps[2] : 0xe0);
  331. r.push_back(sps.size() > 3 ? sps[3] : 0x1f);
  332. r.push_back(0xff); // 4 byte NAL lengths
  333. r.push_back(0xe1); // 1 SPS
  334. r.push_back((uint8_t) (sps.size() >> 8));
  335. r.push_back((uint8_t) sps.size());
  336. r.insert(r.end(), sps.begin(), sps.end());
  337. r.push_back(1); // 1 PPS
  338. r.push_back((uint8_t) (pps.size() >> 8));
  339. r.push_back((uint8_t) pps.size());
  340. r.insert(r.end(), pps.begin(), pps.end());
  341. return r;
  342. }
  343. } // namespace h264
  344. } // namespace mtx