66#define SQR(x) ((x) * (x))
69#define GET_CHAN(color, chan) (((color) >> ((chan) * 5) & 0x1F))
70#define R(color) GET_CHAN(color, RED)
71#define G(color) GET_CHAN(color, GREEN)
72#define B(color) GET_CHAN(color, BLUE)
93 color4[0][0] =
min[0];
94 color4[0][1] =
min[1];
95 color4[0][2] =
min[2];
97 color4[3][0] =
max[0];
98 color4[3][1] =
max[1];
99 color4[3][2] =
max[2];
102 step = (color4[3][0] - color4[0][0] + 1) / 3;
103 color4[1][0] = color4[0][0] + step;
104 color4[2][0] = color4[3][0] - step;
107 step = (color4[3][1] - color4[0][1] + 1) / 3;
108 color4[1][1] = color4[0][1] + step;
109 color4[2][1] = color4[3][1] - step;
112 step = (color4[3][2] - color4[0][2] + 1) / 3;
113 color4[1][2] = color4[0][2] + step;
114 color4[2][2] = color4[3][2] - step;
159static int diff_colors(
const uint8_t *colorA,
const uint8_t *colorB)
163 tot =
SQR(colorA[0] - colorB[0]);
164 tot +=
SQR(colorA[1] - colorB[1]);
165 tot +=
SQR(colorA[2] - colorB[2]);
201 uint8_t min_r, max_r, min_g, max_g, min_b, max_b;
205 min_r = min_g = min_b = UINT8_MAX;
206 max_r = max_g = max_b = 0;
212 min_r =
FFMIN(
R(block_ptr[x]), min_r);
213 min_g =
FFMIN(
G(block_ptr[x]), min_g);
214 min_b =
FFMIN(
B(block_ptr[x]), min_b);
216 max_r =
FFMAX(
R(block_ptr[x]), max_r);
217 max_g =
FFMAX(
G(block_ptr[x]), max_g);
218 max_b =
FFMAX(
B(block_ptr[x]), max_b);
227 if (
r >
g &&
r >
b) {
231 }
else if (
g >
b &&
g >=
r) {
254 if (
diff >= thresh) {
270 int *slope,
int *y_intercept,
int *correlation_coef)
272 int sumx = 0, sumy = 0, sumx2 = 0, sumy2 = 0, sumxy = 0,
273 sumx_sq = 0, sumy_sq = 0,
tmp, tmp2;
284 x =
GET_CHAN(block_ptr[j], xchannel);
285 y =
GET_CHAN(block_ptr[j], ychannel);
295 sumx_sq = sumx * sumx;
296 tmp = (count * sumx2 - sumx_sq);
302 sumy_sq = sumy * sumy;
304 *slope = (sumx * sumy - sumxy) /
tmp;
305 *y_intercept = (sumy - (*slope) * sumx) / count;
307 tmp2 = count * sumy2 - sumy_sq;
309 *correlation_coef = 0;
311 *correlation_coef = (count * sumxy - sumx * sumy) /
322 int min,
int max,
int tmp_min,
int tmp_max,
331 int x_inc, lin_y, lin_x;
332 x =
GET_CHAN(block_ptr[j], xchannel);
333 y =
GET_CHAN(block_ptr[j], ychannel);
340 lin_y = tmp_min + (tmp_max - tmp_min) * x_inc / 3 + 1;
342 err =
FFABS(lin_y - y);
347 lin_x =
min + (
max -
min) * x_inc / 3 + 1;
349 err =
FFABS(lin_x - x);
365 int smallest_variance = INT_MAX;
366 uint8_t dithered_color[3];
372 for (
int palette_entry = 0; palette_entry < 4; palette_entry++) {
373 int variance =
diff_colors(dithered_color, colors[palette_entry]);
375 if (variance < smallest_variance) {
376 smallest_variance = variance;
394 uint8_t color4[4][3];
395 uint16_t rounded_max, rounded_min;
404 put_bits(pb, 16, rounded_max & ~0x8000);
406 put_bits(pb, 16, rounded_min | 0x8000);
410 for (
int y = 0; y < y_size; y++) {
411 for (
int x = 0; x < x_size; x++) {
416 for (
int x = x_size; x < 4; x++)
421 for (
int y = y_size; y < 4; y++) {
422 for (
int x = 0; x < 4; x++)
432 uint16_t *dest_pixels,
438 for (
int y = 0; y < y_size; y++) {
439 memcpy(dest_pixels, src_pixels, x_size);
461 uint8_t min_color[3], uint8_t max_color[3],
462 int *total_rgb,
int *total_pixels,
463 uint8_t avg_color[3],
int first_block)
467 int total_pixels_blk;
470 uint8_t min_color_blk[3], max_color_blk[3];
471 int total_rgb_blk[3];
472 uint8_t avg_color_blk[3];
475 min_color[0] = UINT8_MAX;
476 min_color[1] = UINT8_MAX;
477 min_color[2] = UINT8_MAX;
485 threshold =
s->start_one_color_thresh;
487 threshold =
s->continue_one_color_thresh;
494 min_color_blk[0] = min_color[0];
495 min_color_blk[1] = min_color[1];
496 min_color_blk[2] = min_color[2];
497 max_color_blk[0] = max_color[0];
498 max_color_blk[1] = max_color[1];
499 max_color_blk[2] = max_color[2];
500 total_rgb_blk[0] = total_rgb[0];
501 total_rgb_blk[1] = total_rgb[1];
502 total_rgb_blk[2] = total_rgb[2];
510 total_rgb_blk[0] +=
R(
block[x]);
511 total_rgb_blk[1] +=
G(
block[x]);
512 total_rgb_blk[2] +=
B(
block[x]);
514 min_color_blk[0] =
FFMIN(
R(
block[x]), min_color_blk[0]);
515 min_color_blk[1] =
FFMIN(
G(
block[x]), min_color_blk[1]);
516 min_color_blk[2] =
FFMIN(
B(
block[x]), min_color_blk[2]);
518 max_color_blk[0] =
FFMAX(
R(
block[x]), max_color_blk[0]);
519 max_color_blk[1] =
FFMAX(
G(
block[x]), max_color_blk[1]);
520 max_color_blk[2] =
FFMAX(
B(
block[x]), max_color_blk[2]);
528 avg_color_blk[0] = total_rgb_blk[0] / total_pixels_blk;
529 avg_color_blk[1] = total_rgb_blk[1] / total_pixels_blk;
530 avg_color_blk[2] = total_rgb_blk[2] / total_pixels_blk;
535 is_in_range = (max_color_blk[0] - avg_color_blk[0] <= threshold &&
536 max_color_blk[1] - avg_color_blk[1] <= threshold &&
537 max_color_blk[2] - avg_color_blk[2] <= threshold &&
538 avg_color_blk[0] - min_color_blk[0] <= threshold &&
539 avg_color_blk[1] - min_color_blk[1] <= threshold &&
540 avg_color_blk[2] - min_color_blk[2] <= threshold);
546 min_color[0] = min_color_blk[0];
547 min_color[1] = min_color_blk[1];
548 min_color[2] = min_color_blk[2];
549 max_color[0] = max_color_blk[0];
550 max_color[1] = max_color_blk[1];
551 max_color[2] = max_color_blk[2];
552 total_rgb[0] = total_rgb_blk[0];
553 total_rgb[1] = total_rgb_blk[1];
554 total_rgb[2] = total_rgb_blk[2];
555 *total_pixels = total_pixels_blk;
556 avg_color[0] = avg_color_blk[0];
557 avg_color[1] = avg_color_blk[1];
558 avg_color[2] = avg_color_blk[2];
567 int block_counter = 0;
570 int prev_block_offset;
571 int block_offset = 0;
572 int pblock_offset = 0;
576 int tmp_min, tmp_max;
578 uint8_t avg_color[3];
580 uint8_t min_color[3], max_color[3];
581 int slope, y_intercept, correlation_coef;
582 const uint16_t *src_pixels = (
const uint16_t *)pict->
data[0];
583 uint16_t *prev_pixels = (uint16_t *)
s->prev_frame->data[0];
586 total_blocks = ((
s->frame_width + 3) / 4) * ((
s->frame_height + 3) / 4);
595 while (block_counter < total_blocks) {
599 if (!
s->first_frame) {
601 prev_block_offset = 0;
603 while (n_blocks < 32 && block_counter + n_blocks < total_blocks) {
614 if (prev_block_offset && block_offset - prev_block_offset > 12) {
618 prev_block_offset = block_offset;
621 &src_pixels[block_offset], &bi,
s->skip_frame_thresh) != 0) {
626 put_bits(&
s->pb, 8, 0x80 | (n_blocks - 1));
627 block_counter += n_blocks;
650 put_bits(&
s->pb, 8, 0x80 | (n_blocks - 1));
651 block_counter += n_blocks;
664 min_color, max_color,
665 total_rgb, &pixel_count, avg_color, 1)) {
666 prev_block_offset = block_offset;
672 &prev_pixels[pblock_offset], &bi, block_counter + n_blocks);
675 while (n_blocks < 32 && block_counter + n_blocks < total_blocks) {
681 if (block_offset - prev_block_offset > 12) {
686 min_color, max_color,
687 total_rgb, &pixel_count, avg_color, 0)) {
691 prev_block_offset = block_offset;
695 &prev_pixels[pblock_offset], &bi, block_counter + n_blocks);
701 put_bits(&
s->pb, 8, 0xa0 | (n_blocks - 1));
705 block_counter += n_blocks;
720 for (
i = 0;
i < 3;
i++) {
727 slope = y_intercept = correlation_coef = 0;
730 &slope, &y_intercept, &correlation_coef)) {
731 min_color[
i] =
GET_CHAN(src_pixels[block_offset],
i);
732 max_color[
i] =
GET_CHAN(src_pixels[block_offset],
i);
734 tmp_min = 1 +
min * slope + y_intercept;
735 tmp_max = 1 +
max * slope + y_intercept;
743 min,
max, tmp_min, tmp_max, chan,
i), err);
745 min_color[
i] = tmp_min;
746 max_color[
i] = tmp_max;
750 if (err >
s->sixteen_color_thresh) {
751 const uint16_t *row_ptr;
752 int y_size, x_size, rgb555;
757 row_ptr = &src_pixels[block_offset];
761 for (
int y = 0; y < y_size; y++) {
762 for (
int x = 0; x < x_size; x++) {
763 rgb555 = row_ptr[x] & ~0x8000;
767 for (
int x = x_size; x < 4; x++)
773 for (
int y = y_size; y < 4; y++) {
774 for (
int x = 0; x < 4; x++)
781 &
s->pb, &src_pixels[block_offset], &bi);
786 &prev_pixels[pblock_offset], &bi, block_counter);
795 s->frame_width = avctx->
width;
796 s->frame_height = avctx->
height;
806 const AVFrame *pict,
int *got_packet)
820 if (!
s->prev_frame->data[0]) {
822 s->prev_frame->format = pict->
format;
823 s->prev_frame->width = pict->
width;
824 s->prev_frame->height = pict->
height;
859#define OFFSET(x) offsetof(RpzaContext, x)
860#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
870 .class_name =
"rpza",
const FFCodec ff_rpza_encoder
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
#define i(width, name, range_min, range_max)
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
common internal and external API header
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static int16_t block1[64]
channel
Use these values when setting the channel map with ebur128_set_channel().
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
@ AV_OPT_TYPE_INT
Underlying C type is int.
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
const char * av_default_item_name(void *ptr)
Return the context name.
#define LIBAVUTIL_VERSION_INT
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
#define AV_PIX_FMT_RGB555
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static int put_bytes_output(const PutBitContext *s)
static av_unused void put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
static int get_block_info(BlockInfo *bi, int block, int prev_frame)
static av_cold int rpza_encode_end(AVCodecContext *avctx)
static void get_colors(const uint8_t *min, const uint8_t *max, uint8_t color4[4][3])
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)
static int rpza_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
static void get_max_component_diff(const BlockInfo *bi, const uint16_t *block_ptr, uint8_t *min, uint8_t *max, channel_offset *chan)
static const AVClass rpza_class
static void update_block_in_prev_frame(const uint16_t *src_pixels, uint16_t *dest_pixels, const BlockInfo *bi, int block_counter)
static int match_color(const uint16_t *color, uint8_t colors[4][3])
static int max_component_diff(const uint16_t *colorA, const uint16_t *colorB)
static void rpza_encode_stream(RpzaContext *s, const AVFrame *pict)
static int diff_colors(const uint8_t *colorA, const uint8_t *colorB)
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)
static uint16_t rgb24_to_rgb555(const uint8_t *rgb24)
static int compare_blocks(const uint16_t *block1, const uint16_t *block2, const BlockInfo *bi, int thresh)
static av_cold int rpza_encode_init(AVCodecContext *avctx)
#define GET_CHAN(color, chan)
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)
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)
Describe the class of an AVClass context structure.
main external API structure.
int width
picture width / height.
This structure describes decoded (raw) audio or video data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
This structure stores compressed data.
int continue_one_color_thresh
int start_one_color_thresh
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)