aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/rpzaenc.c
blob: 3a1924d385176023e324b94575737c0279765e42 (plain) (blame)
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
/*
 * QuickTime RPZA Video Encoder
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file rpzaenc.c
 * QT RPZA Video Encoder by Todd Kirby <doubleshot@pacbell.net> and David Adler
 */

#include "libavutil/avassert.h"
#include "libavutil/common.h"
#include "libavutil/opt.h"

#include "avcodec.h"
#include "codec_internal.h"
#include "encode.h"
#include "mathops.h"
#include "put_bits.h"

typedef struct RpzaContext {
    AVClass *avclass;

    int skip_frame_thresh;
    int start_one_color_thresh;
    int continue_one_color_thresh;
    int sixteen_color_thresh;

    AVFrame *prev_frame;    // buffer for previous source frame
    PutBitContext pb;       // buffer for encoded frame data.

    int frame_width;        // width in pixels of source frame
    int frame_height;       // height in pixesl of source frame

    int first_frame;        // flag set to one when the first frame is being processed
                            // so that comparisons with previous frame data in not attempted
} RpzaContext;

typedef enum channel_offset {
    RED = 2,
    GREEN = 1,
    BLUE = 0,
} channel_offset;

typedef struct rgb {
    uint8_t r;
    uint8_t g;
    uint8_t b;
} rgb;

#define SQR(x) ((x) * (x))

/* 15 bit components */
#define GET_CHAN(color, chan) (((color) >> ((chan) * 5) & 0x1F))
#define R(color) GET_CHAN(color, RED)
#define G(color) GET_CHAN(color, GREEN)
#define B(color) GET_CHAN(color, BLUE)

typedef struct BlockInfo {
    int row;
    int col;
    int block_width;
    int block_height;
    int image_width;
    int image_height;
    int block_index;
    uint16_t start;
    int rowstride;
    int prev_rowstride;
    int blocks_per_row;
    int total_blocks;
} BlockInfo;

static void get_colors(const uint8_t *min, const uint8_t *max, uint8_t color4[4][3])
{
    uint8_t step;

    color4[0][0] = min[0];
    color4[0][1] = min[1];
    color4[0][2] = min[2];

    color4[3][0] = max[0];
    color4[3][1] = max[1];
    color4[3][2] = max[2];

    // red components
    step = (color4[3][0] - color4[0][0] + 1) / 3;
    color4[1][0] = color4[0][0] + step;
    color4[2][0] = color4[3][0] - step;

    // green components
    step = (color4[3][1] - color4[0][1] + 1) / 3;
    color4[1][1] = color4[0][1] + step;
    color4[2][1] = color4[3][1] - step;

    // blue components
    step = (color4[3][2] - color4[0][2] + 1) / 3;
    color4[1][2] = color4[0][2] + step;
    color4[2][2] = color4[3][2] - step;
}

/* Fill BlockInfo struct with information about a 4x4 block of the image */
static int get_block_info(BlockInfo *bi, int block, int prev_frame)
{
    bi->row = block / bi->blocks_per_row;
    bi->col = block % bi->blocks_per_row;

    // test for right edge block
    if (bi->col == bi->blocks_per_row - 1 && (bi->image_width % 4) != 0) {
        bi->block_width = bi->image_width % 4;
    } else {
        bi->block_width = 4;
    }

    // test for bottom edge block
    if (bi->row == (bi->image_height / 4) && (bi->image_height % 4) != 0) {
        bi->block_height = bi->image_height % 4;
    } else {
        bi->block_height = 4;
    }

    return block ? (bi->col * 4) + (bi->row * (prev_frame ? bi->prev_rowstride : bi->rowstride) * 4) : 0;
}

static uint16_t rgb24_to_rgb555(const uint8_t *rgb24)
{
    uint16_t rgb555 = 0;
    uint32_t r, g, b;

    r = rgb24[0];
    g = rgb24[1];
    b = rgb24[2];

    rgb555 |= (r << 10);
    rgb555 |= (g << 5);
    rgb555 |= (b << 0);

    return rgb555;
}

/*
 * Returns the total difference between two 24 bit color values
 */
static int diff_colors(const uint8_t *colorA, const uint8_t *colorB)
{
    int tot;

    tot  = SQR(colorA[0] - colorB[0]);
    tot += SQR(colorA[1] - colorB[1]);
    tot += SQR(colorA[2] - colorB[2]);

    return tot;
}

/*
 * Returns the maximum channel difference
 */
static int max_component_diff(const uint16_t *colorA, const uint16_t *colorB)
{
    int diff, max = 0;

    diff = FFABS(R(colorA[0]) - R(colorB[0]));
    if (diff > max) {
        max = diff;
    }
    diff = FFABS(G(colorA[0]) - G(colorB[0]));
    if (diff > max) {
        max = diff;
    }
    diff = FFABS(B(colorA[0]) - B(colorB[0]));
    if (diff > max) {
        max = diff;
    }
    return max;
}

/*
 * Find the channel that has the largest difference between minimum and maximum
 * color values. Put the minimum value in min, maximum in max and the channel
 * in chan.
 */
static void get_max_component_diff(const BlockInfo *bi, const uint16_t *block_ptr,
                                   uint8_t *min, uint8_t *max, channel_offset *chan)
{
    int x, y;
    uint8_t min_r, max_r, min_g, max_g, min_b, max_b;
    uint8_t r, g, b;

    // fix warning about uninitialized vars
    min_r = min_g = min_b = UINT8_MAX;
    max_r = max_g = max_b = 0;

    // loop thru and compare pixels
    for (y = 0; y < bi->block_height; y++) {
        for (x = 0; x < bi->block_width; x++) {
            // TODO:  optimize
            min_r = FFMIN(R(block_ptr[x]), min_r);
            min_g = FFMIN(G(block_ptr[x]), min_g);
            min_b = FFMIN(B(block_ptr[x]), min_b);

            max_r = FFMAX(R(block_ptr[x]), max_r);
            max_g = FFMAX(G(block_ptr[x]), max_g);
            max_b = FFMAX(B(block_ptr[x]), max_b);
        }
        block_ptr += bi->rowstride;
    }

    r = max_r - min_r;
    g = max_g - min_g;
    b = max_b - min_b;

    if (r > g && r > b) {
        *max = max_r;
        *min = min_r;
        *chan = RED;
    } else if (g > b && g >= r) {
        *max = max_g;
        *min = min_g;
        *chan = GREEN;
    } else {
        *max = max_b;
        *min = min_b;
        *chan = BLUE;
    }
}

/*
 * Compare two 4x4 blocks to determine if the total difference between the
 * blocks is greater than the thresh parameter. Returns -1 if difference
 * exceeds threshold or zero otherwise.
 */
static int compare_blocks(const uint16_t *block1, const uint16_t *block2,
                          const BlockInfo *bi, int thresh)
{
    int x, y, diff = 0;
    for (y = 0; y < bi->block_height; y++) {
        for (x = 0; x < bi->block_width; x++) {
            diff = max_component_diff(&block1[x], &block2[x]);
            if (diff >= thresh) {
                return -1;
            }
        }
        block1 += bi->prev_rowstride;
        block2 += bi->rowstride;
    }
    return 0;
}

/*
 * Determine the fit of one channel to another within a 4x4 block. This
 * is used to determine the best palette choices for 4-color encoding.
 */
static int leastsquares(const uint16_t *block_ptr, const BlockInfo *bi,
                        channel_offset xchannel, channel_offset ychannel,
                        int *slope, int *y_intercept, int *correlation_coef)
{
    int sumx = 0, sumy = 0, sumx2 = 0, sumy2 = 0, sumxy = 0,
           sumx_sq = 0, sumy_sq = 0, tmp, tmp2;
    int i, j, count;
    uint8_t x, y;

    count = bi->block_height * bi->block_width;

    if (count < 2)
        return -1;

    for (i = 0; i < bi->block_height; i++) {
        for (j = 0; j < bi->block_width; j++) {
            x = GET_CHAN(block_ptr[j], xchannel);
            y = GET_CHAN(block_ptr[j], ychannel);
            sumx += x;
            sumy += y;
            sumx2 += x * x;
            sumy2 += y * y;
            sumxy += x * y;
        }
        block_ptr += bi->rowstride;
    }

    sumx_sq = sumx * sumx;
    tmp = (count * sumx2 - sumx_sq);

    // guard against div/0
    if (tmp == 0)
        return -2;

    sumy_sq = sumy * sumy;

    *slope = (sumx * sumy - sumxy) / tmp;
    *y_intercept = (sumy - (*slope) * sumx) / count;

    tmp2 = count * sumy2 - sumy_sq;
    if (tmp2 == 0) {
        *correlation_coef = 0;
    } else {
        *correlation_coef = (count * sumxy - sumx * sumy) /
            ff_sqrt((unsigned)tmp * tmp2);
    }

    return 0; // success
}

/*
 * Determine the amount of error in the leastsquares fit.
 */
static int calc_lsq_max_fit_error(const uint16_t *block_ptr, const BlockInfo *bi,
                                  int min, int max, int tmp_min, int tmp_max,
                                  channel_offset xchannel, channel_offset ychannel)
{
    int i, j, x, y;
    int err;
    int max_err = 0;

    for (i = 0; i < bi->block_height; i++) {
        for (j = 0; j < bi->block_width; j++) {
            int x_inc, lin_y, lin_x;
            x = GET_CHAN(block_ptr[j], xchannel);
            y = GET_CHAN(block_ptr[j], ychannel);

            /* calculate x_inc as the 4-color index (0..3) */
            x_inc = (x - min) * 3 / (max - min) + 1;
            x_inc = FFMAX(FFMIN(3, x_inc), 0);

            /* calculate lin_y corresponding to x_inc */
            lin_y = tmp_min + (tmp_max - tmp_min) * x_inc / 3 + 1;

            err = FFABS(lin_y - y);
            if (err > max_err)
                max_err = err;

            /* calculate lin_x corresponding to x_inc */
            lin_x = min + (max - min) * x_inc / 3 + 1;

            err = FFABS(lin_x - x);
            if (err > max_err)
                max_err += err;
        }
        block_ptr += bi->rowstride;
    }

    return max_err;
}

/*
 * Find the closest match to a color within the 4-color palette
 */
static int match_color(const uint16_t *color, uint8_t colors[4][3])
{
    int ret = 0;
    int smallest_variance = INT_MAX;
    uint8_t dithered_color[3];

    for (int channel = 0; channel < 3; channel++) {
        dithered_color[channel] = GET_CHAN(color[0], channel);
    }

    for (int palette_entry = 0; palette_entry < 4; palette_entry++) {
        int variance = diff_colors(dithered_color, colors[palette_entry]);

        if (variance < smallest_variance) {
            smallest_variance = variance;
            ret = palette_entry;
        }
    }

    return ret;
}

/*
 * Encode a block using the 4-color opcode and palette. return number of
 * blocks encoded (until we implement multi-block 4 color runs this will
 * always be 1)
 */
static int encode_four_color_block(const uint8_t *min_color, const uint8_t *max_color,
                                   PutBitContext *pb, const uint16_t *block_ptr, const BlockInfo *bi)
{
    const int y_size = FFMIN(4, bi->image_height - bi->row * 4);
    const int x_size = FFMIN(4, bi->image_width  - bi->col * 4);
    uint8_t color4[4][3];
    uint16_t rounded_max, rounded_min;
    int idx;

    // round min and max wider
    rounded_min = rgb24_to_rgb555(min_color);
    rounded_max = rgb24_to_rgb555(max_color);

    // put a and b colors
    // encode 4 colors = first 16 bit color with MSB zeroed and...
    put_bits(pb, 16, rounded_max & ~0x8000);
    // ...second 16 bit color with MSB on.
    put_bits(pb, 16, rounded_min | 0x8000);

    get_colors(min_color, max_color, color4);

    for (int y = 0; y < y_size; y++) {
        for (int x = 0; x < x_size; x++) {
            idx = match_color(&block_ptr[x], color4);
            put_bits(pb, 2, idx);
        }

        for (int x = x_size; x < 4; x++)
            put_bits(pb, 2, idx);
        block_ptr += bi->rowstride;
    }

    for (int y = y_size; y < 4; y++) {
        for (int x = 0; x < 4; x++)
            put_bits(pb, 2, 0);
    }
    return 1; // num blocks encoded
}

/*
 * Copy a 4x4 block from the current frame buffer to the previous frame buffer.
 */
static void update_block_in_prev_frame(const uint16_t *src_pixels,
                                       uint16_t *dest_pixels,
                                       const BlockInfo *bi, int block_counter)
{
    const int y_size = FFMIN(4, bi->image_height - bi->row * 4);
    const int x_size = FFMIN(4, bi->image_width  - bi->col * 4) * 2;

    for (int y = 0; y < y_size; y++) {
        memcpy(dest_pixels, src_pixels, x_size);
        dest_pixels += bi->prev_rowstride;
        src_pixels += bi->rowstride;
    }
}

/*
 * update statistics for the specified block. If first_block,
 * it initializes the statistics.  Otherwise it updates the statistics IF THIS
 * BLOCK IS SUITABLE TO CONTINUE A 1-COLOR RUN. That is, it checks whether
 * the range of colors (since the routine was called first_block != 0) are
 * all close enough intensities to be represented by a single color.

 * The routine returns 0 if this block is too different to be part of
 * the same run of 1-color blocks. The routine returns 1 if this
 * block can be part of the same 1-color block run.

 * If the routine returns 1, it also updates its arguments to include
 * the statistics of this block. Otherwise, the stats are unchanged
 * and don't include the current block.
 */
static int update_block_stats(RpzaContext *s, const BlockInfo *bi, const uint16_t *block,
                              uint8_t min_color[3], uint8_t max_color[3],
                              int *total_rgb, int *total_pixels,
                              uint8_t avg_color[3], int first_block)
{
    int x, y;
    int is_in_range;
    int total_pixels_blk;
    int threshold;

    uint8_t min_color_blk[3], max_color_blk[3];
    int total_rgb_blk[3];
    uint8_t avg_color_blk[3];

    if (first_block) {
        min_color[0] = UINT8_MAX;
        min_color[1] = UINT8_MAX;
        min_color[2] = UINT8_MAX;
        max_color[0] = 0;
        max_color[1] = 0;
        max_color[2] = 0;
        total_rgb[0] = 0;
        total_rgb[1] = 0;
        total_rgb[2] = 0;
        *total_pixels = 0;
        threshold = s->start_one_color_thresh;
    } else {
        threshold = s->continue_one_color_thresh;
    }

    /*
       The *_blk variables will include the current block.
       Initialize them based on the blocks so far.
     */
    min_color_blk[0] = min_color[0];
    min_color_blk[1] = min_color[1];
    min_color_blk[2] = min_color[2];
    max_color_blk[0] = max_color[0];
    max_color_blk[1] = max_color[1];
    max_color_blk[2] = max_color[2];
    total_rgb_blk[0] = total_rgb[0];
    total_rgb_blk[1] = total_rgb[1];
    total_rgb_blk[2] = total_rgb[2];
    total_pixels_blk = *total_pixels + bi->block_height * bi->block_width;

    /*
       Update stats for this block's pixels
     */
    for (y = 0; y < bi->block_height; y++) {
        for (x = 0; x < bi->block_width; x++) {
            total_rgb_blk[0] += R(block[x]);
            total_rgb_blk[1] += G(block[x]);
            total_rgb_blk[2] += B(block[x]);

            min_color_blk[0] = FFMIN(R(block[x]), min_color_blk[0]);
            min_color_blk[1] = FFMIN(G(block[x]), min_color_blk[1]);
            min_color_blk[2] = FFMIN(B(block[x]), min_color_blk[2]);

            max_color_blk[0] = FFMAX(R(block[x]), max_color_blk[0]);
            max_color_blk[1] = FFMAX(G(block[x]), max_color_blk[1]);
            max_color_blk[2] = FFMAX(B(block[x]), max_color_blk[2]);
        }
        block += bi->rowstride;
    }

    /*
       Calculate average color including current block.
     */
    avg_color_blk[0] = total_rgb_blk[0] / total_pixels_blk;
    avg_color_blk[1] = total_rgb_blk[1] / total_pixels_blk;
    avg_color_blk[2] = total_rgb_blk[2] / total_pixels_blk;

    /*
       Are all the pixels within threshold of the average color?
     */
    is_in_range = (max_color_blk[0] - avg_color_blk[0] <= threshold &&
                   max_color_blk[1] - avg_color_blk[1] <= threshold &&
                   max_color_blk[2] - avg_color_blk[2] <= threshold &&
                   avg_color_blk[0] - min_color_blk[0] <= threshold &&
                   avg_color_blk[1] - min_color_blk[1] <= threshold &&
                   avg_color_blk[2] - min_color_blk[2] <= threshold);

    if (is_in_range) {
        /*
           Set the output variables to include this block.
         */
        min_color[0] = min_color_blk[0];
        min_color[1] = min_color_blk[1];
        min_color[2] = min_color_blk[2];
        max_color[0] = max_color_blk[0];
        max_color[1] = max_color_blk[1];
        max_color[2] = max_color_blk[2];
        total_rgb[0] = total_rgb_blk[0];
        total_rgb[1] = total_rgb_blk[1];
        total_rgb[2] = total_rgb_blk[2];
        *total_pixels = total_pixels_blk;
        avg_color[0] = avg_color_blk[0];
        avg_color[1] = avg_color_blk[1];
        avg_color[2] = avg_color_blk[2];
    }

    return is_in_range;
}

static void rpza_encode_stream(RpzaContext *s, const AVFrame *pict)
{
    BlockInfo bi;
    int block_counter = 0;
    int n_blocks;
    int total_blocks;
    int prev_block_offset;
    int block_offset = 0;
    int pblock_offset = 0;
    uint8_t min = 0, max = 0;
    channel_offset chan;
    int i;
    int tmp_min, tmp_max;
    int total_rgb[3];
    uint8_t avg_color[3];
    int pixel_count;
    uint8_t min_color[3], max_color[3];
    int slope, y_intercept, correlation_coef;
    const uint16_t *src_pixels = (const uint16_t *)pict->data[0];
    uint16_t *prev_pixels = (uint16_t *)s->prev_frame->data[0];

    /* Number of 4x4 blocks in frame. */
    total_blocks = ((s->frame_width + 3) / 4) * ((s->frame_height + 3) / 4);

    bi.image_width = s->frame_width;
    bi.image_height = s->frame_height;
    bi.rowstride = pict->linesize[0] / 2;
    bi.prev_rowstride = s->prev_frame->linesize[0] / 2;

    bi.blocks_per_row = (s->frame_width + 3) / 4;

    while (block_counter < total_blocks) {
        // SKIP CHECK
        // make sure we have a valid previous frame and we're not writing
        // a key frame
        if (!s->first_frame) {
            n_blocks = 0;
            prev_block_offset = 0;

            while (n_blocks < 32 && block_counter + n_blocks < total_blocks) {
                block_offset  = get_block_info(&bi, block_counter + n_blocks, 0);
                pblock_offset = get_block_info(&bi, block_counter + n_blocks, 1);

                // multi-block opcodes cannot span multiple rows.
                // If we're starting a new row, break out and write the opcode
                /* TODO: Should eventually use bi.row here to determine when a
                   row break occurs, but that is currently breaking the
                   quicktime player. This is probably due to a bug in the
                   way I'm calculating the current row.
                 */
                if (prev_block_offset && block_offset - prev_block_offset > 12) {
                    break;
                }

                prev_block_offset = block_offset;

                if (compare_blocks(&prev_pixels[pblock_offset],
                                   &src_pixels[block_offset], &bi, s->skip_frame_thresh) != 0) {
                    // write out skipable blocks
                    if (n_blocks) {

                        // write skip opcode
                        put_bits(&s->pb, 8, 0x80 | (n_blocks - 1));
                        block_counter += n_blocks;

                        goto post_skip;
                    }
                    break;
                }

                /*
                 * NOTE: we don't update skipped blocks in the previous frame buffer
                 * since skipped needs always to be compared against the first skipped
                 * block to avoid artifacts during gradual fade in/outs.
                 */

                // update_block_in_prev_frame(&src_pixels[block_offset],
                //   &prev_pixels[pblock_offset], &bi, block_counter + n_blocks);

                n_blocks++;
            }

            // we're either at the end of the frame or we've reached the maximum
            // of 32 blocks in a run. Write out the run.
            if (n_blocks) {
                // write skip opcode
                put_bits(&s->pb, 8, 0x80 | (n_blocks - 1));
                block_counter += n_blocks;

                continue;
            }

        } else {
            block_offset  = get_block_info(&bi, block_counter, 0);
            pblock_offset = get_block_info(&bi, block_counter, 1);
        }
post_skip :

        // ONE COLOR CHECK
        if (update_block_stats(s, &bi, &src_pixels[block_offset],
                               min_color, max_color,
                               total_rgb, &pixel_count, avg_color, 1)) {
            prev_block_offset = block_offset;

            n_blocks = 1;

            /* update this block in the previous frame buffer */
            update_block_in_prev_frame(&src_pixels[block_offset],
                                       &prev_pixels[pblock_offset], &bi, block_counter + n_blocks);

            // check for subsequent blocks with the same color
            while (n_blocks < 32 && block_counter + n_blocks < total_blocks) {
                block_offset  = get_block_info(&bi, block_counter + n_blocks, 0);
                pblock_offset = get_block_info(&bi, block_counter + n_blocks, 1);

                // multi-block opcodes cannot span multiple rows.
                // If we've hit end of a row, break out and write the opcode
                if (block_offset - prev_block_offset > 12) {
                    break;
                }

                if (!update_block_stats(s, &bi, &src_pixels[block_offset],
                                        min_color, max_color,
                                        total_rgb, &pixel_count, avg_color, 0)) {
                    break;
                }

                prev_block_offset = block_offset;

                /* update this block in the previous frame buffer */
                update_block_in_prev_frame(&src_pixels[block_offset],
                                           &prev_pixels[pblock_offset], &bi, block_counter + n_blocks);

                n_blocks++;
            }

            // write one color opcode.
            put_bits(&s->pb, 8, 0xa0 | (n_blocks - 1));
            // write color to encode.
            put_bits(&s->pb, 16, rgb24_to_rgb555(avg_color));
            // skip past the blocks we've just encoded.
            block_counter += n_blocks;
        } else { // FOUR COLOR CHECK
            int err = 0;

            // get max component diff for block
            get_max_component_diff(&bi, &src_pixels[block_offset], &min, &max, &chan);

            min_color[0] = 0;
            max_color[0] = 0;
            min_color[1] = 0;
            max_color[1] = 0;
            min_color[2] = 0;
            max_color[2] = 0;

            // run least squares against other two components
            for (i = 0; i < 3; i++) {
                if (i == chan) {
                    min_color[i] = min;
                    max_color[i] = max;
                    continue;
                }

                slope = y_intercept = correlation_coef = 0;

                if (leastsquares(&src_pixels[block_offset], &bi, chan, i,
                                 &slope, &y_intercept, &correlation_coef)) {
                    min_color[i] = GET_CHAN(src_pixels[block_offset], i);
                    max_color[i] = GET_CHAN(src_pixels[block_offset], i);
                } else {
                    tmp_min = 1 + min * slope + y_intercept;
                    tmp_max = 1 + max * slope + y_intercept;

                    av_assert0(tmp_min <= tmp_max);
                    // clamp min and max color values
                    tmp_min = av_clip_uint8(tmp_min);
                    tmp_max = av_clip_uint8(tmp_max);

                    err = FFMAX(calc_lsq_max_fit_error(&src_pixels[block_offset], &bi,
                                                       min, max, tmp_min, tmp_max, chan, i), err);

                    min_color[i] = tmp_min;
                    max_color[i] = tmp_max;
                }
            }

            if (err > s->sixteen_color_thresh) { // DO SIXTEEN COLOR BLOCK
                const uint16_t *row_ptr;
                int y_size, x_size, rgb555;

                block_offset  = get_block_info(&bi, block_counter, 0);
                pblock_offset = get_block_info(&bi, block_counter, 1);

                row_ptr = &src_pixels[block_offset];
                y_size = FFMIN(4, bi.image_height - bi.row * 4);
                x_size = FFMIN(4, bi.image_width  - bi.col * 4);

                for (int y = 0; y < y_size; y++) {
                    for (int x = 0; x < x_size; x++) {
                        rgb555 = row_ptr[x] & ~0x8000;

                        put_bits(&s->pb, 16, rgb555);
                    }
                    for (int x = x_size; x < 4; x++)
                        put_bits(&s->pb, 16, 0);

                    row_ptr += bi.rowstride;
                }

                for (int y = y_size; y < 4; y++) {
                    for (int x = 0; x < 4; x++)
                        put_bits(&s->pb, 16, 0);
                }

                block_counter++;
            } else { // FOUR COLOR BLOCK
                block_counter += encode_four_color_block(min_color, max_color,
                                                         &s->pb, &src_pixels[block_offset], &bi);
            }

            /* update this block in the previous frame buffer */
            update_block_in_prev_frame(&src_pixels[block_offset],
                                       &prev_pixels[pblock_offset], &bi, block_counter);
        }
    }
}

static int rpza_encode_init(AVCodecContext *avctx)
{
    RpzaContext *s = avctx->priv_data;

    s->frame_width = avctx->width;
    s->frame_height = avctx->height;

    s->prev_frame = av_frame_alloc();
    if (!s->prev_frame)
        return AVERROR(ENOMEM);

    return 0;
}

static int rpza_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                             const AVFrame *pict, int *got_packet)
{
    RpzaContext *s = avctx->priv_data;
    uint8_t *buf;
    int ret = ff_alloc_packet(avctx, pkt, 4LL + 6LL * FFMAX(avctx->height, 4) * FFMAX(avctx->width, 4));

    if (ret < 0)
        return ret;

    init_put_bits(&s->pb, pkt->data, pkt->size);

    // skip 4 byte header, write it later once the size of the chunk is known
    put_bits32(&s->pb, 0x00);

    if (!s->prev_frame->data[0]) {
        s->first_frame = 1;
        s->prev_frame->format = pict->format;
        s->prev_frame->width = pict->width;
        s->prev_frame->height = pict->height;
        ret = av_frame_get_buffer(s->prev_frame, 0);
        if (ret < 0)
            return ret;
    } else {
        s->first_frame = 0;
    }

    rpza_encode_stream(s, pict);

    flush_put_bits(&s->pb);

    av_shrink_packet(pkt, put_bytes_output(&s->pb));
    buf = pkt->data;

    // write header opcode
    buf[0] = 0xe1; // chunk opcode

    // write chunk length
    AV_WB24(buf + 1, pkt->size);

    *got_packet = 1;

    return 0;
}

static int rpza_encode_end(AVCodecContext *avctx)
{
    RpzaContext *s = (RpzaContext *)avctx->priv_data;

    av_frame_free(&s->prev_frame);

    return 0;
}

#define OFFSET(x) offsetof(RpzaContext, x)
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
static const AVOption options[] = {
    { "skip_frame_thresh", NULL, OFFSET(skip_frame_thresh), AV_OPT_TYPE_INT, {.i64=1}, 0, 24, VE},
    { "start_one_color_thresh", NULL, OFFSET(start_one_color_thresh), AV_OPT_TYPE_INT, {.i64=1}, 0, 24, VE},
    { "continue_one_color_thresh", NULL, OFFSET(continue_one_color_thresh), AV_OPT_TYPE_INT, {.i64=0}, 0, 24, VE},
    { "sixteen_color_thresh", NULL, OFFSET(sixteen_color_thresh), AV_OPT_TYPE_INT, {.i64=1}, 0, 24, VE},
    { NULL },
};

static const AVClass rpza_class = {
    .class_name = "rpza",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

const FFCodec ff_rpza_encoder = {
    .p.name         = "rpza",
    CODEC_LONG_NAME("QuickTime video (RPZA)"),
    .p.type         = AVMEDIA_TYPE_VIDEO,
    .p.id           = AV_CODEC_ID_RPZA,
    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
    .priv_data_size = sizeof(RpzaContext),
    .p.priv_class   = &rpza_class,
    .init           = rpza_encode_init,
    FF_CODEC_ENCODE_CB(rpza_encode_frame),
    .close          = rpza_encode_end,
    .p.pix_fmts     = (const enum AVPixelFormat[]) { AV_PIX_FMT_RGB555,
                                                     AV_PIX_FMT_NONE},
};