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
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
|
/* GStreamer
* Copyright (C) 2008 David Schleef <ds@schleef.org>
* Copyright (C) 2011 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>.
* Copyright (C) 2011 Nokia Corporation. All rights reserved.
* Contact: Stefan Kost <stefan.kost@nokia.com>
* Copyright (C) 2012 Collabora Ltd.
* Author : Edward Hervey <edward@collabora.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:gstvideoencoder
* @short_description: Base class for video encoders
* @see_also:
*
* This base class is for video encoders turning raw video into
* encoded video data.
*
* GstVideoEncoder and subclass should cooperate as follows.
* <orderedlist>
* <listitem>
* <itemizedlist><title>Configuration</title>
* <listitem><para>
* Initially, GstVideoEncoder calls @start when the encoder element
* is activated, which allows subclass to perform any global setup.
* </para></listitem>
* <listitem><para>
* GstVideoEncoder calls @set_format to inform subclass of the format
* of input video data that it is about to receive. Subclass should
* setup for encoding and configure base class as appropriate
* (e.g. latency). While unlikely, it might be called more than once,
* if changing input parameters require reconfiguration. Baseclass
* will ensure that processing of current configuration is finished.
* </para></listitem>
* <listitem><para>
* GstVideoEncoder calls @stop at end of all processing.
* </para></listitem>
* </itemizedlist>
* </listitem>
* <listitem>
* <itemizedlist>
* <title>Data processing</title>
* <listitem><para>
* Base class collects input data and metadata into a frame and hands
* this to subclass' @handle_frame.
* </para></listitem>
* <listitem><para>
* If codec processing results in encoded data, subclass should call
* @gst_video_encoder_finish_frame to have encoded data pushed
* downstream.
* </para></listitem>
* <listitem><para>
* If implemented, baseclass calls subclass @pre_push just prior to
* pushing to allow subclasses to modify some metadata on the buffer.
* If it returns GST_FLOW_OK, the buffer is pushed downstream.
* </para></listitem>
* <listitem><para>
* GstVideoEncoderClass will handle both srcpad and sinkpad events.
* Sink events will be passed to subclass if @event callback has been
* provided.
* </para></listitem>
* </itemizedlist>
* </listitem>
* <listitem>
* <itemizedlist><title>Shutdown phase</title>
* <listitem><para>
* GstVideoEncoder class calls @stop to inform the subclass that data
* parsing will be stopped.
* </para></listitem>
* </itemizedlist>
* </listitem>
* </orderedlist>
*
* Subclass is responsible for providing pad template caps for
* source and sink pads. The pads need to be named "sink" and "src". It should
* also be able to provide fixed src pad caps in @getcaps by the time it calls
* @gst_video_encoder_finish_frame.
*
* Things that subclass need to take care of:
* <itemizedlist>
* <listitem><para>Provide pad templates</para></listitem>
* <listitem><para>
* Provide source pad caps before pushing the first buffer
* </para></listitem>
* <listitem><para>
* Accept data in @handle_frame and provide encoded results to
* @gst_video_encoder_finish_frame.
* </para></listitem>
* </itemizedlist>
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/* TODO
*
* * Calculate actual latency based on input/output timestamp/frame_number
* and if it exceeds the recorded one, save it and emit a GST_MESSAGE_LATENCY
*/
#include <gst/video/video.h>
#include "gstvideoencoder.h"
#include "gstvideoutils.h"
#include "gstvideoutilsprivate.h"
#include <gst/video/gstvideometa.h>
#include <gst/video/gstvideopool.h>
#include <string.h>
GST_DEBUG_CATEGORY (videoencoder_debug);
#define GST_CAT_DEFAULT videoencoder_debug
#define GST_VIDEO_ENCODER_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_VIDEO_ENCODER, \
GstVideoEncoderPrivate))
struct _GstVideoEncoderPrivate
{
guint64 presentation_frame_number;
int distance_from_sync;
/* FIXME : (and introduce a context ?) */
gboolean drained;
gint64 min_latency;
gint64 max_latency;
GList *current_frame_events;
GList *headers;
gboolean new_headers; /* Whether new headers were just set */
GList *force_key_unit; /* List of pending forced keyunits */
guint32 system_frame_number;
GList *frames; /* Protected with OBJECT_LOCK */
GstVideoCodecState *input_state;
GstVideoCodecState *output_state;
gboolean output_state_changed;
gint64 bytes;
gint64 time;
GstAllocator *allocator;
GstAllocationParams params;
/* upstream stream tags (global tags are passed through as-is) */
GstTagList *upstream_tags;
/* subclass tags */
GstTagList *tags;
GstTagMergeMode tags_merge_mode;
gboolean tags_changed;
GstClockTime min_pts;
/* adjustment needed on pts, dts, segment start and stop to accomodate
* min_pts */
GstClockTime time_adjustment;
};
typedef struct _ForcedKeyUnitEvent ForcedKeyUnitEvent;
struct _ForcedKeyUnitEvent
{
GstClockTime running_time;
gboolean pending; /* TRUE if this was requested already */
gboolean all_headers;
guint count;
guint32 frame_id;
};
static void
forced_key_unit_event_free (ForcedKeyUnitEvent * evt)
{
g_slice_free (ForcedKeyUnitEvent, evt);
}
static ForcedKeyUnitEvent *
forced_key_unit_event_new (GstClockTime running_time, gboolean all_headers,
guint count)
{
ForcedKeyUnitEvent *evt = g_slice_new0 (ForcedKeyUnitEvent);
evt->running_time = running_time;
evt->all_headers = all_headers;
evt->count = count;
return evt;
}
static GstElementClass *parent_class = NULL;
static void gst_video_encoder_class_init (GstVideoEncoderClass * klass);
static void gst_video_encoder_init (GstVideoEncoder * enc,
GstVideoEncoderClass * klass);
static void gst_video_encoder_finalize (GObject * object);
static void
gst_video_encoder_release_frame (GstVideoEncoder * enc,
GstVideoCodecFrame * frame);
static gboolean gst_video_encoder_setcaps (GstVideoEncoder * enc,
GstCaps * caps);
static GstCaps *gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder,
GstCaps * filter);
static gboolean gst_video_encoder_src_event (GstPad * pad, GstObject * parent,
GstEvent * event);
static gboolean gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
GstEvent * event);
static GstFlowReturn gst_video_encoder_chain (GstPad * pad, GstObject * parent,
GstBuffer * buf);
static GstStateChangeReturn gst_video_encoder_change_state (GstElement *
element, GstStateChange transition);
static gboolean gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
GstQuery * query);
static gboolean gst_video_encoder_src_query (GstPad * pad, GstObject * parent,
GstQuery * query);
static GstVideoCodecFrame *gst_video_encoder_new_frame (GstVideoEncoder *
encoder, GstBuffer * buf, GstClockTime pts, GstClockTime dts,
GstClockTime duration);
static gboolean gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
GstEvent * event);
static gboolean gst_video_encoder_src_event_default (GstVideoEncoder * encoder,
GstEvent * event);
static gboolean gst_video_encoder_decide_allocation_default (GstVideoEncoder *
encoder, GstQuery * query);
static gboolean gst_video_encoder_propose_allocation_default (GstVideoEncoder *
encoder, GstQuery * query);
static gboolean gst_video_encoder_negotiate_default (GstVideoEncoder * encoder);
static gboolean gst_video_encoder_negotiate_unlocked (GstVideoEncoder *
encoder);
static gboolean gst_video_encoder_sink_query_default (GstVideoEncoder * encoder,
GstQuery * query);
static gboolean gst_video_encoder_src_query_default (GstVideoEncoder * encoder,
GstQuery * query);
static gboolean gst_video_encoder_transform_meta_default (GstVideoEncoder *
encoder, GstVideoCodecFrame * frame, GstMeta * meta);
/* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
* method to get to the padtemplates */
GType
gst_video_encoder_get_type (void)
{
static volatile gsize type = 0;
if (g_once_init_enter (&type)) {
GType _type;
static const GTypeInfo info = {
sizeof (GstVideoEncoderClass),
NULL,
NULL,
(GClassInitFunc) gst_video_encoder_class_init,
NULL,
NULL,
sizeof (GstVideoEncoder),
0,
(GInstanceInitFunc) gst_video_encoder_init,
};
const GInterfaceInfo preset_interface_info = {
NULL, /* interface_init */
NULL, /* interface_finalize */
NULL /* interface_data */
};
_type = g_type_register_static (GST_TYPE_ELEMENT,
"GstVideoEncoder", &info, G_TYPE_FLAG_ABSTRACT);
g_type_add_interface_static (_type, GST_TYPE_PRESET,
&preset_interface_info);
g_once_init_leave (&type, _type);
}
return type;
}
static void
gst_video_encoder_class_init (GstVideoEncoderClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
gobject_class = G_OBJECT_CLASS (klass);
gstelement_class = GST_ELEMENT_CLASS (klass);
GST_DEBUG_CATEGORY_INIT (videoencoder_debug, "videoencoder", 0,
"Base Video Encoder");
parent_class = g_type_class_peek_parent (klass);
g_type_class_add_private (klass, sizeof (GstVideoEncoderPrivate));
gobject_class->finalize = gst_video_encoder_finalize;
gstelement_class->change_state =
GST_DEBUG_FUNCPTR (gst_video_encoder_change_state);
klass->sink_event = gst_video_encoder_sink_event_default;
klass->src_event = gst_video_encoder_src_event_default;
klass->propose_allocation = gst_video_encoder_propose_allocation_default;
klass->decide_allocation = gst_video_encoder_decide_allocation_default;
klass->negotiate = gst_video_encoder_negotiate_default;
klass->sink_query = gst_video_encoder_sink_query_default;
klass->src_query = gst_video_encoder_src_query_default;
klass->transform_meta = gst_video_encoder_transform_meta_default;
}
static GList *
_flush_events (GstPad * pad, GList * events)
{
GList *tmp;
for (tmp = events; tmp; tmp = tmp->next) {
if (GST_EVENT_TYPE (tmp->data) != GST_EVENT_EOS &&
GST_EVENT_TYPE (tmp->data) != GST_EVENT_SEGMENT &&
GST_EVENT_IS_STICKY (tmp->data)) {
gst_pad_store_sticky_event (pad, GST_EVENT_CAST (tmp->data));
}
gst_event_unref (tmp->data);
}
g_list_free (events);
return NULL;
}
static gboolean
gst_video_encoder_reset (GstVideoEncoder * encoder, gboolean hard)
{
GstVideoEncoderPrivate *priv = encoder->priv;
gboolean ret = TRUE;
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
priv->presentation_frame_number = 0;
priv->distance_from_sync = 0;
g_list_foreach (priv->force_key_unit, (GFunc) forced_key_unit_event_free,
NULL);
g_list_free (priv->force_key_unit);
priv->force_key_unit = NULL;
priv->drained = TRUE;
priv->bytes = 0;
priv->time = 0;
priv->time_adjustment = GST_CLOCK_TIME_NONE;
if (hard) {
gst_segment_init (&encoder->input_segment, GST_FORMAT_TIME);
gst_segment_init (&encoder->output_segment, GST_FORMAT_TIME);
if (priv->input_state)
gst_video_codec_state_unref (priv->input_state);
priv->input_state = NULL;
if (priv->output_state)
gst_video_codec_state_unref (priv->output_state);
priv->output_state = NULL;
if (priv->upstream_tags) {
gst_tag_list_unref (priv->upstream_tags);
priv->upstream_tags = NULL;
}
if (priv->tags)
gst_tag_list_unref (priv->tags);
priv->tags = NULL;
priv->tags_merge_mode = GST_TAG_MERGE_APPEND;
priv->tags_changed = FALSE;
g_list_foreach (priv->headers, (GFunc) gst_event_unref, NULL);
g_list_free (priv->headers);
priv->headers = NULL;
priv->new_headers = FALSE;
if (priv->allocator) {
gst_object_unref (priv->allocator);
priv->allocator = NULL;
}
g_list_foreach (priv->current_frame_events, (GFunc) gst_event_unref, NULL);
g_list_free (priv->current_frame_events);
priv->current_frame_events = NULL;
} else {
GList *l;
for (l = priv->frames; l; l = l->next) {
GstVideoCodecFrame *frame = l->data;
frame->events = _flush_events (encoder->srcpad, frame->events);
}
priv->current_frame_events = _flush_events (encoder->srcpad,
encoder->priv->current_frame_events);
}
g_list_foreach (priv->frames, (GFunc) gst_video_codec_frame_unref, NULL);
g_list_free (priv->frames);
priv->frames = NULL;
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
return ret;
}
/* Always call reset() in one way or another after this */
static gboolean
gst_video_encoder_flush (GstVideoEncoder * encoder)
{
GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
gboolean ret = TRUE;
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
if (klass->flush)
ret = klass->flush (encoder);
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
return ret;
}
static void
gst_video_encoder_init (GstVideoEncoder * encoder, GstVideoEncoderClass * klass)
{
GstVideoEncoderPrivate *priv;
GstPadTemplate *pad_template;
GstPad *pad;
GST_DEBUG_OBJECT (encoder, "gst_video_encoder_init");
priv = encoder->priv = GST_VIDEO_ENCODER_GET_PRIVATE (encoder);
pad_template =
gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
g_return_if_fail (pad_template != NULL);
encoder->sinkpad = pad = gst_pad_new_from_template (pad_template, "sink");
gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_video_encoder_chain));
gst_pad_set_event_function (pad,
GST_DEBUG_FUNCPTR (gst_video_encoder_sink_event));
gst_pad_set_query_function (pad,
GST_DEBUG_FUNCPTR (gst_video_encoder_sink_query));
gst_element_add_pad (GST_ELEMENT (encoder), encoder->sinkpad);
pad_template =
gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
g_return_if_fail (pad_template != NULL);
encoder->srcpad = pad = gst_pad_new_from_template (pad_template, "src");
gst_pad_set_query_function (pad,
GST_DEBUG_FUNCPTR (gst_video_encoder_src_query));
gst_pad_set_event_function (pad,
GST_DEBUG_FUNCPTR (gst_video_encoder_src_event));
gst_element_add_pad (GST_ELEMENT (encoder), encoder->srcpad);
gst_segment_init (&encoder->input_segment, GST_FORMAT_TIME);
gst_segment_init (&encoder->output_segment, GST_FORMAT_TIME);
g_rec_mutex_init (&encoder->stream_lock);
priv->headers = NULL;
priv->new_headers = FALSE;
priv->min_latency = 0;
priv->max_latency = 0;
priv->min_pts = GST_CLOCK_TIME_NONE;
priv->time_adjustment = GST_CLOCK_TIME_NONE;
gst_video_encoder_reset (encoder, TRUE);
}
static gboolean
gst_video_encoded_video_convert (gint64 bytes, gint64 time,
GstFormat src_format, gint64 src_value, GstFormat * dest_format,
gint64 * dest_value)
{
gboolean res = FALSE;
g_return_val_if_fail (dest_format != NULL, FALSE);
g_return_val_if_fail (dest_value != NULL, FALSE);
if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
src_value == -1)) {
if (dest_value)
*dest_value = src_value;
return TRUE;
}
if (bytes <= 0 || time <= 0) {
GST_DEBUG ("not enough metadata yet to convert");
goto exit;
}
switch (src_format) {
case GST_FORMAT_BYTES:
switch (*dest_format) {
case GST_FORMAT_TIME:
*dest_value = gst_util_uint64_scale (src_value, time, bytes);
res = TRUE;
break;
default:
res = FALSE;
}
break;
case GST_FORMAT_TIME:
switch (*dest_format) {
case GST_FORMAT_BYTES:
*dest_value = gst_util_uint64_scale (src_value, bytes, time);
res = TRUE;
break;
default:
res = FALSE;
}
break;
default:
GST_DEBUG ("unhandled conversion from %d to %d", src_format,
*dest_format);
res = FALSE;
}
exit:
return res;
}
/**
* gst_video_encoder_set_headers:
* @encoder: a #GstVideoEncoder
* @headers: (transfer full) (element-type GstBuffer): a list of #GstBuffer containing the codec header
*
* Set the codec headers to be sent downstream whenever requested.
*/
void
gst_video_encoder_set_headers (GstVideoEncoder * video_encoder, GList * headers)
{
GST_VIDEO_ENCODER_STREAM_LOCK (video_encoder);
GST_DEBUG_OBJECT (video_encoder, "new headers %p", headers);
if (video_encoder->priv->headers) {
g_list_foreach (video_encoder->priv->headers, (GFunc) gst_buffer_unref,
NULL);
g_list_free (video_encoder->priv->headers);
}
video_encoder->priv->headers = headers;
video_encoder->priv->new_headers = TRUE;
GST_VIDEO_ENCODER_STREAM_UNLOCK (video_encoder);
}
static GstVideoCodecState *
_new_output_state (GstCaps * caps, GstVideoCodecState * reference)
{
GstVideoCodecState *state;
state = g_slice_new0 (GstVideoCodecState);
state->ref_count = 1;
gst_video_info_init (&state->info);
gst_video_info_set_format (&state->info, GST_VIDEO_FORMAT_ENCODED, 0, 0);
state->caps = caps;
if (reference) {
GstVideoInfo *tgt, *ref;
tgt = &state->info;
ref = &reference->info;
/* Copy over extra fields from reference state */
tgt->interlace_mode = ref->interlace_mode;
tgt->flags = ref->flags;
tgt->width = ref->width;
tgt->height = ref->height;
tgt->chroma_site = ref->chroma_site;
tgt->colorimetry = ref->colorimetry;
tgt->par_n = ref->par_n;
tgt->par_d = ref->par_d;
tgt->fps_n = ref->fps_n;
tgt->fps_d = ref->fps_d;
GST_VIDEO_INFO_MULTIVIEW_MODE (tgt) = GST_VIDEO_INFO_MULTIVIEW_MODE (ref);
GST_VIDEO_INFO_MULTIVIEW_FLAGS (tgt) = GST_VIDEO_INFO_MULTIVIEW_FLAGS (ref);
}
return state;
}
static GstVideoCodecState *
_new_input_state (GstCaps * caps)
{
GstVideoCodecState *state;
state = g_slice_new0 (GstVideoCodecState);
state->ref_count = 1;
gst_video_info_init (&state->info);
if (G_UNLIKELY (!gst_video_info_from_caps (&state->info, caps)))
goto parse_fail;
state->caps = gst_caps_ref (caps);
return state;
parse_fail:
{
g_slice_free (GstVideoCodecState, state);
return NULL;
}
}
static gboolean
gst_video_encoder_setcaps (GstVideoEncoder * encoder, GstCaps * caps)
{
GstVideoEncoderClass *encoder_class;
GstVideoCodecState *state;
gboolean ret;
encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
/* subclass should do something here ... */
g_return_val_if_fail (encoder_class->set_format != NULL, FALSE);
GST_DEBUG_OBJECT (encoder, "setcaps %" GST_PTR_FORMAT, caps);
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
if (encoder->priv->input_state) {
GST_DEBUG_OBJECT (encoder,
"Checking if caps changed old %" GST_PTR_FORMAT " new %" GST_PTR_FORMAT,
encoder->priv->input_state->caps, caps);
if (gst_caps_is_equal (encoder->priv->input_state->caps, caps))
goto caps_not_changed;
}
state = _new_input_state (caps);
if (G_UNLIKELY (!state))
goto parse_fail;
if (encoder->priv->input_state
&& gst_video_info_is_equal (&state->info,
&encoder->priv->input_state->info)) {
gst_video_codec_state_unref (state);
goto caps_not_changed;
}
if (encoder_class->reset) {
GST_FIXME_OBJECT (encoder, "GstVideoEncoder::reset() is deprecated");
encoder_class->reset (encoder, TRUE);
}
/* and subclass should be ready to configure format at any time around */
ret = encoder_class->set_format (encoder, state);
if (ret) {
if (encoder->priv->input_state)
gst_video_codec_state_unref (encoder->priv->input_state);
encoder->priv->input_state = state;
} else {
gst_video_codec_state_unref (state);
}
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
if (!ret)
GST_WARNING_OBJECT (encoder, "rejected caps %" GST_PTR_FORMAT, caps);
return ret;
caps_not_changed:
{
GST_DEBUG_OBJECT (encoder, "Caps did not change - ignore");
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
return TRUE;
}
/* ERRORS */
parse_fail:
{
GST_WARNING_OBJECT (encoder, "Failed to parse caps");
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
return FALSE;
}
}
/**
* gst_video_encoder_proxy_getcaps:
* @enc: a #GstVideoEncoder
* @caps: (allow-none): initial caps
* @filter: (allow-none): filter caps
*
* Returns caps that express @caps (or sink template caps if @caps == NULL)
* restricted to resolution/format/... combinations supported by downstream
* elements (e.g. muxers).
*
* Returns: (transfer full): a #GstCaps owned by caller
*/
GstCaps *
gst_video_encoder_proxy_getcaps (GstVideoEncoder * encoder, GstCaps * caps,
GstCaps * filter)
{
return __gst_video_element_proxy_getcaps (GST_ELEMENT_CAST (encoder),
GST_VIDEO_ENCODER_SINK_PAD (encoder),
GST_VIDEO_ENCODER_SRC_PAD (encoder), caps, filter);
}
static GstCaps *
gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder, GstCaps * filter)
{
GstVideoEncoderClass *klass;
GstCaps *caps;
klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
if (klass->getcaps)
caps = klass->getcaps (encoder, filter);
else
caps = gst_video_encoder_proxy_getcaps (encoder, NULL, filter);
GST_LOG_OBJECT (encoder, "Returning caps %" GST_PTR_FORMAT, caps);
return caps;
}
static gboolean
gst_video_encoder_decide_allocation_default (GstVideoEncoder * encoder,
GstQuery * query)
{
GstAllocator *allocator = NULL;
GstAllocationParams params;
gboolean update_allocator;
/* we got configuration from our peer or the decide_allocation method,
* parse them */
if (gst_query_get_n_allocation_params (query) > 0) {
/* try the allocator */
gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
update_allocator = TRUE;
} else {
allocator = NULL;
gst_allocation_params_init (¶ms);
update_allocator = FALSE;
}
if (update_allocator)
gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
else
gst_query_add_allocation_param (query, allocator, ¶ms);
if (allocator)
gst_object_unref (allocator);
return TRUE;
}
static gboolean
gst_video_encoder_propose_allocation_default (GstVideoEncoder * encoder,
GstQuery * query)
{
GstCaps *caps;
GstVideoInfo info;
GstBufferPool *pool;
guint size;
gst_query_parse_allocation (query, &caps, NULL);
if (caps == NULL)
return FALSE;
if (!gst_video_info_from_caps (&info, caps))
return FALSE;
size = GST_VIDEO_INFO_SIZE (&info);
if (gst_query_get_n_allocation_pools (query) == 0) {
GstStructure *structure;
GstAllocator *allocator = NULL;
GstAllocationParams params = { 0, 15, 0, 0 };
if (gst_query_get_n_allocation_params (query) > 0)
gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
else
gst_query_add_allocation_param (query, allocator, ¶ms);
pool = gst_video_buffer_pool_new ();
structure = gst_buffer_pool_get_config (pool);
gst_buffer_pool_config_set_params (structure, caps, size, 0, 0);
gst_buffer_pool_config_set_allocator (structure, allocator, ¶ms);
if (allocator)
gst_object_unref (allocator);
if (!gst_buffer_pool_set_config (pool, structure))
goto config_failed;
gst_query_add_allocation_pool (query, pool, size, 0, 0);
gst_object_unref (pool);
gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
}
return TRUE;
/* ERRORS */
config_failed:
{
GST_ERROR_OBJECT (encoder, "failed to set config");
gst_object_unref (pool);
return FALSE;
}
}
static gboolean
gst_video_encoder_sink_query_default (GstVideoEncoder * encoder,
GstQuery * query)
{
GstPad *pad = GST_VIDEO_ENCODER_SINK_PAD (encoder);
gboolean res = FALSE;
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_CAPS:
{
GstCaps *filter, *caps;
gst_query_parse_caps (query, &filter);
caps = gst_video_encoder_sink_getcaps (encoder, filter);
gst_query_set_caps_result (query, caps);
gst_caps_unref (caps);
res = TRUE;
break;
}
case GST_QUERY_ALLOCATION:
{
GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
if (klass->propose_allocation)
res = klass->propose_allocation (encoder, query);
break;
}
default:
res = gst_pad_query_default (pad, GST_OBJECT (encoder), query);
break;
}
return res;
}
static gboolean
gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
GstQuery * query)
{
GstVideoEncoder *encoder;
GstVideoEncoderClass *encoder_class;
gboolean ret = FALSE;
encoder = GST_VIDEO_ENCODER (parent);
encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
GST_DEBUG_OBJECT (encoder, "received query %d, %s", GST_QUERY_TYPE (query),
GST_QUERY_TYPE_NAME (query));
if (encoder_class->sink_query)
ret = encoder_class->sink_query (encoder, query);
return ret;
}
static void
gst_video_encoder_finalize (GObject * object)
{
GstVideoEncoder *encoder;
GST_DEBUG_OBJECT (object, "finalize");
encoder = GST_VIDEO_ENCODER (object);
g_rec_mutex_clear (&encoder->stream_lock);
if (encoder->priv->allocator) {
gst_object_unref (encoder->priv->allocator);
encoder->priv->allocator = NULL;
}
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static gboolean
gst_video_encoder_push_event (GstVideoEncoder * encoder, GstEvent * event)
{
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_SEGMENT:
{
GstSegment segment;
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
gst_event_copy_segment (event, &segment);
GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
if (segment.format != GST_FORMAT_TIME) {
GST_DEBUG_OBJECT (encoder, "received non TIME segment");
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
break;
}
if (encoder->priv->time_adjustment != GST_CLOCK_TIME_NONE) {
segment.start += encoder->priv->time_adjustment;
if (GST_CLOCK_TIME_IS_VALID (segment.stop)) {
segment.stop += encoder->priv->time_adjustment;
}
}
encoder->output_segment = segment;
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
gst_event_unref (event);
event = gst_event_new_segment (&encoder->output_segment);
break;
}
default:
break;
}
return gst_pad_push_event (encoder->srcpad, event);
}
static GstEvent *
gst_video_encoder_create_merged_tags_event (GstVideoEncoder * enc)
{
GstTagList *merged_tags;
GST_LOG_OBJECT (enc, "upstream : %" GST_PTR_FORMAT, enc->priv->upstream_tags);
GST_LOG_OBJECT (enc, "encoder : %" GST_PTR_FORMAT, enc->priv->tags);
GST_LOG_OBJECT (enc, "mode : %d", enc->priv->tags_merge_mode);
merged_tags =
gst_tag_list_merge (enc->priv->upstream_tags, enc->priv->tags,
enc->priv->tags_merge_mode);
GST_DEBUG_OBJECT (enc, "merged : %" GST_PTR_FORMAT, merged_tags);
if (merged_tags == NULL)
return NULL;
if (gst_tag_list_is_empty (merged_tags)) {
gst_tag_list_unref (merged_tags);
return NULL;
}
return gst_event_new_tag (merged_tags);
}
static inline void
gst_video_encoder_check_and_push_tags (GstVideoEncoder * encoder)
{
if (encoder->priv->tags_changed) {
GstEvent *tags_event;
tags_event = gst_video_encoder_create_merged_tags_event (encoder);
if (tags_event != NULL)
gst_video_encoder_push_event (encoder, tags_event);
encoder->priv->tags_changed = FALSE;
}
}
static gboolean
gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
GstEvent * event)
{
GstVideoEncoderClass *encoder_class;
gboolean ret = FALSE;
encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_CAPS:
{
GstCaps *caps;
gst_event_parse_caps (event, &caps);
ret = gst_video_encoder_setcaps (encoder, caps);
gst_event_unref (event);
event = NULL;
break;
}
case GST_EVENT_EOS:
{
GstFlowReturn flow_ret;
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
if (encoder_class->finish) {
flow_ret = encoder_class->finish (encoder);
} else {
flow_ret = GST_FLOW_OK;
}
if (encoder->priv->current_frame_events) {
GList *l;
for (l = g_list_last (encoder->priv->current_frame_events); l;
l = g_list_previous (l)) {
GstEvent *event = GST_EVENT (l->data);
gst_video_encoder_push_event (encoder, event);
}
}
g_list_free (encoder->priv->current_frame_events);
encoder->priv->current_frame_events = NULL;
gst_video_encoder_check_and_push_tags (encoder);
ret = (flow_ret == GST_FLOW_OK);
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
break;
}
case GST_EVENT_SEGMENT:
{
GstSegment segment;
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
gst_event_copy_segment (event, &segment);
GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
if (segment.format != GST_FORMAT_TIME) {
GST_DEBUG_OBJECT (encoder, "received non TIME newsegment");
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
break;
}
encoder->input_segment = segment;
ret = TRUE;
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
break;
}
case GST_EVENT_CUSTOM_DOWNSTREAM:
{
if (gst_video_event_is_force_key_unit (event)) {
GstClockTime running_time;
gboolean all_headers;
guint count;
if (gst_video_event_parse_downstream_force_key_unit (event,
NULL, NULL, &running_time, &all_headers, &count)) {
ForcedKeyUnitEvent *fevt;
GST_OBJECT_LOCK (encoder);
fevt = forced_key_unit_event_new (running_time, all_headers, count);
encoder->priv->force_key_unit =
g_list_append (encoder->priv->force_key_unit, fevt);
GST_OBJECT_UNLOCK (encoder);
GST_DEBUG_OBJECT (encoder,
"force-key-unit event: running-time %" GST_TIME_FORMAT
", all_headers %d, count %u",
GST_TIME_ARGS (running_time), all_headers, count);
}
gst_event_unref (event);
event = NULL;
ret = TRUE;
}
break;
}
case GST_EVENT_STREAM_START:
{
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
/* Flush upstream tags after a STREAM_START */
GST_DEBUG_OBJECT (encoder, "STREAM_START, clearing upstream tags");
if (encoder->priv->upstream_tags) {
gst_tag_list_unref (encoder->priv->upstream_tags);
encoder->priv->upstream_tags = NULL;
encoder->priv->tags_changed = TRUE;
}
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
break;
}
case GST_EVENT_TAG:
{
GstTagList *tags;
gst_event_parse_tag (event, &tags);
if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
if (encoder->priv->upstream_tags != tags) {
tags = gst_tag_list_copy (tags);
/* FIXME: make generic based on GST_TAG_FLAG_ENCODED */
gst_tag_list_remove_tag (tags, GST_TAG_CODEC);
gst_tag_list_remove_tag (tags, GST_TAG_AUDIO_CODEC);
gst_tag_list_remove_tag (tags, GST_TAG_VIDEO_CODEC);
gst_tag_list_remove_tag (tags, GST_TAG_SUBTITLE_CODEC);
gst_tag_list_remove_tag (tags, GST_TAG_CONTAINER_FORMAT);
gst_tag_list_remove_tag (tags, GST_TAG_BITRATE);
gst_tag_list_remove_tag (tags, GST_TAG_NOMINAL_BITRATE);
gst_tag_list_remove_tag (tags, GST_TAG_MAXIMUM_BITRATE);
gst_tag_list_remove_tag (tags, GST_TAG_MINIMUM_BITRATE);
gst_tag_list_remove_tag (tags, GST_TAG_ENCODER);
gst_tag_list_remove_tag (tags, GST_TAG_ENCODER_VERSION);
if (encoder->priv->upstream_tags)
gst_tag_list_unref (encoder->priv->upstream_tags);
encoder->priv->upstream_tags = tags;
GST_INFO_OBJECT (encoder, "upstream tags: %" GST_PTR_FORMAT, tags);
}
gst_event_unref (event);
event = gst_video_encoder_create_merged_tags_event (encoder);
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
if (!event)
ret = TRUE;
}
break;
}
case GST_EVENT_FLUSH_STOP:{
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
gst_video_encoder_flush (encoder);
gst_segment_init (&encoder->input_segment, GST_FORMAT_TIME);
gst_segment_init (&encoder->output_segment, GST_FORMAT_TIME);
gst_video_encoder_reset (encoder, FALSE);
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
break;
}
default:
break;
}
/* Forward non-serialized events and EOS/FLUSH_STOP immediately.
* For EOS this is required because no buffer or serialized event
* will come after EOS and nothing could trigger another
* _finish_frame() call. *
* If the subclass handles sending of EOS manually it can simply
* not chain up to the parent class' event handler
*
* For FLUSH_STOP this is required because it is expected
* to be forwarded immediately and no buffers are queued anyway.
*/
if (event) {
if (!GST_EVENT_IS_SERIALIZED (event)
|| GST_EVENT_TYPE (event) == GST_EVENT_EOS
|| GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
ret = gst_video_encoder_push_event (encoder, event);
} else {
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
encoder->priv->current_frame_events =
g_list_prepend (encoder->priv->current_frame_events, event);
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
ret = TRUE;
}
}
return ret;
}
static gboolean
gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
GstEvent * event)
{
GstVideoEncoder *enc;
GstVideoEncoderClass *klass;
gboolean ret = TRUE;
enc = GST_VIDEO_ENCODER (parent);
klass = GST_VIDEO_ENCODER_GET_CLASS (enc);
GST_DEBUG_OBJECT (enc, "received event %d, %s", GST_EVENT_TYPE (event),
GST_EVENT_TYPE_NAME (event));
if (klass->sink_event)
ret = klass->sink_event (enc, event);
return ret;
}
static gboolean
gst_video_encoder_src_event_default (GstVideoEncoder * encoder,
GstEvent * event)
{
gboolean ret = FALSE;
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_CUSTOM_UPSTREAM:
{
if (gst_video_event_is_force_key_unit (event)) {
GstClockTime running_time;
gboolean all_headers;
guint count;
if (gst_video_event_parse_upstream_force_key_unit (event,
&running_time, &all_headers, &count)) {
ForcedKeyUnitEvent *fevt;
GST_OBJECT_LOCK (encoder);
fevt = forced_key_unit_event_new (running_time, all_headers, count);
encoder->priv->force_key_unit =
g_list_append (encoder->priv->force_key_unit, fevt);
GST_OBJECT_UNLOCK (encoder);
GST_DEBUG_OBJECT (encoder,
"force-key-unit event: running-time %" GST_TIME_FORMAT
", all_headers %d, count %u",
GST_TIME_ARGS (running_time), all_headers, count);
}
gst_event_unref (event);
event = NULL;
ret = TRUE;
}
break;
}
default:
break;
}
if (event)
ret =
gst_pad_event_default (encoder->srcpad, GST_OBJECT_CAST (encoder),
event);
return ret;
}
static gboolean
gst_video_encoder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
{
GstVideoEncoder *encoder;
GstVideoEncoderClass *klass;
gboolean ret = FALSE;
encoder = GST_VIDEO_ENCODER (parent);
klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
GST_LOG_OBJECT (encoder, "handling event: %" GST_PTR_FORMAT, event);
if (klass->src_event)
ret = klass->src_event (encoder, event);
return ret;
}
static gboolean
gst_video_encoder_src_query_default (GstVideoEncoder * enc, GstQuery * query)
{
GstPad *pad = GST_VIDEO_ENCODER_SRC_PAD (enc);
GstVideoEncoderPrivate *priv;
gboolean res;
priv = enc->priv;
GST_LOG_OBJECT (enc, "handling query: %" GST_PTR_FORMAT, query);
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_CONVERT:
{
GstFormat src_fmt, dest_fmt;
gint64 src_val, dest_val;
gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
res =
gst_video_encoded_video_convert (priv->bytes, priv->time, src_fmt,
src_val, &dest_fmt, &dest_val);
if (!res)
goto error;
gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
break;
}
case GST_QUERY_LATENCY:
{
gboolean live;
GstClockTime min_latency, max_latency;
res = gst_pad_peer_query (enc->sinkpad, query);
if (res) {
gst_query_parse_latency (query, &live, &min_latency, &max_latency);
GST_DEBUG_OBJECT (enc, "Peer latency: live %d, min %"
GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
GST_OBJECT_LOCK (enc);
min_latency += priv->min_latency;
if (max_latency == GST_CLOCK_TIME_NONE
|| enc->priv->max_latency == GST_CLOCK_TIME_NONE)
max_latency = GST_CLOCK_TIME_NONE;
else
max_latency += enc->priv->max_latency;
GST_OBJECT_UNLOCK (enc);
gst_query_set_latency (query, live, min_latency, max_latency);
}
}
break;
default:
res = gst_pad_query_default (pad, GST_OBJECT (enc), query);
}
return res;
error:
GST_DEBUG_OBJECT (enc, "query failed");
return res;
}
static gboolean
gst_video_encoder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
{
GstVideoEncoder *encoder;
GstVideoEncoderClass *encoder_class;
gboolean ret = FALSE;
encoder = GST_VIDEO_ENCODER (parent);
encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
GST_DEBUG_OBJECT (encoder, "received query %d, %s", GST_QUERY_TYPE (query),
GST_QUERY_TYPE_NAME (query));
if (encoder_class->src_query)
ret = encoder_class->src_query (encoder, query);
return ret;
}
static GstVideoCodecFrame *
gst_video_encoder_new_frame (GstVideoEncoder * encoder, GstBuffer * buf,
GstClockTime pts, GstClockTime dts, GstClockTime duration)
{
GstVideoEncoderPrivate *priv = encoder->priv;
GstVideoCodecFrame *frame;
frame = g_slice_new0 (GstVideoCodecFrame);
frame->ref_count = 1;
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
frame->system_frame_number = priv->system_frame_number;
priv->system_frame_number++;
frame->presentation_frame_number = priv->presentation_frame_number;
priv->presentation_frame_number++;
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
frame->events = priv->current_frame_events;
priv->current_frame_events = NULL;
frame->input_buffer = buf;
frame->pts = pts;
frame->dts = dts;
frame->duration = duration;
frame->abidata.ABI.ts = pts;
return frame;
}
static GstFlowReturn
gst_video_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
{
GstVideoEncoder *encoder;
GstVideoEncoderPrivate *priv;
GstVideoEncoderClass *klass;
GstVideoCodecFrame *frame;
GstClockTime pts, duration;
GstFlowReturn ret = GST_FLOW_OK;
guint64 start, stop, cstart, cstop;
encoder = GST_VIDEO_ENCODER (parent);
priv = encoder->priv;
klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
if (!encoder->priv->input_state)
goto not_negotiated;
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
pts = GST_BUFFER_PTS (buf);
duration = GST_BUFFER_DURATION (buf);
GST_LOG_OBJECT (encoder,
"received buffer of size %" G_GSIZE_FORMAT " with PTS %" GST_TIME_FORMAT
", DTS %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
gst_buffer_get_size (buf), GST_TIME_ARGS (pts),
GST_TIME_ARGS (GST_BUFFER_DTS (buf)), GST_TIME_ARGS (duration));
start = pts;
if (GST_CLOCK_TIME_IS_VALID (duration))
stop = start + duration;
else
stop = GST_CLOCK_TIME_NONE;
/* Drop buffers outside of segment */
if (!gst_segment_clip (&encoder->input_segment,
GST_FORMAT_TIME, start, stop, &cstart, &cstop)) {
GST_DEBUG_OBJECT (encoder, "clipping to segment dropped frame");
gst_buffer_unref (buf);
goto done;
}
if (GST_CLOCK_TIME_IS_VALID (cstop))
duration = cstop - cstart;
else
duration = GST_CLOCK_TIME_NONE;
if (priv->min_pts != GST_CLOCK_TIME_NONE
&& priv->time_adjustment == GST_CLOCK_TIME_NONE) {
if (cstart < priv->min_pts) {
priv->time_adjustment = priv->min_pts - cstart;
}
}
if (priv->time_adjustment != GST_CLOCK_TIME_NONE) {
cstart += priv->time_adjustment;
}
/* incoming DTS is not really relevant and does not make sense anyway,
* so pass along _NONE and maybe come up with something better later on */
frame = gst_video_encoder_new_frame (encoder, buf, cstart,
GST_CLOCK_TIME_NONE, duration);
GST_OBJECT_LOCK (encoder);
if (priv->force_key_unit) {
ForcedKeyUnitEvent *fevt = NULL;
GstClockTime running_time;
GList *l;
running_time =
gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
cstart);
for (l = priv->force_key_unit; l; l = l->next) {
ForcedKeyUnitEvent *tmp = l->data;
/* Skip pending keyunits */
if (tmp->pending)
continue;
/* Simple case, keyunit ASAP */
if (tmp->running_time == GST_CLOCK_TIME_NONE) {
fevt = tmp;
break;
}
/* Event for before this frame */
if (tmp->running_time <= running_time) {
fevt = tmp;
break;
}
}
if (fevt) {
fevt->frame_id = frame->system_frame_number;
GST_DEBUG_OBJECT (encoder,
"Forcing a key unit at running time %" GST_TIME_FORMAT,
GST_TIME_ARGS (running_time));
GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME (frame);
if (fevt->all_headers)
GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME_HEADERS (frame);
fevt->pending = TRUE;
}
}
GST_OBJECT_UNLOCK (encoder);
gst_video_codec_frame_ref (frame);
priv->frames = g_list_append (priv->frames, frame);
/* new data, more finish needed */
priv->drained = FALSE;
GST_LOG_OBJECT (encoder, "passing frame pfn %d to subclass",
frame->presentation_frame_number);
ret = klass->handle_frame (encoder, frame);
if (ret == GST_VIDEO_ENCODER_FLOW_DROPPED || ret == GST_FLOW_ERROR) {
GST_INFO_OBJECT (encoder, "Dropping frame %p", frame);
gst_video_encoder_release_frame (encoder, frame);
}
done:
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
return ret;
/* ERRORS */
not_negotiated:
{
GST_ELEMENT_ERROR (encoder, CORE, NEGOTIATION, (NULL),
("encoder not initialized"));
gst_buffer_unref (buf);
return GST_FLOW_NOT_NEGOTIATED;
}
}
static GstStateChangeReturn
gst_video_encoder_change_state (GstElement * element, GstStateChange transition)
{
GstVideoEncoder *encoder;
GstVideoEncoderClass *encoder_class;
GstStateChangeReturn ret;
encoder = GST_VIDEO_ENCODER (element);
encoder_class = GST_VIDEO_ENCODER_GET_CLASS (element);
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
/* open device/library if needed */
if (encoder_class->open && !encoder_class->open (encoder))
goto open_failed;
break;
case GST_STATE_CHANGE_READY_TO_PAUSED:
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
gst_video_encoder_reset (encoder, TRUE);
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
/* Initialize device/library if needed */
if (encoder_class->start && !encoder_class->start (encoder))
goto start_failed;
break;
default:
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
switch (transition) {
case GST_STATE_CHANGE_PAUSED_TO_READY:{
gboolean stopped = TRUE;
if (encoder_class->stop)
stopped = encoder_class->stop (encoder);
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
gst_video_encoder_reset (encoder, TRUE);
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
if (!stopped)
goto stop_failed;
break;
}
case GST_STATE_CHANGE_READY_TO_NULL:
/* close device/library if needed */
if (encoder_class->close && !encoder_class->close (encoder))
goto close_failed;
break;
default:
break;
}
return ret;
/* Errors */
open_failed:
{
GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
("Failed to open encoder"));
return GST_STATE_CHANGE_FAILURE;
}
start_failed:
{
GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
("Failed to start encoder"));
return GST_STATE_CHANGE_FAILURE;
}
stop_failed:
{
GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
("Failed to stop encoder"));
return GST_STATE_CHANGE_FAILURE;
}
close_failed:
{
GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
("Failed to close encoder"));
return GST_STATE_CHANGE_FAILURE;
}
}
static gboolean
gst_video_encoder_negotiate_default (GstVideoEncoder * encoder)
{
GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
GstAllocator *allocator;
GstAllocationParams params;
gboolean ret = TRUE;
GstVideoCodecState *state = encoder->priv->output_state;
GstVideoInfo *info = &state->info;
GstQuery *query = NULL;
GstVideoCodecFrame *frame;
GstCaps *prevcaps;
g_return_val_if_fail (state->caps != NULL, FALSE);
if (encoder->priv->output_state_changed) {
state->caps = gst_caps_make_writable (state->caps);
/* Fill caps */
gst_caps_set_simple (state->caps, "width", G_TYPE_INT, info->width,
"height", G_TYPE_INT, info->height,
"pixel-aspect-ratio", GST_TYPE_FRACTION,
info->par_n, info->par_d, NULL);
if (info->flags & GST_VIDEO_FLAG_VARIABLE_FPS && info->fps_n != 0) {
/* variable fps with a max-framerate */
gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION, 0, 1,
"max-framerate", GST_TYPE_FRACTION, info->fps_n, info->fps_d, NULL);
} else {
/* no variable fps or no max-framerate */
gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION,
info->fps_n, info->fps_d, NULL);
}
if (state->codec_data)
gst_caps_set_simple (state->caps, "codec_data", GST_TYPE_BUFFER,
state->codec_data, NULL);
if (GST_VIDEO_INFO_MULTIVIEW_MODE (info) != GST_VIDEO_MULTIVIEW_MODE_NONE) {
const gchar *caps_mview_mode =
gst_video_multiview_mode_to_caps_string (GST_VIDEO_INFO_MULTIVIEW_MODE
(info));
gst_caps_set_simple (state->caps, "multiview-mode", G_TYPE_STRING,
caps_mview_mode, "multiview-flags", GST_TYPE_VIDEO_MULTIVIEW_FLAGSET,
GST_VIDEO_INFO_MULTIVIEW_FLAGS (info), GST_FLAG_SET_MASK_EXACT, NULL);
}
encoder->priv->output_state_changed = FALSE;
}
/* Push all pending pre-caps events of the oldest frame before
* setting caps */
frame = encoder->priv->frames ? encoder->priv->frames->data : NULL;
if (frame || encoder->priv->current_frame_events) {
GList **events, *l;
if (frame) {
events = &frame->events;
} else {
events = &encoder->priv->current_frame_events;
}
for (l = g_list_last (*events); l;) {
GstEvent *event = GST_EVENT (l->data);
GList *tmp;
if (GST_EVENT_TYPE (event) < GST_EVENT_CAPS) {
gst_video_encoder_push_event (encoder, event);
tmp = l;
l = l->prev;
*events = g_list_delete_link (*events, tmp);
} else {
l = l->prev;
}
}
}
prevcaps = gst_pad_get_current_caps (encoder->srcpad);
if (!prevcaps || !gst_caps_is_equal (prevcaps, state->caps))
ret = gst_pad_set_caps (encoder->srcpad, state->caps);
else
ret = TRUE;
if (prevcaps)
gst_caps_unref (prevcaps);
if (!ret)
goto done;
query = gst_query_new_allocation (state->caps, TRUE);
if (!gst_pad_peer_query (encoder->srcpad, query)) {
GST_DEBUG_OBJECT (encoder, "didn't get downstream ALLOCATION hints");
}
g_assert (klass->decide_allocation != NULL);
ret = klass->decide_allocation (encoder, query);
GST_DEBUG_OBJECT (encoder, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, ret,
query);
if (!ret)
goto no_decide_allocation;
/* we got configuration from our peer or the decide_allocation method,
* parse them */
if (gst_query_get_n_allocation_params (query) > 0) {
gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
} else {
allocator = NULL;
gst_allocation_params_init (¶ms);
}
if (encoder->priv->allocator)
gst_object_unref (encoder->priv->allocator);
encoder->priv->allocator = allocator;
encoder->priv->params = params;
done:
if (query)
gst_query_unref (query);
return ret;
/* Errors */
no_decide_allocation:
{
GST_WARNING_OBJECT (encoder, "Subclass failed to decide allocation");
goto done;
}
}
static gboolean
gst_video_encoder_negotiate_unlocked (GstVideoEncoder * encoder)
{
GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
gboolean ret = TRUE;
if (G_LIKELY (klass->negotiate))
ret = klass->negotiate (encoder);
return ret;
}
/**
* gst_video_encoder_negotiate:
* @encoder: a #GstVideoEncoder
*
* Negotiate with downstream elements to currently configured #GstVideoCodecState.
* Unmark GST_PAD_FLAG_NEED_RECONFIGURE in any case. But mark it again if
* negotiate fails.
*
* Returns: #TRUE if the negotiation succeeded, else #FALSE.
*/
gboolean
gst_video_encoder_negotiate (GstVideoEncoder * encoder)
{
GstVideoEncoderClass *klass;
gboolean ret = TRUE;
g_return_val_if_fail (GST_IS_VIDEO_ENCODER (encoder), FALSE);
g_return_val_if_fail (encoder->priv->output_state, FALSE);
klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
gst_pad_check_reconfigure (encoder->srcpad);
if (klass->negotiate) {
ret = klass->negotiate (encoder);
if (!ret)
gst_pad_mark_reconfigure (encoder->srcpad);
}
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
return ret;
}
/**
* gst_video_encoder_allocate_output_buffer:
* @encoder: a #GstVideoEncoder
* @size: size of the buffer
*
* Helper function that allocates a buffer to hold an encoded video frame
* for @encoder's current #GstVideoCodecState.
*
* Returns: (transfer full): allocated buffer
*/
GstBuffer *
gst_video_encoder_allocate_output_buffer (GstVideoEncoder * encoder, gsize size)
{
GstBuffer *buffer;
gboolean needs_reconfigure = FALSE;
g_return_val_if_fail (size > 0, NULL);
GST_DEBUG ("alloc src buffer");
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
needs_reconfigure = gst_pad_check_reconfigure (encoder->srcpad);
if (G_UNLIKELY (encoder->priv->output_state_changed
|| (encoder->priv->output_state && needs_reconfigure))) {
if (!gst_video_encoder_negotiate_unlocked (encoder)) {
GST_DEBUG_OBJECT (encoder, "Failed to negotiate, fallback allocation");
gst_pad_mark_reconfigure (encoder->srcpad);
goto fallback;
}
}
buffer =
gst_buffer_new_allocate (encoder->priv->allocator, size,
&encoder->priv->params);
if (!buffer) {
GST_INFO_OBJECT (encoder, "couldn't allocate output buffer");
goto fallback;
}
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
return buffer;
fallback:
buffer = gst_buffer_new_allocate (NULL, size, NULL);
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
return buffer;
}
/**
* gst_video_encoder_allocate_output_frame:
* @encoder: a #GstVideoEncoder
* @frame: a #GstVideoCodecFrame
* @size: size of the buffer
*
* Helper function that allocates a buffer to hold an encoded video frame for @encoder's
* current #GstVideoCodecState. Subclass should already have configured video
* state and set src pad caps.
*
* The buffer allocated here is owned by the frame and you should only
* keep references to the frame, not the buffer.
*
* Returns: %GST_FLOW_OK if an output buffer could be allocated
*/
GstFlowReturn
gst_video_encoder_allocate_output_frame (GstVideoEncoder *
encoder, GstVideoCodecFrame * frame, gsize size)
{
gboolean needs_reconfigure = FALSE;
g_return_val_if_fail (frame->output_buffer == NULL, GST_FLOW_ERROR);
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
needs_reconfigure = gst_pad_check_reconfigure (encoder->srcpad);
if (G_UNLIKELY (encoder->priv->output_state_changed
|| (encoder->priv->output_state && needs_reconfigure))) {
if (!gst_video_encoder_negotiate_unlocked (encoder)) {
GST_DEBUG_OBJECT (encoder, "Failed to negotiate, fallback allocation");
gst_pad_mark_reconfigure (encoder->srcpad);
}
}
GST_LOG_OBJECT (encoder, "alloc buffer size %" G_GSIZE_FORMAT, size);
frame->output_buffer =
gst_buffer_new_allocate (encoder->priv->allocator, size,
&encoder->priv->params);
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
return frame->output_buffer ? GST_FLOW_OK : GST_FLOW_ERROR;
}
static void
gst_video_encoder_release_frame (GstVideoEncoder * enc,
GstVideoCodecFrame * frame)
{
GList *link;
/* unref once from the list */
link = g_list_find (enc->priv->frames, frame);
if (link) {
gst_video_codec_frame_unref (frame);
enc->priv->frames = g_list_delete_link (enc->priv->frames, link);
}
/* unref because this function takes ownership */
gst_video_codec_frame_unref (frame);
}
static gboolean
gst_video_encoder_transform_meta_default (GstVideoEncoder *
encoder, GstVideoCodecFrame * frame, GstMeta * meta)
{
const GstMetaInfo *info = meta->info;
const gchar *const *tags;
tags = gst_meta_api_type_get_tags (info->api);
if (!tags || (g_strv_length ((gchar **) tags) == 1
&& gst_meta_api_type_has_tag (info->api,
g_quark_from_string (GST_META_TAG_VIDEO_STR))))
return TRUE;
return FALSE;
}
typedef struct
{
GstVideoEncoder *encoder;
GstVideoCodecFrame *frame;
} CopyMetaData;
static gboolean
foreach_metadata (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
{
CopyMetaData *data = user_data;
GstVideoEncoder *encoder = data->encoder;
GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
GstVideoCodecFrame *frame = data->frame;
const GstMetaInfo *info = (*meta)->info;
gboolean do_copy = FALSE;
if (gst_meta_api_type_has_tag (info->api, _gst_meta_tag_memory)) {
/* never call the transform_meta with memory specific metadata */
GST_DEBUG_OBJECT (encoder, "not copying memory specific metadata %s",
g_type_name (info->api));
do_copy = FALSE;
} else if (klass->transform_meta) {
do_copy = klass->transform_meta (encoder, frame, *meta);
GST_DEBUG_OBJECT (encoder, "transformed metadata %s: copy: %d",
g_type_name (info->api), do_copy);
}
/* we only copy metadata when the subclass implemented a transform_meta
* function and when it returns %TRUE */
if (do_copy) {
GstMetaTransformCopy copy_data = { FALSE, 0, -1 };
GST_DEBUG_OBJECT (encoder, "copy metadata %s", g_type_name (info->api));
/* simply copy then */
info->transform_func (frame->output_buffer, *meta, inbuf,
_gst_meta_transform_copy, ©_data);
}
return TRUE;
}
/**
* gst_video_encoder_finish_frame:
* @encoder: a #GstVideoEncoder
* @frame: (transfer full): an encoded #GstVideoCodecFrame
*
* @frame must have a valid encoded data buffer, whose metadata fields
* are then appropriately set according to frame data or no buffer at
* all if the frame should be dropped.
* It is subsequently pushed downstream or provided to @pre_push.
* In any case, the frame is considered finished and released.
*
* After calling this function the output buffer of the frame is to be
* considered read-only. This function will also change the metadata
* of the buffer.
*
* Returns: a #GstFlowReturn resulting from sending data downstream
*/
GstFlowReturn
gst_video_encoder_finish_frame (GstVideoEncoder * encoder,
GstVideoCodecFrame * frame)
{
GstVideoEncoderPrivate *priv = encoder->priv;
GstFlowReturn ret = GST_FLOW_OK;
GstVideoEncoderClass *encoder_class;
GList *l;
gboolean send_headers = FALSE;
gboolean discont = (frame->presentation_frame_number == 0);
GstBuffer *buffer;
gboolean needs_reconfigure = FALSE;
encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
GST_LOG_OBJECT (encoder,
"finish frame fpn %d", frame->presentation_frame_number);
GST_LOG_OBJECT (encoder, "frame PTS %" GST_TIME_FORMAT
", DTS %" GST_TIME_FORMAT, GST_TIME_ARGS (frame->pts),
GST_TIME_ARGS (frame->dts));
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
needs_reconfigure = gst_pad_check_reconfigure (encoder->srcpad);
if (G_UNLIKELY (priv->output_state_changed || (priv->output_state
&& needs_reconfigure))) {
if (!gst_video_encoder_negotiate_unlocked (encoder)) {
gst_pad_mark_reconfigure (encoder->srcpad);
if (GST_PAD_IS_FLUSHING (encoder->srcpad))
ret = GST_FLOW_FLUSHING;
else
ret = GST_FLOW_NOT_NEGOTIATED;
goto done;
}
}
if (G_UNLIKELY (priv->output_state == NULL))
goto no_output_state;
/* Push all pending events that arrived before this frame */
for (l = priv->frames; l; l = l->next) {
GstVideoCodecFrame *tmp = l->data;
if (tmp->events) {
GList *k;
for (k = g_list_last (tmp->events); k; k = k->prev)
gst_video_encoder_push_event (encoder, k->data);
g_list_free (tmp->events);
tmp->events = NULL;
}
if (tmp == frame)
break;
}
gst_video_encoder_check_and_push_tags (encoder);
/* no buffer data means this frame is skipped/dropped */
if (!frame->output_buffer) {
GST_DEBUG_OBJECT (encoder, "skipping frame %" GST_TIME_FORMAT,
GST_TIME_ARGS (frame->pts));
goto done;
}
if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame) && priv->force_key_unit) {
GstClockTime stream_time, running_time;
GstEvent *ev;
ForcedKeyUnitEvent *fevt = NULL;
GList *l;
running_time =
gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
frame->pts);
GST_OBJECT_LOCK (encoder);
for (l = priv->force_key_unit; l; l = l->next) {
ForcedKeyUnitEvent *tmp = l->data;
/* Skip non-pending keyunits */
if (!tmp->pending)
continue;
/* Exact match using the frame id */
if (frame->system_frame_number == tmp->frame_id) {
fevt = tmp;
break;
}
/* Simple case, keyunit ASAP */
if (tmp->running_time == GST_CLOCK_TIME_NONE) {
fevt = tmp;
break;
}
/* Event for before this frame */
if (tmp->running_time <= running_time) {
fevt = tmp;
break;
}
}
if (fevt) {
priv->force_key_unit = g_list_remove (priv->force_key_unit, fevt);
}
GST_OBJECT_UNLOCK (encoder);
if (fevt) {
stream_time =
gst_segment_to_stream_time (&encoder->output_segment, GST_FORMAT_TIME,
frame->pts);
ev = gst_video_event_new_downstream_force_key_unit
(frame->pts, stream_time, running_time,
fevt->all_headers, fevt->count);
gst_video_encoder_push_event (encoder, ev);
if (fevt->all_headers)
send_headers = TRUE;
GST_DEBUG_OBJECT (encoder,
"Forced key unit: running-time %" GST_TIME_FORMAT
", all_headers %d, count %u",
GST_TIME_ARGS (running_time), fevt->all_headers, fevt->count);
forced_key_unit_event_free (fevt);
}
}
if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
priv->distance_from_sync = 0;
GST_BUFFER_FLAG_UNSET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
/* For keyframes, DTS = PTS, if encoder doesn't decide otherwise */
if (!GST_CLOCK_TIME_IS_VALID (frame->dts)) {
frame->dts = frame->pts;
}
} else {
GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
}
/* DTS is expected monotone ascending,
* so a good guess is the lowest unsent PTS (all being OK) */
{
GstClockTime min_ts = GST_CLOCK_TIME_NONE;
GstVideoCodecFrame *oframe = NULL;
gboolean seen_none = FALSE;
/* some maintenance regardless */
for (l = priv->frames; l; l = l->next) {
GstVideoCodecFrame *tmp = l->data;
if (!GST_CLOCK_TIME_IS_VALID (tmp->abidata.ABI.ts)) {
seen_none = TRUE;
continue;
}
if (!GST_CLOCK_TIME_IS_VALID (min_ts) || tmp->abidata.ABI.ts < min_ts) {
min_ts = tmp->abidata.ABI.ts;
oframe = tmp;
}
}
/* save a ts if needed */
if (oframe && oframe != frame) {
oframe->abidata.ABI.ts = frame->abidata.ABI.ts;
}
/* and set if needed */
if (!GST_CLOCK_TIME_IS_VALID (frame->dts) && !seen_none) {
frame->dts = min_ts;
GST_DEBUG_OBJECT (encoder,
"no valid DTS, using oldest PTS %" GST_TIME_FORMAT,
GST_TIME_ARGS (frame->pts));
}
}
frame->distance_from_sync = priv->distance_from_sync;
priv->distance_from_sync++;
GST_BUFFER_PTS (frame->output_buffer) = frame->pts;
GST_BUFFER_DTS (frame->output_buffer) = frame->dts;
GST_BUFFER_DURATION (frame->output_buffer) = frame->duration;
/* update rate estimate */
priv->bytes += gst_buffer_get_size (frame->output_buffer);
if (GST_CLOCK_TIME_IS_VALID (frame->duration)) {
priv->time += frame->duration;
} else {
/* better none than nothing valid */
priv->time = GST_CLOCK_TIME_NONE;
}
if (G_UNLIKELY (send_headers || priv->new_headers)) {
GList *tmp, *copy = NULL;
GST_DEBUG_OBJECT (encoder, "Sending headers");
/* First make all buffers metadata-writable */
for (tmp = priv->headers; tmp; tmp = tmp->next) {
GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
copy = g_list_append (copy, gst_buffer_make_writable (tmpbuf));
}
g_list_free (priv->headers);
priv->headers = copy;
for (tmp = priv->headers; tmp; tmp = tmp->next) {
GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
priv->bytes += gst_buffer_get_size (tmpbuf);
if (G_UNLIKELY (discont)) {
GST_LOG_OBJECT (encoder, "marking discont");
GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
discont = FALSE;
}
gst_pad_push (encoder->srcpad, gst_buffer_ref (tmpbuf));
}
priv->new_headers = FALSE;
}
if (G_UNLIKELY (discont)) {
GST_LOG_OBJECT (encoder, "marking discont");
GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DISCONT);
}
if (encoder_class->pre_push)
ret = encoder_class->pre_push (encoder, frame);
if (encoder_class->transform_meta) {
if (G_LIKELY (frame->input_buffer)) {
CopyMetaData data;
data.encoder = encoder;
data.frame = frame;
gst_buffer_foreach_meta (frame->input_buffer, foreach_metadata, &data);
} else {
GST_WARNING_OBJECT (encoder,
"Can't copy metadata because input frame disappeared");
}
}
/* Get an additional ref to the buffer, which is going to be pushed
* downstream, the original ref is owned by the frame */
buffer = gst_buffer_ref (frame->output_buffer);
/* Release frame so the buffer is writable when we push it downstream
* if possible, i.e. if the subclass does not hold additional references
* to the frame
*/
gst_video_encoder_release_frame (encoder, frame);
frame = NULL;
if (ret == GST_FLOW_OK)
ret = gst_pad_push (encoder->srcpad, buffer);
done:
/* handed out */
if (frame)
gst_video_encoder_release_frame (encoder, frame);
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
return ret;
/* ERRORS */
no_output_state:
{
gst_video_encoder_release_frame (encoder, frame);
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
GST_ERROR_OBJECT (encoder, "Output state was not configured");
return GST_FLOW_ERROR;
}
}
/**
* gst_video_encoder_get_output_state:
* @encoder: a #GstVideoEncoder
*
* Get the current #GstVideoCodecState
*
* Returns: (transfer full): #GstVideoCodecState describing format of video data.
*/
GstVideoCodecState *
gst_video_encoder_get_output_state (GstVideoEncoder * encoder)
{
GstVideoCodecState *state;
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
state = gst_video_codec_state_ref (encoder->priv->output_state);
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
return state;
}
/**
* gst_video_encoder_set_output_state:
* @encoder: a #GstVideoEncoder
* @caps: (transfer full): the #GstCaps to use for the output
* @reference: (allow-none) (transfer none): An optional reference @GstVideoCodecState
*
* Creates a new #GstVideoCodecState with the specified caps as the output state
* for the encoder.
* Any previously set output state on @encoder will be replaced by the newly
* created one.
*
* The specified @caps should not contain any resolution, pixel-aspect-ratio,
* framerate, codec-data, .... Those should be specified instead in the returned
* #GstVideoCodecState.
*
* If the subclass wishes to copy over existing fields (like pixel aspect ratio,
* or framerate) from an existing #GstVideoCodecState, it can be provided as a
* @reference.
*
* If the subclass wishes to override some fields from the output state (like
* pixel-aspect-ratio or framerate) it can do so on the returned #GstVideoCodecState.
*
* The new output state will only take effect (set on pads and buffers) starting
* from the next call to #gst_video_encoder_finish_frame().
*
* Returns: (transfer full): the newly configured output state.
*/
GstVideoCodecState *
gst_video_encoder_set_output_state (GstVideoEncoder * encoder, GstCaps * caps,
GstVideoCodecState * reference)
{
GstVideoEncoderPrivate *priv = encoder->priv;
GstVideoCodecState *state;
g_return_val_if_fail (caps != NULL, NULL);
state = _new_output_state (caps, reference);
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
if (priv->output_state)
gst_video_codec_state_unref (priv->output_state);
priv->output_state = gst_video_codec_state_ref (state);
priv->output_state_changed = TRUE;
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
return state;
}
/**
* gst_video_encoder_set_latency:
* @encoder: a #GstVideoEncoder
* @min_latency: minimum latency
* @max_latency: maximum latency
*
* Informs baseclass of encoding latency.
*/
void
gst_video_encoder_set_latency (GstVideoEncoder * encoder,
GstClockTime min_latency, GstClockTime max_latency)
{
g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
g_return_if_fail (max_latency >= min_latency);
GST_OBJECT_LOCK (encoder);
encoder->priv->min_latency = min_latency;
encoder->priv->max_latency = max_latency;
GST_OBJECT_UNLOCK (encoder);
gst_element_post_message (GST_ELEMENT_CAST (encoder),
gst_message_new_latency (GST_OBJECT_CAST (encoder)));
}
/**
* gst_video_encoder_get_latency:
* @encoder: a #GstVideoEncoder
* @min_latency: (out) (allow-none): address of variable in which to store the
* configured minimum latency, or %NULL
* @max_latency: (out) (allow-none): address of variable in which to store the
* configured maximum latency, or %NULL
*
* Query the configured encoding latency. Results will be returned via
* @min_latency and @max_latency.
*/
void
gst_video_encoder_get_latency (GstVideoEncoder * encoder,
GstClockTime * min_latency, GstClockTime * max_latency)
{
GST_OBJECT_LOCK (encoder);
if (min_latency)
*min_latency = encoder->priv->min_latency;
if (max_latency)
*max_latency = encoder->priv->max_latency;
GST_OBJECT_UNLOCK (encoder);
}
/**
* gst_video_encoder_get_oldest_frame:
* @encoder: a #GstVideoEncoder
*
* Get the oldest unfinished pending #GstVideoCodecFrame
*
* Returns: (transfer full): oldest unfinished pending #GstVideoCodecFrame
*/
GstVideoCodecFrame *
gst_video_encoder_get_oldest_frame (GstVideoEncoder * encoder)
{
GstVideoCodecFrame *frame = NULL;
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
if (encoder->priv->frames)
frame = gst_video_codec_frame_ref (encoder->priv->frames->data);
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
return (GstVideoCodecFrame *) frame;
}
/**
* gst_video_encoder_get_frame:
* @encoder: a #GstVideoEnccoder
* @frame_number: system_frame_number of a frame
*
* Get a pending unfinished #GstVideoCodecFrame
*
* Returns: (transfer full): pending unfinished #GstVideoCodecFrame identified by @frame_number.
*/
GstVideoCodecFrame *
gst_video_encoder_get_frame (GstVideoEncoder * encoder, int frame_number)
{
GList *g;
GstVideoCodecFrame *frame = NULL;
GST_DEBUG_OBJECT (encoder, "frame_number : %d", frame_number);
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
for (g = encoder->priv->frames; g; g = g->next) {
GstVideoCodecFrame *tmp = g->data;
if (tmp->system_frame_number == frame_number) {
frame = gst_video_codec_frame_ref (tmp);
break;
}
}
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
return frame;
}
/**
* gst_video_encoder_get_frames:
* @encoder: a #GstVideoEncoder
*
* Get all pending unfinished #GstVideoCodecFrame
*
* Returns: (transfer full) (element-type GstVideoCodecFrame): pending unfinished #GstVideoCodecFrame.
*/
GList *
gst_video_encoder_get_frames (GstVideoEncoder * encoder)
{
GList *frames;
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
frames = g_list_copy (encoder->priv->frames);
g_list_foreach (frames, (GFunc) gst_video_codec_frame_ref, NULL);
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
return frames;
}
/**
* gst_video_encoder_merge_tags:
* @encoder: a #GstVideoEncoder
* @tags: (allow-none): a #GstTagList to merge, or NULL to unset
* previously-set tags
* @mode: the #GstTagMergeMode to use, usually #GST_TAG_MERGE_REPLACE
*
* Sets the video encoder tags and how they should be merged with any
* upstream stream tags. This will override any tags previously-set
* with gst_video_encoder_merge_tags().
*
* Note that this is provided for convenience, and the subclass is
* not required to use this and can still do tag handling on its own.
*
* MT safe.
*/
void
gst_video_encoder_merge_tags (GstVideoEncoder * encoder,
const GstTagList * tags, GstTagMergeMode mode)
{
g_return_if_fail (GST_IS_VIDEO_ENCODER (encoder));
g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
g_return_if_fail (tags == NULL || mode != GST_TAG_MERGE_UNDEFINED);
GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
if (encoder->priv->tags != tags) {
if (encoder->priv->tags) {
gst_tag_list_unref (encoder->priv->tags);
encoder->priv->tags = NULL;
encoder->priv->tags_merge_mode = GST_TAG_MERGE_APPEND;
}
if (tags) {
encoder->priv->tags = gst_tag_list_ref ((GstTagList *) tags);
encoder->priv->tags_merge_mode = mode;
}
GST_DEBUG_OBJECT (encoder, "setting encoder tags to %" GST_PTR_FORMAT,
tags);
encoder->priv->tags_changed = TRUE;
}
GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
}
/**
* gst_video_encoder_get_allocator:
* @encoder: a #GstVideoEncoder
* @allocator: (out) (allow-none) (transfer full): the #GstAllocator
* used
* @params: (out) (allow-none) (transfer full): the
* #GstAllocatorParams of @allocator
*
* Lets #GstVideoEncoder sub-classes to know the memory @allocator
* used by the base class and its @params.
*
* Unref the @allocator after use it.
*/
void
gst_video_encoder_get_allocator (GstVideoEncoder * encoder,
GstAllocator ** allocator, GstAllocationParams * params)
{
g_return_if_fail (GST_IS_VIDEO_ENCODER (encoder));
if (allocator)
*allocator = encoder->priv->allocator ?
gst_object_ref (encoder->priv->allocator) : NULL;
if (params)
*params = encoder->priv->params;
}
/**
* gst_video_encoder_set_min_pts:
* @encoder: a #GstVideoEncoder
* @min_pts: minimal PTS that will be passed to handle_frame
*
* Request minimal value for PTS passed to handle_frame.
*
* For streams with reordered frames this can be used to ensure that there
* is enough time to accomodate first DTS, which may be less than first PTS
*
* Since 1.6
*/
void
gst_video_encoder_set_min_pts (GstVideoEncoder * encoder, GstClockTime min_pts)
{
g_return_if_fail (GST_IS_VIDEO_ENCODER (encoder));
encoder->priv->min_pts = min_pts;
encoder->priv->time_adjustment = GST_CLOCK_TIME_NONE;
}
|