-
Notifications
You must be signed in to change notification settings - Fork 0
/
pl_mpeg.go
3073 lines (3053 loc) · 129 KB
/
pl_mpeg.go
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
// Code generated by go generate; DO NOT EDIT.
// PL_MPEG Copywrite Dominic Szablewski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files(the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions :
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package mpg
import (
"github.com/gotranspile/cxgo/runtime/cmath"
"github.com/gotranspile/cxgo/runtime/libc"
"github.com/gotranspile/cxgo/runtime/stdio"
"math"
"unsafe"
)
const plm_packet_invalid_ts1 = -1
const plm_audio_samples_per_frame = 1152
const plm_buffer_default_size = 0x20000
const _true = 1
const _false = 0
type plm_t struct {
Demux *plm_demux_t
Time float64
Has_ended int64
Loop int64
Has_decoders int64
Video_enabled int64
Video_packet_type int64
Video_buffer *plm_buffer_t
Video_decoder *plm_video_t
Audio_enabled int64
Audio_stream_index int64
Audio_packet_type int64
Audio_lead_time float64
Audio_buffer *plm_buffer_t
Audio_decoder *plm_audio_t
Video_decode_callback plm_video_decode_callback
Video_decode_callback_user_data unsafe.Pointer
Audio_decode_callback plm_audio_decode_callback
Audio_decode_callback_user_data unsafe.Pointer
}
type plm_buffer_t struct {
Bit_index uint64
Capacity uint64
Length uint64
Total_size uint64
Discard_read_bytes int64
Has_ended int64
Free_when_done int64
Close_when_done int64
Fh *stdio.File
Load_callback plm_buffer_load_callback
Load_callback_user_data unsafe.Pointer
Bytes *uint8
Mode plm_buffer_mode
}
type plm_demux_t struct {
Buffer *plm_buffer_t
Destroy_buffer_when_done int64
System_clock_ref float64
Last_file_size uint64
Last_decoded_pts float64
Start_time float64
Duration float64
Start_code int64
Has_pack_header int64
Has_system_header int64
Has_headers int64
Num_audio_streams int64
Num_video_streams int64
Current_packet plm_packet_t
Next_packet plm_packet_t
}
type plm_video_t struct {
Framerate float64
Time float64
Frames_decoded int64
Width int64
Height int64
Mb_width int64
Mb_height int64
Mb_size int64
Luma_width int64
Luma_height int64
Chroma_width int64
Chroma_height int64
Start_code int64
Picture_type int64
Motion_forward plm_video_motion_t
Motion_backward plm_video_motion_t
Has_sequence_header int64
Quantizer_scale int64
Slice_begin int64
Macroblock_address int64
Mb_row int64
Mb_col int64
Macroblock_type int64
Macroblock_intra int64
Dc_predictor [3]int64
Buffer *plm_buffer_t
Destroy_buffer_when_done int64
Frame_current plm_frame_t
Frame_forward plm_frame_t
Frame_backward plm_frame_t
Frames_data *uint8
Block_data [64]int64
Intra_quant_matrix [64]uint8
Non_intra_quant_matrix [64]uint8
Has_reference_frame int64
Assume_no_b_frames int64
}
type plm_audio_t struct {
Time float64
Samples_decoded int64
Samplerate_index int64
Bitrate_index int64
Version int64
Layer int64
Mode int64
Bound int64
V_pos int64
Next_frame_data_size int64
Has_header int64
Buffer *plm_buffer_t
Destroy_buffer_when_done int64
Allocation [2][32]*plm_quantizer_spec_t
Scale_factor_info [2][32]uint8
Scale_factor [2][32][3]int64
Sample [2][32][3]int64
Samples plm_samples_t
D [1024]float32
V [2][1024]float32
U [32]float32
}
type plm_packet_t struct {
Type int64
Pts float64
Length uint64
Data *uint8
}
type plm_plane_t struct {
Width uint64
Height uint64
Data *uint8
}
type plm_frame_t struct {
Time float64
Width uint64
Height uint64
Y plm_plane_t
Cr plm_plane_t
Cb plm_plane_t
}
type plm_video_decode_callback func(self *plm_t, frame *plm_frame_t, user unsafe.Pointer)
type plm_samples_t struct {
Time float64
Count uint64
Interleaved [2304]float32
}
type plm_audio_decode_callback func(self *plm_t, samples *plm_samples_t, user unsafe.Pointer)
type plm_buffer_load_callback func(self *plm_buffer_t, user unsafe.Pointer)
var plm_demux_packet_private int64 = 189
var plm_demux_packet_audio_1 int64 = 192
var plm_demux_packet_audio_2 int64 = 193
var plm_demux_packet_audio_3 int64 = 194
var plm_demux_packet_audio_4 int64 = 194
var plm_demux_packet_video_1 int64 = 224
func plm_create_with_filename(filename *byte) *plm_t {
var buffer *plm_buffer_t = plm_buffer_create_with_filename(filename)
if buffer == nil {
return nil
}
return plm_create_with_buffer(buffer, _true)
}
func plm_create_with_file(fh *stdio.File, close_when_done int64) *plm_t {
var buffer *plm_buffer_t = plm_buffer_create_with_file(fh, close_when_done)
return plm_create_with_buffer(buffer, _true)
}
func plm_create_with_memory(bytes *uint8, length uint64, free_when_done int64) *plm_t {
var buffer *plm_buffer_t = plm_buffer_create_with_memory(bytes, length, free_when_done)
return plm_create_with_buffer(buffer, _true)
}
func plm_create_with_buffer(buffer *plm_buffer_t, destroy_when_done int64) *plm_t {
var self *plm_t = new(plm_t)
*self = plm_t{}
self.Demux = plm_demux_create(buffer, destroy_when_done)
self.Video_enabled = _true
self.Audio_enabled = _true
plm_init_decoders(self)
return self
}
func plm_init_decoders(self *plm_t) int64 {
if self.Has_decoders != 0 {
return _true
}
if plm_demux_has_headers(self.Demux) == 0 {
return _false
}
if plm_demux_get_num_video_streams(self.Demux) > 0 {
if self.Video_enabled != 0 {
self.Video_packet_type = plm_demux_packet_video_1
}
self.Video_buffer = plm_buffer_create_with_capacity(128 * 1024)
plm_buffer_set_load_callback(self.Video_buffer, func(self *plm_buffer_t, user unsafe.Pointer) {
plm_read_video_packet(self, user)
}, unsafe.Pointer(self))
}
if plm_demux_get_num_audio_streams(self.Demux) > 0 {
if self.Audio_enabled != 0 {
self.Audio_packet_type = plm_demux_packet_audio_1 + self.Audio_stream_index
}
self.Audio_buffer = plm_buffer_create_with_capacity(128 * 1024)
plm_buffer_set_load_callback(self.Audio_buffer, func(self *plm_buffer_t, user unsafe.Pointer) {
plm_read_audio_packet(self, user)
}, unsafe.Pointer(self))
}
if self.Video_buffer != nil {
self.Video_decoder = plm_video_create_with_buffer(self.Video_buffer, _true)
}
if self.Audio_buffer != nil {
self.Audio_decoder = plm_audio_create_with_buffer(self.Audio_buffer, _true)
}
self.Has_decoders = _true
return _true
}
func plm_destroy(self *plm_t) {
if self.Video_decoder != nil {
plm_video_destroy(self.Video_decoder)
}
if self.Audio_decoder != nil {
plm_audio_destroy(self.Audio_decoder)
}
plm_demux_destroy(self.Demux)
libc.Free(unsafe.Pointer(self))
}
func plm_get_audio_enabled(self *plm_t) int64 {
return self.Audio_enabled
}
func plm_has_headers(self *plm_t) int64 {
if plm_demux_has_headers(self.Demux) == 0 {
return _false
}
if plm_init_decoders(self) == 0 {
return _false
}
if self.Video_decoder != nil && plm_video_has_header(self.Video_decoder) == 0 || self.Audio_decoder != nil && plm_audio_has_header(self.Audio_decoder) == 0 {
return _false
}
return _true
}
func plm_set_audio_enabled(self *plm_t, enabled int64) {
self.Audio_enabled = enabled
if enabled == 0 {
self.Audio_packet_type = 0
return
}
if plm_init_decoders(self) != 0 && self.Audio_decoder != nil {
self.Audio_packet_type = plm_demux_packet_audio_1 + self.Audio_stream_index
} else {
self.Audio_packet_type = 0
}
}
func plm_set_audio_stream(self *plm_t, stream_index int64) {
if stream_index < 0 || stream_index > 3 {
return
}
self.Audio_stream_index = stream_index
plm_set_audio_enabled(self, self.Audio_enabled)
}
func plm_get_video_enabled(self *plm_t) int64 {
return self.Video_enabled
}
func plm_set_video_enabled(self *plm_t, enabled int64) {
self.Video_enabled = enabled
if enabled == 0 {
self.Video_packet_type = 0
return
}
if plm_init_decoders(self) != 0 && self.Video_decoder != nil {
self.Video_packet_type = plm_demux_packet_video_1
} else {
self.Video_packet_type = 0
}
}
func plm_get_num_video_streams(self *plm_t) int64 {
return plm_demux_get_num_video_streams(self.Demux)
}
func plm_get_width(self *plm_t) int64 {
if plm_init_decoders(self) != 0 && self.Video_decoder != nil {
return plm_video_get_width(self.Video_decoder)
}
return 0
}
func plm_get_height(self *plm_t) int64 {
if plm_init_decoders(self) != 0 && self.Video_decoder != nil {
return plm_video_get_height(self.Video_decoder)
}
return 0
}
func plm_get_framerate(self *plm_t) float64 {
if plm_init_decoders(self) != 0 && self.Video_decoder != nil {
return plm_video_get_framerate(self.Video_decoder)
}
return 0
}
func plm_get_num_audio_streams(self *plm_t) int64 {
return plm_demux_get_num_audio_streams(self.Demux)
}
func plm_get_samplerate(self *plm_t) int64 {
if plm_init_decoders(self) != 0 && self.Audio_decoder != nil {
return plm_audio_get_samplerate(self.Audio_decoder)
}
return 0
}
func plm_get_audio_lead_time(self *plm_t) float64 {
return self.Audio_lead_time
}
func plm_set_audio_lead_time(self *plm_t, lead_time float64) {
self.Audio_lead_time = lead_time
}
func plm_get_time(self *plm_t) float64 {
return self.Time
}
func plm_get_duration(self *plm_t) float64 {
return plm_demux_get_duration(self.Demux, plm_demux_packet_video_1)
}
func plm_rewind(self *plm_t) {
if self.Video_decoder != nil {
plm_video_rewind(self.Video_decoder)
}
if self.Audio_decoder != nil {
plm_audio_rewind(self.Audio_decoder)
}
plm_demux_rewind(self.Demux)
self.Time = 0
}
func plm_get_loop(self *plm_t) int64 {
return self.Loop
}
func plm_set_loop(self *plm_t, loop int64) {
self.Loop = loop
}
func plm_has_ended(self *plm_t) int64 {
return self.Has_ended
}
func plm_set_video_decode_callback(self *plm_t, fp plm_video_decode_callback, user unsafe.Pointer) {
self.Video_decode_callback = fp
self.Video_decode_callback_user_data = user
}
func plm_set_audio_decode_callback(self *plm_t, fp plm_audio_decode_callback, user unsafe.Pointer) {
self.Audio_decode_callback = fp
self.Audio_decode_callback_user_data = user
}
func plm_decode(self *plm_t, tick float64) {
if plm_init_decoders(self) == 0 {
return
}
var decode_video int64 = int64(libc.BoolToInt(self.Video_decode_callback != nil && self.Video_packet_type != 0))
var decode_audio int64 = int64(libc.BoolToInt(self.Audio_decode_callback != nil && self.Audio_packet_type != 0))
if decode_video == 0 && decode_audio == 0 {
return
}
var did_decode int64 = _false
var decode_video_failed int64 = _false
var decode_audio_failed int64 = _false
var video_target_time float64 = self.Time + tick
var audio_target_time float64 = self.Time + tick + self.Audio_lead_time
for {
did_decode = _false
if decode_video != 0 && plm_video_get_time(self.Video_decoder) < video_target_time {
var frame *plm_frame_t = plm_video_decode(self.Video_decoder)
if frame != nil {
self.Video_decode_callback(self, frame, self.Video_decode_callback_user_data)
did_decode = _true
} else {
decode_video_failed = _true
}
}
if decode_audio != 0 && plm_audio_get_time(self.Audio_decoder) < audio_target_time {
var samples *plm_samples_t = plm_audio_decode(self.Audio_decoder)
if samples != nil {
self.Audio_decode_callback(self, samples, self.Audio_decode_callback_user_data)
did_decode = _true
} else {
decode_audio_failed = _true
}
}
if did_decode == 0 {
break
}
}
if (decode_video == 0 || decode_video_failed != 0) && (decode_audio == 0 || decode_audio_failed != 0) && plm_demux_has_ended(self.Demux) != 0 {
plm_handle_end(self)
return
}
self.Time += tick
}
func plm_decode_video(self *plm_t) *plm_frame_t {
if plm_init_decoders(self) == 0 {
return nil
}
if self.Video_packet_type == 0 {
return nil
}
var frame *plm_frame_t = plm_video_decode(self.Video_decoder)
if frame != nil {
self.Time = frame.Time
} else if plm_demux_has_ended(self.Demux) != 0 {
plm_handle_end(self)
}
return frame
}
func plm_decode_audio(self *plm_t) *plm_samples_t {
if plm_init_decoders(self) == 0 {
return nil
}
if self.Audio_packet_type == 0 {
return nil
}
var samples *plm_samples_t = plm_audio_decode(self.Audio_decoder)
if samples != nil {
self.Time = samples.Time
} else if plm_demux_has_ended(self.Demux) != 0 {
plm_handle_end(self)
}
return samples
}
func plm_handle_end(self *plm_t) {
if self.Loop != 0 {
plm_rewind(self)
} else {
self.Has_ended = _true
}
}
func plm_read_video_packet(buffer *plm_buffer_t, user unsafe.Pointer) {
_ = buffer
var self *plm_t = (*plm_t)(user)
plm_read_packets(self, self.Video_packet_type)
}
func plm_read_audio_packet(buffer *plm_buffer_t, user unsafe.Pointer) {
_ = buffer
var self *plm_t = (*plm_t)(user)
plm_read_packets(self, self.Audio_packet_type)
}
func plm_read_packets(self *plm_t, requested_type int64) {
var packet *plm_packet_t
for (func() *plm_packet_t {
packet = plm_demux_decode(self.Demux)
return packet
}()) != nil {
if packet.Type == self.Video_packet_type {
plm_buffer_write(self.Video_buffer, packet.Data, packet.Length)
} else if packet.Type == self.Audio_packet_type {
plm_buffer_write(self.Audio_buffer, packet.Data, packet.Length)
}
if packet.Type == requested_type {
return
}
}
if plm_demux_has_ended(self.Demux) != 0 {
if self.Video_buffer != nil {
plm_buffer_signal_end(self.Video_buffer)
}
if self.Audio_buffer != nil {
plm_buffer_signal_end(self.Audio_buffer)
}
}
}
func plm_seek_frame(self *plm_t, time float64, seek_exact int64) *plm_frame_t {
if plm_init_decoders(self) == 0 {
return nil
}
if self.Video_packet_type == 0 {
return nil
}
var type_ int64 = self.Video_packet_type
var start_time float64 = plm_demux_get_start_time(self.Demux, type_)
var duration float64 = plm_demux_get_duration(self.Demux, type_)
if time < 0 {
time = 0
} else if time > duration {
time = duration
}
var packet *plm_packet_t = plm_demux_seek(self.Demux, time, type_, _true)
if packet == nil {
return nil
}
var previous_audio_packet_type int64 = self.Audio_packet_type
self.Audio_packet_type = 0
plm_video_rewind(self.Video_decoder)
plm_video_set_time(self.Video_decoder, packet.Pts-start_time)
plm_buffer_write(self.Video_buffer, packet.Data, packet.Length)
var frame *plm_frame_t = plm_video_decode(self.Video_decoder)
if seek_exact != 0 {
for frame != nil && frame.Time < time {
frame = plm_video_decode(self.Video_decoder)
}
}
self.Audio_packet_type = previous_audio_packet_type
if frame != nil {
self.Time = frame.Time
}
self.Has_ended = _false
return frame
}
func plm_seek(self *plm_t, time float64, seek_exact int64) int64 {
var frame *plm_frame_t = plm_seek_frame(self, time, seek_exact)
if frame == nil {
return _false
}
if self.Video_decode_callback != nil {
self.Video_decode_callback(self, frame, self.Video_decode_callback_user_data)
}
if self.Audio_packet_type == 0 {
return _true
}
var start_time float64 = plm_demux_get_start_time(self.Demux, self.Video_packet_type)
plm_audio_rewind(self.Audio_decoder)
var packet *plm_packet_t = nil
for (func() *plm_packet_t {
packet = plm_demux_decode(self.Demux)
return packet
}()) != nil {
if packet.Type == self.Video_packet_type {
plm_buffer_write(self.Video_buffer, packet.Data, packet.Length)
} else if packet.Type == self.Audio_packet_type && packet.Pts-start_time > self.Time {
plm_audio_set_time(self.Audio_decoder, packet.Pts-start_time)
plm_buffer_write(self.Audio_buffer, packet.Data, packet.Length)
plm_decode(self, 0)
break
}
}
return _true
}
type plm_buffer_mode int64
const (
plm_buffer_mode_file = plm_buffer_mode(iota)
plm_buffer_mode_fixed_mem
plm_buffer_mode_ring
plm_buffer_mode_append
)
type plm_vlc_t struct {
Index int16
Value int16
}
type plm_vlc_uint_t struct {
Index int16
Value uint16
}
func plm_buffer_create_with_filename(filename *byte) *plm_buffer_t {
var fh *stdio.File = stdio.FOpen(libc.GoString(filename), "rb")
if fh == nil {
return nil
}
return plm_buffer_create_with_file(fh, _true)
}
func plm_buffer_create_with_file(fh *stdio.File, close_when_done int64) *plm_buffer_t {
var self *plm_buffer_t = plm_buffer_create_with_capacity(128 * 1024)
self.Fh = fh
self.Close_when_done = close_when_done
self.Mode = plm_buffer_mode(plm_buffer_mode_file)
self.Discard_read_bytes = _true
self.Fh.Seek(0, stdio.SEEK_END)
self.Total_size = uint64(self.Fh.Tell())
self.Fh.Seek(0, stdio.SEEK_SET)
plm_buffer_set_load_callback(self, func(self *plm_buffer_t, user unsafe.Pointer) {
plm_buffer_load_file_callback(self, user)
}, nil)
return self
}
func plm_buffer_create_with_memory(bytes *uint8, length uint64, free_when_done int64) *plm_buffer_t {
var self *plm_buffer_t = new(plm_buffer_t)
*self = plm_buffer_t{}
self.Capacity = length
self.Length = length
self.Total_size = length
self.Free_when_done = free_when_done
self.Bytes = bytes
self.Mode = plm_buffer_mode(plm_buffer_mode_fixed_mem)
self.Discard_read_bytes = _false
return self
}
func plm_buffer_create_with_capacity(capacity uint64) *plm_buffer_t {
var self *plm_buffer_t = new(plm_buffer_t)
*self = plm_buffer_t{}
self.Capacity = capacity
self.Free_when_done = _true
self.Bytes = (*uint8)(libc.Malloc(int(capacity)))
self.Mode = plm_buffer_mode(plm_buffer_mode_ring)
self.Discard_read_bytes = _true
return self
}
func plm_buffer_create_for_appending(initial_capacity uint64) *plm_buffer_t {
var self *plm_buffer_t = plm_buffer_create_with_capacity(initial_capacity)
self.Mode = plm_buffer_mode(plm_buffer_mode_append)
self.Discard_read_bytes = _false
return self
}
func plm_buffer_destroy(self *plm_buffer_t) {
if self.Fh != nil && self.Close_when_done != 0 {
self.Fh.Close()
}
if self.Free_when_done != 0 {
libc.Free(unsafe.Pointer(self.Bytes))
}
libc.Free(unsafe.Pointer(self))
}
func plm_buffer_get_size(self *plm_buffer_t) uint64 {
if self.Mode == plm_buffer_mode(plm_buffer_mode_file) {
return self.Total_size
}
return self.Length
}
func plm_buffer_get_remaining(self *plm_buffer_t) uint64 {
return self.Length - (self.Bit_index >> 3)
}
func plm_buffer_write(self *plm_buffer_t, bytes *uint8, length uint64) uint64 {
if self.Mode == plm_buffer_mode(plm_buffer_mode_fixed_mem) {
return 0
}
if self.Discard_read_bytes != 0 {
plm_buffer_discard_read_bytes(self)
if self.Mode == plm_buffer_mode(plm_buffer_mode_ring) {
self.Total_size = 0
}
}
var bytes_available uint64 = self.Capacity - self.Length
if bytes_available < length {
var new_size uint64 = self.Capacity
for {
new_size *= 2
if new_size-self.Length >= length {
break
}
}
self.Bytes = (*uint8)(libc.Realloc(unsafe.Pointer(self.Bytes), int(new_size)))
self.Capacity = new_size
}
libc.MemCpy(unsafe.Add(unsafe.Pointer(self.Bytes), self.Length), unsafe.Pointer(bytes), int(length))
self.Length += length
self.Has_ended = _false
return length
}
func plm_buffer_signal_end(self *plm_buffer_t) {
self.Total_size = self.Length
}
func plm_buffer_set_load_callback(self *plm_buffer_t, fp plm_buffer_load_callback, user unsafe.Pointer) {
self.Load_callback = fp
self.Load_callback_user_data = user
}
func plm_buffer_rewind(self *plm_buffer_t) {
plm_buffer_seek(self, 0)
}
func plm_buffer_seek(self *plm_buffer_t, pos uint64) {
self.Has_ended = _false
if self.Mode == plm_buffer_mode(plm_buffer_mode_file) {
self.Fh.Seek(int64(pos), stdio.SEEK_SET)
self.Bit_index = 0
self.Length = 0
} else if self.Mode == plm_buffer_mode(plm_buffer_mode_ring) {
if pos != 0 {
return
}
self.Bit_index = 0
self.Length = 0
self.Total_size = 0
} else if pos < self.Length {
self.Bit_index = pos << 3
}
}
func plm_buffer_tell(self *plm_buffer_t) uint64 {
if self.Mode == plm_buffer_mode(plm_buffer_mode_file) {
return uint64(self.Fh.Tell()) + (self.Bit_index >> 3) - self.Length
}
return self.Bit_index >> 3
}
func plm_buffer_discard_read_bytes(self *plm_buffer_t) {
var byte_pos uint64 = self.Bit_index >> 3
if byte_pos == self.Length {
self.Bit_index = 0
self.Length = 0
} else if byte_pos > 0 {
libc.MemMove(unsafe.Pointer(self.Bytes), unsafe.Add(unsafe.Pointer(self.Bytes), byte_pos), int(self.Length-byte_pos))
self.Bit_index -= byte_pos << 3
self.Length -= byte_pos
}
}
func plm_buffer_load_file_callback(self *plm_buffer_t, user unsafe.Pointer) {
_ = user
if self.Discard_read_bytes != 0 {
plm_buffer_discard_read_bytes(self)
}
var bytes_available uint64 = self.Capacity - self.Length
var bytes_read uint64 = uint64(self.Fh.ReadN((*byte)(unsafe.Add(unsafe.Pointer(self.Bytes), self.Length)), 1, int(bytes_available)))
self.Length += bytes_read
if bytes_read == 0 {
self.Has_ended = _true
}
}
func plm_buffer_has_ended(self *plm_buffer_t) int64 {
return self.Has_ended
}
func plm_buffer_has(self *plm_buffer_t, count uint64) int64 {
if ((self.Length << 3) - self.Bit_index) >= count {
return _true
}
if self.Load_callback != nil {
self.Load_callback(self, self.Load_callback_user_data)
if ((self.Length << 3) - self.Bit_index) >= count {
return _true
}
}
if self.Total_size != 0 && self.Length == self.Total_size {
self.Has_ended = _true
}
return _false
}
func plm_buffer_read(self *plm_buffer_t, count int64) int64 {
if plm_buffer_has(self, uint64(count)) == 0 {
return 0
}
var value int64 = 0
for count != 0 {
var (
current_byte int64 = int64(*(*uint8)(unsafe.Add(unsafe.Pointer(self.Bytes), self.Bit_index>>3)))
remaining int64 = int64(8 - (self.Bit_index & 7))
read int64
)
if remaining < count {
read = remaining
} else {
read = count
}
var shift int64 = remaining - read
var mask int64 = (math.MaxUint8 >> (8 - read))
value = (value << read) | (current_byte&(mask<<shift))>>shift
self.Bit_index += uint64(read)
count -= read
}
return value
}
func plm_buffer_align(self *plm_buffer_t) {
self.Bit_index = ((self.Bit_index + 7) >> 3) << 3
}
func plm_buffer_skip(self *plm_buffer_t, count uint64) {
if plm_buffer_has(self, count) != 0 {
self.Bit_index += count
}
}
func plm_buffer_skip_bytes(self *plm_buffer_t, v uint8) int64 {
plm_buffer_align(self)
var skipped int64 = 0
for plm_buffer_has(self, 8) != 0 && int64(*(*uint8)(unsafe.Add(unsafe.Pointer(self.Bytes), self.Bit_index>>3))) == int64(v) {
self.Bit_index += 8
skipped++
}
return skipped
}
func plm_buffer_next_start_code(self *plm_buffer_t) int64 {
plm_buffer_align(self)
for plm_buffer_has(self, 5<<3) != 0 {
var byte_index uint64 = self.Bit_index >> 3
if int64(*(*uint8)(unsafe.Add(unsafe.Pointer(self.Bytes), byte_index))) == 0 && int64(*(*uint8)(unsafe.Add(unsafe.Pointer(self.Bytes), byte_index+1))) == 0 && int64(*(*uint8)(unsafe.Add(unsafe.Pointer(self.Bytes), byte_index+2))) == 1 {
self.Bit_index = (byte_index + 4) << 3
return int64(*(*uint8)(unsafe.Add(unsafe.Pointer(self.Bytes), byte_index+3)))
}
self.Bit_index += 8
}
return -1
}
func plm_buffer_find_start_code(self *plm_buffer_t, code int64) int64 {
var current int64 = 0
for _true != 0 {
current = plm_buffer_next_start_code(self)
if current == code || current == -1 {
return current
}
}
return -1
}
func plm_buffer_has_start_code(self *plm_buffer_t, code int64) int64 {
var (
previous_bit_index uint64 = self.Bit_index
previous_discard_read_bytes int64 = self.Discard_read_bytes
)
self.Discard_read_bytes = _false
var current int64 = plm_buffer_find_start_code(self, code)
self.Bit_index = previous_bit_index
self.Discard_read_bytes = previous_discard_read_bytes
return current
}
func plm_buffer_peek_non_zero(self *plm_buffer_t, bit_count int64) int64 {
if plm_buffer_has(self, uint64(bit_count)) == 0 {
return _false
}
var val int64 = plm_buffer_read(self, bit_count)
self.Bit_index -= uint64(bit_count)
return int64(libc.BoolToInt(val != 0))
}
func plm_buffer_read_vlc(self *plm_buffer_t, table *plm_vlc_t) int16 {
var state plm_vlc_t = plm_vlc_t{}
for {
state = *(*plm_vlc_t)(unsafe.Add(unsafe.Pointer(table), unsafe.Sizeof(plm_vlc_t{})*uintptr(int64(state.Index)+plm_buffer_read(self, 1))))
if int64(state.Index) <= 0 {
break
}
}
return state.Value
}
func plm_buffer_read_vlc_uint(self *plm_buffer_t, table *plm_vlc_uint_t) uint16 {
return uint16(plm_buffer_read_vlc(self, (*plm_vlc_t)(unsafe.Pointer(table))))
}
var plm_start_pack int64 = 186
var plm_start_end int64 = 185
var plm_start_system int64 = 187
func plm_demux_create(buffer *plm_buffer_t, destroy_when_done int64) *plm_demux_t {
var self *plm_demux_t = new(plm_demux_t)
*self = plm_demux_t{}
self.Buffer = buffer
self.Destroy_buffer_when_done = destroy_when_done
self.Start_time = float64(-1)
self.Duration = float64(-1)
self.Start_code = -1
plm_demux_has_headers(self)
return self
}
func plm_demux_destroy(self *plm_demux_t) {
if self.Destroy_buffer_when_done != 0 {
plm_buffer_destroy(self.Buffer)
}
libc.Free(unsafe.Pointer(self))
}
func plm_demux_has_headers(self *plm_demux_t) int64 {
if self.Has_headers != 0 {
return _true
}
if self.Has_pack_header == 0 {
if self.Start_code != plm_start_pack && plm_buffer_find_start_code(self.Buffer, plm_start_pack) == -1 {
return _false
}
self.Start_code = plm_start_pack
if plm_buffer_has(self.Buffer, 64) == 0 {
return _false
}
self.Start_code = -1
if plm_buffer_read(self.Buffer, 4) != 2 {
return _false
}
self.System_clock_ref = plm_demux_decode_time(self)
plm_buffer_skip(self.Buffer, 1)
plm_buffer_skip(self.Buffer, 22)
plm_buffer_skip(self.Buffer, 1)
self.Has_pack_header = _true
}
if self.Has_system_header == 0 {
if self.Start_code != plm_start_system && plm_buffer_find_start_code(self.Buffer, plm_start_system) == -1 {
return _false
}
self.Start_code = plm_start_system
if plm_buffer_has(self.Buffer, 56) == 0 {
return _false
}
self.Start_code = -1
plm_buffer_skip(self.Buffer, 16)
plm_buffer_skip(self.Buffer, 24)
self.Num_audio_streams = plm_buffer_read(self.Buffer, 6)
plm_buffer_skip(self.Buffer, 5)
self.Num_video_streams = plm_buffer_read(self.Buffer, 5)
self.Has_system_header = _true
}
self.Has_headers = _true
return _true
}
func plm_demux_get_num_video_streams(self *plm_demux_t) int64 {
if plm_demux_has_headers(self) != 0 {
return self.Num_video_streams
}
return 0
}
func plm_demux_get_num_audio_streams(self *plm_demux_t) int64 {
if plm_demux_has_headers(self) != 0 {
return self.Num_audio_streams
}
return 0
}
func plm_demux_rewind(self *plm_demux_t) {
plm_buffer_rewind(self.Buffer)
self.Current_packet.Length = 0
self.Next_packet.Length = 0
self.Start_code = -1
}
func plm_demux_has_ended(self *plm_demux_t) int64 {
return plm_buffer_has_ended(self.Buffer)
}
func plm_demux_buffer_seek(self *plm_demux_t, pos uint64) {
plm_buffer_seek(self.Buffer, pos)
self.Current_packet.Length = 0
self.Next_packet.Length = 0
self.Start_code = -1
}
func plm_demux_get_start_time(self *plm_demux_t, type_ int64) float64 {
if self.Start_time != float64(-1) {
return self.Start_time
}
var previous_pos int64 = int64(plm_buffer_tell(self.Buffer))
var previous_start_code int64 = self.Start_code
plm_demux_rewind(self)
for {
{
var packet *plm_packet_t = plm_demux_decode(self)
if packet == nil {
break
}
if packet.Type == type_ {
self.Start_time = packet.Pts
}
}
if self.Start_time != float64(-1) {
break
}
}
plm_demux_buffer_seek(self, uint64(previous_pos))
self.Start_code = previous_start_code
return self.Start_time
}
func plm_demux_get_duration(self *plm_demux_t, type_ int64) float64 {
var file_size uint64 = plm_buffer_get_size(self.Buffer)
if self.Duration != float64(-1) && self.Last_file_size == file_size {
return self.Duration
}
var previous_pos uint64 = plm_buffer_tell(self.Buffer)
var previous_start_code int64 = self.Start_code
var start_range int64 = 64 * 1024
var max_range int64 = 4096 * 1024
for range_ := int64(start_range); range_ <= max_range; range_ *= 2 {
var seek_pos int64 = int64(file_size - uint64(range_))
if seek_pos < 0 {
seek_pos = 0
range_ = max_range
}
plm_demux_buffer_seek(self, uint64(seek_pos))
self.Current_packet.Length = 0
var last_pts float64 = float64(-1)
var packet *plm_packet_t = nil
for (func() *plm_packet_t {
packet = plm_demux_decode(self)
return packet
}()) != nil {
if packet.Pts != float64(-1) && packet.Type == type_ {
last_pts = packet.Pts
}
}
if last_pts != float64(-1) {
self.Duration = last_pts - plm_demux_get_start_time(self, type_)
break
}
}
plm_demux_buffer_seek(self, previous_pos)
self.Start_code = previous_start_code
self.Last_file_size = file_size
return self.Duration
}
func plm_demux_seek(self *plm_demux_t, seek_time float64, type_ int64, force_intra int64) *plm_packet_t {
if plm_demux_has_headers(self) == 0 {
return nil
}
var duration float64 = plm_demux_get_duration(self, type_)
var file_size int64 = int64(plm_buffer_get_size(self.Buffer))
var byterate int64 = int64(float64(file_size) / duration)
var cur_time float64 = self.Last_decoded_pts
var scan_span float64 = 1
if seek_time > duration {
seek_time = duration
} else if seek_time < 0 {
seek_time = 0
}