summaryrefslogtreecommitdiffstats
path: root/content/common/gpu/media/vaapi_h264_decoder.cc
blob: b459ebbcc0e1864e59a793f63099f1f8b6df4aa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <dlfcn.h>

#include <algorithm>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/stl_util.h"
#include "content/common/gpu/gl_scoped_binders.h"
#include "content/common/gpu/media/vaapi_h264_decoder.h"
#include "third_party/libva/va/va.h"
#include "third_party/libva/va/va_x11.h"
#include "ui/gl/gl_bindings.h"

#define VA_LOG_ON_ERROR(va_res, err_msg)                   \
  do {                                                     \
    if ((va_res) != VA_STATUS_SUCCESS) {                   \
      DVLOG(1) << err_msg                                  \
               << " VA error: " << VAAPI_ErrorStr(va_res); \
    }                                                      \
  } while (0)

#define VA_SUCCESS_OR_RETURN(va_res, err_msg, ret)         \
  do {                                                     \
    if ((va_res) != VA_STATUS_SUCCESS) {                   \
      DVLOG(1) << err_msg                                  \
               << " VA error: " << VAAPI_ErrorStr(va_res); \
      return (ret);                                        \
    }                                                      \
  } while (0)

namespace content {

void *vaapi_handle = NULL;
void *vaapi_x11_handle = NULL;

typedef VADisplay (*VaapiGetDisplay)(Display *dpy);
typedef int (*VaapiDisplayIsValid)(VADisplay dpy);
typedef VAStatus (*VaapiInitialize)(VADisplay dpy,
                                    int *major_version,
                                    int *minor_version);
typedef VAStatus (*VaapiTerminate)(VADisplay dpy);
typedef VAStatus (*VaapiGetConfigAttributes)(VADisplay dpy,
                                             VAProfile profile,
                                             VAEntrypoint entrypoint,
                                             VAConfigAttrib *attrib_list,
                                             int num_attribs);
typedef VAStatus (*VaapiCreateConfig)(VADisplay dpy,
                                      VAProfile profile,
                                      VAEntrypoint entrypoint,
                                      VAConfigAttrib *attrib_list,
                                      int num_attribs,
                                      VAConfigID *config_id);
typedef VAStatus (*VaapiDestroyConfig)(VADisplay dpy, VAConfigID config_id);
typedef VAStatus (*VaapiCreateSurfaces)(VADisplay dpy,
                                        int width,
                                        int height,
                                        int format,
                                        int num_surfaces,
                                        VASurfaceID *surfaces);
typedef VAStatus (*VaapiDestroySurfaces)(VADisplay dpy,
                                         VASurfaceID *surfaces,
                                         int num_surfaces);
typedef VAStatus (*VaapiCreateContext)(VADisplay dpy,
                                       VAConfigID config_id,
                                       int picture_width,
                                       int picture_height,
                                       int flag,
                                       VASurfaceID *render_targets,
                                       int num_render_targets,
                                       VAContextID *context);
typedef VAStatus (*VaapiDestroyContext)(VADisplay dpy, VAContextID context);
typedef VAStatus (*VaapiPutSurface)(VADisplay dpy,
                                    VASurfaceID surface,
                                    Drawable draw,
                                    short srcx,
                                    short srcy,
                                    unsigned short srcw,
                                    unsigned short srch,
                                    short destx,
                                    short desty,
                                    unsigned short destw,
                                    unsigned short desth,
                                    VARectangle *cliprects,
                                    unsigned int number_cliprects,
                                    unsigned int flags);
typedef VAStatus (*VaapiSyncSurface)(VADisplay dpy, VASurfaceID render_target);
typedef VAStatus (*VaapiBeginPicture)(VADisplay dpy,
                                      VAContextID context,
                                      VASurfaceID render_target);
typedef VAStatus (*VaapiRenderPicture)(VADisplay dpy,
                                       VAContextID context,
                                       VABufferID *buffers,
                                       int num_buffers);
typedef VAStatus (*VaapiEndPicture)(VADisplay dpy, VAContextID context);
typedef VAStatus (*VaapiCreateBuffer)(VADisplay dpy,
                                      VAContextID context,
                                      VABufferType type,
                                      unsigned int size,
                                      unsigned int num_elements,
                                      void *data,
                                      VABufferID *buf_id);
typedef VAStatus (*VaapiDestroyBuffer)(VADisplay dpy, VABufferID buffer_id);
typedef const char* (*VaapiErrorStr)(VAStatus error_status);

#define VAAPI_SYM(name, handle) Vaapi##name VAAPI_##name = NULL

VAAPI_SYM(GetDisplay, vaapi_x11_handle);
VAAPI_SYM(DisplayIsValid, vaapi_handle);
VAAPI_SYM(Initialize, vaapi_handle);
VAAPI_SYM(Terminate, vaapi_handle);
VAAPI_SYM(GetConfigAttributes, vaapi_handle);
VAAPI_SYM(CreateConfig, vaapi_handle);
VAAPI_SYM(DestroyConfig, vaapi_handle);
VAAPI_SYM(CreateSurfaces, vaapi_handle);
VAAPI_SYM(DestroySurfaces, vaapi_handle);
VAAPI_SYM(CreateContext, vaapi_handle);
VAAPI_SYM(DestroyContext, vaapi_handle);
VAAPI_SYM(PutSurface, vaapi_x11_handle);
VAAPI_SYM(SyncSurface, vaapi_x11_handle);
VAAPI_SYM(BeginPicture, vaapi_handle);
VAAPI_SYM(RenderPicture, vaapi_handle);
VAAPI_SYM(EndPicture, vaapi_handle);
VAAPI_SYM(CreateBuffer, vaapi_handle);
VAAPI_SYM(DestroyBuffer, vaapi_handle);
VAAPI_SYM(ErrorStr, vaapi_handle);

// static
bool VaapiH264Decoder::pre_sandbox_init_done_ = false;

class VaapiH264Decoder::DecodeSurface {
 public:
  DecodeSurface(const GLXFBConfig& fb_config,
                Display* x_display,
                VADisplay va_display,
                const base::Callback<bool(void)>& make_context_current,
                VASurfaceID va_surface_id,
                int32 picture_buffer_id,
                uint32 texture_id,
                int width, int height);
  ~DecodeSurface();

  VASurfaceID va_surface_id() {
    return va_surface_id_;
  }

  int32 picture_buffer_id() {
    return picture_buffer_id_;
  }

  uint32 texture_id() {
    return texture_id_;
  }

  bool available() {
    return available_;
  }

  int32 input_id() {
    return input_id_;
  }

  int poc() {
    return poc_;
  }

  // Associate the surface with |input_id| and |poc|, and make it unavailable
  // (in use).
  void Acquire(int32 input_id, int poc);

  // Make this surface available, ready to be reused.
  void Release();

  // Has to be called before output to sync texture contents.
  // Returns true if successful.
  bool Sync();

 private:
  Display* x_display_;
  VADisplay va_display_;
  base::Callback<bool(void)> make_context_current_;
  VASurfaceID va_surface_id_;

  // Client-provided ids.
  int32 input_id_;
  int32 picture_buffer_id_;
  uint32 texture_id_;

  int width_;
  int height_;

  // Available for decoding (data no longer used for reference or output).
  bool available_;

  // PicOrderCount
  int poc_;

  // Pixmaps bound to this texture.
  Pixmap x_pixmap_;
  GLXPixmap glx_pixmap_;

  DISALLOW_COPY_AND_ASSIGN(DecodeSurface);
};

VaapiH264Decoder::DecodeSurface::DecodeSurface(
    const GLXFBConfig& fb_config,
    Display* x_display,
    VADisplay va_display,
    const base::Callback<bool(void)>& make_context_current,
    VASurfaceID va_surface_id,
    int32 picture_buffer_id,
    uint32 texture_id,
    int width, int height)
    : x_display_(x_display),
      va_display_(va_display),
      make_context_current_(make_context_current),
      va_surface_id_(va_surface_id),
      input_id_(0),
      picture_buffer_id_(picture_buffer_id),
      texture_id_(texture_id),
      width_(width),
      height_(height),
      available_(false),
      poc_(0),
      x_pixmap_(0),
      glx_pixmap_(0) {
  // Bind the surface to a texture of the given width and height,
  // allocating pixmaps as needed.
  if (!make_context_current_.Run())
    return;

  content::ScopedTextureBinder texture_binder(texture_id_);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

  XWindowAttributes win_attr;
  int screen = DefaultScreen(x_display_);
  XGetWindowAttributes(x_display_, RootWindow(x_display_, screen), &win_attr);
  //TODO(posciak): pass the depth required by libva, not the RootWindow's depth
  x_pixmap_ = XCreatePixmap(x_display_, RootWindow(x_display_, screen),
                            width_, height_, win_attr.depth);
  if (!x_pixmap_) {
    DVLOG(1) << "Failed creating an X Pixmap for TFP";
    return;
  }

  static const int pixmap_attr[] = {
    GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
    GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGB_EXT,
    GL_NONE,
  };

  glx_pixmap_ = glXCreatePixmap(x_display_, fb_config, x_pixmap_, pixmap_attr);
  if (!glx_pixmap_) {
    // x_pixmap_ will be freed in the destructor.
    DVLOG(1) << "Failed creating a GLX Pixmap for TFP";
    return;
  }

  glXBindTexImageEXT(x_display_, glx_pixmap_, GLX_FRONT_LEFT_EXT, NULL);

  available_ = true;
}

VaapiH264Decoder::DecodeSurface::~DecodeSurface() {
  // Unbind surface from texture and deallocate resources.
  if (glx_pixmap_ && make_context_current_.Run()) {
    glXReleaseTexImageEXT(x_display_, glx_pixmap_, GLX_FRONT_LEFT_EXT);
    glXDestroyPixmap(x_display_, glx_pixmap_);
  }

  if (x_pixmap_)
    XFreePixmap(x_display_, x_pixmap_);
}

void VaapiH264Decoder::DecodeSurface::Acquire(int32 input_id, int poc) {
  DCHECK_EQ(available_, true);
  available_ = false;
  input_id_ = input_id;
  poc_ = poc;
}

void VaapiH264Decoder::DecodeSurface::Release() {
  available_ = true;
}

bool VaapiH264Decoder::DecodeSurface::Sync() {
  if (!make_context_current_.Run())
    return false;

  // Put the decoded data into XPixmap bound to the texture.
  VAStatus va_res = VAAPI_PutSurface(va_display_,
                                     va_surface_id_, x_pixmap_,
                                     0, 0, width_, height_,
                                     0, 0, width_, height_,
                                     NULL, 0, 0);
  VA_SUCCESS_OR_RETURN(va_res, "Failed putting decoded picture to texture",
                       false);

  // Wait for the data to be put into the buffer so it'd ready for output.
  va_res = VAAPI_SyncSurface(va_display_, va_surface_id_);
  VA_SUCCESS_OR_RETURN(va_res, "Failed syncing decoded picture", false);

  return true;
}

VaapiH264Decoder::VaapiH264Decoder() {
  Reset();
  curr_input_id_ = -1;
  x_display_ = NULL;
  fb_config_ = NULL;
  va_display_ = NULL;
  curr_sps_id_ = -1;
  curr_pps_id_ = -1;
  pic_width_ = -1;
  pic_height_ = -1;
  max_frame_num_ = 0;
  max_pic_num_ = 0;
  max_long_term_frame_idx_ = 0;
  max_pic_order_cnt_lsb_ = 0;
  state_ = kUninitialized;
  num_available_decode_surfaces_ = 0;
  va_context_created_ = false;
}

VaapiH264Decoder::~VaapiH264Decoder() {
  Destroy();
}

// This puts the decoder in state where it keeps stream data and is ready
// to resume playback from a random location in the stream, but drops all
// inputs and outputs and makes all surfaces available for use.
void VaapiH264Decoder::Reset() {
  frame_ready_at_hw_ = false;

  curr_pic_.reset();

  frame_num_ = 0;
  prev_frame_num_ = -1;
  prev_frame_num_offset_ = -1;

  prev_ref_has_memmgmnt5_ = false;
  prev_ref_top_field_order_cnt_ = -1;
  prev_ref_pic_order_cnt_msb_ = -1;
  prev_ref_pic_order_cnt_lsb_ = -1;
  prev_ref_field_ = H264Picture::FIELD_NONE;

  // When called from the constructor, although va_display_ is invalid,
  // |pending_slice_bufs_| and |pending_va_bufs_| are empty.
  DestroyPendingBuffers();

  pending_slice_bufs_ = std::queue<VABufferID>();
  pending_va_bufs_ = std::queue<VABufferID>();

  ref_pic_list0_.clear();
  ref_pic_list1_.clear();

  for (POCToDecodeSurfaces::iterator it = poc_to_decode_surfaces_.begin();
       it != poc_to_decode_surfaces_.end(); ) {
    int poc = it->second->poc();
    // Must be incremented before UnassignSurfaceFromPoC as this call
    // invalidates |it|.
    ++it;
    DecodeSurface *dec_surface = UnassignSurfaceFromPoC(poc);
    if (dec_surface) {
      dec_surface->Release();
      ++num_available_decode_surfaces_;
    }
  }
  DCHECK(poc_to_decode_surfaces_.empty());

  dpb_.Clear();
  parser_.Reset();

  // Still initialized and ready to decode, unless called from constructor,
  // which will change it back.
  state_ = kAfterReset;
}

void VaapiH264Decoder::Destroy() {
  if (state_ == kUninitialized)
    return;

  VAStatus va_res;
  bool destroy_surfaces = false;
  switch (state_) {
    case kDecoding:
    case kAfterReset:
    case kError:
      destroy_surfaces = true;
      // fallthrough
    case kInitialized:
      if (!make_context_current_.Run())
        break;
      if (destroy_surfaces)
        DestroyVASurfaces();
      DestroyPendingBuffers();
      va_res = VAAPI_DestroyConfig(va_display_, va_config_id_);
      VA_LOG_ON_ERROR(va_res, "vaDestroyConfig failed");
      va_res = VAAPI_Terminate(va_display_);
      VA_LOG_ON_ERROR(va_res, "vaTerminate failed");
      // fallthrough
    case kUninitialized:
      break;
  }

  state_ = kUninitialized;
}

// Maps Profile enum values to VaProfile values.
bool VaapiH264Decoder::SetProfile(media::VideoCodecProfile profile) {
  switch (profile) {
    case media::H264PROFILE_BASELINE:
      profile_ = VAProfileH264Baseline;
      break;
    case media::H264PROFILE_MAIN:
      profile_ = VAProfileH264Main;
      break;
    case media::H264PROFILE_HIGH:
      profile_ = VAProfileH264High;
      break;
    default:
      return false;
  }
  return true;
}

class ScopedPtrXFree {
 public:
  void operator()(void* x) const {
    ::XFree(x);
  }
};

bool VaapiH264Decoder::InitializeFBConfig() {
  const int fbconfig_attr[] = {
    GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
    GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_BIT_EXT,
    GLX_BIND_TO_TEXTURE_RGB_EXT, GL_TRUE,
    GLX_Y_INVERTED_EXT, GL_TRUE,
    GL_NONE,
  };

  int num_fbconfigs;
  scoped_ptr_malloc<GLXFBConfig, ScopedPtrXFree> glx_fb_configs(
      glXChooseFBConfig(x_display_, DefaultScreen(x_display_), fbconfig_attr,
                        &num_fbconfigs));
  if (!glx_fb_configs.get())
    return false;
  if (!num_fbconfigs)
    return false;

  fb_config_ = glx_fb_configs.get()[0];
  return true;
}

bool VaapiH264Decoder::Initialize(
    media::VideoCodecProfile profile,
    Display* x_display,
    GLXContext glx_context,
    const base::Callback<bool(void)>& make_context_current,
    const OutputPicCB& output_pic_cb) {
  DCHECK_EQ(state_, kUninitialized);

  output_pic_cb_ = output_pic_cb;

  x_display_ = x_display;
  make_context_current_ = make_context_current;

  if (!make_context_current_.Run())
    return false;

  if (!SetProfile(profile)) {
    DVLOG(1) << "Unsupported profile";
    return false;
  }

  if (!InitializeFBConfig()) {
    DVLOG(1) << "Could not get a usable FBConfig";
    return false;
  }

  va_display_ = VAAPI_GetDisplay(x_display_);
  if (!VAAPI_DisplayIsValid(va_display_)) {
    DVLOG(1) << "Could not get a valid VA display";
    return false;
  }

  int major_version, minor_version;
  VAStatus va_res;
  va_res = VAAPI_Initialize(va_display_, &major_version, &minor_version);
  VA_SUCCESS_OR_RETURN(va_res, "vaInitialize failed", false);
  DVLOG(1) << "VAAPI version: " << major_version << "." << minor_version;

  VAConfigAttrib attrib;
  attrib.type = VAConfigAttribRTFormat;

  VAEntrypoint entrypoint = VAEntrypointVLD;
  va_res = VAAPI_GetConfigAttributes(va_display_, profile_, entrypoint,
                                     &attrib, 1);
  VA_SUCCESS_OR_RETURN(va_res, "vaGetConfigAttributes failed", false);

  if (!(attrib.value & VA_RT_FORMAT_YUV420)) {
    DVLOG(1) << "YUV420 not supported";
    return false;
  }

  va_res = VAAPI_CreateConfig(va_display_, profile_, entrypoint,
                              &attrib, 1, &va_config_id_);
  VA_SUCCESS_OR_RETURN(va_res, "vaCreateConfig failed", false);

  state_ = kInitialized;
  return true;
}

void VaapiH264Decoder::ReusePictureBuffer(int32 picture_buffer_id) {
  DecodeSurfaces::iterator it = decode_surfaces_.find(picture_buffer_id);
  if (it == decode_surfaces_.end() || it->second->available()) {
    DVLOG(1) << "Asked to reuse an invalid/already available surface";
    return;
  }
  it->second->Release();
  ++num_available_decode_surfaces_;
}

bool VaapiH264Decoder::AssignPictureBuffer(int32 picture_buffer_id,
                                           uint32 texture_id) {
  DCHECK_EQ(state_, kDecoding);

  if (decode_surfaces_.size() >= GetRequiredNumOfPictures()) {
    DVLOG(1) << "Got more surfaces than required";
    return false;
  }

  // This will not work if we start using VDA.DismissPicture()
  linked_ptr<DecodeSurface> dec_surface(new DecodeSurface(
      fb_config_, x_display_, va_display_, make_context_current_,
      va_surface_ids_[decode_surfaces_.size()], picture_buffer_id, texture_id,
      pic_width_, pic_height_));
  if (!dec_surface->available()) {
    DVLOG(1) << "Error creating a decoding surface (binding to texture?)";
    return false;
  }

  DVLOG(2) << "New picture assigned, texture id: " << dec_surface->texture_id()
           << " pic buf id: " << dec_surface->picture_buffer_id()
           << " will use va surface " << dec_surface->va_surface_id();

  bool inserted = decode_surfaces_.insert(std::make_pair(picture_buffer_id,
                                                         dec_surface)).second;
  DCHECK(inserted);
  ++num_available_decode_surfaces_;

  return true;
}

bool VaapiH264Decoder::CreateVASurfaces() {
  DCHECK_NE(pic_width_, -1);
  DCHECK_NE(pic_height_, -1);
  if (state_ == kAfterReset)
    return true;
  DCHECK_EQ(state_, kInitialized);

  // Allocate VASurfaces in driver.
  VAStatus va_res = VAAPI_CreateSurfaces(va_display_, pic_width_,
                                         pic_height_, VA_RT_FORMAT_YUV420,
                                         GetRequiredNumOfPictures(),
                                         va_surface_ids_);
  VA_SUCCESS_OR_RETURN(va_res, "vaCreateSurfaces failed", false);

  DCHECK(decode_surfaces_.empty());
  // And create a context associated with them.
  va_res = VAAPI_CreateContext(va_display_, va_config_id_,
                               pic_width_, pic_height_, VA_PROGRESSIVE,
                               va_surface_ids_, GetRequiredNumOfPictures(),
                               &va_context_id_);

  if (va_res != VA_STATUS_SUCCESS) {
    DVLOG(1) << "Error creating a decoding surface (binding to texture?)";
    VAAPI_DestroySurfaces(va_display_, va_surface_ids_,
                          GetRequiredNumOfPictures());
    return false;
  }

  va_context_created_ = true;

  return true;
}

void VaapiH264Decoder::DestroyVASurfaces() {
  DCHECK(state_ == kDecoding || state_ == kError || state_ == kAfterReset);
  decode_surfaces_.clear();

  // This can happen if we fail during DecodeInitial.
  if (!va_context_created_)
    return;

  VAStatus va_res = VAAPI_DestroyContext(va_display_, va_context_id_);
  VA_LOG_ON_ERROR(va_res, "vaDestroyContext failed");

  va_res = VAAPI_DestroySurfaces(va_display_, va_surface_ids_,
                                 GetRequiredNumOfPictures());
  VA_LOG_ON_ERROR(va_res, "vaDestroySurfaces failed");

  va_context_created_ = false;
}

void VaapiH264Decoder::DestroyPendingBuffers() {
  while (!pending_slice_bufs_.empty()) {
    VABufferID buffer = pending_slice_bufs_.front();
    VAStatus va_res = VAAPI_DestroyBuffer(va_display_, buffer);
    VA_LOG_ON_ERROR(va_res, "vaDestroyBuffer failed");
    pending_slice_bufs_.pop();
  }
  while (!pending_va_bufs_.empty()) {
    VABufferID buffer = pending_va_bufs_.front();
    VAStatus va_res = VAAPI_DestroyBuffer(va_display_, buffer);
    VA_LOG_ON_ERROR(va_res, "vaDestroyBuffer failed");
    pending_va_bufs_.pop();
  }
}

// Fill |va_pic| with default/neutral values.
static void InitVAPicture(VAPictureH264* va_pic) {
  memset(va_pic, 0, sizeof(*va_pic));
  va_pic->picture_id = VA_INVALID_ID;
  va_pic->flags = VA_PICTURE_H264_INVALID;
}

void VaapiH264Decoder::FillVAPicture(VAPictureH264 *va_pic, H264Picture* pic) {
  POCToDecodeSurfaces::iterator iter = poc_to_decode_surfaces_.find(
      pic->pic_order_cnt);
  if (iter == poc_to_decode_surfaces_.end()) {
    DVLOG(1) << "Could not find surface with POC: " << pic->pic_order_cnt;
    // Cannot provide a ref picture, will corrupt output, but may be able
    // to recover.
    InitVAPicture(va_pic);
    return;
  }

  va_pic->picture_id = iter->second->va_surface_id();
  va_pic->frame_idx = pic->frame_num;
  va_pic->flags = 0;

  switch (pic->field) {
    case H264Picture::FIELD_NONE:
      break;
    case H264Picture::FIELD_TOP:
      va_pic->flags |= VA_PICTURE_H264_TOP_FIELD;
      break;
    case H264Picture::FIELD_BOTTOM:
      va_pic->flags |= VA_PICTURE_H264_BOTTOM_FIELD;
      break;
  }

  if (pic->ref) {
    va_pic->flags |= pic->long_term ? VA_PICTURE_H264_LONG_TERM_REFERENCE
                                    : VA_PICTURE_H264_SHORT_TERM_REFERENCE;
  }

  va_pic->TopFieldOrderCnt = pic->top_field_order_cnt;
  va_pic->BottomFieldOrderCnt = pic->bottom_field_order_cnt;
}

int VaapiH264Decoder::FillVARefFramesFromDPB(VAPictureH264 *va_pics,
                                             int num_pics) {
  H264DPB::Pictures::reverse_iterator rit;
  int i;

  // Return reference frames in reverse order of insertion.
  // Libva does not document this, but other implementations (e.g. mplayer)
  // do it this way as well.
  for (rit = dpb_.rbegin(), i = 0; rit != dpb_.rend() && i < num_pics; ++rit) {
    if ((*rit)->ref)
      FillVAPicture(&va_pics[i++], *rit);
  }

  return i;
}

// Can only be called when all surfaces are already bound
// to textures (cannot be run at the same time as AssignPictureBuffer).
bool VaapiH264Decoder::AssignSurfaceToPoC(int poc) {
  DCHECK_GT(num_available_decode_surfaces_, 0) << decode_surfaces_.size();

  // Find a surface not currently holding data used for reference and/or
  // to be displayed and mark it as used.
  DecodeSurfaces::iterator iter = decode_surfaces_.begin();
  for (; iter != decode_surfaces_.end(); ++iter) {
    if (!iter->second->available())
      continue;

    --num_available_decode_surfaces_;
    DCHECK_GE(num_available_decode_surfaces_, 0);

    // Associate with input id and poc and mark as unavailable.
    iter->second->Acquire(curr_input_id_, poc);
    DVLOG(4) << "Will use surface " << iter->second->va_surface_id()
             << " for POC " << iter->second->poc()
             << " input ID: " << iter->second->input_id();
    bool inserted = poc_to_decode_surfaces_.insert(std::make_pair(
        poc, iter->second.get())).second;
    DCHECK(inserted);
    return true;
  }

  // Could not find an available surface.
  return false;
}

// Can only be called when all surfaces are already bound
// to textures (cannot be run at the same time as AssignPictureBuffer).
VaapiH264Decoder::DecodeSurface* VaapiH264Decoder::UnassignSurfaceFromPoC(
    int poc) {
  DecodeSurface* dec_surface;
  POCToDecodeSurfaces::iterator it = poc_to_decode_surfaces_.find(poc);
  if (it == poc_to_decode_surfaces_.end()) {
    DVLOG(1) << "Asked to unassign an unassigned POC";
    return NULL;
  }
  dec_surface = it->second;
  DVLOG(4) << "POC " << poc << " no longer using surface "
           << dec_surface->va_surface_id();
  poc_to_decode_surfaces_.erase(it);
  return dec_surface;
}

// Fill a VAPictureParameterBufferH264 to be later sent to the HW decoder.
bool VaapiH264Decoder::SendPPS() {
  const H264PPS* pps = parser_.GetPPS(curr_pps_id_);
  DCHECK(pps);

  const H264SPS* sps = parser_.GetSPS(pps->seq_parameter_set_id);
  DCHECK(sps);

  DCHECK(curr_pic_.get());

  VAPictureParameterBufferH264 pic_param;
  memset(&pic_param, 0, sizeof(VAPictureParameterBufferH264));

#define FROM_SPS_TO_PP(a) pic_param.a = sps->a;
#define FROM_SPS_TO_PP2(a, b) pic_param.b = sps->a;
  FROM_SPS_TO_PP2(pic_width_in_mbs_minus1, picture_width_in_mbs_minus1);
  // This assumes non-interlaced video
  FROM_SPS_TO_PP2(pic_height_in_map_units_minus1,
                  picture_height_in_mbs_minus1);
  FROM_SPS_TO_PP(bit_depth_luma_minus8);
  FROM_SPS_TO_PP(bit_depth_chroma_minus8);
#undef FROM_SPS_TO_PP
#undef FROM_SPS_TO_PP2

#define FROM_SPS_TO_PP_SF(a) pic_param.seq_fields.bits.a = sps->a;
#define FROM_SPS_TO_PP_SF2(a, b) pic_param.seq_fields.bits.b = sps->a;
  FROM_SPS_TO_PP_SF(chroma_format_idc);
  FROM_SPS_TO_PP_SF2(separate_colour_plane_flag,
                     residual_colour_transform_flag);
  FROM_SPS_TO_PP_SF(gaps_in_frame_num_value_allowed_flag);
  FROM_SPS_TO_PP_SF(frame_mbs_only_flag);
  FROM_SPS_TO_PP_SF(mb_adaptive_frame_field_flag);
  FROM_SPS_TO_PP_SF(direct_8x8_inference_flag);
  pic_param.seq_fields.bits.MinLumaBiPredSize8x8 = (sps->level_idc >= 31);
  FROM_SPS_TO_PP_SF(log2_max_frame_num_minus4);
  FROM_SPS_TO_PP_SF(pic_order_cnt_type);
  FROM_SPS_TO_PP_SF(log2_max_pic_order_cnt_lsb_minus4);
  FROM_SPS_TO_PP_SF(delta_pic_order_always_zero_flag);
#undef FROM_SPS_TO_PP_SF
#undef FROM_SPS_TO_PP_SF2

#define FROM_PPS_TO_PP(a) pic_param.a = pps->a;
  FROM_PPS_TO_PP(num_slice_groups_minus1);
  pic_param.slice_group_map_type = 0;
  pic_param.slice_group_change_rate_minus1 = 0;
  FROM_PPS_TO_PP(pic_init_qp_minus26);
  FROM_PPS_TO_PP(pic_init_qs_minus26);
  FROM_PPS_TO_PP(chroma_qp_index_offset);
  FROM_PPS_TO_PP(second_chroma_qp_index_offset);
#undef FROM_PPS_TO_PP

#define FROM_PPS_TO_PP_PF(a) pic_param.pic_fields.bits.a = pps->a;
#define FROM_PPS_TO_PP_PF2(a, b) pic_param.pic_fields.bits.b = pps->a;
  FROM_PPS_TO_PP_PF(entropy_coding_mode_flag);
  FROM_PPS_TO_PP_PF(weighted_pred_flag);
  FROM_PPS_TO_PP_PF(weighted_bipred_idc);
  FROM_PPS_TO_PP_PF(transform_8x8_mode_flag);

  pic_param.pic_fields.bits.field_pic_flag = 0;
  FROM_PPS_TO_PP_PF(constrained_intra_pred_flag);
  FROM_PPS_TO_PP_PF2(bottom_field_pic_order_in_frame_present_flag,
                pic_order_present_flag);
  FROM_PPS_TO_PP_PF(deblocking_filter_control_present_flag);
  FROM_PPS_TO_PP_PF(redundant_pic_cnt_present_flag);
  pic_param.pic_fields.bits.reference_pic_flag = curr_pic_->ref;
#undef FROM_PPS_TO_PP_PF
#undef FROM_PPS_TO_PP_PF2

  pic_param.frame_num = curr_pic_->frame_num;

  InitVAPicture(&pic_param.CurrPic);
  FillVAPicture(&pic_param.CurrPic, curr_pic_.get());

  // Init reference pictures' array.
  for (int i = 0; i < 16; ++i)
    InitVAPicture(&pic_param.ReferenceFrames[i]);

  // And fill it with picture info from DPB.
  FillVARefFramesFromDPB(pic_param.ReferenceFrames,
                         arraysize(pic_param.ReferenceFrames));

  pic_param.num_ref_frames = sps->max_num_ref_frames;

  // Allocate a buffer in driver for this parameter buffer and upload data.
  VABufferID pic_param_buf_id;
  VAStatus va_res = VAAPI_CreateBuffer(va_display_, va_context_id_,
                                       VAPictureParameterBufferType,
                                       sizeof(VAPictureParameterBufferH264),
                                       1, &pic_param, &pic_param_buf_id);
  VA_SUCCESS_OR_RETURN(va_res, "Failed to create a buffer for PPS", false);

  // Queue its VA buffer ID to be committed on HW decode run.
  pending_va_bufs_.push(pic_param_buf_id);

  return true;
}

// Fill a VAIQMatrixBufferH264 to be later sent to the HW decoder.
bool VaapiH264Decoder::SendIQMatrix() {
  const H264PPS* pps = parser_.GetPPS(curr_pps_id_);
  DCHECK(pps);

  VAIQMatrixBufferH264 iq_matrix_buf;
  memset(&iq_matrix_buf, 0, sizeof(VAIQMatrixBufferH264));

  if (pps->pic_scaling_matrix_present_flag) {
    for (int i = 0; i < 6; ++i) {
      for (int j = 0; j < 16; ++j)
        iq_matrix_buf.ScalingList4x4[i][j] = pps->scaling_list4x4[i][j];
    }

    for (int i = 0; i < 2; ++i) {
      for (int j = 0; j < 64; ++j)
        iq_matrix_buf.ScalingList8x8[i][j] = pps->scaling_list8x8[i][j];
    }
  } else {
    const H264SPS* sps = parser_.GetSPS(pps->seq_parameter_set_id);
    DCHECK(sps);
    for (int i = 0; i < 6; ++i) {
      for (int j = 0; j < 16; ++j)
        iq_matrix_buf.ScalingList4x4[i][j] = sps->scaling_list4x4[i][j];
    }

    for (int i = 0; i < 2; ++i) {
      for (int j = 0; j < 64; ++j)
        iq_matrix_buf.ScalingList8x8[i][j] = sps->scaling_list8x8[i][j];
    }
  }

  // Allocate a buffer in driver for this parameter buffer and upload data.
  VABufferID iq_matrix_buf_id;
  VAStatus va_res = VAAPI_CreateBuffer(va_display_, va_context_id_,
                                       VAIQMatrixBufferType,
                                       sizeof(VAIQMatrixBufferH264), 1,
                                       &iq_matrix_buf, &iq_matrix_buf_id);
  VA_SUCCESS_OR_RETURN(va_res, "Failed to create a buffer for IQMatrix",
                       false);

  // Queue its VA buffer ID to be committed on HW decode run.
  pending_va_bufs_.push(iq_matrix_buf_id);

  return true;
}

bool VaapiH264Decoder::SendVASliceParam(H264SliceHeader* slice_hdr) {
  const H264PPS* pps = parser_.GetPPS(slice_hdr->pic_parameter_set_id);
  DCHECK(pps);

  const H264SPS* sps = parser_.GetSPS(pps->seq_parameter_set_id);
  DCHECK(sps);

  VASliceParameterBufferH264 slice_param;
  memset(&slice_param, 0, sizeof(VASliceParameterBufferH264));

  slice_param.slice_data_size = slice_hdr->nalu_size;
  slice_param.slice_data_offset = 0;
  slice_param.slice_data_flag = VA_SLICE_DATA_FLAG_ALL;
  slice_param.slice_data_bit_offset = slice_hdr->header_bit_size;

#define SHDRToSP(a) slice_param.a = slice_hdr->a;
  SHDRToSP(first_mb_in_slice);
  slice_param.slice_type = slice_hdr->slice_type % 5;
  SHDRToSP(direct_spatial_mv_pred_flag);

  // TODO posciak: make sure parser sets those even when override flags
  // in slice header is off.
  SHDRToSP(num_ref_idx_l0_active_minus1);
  SHDRToSP(num_ref_idx_l1_active_minus1);
  SHDRToSP(cabac_init_idc);
  SHDRToSP(slice_qp_delta);
  SHDRToSP(disable_deblocking_filter_idc);
  SHDRToSP(slice_alpha_c0_offset_div2);
  SHDRToSP(slice_beta_offset_div2);

  if (((slice_hdr->IsPSlice() || slice_hdr->IsSPSlice()) &&
       pps->weighted_pred_flag) ||
      (slice_hdr->IsBSlice() && pps->weighted_bipred_idc == 1)) {
    SHDRToSP(luma_log2_weight_denom);
    SHDRToSP(chroma_log2_weight_denom);

    SHDRToSP(luma_weight_l0_flag);
    SHDRToSP(luma_weight_l1_flag);

    SHDRToSP(chroma_weight_l0_flag);
    SHDRToSP(chroma_weight_l1_flag);

    for (int i = 0; i <= slice_param.num_ref_idx_l0_active_minus1; ++i) {
      slice_param.luma_weight_l0[i] =
          slice_hdr->pred_weight_table_l0.luma_weight[i];
      slice_param.luma_offset_l0[i] =
          slice_hdr->pred_weight_table_l0.luma_offset[i];

      for (int j = 0; j < 2; ++j) {
        slice_param.chroma_weight_l0[i][j] =
            slice_hdr->pred_weight_table_l0.chroma_weight[i][j];
        slice_param.chroma_offset_l0[i][j] =
            slice_hdr->pred_weight_table_l0.chroma_offset[i][j];
      }
    }

    if (slice_hdr->IsBSlice()) {
      for (int i = 0; i <= slice_param.num_ref_idx_l1_active_minus1; ++i) {
        slice_param.luma_weight_l1[i] =
            slice_hdr->pred_weight_table_l1.luma_weight[i];
        slice_param.luma_offset_l1[i] =
            slice_hdr->pred_weight_table_l1.luma_offset[i];

        for (int j = 0; j < 2; ++j) {
          slice_param.chroma_weight_l1[i][j] =
              slice_hdr->pred_weight_table_l1.chroma_weight[i][j];
          slice_param.chroma_offset_l1[i][j] =
              slice_hdr->pred_weight_table_l1.chroma_offset[i][j];
        }
      }
    }
  }

  for (int i = 0; i < 32; ++i) {
    InitVAPicture(&slice_param.RefPicList0[i]);
    InitVAPicture(&slice_param.RefPicList1[i]);
  }

  int i;
  H264Picture::PtrVector::iterator it;
  for (it = ref_pic_list0_.begin(), i = 0; it != ref_pic_list0_.end();
       ++it, ++i)
    FillVAPicture(&slice_param.RefPicList0[i], *it);
  for (it = ref_pic_list1_.begin(), i = 0; it != ref_pic_list1_.end();
       ++it, ++i)
    FillVAPicture(&slice_param.RefPicList1[i], *it);

  // Allocate a buffer in driver for this parameter buffer and upload data.
  VABufferID slice_param_buf_id;
  VAStatus va_res = VAAPI_CreateBuffer(va_display_, va_context_id_,
                                       VASliceParameterBufferType,
                                       sizeof(VASliceParameterBufferH264),
                                       1, &slice_param, &slice_param_buf_id);
  VA_SUCCESS_OR_RETURN(va_res, "Failed creating a buffer for slice param",
                       false);

  // Queue its VA buffer ID to be committed on HW decode run.
  pending_slice_bufs_.push(slice_param_buf_id);

  return true;
}

bool VaapiH264Decoder::SendSliceData(const uint8* ptr, size_t size) {
    // Can't help it, blame libva...
    void* non_const_ptr = const_cast<uint8*>(ptr);

    VABufferID slice_data_buf_id;
    VAStatus va_res = VAAPI_CreateBuffer(va_display_, va_context_id_,
                                         VASliceDataBufferType, size, 1,
                                         non_const_ptr, &slice_data_buf_id);
    VA_SUCCESS_OR_RETURN(va_res, "Failed creating a buffer for slice data",
                         false);

    pending_slice_bufs_.push(slice_data_buf_id);
    return true;
}

bool VaapiH264Decoder::QueueSlice(H264SliceHeader* slice_hdr) {
  DCHECK(curr_pic_.get());

  if (!SendVASliceParam(slice_hdr))
    return false;

  if (!SendSliceData(slice_hdr->nalu_data, slice_hdr->nalu_size))
    return false;

  return true;
}

void VaapiH264Decoder::DestroyBuffers(size_t num_va_buffers,
                                      const VABufferID* va_buffers) {
  for (size_t i = 0; i < num_va_buffers; ++i) {
    VAStatus va_res = VAAPI_DestroyBuffer(va_display_, va_buffers[i]);
    VA_LOG_ON_ERROR(va_res, "vaDestroyBuffer failed");
  }
}

// TODO(posciak) start using vaMapBuffer instead of vaCreateBuffer wherever
// possible.

bool VaapiH264Decoder::DecodePicture() {
  DCHECK(!frame_ready_at_hw_);
  DCHECK(curr_pic_.get());

  static const size_t kMaxVABuffers = 32;
  DCHECK_LE(pending_va_bufs_.size(), kMaxVABuffers);
  DCHECK_LE(pending_slice_bufs_.size(), kMaxVABuffers);

  DVLOG(4) << "Pending VA bufs to commit: " << pending_va_bufs_.size();
  DVLOG(4) << "Pending slice bufs to commit: " << pending_slice_bufs_.size();

  // Find the surface associated with the picture to be decoded.
  DCHECK(pending_slice_bufs_.size());
  DecodeSurface* dec_surface =
      poc_to_decode_surfaces_[curr_pic_->pic_order_cnt];
  DVLOG(4) << "Decoding POC " << curr_pic_->pic_order_cnt
           << " into surface " << dec_surface->va_surface_id();

  // Get ready to decode into surface.
  VAStatus va_res = VAAPI_BeginPicture(va_display_, va_context_id_,
                                       dec_surface->va_surface_id());
  VA_SUCCESS_OR_RETURN(va_res, "vaBeginPicture failed", false);

  // Put buffer IDs for pending parameter buffers into va_buffers[].
  VABufferID va_buffers[kMaxVABuffers];
  size_t num_va_buffers = pending_va_bufs_.size();
  for (size_t i = 0; i < num_va_buffers && i < kMaxVABuffers; ++i) {
    va_buffers[i] = pending_va_bufs_.front();
    pending_va_bufs_.pop();
  }
  base::Closure va_buffers_callback =
      base::Bind(&VaapiH264Decoder::DestroyBuffers, base::Unretained(this),
                 num_va_buffers, va_buffers);
  base::ScopedClosureRunner va_buffers_deleter(va_buffers_callback);

  // And send them to the HW decoder.
  va_res = VAAPI_RenderPicture(va_display_, va_context_id_, va_buffers,
                               num_va_buffers);
  VA_SUCCESS_OR_RETURN(va_res, "vaRenderPicture for va_bufs failed", false);

  DVLOG(4) << "Committed " << num_va_buffers << "VA buffers";

  // Put buffer IDs for pending slice data buffers into slice_buffers[].
  VABufferID slice_buffers[kMaxVABuffers];
  size_t num_slice_buffers = pending_slice_bufs_.size();
  for (size_t i = 0; i < num_slice_buffers && i < kMaxVABuffers; ++i) {
    slice_buffers[i] = pending_slice_bufs_.front();
    pending_slice_bufs_.pop();
  }
  base::Closure va_slices_callback =
      base::Bind(&VaapiH264Decoder::DestroyBuffers, base::Unretained(this),
                 num_slice_buffers, slice_buffers);
  base::ScopedClosureRunner slice_buffers_deleter(va_slices_callback);

  // And send them to the Hw decoder.
  va_res = VAAPI_RenderPicture(va_display_, va_context_id_, slice_buffers,
                               num_slice_buffers);
  VA_SUCCESS_OR_RETURN(va_res, "vaRenderPicture for slices failed", false);

  DVLOG(4) << "Committed " << num_slice_buffers << "slice buffers";

  // Instruct HW decoder to start processing committed buffers (decode this
  // picture). This does not block until the end of decode.
  va_res = VAAPI_EndPicture(va_display_, va_context_id_);
  VA_SUCCESS_OR_RETURN(va_res, "vaEndPicture failed", false);

  // Used to notify clients that we had sufficient data to start decoding
  // a new frame.
  frame_ready_at_hw_ = true;
  return true;
}


bool VaapiH264Decoder::InitCurrPicture(H264SliceHeader* slice_hdr) {
  DCHECK(curr_pic_.get());

  memset(curr_pic_.get(), 0, sizeof(H264Picture));

  curr_pic_->idr = slice_hdr->idr_pic_flag;

  if (slice_hdr->field_pic_flag) {
    curr_pic_->field = slice_hdr->bottom_field_flag ? H264Picture::FIELD_BOTTOM
                                                    : H264Picture::FIELD_TOP;
  } else {
    curr_pic_->field = H264Picture::FIELD_NONE;
  }

  curr_pic_->ref = slice_hdr->nal_ref_idc != 0;
  // This assumes non-interlaced stream.
  curr_pic_->frame_num = curr_pic_->pic_num = slice_hdr->frame_num;

  if (!CalculatePicOrderCounts(slice_hdr))
    return false;

  // Try to get an empty surface to decode this picture to.
  if (!AssignSurfaceToPoC(curr_pic_->pic_order_cnt)) {
    DVLOG(1) << "Failed getting a free surface for a picture";
    return false;
  }

  curr_pic_->long_term_reference_flag = slice_hdr->long_term_reference_flag;
  curr_pic_->adaptive_ref_pic_marking_mode_flag =
      slice_hdr->adaptive_ref_pic_marking_mode_flag;

  // If the slice header indicates we will have to perform reference marking
  // process after this picture is decoded, store required data for that
  // purpose.
  if (slice_hdr->adaptive_ref_pic_marking_mode_flag) {
    COMPILE_ASSERT(sizeof(curr_pic_->ref_pic_marking) ==
                   sizeof(slice_hdr->ref_pic_marking),
                   ref_pic_marking_array_sizes_do_not_match);
    memcpy(curr_pic_->ref_pic_marking, slice_hdr->ref_pic_marking,
           sizeof(curr_pic_->ref_pic_marking));
  }

  return true;
}

bool VaapiH264Decoder::CalculatePicOrderCounts(H264SliceHeader* slice_hdr) {
  DCHECK_NE(curr_sps_id_, -1);

  int pic_order_cnt_lsb = slice_hdr->pic_order_cnt_lsb;
  curr_pic_->pic_order_cnt_lsb = pic_order_cnt_lsb;
  if (parser_.GetSPS(curr_sps_id_)->pic_order_cnt_type != 0) {
    DVLOG(1) << "Unsupported pic_order_cnt_type";
    return false;
  }

  // See spec 8.2.1.1.
  int prev_pic_order_cnt_msb, prev_pic_order_cnt_lsb;
  if (slice_hdr->idr_pic_flag) {
    prev_pic_order_cnt_msb = prev_pic_order_cnt_lsb = 0;
  } else {
    if (prev_ref_has_memmgmnt5_) {
      if (prev_ref_field_ != H264Picture::FIELD_BOTTOM) {
        prev_pic_order_cnt_msb = 0;
        prev_pic_order_cnt_lsb = prev_ref_top_field_order_cnt_;
      } else {
        prev_pic_order_cnt_msb = 0;
        prev_pic_order_cnt_lsb = 0;
      }
    } else {
      prev_pic_order_cnt_msb = prev_ref_pic_order_cnt_msb_;
      prev_pic_order_cnt_lsb = prev_ref_pic_order_cnt_lsb_;
    }
  }

  DCHECK_NE(max_pic_order_cnt_lsb_, 0);
  if ((pic_order_cnt_lsb < prev_pic_order_cnt_lsb) &&
      (prev_pic_order_cnt_lsb - pic_order_cnt_lsb >=
       max_pic_order_cnt_lsb_ / 2)) {
    curr_pic_->pic_order_cnt_msb = prev_pic_order_cnt_msb +
        max_pic_order_cnt_lsb_;
  } else if ((pic_order_cnt_lsb > prev_pic_order_cnt_lsb) &&
      (pic_order_cnt_lsb - prev_pic_order_cnt_lsb >
       max_pic_order_cnt_lsb_ / 2)) {
    curr_pic_->pic_order_cnt_msb = prev_pic_order_cnt_msb -
        max_pic_order_cnt_lsb_;
  } else {
    curr_pic_->pic_order_cnt_msb = prev_pic_order_cnt_msb;
  }

  if (curr_pic_->field != H264Picture::FIELD_BOTTOM) {
    curr_pic_->top_field_order_cnt = curr_pic_->pic_order_cnt_msb +
        pic_order_cnt_lsb;
  }

  if (curr_pic_->field != H264Picture::FIELD_TOP) {
    // TODO posciak: perhaps replace with pic->field?
    if (!slice_hdr->field_pic_flag) {
      curr_pic_->bottom_field_order_cnt = curr_pic_->top_field_order_cnt +
          slice_hdr->delta_pic_order_cnt_bottom;
    } else {
      curr_pic_->bottom_field_order_cnt = curr_pic_->pic_order_cnt_msb +
          pic_order_cnt_lsb;
    }
  }

  switch (curr_pic_->field) {
    case H264Picture::FIELD_NONE:
      curr_pic_->pic_order_cnt = std::min(curr_pic_->top_field_order_cnt,
                                          curr_pic_->bottom_field_order_cnt);
      break;
    case H264Picture::FIELD_TOP:
      curr_pic_->pic_order_cnt = curr_pic_->top_field_order_cnt;
      break;
    case H264Picture::FIELD_BOTTOM:
      curr_pic_->pic_order_cnt = curr_pic_->bottom_field_order_cnt;
      break;
  }

  return true;
}

void VaapiH264Decoder::UpdatePicNums() {
  for (H264DPB::Pictures::iterator it = dpb_.begin(); it != dpb_.end(); ++it) {
    H264Picture* pic = *it;
    DCHECK(pic);
    if (!pic->ref)
      continue;

    // Below assumes non-interlaced stream.
    DCHECK_EQ(pic->field, H264Picture::FIELD_NONE);
    if (pic->long_term) {
      pic->long_term_pic_num = pic->long_term_frame_idx;
    } else {
      if (pic->frame_num > frame_num_)
        pic->frame_num_wrap = pic->frame_num - max_frame_num_;
      else
        pic->frame_num_wrap = pic->frame_num;

      pic->pic_num = pic->frame_num_wrap;
    }
  }
}

struct PicNumDescCompare {
  bool operator()(const H264Picture* a, const H264Picture* b) const {
    return a->pic_num > b->pic_num;
  }
};

struct LongTermPicNumAscCompare {
  bool operator()(const H264Picture* a, const H264Picture* b) const {
    return a->long_term_pic_num < b->long_term_pic_num;
  }
};

void VaapiH264Decoder::ConstructReferencePicListsP(H264SliceHeader* slice_hdr) {
  // RefPicList0 (8.2.4.2.1) [[1] [2]], where:
  // [1] shortterm ref pics sorted by descending pic_num,
  // [2] longterm ref pics by ascending long_term_pic_num.
  DCHECK(ref_pic_list0_.empty() && ref_pic_list1_.empty());
  // First get the short ref pics...
  dpb_.GetShortTermRefPicsAppending(ref_pic_list0_);
  size_t num_short_refs = ref_pic_list0_.size();

  // and sort them to get [1].
  std::sort(ref_pic_list0_.begin(), ref_pic_list0_.end(), PicNumDescCompare());

  // Now get long term pics and sort them by long_term_pic_num to get [2].
  dpb_.GetLongTermRefPicsAppending(ref_pic_list0_);
  std::sort(ref_pic_list0_.begin() + num_short_refs, ref_pic_list0_.end(),
            LongTermPicNumAscCompare());

  // Cut off if we have more than requested in slice header.
  ref_pic_list0_.resize(slice_hdr->num_ref_idx_l0_active_minus1 + 1);
}

struct POCAscCompare {
  bool operator()(const H264Picture* a, const H264Picture* b) const {
    return a->pic_order_cnt < b->pic_order_cnt;
  }
};

struct POCDescCompare {
  bool operator()(const H264Picture* a, const H264Picture* b) const {
    return a->pic_order_cnt > b->pic_order_cnt;
  }
};

void VaapiH264Decoder::ConstructReferencePicListsB(H264SliceHeader* slice_hdr) {
  // RefPicList0 (8.2.4.2.3) [[1] [2] [3]], where:
  // [1] shortterm ref pics with POC < curr_pic's POC sorted by descending POC,
  // [2] shortterm ref pics with POC > curr_pic's POC by ascending POC,
  // [3] longterm ref pics by ascending long_term_pic_num.
  DCHECK(ref_pic_list0_.empty() && ref_pic_list1_.empty());
  dpb_.GetShortTermRefPicsAppending(ref_pic_list0_);
  size_t num_short_refs = ref_pic_list0_.size();

  // First sort ascending, this will put [1] in right place and finish [2].
  std::sort(ref_pic_list0_.begin(), ref_pic_list0_.end(), POCAscCompare());

  // Find first with POC > curr_pic's POC to get first element in [2]...
  H264Picture::PtrVector::iterator iter;
  iter = std::upper_bound(ref_pic_list0_.begin(), ref_pic_list0_.end(),
                          curr_pic_.get(), POCAscCompare());

  // and sort [1] descending, thus finishing sequence [1] [2].
  std::sort(ref_pic_list0_.begin(), iter, POCDescCompare());

  // Now add [3] and sort by ascending long_term_pic_num.
  dpb_.GetLongTermRefPicsAppending(ref_pic_list0_);
  std::sort(ref_pic_list0_.begin() + num_short_refs, ref_pic_list0_.end(),
            LongTermPicNumAscCompare());

  // RefPicList1 (8.2.4.2.4) [[1] [2] [3]], where:
  // [1] shortterm ref pics with POC > curr_pic's POC sorted by ascending POC,
  // [2] shortterm ref pics with POC < curr_pic's POC by descending POC,
  // [3] longterm ref pics by ascending long_term_pic_num.

  dpb_.GetShortTermRefPicsAppending(ref_pic_list1_);
  num_short_refs = ref_pic_list1_.size();

  // First sort by descending POC.
  std::sort(ref_pic_list1_.begin(), ref_pic_list1_.end(), POCDescCompare());

  // Find first with POC < curr_pic's POC to get first element in [2]...
  iter = std::upper_bound(ref_pic_list1_.begin(), ref_pic_list1_.end(),
                          curr_pic_.get(), POCDescCompare());

  // and sort [1] ascending.
  std::sort(ref_pic_list1_.begin(), iter, POCAscCompare());

  // Now add [3] and sort by ascending long_term_pic_num
  dpb_.GetShortTermRefPicsAppending(ref_pic_list1_);
  std::sort(ref_pic_list1_.begin() + num_short_refs, ref_pic_list1_.end(),
            LongTermPicNumAscCompare());

  // If lists identical, swap first two entries in RefPicList1 (spec 8.2.4.2.3)
  if (ref_pic_list1_.size() > 1 &&
      std::equal(ref_pic_list0_.begin(), ref_pic_list0_.end(),
                 ref_pic_list1_.begin()))
    std::swap(ref_pic_list1_[0], ref_pic_list1_[1]);

  // Per 8.2.4.2 it's possible for num_ref_idx_lX_active_minus1 to indicate
  // there should be more ref pics on list than we constructed.
  // Those superfluous ones should be treated as non-reference.
  ref_pic_list0_.resize(slice_hdr->num_ref_idx_l0_active_minus1 + 1);
  ref_pic_list1_.resize(slice_hdr->num_ref_idx_l1_active_minus1 + 1);
}

// See 8.2.4
int VaapiH264Decoder::PicNumF(H264Picture *pic) {
  if (!pic)
      return -1;

  if (!pic->long_term)
      return pic->pic_num;
  else
      return max_pic_num_;
}

// See 8.2.4
int VaapiH264Decoder::LongTermPicNumF(H264Picture *pic) {
  if (pic->ref && pic->long_term)
    return pic->long_term_pic_num;
  else
    return 2 * (max_long_term_frame_idx_ + 1);
}

// Shift elements on the |v| starting from |from| to |to|, inclusive,
// one position to the right and insert pic at |from|.
static void ShiftRightAndInsert(H264Picture::PtrVector& v,
                                int from,
                                int to,
                                H264Picture* pic) {
  DCHECK(pic);
  for (int i = to + 1; i > from; --i)
    v[i] = v[i - 1];

  v[from] = pic;
}

bool VaapiH264Decoder::ModifyReferencePicList(H264SliceHeader *slice_hdr,
                                              int list) {
  int num_ref_idx_lX_active_minus1;
  H264Picture::PtrVector* ref_pic_listx;
  H264ModificationOfPicNum* list_mod;

  // This can process either ref_pic_list0 or ref_pic_list1, depending on
  // the list argument. Set up pointers to proper list to be processed here.
  if (list == 0) {
    if (!slice_hdr->ref_pic_list_modification_flag_l0)
      return true;

    list_mod = slice_hdr->ref_list_l0_modifications;
    num_ref_idx_lX_active_minus1 = ref_pic_list0_.size() - 1;

    ref_pic_listx = &ref_pic_list0_;
  } else {
    if (!slice_hdr->ref_pic_list_modification_flag_l1)
      return true;

    list_mod = slice_hdr->ref_list_l1_modifications;
    num_ref_idx_lX_active_minus1 = ref_pic_list1_.size() - 1;

    ref_pic_listx = &ref_pic_list1_;
  }

  DCHECK_GT(num_ref_idx_lX_active_minus1, 0);

  // Spec 8.2.4.3:
  // Reorder pictures on the list in a way specified in the stream.
  int pic_num_lx_pred = curr_pic_->pic_num;
  int ref_idx_lx = 0;
  int pic_num_lx_no_wrap;
  int pic_num_lx;
  H264Picture *pic ;
  for (int i = 0; i < H264SliceHeader::kRefListModSize; ++i) {
    switch (list_mod->modification_of_pic_nums_idc) {
      case 0:
      case 1:
        // Modify short reference picture position.
        if (list_mod->modification_of_pic_nums_idc == 0) {
          // Subtract given value from predicted PicNum.
          pic_num_lx_no_wrap = pic_num_lx_pred -
              (static_cast<int>(list_mod->abs_diff_pic_num_minus1) + 1);
          // Wrap around max_pic_num_ if it becomes < 0 as result
          // of subtraction.
          if (pic_num_lx_no_wrap < 0)
            pic_num_lx_no_wrap += max_pic_num_;
        } else {
          // Add given value to predicted PicNum.
          pic_num_lx_no_wrap = pic_num_lx_pred +
              (static_cast<int>(list_mod->abs_diff_pic_num_minus1) + 1);
          // Wrap around max_pic_num_ if it becomes >= max_pic_num_ as result
          // of the addition.
          if (pic_num_lx_no_wrap >= max_pic_num_)
            pic_num_lx_no_wrap -= max_pic_num_;
        }

        // For use in next iteration.
        pic_num_lx_pred = pic_num_lx_no_wrap;

        if (pic_num_lx_no_wrap > curr_pic_->pic_num)
          pic_num_lx = pic_num_lx_no_wrap - max_pic_num_;
        else
          pic_num_lx = pic_num_lx_no_wrap;

        DCHECK_LT(num_ref_idx_lX_active_minus1 + 1,
                  H264SliceHeader::kRefListModSize);
        pic = dpb_.GetShortRefPicByPicNum(pic_num_lx);
        if (!pic) {
          DVLOG(1) << "Malformed stream, no pic num " << pic_num_lx;
          return false;
        }
        ShiftRightAndInsert(*ref_pic_listx, ref_idx_lx,
                            num_ref_idx_lX_active_minus1, pic);
        ref_idx_lx++;

        for (int src = ref_idx_lx, dst = ref_idx_lx;
             src <= num_ref_idx_lX_active_minus1 + 1; ++src) {
          if (PicNumF((*ref_pic_listx)[src]) != pic_num_lx)
            (*ref_pic_listx)[dst++] = (*ref_pic_listx)[src];
        }
        break;

      case 2:
        // Modify long term reference picture position.
        DCHECK_LT(num_ref_idx_lX_active_minus1 + 1,
                  H264SliceHeader::kRefListModSize);
        pic = dpb_.GetLongRefPicByLongTermPicNum(list_mod->long_term_pic_num);
        if (!pic) {
          DVLOG(1) << "Malformed stream, no pic num " << pic_num_lx;
          return false;
        }
        ShiftRightAndInsert(*ref_pic_listx, ref_idx_lx,
                            num_ref_idx_lX_active_minus1, pic);
        ref_idx_lx++;

        for (int src = ref_idx_lx, dst = ref_idx_lx;
             src <= num_ref_idx_lX_active_minus1 + 1; ++src) {
          if (LongTermPicNumF((*ref_pic_listx)[src])
              != static_cast<int>(list_mod->long_term_pic_num))
            (*ref_pic_listx)[dst++] = (*ref_pic_listx)[src];
        }
        break;

      case 3:
        // End of modification list.
        return true;

      default:
        // May be recoverable.
        DVLOG(1) << "Invalid modification_of_pic_nums_idc="
                 << list_mod->modification_of_pic_nums_idc
                 << " in position " << i;
        break;
    }

    ++list_mod;
  }

  return true;
}

bool VaapiH264Decoder::PutPicToTexture(int32 picture_buffer_id) {
  DecodeSurfaces::iterator it = decode_surfaces_.find(picture_buffer_id);
  if (it == decode_surfaces_.end()) {
    DVLOG(1) << "Asked to put an invalid buffer";
    return false;
  }

  DVLOG(3) << "Will output from VASurface " << it->second->va_surface_id()
           << " to texture id " << it->second->texture_id();

  return it->second->Sync();
}

bool VaapiH264Decoder::OutputPic(H264Picture* pic) {
  // No longer need to keep POC->surface mapping, since for decoder this POC
  // is finished with. When the client returns this surface via
  // ReusePictureBuffer(), it will be marked back as available for use.
  DCHECK(!pic->outputted);
  pic->outputted = true;
  DecodeSurface* dec_surface = UnassignSurfaceFromPoC(pic->pic_order_cnt);
  if (!dec_surface)
    return false;

  // Notify the client that a picture can be output. The decoded picture may
  // not be synced with texture contents yet at this point. The client has
  // to use PutPicToTexture() to ensure that.
  DVLOG(4) << "Posting output task for input_id: " << dec_surface->input_id()
           << "output_id: " << dec_surface->picture_buffer_id();
  output_pic_cb_.Run(dec_surface->input_id(),
                     dec_surface->picture_buffer_id());
  return true;
}

bool VaapiH264Decoder::Flush() {
  // Output all pictures that are waiting to be outputted.
  FinishPrevFrameIfPresent();
  H264Picture::PtrVector to_output;
  dpb_.GetNotOutputtedPicsAppending(to_output);
  // Sort them by ascending POC to output in order.
  std::sort(to_output.begin(), to_output.end(), POCAscCompare());

  H264Picture::PtrVector::iterator it;
  for (it = to_output.begin(); it != to_output.end(); ++it) {
    if (!OutputPic(*it)) {
      DVLOG(1) << "Failed to output pic POC: " << (*it)->pic_order_cnt;
      return false;
    }
  }

  // And clear DPB contents.
  dpb_.Clear();

  return true;
}

bool VaapiH264Decoder::StartNewFrame(H264SliceHeader* slice_hdr) {
  // TODO posciak: add handling of max_num_ref_frames per spec.

  // If the new frame is an IDR, output what's left to output and clear DPB
  if (slice_hdr->idr_pic_flag) {
    // (unless we are explicitly instructed not to do so).
    if (!slice_hdr->no_output_of_prior_pics_flag) {
      // Output DPB contents.
      if (!Flush())
        return false;
    }
    dpb_.Clear();
  }

  // curr_pic_ should have either been added to DPB or discarded when finishing
  // the last frame. DPB is responsible for releasing that memory once it's
  // not needed anymore.
  DCHECK(!curr_pic_.get());
  curr_pic_.reset(new H264Picture);
  CHECK(curr_pic_.get());

  if (!InitCurrPicture(slice_hdr))
    return false;

  DCHECK_GT(max_frame_num_, 0);

  UpdatePicNums();

  // Prepare reference picture lists if required (B and S/SP slices).
  ref_pic_list0_.clear();
  ref_pic_list1_.clear();
  if (slice_hdr->IsPSlice() || slice_hdr->IsSPSlice()) {
    ConstructReferencePicListsP(slice_hdr);
    if (!ModifyReferencePicList(slice_hdr, 0))
      return false;
  } else if (slice_hdr->IsBSlice()) {
    ConstructReferencePicListsB(slice_hdr);
    if (!ModifyReferencePicList(slice_hdr, 0))
      return false;
    if (!ModifyReferencePicList(slice_hdr, 1))
      return false;
  }

  // Send parameter buffers before each new picture, before the first slice.
  if (!SendPPS())
    return false;

  if (!SendIQMatrix())
    return false;

  if (!QueueSlice(slice_hdr))
    return false;

  return true;
}

bool VaapiH264Decoder::HandleMemoryManagementOps() {
  // 8.2.5.4
  for (unsigned int i = 0; i < arraysize(curr_pic_->ref_pic_marking); ++i) {
    // Code below does not support interlaced stream (per-field pictures).
    H264DecRefPicMarking* ref_pic_marking = &curr_pic_->ref_pic_marking[i];
    H264Picture* to_mark;
    int pic_num_x;

    switch (ref_pic_marking->memory_mgmnt_control_operation) {
      case 0:
        // Normal end of operations' specification.
        return true;

      case 1:
        // Mark a short term reference picture as unused so it can be removed
        // if outputted.
        pic_num_x = curr_pic_->pic_num -
            (ref_pic_marking->difference_of_pic_nums_minus1 + 1);
        to_mark = dpb_.GetShortRefPicByPicNum(pic_num_x);
        if (to_mark) {
          to_mark->ref = false;
        } else {
          DVLOG(1) << "Invalid short ref pic num to unmark";
          return false;
        }
        break;

      case 2:
        // Mark a long term reference picture as unused so it can be removed
        // if outputted.
        to_mark = dpb_.GetLongRefPicByLongTermPicNum(
            ref_pic_marking->long_term_pic_num);
        if (to_mark) {
          to_mark->ref = false;
        } else {
          DVLOG(1) << "Invalid long term ref pic num to unmark";
          return false;
        }
        break;

      case 3:
        // Mark a short term reference picture as long term reference.
        pic_num_x = curr_pic_->pic_num -
            (ref_pic_marking->difference_of_pic_nums_minus1 + 1);
        to_mark = dpb_.GetShortRefPicByPicNum(pic_num_x);
        if (to_mark) {
          DCHECK(to_mark->ref && !to_mark->long_term);
          to_mark->long_term = true;
          to_mark->long_term_frame_idx = ref_pic_marking->long_term_frame_idx;
        } else {
          DVLOG(1) << "Invalid short term ref pic num to mark as long ref";
          return false;
        }
        break;

      case 4: {
        // Unmark all reference pictures with long_term_frame_idx over new max.
        max_long_term_frame_idx_
            = ref_pic_marking->max_long_term_frame_idx_plus1 - 1;
        H264Picture::PtrVector long_terms;
        dpb_.GetLongTermRefPicsAppending(long_terms);
        for (size_t i = 0; i < long_terms.size(); ++i) {
          H264Picture* pic = long_terms[i];
          DCHECK(pic->ref && pic->long_term);
          // Ok to cast, max_long_term_frame_idx is much smaller than 16bit.
          if (pic->long_term_frame_idx >
              static_cast<int>(max_long_term_frame_idx_))
            pic->ref = false;
        }
        break;
      }

      case 5:
        // Unmark all reference pictures.
        dpb_.MarkAllUnusedForRef();
        max_long_term_frame_idx_ = -1;
        curr_pic_->mem_mgmt_5 = true;
        break;

      case 6: {
        // Replace long term reference pictures with current picture.
        // First unmark if any existing with this long_term_frame_idx...
        H264Picture::PtrVector long_terms;
        dpb_.GetLongTermRefPicsAppending(long_terms);
        for (size_t i = 0; i < long_terms.size(); ++i) {
          H264Picture* pic = long_terms[i];
          DCHECK(pic->ref && pic->long_term);
          // Ok to cast, long_term_frame_idx is much smaller than 16bit.
          if (pic->long_term_frame_idx ==
              static_cast<int>(ref_pic_marking->long_term_frame_idx))
            pic->ref = false;
        }

        // and mark the current one instead.
        curr_pic_->ref = true;
        curr_pic_->long_term = true;
        curr_pic_->long_term_frame_idx = ref_pic_marking->long_term_frame_idx;
        break;
      }

      default:
        // Would indicate a bug in parser.
        NOTREACHED();
    }
  }

  return true;
}

// This method ensures that DPB does not overflow, either by removing
// reference pictures as specified in the stream, or using a sliding window
// procedure to remove the oldest one.
// It also performs marking and unmarking pictures as reference.
// See spac 8.2.5.1.
void VaapiH264Decoder::ReferencePictureMarking() {
  if (curr_pic_->idr) {
    // If current picture is an IDR, all reference pictures are unmarked.
    dpb_.MarkAllUnusedForRef();

    if (curr_pic_->long_term_reference_flag) {
      curr_pic_->long_term = true;
      curr_pic_->long_term_frame_idx = 0;
      max_long_term_frame_idx_ = 0;
    } else {
      curr_pic_->long_term = false;
      max_long_term_frame_idx_ = -1;
    }
  } else {
    if (!curr_pic_->adaptive_ref_pic_marking_mode_flag) {
      // If non-IDR, and the stream does not indicate what we should do to
      // ensure DPB doesn't overflow, discard oldest picture.
      // See spec 8.2.5.3.
      if (curr_pic_->field == H264Picture::FIELD_NONE) {
        DCHECK_LE(dpb_.CountRefPics(),
            std::max<int>(parser_.GetSPS(curr_sps_id_)->max_num_ref_frames,
                          1));
        if (dpb_.CountRefPics() ==
            std::max<int>(parser_.GetSPS(curr_sps_id_)->max_num_ref_frames,
                          1)) {
          // Max number of reference pics reached,
          // need to remove one of the short term ones.
          // Find smallest frame_num_wrap short reference picture and mark
          // it as unused.
          H264Picture* to_unmark = dpb_.GetLowestFrameNumWrapShortRefPic();
          if (to_unmark == NULL) {
            DVLOG(1) << "Couldn't find a short ref picture to unmark";
            return;
          }
          to_unmark->ref = false;
        }
      } else {
        // Shouldn't get here.
        DVLOG(1) << "Interlaced video not supported.";
      }
    } else {
      // Stream has instructions how to discard pictures from DPB and how
      // to mark/unmark existing reference pictures. Do it.
      // Spec 8.2.5.4.
      if (curr_pic_->field == H264Picture::FIELD_NONE) {
        HandleMemoryManagementOps();
      } else {
        // Shouldn't get here.
        DVLOG(1) << "Interlaced video not supported.";
      }
    }
  }
}

bool VaapiH264Decoder::FinishPicture() {
  DCHECK(curr_pic_.get());

  // Finish processing previous picture.
  // Start by storing previous reference picture data for later use,
  // if picture being finished is a reference picture.
  if (curr_pic_->ref) {
    ReferencePictureMarking();
    prev_ref_has_memmgmnt5_ = curr_pic_->mem_mgmt_5;
    prev_ref_top_field_order_cnt_ = curr_pic_->top_field_order_cnt;
    prev_ref_pic_order_cnt_msb_ = curr_pic_->pic_order_cnt_msb;
    prev_ref_pic_order_cnt_lsb_ = curr_pic_->pic_order_cnt_lsb;
    prev_ref_field_ = curr_pic_->field;
  }

  // Remove unused (for reference or later output) pictures from DPB.
  dpb_.RemoveUnused();

  DVLOG(4) << "Finishing picture, DPB entries: " << dpb_.size()
           << " Num available dec surfaces: "
           << num_available_decode_surfaces_;

  if (dpb_.IsFull()) {
    // DPB is full, we have to make space for the new picture.
    // Get all pictures that haven't been outputted yet.
    H264Picture::PtrVector not_outputted;
    dpb_.GetNotOutputtedPicsAppending(not_outputted);
    std::sort(not_outputted.begin(), not_outputted.end(), POCAscCompare());
    H264Picture::PtrVector::iterator output_candidate = not_outputted.begin();

    // Keep outputting pictures until we can either output the picture being
    // finished and discard it (if it is not a reference picture), or until
    // we can discard an older picture that was just waiting for output and
    // is not a reference picture, thus making space for the current one.
    while (dpb_.IsFull()) {
      // Maybe outputted enough to output current picture.
      if (!curr_pic_->ref && (output_candidate == not_outputted.end() ||
          curr_pic_->pic_order_cnt < (*output_candidate)->pic_order_cnt)) {
        // curr_pic_ is not a reference picture and no preceding pictures are
        // waiting for output in DPB, so it can be outputted and discarded
        // without storing in DPB.
        if (!OutputPic(curr_pic_.get()))
          return false;

        // Managed to output current picture, return without adding to DPB.
        return true;
      }

      // Couldn't output current picture, so try to output the lowest PoC
      // from DPB.
      if (output_candidate != not_outputted.end()) {
        if (!OutputPic(*output_candidate))
          return false;

        // If outputted picture wasn't a reference picture, it can be removed.
        if (!(*output_candidate)->ref)
          dpb_.RemoveByPOC((*output_candidate)->pic_order_cnt);
      } else {
        // Couldn't output current pic and couldn't do anything
        // with existing pictures in DPB, so we can't make space.
        // This should not happen.
        DVLOG(1) << "Could not free up space in DPB!";
        return false;
      }
    }
    ++output_candidate;
  }

  // Store current picture for later output and/or reference (ownership now
  // with the DPB).
  dpb_.StorePic(curr_pic_.release());

  return true;
}

bool VaapiH264Decoder::ProcessSPS(int sps_id) {
  const H264SPS* sps = parser_.GetSPS(sps_id);
  DCHECK(sps);

  if (sps->frame_mbs_only_flag == 0) {
    // Fields/interlaced video not supported.
    DVLOG(1) << "frame_mbs_only_flag != 1 not supported";
    return false;
  }

  if (sps->gaps_in_frame_num_value_allowed_flag) {
    DVLOG(1) << "Gaps in frame numbers not supported";
    return false;
  }

  if (sps->pic_order_cnt_type != 0) {
    DVLOG(1) << "Unsupported pic_order_cnt_type";
    return false;
  }

  curr_sps_id_ = sps->seq_parameter_set_id;

  // Calculate picture height/width (spec 7.4.2.1.1, 7.4.3).
  int width = 16 * (sps->pic_width_in_mbs_minus1 + 1);
  int height = 16 * (2 - sps->frame_mbs_only_flag) *
      (sps->pic_height_in_map_units_minus1 + 1);

  if ((pic_width_ != -1 || pic_height_ != -1) &&
      (width != pic_width_ || height != pic_height_)) {
    DVLOG(1) << "Picture size changed mid-stream";
    return false;
  }

  pic_width_ = width;
  pic_height_ = height;
  DVLOG(1) << "New picture size: " << pic_width_ << "x" << pic_height_;

  max_pic_order_cnt_lsb_ = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
  max_frame_num_ = 1 << (sps->log2_max_frame_num_minus4 + 4);

  return true;
}

bool VaapiH264Decoder::ProcessPPS(int pps_id) {
  const H264PPS* pps = parser_.GetPPS(pps_id);
  DCHECK(pps);

  curr_pps_id_ = pps->pic_parameter_set_id;

  return true;
}

bool VaapiH264Decoder::FinishPrevFrameIfPresent() {
  // If we already have a frame waiting to be decoded, decode it and finish.
  if (curr_pic_ != NULL) {
    if (!DecodePicture())
      return false;
    return FinishPicture();
  }

  return true;
}

bool VaapiH264Decoder::ProcessSlice(H264SliceHeader* slice_hdr) {
  prev_frame_num_ = frame_num_;
  frame_num_ = slice_hdr->frame_num;

  if (prev_frame_num_ > 0 && prev_frame_num_ < frame_num_ - 1) {
    DVLOG(1) << "Gap in frame_num!";
    return false;
  }

  if (slice_hdr->field_pic_flag == 0)
    max_pic_num_ = max_frame_num_;
  else
    max_pic_num_ = 2 * max_frame_num_;

  // TODO posciak: switch to new picture detection per 7.4.1.2.4.
  if (curr_pic_ != NULL && slice_hdr->first_mb_in_slice != 0) {
    // This is just some more slice data of the current picture, so
    // just queue it and return.
    QueueSlice(slice_hdr);
    return true;
  } else {
    // A new frame, so first finish the previous one before processing it...
    if (!FinishPrevFrameIfPresent())
      return false;

    // and then start a new one.
    return StartNewFrame(slice_hdr);
  }
}

#define SET_ERROR_AND_RETURN()             \
  do {                                     \
    DVLOG(1) << "Error during decode";     \
    state_ = kError;                       \
    return VaapiH264Decoder::kDecodeError; \
  } while (0)

VaapiH264Decoder::DecResult VaapiH264Decoder::DecodeInitial(int32 input_id) {
  // Decode enough to get required picture size (i.e. until we find an SPS),
  // if we get any slice data, we are missing the beginning of the stream.
  H264NALU nalu;
  H264Parser::Result res;

  DCHECK_NE(state_, kUninitialized);

  curr_input_id_ = input_id;

  while (1) {
    // Get next NALU looking for SPS or IDR if after reset.
    res = parser_.AdvanceToNextNALU(&nalu);
    if (res == H264Parser::kEOStream) {
      DVLOG(1) << "Could not find SPS before EOS";
      return kNeedMoreStreamData;
    } else if (res != H264Parser::kOk) {
      SET_ERROR_AND_RETURN();
    }

    DVLOG(4) << " NALU found: " << static_cast<int>(nalu.nal_unit_type);

    switch (nalu.nal_unit_type) {
      case H264NALU::kSPS:
        res = parser_.ParseSPS(&curr_sps_id_);
        if (res != H264Parser::kOk)
          SET_ERROR_AND_RETURN();

        if (!ProcessSPS(curr_sps_id_))
          SET_ERROR_AND_RETURN();

        // Just got information about the video size from SPS, so we can
        // now allocate surfaces and let the client now we are ready to
        // accept output buffers and decode.
        if (!CreateVASurfaces())
          SET_ERROR_AND_RETURN();

        state_ = kDecoding;
        return kReadyToDecode;

      case H264NALU::kIDRSlice:
        // If after reset, should be able to recover from an IDR.
        if (state_ == kAfterReset) {
          H264SliceHeader slice_hdr;

          res = parser_.ParseSliceHeader(nalu, &slice_hdr);
          if (res != H264Parser::kOk)
            SET_ERROR_AND_RETURN();

          if (!ProcessSlice(&slice_hdr))
            SET_ERROR_AND_RETURN();

          state_ = kDecoding;
          return kReadyToDecode;
        }  // else fallthrough
      case H264NALU::kNonIDRSlice:
      case H264NALU::kPPS:
        // Non-IDR slices cannot be used as resume points, as we may not
        // have all reference pictures that they may require.
        // fallthrough
      default:
        // Skip everything unless it's SPS or an IDR slice (if after reset).
        DVLOG(4) << "Skipping NALU";
        break;
    }
  }
}

void VaapiH264Decoder::SetStream(uint8* ptr, size_t size) {
  DCHECK(ptr);
  DCHECK(size);

  // Got new input stream data from the client.
  DVLOG(4) << "New input stream chunk at " << (void*) ptr
           << " size:  " << size;
  parser_.SetStream(ptr, size);
}

VaapiH264Decoder::DecResult VaapiH264Decoder::DecodeOneFrame(int32 input_id) {
  // Decode until one full frame is decoded or return it or until end
  // of stream (end of input data is reached).
  H264Parser::Result par_res;
  H264NALU nalu;

  curr_input_id_ = input_id;

  if (state_ != kDecoding) {
    DVLOG(1) << "Decoder not ready: error in stream or not initialized";
    return kDecodeError;
  }

  // All of the actions below might result in decoding a picture from
  // previously parsed data, but we still have to handle/parse current input
  // first.
  // Note: this may drop some already decoded frames if there are errors
  // further in the stream, but we are OK with that.
  while (1) {
    if (num_available_decode_surfaces_ < 1) {
      DVLOG(4) << "No output surfaces available";
      return kNoOutputAvailable;
    }
    par_res = parser_.AdvanceToNextNALU(&nalu);
    if (par_res == H264Parser::kEOStream)
      return kNeedMoreStreamData;
    else if (par_res != H264Parser::kOk)
      SET_ERROR_AND_RETURN();

    DVLOG(4) << "NALU found: " << static_cast<int>(nalu.nal_unit_type);

    switch (nalu.nal_unit_type) {
      case H264NALU::kNonIDRSlice:
      case H264NALU::kIDRSlice: {
        H264SliceHeader slice_hdr;

        par_res = parser_.ParseSliceHeader(nalu, &slice_hdr);
        if (par_res != H264Parser::kOk)
          SET_ERROR_AND_RETURN();

        if (!ProcessSlice(&slice_hdr))
          SET_ERROR_AND_RETURN();
        break;
      }

      case H264NALU::kSPS:
        int sps_id;

        if (!FinishPrevFrameIfPresent())
          SET_ERROR_AND_RETURN();

        par_res = parser_.ParseSPS(&sps_id);
        if (par_res != H264Parser::kOk)
          SET_ERROR_AND_RETURN();

        if (!ProcessSPS(sps_id))
          SET_ERROR_AND_RETURN();
        break;

      case H264NALU::kPPS:
        int pps_id;

        if (!FinishPrevFrameIfPresent())
          SET_ERROR_AND_RETURN();

        par_res = parser_.ParsePPS(&pps_id);
        if (par_res != H264Parser::kOk)
          SET_ERROR_AND_RETURN();

        if (!ProcessPPS(pps_id))
          SET_ERROR_AND_RETURN();
        break;

      default:
        // skip NALU
        break;
    }

    // If the last action resulted in decoding a frame, possibly from older
    // data, return. Otherwise keep reading the stream.
    if (frame_ready_at_hw_) {
      frame_ready_at_hw_ = false;
      return kDecodedFrame;
    }
  }
}

// static
size_t VaapiH264Decoder::GetRequiredNumOfPictures() {
  return kNumReqPictures;
}

// static
void VaapiH264Decoder::PreSandboxInitialization() {
  DCHECK(!pre_sandbox_init_done_);
  vaapi_handle = dlopen("libva.so", RTLD_NOW);
  vaapi_x11_handle = dlopen("libva-x11.so", RTLD_NOW);
  pre_sandbox_init_done_ = vaapi_handle && vaapi_x11_handle;
}

// static
bool VaapiH264Decoder::PostSandboxInitialization() {
  if (!pre_sandbox_init_done_)
    return false;
#define VAAPI_DLSYM(name, handle)                                       \
  VAAPI_##name = reinterpret_cast<Vaapi##name>(dlsym((handle), "va"#name)) \

  VAAPI_DLSYM(GetDisplay, vaapi_x11_handle);
  VAAPI_DLSYM(DisplayIsValid, vaapi_handle);
  VAAPI_DLSYM(Initialize, vaapi_handle);
  VAAPI_DLSYM(Terminate, vaapi_handle);
  VAAPI_DLSYM(GetConfigAttributes, vaapi_handle);
  VAAPI_DLSYM(CreateConfig, vaapi_handle);
  VAAPI_DLSYM(DestroyConfig, vaapi_handle);
  VAAPI_DLSYM(CreateSurfaces, vaapi_handle);
  VAAPI_DLSYM(DestroySurfaces, vaapi_handle);
  VAAPI_DLSYM(CreateContext, vaapi_handle);
  VAAPI_DLSYM(DestroyContext, vaapi_handle);
  VAAPI_DLSYM(PutSurface, vaapi_x11_handle);
  VAAPI_DLSYM(SyncSurface, vaapi_x11_handle);
  VAAPI_DLSYM(BeginPicture, vaapi_handle);
  VAAPI_DLSYM(RenderPicture, vaapi_handle);
  VAAPI_DLSYM(EndPicture, vaapi_handle);
  VAAPI_DLSYM(CreateBuffer, vaapi_handle);
  VAAPI_DLSYM(DestroyBuffer, vaapi_handle);
  VAAPI_DLSYM(ErrorStr, vaapi_handle);
#undef VAAPI_DLSYM

  return VAAPI_GetDisplay &&
      VAAPI_DisplayIsValid &&
      VAAPI_Initialize &&
      VAAPI_Terminate &&
      VAAPI_GetConfigAttributes &&
      VAAPI_CreateConfig &&
      VAAPI_DestroyConfig &&
      VAAPI_CreateSurfaces &&
      VAAPI_DestroySurfaces &&
      VAAPI_CreateContext &&
      VAAPI_DestroyContext &&
      VAAPI_PutSurface &&
      VAAPI_SyncSurface &&
      VAAPI_BeginPicture &&
      VAAPI_RenderPicture &&
      VAAPI_EndPicture &&
      VAAPI_CreateBuffer &&
      VAAPI_DestroyBuffer &&
      VAAPI_ErrorStr;
}

}  // namespace content