aboutsummaryrefslogtreecommitdiffstats
path: root/channels/console_video.c
blob: 86473e5898821722065ec4d0c2b1414d0c3b740c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
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
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
/*
 * Experimental support for video sessions. We use SDL for rendering, ffmpeg
 * as the codec library for encoding and decoding, and Video4Linux and X11
 * to generate the local video stream.
 *
 * If one of these pieces is not available, either at compile time or at
 * runtime, we do our best to run without it. Of course, no codec library
 * means we can only deal with raw data, no SDL means we cannot do rendering,
 * no V4L or X11 means we cannot generate data (but in principle we could
 * stream from or record to a file).
 *
 * We need a recent (2007.07.12 or newer) version of ffmpeg to avoid warnings.
 * Older versions might give 'deprecated' messages during compilation,
 * thus not compiling in AST_DEVMODE, or don't have swscale, in which case
 * you can try to compile #defining OLD_FFMPEG here.
 *
 * $Revision$
 */

//#define DROP_PACKETS 5       /* if set, drop this % of video packets */
//#define OLD_FFMPEG	1	/* set for old ffmpeg with no swscale */

#include "asterisk.h"
#include <sys/ioctl.h>
#include <math.h>	/* sqrt */
#include "asterisk/cli.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"

#include "console_video.h"

/*
The code is structured as follows.

When a new console channel is created, we call console_video_start()
to initialize SDL, the source, and the encoder/ decoder for the
formats in use (XXX the latter two should be done later, once the
codec negotiation is complete).  Also, a thread is created to handle
the video source and generate frames.

While communication is on, the local source is generated by the
video thread, which wakes up periodically, generates frames and
enqueues them in chan->readq.  Incoming rtp frames are passed to
console_write_video(), decoded and passed to SDL for display.

For as unfortunate and confusing as it can be, we need to deal with a
number of different video representations (size, codec/pixel format,
codec parameters), as follows:

 loc_src	is the data coming from the camera/X11/etc.
	The format is typically constrained by the video source.

 enc_in		is the input required by the encoder.
	Typically constrained in size by the encoder type.

 enc_out	is the bitstream transmitted over RTP.
	Typically negotiated while the call is established.

 loc_dpy	is the format used to display the local video source.
	Depending on user preferences this can have the same size as
	loc_src_fmt, or enc_in_fmt, or thumbnail size (e.g. PiP output)

 dec_in		is the incoming RTP bitstream. Negotiated
	during call establishment, it is not necessarily the same as
	enc_in_fmt

 dec_out	the output of the decoder.
	The format is whatever the other side sends, and the
	buffer is allocated by avcodec_decode_... so we only
	copy the data here.

 rem_dpy	the format used to display the remote stream

We store the format info together with the buffer storing the data.
As a future optimization, a format/buffer may reference another one
if the formats are equivalent. This will save some unnecessary format
conversion.


In order to handle video you need to add to sip.conf (and presumably
iax.conf too) the following:

	[general](+)
		videosupport=yes
		allow=h263	; this or other video formats
		allow=h263p	; this or other video formats

 */

/*
 * Codecs are absolutely necessary or we cannot do anything.
 * In principle SDL is optional too (used for rendering only, but we
 * could still source data withouth it), however at the moment it is required.
 */
#if !defined(HAVE_VIDEO_CONSOLE) || !defined(HAVE_FFMPEG) || !defined(HAVE_SDL)
/* stubs if required pieces are missing */
int console_write_video(struct ast_channel *chan, struct ast_frame *f)
{
	return 0;	/* writing video not supported */
}

int console_video_cli(struct video_desc *env, const char *var, int fd)
{
	return 1;	/* nothing matched */
}

int console_video_config(struct video_desc **penv, const char *var, const char *val)
{
	return 1;	/* no configuration */
}

void console_video_start(struct video_desc *env, struct ast_channel *owner)
{
	ast_log(LOG_WARNING, "console video support not present\n");
}

void console_video_uninit(struct video_desc *env)
{
}

int console_video_formats = 0;

#else /* defined(HAVE_FFMPEG) && defined(HAVE_SDL) */

/*! The list of video formats we support. */
int console_video_formats = 
	AST_FORMAT_H263_PLUS | AST_FORMAT_H263 |
	AST_FORMAT_MP4_VIDEO | AST_FORMAT_H264 | AST_FORMAT_H261 ;

#ifdef HAVE_X11
#include <X11/Xlib.h>		/* this should be conditional */
#endif

#include <ffmpeg/avcodec.h>
#ifndef OLD_FFMPEG
#include <ffmpeg/swscale.h>	/* requires a recent ffmpeg */
#endif

#include <SDL/SDL.h>
#ifdef HAVE_SDL_IMAGE
#include <SDL/SDL_image.h>	/* for loading images */
#endif
#ifdef HAVE_SDL_TTF
#include <SDL/SDL_ttf.h>	/* render text on sdl surfaces */
#endif

/*
 * In many places we use buffers to store the raw frames (but not only),
 * so here is a structure to keep all the info. data = NULL means the
 * structure is not initialized, so the other fields are invalid.
 * size = 0 means the buffer is not malloc'ed so we don't have to free it.
 */
struct fbuf_t {		/* frame buffers, dynamically allocated */
	uint8_t	*data;	/* memory, malloced if size > 0, just reference
			 * otherwise */
	int	size;	/* total size in bytes */
	int	used;	/* space used so far */
	int	ebit;	/* bits to ignore at the end */
	int	x;	/* origin, if necessary */
	int	y;
	int	w;	/* size */ 
	int	h;
	int	pix_fmt;
};

struct video_codec_desc;	/* forward declaration */
/*
 * Descriptor of the local source, made of the following pieces:
 *  + configuration info (geometry, device name, fps...). These are read
 *    from the config file and copied here before calling video_out_init();
 *  + the frame buffer (buf) and source pixel format, allocated at init time;
 *  + the encoding and RTP info, including timestamps to generate
 *    frames at the correct rate;
 *  + source-specific info, i.e. fd for /dev/video, dpy-image for x11, etc,
 *    filled in by video_open
 * NOTE: loc_src.data == NULL means the rest of the struct is invalid, and
 *	the video source is not available.
 */
struct video_out_desc {
	/* video device support.
	 * videodevice and geometry are read from the config file.
	 * At the right time we try to open it and allocate a buffer.
	 * If we are successful, webcam_bufsize > 0 and we can read.
	 */
	/* all the following is config file info copied from the parent */
	char		videodevice[64];
	int		fps;
	int		bitrate;
	int		qmin;

	int sendvideo;

	struct fbuf_t	loc_src;	/* local source buffer, allocated in video_open() */
	struct fbuf_t	enc_in;		/* encoder input buffer, allocated in video_out_init() */
	struct fbuf_t	enc_out;	/* encoder output buffer, allocated in video_out_init() */
	struct fbuf_t	loc_dpy;	/* display source buffer, no buffer (managed by SDL in bmp[1]) */
	struct fbuf_t	keypad_dpy;	/* keypad source buffer, XXX */

	struct video_codec_desc *enc;	/* encoder */
	AVCodecContext	*enc_ctx;	/* encoding context */
	AVCodec		*codec;
	AVFrame		*frame;	/* The initial part is an AVPicture */
	int		mtu;
	struct timeval	last_frame;	/* when we read the last frame ? */

	/* device specific info */
	int 		fd;		/* file descriptor, for webcam */
#ifdef HAVE_X11
	Display		*dpy;			/* x11 grabber info */
	XImage		*image;
	int		screen_width;	/* width of X screen */
	int		screen_height;	/* height of X screen */
#endif
};

/*
 * Descriptor for the incoming stream, with a buffer for the bitstream
 * extracted by the RTP packets, RTP reassembly info, and a frame buffer
 * for the decoded frame (buf).
 * and store the result in a suitable frame buffer for later display.
 * NOTE: dec_ctx == NULL means the rest is invalid (e.g. because no
 *	codec, no memory, etc.) and we must drop all incoming frames.
 *
 * Incoming payload is stored in one of the dec_in[] buffers, which are
 * emptied by the video thread. These buffers are organized in a circular
 * queue, with dec_in_cur being the buffer in use by the incoming stream,
 * and dec_in_dpy is the one being displayed. When the pointers need to
 * be changed, we synchronize the access to them with dec_in_lock.
 * When the list is full dec_in_cur = NULL (we cannot store new data),
 * when the list is empty dec_in_dpy is NULL (we cannot display frames).
 */
struct video_in_desc {
	struct video_codec_desc *dec;	/* decoder */
	AVCodecContext          *dec_ctx;	/* information about the codec in the stream */
	AVCodec                 *codec;		/* reference to the codec */
	AVFrame                 *d_frame;	/* place to store the decoded frame */
	AVCodecParserContext    *parser;
	uint16_t 		next_seq;	/* must be 16 bit */
	int                     discard;	/* flag for discard status */
#define N_DEC_IN	3	/* number of incoming buffers */
	struct fbuf_t		*dec_in_cur;	/* buffer being filled in */
	struct fbuf_t		*dec_in_dpy;	/* buffer to display */
	ast_mutex_t		dec_in_lock;
	struct fbuf_t dec_in[N_DEC_IN];	/* incoming bitstream, allocated/extended in fbuf_append() */
	struct fbuf_t dec_out;	/* decoded frame, no buffer (data is in AVFrame) */
	struct fbuf_t rem_dpy;	/* display remote image, no buffer (it is in win[WIN_REMOTE].bmp) */
};

/*
 * Each codec is defined by a number of callbacks
 */
/*! \brief initialize the encoder */
typedef int (*encoder_init_f)(struct video_out_desc *v);

/*! \brief actually call the encoder */
typedef int (*encoder_encode_f)(struct video_out_desc *v);

/*! \brief encapsulate the bistream in RTP frames */
typedef struct ast_frame *(*encoder_encap_f)(struct video_out_desc *out,
		struct ast_frame **tail);

/*! \brief inizialize the decoder */
typedef int (*decoder_init_f)(struct video_in_desc *v);

/*! \brief extract the bitstream from RTP frames and store in the fbuf.
 * return 0 if ok, 1 on error
 */
typedef int (*decoder_decap_f)(struct fbuf_t *b, uint8_t *data, int len);

/*! \brief actually call the decoder */
typedef int (*decoder_decode_f)(struct video_in_desc *v, struct fbuf_t *b);

struct video_codec_desc {
	const char		*name;		/* format name */
	int			format;		/* AST_FORMAT_* */
	encoder_init_f		enc_init;
	encoder_encap_f		enc_encap;
	encoder_encode_f	enc_run;
	decoder_init_f		dec_init;
	decoder_decap_f		dec_decap;
	decoder_decode_f	dec_run;
};

/* our representation of a displayed window. SDL can only do one main
 * window so we map everything within that one
 */
enum { WIN_LOCAL, WIN_REMOTE, WIN_KEYPAD, WIN_MAX };

struct display_window	{
	SDL_Overlay             *bmp;
	SDL_Rect		rect;	/* loc. of images */
};

#define GUI_BUFFER_LEN 256			/* buffer lenght used for input buffers */

enum kp_type { KP_NONE, KP_RECT, KP_CIRCLE };
struct keypad_entry {
	int c;	/* corresponding character */
	int x0, y0, x1, y1, h;	/* arguments */
	enum kp_type type;
};

/*! \brief info related to the gui: button status, mouse coords, etc. */
struct gui_info {
	char			inbuf[GUI_BUFFER_LEN];	/* buffer for to-dial buffer */
	int			inbuf_pos;	/* next free position in inbuf */
	char			msgbuf[GUI_BUFFER_LEN];	/* buffer for text-message buffer */
	int			msgbuf_pos;	/* next free position in msgbuf */
	int			text_mode;	/* switch to-dial and text-message mode */
	int			drag_mode;	/* switch phone and drag-source mode */
	int			x_drag;		/* x coordinate where the drag starts */
	int			y_drag;		/* y coordinate where the drag starts */
#ifdef HAVE_SDL_TTF
	TTF_Font                *font;          /* font to be used */ 
#endif
	int			outfd;		/* fd for output */
	SDL_Surface		*keypad;	/* the pixmap for the keypad */
	int kp_size, kp_used;
	struct keypad_entry *kp;
};

/*
 * The overall descriptor, with room for config info, video source and
 * received data descriptors, SDL info, etc.
 */
struct video_desc {
	char			codec_name[64];	/* the codec we use */

	pthread_t		vthread;	/* video thread */
	int			shutdown;	/* set to shutdown vthread */
	struct ast_channel	*owner;		/* owner channel */

	struct video_in_desc	in;		/* remote video descriptor */
	struct video_out_desc	out;		/* local video descriptor */

	struct gui_info		gui;

	/* support for display. */
	int                     sdl_ok;
	int			gui_ok;
	SDL_Surface             *screen;	/* the main window */
	char			keypad_file[256];	/* image for the keypad */
	char                    keypad_font[256];       /* font for the keypad */
	struct display_window	win[WIN_MAX];
};

static AVPicture *fill_pict(struct fbuf_t *b, AVPicture *p);

static void fbuf_free(struct fbuf_t *b)
{
	struct fbuf_t x = *b;

	if (b->data && b->size)
		ast_free(b->data);
	bzero(b, sizeof(*b));
	/* restore some fields */
	b->w = x.w;
	b->h = x.h;
	b->pix_fmt = x.pix_fmt;
}

/*
 * Append a chunk of data to a buffer taking care of bit alignment
 * Return 0 on success, != 0 on failure
 */
static int fbuf_append(struct fbuf_t *b, uint8_t *src, int len,
	int sbit, int ebit)
{
	/*
	 * Allocate buffer. ffmpeg wants an extra FF_INPUT_BUFFER_PADDING_SIZE,
	 * and also wants 0 as a buffer terminator to prevent trouble.
	 */
	int need = len + FF_INPUT_BUFFER_PADDING_SIZE;
	int i;
	uint8_t *dst, mask;

	if (b->data == NULL) {
		b->size = need;
		b->used = 0;
		b->ebit = 0;
		b->data = ast_calloc(1, b->size);
	} else if (b->used + need > b->size) {
		b->size = b->used + need;
		b->data = ast_realloc(b->data, b->size);
	}
	if (b->data == NULL) {
		ast_log(LOG_WARNING, "alloc failure for %d, discard\n",
			b->size);
		return 1;
	}
	if (b->used == 0 && b->ebit != 0) {
		ast_log(LOG_WARNING, "ebit not reset at start\n");
		b->ebit = 0;
	}
	dst = b->data + b->used;
	i = b->ebit + sbit;	/* bits to ignore around */
	if (i == 0) {	/* easy case, just append */
		/* do everything in the common block */
	} else if (i == 8) { /* easy too, just handle the overlap byte */
		mask = (1 << b->ebit) - 1;
		/* update the last byte in the buffer */
		dst[-1] &= ~mask;	/* clear bits to ignore */
		dst[-1] |= (*src & mask);	/* append new bits */
		src += 1;	/* skip and prepare for common block */
		len --;
	} else {	/* must shift the new block, not done yet */
		ast_log(LOG_WARNING, "must handle shift %d %d at %d\n",
			b->ebit, sbit, b->used);
		return 1;
	}
	memcpy(dst, src, len);
	b->used += len;
	b->ebit = ebit;
	b->data[b->used] = 0;	/* padding */
	return 0;
}

/*!
 * Build an ast_frame for a given chunk of data, and link it into
 * the queue, with possibly 'head' bytes at the beginning to
 * fill in some fields later.
 */
static struct ast_frame *create_video_frame(uint8_t *start, uint8_t *end,
	               int format, int head, struct ast_frame *prev)
{
	int len = end-start;
	uint8_t *data;
	struct ast_frame *f;

	data = ast_calloc(1, len+head);
	f = ast_calloc(1, sizeof(*f));
	if (f == NULL || data == NULL) {
		ast_log(LOG_WARNING, "--- frame error f %p data %p len %d format %d\n",
				f, data, len, format);
		if (f)
			ast_free(f);
		if (data)
			ast_free(data);
		return NULL;
	}
	memcpy(data+head, start, len);
	f->data = data;
	f->mallocd = AST_MALLOCD_DATA | AST_MALLOCD_HDR;
	//f->has_timing_info = 1;
	//f->ts = ast_tvdiff_ms(ast_tvnow(), out->ts);
	f->datalen = len+head;
	f->frametype = AST_FRAME_VIDEO;
	f->subclass = format;
	f->samples = 0;
	f->offset = 0;
	f->src = "Console";
	f->delivery.tv_sec = 0;
	f->delivery.tv_usec = 0;
	f->seqno = 0;
	AST_LIST_NEXT(f, frame_list) = NULL;

	if (prev)
	        AST_LIST_NEXT(prev, frame_list) = f;

	return f;
}

/* some debugging code to check the bitstream:
 * declare a bit buffer, initialize it, and fetch data from it.
 */
struct bitbuf {
	const uint8_t *base;
	int	bitsize;	/* total size in bits */
	int	ofs;	/* next bit to read */
};

static struct bitbuf bitbuf_init(const uint8_t *base, int bitsize, int start_ofs)
{
	struct bitbuf a;
	a.base = base;
	a.bitsize = bitsize;
	a.ofs = start_ofs;
	return a;
}

static int bitbuf_left(struct bitbuf *b)
{
	return b->bitsize - b->ofs;
}

static uint32_t getbits(struct bitbuf *b, int n)
{
	int i, ofs;
	const uint8_t *d;
	uint8_t mask;
	uint32_t retval = 0;
	if (n> 31) {
		ast_log(LOG_WARNING, "too many bits %d, max 32\n", n);
		return 0;
	}
	if (n + b->ofs > b->bitsize) {
		ast_log(LOG_WARNING, "bitbuf overflow %d of %d\n", n + b->ofs, b->bitsize);
		n = b->bitsize - b->ofs;
	}
	ofs = 7 - b->ofs % 8;	/* start from msb */
	mask = 1 << ofs;
	d = b->base + b->ofs / 8;	/* current byte */
	for (i=0 ; i < n; i++) {
		retval += retval + (*d & mask ? 1 : 0);	/* shift in new byte */
		b->ofs++;
		mask >>= 1;
		if (mask == 0) {
			d++;
			mask = 0x80;
		}
	}
	return retval;
}

static void check_h261(struct fbuf_t *b)
{
	struct bitbuf a = bitbuf_init(b->data, b->used * 8, 0);
	uint32_t x, y;
	
	x = getbits(&a, 20);	/* PSC, 0000 0000 0000 0001 0000 */
	if (x != 0x10) {
		ast_log(LOG_WARNING, "bad PSC 0x%x\n", x);
		return;
	}
	x = getbits(&a, 5);	/* temporal reference */
	y = getbits(&a, 6);	/* ptype */
	if (0)
	ast_log(LOG_WARNING, "size %d TR %d PTY spl %d doc %d freeze %d %sCIF hi %d\n",
		b->used,
		x,
		(y & 0x20) ? 1 : 0,
		(y & 0x10) ? 1 : 0,
		(y & 0x8) ? 1 : 0,
		(y & 0x4) ? "" : "Q",
		(y & 0x2) ? 1:0);
	while ( (x = getbits(&a, 1)) == 1)
		ast_log(LOG_WARNING, "PSPARE 0x%x\n", getbits(&a, 8));
	// ast_log(LOG_WARNING, "PSPARE 0 - start GOB LAYER\n");
	while ( (x = bitbuf_left(&a)) > 0) {
		// ast_log(LOG_WARNING, "GBSC %d bits left\n", x);
		x = getbits(&a, 16); /* GBSC 0000 0000 0000 0001 */
		if (x != 0x1) {
			ast_log(LOG_WARNING, "bad GBSC 0x%x\n", x);
			break;
		}
		x = getbits(&a, 4);	/* group number */
		y = getbits(&a, 5);	/* gquant */
		if (x == 0) {
			ast_log(LOG_WARNING, "  bad GN %d\n", x);
			break;
		}
		while ( (x = getbits(&a, 1)) == 1)
			ast_log(LOG_WARNING, "GSPARE 0x%x\n", getbits(&a, 8));
		while ( (x = bitbuf_left(&a)) > 0) { /* MB layer */
			break;
		}
	}
}

void dump_buf(struct fbuf_t *b);
void dump_buf(struct fbuf_t *b)
{
	int i, x, last2lines;
	char buf[80];

	last2lines = (b->used - 16) & ~0xf;
	ast_log(LOG_WARNING, "buf size %d of %d\n", b->used, b->size);
	for (i = 0; i < b->used; i++) {
		x = i & 0xf;
		if ( x == 0) {	/* new line */
			if (i != 0)
				ast_log(LOG_WARNING, "%s\n", buf);
			bzero(buf, sizeof(buf));
			sprintf(buf, "%04x: ", i);
		}
		sprintf(buf + 6 + x*3, "%02x ", b->data[i]);
		if (i > 31 && i < last2lines)
			i = last2lines - 1;
	}
	if (buf[0])
		ast_log(LOG_WARNING, "%s\n", buf);
}
/*
 * Here starts the glue code for the various supported video codecs.
 * For each of them, we need to provide routines for initialization,
 * calling the encoder, encapsulating the bitstream in ast_frames,
 * extracting payload from ast_frames, and calling the decoder.
 */

/*--- h263+ support --- */

/*! \brief initialization of h263p */
static int h263p_enc_init(struct video_out_desc *v)
{
	/* modes supported are
	- Unrestricted Motion Vector (annex D)
	- Advanced Prediction (annex F)
	- Advanced Intra Coding (annex I)
	- Deblocking Filter (annex J)
	- Slice Structure (annex K)
	- Alternative Inter VLC (annex S)
	- Modified Quantization (annex T)
	*/
	v->enc_ctx->flags |=CODEC_FLAG_H263P_UMV; /* annex D */
	v->enc_ctx->flags |=CODEC_FLAG_AC_PRED; /* annex f ? */
	v->enc_ctx->flags |=CODEC_FLAG_H263P_SLICE_STRUCT; /* annex k */
	v->enc_ctx->flags |= CODEC_FLAG_H263P_AIC; /* annex I */

	v->enc_ctx->gop_size = v->fps*5; // emit I frame every 5 seconds
	return 0;
}


/*
 * Create RTP/H.263 fragments to avoid IP fragmentation. We fragment on a
 * PSC or a GBSC, but if we don't find a suitable place just break somewhere.
 * Everything is byte-aligned.
 */
static struct ast_frame *h263p_encap(struct video_out_desc *out,
	struct ast_frame **tail)
{
	struct ast_frame *cur = NULL, *first = NULL;
	uint8_t *d = out->enc_out.data;
	int len = out->enc_out.used;
	int l = len; /* size of the current fragment. If 0, must look for a psc */

	for (;len > 0; len -= l, d += l) {
		uint8_t *data;
		struct ast_frame *f;
		int i, h;

		if (len >= 3 && d[0] == 0 && d[1] == 0 && d[2] >= 0x80) {
			/* we are starting a new block, so look for a PSC. */
			for (i = 3; i < len - 3; i++) {
				if (d[i] == 0 && d[i+1] == 0 && d[i+2] >= 0x80) {
					l = i;
					break;
				}
			}
		}
		if (l > out->mtu || l > len) { /* psc not found, split */
			l = MIN(len, out->mtu);
		}
		if (l < 1 || l > out->mtu) {
			ast_log(LOG_WARNING, "--- frame error l %d\n", l);
			break;
		}
		
		if (d[0] == 0 && d[1] == 0) { /* we start with a psc */
			h = 0;
		} else { /* no psc, create a header */
			h = 2;
		}

		f = create_video_frame(d, d+l, AST_FORMAT_H263_PLUS, h, cur);
		if (!f)
			break;

		data = f->data;
		if (h == 0) {	/* we start with a psc */
			data[0] |= 0x04;	// set P == 1, and we are done
		} else {	/* no psc, create a header */
			data[0] = data[1] = 0;	// P == 0
		}

		if (!cur)
			first = f;
		cur = f;
	}

	if (cur)
		cur->subclass |= 1; // RTP Marker

	*tail = cur;	/* end of the list */
	return first;
}

/*! \brief extract the bitstreem from the RTP payload.
 * This is format dependent.
 * For h263+, the format is defined in RFC 2429
 * and basically has a fixed 2-byte header as follows:
 * 5 bits	RR	reserved, shall be 0
 * 1 bit	P	indicate a start/end condition,
 *			in which case the payload should be prepended
 *			by two zero-valued bytes.
 * 1 bit	V	there is an additional VRC header after this header
 * 6 bits	PLEN	length in bytes of extra picture header
 * 3 bits	PEBIT	how many bits to be ignored in the last byte
 *
 * XXX the code below is not complete.
 */
static int h263p_decap(struct fbuf_t *b, uint8_t *data, int len)
{
	int PLEN;

	if (len < 2) {
		ast_log(LOG_WARNING, "invalid framesize %d\n", len);
		return 1;
	}
	PLEN = ( (data[0] & 1) << 5 ) | ( (data[1] & 0xf8) >> 3);

	if (PLEN > 0) {
		data += PLEN;
		len -= PLEN;
	}
	if (data[0] & 4)	/* bit P */
		data[0] = data[1] = 0;
	else {
		data += 2;
		len -= 2;
	}
	return fbuf_append(b, data, len, 0, 0);	/* ignore trail bits */
}


/*
 * generic encoder, used by the various protocols supported here.
 * We assume that the buffer is empty at the beginning.
 */
static int ffmpeg_encode(struct video_out_desc *v)
{
	struct fbuf_t *b = &v->enc_out;
	int i;

	b->used = avcodec_encode_video(v->enc_ctx, b->data, b->size, v->frame);
	i = avcodec_encode_video(v->enc_ctx, b->data + b->used, b->size - b->used, NULL); /* delayed frames ? */
	if (i > 0) {
		ast_log(LOG_WARNING, "have %d more bytes\n", i);
		b->used += i;
	}
	return 0;
}

/*
 * Generic decoder, which is used by h263p, h263 and h261 as it simply
 * invokes ffmpeg's decoder.
 * av_parser_parse should merge a randomly chopped up stream into
 * proper frames. After that, if we have a valid frame, we decode it
 * until the entire frame is processed.
 */
static int ffmpeg_decode(struct video_in_desc *v, struct fbuf_t *b)
{
	uint8_t *src = b->data;
	int srclen = b->used;
	int full_frame = 0;

	if (srclen == 0)	/* no data */
		return 0;
	if (0)
		check_h261(b);
	// ast_log(LOG_WARNING, "rx size %d\n", srclen);
	while (srclen) {
		uint8_t *data;
		int datalen, ret;
		int len = av_parser_parse(v->parser, v->dec_ctx, &data, &datalen, src, srclen, 0, 0);

		src += len;
		srclen -= len;
		/* The parser might return something it cannot decode, so it skips
		 * the block returning no data
		 */
		if (data == NULL || datalen == 0)
			continue;
		ret = avcodec_decode_video(v->dec_ctx, v->d_frame, &full_frame, data, datalen);
		if (full_frame == 1)	/* full frame */
			break;
		if (ret < 0) {
			ast_log(LOG_NOTICE, "Error decoding\n");
			break;
		}
	}
	if (srclen != 0)	/* update b with leftover data */
		bcopy(src, b->data, srclen);
	b->used = srclen;
	b->ebit = 0;
	return full_frame;
}

static struct video_codec_desc h263p_codec = {
	.name = "h263p",
	.format = AST_FORMAT_H263_PLUS,
	.enc_init = h263p_enc_init,
	.enc_encap = h263p_encap,
	.enc_run = ffmpeg_encode,
	.dec_init = NULL,
	.dec_decap = h263p_decap,
	.dec_run = ffmpeg_decode
};

/*--- Plain h263 support --------*/

static int h263_enc_init(struct video_out_desc *v)
{
	/* XXX check whether these are supported */
	v->enc_ctx->flags |= CODEC_FLAG_H263P_UMV;
	v->enc_ctx->flags |= CODEC_FLAG_H263P_AIC;
	v->enc_ctx->flags |= CODEC_FLAG_H263P_SLICE_STRUCT;
	v->enc_ctx->flags |= CODEC_FLAG_AC_PRED;

	v->enc_ctx->gop_size = v->fps*5;

	return 0;
}

/*
 * h263 encapsulation is specified in RFC2190. There are three modes
 * defined (A, B, C), with 4, 8 and 12 bytes of header, respectively.
 * The header is made as follows
 *     0.....................|.......................|.............|....31
 *	F:1 P:1 SBIT:3 EBIT:3 SRC:3 I:1 U:1 S:1 A:1 R:4 DBQ:2 TRB:3 TR:8
 * FP = 0- mode A, (only one word of header)
 * FP = 10 mode B, and also means this is an I or P frame
 * FP = 11 mode C, and also means this is a PB frame.
 * SBIT, EBIT nuber of bits to ignore at beginning (msbits) and end (lsbits)
 * SRC  bits 6,7,8 from the h263 PTYPE field
 * I = 0 intra-coded, 1 = inter-coded (bit 9 from PTYPE)
 * U = 1 for Unrestricted Motion Vector (bit 10 from PTYPE)
 * S = 1 for Syntax Based Arith coding (bit 11 from PTYPE)
 * A = 1 for Advanced Prediction (bit 12 from PTYPE)
 * R = reserved, must be 0
 * DBQ = differential quantization, DBQUANT from h263, 0 unless we are using
 *	PB frames
 * TRB = temporal reference for bframes, also 0 unless this is a PB frame
 * TR = temporal reference for P frames, also 0 unless PB frame.
 *
 * Mode B and mode C description omitted.
 *
 * An RTP frame can start with a PSC 0000 0000 0000 0000 1000 0
 * or with a GBSC, which also has the first 17 bits as a PSC.
 * Note - PSC are byte-aligned, GOB not necessarily. PSC start with
 *	PSC:22 0000 0000 0000 0000 1000 00 	picture start code
 *	TR:8   .... ....			temporal reference
 *      PTYPE:13 or more 			ptype...
 * If we don't fragment a GOB SBIT and EBIT = 0.
 * reference, 8 bit) 
 * 
 * The assumption below is that we start with a PSC.
 */
static struct ast_frame *h263_encap(struct video_out_desc *out,
		struct ast_frame **tail)
{
	uint8_t *d = out->enc_out.data;
	int start = 0, i, len = out->enc_out.used;
	struct ast_frame *f, *cur = NULL, *first = NULL;
	const int pheader_len = 4;	/* Use RFC-2190 Mode A */
	uint8_t h263_hdr[12];	/* worst case, room for a type c header */
	uint8_t *h = h263_hdr;	/* shorthand */

#define H263_MIN_LEN	6
	if (len < H263_MIN_LEN)	/* unreasonably small */
		return NULL;

	bzero(h263_hdr, sizeof(h263_hdr));
	/* Now set the header bytes. Only type A by now,
	 * and h[0] = h[2] = h[3] = 0 by default.
	 * PTYPE starts 30 bits in the picture, so the first useful
	 * bit for us is bit 36 i.e. within d[4] (0 is the msbit).
	 * SRC = d[4] & 0x1c goes into data[1] & 0xe0
	 * I   = d[4] & 0x02 goes into data[1] & 0x10
	 * U   = d[4] & 0x01 goes into data[1] & 0x08
	 * S   = d[5] & 0x80 goes into data[1] & 0x04
	 * A   = d[5] & 0x40 goes into data[1] & 0x02
	 * R   = 0           goes into data[1] & 0x01
	 * Optimizing it, we have
	 */
	h[1] = ( (d[4] & 0x1f) << 3 ) |	/* SRC, I, U */
		( (d[5] & 0xc0) >> 5 );		/* S, A, R */

	/* now look for the next PSC or GOB header. First try to hit
	 * a '0' byte then look around for the 0000 0000 0000 0000 1 pattern
	 * which is both in the PSC and the GBSC.
	 */
	for (i = H263_MIN_LEN, start = 0; start < len; start = i, i += 3) {
		//ast_log(LOG_WARNING, "search at %d of %d/%d\n", i, start, len);
		for (; i < len ; i++) {
			uint8_t x, rpos, lpos;
			int rpos_i;	/* index corresponding to rpos */
			if (d[i] != 0)		/* cannot be in a GBSC */
				continue;
			if (i > len - 1)
				break;
			x = d[i+1];
			if (x == 0)	/* next is equally good */
				continue;
			/* see if around us we can make 16 '0' bits for the GBSC.
			 * Look for the first bit set on the right, and then
			 * see if we have enough 0 on the left.
			 * We are guaranteed to end before rpos == 0
			 */
			for (rpos = 0x80, rpos_i = 8; rpos; rpos >>= 1, rpos_i--)
				if (x & rpos)	/* found the '1' bit in GBSC */
					break;
			x = d[i-1];		/* now look behind */
			for (lpos = rpos; lpos ; lpos >>= 1)
				if (x & lpos)	/* too early, not a GBSC */
					break;
			if (lpos)		/* as i said... */
				continue;
			/* now we have a GBSC starting somewhere in d[i-1],
			 * but it might be not byte-aligned
			 */
			if (rpos == 0x80) {	/* lucky case */
				i = i - 1;
			} else {	/* XXX to be completed */
				ast_log(LOG_WARNING, "unaligned GBSC 0x%x %d\n",
					rpos, rpos_i);
			}
			break;
		}
		/* This frame is up to offset i (not inclusive).
		 * We do not split it yet even if larger than MTU.
		 */
		f = create_video_frame(d + start, d+i, AST_FORMAT_H263,
				pheader_len, cur);

		if (!f)
			break;
		bcopy(h, f->data, 4);	/* copy the h263 header */
		/* XXX to do: if not aligned, fix sbit and ebit,
		 * then move i back by 1 for the next frame
		 */
		if (!cur)
			first = f;
		cur = f;
	}

	if (cur)
		cur->subclass |= 1;	// RTP Marker

	*tail = cur;
	return first;
}

/* XXX We only drop the header here, but maybe we need more. */
static int h263_decap(struct fbuf_t *b, uint8_t *data, int len)
{
	if (len < 4) {
		ast_log(LOG_WARNING, "invalid framesize %d\n", len);
		return 1;	/* error */
	}

	if ( (data[0] & 0x80) == 0) {
		len -= 4;
		data += 4;
	} else {
		ast_log(LOG_WARNING, "unsupported mode 0x%x\n",
			data[0]);
		return 1;
	}
	return fbuf_append(b, data, len, 0, 0);	/* XXX no bit alignment support yet */
}

static struct video_codec_desc h263_codec = {
	.name = "h263",
	.format = AST_FORMAT_H263,
	.enc_init = h263_enc_init,
	.enc_encap = h263_encap,
	.enc_run = ffmpeg_encode,
	.dec_init = NULL,
	.dec_decap = h263_decap,
	.dec_run = ffmpeg_decode
						
};

/*---- h261 support -----*/
static int h261_enc_init(struct video_out_desc *v)
{
	/* It is important to set rtp_payload_size = 0, otherwise
	 * ffmpeg in h261 mode will produce output that it cannot parse.
	 * Also try to send I frames more frequently than with other codecs.
	 */
	v->enc_ctx->rtp_payload_size = 0; /* important - ffmpeg fails otherwise */
	v->enc_ctx->gop_size = v->fps*2;	/* be more responsive */

	return 0;
}

/*
 * The encapsulation of H261 is defined in RFC4587 which obsoletes RFC2032
 * The bitstream is preceded by a 32-bit header word:
 *  SBIT:3 EBIT:3 I:1 V:1 GOBN:4 MBAP:5 QUANT:5 HMVD:5 VMVD:5
 * SBIT and EBIT are the bits to be ignored at beginning and end,
 * I=1 if the stream has only INTRA frames - cannot change during the stream.
 * V=0 if motion vector is not used. Cannot change.
 * GOBN is the GOB number in effect at the start of packet, 0 if we
 *	start with a GOB header
 * QUANT is the quantizer in effect, 0 if we start with GOB header
 * HMVD  reference horizontal motion vector. 10000 is forbidden
 * VMVD  reference vertical motion vector, as above.
 * Packetization should occur at GOB boundaries, and if not possible
 * with MacroBlock fragmentation. However it is likely that blocks
 * are not bit-aligned so we must take care of this.
 */
static struct ast_frame *h261_encap(struct video_out_desc *out,
		struct ast_frame **tail)
{
	uint8_t *d = out->enc_out.data;
	int start = 0, i, len = out->enc_out.used;
	struct ast_frame *f, *cur = NULL, *first = NULL;
	const int pheader_len = 4;
	uint8_t h261_hdr[4];
	uint8_t *h = h261_hdr;	/* shorthand */
	int sbit = 0, ebit = 0;

#define H261_MIN_LEN 10
	if (len < H261_MIN_LEN)	/* unreasonably small */
		return NULL;

	bzero(h261_hdr, sizeof(h261_hdr));

	/* Similar to the code in h263_encap, but the marker there is longer.
	 * Start a few bytes within the bitstream to avoid hitting the marker
	 * twice. Note we might access the buffer at len, but this is ok because
	 * the caller has it oversized.
	 */
	for (i = H261_MIN_LEN, start = 0; start < len - 1; start = i, i += 4) {
#if 0	/* test - disable packetization */
		i = len;	/* wrong... */
#else
		int found = 0, found_ebit = 0;	/* last GBSC position found */
		for (; i < len ; i++) {
			uint8_t x, rpos, lpos;
			if (d[i] != 0)		/* cannot be in a GBSC */
				continue;
			x = d[i+1];
			if (x == 0)	/* next is equally good */
				continue;
			/* See if around us we find 15 '0' bits for the GBSC.
			 * Look for the first bit set on the right, and then
			 * see if we have enough 0 on the left.
			 * We are guaranteed to end before rpos == 0
			 */
			for (rpos = 0x80, ebit = 7; rpos; ebit--, rpos >>= 1)
				if (x & rpos)	/* found the '1' bit in GBSC */
					break;
			x = d[i-1];		/* now look behind */
			for (lpos = (rpos >> 1); lpos ; lpos >>= 1)
				if (x & lpos)	/* too early, not a GBSC */
					break;
			if (lpos)		/* as i said... */
				continue;
			/* now we have a GBSC starting somewhere in d[i-1],
			 * but it might be not byte-aligned. Just remember it.
			 */
			if (i - start > out->mtu) /* too large, stop now */
				break;
			found_ebit = ebit;
			found = i;
			i += 4;	/* continue forward */
		}
		if (i >= len) {	/* trim if we went too forward */
			i = len;
			ebit = 0;	/* hopefully... should ask the bitstream ? */
		}
		if (i - start > out->mtu && found) {
			/* use the previous GBSC, hope is within the mtu */
			i = found;
			ebit = found_ebit;
		}
#endif /* test */
		if (i - start < 4)	/* XXX too short ? */
			continue;
		/* This frame is up to offset i (not inclusive).
		 * We do not split it yet even if larger than MTU.
		 */
		f = create_video_frame(d + start, d+i, AST_FORMAT_H261,
				pheader_len, cur);

		if (!f)
			break;
		/* recompute header with I=0, V=1 */
		h[0] = ( (sbit & 7) << 5 ) | ( (ebit & 7) << 2 ) | 1;
		bcopy(h, f->data, 4);	/* copy the h261 header */
		if (ebit)	/* not aligned, restart from previous byte */
			i--;
		sbit = (8 - ebit) & 7;
		ebit = 0;
		if (!cur)
			first = f;
		cur = f;
	}
	if (cur)
		cur->subclass |= 1;	// RTP Marker

	*tail = cur;
	return first;
}

/*
 * Pieces might be unaligned so we really need to put them together.
 */
static int h261_decap(struct fbuf_t *b, uint8_t *data, int len)
{
	int ebit, sbit;

	if (len < 8) {
		ast_log(LOG_WARNING, "invalid framesize %d\n", len);
		return 1;
	}
	sbit = (data[0] >> 5) & 7;
	ebit = (data[0] >> 2) & 7;
	len -= 4;
	data += 4;
	return fbuf_append(b, data, len, sbit, ebit);
}

static struct video_codec_desc h261_codec = {
	.name = "h261",
	.format = AST_FORMAT_H261,
	.enc_init = h261_enc_init,
	.enc_encap = h261_encap,
	.enc_run = ffmpeg_encode,
	.dec_init = NULL,
	.dec_decap = h261_decap,
	.dec_run = ffmpeg_decode
};

/* mpeg4 support */
static int mpeg4_enc_init(struct video_out_desc *v)
{
#if 0
	//v->enc_ctx->flags |= CODEC_FLAG_LOW_DELAY; /*don't use b frames ?*/
	v->enc_ctx->flags |= CODEC_FLAG_AC_PRED;
	v->enc_ctx->flags |= CODEC_FLAG_H263P_UMV;
	v->enc_ctx->flags |= CODEC_FLAG_QPEL;
	v->enc_ctx->flags |= CODEC_FLAG_4MV;
	v->enc_ctx->flags |= CODEC_FLAG_GMC;
	v->enc_ctx->flags |= CODEC_FLAG_LOOP_FILTER;
	v->enc_ctx->flags |= CODEC_FLAG_H263P_SLICE_STRUCT;
#endif
	v->enc_ctx->gop_size = v->fps*5;
	v->enc_ctx->rtp_payload_size = 0; /* important - ffmpeg fails otherwise */
	return 0;
}

/* simplistic encapsulation - just split frames in mtu-size units */
static struct ast_frame *mpeg4_encap(struct  video_out_desc *out,
	struct ast_frame **tail)
{
	struct ast_frame *f, *cur = NULL, *first = NULL;
	uint8_t *d = out->enc_out.data;
	uint8_t *end = d+out->enc_out.used;
	int len;

	for (;d < end; d += len, cur = f) {
		len = MIN(out->mtu, end-d);
		f = create_video_frame(d, d+len, AST_FORMAT_MP4_VIDEO, 0, cur);
		if (!f)
			break;
		if (!first)
			first = f;
	}
	if (cur)
		cur->subclass |= 1;
	*tail = cur;
	return first;
}

static int mpeg4_decap(struct fbuf_t *b, uint8_t *data, int len)
{
	return fbuf_append(b, data, len, 0, 0);
}

static int mpeg4_decode(struct video_in_desc *v, struct fbuf_t *b)
{
	int full_frame = 0, datalen = b->used;
	int ret = avcodec_decode_video(v->dec_ctx, v->d_frame, &full_frame,
		b->data, datalen);
	if (ret < 0) {
		ast_log(LOG_NOTICE, "Error decoding\n");
		ret = datalen; /* assume we used everything. */
	}
	datalen -= ret;
	if (datalen > 0)	/* update b with leftover bytes */
		bcopy(b->data + ret, b->data, datalen);
	b->used = datalen;
	b->ebit = 0;
	return full_frame;
}

static struct video_codec_desc mpeg4_codec = {
	.name = "mpeg4",
	.format = AST_FORMAT_MP4_VIDEO,
	.enc_init = mpeg4_enc_init,
	.enc_encap = mpeg4_encap,
	.enc_run = ffmpeg_encode,
	.dec_init = NULL,
	.dec_decap = mpeg4_decap,
	.dec_run = mpeg4_decode
};

static int h264_enc_init(struct video_out_desc *v)
{
	v->enc_ctx->flags |= CODEC_FLAG_TRUNCATED;
	//v->enc_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
	//v->enc_ctx->flags2 |= CODEC_FLAG2_FASTPSKIP;
	/* TODO: Maybe we need to add some other flags */
	v->enc_ctx->gop_size = v->fps*5; // emit I frame every 5 seconds
	v->enc_ctx->rtp_mode = 0;
	v->enc_ctx->rtp_payload_size = 0;
	v->enc_ctx->bit_rate_tolerance = v->enc_ctx->bit_rate;
	return 0;
}

static int h264_dec_init(struct video_in_desc *v)
{
	v->dec_ctx->flags |= CODEC_FLAG_TRUNCATED;

	return 0;
}

/*
 * The structure of a generic H.264 stream is:
 * - 0..n 0-byte(s), unused, optional. one zero-byte is always present
 *   in the first NAL before the start code prefix.
 * - start code prefix (3 bytes): 0x000001
 *   (the first bytestream has a 
 *   like these 0x00000001!)
 * - NAL header byte ( F[1] | NRI[2] | Type[5] ) where type != 0
 * - byte-stream
 * - 0..n 0-byte(s) (padding, unused).
 * Segmentation in RTP only needs to be done on start code prefixes.
 * If fragments are too long... we don't support it yet.
 * - encapsulate (or fragment) the byte-stream (with NAL header included)
 */
static struct ast_frame *h264_encap(struct video_out_desc *out,
	struct ast_frame **tail)
{
	struct ast_frame *f = NULL, *cur = NULL, *first = NULL;
	uint8_t *d, *start = out->enc_out.data;
	uint8_t *end = start + out->enc_out.used;

	/* Search the first start code prefix - ITU-T H.264 sec. B.2,
	 * and move start right after that, on the NAL header byte.
	 */
#define HAVE_NAL(x) (x[-4] == 0 && x[-3] == 0 && x[-2] == 0 && x[-1] == 1)
	for (start += 4; start < end; start++) {
		int ty = start[0] & 0x1f;
		if (HAVE_NAL(start) && ty != 0 && ty != 31)
			break;
	}
	/* if not found, or too short, we just skip the next loop and are done. */

	/* Here follows the main loop to create frames. Search subsequent start
	 * codes, and then possibly fragment the unit into smaller fragments.
	 */
   for (;start < end - 4; start = d) {
	int size;		/* size of current block */
	uint8_t hdr[2];		/* add-on header when fragmenting */
	int ty = 0;

	/* now search next nal */
	for (d = start + 4; d < end; d++) {
		ty = d[0] & 0x1f;
		if (HAVE_NAL(d))
			break;	/* found NAL */
	}
	/* have a block to send. d past the start code unless we overflow */
	if (d >= end) {	/* NAL not found */
		d = end + 4;
	} else if (ty == 0 || ty == 31) { /* found but invalid type, skip */
		ast_log(LOG_WARNING, "skip invalid nal type %d at %d of %d\n",
			ty, d - out->enc_out.data, out->enc_out.used);
		continue;
	}

	size = d - start - 4;	/* don't count the end */

	if (size < out->mtu) {	// test - don't fragment
		// Single NAL Unit
		f = create_video_frame(start, d - 4, AST_FORMAT_H264, 0, cur);
		if (!f)
			break;
		if (!first)
			first = f;

		cur = f;
		continue;
	}

	// Fragmented Unit (Mode A: no DON, very weak)
	hdr[0] = (*start & 0xe0) | 28;	/* mark as a fragmentation unit */
	hdr[1] = (*start++ & 0x1f) | 0x80 ;	/* keep type and set START bit */
	size--;		/* skip the NAL header */
	while (size) {
		uint8_t *data;
		int frag_size = MIN(size, out->mtu);

		f = create_video_frame(start, start+frag_size, AST_FORMAT_H264, 2, cur);
		if (!f)
			break;
		size -= frag_size;	/* skip this data block */
		start += frag_size;

		data = f->data;
		data[0] = hdr[0];
		data[1] = hdr[1] | (size == 0 ? 0x40 : 0);	/* end bit if we are done */
		hdr[1] &= ~0x80;	/* clear start bit for subsequent frames */
		if (!first)
			first = f;
		cur = f;
	}
    }

	if (cur)
		cur->subclass |= 1;     // RTP Marker

	*tail = cur;

	return first;
}

static int h264_decap(struct fbuf_t *b, uint8_t *data, int len)
{
	/* Start Code Prefix (Annex B in specification) */
	uint8_t scp[] = { 0x00, 0x00, 0x00, 0x01 };
	int retval = 0;
	int type, ofs = 0;

	if (len < 2) {
		ast_log(LOG_WARNING, "--- invalid len %d\n", len);
		return 1;
	}
	/* first of all, check if the packet has F == 0 */
	if (data[0] & 0x80) {
		ast_log(LOG_WARNING, "--- forbidden packet; nal: %02x\n",
			data[0]);
		return 1;
	}

	type = data[0] & 0x1f;
	switch (type) {
	case 0:
	case 31:
		ast_log(LOG_WARNING, "--- invalid type: %d\n", type);
		return 1;
	case 24:
	case 25:
	case 26:
	case 27:
	case 29:
		ast_log(LOG_WARNING, "--- encapsulation not supported : %d\n", type);
		return 1;
	case 28:	/* FU-A Unit */
		if (data[1] & 0x80) { // S == 1, import F and NRI from next
			data[1] &= 0x1f;	/* preserve type */
			data[1] |= (data[0] & 0xe0);	/* import F & NRI */
			retval = fbuf_append(b, scp, sizeof(scp), 0, 0);
			ofs = 1;
		} else {
			ofs = 2;
		}
		break;
	default:	/* From 1 to 23 (Single NAL Unit) */
		retval = fbuf_append(b, scp, sizeof(scp), 0, 0);
	}
	if (!retval)
		retval = fbuf_append(b, data + ofs, len - ofs, 0, 0);
	if (retval)
		ast_log(LOG_WARNING, "result %d\n", retval);
	return retval;
}

static struct video_codec_desc h264_codec = {
	.name = "h264",
	.format = AST_FORMAT_H264,
	.enc_init = h264_enc_init,
	.enc_encap = h264_encap,
	.enc_run = ffmpeg_encode,
	.dec_init = h264_dec_init,
	.dec_decap = h264_decap,
	.dec_run = ffmpeg_decode
};

/*------ end codec specific code -----*/


/* Video4Linux stuff is only used in video_open() */
#ifdef HAVE_VIDEODEV_H
#include <linux/videodev.h>
#endif

/*!
 * Open the local video source and allocate a buffer
 * for storing the image. Return 0 on success, -1 on error
 */
static int video_open(struct video_out_desc *v)
{
	struct fbuf_t *b = &v->loc_src;
	if (b->data)	/* buffer allocated means device already open */
		return v->fd;
	v->fd = -1;
	/*
	 * if the device is "X11", then open the x11 grabber
	 */
    if (!strcasecmp(v->videodevice, "X11")) {
	XImage *im;
	int screen_num;

	/* init the connection with the X server */
	v->dpy = XOpenDisplay(NULL);
	if (v->dpy == NULL) {
		ast_log(LOG_WARNING, "error opening display\n");
		goto error;
	}

	/* find width and height of the screen */
	screen_num = DefaultScreen(v->dpy);
	v->screen_width = DisplayWidth(v->dpy, screen_num);
	v->screen_height = DisplayHeight(v->dpy, screen_num);

	v->image = im = XGetImage(v->dpy,
		RootWindow(v->dpy, DefaultScreen(v->dpy)),
		b->x, b->y, b->w, b->h, AllPlanes, ZPixmap);
	if (v->image == NULL) {
		ast_log(LOG_WARNING, "error creating Ximage\n");
		goto error;
	}
	switch (im->bits_per_pixel) {
	case 32:
		b->pix_fmt = PIX_FMT_RGBA32;
		break;
	case 16:
		b->pix_fmt = (im->green_mask == 0x7e0) ? PIX_FMT_RGB565 : PIX_FMT_RGB555;
		break;
	}

	ast_log(LOG_NOTICE, "image: data %p %d bpp fmt %d, mask 0x%lx 0x%lx 0x%lx\n",
		im->data,
		im->bits_per_pixel,
		b->pix_fmt,
		im->red_mask, im->green_mask, im->blue_mask);

	/* set the pointer but not the size as this is not malloc'ed */
	b->data = (uint8_t *)im->data;
	v->fd = -2;
    }
#ifdef HAVE_VIDEODEV_H
    else {
	/* V4L specific */
	struct video_window vw = { 0 };	/* camera attributes */
	struct video_picture vp;
	int i;
	const char *dev = v->videodevice;

	v->fd = open(dev, O_RDONLY | O_NONBLOCK);
	if (v->fd < 0) {
		ast_log(LOG_WARNING, "error opening camera %s\n", v->videodevice);
		return v->fd;
	}

	i = fcntl(v->fd, F_GETFL);
	if (-1 == fcntl(v->fd, F_SETFL, i | O_NONBLOCK)) {
		/* non fatal, just emit a warning */
		ast_log(LOG_WARNING, "error F_SETFL for %s [%s]\n",
			dev, strerror(errno));
	}
	/* set format for the camera.
	 * In principle we could retry with a different format if the
	 * one we are asking for is not supported.
	 */
	vw.width = v->loc_src.w;
	vw.height = v->loc_src.h;
	vw.flags = v->fps << 16;
	if (ioctl(v->fd, VIDIOCSWIN, &vw) == -1) {
		ast_log(LOG_WARNING, "error setting format for %s [%s]\n",
			dev, strerror(errno));
		goto error;
	}
	if (ioctl(v->fd, VIDIOCGPICT, &vp) == -1) {
		ast_log(LOG_WARNING, "error reading picture info\n");
		goto error;
	}
	ast_log(LOG_WARNING,
		"contrast %d bright %d colour %d hue %d white %d palette %d\n",
		vp.contrast, vp.brightness,
		vp.colour, vp.hue,
		vp.whiteness, vp.palette);
	/* set the video format. Here again, we don't necessary have to
	 * fail if the required format is not supported, but try to use
	 * what the camera gives us.
	 */
	b->pix_fmt = vp.palette;
	vp.palette = VIDEO_PALETTE_YUV420P;
	if (ioctl(v->fd, VIDIOCSPICT, &vp) == -1) {
		ast_log(LOG_WARNING, "error setting palette, using %d\n",
			b->pix_fmt);
	} else
		b->pix_fmt = vp.palette;
	/* allocate the source buffer.
	 * XXX, the code here only handles yuv411, for other formats
	 * we need to look at pix_fmt and set size accordingly
	 */
	b->size = (b->w * b->h * 3)/2;	/* yuv411 */
	ast_log(LOG_WARNING, "videodev %s opened, size %dx%d %d\n",
		dev, b->w, b->h, b->size);
	v->loc_src.data = ast_calloc(1, b->size);
	if (!b->data) {
		ast_log(LOG_WARNING, "error allocating buffer %d bytes\n",
			b->size);
		goto error;
	}
	ast_log(LOG_WARNING, "success opening camera\n");
    }
#endif /* HAVE_VIDEODEV_H */

	if (v->image == NULL && v->fd < 0)
		goto error;
	b->used = 0;
	return 0;

error:
	ast_log(LOG_WARNING, "fd %d dpy %p img %p data %p\n",
		v->fd, v->dpy, v->image, v->loc_src.data);
	/* XXX maybe XDestroy (v->image) ? */
	if (v->dpy)
		XCloseDisplay(v->dpy);
	v->dpy = NULL;
	if (v->fd >= 0)
		close(v->fd);
	v->fd = -1;
	fbuf_free(&v->loc_src);
	return -1;
}

/*! \brief complete a buffer from the local video source.
 * Called by get_video_frames(), in turn called by the video thread.
 */
static int video_read(struct video_out_desc *v)
{
	struct timeval now = ast_tvnow();
	struct fbuf_t *b = &v->loc_src;

	if (b->data == NULL)	/* not initialized */
		return 0;

	/* check if it is time to read */
	if (ast_tvzero(v->last_frame))
		v->last_frame = now;
	if (ast_tvdiff_ms(now, v->last_frame) < 1000/v->fps)
		return 0;	/* too early */
	v->last_frame = now; /* XXX actually, should correct for drift */

#ifdef HAVE_X11
	if (v->image) {
		/* read frame from X11 */
		AVPicture p;
		XGetSubImage(v->dpy,
		    RootWindow(v->dpy, DefaultScreen(v->dpy)),
			b->x, b->y, b->w, b->h, AllPlanes, ZPixmap, v->image, 0, 0);

		b->data = (uint8_t *)v->image->data;
		fill_pict(b, &p);
		return p.linesize[0] * b->h;
	}
#endif
	if (v->fd < 0)			/* no other source */
		return 0;
	for (;;) {
		int r, l = v->loc_src.size - v->loc_src.used;
		r = read(v->fd, v->loc_src.data + v->loc_src.used, l);
		// ast_log(LOG_WARNING, "read %d of %d bytes from webcam\n", r, l);
		if (r < 0)	/* read error */
			return 0;
		if (r == 0)	/* no data */
			return 0;
		v->loc_src.used += r;
		if (r == l) {
			v->loc_src.used = 0; /* prepare for next frame */
			return v->loc_src.size;
		}
	}
}

/* Helper function to process incoming video.
 * For each incoming video call invoke ffmpeg_init() to intialize
 * the decoding structure then incoming video frames are processed
 * by write_video() which in turn calls pre_process_data(), to extract
 * the bitstream; accumulates data into a buffer within video_desc. When
 * a frame is complete (determined by the marker bit in the RTP header)
 * call decode_video() to decoding and if it successful call show_frame()
 * to display the frame.
 */

/*
 * Table of translation between asterisk and ffmpeg formats.
 * We need also a field for read and write (encoding and decoding), because
 * e.g. H263+ uses different codec IDs in ffmpeg when encoding or decoding.
 */
struct _cm {	/* map ffmpeg codec types to asterisk formats */
	uint32_t	ast_format;	/* 0 is a terminator */
	enum CodecID	codec;
	enum { CM_RD = 1, CM_WR = 2, CM_RDWR = 3 } rw;	/* read or write or both ? */
	struct video_codec_desc *codec_desc;
};

static struct _cm video_formats[] = {
	{ AST_FORMAT_H263_PLUS,	CODEC_ID_H263,  CM_RD }, /* incoming H263P ? */
	{ AST_FORMAT_H263_PLUS,	CODEC_ID_H263P, CM_WR },
	{ AST_FORMAT_H263,	CODEC_ID_H263,  CM_RD },
	{ AST_FORMAT_H263,	CODEC_ID_H263,  CM_WR },
	{ AST_FORMAT_H261,	CODEC_ID_H261,  CM_RDWR },
	{ AST_FORMAT_H264,	CODEC_ID_H264,  CM_RDWR },
	{ AST_FORMAT_MP4_VIDEO,	CODEC_ID_MPEG4, CM_RDWR },
	{ 0,			0, 0 },
};


/*! \brief map an asterisk format into an ffmpeg one */
static enum CodecID map_video_format(uint32_t ast_format, int rw)
{
	struct _cm *i;

	for (i = video_formats; i->ast_format != 0; i++)
		if (ast_format & i->ast_format && rw & i->rw && rw & i->rw)
			return i->codec;
	return CODEC_ID_NONE;
}

/* pointers to supported codecs. We assume the first one to be non null. */
static struct video_codec_desc *supported_codecs[] = {
	&h263p_codec,
	&h264_codec,
	&h263_codec,
	&h261_codec,
	&mpeg4_codec,
	NULL
};

/*
 * Map the AST_FORMAT to the library. If not recognised, fail.
 * This is useful in the input path where we get frames.
 */
static struct video_codec_desc *map_video_codec(int fmt)
{
	int i;

	for (i = 0; supported_codecs[i]; i++)
		if (fmt == supported_codecs[i]->format) {
			ast_log(LOG_WARNING, "using %s for format 0x%x\n",
				supported_codecs[i]->name, fmt);
			return supported_codecs[i];
		}
	return NULL;
}
;
/*
 * Map the codec name to the library. If not recognised, use a default.
 * This is useful in the output path where we decide by name, presumably.
 */
static struct video_codec_desc *map_config_video_format(char *name)
{
	int i;

	for (i = 0; supported_codecs[i]; i++)
		if (!strcasecmp(name, supported_codecs[i]->name))
			break;
	if (supported_codecs[i] == NULL) {
		ast_log(LOG_WARNING, "Cannot find codec for '%s'\n", name);
		i = 0;
		strcpy(name, supported_codecs[i]->name);
	}
	ast_log(LOG_WARNING, "Using codec '%s'\n", name);
	return supported_codecs[i];
}

/*! \brief uninitialize the descriptor for remote video stream */
static int video_in_uninit(struct video_in_desc *v)
{
	int i;

	if (v->parser) {
		av_parser_close(v->parser);
		v->parser = NULL;
	}
	if (v->dec_ctx) {
		avcodec_close(v->dec_ctx);
		av_free(v->dec_ctx);
		v->dec_ctx = NULL;
	}
	if (v->d_frame) {
		av_free(v->d_frame);
		v->d_frame = NULL;
	}
	v->codec = NULL;	/* only a reference */
	v->dec = NULL;		/* forget the decoder */
	v->discard = 1;		/* start in discard mode */
	for (i = 0; i < N_DEC_IN; i++)
		fbuf_free(&v->dec_in[i]);
	fbuf_free(&v->dec_out);
	fbuf_free(&v->rem_dpy);
	return -1;	/* error, in case someone cares */
}

/*
 * initialize ffmpeg resources used for decoding frames from the network.
 */
static int video_in_init(struct video_in_desc *v, uint32_t format)
{
	enum CodecID codec;

	/* XXX should check that these are already set */
	v->codec = NULL;
	v->dec_ctx = NULL;
	v->d_frame = NULL;
	v->parser = NULL;
	v->discard = 1;

	codec = map_video_format(format, CM_RD);

	v->codec = avcodec_find_decoder(codec);
	if (!v->codec) {
		ast_log(LOG_WARNING, "Unable to find the decoder for format %d\n", codec);
		return video_in_uninit(v);
	}
	/*
	* Initialize the codec context.
	*/
	v->dec_ctx = avcodec_alloc_context();
	if (avcodec_open(v->dec_ctx, v->codec) < 0) {
		ast_log(LOG_WARNING, "Cannot open the codec context\n");
		av_free(v->dec_ctx);
		v->dec_ctx = NULL;
		return video_in_uninit(v);
	}

	v->parser = av_parser_init(codec);
	if (!v->parser) {
		ast_log(LOG_WARNING, "Cannot initialize the decoder parser\n");
		return video_in_uninit(v);
	}

	v->d_frame = avcodec_alloc_frame();
	if (!v->d_frame) {
		ast_log(LOG_WARNING, "Cannot allocate decoding video frame\n");
		return video_in_uninit(v);
	}
	return 0;	/* ok */
}

/*! \brief uninitialize the descriptor for local video stream */
static int video_out_uninit(struct video_out_desc *v)
{
	if (v->enc_ctx) {
		avcodec_close(v->enc_ctx);
		av_free(v->enc_ctx);
		v->enc_ctx = NULL;
	}
	if (v->frame) {
		av_free(v->frame);
		v->frame = NULL;
	}
	v->codec = NULL;	/* only a reference */
	
	fbuf_free(&v->loc_src);
	fbuf_free(&v->enc_in);
	fbuf_free(&v->enc_out);
	fbuf_free(&v->loc_dpy);
	if (v->image) {	/* X11 grabber */
		XCloseDisplay(v->dpy);
		v->dpy = NULL;
		v->image = NULL;
	}
	if (v->fd >= 0) {
		close(v->fd);
		v->fd = -1;
	}
	return -1;
}

/*
 * Initialize the encoder for the local source:
 * - AVCodecContext, AVCodec, AVFrame are used by ffmpeg for encoding;
 * - encbuf is used to store the encoded frame (to be sent)
 * - mtu is used to determine the max size of video fragment
 * NOTE: we enter here with the video source already open.
 */
static int video_out_init(struct video_desc *env)
{
	int codec;
	int size;
	struct fbuf_t *enc_in;
	struct video_out_desc *v = &env->out;

	v->enc_ctx		= NULL;
	v->codec		= NULL;
	v->frame		= NULL;
	v->enc_out.data		= NULL;

	if (v->loc_src.data == NULL) {
		ast_log(LOG_WARNING, "No local source active\n");
		return video_out_uninit(v);
	}
	codec = map_video_format(v->enc->format, CM_WR);
	v->codec = avcodec_find_encoder(codec);
	if (!v->codec) {
		ast_log(LOG_WARNING, "Cannot find the encoder for format %d\n",
			codec);
		return video_out_uninit(v);
	}

	v->mtu = 1400;	/* set it early so the encoder can use it */

	/* allocate the input buffer for encoding.
	 * ffmpeg only supports PIX_FMT_YUV420P for the encoding.
	 */
	enc_in = &v->enc_in;
	enc_in->pix_fmt = PIX_FMT_YUV420P;
	enc_in->size = (enc_in->w * enc_in->h * 3)/2;
	enc_in->data = ast_calloc(1, enc_in->size);
	if (!enc_in->data) {
		ast_log(LOG_WARNING, "Cannot allocate encoder input buffer\n");
		return video_out_uninit(v);
	}
	v->frame = avcodec_alloc_frame();
	if (!v->frame) {
		ast_log(LOG_WARNING, "Unable to allocate the encoding video frame\n");
		return video_out_uninit(v);
	}

	/* parameters for PIX_FMT_YUV420P */
	size = enc_in->w * enc_in->h;
	v->frame->data[0] = enc_in->data;
	v->frame->data[1] = v->frame->data[0] + size;
	v->frame->data[2] = v->frame->data[1] + size/4;
	v->frame->linesize[0] = enc_in->w;
	v->frame->linesize[1] = enc_in->w/2;
	v->frame->linesize[2] = enc_in->w/2;

	/* now setup the parameters for the encoder */
	v->enc_ctx = avcodec_alloc_context();
	v->enc_ctx->pix_fmt = enc_in->pix_fmt;
	v->enc_ctx->width = enc_in->w;
	v->enc_ctx->height = enc_in->h;
	/* XXX rtp_callback ?
	 * rtp_mode so ffmpeg inserts as many start codes as possible.
	 */
	v->enc_ctx->rtp_mode = 1;
	v->enc_ctx->rtp_payload_size = v->mtu / 2; // mtu/2
	v->enc_ctx->bit_rate = v->bitrate;
	v->enc_ctx->bit_rate_tolerance = v->enc_ctx->bit_rate/2;
	v->enc_ctx->qmin = v->qmin;	/* should be configured */
	v->enc_ctx->time_base = (AVRational){1, v->fps};

	v->enc->enc_init(v);
 
	if (avcodec_open(v->enc_ctx, v->codec) < 0) {
		ast_log(LOG_WARNING, "Unable to initialize the encoder %d\n",
			codec);
		av_free(v->enc_ctx);
		v->enc_ctx = NULL;
		return video_out_uninit(v);
	}

	/*
	 * Allocate enough for the encoded bitstream. As we are compressing,
	 * we hope that the output is never larger than the input size.
	 */
	v->enc_out.data = ast_calloc(1, enc_in->size);
	v->enc_out.size = enc_in->size;
	v->enc_out.used = 0;

	return 0;
}

static void cleanup_sdl(struct video_desc *env)  
{
	int i;

#ifdef HAVE_SDL_TTF
	/* unload font file */ 
	if (env->gui.font) {
		TTF_CloseFont(env->gui.font);
		env->gui.font = NULL; 
	}

	/* uninitialize SDL_ttf library */
	if ( TTF_WasInit() )
		TTF_Quit();
#endif

	/* uninitialize the SDL environment */
	for (i = 0; i < WIN_MAX; i++) {
		if (env->win[i].bmp)
			SDL_FreeYUVOverlay(env->win[i].bmp);
	}
	if (env->gui.keypad)
		SDL_FreeSurface(env->gui.keypad);
	env->gui.keypad = NULL;
	SDL_Quit();
	env->screen = NULL; /* XXX check reference */
	bzero(env->win, sizeof(env->win));
	if (env->sdl_ok)
		ast_mutex_destroy(&(env->in.dec_in_lock));
}

/*! \brief uninitialize the entire environment.
 * In practice, signal the thread and give it a bit of time to
 * complete, giving up if it gets stuck. Because uninit
 * is called from hangup with the channel locked, and the thread
 * uses the chan lock, we need to unlock here. This is unsafe,
 * and we should really use refcounts for the channels.
 */
void console_video_uninit(struct video_desc *env)
{
	int i, t = 100;	/* initial wait is shorter, than make it longer */
	env->shutdown = 1;
	for (i=0; env->shutdown && i < 10; i++) {
                ast_channel_unlock(env->owner);
                usleep(t);
		t = 1000000;
                ast_channel_lock(env->owner);
        }
	env->owner = NULL;
}

/*! fill an AVPicture from our fbuf info, as it is required by
 * the image conversion routines in ffmpeg.
 * XXX This depends on the format.
 */
static AVPicture *fill_pict(struct fbuf_t *b, AVPicture *p)
{
	/* provide defaults for commonly used formats */
	int l4 = b->w * b->h/4; /* size of U or V frame */
	int len = b->w;		/* Y linesize, bytes */
	int luv = b->w/2;	/* U/V linesize, bytes */

	bzero(p, sizeof(*p));
	switch (b->pix_fmt) {
	case PIX_FMT_RGB555:
	case PIX_FMT_RGB565:
		len *= 2;
		luv = 0;
		break;
	case PIX_FMT_RGBA32:
		len *= 4;
		luv = 0;
		break;
	case PIX_FMT_YUYV422:	/* Packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr */
		len *= 2;	/* all data in first plane, probably */
		luv = 0;
		break;
	}
	p->data[0] = b->data;
	p->linesize[0] = len;
	/* these are only valid for component images */
	p->data[1] = luv ? b->data + 4*l4 : b->data+len;
	p->data[2] = luv ? b->data + 5*l4 : b->data+len;
	p->linesize[1] = luv;
	p->linesize[2] = luv;
	return p;
}

/*! convert/scale between an input and an output format.
 * Old version of ffmpeg only have img_convert, which does not rescale.
 * New versions use sws_scale which does both.
 */
static void my_scale(struct fbuf_t *in, AVPicture *p_in,
	struct fbuf_t *out, AVPicture *p_out)
{
	AVPicture my_p_in, my_p_out;

	if (p_in == NULL)
		p_in = fill_pict(in, &my_p_in);
	if (p_out == NULL)
		p_out = fill_pict(out, &my_p_out);

#ifdef OLD_FFMPEG
	/* XXX img_convert is deprecated, and does not do rescaling */
	img_convert(p_out, out->pix_fmt,
		p_in, in->pix_fmt, in->w, in->h);
#else /* XXX replacement */
    {
	struct SwsContext *convert_ctx;

	convert_ctx = sws_getContext(in->w, in->h, in->pix_fmt,
		out->w, out->h, out->pix_fmt,
		SWS_BICUBIC, NULL, NULL, NULL);
	if (convert_ctx == NULL) {
		ast_log(LOG_ERROR, "FFMPEG::convert_cmodel : swscale context initialization failed");
		return;
	}
	if (0)
		ast_log(LOG_WARNING, "in %d %dx%d out %d %dx%d\n",
			in->pix_fmt, in->w, in->h, out->pix_fmt, out->w, out->h);
	sws_scale(convert_ctx,
		p_in->data, p_in->linesize,
		in->w, in->h, /* src slice */
		p_out->data, p_out->linesize);

	sws_freeContext(convert_ctx);
    }
#endif /* XXX replacement */
}

/*
 * Display video frames (from local or remote stream) using the SDL library.
 * - Set the video mode to use the resolution specified by the codec context
 * - Create a YUV Overlay to copy the frame into it;
 * - After the frame is copied into the overlay, display it
 *
 * The size is taken from the configuration.
 *
 * 'out' is 0 for remote video, 1 for the local video
 */
static void show_frame(struct video_desc *env, int out)
{
	AVPicture *p_in, p_out;
	struct fbuf_t *b_in, *b_out;
	SDL_Overlay *bmp;

	if (!env->sdl_ok)
		return;

	if (out == WIN_LOCAL) {	/* webcam/x11 to sdl */
		b_in = &env->out.enc_in;
		b_out = &env->out.loc_dpy;
		p_in = NULL;
	} else {
		/* copy input format from the decoding context */
		AVCodecContext *c = env->in.dec_ctx;
		b_in = &env->in.dec_out;
                b_in->pix_fmt = c->pix_fmt;
                b_in->w = c->width;
                b_in->h = c->height;

		b_out = &env->in.rem_dpy;
		p_in = (AVPicture *)env->in.d_frame;
	}
	bmp = env->win[out].bmp;
	SDL_LockYUVOverlay(bmp);
	/* output picture info - this is sdl, YUV420P */
	bzero(&p_out, sizeof(p_out));
	p_out.data[0] = bmp->pixels[0];
	p_out.data[1] = bmp->pixels[1];
	p_out.data[2] = bmp->pixels[2];
	p_out.linesize[0] = bmp->pitches[0];
	p_out.linesize[1] = bmp->pitches[1];
	p_out.linesize[2] = bmp->pitches[2];

	my_scale(b_in, p_in, b_out, &p_out);

	/* lock to protect access to Xlib by different threads. */
	SDL_DisplayYUVOverlay(bmp, &env->win[out].rect);
	SDL_UnlockYUVOverlay(bmp);
}

struct video_desc *get_video_desc(struct ast_channel *c);

/*
 * This function is called (by asterisk) for each video packet
 * coming from the network (the 'in' path) that needs to be processed.
 * We need to reconstruct the entire video frame before we can decode it.
 * After a video packet is received we have to:
 * - extract the bitstream with pre_process_data()
 * - append the bitstream to a buffer
 * - if the fragment is the last (RTP Marker) we decode it with decode_video()
 * - after the decoding is completed we display the decoded frame with show_frame()
 */
int console_write_video(struct ast_channel *chan, struct ast_frame *f);
int console_write_video(struct ast_channel *chan, struct ast_frame *f)
{
	struct video_desc *env = get_video_desc(chan);
	struct video_in_desc *v = &env->in;

	if (v->dec == NULL) {	/* try to get the codec */
		v->dec = map_video_codec(f->subclass & ~1);
		if (v->dec == NULL) {
			ast_log(LOG_WARNING, "cannot find video codec, drop input 0x%x\n", f->subclass);
			return 0;
		}
		if (video_in_init(v, v->dec->format)) {
			/* This is not fatal, but we won't have incoming video */
			ast_log(LOG_WARNING, "Cannot initialize input decoder\n");
			v->dec = NULL;
			return 0;
		}
	}
	if (v->dec_ctx == NULL) {
		ast_log(LOG_WARNING, "cannot decode, dropping frame\n");
		return 0;	/* error */
	}

	if (v->dec_in_cur == NULL)	/* no buffer for incoming frames, drop */
		return 0;
#if defined(DROP_PACKETS) && DROP_PACKETS > 0
	/* Simulate lost packets */
	if ((random() % 10000) <= 100*DROP_PACKETS) {
		ast_log(LOG_NOTICE, "Packet lost [%d]\n", f->seqno);
		return 0;
	}
#endif
	if (v->discard) {
		/*
		 * In discard mode, drop packets until we find one with
		 * the RTP marker set (which is the end of frame).
		 * Note that the RTP marker flag is sent as the LSB of the
		 * subclass, which is a  bitmask of formats. The low bit is
		 * normally used for audio so there is no interference.
		 */
		if (f->subclass & 0x01) {
			v->dec_in_cur->used = 0;
			v->dec_in_cur->ebit = 0;
			v->next_seq = f->seqno + 1;	/* wrap at 16 bit */
			v->discard = 0;
			ast_log(LOG_WARNING, "out of discard mode, frame %d\n", f->seqno);
		}
		return 0;
	}

	/*
	 * Only in-order fragments will be accepted. Remember seqno
	 * has 16 bit so there is wraparound. Also, ideally we could
	 * accept a bit of reordering, but at the moment we don't.
	 */
	if (v->next_seq != f->seqno) {
		ast_log(LOG_WARNING, "discarding frame out of order, %d %d\n",
			v->next_seq, f->seqno);
		v->discard = 1;
		return 0;
	}
	v->next_seq++;

	if (f->data == NULL || f->datalen < 2) {
		ast_log(LOG_WARNING, "empty video frame, discard\n");
		return 0;
	}
	if (v->dec->dec_decap(v->dec_in_cur, f->data, f->datalen)) {
		ast_log(LOG_WARNING, "error in dec_decap, enter discard\n");
		v->discard = 1;
	}
	if (f->subclass & 0x01) {	// RTP Marker
		/* prepare to decode: advance the buffer so the video thread knows. */
		struct fbuf_t *tmp = v->dec_in_cur;	/* store current pointer */
		ast_mutex_lock(&v->dec_in_lock);
		if (++v->dec_in_cur == &v->dec_in[N_DEC_IN])	/* advance to next, circular */
			v->dec_in_cur = &v->dec_in[0];
		if (v->dec_in_dpy == NULL) {	/* were not displaying anything, so set it */
			v->dec_in_dpy = tmp;
		} else if (v->dec_in_dpy == v->dec_in_cur) { /* current slot is busy */
			v->dec_in_cur = NULL;
		}
		ast_mutex_unlock(&v->dec_in_lock);
	}
	return 0;
}


/*! \brief read a frame from webcam or X11 through video_read(),
 * display it,  then encode and split it.
 * Return a list of ast_frame representing the video fragments.
 * The head pointer is returned by the function, the tail pointer
 * is returned as an argument.
 */
static struct ast_frame *get_video_frames(struct video_desc *env, struct ast_frame **tail)
{
	struct video_out_desc *v = &env->out;
	struct ast_frame *dummy;

	if (!v->loc_src.data) {
		static volatile int a = 0;
		if (a++ < 2)
			ast_log(LOG_WARNING, "fail, no loc_src buffer\n");
		return NULL;
	}
	if (!video_read(v))
		return NULL;	/* can happen, e.g. we are reading too early */

	if (tail == NULL)
		tail = &dummy;
	*tail = NULL;
	/* Scale the video for the encoder, then use it for local rendering
	 * so we will see the same as the remote party.
	 */
	my_scale(&v->loc_src, NULL, &v->enc_in, NULL);
	show_frame(env, WIN_LOCAL);
	if (!v->sendvideo)
		return NULL;
	if (v->enc_out.data == NULL) {
		static volatile int a = 0;
		if (a++ < 2)
			ast_log(LOG_WARNING, "fail, no encbuf\n");
		return NULL;
	}
	v->enc->enc_run(v);
	return v->enc->enc_encap(v, tail);
}

/*
 * GUI layout, structure and management
 *

For the GUI we use SDL to create a large surface (env->screen)
containing tree sections: remote video on the left, local video
on the right, and the keypad with all controls and text windows
in the center.
The central section is built using two images: one is the skin,
the other one is a mask where the sensitive areas of the skin
are colored in different grayscale levels according to their
functions. The mapping between colors and function is defined
in the 'enum pixel_value' below.

Mouse and keyboard events are detected on the whole surface, and
handled differently according to their location, as follows:

- drag on the local video window are used to move the captured
  area (in the case of X11 grabber) or the picture-in-picture
  location (in case of camera included on the X11 grab).
- click on the keypad are mapped to the corresponding key;
- drag on some keypad areas (sliders etc.) are mapped to the
  corresponding functions;
- keystrokes are used as keypad functions, or as text input
  if we are in text-input mode.

To manage these behavior we use two status variables,
that defines if keyboard events should be redirect to dialing functions
or to write message functions, and if mouse events should be used
to implement keypad functionalities or to drag the capture device.

Configuration options control the appeareance of the gui:

    keypad = /tmp/phone.jpg		; the keypad on the screen
    keypad_font = /tmp/font.ttf		; the font to use for output

 *
 */

/* enumerate for the pixel value. 0..127 correspond to ascii chars */
enum pixel_value {
	/* answer/close functions */
	KEY_PICK_UP = 128,
	KEY_HANG_UP = 129,

	/* other functions */
	KEY_MUTE = 130,
	KEY_AUTOANSWER = 131,
	KEY_SENDVIDEO = 132,
	KEY_LOCALVIDEO = 133,
	KEY_REMOTEVIDEO = 134,
	KEY_WRITEMESSAGE = 135,
	KEY_GUI_CLOSE = 136,		/* close gui */

	/* other areas within the keypad */
	KEY_DIGIT_BACKGROUND = 255,

	/* areas outside the keypad - simulated */
	KEY_OUT_OF_KEYPAD = 251,
	KEY_REM_DPY = 252,
	KEY_LOC_DPY = 253,
};

/*
 * Handlers for the various keypad functions
 */

/*! \brief append a character, or reset if '\0' */
static void append_char(char *str, int *str_pos, const char c)
{
	int i = *str_pos;
	if (c == '\0')
		i = 0;
	else if (i < GUI_BUFFER_LEN - 1)
		str[i++] = c;
	else
		i = GUI_BUFFER_LEN - 1; /* unnecessary, i think */
	str = '\0';
	*str_pos = i;
}

/* accumulate digits, possibly call dial if in connected mode */
static void keypad_digit(struct video_desc *env, int digit)
{	
	if (env->owner) {		/* we have a call, send the digit */
		struct ast_frame f = { AST_FRAME_DTMF, 0 };

		f.subclass = digit;
		ast_queue_frame(env->owner, &f);
	} else {		/* no call, accumulate digits */
		append_char(env->gui.inbuf, &env->gui.inbuf_pos, digit);
	}
}

/* this is a wrapper for actions that are available through the cli */
/* TODO append arg to command and send the resulting string as cli command */
static void keypad_send_command(struct video_desc *env, char *command)
{	
	ast_log(LOG_WARNING, "keypad_send_command(%s) called\n", command);
	ast_cli_command(env->gui.outfd, command);
	return;
}

/* function used to toggle on/off the status of some variables */
static char *keypad_toggle(struct video_desc *env, int index)
{
	ast_log(LOG_WARNING, "keypad_toggle(%i) called\n", index);

	switch (index) {
	case KEY_SENDVIDEO:
		env->out.sendvideo = !env->out.sendvideo;
		break;
#ifdef notyet
	case KEY_MUTE: {
		struct chan_oss_pvt *o = find_desc(oss_active);
		o->mute = !o->mute;
		}
		break;
	case KEY_AUTOANSWER: {
		struct chan_oss_pvt *o = find_desc(oss_active);
		o->autoanswer = !o->autoanswer;
		}
		break;
#endif
	}
	return NULL;
}

char *console_do_answer(int fd);
/*
 * Function called when the pick up button is pressed
 * perform actions according the channel status:
 *
 *  - if no one is calling us and no digits was pressed,
 *    the operation have no effects,
 *  - if someone is calling us we answer to the call.
 *  - if we have no call in progress and we pressed some
 *    digit, send the digit to the console.
 */
static void keypad_pick_up(struct video_desc *env)
{
	ast_log(LOG_WARNING, "keypad_pick_up called\n");

	if (env->owner) { /* someone is calling us, just answer */
		console_do_answer(-1);
	} else if (env->gui.inbuf_pos) { /* we have someone to call */
		ast_cli_command(env->gui.outfd, env->gui.inbuf);
	}

	append_char(env->gui.inbuf, &env->gui.inbuf_pos, '\0'); /* clear buffer */
}

#if 0 /* still unused */
/*
 * As an alternative to SDL_TTF, we can simply load the font from
 * an image and blit characters on the background of the GUI.
 *
 * To generate a font we can use the 'fly' command with the
 * following script (3 lines with 32 chars each)
 
size 320,64
name font.png
transparent 0,0,0
string 255,255,255,  0, 0,giant, !"#$%&'()*+,-./0123456789:;<=>?
string 255,255,255,  0,20,giant,@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
string 255,255,255,  0,40,giant,`abcdefghijklmnopqrstuvwxyz{|}~
end

 */

/* Print given text on the gui */
static int gui_output(struct video_desc *env, const char *text)
{
#ifndef HAVE_SDL_TTF
	return 1;	/* error, not supported */
#else
	int x = 30, y = 20;	/* XXX change */
	SDL_Surface *output = NULL;
	SDL_Color color = {0, 0, 0};	/* text color */
	SDL_Rect dest = {env->win[WIN_KEYPAD].rect.x + x, y};

	/* clean surface each rewrite */
	SDL_BlitSurface(env->gui.keypad, NULL, env->screen, &env->win[WIN_KEYPAD].rect);

	output = TTF_RenderText_Solid(env->gui.font, text, color);
	if (output == NULL) {
		ast_log(LOG_WARNING, "Cannot render text on gui - %s\n", TTF_GetError());
		return 1;
	}

	SDL_BlitSurface(output, NULL, env->screen, &dest);
	
	SDL_UpdateRects(env->gui.keypad, 1, &env->win[WIN_KEYPAD].rect);
	SDL_FreeSurface(output);
	return 0;	/* success */
#endif
}
#endif 

static int video_geom(struct fbuf_t *b, const char *s);
static void sdl_setup(struct video_desc *env);
static int kp_match_area(const struct keypad_entry *e, int x, int y);

/*
 * Handle SDL_MOUSEBUTTONDOWN type, finding the palette
 * index value and calling the right callback.
 *
 * x, y are referred to the upper left corner of the main SDL window.
 */
static void handle_button_event(struct video_desc *env, SDL_MouseButtonEvent button)
{
	uint8_t index = KEY_OUT_OF_KEYPAD;	/* the key or region of the display we clicked on */

	/* for each click we come back in normal mode */
	env->gui.text_mode = 0;

	/* define keypad boundary */
	if (button.x < env->in.rem_dpy.w)
		index = KEY_REM_DPY; /* click on remote video */
	else if (button.x > env->in.rem_dpy.w + env->out.keypad_dpy.w)
		index = KEY_LOC_DPY; /* click on local video */
	else if (button.y > env->out.keypad_dpy.h)
		index = KEY_OUT_OF_KEYPAD; /* click outside the keypad */
	else if (env->gui.kp) {
		int i;
		for (i = 0; i < env->gui.kp_used; i++) {
			if (kp_match_area(&env->gui.kp[i], button.x - env->in.rem_dpy.w, button.y)) {
				index = env->gui.kp[i].c;
				break;
			}
		}
	}

	/* exec the function */
	if (index < 128) {	/* surely clicked on the keypad, don't care which key */
		keypad_digit(env, index);
		return;
	}
	switch (index) {
	/* answer/close function */
	case KEY_PICK_UP:
		keypad_pick_up(env);
		break;
	case KEY_HANG_UP:
		keypad_send_command(env, "console hangup");
		break;

	/* other functions */
	case KEY_MUTE:
	case KEY_AUTOANSWER:
	case KEY_SENDVIDEO:
		keypad_toggle(env, index);
		break;

	case KEY_LOCALVIDEO:
		break;
	case KEY_REMOTEVIDEO:
		break;
	case KEY_WRITEMESSAGE:
		/* goes in text-mode */
		env->gui.text_mode = 1;
		break;


	/* press outside the keypad. right increases size, center decreases, left drags */
	case KEY_LOC_DPY:
	case KEY_REM_DPY:
		if (button.button == SDL_BUTTON_LEFT) {
			if (index == KEY_LOC_DPY) {
				/* store points where the drag start
				* and switch in drag mode */
				env->gui.x_drag = button.x;
				env->gui.y_drag = button.y;
				env->gui.drag_mode = 1;
			}
			break;
		} else {
			char buf[128];
			struct fbuf_t *fb = index == KEY_LOC_DPY ? &env->out.loc_dpy : &env->in.rem_dpy;
			sprintf(buf, "%c%dx%d", button.button == SDL_BUTTON_RIGHT ? '>' : '<',
				fb->w, fb->h);
			video_geom(fb, buf);
			sdl_setup(env);
		}
		break;
	case KEY_OUT_OF_KEYPAD:
		break;

	case KEY_GUI_CLOSE:
		cleanup_sdl(env);
		break;
	case KEY_DIGIT_BACKGROUND:
		break;
	default:
		ast_log(LOG_WARNING, "function not yet defined %i\n", index);
	}
}

/*
 * Handle SDL_KEYDOWN type event, put the key pressed
 * in the dial buffer or in the text-message buffer,
 * depending on the text_mode variable value.
 *
 * key is the SDLKey structure corresponding to the key pressed.
 */
static void handle_keyboard_input(struct video_desc *env, SDLKey key)
{
	if (env->gui.text_mode) {
		/* append in the text-message buffer */
		if (key == SDLK_RETURN) {
			/* send the text message and return in normal mode */
			env->gui.text_mode = 0;
			keypad_send_command(env, "send text");
		} else {
			/* accumulate the key in the message buffer */
			append_char(env->gui.msgbuf, &env->gui.msgbuf_pos, key);
		}
	}
	else {
		/* append in the dial buffer */
		append_char(env->gui.inbuf, &env->gui.inbuf_pos, key);
	}

	return;
}

/*
 * Check if the grab point is inside the X screen.
 *
 * x represent the new grab value
 * limit represent the upper value to use
 */
static int boundary_checks(int x, int limit)
{
	return (x <= 0) ? 0 : (x > limit ? limit : x);
}

/* implement superlinear acceleration on the movement */
static int move_accel(int delta)
{
	int d1 = delta*delta / 100;
	return (delta > 0) ? delta + d1 : delta - d1;
}

/*
 * Move the source of the captured video.
 *
 * x_final_drag and y_final_drag are the coordinates where the drag ends,
 * start coordinares are in the gui_info structure.
 */
static void move_capture_source(struct video_desc *env, int x_final_drag, int y_final_drag)
{
	int new_x, new_y;		/* new coordinates for grabbing local video */
	int x = env->out.loc_src.x;	/* old value */
	int y = env->out.loc_src.y;	/* old value */

	/* move the origin */
#define POLARITY -1		/* +1 or -1 depending on the desired direction */
	new_x = x + POLARITY*move_accel(x_final_drag - env->gui.x_drag) * 3;
	new_y = y + POLARITY*move_accel(y_final_drag - env->gui.y_drag) * 3;
#undef POLARITY
	env->gui.x_drag = x_final_drag;	/* update origin */
	env->gui.y_drag = y_final_drag;

	/* check boundary and let the source to grab from the new points */
	env->out.loc_src.x = boundary_checks(new_x, env->out.screen_width - env->out.loc_src.w);
	env->out.loc_src.y = boundary_checks(new_y, env->out.screen_height - env->out.loc_src.h);
	return;
}

/*
 * I am seeing some kind of deadlock or stall around
 * SDL_PumpEvents() while moving the window on a remote X server
 * (both xfree-4.4.0 and xorg 7.2)
 * and windowmaker. It is unclear what causes it.
 */

/* grab a bunch of events */
static void eventhandler(struct video_desc *env)
{
#define N_EVENTS	32
	int i, n;
	SDL_Event ev[N_EVENTS];

#define MY_EV (SDL_MOUSEBUTTONDOWN|SDL_KEYDOWN)
	while ( (n = SDL_PeepEvents(ev, N_EVENTS, SDL_GETEVENT, SDL_ALLEVENTS)) > 0) {
		for (i = 0; i < n; i++) {
#if 0
			ast_log(LOG_WARNING, "------ event %d at %d %d\n",
				ev[i].type,  ev[i].button.x,  ev[i].button.y);
#endif
			switch (ev[i].type) {
			case SDL_KEYDOWN:
				handle_keyboard_input(env, ev[i].key.keysym.sym);
				break;
			case SDL_MOUSEMOTION:
				if (env->gui.drag_mode != 0)
					move_capture_source(env, ev[i].motion.x, ev[i].motion.y);
				break;
			case SDL_MOUSEBUTTONDOWN:
				handle_button_event(env, ev[i].button);
				break;
			case SDL_MOUSEBUTTONUP:
				if (env->gui.drag_mode != 0) {
					move_capture_source(env, ev[i].button.x, ev[i].button.y);
					env->gui.drag_mode = 0;
				}
				break;
			}

		}
	}
	if (1) {
		struct timeval b, a = ast_tvnow();
		int i;
		//SDL_Lock_EventThread();
		SDL_PumpEvents();
		b = ast_tvnow();
		i = ast_tvdiff_ms(b, a);
		if (i > 3)
			fprintf(stderr, "-------- SDL_PumpEvents took %dms\n", i);
		//SDL_Unlock_EventThread();
	}
}

static SDL_Surface *get_keypad(const char *file)
{
	SDL_Surface *temp;
 
#ifdef HAVE_SDL_IMAGE
	temp = IMG_Load(file);
#else
	temp = SDL_LoadBMP(file);
#endif
	if (temp == NULL)
		fprintf(stderr, "Unable to load image %s: %s\n",
			file, SDL_GetError());
	return temp;
}

/* TODO: consistency checks, check for bpp, widht and height */
/* Init the mask image used to grab the action. */
static int gui_init(struct video_desc *env)
{
	/* initialize keypad status */
	env->gui.text_mode = 0;
	env->gui.drag_mode = 0;

	/* initialize grab coordinates */
	env->out.loc_src.x = 0;
	env->out.loc_src.y = 0;

	/* initialize keyboard buffer */
	append_char(env->gui.inbuf, &env->gui.inbuf_pos, '\0');
	append_char(env->gui.msgbuf, &env->gui.msgbuf_pos, '\0');

#ifdef HAVE_SDL_TTF
	/* Initialize SDL_ttf library and load font */
	if (TTF_Init() == -1) {
		ast_log(LOG_WARNING, "Unable to init SDL_ttf, no output available\n");
		return -1;
	}

#define GUI_FONTSIZE 28
	env->gui.font = TTF_OpenFont( env->keypad_font, GUI_FONTSIZE);
	if (!env->gui.font) {
		ast_log(LOG_WARNING, "Unable to load font %s, no output available\n", env->keypad_font);
		return -1;
	}
	ast_log(LOG_WARNING, "Loaded font %s\n", env->keypad_font);
#endif

	env->gui.outfd = open ("/dev/null", O_WRONLY);	/* discard output, temporary */
	if ( env->gui.outfd < 0 ) {
		ast_log(LOG_WARNING, "Unable output fd\n");
		return -1;
	}

	return 0;
}

static void sdl_setup(struct video_desc *env);

/*
 * Helper thread to periodically poll the video source and enqueue the
 * generated frames to the channel's queue.
 * Using a separate thread also helps because the encoding can be
 * computationally expensive so we don't want to starve the main thread.
 */
static void *video_thread(void *arg)
{
	struct video_desc *env = arg;
	int count = 0;

	env->screen = NULL;
	bzero(env->win, sizeof(env->win));

	if (SDL_Init(SDL_INIT_VIDEO)) {
		ast_log(LOG_WARNING, "Could not initialize SDL - %s\n",
			SDL_GetError());
		/* again not fatal, just we won't display anything */
	} else {
		sdl_setup(env);
		if (env->sdl_ok)
			ast_mutex_init(&env->in.dec_in_lock);
		/* TODO, segfault if not X display present */
		env->gui_ok = !gui_init(env);
		if (!env->gui_ok)
			ast_log(LOG_WARNING, "cannot init console gui\n");
	}
	if (video_open(&env->out)) {
		ast_log(LOG_WARNING, "cannot open local video source\n");
	} else {
		/* try to register the fd. Unfortunately, if the webcam
		 * driver does not support select/poll we are out of luck.
		 */
		if (env->out.fd >= 0)
			ast_channel_set_fd(env->owner, 1, env->out.fd);
		video_out_init(env);
	}

	for (;;) {
		/* XXX 20 times/sec */
		struct timeval t = { 0, 50000 };
		struct ast_frame *p, *f;
		struct video_in_desc *v = &env->in;
		struct ast_channel *chan = env->owner;
		int fd = chan->alertpipe[1];

		/* determine if video format changed */
		if (count++ % 10 == 0) {
			char buf[160];
			if (env->out.sendvideo)
			    sprintf(buf, "%s %s %dx%d @@ %dfps %dkbps",
				env->out.videodevice, env->codec_name,
				env->out.enc_in.w, env->out.enc_in.h,
				env->out.fps, env->out.bitrate/1000);
			else
			    sprintf(buf, "hold");
			SDL_WM_SetCaption(buf, NULL);
		}

		/* manage keypad events */
		/* XXX here we should always check for events,
		* otherwise the drag will not work */ 
		if (env->gui_ok)
			eventhandler(env);
 
		/* sleep for a while */
		ast_select(0, NULL, NULL, NULL, &t);

		SDL_UpdateRects(env->screen, 1, &env->win[WIN_KEYPAD].rect);// XXX inefficient
		/*
		 * While there is something to display, call the decoder and free
		 * the buffer, possibly enabling the receiver to store new data.
		 */
		while (v->dec_in_dpy) {
			struct fbuf_t *tmp = v->dec_in_dpy;	/* store current pointer */

			if (v->dec->dec_run(v, tmp))
				show_frame(env, WIN_REMOTE);
			tmp->used = 0;	/* mark buffer as free */
			tmp->ebit = 0;
			ast_mutex_lock(&v->dec_in_lock);
			if (++v->dec_in_dpy == &v->dec_in[N_DEC_IN])	/* advance to next, circular */
				v->dec_in_dpy = &v->dec_in[0];

			if (v->dec_in_cur == NULL)	/* receiver was idle, enable it... */
				v->dec_in_cur = tmp;	/* using the slot just freed */
			else if (v->dec_in_dpy == v->dec_in_cur) /* this was the last slot */
				v->dec_in_dpy = NULL;	/* nothing more to display */
			ast_mutex_unlock(&v->dec_in_lock);
		}


		f = get_video_frames(env, &p);	/* read and display */
		if (!f)
			continue;
		if (env->shutdown)
			break;
		chan = env->owner;
		ast_channel_lock(chan);

		/* AST_LIST_INSERT_TAIL is only good for one frame, cannot use here */
		if (chan->readq.first == NULL) {
			chan->readq.first = f;
		} else {
			chan->readq.last->frame_list.next = f;
		}
		chan->readq.last = p;
		/*
		 * more or less same as ast_queue_frame, but extra
		 * write on the alertpipe to signal frames.
		 */
		if (fd > -1) {
			int blah = 1, l = sizeof(blah);
			for (p = f; p; p = AST_LIST_NEXT(p, frame_list)) {
				if (write(fd, &blah, l) != l)
					ast_log(LOG_WARNING, "Unable to write to alert pipe on %s, frametype/subclass %d/%d: %s!\n",
					    chan->name, f->frametype, f->subclass, strerror(errno));
			}
		}
		ast_channel_unlock(chan);
	}
	/* thread terminating, here could call the uninit */
	/* uninitialize the local and remote video environments */
	video_in_uninit(&env->in);
	video_out_uninit(&env->out);

	if (env->sdl_ok)
		cleanup_sdl(env);

	env->shutdown = 0;
	return NULL;
}

static void copy_geometry(struct fbuf_t *src, struct fbuf_t *dst)
{
	if (dst->w == 0)
		dst->w = src->w;
	if (dst->h == 0)
		dst->h = src->h;
}

/*! initialize the video environment.
 * Apart from the formats (constant) used by sdl and the codec,
 * we use enc_in as the basic geometry.
 */
static void init_env(struct video_desc *env)
{
	struct fbuf_t *c = &(env->out.loc_src);		/* local source */
	struct fbuf_t *ei = &(env->out.enc_in);		/* encoder input */
	struct fbuf_t *ld = &(env->out.loc_dpy);	/* local display */
	struct fbuf_t *rd = &(env->in.rem_dpy);		/* remote display */

	c->pix_fmt = PIX_FMT_YUV420P;	/* default - camera format */
	ei->pix_fmt = PIX_FMT_YUV420P;	/* encoder input */
	if (ei->w == 0 || ei->h == 0) {
		ei->w = 352;
		ei->h = 288;
	}
	ld->pix_fmt = rd->pix_fmt = PIX_FMT_YUV420P; /* sdl format */
	/* inherit defaults */
	copy_geometry(ei, c);	/* camera inherits from encoder input */
	copy_geometry(ei, rd);	/* remote display inherits from encoder input */
	copy_geometry(rd, ld);	/* local display inherits from remote display */
}

/* setup an sdl overlay and associated info, return 0 on success, != 0 on error */
static int set_win(SDL_Surface *screen, struct display_window *win, int fmt,
	int w, int h, int x, int y)
{
	win->bmp = SDL_CreateYUVOverlay(w, h, fmt, screen);
	if (win->bmp == NULL)
		return -1;	/* error */
	win->rect.x = x;
	win->rect.y = y;
	win->rect.w = w;
	win->rect.h = h;
	return 0;
}

/*!
 * The first call to the video code, called by oss_new() or similar.
 * Here we initialize the various components we use, namely SDL for display,
 * ffmpeg for encoding/decoding, and a local video source.
 * We do our best to progress even if some of the components are not
 * available.
 */
void console_video_start(struct video_desc *env, struct ast_channel *owner)
{
	if (env == NULL)	/* video not initialized */
		return;
	if (owner == NULL)	/* nothing to do if we don't have a channel */
		return;
	env->owner = owner;
	init_env(env);
	env->out.enc = map_config_video_format(env->codec_name);

	ast_log(LOG_WARNING, "start video out %s %dx%d\n",
		env->codec_name, env->out.enc_in.w,  env->out.enc_in.h);
	/*
	 * Register all codecs supported by the ffmpeg library.
	 * We only need to do it once, but probably doesn't
	 * harm to do it multiple times.
	 */
	avcodec_init();
	avcodec_register_all();
	av_log_set_level(AV_LOG_ERROR);	/* only report errors */

	if (env->out.fps == 0) {
		env->out.fps = 15;
		ast_log(LOG_WARNING, "fps unset, forcing to %d\n", env->out.fps);
	}
	if (env->out.bitrate == 0) {
		env->out.bitrate = 65000;
		ast_log(LOG_WARNING, "bitrate unset, forcing to %d\n", env->out.bitrate);
	}

	ast_pthread_create_background(&env->vthread, NULL, video_thread, env);
}

static int keypad_cfg_read(struct gui_info *gui, const char *val);

static void keypad_setup(struct video_desc *env)
{
	int fd = -1;
	void *p = NULL;
	off_t l = 0;

	if (env->gui.keypad)
		return;
	env->gui.keypad = get_keypad(env->keypad_file);
	if (!env->gui.keypad)
		return;

	env->out.keypad_dpy.w = env->gui.keypad->w;
	env->out.keypad_dpy.h = env->gui.keypad->h;
	/*
	 * If the keypad image has a comment field, try to read
	 * the button location from there. The block must be
	 *	keypad_entry = token shape x0 y0 x1 y1 h
	 *	...
	 * (basically, lines have the same format as config file entries.
	 * same as the keypad_entry.
	 * You can add it to a jpeg file using wrjpgcom
	 */
	do { /* only once, in fact */
		const char region[] = "region";
		int reg_len = strlen(region);
		const unsigned char *s, *e;

		fd = open(env->keypad_file, O_RDONLY);
		if (fd < 0) {
			ast_log(LOG_WARNING, "fail to open %s\n", env->keypad_file);
			break;
		}
		l = lseek(fd, 0, SEEK_END);
		if (l <= 0) {
			ast_log(LOG_WARNING, "fail to lseek %s\n", env->keypad_file);
			break;
		}
		p = mmap(NULL, l, PROT_READ, 0, fd, 0);
		if (p == NULL) {
			ast_log(LOG_WARNING, "fail to mmap %s size %ld\n", env->keypad_file, (long)l);
			break;
		}
		e = (const unsigned char *)p + l;
		for (s = p; s < e - 20 ; s++) {
			if (!memcmp(s, region, reg_len)) { /* keyword found */
				/* reset previous entries */
				keypad_cfg_read(&env->gui, "reset");
				break;
			}
		}
		for ( ;s < e - 20; s++) {
			char buf[256];
			const unsigned char *s1;
			if (index(" \t\r\n", *s))	/* ignore blanks */
				continue;
			if (*s > 127)	/* likely end of comment */
				break;
			if (memcmp(s, region, reg_len)) /* keyword not found */
				break;
			s += reg_len;
			l = MIN(sizeof(buf), e - s);
			ast_copy_string(buf, s, l);
			s1 = ast_skip_blanks(buf);	/* between token and '=' */
			if (*s1++ != '=')	/* missing separator */
				break;
			if (*s1 == '>')	/* skip => */
				s1++;
			keypad_cfg_read(&env->gui, ast_skip_blanks(s1));
			/* now wait for a newline */
			s1 = s;
			while (s1 < e - 20 && !index("\r\n", *s1) && *s1 < 128)
				s1++;
			s = s1;
		}
	} while (0);
	if (p)
		munmap(p, l);
	if (fd >= 0)
		close(fd);
}

/* [re]set the main sdl window, useful in case of resize */
static void sdl_setup(struct video_desc *env)
{
	int dpy_fmt = SDL_IYUV_OVERLAY;	/* YV12 causes flicker in SDL */
	int depth, maxw, maxh;
	const SDL_VideoInfo *info = SDL_GetVideoInfo();

	/* We want at least 16bpp to support YUV overlays.
	 * E.g with SDL_VIDEODRIVER = aalib the default is 8
	 */
	depth = info->vfmt->BitsPerPixel;
	if (depth < 16)
		depth = 16;
	/*
	 * initialize the SDL environment. We have one large window
	 * with local and remote video, and a keypad.
	 * At the moment we arrange them statically, as follows:
	 * - on the left, the remote video;
	 * - on the center, the keypad
	 * - on the right, the local video
	 */

	keypad_setup(env);
#define BORDER	5	/* border around our windows */
	maxw = env->in.rem_dpy.w + env->out.loc_dpy.w + env->out.keypad_dpy.w;
	maxh = MAX( MAX(env->in.rem_dpy.h, env->out.loc_dpy.h), env->out.keypad_dpy.h);
	maxw += 4 * BORDER;
	maxh += 2 * BORDER;
	env->screen = SDL_SetVideoMode(maxw, maxh, depth, 0);
	if (!env->screen) {
		ast_log(LOG_ERROR, "SDL: could not set video mode - exiting\n");
		goto no_sdl;
	}

	SDL_WM_SetCaption("Asterisk console Video Output", NULL);
	if (set_win(env->screen, &env->win[WIN_REMOTE], dpy_fmt,
			env->in.rem_dpy.w, env->in.rem_dpy.h, BORDER, BORDER))
		goto no_sdl;
	if (set_win(env->screen, &env->win[WIN_LOCAL], dpy_fmt,
			env->out.loc_dpy.w, env->out.loc_dpy.h,
			3*BORDER+env->in.rem_dpy.w + env->out.keypad_dpy.w, BORDER))
		goto no_sdl;

	/* display the skin, but do not free it as we need it later to
	 * restore text areas and maybe sliders too.
	 */
	if (env->gui.keypad) {
		struct SDL_Rect *dest = &env->win[WIN_KEYPAD].rect;
		dest->x = 2*BORDER + env->in.rem_dpy.w;
		dest->y = BORDER;
		dest->w = env->gui.keypad->w;
		dest->h = env->gui.keypad->h;
		SDL_BlitSurface(env->gui.keypad, NULL, env->screen, dest);
		SDL_UpdateRects(env->screen, 1, dest);
	}
	env->in.dec_in_cur = &env->in.dec_in[0];
	env->in.dec_in_dpy = NULL;	/* nothing to display */
	env->sdl_ok = 1;

no_sdl:
	if (env->sdl_ok == 0)	/* free resources in case of errors */
		cleanup_sdl(env);
}

/*
 * Parse a geometry string, accepting also common names for the formats.
 * Trick: if we have a leading > or < and a numeric geometry,
 * return the larger or smaller one.
 * E.g. <352x288 gives the smaller one, 320x240
 */
static int video_geom(struct fbuf_t *b, const char *s)
{
	int w = 0, h = 0;

	static struct {
		const char *s; int w; int h;
	} *fp, formats[] = {
		{"vga",		640, 480 },
		{"cif",		352, 288 },
		{"qvga",	320, 240 },
		{"qcif",	176, 144 },
		{"sqcif",	128, 96 },
		{NULL,		0, 0 },
	};
	if (*s == '<' || *s == '>')
		sscanf(s+1,"%dx%d", &w, &h);
	for (fp = formats; fp->s; fp++) {
		if (*s == '>') {	/* look for a larger one */
			if (fp->w <= w) {
				if (fp > formats)
					fp--; /* back one step if possible */
				break;
			}
		} else if (*s == '<') {	/* look for a smaller one */
			if (fp->w < w)
				break;
		} else if (!strcasecmp(s, fp->s)) { /* look for a string */
			break;
		}
	}
	if (*s == '<' && fp->s == NULL)	/* smallest */
		fp--;
	if (fp->s) {
		b->w = fp->w;
		b->h = fp->h;
	} else if (sscanf(s, "%dx%d", &b->w, &b->h) != 2) {
		ast_log(LOG_WARNING, "Invalid video_size %s, using 352x288\n", s);
		b->w = 352;
		b->h = 288;
	}
	return 0;
}

/*
 * Functions to determine if a point is within a region. Return 1 if success.
 * First rotate the point, with
 *	x' =  (x - x0) * cos A + (y - y0) * sin A
 *	y' = -(x - x0) * sin A + (y - y0) * cos A
 * where cos A = (x1-x0)/l, sin A = (y1 - y0)/l, and
 *	l = sqrt( (x1-x0)^2 + (y1-y0)^2
 * Then determine inclusion by simple comparisons i.e.:
 *	rectangle: x >= 0 && x < l && y >= 0 && y < h
 *	ellipse: (x-xc)^2/l^2 + (y-yc)^2/h2 < 1
 */
static int kp_match_area(const struct keypad_entry *e, int x, int y)
{
	double xp, dx = (e->x1 - e->x0);
	double yp, dy = (e->y1 - e->y0);
	double l = sqrt(dx*dx + dy*dy);
	int ret = 0;

	if (l > 1) { /* large enough */
		xp = ((x - e->x0)*dx + (y - e->y0)*dy)/l;
		yp = (-(x - e->x0)*dy + (y - e->y0)*dx)/l;
		if (e->type == KP_RECT) {
			ret = (xp >= 0 && xp < l && yp >=0 && yp < l);
		} else if (e->type == KP_CIRCLE) {
			dx = xp*xp/(l*l) + yp*yp/(e->h*e->h);
			ret = (dx < 1);
		}
	}
#if 0
	ast_log(LOG_WARNING, "result %d [%d] for match %d,%d in type %d p0 %d,%d p1 %d,%d h %d\n",
		ret, e->c, x, y, e->type, e->x0, e->y0, e->x1, e->y1, e->h);
#endif
	return ret;
}

/*
 * read a keypad entry line in the format
 *	reset
 *	token circle xc yc diameter
 *	token circle xc yc x1 y1 h	# ellipse, main diameter and height
 *	token rect x0 y0 x1 y1 h	# rectangle with main side and eight
 * token is the token to be returned, either a character or a symbol
 * as KEY_* above
 */
struct _s_k { const char *s; int k; };
static struct _s_k gui_key_map[] = {
	{"PICK_UP",	KEY_PICK_UP },
	{"PICKUP",	KEY_PICK_UP },
        {"HANG_UP",	KEY_HANG_UP },
        {"HANGUP",	KEY_HANG_UP },
        {"MUTE",	KEY_MUTE },
        {"AUTOANSWER",	KEY_AUTOANSWER },
        {"SENDVIDEO",	KEY_SENDVIDEO },
        {"LOCALVIDEO",	KEY_LOCALVIDEO },
        {"REMOTEVIDEO",	KEY_REMOTEVIDEO },
        {"WRITEMESSAGE", KEY_WRITEMESSAGE },
        {"GUI_CLOSE",	KEY_GUI_CLOSE },
        {NULL, 0 } };

static int keypad_cfg_read(struct gui_info *gui, const char *val)
{
	struct keypad_entry e;
	char s1[16], s2[16];
	int i, ret = 0;

	bzero(&e, sizeof(e));
	i = sscanf(val, "%14s %14s %d %d %d %d %d",
                s1, s2, &e.x0, &e.y0, &e.x1, &e.y1, &e.h);

	switch (i) {
	default:
		break;
	case 1:	/* only "reset" is allowed */
		if (strcasecmp(s1, "reset"))	/* invalid */
			break;
		if (gui->kp) {
			gui->kp_used = 0;
		}
		break;
	case 5: /* token circle xc yc diameter */
		if (strcasecmp(s2, "circle"))	/* invalid */
			break;
		e.h = e.x1;
		e.y1 = e.y0;	/* map radius in x1 y1 */
		e.x1 = e.x0 + e.h;	/* map radius in x1 y1 */
		e.x0 = e.x0 - e.h;	/* map radius in x1 y1 */
		/* fallthrough */

	case 7: /* token circle|rect x0 y0 x1 y1 h */
		if (e.x1 < e.x0 || e.h <= 0) {
			ast_log(LOG_WARNING, "error in coordinates\n");
			e.type = 0;
			break;
		}
		if (!strcasecmp(s2, "circle")) {
			/* for a circle we specify the diameter but store center and radii */
			e.type = KP_CIRCLE;
			e.x0 = (e.x1 + e.x0) / 2;
			e.y0 = (e.y1 + e.y0) / 2;
			e.h = e.h / 2;
		} else if (!strcasecmp(s2, "rect")) {
			e.type = KP_RECT;
		} else
			break;
		ret = 1;
	}
	// ast_log(LOG_WARNING, "reading [%s] returns %d %d\n", val, i, ret);
	if (ret == 0)
		return 0;
	/* map the string into token to be returned */
	i = atoi(s1);
	if (i > 0 || s1[1] == '\0')	/* numbers or single characters */
		e.c = (i > 9) ? i : s1[0];
	else {
		struct _s_k *p;
		for (p = gui_key_map; p->s; p++) {
			if (!strcasecmp(p->s, s1)) {
				e.c = p->k;
				break;
			}
		}
	}
	if (e.c == 0) {
		ast_log(LOG_WARNING, "missing token\n");
		return 0;
	}
	if (gui->kp_size == 0) {
		gui->kp = ast_calloc(10, sizeof(e));
		if (gui->kp == NULL) {
			ast_log(LOG_WARNING, "cannot allocate kp");
			return 0;
		}
		gui->kp_size = 10;
	}
	if (gui->kp_size == gui->kp_used) { /* must allocate */
		struct keypad_entry *a = ast_realloc(gui->kp, sizeof(e)*(gui->kp_size+10));
		if (a == NULL) {
			ast_log(LOG_WARNING, "cannot reallocate kp");
			return 0;
		}
		gui->kp = a;
		gui->kp_size += 10;
	}
	if (gui->kp_size == gui->kp_used)
		return 0;
	gui->kp[gui->kp_used++] = e;
	return 1;
}

/* extend ast_cli with video commands. Called by console_video_config */
int console_video_cli(struct video_desc *env, const char *var, int fd)
{
	if (env == NULL)
		return 1;	/* unrecognised */

        if (!strcasecmp(var, "videodevice")) {
		ast_cli(fd, "videodevice is [%s]\n", env->out.videodevice);
        } else if (!strcasecmp(var, "videocodec")) {
		ast_cli(fd, "videocodec is [%s]\n", env->codec_name);
        } else if (!strcasecmp(var, "sendvideo")) {
		ast_cli(fd, "sendvideo is [%s]\n", env->out.sendvideo ? "on" : "off");
        } else if (!strcasecmp(var, "video_size")) {
		ast_cli(fd, "sizes: video %dx%d camera %dx%d local %dx%d remote %dx%d in %dx%d\n",
			env->out.enc_in.w, env->out.enc_in.h,
			env->out.loc_src.w, env->out.loc_src.h,
			env->out.loc_dpy.w, env->out.loc_src.h,
			env->in.rem_dpy.w, env->in.rem_dpy.h,
			env->in.dec_out.w, env->in.dec_out.h);
        } else if (!strcasecmp(var, "bitrate")) {
		ast_cli(fd, "bitrate is [%d]\n", env->out.bitrate);
        } else if (!strcasecmp(var, "qmin")) {
		ast_cli(fd, "qmin is [%d]\n", env->out.qmin);
        } else if (!strcasecmp(var, "fps")) {
		ast_cli(fd, "fps is [%d]\n", env->out.fps);
        } else {
		return 1;	/* unrecognised */
	}
	return 0;	/* recognised */
}

/*! parse config command for video support. */
int console_video_config(struct video_desc **penv,
	const char *var, const char *val)
{
	struct video_desc *env;

	if (penv == NULL) {
		ast_log(LOG_WARNING, "bad argument penv=NULL\n");
		return 1;	/* error */
	}
	/* allocate the video descriptor first time we get here */
	env = *penv;
	if (env == NULL) {
		env = *penv = ast_calloc(1, sizeof(struct video_desc));
		if (env == NULL) {
			ast_log(LOG_WARNING, "fail to allocate video_desc\n");
			return 1;	/* error */
		
		}
		/* set default values */
		ast_copy_string(env->out.videodevice, "X11", sizeof(env->out.videodevice));
		env->out.fps = 5;
		env->out.bitrate = 65000;
		env->out.sendvideo = 1;
		env->out.qmin = 3;
	}
	CV_START(var, val);
	CV_STR("videodevice", env->out.videodevice);
	CV_BOOL("sendvideo", env->out.sendvideo);
	CV_F("video_size", video_geom(&env->out.enc_in, val));
	CV_F("camera_size", video_geom(&env->out.loc_src, val));
	CV_F("local_size", video_geom(&env->out.loc_dpy, val));
	CV_F("remote_size", video_geom(&env->in.rem_dpy, val));
	CV_STR("keypad", env->keypad_file);
	CV_F("region", keypad_cfg_read(&env->gui, val));
	CV_STR("keypad_font", env->keypad_font);
	CV_UINT("fps", env->out.fps);
	CV_UINT("bitrate", env->out.bitrate);
	CV_UINT("qmin", env->out.qmin);
	CV_STR("videocodec", env->codec_name);
	return 1;	/* nothing found */

	CV_END;		/* the 'nothing found' case */
	return 0;		/* found something */
}

#endif	/* video support */