-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconv_sd.lua
executable file
·1279 lines (1212 loc) · 33.2 KB
/
conv_sd.lua
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
-- convs_sd.lua : Conversion fichier video en fichier sd-drive
--
-- Version $Version$ $Date$ par Samuel DEVULDER
--
-- Ici l'écran fait 80x50 avec des pixels de 4x4 permettant d'afficher
-- 64 couleurs sur tous les thomson. Les couleurs sont approximées par
-- tramage. Chaque pixel a besoin de 2*3 = 6 bits. Donc sur 3 octets
-- on encode au plus 4 pixels.
--
-- Le son joue à 5025Hz.
--
-- Variables d'environnement:
-- ==========================
--
-- GRAY=0/1/nil (défaut: nil)
-- 0 = couleur
-- 1 = gris
-- nil = détecte tout seul en fonction de la vidéo
--
-- FPS=<nombre d'images par secondes souhaité>
-- La taille écran s'ajuste pour maintenir le débit souhaité si la
-- vidéo est complexe. Si en revanche elle est simple, le débit
-- peut dépasser celui souhaité pour amméliorer la fluidité.
--
-- Si l'on indique une valeur négative, le débit souhaité sera la
-- valeur absolue de ce nombre. Si la vidéo est complexe et que le
-- débit de la carte SD ne permet pas d'encoder la vidéo, la taille
-- de l'image ne sera pas réduite. En revanche le débit sera réduite
-- par rapport à celui souhaité.
--
-- Avec le débit de la carte SD dans SD-drive, la valeur de 11
-- images/secondes est un bon compromis. C'est la valeur par défaut.
--
-- DITH=num (par défaut: 8)
-- Si num>0 utilise un tramage Bayer de niveau num
-- Si num<0 utilise un tramage void&cluster num x num
--
-- Work in progress!
-- =================
-- le code doit être nettoye et rendu plus amical pour l'utilisateur
--
-- Travail débuté en Aout-Oct 2018.
-- ===========================================================================
-- helper functions
local function round(x)
return math.floor(x+.5)
end
local function exists(file)
local ok, err, code = os.rename(file, file)
if not ok then
if code == 13 then
-- Permission denied, but it exists
return true
end
end
return ok, err
end
local function isdir(file)
return exists(file..'/')
end
local function env(var, default)
return loadstring('return ' .. (os.getenv(var) or default))();
end
local function locate(file,...)
-- look locally
local pwd = arg[0]:match("(.*[/\\])") or ''
for _,ext in ipairs{'','.exe'} do
for _,sep in ipairs{'\\','/'} do
for _,root in ipairs{pwd, pwd .. '..' .. sep} do
for _,dir in ipairs{'', ...} do
dir = dir=='' and dir or dir..sep
local tmp = root .. dir .. file .. ext
if exists(tmp) then return tmp end
end
end
end
end
-- else try if system knows about the file
local IN = io.popen('which ' .. file, 'r')
if IN then
local found, line
for line in IN:lines() do
if not line:match(" ") then found = line end
end
IN:close()
if found then return found end
end
error('Cannot locate "' .. file .. '"')
end
local GRAY = env('GRAY','nil')
local FPS = env('FPS',11)
local dither = env('DITH', 3) -- -8 for vac
local FFMPEG = locate('ffmpeg', 'tools')
local YT_DL = locate('yt-dlp', 'tools')
local BIN = locate('bin/')
local POPEN_READBIN = FFMPEG:match(".*%.exe") and "rb" or "r"
local CYCLES = 199 -- cycles par échantillons audio
local BUFFER_SIZE = 4096*4
local FPS_MAX = 30
local TIMEOUT = 2
local GRAY_THR = .1 -- .07
local FILTER_DEPTH = 1
local GRAY_R = 0.30 -- 0.2126
local GRAY_G = 0.59 -- 0.7152
local GRAY_B = 0.11 -- 0.0722
local interlace = nil -- useless
local mode = 'p'
local SPECIAL_4 = false -- experiment ==> no interrest
if SPECIAL_4 then
GRAY = 1
dither = 3
end
-- sanitise
if GRAY~=nil and GRAY~=0 and GRAY~=1 then
error("Env. var. GRAY shall be unset or set to 0 or 1.")
end
if type(FPS)~='number' then
error('Env. var. ENV shall be a positive integer.')
else
FPS = round(FPS)
end
-- if os.execute('cygpath -W >/dev/null 2>&1')==0 then
-- ffmpeg = 'tools/ffmpeg.exe'
-- end
local function percent(x)
return round(math.min(1,x)*100)
end
local function hms(secs, fmt)
secs = round(secs)
return string.format(fmt or "%d:%02d:%02d",
math.floor(secs/3600), math.floor(secs/60)%60, math.floor(secs)%60)
end
function basename(file)
return file:gsub('^/cygdrive/(%w)/','%1:/'):gsub('.*[/\\]',''):gsub('%.[%a%d]+','')
end
-- os.exit(0)
-- flux audio
local AUDIO = {}
function AUDIO:new(file)
-- value such that group_size*1000000/cycles is the most integer
local size = 8
if false then
local min = 10
for i=1,16 do
local x = i*1000000/CYCLES
x = math.abs(x-round(x))
if x<min then size,min = i,x end
print(i,x,size)
end
end
local hz = round(size*1000000/CYCLES)
local I='I=-16' -- volume final -24=superbas, -16=fable
local LRA='LRA=11'
local tp='tp=-2'
local loudnorm = '-af loudnorm='..I..':'..LRA..' '
if true then
io.stderr:write('> analyzing audio...')
io.stderr:flush()
local measured={}
local IN,line = assert(io.popen(FFMPEG..' -i "'..file ..'" -ar '..hz..' -af loudnorm=print_format=json -ac 1 -vn -f null x 2>&1', 'r'))
for line in IN:lines() do
-- print(line)
local k,v = line:match('"([^"]+)" : "([^"]+)"')
if k then
measured[k] = v
-- print(k,v)
-- elseif line:match('speed=') then -- debug
-- io.stderr:write(line)
-- io.stderr:flush()
end
end
IN:close()
io.stderr:write('\r \r')
io.stderr:flush()
loudnorm = '-af loudnorm=linear=true:'..I..':'..LRA..':'..tp..
':measured_I=' .. measured['input_i'] ..
':measured_LRA=' .. measured['input_lra'] ..
':measured_tp=' .. measured['input_tp'] ..
':measured_thresh=' .. measured['input_thresh'] ..
':offset=' .. measured['target_offset']..
' '
-- print(loudnorm)
end
local o = {
hz = hz,
stream = assert(io.popen(FFMPEG..' -i "'..file ..'" -v 0 ' ..
-- 'dynaudnorm=f=8000:c:b:s=10:m=4 ' ..
-- 'dynaudnorm=p=0.71:m=100:s=10:g=15 ' ..
-- 'dynaudnorm=p=0.71:m=6:s=10:g=15 ' ..
-- 'dynaudnorm=p=0.71:s=12:g=15:m=12:f=8000 ' ..
loudnorm ..
'-f u8 -ac 1 -ar '..hz..' -acodec pcm_u8 pipe:', POPEN_READBIN)),
vol=2.2,
size = size,
mute = '',
buf = '', -- buffer
vol = 1.9,
running = true
}
for i=1,size do o.mute = o.mute .. string.char(128) end
setmetatable(o, self)
self.__index = self
return o
end
function AUDIO:close()
self.stream:close()
end
function AUDIO:next_sample()
local buf,siz = self.buf,self.size
if buf:len()<=siz then
local t = self.stream:read(BUFFER_SIZE)
if not t then
self.running,t = false, self.mute
end
buf = buf .. t
end
local v = 0
for i=1,siz do v = v + buf:byte(i) end
self.buf,v = buf:sub(siz+1),self.vol*(v/(siz*4)-32) + 32 + math.random()
if v<0 then v=0 elseif v>63 then v=63 end
return math.floor(v)
end
-- filtre video
local FILTER = {}
function FILTER:new()
local o = {t={}}
setmetatable(o, self)
self.__index = self
return o
end
function FILTER:push(bytecode)
table.insert(self.t, bytecode)
return self
end
function FILTER:flush()
-- for i=#self.t,1,-1 do self.t[i]=nil end
-- self.t = {}
for i=FILTER_DEPTH,#self.t do table.remove(self.t,1) end
end
function FILTER:byte(offset)
local m = #self.t
if m==1 then
return self.t[1]:byte(offset)
elseif m==2 then
return math.floor(.5+(self.t[1]:byte(offset)+2*self.t[2]:byte(offset))*.3333333)
elseif m==3 then
return math.floor(.5+(self.t[1]:byte(offset)+2*self.t[2]:byte(offset)+3*self.t[3]:byte(offset))*.16666666)
else
local v,d = 0,0
for i=1,m do
local t=i
v = v + self.t[i]:byte(offset)*t
d = d + t
end
return math.floor(.5 + v/d)
end
end
-- o.filter:new{1,4,10,30,10,4,1} -- {1,2,4,2,1} -- {1,4,10,4,1} -- {1,2,6,2,1} -- {1,1,2,4,2,1,1} -- {1,2,3,6,3,2,1} -- ,2,4,8,16,32}
-- function FILTER:new()
-- local o = {map={}}
-- setmetatable(o, self)
-- local function linear(u)
-- return u<10.31475 and u/3294.6 or (((u+14.025)/269.025)^2.4)
-- end
-- local function unlinear(u)
-- return u<0 and 0 or u>1 and 255 or u<0.00313 and (u*3294.6) or ((u^(1/2.4))*269.025-14.025)
-- end
-- for i=0,80*50*3-1 do o[i] = 0 end
-- local alpha =0.6
-- for i=0,255 do
-- local x = linear(i)
-- o.map[i] = {}
-- for j=0,255 do
-- local y = linear(j)
-- local z = x*alpha + (1-alpha)*y
-- o.map[i][j] = round(unlinear(z))
-- end
-- end
-- self.__index = self
-- return o
-- end
-- function FILTER:push(bytecode)
-- local map = self.map
-- for i=1,bytecode:len() do
-- self[i] = map[self[i]][bytecode:byte(i)]
-- end
-- return self
-- end
-- function FILTER:flush()
-- end
-- function FILTER:byte(offset)
-- return self[offset]
-- end
-- flux video
local VIDEO = {}
function VIDEO:new(file, w, h, fps)
local o = {
file = file,
cpt = 1, -- compteur image
width = w,
height = h,
screen_width = 80,
screen_height = 50,
interlace = interlace,
fps = fps or 10,
image = {},
dither = nil,
expected_size = 3*h*w, -- --54 + h*(math.floor((w*3+3)/4)*4),
running=true,
input = assert(io.popen(FFMPEG..
' -i "'..file..'" -v 0 -r '..fps..
' -s '..w..'x'..h..
' -an -f rawvideo -pix_fmt rgb24 pipe:',
POPEN_READBIN))
}
setmetatable(o, self)
self.__index = self
for i=0,7999+3 do o.image[i]=0 end
o.filter = FILTER:new()
return o
end
function VIDEO:close()
if io.type(self.input)=='file' then self.input:close() end
end
function VIDEO:init_dither()
local function bayer(t)
local m=#t
local n=#t[1]
local d={}
for i=1,2*m do
d[i] = {}
for j=1,2*n do
d[i][j] = 0
end
end
for i=1,m do
for j=1,n do
local z = 4*t[i][j]
d[m*0+i][n*0+j] = z-3
d[m*1+i][n*1+j] = z-2
d[m*1+i][n*0+j] = z-1
d[m*0+i][n*1+j] = z-0
end
end
return d
end
local function vac(n,m)
math.randomseed(os.time())
local function mat(w,h)
local t={}
for i=1,h do
local r={}
for j=1,w do
table.insert(r,0)
end
table.insert(t,r)
end
t.mt={}
setmetatable(t, t.mt)
function t.mt.__tostring(t)
local s=''
for i=1,#t do
for j=1,#t[1] do
if j>1 then s=s..',' end
s = s..string.format("%9.6f",t[i][j])
end
s = s..'\n'
end
return s
end
return t
end
local function rangexy(w,h)
local l = {}
for y=1,h do
for x=1,w do
table.insert(l,{x,y})
end
end
local size = #l
for i = size, 2, -1 do
local j = math.random(i)
l[i], l[j] = l[j], l[i]
end
local i=0
return function()
i = i + 1
if i<=size then
-- print(i, l[i][1], l[i][2] )
return l[i][1], l[i][2]
else
-- print("")
end
end
end
local function makegauss(w,h)
local w2 = math.ceil(w/2)
local h2 = math.ceil(h/2)
local m = mat(w,h)
for x,y in rangexy(w, h) do
local i = ((x-1+w2)%w)-w/2
local j = ((y-1+h2)%h)-h/2
m[y][x] = math.exp(-40*(i^2+j^2)/(w*h))
end
-- print(m)
return m
end
local function countones(m)
local t=0
for _,l in ipairs(m) do
for _,x in ipairs(l) do
if x>0.5 then t=t+1 end
end
end
return t
end
local GAUSS = makegauss(n,m)
local function getminmax(m, c)
local min,max,max_x,max_y,min_x,min_y=1e38,0
local h,w = #m, #m[1]
local z = mat(w,h)
for x,y in rangexy(w,h) do
if math.abs(m[y][x]-c)<0.5 then
local t=0
for i,j in rangexy(#GAUSS[1],#GAUSS) do
if m[1+((y+j-2)%h)][1+((x+i-2)%w)]>0.5 then
t = t + GAUSS[j][i]
end
end
z[y][x] = t
if t>max then max,max_x,max_y = t,x,y end
if t<min then min,min_x,min_y = t,x,y end
end
end
-- print(m)
-- print(z)
-- print(max,max_y,max_x, c)
-- print(min,min_y,min_x)
return min_x, min_y, max_x, max_y
end
local function makeuniform(n,m)
local t = mat(n,m)
for i=0,math.floor(m*n/10) do
t[math.random(n)][math.random(m)] = 1
end
for i=1,m*n*10 do
local a1,b1,x1,y1 = getminmax(t,1)
t[y1][x1] = 0
local x2,y2,a2,b2 = getminmax(t,0)
t[y2][x2] = 1
-- print(t)
if x1==x2 and y1==y2 then break end
end
return t
end
local vnc = mat(n,m)
local m2 = mat(n,m)
local m1 = makeuniform(n,m)
local rank = countones(m1)
for x,y in rangexy(n,m) do m2[y][x] = m1[y][x] end
for r=rank,1,-1 do
local a,b,x,y = getminmax(m1,1)
m1[y][x] = 0
-- print(m1)
vnc[y][x] = r
end
for r=rank+1,n*m do
local x,y,a,b = getminmax(m2,0)
m2[y][x] = 1
-- print(m2)
vnc[y][x] = r
end
-- print(vnc)
return vnc
end
local m = {{1}}
-- m={{1,3},{3,1}}
for i=1,dither do m = bayer(m) end
if dither<0 then m = vac(-dither,-dither) end
m.w = #m
m.h = #m[1]
function m:get(i,j)
return self[1+(i % self.w)][1+(j % self.h)]
end
local x = 0
for i=1,m.w do
for j=1,m.h do
-- print(m[i][j])
x = math.max(x, m[i][j])
end
end
x = 1/(x + 1)
for i = 1,m.w do
for j = 1,m.h do
m[i][j] = m[i][j]*x
end
end
self.dither = m
end
function VIDEO:linear(u)
return u<0.04045 and u/12.92 or (((u+0.055)/1.055)^2.4)
end
function VIDEO:pset(x,y, r,g,b)
if not self._linear then
self._linear = {}
for i=0,255 do self._linear[i] = self:linear(i/255) end
self:init_dither()
self._pset = {}
self._pset[0] = {}
self._pset[1] = {}
for i=0,15 do
self._pset[0][i] = {}
self._pset[1][i] = {}
for j=0,3 do
self._pset[0][i][j] = (i%4) + 4*j
self._pset[1][i][j] = (i-(i%4)) + j
end
end
end
r,g,b = self._linear[r],self._linear[g],self._linear[b]
local d = self.dither:get(x,y)
local o,p = x%2,math.floor(x/2) + y*160
local function v(v)
-- assert(0<=v and v<=3, 'v=' .. v)
self.image[p] = self._pset[o][self.image[p]][v]
p = p+40
end
if interlace then
local q = self.cpt%2 == 0
function v(v)
if q then
self.image[p],q,p = self._pset[o][self.image[p]][v],false,p+40
else
q,p = true,p+40
end
end
end
if GRAY==1 then
if SPECIAL_4 then
r = (GRAY_R*r + GRAY_G*g + GRAY_B*b)*3 + d
if r>=2 then v(3)
elseif r>=1 then v(2)
else v(0)
end
if r>=3 then v(3)
elseif r>=2 then v(2)
elseif r>=1 then v(1)
else v(0)
end
if r>=3 then v(3)
elseif r>=2 then v(1)
else v(0)
end
else
r = (GRAY_R*r + GRAY_G*g + GRAY_B*b)*9 + d
if r>=4 then v(3)
elseif r>=2 then v(2)
elseif r>=1 then v(1)
else v(0)
end
if r>=7 then v(3)
elseif r>=5 then v(2)
elseif r>=3 then v(1)
else v(0)
end
if r>=9 then v(3)
elseif r>=8 then v(2)
elseif r>=6 then v(1)
else v(0)
end
end
else
v(math.floor(r*3 + d))
v(math.floor(g*3 + d))
v(math.floor(b*3 + d))
end
end
function VIDEO:clear()
for p=0,#self.image do self.image[p] = 0 end
end
function VIDEO:read_rgb24(raw)
self:clear()
local i,w,b,p = math.floor,self.width,FILTER.byte,self.pset
local ox = i((self.screen_width - w)/2)
local oy = i((self.screen_height - self.height)/2)
local pr = self.filter:push(raw)
for o=0,w*self.height-1 do
local x,y,o = ox+(o % w), i(o/w)+oy,o*3
p(self, x, y,
b(pr,o+1), -- r
b(pr,o+2), -- g
b(pr,o+3) -- b
)
end
self.filter:flush()
end
function VIDEO:next_image()
if not self.running then return end
self.cpt = self.cpt + 1
local buf,len = '', self.expected_size
while len>0 do
local b = self.input:read(len)
if not b then break end
buf,len = buf .. b,len - b:len()
end
-- print(self.cpt, len) io.stdout:flush()
if len==0 then
self:read_rgb24(buf)
else
self.running = false
self.input:close()
self.input = nil
end
end
function VIDEO:skip_image()
local bak = self.read_rgb24
function self:read_rgb24(raw)
self.filter:push(raw)
end
self:next_image()
self.read_rgb24 = bak
end
--------------------------------------------------
local CONVERTER = {}
function CONVERTER:new(file, out, fps)
file = file:gsub('^/cygdrive/(%w)/','%1:/')
if not exists(file) then return nil end
local o = {
file = file,
out = out,
fps = fps,
interlace = interlace
}
-- recherche la bonne taille d'image
local x,y = 80,50
local IN,line = assert(io.popen(FFMPEG..' -i "'..file ..'" 2>&1', 'r'))
for line in IN:lines() do
-- print(line)
local h,m,s = line:match('Duration: (%d+):(%d+):(%d+%.%d+),')
if h and m and s then o.duration = h*3600 + m*60 + s end
local a,b = line:match(', (%d+)x(%d+)')
if a and b then x,y=a,b end
end
IN:close()
if not o.duration then print(file..": Can't get duration!"); return nil end
-- determine aspect ratio
local max_ar
for i=2,10 do
local t = x*i/y
t = math.abs(t-round(t))
if max_ar==nil or t<max_ar then
max_ar = t
o.aspect_ratio = round(x*i/y)..':'..i
end
end
-- size of image
local w = 80
local h = round(w*y/x)
if h>50 then
h = 50
w = round(h*x/y)
end
if mode==nil then
mode = (h*w>32*48 and 'i') or 'p'
end
o.mode = mode
o.w = w
o.h = h
-- initialise la progression dans les octets de l'image
local lines,indices = {},{}
for i=math.floor((50-h)/2)*4,(math.floor((50-h)/2)+h)*4-1 do
if i%4<3 then
table.insert(lines, i)
end
end
if mode=='p' or mode=='a' then
-- rien
elseif mode=='i' then
local t = {}
for i=1,#lines,2 do
table.insert(t, lines[i])
end
for i=2,#lines,2 do
table.insert(t, lines[i])
end
lines = t
elseif mode=='2' then
local t = {}
for i=1,#lines,3 do
table.insert(t, lines[i])
end
for i=2,#lines,3 do
table.insert(t, lines[i])
end
table.sort(t)
for i=3,#lines,3 do
table.insert(t, lines[i])
end
lines = t
elseif mode=='3' then
local t = {}
for i=2,#lines,3 do
table.insert(t, lines[i])
end
for i=1,#lines,3 do
table.insert(t, lines[i])
end
for i=2,#lines,3 do
table.insert(t, lines[i])
end
lines = t
elseif mode=='I' then
local t = {}
for i=1,#lines,6 do
table.insert(t, lines[i])
table.insert(t, lines[i+1])
table.insert(t, lines[i+2])
end
for i=4,#lines,6 do
table.insert(t, lines[i])
table.insert(t, lines[i+1])
table.insert(t, lines[i+2])
end
lines = t
elseif mode=='ipi' then
local t = {}
for i=1+math.floor(#lines/3),math.floor(2*#lines/3) do
table.insert(t, lines[i])
end
for i=1,math.floor(#lines/3),2 do
table.insert(t, lines[i])
end
for i=1+math.floor(2*#lines/3),#lines,2 do
table.insert(t, lines[i])
end
for i=2,math.floor(#lines/3),2 do
table.insert(t, lines[i])
end
for i=2+math.floor(2*#lines/3),#lines,2 do
table.insert(t, lines[i])
end
lines = t
elseif mode=='r' then
local size = #lines
for i = size, 1, -1 do
local rand = math.random(size)
lines[i], lines[rand] = lines[rand], lines[i]
end
elseif mode=='3i' then
-- 14 + 14+12
local w = 2+4+4+4+4+4
local x = math.floor((39-w)/2)
for _,j in ipairs(lines) do
for i=j*40+x,j*40+x+w-1 do
table.insert(indices,i)
end
end
for j=1,#lines,2 do
for i=lines[j]*40,lines[j]*40+x-1 do
table.insert(indices,i)
end
for i=lines[j]*40+x+w,lines[j]*40+39 do
table.insert(indices,i)
end
end
for j=2,#lines,2 do
for i=lines[j]*40,lines[j]*40+x-1 do
table.insert(indices,i)
end
for i=lines[j]*40+x+w,lines[j]*40+39 do
table.insert(indices,i)
end
end
lines = {}
elseif mode=='d' then
lines = {}
for x=0,39 do
for y=0,math.min(24,x) do
local p = y*320+(x-y)
for j=p,p+319,40 do
table.insert(indices,j)
end
end
end
for y=1,24 do
for x=0,24-y do
local p = (y+x)*320+39-x
for j=p,p+319,40 do
table.insert(indices,j)
end
end
end
else
error('Unknown mode: ' .. mode)
end
for _,i in ipairs(lines) do
-- print(i)
for j=i*40,i*40+39 do
table.insert(indices, j)
end
end
o.indices = indices
setmetatable(o, self)
self.__index = self
return o
end
function CONVERTER:_new_video()
return VIDEO:new(self.file, self.w, self.h, self.fps)
end
function CONVERTER:_stat()
io.stdout:write(self.file..'\n')
io.stdout:flush()
-- auto determination des parametres
local stat = VIDEO:new(self.file,self.w,self.h,5)
stat.super_pset = stat.pset
stat.histo = {n=0}; for i=0,255 do stat.histo[i]=0 end
function stat:pset(x,y, r,g,b)
local h=self.histo
h[r] = h[r]+1
h[g] = h[g]+1
h[b] = h[b]+1
self:super_pset(x,y,r,g,b)
if GRAY==nil then
if self.mnt==nil then
self.mnt = {n=0,r1=0,g1=0,b1=0,r2=0,g2=0,b2=0}
end
local m = math.max(r,g,b)
if m>10 then
m=1/m
r,g,b = r*m,g*m,b*m
m = self.mnt
m.n = m.n + 1
m.r1 = m.r1 + r
m.g1 = m.g1 + g
m.b1 = m.b1 + b
m.r2 = m.r2 + r*r
m.g2 = m.g2 + g*g
m.b2 = m.b2 + b*b
end
end
end
stat.super_next_image = stat.next_image
stat.mill = {'|', '/', '-', '\\'}
stat.mill[0] = stat.mill[4]
local indices,duration = self.indices,self.duration
function stat:next_image()
self:super_next_image()
io.stderr:write(string.format('> analyzing video...%s %d%%\r', self.mill[self.cpt % 4], percent(self.cpt/self.fps/duration)))
io.stderr:flush()
end
stat.trames = 0
stat.prev_img = {}
for i=0,7999 do stat.prev_img[i]=-1 end
function stat:count_trames()
local pos,prev,curr,k = 0,stat.prev_img,stat.image
for _,i in ipairs(indices) do
if prev[i] ~= curr[i] then
stat.trames,k = stat.trames + 1,i-pos
if k<0 then
prev[i],pos = curr[i],i+1
elseif k<=2 then
prev[pos],prev[pos+1],prev[pos+2],prev[pos+3],pos =
curr[pos],curr[pos+1],curr[pos+2],curr[pos+3],pos+4
elseif k<=256 then
prev[i],prev[i+1],pos = curr[i],curr[i+1],i+2
else
prev[i],pos = curr[i],i+1
end
end
end
end
while stat.running do
stat:next_image()
stat:count_trames()
end
io.stderr:write(string.rep(' ',79)..'\r')
io.stderr:flush()
-- determine if monochrome
if GRAY==nil then
local m = stat.mnt
m.r1,m.g1,m.b1 = m.r1/m.n,m.g1/m.n,m.b1/m.n
m.r2,m.g2,m.b2 = m.r2/m.n,m.g2/m.n,m.b2/m.n
local e = 0
e = e + math.sqrt(m.r2 - m.r1*m.r1)
e = e + math.sqrt(m.g2 - m.g1*m.g1)
e = e + math.sqrt(m.b2 - m.b1*m.b1)
e = e/3
GRAY = e<GRAY_THR and 1 or 0
end
local neg_fps = self.fps<0
self.fps = math.abs(self.fps)
local max_trames = 1000000/(self.fps*CYCLES)
local avg_trames = (stat.trames/stat.cpt) * 1.03 -- 001 -- 0.11% safety margin
local ratio = max_trames / avg_trames
if neg_fps and self.fps>FPS_MAX then
self.fps = FPS_MAX
elseif ratio>1 or neg_fps then
self.fps = math.min(math.floor(self.fps*ratio),self.interlace and 2*FPS_MAX or FPS_MAX)
elseif ratio<1 then
local zoom = ratio^.5
self.w=math.floor(self.w*zoom)
self.h=math.floor(self.h*zoom)
end
stat.total = 0
for i=1,255 do
stat.total = stat.total + stat.histo[i]
end
stat.threshold_min = (GRAY==1 and .03 or .05)*stat.total
stat.min = 0
for i=1,255 do
stat.min = stat.min + stat.histo[i]
if stat.min>stat.threshold_min then
stat.min = i-1
break
end
end
stat.max = 0
stat.threshold_max = (GRAY==1 and .03 or .03)*stat.total
for i=254,1,-1 do
stat.max = stat.max + stat.histo[i]
if stat.max>stat.threshold_max then
stat.max = i+1
break
end
end
-- print(stat.min, stat.max)
-- io.stdout:flush()
self.video_cor = {stat.min, 255/(stat.max - stat.min)}
-- info
io.stdout:write(string.format('> %dx%d %s (%s) %s at %d fps (%d%% zoom, %s)\n',
self.w, self.h, mode,self.aspect_ratio,hms(duration, "%dh %dm %ds"), self.fps,
percent(math.max(self.w/80,self.h/50)), GRAY==1 and "gray" or "color"))
io.stdout:flush()
end
function CONVERTER:process()
-- collect stats
self:_stat()
-- flux audio/video
local audio = AUDIO:new(self.file)
local video = self:_new_video()
-- adaptation luminosité
-- print(self.video_cor[1],self.video_cor[2])
if self.video_cor[1]~=0 or self.video_cor[2]~=1 then
local cor = self.video_cor
local super_pset = video.pset
function video:pset(x,y, r,g,b)
local function f(x)
x = round((x-cor[1])*cor[2]);