74 #define MAX_CBS_4x4 256
76 #define MAX_CBS_2x2 256
79 #define ROQ_LAMBDA_SCALE ((uint64_t) FF_LAMBDA_SCALE)
163 memcpy(
u ,
cell->y, 4);
164 memset(
u+4,
cell->u, 4);
165 memset(
u+8,
cell->v, 4);
171 static const int offsets[4] = {0, 2, 8, 10};
173 for (cp=0; cp<3; cp++)
174 for (
i=0;
i<4;
i++) {
176 u[4*4*cp +
offsets[
i]+1] = cb2[qcell->
idx[
i]*2*2*3 + 4*cp+1];
177 u[4*4*cp +
offsets[
i]+4] = cb2[qcell->
idx[
i]*2*2*3 + 4*cp+2];
178 u[4*4*cp +
offsets[
i]+5] = cb2[qcell->
idx[
i]*2*2*3 + 4*cp+3];
187 for(cp=0; cp<3; cp++)
190 *
u++ =
base[(y/2)*4 + (x/2) + 16*cp];
211 int x2,
int y2,
const int *stride1,
const int *stride2,
int size)
216 for (k=0; k<3; k++) {
220 buf2[k] + (y2+
i)*stride2[k] + x2,
size);
233 if (mx < -7 || mx > 7)
236 if (my < -7 || my > 7)
258 for(cp=0;cp<3;cp++) {
280 for (
int y = 0, n = 0; y < roq->
height; y += 16)
281 for (
int x = 0; x < roq->
width; x += 16)
282 for(
int i = 0;
i < 4;
i++) {
297 for (cp=0; cp<3; cp++) {
300 for (j=0; j<
dim; j++)
309 int *outIndex,
int dim)
311 int i, lDiff = INT_MAX, pick=0;
314 for (
i=0;
i<numCB;
i++) {
326 #define EVAL_MOTION(MOTION) \
328 diff = eval_motion_dist(enc, j, i, MOTION, blocksize); \
330 if (diff < lowestdiff) { \
350 int diff, lowestdiff, oldbest;
358 const int max = (roq->
width / blocksize) * roq->
height / blocksize;
360 if (blocksize == 4) {
368 for (
i = 0;
i< roq->
height;
i += blocksize)
369 for (j = 0; j < roq->
width; j += blocksize) {
378 offset = (
i/blocksize) * roq->
width / blocksize + j / blocksize;
379 if (offset < max && offset >= 0)
383 if (offset < max && offset >= 0)
386 offset = (
i/blocksize + 1) * roq->
width / blocksize + j / blocksize;
387 if (offset < max && offset >= 0)
390 off[0]= (
i/blocksize) * roq->
width / blocksize + j/blocksize - 1;
391 off[1]= off[0] - roq->
width / blocksize + 1;
397 vect.
d[k]=
mid_pred(this_motion[off[0]].d[k],
398 this_motion[off[1]].d[k],
399 this_motion[off[2]].d[k]);
410 while (oldbest != lowestdiff) {
411 oldbest = lowestdiff;
412 for (k=0; k<8; k++) {
420 offset = (
i/blocksize) * roq->
width / blocksize + j/blocksize;
421 this_motion[
offset] = bestpick;
438 static const int bitsUsed[4] = {2, 10, 10, 34};
459 cluster_index = y * roq->
width / 16 + x / 4;
500 int i, j, best_dist, divide_bit_use;
502 int bitsUsed[4] = {2, 10, 10, 0};
536 for (
i=0;
i<4;
i++) {
543 bitsUsed[3] = 2 + divide_bit_use;
560 for (
i=0;
i<4;
i++) {
578 tempData->
i2f4[
i] = idx;
579 tempData->
f2i4[idx] =
i;
591 tempData->
i2f2[
i] = idx;
592 tempData->
f2i2[idx] =
i;
612 bytestream_put_le32(outp, tempData->
numCB2*6 + tempData->
numCB4*4);
613 bytestream_put_byte(outp, tempData->
numCB4);
614 bytestream_put_byte(outp, tempData->
numCB2);
618 bytestream_put_byte(outp, roq->
cb2x2[tempData->
f2i2[
i]].
u);
619 bytestream_put_byte(outp, roq->
cb2x2[tempData->
f2i2[
i]].
v);
633 return ((ax&15)<<4) | (ay&15);
647 s->typeSpool |= (
type & 3) << (14 -
s->typeSpoolLength);
648 s->typeSpoolLength += 2;
649 if (
s->typeSpoolLength == 16) {
650 bytestream_put_le16(
s->pout,
s->typeSpool);
652 s->args -
s->argumentSpool);
653 s->typeSpoolLength = 0;
655 s->args =
s->argumentSpool;
660 int w,
int h,
int numBlocks)
685 bytestream_put_byte(&enc->
out_buf, 0x0);
686 bytestream_put_byte(&enc->
out_buf, 0x0);
688 for (
i=0;
i<numBlocks;
i++) {
722 for (j=0; j<4; j++) {
731 bytestream_put_byte(&spool.
args,
740 bytestream_put_byte(&spool.
args,
756 for (k=0; k<4; k++) {
758 bytestream_put_byte(&spool.
args,
759 tempData->
i2f2[cb_idx]);
762 roq->
cb2x2 + cb_idx);
782 int top,
int left,
const int *
stride)
787 for (j=0; j<2; j++) {
788 int x = (top+
i)*
stride[0] + left + j;
790 x = (top+
i)*
stride[1] + left + j;
807 for (j=0; j<
w; j+=4) {
808 for (k=0; k < 2; k++)
809 for (l=0; l < 2; l++)
817 int *points,
int inputCount,
roq_cell *results,
818 int size,
int cbsize)
820 int i, j, k, ret = 0;
827 cbsize, 1, closest_cb, &enc->
randctx);
831 cbsize, 1, closest_cb, &enc->
randctx);
836 for (
i=0;
i<cbsize;
i++)
837 for (k=0; k<c_size; k++) {
839 results->
y[j] = *buf++;
855 int *points = enc->
points;
864 4, codebooks->
numCB4)) < 0)
880 for (j=0; j<4; j++) {
900 memset(tempData, 0,
sizeof(*tempData));
917 if (enc->
lambda > 100000) {
922 "Warning, generated a frame too big for Quake (%d > 65535), "
923 "now switching to a bigger qscale value.\n",
980 if ((avctx->
width & 0xf) || (avctx->
height & 0xf)) {
985 if (avctx->
width > 65535 || avctx->
height > 65535) {
991 av_log(avctx,
AV_LOG_ERROR,
"Warning: dimensions not power of two, this is not supported by quake\n");
1036 bytestream_put_le32(&enc->
out_buf, 8);
1039 bytestream_put_byte(&enc->
out_buf, 0x00);
1040 bytestream_put_byte(&enc->
out_buf, 0x00);
1049 bytestream_put_byte(&enc->
out_buf, 0x08);
1050 bytestream_put_byte(&enc->
out_buf, 0x00);
1051 bytestream_put_byte(&enc->
out_buf, 0x04);
1052 bytestream_put_byte(&enc->
out_buf, 0x00);
1073 size = ((roq->
width * roq->
height / 64) * 138 + 7) / 8 + 256 * (6 + 4) + 8;
1108 #define OFFSET(x) offsetof(RoqEncContext, x)
1109 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1111 {
"quake3_compat",
"Whether to respect known limitations in Quake 3 decoder",
OFFSET(quake3_compat),
AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1,
VE },
Macro definitions for various function/variable attributes.
static av_cold int init(AVCodecContext *avctx)
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
#define u(width, name, range_min, range_max)
static const unsigned codebook[256][2]
#define FFSWAP(type, a, b)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
int avpriv_init_elbg(int *points, int dim, int numpoints, int *codebook, int numCB, int max_steps, int *closest_cb, AVLFG *rand_state)
Initialize the **codebook vector for the elbg algorithm.
int avpriv_do_elbg(int *points, int dim, int numpoints, int *codebook, int numCB, int max_steps, int *closest_cb, AVLFG *rand_state)
Implementation of the Enhanced LBG Algorithm Based on the paper "Neural Networks 14:1219-1237" that c...
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
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.
const char * av_default_item_name(void *ptr)
Return the context name.
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
#define LIBAVUTIL_VERSION_INT
static const int offsets[]
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static enum AVPixelFormat pix_fmts[]
static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride)
AVPixelFormat
Pixel format.
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
void ff_apply_vector_4x4(RoqContext *ri, int x, int y, roq_cell *cell)
void ff_apply_motion_4x4(RoqContext *ri, int x, int y, int deltax, int deltay)
void ff_apply_vector_2x2(RoqContext *ri, int x, int y, roq_cell *cell)
void ff_apply_motion_8x8(RoqContext *ri, int x, int y, int deltax, int deltay)
#define RoQ_QUAD_CODEBOOK
static av_cold int roq_encode_init(AVCodecContext *avctx)
static void get_frame_mb(const AVFrame *frame, int x, int y, uint8_t mb[], int dim)
Get macroblocks from parts of the image.
static void gather_data_for_cel(CelEvaluation *cel, RoqEncContext *enc)
Get distortion for all options available to a cel.
static const AVOption options[]
static uint8_t motion_arg(motion_vect mot)
static void gather_data_for_subcel(SubcelEvaluation *subcel, int x, int y, RoqEncContext *enc)
Get distortion for all options available to a subcel.
static void frame_block_to_cell(int *block, uint8_t *const *data, int top, int left, const int *stride)
Create a single YUV cell from a 2x2 section of the image.
static void roq_write_video_info_chunk(RoqEncContext *enc)
static const AVClass roq_class
static void create_clusters(const AVFrame *frame, int w, int h, int *points)
Create YUV clusters for the entire image.
#define MAX_CBS_2x2
Maximum number of 2x2 codebooks.
static av_cold int roq_encode_end(AVCodecContext *avctx)
static int block_sse(uint8_t *const *buf1, uint8_t *const *buf2, int x1, int y1, int x2, int y2, const int *stride1, const int *stride2, int size)
static int create_cel_evals(RoqEncContext *enc)
Initialize cel evaluators and set their source coordinates.
static int eval_motion_dist(RoqEncContext *enc, int x, int y, motion_vect vect, int size)
static int index_mb(uint8_t cluster[], uint8_t cb[], int numCB, int *outIndex, int dim)
Find the codebook with the lowest distortion from an image.
static void motion_search(RoqEncContext *enc, int blocksize)
static void reconstruct_and_encode_image(RoqEncContext *enc, int w, int h, int numBlocks)
#define EVAL_MOTION(MOTION)
static int squared_diff_macroblock(uint8_t a[], uint8_t b[], int size)
static int eval_sse(const uint8_t *a, const uint8_t *b, int count)
static int generate_new_codebooks(RoqEncContext *enc)
static int roq_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
static void write_typecode(CodingSpool *s, uint8_t type)
static void unpack_roq_qcell(uint8_t cb2[], roq_qcell *qcell, uint8_t u[4 *4 *3])
static void remap_codebooks(RoqEncContext *enc)
#define MAX_CBS_4x4
Maximum number of generated 4x4 codebooks.
static void unpack_roq_cell(roq_cell *cell, uint8_t u[4 *3])
static void enlarge_roq_mb4(uint8_t base[3 *16], uint8_t u[3 *64])
static int roq_encode_video(RoqEncContext *enc)
static void write_codebooks(RoqEncContext *enc)
Write codebook chunk.
static int generate_codebook(RoqEncContext *enc, int *points, int inputCount, roq_cell *results, int size, int cbsize)
Describe the class of an AVClass context structure.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
main external API structure.
int width
picture width / height.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
const char * name
Name of the codec implementation.
This structure describes decoded (raw) audio or video data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Context structure for the Lagged Fibonacci PRNG.
This structure stores compressed data.
int flags
A combination of AV_PKT_FLAG values.
SubcelEvaluation subCels[4]
uint8_t argumentSpool[64]
uint8_t unpacked_cb4[MAX_CBS_4x4 *4 *4 *3]
uint8_t unpacked_cb2[MAX_CBS_2x2 *2 *2 *3]
uint8_t unpacked_cb4_enlarged[MAX_CBS_4x4 *8 *8 *3]
motion_vect * last_motion8
motion_vect * this_motion8
CelEvaluation * cel_evals
const AVFrame * frame_to_enc
motion_vect * this_motion4
roq_cell results4[4 *MAX_CBS_4x4]
unsigned int framesSinceKeyframe
int tmp_codebook_buf[FFMAX(24 *MAX_CBS_4x4, 6 *MAX_CBS_2x2)]
motion_vect * last_motion4
In the ELBG jargon, a cell is the set of points that are closest to a codebook entry.
#define av_malloc_array(a, b)
static double cb(void *priv, double x, double y)
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static const uint8_t offset[127][2]