-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
716 lines (687 loc) · 31.4 KB
/
Form1.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using NAudio.Wave;
using AviFile;
using LibMobiclip.Utils;
using LibMobiclip.Containers.Moflex;
using LibMobiclip.Codec;
using LibMobiclip.Containers.Mods;
using LibMobiclip.Containers.Vx;
using LibMobiclip.Codec.Sx;
using LibMobiclip.Codec.FastAudio;
using LibMobiclip.Codec.Mobiclip.Encoder;
using LibMobiclip.Codec.Majesco;
namespace MobiclipDecoder
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread MobiThread = null;
volatile bool StopThread = false;
private unsafe void Form1_Load(object sender, EventArgs e)
{
/*Console.WriteLine("private byte[] REV_byte_1165C4 = {");
for (int i = 0; i < byte_1165C4.Length; i++)
{
Console.Write("{0}, ", Array.IndexOf<byte>(byte_1165C4, (byte)i));
}
Console.WriteLine("};");
return;*/
/*Console.WriteLine("private int[,,] VxTable0_A_Ref_Simple = {");
for (int i = 0; i < 32; i++)
{
Console.Write("{");
for (int j = 0; j < 64; j++)
{
Console.Write("{");
for (int k = 0; k < 2; k++)
{
ushort search = (ushort)((i << 4) | (j << 9) | (k << 15));
int idx = Array.FindIndex(LibMobiclip.Codec.Mobiclip.MobiConst.Vx2Table0_A, (a) => (a & 0xFFF0) == search);
Console.Write("{0}", idx);
if (k < 1) Console.Write(", ");
}
Console.Write("}");
if (j < 63) Console.Write(", ");
}
Console.WriteLine("}, ");
}
Console.WriteLine("};");*/
//tempoarly use a memorystream
//Use a filestream later on
/*AviManager m = new AviManager(@"d:\Projects\DS\Moflex\Test\TestVid.avi", true);
VideoStream ss = m.GetVideoStream();
FileStream fs = File.Create(@"d:\Projects\DS\Moflex\Test\bw_TestVid_new_short_25.moflex");
int scale = 1;
double rate = Math.Round(ss.FrameRate, 3);
while ((rate % 1.0) != 0)
{
scale *= 10;
rate *= 10;
}
ss.GetFrameOpen();
MoflexSimpleVideoMuxer mux = new MoflexSimpleVideoMuxer(fs, ss.GetBitmap(0), (int)rate, scale);
for (int i = 1; i < ss.CountFrames / 4; i++)
{
mux.AddFrame(ss.GetBitmap(i));
}
ss.GetFrameClose();
mux.FinalizeMoflex();
fs.Close();
return;*/
//Write a custom video file:
/*AviManager m = new AviManager(@"d:\Projects\DS\CW\VX2Player\resources\TestVid6_20fps.avi", true);
AudioStream ass = m.GetWaveStream();
Avi.AVISTREAMINFO streaminfo = new Avi.AVISTREAMINFO();
Avi.PCMWAVEFORMAT waveformat = new Avi.PCMWAVEFORMAT();
int strlength = 0;
IntPtr wavedata = ass.GetStreamData(ref streaminfo, ref waveformat, ref strlength);
short* wavedataptr = (short*)wavedata;
VideoStream ss = m.GetVideoStream();
FileStream fs = File.Create(@"d:\Projects\DS\CW\VX2Player\files\data\testvid.vx2");
ss.GetFrameOpen();
int bitrate = 1500000;
int framerate = (int)Math.Round(ss.FrameRate);
MobiEncoder mEncoder = new MobiEncoder(12, ss.Width, ss.Height, bitrate / framerate);
int audiosize = 32768;//32768 * 10 / 20;//16;
for (int i = 0; i < ss.CountFrames; i++)
{
if ((i % framerate) == 0)
{
short[] data3 = new short[audiosize];
for (int i2 = 0; i2 < audiosize; i2++)
{
data3[i2] = *wavedataptr++;
wavedataptr++;
}
byte[] data2 = new byte[audiosize * 2];
IOUtil.WriteS16sLE(data2, 0, data3);
fs.Write(data2, 0, data2.Length);
}
byte[] data = mEncoder.EncodeFrame(ss.GetBitmap(i));
uint len = (uint)data.Length;
while(((fs.Position + len + 4) % 512) != 0) len++;
byte[] length = new byte[4];
IOUtil.WriteU32LE(length, 0, len);
fs.Write(length, 0, 4);
fs.Write(data, 0, data.Length);
if (len > data.Length) fs.Write(new byte[len - data.Length], 0, (int)len - data.Length);
}
ss.GetFrameClose();
fs.Flush();
fs.Close();
return;*/
/* MemoryStream m = new MemoryStream();
MoflexMuxer mux = new MoflexMuxer(m);
mux.WriteSynchroHeader();
mux.WriteSynchroChunk(new MoLiveStreamVideo(25, 1, 400, 240, 1, 1));
mux.WriteSynchroChunk(null);
byte[] data = MobiclipEncoder.Encode(new Bitmap(new MemoryStream(File.ReadAllBytes(@"d:\Projects\DS\Moflex\TestPic3.png"))));
if (data.Length <= (0x1000 - 0x80))//save margin
{
mux.WriteDataBlock();
mux.WriteEp(0, data, 0, data.Length, true);
mux.WriteEp(0, null, 0, 0);
}
else
{
int pos = 0;
int left = data.Length;
while (left >= (0x1000 - 0x80))
{
mux.WriteDataBlock();
mux.WriteEp(0, data, pos, 0x1000 - 0x80, left == (0x1000 - 0x80));
mux.WriteEp(0, null, 0, 0);
pos += 0x1000 - 0x80;
left -= 0x1000 - 0x80;
}
if (left > 0)
{
mux.WriteDataBlock();
mux.WriteEp(0, data, pos, left, true);
mux.WriteEp(0, null, 0, 0);
}
}
m.Write(new byte[0x1000], 0, 0x1000);
byte[] res = m.ToArray();
m.Close();
File.Create(@"d:\Projects\DS\Moflex\TestPic3.moflex").Close();
File.WriteAllBytes(@"d:\Projects\DS\Moflex\TestPic3.moflex", res);*/
//return;
/*byte[] data = File.ReadAllBytes(@"d:\Temp\ev110.sfd");
int offs = 0;//0x01D718 + 4;// 70 16 02 00;
while (true)
{
AsmData d = new AsmData(160, 120, AsmData.MobiclipVersion.Moflex3DS);
d.Data = data;
d.Offset = offs++;
Bitmap b = d.MobiclipUnpack_0_0();
if (b != null)
{
//offs = d.Offset;// +4;
}
}*/
/* var d = new MoLiveDemux(new MemoryStream(File.ReadAllBytes(@"d:\Old\Temp\3DS Files\MK7\Mobi\CourseSelectRace\CourseSelectRace.moflex")));
uint res;
while ((res = d.ReadPacket()) == 0) ;
d.s2.Flush();
d.s2.Close();*/
string[] files = Directory.GetFiles(".", "*.mo", SearchOption.AllDirectories);
foreach (string file in files) {
MobiThread = new Thread((ParameterizedThreadStart)MOC5ThreadMain);
MobiThread.Start(file);
}
}
private void VX2ThreadMain(Object Args)
{
AudioBuffer = new BufferedWaveProvider(new WaveFormat((int)32768, 16, 1));
AudioBuffer.DiscardOnBufferOverflow = true;
AudioBuffer.BufferLength = 1024 * 512;
Player = new WaveOut();
Player.DesiredLatency = 150;
Player.Init(AudioBuffer);
Player.Play();
FileStream fs = File.OpenRead((String)Args);
Invoke((Action)delegate { ClientSize = new Size(256, 192); });
double fps = 25;
TimeSpan ts = TimeSpan.FromMilliseconds(1000d / (double)(fps));
LibMobiclip.Codec.Mobiclip.MobiclipDecoder d = new LibMobiclip.Codec.Mobiclip.MobiclipDecoder(256, 192, LibMobiclip.Codec.Mobiclip.MobiclipDecoder.MobiclipVersion.Moflex3DS);
int frame = 0;
while (!StopThread)
{
if (fs.Position >= fs.Length)
{
fs.Close();
Application.Exit();
break;
}
if ((frame % 20) == 0)//Audio
{
byte[] adata = new byte[32768 * 2];
fs.Read(adata, 0, 32768 * 2);
AudioBuffer.AddSamples(adata, 0, adata.Length);
}
int length = (fs.ReadByte() << 0) | (fs.ReadByte() << 8) | (fs.ReadByte() << 16) | (fs.ReadByte() << 24);
byte[] data = new byte[length];
fs.Read(data, 0, length);
frame++;
d.Data = data;
d.Offset = 0;
Bitmap b = d.DecodeFrame();
if (lastval != 0)
{
while ((s.Value - lastval) < (long)(ts.TotalSeconds * s.Frequency)) ;
}
lastval = s.Value;
try
{
pictureBox1.BeginInvoke((Action)delegate
{
pictureBox1.Image = b;
pictureBox1.Invalidate();
});
}
catch { }
}
fs.Close();
}
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(j = 0; j < encoders.Length; ++j)
{
if(encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
private void MOC5ThreadMain(Object Args)
{
byte[] data = File.ReadAllBytes((String)Args);
int offs = (int)IOUtil.ReadU32LE(data, 0x4) + 8;//(int)IOUtil.ReadU32LE(data, 0xD8);
uint width = IOUtil.ReadU32LE(data, 0x1C);
uint height = IOUtil.ReadU32LE(data, 0x20);
Invoke((Action)delegate { ClientSize = new Size((int)width, (int)height); });
double fps = IOUtil.ReadU32LE(data, 0xC) / 128d;
uint i = 1;
TimeSpan ts = TimeSpan.FromMilliseconds(1000d / (double)(fps));
LibMobiclip.Codec.Mobiclip.MobiclipDecoder d = new LibMobiclip.Codec.Mobiclip.MobiclipDecoder(width, height, LibMobiclip.Codec.Mobiclip.MobiclipDecoder.MobiclipVersion.Moflex3DS);
d.Data = data;
ImageCodecInfo myImageCodecInfo;
System.Drawing.Imaging.Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
myImageCodecInfo = GetEncoderInfo("image/png");
myEncoder = System.Drawing.Imaging.Encoder.Quality;
myEncoderParameters = new EncoderParameters(1);
myEncoderParameter = new EncoderParameter(myEncoder, 1L);
myEncoderParameters.Param[0] = myEncoderParameter;
while (!StopThread)
{
if (offs >= data.Length)
{
Application.Exit();
break;
}
uint blocksize = IOUtil.ReadU32LE(data, offs);
d.Offset = offs + 8;
Bitmap b = d.DecodeFrame();
/*if (lastval != 0)
{
while ((s.Value - lastval) < (long)(ts.TotalSeconds * s.Frequency)) ;
}
lastval = s.Value;*/
try
{
b.Save(String.Concat(i.ToString(), ".png"), myImageCodecInfo, myEncoderParameters);
/*pictureBox1.BeginInvoke((Action)delegate
{
pictureBox1.Image = b;
pictureBox1.Invalidate();
});*/
}
catch { }
offs += 4 + (int)(blocksize & ~1);
while ((offs % 4) != 0) offs++;
i++;
}
Process modump = new Process();
modump.StartInfo.FileName = System.IO.Path.GetFullPath($"MoDump.exe");
modump.StartInfo.Arguments = $@"""{(String)Args}"" -CheckFrames -AudioDump 1.wav";
modump.Start();
modump.WaitForExit();
Process ff = new Process();
ff.StartInfo.FileName = System.IO.Path.GetFullPath($"ffmpeg.exe");
ff.StartInfo.Arguments = $@"-r ""{fps.ToString()}""/1 -f image2 -s 256x192 -i %d.png -i 1.wav -crf 1 -pix_fmt yuv420p ""{Args.ToString().Replace(".mo", ".mp4")}""";
ff.Start();
ff.WaitForExit();
var dir = new DirectoryInfo(".");
foreach (var file in dir.EnumerateFiles("*.png")) {
file.Delete();
}
foreach (var file in dir.EnumerateFiles("1.wav")) {
file.Delete();
}
}
private void ModsThreadMain(Object Args)
{
byte[] data = File.ReadAllBytes((String)Args);
ModsDemuxer dm = new ModsDemuxer(new MemoryStream(data));
if ((dm.Header.AudioCodec == 1 || dm.Header.AudioCodec == 2 || dm.Header.AudioCodec == 3) && dm.Header.NbChannel > 0 && dm.Header.Frequency > 0)
{
AudioBuffer = new BufferedWaveProvider(new WaveFormat((int)dm.Header.Frequency, 16, dm.Header.NbChannel));
AudioBuffer.DiscardOnBufferOverflow = true;
AudioBuffer.BufferLength = 1024 * 512;
Player = new WaveOut();
Player.DesiredLatency = 150;
Player.Init(AudioBuffer);
Player.Play();
}
Invoke((Action)delegate { ClientSize = new Size((int)dm.Header.Width, (int)dm.Header.Height); });
LibMobiclip.Codec.Mobiclip.MobiclipDecoder d = new LibMobiclip.Codec.Mobiclip.MobiclipDecoder(dm.Header.Width, dm.Header.Height, LibMobiclip.Codec.Mobiclip.MobiclipDecoder.MobiclipVersion.ModsDS);
TimeSpan ts = TimeSpan.FromMilliseconds(1000d / (double)(dm.Header.Fps / (double)0x01000000));
int CurChannel = 0;
List<short>[] channels = new List<short>[dm.Header.NbChannel];
IMAADPCMDecoder[] decoders = new IMAADPCMDecoder[dm.Header.NbChannel];
SxDecoder[] sxd = new SxDecoder[dm.Header.NbChannel];
FastAudioDecoder[] fad = new FastAudioDecoder[dm.Header.NbChannel];
bool[] isinit = new bool[dm.Header.NbChannel];
for (int i = 0; i < dm.Header.NbChannel; i++)
{
channels[i] = new List<short>();
decoders[i] = new IMAADPCMDecoder();
sxd[i] = new SxDecoder();
fad[i] = new FastAudioDecoder();
isinit[i] = false;
}
while (!StopThread)
{
uint NrAudioPackets;
bool IsKeyFrame;
byte[] framedata = dm.ReadFrame(out NrAudioPackets, out IsKeyFrame);
if (framedata == null) break;
d.Data = framedata;
d.Offset = 0;
Bitmap b = d.DecodeFrame();
if (NrAudioPackets > 0 && AudioBuffer != null)
{
int Offset = d.Offset - 2;
if (dm.Header.TagId == 0x334E && (IOUtil.ReadU16LE(framedata, 0) & 0x8000) != 0)
Offset += 4;
if (dm.Header.AudioCodec == 3)
{
if (IsKeyFrame)
{
for (int i = 0; i < dm.Header.NbChannel; i++)
{
channels[i] = new List<short>();
decoders[i] = new IMAADPCMDecoder();
sxd[i] = new SxDecoder();
isinit[i] = false;
}
}
for (int i = 0; i < NrAudioPackets; i++)
{
channels[CurChannel].AddRange(decoders[CurChannel].GetWaveData(framedata, Offset, 128 + (!isinit[CurChannel] ? 4 : 0)));
Offset += 128 + (!isinit[CurChannel] ? 4 : 0);
isinit[CurChannel] = true;
CurChannel++;
if (CurChannel >= dm.Header.NbChannel) CurChannel = 0;
}
}
else if (dm.Header.AudioCodec == 1)
{
for (int i = 0; i < NrAudioPackets; i++)
{
if (!isinit[CurChannel]) sxd[CurChannel].Codebook = dm.AudioCodebooks[CurChannel];
isinit[CurChannel] = true;
sxd[CurChannel].Data = framedata;
sxd[CurChannel].Offset = Offset;
channels[CurChannel].AddRange(sxd[CurChannel].Decode());
Offset = sxd[CurChannel].Offset;
CurChannel++;
if (CurChannel >= dm.Header.NbChannel) CurChannel = 0;
}
}
else if (dm.Header.AudioCodec == 2)
{
for (int i = 0; i < NrAudioPackets; i++)
{
fad[CurChannel].Data = framedata;
fad[CurChannel].Offset = Offset;
channels[CurChannel].AddRange(fad[CurChannel].Decode());
Offset = fad[CurChannel].Offset;
CurChannel++;
if (CurChannel >= dm.Header.NbChannel) CurChannel = 0;
}
}
int smallest = int.MaxValue;
for (int i = 0; i < dm.Header.NbChannel; i++)
{
if (channels[i].Count < smallest) smallest = channels[i].Count;
}
if (smallest > 0)
{
//Gather samples
short[][] samps = new short[dm.Header.NbChannel][];
for (int i = 0; i < dm.Header.NbChannel; i++)
{
samps[i] = new short[smallest];
channels[i].CopyTo(0, samps[i], 0, smallest);
channels[i].RemoveRange(0, smallest);
}
byte[] result = InterleaveChannels(samps);
AudioBuffer.AddSamples(result, 0, result.Length);
}
}
if (lastval != 0)
{
while ((s.Value - lastval) < (long)(ts.TotalSeconds * s.Frequency)) ;
}
lastval = s.Value;
try
{
pictureBox1.BeginInvoke((Action)delegate
{
pictureBox1.Image = b;
pictureBox1.Invalidate();
});
}
catch { }
}
}
private void VxThreadMain(Object Args)
{
byte[] data = File.ReadAllBytes((String)Args);
MemoryStream m = new MemoryStream(data);
VxDemuxer dm = new VxDemuxer(m);
Invoke((Action)delegate { ClientSize = new Size((int)dm.Header.Width, (int)dm.Header.Height); });
LibMobiclip.Codec.Mobiclip.MobiclipDecoder d = new LibMobiclip.Codec.Mobiclip.MobiclipDecoder(dm.Header.Width, dm.Header.Height, LibMobiclip.Codec.Mobiclip.MobiclipDecoder.MobiclipVersion.VxDS);
TimeSpan ts = TimeSpan.FromMilliseconds(1000d / (double)(dm.Header.Fps / (double)0x01000000));
while (!StopThread)
{
uint NrAudioPackets;
byte[] framedata = new byte[dm.Header.BiggestFrame];//dm.ReadFrame(out NrAudioPackets);
m.Read(framedata, 0, (int)dm.Header.BiggestFrame);
d.Data = framedata;
d.Offset = 0;
Bitmap b = d.DecodeFrame();
if (lastval != 0)
{
while ((s.Value - lastval) < (long)(ts.TotalSeconds * s.Frequency)) ;
}
lastval = s.Value;
try
{
pictureBox1.BeginInvoke((Action)delegate
{
pictureBox1.Image = b;
pictureBox1.Invalidate();
});
}
catch { }
}
}
LibMobiclip.Codec.Mobiclip.MobiclipDecoder ddd;
bool left = false;
private void MoflexThreadMain(Object Args)
{
FileStream s = File.OpenRead((String)Args);
var d = new MoLiveDemux(s);//@"d:\Old\Temp\3DS Files\Moflex Audio\law_end(3d).moflex")));
d.OnCompleteFrameReceived += new MoLiveDemux.CompleteFrameReceivedEventHandler(d_OnCompleteFrameReceived);
bool left = false;
while (!StopThread)
{
d.ReadPacket();
}
s.Close();
}
Bitmap lastleft = null;
BufferedWaveProvider AudioBuffer = null;
WaveOut Player = null;
HiResTimer s = new HiResTimer();
long lastval = 0;
bool sizeset = false;
int PlayingVideoStream = -1;
bool Is3D = false;
FastAudioDecoder[] mFastAudioDecoders;
void d_OnCompleteFrameReceived(MoLiveChunk Chunk, byte[] Data)
{
if ((Chunk is MoLiveStreamVideo || Chunk is MoLiveStreamVideoWithLayout) && ((PlayingVideoStream == -1) || ((MoLiveStream)Chunk).StreamIndex == PlayingVideoStream))
{
if (!sizeset) Invoke((Action)delegate { ClientSize = new Size((int)((MoLiveStreamVideo)Chunk).Width, (int)((MoLiveStreamVideo)Chunk).Height); });
sizeset = true;
left = !left;
if (ddd == null)
{
ddd = new LibMobiclip.Codec.Mobiclip.MobiclipDecoder(((MoLiveStreamVideo)Chunk).Width, ((MoLiveStreamVideo)Chunk).Height, LibMobiclip.Codec.Mobiclip.MobiclipDecoder.MobiclipVersion.Moflex3DS);
PlayingVideoStream = ((MoLiveStream)Chunk).StreamIndex;
if (!(Chunk is MoLiveStreamVideoWithLayout)) Is3D = false;
else if (((MoLiveStreamVideoWithLayout)Chunk).ImageLayout == MoLiveStreamVideoWithLayout.VideoLayout.Simple2D) Is3D = false;
else Is3D = true;
}
ddd.Data = Data;
ddd.Offset = 0;
Bitmap b = ddd.DecodeFrame();
if (!Is3D || left)
{
TimeSpan ts = TimeSpan.FromMilliseconds((!Is3D ? 1000d : 2000d) / ((double)((MoLiveStreamVideo)Chunk).FpsRate / (double)((MoLiveStreamVideo)Chunk).FpsScale));
if (lastval != 0)
{
while ((s.Value - lastval) < (long)(ts.TotalSeconds * s.Frequency)) ;//milliseconds) ;
}
lastval = s.Value;
try
{
pictureBox1.BeginInvoke((Action)delegate
{
pictureBox1.Image = b;
pictureBox1.Invalidate();
});
}
catch { }
}
}
else if (Chunk is MoLiveStreamAudio)
{
if (AudioBuffer == null)
{
AudioBuffer = new BufferedWaveProvider(new WaveFormat((int)((MoLiveStreamAudio)Chunk).Frequency, 16, (int)((MoLiveStreamAudio)Chunk).Channel));
AudioBuffer.DiscardOnBufferOverflow = true;
AudioBuffer.BufferLength = 1024 * 512;
Player = new WaveOut();
Player.DesiredLatency = 150;
Player.Init(AudioBuffer);
Player.Play();
}
switch ((int)((MoLiveStreamAudio)Chunk).CodecId)
{
case 0://fastaudio
{
if (mFastAudioDecoders == null)
{
mFastAudioDecoders = new FastAudioDecoder[(int)((MoLiveStreamAudio)Chunk).Channel];
for (int i = 0; i < (int)((MoLiveStreamAudio)Chunk).Channel; i++)
{
mFastAudioDecoders[i] = new FastAudioDecoder();
}
}
List<short>[] channels = new List<short>[(int)((MoLiveStreamAudio)Chunk).Channel];
for (int i = 0; i < (int)((MoLiveStreamAudio)Chunk).Channel; i++)
{
channels[i] = new List<short>();
}
int offset = 0;
int size = 40;
while (offset + size < Data.Length)
{
for (int i = 0; i < (int)((MoLiveStreamAudio)Chunk).Channel; i++)
{
mFastAudioDecoders[i].Data = Data;
mFastAudioDecoders[i].Offset = offset;
channels[i].AddRange(mFastAudioDecoders[i].Decode());
offset = mFastAudioDecoders[i].Offset;
}
}
short[][] channelsresult = new short[(int)((MoLiveStreamAudio)Chunk).Channel][];
for (int i = 0; i < (int)((MoLiveStreamAudio)Chunk).Channel; i++)
{
channelsresult[i] = channels[i].ToArray();
}
byte[] result = InterleaveChannels(channelsresult);
AudioBuffer.AddSamples(result, 0, result.Length);
}
break;
case 1://IMA-ADPCM
{
IMAADPCMDecoder[] decoders = new IMAADPCMDecoder[(int)((MoLiveStreamAudio)Chunk).Channel];
List<short>[] channels = new List<short>[(int)((MoLiveStreamAudio)Chunk).Channel];
for (int i = 0; i < (int)((MoLiveStreamAudio)Chunk).Channel; i++)
{
decoders[i] = new IMAADPCMDecoder();
decoders[i].GetWaveData(Data, 4 * i, 4);
channels[i] = new List<short>();
}
int offset = 4 * (int)((MoLiveStreamAudio)Chunk).Channel;
int size = 128 * (int)((MoLiveStreamAudio)Chunk).Channel;
while (offset + size < Data.Length)
{
for (int i = 0; i < (int)((MoLiveStreamAudio)Chunk).Channel; i++)
{
channels[i].AddRange(decoders[i].GetWaveData(Data, offset, 128));
offset += 128;
}
}
short[][] channelsresult = new short[(int)((MoLiveStreamAudio)Chunk).Channel][];
for (int i = 0; i < (int)((MoLiveStreamAudio)Chunk).Channel; i++)
{
channelsresult[i] = channels[i].ToArray();
}
byte[] result = InterleaveChannels(channelsresult);
AudioBuffer.AddSamples(result, 0, result.Length);
}
break;
case 2://PCM16
{
AudioBuffer.AddSamples(Data, 0, Data.Length - (Data.Length % ((int)((MoLiveStreamAudio)Chunk).Channel * 2)));
}
break;
}
}
}
private byte[] InterleaveChannels(params Int16[][] Channels)
{
if (Channels.Length == 0) return new byte[0];
byte[] Result = new byte[Channels[0].Length * Channels.Length * 2];
for (int i = 0; i < Channels[0].Length; i++)
{
for (int j = 0; j < Channels.Length; j++)
{
Result[i * 2 * Channels.Length + j * 2] = (byte)(Channels[j][i] & 0xFF);
Result[i * 2 * Channels.Length + j * 2 + 1] = (byte)(Channels[j][i] >> 8);
}
}
return Result;
}
private unsafe Bitmap CreateAnaglyph(Bitmap Left, Bitmap Right)
{
if (Left == null || Right == null) return null;
Bitmap result = new Bitmap(Left.Width, Left.Height);
BitmapData dres = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
BitmapData dl = Left.LockBits(new Rectangle(0, 0, Left.Width, Left.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData dr = Right.LockBits(new Rectangle(0, 0, Right.Width, Right.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
for (int y = 0; y < result.Height; y++)
{
for (int x = 0; x < result.Width; x++)
{
Color left = Color.FromArgb(((int*)(((byte*)dl.Scan0) + y * dl.Stride + x * 4))[0]);
Color right = Color.FromArgb(((int*)(((byte*)dr.Scan0) + y * dr.Stride + x * 4))[0]);
((int*)(((byte*)dres.Scan0) + y * dres.Stride + x * 4))[0] = Color.FromArgb(
left.R,
right.G,
right.B).ToArgb();
}
}
Left.UnlockBits(dl);
Right.UnlockBits(dr);
result.UnlockBits(dres);
return result;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MobiThread != null && MobiThread.IsAlive)
{
StopThread = true;
}
if (AudioBuffer != null)
{
Player.Stop();
Player.Dispose();
Player = null;
AudioBuffer = null;
}
MobiThread = null;
}
private void Form1_Shown(object sender, EventArgs e)
{
TopMost = true;
TopMost = false;
}
}
}