-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathvideocodec.h
More file actions
3754 lines (3317 loc) · 159 KB
/
videocodec.h
File metadata and controls
3754 lines (3317 loc) · 159 KB
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
/*
------------------------------------------------------------------------------
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
videocodec.h - v0.1 - Custom mpeg-style video codec for game FMVs.
Do this:
#define VIDEOCODEC_IMPLEMENTATION
before you include this file in *one* C/C++ file to create the implementation.
The file includes two example programs, an encoder commandline app and a basic
player app. They can be copied out of the videocodec.h file and put into
separate .c files, or they can be built directly from videocodec.h like this:
clang -O2 -DVIDEOCODEC_BUILD_ENCODER -DVIDEOCODEC_IMPLEMENTATION -xc videocodec.h -o encoder.exe
clang -O2 -DVIDEOCODEC_BUILD_PLAYER -DVIDEOCODEC_IMPLEMENTATION -xc videocodec.h -o player.exe
cl -O2 -DVIDEOCODEC_BUILD_ENCODER -DVIDEOCODEC_IMPLEMENTATION -Tc videocodec.h -Fe:encoder.exe
cl -O2 -DVIDEOCODEC_BUILD_PLAYER -DVIDEOCODEC_IMPLEMENTATION -Tc videocodec.h -Fe:player.exe
*/
#ifndef videocodec_h
#define videocodec_h
#define _CRT_SECURE_NO_WARNINGS
#include <stddef.h>
#include <stdint.h>
typedef struct videocodec_enc_stats_t { uint64_t frames_total; uint64_t frames_i; uint64_t frames_p;
uint64_t bytes_raw_total; uint64_t bytes_compressed_total; } videocodec_enc_stats_t;
enum videocodec_quality_t { VIDEOCODEC_QUALITY_MIN, VIDEOCODEC_QUALITY_LOW,
VIDEOCODEC_QUALITY_DEFAULT, VIDEOCODEC_QUALITY_HIGH, VIDEOCODEC_QUALITY_MAX };
typedef struct videocodec_enc_t videocodec_enc_t;
videocodec_enc_t* videocodec_enc_create( int width, int height, int fps_n, int fps_d, int sar_n, int sar_d,
enum videocodec_quality_t quality, videocodec_enc_stats_t* out_stats );
void videocodec_enc_destroy( videocodec_enc_t* enc );
typedef struct videocodec_enc_frame_t { size_t size; void* data; } videocodec_enc_frame_t;
videocodec_enc_frame_t videocodec_enc_encode_yuv420( videocodec_enc_t* enc, void const* yuv420 );
videocodec_enc_frame_t videocodec_enc_encode_xbgr( videocodec_enc_t* enc, uint32_t const* xbgr );
videocodec_enc_frame_t videocodec_enc_finalize( videocodec_enc_t* enc );
#define VIDEOCODEC_DEC_HEADER_SIZE 96
typedef struct videocodec_dec_t videocodec_dec_t;
videocodec_dec_t* videocodec_dec_create( uint8_t const data[ VIDEOCODEC_DEC_HEADER_SIZE ] );
void videocodec_dec_destroy( videocodec_dec_t* dec );
int videocodec_dec_width( videocodec_dec_t* dec );
int videocodec_dec_height( videocodec_dec_t* dec );
void videocodec_dec_fps( videocodec_dec_t* dec, int* fps_n, int* fps_d );
void videocodec_dec_ar ( videocodec_dec_t* dec, int* ar_n, int* ar_d );
size_t videocodec_dec_next_frame( videocodec_dec_t* dec, void const* data, size_t size, uint32_t* out_xbgr );
#endif // videocodec_h
/**
videocodec.h
============
Custom mpeg-style video codec for game FMVs.
videocodec_enc_create
---------------------
videocodec_enc_t* videocodec_enc_create( int width, int height, int fps_n, int fps_d, int sar_n, int sar_d,
enum videocodec_quality_t quality, videocodec_enc_stats_t* out_stats )
Creates a new encoder instance for compressing video frames. The video stream is defined by its resolution (`width` x
`height`), frame rate (expressed as a fraction `fps_n/fps_d`), and sample aspect ratio (`sar_n/sar_d`). The `quality`
parameter selects the compression quality, where higher quality means larger output size, and lower quality means
smaller output size.
`width` and `height` must be positive and multiples of 8, or the function returns NULL. If `sar_n` or `sar_d` are
non-positive, they are clamped to 1.
An optional pointer to a `videocodec_enc_stats_t` structure can be provided to receive statistics about the encoding
process. The encoder will update this structure in place after every frame is encoded. This structure is owned by the
caller and must remain valid for the lifetime of the encoder. When no longer needed, the encoder must be destroyed with
`videocodec_enc_destroy`.
videocodec_enc_destroy
----------------------
void videocodec_enc_destroy( videocodec_enc_t* enc )
Releases the resources held by an encoder instance previously created with `videocodec_enc_create`. After calling this
function, the `enc` pointer is no longer valid and must not be used in further API calls.
videocodec_enc_encode_yuv420
----------------------------
videocodec_enc_frame_t videocodec_enc_encode_yuv420( videocodec_enc_t* enc, void const* yuv420 )
Encodes a single raw video frame in planar YUV420 format. The pointer `yuv420` must reference a frame of the dimensions
specified when the encoder was created. The buffer must contain the Y plane followed by the U and V planes.
The function returns a `videocodec_enc_frame_t` containing the compressed frame data and its size. The returned `data`
pointer is owned by the codec and remains valid until the next call to any encode/finalize function or until
`videocodec_enc_destroy` is called. On the first frame, this function automatically writes the stream header before the
compressed data.
videocodec_enc_encode_xbgr
--------------------------
videocodec_enc_frame_t videocodec_enc_encode_xbgr( videocodec_enc_t* enc, uint32_t const* xbgr )
Encodes a single raw video frame in packed 32-bit XBGR format (`0xXXBBGGRR` in memory, i.e. little-endian byte order
R,G,B,X). The pointer `xbgr` must reference a frame of the dimensions specified when the encoder was created.
The function returns a `videocodec_enc_frame_t` containing the compressed frame data and its size. The returned `data`
pointer is owned by the codec and remains valid until the next call to any encode/finalize function or until
`videocodec_enc_destroy` is called. On the first frame, this function automatically writes the stream header before the
compressed data.
videocodec_enc_finalize
-----------------------
videocodec_enc_frame_t videocodec_enc_finalize( videocodec_enc_t* enc )
Finalizes an encoding session, flushing any remaining data from the encoder and emitting the end-of-stream marker. If
the stream header has not yet been written, it will be included as part of the returned data. The function returns a
`videocodec_enc_frame_t` containing the final compressed data needed to complete the stream.
Once this has been called, the encoding session is considered finished. No further frames should be encoded with the
same encoder instance. To begin a new stream, create a new encoder with `videocodec_enc_create`.
videocodec_dec_create
---------------------
videocodec_dec_t* videocodec_dec_create( const uint8_t data[ VIDEOCODEC_DEC_HEADER_SIZE ] )
Creates a new decoder instance from a compressed video stream header. The `data` buffer must contain exactly
`VIDEOCODEC_DEC_HEADER_SIZE` bytes from the start of the compressed stream. The decoder validates the header
signature, version, and parameters. If invalid, the function returns NULL.
When no longer needed, the decoder must be destroyed with `videocodec_dec_destroy`.
videocodec_dec_destroy
----------------------
void videocodec_dec_destroy( videocodec_dec_t* dec )
Releases the resources held by a decoder instance previously created with `videocodec_dec_create`. After calling this
function, the `dec` pointer is no longer valid and must not be used in further API calls.
videocodec_dec_width
--------------------
int videocodec_dec_width( videocodec_dec_t* dec )
Returns the width, in pixels, of the decoded video stream.
videocodec_dec_height
---------------------
int videocodec_dec_height( videocodec_dec_t* dec )
Returns the height, in pixels, of the decoded video stream.
videocodec_dec_fps
------------------
void videocodec_dec_fps( videocodec_dec_t* dec, int* fps_n, int* fps_d )
Retrieves the frame rate of the decoded video stream, expressed as a rational fraction `fps_n/fps_d`.
videocodec_dec_ar
-----------------
void videocodec_dec_ar( videocodec_dec_t* dec, int* ar_n, int* ar_d )
Retrieves the sample aspect ratio of the decoded video stream, expressed as a rational fraction `ar_n/ar_d`.
videocodec_dec_next_frame
-------------------------
size_t videocodec_dec_next_frame( videocodec_dec_t* dec, const void* data, size_t size, uint32_t* out_xbgr )
Decodes the next frame from the compressed bitstream. The parameters `data` and `size` specify the available compressed
data, and `out_xbgr` must point to a buffer large enough to hold the decoded frame in 32-bit XBGR format.
The return value indicates how many bytes are required to provide the next frame. If you wish to query the number of
bytes needed without supplying data, pass `NULL` for `data`, zero for `size`, and `NULL` for `out_xbgr`.
A return value of 0 indicates either end-of-stream or that the provided input was insufficient/invalid; in both cases,
no frame was produced, and the stream can be considered ended.
*/
#ifdef VIDEOCODEC_IMPLEMENTATION
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#if defined( VIDEOCODEC_PACK ) || defined( VIDEOCODEC_UNPACK ) || defined( VIDEOCODEC_PACK_ARENA_SIZE )
#if !defined( VIDEOCODEC_PACK ) || !defined( VIDEOCODEC_UNPACK ) || !defined( VIDEOCODEC_PACK_ARENA_SIZE )
#error "You can not #define only some of VIDEOCODEC_PACK, VIDEOCODEC_UNPACK or VIDEOCODEC_PACK_ARENA_SIZE. It's all or none."
#endif
#else
int internal_videocodec_inflate( void* out, int cap, void const* in, int size ) {
uint8_t* dst = ( uint8_t* )out, *ode = dst + cap, *o0 = dst;
uint8_t const* ip = ( uint8_t const* )in, *ie = ip + size;
uint64_t bitbuf = 0; int bitcnt = 0;
static const uint8_t order[ 19 ]= {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
static const short dbase[ 30 ]= {1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
static const uint8_t dbits[ 30 ]= {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
static const short lbase[ 29 ]= {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
static const uint8_t lbits[ 29 ]= {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
uint8_t ll_lens[ 288 ], dd_lens[ 32 ], cl_lens[ 19 ]; int ll_cnt[ 16 ], dd_cnt[ 16 ], cl_cnt[ 16 ]; int ll_off[ 16 ], dd_off[ 16 ], cl_off[ 16 ];
uint16_t ll_rev[ 288 ], dd_rev[ 32 ], cl_rev[ 19 ]; uint16_t ll_sym[ 288 ], dd_sym[ 32 ], cl_sym[ 19 ];
int ll_max = 0, dd_max = 0, cl_max = 0;
#define INTERNAL_INFLATE_ENSURE( n ) while( bitcnt <( n ) ) { if( ip >= ie ) return( int )( dst - o0 ); bitbuf |= ( uint64_t )( *ip++ ) << bitcnt; bitcnt += 8; }
#define INTERNAL_INFLATE_GETBITS( n, v ) do { INTERNAL_INFLATE_ENSURE( n );( v ) = ( int )( bitbuf &( ( 1u<<( n ) ) - 1u ) ); bitbuf >>= ( n ); bitcnt -= ( n ); } while( 0 )
#define INTERNAL_INFLATE_GET1( v ) do { INTERNAL_INFLATE_ENSURE( 1 );( v ) = ( int )( bitbuf & 1u ); bitbuf >>= 1; bitcnt -= 1; } while( 0 )
#define INTERNAL_INFLATE_BUILD_DECODE( LENS, SYMCNT, CNT, OFF, REV, SYM, MAXL ) do{\
int _i, _b; uint16_t _code[ 16 ]; uint16_t _next[ 16 ]; MAXL = 0; \
for( _b = 0;_b<16;_b++ ) { CNT[ _b ]= 0; OFF[ _b ]= 0; _code[ _b ]= 0; _next[ _b ]= 0; } \
for( _i = 0; _i < ( SYMCNT ); ++_i ) { int L = ( LENS )[ _i ]; if( L<0||L>15 ) L = 0; if( L ) { CNT[ L ]++; if( L>MAXL ) MAXL = L; } } \
OFF[ 0 ]= 0; for( _b = 1; _b <= 15; ++_b ) OFF[ _b ]= OFF[ _b-1 ]+ CNT[ _b-1 ]; \
_code[ 0 ]= 0; for( _b = 1; _b <= 15; ++_b ) _code[ _b ]= ( uint16_t )( ( _code[ _b-1 ]+ CNT[ _b-1 ])<<1 ); \
for( _b = 1; _b <= 15; ++_b ) _next[ _b ]= _code[ _b ]; \
for( _i = 0; _i < ( SYMCNT ); ++_i ) { int L = ( LENS )[ _i ]; if( L ) { uint16_t c = _next[ L ]++; uint16_t r = 0; int k = L; while( k-- ) { r = ( uint16_t )( ( r<<1 )|( c&1 ) ); c>>= 1; } \
{ int pos = OFF[ L ]++; REV[ pos ]= r; SYM[ pos ]= ( uint16_t )_i; } } } \
for( _b = 15; _b>= 1; --_b ) { OFF[ _b ]= OFF[ _b-1 ]; } OFF[ 0 ]= 0; \
}while( 0 )
#define INTERNAL_INFLATE_BUILD_HUFF_DECODE( SYM, CNT, OFF, REV, SYMS, MAXL ) do{\
int _len = 0, _code = 0, _found = 0; \
while( !_found ) { int _bit; INTERNAL_INFLATE_GET1( _bit ); _code |= ( _bit<<_len ); ++_len; if( _len>( MAXL ) ) return( int )( dst - o0 ); \
{ int _s = OFF[ _len ], _c = CNT[ _len ], _k; for( _k = 0; _k<_c; ++_k ) { if( REV[ _s+_k ]== ( unsigned )_code ) {( SYM ) = SYMS[ _s+_k ]; _found = 1; break; } } } \
} \
}while( 0 )
enum {ST_HDR, ST_STO, ST_FIX, ST_DYN, ST_BLK}; int state = ST_HDR, last = 0;
for( ;; ) {
if( state == ST_HDR ) {
int type; INTERNAL_INFLATE_GET1( last ); INTERNAL_INFLATE_GETBITS( 2, type );
if( type == 0 ) state = ST_STO; else if( type == 1 ) state = ST_FIX; else if( type == 2 ) state = ST_DYN; else return( int )( dst - o0 );
} else if( state == ST_STO ) {
int pad = bitcnt & 7; if( pad ) { bitbuf >>= pad; bitcnt -= pad; }
{ int len, nlen; INTERNAL_INFLATE_GETBITS( 16, len ); INTERNAL_INFLATE_GETBITS( 16, nlen );
if( ( uint16_t )len != ( uint16_t )( ~( unsigned )nlen ) ) return( int )( dst - o0 );
if( ip + len > ie ) return( int )( dst - o0 );
if( dst + len > ode ) return -2;
memcpy( dst, ip, ( size_t )len ); dst += len; ip += len;
if( last ) return( int )( dst - o0 ); state = ST_HDR; }
} else if( state == ST_FIX ) {
int i; for( i = 0; i < 288; i++ ) ll_lens[ i ]= 0; for( i = 0; i <= 143; i++ ) ll_lens[ i ]= 8; for( i = 144; i <= 255; i++ ) ll_lens[ i ]= 9; for( i = 256; i <= 279; i++ ) ll_lens[ i ]= 7; for( i = 280; i <= 287; i++ ) ll_lens[ i ]= 8;
for( i = 0; i < 32; i++ ) dd_lens[ i ]= 5;
INTERNAL_INFLATE_BUILD_DECODE( ll_lens, 288, ll_cnt, ll_off, ll_rev, ll_sym, ll_max );
INTERNAL_INFLATE_BUILD_DECODE( dd_lens, 32 , dd_cnt, dd_off, dd_rev, dd_sym, dd_max );
state = ST_BLK;
} else if( state == ST_DYN ) {
int i, nlit, ndist, nlen, npos = 0;
for( i = 0; i < 19; i++ ) cl_lens[ i ]= 0;
INTERNAL_INFLATE_GETBITS( 5, nlit ); nlit += 257;
INTERNAL_INFLATE_GETBITS( 5, ndist ); ndist += 1;
INTERNAL_INFLATE_GETBITS( 4, nlen ); nlen += 4;
for( i = 0; i < nlen; i++ ) { int v; INTERNAL_INFLATE_GETBITS( 3, v ); cl_lens[ order[ i ] ]= ( uint8_t )v; }
INTERNAL_INFLATE_BUILD_DECODE( cl_lens, 19, cl_cnt, cl_off, cl_rev, cl_sym, cl_max );
for( i = 0; i < 288; i++ ) ll_lens[ i ]= 0; for( i = 0; i < 32; i++ ) dd_lens[ i ]= 0;
while( npos <( nlit + ndist ) ) {
int csym; INTERNAL_INFLATE_BUILD_HUFF_DECODE( csym, cl_cnt, cl_off, cl_rev, cl_sym, cl_max );
if( csym <= 15 ) {
if( npos < nlit ) ll_lens[ npos++ ]= ( uint8_t )csym; else dd_lens[ npos++ - nlit ]= ( uint8_t )csym;
} else if( csym == 16 ) {
int rep, prev; if( npos == 0 ) return( int )( dst - o0 );
INTERNAL_INFLATE_GETBITS( 2, rep ); rep += 3;
prev = ( npos <= nlit ) ?( npos-1 < nlit ? ll_lens[ npos-1 ]: ll_lens[ nlit-1 ]) : dd_lens[ ( npos-1 )-nlit ];
while( rep-- ) { if( npos < nlit ) ll_lens[ npos++ ]= ( uint8_t )prev; else dd_lens[ npos++ - nlit ]= ( uint8_t )prev; }
} else if( csym == 17 ) {
int rep; INTERNAL_INFLATE_GETBITS( 3, rep ); rep += 3; while( rep-- ) { if( npos < nlit ) ll_lens[ npos++ ]= 0; else dd_lens[ npos++ - nlit ]= 0; }
} else {
int rep; INTERNAL_INFLATE_GETBITS( 7, rep ); rep += 11; while( rep-- ) { if( npos < nlit ) ll_lens[ npos++ ]= 0; else dd_lens[ npos++ - nlit ]= 0; }
}
}
INTERNAL_INFLATE_BUILD_DECODE( ll_lens, nlit , ll_cnt, ll_off, ll_rev, ll_sym, ll_max );
INTERNAL_INFLATE_BUILD_DECODE( dd_lens, ndist, dd_cnt, dd_off, dd_rev, dd_sym, dd_max );
state = ST_BLK;
} else {
for( ;; ) {
int sym; INTERNAL_INFLATE_BUILD_HUFF_DECODE( sym, ll_cnt, ll_off, ll_rev, ll_sym, ll_max );
if( sym < 256 ) {
if( dst >= ode ) return -2;
*dst++= ( uint8_t )sym;
} else if( sym == 256 ) {
if( last ) return( int )( dst - o0 );
state = ST_HDR; break;
} else {
int len, dsym, offs;
if( sym >= 286 ) return( int )( dst - o0 );
sym -= 257;
{ int eb = lbits[ sym ]; int add = 0; if( eb ) { INTERNAL_INFLATE_GETBITS( eb, add ); } len = lbase[ sym ]+ add; }
INTERNAL_INFLATE_BUILD_HUFF_DECODE( dsym, dd_cnt, dd_off, dd_rev, dd_sym, dd_max );
if( dsym >= 30 ) return( int )( dst - o0 );
{ int eb = dbits[ dsym ]; int add = 0; if( eb ) { INTERNAL_INFLATE_GETBITS( eb, add ); } offs = dbase[ dsym ]+ add; }
if( offs <= 0 || offs >( int )( dst - o0 ) ) return( int )( dst - o0 );
if( dst + len > ode ) return -2;
{ uint8_t *s = dst - offs, *d = dst, *e = dst + len; if( offs == 1 ) { uint8_t v = *s; while( d < e ) { *d++= v; } } else { while( d < e ) { *d++= *s++; } } dst = e; }
}
}
}
}
#undef INTERNAL_INFLATE_ENSURE
#undef INTERNAL_INFLATE_GETBITS
#undef INTERNAL_INFLATE_GET1
#undef INTERNAL_INFLATE_BUILD_DECODE
#undef INTERNAL_INFLATE_BUILD_HUFF_DECODE
}
#define INTERNAL_VIDEOCODEC_DEFLATE_ARENA_SIZE 1316320
int internal_videocodec_deflate( void* out_buf, void const* in_buf, int n_bytes, uint8_t arena[ INTERNAL_VIDEOCODEC_DEFLATE_ARENA_SIZE ]) {
if( n_bytes <= 0 ) return 0;
if( !out_buf || !in_buf ) { int max_blocks = 1 +( ( n_bytes + 65535 ) / 65536 ); int bound = 5 * max_blocks + n_bytes + 1 + 4 + 8 + 3; return bound; }
enum { WBITS = 15, WSIZE = 1 << WBITS, WMASK = WSIZE - 1, HBITS = 15, HSIZE = 1 << HBITS, MINM = 4, MAXM = 258, BLKMAX = 256 * 1024, LIT_EOB = 256, MAXBITS = 15, MAX_CHAIN = 1 << 13 };
static const uint16_t LBASE[ 29 ]= {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
static const uint8_t LBITS[ 29 ]= {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
static const uint16_t DBASE[ 30 ]= {1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
static const uint8_t DBITS[ 30 ]= {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
static const uint8_t PREPERM[ 19 ]= {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
int32_t *head = ( int32_t* )arena; arena += ( size_t )HSIZE * sizeof( int32_t );
int32_t *link = ( int32_t* )arena; arena += ( size_t )WSIZE * sizeof( int32_t );
uint16_t *tok_len = ( uint16_t* )arena; arena += ( size_t )BLKMAX * sizeof( uint16_t );
uint16_t *tok_dist = ( uint16_t* )arena; arena += ( size_t )BLKMAX * sizeof( uint16_t );
int32_t *heap_w = ( int32_t* )arena; arena += ( size_t )700 * sizeof( int32_t );
int32_t *heap_s = ( int32_t* )arena; arena += ( size_t )700 * sizeof( int32_t );
for( int i = 0; i < HSIZE;++i ) head[ i ]= -1; for( int i = 0; i < WSIZE;++i ) link[ i ]= -1;
unsigned char *op = ( unsigned char* )out_buf; uint32_t bw_acc = 0; int bw_n = 0;
#define INTERNAL_DEFLATE_BW_PUT( v, nb ) do{ uint32_t _nb = ( nb ); if( _nb ) { bw_acc |= ( ( uint32_t )( v ) << bw_n ); bw_n += ( int )_nb; while( bw_n >= 8 ) { *op++= ( unsigned char )( bw_acc & 255u ); bw_acc >>= 8; bw_n -= 8; } } }while( 0 )
#define INTERNAL_DEFLATE_BW_ALIGN( ) do{ if( bw_n ) INTERNAL_DEFLATE_BW_PUT( 0, 8 - bw_n ); }while( 0 )
#define INTERNAL_DEFLATE_BW_U16LE( x ) do{ INTERNAL_DEFLATE_BW_ALIGN( ); *op++= ( unsigned char )( ( x )&255u ); *op++= ( unsigned char )( ( ( x )>>8 )&255u ); }while( 0 )
uint8_t const* in = ( uint8_t const* )in_buf;
#define INTERNAL_DEFLATE_RD32_LE( p )( ( uint32_t )( ( p )[ 0 ]|( ( uint32_t )( p )[ 1 ]<<8 ) |( ( uint32_t )( p )[ 2 ]<<16 ) |( ( uint32_t )( p )[ 3 ]<<24 ) ) )
#define INTERNAL_DEFLATE_HASH4( p ) ( ( uint32_t )( ( INTERNAL_DEFLATE_RD32_LE( p ) * 0x9E377989u ) >>( 32 - HBITS ) ) )
#define INTERNAL_DEFLATE_LT( a, b )( ( heap_w[ a ]< heap_w[ b ]) ||( heap_w[ a ]== heap_w[ b ]&& heap_s[ a ]< heap_s[ b ]) )
#define INTERNAL_DEFLATE_SIFTDOWN( n, i ) do{ int _r = ( i ); for( ;; ) { int _l = ( _r<<1 )+1, _m = _r; if( _l<( n ) && INTERNAL_DEFLATE_LT( _l, _m ) ) _m = _l; if( _l+1<( n ) && INTERNAL_DEFLATE_LT( _l+1, _m ) ) _m = _l+1; if( _m == _r ) break; int32_t _tw = heap_w[ _r ], _ts = heap_s[ _r ]; heap_w[ _r ]= heap_w[ _m ]; heap_s[ _r ]= heap_s[ _m ]; heap_w[ _m ]= _tw; heap_s[ _m ]= _ts; _r = _m; } }while( 0 )
#define INTERNAL_DEFLATE_HEAP_PUSH( n, w, s ) do{ int _c = ( n )++; heap_w[ _c ]= ( w ); heap_s[ _c ]= ( s ); while( _c>0 ) { int _p = ( _c-1 )>>1; if( !INTERNAL_DEFLATE_LT( _c, _p ) ) break; int32_t _tw = heap_w[ _p ], _ts = heap_s[ _p ]; heap_w[ _p ]= heap_w[ _c ]; heap_s[ _p ]= heap_s[ _c ]; heap_w[ _c ]= _tw; heap_s[ _c ]= _ts; _c = _p; } }while( 0 )
#define INTERNAL_DEFLATE_HEAP_POP( n, ow, os ) do{( ow ) = heap_w[ 0 ];( os ) = heap_s[ 0 ]; --( n ); heap_w[ 0 ]= heap_w[ ( n ) ]; heap_s[ 0 ]= heap_s[ ( n ) ]; INTERNAL_DEFLATE_SIFTDOWN( ( n ), 0 ); }while( 0 )
#define INTERNAL_DEFLATE_BUILD_LEN( freqArr, NS, MAXB_, outLens ) do{ \
uint8_t rawL[ NS ]; memset( rawL, 0, sizeof( rawL ) ); int hn = 0; \
for( int _i = 0; _i < ( NS ); ++_i ) if( ( freqArr )[ _i ]) INTERNAL_DEFLATE_HEAP_PUSH( hn, ( int32_t )( freqArr )[ _i ], _i ); \
if( hn == 0 ) { memset( ( outLens ), 0, ( NS ) ); } else { \
if( hn == 1 ) { INTERNAL_DEFLATE_HEAP_PUSH( hn, heap_w[ 0 ], heap_s[ 0 ]== 0?1:0 ); } \
int ip = 0; int32_t Lc[ 700 ], Rc[ 700 ]; \
while( hn>= 2 ) { int32_t w1, s1, w2, s2; INTERNAL_DEFLATE_HEAP_POP( hn, w1, s1 ); INTERNAL_DEFLATE_HEAP_POP( hn, w2, s2 ); Lc[ ip ]= s1; Rc[ ip ]= s2; INTERNAL_DEFLATE_HEAP_PUSH( hn, w1+w2, ( NS )+ip ); ip++; } \
int stkN[ 700 ], stkD[ 700 ], sp = 0; stkN[ sp ]= heap_s[ 0 ]; stkD[ sp ]= 0; sp++; \
while( sp ) { int n = stkN[ --sp ], d = stkD[ sp ]; if( n<( NS ) ) { if( ( freqArr )[ n ]) rawL[ n ]= ( uint8_t )d; } else { int j = n-( NS ); stkN[ sp ]= Lc[ j ]; stkD[ sp ]= d+1; sp++; stkN[ sp ]= Rc[ j ]; stkD[ sp ]= d+1; sp++; } } \
uint32_t blc[ 600 ]; memset( blc, 0, sizeof( blc ) ); int overflow = 0; int cnt = 0; \
for( int _i = 0; _i < ( NS ); ++_i ) { if( ( freqArr )[ _i ]) { cnt++; int d = rawL[ _i ]; if( d>MAXB_ ) { d = MAXB_; overflow++; } blc[ d ]++; } } \
while( overflow>0 ) { int b = ( MAXB_ )-1; while( b>0 && !blc[ b ]) b--; blc[ b ]--; blc[ b+1 ]+= 2; blc[ MAXB_ ]--; overflow-= 2; } \
int ord[ 700 ], p = 0; for( int _i = 0; _i < ( NS ); ++_i ) if( ( freqArr )[ _i ]) ord[ p++ ]= _i; \
for( int a = 1;a<cnt;++a ) { int v = ord[ a ], j = a-1; while( j>= 0 &&( ( freqArr )[ ord[ j ] ]<( freqArr )[ v ]||( ( freqArr )[ ord[ j ] ]== ( freqArr )[ v ]&& ord[ j ]< v ) ) ) { ord[ j+1 ]= ord[ j ]; j--; } ord[ j+1 ]= v; } \
memset( ( outLens ), 0, ( NS ) ); int posi = 0; for( int b = 1; b <= ( MAXB_ ); ++b ) { int k = ( int )blc[ b ]; while( k-- > 0 && posi < cnt ) {( outLens )[ ord[ posi++ ] ]= ( uint8_t )b; } } \
} \
}while( 0 )
#define INTERNAL_DEFLATE_GEN_CANON_CODES( LEN, NS, MAXB_, OUT ) do{ uint16_t t_[ NS ]; memset( t_, 0, sizeof( t_ ) ); uint16_t code = 0; for( int len = 1;len <= ( MAXB_ );++len ) { for( int sym = 0;sym<( NS );++sym ) { if( ( LEN )[ sym ]== len ) { t_[ sym ]= code++; } } code <<= 1; } for( int c = 0; c<( NS ); ++c ) { if( ( LEN )[ c ]) { unsigned _temp = t_[ c ]; _temp = ( ( _temp&0x5555 )<<1 )|( ( _temp&0xAAAA )>>1 ); _temp = ( ( _temp&0x3333 )<<2 )|( ( _temp&0xCCCC )>>2 ); _temp = ( ( _temp&0x0F0F )<<4 )|( ( _temp&0xF0F0 )>>4 ); _temp = ( ( _temp&0x00FF )<<8 )|( ( _temp&0xFF00 )>>8 ); t_[ c ]= _temp >>( 16-( LEN )[ c ]); } } memcpy( ( OUT ), t_, sizeof( t_ ) ); }while( 0 )
#define INTERNAL_DEFLATE_FIND_LENGTH_CODE( LEN, LC, LXB, LXV ) do{ if( ( LEN ) == 258 ) {( LC ) = 285; } else {( LC ) = 257; for( int z = 0;z<29;++z ) { int base = LBASE[ z ], eb = LBITS[ z ], maxv = base+( ( 1<<eb )-1 ); if( ( LEN )>= base&&( LEN ) <= maxv ) {( LC ) = 257+z;( LXB ) = eb;( LXV ) = ( LEN )-base;break; } } } }while( 0 )
#define INTERNAL_DEFLATE_FIND_DISTANCE_CODE( DIST, DC, DXB, DXV ) do{( DC ) = 0; for( int z = 0;z < 30;++z ) { int base = DBASE[ z ], eb = DBITS[ z ], maxv = base+( ( 1<<eb )-1 ); if( ( DIST )>= base&&( DIST ) <= maxv ) {( DC ) = z;( DXB ) = eb;( DXV ) = ( DIST )-base;break; } } }while( 0 )
#define INTERNAL_DEFLATE_FIND_MATCH( OFF, MATCH, MAX_MATCH ) do{ uint32_t h = INTERNAL_DEFLATE_HASH4( in +( OFF ) ); int c = head[ h ]; int limit = ( ( OFF ) - WSIZE < -1 ) ? -1 :( ( OFF ) - WSIZE ); int chain_len = MAX_CHAIN; \
while( c > limit ) { if( c + MINM <= blk_end && in[ c +( MATCH ).len ]== in[ ( OFF ) +( MATCH ).len ]&& INTERNAL_DEFLATE_RD32_LE( in + c ) == INTERNAL_DEFLATE_RD32_LE( in +( OFF ) ) ) { int n = MINM; while( n <( MAX_MATCH ) && in[ c + n ]== in[ ( OFF ) + n ]) { n++; } if( n >( MATCH ).len ) {( MATCH ).len = n;( MATCH ).off = ( OFF ) - c; if( n == ( MAX_MATCH ) ) break; } } if( !( --chain_len ) ) break; c = link[ c & WMASK ]; } }while( 0 )
for( int pos = 0; pos < n_bytes; ) {
int blk_end = pos + BLKMAX; if( blk_end > n_bytes ) blk_end = n_bytes; int litlen = 0; int ntok = 0; int i = pos;
uint32_t flit[ 286 ]; memset( flit, 0, sizeof( flit ) ); uint32_t fdis[ 30 ]; memset( fdis, 0, sizeof( fdis ) );
while( i < blk_end ) {
struct { int off; int len; } m = {0, 0};
int left = blk_end - i; int max_match = ( left > MAXM ) ? MAXM : left; int run = 1;
if( max_match > MINM ) { INTERNAL_DEFLATE_FIND_MATCH( i, m, max_match ); }
if( m.len >= MINM ) {
const int nice_match = 130;
if( m.len + 1 < nice_match ) {
struct { int off; int len; } m2 = {0, 0};
if( i + 1 < blk_end && i + 1 + MINM <= blk_end ) {
int max_match2 = m.len + 1;
int remain2 = blk_end -( i + 1 );
if( max_match2 > remain2 ) max_match2 = remain2;
INTERNAL_DEFLATE_FIND_MATCH( i + 1, m2, max_match2 );
}
if( m2.len > m.len ) { m.len = 0; }
}
}
if( m.len >= MINM ) {
if( litlen ) { for( int j = i - litlen; j < i; j++ ) { tok_len[ ntok ]= in[ j ]; tok_dist[ ntok ]= 0; ntok++; flit[ in[ j ] ]++; } litlen = 0; }
int lc, lxb = 0, lxv = 0; INTERNAL_DEFLATE_FIND_LENGTH_CODE( m.len, lc, lxb, lxv );
int dc = 0, dxb = 0, dxv = 0; INTERNAL_DEFLATE_FIND_DISTANCE_CODE( m.off, dc, dxb, dxv );
flit[ lc ]++; fdis[ dc ]++;
tok_len[ ntok ]= ( uint16_t )m.len; tok_dist[ ntok ]= ( uint16_t )m.off; ntok++;
run = m.len;
} else {
litlen++;
}
if( blk_end -( i + run ) > MINM ) { while( run-- > 0 ) { if( i+MINM <= blk_end ) { uint32_t _h = INTERNAL_DEFLATE_HASH4( in + i ); link[ i&WMASK ]= head[ _h ]; head[ _h ]= i; } i ++; } } else { i += run; }
}
if( litlen ) for( int j = i - litlen; j < i; j++ ) { tok_len[ ntok ]= in[ j ]; tok_dist[ ntok ]= 0; ntok++; flit[ in[ j ] ]++; }
flit[ LIT_EOB ]++;
uint8_t ll_len[ 286 ], d_len[ 30 ]; memset( ll_len, 0, sizeof( ll_len ) ); memset( d_len, 0, sizeof( d_len ) );
INTERNAL_DEFLATE_BUILD_LEN( flit, 286, 15, ll_len );
INTERNAL_DEFLATE_BUILD_LEN( fdis, 30, 15, d_len );
int HLIT = 286; while( HLIT>257 && ll_len[ HLIT-1 ]== 0 ) HLIT--;
int HDIST = 30; while( HDIST>1 && d_len[ HDIST-1 ]== 0 ) HDIST--;
if( HDIST == 1 && d_len[ 0 ]== 0 ) d_len[ 0 ]= 1;
uint16_t codeL[ 286 ], codeD[ 30 ];
INTERNAL_DEFLATE_GEN_CANON_CODES( ll_len, 286, MAXBITS, codeL );
INTERNAL_DEFLATE_GEN_CANON_CODES( d_len, 30, MAXBITS, codeD );
#define INTERNAL_DEFLATE_CL_PUSH( S, X ) do{ cl[ cln ].sym = ( uint8_t )( S ); cl[ cln ].xb = ( uint8_t )( X ); cln++; pref[ ( S ) ]++; }while( 0 )
struct { uint8_t sym; uint8_t xb; } cl[ 700 ]; int cln = 0;
uint32_t pref[ 19 ]; memset( pref, 0, sizeof( pref ) );
int tot = HLIT + HDIST; uint8_t cat[ 316 ];
for( int a = 0;a<HLIT;++a ) cat[ a ]= ll_len[ a ];
for( int a = 0;a<HDIST;++a ) cat[ HLIT+a ]= d_len[ a ];
for( int q, p = 0; p<tot; p = q ) {
uint8_t L = cat[ p ]; q = p+1; while( q<tot && cat[ q ]== L ) q++; int run = q-p;
if( L == 0 ) {
while( run >= 11 ) { int r = run-11; if( r>127 ) r = 127; INTERNAL_DEFLATE_CL_PUSH( 18, r ); run -= 11 + r; }
if( run >= 3 ) { int r = run-3; if( r>7 ) r = 7; INTERNAL_DEFLATE_CL_PUSH( 17, r ); run -= 3 + r; }
while( run-- ) { INTERNAL_DEFLATE_CL_PUSH( 0, 0 ); }
} else {
INTERNAL_DEFLATE_CL_PUSH( L, 0 ); run--;
while( run >= 3 ) { int r = run-3; if( r>3 ) r = 3; INTERNAL_DEFLATE_CL_PUSH( 16, r ); run -= 3 + r; }
while( run-- ) { INTERNAL_DEFLATE_CL_PUSH( L, 0 ); }
}
}
uint8_t pre_len[ 19 ]; INTERNAL_DEFLATE_BUILD_LEN( pref, 19, 7, pre_len );
uint16_t pre_code[ 19 ];
uint16_t blc_[ 16 ]= {0}; for( int _i = 0; _i < 19; ++_i ) if( pre_len[ _i ]) blc_[ pre_len[ _i ] ]++;
uint16_t next_[ 16 ]= {0}; uint16_t _c = 0; for( int b = 1;b <= 7; ++b ) { _c = ( uint16_t )( ( _c+blc_[ b-1 ])<<1 ); next_[ b ]= _c; }
for( int _i = 0; _i < 19; ++_i ) { uint8_t L = pre_len[ _i ]; if( !L ) { pre_code[ _i ]= 0; } else { uint16_t c = next_[ L ]++, r = 0; for( int t = 0;t<L;++t ) { r = ( uint16_t )( ( r<<1 )|( c&1 ) ); c>>= 1; } pre_code[ _i ]= r; } }
int HCLEN = 4; for( int i2 = 19; i2>4;--i2 ) { if( pre_len[ PREPERM[ i2-1 ] ]) { HCLEN = i2; break; } }
uint64_t dyn_bits = 3 + 5 + 5 + 4 +( uint64_t )HCLEN * 3;
for( int i2 = 0; i2<cln;++i2 ) { uint8_t s = cl[ i2 ].sym; dyn_bits += pre_len[ s ]+( s == 16?2:s == 17?3:s == 18?7:0 ); }
for( int s = 0;s<286;++s ) { uint32_t f = flit[ s ]; if( f ) { dyn_bits += ( uint64_t )f *( ( s <= 255||s == LIT_EOB ) ? ll_len[ s ]:( ll_len[ s ]+ LBITS[ s-257 ]) ); } }
for( int s = 0;s<30;++s ) { uint32_t f = fdis[ s ]; if( f ) dyn_bits += ( uint64_t )f *( d_len[ s ]+ DBITS[ s ]); }
int remain = ( blk_end - pos ); uint64_t bits = 0; while( remain > 0 ) { int chunk = remain > 65535 ? 65535 : remain; bits += 3; bits += ( ( 8 -( bits & 7u ) ) & 7u ); bits += ( 4 +( uint64_t )chunk ) * 8u; remain -= chunk; }
if( bits <= dyn_bits ) {
int remain = ( blk_end - pos ), off = pos;
while( remain > 0 ) {
int chunk = remain > 65535 ? 65535 : remain;
INTERNAL_DEFLATE_BW_PUT( ( blk_end == n_bytes ) &&( remain == chunk ) ? 1u : 0u, 1 ); INTERNAL_DEFLATE_BW_PUT( 0u, 2 );
INTERNAL_DEFLATE_BW_ALIGN( ); INTERNAL_DEFLATE_BW_U16LE( ( uint16_t )chunk ); INTERNAL_DEFLATE_BW_U16LE( ( uint16_t )~chunk );
memcpy( op, in + off, ( size_t )chunk ); op += chunk;
off += chunk; remain -= chunk;
}
} else {
INTERNAL_DEFLATE_BW_PUT( ( blk_end == n_bytes ) ? 1u : 0u, 1 ); INTERNAL_DEFLATE_BW_PUT( 2u, 2 );
INTERNAL_DEFLATE_BW_PUT( ( uint32_t )( HLIT - 257 ), 5 );
INTERNAL_DEFLATE_BW_PUT( ( uint32_t )( HDIST - 1 ), 5 );
INTERNAL_DEFLATE_BW_PUT( ( uint32_t )( HCLEN - 4 ), 4 );
for( int k = 0;k<HCLEN;++k ) INTERNAL_DEFLATE_BW_PUT( pre_len[ PREPERM[ k ] ], 3 );
for( int k = 0;k<cln;++k ) {
uint8_t s = cl[ k ].sym; INTERNAL_DEFLATE_BW_PUT( pre_code[ s ], pre_len[ s ]);
if( s == 16 ) INTERNAL_DEFLATE_BW_PUT( cl[ k ].xb, 2 );
else if( s == 17 ) INTERNAL_DEFLATE_BW_PUT( cl[ k ].xb, 3 );
else if( s == 18 ) INTERNAL_DEFLATE_BW_PUT( cl[ k ].xb, 7 );
}
for( int t = 0;t<ntok;++t ) {
if( tok_dist[ t ]== 0 ) {
uint16_t lit = tok_len[ t ];
INTERNAL_DEFLATE_BW_PUT( codeL[ lit ], ll_len[ lit ]);
} else {
int L = tok_len[ t ], D = tok_dist[ t ];
int lc, lxb = 0, lxv = 0; INTERNAL_DEFLATE_FIND_LENGTH_CODE( L, lc, lxb, lxv );
int dc = 0, dxb = 0, dxv = 0; INTERNAL_DEFLATE_FIND_DISTANCE_CODE( D, dc, dxb, dxv );
INTERNAL_DEFLATE_BW_PUT( codeL[ lc ], ll_len[ lc ]); if( lxb ) INTERNAL_DEFLATE_BW_PUT( ( uint32_t )lxv, lxb );
INTERNAL_DEFLATE_BW_PUT( codeD[ dc ], d_len[ dc ]); if( dxb ) INTERNAL_DEFLATE_BW_PUT( ( uint32_t )dxv, dxb );
}
}
INTERNAL_DEFLATE_BW_PUT( codeL[ LIT_EOB ], ll_len[ LIT_EOB ]);
}
pos = blk_end;
}
INTERNAL_DEFLATE_BW_ALIGN( );
return( int )( op -( unsigned char* )out_buf );
#undef INTERNAL_DEFLATE_BUILD_LEN
#undef INTERNAL_DEFLATE_HEAP_POP
#undef INTERNAL_DEFLATE_HEAP_PUSH
#undef INTERNAL_DEFLATE_SIFTDOWN
#undef INTERNAL_DEFLATE_LT
#undef INTERNAL_DEFLATE_HASH4
#undef INTERNAL_DEFLATE_RD32_LE
#undef INTERNAL_DEFLATE_BW_U16LE
#undef INTERNAL_DEFLATE_CL_PUSH
#undef INTERNAL_DEFLATE_BW_PUT
#undef INTERNAL_DEFLATE_BW_ALIGN
#undef INTERNAL_DEFLATE_GEN_CANON_CODES
#undef INTERNAL_DEFLATE_FIND_LENGTH_CODE
#undef INTERNAL_DEFLATE_FIND_DISTANCE_CODE
#undef INTERNAL_DEFLATE_FIND_MATCH
}
#define VIDEOCODEC_PACK_ARENA_SIZE INTERNAL_VIDEOCODEC_DEFLATE_ARENA_SIZE
#define VIDEOCODEC_PACK( out, in, size, arena ) internal_videocodec_deflate( out, in, size, arena )
#define VIDEOCODEC_UNPACK( out, capacity, in, size ) internal_videocodec_inflate( out, capacity, in, size )
#endif
static const uint8_t INTERNAL_VIDEOCODEC_VERSION = 0;
typedef struct internal_videocodec_quality_params_t {
int q_ac_y_num, q_ac_y_den;
int q_ac_c_num, q_ac_c_den;
int q_dc_y_num, q_dc_y_den;
int q_dc_c_num, q_dc_c_den;
int q_edge_num, q_edge_den;
int q_hf_y_num, q_hf_y_den;
int q_hf_c_num, q_hf_c_den;
int q_dz_num, q_dz_den;
int rd_lambda;
int skip_luma_max, skip_luma_sum;
int skip_chroma_max, skip_chroma_sum;
int skip_luma_mean_abs_max, skip_chroma_mean_abs_max;
int skip_coarse_mv_margin;
} internal_videocodec_quality_params_t;
static inline void internal_videocodec_quality_from_enum( enum videocodec_quality_t lvl, internal_videocodec_quality_params_t* q ) {
switch( lvl ) {
case VIDEOCODEC_QUALITY_MIN:
q->q_dc_y_num = 180; q->q_dc_y_den = 100;
q->q_dc_c_num = 210; q->q_dc_c_den = 100;
q->q_ac_y_num = 160; q->q_ac_y_den = 100;
q->q_ac_c_num = 180; q->q_ac_c_den = 100;
q->q_dz_num = 80; q->q_dz_den = 40;
q->rd_lambda = 110;
q->skip_luma_max = 8; q->skip_luma_sum = 2048;
q->skip_chroma_max = 9; q->skip_chroma_sum = 512;
q->skip_luma_mean_abs_max = 2; q->skip_chroma_mean_abs_max = 3;
q->skip_coarse_mv_margin = 16;
q->q_edge_num = 6; q->q_edge_den = 5;
q->q_hf_y_num = 64; q->q_hf_y_den = 40;
q->q_hf_c_num = 68; q->q_hf_c_den = 40;
break;
case VIDEOCODEC_QUALITY_LOW:
q->q_dc_y_num = 120; q->q_dc_y_den = 100;
q->q_dc_c_num = 130; q->q_dc_c_den = 100;
q->q_ac_y_num = 130; q->q_ac_y_den = 100;
q->q_ac_c_num = 145; q->q_ac_c_den = 100;
q->q_dz_num = 60; q->q_dz_den = 40;
q->rd_lambda = 60;
q->skip_luma_max = 3; q->skip_luma_sum = 384;
q->skip_chroma_max = 4; q->skip_chroma_sum = 96;
q->skip_luma_mean_abs_max = 1; q->skip_chroma_mean_abs_max = 1;
q->skip_coarse_mv_margin = 8;
q->q_edge_num = 5; q->q_edge_den = 5;
q->q_hf_y_num = 56; q->q_hf_y_den = 40;
q->q_hf_c_num = 60; q->q_hf_c_den = 40;
break;
case VIDEOCODEC_QUALITY_HIGH:
q->q_dc_y_num = 100; q->q_dc_y_den = 100;
q->q_dc_c_num = 100; q->q_dc_c_den = 100;
q->q_ac_y_num = 70; q->q_ac_y_den = 100;
q->q_ac_c_num = 75; q->q_ac_c_den = 100;
q->q_dz_num = 40; q->q_dz_den = 40;
q->rd_lambda = 28;
q->skip_luma_max = 1; q->skip_luma_sum = 128;
q->skip_chroma_max = 2; q->skip_chroma_sum = 32;
q->skip_luma_mean_abs_max = 0; q->skip_chroma_mean_abs_max = 0;
q->skip_coarse_mv_margin = 3;
q->q_edge_num = 3; q->q_edge_den = 5;
q->q_hf_y_num = 43; q->q_hf_y_den = 40;
q->q_hf_c_num = 45; q->q_hf_c_den = 40;
break;
case VIDEOCODEC_QUALITY_MAX:
q->q_dc_y_num = 100; q->q_dc_y_den = 100;
q->q_dc_c_num = 100; q->q_dc_c_den = 100;
q->q_ac_y_num = 50; q->q_ac_y_den = 100;
q->q_ac_c_num = 55; q->q_ac_c_den = 100;
q->q_dz_num = 20; q->q_dz_den = 40;
q->rd_lambda = 18;
q->skip_luma_max = 0; q->skip_luma_sum = 64;
q->skip_chroma_max = 1; q->skip_chroma_sum = 16;
q->skip_luma_mean_abs_max = 0; q->skip_chroma_mean_abs_max = 0;
q->skip_coarse_mv_margin = 2;
q->q_edge_num = 2; q->q_edge_den = 5;
q->q_hf_y_num = 41; q->q_hf_y_den = 40;
q->q_hf_c_num = 43; q->q_hf_c_den = 40;
break;
case VIDEOCODEC_QUALITY_DEFAULT:
default:
q->q_dc_y_num = 100; q->q_dc_y_den = 100;
q->q_dc_c_num = 100; q->q_dc_c_den = 100;
q->q_ac_y_num = 103; q->q_ac_y_den = 100;
q->q_ac_c_num = 109; q->q_ac_c_den = 100;
q->q_dz_num = 53; q->q_dz_den = 40;
q->rd_lambda = 42;
q->skip_luma_max = 2; q->skip_luma_sum = 256;
q->skip_chroma_max = 3; q->skip_chroma_sum = 64;
q->skip_luma_mean_abs_max = 0; q->skip_chroma_mean_abs_max = 1;
q->skip_coarse_mv_margin = 6;
q->q_edge_num = 4; q->q_edge_den = 5;
q->q_hf_y_num = 47; q->q_hf_y_den = 40;
q->q_hf_c_num = 51; q->q_hf_c_den = 40;
break;
}
}
static const uint8_t internal_videocodec_zz[ 64 ]={
0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
};
static const uint8_t internal_videocodec_QY[ 64 ]={
8, 16, 19, 22, 26, 27, 29, 34, 16, 16, 22, 24, 27, 29, 34, 37,
19, 22, 26, 27, 29, 34, 34, 38, 22, 22, 26, 27, 29, 34, 37, 40,
22, 26, 27, 29, 32, 35, 40, 48, 26, 27, 29, 32, 35, 40, 48, 58,
26, 27, 29, 34, 38, 46, 56, 69, 27, 29, 35, 38, 46, 56, 69, 83
};
static const uint8_t internal_videocodec_QC[ 64 ]={
16, 17, 18, 19, 20, 21, 22, 24, 17, 18, 19, 20, 21, 22, 24, 25,
18, 19, 20, 21, 22, 24, 25, 27, 19, 20, 21, 22, 24, 25, 27, 28,
20, 21, 22, 24, 25, 27, 28, 30, 21, 22, 24, 25, 27, 28, 30, 32,
22, 24, 25, 27, 28, 30, 32, 35, 24, 25, 27, 28, 30, 32, 35, 38
};
#define INTERNAL_VIDEOCODEC_COS_SHIFT 14
static const int16_t internal_videocodec_C8[ 8 ][ 8 ] = {
{ 5793, 5793, 5793, 5793, 5793, 5793, 5793, 5793 },
{ 8035, 6811, 4551, 1598, -1598, -4551, -6811, -8035 },
{ 7568, 3135, -3135, -7568, -7568, -3135, 3135, 7568 },
{ 6811, -1598, -8035, -4551, 4551, 8035, 1598, -6811 },
{ 5793, -5793, -5793, 5793, 5793, -5793, -5793, 5793 },
{ 4551, -8035, 1598, 6811, -6811, -1598, 8035, -4551 },
{ 3135, -7568, 7568, -3135, -3135, 7568, -7568, 3135 },
{ 1598, -4551, 6811, -8035, 8035, -6811, 4551, -1598 }
};
static inline int internal_videocodec_abs( int a ) { return a < 0 ? -a : a; }
static inline int internal_videocodec_imax( int a, int b ) {return a > b ? a : b; }
static inline int internal_videocodec_imin( int a, int b ) {return a < b ? a : b; }
static inline int internal_videocodec_clampi( int v ) { return v < 0 ? 0 : ( v > 255 ? 255 : v ); }
static inline int internal_videocodec_clampii( int v, int lo, int hi ) { return v < lo ? lo : ( v > hi ? hi : v ); }
static inline int internal_videocodec_floor_div2( int v ) { return ( v >= 0 ) ? ( v >> 1 ) : -( ( -v + 1 ) >> 1 ); }
static inline int internal_videocodec_floor_div4( int v ) { return ( v >= 0 ) ? ( v >> 2 ) : -( ( -v + 3 ) >> 2 ); }
static inline void internal_videocodec_build_window( uint16_t W8[ 64 ] ) {
const int shift = 8;
const int W_SOFT = 240;
const int W_MID = 212;
const int W_HIGH = 190;
const int W_XHI = 176;
for( int v = 0; v < 8; ++v ) {
for( int u = 0; u < 8; ++u ) {
int idx = v * 8 + u;
int s = u + v;
int w = 256;
if( u == 0 && v == 0 ) w = 256;
else if( s <= 2 ) w = 256;
else if( s <= 4 ) w = W_SOFT;
else if( s <= 6 ) w = W_MID;
else {
w = W_HIGH;
if( ( u >= 6 || v >= 6 ) ) w = ( w * W_XHI + ( 1 << ( shift - 1 ) ) ) >> shift;
}
W8[ idx ] = (uint16_t) w;
}
}
}
static inline void internal_videocodec_reshape_quant_one( uint8_t* dst, uint8_t const* src, int chroma, int edge_num, int edge_den, int hf_num, int hf_den ) {
for( int i = 0; i < 64; i++ ) {
int u = i & 7, v = i >> 3, s = u + v;
int q = src[ i ];
if( i == 0 ) {
dst[ i ] = (uint8_t) q;
continue;
}
if( s <= 2 ) {
q = ( q * edge_num + ( edge_den >> 1 ) ) / edge_den;
if( q < 1 ) q = 1;
} else if( s >= 8 || u >= 6 || v >= 6 ) {
q = ( q * hf_num + ( hf_den >> 1 ) ) / hf_den;
if( q > 255 ) q = 255;
}
dst[ i ] = (uint8_t) q;
}
}
static inline void internal_videocodec_build_quants( uint8_t QYx[ 64 ], uint8_t QCx[ 64 ], internal_videocodec_quality_params_t const* q ) {
for( int i = 0; i < 64; i++ ) {
QYx[ i ] = internal_videocodec_QY[ i ];
QCx[ i ] = internal_videocodec_QC[ i ];
}
internal_videocodec_reshape_quant_one( QYx, internal_videocodec_QY, 0, q->q_edge_num, q->q_edge_den, q->q_hf_y_num, q->q_hf_y_den );
internal_videocodec_reshape_quant_one( QCx, internal_videocodec_QC, 1, q->q_edge_num, q->q_edge_den, q->q_hf_c_num, q->q_hf_c_den );
int q0y = QYx[ 0 ];
q0y = ( q0y * q->q_dc_y_num + ( q->q_dc_y_den >> 1 ) ) / q->q_dc_y_den;
if( q0y < 1 ) q0y = 1;
else if( q0y > 255 ) q0y = 255;
QYx[ 0 ] = (uint8_t) q0y;
int q0c = QCx[ 0 ];
q0c = ( q0c * q->q_dc_c_num + ( q->q_dc_c_den >> 1 ) ) / q->q_dc_c_den;
if( q0c < 1 ) q0c = 1;
else if( q0c > 255 ) q0c = 255;
QCx[ 0 ] = (uint8_t) q0c;
for( int i = 1; i < 64; i++ ) {
int qy = QYx[ i ];
qy = ( qy * q->q_ac_y_num + ( q->q_ac_y_den >> 1 ) ) / q->q_ac_y_den;
if( qy < 1 ) qy = 1;
else if( qy > 255 ) qy = 255;
QYx[ i ] = (uint8_t) qy;
int qc = QCx[ i ];
qc = ( qc * q->q_ac_c_num + ( q->q_ac_c_den >> 1 ) ) / q->q_ac_c_den;
if( qc < 1 ) qc = 1;
else if( qc > 255 ) qc = 255;
QCx[ i ] = (uint8_t) qc;
}
}
static inline int16_t internal_videocodec_div_rnd_q( int32_t t, int16_t qstep ) {
int32_t d = ( qstep <= 0 ) ? 1 : (int32_t) qstep;
return (int16_t) ( ( t >= 0 ) ? ( ( t + ( d >> 1 ) ) / d ) : ( -( ( ( -t ) + ( d >> 1 ) ) / d ) ) );
}
static inline int16_t internal_videocodec_quant_dc_plain( int32_t F, int16_t qstep ) {
return internal_videocodec_div_rnd_q( F, qstep );
}
static inline int16_t internal_videocodec_quant_ac_deadzone( int32_t F, int16_t qstep, int dz_num, int dz_den ) {
int32_t a = ( F >= 0 ) ? F : -F;
int32_t t0 = ( (int32_t) qstep * (int32_t) dz_num + (int32_t) dz_den ) / ( 2 * (int32_t) dz_den );
if( a <= t0 ) return 0;
return internal_videocodec_div_rnd_q( F, qstep );
}
static void internal_videocodec_fdct8x8_u8( uint8_t const* src, int stride, int32_t F[ 64 ] ) {
int32_t tmp[ 64 ];
for( int y = 0; y < 8; y++ ) {
int32_t r[ 8 ];
for( int x = 0; x < 8; x++ ) r[ x ] = (int32_t) src[ y * stride + x ] - 128;
for( int u = 0; u < 8; u++ ) {
int64_t s = 0;
for( int x = 0; x < 8; x++ ) s += (int64_t) internal_videocodec_C8[ u ][ x ] * (int64_t) r[ x ];
tmp[ y * 8 + u ] = (int32_t) ( ( s + ( (int64_t) 1 << ( INTERNAL_VIDEOCODEC_COS_SHIFT - 1 ) ) ) >> INTERNAL_VIDEOCODEC_COS_SHIFT );
}
}
for( int u = 0; u < 8; u++ ) {
for( int v = 0; v < 8; v++ ) {
int64_t s = 0;
for( int y = 0; y < 8; y++ ) s += (int64_t) internal_videocodec_C8[ v ][ y ] * (int64_t) tmp[ y * 8 + u ];
F[ v * 8 + u ] = (int32_t) ( ( s + ( (int64_t) 1 << ( INTERNAL_VIDEOCODEC_COS_SHIFT - 1 ) ) ) >> INTERNAL_VIDEOCODEC_COS_SHIFT );
}
}
}
static void internal_videocodec_fdct8x8_s16( int16_t const* src, int stride, int32_t F[ 64 ] ) {
int32_t tmp[ 64 ];
for( int y = 0; y < 8; y++ ) {
int32_t r[ 8 ];
for( int x = 0; x < 8; x++ ) r[ x ] = (int32_t) src[ y * stride + x ];
for( int u = 0; u < 8; u++ ) {
int64_t s = 0;
for( int x = 0; x < 8; x++ ) s += (int64_t) internal_videocodec_C8[ u ][ x ] * (int64_t) r[ x ];
tmp[ y * 8 + u ] = (int32_t) ( ( s + ( (int64_t) 1 << ( INTERNAL_VIDEOCODEC_COS_SHIFT - 1 ) ) ) >> INTERNAL_VIDEOCODEC_COS_SHIFT );
}
}
for( int u = 0; u < 8; u++ ) {
for( int v = 0; v < 8; v++ ) {
int64_t s = 0;
for( int y = 0; y < 8; y++ ) s += (int64_t) internal_videocodec_C8[ v ][ y ] * (int64_t) tmp[ y * 8 + u ];
F[ v * 8 + u ] = (int32_t) ( ( s + ( (int64_t) 1 << ( INTERNAL_VIDEOCODEC_COS_SHIFT - 1 ) ) ) >> INTERNAL_VIDEOCODEC_COS_SHIFT );
}
}
}
static inline void internal_videocodec_post_weight_F_ctx( int32_t F[ 64 ], uint16_t const* W8 ) {
const int shift = 8;
for( int i = 0; i < 64; i++ ) {
int64_t t = (int64_t) F[ i ] * (int64_t) W8[ i ];
F[ i ] = (int32_t) ( ( t + ( (int64_t) 1 << ( shift - 1 ) ) ) >> shift );
}
}
static inline void internal_videocodec_copy_block_from( uint8_t const* src, int w, int h, int sx, int sy, uint8_t* dst ) {
for( int by = 0; by < 8; ++by ) {
int yy = internal_videocodec_clampii( sy + by, 0, h - 1 );
const uint8_t* srow = src + (size_t) yy * w;
for( int bx = 0; bx < 8; ++bx ) {
int xx = internal_videocodec_clampii( sx + bx, 0, w - 1 );
dst[ by * 8 + bx ] = srow[ xx ];
}
}
}
static inline void internal_videocodec_store_block( uint8_t* img, int w, int h, int x, int y, uint8_t const* blk ) {
int bwid = ( x + 8 <= w ) ? 8 : ( w - x ), bhgt = ( y + 8 <= h ) ? 8 : ( h - y );
for( int by = 0; by < bhgt; ++by ) {
memcpy( img + (size_t) ( y + by ) * w + x, blk + (size_t) by * 8, (size_t) bwid );
}
}
static inline uint8_t internal_videocodec_sample_chroma_qpel_px( uint8_t const* img, int w, int h, int x, int y, int dxq, int dyq ) {
int bx = x + internal_videocodec_floor_div4( dxq );
int by = y + internal_videocodec_floor_div4( dyq );
int rx = dxq - 4 * internal_videocodec_floor_div4( dxq );
int ry = dyq - 4 * internal_videocodec_floor_div4( dyq );
int x0 = internal_videocodec_clampii( bx, 0, w - 1 );
int y0 = internal_videocodec_clampii( by, 0, h - 1 );
int x1 = internal_videocodec_clampii( bx + 1, 0, w - 1 );
int y1 = internal_videocodec_clampii( by + 1, 0, h - 1 );
uint8_t p00 = img[ (size_t) y0 * w + x0 ];
uint8_t p10 = img[ (size_t) y0 * w + x1 ];
uint8_t p01 = img[ (size_t) y1 * w + x0 ];
uint8_t p11 = img[ (size_t) y1 * w + x1 ];
int w00 = ( 4 - rx ) * ( 4 - ry );
int w10 = ( rx ) * ( 4 - ry );
int w01 = ( 4 - rx ) * ( ry );
int w11 = ( rx ) * ( ry );
int s = w00 * p00 + w10 * p10 + w01 * p01 + w11 * p11;
return (uint8_t) ( ( s + 8 ) >> 4 );
}
static inline void internal_videocodec_copy_block_from_frac_chroma( uint8_t const* src, int w, int h, int sx, int sy, int dxq, int dyq, uint8_t* dst ) {
for( int by = 0; by < 8; ++by ) {
for( int bx = 0; bx < 8; ++bx ) {
dst[ by * 8 + bx ] = internal_videocodec_sample_chroma_qpel_px( src, w, h, sx + bx, sy + by, dxq, dyq );
}
}
}
static inline uint8_t internal_videocodec_sample_luma_hpel_px( uint8_t const* img, int w, int h, int x, int y, int dxh, int dyh ) {
int bx = x + internal_videocodec_floor_div2( dxh );
int by = y + internal_videocodec_floor_div2( dyh );
int fx = ( dxh & 1 ) != 0;
int fy = ( dyh & 1 ) != 0;
int x0 = internal_videocodec_clampii( bx, 0, w - 1 );
int y0 = internal_videocodec_clampii( by, 0, h - 1 );
int x1 = internal_videocodec_clampii( bx + 1, 0, w - 1 );
int y1 = internal_videocodec_clampii( by + 1, 0, h - 1 );
uint8_t p00 = img[ (size_t) y0 * w + x0 ];
if( !fx && !fy ) return p00;
if( fx && !fy ) {
uint8_t p10 = img[ (size_t) y0 * w + x1 ];
return (uint8_t) ( ( p00 + p10 + 1 ) >> 1 );
} else if( !fx && fy ) {
uint8_t p01 = img[ (size_t) y1 * w + x0 ];
return (uint8_t) ( ( p00 + p01 + 1 ) >> 1 );
} else {
uint8_t p10 = img[ (size_t) y0 * w + x1 ];
uint8_t p01 = img[ (size_t) y1 * w + x0 ];
uint8_t p11 = img[ (size_t) y1 * w + x1 ];
int s = (int) p00 + (int) p10 + (int) p01 + (int) p11;
return (uint8_t) ( ( s + 2 ) >> 2 );
}
}
static inline void internal_videocodec_copy_block_from_frac_luma( uint8_t const* src, int w, int h, int sx, int sy, int dxh, int dyh, uint8_t* dst ) {
for( int by = 0; by < 8; ++by ) {
for( int bx = 0; bx < 8; ++bx ) {
dst[ by * 8 + bx ] = internal_videocodec_sample_luma_hpel_px( src, w, h, sx + bx, sy + by, dxh, dyh );
}
}
}
static inline int internal_videocodec_hadamard4_abs_sum( const int r[ 16 ] ) {
int a[ 16 ];
for( int i = 0; i < 4; i++ ) {
int r0 = r[ i * 4 + 0 ], r1 = r[ i * 4 + 1 ], r2 = r[ i * 4 + 2 ], r3 = r[ i * 4 + 3 ];
int t0 = r0 + r1, t1 = r0 - r1, t2 = r2 + r3, t3 = r2 - r3;
a[ i * 4 + 0 ] = t0 + t2;
a[ i * 4 + 1 ] = t1 + t3;
a[ i * 4 + 2 ] = t0 - t2;
a[ i * 4 + 3 ] = t1 - t3;
}
int sum = 0;
for( int j = 0; j < 4; j++ ) {
int b0 = a[ 0 * 4 + j ] + a[ 1 * 4 + j ], b1 = a[ 0 * 4 + j ] - a[ 1 * 4 + j ];
int b2 = a[ 2 * 4 + j ] + a[ 3 * 4 + j ], b3 = a[ 2 * 4 + j ] - a[ 3 * 4 + j ];
int c0 = b0 + b2, c1 = b1 + b3, c2 = b0 - b2, c3 = b1 - b3;
sum += internal_videocodec_abs( c0 ) + internal_videocodec_abs( c1 ) + internal_videocodec_abs( c2 ) + internal_videocodec_abs( c3 );
}
return sum;
}
static int internal_videocodec_satd16x16_luma_hpel( uint8_t const* cur, int w, int h, int x, int y, uint8_t const* ref, int dxh, int dyh, int cutoff ) {
int sum = 0;
int r[ 16 ];
for( int ty = 0; ty < 16; ty += 4 ) {
for( int tx = 0; tx < 16; tx += 4 ) {
for( int j = 0; j < 4; j++ ) {
int yy = internal_videocodec_clampii( y + ty + j, 0, h - 1 );
const uint8_t* crow = cur + (size_t) yy * w;
for( int i = 0; i < 4; i++ ) {
int xx = internal_videocodec_clampii( x + tx + i, 0, w - 1 );
uint8_t p = internal_videocodec_sample_luma_hpel_px( ref, w, h, x + tx + i, y + ty + j, dxh, dyh );
r[ j * 4 + i ] = (int) crow[ xx ] - (int) p;
}
}
sum += internal_videocodec_hadamard4_abs_sum( r );
if( sum >= cutoff ) return sum;
}
}
return sum;
}
typedef struct internal_videocodec_buffer_t {
uint8_t* buf;
size_t cap, len;
} internal_videocodec_buffer_t;
static void internal_videocodec_buffer_reset( internal_videocodec_buffer_t* buffer ) {
buffer->len = 0;
}
static inline void internal_videocodec_buffer_put_u8( internal_videocodec_buffer_t* buffer, uint32_t v ) {
buffer->buf[ buffer->len++ ] = (uint8_t) v;
}
static inline void internal_videocodec_buffer_put_i16( internal_videocodec_buffer_t* buffer, int32_t v ) {
int16_t t = (int16_t) v;
memcpy( buffer->buf + buffer->len, &t, 2 );
buffer->len += 2;
}
static void internal_videocodec_deblock_plane( uint8_t* img, int w, int h, int is_chroma ) {
if( w < 16 || h < 16 ) return;
const int step_cap = is_chroma ? 3 : 6;
const int edge_floor = is_chroma ? 2 : 1;
for( int x = 8; x < w; x += 8 ) {
const int i = x - 1;
for( int y = 0; y < h; ++y ) {
uint8_t* row = img + (size_t)y * w;
int p2 = row[ i - 2 ], p1 = row[ i - 1 ], p0 = row[ i ];
int q0 = row[ i + 1 ], q1 = row[ i + 2 ], q2 = ( i + 3 < w ) ? row[ i + 3 ] : row[ i + 2 ];
int g = internal_videocodec_abs( p0 - q0 );
int rL = internal_videocodec_imax( internal_videocodec_abs( p2 - p1 ), internal_videocodec_abs( p1 - p0 ) );
int rR = internal_videocodec_imax( internal_videocodec_abs( q2 - q1 ), internal_videocodec_abs( q1 - q0 ) );
int flat = internal_videocodec_imax( rL, rR );
if( g <= edge_floor ) continue;
if( g <= flat ) continue;
int a = ( p1 + 3*p0 + 3*q0 + q1 + 4 ) >> 3; /* cross-edge target */
int dp = a - p0, dq = a - q0;
int wgt = g - flat;
if( wgt < 0 ) wgt = 0;
if( wgt > 12 ) wgt = 12;
int step = ( ( wgt + 1 ) >> 1 ); /* 0..6 */
if( step > step_cap ) step = step_cap;