FFmpeg
Loading...
Searching...
No Matches
snow.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (C) 2006 Robert Edele <yartrebo@earthlink.net>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef AVCODEC_SNOW_H
23#define AVCODEC_SNOW_H
24
26
27#include "avcodec.h"
28#include "hpeldsp.h"
29#include "snow_dwt.h"
30
31#include "rangecoder.h"
32#include "mathops.h"
33
34#include "h264qpel.h"
35#include "videodsp.h"
36
37#define SNOW_MAX_PLANES 4
38
39#define MID_STATE 128
40
41#define MAX_PLANES 4
42#define QSHIFT 5
43#define QROOT (1<<QSHIFT)
44#define LOSSLESS_QLOG -128
45#define FRAC_BITS 4
46#define MAX_REF_FRAMES 8
47
48#define LOG2_OBMC_MAX 6
49#define OBMC_MAX (1<<(LOG2_OBMC_MAX))
50typedef struct BlockNode{
51 int16_t mx; ///< Motion vector component X, see mv_scale
52 int16_t my; ///< Motion vector component Y, see mv_scale
53 uint8_t ref; ///< Reference frame index
54 uint8_t color[3]; ///< Color for intra
55 uint8_t type; ///< Bitfield of BLOCK_*
56//#define TYPE_SPLIT 1
57#define BLOCK_INTRA 1 ///< Intra block, inter otherwise
58#define BLOCK_OPT 2 ///< Block needs no checks in this round of iterative motion estiation
59//#define TYPE_NOCOLOR 4
60 uint8_t level; //FIXME merge into type?
62
63static const BlockNode null_block= { //FIXME add border maybe
64 .color= {128,128,128},
65 .mx= 0,
66 .my= 0,
67 .ref= 0,
68 .type= 0,
69 .level= 0,
70};
71
72#define LOG2_MB_SIZE 4
73#define MB_SIZE (1<<LOG2_MB_SIZE)
74#define ENCODER_EXTRA_BITS 4
75#define HTAPS_MAX 8
76
77typedef struct x_and_coeff{
78 int16_t x;
79 uint16_t coeff;
81
82typedef struct SubBand{
83 int level;
84 int stride;
85 int width;
86 int height;
87 int qlog; ///< log(qscale)/log[2^(1/6)]
92 int stride_line; ///< Stride measured in lines, not pixels.
94 struct SubBand *parent;
95 uint8_t state[34][32];
96}SubBand;
97
98typedef struct Plane{
99 int width;
100 int height;
102
103 int htaps;
104 int8_t hcoeff[HTAPS_MAX/2];
107
111}Plane;
112
113typedef struct SnowContext{
114 AVClass *class;
119 union {
120 /// everything except size 2 are from H.264
123 };
125 AVFrame *input_picture; ///< new_picture with the internal linesizes
129// uint8_t q_context[16];
130 uint8_t header_state[32];
131 uint8_t block_state[128 + 32*128];
143 int16_t (*ref_mvs[MAX_REF_FRAMES])[2];
154 int qlog;
158 int qbias;
160#define QBIAS_SHIFT 3
169
170 uint8_t *scratchbuf;
171
173 unsigned avmv_size;
176
177/* Tables */
178extern const uint8_t * const ff_obmc_tab[4];
179extern const uint8_t ff_qexp[QROOT];
181
182/* common code */
183
190void ff_snow_pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, ptrdiff_t stride,
191 int sx, int sy, int b_w, int b_h, const BlockNode *block,
192 int plane_index, int w, int h);
193/* common inline functions */
194//XXX doublecheck all of them should stay inlined
195
196static inline void pred_mv(SnowContext *s, int *mx, int *my, int ref,
197 const BlockNode *left, const BlockNode *top, const BlockNode *tr){
198 if(s->ref_frames == 1){
199 *mx = mid_pred(left->mx, top->mx, tr->mx);
200 *my = mid_pred(left->my, top->my, tr->my);
201 }else{
202 const int *scale = ff_scale_mv_ref[ref];
203 *mx = mid_pred((left->mx * scale[left->ref] + 128) >>8,
204 (top ->mx * scale[top ->ref] + 128) >>8,
205 (tr ->mx * scale[tr ->ref] + 128) >>8);
206 *my = mid_pred((left->my * scale[left->ref] + 128) >>8,
207 (top ->my * scale[top ->ref] + 128) >>8,
208 (tr ->my * scale[tr ->ref] + 128) >>8);
209 }
210}
211
213 if((a->type&BLOCK_INTRA) && (b->type&BLOCK_INTRA)){
214 return !((a->color[0] - b->color[0]) | (a->color[1] - b->color[1]) | (a->color[2] - b->color[2]));
215 }else{
216 return !((a->mx - b->mx) | (a->my - b->my) | (a->ref - b->ref) | ((a->type ^ b->type)&BLOCK_INTRA));
217 }
218}
219
220//FIXME name cleanup (b_w, block_w, b_width stuff)
221//XXX should we really inline it?
222static av_always_inline void add_yblock(SnowContext *s, int sliced, slice_buffer *sb, IDWTELEM *dst, uint8_t *dst8, const uint8_t *obmc, int src_x, int src_y, int b_w, int b_h, int w, int h, int dst_stride, int src_stride, int obmc_stride, int b_x, int b_y, int add, int offset_dst, int plane_index){
223 const int b_width = s->b_width << s->block_max_depth;
224 const int b_height= s->b_height << s->block_max_depth;
225 const int b_stride= b_width;
226 BlockNode *lt= &s->block[b_x + b_y*b_stride];
227 BlockNode *rt= lt+1;
228 BlockNode *lb= lt+b_stride;
229 BlockNode *rb= lb+1;
230 uint8_t *block[4];
231 // When src_stride is large enough, it is possible to interleave the blocks.
232 // Otherwise the blocks are written sequentially in the tmp buffer.
233 int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride;
234 uint8_t *tmp = s->scratchbuf;
235 uint8_t *ptmp;
236 int x,y;
237
238 if(b_x<0){
239 lt= rt;
240 lb= rb;
241 }else if(b_x + 1 >= b_width){
242 rt= lt;
243 rb= lb;
244 }
245 if(b_y<0){
246 lt= lb;
247 rt= rb;
248 }else if(b_y + 1 >= b_height){
249 lb= lt;
250 rb= rt;
251 }
252
253 if(src_x<0){ //FIXME merge with prev & always round internal width up to *16
254 obmc -= src_x;
255 b_w += src_x;
256 if(!sliced && !offset_dst)
257 dst -= src_x;
258 src_x=0;
259 }
260 if(src_x + b_w > w){
261 b_w = w - src_x;
262 }
263 if(src_y<0){
264 obmc -= src_y*obmc_stride;
265 b_h += src_y;
266 if(!sliced && !offset_dst)
267 dst -= src_y*dst_stride;
268 src_y=0;
269 }
270 if(src_y + b_h> h){
271 b_h = h - src_y;
272 }
273
274 if(b_w<=0 || b_h<=0) return;
275
276 if(!sliced && offset_dst)
277 dst += src_x + src_y*dst_stride;
278 if (sliced || add)
279 dst8+= src_x + src_y*src_stride;
280// src += src_x + src_y*src_stride;
281
282 ptmp= tmp + 3*tmp_step;
283 block[0]= ptmp;
284 ptmp+=tmp_step;
285 ff_snow_pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
286
287 if(same_block(lt, rt)){
288 block[1]= block[0];
289 }else{
290 block[1]= ptmp;
291 ptmp+=tmp_step;
292 ff_snow_pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
293 }
294
295 if(same_block(lt, lb)){
296 block[2]= block[0];
297 }else if(same_block(rt, lb)){
298 block[2]= block[1];
299 }else{
300 block[2]= ptmp;
301 ptmp+=tmp_step;
302 ff_snow_pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
303 }
304
305 if(same_block(lt, rb) ){
306 block[3]= block[0];
307 }else if(same_block(rt, rb)){
308 block[3]= block[1];
309 }else if(same_block(lb, rb)){
310 block[3]= block[2];
311 }else{
312 block[3]= ptmp;
313 ff_snow_pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
314 }
315 if(sliced){
316 s->dwt.inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x, src_stride, sb->line + src_y, add, dst8);
317 }else{
318 for(y=0; y<b_h; y++){
319 //FIXME ugly misuse of obmc_stride
320 const uint8_t *obmc1= obmc + y*obmc_stride;
321 const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
322 const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
323 const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
324 for(x=0; x<b_w; x++){
325 int v= obmc1[x] * block[3][x + y*src_stride]
326 +obmc2[x] * block[2][x + y*src_stride]
327 +obmc3[x] * block[1][x + y*src_stride]
328 +obmc4[x] * block[0][x + y*src_stride];
329
330 v <<= 8 - LOG2_OBMC_MAX;
331 if(FRAC_BITS != 8){
332 v >>= 8 - FRAC_BITS;
333 }
334 if(add){
335 v += dst[x + y*dst_stride];
336 v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
337 if(v&(~255)) v= ~(v>>31);
338 dst8[x + y*src_stride] = v;
339 }else{
340 dst[x + y*dst_stride] -= v;
341 }
342 }
343 }
344 }
345}
346
347static av_always_inline void predict_slice(SnowContext *s, IDWTELEM *buf, int plane_index, int add, int mb_y){
348 Plane *p= &s->plane[plane_index];
349 const int mb_w= s->b_width << s->block_max_depth;
350 const int mb_h= s->b_height << s->block_max_depth;
351 int x, y, mb_x;
352 int block_size = MB_SIZE >> s->block_max_depth;
353 int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
354 int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
355 const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
356 const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
357 int ref_stride= s->current_picture->linesize[plane_index];
358 uint8_t *dst8= s->current_picture->data[plane_index];
359 int w= p->width;
360 int h= p->height;
361 av_assert2(s->chroma_h_shift == s->chroma_v_shift); // obmc params assume squares
362 if(s->keyframe || (s->avctx->debug&512)){
363 if(mb_y==mb_h)
364 return;
365
366 if(add){
367 for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
368 for(x=0; x<w; x++){
369 int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
370 v >>= FRAC_BITS;
371 if(v&(~255)) v= ~(v>>31);
372 dst8[x + y*ref_stride]= v;
373 }
374 }
375 }else{
376 for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
377 for(x=0; x<w; x++){
378 buf[x + y*w]-= 128<<FRAC_BITS;
379 }
380 }
381 }
382
383 return;
384 }
385
386 for(mb_x=0; mb_x<=mb_w; mb_x++){
387 add_yblock(s, 0, NULL, buf, dst8, obmc,
388 block_w*mb_x - block_w/2,
389 block_h*mb_y - block_h/2,
390 block_w, block_h,
391 w, h,
392 w, ref_stride, obmc_stride,
393 mb_x - 1, mb_y - 1,
394 add, 1, plane_index);
395 }
396}
397
398static av_always_inline void predict_plane(SnowContext *s, IDWTELEM *buf, int plane_index, int add){
399 const int mb_h= s->b_height << s->block_max_depth;
400 int mb_y;
401 for(mb_y=0; mb_y<=mb_h; mb_y++)
402 predict_slice(s, buf, plane_index, add, mb_y);
403}
404
405static inline void set_blocks(SnowContext *s, int level, int x, int y, int l, int cb, int cr, int mx, int my, int ref, int type){
406 const int w= s->b_width << s->block_max_depth;
407 const int rem_depth= s->block_max_depth - level;
408 const int index= (x + y*w) << rem_depth;
409 const int block_w= 1<<rem_depth;
410 const int block_h= 1<<rem_depth; //FIXME "w!=h"
412 int i,j;
413
414 block.color[0]= l;
415 block.color[1]= cb;
416 block.color[2]= cr;
417 block.mx= mx;
418 block.my= my;
419 block.ref= ref;
420 block.type= type;
421 block.level= level;
422
423 for(j=0; j<block_h; j++){
424 for(i=0; i<block_w; i++){
425 s->block[index + i + j*w]= block;
426 }
427 }
428}
429
430extern const int8_t ff_quant3bA[256];
431
432#define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0
433
434#endif /* AVCODEC_SNOW_H */
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t my
Definition dsp.h:57
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t mx
Definition dsp.h:57
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
Libavcodec external API header.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define MB_SIZE
Definition cinepakenc.c:54
#define NULL
Definition coverity.c:32
static int16_t block[64]
Definition dct.c:125
int DWTELEM
Definition dirac_dwt.h:27
#define MAX_DECOMPOSITIONS
Definition dirac_dwt.h:31
short IDWTELEM
Definition dirac_dwt.h:28
#define FRAC_BITS
int index
Definition gxfenc.c:90
int a
Half-pel DSP functions.
cl_device_type type
#define b
Definition input.c:43
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
#define av_always_inline
Definition attributes.h:72
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
#define mid_pred
Definition mathops.h:115
void(* qpel_mc_func)(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
Definition qpeldsp.h:65
Range coder.
#define QROOT
Definition snow.h:43
static av_always_inline void predict_plane(SnowContext *s, IDWTELEM *buf, int plane_index, int add)
Definition snow.h:398
static void pred_mv(SnowContext *s, int *mx, int *my, int ref, const BlockNode *left, const BlockNode *top, const BlockNode *tr)
Definition snow.h:196
void ff_snow_pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, ptrdiff_t stride, int sx, int sy, int b_w, int b_h, const BlockNode *block, int plane_index, int w, int h)
Definition snow.c:379
static av_always_inline void predict_slice(SnowContext *s, IDWTELEM *buf, int plane_index, int add, int mb_y)
Definition snow.h:347
void ff_snow_common_end(SnowContext *s)
Definition snow.c:634
#define MAX_PLANES
Definition snow.h:41
const uint8_t ff_qexp[QROOT]
Definition snowdata.h:129
static void set_blocks(SnowContext *s, int level, int x, int y, int l, int cb, int cr, int mx, int my, int ref, int type)
Definition snow.h:405
const uint8_t *const ff_obmc_tab[4]
Definition snowdata.h:124
int ff_snow_common_init(AVCodecContext *avctx)
Definition snow.c:487
#define HTAPS_MAX
Definition snow.h:75
void ff_snow_reset_contexts(SnowContext *s)
Definition snow.c:157
#define BLOCK_INTRA
Intra block, inter otherwise.
Definition snow.h:57
int ff_snow_common_init_after_header(AVCodecContext *avctx)
Definition snow.c:542
#define MAX_REF_FRAMES
Definition snow.h:46
static av_always_inline int same_block(BlockNode *a, BlockNode *b)
Definition snow.h:212
#define LOG2_OBMC_MAX
Definition snow.h:48
int ff_scale_mv_ref[MAX_REF_FRAMES][MAX_REF_FRAMES]
Definition snowdata.h:136
const int8_t ff_quant3bA[256]
Definition snowdata.h:105
static av_always_inline void add_yblock(SnowContext *s, int sliced, slice_buffer *sb, IDWTELEM *dst, uint8_t *dst8, const uint8_t *obmc, int src_x, int src_y, int b_w, int b_h, int w, int h, int dst_stride, int src_stride, int obmc_stride, int b_x, int b_y, int add, int offset_dst, int plane_index)
Definition snow.h:222
int ff_snow_frames_prepare(SnowContext *s)
Definition snow.c:603
int ff_snow_alloc_blocks(SnowContext *s)
Definition snow.c:171
static const BlockNode null_block
Definition snow.h:63
static const uint8_t obmc4[16]
Definition snowdata.h:97
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t ref
Reference frame index.
Definition snow.h:53
int16_t mx
Motion vector component X, see mv_scale.
Definition snow.h:51
uint8_t color[3]
Color for intra.
Definition snow.h:54
int16_t my
Motion vector component Y, see mv_scale.
Definition snow.h:52
uint8_t type
Bitfield of BLOCK_*.
Definition snow.h:55
uint8_t level
Definition snow.h:60
Half-pel DSP context.
Definition hpeldsp.h:46
Definition cfhd.h:125
int last_htaps
Definition snow.h:108
SubBand band[DWT_LEVELS_3D][4]
Definition cfhd.h:138
int htaps
Definition snow.h:103
int last_diag_mc
Definition snow.h:110
int8_t last_hcoeff[HTAPS_MAX/2]
Definition snow.h:109
int fast_mc
Definition snow.h:106
int8_t hcoeff[HTAPS_MAX/2]
Definition snow.h:104
int diag_mc
Definition snow.h:105
unsigned avmv_size
Definition snow.h:173
AVCodecContext * avctx
Definition snow.h:115
int b_height
Definition snow.h:162
int qlog
Definition snow.h:154
int16_t(*[MAX_REF_FRAMES] ref_mvs)[2]
Definition snow.h:143
int last_block_max_depth
Definition snow.h:164
int max_ref_frames
Definition snow.h:141
BlockNode * block
Definition snow.h:167
Plane plane[MAX_PLANES]
Definition snow.h:166
DWTELEM * spatial_dwt_buffer
Definition snow.h:145
RangeCoder c
Definition snow.h:116
uint8_t header_state[32]
Definition snow.h:130
int always_reset
Definition snow.h:133
uint32_t * ref_scores[MAX_REF_FRAMES]
Definition snow.h:144
int last_spatial_decomposition_count
Definition snow.h:139
int b_width
Definition snow.h:161
int version
Definition snow.h:134
AVMotionVector * avmv
Definition snow.h:172
SnowDWTContext dwt
Definition snow.h:124
qpel_mc_func put_snow_qpel_pixels_tab[4][16]
everything except size 2 are from H.264
Definition snow.h:121
DWTELEM * temp_dwt_buffer
Definition snow.h:146
int last_mv_scale
Definition snow.h:157
HpelDSPContext hdsp
Definition snow.h:117
uint8_t * scratchbuf
Definition snow.h:170
AVFrame * last_picture[MAX_REF_FRAMES]
Definition snow.h:127
int keyframe
Definition snow.h:132
int ref_frames
Definition snow.h:142
int chroma_h_shift
Definition snow.h:151
slice_buffer sb
Definition snow.h:168
AVFrame * current_picture
Definition snow.h:126
int spatial_decomposition_type
Definition snow.h:135
AVFrame * input_picture
new_picture with the internal linesizes
Definition snow.h:125
H264QpelContext h264qpel
Definition snow.h:122
int avmv_index
Definition snow.h:174
IDWTELEM * spatial_idwt_buffer
Definition snow.h:147
uint8_t block_state[128+32 *128]
Definition snow.h:131
int last_qbias
Definition snow.h:159
IDWTELEM * temp_idwt_buffer
Definition snow.h:148
int mv_scale
Definition snow.h:156
int colorspace_type
Definition snow.h:150
int temporal_decomposition_type
Definition snow.h:137
int spatial_scalability
Definition snow.h:153
int chroma_v_shift
Definition snow.h:152
int qbias
Definition snow.h:158
int temporal_decomposition_count
Definition snow.h:140
int block_max_depth
Definition snow.h:163
int nb_planes
Definition snow.h:165
VideoDSPContext vdsp
Definition snow.h:118
AVFrame * mconly_picture
Definition snow.h:128
int * run_buffer
Definition snow.h:149
int last_qlog
Definition snow.h:155
int spatial_decomposition_count
Definition snow.h:138
int last_spatial_decomposition_type
Definition snow.h:136
DWTELEM * buf
Definition snow.h:88
x_and_coeff * x_coeff
Definition snow.h:93
int buf_y_offset
Definition snow.h:91
struct SubBand * parent
Definition diracdec.c:102
int buf_x_offset
Definition snow.h:90
uint8_t state[34][32]
Definition snow.h:95
uint8_t * ibuf
Definition diracdec.c:101
int qlog
log(qscale)/log[2^(1/6)]
Definition snow.h:87
int stride_line
Stride measured in lines, not pixels.
Definition snow.h:92
Used to minimize the amount of memory used in order to optimize cache performance.
Definition snow_dwt.h:46
IDWTELEM ** line
For use by idwt and predict_slices.
Definition snow_dwt.h:47
uint16_t coeff
Definition snow.h:79
int16_t x
Definition snow.h:78
uint8_t level
Definition svq3.c:208
#define stride
static uint8_t tmp[40]
Definition aes_ctr.c:52
static int ref[MAX_W *MAX_W]
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
static double cr(void *priv, double x, double y)
Definition vf_geq.c:248
static double cb(void *priv, double x, double y)
Definition vf_geq.c:247
Core video DSP helper functions.