h263.c
Go to the documentation of this file.
1 /*
2  * H263/MPEG4 backend for encoder and decoder
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * H263+ support.
5  * Copyright (c) 2001 Juan J. Sierralta P
6  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 /**
26  * @file
27  * h263/mpeg4 codec.
28  */
29 
30 //#define DEBUG
31 #include <limits.h>
32 
33 #include "avcodec.h"
34 #include "mpegvideo.h"
35 #include "h263.h"
36 #include "h263data.h"
37 #include "mathops.h"
38 #include "unary.h"
39 #include "flv.h"
40 #include "mpeg4video.h"
41 
42 //#undef NDEBUG
43 //#include <assert.h>
44 
46 
47 
49  const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
50  //FIXME a lot of that is only needed for !low_delay
51  const int wrap = s->b8_stride;
52  const int xy = s->block_index[0];
53 
55 
56  if(s->mv_type != MV_TYPE_8X8){
57  int motion_x, motion_y;
58  if (s->mb_intra) {
59  motion_x = 0;
60  motion_y = 0;
61  } else if (s->mv_type == MV_TYPE_16X16) {
62  motion_x = s->mv[0][0][0];
63  motion_y = s->mv[0][0][1];
64  } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
65  int i;
66  motion_x = s->mv[0][0][0] + s->mv[0][1][0];
67  motion_y = s->mv[0][0][1] + s->mv[0][1][1];
68  motion_x = (motion_x>>1) | (motion_x&1);
69  for(i=0; i<2; i++){
70  s->p_field_mv_table[i][0][mb_xy][0]= s->mv[0][i][0];
71  s->p_field_mv_table[i][0][mb_xy][1]= s->mv[0][i][1];
72  }
73  s->current_picture.ref_index[0][4*mb_xy ] =
74  s->current_picture.ref_index[0][4*mb_xy + 1] = s->field_select[0][0];
75  s->current_picture.ref_index[0][4*mb_xy + 2] =
76  s->current_picture.ref_index[0][4*mb_xy + 3] = s->field_select[0][1];
77  }
78 
79  /* no update if 8X8 because it has been done during parsing */
80  s->current_picture.motion_val[0][xy][0] = motion_x;
81  s->current_picture.motion_val[0][xy][1] = motion_y;
82  s->current_picture.motion_val[0][xy + 1][0] = motion_x;
83  s->current_picture.motion_val[0][xy + 1][1] = motion_y;
84  s->current_picture.motion_val[0][xy + wrap][0] = motion_x;
85  s->current_picture.motion_val[0][xy + wrap][1] = motion_y;
86  s->current_picture.motion_val[0][xy + 1 + wrap][0] = motion_x;
87  s->current_picture.motion_val[0][xy + 1 + wrap][1] = motion_y;
88  }
89 
90  if(s->encoding){ //FIXME encoding MUST be cleaned up
91  if (s->mv_type == MV_TYPE_8X8)
93  else if(s->mb_intra)
95  else
97  }
98 }
99 
100 int ff_h263_pred_dc(MpegEncContext * s, int n, int16_t **dc_val_ptr)
101 {
102  int x, y, wrap, a, c, pred_dc;
103  int16_t *dc_val;
104 
105  /* find prediction */
106  if (n < 4) {
107  x = 2 * s->mb_x + (n & 1);
108  y = 2 * s->mb_y + ((n & 2) >> 1);
109  wrap = s->b8_stride;
110  dc_val = s->dc_val[0];
111  } else {
112  x = s->mb_x;
113  y = s->mb_y;
114  wrap = s->mb_stride;
115  dc_val = s->dc_val[n - 4 + 1];
116  }
117  /* B C
118  * A X
119  */
120  a = dc_val[(x - 1) + (y) * wrap];
121  c = dc_val[(x) + (y - 1) * wrap];
122 
123  /* No prediction outside GOB boundary */
124  if(s->first_slice_line && n!=3){
125  if(n!=2) c= 1024;
126  if(n!=1 && s->mb_x == s->resync_mb_x) a= 1024;
127  }
128  /* just DC prediction */
129  if (a != 1024 && c != 1024)
130  pred_dc = (a + c) >> 1;
131  else if (a != 1024)
132  pred_dc = a;
133  else
134  pred_dc = c;
135 
136  /* we assume pred is positive */
137  *dc_val_ptr = &dc_val[x + y * wrap];
138  return pred_dc;
139 }
140 
142  int qp_c;
143  const int linesize = s->linesize;
144  const int uvlinesize= s->uvlinesize;
145  const int xy = s->mb_y * s->mb_stride + s->mb_x;
146  uint8_t *dest_y = s->dest[0];
147  uint8_t *dest_cb= s->dest[1];
148  uint8_t *dest_cr= s->dest[2];
149 
150 // if(s->pict_type==AV_PICTURE_TYPE_B && !s->readable) return;
151 
152  /*
153  Diag Top
154  Left Center
155  */
156  if (!IS_SKIP(s->current_picture.mb_type[xy])) {
157  qp_c= s->qscale;
158  s->dsp.h263_v_loop_filter(dest_y+8*linesize , linesize, qp_c);
159  s->dsp.h263_v_loop_filter(dest_y+8*linesize+8, linesize, qp_c);
160  }else
161  qp_c= 0;
162 
163  if(s->mb_y){
164  int qp_dt, qp_tt, qp_tc;
165 
166  if (IS_SKIP(s->current_picture.mb_type[xy - s->mb_stride]))
167  qp_tt=0;
168  else
169  qp_tt = s->current_picture.qscale_table[xy - s->mb_stride];
170 
171  if(qp_c)
172  qp_tc= qp_c;
173  else
174  qp_tc= qp_tt;
175 
176  if(qp_tc){
177  const int chroma_qp= s->chroma_qscale_table[qp_tc];
178  s->dsp.h263_v_loop_filter(dest_y , linesize, qp_tc);
179  s->dsp.h263_v_loop_filter(dest_y+8, linesize, qp_tc);
180 
181  s->dsp.h263_v_loop_filter(dest_cb , uvlinesize, chroma_qp);
182  s->dsp.h263_v_loop_filter(dest_cr , uvlinesize, chroma_qp);
183  }
184 
185  if(qp_tt)
186  s->dsp.h263_h_loop_filter(dest_y-8*linesize+8 , linesize, qp_tt);
187 
188  if(s->mb_x){
189  if (qp_tt || IS_SKIP(s->current_picture.mb_type[xy - 1 - s->mb_stride]))
190  qp_dt= qp_tt;
191  else
192  qp_dt = s->current_picture.qscale_table[xy - 1 - s->mb_stride];
193 
194  if(qp_dt){
195  const int chroma_qp= s->chroma_qscale_table[qp_dt];
196  s->dsp.h263_h_loop_filter(dest_y -8*linesize , linesize, qp_dt);
197  s->dsp.h263_h_loop_filter(dest_cb-8*uvlinesize, uvlinesize, chroma_qp);
198  s->dsp.h263_h_loop_filter(dest_cr-8*uvlinesize, uvlinesize, chroma_qp);
199  }
200  }
201  }
202 
203  if(qp_c){
204  s->dsp.h263_h_loop_filter(dest_y +8, linesize, qp_c);
205  if(s->mb_y + 1 == s->mb_height)
206  s->dsp.h263_h_loop_filter(dest_y+8*linesize+8, linesize, qp_c);
207  }
208 
209  if(s->mb_x){
210  int qp_lc;
211  if (qp_c || IS_SKIP(s->current_picture.mb_type[xy - 1]))
212  qp_lc= qp_c;
213  else
214  qp_lc = s->current_picture.qscale_table[xy - 1];
215 
216  if(qp_lc){
217  s->dsp.h263_h_loop_filter(dest_y, linesize, qp_lc);
218  if(s->mb_y + 1 == s->mb_height){
219  const int chroma_qp= s->chroma_qscale_table[qp_lc];
220  s->dsp.h263_h_loop_filter(dest_y +8* linesize, linesize, qp_lc);
221  s->dsp.h263_h_loop_filter(dest_cb , uvlinesize, chroma_qp);
222  s->dsp.h263_h_loop_filter(dest_cr , uvlinesize, chroma_qp);
223  }
224  }
225  }
226 }
227 
228 void ff_h263_pred_acdc(MpegEncContext * s, int16_t *block, int n)
229 {
230  int x, y, wrap, a, c, pred_dc, scale, i;
231  int16_t *dc_val, *ac_val, *ac_val1;
232 
233  /* find prediction */
234  if (n < 4) {
235  x = 2 * s->mb_x + (n & 1);
236  y = 2 * s->mb_y + (n>> 1);
237  wrap = s->b8_stride;
238  dc_val = s->dc_val[0];
239  ac_val = s->ac_val[0][0];
240  scale = s->y_dc_scale;
241  } else {
242  x = s->mb_x;
243  y = s->mb_y;
244  wrap = s->mb_stride;
245  dc_val = s->dc_val[n - 4 + 1];
246  ac_val = s->ac_val[n - 4 + 1][0];
247  scale = s->c_dc_scale;
248  }
249 
250  ac_val += ((y) * wrap + (x)) * 16;
251  ac_val1 = ac_val;
252 
253  /* B C
254  * A X
255  */
256  a = dc_val[(x - 1) + (y) * wrap];
257  c = dc_val[(x) + (y - 1) * wrap];
258 
259  /* No prediction outside GOB boundary */
260  if(s->first_slice_line && n!=3){
261  if(n!=2) c= 1024;
262  if(n!=1 && s->mb_x == s->resync_mb_x) a= 1024;
263  }
264 
265  if (s->ac_pred) {
266  pred_dc = 1024;
267  if (s->h263_aic_dir) {
268  /* left prediction */
269  if (a != 1024) {
270  ac_val -= 16;
271  for(i=1;i<8;i++) {
272  block[s->dsp.idct_permutation[i<<3]] += ac_val[i];
273  }
274  pred_dc = a;
275  }
276  } else {
277  /* top prediction */
278  if (c != 1024) {
279  ac_val -= 16 * wrap;
280  for(i=1;i<8;i++) {
281  block[s->dsp.idct_permutation[i ]] += ac_val[i + 8];
282  }
283  pred_dc = c;
284  }
285  }
286  } else {
287  /* just DC prediction */
288  if (a != 1024 && c != 1024)
289  pred_dc = (a + c) >> 1;
290  else if (a != 1024)
291  pred_dc = a;
292  else
293  pred_dc = c;
294  }
295 
296  /* we assume pred is positive */
297  block[0]=block[0]*scale + pred_dc;
298 
299  if (block[0] < 0)
300  block[0] = 0;
301  else
302  block[0] |= 1;
303 
304  /* Update AC/DC tables */
305  dc_val[(x) + (y) * wrap] = block[0];
306 
307  /* left copy */
308  for(i=1;i<8;i++)
309  ac_val1[i ] = block[s->dsp.idct_permutation[i<<3]];
310  /* top copy */
311  for(i=1;i<8;i++)
312  ac_val1[8 + i] = block[s->dsp.idct_permutation[i ]];
313 }
314 
315 int16_t *ff_h263_pred_motion(MpegEncContext * s, int block, int dir,
316  int *px, int *py)
317 {
318  int wrap;
319  int16_t *A, *B, *C, (*mot_val)[2];
320  static const int off[4]= {2, 1, 1, -1};
321 
322  wrap = s->b8_stride;
323  mot_val = s->current_picture.motion_val[dir] + s->block_index[block];
324 
325  A = mot_val[ - 1];
326  /* special case for first (slice) line */
327  if (s->first_slice_line && block<3) {
328  // we can't just change some MVs to simulate that as we need them for the B frames (and ME)
329  // and if we ever support non rectangular objects than we need to do a few ifs here anyway :(
330  if(block==0){ //most common case
331  if(s->mb_x == s->resync_mb_x){ //rare
332  *px= *py = 0;
333  }else if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
334  C = mot_val[off[block] - wrap];
335  if(s->mb_x==0){
336  *px = C[0];
337  *py = C[1];
338  }else{
339  *px = mid_pred(A[0], 0, C[0]);
340  *py = mid_pred(A[1], 0, C[1]);
341  }
342  }else{
343  *px = A[0];
344  *py = A[1];
345  }
346  }else if(block==1){
347  if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
348  C = mot_val[off[block] - wrap];
349  *px = mid_pred(A[0], 0, C[0]);
350  *py = mid_pred(A[1], 0, C[1]);
351  }else{
352  *px = A[0];
353  *py = A[1];
354  }
355  }else{ /* block==2*/
356  B = mot_val[ - wrap];
357  C = mot_val[off[block] - wrap];
358  if(s->mb_x == s->resync_mb_x) //rare
359  A[0]=A[1]=0;
360 
361  *px = mid_pred(A[0], B[0], C[0]);
362  *py = mid_pred(A[1], B[1], C[1]);
363  }
364  } else {
365  B = mot_val[ - wrap];
366  C = mot_val[off[block] - wrap];
367  *px = mid_pred(A[0], B[0], C[0]);
368  *py = mid_pred(A[1], B[1], C[1]);
369  }
370  return *mot_val;
371 }
372 
373 
374 /**
375  * Get the GOB height based on picture height.
376  */
378  if (s->height <= 400)
379  return 1;
380  else if (s->height <= 800)
381  return 2;
382  else
383  return 4;
384 }
int8_t * ref_index[2]
Definition: mpegvideo.h:114
const char * s
Definition: avisynth_c.h:668
#define IS_SKIP(a)
Definition: mpegvideo.h:140
#define C
#define B
Definition: dsputil.c:2025
int16_t(*[3] ac_val)[16]
used for for mpeg4 AC prediction, all 3 arrays must be continuous
Definition: mpegvideo.h:357
int16_t(*[2][2] p_field_mv_table)[2]
MV table (2MV per MB) interlaced p-frame encoding.
Definition: mpegvideo.h:411
#define wrap(func)
Definition: w64xmmtest.h:70
mpegvideo header.
int qscale
QP.
Definition: mpegvideo.h:369
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
Definition: h263.c:315
int encoding
true if we are encoding (vs decoding)
Definition: mpegvideo.h:259
int field_select[2][2]
Definition: mpegvideo.h:432
#define MB_TYPE_INTRA
Definition: mpegvideo.h:134
uint8_t
H.263 tables.
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:343
uint8_t idct_permutation[64]
idct input permutation.
Definition: dsputil.h:249
int ff_h263_pred_dc(MpegEncContext *s, int n, int16_t **dc_val_ptr)
Definition: h263.c:100
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:277
#define A(x)
#define MAX_LEVEL
Definition: rl.h:35
Discrete Time axis x
int16_t * dc_val[3]
used for mpeg4 DC prediction, all 3 arrays must be continuous
Definition: mpegvideo.h:350
uint8_t * mbskip_table
Definition: mpegvideo.h:111
int mb_skipped
MUST BE SET only during DECODING.
Definition: mpegvideo.h:358
void(* h263_h_loop_filter)(uint8_t *src, int stride, int qscale)
Definition: dsputil.h:212
external API header
int resync_mb_x
x position of last resync marker
Definition: mpegvideo.h:527
void ff_h263_loop_filter(MpegEncContext *s)
Definition: h263.c:141
int ff_h263_get_gob_height(MpegEncContext *s)
Get the GOB height based on picture height.
Definition: h263.c:377
int16_t(*[2] motion_val)[2]
Definition: mpegvideo.h:105
int block_index[6]
index to current MB in block based arrays with edges
Definition: mpegvideo.h:465
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:421
int first_slice_line
used in mpeg4 too to handle resync markers
Definition: mpegvideo.h:637
void ff_h263_update_motion_val(MpegEncContext *s)
Definition: h263.c:48
uint8_t ff_h263_static_rl_table_store[2][2][2 *MAX_RUN+MAX_LEVEL+3]
Definition: h263.c:45
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:245
synthesis window for stochastic i
#define MB_TYPE_16x16
#define mid_pred
Definition: mathops.h:94
DSPContext dsp
pointers for accelerated dsp functions
Definition: mpegvideo.h:391
int h263_pred
use mpeg4/h263 ac/dc predictions
Definition: mpegvideo.h:250
int mv[2][4][2]
motion vectors for a macroblock first coordinate : 0 = forward 1 = backward second " : depend...
Definition: mpegvideo.h:431
int b8_stride
2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
Definition: mpegvideo.h:279
MpegEncContext.
Definition: mpegvideo.h:241
int8_t * qscale_table
Definition: mpegvideo.h:102
#define MAX_RUN
Definition: rl.h:34
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 ...
Definition: mpegvideo.h:278
void ff_h263_pred_acdc(MpegEncContext *s, int16_t *block, int n)
Definition: h263.c:228
uint8_t * dest[3]
Definition: mpegvideo.h:467
static double c[64]
#define MB_TYPE_8x8
Same thing on a dB scale
function y
Definition: D.m:1
const uint8_t * chroma_qscale_table
qscale -> chroma_qscale (h263)
Definition: mpegvideo.h:353
void(* h263_v_loop_filter)(uint8_t *src, int stride, int qscale)
Definition: dsputil.h:211
int linesize
line size, in bytes, may be different from width
Definition: mpegvideo.h:283
uint32_t * mb_type
Definition: mpegvideo.h:108
#define MV_TYPE_8X8
4 vectors (h263, mpeg4 4MV)
Definition: mpegvideo.h:422
int h263_aic_dir
AIC direction: 0 = left, 1 = top.
Definition: mpegvideo.h:547
int uvlinesize
line size, for chroma in bytes, may be different from width
Definition: mpegvideo.h:284
#define MB_TYPE_L0