📄 roqvideoenc.c
字号:
/* * RoQ Video Encoder. * * Copyright (C) 2007 Vitor Sessak <vitor1001@gmail.com> * Copyright (C) 2004-2007 Eric Lasota * Based on RoQ specs (C) 2001 Tim Ferguson * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *//** * @file roqvideoenc.c * Id RoQ encoder by Vitor. Based on the Switchblade3 library and the * Switchblade3 FFmpeg glue by Eric Lasota. *//* * COSTS: * Level 1: * SKIP - 2 bits * MOTION - 2 + 8 bits * CODEBOOK - 2 + 8 bits * SUBDIVIDE - 2 + combined subcel cost * * Level 2: * SKIP - 2 bits * MOTION - 2 + 8 bits * CODEBOOK - 2 + 8 bits * SUBDIVIDE - 2 + 4*8 bits * * Maximum cost: 138 bits per cel * * Proper evaluation requires LCD fraction comparison, which requires * Squared Error (SE) loss * savings increase * * Maximum savings increase: 136 bits * Maximum SE loss without overflow: 31580641 * Components in 8x8 supercel: 192 * Maximum SE precision per component: 164482 * >65025, so no truncation is needed (phew) */#include <string.h>#include <unistd.h>#include "roqvideo.h"#include "bytestream.h"#include "elbg.h"#define CHROMA_BIAS 1/** * Maximum number of generated 4x4 codebooks. Can't be 256 to workaround a * Quake 3 bug. */#define MAX_CBS_4x4 255#define MAX_CBS_2x2 256 ///< Maximum number of 2x2 codebooks./* The cast is useful when multiplying it by INT_MAX */#define ROQ_LAMBDA_SCALE ((uint64_t) FF_LAMBDA_SCALE)/* Macroblock support functions */static void unpack_roq_cell(roq_cell *cell, uint8_t u[4*3]){ memcpy(u , cell->y, 4); memset(u+4, cell->u, 4); memset(u+8, cell->v, 4);}static void unpack_roq_qcell(uint8_t cb2[], roq_qcell *qcell, uint8_t u[4*4*3]){ int i,cp; static const int offsets[4] = {0, 2, 8, 10}; for (cp=0; cp<3; cp++) for (i=0; i<4; i++) { u[4*4*cp + offsets[i] ] = cb2[qcell->idx[i]*2*2*3 + 4*cp ]; u[4*4*cp + offsets[i]+1] = cb2[qcell->idx[i]*2*2*3 + 4*cp+1]; u[4*4*cp + offsets[i]+4] = cb2[qcell->idx[i]*2*2*3 + 4*cp+2]; u[4*4*cp + offsets[i]+5] = cb2[qcell->idx[i]*2*2*3 + 4*cp+3]; }}static void enlarge_roq_mb4(uint8_t base[3*16], uint8_t u[3*64]){ int x,y,cp; for(cp=0; cp<3; cp++) for(y=0; y<8; y++) for(x=0; x<8; x++) *u++ = base[(y/2)*4 + (x/2) + 16*cp];}static inline int square(int x){ return x*x;}static inline int eval_sse(uint8_t *a, uint8_t *b, int count){ int diff=0; while(count--) diff += square(*b++ - *a++); return diff;}// FIXME Could use DSPContext.sse, but it is not so speed critical (used// just for motion estimation).static int block_sse(uint8_t **buf1, uint8_t **buf2, int x1, int y1, int x2, int y2, int *stride1, int *stride2, int size){ int i, k; int sse=0; for (k=0; k<3; k++) { int bias = (k ? CHROMA_BIAS : 4); for (i=0; i<size; i++) sse += bias*eval_sse(buf1[k] + (y1+i)*stride1[k] + x1, buf2[k] + (y2+i)*stride2[k] + x2, size); } return sse;}static int eval_motion_dist(RoqContext *enc, int x, int y, motion_vect vect, int size){ int mx=vect.d[0]; int my=vect.d[1]; if (mx < -7 || mx > 7) return INT_MAX; if (my < -7 || my > 7) return INT_MAX; mx += x; my += y; if ((unsigned) mx > enc->width-size || (unsigned) my > enc->height-size) return INT_MAX; return block_sse(enc->frame_to_enc->data, enc->last_frame->data, x, y, mx, my, enc->frame_to_enc->linesize, enc->last_frame->linesize, size);}/** * Returns distortion between two macroblocks */static inline int squared_diff_macroblock(uint8_t a[], uint8_t b[], int size){ int cp, sdiff=0; for(cp=0;cp<3;cp++) { int bias = (cp ? CHROMA_BIAS : 4); sdiff += bias*eval_sse(a, b, size*size); a += size*size; b += size*size; } return sdiff;}typedef struct{ int eval_dist[4]; int best_bit_use; int best_coding; int subCels[4]; motion_vect motion; int cbEntry;} subcel_evaluation_t;typedef struct{ int eval_dist[4]; int best_coding; subcel_evaluation_t subCels[4]; motion_vect motion; int cbEntry; int sourceX, sourceY;} cel_evaluation_t;typedef struct{ int numCB4; int numCB2; int usedCB2[MAX_CBS_2x2]; int usedCB4[MAX_CBS_4x4]; uint8_t unpacked_cb2[MAX_CBS_2x2*2*2*3]; uint8_t unpacked_cb4[MAX_CBS_4x4*4*4*3]; uint8_t unpacked_cb4_enlarged[MAX_CBS_4x4*8*8*3];} roq_codebooks_t;/** * Temporary vars */typedef struct{ cel_evaluation_t *cel_evals; int f2i4[MAX_CBS_4x4]; int i2f4[MAX_CBS_4x4]; int f2i2[MAX_CBS_2x2]; int i2f2[MAX_CBS_2x2]; int mainChunkSize; int numCB4; int numCB2; roq_codebooks_t codebooks; int *closest_cb2; int used_option[4];} roq_tempdata_t;/** * Initializes cel evaluators and sets their source coordinates */static void create_cel_evals(RoqContext *enc, roq_tempdata_t *tempData){ int n=0, x, y, i; tempData->cel_evals = av_malloc(enc->width*enc->height/64 * sizeof(cel_evaluation_t)); /* Map to the ROQ quadtree order */ for (y=0; y<enc->height; y+=16) for (x=0; x<enc->width; x+=16) for(i=0; i<4; i++) { tempData->cel_evals[n ].sourceX = x + (i&1)*8; tempData->cel_evals[n++].sourceY = y + (i&2)*4; }}/** * Get macroblocks from parts of the image */static void get_frame_mb(AVFrame *frame, int x, int y, uint8_t mb[], int dim){ int i, j, cp; for (cp=0; cp<3; cp++) { int stride = frame->linesize[cp]; for (i=0; i<dim; i++) for (j=0; j<dim; j++) *mb++ = frame->data[cp][(y+i)*stride + x + j]; }}/** * Find the codebook with the lowest distortion from an image */static int index_mb(uint8_t cluster[], uint8_t cb[], int numCB, int *outIndex, int dim){ int i, lDiff = INT_MAX, pick=0; /* Diff against the others */ for (i=0; i<numCB; i++) { int diff = squared_diff_macroblock(cluster, cb + i*dim*dim*3, dim); if (diff < lDiff) { lDiff = diff; pick = i; } } *outIndex = pick; return lDiff;}#define EVAL_MOTION(MOTION) \ do { \ diff = eval_motion_dist(enc, j, i, MOTION, blocksize); \ \ if (diff < lowestdiff) { \ lowestdiff = diff; \ bestpick = MOTION; \ } \ } while(0)static void motion_search(RoqContext *enc, int blocksize){ static const motion_vect offsets[8] = { {{ 0,-1}}, {{ 0, 1}}, {{-1, 0}}, {{ 1, 0}}, {{-1, 1}}, {{ 1,-1}}, {{-1,-1}}, {{ 1, 1}}, }; int diff, lowestdiff, oldbest; int off[3]; motion_vect bestpick = {{0,0}}; int i, j, k, offset; motion_vect *last_motion; motion_vect *this_motion; motion_vect vect, vect2; int max=(enc->width/blocksize)*enc->height/blocksize; if (blocksize == 4) { last_motion = enc->last_motion4; this_motion = enc->this_motion4; } else { last_motion = enc->last_motion8; this_motion = enc->this_motion8; } for (i=0; i<enc->height; i+=blocksize) for (j=0; j<enc->width; j+=blocksize) { lowestdiff = eval_motion_dist(enc, j, i, (motion_vect) {{0,0}}, blocksize); bestpick.d[0] = 0; bestpick.d[1] = 0; if (blocksize == 4) EVAL_MOTION(enc->this_motion8[(i/8)*(enc->width/8) + j/8]); offset = (i/blocksize)*enc->width/blocksize + j/blocksize; if (offset < max && offset >= 0) EVAL_MOTION(last_motion[offset]); offset++; if (offset < max && offset >= 0) EVAL_MOTION(last_motion[offset]); offset = (i/blocksize + 1)*enc->width/blocksize + j/blocksize; if (offset < max && offset >= 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -