33#define MAX_RLE_BULK 127
35#define MAX_RLE_REPEAT 128
37#define MAX_RLE_SKIP 254
87 s->logical_width=avctx->
width;
91 if (avctx->
width % 4) {
95 s->logical_width = avctx->
width / 4;
115 s->length_table =
av_calloc(
s->logical_width + 1,
sizeof(*
s->length_table));
116 if (!
s->skip_table || !
s->length_table || !
s->rlecode_table) {
121 if (!
s->previous_frame) {
126 s->max_buf_size =
s->logical_width*
s->avctx->height*
s->pixel_size*2
139 int width=
s->logical_width;
145 unsigned int skipcount;
152 int total_repeat_cost;
155 int lowest_bulk_cost;
156 int lowest_bulk_cost_index;
157 int sec_lowest_bulk_cost;
158 int sec_lowest_bulk_cost_index;
160 const uint8_t *this_line = p->data[0] +
line * p->linesize[0] +
width *
s->pixel_size;
164 const uint8_t *prev_line =
s->key_frame ? this_line
165 :
s->previous_frame->data[0]
166 +
line *
s->previous_frame->linesize[0]
169 s->length_table[
width] = 0;
173 lowest_bulk_cost = INT_MAX / 2;
174 lowest_bulk_cost_index =
width;
175 sec_lowest_bulk_cost = INT_MAX / 2;
176 sec_lowest_bulk_cost_index =
width;
178 base_bulk_cost = 1 +
s->pixel_size;
184 this_line -=
s->pixel_size;
185 prev_line -=
s->pixel_size;
190 lowest_bulk_cost = sec_lowest_bulk_cost;
191 lowest_bulk_cost_index = sec_lowest_bulk_cost_index;
193 sec_lowest_bulk_cost = INT_MAX / 2;
194 sec_lowest_bulk_cost_index =
width;
201 sec_lowest_bulk_cost++;
206 prev_bulk_cost =
s->length_table[
i + 1] + base_bulk_cost;
207 if (prev_bulk_cost <= sec_lowest_bulk_cost) {
210 if (prev_bulk_cost <= lowest_bulk_cost) {
215 sec_lowest_bulk_cost = INT_MAX / 2;
217 lowest_bulk_cost = prev_bulk_cost;
218 lowest_bulk_cost_index =
i + 1;
221 sec_lowest_bulk_cost = prev_bulk_cost;
222 sec_lowest_bulk_cost_index =
i + 1;
226 if (!
s->key_frame && !memcmp(this_line, prev_line,
s->pixel_size))
231 total_skip_cost =
s->length_table[
i + skipcount] + 2;
232 s->skip_table[
i] = skipcount;
235 if (
i <
width - 1 && !memcmp(this_line, this_line +
s->pixel_size,
s->pixel_size))
240 total_repeat_cost =
s->length_table[
i + repeatcount] + 1 +
s->pixel_size;
249 if (repeatcount > 1 && (skipcount == 0 || total_repeat_cost < total_skip_cost)) {
251 s->length_table[
i] = total_repeat_cost;
252 s->rlecode_table[
i] = -repeatcount;
254 else if (skipcount > 0) {
256 s->length_table[
i] = total_skip_cost;
257 s->rlecode_table[
i] = 0;
263 s->length_table[
i] = lowest_bulk_cost;
264 s->rlecode_table[
i] = lowest_bulk_cost_index -
i;
269 lowest_bulk_cost +=
s->pixel_size;
270 sec_lowest_bulk_cost +=
s->pixel_size;
279 this_line = p->
data[0] +
line*p->linesize[0];
281 if (
s->rlecode_table[0] == 0) {
282 bytestream_put_byte(buf,
s->skip_table[0] + 1);
283 i +=
s->skip_table[0];
285 else bytestream_put_byte(buf, 1);
289 rlecode =
s->rlecode_table[
i];
290 bytestream_put_byte(buf, rlecode);
293 bytestream_put_byte(buf,
s->skip_table[
i] + 1);
294 i +=
s->skip_table[
i];
296 else if (rlecode > 0) {
303 for (j = 0; j < rlecode*
s->pixel_size; ++j)
304 bytestream_put_byte(buf, *(this_line +
i*
s->pixel_size + j) ^ 0xff);
315 for (j = 0; j <
s->pixel_size; ++j)
316 bytestream_put_byte(buf, *(this_line +
i*
s->pixel_size + j) ^ 0xff);
323 bytestream_put_byte(buf, -1);
331 int end_line =
s->avctx->height;
332 uint8_t *orig_buf = buf;
335 unsigned line_size =
s->logical_width *
s->pixel_size;
336 for (start_line = 0; start_line <
s->avctx->height; start_line++)
337 if (memcmp(p->data[0] + start_line*p->linesize[0],
338 s->previous_frame->data[0] + start_line *
s->previous_frame->linesize[0],
342 for (end_line=
s->avctx->height; end_line > start_line; end_line--)
343 if (memcmp(p->data[0] + (end_line - 1)*p->linesize[0],
344 s->previous_frame->data[0] + (end_line - 1) *
s->previous_frame->linesize[0],
349 bytestream_put_be32(&buf, 0);
351 if ((start_line == 0 && end_line ==
s->avctx->height) || start_line ==
s->avctx->height)
352 bytestream_put_be16(&buf, 0);
354 bytestream_put_be16(&buf, 8);
355 bytestream_put_be16(&buf, start_line);
356 bytestream_put_be16(&buf, 0);
357 bytestream_put_be16(&buf, end_line - start_line);
358 bytestream_put_be16(&buf, 0);
360 for (
i = start_line;
i < end_line;
i++)
363 bytestream_put_byte(&buf, 0);
364 AV_WB32(orig_buf, buf - orig_buf);
365 return buf - orig_buf;
369 const AVFrame *pict,
int *got_packet)
377 if (avctx->
gop_size == 0 || !
s->previous_frame->data[0] ||
378 (
s->avctx->frame_num % avctx->
gop_size) == 0) {
const FFCodec ff_qtrle_encoder
Libavcodec external API header.
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
#define i(width, name, range_min, range_max)
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
int(* init)(AVBSFContext *ctx)
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
#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.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
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.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
@ AV_PIX_FMT_RGB555BE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), big-endian , X=unused/undefined
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
static av_cold int qtrle_encode_end(AVCodecContext *avctx)
static av_cold int qtrle_encode_init(AVCodecContext *avctx)
#define MAX_RLE_REPEAT
Maximum RLE code for repeat.
static void qtrle_encode_line(QtrleEncContext *s, const AVFrame *p, int line, uint8_t **buf)
Compute the best RLE sequence for a line.
static int qtrle_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
#define MAX_RLE_SKIP
Maximum RLE code for skip.
#define MAX_RLE_BULK
Maximum RLE code for bulk copy.
static int encode_frame(QtrleEncContext *s, const AVFrame *p, uint8_t *buf)
Encode frame including header.
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int width
picture width / height.
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
unsigned int max_buf_size
uint8_t * skip_table
Will contain at ith position the number of consecutive pixels equal to the previous frame starting fr...
int * length_table
This array will contain the length of the best rle encoding of the line starting at ith pixel.
signed char * rlecode_table
This array will contain at ith position the value of the best RLE code if the line started at pixel i...
int key_frame
Encoded frame is a key frame.