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++) {
175 u[4*4*cp + offsets[
i] ] = cb2[qcell->
idx[
i]*2*2*3 + 4*cp ];
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];
203 diff +=
square(*b++ - *a++);
211 int x2,
int y2,
const int *stride1,
const int *stride2,
int size)
216 for (k=0; k<3; k++) {
218 for (i=0; i<
size; i++)
219 sse += bias*
eval_sse(buf1[k] + (y1+i)*stride1[k] + x1,
220 buf2[k] + (y2+
i)*stride2[k] + x2, size);
233 if (mx < -7 || mx > 7)
236 if (my < -7 || my > 7)
242 if ((
unsigned) mx > roq->
width-size || (
unsigned) my > roq->
height-size)
258 for(cp=0;cp<3;cp++) {
260 sdiff += bias*
eval_sse(a, b, size*size);
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++) {
299 for (i=0; i<
dim; i++)
300 for (j=0; j<
dim; j++)
301 *mb++ = frame->
data[cp][(y+i)*stride + x + 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++) {
414 vect2.
d[0] += offsets[k].
d[0];
415 vect2.
d[1] += offsets[k].
d[1];
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);
616 for (i=0; i<tempData->
numCB2; i++) {
618 bytestream_put_byte(outp, roq->
cb2x2[tempData->
f2i2[i]].
u);
619 bytestream_put_byte(outp, roq->
cb2x2[tempData->
f2i2[i]].
v);
622 for (i=0; i<tempData->
numCB4; i++)
624 bytestream_put_byte(outp, tempData->
i2f2[roq->
cb4x4[tempData->
f2i4[i]].
idx[j]]);
633 return ((ax&15)<<4) | (ay&15);
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);
787 for (j=0; j<2; j++) {
788 int x = (top+
i)*stride[0] + left + j;
789 *block++ = data[0][x];
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;
821 int c_size = size*size/4;
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)
875 for (i=0; i<codebooks->
numCB2; i++)
879 for (i=0; i<codebooks->
numCB4; i++) {
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",
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 },
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Context structure for the Lagged Fibonacci PRNG.
SubcelEvaluation subCels[4]
static const unsigned codebook[256][2]
unsigned int framesSinceKeyframe
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
#define EVAL_MOTION(MOTION)
#define LIBAVUTIL_VERSION_INT
static int generate_codebook(RoqEncContext *enc, int *points, int inputCount, roq_cell *results, int size, int cbsize)
static av_cold int init(AVCodecContext *avctx)
CelEvaluation * cel_evals
void ff_apply_vector_2x2(RoqContext *ri, int x, int y, roq_cell *cell)
const AVFrame * frame_to_enc
const char * av_default_item_name(void *ptr)
Return the context name.
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
static void reconstruct_and_encode_image(RoqEncContext *enc, int w, int h, int numBlocks)
static void create_clusters(const AVFrame *frame, int w, int h, int *points)
Create YUV clusters for the entire image.
static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride)
static void gather_data_for_cel(CelEvaluation *cel, RoqEncContext *enc)
Get distortion for all options available to a cel.
static const AVOption options[]
Macro definitions for various function/variable attributes.
#define MAX_CBS_4x4
Maximum number of generated 4x4 codebooks.
static void remap_codebooks(RoqEncContext *enc)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
static double cb(void *priv, double x, double y)
The exact code depends on how similar the blocks are and how related they are to the block
static const int offsets[]
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define RoQ_QUAD_CODEBOOK
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
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...
#define u(width, name, range_min, range_max)
static int generate_new_codebooks(RoqEncContext *enc)
motion_vect * last_motion4
uint8_t unpacked_cb2[MAX_CBS_2x2 *2 *2 *3]
void ff_apply_motion_4x4(RoqContext *ri, int x, int y, int deltax, int deltay)
void ff_apply_motion_8x8(RoqContext *ri, int x, int y, int deltax, int deltay)
#define MAX_CBS_2x2
Maximum number of 2x2 codebooks.
static int roq_encode_video(RoqEncContext *enc)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static void unpack_roq_cell(roq_cell *cell, uint8_t u[4 *3])
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static void roq_write_video_info_chunk(RoqEncContext *enc)
const char * name
Name of the codec implementation.
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.
int flags
A combination of AV_PKT_FLAG values.
motion_vect * this_motion8
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)
motion_vect * last_motion8
static void write_typecode(CodingSpool *s, uint8_t type)
static int squared_diff_macroblock(uint8_t a[], uint8_t b[], int size)
In the ELBG jargon, a cell is the set of points that are closest to a codebook entry.
int width
picture width / height.
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
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 uint8_t motion_arg(motion_vect mot)
static void motion_search(RoqEncContext *enc, int blocksize)
static av_cold int roq_encode_end(AVCodecContext *avctx)
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.
static int create_cel_evals(RoqEncContext *enc)
Initialize cel evaluators and set their source coordinates.
static void gather_data_for_subcel(SubcelEvaluation *subcel, int x, int y, RoqEncContext *enc)
Get distortion for all options available to a subcel.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2]...the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so...,+,-,+,-,+,+,-,+,-,+,...hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32-hcoeff[1]-hcoeff[2]-...a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2}an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||.........intra?||||:Block01:yes no||||:Block02:.................||||:Block03::y DC::ref index:||||:Block04::cb DC::motion x:||||.........:cr DC::motion y:||||.................|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------------------------------|||Y subbands||Cb subbands||Cr subbands||||------||------||------|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||------||------||------||||------||------||------|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||------||------||------||||------||------||------|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||------||------||------||||------||------||------|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------------------------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction------------|\Dequantization-------------------\||Reference frames|\IDWT|--------------|Motion\|||Frame 0||Frame 1||Compensation.OBMC v-------|--------------|--------------.\------> Frame n output Frame Frame<----------------------------------/|...|-------------------Range Coder:============Binary Range Coder:-------------------The implemented range coder is an adapted version based upon"Range encoding: an algorithm for removing redundancy from a digitised message."by G.N.N.Martin.The symbols encoded by the Snow range coder are bits(0|1).The associated probabilities are not fix but change depending on the symbol mix seen so far.bit seen|new state---------+-----------------------------------------------0|256-state_transition_table[256-old_state];1|state_transition_table[old_state];state_transition_table={0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:-------------------------FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1.the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
static int eval_sse(const uint8_t *a, const uint8_t *b, int count)
Describe the class of an AVClass context structure.
roq_cell results4[4 *MAX_CBS_4x4]
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
uint8_t argumentSpool[64]
static enum AVPixelFormat pix_fmts[]
static int roq_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
static void write_codebooks(RoqEncContext *enc)
Write codebook chunk.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static av_cold int roq_encode_init(AVCodecContext *avctx)
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
GLint GLenum GLboolean GLsizei stride
common internal api header.
int tmp_codebook_buf[FFMAX(24 *MAX_CBS_4x4, 6 *MAX_CBS_2x2)]
static void get_frame_mb(const AVFrame *frame, int x, int y, uint8_t mb[], int dim)
Get macroblocks from parts of the image.
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static void enlarge_roq_mb4(uint8_t base[3 *16], uint8_t u[3 *64])
#define xf(width, name, var, range_min, range_max, subs,...)
void ff_apply_vector_4x4(RoqContext *ri, int x, int y, roq_cell *cell)
static int eval_motion_dist(RoqEncContext *enc, int x, int y, motion_vect vect, int size)
uint8_t unpacked_cb4[MAX_CBS_4x4 *4 *4 *3]
#define av_malloc_array(a, b)
static void unpack_roq_qcell(uint8_t cb2[], roq_qcell *qcell, uint8_t u[4 *4 *3])
#define FFSWAP(type, a, b)
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
static const AVClass roq_class
motion_vect * this_motion4
AVPixelFormat
Pixel format.
This structure stores compressed data.
uint8_t unpacked_cb4_enlarged[MAX_CBS_4x4 *8 *8 *3]
void * av_mallocz_array(size_t nmemb, size_t size)