FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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.f.ref_index[0][4*mb_xy ] =
74  s->current_picture.f.ref_index[0][4*mb_xy + 1] = s->field_select[0][0];
75  s->current_picture.f.ref_index[0][4*mb_xy + 2] =
76  s->current_picture.f.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.f.motion_val[0][xy][0] = motion_x;
81  s->current_picture.f.motion_val[0][xy][1] = motion_y;
82  s->current_picture.f.motion_val[0][xy + 1][0] = motion_x;
83  s->current_picture.f.motion_val[0][xy + 1][1] = motion_y;
84  s->current_picture.f.motion_val[0][xy + wrap][0] = motion_x;
85  s->current_picture.f.motion_val[0][xy + wrap][1] = motion_y;
86  s->current_picture.f.motion_val[0][xy + 1 + wrap][0] = motion_x;
87  s->current_picture.f.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.f.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.f.mb_type[xy - s->mb_stride]))
167  qp_tt=0;
168  else
169  qp_tt = s->current_picture.f.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.f.mb_type[xy - 1 - s->mb_stride]))
190  qp_dt= qp_tt;
191  else
192  qp_dt = s->current_picture.f.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.f.mb_type[xy - 1]))
212  qp_lc= qp_c;
213  else
214  qp_lc = s->current_picture.f.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.f.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 }