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
|
/* decavcodec.c
Copyright (c) 2003-2018 HandBrake Team
This file is part of the HandBrake source code
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License v2.
For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
*/
/* This module is Handbrake's interface to the ffmpeg decoder library
(libavcodec & small parts of libavformat). It contains four Handbrake
"work objects":
decavcodeca connects HB to an ffmpeg audio decoder
decavcodecv connects HB to an ffmpeg video decoder
(Two different routines are needed because the ffmpeg library
has different decoder calling conventions for audio & video.
These work objects are self-contained & follow all
of HB's conventions for a decoder module. They can be used like
any other HB decoder
These decoders handle 2 kinds of input. Streams that are demuxed
by HandBrake and streams that are demuxed by libavformat. In the
case of streams that are demuxed by HandBrake, there is an extra
parse step required that happens in decodeVideo and decavcodecaWork.
In the case of streams that are demuxed by libavformat, there is context
information that we need from the libavformat. This information is
propagated from hb_stream_open to these decoders through title->opaque_priv.
A consequence of the above is that the streams that are demuxed by HandBrake
*can't* use information from the AVStream because there isn't one - they
get their data from either the dvd reader or the mpeg reader, not the ffmpeg
stream reader. That means that they have to make up for deficiencies in the
AVCodecContext info by using stuff kept in the HB "title" struct. It
also means that ffmpeg codecs that randomly scatter state needed by
the decoder across both the AVCodecContext & the AVStream (e.g., the
VC1 decoder) can't easily be used by the HB mpeg stream reader.
*/
#include "hb.h"
#include "hbffmpeg.h"
#include "libavfilter/avfilter.h"
#include "libavfilter/buffersrc.h"
#include "libavfilter/buffersink.h"
#include "lang.h"
#include "audio_resample.h"
#ifdef USE_QSV
#include "qsv_common.h"
#endif
static void compute_frame_duration( hb_work_private_t *pv );
static int decavcodecaInit( hb_work_object_t *, hb_job_t * );
static int decavcodecaWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
static void decavcodecClose( hb_work_object_t * );
static int decavcodecaInfo( hb_work_object_t *, hb_work_info_t * );
static int decavcodecaBSInfo( hb_work_object_t *, const hb_buffer_t *, hb_work_info_t * );
static int get_color_prim(int color_primaries, hb_geometry_t geometry, hb_rational_t rate);
static int get_color_transfer(int color_trc);
static int get_color_matrix(int colorspace, hb_geometry_t geometry);
hb_work_object_t hb_decavcodeca =
{
.id = WORK_DECAVCODEC,
.name = "Audio decoder (libavcodec)",
.init = decavcodecaInit,
.work = decavcodecaWork,
.close = decavcodecClose,
.info = decavcodecaInfo,
.bsinfo = decavcodecaBSInfo
};
typedef struct
{
uint8_t * data;
int size;
int64_t pts;
int64_t dts;
int frametype;
int scr_sequence;
int new_chap;
} packet_info_t;
typedef struct reordered_data_s reordered_data_t;
struct reordered_data_s
{
int64_t sequence;
int64_t pts;
int scr_sequence;
int new_chap;
};
#define REORDERED_HASH_SZ (2 << 7)
#define REORDERED_HASH_MASK (REORDERED_HASH_SZ - 1)
struct video_filters_s
{
AVFilterGraph * graph;
AVFilterContext * last;
AVFilterContext * input;
AVFilterContext * output;
int width;
int height;
int pix_fmt;
};
struct hb_work_private_s
{
hb_job_t * job;
hb_title_t * title;
AVCodecContext * context;
AVCodecParserContext * parser;
AVFrame * frame;
hb_buffer_t * palette;
int threads;
int video_codec_opened;
hb_buffer_list_t list;
double duration; // frame duration (for video)
double field_duration; // field duration (for video)
int64_t chap_time; // time of next chap mark
int chap_scr;
int new_chap; // output chapter mark pending
int64_t last_pts;
double next_pts;
uint32_t nframes;
uint32_t decode_errors;
packet_info_t packet_info;
uint8_t unfinished;
reordered_data_t * reordered_hash[REORDERED_HASH_SZ];
int64_t sequence;
int last_scr_sequence;
int last_chapter;
struct video_filters_s video_filters;
hb_audio_t * audio;
hb_audio_resample_t * resample;
int drop_samples;
#ifdef USE_QSV
// QSV-specific settings
struct
{
int decode;
hb_qsv_config config;
const char * codec_name;
} qsv;
#endif
hb_list_t * list_subtitle;
};
static void decodeAudio( hb_work_private_t *pv, packet_info_t * packet_info );
/***********************************************************************
* hb_work_decavcodec_init
***********************************************************************
*
**********************************************************************/
static int decavcodecaInit( hb_work_object_t * w, hb_job_t * job )
{
AVCodec * codec;
hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
w->private_data = pv;
pv->job = job;
pv->audio = w->audio;
pv->drop_samples = w->audio->config.in.encoder_delay;
pv->next_pts = (int64_t)AV_NOPTS_VALUE;
if (job)
pv->title = job->title;
else
pv->title = w->title;
hb_buffer_list_clear(&pv->list);
codec = avcodec_find_decoder(w->codec_param);
pv->context = avcodec_alloc_context3(codec);
if (pv->title->opaque_priv != NULL)
{
AVFormatContext *ic = (AVFormatContext*)pv->title->opaque_priv;
avcodec_parameters_to_context(pv->context,
ic->streams[w->audio->id]->codecpar);
// libav's eac3 parser toggles the codec_id in the context as
// it reads eac3 data between AV_CODEC_ID_AC3 and AV_CODEC_ID_EAC3.
// It detects an AC3 sync pattern sometimes in ac3_sync() which
// causes it to eventually set avctx->codec_id to AV_CODEC_ID_AC3
// in ff_aac_ac3_parse(). Since we are parsing some data before
// we get here, the codec_id may have flipped. This will cause an
// error in hb_avcodec_open(). So flip it back!
pv->context->codec_id = w->codec_param;
}
else
{
pv->parser = av_parser_init(w->codec_param);
}
hb_ff_set_sample_fmt(pv->context, codec, AV_SAMPLE_FMT_FLT);
/* Downmixing & sample_fmt conversion */
if (!(w->audio->config.out.codec & HB_ACODEC_PASS_FLAG))
{
pv->resample =
hb_audio_resample_init(AV_SAMPLE_FMT_FLT,
w->audio->config.out.mixdown,
w->audio->config.out.normalize_mix_level);
if (pv->resample == NULL)
{
hb_error("decavcodecaInit: hb_audio_resample_init() failed");
return 1;
}
/*
* Some audio decoders can downmix using embedded coefficients,
* or dedicated audio substreams for a specific channel layout.
*
* But some will e.g. use normalized mix coefficients unconditionally,
* so we need to make sure this matches what the user actually requested.
*/
int avcodec_downmix = 0;
switch (w->codec_param)
{
case AV_CODEC_ID_AC3:
case AV_CODEC_ID_EAC3:
avcodec_downmix = w->audio->config.out.normalize_mix_level != 0;
break;
case AV_CODEC_ID_DTS:
avcodec_downmix = w->audio->config.out.normalize_mix_level == 0;
break;
case AV_CODEC_ID_TRUEHD:
avcodec_downmix = (w->audio->config.out.normalize_mix_level == 0 ||
w->audio->config.out.mixdown == HB_AMIXDOWN_MONO ||
w->audio->config.out.mixdown == HB_AMIXDOWN_DOLBY ||
w->audio->config.out.mixdown == HB_AMIXDOWN_DOLBYPLII);
break;
default:
break;
}
if (avcodec_downmix)
{
switch (w->audio->config.out.mixdown)
{
// request 5.1 before downmixing to dpl1/dpl2
case HB_AMIXDOWN_DOLBY:
case HB_AMIXDOWN_DOLBYPLII:
pv->context->request_channel_layout = AV_CH_LAYOUT_5POINT1;
break;
// request the layout corresponding to the selected mixdown
default:
pv->context->request_channel_layout =
hb_ff_mixdown_xlat(w->audio->config.out.mixdown, NULL);
break;
}
}
}
// libavcodec can't decode TrueHD Mono (bug #356)
// work around it by requesting Stereo and downmixing
if (w->codec_param == AV_CODEC_ID_TRUEHD &&
w->audio->config.in.channel_layout == AV_CH_LAYOUT_MONO)
{
pv->context->request_channel_layout = AV_CH_LAYOUT_STEREO;
}
// Set decoder opts...
AVDictionary * av_opts = NULL;
av_dict_set( &av_opts, "refcounted_frames", "1", 0 );
// Dynamic Range Compression
if (w->audio->config.out.dynamic_range_compression >= 0.0f &&
hb_audio_can_apply_drc(w->audio->config.in.codec,
w->audio->config.in.codec_param, 0))
{
float drc_scale_max = 1.0f;
/*
* avcodec_open will fail if the value for any of the options is out of
* range, so assume a conservative maximum of 1 and try to determine the
* option's actual upper limit.
*/
if (codec != NULL && codec->priv_class != NULL)
{
const AVOption *opt;
opt = av_opt_find2((void*)&codec->priv_class, "drc_scale", NULL,
AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_AUDIO_PARAM,
AV_OPT_SEARCH_FAKE_OBJ, NULL);
if (opt != NULL)
{
drc_scale_max = opt->max;
}
}
if (w->audio->config.out.dynamic_range_compression > drc_scale_max)
{
hb_log("decavcodecaInit: track %d, sanitizing out-of-range DRC %.2f to %.2f",
w->audio->config.out.track,
w->audio->config.out.dynamic_range_compression, drc_scale_max);
w->audio->config.out.dynamic_range_compression = drc_scale_max;
}
char drc_scale[5]; // "?.??\n"
snprintf(drc_scale, sizeof(drc_scale), "%.2f",
w->audio->config.out.dynamic_range_compression);
av_dict_set(&av_opts, "drc_scale", drc_scale, 0);
}
if (hb_avcodec_open(pv->context, codec, &av_opts, 0))
{
av_dict_free( &av_opts );
hb_log("decavcodecaInit: avcodec_open failed");
return 1;
}
// avcodec_open populates av_opts with the things it didn't recognize.
AVDictionaryEntry *t = NULL;
while ((t = av_dict_get(av_opts, "", t, AV_DICT_IGNORE_SUFFIX)) != NULL)
{
hb_log("decavcodecaInit: unknown option '%s'", t->key);
}
av_dict_free( &av_opts );
pv->frame = av_frame_alloc();
if (pv->frame == NULL)
{
hb_log("decavcodecaInit: av_frame_alloc failed");
return 1;
}
return 0;
}
/***********************************************************************
* Close
***********************************************************************
*
**********************************************************************/
static void close_video_filters(hb_work_private_t *pv)
{
if (pv->video_filters.input != NULL)
{
avfilter_free(pv->video_filters.input);
pv->video_filters.input = NULL;
}
if (pv->video_filters.output != NULL)
{
avfilter_free(pv->video_filters.output);
pv->video_filters.output = NULL;
}
if (pv->video_filters.graph != NULL)
{
avfilter_graph_free(&pv->video_filters.graph);
}
pv->video_filters.last = NULL;
}
static void closePrivData( hb_work_private_t ** ppv )
{
hb_work_private_t * pv = *ppv;
if ( pv )
{
hb_buffer_list_close(&pv->list);
if ( pv->job && pv->context && pv->context->codec )
{
hb_log( "%s-decoder done: %u frames, %u decoder errors",
pv->context->codec->name, pv->nframes, pv->decode_errors);
}
av_frame_free(&pv->frame);
close_video_filters(pv);
if ( pv->parser )
{
av_parser_close(pv->parser);
}
if ( pv->context && pv->context->codec )
{
#ifdef USE_QSV
/*
* FIXME: knowingly leaked.
*
* If we're using our FFmpeg QSV wrapper, qsv_decode_end() will call
* MFXClose() on the QSV session. Even if decoding is complete, we
* still need that session for QSV filtering and/or encoding, so we
* we can't close the context here until we implement a proper fix.
*
* Interestingly, this may cause crashes even when QSV-accelerated
* decoding and encoding sessions are independent (e.g. decoding via
* libavcodec, but encoding using libhb, without us requesting any
* form of communication between the two libmfx sessions).
*/
//if (!(pv->qsv.decode && pv->job != NULL && (pv->job->vcodec & HB_VCODEC_QSV_MASK)))
#endif
{
hb_avcodec_free_context(&pv->context);
}
}
if ( pv->context )
{
hb_avcodec_free_context(&pv->context);
}
hb_audio_resample_free(pv->resample);
int ii;
for (ii = 0; ii < REORDERED_HASH_SZ; ii++)
{
free(pv->reordered_hash[ii]);
}
free(pv);
}
*ppv = NULL;
}
static void decavcodecClose( hb_work_object_t * w )
{
hb_work_private_t * pv = w->private_data;
if ( pv )
{
closePrivData( &pv );
w->private_data = NULL;
}
}
/***********************************************************************
* Work
***********************************************************************
*
**********************************************************************/
static int decavcodecaWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
hb_buffer_t ** buf_out )
{
hb_work_private_t * pv = w->private_data;
hb_buffer_t * in = *buf_in;
// libavcodec/mpeg12dec.c requires buffers to be zero padded.
// If not zero padded, it can get stuck in an infinite loop.
// It's likely there are other decoders that expect the same.
if (in->data != NULL)
{
memset(in->data + in->size, 0, in->alloc - in->size);
}
if (in->s.flags & HB_BUF_FLAG_EOF)
{
/* EOF on input stream - send it downstream & say that we're done */
decodeAudio(pv, NULL);
hb_buffer_list_append(&pv->list, in);
*buf_in = NULL;
*buf_out = hb_buffer_list_clear(&pv->list);
return HB_WORK_DONE;
}
*buf_out = NULL;
int pos, len;
int64_t pts = in->s.start;
// There are a 3 scenarios that can happen here.
// 1. The buffer contains exactly one frame of data
// 2. The buffer contains multiple frames of data
// 3. The buffer contains a partial frame of data
//
// In scenario 2, we want to be sure that the timestamps are only
// applied to the first frame in the buffer. Additional frames
// in the buffer will have their timestamps computed in sync.
//
// In scenario 3, we need to save the ancillary buffer info of an
// unfinished frame so it can be applied when we receive the last
// buffer of that frame.
if (!pv->unfinished)
{
// New packet, and no previous data pending
pv->packet_info.scr_sequence = in->s.scr_sequence;
pv->packet_info.new_chap = in->s.new_chap;
pv->packet_info.frametype = in->s.frametype;
}
for (pos = 0; pos < in->size; pos += len)
{
uint8_t * pout;
int pout_len;
int64_t parser_pts;
if ( pv->parser != NULL )
{
len = av_parser_parse2(pv->parser, pv->context, &pout, &pout_len,
in->data + pos, in->size - pos,
pts, pts, 0 );
parser_pts = pv->parser->pts;
pts = AV_NOPTS_VALUE;
}
else
{
pout = in->data;
len = pout_len = in->size;
parser_pts = in->s.start;
}
if (pout != NULL && pout_len > 0)
{
pv->packet_info.data = pout;
pv->packet_info.size = pout_len;
pv->packet_info.pts = parser_pts;
decodeAudio(pv, &pv->packet_info);
// There could have been an unfinished packet when we entered
// decodeAudio that is now finished. The next packet is associated
// with the input buffer, so set it's chapter and scr info.
pv->packet_info.scr_sequence = in->s.scr_sequence;
pv->unfinished = 0;
}
if (len > 0 && pout_len <= 0)
{
pv->unfinished = 1;
}
}
*buf_out = hb_buffer_list_clear(&pv->list);
return HB_WORK_OK;
}
static int decavcodecaInfo( hb_work_object_t *w, hb_work_info_t *info )
{
hb_work_private_t *pv = w->private_data;
memset( info, 0, sizeof(*info) );
if ( pv && pv->context )
{
AVCodecContext *context = pv->context;
info->bitrate = context->bit_rate;
info->rate.num = context->time_base.num;
info->rate.den = context->time_base.den;
info->profile = context->profile;
info->level = context->level;
return 1;
}
return 0;
}
static int parse_adts_extradata( hb_audio_t * audio, AVCodecContext * context,
AVPacket * pkt )
{
const AVBitStreamFilter * bsf;
AVBSFContext * ctx = NULL;
int ret;
bsf = av_bsf_get_by_name("aac_adtstoasc");
ret = av_bsf_alloc(bsf, &ctx);
if (ret < 0)
{
hb_error("decavcodec: bitstream filter alloc failure");
return ret;
}
ctx->time_base_in.num = 1;
ctx->time_base_in.den = audio->config.out.samplerate;
avcodec_parameters_from_context(ctx->par_in, context);
ret = av_bsf_init(ctx);
if (ret < 0)
{
hb_error("decavcodec: bitstream filter init failure");
av_bsf_free(&ctx);
return ret;
}
ret = av_bsf_send_packet(ctx, pkt);
if (ret < 0)
{
hb_error("decavcodec: av_bsf_send_packet failure");
av_bsf_free(&ctx);
return ret;
}
ret = av_bsf_receive_packet(ctx, pkt);
av_bsf_free(&ctx);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
{
return 0;
}
else if (ret < 0)
{
if (ret != AVERROR_INVALIDDATA)
{
hb_error("decavcodec: av_bsf_receive_packet failure %x", -ret);
}
return ret;
}
if (audio->priv.config.extradata.length == 0)
{
const uint8_t * extradata;
int size;
extradata = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
&size);
if (extradata != NULL && size > 0)
{
int len;
len = MIN(size, HB_CONFIG_MAX_SIZE);
memcpy(audio->priv.config.extradata.bytes, extradata, len);
audio->priv.config.extradata.length = len;
}
}
return 0;
}
static int decavcodecaBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
hb_work_info_t *info )
{
hb_work_private_t *pv = w->private_data;
int result = 0, done = 0;
hb_audio_t *audio = w->audio;
memset( info, 0, sizeof(*info) );
if ( pv && pv->context )
{
return decavcodecaInfo( w, info );
}
AVCodec *codec = avcodec_find_decoder( w->codec_param );
if ( ! codec )
{
// there's no ffmpeg codec for this audio type - give up
return -1;
}
static char codec_name[64];
info->name = strncpy( codec_name, codec->name, sizeof(codec_name)-1 );
AVCodecContext *context = avcodec_alloc_context3(codec);
AVCodecParserContext *parser = NULL;
if (w->title && w->title->opaque_priv != NULL)
{
AVFormatContext *ic = (AVFormatContext*)w->title->opaque_priv;
avcodec_parameters_to_context(context,
ic->streams[audio->id]->codecpar);
// libav's eac3 parser toggles the codec_id in the context as
// it reads eac3 data between AV_CODEC_ID_AC3 and AV_CODEC_ID_EAC3.
// It detects an AC3 sync pattern sometimes in ac3_sync() which
// causes it to eventually set avctx->codec_id to AV_CODEC_ID_AC3
// in ff_aac_ac3_parse(). Since we are parsing some data before
// we get here, the codec_id may have flipped. This will cause an
// error in hb_avcodec_open(). So flip it back!
context->codec_id = w->codec_param;
}
else
{
parser = av_parser_init(codec->id);
}
hb_ff_set_sample_fmt( context, codec, AV_SAMPLE_FMT_FLT );
AVDictionary * av_opts = NULL;
av_dict_set( &av_opts, "err_detect", "crccheck+explode", 0 );
if ( hb_avcodec_open( context, codec, &av_opts, 0 ) )
{
av_dict_free( &av_opts );
return -1;
}
av_dict_free( &av_opts );
unsigned char *parse_buffer;
int parse_pos, parse_buffer_size;
while (buf != NULL && !done)
{
parse_pos = 0;
while (parse_pos < buf->size && !done)
{
int parse_len, truehd_mono = 0, ret;
if (parser != NULL)
{
parse_len = av_parser_parse2(parser, context,
&parse_buffer, &parse_buffer_size,
buf->data + parse_pos, buf->size - parse_pos,
buf->s.start, buf->s.start, 0);
}
else
{
parse_buffer = buf->data + parse_pos;
parse_len = parse_buffer_size = buf->size - parse_pos;
}
if (parse_buffer_size == 0)
{
parse_pos += parse_len;
continue;
}
// libavcodec can't decode TrueHD Mono (bug #356)
// work around it by requesting Stereo before decoding
if (context->codec_id == AV_CODEC_ID_TRUEHD &&
context->channel_layout == AV_CH_LAYOUT_MONO)
{
truehd_mono = 1;
context->request_channel_layout = AV_CH_LAYOUT_STEREO;
}
else
{
context->request_channel_layout = 0;
}
AVPacket avp;
av_init_packet(&avp);
avp.data = parse_buffer;
avp.size = parse_buffer_size;
ret = avcodec_send_packet(context, &avp);
if (ret < 0 && ret != AVERROR_EOF)
{
parse_pos += parse_len;
av_packet_unref(&avp);
continue;
}
AVFrame *frame = NULL;
do
{
if (frame == NULL)
{
frame = av_frame_alloc();
}
ret = avcodec_receive_frame(context, frame);
if (ret >= 0)
{
// libavcoded doesn't consistently set frame->sample_rate
if (frame->sample_rate != 0)
{
info->rate.num = frame->sample_rate;
}
else
{
info->rate.num = context->sample_rate;
hb_log("decavcodecaBSInfo: warning: invalid frame sample_rate! Using context sample_rate.");
}
info->rate.den = 1;
info->samples_per_frame = frame->nb_samples;
info->sample_bit_depth = context->bits_per_raw_sample;
int bps = av_get_bits_per_sample(context->codec_id);
int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
if (bps > 0)
{
info->bitrate = bps * channels * info->rate.num;
}
else if (context->bit_rate > 0)
{
info->bitrate = context->bit_rate;
}
else
{
info->bitrate = 1;
}
if (truehd_mono)
{
info->channel_layout = AV_CH_LAYOUT_MONO;
info->matrix_encoding = AV_MATRIX_ENCODING_NONE;
}
else
{
AVFrameSideData *side_data;
if ((side_data =
av_frame_get_side_data(frame,
AV_FRAME_DATA_MATRIXENCODING)) != NULL)
{
info->matrix_encoding = *side_data->data;
}
else
{
info->matrix_encoding = AV_MATRIX_ENCODING_NONE;
}
if (info->matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
info->matrix_encoding == AV_MATRIX_ENCODING_DPLII)
{
info->channel_layout = AV_CH_LAYOUT_STEREO_DOWNMIX;
}
else
{
info->channel_layout = frame->channel_layout;
}
}
if (context->codec_id == AV_CODEC_ID_AC3 ||
context->codec_id == AV_CODEC_ID_EAC3)
{
if (context->audio_service_type == AV_AUDIO_SERVICE_TYPE_KARAOKE)
{
info->mode = 7;
}
else
{
info->mode = context->audio_service_type;
}
}
else if (context->codec_id == AV_CODEC_ID_AAC &&
context->extradata_size == 0)
{
// Parse ADTS AAC streams for AudioSpecificConfig.
// This data is required in order to write
// proper headers in MP4 and MKV files.
parse_adts_extradata(audio, context, &avp);
}
result = 1;
done = 1;
av_frame_unref(frame);
break;
}
} while (ret >= 0);
av_packet_unref(&avp);
av_frame_free(&frame);
parse_pos += parse_len;
}
buf = buf->next;
}
info->profile = context->profile;
info->level = context->level;
info->channel_map = &hb_libav_chan_map;
if ( parser != NULL )
av_parser_close( parser );
hb_avcodec_free_context(&context);
return result;
}
reordered_data_t *
reordered_hash_rem(hb_work_private_t * pv, int64_t sequence)
{
reordered_data_t * reordered;
int slot = sequence & REORDERED_HASH_MASK;
reordered = pv->reordered_hash[slot];
if (reordered == NULL)
{
// This shouldn't happen...
// But, this happens sometimes when libav outputs exactly the same
// frame twice for some reason.
hb_deep_log(3, "decavcodec: missing sequence %"PRId64"", sequence);
}
pv->reordered_hash[slot] = NULL;
return reordered;
}
void
reordered_hash_add(hb_work_private_t * pv, reordered_data_t * reordered)
{
int slot = reordered->sequence & REORDERED_HASH_MASK;
// Free any unused previous entries.
// This can happen due to libav parser feeding partial
// frames data to the decoder.
// It can also happen due to decoding errors.
free(pv->reordered_hash[slot]);
pv->reordered_hash[slot] = reordered;
}
/* -------------------------------------------------------------
* General purpose video decoder using libavcodec
*/
// send cc_buf to the CC decoder(s)
static void cc_send_to_decoder(hb_work_private_t *pv, hb_buffer_t *buf)
{
if (buf == NULL)
return;
// if there's more than one decoder for the captions send a copy
// of the buffer to all.
hb_subtitle_t *subtitle;
int ii = 0, n = hb_list_count(pv->list_subtitle);
while (--n > 0)
{
// make a copy of the buf then forward it to the decoder
hb_buffer_t *cpy = hb_buffer_dup(buf);
subtitle = hb_list_item(pv->list_subtitle, ii++);
hb_fifo_push(subtitle->fifo_in, cpy);
}
subtitle = hb_list_item(pv->list_subtitle, ii);
hb_fifo_push( subtitle->fifo_in, buf );
}
static hb_buffer_t * cc_fill_buffer(hb_work_private_t *pv, uint8_t *cc, int size)
{
int cc_count[4] = {0,};
int ii;
hb_buffer_t *buf = NULL;
for (ii = 0; ii < size; ii += 3)
{
if ((cc[ii] & 0x04) == 0) // not valid
continue;
if ((cc[ii+1] & 0x7f) == 0 && (cc[ii+2] & 0x7f) == 0) // stuffing
continue;
int type = cc[ii] & 0x03;
cc_count[type]++;
}
// Only handles CC1 for now.
if (cc_count[0] > 0)
{
buf = hb_buffer_init(cc_count[0] * 2);
int jj = 0;
for (ii = 0; ii < size; ii += 3)
{
if ((cc[ii] & 0x04) == 0) // not valid
continue;
if ((cc[ii+1] & 0x7f) == 0 && (cc[ii+2] & 0x7f) == 0) // stuffing
continue;
int type = cc[ii] & 0x03;
if (type == 0)
{
buf->data[jj++] = cc[ii+1];
buf->data[jj++] = cc[ii+2];
}
}
}
return buf;
}
// copy one video frame into an HB buf. If the frame isn't in our color space
// or at least one of its dimensions is odd, use sws_scale to convert/rescale it.
// Otherwise just copy the bits.
static hb_buffer_t *copy_frame( hb_work_private_t *pv )
{
reordered_data_t * reordered = NULL;
hb_buffer_t * out;
#ifdef USE_QSV
// no need to copy the frame data when decoding with QSV to opaque memory
if (pv->qsv.decode &&
pv->qsv.config.io_pattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY)
{
out = hb_frame_buffer_init(pv->frame->format, pv->frame->width, pv->frame->height);
hb_avframe_set_video_buffer_flags(out, pv->frame, (AVRational){1,1});
out->qsv_details.qsv_atom = pv->frame->data[2];
out->qsv_details.ctx = pv->job->qsv.ctx;
}
else
#endif
{
out = hb_avframe_to_video_buffer(pv->frame, (AVRational){1,1});
}
if (pv->frame->pts != AV_NOPTS_VALUE)
{
reordered = reordered_hash_rem(pv, pv->frame->pts);
}
if (reordered != NULL)
{
out->s.scr_sequence = reordered->scr_sequence;
out->s.start = reordered->pts;
out->s.new_chap = reordered->new_chap;
pv->last_scr_sequence = reordered->scr_sequence;
pv->last_chapter = reordered->new_chap;
free(reordered);
}
else
{
out->s.scr_sequence = pv->last_scr_sequence;
out->s.start = AV_NOPTS_VALUE;
}
double frame_dur = pv->duration;
if (pv->frame->repeat_pict)
{
frame_dur += pv->frame->repeat_pict * pv->field_duration;
}
if (out->s.start == AV_NOPTS_VALUE)
{
out->s.start = pv->next_pts;
}
else
{
pv->next_pts = out->s.start;
}
if (pv->next_pts != (int64_t)AV_NOPTS_VALUE)
{
pv->next_pts += frame_dur;
out->s.stop = pv->next_pts;
}
out->s.duration = frame_dur;
if (out->s.new_chap > 0 && out->s.new_chap == pv->new_chap)
{
pv->new_chap = 0;
}
// It is possible that the buffer with new_chap gets dropped
// by the decoder. So also check if the output buffer is after
// the new_chap in the timeline.
if (pv->new_chap > 0 &&
(out->s.scr_sequence > pv->chap_scr ||
(out->s.scr_sequence == pv->chap_scr && out->s.start > pv->chap_time)))
{
out->s.new_chap = pv->new_chap;
pv->new_chap = 0;
}
// Check for CC data
AVFrameSideData *sd;
sd = av_frame_get_side_data(pv->frame, AV_FRAME_DATA_A53_CC);
if (sd != NULL)
{
if (!pv->job && pv->title && sd->size > 0)
{
hb_subtitle_t *subtitle;
int i = 0;
while ((subtitle = hb_list_item(pv->title->list_subtitle, i++)))
{
/*
* Let's call them 608 subs for now even if they aren't,
* since they are the only types we grok.
*/
if (subtitle->source == CC608SUB)
{
break;
}
}
if (subtitle == NULL)
{
iso639_lang_t * lang;
hb_audio_t * audio;
subtitle = calloc(sizeof( hb_subtitle_t ), 1);
subtitle->track = hb_list_count(pv->title->list_subtitle);
subtitle->id = 0;
subtitle->format = TEXTSUB;
subtitle->source = CC608SUB;
subtitle->config.dest = PASSTHRUSUB;
subtitle->codec = WORK_DECCC608;
subtitle->attributes = HB_SUBTITLE_ATTR_CC;
/*
* The language of the subtitles will be the same as the
* first audio track, i.e. the same as the video.
*/
audio = hb_list_item(pv->title->list_audio, 0);
if (audio != NULL)
{
lang = lang_for_code2( audio->config.lang.iso639_2 );
} else {
lang = lang_for_code2( "und" );
}
snprintf(subtitle->lang, sizeof(subtitle->lang),
"%s, Closed Caption [%s]",
strlen(lang->native_name) ? lang->native_name :
lang->eng_name,
hb_subsource_name(subtitle->source));
snprintf(subtitle->iso639_2, sizeof(subtitle->iso639_2),
"%s", lang->iso639_2);
hb_list_add(pv->title->list_subtitle, subtitle);
}
}
if (pv->list_subtitle != NULL && sd->size > 0)
{
hb_buffer_t *cc_buf;
cc_buf = cc_fill_buffer(pv, sd->data, sd->size);
if (cc_buf != NULL)
{
cc_buf->s.start = out->s.start;
cc_buf->s.scr_sequence = out->s.scr_sequence;
}
cc_send_to_decoder(pv, cc_buf);
}
}
return out;
}
static AVFilterContext * append_filter(hb_work_private_t * pv,
const char * name, const char * args)
{
AVFilterContext * filter;
int result;
result = avfilter_graph_create_filter(&filter, avfilter_get_by_name(name),
name, args, NULL,
pv->video_filters.graph);
if (result < 0)
{
return NULL;
}
if (pv->video_filters.last != NULL)
{
result = avfilter_link(pv->video_filters.last, 0, filter, 0);
if (result < 0)
{
avfilter_free(filter);
return NULL;
}
}
pv->video_filters.last = filter;
return filter;
}
int reinit_video_filters(hb_work_private_t * pv)
{
char * sws_flags;
int result;
AVFilterContext * avfilter;
char * graph_str = NULL, * filter_str;
AVFilterInOut * in = NULL, * out = NULL;
int orig_width;
int orig_height;
#ifdef USE_QSV
if (pv->qsv.decode &&
pv->qsv.config.io_pattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY)
{
// Can't use software filters when decoding with QSV opaque memory
return 0;
}
#endif
if (!pv->job)
{
// HandBrake's video pipeline uses yuv420 color. This means all
// dimensions must be even. So we must adjust the dimensions
// of incoming video if not even.
orig_width = pv->context->width & ~1;
orig_height = pv->context->height & ~1;
}
else
{
if (pv->title->rotation == HB_ROTATION_90 ||
pv->title->rotation == HB_ROTATION_270)
{
orig_width = pv->job->title->geometry.height;
orig_height = pv->job->title->geometry.width;
}
else
{
orig_width = pv->job->title->geometry.width;
orig_height = pv->job->title->geometry.height;
}
}
if (AV_PIX_FMT_YUV420P == pv->frame->format &&
orig_width == pv->frame->width &&
orig_height == pv->frame->height &&
HB_ROTATION_0 == pv->title->rotation)
{
// No filtering required.
close_video_filters(pv);
return 0;
}
if (pv->video_filters.graph != NULL &&
pv->video_filters.width == pv->frame->width &&
pv->video_filters.height == pv->frame->height &&
pv->video_filters.pix_fmt == pv->frame->format)
{
// Current filter settings are good
return 0;
}
pv->video_filters.width = pv->frame->width;
pv->video_filters.height = pv->frame->height;
pv->video_filters.pix_fmt = pv->frame->format;
// New filter required, create filter graph
close_video_filters(pv);
pv->video_filters.graph = avfilter_graph_alloc();
if (pv->video_filters.graph == NULL)
{
hb_log("reinit_video_filters: avfilter_graph_alloc failed");
goto fail;
}
sws_flags = hb_strdup_printf("flags=%d", SWS_LANCZOS|SWS_ACCURATE_RND);
// avfilter_graph_free uses av_free to release scale_sws_opts. Due
// to the hacky implementation of av_free/av_malloc on windows,
// you must av_malloc anything that is av_free'd.
pv->video_filters.graph->scale_sws_opts = av_malloc(strlen(sws_flags) + 1);
strcpy(pv->video_filters.graph->scale_sws_opts, sws_flags);
free(sws_flags);
int clock_min, clock_max, clock;
hb_rational_t vrate;
hb_video_framerate_get_limits(&clock_min, &clock_max, &clock);
vrate.num = clock;
vrate.den = pv->duration * (clock / 90000.);
if (AV_PIX_FMT_YUV420P != pv->frame->format ||
orig_width != pv->frame->width ||
orig_height != pv->frame->height)
{
filter_str = hb_strdup_printf(
"scale='w=%d:h=%d:flags=lanczos+accurate_rnd',"
"format='pix_fmts=yuv420p'",
orig_width, orig_height);
graph_str = hb_append_filter_string(graph_str, filter_str);
free(filter_str);
}
if (pv->title->rotation != HB_ROTATION_0)
{
switch (pv->title->rotation)
{
case HB_ROTATION_90:
filter_str = "transpose='dir=cclock'";
break;
case HB_ROTATION_180:
filter_str = "hflip,vflip";
break;
case HB_ROTATION_270:
filter_str = "transpose='dir=clock'";
break;
default:
hb_log("reinit_video_filters: Unknown rotation, failed");
goto fail;
}
graph_str = hb_append_filter_string(graph_str, filter_str);
}
// Build filter input
filter_str = hb_strdup_printf(
"width=%d:height=%d:pix_fmt=%d:sar=%d/%d:"
"time_base=%d/%d:frame_rate=%d/%d",
pv->frame->width, pv->frame->height,
pv->frame->format,
pv->frame->sample_aspect_ratio.num,
pv->frame->sample_aspect_ratio.den,
1, 1, vrate.num, vrate.den);
avfilter = append_filter(pv, "buffer", filter_str);
free(filter_str);
if (avfilter == NULL)
{
hb_error("reinit_video_filters: failed to create buffer source filter");
goto fail;
}
pv->video_filters.input = avfilter;
// Build the filter graph
result = avfilter_graph_parse2(pv->video_filters.graph,
graph_str, &in, &out);
if (result < 0 || in == NULL || out == NULL)
{
hb_error("reinit_video_filters: avfilter_graph_parse2 failed (%s)",
graph_str);
goto fail;
}
// Link input to filter graph
result = avfilter_link(pv->video_filters.last, 0, in->filter_ctx, 0);
if (result < 0)
{
goto fail;
}
pv->video_filters.last = out->filter_ctx;
// Build filter output and append to filter graph
avfilter = append_filter(pv, "buffersink", NULL);
if (avfilter == NULL)
{
hb_error("reinit_video_filters: failed to create buffer output filter");
goto fail;
}
pv->video_filters.output = avfilter;
result = avfilter_graph_config(pv->video_filters.graph, NULL);
if (result < 0)
{
hb_error("reinit_video_filters: failed to configure filter graph");
goto fail;
}
free(graph_str);
avfilter_inout_free(&in);
avfilter_inout_free(&out);
return 0;
fail:
free(graph_str);
avfilter_inout_free(&in);
avfilter_inout_free(&out);
close_video_filters(pv);
return 1;
}
static void filter_video(hb_work_private_t *pv)
{
reinit_video_filters(pv);
if (pv->video_filters.graph != NULL)
{
int result;
result = av_buffersrc_add_frame(pv->video_filters.input, pv->frame);
if (result < 0) {
hb_error("filter_video: failed to add frame");
} else {
result = av_buffersink_get_frame(pv->video_filters.output, pv->frame);
}
while (result >= 0)
{
hb_buffer_t * buf = copy_frame(pv);
hb_buffer_list_append(&pv->list, buf);
av_frame_unref(pv->frame);
++pv->nframes;
result = av_buffersink_get_frame(pv->video_filters.output,
pv->frame);
}
}
else
{
hb_buffer_t * buf = copy_frame(pv);
hb_buffer_list_append(&pv->list, buf);
av_frame_unref(pv->frame);
++pv->nframes;
}
}
/*
* Decodes a video frame from the specified raw packet data
* ('data', 'size').
* The output of this function is stored in 'pv->list', which contains a list
* of zero or more decoded packets.
*/
static int decodeFrame( hb_work_object_t *w, packet_info_t * packet_info )
{
hb_work_private_t *pv = w->private_data;
int got_picture = 0, oldlevel = 0, ret;
AVPacket avp;
reordered_data_t * reordered;
if ( global_verbosity_level <= 1 )
{
oldlevel = av_log_get_level();
av_log_set_level( AV_LOG_QUIET );
}
av_init_packet(&avp);
if (packet_info != NULL)
{
avp.data = packet_info->data;
avp.size = packet_info->size;
avp.pts = pv->sequence;
avp.dts = pv->sequence;
reordered = malloc(sizeof(*reordered));
if (reordered != NULL)
{
reordered->sequence = pv->sequence++;
reordered->pts = packet_info->pts;
reordered->scr_sequence = packet_info->scr_sequence;
reordered->new_chap = packet_info->new_chap;
reordered_hash_add(pv, reordered);
}
// libav avcodec video decoder needs AVPacket flagged with
// AV_PKT_FLAG_KEY for some codecs. For example, sequence of
// PNG in a mov container.
if (packet_info->frametype & HB_FRAME_MASK_KEY)
{
avp.flags |= AV_PKT_FLAG_KEY;
}
}
else
{
avp.data = NULL;
avp.size = 0;
}
if (pv->palette != NULL)
{
uint8_t * palette;
int size;
palette = av_packet_new_side_data(&avp, AV_PKT_DATA_PALETTE,
AVPALETTE_SIZE);
size = MIN(pv->palette->size, AVPALETTE_SIZE);
memcpy(palette, pv->palette->data, size);
hb_buffer_close(&pv->palette);
}
ret = avcodec_send_packet(pv->context, &avp);
av_packet_unref(&avp);
if (ret < 0 && ret != AVERROR_EOF)
{
++pv->decode_errors;
return 0;
}
#ifdef USE_QSV
if (pv->qsv.decode &&
pv->qsv.config.io_pattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY &&
pv->job->qsv.ctx == NULL && pv->video_codec_opened > 0)
{
// this is quite late, but we can't be certain that the QSV context is
// available until after we call avcodec_send_packet() at least once
pv->job->qsv.ctx = pv->context->priv_data;
}
#endif
do
{
ret = avcodec_receive_frame(pv->context, pv->frame);
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
{
++pv->decode_errors;
}
if (ret < 0)
{
break;
}
got_picture = 1;
// recompute the frame/field duration, because sometimes it changes
compute_frame_duration( pv );
filter_video(pv);
} while (ret >= 0);
if ( global_verbosity_level <= 1 )
{
av_log_set_level( oldlevel );
}
return got_picture;
}
static void decodeVideo( hb_work_object_t *w, hb_buffer_t * in)
{
hb_work_private_t *pv = w->private_data;
/*
* The following loop is a do..while because we need to handle both
* data & the flush at the end (signaled by size=0). At the end there's
* generally a frame in the parser & one or more frames in the decoder
* (depending on the bframes setting).
*/
int pos, len;
int64_t pts = in->s.start;
int64_t dts = in->s.renderOffset;
if (in->s.new_chap > 0)
{
pv->new_chap = in->s.new_chap;
pv->chap_scr = in->s.scr_sequence;
if (in->s.start != AV_NOPTS_VALUE)
{
pv->chap_time = in->s.start;
}
else
{
pv->chap_time = pv->last_pts + 1;
}
}
if (in->s.start != AV_NOPTS_VALUE)
{
pv->last_pts = in->s.start;
}
// There are a 3 scenarios that can happen here.
// 1. The buffer contains exactly one frame of data
// 2. The buffer contains multiple frames of data
// 3. The buffer contains a partial frame of data
//
// In scenario 2, we want to be sure that the timestamps are only
// applied to the first frame in the buffer. Additional frames
// in the buffer will have their timestamps computed in sync.
//
// In scenario 3, we need to save the ancillary buffer info of an
// unfinished frame so it can be applied when we receive the last
// buffer of that frame.
if (!pv->unfinished)
{
// New packet, and no previous data pending
pv->packet_info.scr_sequence = in->s.scr_sequence;
pv->packet_info.new_chap = in->s.new_chap;
pv->packet_info.frametype = in->s.frametype;
}
for (pos = 0; pos < in->size; pos += len)
{
uint8_t * pout;
int pout_len;
int64_t parser_pts, parser_dts;
if (pv->parser)
{
len = av_parser_parse2(pv->parser, pv->context, &pout, &pout_len,
in->data + pos, in->size - pos,
pts, dts, 0 );
parser_pts = pv->parser->pts;
parser_dts = pv->parser->dts;
pts = AV_NOPTS_VALUE;
dts = AV_NOPTS_VALUE;
}
else
{
pout = in->data;
len = pout_len = in->size;
parser_pts = pts;
parser_dts = dts;
}
if (pout != NULL && pout_len > 0)
{
pv->packet_info.data = pout;
pv->packet_info.size = pout_len;
pv->packet_info.pts = parser_pts;
pv->packet_info.dts = parser_dts;
decodeFrame(w, &pv->packet_info);
w->frame_count++;
// There could have been an unfinished packet when we entered
// decodeVideo that is now finished. The next packet is associated
// with the input buffer, so set it's chapter and scr info.
pv->packet_info.scr_sequence = in->s.scr_sequence;
pv->packet_info.new_chap = in->s.new_chap;
pv->packet_info.frametype = in->s.frametype;
pv->unfinished = 0;
}
if (len > 0 && pout_len <= 0)
{
pv->unfinished = 1;
}
}
/* the stuff above flushed the parser, now flush the decoder */
if (in->s.flags & HB_BUF_FLAG_EOF)
{
while (decodeFrame(w, NULL))
{
continue;
}
#ifdef USE_QSV
if (pv->qsv.decode &&
pv->qsv.config.io_pattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY)
{
// flush a second time
while (decodeFrame(w, NULL))
{
continue;
}
}
#endif
if (pv->list_subtitle != NULL)
cc_send_to_decoder(pv, hb_buffer_eof_init());
}
}
static int decavcodecvInit( hb_work_object_t * w, hb_job_t * job )
{
hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
w->private_data = pv;
pv->job = job;
pv->next_pts = (int64_t)AV_NOPTS_VALUE;
if ( job )
pv->title = job->title;
else
pv->title = w->title;
if (pv->title->flags & HBTF_RAW_VIDEO)
pv->next_pts = 0;
hb_buffer_list_clear(&pv->list);
#ifdef USE_QSV
if ((pv->qsv.decode = hb_qsv_decode_is_enabled(job)))
{
pv->qsv.codec_name = hb_qsv_decode_get_codec_name(w->codec_param);
pv->qsv.config.io_pattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
#if 0 // TODO: re-implement QSV zerocopy path
hb_qsv_info_t *info = hb_qsv_info_get(job->vcodec);
if (info != NULL)
{
// setup the QSV configuration
pv->qsv.config.io_pattern = MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
pv->qsv.config.impl_requested = info->implementation;
pv->qsv.config.async_depth = job->qsv.async_depth;
pv->qsv.config.sync_need = 0;
pv->qsv.config.usage_threaded = 1;
pv->qsv.config.additional_buffers = 64; // FIFO_LARGE
if (info->capabilities & HB_QSV_CAP_RATECONTROL_LA)
{
// more surfaces may be needed for the lookahead
pv->qsv.config.additional_buffers = 160;
}
}
#endif // QSV zerocopy path
}
#endif
if( pv->job && pv->job->title && !pv->job->title->has_resolution_change )
{
pv->threads = HB_FFMPEG_THREADS_AUTO;
}
AVCodec *codec = NULL;
#ifdef USE_QSV
if (pv->qsv.decode)
{
codec = avcodec_find_decoder_by_name(pv->qsv.codec_name);
}
else
#endif
{
codec = avcodec_find_decoder(w->codec_param);
}
if ( codec == NULL )
{
hb_log( "decavcodecvInit: failed to find codec for id (%d)", w->codec_param );
return 1;
}
if ( pv->title->opaque_priv )
{
AVFormatContext *ic = (AVFormatContext*)pv->title->opaque_priv;
pv->context = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(pv->context,
ic->streams[pv->title->video_id]->codecpar);
pv->context->workaround_bugs = FF_BUG_AUTODETECT;
pv->context->err_recognition = AV_EF_CRCCHECK;
pv->context->error_concealment = FF_EC_GUESS_MVS|FF_EC_DEBLOCK;
#ifdef USE_QSV
if (pv->qsv.decode &&
pv->qsv.config.io_pattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY)
{
// set the QSV configuration before opening the decoder
pv->context->hwaccel_context = &pv->qsv.config;
}
#endif
// Set encoder opts...
AVDictionary * av_opts = NULL;
av_dict_set( &av_opts, "refcounted_frames", "1", 0 );
if (pv->title->flags & HBTF_NO_IDR)
{
av_dict_set( &av_opts, "flags", "output_corrupt", 0 );
}
#ifdef USE_QSV
if (pv->qsv.decode && pv->context->codec_id == AV_CODEC_ID_HEVC)
{
av_dict_set( &av_opts, "load_plugin", "hevc_hw", 0 );
}
#endif
if ( hb_avcodec_open( pv->context, codec, &av_opts, pv->threads ) )
{
av_dict_free( &av_opts );
hb_log( "decavcodecvInit: avcodec_open failed" );
return 1;
}
av_dict_free( &av_opts );
pv->video_codec_opened = 1;
}
else
{
pv->parser = av_parser_init( w->codec_param );
}
pv->frame = av_frame_alloc();
if (pv->frame == NULL)
{
hb_log("decavcodecvInit: av_frame_alloc failed");
return 1;
}
/*
* If not scanning, then are we supposed to extract Closed Captions
* and send them to the decoder?
*/
if (job != NULL && hb_list_count(job->list_subtitle) > 0)
{
hb_subtitle_t *subtitle;
int i = 0;
while ((subtitle = hb_list_item(job->list_subtitle, i++)) != NULL)
{
if (subtitle->source == CC608SUB)
{
if (pv->list_subtitle == NULL)
{
pv->list_subtitle = hb_list_init();
}
hb_list_add(pv->list_subtitle, subtitle);
}
}
}
return 0;
}
static int setup_extradata( hb_work_object_t *w, hb_buffer_t *in )
{
hb_work_private_t *pv = w->private_data;
// we can't call the avstream funcs but the read_header func in the
// AVInputFormat may set up some state in the AVContext. In particular
// vc1t_read_header allocates 'extradata' to deal with header issues
// related to Microsoft's bizarre engineering notions. We alloc a chunk
// of space to make vc1 work then associate the codec with the context.
if (pv->context->extradata == NULL)
{
if (pv->parser == NULL || pv->parser->parser == NULL ||
pv->parser->parser->split == NULL)
{
return 0;
}
else
{
int size;
size = pv->parser->parser->split(pv->context, in->data, in->size);
if (size > 0)
{
pv->context->extradata_size = size;
pv->context->extradata =
av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
if (pv->context->extradata == NULL)
return 1;
memcpy(pv->context->extradata, in->data, size);
return 0;
}
}
return 1;
}
return 0;
}
static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
hb_buffer_t ** buf_out )
{
hb_work_private_t *pv = w->private_data;
hb_buffer_t *in = *buf_in;
*buf_in = NULL;
*buf_out = NULL;
// libavcodec/mpeg12dec.c requires buffers to be zero padded.
// If not zero padded, it can get stuck in an infinite loop.
// It's likely there are other decoders that expect the same.
if (in->data != NULL)
{
memset(in->data + in->size, 0, in->alloc - in->size);
}
/* if we got an empty buffer signaling end-of-stream send it downstream */
if (in->s.flags & HB_BUF_FLAG_EOF)
{
if (pv->context != NULL && pv->context->codec != NULL)
{
decodeVideo(w, in);
}
hb_buffer_list_append(&pv->list, in);
*buf_out = hb_buffer_list_clear(&pv->list);
return HB_WORK_DONE;
}
// if this is the first frame open the codec (we have to wait for the
// first frame because of M$ VC1 braindamage).
if ( !pv->video_codec_opened )
{
AVCodec *codec = NULL;
#ifdef USE_QSV
if (pv->qsv.decode)
{
codec = avcodec_find_decoder_by_name(pv->qsv.codec_name);
}
else
#endif
{
codec = avcodec_find_decoder(w->codec_param);
}
if ( codec == NULL )
{
hb_log( "decavcodecvWork: failed to find codec for id (%d)", w->codec_param );
*buf_out = hb_buffer_eof_init();
return HB_WORK_DONE;
}
if (pv->context == NULL)
{
pv->context = avcodec_alloc_context3( codec );
}
pv->context->workaround_bugs = FF_BUG_AUTODETECT;
pv->context->err_recognition = AV_EF_CRCCHECK;
pv->context->error_concealment = FF_EC_GUESS_MVS|FF_EC_DEBLOCK;
if ( setup_extradata( w, in ) )
{
// we didn't find the headers needed to set up extradata.
// the codec will abort if we open it so just free the buf
// and hope we eventually get the info we need.
hb_buffer_close( &in );
return HB_WORK_OK;
}
#ifdef USE_QSV
if (pv->qsv.decode &&
pv->qsv.config.io_pattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY)
{
// set the QSV configuration before opening the decoder
pv->context->hwaccel_context = &pv->qsv.config;
}
#endif
AVDictionary * av_opts = NULL;
av_dict_set( &av_opts, "refcounted_frames", "1", 0 );
if (pv->title->flags & HBTF_NO_IDR)
{
av_dict_set( &av_opts, "flags", "output_corrupt", 0 );
}
// disable threaded decoding for scan, can cause crashes
if ( hb_avcodec_open( pv->context, codec, &av_opts, pv->threads ) )
{
av_dict_free( &av_opts );
hb_log( "decavcodecvWork: avcodec_open failed" );
// avcodec_open can fail due to incorrectly parsed extradata
// so try again when this fails
av_freep( &pv->context->extradata );
pv->context->extradata_size = 0;
hb_buffer_close( &in );
return HB_WORK_OK;
}
av_dict_free( &av_opts );
pv->video_codec_opened = 1;
}
if (in->palette != NULL)
{
pv->palette = in->palette;
in->palette = NULL;
}
decodeVideo(w, in);
hb_buffer_close( &in );
*buf_out = hb_buffer_list_clear(&pv->list);
return HB_WORK_OK;
}
static void compute_frame_duration( hb_work_private_t *pv )
{
double duration = 0.;
int64_t max_fps = 64LL;
// context->time_base may be in fields, so set the max *fields* per second
if ( pv->context->ticks_per_frame > 1 )
max_fps *= pv->context->ticks_per_frame;
if ( pv->title->opaque_priv )
{
// If ffmpeg is demuxing for us, it collects some additional
// information about framerates that is often more accurate
// than context->time_base.
AVFormatContext *ic = (AVFormatContext*)pv->title->opaque_priv;
AVStream *st = ic->streams[pv->title->video_id];
if ( st->nb_frames && st->duration )
{
// compute the average frame duration from the total number
// of frames & the total duration.
duration = ( (double)st->duration * (double)st->time_base.num ) /
( (double)st->nb_frames * (double)st->time_base.den );
}
// Raw demuxers set a default fps of 25 and do not parse
// a value from the container. So use the codec time_base
// for raw demuxers.
else if (ic->iformat->raw_codec_id == AV_CODEC_ID_NONE)
{
// XXX We don't have a frame count or duration so try to use the
// far less reliable time base info in the stream.
// Because the time bases are so screwed up, we only take values
// in the range 8fps - 64fps.
AVRational *tb = NULL;
if ( st->avg_frame_rate.den * 64LL > st->avg_frame_rate.num &&
st->avg_frame_rate.num > st->avg_frame_rate.den * 8LL )
{
tb = &(st->avg_frame_rate);
duration = (double)tb->den / (double)tb->num;
}
else if ( st->time_base.num * 64LL > st->time_base.den &&
st->time_base.den > st->time_base.num * 8LL )
{
tb = &(st->time_base);
duration = (double)tb->num / (double)tb->den;
}
}
if ( !duration &&
pv->context->time_base.num * max_fps > pv->context->time_base.den &&
pv->context->time_base.den > pv->context->time_base.num * 8LL )
{
duration = (double)pv->context->time_base.num /
(double)pv->context->time_base.den;
if ( pv->context->ticks_per_frame > 1 )
{
// for ffmpeg 0.5 & later, the H.264 & MPEG-2 time base is
// field rate rather than frame rate so convert back to frames.
duration *= pv->context->ticks_per_frame;
}
}
}
else
{
if ( pv->context->time_base.num * max_fps > pv->context->time_base.den &&
pv->context->time_base.den > pv->context->time_base.num * 8LL )
{
duration = (double)pv->context->time_base.num /
(double)pv->context->time_base.den;
if ( pv->context->ticks_per_frame > 1 )
{
// for ffmpeg 0.5 & later, the H.264 & MPEG-2 time base is
// field rate rather than frame rate so convert back to frames.
duration *= pv->context->ticks_per_frame;
}
}
}
if ( duration == 0 )
{
// No valid timing info found in the stream, so pick some value
duration = 1001. / 24000.;
}
pv->duration = duration * 90000.;
pv->field_duration = pv->duration;
if ( pv->context->ticks_per_frame > 1 )
{
pv->field_duration /= pv->context->ticks_per_frame;
}
}
static int get_color_prim(int color_primaries, hb_geometry_t geometry, hb_rational_t rate)
{
switch (color_primaries)
{
case AVCOL_PRI_BT709:
return HB_COLR_PRI_BT709;
case AVCOL_PRI_BT470BG:
return HB_COLR_PRI_EBUTECH;
case AVCOL_PRI_BT470M:
case AVCOL_PRI_SMPTE170M:
case AVCOL_PRI_SMPTE240M:
return HB_COLR_PRI_SMPTEC;
case AVCOL_PRI_BT2020:
return HB_COLR_PRI_BT2020;
default:
{
if ((geometry.width >= 1280 || geometry.height >= 720)||
(geometry.width > 720 && geometry.height > 576 ))
// ITU BT.709 HD content
return HB_COLR_PRI_BT709;
else if (rate.den == 1080000)
// ITU BT.601 DVD or SD TV content (PAL)
return HB_COLR_PRI_EBUTECH;
else
// ITU BT.601 DVD or SD TV content (NTSC)
return HB_COLR_PRI_SMPTEC;
}
}
}
static int get_color_transfer(int color_trc)
{
switch (color_trc)
{
case AVCOL_TRC_SMPTE240M:
return HB_COLR_TRA_SMPTE240M;
case AVCOL_TRC_SMPTEST2084:
return HB_COLR_TRA_SMPTEST2084;
case AVCOL_TRC_ARIB_STD_B67:
return HB_COLR_TRA_ARIB_STD_B67;
case AVCOL_TRC_BT2020_10:
return HB_COLR_TRA_BT2020_10;
case AVCOL_TRC_BT2020_12:
return HB_COLR_TRA_BT2020_12;
default:
// ITU BT.601, BT.709, anything else
return HB_COLR_TRA_BT709;
}
}
static int get_color_matrix(int colorspace, hb_geometry_t geometry)
{
switch (colorspace)
{
case AVCOL_SPC_BT709:
return HB_COLR_MAT_BT709;
case AVCOL_SPC_FCC:
case AVCOL_SPC_BT470BG:
case AVCOL_SPC_SMPTE170M:
case AVCOL_SPC_RGB: // libswscale rgb2yuv
return HB_COLR_MAT_SMPTE170M;
case AVCOL_SPC_SMPTE240M:
return HB_COLR_MAT_SMPTE240M;
case AVCOL_SPC_BT2020_NCL:
return HB_COLR_MAT_BT2020_NCL;
case AVCOL_SPC_BT2020_CL:
return HB_COLR_MAT_BT2020_CL;
default:
{
if ((geometry.width >= 1280 || geometry.height >= 720)||
(geometry.width > 720 && geometry.height > 576 ))
// ITU BT.709 HD content
return HB_COLR_MAT_BT709;
else
// ITU BT.601 DVD or SD TV content (PAL)
// ITU BT.601 DVD or SD TV content (NTSC)
return HB_COLR_MAT_SMPTE170M;
}
}
}
static int decavcodecvInfo( hb_work_object_t *w, hb_work_info_t *info )
{
hb_work_private_t *pv = w->private_data;
int clock_min, clock_max, clock;
hb_video_framerate_get_limits(&clock_min, &clock_max, &clock);
memset( info, 0, sizeof(*info) );
if (pv->context == NULL || pv->context->codec == NULL)
return 0;
info->bitrate = pv->context->bit_rate;
if (w->title->rotation == HB_ROTATION_90 ||
w->title->rotation == HB_ROTATION_270)
{
// HandBrake's video pipeline uses yuv420 color. This means all
// dimensions must be even. So we must adjust the dimensions
// of incoming video if not even.
info->geometry.width = pv->context->height & ~1;
info->geometry.height = pv->context->width & ~1;
info->geometry.par.num = pv->context->sample_aspect_ratio.den;
info->geometry.par.den = pv->context->sample_aspect_ratio.num;
}
else
{
// HandBrake's video pipeline uses yuv420 color. This means all
// dimensions must be even. So we must adjust the dimensions
// of incoming video if not even.
info->geometry.width = pv->context->width & ~1;
info->geometry.height = pv->context->height & ~1;
info->geometry.par.num = pv->context->sample_aspect_ratio.num;
info->geometry.par.den = pv->context->sample_aspect_ratio.den;
}
compute_frame_duration( pv );
info->rate.num = clock;
info->rate.den = pv->duration * (clock / 90000.);
info->profile = pv->context->profile;
info->level = pv->context->level;
info->name = pv->context->codec->name;
info->color_prim = get_color_prim(pv->context->color_primaries, info->geometry, info->rate);
info->color_transfer = get_color_transfer(pv->context->color_trc);
info->color_matrix = get_color_matrix(pv->context->colorspace, info->geometry);
info->video_decode_support = HB_DECODE_SUPPORT_SW;
#ifdef USE_QSV
if (avcodec_find_decoder_by_name(hb_qsv_decode_get_codec_name(pv->context->codec_id)))
{
switch (pv->context->codec_id)
{
case AV_CODEC_ID_HEVC:
case AV_CODEC_ID_H264:
if (pv->context->pix_fmt == AV_PIX_FMT_YUV420P ||
pv->context->pix_fmt == AV_PIX_FMT_YUVJ420P ||
pv->context->pix_fmt == AV_PIX_FMT_YUV420P10LE)
{
info->video_decode_support |= HB_DECODE_SUPPORT_QSV;
}
break;
default:
break;
}
}
#endif
return 1;
}
static int decavcodecvBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
hb_work_info_t *info )
{
return 0;
}
static void decavcodecvFlush( hb_work_object_t *w )
{
hb_work_private_t *pv = w->private_data;
if (pv->context != NULL && pv->context->codec != NULL)
{
hb_buffer_list_close(&pv->list);
if ( pv->title->opaque_priv == NULL )
{
pv->video_codec_opened = 0;
hb_avcodec_free_context(&pv->context);
if ( pv->parser )
{
av_parser_close(pv->parser);
}
pv->parser = av_parser_init( w->codec_param );
}
else
{
avcodec_flush_buffers( pv->context );
}
}
}
hb_work_object_t hb_decavcodecv =
{
.id = WORK_DECAVCODECV,
.name = "Video decoder (libavcodec)",
.init = decavcodecvInit,
.work = decavcodecvWork,
.close = decavcodecClose,
.flush = decavcodecvFlush,
.info = decavcodecvInfo,
.bsinfo = decavcodecvBSInfo
};
static void decodeAudio(hb_work_private_t *pv, packet_info_t * packet_info)
{
AVCodecContext * context = pv->context;
AVPacket avp;
int ret;
// libav does not supply timestamps for wmapro audio (possibly others)
// if there is an input timestamp, initialize next_pts
if (pv->next_pts == (int64_t)AV_NOPTS_VALUE &&
packet_info != NULL &&
packet_info->pts != AV_NOPTS_VALUE)
{
pv->next_pts = packet_info->pts;
}
av_init_packet(&avp);
if (packet_info != NULL)
{
avp.data = packet_info->data;
avp.size = packet_info->size;
avp.pts = packet_info->pts;
avp.dts = AV_NOPTS_VALUE;
}
else
{
avp.data = NULL;
avp.size = 0;
}
ret = avcodec_send_packet(context, &avp);
if (ret < 0 && ret != AVERROR_EOF)
{
av_packet_unref(&avp);
return;
}
do
{
ret = avcodec_receive_frame(context, pv->frame);
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
{
++pv->decode_errors;
}
if (ret < 0)
{
break;
}
hb_buffer_t * out;
int samplerate;
// libavcoded doesn't yet consistently set frame->sample_rate
if (pv->frame->sample_rate != 0)
{
samplerate = pv->frame->sample_rate;
}
else
{
samplerate = context->sample_rate;
}
pv->duration = (90000. * pv->frame->nb_samples / samplerate);
int64_t pts = pv->frame->pts;
double duration = pv->duration;
if (pv->audio->config.out.codec & HB_ACODEC_PASS_FLAG)
{
// Note that even though we are doing passthru, we had to decode
// so that we know the stop time and the pts of the next audio
// packet.
out = hb_buffer_init(avp.size);
memcpy(out->data, avp.data, avp.size);
}
else
{
AVFrameSideData *side_data;
if ((side_data =
av_frame_get_side_data(pv->frame,
AV_FRAME_DATA_DOWNMIX_INFO)) != NULL)
{
double surround_mix_level, center_mix_level;
AVDownmixInfo * downmix_info;
downmix_info = (AVDownmixInfo*)side_data->data;
if (pv->audio->config.out.mixdown == HB_AMIXDOWN_DOLBY ||
pv->audio->config.out.mixdown == HB_AMIXDOWN_DOLBYPLII)
{
surround_mix_level = downmix_info->surround_mix_level_ltrt;
center_mix_level = downmix_info->center_mix_level_ltrt;
}
else
{
surround_mix_level = downmix_info->surround_mix_level;
center_mix_level = downmix_info->center_mix_level;
}
hb_audio_resample_set_mix_levels(pv->resample,
surround_mix_level,
center_mix_level,
downmix_info->lfe_mix_level);
}
hb_audio_resample_set_channel_layout(pv->resample,
pv->frame->channel_layout);
hb_audio_resample_set_sample_fmt(pv->resample,
pv->frame->format);
if (hb_audio_resample_update(pv->resample))
{
hb_log("decavcodec: hb_audio_resample_update() failed");
av_frame_unref(pv->frame);
av_packet_unref(&avp);
return;
}
out = hb_audio_resample(pv->resample, pv->frame->extended_data,
pv->frame->nb_samples);
if (out != NULL && pv->drop_samples > 0)
{
/* drop audio samples that are part of the encoder delay */
int channels = hb_mixdown_get_discrete_channel_count(
pv->audio->config.out.mixdown);
int sample_size = channels * sizeof(float);
int samples = out->size / sample_size;
if (samples <= pv->drop_samples)
{
hb_buffer_close(&out);
pv->drop_samples -= samples;
}
else
{
int size = pv->drop_samples * sample_size;
double drop_duration = pv->drop_samples * 90000L /
pv->audio->config.out.samplerate;
memmove(out->data, out->data + size, out->size - size);
out->size -= size;
pts += drop_duration;
duration -= drop_duration;
pv->drop_samples = 0;
}
}
}
if (out != NULL)
{
out->s.scr_sequence = packet_info->scr_sequence;
out->s.start = pts;
out->s.duration = duration;
if (out->s.start == AV_NOPTS_VALUE)
{
out->s.start = pv->next_pts;
}
else
{
pv->next_pts = out->s.start;
}
if (pv->next_pts != (int64_t)AV_NOPTS_VALUE)
{
pv->next_pts += pv->duration;
out->s.stop = pv->next_pts;
}
hb_buffer_list_append(&pv->list, out);
}
av_frame_unref(pv->frame);
++pv->nframes;
} while (ret >= 0);
av_packet_unref(&avp);
}
|