gstdspparse.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. /*
  2. * Copyright (C) 2007-2010 Nokia Corporation
  3. * Copyright (C) 2009 Marco Ballesio
  4. *
  5. * Authors:
  6. * Juha Alanen <juha.m.alanen@nokia.com>
  7. * Marco Ballesio <marco.ballesio@nokia.com>
  8. * Felipe Contreras <felipe.contreras@nokia.com>
  9. *
  10. * This file may be used under the terms of the GNU Lesser General Public
  11. * License version 2.1, a copy of which is found in LICENSE included in the
  12. * packaging of this file.
  13. */
  14. #include <gst/gst.h>
  15. #include "gstdspbase.h"
  16. #include "gstdspvdec.h"
  17. #include "gstdspparse.h"
  18. #include "get_bits.h"
  19. static inline void
  20. set_framesize(GstDspBase *base,
  21. int width, int height,
  22. int par_num, int par_den,
  23. int crop_width, int crop_height)
  24. {
  25. GstDspVDec *vdec = GST_DSP_VDEC(base);
  26. gint w = crop_width ? crop_width : width;
  27. gint h = crop_height ? crop_height : height;
  28. /* update the framesize only if it hasn't been set yet or received wrong hxw through in-caps */
  29. if ((vdec->crop_width == 0 || vdec->crop_height == 0) ||
  30. (vdec->crop_width != w || vdec->crop_height != h)) {
  31. GstCaps *out_caps;
  32. GstStructure *struc;
  33. out_caps = base->tmp_caps;
  34. if (out_caps) {
  35. struc = gst_caps_get_structure(out_caps, 0);
  36. gst_structure_set(struc,
  37. "width", G_TYPE_INT, w,
  38. "height", G_TYPE_INT, h, NULL);
  39. if (par_num && par_den)
  40. gst_structure_set(struc,
  41. "pixel-aspect-ratio", GST_TYPE_FRACTION,
  42. par_num, par_den, NULL);
  43. }
  44. vdec->crop_width = w;
  45. vdec->crop_height = h;
  46. }
  47. if (vdec->color_format == GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'))
  48. base->output_buffer_size = width * height * 2;
  49. else
  50. base->output_buffer_size = width * height * 3 / 2;
  51. vdec->width = width;
  52. vdec->height = height;
  53. base->parsed = true;
  54. }
  55. bool gst_dsp_h263_parse(GstDspBase *base, GstBuffer *buf)
  56. {
  57. struct get_bit_context s;
  58. unsigned bits;
  59. unsigned type;
  60. bool baseline = true;
  61. int width, height;
  62. int par_num = 11, par_den = 12;
  63. struct size {
  64. int width;
  65. int height;
  66. } sizes[] = {
  67. { 0, 0 },
  68. { 128, 96 },
  69. { 176, 144 },
  70. { 352, 288 },
  71. { 704, 576 },
  72. { 1408, 1152 },
  73. };
  74. struct par {
  75. int num;
  76. int den;
  77. } pars[] = {
  78. { 0, 0 },
  79. { 1, 1 },
  80. { 12, 11 },
  81. { 10, 11 },
  82. { 16, 11 },
  83. { 40, 33 },
  84. };
  85. init_get_bits(&s, buf->data, buf->size * 8);
  86. if (get_bits_left(&s) < 38)
  87. goto not_enough;
  88. /* picture start code */
  89. if (get_bits(&s, 22) != 0x20)
  90. goto bail;
  91. /* temporal reference */
  92. skip_bits(&s, 8);
  93. type = get_bits(&s, 8) & 0x7;
  94. switch (type) {
  95. case 0:
  96. case 6:
  97. /* forbidden or reserved */
  98. goto bail;
  99. case 7: {
  100. unsigned custom_pf;
  101. bool extended_par = false;
  102. /* extended */
  103. baseline = false;
  104. if (get_bits_left(&s) < 54)
  105. goto not_enough;
  106. /* Updated Full Extended PTYPE */
  107. if (get_bits(&s, 3) != 1) {
  108. /* spec wise, should be present at start */
  109. goto bail;
  110. }
  111. type = get_bits(&s, 18) >> 15;
  112. if (type == 0 || type == 7) {
  113. goto bail;
  114. } else if (type != 6) {
  115. pr_debug(base, "format=%d", type);
  116. width = sizes[type].width;
  117. height = sizes[type].height;
  118. /* have all we need, exit */
  119. goto exit;
  120. }
  121. /* mandatory PLUSPTYPE part */
  122. skip_bits(&s, 9);
  123. /* CPM */
  124. if (get_bits1(&s)) {
  125. if (get_bits_left(&s) < 25)
  126. goto not_enough;
  127. /* PSBI */
  128. skip_bits(&s, 2);
  129. }
  130. custom_pf = get_bits(&s, 4);
  131. if (custom_pf == 0x0F) {
  132. extended_par = true;
  133. } else if (custom_pf && custom_pf < 6) {
  134. par_num = pars[custom_pf].num;
  135. par_den = pars[custom_pf].den;
  136. }
  137. bits = get_bits(&s, 19);
  138. height = (bits & 0x1FF) * 4;
  139. bits >>= 10;
  140. width = ((bits & 0x1FF) + 1) * 4;
  141. if (!extended_par)
  142. goto exit;
  143. if (get_bits_left(&s) < 16)
  144. goto not_enough;
  145. bits = get_bits(&s, 16);
  146. if (bits) {
  147. par_num = bits >> 8;
  148. par_den = bits & 0x0F;
  149. }
  150. break;
  151. }
  152. default:
  153. /* regular */
  154. pr_debug(base, "format=%d", type);
  155. width = sizes[type].width;
  156. height = sizes[type].height;
  157. if (get_bits_left(&s) < 11)
  158. goto not_enough;
  159. /* optional PTYPE part */
  160. baseline = !(get_bits(&s, 5) & 1);
  161. /* PQUANT */
  162. skip_bits(&s, 5);
  163. /* CPM */
  164. baseline = baseline && !get_bits1(&s);
  165. break;
  166. }
  167. exit:
  168. /* TODO use to decide on node */
  169. pr_debug(base, "baseline=%u", baseline);
  170. pr_debug(base, "width=%u, height=%u, par=%d:%d", width, height,
  171. par_num, par_den);
  172. set_framesize(base, width, height, par_num, par_den, 0, 0);
  173. return true;
  174. not_enough:
  175. pr_err(base, "not enough data");
  176. bail:
  177. return false;
  178. }
  179. static inline bool mpeg4_next_start_code(struct get_bit_context *s)
  180. {
  181. if (get_bits_left(s) < 8 - (int) s->index % 8)
  182. goto failed;
  183. if (get_bits1(s))
  184. goto failed;
  185. while (s->index % 8 != 0) {
  186. if (!get_bits1(s))
  187. goto failed;
  188. }
  189. return true;
  190. failed:
  191. return false;
  192. }
  193. static inline bool mpeg4_skip_user_data(struct get_bit_context *s, unsigned *bits)
  194. {
  195. while (*bits == 0x1B2) {
  196. do {
  197. unsigned b;
  198. if (get_bits_left(s) < 8)
  199. goto failed;
  200. b = get_bits(s, 8);
  201. *bits = (*bits << 8) | b;
  202. } while ((*bits >> 8) != 0x1);
  203. }
  204. return true;
  205. failed:
  206. return false;
  207. }
  208. bool gst_dsp_mpeg4_parse(GstDspBase *base, GstBuffer *buf)
  209. {
  210. struct get_bit_context s;
  211. unsigned bits;
  212. int time_increment_resolution;
  213. int width, height;
  214. unsigned ar;
  215. init_get_bits(&s, buf->data, buf->size * 8);
  216. if (get_bits_left(&s) < 32)
  217. goto failed;
  218. /* Expect Visual Object Sequence startcode (0x000001B0) */
  219. bits = get_bits(&s, 32);
  220. if (bits != 0x1B0) {
  221. unsigned i;
  222. pr_debug(base, "MPEG4 data does not start with VOSH, locating VOS");
  223. /* find Video Object startcode and take it from there */
  224. for (i = 4; i < buf->size; i++) {
  225. if (G_UNLIKELY(bits <= 0x11F)) {
  226. pr_debug(base, "VOS start code at offset %d", i - 4);
  227. init_get_bits(&s, buf->data + i - 4, (buf->size - i + 4) * 8);
  228. goto VOS;
  229. }
  230. bits = (bits << 8) | buf->data[i];
  231. }
  232. goto failed;
  233. }
  234. if (get_bits_left(&s) < 40)
  235. goto failed;
  236. /* profile and level indication */
  237. bits = get_bits(&s, 8);
  238. pr_debug(base, "profile id: %d", bits);
  239. if (!bits)
  240. pr_debug(base, "invalid profile id; carrying on nevertheless");
  241. /* Expect Visual Object startcode (0x000001B5) */
  242. bits = get_bits(&s, 32);
  243. /* but skip optional user data */
  244. if (!mpeg4_skip_user_data(&s, &bits))
  245. goto failed;
  246. if (bits != 0x1B5)
  247. goto failed;
  248. if (get_bits_left(&s) < 6)
  249. goto failed;
  250. if (get_bits1(&s)) {
  251. if (get_bits_left(&s) < 12)
  252. goto failed;
  253. /* Skip visual_object_verid and priority */
  254. skip_bits(&s, 7);
  255. }
  256. /* Only support video ID */
  257. if (get_bits(&s, 4) != 1)
  258. goto failed;
  259. /* video signal type */
  260. if (get_bits1(&s)) {
  261. if (get_bits_left(&s) < 5)
  262. goto failed;
  263. /* video signal type, ignore format and range */
  264. skip_bits(&s, 4);
  265. if (get_bits1(&s)) {
  266. if (get_bits_left(&s) < 24)
  267. goto failed;
  268. /* ignore color description */
  269. skip_bits(&s, 24);
  270. }
  271. }
  272. if (!mpeg4_next_start_code(&s))
  273. goto failed;
  274. VOS:
  275. if (get_bits_left(&s) < 32)
  276. goto failed;
  277. /* expecting a video object startcode */
  278. bits = get_bits(&s, 32);
  279. /* skip optional user data */
  280. if (!mpeg4_skip_user_data(&s, &bits))
  281. goto failed;
  282. if (bits > 0x11F)
  283. goto failed;
  284. if (get_bits_left(&s) < 47)
  285. goto failed;
  286. /* expecting a video object layer startcode */
  287. bits = get_bits(&s, 32);
  288. if (bits < 0x120 || bits > 0x12F)
  289. goto failed;
  290. /* ignore random accessible vol and video object type indication */
  291. skip_bits(&s, 9);
  292. if (get_bits1(&s)) {
  293. if (get_bits_left(&s) < 12)
  294. goto failed;
  295. /* skip video object layer verid and priority */
  296. skip_bits(&s, 7);
  297. }
  298. /* aspect ratio */
  299. ar = get_bits(&s, 4);
  300. if (ar == 0) {
  301. goto failed;
  302. } else if (ar == 0xf) {
  303. /* info is extended par */
  304. if (get_bits_left(&s) < 17)
  305. goto failed;
  306. /* aspect_ratio_width */
  307. skip_bits(&s, 8);
  308. /* aspect_ratio_height */
  309. skip_bits(&s, 8);
  310. } else if (ar < 0x6) {
  311. /* TODO get aspect ratio width and height from aspect ratio table */
  312. }
  313. if (get_bits1(&s)) {
  314. if (get_bits_left(&s) < 4)
  315. goto failed;
  316. /* vol control parameters, skip chroma and low delay */
  317. skip_bits(&s, 3);
  318. if (get_bits1(&s)) {
  319. if (get_bits_left(&s) < 79)
  320. goto failed;
  321. /* skip vbv_parameters */
  322. skip_bits(&s, 79);
  323. }
  324. }
  325. if (get_bits_left(&s) < 21)
  326. goto failed;
  327. /* layer shape */
  328. if (get_bits(&s, 2))
  329. /* only support rectangular */
  330. goto failed;
  331. if (!get_bits1(&s)) /* marker bit */
  332. goto failed;
  333. time_increment_resolution = get_bits(&s, 16);
  334. if (!get_bits1(&s)) /* marker bit */
  335. goto failed;
  336. if (get_bits1(&s)) {
  337. /* fixed time increment */
  338. int n;
  339. /*
  340. * Length of the time increment is the minimal number of bits
  341. * needed to represent time_increment_resolution.
  342. */
  343. for (n = 0; time_increment_resolution >> n; n++)
  344. ;
  345. if (get_bits_left(&s) < n)
  346. goto failed;
  347. skip_bits(&s, n);
  348. }
  349. /* assuming rectangular shape */
  350. if (get_bits_left(&s) < 29)
  351. goto failed;
  352. if (!get_bits1(&s)) /* marker bit */
  353. goto failed;
  354. width = get_bits(&s, 13);
  355. if (!width)
  356. goto failed;
  357. if (!get_bits1(&s)) /* marker bit */
  358. goto failed;
  359. height = get_bits(&s, 13);
  360. if (!height)
  361. goto failed;
  362. if (!get_bits1(&s)) /* marker bit */
  363. goto failed;
  364. pr_debug(base, "width=%u, height=%u", width, height);
  365. {
  366. /* scan for user_data DivX marker */
  367. GstDspVDec *vdec = GST_DSP_VDEC(base);
  368. if (memmem(buf->data, buf->size, "\0\0\1\262DivX", 8)) {
  369. pr_debug(base, "DivX marker found");
  370. vdec->priv.mpeg4.is_divx = TRUE;
  371. }
  372. /* but maybe it is XviD, and perhaps we don't mind that */
  373. if (memmem(buf->data, buf->size, "\0\0\1\262XviD", 8)) {
  374. pr_debug(base, "also XviD marker found");
  375. vdec->priv.mpeg4.is_divx = FALSE;
  376. }
  377. }
  378. set_framesize(base, width, height, 0, 0, 0, 0);
  379. return true;
  380. failed:
  381. return false;
  382. }
  383. static unsigned read_bits(struct get_bit_context *s, int n)
  384. {
  385. n = MIN(n, get_bits_left(s));
  386. if (n == 0)
  387. return 0;
  388. return get_bits(s, n);
  389. }
  390. /* read unsigned Exp-Golomb code */
  391. static unsigned get_ue_golomb(struct get_bit_context *s)
  392. {
  393. unsigned i;
  394. for (i = 0; i < 32; i++) {
  395. if (read_bits(s, 1) != 0)
  396. break;
  397. if (get_bits_left(s) <= 0)
  398. break;
  399. }
  400. return (1 << i) - 1 + read_bits(s, i);
  401. }
  402. /* read signed Exp-Golomb code */
  403. static int get_se_golomb(struct get_bit_context *s)
  404. {
  405. int i = 0;
  406. i = get_ue_golomb(s);
  407. /* (-1)^(i+1) Ceil (i / 2) */
  408. i = (i + 1) / 2 * (i & 1 ? 1 : -1);
  409. return i;
  410. }
  411. #define CHECK_EOS(s) \
  412. do { \
  413. if (get_bits_left(s) <= 0) \
  414. goto not_enough_data; \
  415. } while (0)
  416. /* remove emulation prevention bytes (if needed) */
  417. static bool rbsp_unescape(uint8_t *b, unsigned len, uint8_t **ret, unsigned *ret_len)
  418. {
  419. unsigned i, si, di;
  420. uint8_t *dst;
  421. for (i = 0; i + 3 < len; i++) {
  422. if (b[i] == 0 && b[i + 1] == 0 && b[i + 2] <= 3) {
  423. if (b[i + 2] != 3)
  424. /* startcode */
  425. len = i;
  426. break;
  427. }
  428. }
  429. if (i >= len - 3)
  430. return false;
  431. /* escaped */
  432. dst = malloc(len);
  433. memcpy(dst, b, i);
  434. si = di = i;
  435. while (si + 2 < len) {
  436. if (b[si] == 0 && b[si + 1] == 0 && b[si + 2] == 3) {
  437. dst[di++] = 0;
  438. dst[di++] = 0;
  439. si += 3;
  440. } else {
  441. dst[di++] = b[si++];
  442. }
  443. }
  444. while (si < len)
  445. dst[di++] = b[si++];
  446. *ret = dst;
  447. *ret_len = di;
  448. return true;
  449. }
  450. bool gst_dsp_h264_parse(GstDspBase *base, GstBuffer *buf)
  451. {
  452. GstDspVDec *vdec = GST_DSP_VDEC(base);
  453. struct get_bit_context s;
  454. guint8 b, profile, chroma, frame;
  455. guint fc_top, fc_bottom, fc_left, fc_right;
  456. gint width, height;
  457. gint crop_width, crop_height;
  458. guint subwc[] = { 1, 2, 2, 1 }, subhc[] = { 1, 2, 1, 1 };
  459. guint32 d;
  460. bool avc;
  461. uint8_t *rbsp_buffer = NULL;
  462. unsigned rbsp_len;
  463. init_get_bits(&s, buf->data, buf->size * 8);
  464. if (base->parsed) {
  465. avc = vdec->priv.h264.is_avc;
  466. goto try_again;
  467. }
  468. /* auto-detect whether avc or byte-stream;
  469. * as unconvential codec-data cases contain bytestream NALs */
  470. if (get_bits_left(&s) < 32)
  471. goto not_enough_data;
  472. d = get_bits(&s, 32);
  473. avc = (d != 1 && (d >> 8) != 1);
  474. try_again:
  475. pr_debug(base, "avc codec_data: %d", avc);
  476. if (avc) {
  477. unsigned tsize;
  478. /* provided buffer is then codec_data */
  479. if (get_bits_left(&s) < 32)
  480. goto not_enough_data;
  481. /* configuration version == 1 */
  482. if (buf->data[0] != 1)
  483. return FALSE;
  484. /* reserved */
  485. d = get_bits(&s, 8);
  486. if ((d & 0xfc) != 0xfc)
  487. return FALSE;
  488. d = get_bits(&s, 8);
  489. if ((d & 0xe0) != 0xe0)
  490. pr_debug(base, "unexpected parameter in codec_data, never minding");
  491. /* number of SPS */
  492. if ((d & 0x1f) == 0) {
  493. pr_debug(base, "invalid parameters in codec_data");
  494. return false;
  495. }
  496. tsize = get_bits(&s, 16);
  497. } else {
  498. s.index = 0;
  499. /* frame size is recorded in Sequence Parameter Set (SPS) */
  500. /* locate SPS NAL unit in bytestream */
  501. while (get_bits_left(&s) >= 32) {
  502. uint32_t d = show_bits(&s, 32);
  503. if ((d >> 8 == 0x1) && ((d & 0x1F) == 0x07))
  504. break;
  505. skip_bits(&s, 8);
  506. }
  507. if (get_bits_left(&s) < 32)
  508. goto bail;
  509. skip_bits(&s, 24);
  510. }
  511. /* pointing at NAL SPS, now analyze it */
  512. if (get_bits_left(&s) < 40) {
  513. if (avc && !base->parsed) {
  514. avc = false;
  515. goto try_again;
  516. } else {
  517. goto not_enough_data;
  518. }
  519. }
  520. if (rbsp_unescape(buf->data + (get_bits_count(&s) >> 3),
  521. get_bits_left(&s) >> 3,
  522. &rbsp_buffer, &rbsp_len))
  523. {
  524. /* reinitialize bitreader */
  525. init_get_bits(&s, rbsp_buffer, rbsp_len << 3);
  526. }
  527. b = get_bits(&s, 8);
  528. /* forbidden bit */
  529. if (b & 0x80)
  530. goto bail;
  531. /* need SPS NAL unit */
  532. if ((b & 0x1f) != 0x07)
  533. goto bail;
  534. profile = get_bits(&s, 8);
  535. if (get_bits_left(&s) < 16)
  536. goto not_enough_data;
  537. skip_bits(&s, 16);
  538. /* seq_parameter_set_id */
  539. get_ue_golomb(&s);
  540. CHECK_EOS(&s);
  541. if (profile == 100 || profile == 110 || profile == 122 || profile == 244 ||
  542. profile == 44 || profile == 83 || profile == 86)
  543. {
  544. int scp_flag = 0;
  545. /* chroma_format_idc */
  546. chroma = get_ue_golomb(&s);
  547. CHECK_EOS(&s);
  548. if (chroma == 3) {
  549. /* separate_colour_plane_flag */
  550. if (get_bits_left(&s) < 1)
  551. goto not_enough_data;
  552. scp_flag = get_bits1(&s);
  553. }
  554. /* bit_depth_luma_minus8 */
  555. get_ue_golomb(&s);
  556. CHECK_EOS(&s);
  557. /* bit_depth_chroma_minus8 */
  558. get_ue_golomb(&s);
  559. CHECK_EOS(&s);
  560. if (get_bits_left(&s) < 2)
  561. goto not_enough_data;
  562. /* qpprime_y_zero_transform_bypass_flag */
  563. skip_bits(&s, 1);
  564. /* seq_scaling_matrix_present_flag */
  565. if (get_bits1(&s)) {
  566. int i, j, m;
  567. m = (chroma != 3) ? 8 : 12;
  568. for (i = 0; i < m; i++) {
  569. if (get_bits_left(&s) < 1)
  570. goto not_enough_data;
  571. /* seq_scaling_list_present_flag[i] */
  572. if (get_bits1(&s)) {
  573. int last_scale = 8, next_scale = 8, delta_scale;
  574. j = (i < 6) ? 16 : 64;
  575. for (; j > 0; j--) {
  576. if (next_scale) {
  577. delta_scale = get_se_golomb(&s);
  578. CHECK_EOS(&s);
  579. next_scale = (last_scale + delta_scale + 256) % 256;
  580. }
  581. if (next_scale)
  582. last_scale = next_scale;
  583. }
  584. }
  585. }
  586. }
  587. if (scp_flag)
  588. chroma = 0;
  589. } else {
  590. /* inferred value */
  591. chroma = 1;
  592. }
  593. /* log2_max_frame_num_minus4 */
  594. get_ue_golomb(&s);
  595. CHECK_EOS(&s);
  596. /* pic_order_cnt_type */
  597. b = get_ue_golomb(&s);
  598. CHECK_EOS(&s);
  599. if (b == 0) {
  600. /* log2_max_pic_order_cnt_lsb_minus4 */
  601. get_ue_golomb(&s);
  602. CHECK_EOS(&s);
  603. } else if (b == 1) {
  604. if (get_bits_left(&s) < 1)
  605. goto not_enough_data;
  606. /* delta_pic_order_always_zero_flag */
  607. skip_bits(&s, 1);
  608. /* offset_for_non_ref_pic */
  609. get_ue_golomb(&s);
  610. CHECK_EOS(&s);
  611. /* offset_for_top_to_bottom_field */
  612. get_ue_golomb(&s);
  613. CHECK_EOS(&s);
  614. /* num_ref_frames_in_pic_order_cnt_cycle */
  615. d = get_ue_golomb(&s);
  616. CHECK_EOS(&s);
  617. for (; d > 0; d--) {
  618. /* offset_for_ref_frame[i] */
  619. get_ue_golomb(&s);
  620. CHECK_EOS(&s);
  621. }
  622. }
  623. /* num_ref_frames */
  624. get_ue_golomb(&s);
  625. CHECK_EOS(&s);
  626. /* gaps_in_frame_num_value_allowed_flag */
  627. read_bits(&s, 1);
  628. CHECK_EOS(&s);
  629. /* pic_width_in_mbs_minus1 */
  630. width = get_ue_golomb(&s) + 1;
  631. width *= 16;
  632. /* pic_height_in_map_units_minus1 */
  633. height = get_ue_golomb(&s) + 1;
  634. CHECK_EOS(&s);
  635. /* frame_mbs_only_flag */
  636. frame = read_bits(&s, 1);
  637. CHECK_EOS(&s);
  638. height *= 16 * (2 - frame);
  639. if (!frame) {
  640. /* mb_adaptive_frame_field_flag */
  641. read_bits(&s, 1);
  642. CHECK_EOS(&s);
  643. }
  644. /* direct_8x8_inference_flag */
  645. read_bits(&s, 1);
  646. CHECK_EOS(&s);
  647. /* frame_cropping_flag */
  648. b = read_bits(&s, 1);
  649. CHECK_EOS(&s);
  650. if (b) {
  651. fc_left = get_ue_golomb(&s);
  652. CHECK_EOS(&s);
  653. fc_right = get_ue_golomb(&s);
  654. CHECK_EOS(&s);
  655. fc_top = get_ue_golomb(&s);
  656. CHECK_EOS(&s);
  657. fc_bottom = get_ue_golomb(&s);
  658. CHECK_EOS(&s);
  659. } else
  660. fc_left = fc_right = fc_top = fc_bottom = 0;
  661. pr_debug(base, "initial width=%d, height=%d", width, height);
  662. pr_debug(base, "crop (%d,%d)(%d,%d)",
  663. fc_left, fc_top, fc_right, fc_bottom);
  664. if (chroma > 3) {
  665. if (!base->parsed)
  666. pr_err(base, "invalid SPS");
  667. goto bail;
  668. }
  669. crop_width = width - (fc_left + fc_right) * subwc[chroma];
  670. crop_height = height - (fc_top + fc_bottom) * subhc[chroma] * (2 - frame);
  671. if (width < 0 || height < 0 || crop_width < 0 || crop_height < 0) {
  672. if (!base->parsed)
  673. pr_err(base, "invalid SPS");
  674. goto bail;
  675. }
  676. pr_debug(base, "final width=%u, height=%u", crop_width, crop_height);
  677. vdec->priv.h264.is_avc = avc;
  678. set_framesize(base, width, height, 0, 0, crop_width, crop_height);
  679. free(rbsp_buffer);
  680. return true;
  681. not_enough_data:
  682. if (!base->parsed)
  683. pr_err(base, "not enough data");
  684. bail:
  685. free(rbsp_buffer);
  686. return false;
  687. }