• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * QuickTime RPZA Video Encoder
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file rpzaenc.c
23  * QT RPZA Video Encoder by Todd Kirby <doubleshot@pacbell.net> and David Adler
24  */
25 
26 #include "libavutil/avassert.h"
27 #include "libavutil/common.h"
28 #include "libavutil/opt.h"
29 
30 #include "avcodec.h"
31 #include "internal.h"
32 #include "put_bits.h"
33 
34 typedef struct RpzaContext {
35     AVClass *avclass;
36 
37     int skip_frame_thresh;
38     int start_one_color_thresh;
39     int continue_one_color_thresh;
40     int sixteen_color_thresh;
41 
42     AVFrame *prev_frame;    // buffer for previous source frame
43     PutBitContext pb;       // buffer for encoded frame data.
44 
45     int frame_width;        // width in pixels of source frame
46     int frame_height;       // height in pixesl of source frame
47 
48     int first_frame;        // flag set to one when the first frame is being processed
49                             // so that comparisons with previous frame data in not attempted
50 } RpzaContext;
51 
52 typedef enum channel_offset {
53     RED = 2,
54     GREEN = 1,
55     BLUE = 0,
56 } channel_offset;
57 
58 typedef struct rgb {
59     uint8_t r;
60     uint8_t g;
61     uint8_t b;
62 } rgb;
63 
64 #define SQR(x) ((x) * (x))
65 
66 /* 15 bit components */
67 #define GET_CHAN(color, chan) (((color) >> ((chan) * 5) & 0x1F) * 8)
68 #define R(color) GET_CHAN(color, RED)
69 #define G(color) GET_CHAN(color, GREEN)
70 #define B(color) GET_CHAN(color, BLUE)
71 
72 typedef struct BlockInfo {
73     int row;
74     int col;
75     int block_width;
76     int block_height;
77     int image_width;
78     int image_height;
79     int block_index;
80     uint16_t start;
81     int rowstride;
82     int blocks_per_row;
83     int total_blocks;
84 } BlockInfo;
85 
get_colors(uint8_t * min,uint8_t * max,uint8_t color4[4][3])86 static void get_colors(uint8_t *min, uint8_t *max, uint8_t color4[4][3])
87 {
88     uint8_t step;
89 
90     color4[0][0] = min[0];
91     color4[0][1] = min[1];
92     color4[0][2] = min[2];
93 
94     color4[3][0] = max[0];
95     color4[3][1] = max[1];
96     color4[3][2] = max[2];
97 
98     // red components
99     step = (color4[3][0] - color4[0][0] + 1) / 3;
100     color4[1][0] = color4[0][0] + step;
101     color4[2][0] = color4[3][0] - step;
102 
103     // green components
104     step = (color4[3][1] - color4[0][1] + 1) / 3;
105     color4[1][1] = color4[0][1] + step;
106     color4[2][1] = color4[3][1] - step;
107 
108     // blue components
109     step = (color4[3][2] - color4[0][2] + 1) / 3;
110     color4[1][2] = color4[0][2] + step;
111     color4[2][2] = color4[3][2] - step;
112 }
113 
114 /* Fill BlockInfo struct with information about a 4x4 block of the image */
get_block_info(BlockInfo * bi,int block)115 static int get_block_info(BlockInfo *bi, int block)
116 {
117     bi->row = block / bi->blocks_per_row;
118     bi->col = block % bi->blocks_per_row;
119 
120     // test for right edge block
121     if (bi->col == bi->blocks_per_row - 1 && (bi->image_width % 4) != 0) {
122         bi->block_width = bi->image_width % 4;
123     } else {
124         bi->block_width = 4;
125     }
126 
127     // test for bottom edge block
128     if (bi->row == (bi->image_height / 4) && (bi->image_height % 4) != 0) {
129         bi->block_height = bi->image_height % 4;
130     } else {
131         bi->block_height = 4;
132     }
133 
134     return block ? (bi->col * 4) + (bi->row * bi->rowstride * 4) : 0;
135 }
136 
rgb24_to_rgb555(uint8_t * rgb24)137 static uint16_t rgb24_to_rgb555(uint8_t *rgb24)
138 {
139     uint16_t rgb555 = 0;
140     uint32_t r, g, b;
141 
142     r = rgb24[0] >> 3;
143     g = rgb24[1] >> 3;
144     b = rgb24[2] >> 3;
145 
146     rgb555 |= (r << 10);
147     rgb555 |= (g << 5);
148     rgb555 |= (b << 0);
149 
150     return rgb555;
151 }
152 
153 /*
154  * Returns the total difference between two 24 bit color values
155  */
diff_colors(uint8_t * colorA,uint8_t * colorB)156 static int diff_colors(uint8_t *colorA, uint8_t *colorB)
157 {
158     int tot;
159 
160     tot  = SQR(colorA[0] - colorB[0]);
161     tot += SQR(colorA[1] - colorB[1]);
162     tot += SQR(colorA[2] - colorB[2]);
163 
164     return tot;
165 }
166 
167 /*
168  * Returns the maximum channel difference
169  */
max_component_diff(uint16_t * colorA,uint16_t * colorB)170 static int max_component_diff(uint16_t *colorA, uint16_t *colorB)
171 {
172     int diff, max = 0;
173 
174     diff = FFABS(R(colorA[0]) - R(colorB[0]));
175     if (diff > max) {
176         max = diff;
177     }
178     diff = FFABS(G(colorA[0]) - G(colorB[0]));
179     if (diff > max) {
180         max = diff;
181     }
182     diff = FFABS(B(colorA[0]) - B(colorB[0]));
183     if (diff > max) {
184         max = diff;
185     }
186     return max * 8;
187 }
188 
189 /*
190  * Find the channel that has the largest difference between minimum and maximum
191  * color values. Put the minimum value in min, maximum in max and the channel
192  * in chan.
193  */
get_max_component_diff(BlockInfo * bi,uint16_t * block_ptr,uint8_t * min,uint8_t * max,channel_offset * chan)194 static void get_max_component_diff(BlockInfo *bi, uint16_t *block_ptr,
195                                    uint8_t *min, uint8_t *max, channel_offset *chan)
196 {
197     int x, y;
198     uint8_t min_r, max_r, min_g, max_g, min_b, max_b;
199     uint8_t r, g, b;
200 
201     // fix warning about uninitialized vars
202     min_r = min_g = min_b = UINT8_MAX;
203     max_r = max_g = max_b = 0;
204 
205     // loop thru and compare pixels
206     for (y = 0; y < bi->block_height; y++) {
207         for (x = 0; x < bi->block_width; x++) {
208             // TODO:  optimize
209             min_r = FFMIN(R(block_ptr[x]), min_r);
210             min_g = FFMIN(G(block_ptr[x]), min_g);
211             min_b = FFMIN(B(block_ptr[x]), min_b);
212 
213             max_r = FFMAX(R(block_ptr[x]), max_r);
214             max_g = FFMAX(G(block_ptr[x]), max_g);
215             max_b = FFMAX(B(block_ptr[x]), max_b);
216         }
217         block_ptr += bi->rowstride;
218     }
219 
220     r = max_r - min_r;
221     g = max_g - min_g;
222     b = max_b - min_b;
223 
224     if (r > g && r > b) {
225         *max = max_r;
226         *min = min_r;
227         *chan = RED;
228     } else if (g > b && g >= r) {
229         *max = max_g;
230         *min = min_g;
231         *chan = GREEN;
232     } else {
233         *max = max_b;
234         *min = min_b;
235         *chan = BLUE;
236     }
237 }
238 
239 /*
240  * Compare two 4x4 blocks to determine if the total difference between the
241  * blocks is greater than the thresh parameter. Returns -1 if difference
242  * exceeds threshold or zero otherwise.
243  */
compare_blocks(uint16_t * block1,uint16_t * block2,BlockInfo * bi,int thresh)244 static int compare_blocks(uint16_t *block1, uint16_t *block2, BlockInfo *bi, int thresh)
245 {
246     int x, y, diff = 0;
247     for (y = 0; y < bi->block_height; y++) {
248         for (x = 0; x < bi->block_width; x++) {
249             diff = max_component_diff(&block1[x], &block2[x]);
250             if (diff >= thresh) {
251                 return -1;
252             }
253         }
254         block1 += bi->rowstride;
255         block2 += bi->rowstride;
256     }
257     return 0;
258 }
259 
260 /*
261  * Determine the fit of one channel to another within a 4x4 block. This
262  * is used to determine the best palette choices for 4-color encoding.
263  */
leastsquares(uint16_t * block_ptr,BlockInfo * bi,channel_offset xchannel,channel_offset ychannel,double * slope,double * y_intercept,double * correlation_coef)264 static int leastsquares(uint16_t *block_ptr, BlockInfo *bi,
265                         channel_offset xchannel, channel_offset ychannel,
266                         double *slope, double *y_intercept, double *correlation_coef)
267 {
268     double sumx = 0, sumy = 0, sumx2 = 0, sumy2 = 0, sumxy = 0,
269            sumx_sq = 0, sumy_sq = 0, tmp, tmp2;
270     int i, j, count;
271     uint8_t x, y;
272 
273     count = bi->block_height * bi->block_width;
274 
275     if (count < 2)
276         return -1;
277 
278     for (i = 0; i < bi->block_height; i++) {
279         for (j = 0; j < bi->block_width; j++) {
280             x = GET_CHAN(block_ptr[j], xchannel);
281             y = GET_CHAN(block_ptr[j], ychannel);
282             sumx += x;
283             sumy += y;
284             sumx2 += x * x;
285             sumy2 += y * y;
286             sumxy += x * y;
287         }
288         block_ptr += bi->rowstride;
289     }
290 
291     sumx_sq = sumx * sumx;
292     tmp = (count * sumx2 - sumx_sq);
293 
294     // guard against div/0
295     if (tmp == 0)
296         return -2;
297 
298     sumy_sq = sumy * sumy;
299 
300     *slope = (sumx * sumy - sumxy) / tmp;
301     *y_intercept = (sumy - (*slope) * sumx) / count;
302 
303     tmp2 = count * sumy2 - sumy_sq;
304     if (tmp2 == 0) {
305         *correlation_coef = 0.0;
306     } else {
307         *correlation_coef = (count * sumxy - sumx * sumy) /
308             sqrt(tmp * tmp2);
309     }
310 
311     return 0; // success
312 }
313 
314 /*
315  * Determine the amount of error in the leastsquares fit.
316  */
calc_lsq_max_fit_error(uint16_t * block_ptr,BlockInfo * bi,int min,int max,int tmp_min,int tmp_max,channel_offset xchannel,channel_offset ychannel)317 static int calc_lsq_max_fit_error(uint16_t *block_ptr, BlockInfo *bi,
318                                   int min, int max, int tmp_min, int tmp_max,
319                                   channel_offset xchannel, channel_offset ychannel)
320 {
321     int i, j, x, y;
322     int err;
323     int max_err = 0;
324 
325     for (i = 0; i < bi->block_height; i++) {
326         for (j = 0; j < bi->block_width; j++) {
327             int x_inc, lin_y, lin_x;
328             x = GET_CHAN(block_ptr[j], xchannel);
329             y = GET_CHAN(block_ptr[j], ychannel);
330 
331             /* calculate x_inc as the 4-color index (0..3) */
332             x_inc = floor( (x - min) * 3.0 / (max - min) + 0.5);
333             x_inc = FFMAX(FFMIN(3, x_inc), 0);
334 
335             /* calculate lin_y corresponding to x_inc */
336             lin_y = (int)(tmp_min + (tmp_max - tmp_min) * x_inc / 3.0 + 0.5);
337 
338             err = FFABS(lin_y - y);
339             if (err > max_err)
340                 max_err = err;
341 
342             /* calculate lin_x corresponding to x_inc */
343             lin_x = (int)(min + (max - min) * x_inc / 3.0 + 0.5);
344 
345             err = FFABS(lin_x - x);
346             if (err > max_err)
347                 max_err += err;
348         }
349         block_ptr += bi->rowstride;
350     }
351 
352     return max_err;
353 }
354 
355 /*
356  * Find the closest match to a color within the 4-color palette
357  */
match_color(uint16_t * color,uint8_t colors[4][3])358 static int match_color(uint16_t *color, uint8_t colors[4][3])
359 {
360     int ret = 0;
361     int smallest_variance = INT_MAX;
362     uint8_t dithered_color[3];
363 
364     for (int channel = 0; channel < 3; channel++) {
365         dithered_color[channel] = GET_CHAN(color[0], channel);
366     }
367 
368     for (int palette_entry = 0; palette_entry < 4; palette_entry++) {
369         int variance = diff_colors(dithered_color, colors[palette_entry]);
370 
371         if (variance < smallest_variance) {
372             smallest_variance = variance;
373             ret = palette_entry;
374         }
375     }
376 
377     return ret;
378 }
379 
380 /*
381  * Encode a block using the 4-color opcode and palette. return number of
382  * blocks encoded (until we implement multi-block 4 color runs this will
383  * always be 1)
384  */
encode_four_color_block(uint8_t * min_color,uint8_t * max_color,PutBitContext * pb,uint16_t * block_ptr,BlockInfo * bi)385 static int encode_four_color_block(uint8_t *min_color, uint8_t *max_color,
386                                    PutBitContext *pb, uint16_t *block_ptr, BlockInfo *bi)
387 {
388     int x, y, idx;
389     uint8_t color4[4][3];
390     uint16_t rounded_max, rounded_min;
391 
392     // round min and max wider
393     rounded_min = rgb24_to_rgb555(min_color);
394     rounded_max = rgb24_to_rgb555(max_color);
395 
396     // put a and b colors
397     // encode 4 colors = first 16 bit color with MSB zeroed and...
398     put_bits(pb, 16, rounded_max & ~0x8000);
399     // ...second 16 bit color with MSB on.
400     put_bits(pb, 16, rounded_min | 0x8000);
401 
402     get_colors(min_color, max_color, color4);
403 
404     for (y = 0; y < 4; y++) {
405         for (x = 0; x < 4; x++) {
406             idx = match_color(&block_ptr[x], color4);
407             put_bits(pb, 2, idx);
408         }
409         block_ptr += bi->rowstride;
410     }
411     return 1; // num blocks encoded
412 }
413 
414 /*
415  * Copy a 4x4 block from the current frame buffer to the previous frame buffer.
416  */
update_block_in_prev_frame(const uint16_t * src_pixels,uint16_t * dest_pixels,const BlockInfo * bi,int block_counter)417 static void update_block_in_prev_frame(const uint16_t *src_pixels,
418                                        uint16_t *dest_pixels,
419                                        const BlockInfo *bi, int block_counter)
420 {
421     const int y_size = FFMIN(4, bi->image_height - bi->row * 4);
422 
423     for (int y = 0; y < y_size; y++) {
424         memcpy(dest_pixels, src_pixels, 8);
425         dest_pixels += bi->rowstride;
426         src_pixels += bi->rowstride;
427     }
428 }
429 
430 /*
431  * update statistics for the specified block. If first_block,
432  * it initializes the statistics.  Otherwise it updates the statistics IF THIS
433  * BLOCK IS SUITABLE TO CONTINUE A 1-COLOR RUN. That is, it checks whether
434  * the range of colors (since the routine was called first_block != 0) are
435  * all close enough intensities to be represented by a single color.
436 
437  * The routine returns 0 if this block is too different to be part of
438  * the same run of 1-color blocks. The routine returns 1 if this
439  * block can be part of the same 1-color block run.
440 
441  * If the routine returns 1, it also updates its arguments to include
442  * the statistics of this block. Otherwise, the stats are unchanged
443  * and don't include the current block.
444  */
update_block_stats(RpzaContext * s,BlockInfo * bi,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)445 static int update_block_stats(RpzaContext *s, BlockInfo *bi, uint16_t *block,
446                               uint8_t min_color[3], uint8_t max_color[3],
447                               int *total_rgb, int *total_pixels,
448                               uint8_t avg_color[3], int first_block)
449 {
450     int x, y;
451     int is_in_range;
452     int total_pixels_blk;
453     int threshold;
454 
455     uint8_t min_color_blk[3], max_color_blk[3];
456     int total_rgb_blk[3];
457     uint8_t avg_color_blk[3];
458 
459     if (first_block) {
460         min_color[0] = UINT8_MAX;
461         min_color[1] = UINT8_MAX;
462         min_color[2] = UINT8_MAX;
463         max_color[0] = 0;
464         max_color[1] = 0;
465         max_color[2] = 0;
466         total_rgb[0] = 0;
467         total_rgb[1] = 0;
468         total_rgb[2] = 0;
469         *total_pixels = 0;
470         threshold = s->start_one_color_thresh;
471     } else {
472         threshold = s->continue_one_color_thresh;
473     }
474 
475     /*
476        The *_blk variables will include the current block.
477        Initialize them based on the blocks so far.
478      */
479     min_color_blk[0] = min_color[0];
480     min_color_blk[1] = min_color[1];
481     min_color_blk[2] = min_color[2];
482     max_color_blk[0] = max_color[0];
483     max_color_blk[1] = max_color[1];
484     max_color_blk[2] = max_color[2];
485     total_rgb_blk[0] = total_rgb[0];
486     total_rgb_blk[1] = total_rgb[1];
487     total_rgb_blk[2] = total_rgb[2];
488     total_pixels_blk = *total_pixels + bi->block_height * bi->block_width;
489 
490     /*
491        Update stats for this block's pixels
492      */
493     for (y = 0; y < bi->block_height; y++) {
494         for (x = 0; x < bi->block_width; x++) {
495             total_rgb_blk[0] += R(block[x]);
496             total_rgb_blk[1] += G(block[x]);
497             total_rgb_blk[2] += B(block[x]);
498 
499             min_color_blk[0] = FFMIN(R(block[x]), min_color_blk[0]);
500             min_color_blk[1] = FFMIN(G(block[x]), min_color_blk[1]);
501             min_color_blk[2] = FFMIN(B(block[x]), min_color_blk[2]);
502 
503             max_color_blk[0] = FFMAX(R(block[x]), max_color_blk[0]);
504             max_color_blk[1] = FFMAX(G(block[x]), max_color_blk[1]);
505             max_color_blk[2] = FFMAX(B(block[x]), max_color_blk[2]);
506         }
507         block += bi->rowstride;
508     }
509 
510     /*
511        Calculate average color including current block.
512      */
513     avg_color_blk[0] = total_rgb_blk[0] / total_pixels_blk;
514     avg_color_blk[1] = total_rgb_blk[1] / total_pixels_blk;
515     avg_color_blk[2] = total_rgb_blk[2] / total_pixels_blk;
516 
517     /*
518        Are all the pixels within threshold of the average color?
519      */
520     is_in_range = (max_color_blk[0] - avg_color_blk[0] <= threshold &&
521                    max_color_blk[1] - avg_color_blk[1] <= threshold &&
522                    max_color_blk[2] - avg_color_blk[2] <= threshold &&
523                    avg_color_blk[0] - min_color_blk[0] <= threshold &&
524                    avg_color_blk[1] - min_color_blk[1] <= threshold &&
525                    avg_color_blk[2] - min_color_blk[2] <= threshold);
526 
527     if (is_in_range) {
528         /*
529            Set the output variables to include this block.
530          */
531         min_color[0] = min_color_blk[0];
532         min_color[1] = min_color_blk[1];
533         min_color[2] = min_color_blk[2];
534         max_color[0] = max_color_blk[0];
535         max_color[1] = max_color_blk[1];
536         max_color[2] = max_color_blk[2];
537         total_rgb[0] = total_rgb_blk[0];
538         total_rgb[1] = total_rgb_blk[1];
539         total_rgb[2] = total_rgb_blk[2];
540         *total_pixels = total_pixels_blk;
541         avg_color[0] = avg_color_blk[0];
542         avg_color[1] = avg_color_blk[1];
543         avg_color[2] = avg_color_blk[2];
544     }
545 
546     return is_in_range;
547 }
548 
rpza_encode_stream(RpzaContext * s,const AVFrame * pict)549 static void rpza_encode_stream(RpzaContext *s, const AVFrame *pict)
550 {
551     BlockInfo bi;
552     int block_counter = 0;
553     int n_blocks;
554     int total_blocks;
555     int prev_block_offset;
556     int block_offset = 0;
557     uint8_t min = 0, max = 0;
558     channel_offset chan;
559     int i;
560     int tmp_min, tmp_max;
561     int total_rgb[3];
562     uint8_t avg_color[3];
563     int pixel_count;
564     uint8_t min_color[3], max_color[3];
565     double slope, y_intercept, correlation_coef;
566     uint16_t *src_pixels = (uint16_t *)pict->data[0];
567     uint16_t *prev_pixels = (uint16_t *)s->prev_frame->data[0];
568 
569     /* Number of 4x4 blocks in frame. */
570     total_blocks = ((s->frame_width + 3) / 4) * ((s->frame_height + 3) / 4);
571 
572     bi.image_width = s->frame_width;
573     bi.image_height = s->frame_height;
574     bi.rowstride = pict->linesize[0] / 2;
575 
576     bi.blocks_per_row = (s->frame_width + 3) / 4;
577 
578     while (block_counter < total_blocks) {
579         // SKIP CHECK
580         // make sure we have a valid previous frame and we're not writing
581         // a key frame
582         if (!s->first_frame) {
583             n_blocks = 0;
584             prev_block_offset = 0;
585 
586             while (n_blocks < 32 && block_counter + n_blocks < total_blocks) {
587 
588                 block_offset = get_block_info(&bi, block_counter + n_blocks);
589 
590                 // multi-block opcodes cannot span multiple rows.
591                 // If we're starting a new row, break out and write the opcode
592                 /* TODO: Should eventually use bi.row here to determine when a
593                    row break occurs, but that is currently breaking the
594                    quicktime player. This is probably due to a bug in the
595                    way I'm calculating the current row.
596                  */
597                 if (prev_block_offset && block_offset - prev_block_offset > 12) {
598                     break;
599                 }
600 
601                 prev_block_offset = block_offset;
602 
603                 if (compare_blocks(&prev_pixels[block_offset],
604                                    &src_pixels[block_offset], &bi, s->skip_frame_thresh) != 0) {
605                     // write out skipable blocks
606                     if (n_blocks) {
607 
608                         // write skip opcode
609                         put_bits(&s->pb, 8, 0x80 | (n_blocks - 1));
610                         block_counter += n_blocks;
611 
612                         goto post_skip;
613                     }
614                     break;
615                 }
616 
617                 /*
618                  * NOTE: we don't update skipped blocks in the previous frame buffer
619                  * since skipped needs always to be compared against the first skipped
620                  * block to avoid artifacts during gradual fade in/outs.
621                  */
622 
623                 // update_block_in_prev_frame(&src_pixels[block_offset],
624                 //   &prev_pixels[block_offset], &bi, block_counter + n_blocks);
625 
626                 n_blocks++;
627             }
628 
629             // we're either at the end of the frame or we've reached the maximum
630             // of 32 blocks in a run. Write out the run.
631             if (n_blocks) {
632                 // write skip opcode
633                 put_bits(&s->pb, 8, 0x80 | (n_blocks - 1));
634                 block_counter += n_blocks;
635 
636                 continue;
637             }
638 
639         } else {
640             block_offset = get_block_info(&bi, block_counter);
641         }
642 post_skip :
643 
644         // ONE COLOR CHECK
645         if (update_block_stats(s, &bi, &src_pixels[block_offset],
646                                min_color, max_color,
647                                total_rgb, &pixel_count, avg_color, 1)) {
648             prev_block_offset = block_offset;
649 
650             n_blocks = 1;
651 
652             /* update this block in the previous frame buffer */
653             update_block_in_prev_frame(&src_pixels[block_offset],
654                                        &prev_pixels[block_offset], &bi, block_counter + n_blocks);
655 
656             // check for subsequent blocks with the same color
657             while (n_blocks < 32 && block_counter + n_blocks < total_blocks) {
658                 block_offset = get_block_info(&bi, block_counter + n_blocks);
659 
660                 // multi-block opcodes cannot span multiple rows.
661                 // If we've hit end of a row, break out and write the opcode
662                 if (block_offset - prev_block_offset > 12) {
663                     break;
664                 }
665 
666                 if (!update_block_stats(s, &bi, &src_pixels[block_offset],
667                                         min_color, max_color,
668                                         total_rgb, &pixel_count, avg_color, 0)) {
669                     break;
670                 }
671 
672                 prev_block_offset = block_offset;
673 
674                 /* update this block in the previous frame buffer */
675                 update_block_in_prev_frame(&src_pixels[block_offset],
676                                            &prev_pixels[block_offset], &bi, block_counter + n_blocks);
677 
678                 n_blocks++;
679             }
680 
681             // write one color opcode.
682             put_bits(&s->pb, 8, 0xa0 | (n_blocks - 1));
683             // write color to encode.
684             put_bits(&s->pb, 16, rgb24_to_rgb555(avg_color));
685             // skip past the blocks we've just encoded.
686             block_counter += n_blocks;
687         } else { // FOUR COLOR CHECK
688             int err = 0;
689 
690             // get max component diff for block
691             get_max_component_diff(&bi, &src_pixels[block_offset], &min, &max, &chan);
692 
693             min_color[0] = 0;
694             max_color[0] = 0;
695             min_color[1] = 0;
696             max_color[1] = 0;
697             min_color[2] = 0;
698             max_color[2] = 0;
699 
700             // run least squares against other two components
701             for (i = 0; i < 3; i++) {
702                 if (i == chan) {
703                     min_color[i] = min;
704                     max_color[i] = max;
705                     continue;
706                 }
707 
708                 slope = y_intercept = correlation_coef = 0;
709 
710                 if (leastsquares(&src_pixels[block_offset], &bi, chan, i,
711                                  &slope, &y_intercept, &correlation_coef)) {
712                     min_color[i] = GET_CHAN(src_pixels[block_offset], i);
713                     max_color[i] = GET_CHAN(src_pixels[block_offset], i);
714                 } else {
715                     tmp_min = (int)(0.5 + min * slope + y_intercept);
716                     tmp_max = (int)(0.5 + max * slope + y_intercept);
717 
718                     av_assert0(tmp_min <= tmp_max);
719                     // clamp min and max color values
720                     tmp_min = av_clip_uint8(tmp_min);
721                     tmp_max = av_clip_uint8(tmp_max);
722 
723                     err = FFMAX(calc_lsq_max_fit_error(&src_pixels[block_offset], &bi,
724                                                        min, max, tmp_min, tmp_max, chan, i), err);
725 
726                     min_color[i] = tmp_min;
727                     max_color[i] = tmp_max;
728                 }
729             }
730 
731             if (err > s->sixteen_color_thresh) { // DO SIXTEEN COLOR BLOCK
732                 uint16_t *row_ptr;
733                 int y_size, rgb555;
734 
735                 block_offset = get_block_info(&bi, block_counter);
736 
737                 row_ptr = &src_pixels[block_offset];
738                 y_size = FFMIN(4, bi.image_height - bi.row * 4);
739 
740                 for (int y = 0; y < y_size; y++) {
741                     for (int x = 0; x < 4; x++) {
742                         rgb555 = row_ptr[x] & ~0x8000;
743 
744                         put_bits(&s->pb, 16, rgb555);
745                     }
746                     row_ptr += bi.rowstride;
747                 }
748 
749                 for (int y = y_size; y < 4; y++) {
750                     for (int x = 0; x < 4; x++)
751                         put_bits(&s->pb, 16, 0);
752                 }
753 
754                 block_counter++;
755             } else { // FOUR COLOR BLOCK
756                 block_counter += encode_four_color_block(min_color, max_color,
757                                                          &s->pb, &src_pixels[block_offset], &bi);
758             }
759 
760             /* update this block in the previous frame buffer */
761             update_block_in_prev_frame(&src_pixels[block_offset],
762                                        &prev_pixels[block_offset], &bi, block_counter);
763         }
764     }
765 }
766 
rpza_encode_init(AVCodecContext * avctx)767 static int rpza_encode_init(AVCodecContext *avctx)
768 {
769     RpzaContext *s = avctx->priv_data;
770 
771     s->frame_width = avctx->width;
772     s->frame_height = avctx->height;
773 
774     s->prev_frame = av_frame_alloc();
775     if (!s->prev_frame)
776         return AVERROR(ENOMEM);
777 
778     return 0;
779 }
780 
rpza_encode_frame(AVCodecContext * avctx,AVPacket * pkt,const AVFrame * frame,int * got_packet)781 static int rpza_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
782                                 const AVFrame *frame, int *got_packet)
783 {
784     RpzaContext *s = avctx->priv_data;
785     const AVFrame *pict = frame;
786     uint8_t *buf;
787     int ret;
788 
789     if ((ret = ff_alloc_packet2(avctx, pkt, 6LL * avctx->height * avctx->width, 0)) < 0)
790         return ret;
791 
792     init_put_bits(&s->pb, pkt->data, pkt->size);
793 
794     // skip 4 byte header, write it later once the size of the chunk is known
795     put_bits32(&s->pb, 0x00);
796 
797     if (!s->prev_frame->data[0]) {
798         s->first_frame = 1;
799         s->prev_frame->format = pict->format;
800         s->prev_frame->width = pict->width;
801         s->prev_frame->height = pict->height;
802         ret = av_frame_get_buffer(s->prev_frame, 0);
803         if (ret < 0)
804             return ret;
805     } else {
806         s->first_frame = 0;
807     }
808 
809     rpza_encode_stream(s, pict);
810 
811     flush_put_bits(&s->pb);
812 
813     av_shrink_packet(pkt, put_bits_count(&s->pb) >> 3);
814     buf = pkt->data;
815 
816     // write header opcode
817     buf[0] = 0xe1; // chunk opcode
818 
819     // write chunk length
820     AV_WB24(buf + 1, pkt->size);
821 
822     *got_packet = 1;
823 
824     return 0;
825 }
826 
rpza_encode_end(AVCodecContext * avctx)827 static int rpza_encode_end(AVCodecContext *avctx)
828 {
829     RpzaContext *s = (RpzaContext *)avctx->priv_data;
830 
831     av_frame_free(&s->prev_frame);
832 
833     return 0;
834 }
835 
836 #define OFFSET(x) offsetof(RpzaContext, x)
837 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
838 static const AVOption options[] = {
839     { "skip_frame_thresh", NULL, OFFSET(skip_frame_thresh), AV_OPT_TYPE_INT, {.i64=1}, 0, 24, VE},
840     { "start_one_color_thresh", NULL, OFFSET(start_one_color_thresh), AV_OPT_TYPE_INT, {.i64=1}, 0, 24, VE},
841     { "continue_one_color_thresh", NULL, OFFSET(continue_one_color_thresh), AV_OPT_TYPE_INT, {.i64=0}, 0, 24, VE},
842     { "sixteen_color_thresh", NULL, OFFSET(sixteen_color_thresh), AV_OPT_TYPE_INT, {.i64=1}, 0, 24, VE},
843     { NULL },
844 };
845 
846 static const AVClass rpza_class = {
847     .class_name = "rpza",
848     .item_name  = av_default_item_name,
849     .option     = options,
850     .version    = LIBAVUTIL_VERSION_INT,
851 };
852 
853 AVCodec ff_rpza_encoder = {
854     .name           = "rpza",
855     .long_name      = NULL_IF_CONFIG_SMALL("QuickTime video (RPZA)"),
856     .type           = AVMEDIA_TYPE_VIDEO,
857     .id             = AV_CODEC_ID_RPZA,
858     .priv_data_size = sizeof(RpzaContext),
859     .priv_class     = &rpza_class,
860     .init           = rpza_encode_init,
861     .encode2        = rpza_encode_frame,
862     .close          = rpza_encode_end,
863     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
864     .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_RGB555,
865                                                      AV_PIX_FMT_NONE},
866 };
867