-
Notifications
You must be signed in to change notification settings - Fork 11
/
video.go
1627 lines (1415 loc) · 42.1 KB
/
video.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
package mpeg
import (
"image"
"image/color"
"image/draw"
"unsafe"
)
// Frame represents decoded video frame.
type Frame struct {
Time float64
Width int
Height int
Y Plane
Cb Plane
Cr Plane
imYCbCr image.YCbCr
imRGBA image.RGBA
}
// YCbCr returns frame as image.YCbCr.
func (f *Frame) YCbCr() *image.YCbCr {
return &f.imYCbCr
}
// RGBA returns frame as image.RGBA.
func (f *Frame) RGBA() *image.RGBA {
b := f.imYCbCr.Bounds()
draw.Draw(&f.imRGBA, b.Bounds(), &f.imYCbCr, b.Min, draw.Src)
return &f.imRGBA
}
// Pixels returns frame as slice of color.RGBA.
func (f *Frame) Pixels() []color.RGBA {
img := f.RGBA()
return unsafe.Slice((*color.RGBA)(unsafe.Pointer(&img.Pix[0])), len(img.Pix)/4)
}
// Plane represents decoded video plane.
// The byte length of the data is width * height. Note that different planes have different sizes:
// the Luma plane (Y) is double the size of each of the two Chroma planes (Cr, Cb) - i.e. 4 times the byte length.
// Also note that the size of the plane does *not* denote the size of the displayed frame.
// The sizes of planes are always rounded up to the nearest macroblock (16px).
type Plane struct {
Width int
Height int
Data []byte
}
// Video decodes MPEG-1 Video (mpeg1) data into raw YCrCb frames.
type Video struct {
aspectRatio float64
frameRate float64
time float64
bitRate int
framesDecoded int
width int
height int
mbWidth int
mbHeight int
mbSize int
lumaWidth int
lumaHeight int
chromaWidth int
chromaHeight int
startCode int
pictureType int
motionForward motion
motionBackward motion
hasSequenceHeader bool
quantizerScale int
sliceBegin bool
macroblockAddress int
mbRow int
mbCol int
macroblockType int
macroblockIntra bool
dcPredictor []int
buf *Buffer
frameCurrent Frame
frameForward Frame
frameBackward Frame
blockData []int
intraQuantMatrix []byte
nonIntraQuantMatrix []byte
hasReferenceFrame bool
assumeNoBFrames bool
}
// NewVideo creates a video decoder with buffer as a source.
func NewVideo(buf *Buffer) *Video {
video := &Video{}
video.buf = buf
video.dcPredictor = make([]int, 3)
video.blockData = make([]int, 64)
video.intraQuantMatrix = make([]byte, 64)
video.nonIntraQuantMatrix = make([]byte, 64)
// Attempt to decode the sequence header
video.startCode = video.buf.findStartCode(startSequence)
if video.startCode != -1 {
video.decodeSequenceHeader()
}
return video
}
// Buffer returns video buffer.
func (v *Video) Buffer() *Buffer {
return v.buf
}
// HasHeader checks whether a sequence header was found, and we can accurately report on
// dimensions and framerate.
func (v *Video) HasHeader() bool {
if v.hasSequenceHeader {
return true
}
if v.startCode != startSequence {
v.startCode = v.buf.findStartCode(startSequence)
}
if v.startCode == -1 {
return false
}
if !v.decodeSequenceHeader() {
return false
}
return true
}
// Framerate returns the framerate in frames per second.
func (v *Video) Framerate() float64 {
if v.HasHeader() {
return v.frameRate
}
return 0
}
// Width returns the display width.
func (v *Video) Width() int {
if v.HasHeader() {
return v.width
}
return 0
}
// Height returns the display height.
func (v *Video) Height() int {
if v.HasHeader() {
return v.height
}
return 0
}
// SetNoDelay sets "no delay" mode. When enabled, the decoder assumes that the video does
// *not* contain any B-Frames. This is useful for reducing lag when streaming.
func (v *Video) SetNoDelay(noDelay bool) {
v.assumeNoBFrames = noDelay
}
// Time returns the current internal time in seconds.
func (v *Video) Time() float64 {
return v.time
}
// SetTime sets the current internal time in seconds. This is only useful when you
// manipulate the underlying video buffer and want to enforce a correct timestamps.
func (v *Video) SetTime(time float64) {
v.framesDecoded = int(v.frameRate * v.time)
v.time = time
}
// Rewind rewinds the internal buffer.
func (v *Video) Rewind() {
v.buf.Rewind()
v.time = 0
v.framesDecoded = 0
v.hasReferenceFrame = false
v.startCode = -1
}
// HasEnded checks whether the file has ended. This will be cleared on rewind.
func (v *Video) HasEnded() bool {
return v.buf.HasEnded()
}
// Decode decodes and returns one frame of video and advance the internal time by 1/framerate seconds.
func (v *Video) Decode() *Frame {
if !v.HasHeader() {
return nil
}
var frame *Frame
for {
if v.startCode != startPicture {
v.startCode = v.buf.findStartCode(startPicture)
if v.startCode == -1 {
// If we reached the end of the file and the previously decoded
// frame was a reference frame, we still have to return it.
if v.hasReferenceFrame && !v.assumeNoBFrames && v.buf.HasEnded() &&
(v.pictureType == pictureTypeIntra || v.pictureType == pictureTypePredictive) {
v.hasReferenceFrame = false
frame = &v.frameBackward
break
}
return nil
}
}
// Make sure we have a full picture in the buffer before attempting to
// decode it. Sadly, this can only be done by seeking for the start code
// of the next picture. Also, if we didn't find the start code for the
// next picture, but the source has ended, we assume that this last
// picture is in the buffer.
if v.buf.hasStartCode(startPicture) == -1 && !v.buf.HasEnded() {
return nil
}
v.buf.discardReadBytes()
v.decodePicture()
switch {
case v.assumeNoBFrames:
frame = &v.frameBackward
case v.pictureType == pictureTypeB:
frame = &v.frameCurrent
case v.hasReferenceFrame:
frame = &v.frameForward
default:
v.hasReferenceFrame = true
}
if frame != nil {
break
}
}
frame.Time = v.time
v.framesDecoded++
v.time = float64(v.framesDecoded) / v.frameRate
return frame
}
func (v *Video) decodeSequenceHeader() bool {
maxHeaderSize := 64 + 2*64*8 // 64 bit header + 2x 64 byte matrix
if !v.buf.has(maxHeaderSize) {
return false
}
v.width = v.buf.read(12)
v.height = v.buf.read(12)
if v.width <= 0 || v.height <= 0 {
return false
}
v.aspectRatio = videoAspectRatio[v.buf.read(4)]
v.frameRate = videoPictureRate[v.buf.read(4)]
v.bitRate = v.buf.read(18)
// Skip marker, buffer_size and constrained bit
v.buf.skip(1 + 10 + 1)
// Load custom intra quant matrix?
if v.buf.read1() != 0 {
for i := 0; i < 64; i++ {
idx := videoZigZag[i]
v.intraQuantMatrix[idx] = byte(v.buf.read(8))
}
} else {
copy(v.intraQuantMatrix, videoIntraQuantMatrix)
}
// Load custom non intra quant matrix?
if v.buf.read1() != 0 {
for i := 0; i < 64; i++ {
idx := videoZigZag[i]
v.nonIntraQuantMatrix[idx] = byte(v.buf.read(8))
}
} else {
copy(v.nonIntraQuantMatrix, videoNonIntraQuantMatrix)
}
v.mbWidth = (v.width + 15) >> 4
v.mbHeight = (v.height + 15) >> 4
v.mbSize = v.mbWidth * v.mbHeight
v.lumaWidth = v.mbWidth << 4
v.lumaHeight = v.mbHeight << 4
v.chromaWidth = v.mbWidth << 3
v.chromaHeight = v.mbHeight << 3
v.initFrame(&v.frameCurrent)
v.initFrame(&v.frameForward)
v.initFrame(&v.frameBackward)
v.hasSequenceHeader = true
return true
}
func (v *Video) initFrame(frame *Frame) {
lumaSize := v.lumaWidth * v.lumaHeight
chromaSize := v.chromaWidth * v.chromaHeight
frameSize := lumaSize + 2*chromaSize
base := make([]byte, frameSize)
frame.Width = v.width
frame.Height = v.height
frame.Y.Width = v.lumaWidth
frame.Y.Height = v.lumaHeight
frame.Y.Data = base[0:lumaSize:lumaSize]
frame.Cb.Width = v.chromaWidth
frame.Cb.Height = v.chromaHeight
frame.Cb.Data = base[lumaSize : lumaSize+chromaSize : lumaSize+chromaSize]
frame.Cr.Width = v.chromaWidth
frame.Cr.Height = v.chromaHeight
frame.Cr.Data = base[lumaSize+chromaSize : frameSize : frameSize]
frame.imYCbCr = image.YCbCr{
Y: frame.Y.Data,
Cb: frame.Cb.Data,
Cr: frame.Cr.Data,
SubsampleRatio: image.YCbCrSubsampleRatio420,
YStride: v.lumaWidth,
CStride: v.chromaWidth,
Rect: image.Rect(0, 0, v.width, v.height),
}
frame.imRGBA = image.RGBA{
Pix: make([]byte, v.width*v.height*4),
Stride: 4 * v.width,
Rect: image.Rect(0, 0, v.width, v.height),
}
}
func (v *Video) decodePicture() {
v.buf.skip(10) // skip temporalReference
v.pictureType = v.buf.read(3)
v.buf.skip(16) // skip vbv_delay
// D frames or unknown coding type
if v.pictureType <= 0 || v.pictureType > pictureTypeB {
return
}
// Forward fullPx, fCode
if v.pictureType == pictureTypePredictive || v.pictureType == pictureTypeB {
v.motionForward.FullPx = v.buf.read1()
fCode := v.buf.read(3)
if fCode == 0 {
// Ignore picture with zero fCode
return
}
v.motionForward.RSize = fCode - 1
}
// Backward fullPx, fCode
if v.pictureType == pictureTypeB {
v.motionBackward.FullPx = v.buf.read1()
fCode := v.buf.read(3)
if fCode == 0 {
// Ignore picture with zero fCode
return
}
v.motionBackward.RSize = fCode - 1
}
frameTemp := v.frameForward
if v.pictureType == pictureTypeIntra || v.pictureType == pictureTypePredictive {
v.frameForward = v.frameBackward
}
// Find first slice start code; skip extension and user data
for {
v.startCode = v.buf.nextStartCode()
if v.startCode != startExtension && v.startCode != startUserData {
break
}
}
// Decode all slices
for startIsSlice(v.startCode) {
v.decodeSlice(v.startCode & 0x000000FF)
if v.macroblockAddress >= v.mbSize-2 {
break
}
v.startCode = v.buf.nextStartCode()
}
// If this is a reference picture rotate the prediction pointers
if v.pictureType == pictureTypeIntra || v.pictureType == pictureTypePredictive {
v.frameBackward = v.frameCurrent
v.frameCurrent = frameTemp
}
}
func (v *Video) decodeSlice(slice int) {
v.sliceBegin = true
v.macroblockAddress = (slice-1)*v.mbWidth - 1
// Reset motion vectors and DC predictors
v.motionBackward.H, v.motionForward.H = 0, 0
v.motionBackward.V, v.motionForward.V = 0, 0
v.dcPredictor[0] = 128
v.dcPredictor[1] = 128
v.dcPredictor[2] = 128
v.quantizerScale = v.buf.read(5)
// Skip extra
for v.buf.read1() != 0 {
v.buf.skip(8)
}
for {
v.decodeMacroblock()
if v.macroblockAddress >= v.mbSize-1 || !v.buf.peekNonZero(23) {
break
}
}
}
func (v *Video) decodeMacroblock() {
// Decode increment
increment := 0
t := v.buf.readVlc(videoMacroblockAddressIncrement)
for t == 34 {
// macroblock_stuffing
t = v.buf.readVlc(videoMacroblockAddressIncrement)
}
for t == 35 {
// macroblock_escape
increment += 33
t = v.buf.readVlc(videoMacroblockAddressIncrement)
}
increment += t
// Process any skipped macroblocks
if v.sliceBegin {
// The first increment of each slice is relative to beginning of the
// previous row, not the previous macroblock
v.sliceBegin = false
v.macroblockAddress += increment
} else {
if v.macroblockAddress+increment >= v.mbSize {
return // invalid
}
if increment > 1 {
// Skipped macroblocks reset DC predictors
v.dcPredictor[0] = 128
v.dcPredictor[1] = 128
v.dcPredictor[2] = 128
// Skipped macroblocks in P-pictures reset motion vectors
if v.pictureType == pictureTypePredictive {
v.motionForward.H = 0
v.motionForward.V = 0
}
}
// Predict skipped macroblocks
for increment > 1 {
v.macroblockAddress++
v.mbRow = v.macroblockAddress / v.mbWidth
v.mbCol = v.macroblockAddress % v.mbWidth
v.predictMacroblock()
increment--
}
v.macroblockAddress++
}
v.mbRow = v.macroblockAddress / v.mbWidth
v.mbCol = v.macroblockAddress % v.mbWidth
if v.mbCol >= v.mbWidth || v.mbRow >= v.mbHeight {
return // corrupt stream
}
// Process the current macroblock
v.macroblockType = v.buf.readVlc(videoMacroBlockType[v.pictureType])
v.macroblockIntra = v.macroblockType&0x01 != 0
v.motionForward.IsSet = v.macroblockType&0x08 != 0
v.motionBackward.IsSet = v.macroblockType&0x04 != 0
// Quantizer scale
if (v.macroblockType & 0x10) != 0 {
v.quantizerScale = v.buf.read(5)
}
if v.macroblockIntra {
// Intra-coded macroblocks reset motion vectors
v.motionBackward.H, v.motionForward.H = 0, 0
v.motionBackward.V, v.motionForward.V = 0, 0
} else {
// Non-intra macroblocks reset DC predictors
v.dcPredictor[0] = 128
v.dcPredictor[1] = 128
v.dcPredictor[2] = 128
v.decodeMotionVectors()
v.predictMacroblock()
}
// Decode blocks
cbp := 0
if (v.macroblockType & 0x02) != 0 {
cbp = v.buf.readVlc(videoCodeBlockPattern)
} else if v.macroblockIntra {
cbp = 0x3f
}
mask := 0x20
for block := 0; block < 6; block++ {
if (cbp & mask) != 0 {
v.decodeBlock(block)
}
mask >>= 1
}
}
func (v *Video) decodeMotionVectors() {
// Forward
if v.motionForward.IsSet {
rSize := v.motionForward.RSize
v.motionForward.H = v.decodeMotionVector(rSize, v.motionForward.H)
v.motionForward.V = v.decodeMotionVector(rSize, v.motionForward.V)
} else if v.pictureType == pictureTypePredictive {
// No motion information in P-picture, reset vectors
v.motionForward.H = 0
v.motionForward.V = 0
}
if v.motionBackward.IsSet {
rSize := v.motionBackward.RSize
v.motionBackward.H = v.decodeMotionVector(rSize, v.motionBackward.H)
v.motionBackward.V = v.decodeMotionVector(rSize, v.motionBackward.V)
}
}
func (v *Video) decodeMotionVector(rSize, motion int) int {
fscale := 1 << rSize
mCode := v.buf.readVlc(videoMotion)
var r, d int
if mCode != 0 && fscale != 1 {
r = v.buf.read(rSize)
d = ((abs(mCode) - 1) << rSize) + r + 1
if mCode < 0 {
d = -d
}
} else {
d = mCode
}
motion += d
if motion > (fscale<<4)-1 {
motion -= fscale << 5
} else if motion < ((-fscale) << 4) {
motion += fscale << 5
}
return motion
}
func (v *Video) predictMacroblock() {
fwH := v.motionForward.H
fwV := v.motionForward.V
if v.motionForward.FullPx != 0 {
fwH <<= 1
fwV <<= 1
}
if v.pictureType == pictureTypeB {
bwH := v.motionBackward.H
bwV := v.motionBackward.V
if v.motionBackward.FullPx != 0 {
bwH <<= 1
bwV <<= 1
}
if v.motionForward.IsSet {
v.copyMacroblock(fwH, fwV, &v.frameForward)
if v.motionBackward.IsSet {
v.copyMacroblock(bwH, bwV, &v.frameBackward)
}
} else {
v.copyMacroblock(bwH, bwV, &v.frameBackward)
}
} else {
v.copyMacroblock(fwH, fwV, &v.frameForward)
}
}
func (v *Video) copyMacroblock(motionH, motionV int, s *Frame) {
// We use 32bit writes here
d := &v.frameCurrent
dY := unsafe.Slice((*uint32)(unsafe.Pointer(&d.Y.Data[0])), len(d.Y.Data)/4)
dCb := unsafe.Slice((*uint32)(unsafe.Pointer(&d.Cb.Data[0])), len(d.Cb.Data)/4)
dCr := unsafe.Slice((*uint32)(unsafe.Pointer(&d.Cr.Data[0])), len(d.Cr.Data)/4)
// Luminance
width := v.lumaWidth
scan := width - 16
hp := motionH >> 1
vp := motionV >> 1
oddH := (motionH & 1) == 1
oddV := (motionV & 1) == 1
si := ((v.mbRow<<4)+vp)*width + (v.mbCol << 4) + hp
di := (v.mbRow*width + v.mbCol) << 2
last := di + (width << 2)
var y1, y2, y uint64
if oddH {
if oddV {
for di < last {
y1 = uint64(s.Y.Data[si]) + uint64(s.Y.Data[si+width])
si++
for x := 0; x < 4; x++ {
y2 = uint64(s.Y.Data[si]) + uint64(s.Y.Data[si+width])
si++
y = ((y1 + y2 + 2) >> 2) & 0xff
y1 = uint64(s.Y.Data[si]) + uint64(s.Y.Data[si+width])
si++
y |= ((y1 + y2 + 2) << 6) & 0xff00
y2 = uint64(s.Y.Data[si]) + uint64(s.Y.Data[si+width])
si++
y |= ((y1 + y2 + 2) << 14) & 0xff0000
y1 = uint64(s.Y.Data[si]) + uint64(s.Y.Data[si+width])
si++
y |= ((y1 + y2 + 2) << 22) & 0xff000000
dY[di] = uint32(y)
di++
}
di += scan >> 2
si += scan - 1
}
} else {
for di < last {
y1 = uint64(s.Y.Data[si])
si++
for x := 0; x < 4; x++ {
y2 = uint64(s.Y.Data[si])
si++
y = ((y1 + y2 + 1) >> 1) & 0xff
y1 = uint64(s.Y.Data[si])
si++
y |= ((y1 + y2 + 1) << 7) & 0xff00
y2 = uint64(s.Y.Data[si])
si++
y |= ((y1 + y2 + 1) << 15) & 0xff0000
y1 = uint64(s.Y.Data[si])
si++
y |= ((y1 + y2 + 1) << 23) & 0xff000000
dY[di] = uint32(y)
di++
}
di += scan >> 2
si += scan - 1
}
}
} else {
if oddV {
for di < last {
for x := 0; x < 4; x++ {
y = ((uint64(s.Y.Data[si]) + uint64(s.Y.Data[si+width]) + 1) >> 1) & 0xff
si++
y |= ((uint64(s.Y.Data[si]) + uint64(s.Y.Data[si+width]) + 1) << 7) & 0xff00
si++
y |= ((uint64(s.Y.Data[si]) + uint64(s.Y.Data[si+width]) + 1) << 15) & 0xff0000
si++
y |= ((uint64(s.Y.Data[si]) + uint64(s.Y.Data[si+width]) + 1) << 23) & 0xff000000
si++
dY[di] = uint32(y)
di++
}
di += scan >> 2
si += scan
}
} else {
for di < last {
for x := 0; x < 4; x++ {
y = uint64(s.Y.Data[si])
si++
y |= uint64(s.Y.Data[si]) << 8
si++
y |= uint64(s.Y.Data[si]) << 16
si++
y |= uint64(s.Y.Data[si]) << 24
si++
dY[di] = uint32(y)
di++
}
di += scan >> 2
si += scan
}
}
}
// Chrominance
width = v.chromaWidth
scan = width - 8
hp = (motionH / 2) >> 1
vp = (motionV / 2) >> 1
oddH = ((motionH / 2) & 1) == 1
oddV = ((motionV / 2) & 1) == 1
si = ((v.mbRow<<3)+vp)*width + (v.mbCol << 3) + hp
di = (v.mbRow*width + v.mbCol) << 1
last = di + (width << 1)
var cb1, cb2, cb, cr1, cr2, cr uint64
if oddH {
if oddV {
for di < last {
cr1 = uint64(s.Cr.Data[si]) + uint64(s.Cr.Data[si+width])
cb1 = uint64(s.Cb.Data[si]) + uint64(s.Cb.Data[si+width])
si++
for x := 0; x < 2; x++ {
cr2 = uint64(s.Cr.Data[si]) + uint64(s.Cr.Data[si+width])
cb2 = uint64(s.Cb.Data[si]) + uint64(s.Cb.Data[si+width])
si++
cr = ((cr1 + cr2 + 2) >> 2) & 0xff
cb = ((cb1 + cb2 + 2) >> 2) & 0xff
cr1 = uint64(s.Cr.Data[si]) + uint64(s.Cr.Data[si+width])
cb1 = uint64(s.Cb.Data[si]) + uint64(s.Cb.Data[si+width])
si++
cr |= ((cr1 + cr2 + 2) << 6) & 0xff00
cb |= ((cb1 + cb2 + 2) << 6) & 0xff00
cr2 = uint64(s.Cr.Data[si]) + uint64(s.Cr.Data[si+width])
cb2 = uint64(s.Cb.Data[si]) + uint64(s.Cb.Data[si+width])
si++
cr |= ((cr1 + cr2 + 2) << 14) & 0xff0000
cb |= ((cb1 + cb2 + 2) << 14) & 0xff0000
cr1 = uint64(s.Cr.Data[si]) + uint64(s.Cr.Data[si+width])
cb1 = uint64(s.Cb.Data[si]) + uint64(s.Cb.Data[si+width])
si++
cr |= ((cr1 + cr2 + 2) << 22) & 0xff000000
cb |= ((cb1 + cb2 + 2) << 22) & 0xff000000
dCr[di] = uint32(cr)
dCb[di] = uint32(cb)
di++
}
di += scan >> 2
si += scan - 1
}
} else {
for di < last {
cr1 = uint64(s.Cr.Data[si])
cb1 = uint64(s.Cb.Data[si])
si++
for x := 0; x < 2; x++ {
cr2 = uint64(s.Cr.Data[si])
cb2 = uint64(s.Cb.Data[si])
si++
cr = ((cr1 + cr2 + 1) >> 1) & 0xff
cb = ((cb1 + cb2 + 1) >> 1) & 0xff
cr1 = uint64(s.Cr.Data[si])
cb1 = uint64(s.Cb.Data[si])
si++
cr |= ((cr1 + cr2 + 1) << 7) & 0xff00
cb |= ((cb1 + cb2 + 1) << 7) & 0xff00
cr2 = uint64(s.Cr.Data[si])
cb2 = uint64(s.Cb.Data[si])
si++
cr |= ((cr1 + cr2 + 1) << 15) & 0xff0000
cb |= ((cb1 + cb2 + 1) << 15) & 0xff0000
cr1 = uint64(s.Cr.Data[si])
cb1 = uint64(s.Cb.Data[si])
si++
cr |= ((cr1 + cr2 + 1) << 23) & 0xff000000
cb |= ((cb1 + cb2 + 1) << 23) & 0xff000000
dCr[di] = uint32(cr)
dCb[di] = uint32(cb)
di++
}
di += scan >> 2
si += scan - 1
}
}
} else {
if oddV {
for di < last {
for x := 0; x < 2; x++ {
cr = ((uint64(s.Cr.Data[si]) + uint64(s.Cr.Data[si+width]) + 1) >> 1) & 0xff
cb = ((uint64(s.Cb.Data[si]) + uint64(s.Cb.Data[si+width]) + 1) >> 1) & 0xff
si++
cr |= ((uint64(s.Cr.Data[si]) + uint64(s.Cr.Data[si+width]) + 1) << 7) & 0xff00
cb |= ((uint64(s.Cb.Data[si]) + uint64(s.Cb.Data[si+width]) + 1) << 7) & 0xff00
si++
cr |= ((uint64(s.Cr.Data[si]) + uint64(s.Cr.Data[si+width]) + 1) << 15) & 0xff0000
cb |= ((uint64(s.Cb.Data[si]) + uint64(s.Cb.Data[si+width]) + 1) << 15) & 0xff0000
si++
cr |= ((uint64(s.Cr.Data[si]) + uint64(s.Cr.Data[si+width]) + 1) << 23) & 0xff000000
cb |= ((uint64(s.Cb.Data[si]) + uint64(s.Cb.Data[si+width]) + 1) << 23) & 0xff000000
si++
dCr[di] = uint32(cr)
dCb[di] = uint32(cb)
di++
}
di += scan >> 2
si += scan
}
} else {
for di < last {
for x := 0; x < 2; x++ {
cr = uint64(s.Cr.Data[si])
cb = uint64(s.Cb.Data[si])
si++
cr |= uint64(s.Cr.Data[si]) << 8
cb |= uint64(s.Cb.Data[si]) << 8
si++
cr |= uint64(s.Cr.Data[si]) << 16
cb |= uint64(s.Cb.Data[si]) << 16
si++
cr |= uint64(s.Cr.Data[si]) << 24
cb |= uint64(s.Cb.Data[si]) << 24
si++
dCr[di] = uint32(cr)
dCb[di] = uint32(cb)
di++
}
di += scan >> 2
si += scan
}
}
}
}
func (v *Video) decodeBlock(block int) {
var n int
var quantMatrix []byte
// Decode DC coefficient of intra-coded blocks
if v.macroblockIntra {
var predictor int
var dctSize int
// DC prediction
planeIndex := 0
if block > 3 {
planeIndex = block - 3
}
predictor = v.dcPredictor[planeIndex]
dctSize = v.buf.readVlc(videoDctSize[planeIndex])
// Read DC coeff
if dctSize > 0 {
differential := v.buf.read(dctSize)
if (differential & (1 << (dctSize - 1))) != 0 {
v.blockData[0] = predictor + differential
} else {
v.blockData[0] = predictor + ((-1 << dctSize) | (differential + 1))
}
} else {
v.blockData[0] = predictor
}
// Save predictor value
v.dcPredictor[planeIndex] = v.blockData[0]
// Dequantize + premultiply
v.blockData[0] <<= 3 + 5
quantMatrix = v.intraQuantMatrix
n = 1
} else {
quantMatrix = v.nonIntraQuantMatrix
}
// Decode AC coefficients (+DC for non-intra)
level := 0
for {
run := 0
coeff := int(v.buf.readVlcUint(videoDctCoeff))
if (coeff == 0x0001) && (n > 0) && (v.buf.read1() == 0) {
// end_of_block
break
}
if coeff == 0xffff {
// escape
run = v.buf.read(6)
level = v.buf.read(8)
switch {
case level == 0:
level = v.buf.read(8)
case level == 128:
level = v.buf.read(8) - 256
case level > 128:
level -= 256
}
} else {
run = coeff >> 8
level = coeff & 0xff
if (v.buf.read1()) != 0 {
level = -level
}
}
n += run
if n < 0 || n >= 64 {
return // invalid
}
deZigZagged := videoZigZag[n]
n++
// Dequantize, oddify, clip
level <<= 1
if !v.macroblockIntra {
if level < 0 {
level += -1
} else {
level += 1
}
}
level = (level * v.quantizerScale * int(quantMatrix[deZigZagged])) >> 4
if (level & 1) == 0 {
if level > 0 {
level -= 1
} else {
level -= -1
}