FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h263.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 190 201 94.5%
Branches: 120 124 96.8%

Line Branch Exec Source
1 /*
2 * H.263/MPEG-4 backend for encoder and decoder
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * H.263+ 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 * H.263/MPEG-4 codec.
28 */
29
30 #include "libavutil/thread.h"
31 #include "mpegvideo.h"
32 #include "h263.h"
33 #include "h263data.h"
34 #include "h263dsp.h"
35 #include "idctdsp.h"
36 #include "mathops.h"
37 #include "mpegpicture.h"
38 #include "mpegutils.h"
39 #include "rl.h"
40
41 283 static av_cold void h263_init_rl_inter(void)
42 {
43 static uint8_t h263_rl_inter_table[2][2 * MAX_RUN + MAX_LEVEL + 3];
44 283 ff_rl_init(&ff_h263_rl_inter, h263_rl_inter_table);
45 283 }
46
47 283 av_cold void ff_h263_init_rl_inter(void)
48 {
49 static AVOnce init_static_once = AV_ONCE_INIT;
50 283 ff_thread_once(&init_static_once, h263_init_rl_inter);
51 283 }
52
53 3313165 void ff_h263_update_motion_val(MpegEncContext * s){
54 3313165 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
55 //FIXME a lot of that is only needed for !low_delay
56 3313165 const int wrap = s->b8_stride;
57 3313165 const int xy = s->block_index[0];
58
59 3313165 s->current_picture.mbskip_table[mb_xy] = s->mb_skipped;
60
61
2/2
✓ Branch 0 taken 3108961 times.
✓ Branch 1 taken 204204 times.
3313165 if(s->mv_type != MV_TYPE_8X8){
62 int motion_x, motion_y;
63
2/2
✓ Branch 0 taken 490715 times.
✓ Branch 1 taken 2618246 times.
3108961 if (s->mb_intra) {
64 490715 motion_x = 0;
65 490715 motion_y = 0;
66
1/2
✓ Branch 0 taken 2618246 times.
✗ Branch 1 not taken.
2618246 } else if (s->mv_type == MV_TYPE_16X16) {
67 2618246 motion_x = s->mv[0][0][0];
68 2618246 motion_y = s->mv[0][0][1];
69 } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
70 int i;
71 motion_x = s->mv[0][0][0] + s->mv[0][1][0];
72 motion_y = s->mv[0][0][1] + s->mv[0][1][1];
73 motion_x = (motion_x>>1) | (motion_x&1);
74 for(i=0; i<2; i++){
75 s->p_field_mv_table[i][0][mb_xy][0]= s->mv[0][i][0];
76 s->p_field_mv_table[i][0][mb_xy][1]= s->mv[0][i][1];
77 }
78 s->current_picture.ref_index[0][4*mb_xy ] =
79 s->current_picture.ref_index[0][4*mb_xy + 1] = s->field_select[0][0];
80 s->current_picture.ref_index[0][4*mb_xy + 2] =
81 s->current_picture.ref_index[0][4*mb_xy + 3] = s->field_select[0][1];
82 }
83
84 /* no update if 8X8 because it has been done during parsing */
85 3108961 s->current_picture.motion_val[0][xy][0] = motion_x;
86 3108961 s->current_picture.motion_val[0][xy][1] = motion_y;
87 3108961 s->current_picture.motion_val[0][xy + 1][0] = motion_x;
88 3108961 s->current_picture.motion_val[0][xy + 1][1] = motion_y;
89 3108961 s->current_picture.motion_val[0][xy + wrap][0] = motion_x;
90 3108961 s->current_picture.motion_val[0][xy + wrap][1] = motion_y;
91 3108961 s->current_picture.motion_val[0][xy + 1 + wrap][0] = motion_x;
92 3108961 s->current_picture.motion_val[0][xy + 1 + wrap][1] = motion_y;
93 }
94
95
2/2
✓ Branch 0 taken 1299795 times.
✓ Branch 1 taken 2013370 times.
3313165 if(s->encoding){ //FIXME encoding MUST be cleaned up
96
2/2
✓ Branch 0 taken 79330 times.
✓ Branch 1 taken 1220465 times.
1299795 if (s->mv_type == MV_TYPE_8X8)
97 79330 s->current_picture.mb_type[mb_xy] = MB_TYPE_L0 | MB_TYPE_8x8;
98
2/2
✓ Branch 0 taken 208843 times.
✓ Branch 1 taken 1011622 times.
1220465 else if(s->mb_intra)
99 208843 s->current_picture.mb_type[mb_xy] = MB_TYPE_INTRA;
100 else
101 1011622 s->current_picture.mb_type[mb_xy] = MB_TYPE_L0 | MB_TYPE_16x16;
102 }
103 3313165 }
104
105 82098 int ff_h263_pred_dc(MpegEncContext * s, int n, int16_t **dc_val_ptr)
106 {
107 int x, y, wrap, a, c, pred_dc;
108 int16_t *dc_val;
109
110 /* find prediction */
111
2/2
✓ Branch 0 taken 54732 times.
✓ Branch 1 taken 27366 times.
82098 if (n < 4) {
112 54732 x = 2 * s->mb_x + (n & 1);
113 54732 y = 2 * s->mb_y + ((n & 2) >> 1);
114 54732 wrap = s->b8_stride;
115 54732 dc_val = s->dc_val[0];
116 } else {
117 27366 x = s->mb_x;
118 27366 y = s->mb_y;
119 27366 wrap = s->mb_stride;
120 27366 dc_val = s->dc_val[n - 4 + 1];
121 }
122 /* B C
123 * A X
124 */
125 82098 a = dc_val[(x - 1) + (y) * wrap];
126 82098 c = dc_val[(x) + (y - 1) * wrap];
127
128 /* No prediction outside GOB boundary */
129
4/4
✓ Branch 0 taken 48438 times.
✓ Branch 1 taken 33660 times.
✓ Branch 2 taken 40365 times.
✓ Branch 3 taken 8073 times.
82098 if(s->first_slice_line && n!=3){
130
2/2
✓ Branch 0 taken 32292 times.
✓ Branch 1 taken 8073 times.
40365 if(n!=2) c= 1024;
131
4/4
✓ Branch 0 taken 32292 times.
✓ Branch 1 taken 8073 times.
✓ Branch 2 taken 1140 times.
✓ Branch 3 taken 31152 times.
40365 if(n!=1 && s->mb_x == s->resync_mb_x) a= 1024;
132 }
133 /* just DC prediction */
134
4/4
✓ Branch 0 taken 76882 times.
✓ Branch 1 taken 5216 times.
✓ Branch 2 taken 47737 times.
✓ Branch 3 taken 29145 times.
82098 if (a != 1024 && c != 1024)
135 47737 pred_dc = (a + c) >> 1;
136
2/2
✓ Branch 0 taken 29145 times.
✓ Branch 1 taken 5216 times.
34361 else if (a != 1024)
137 29145 pred_dc = a;
138 else
139 5216 pred_dc = c;
140
141 /* we assume pred is positive */
142 82098 *dc_val_ptr = &dc_val[x + y * wrap];
143 82098 return pred_dc;
144 }
145
146 260100 void ff_h263_loop_filter(MpegEncContext * s){
147 int qp_c;
148 260100 const int linesize = s->linesize;
149 260100 const int uvlinesize= s->uvlinesize;
150 260100 const int xy = s->mb_y * s->mb_stride + s->mb_x;
151 260100 uint8_t *dest_y = s->dest[0];
152 260100 uint8_t *dest_cb= s->dest[1];
153 260100 uint8_t *dest_cr= s->dest[2];
154
155 /*
156 Diag Top
157 Left Center
158 */
159
2/2
✓ Branch 0 taken 186447 times.
✓ Branch 1 taken 73653 times.
260100 if (!IS_SKIP(s->current_picture.mb_type[xy])) {
160 186447 qp_c= s->qscale;
161 186447 s->h263dsp.h263_v_loop_filter(dest_y + 8 * linesize, linesize, qp_c);
162 186447 s->h263dsp.h263_v_loop_filter(dest_y + 8 * linesize + 8, linesize, qp_c);
163 }else
164 73653 qp_c= 0;
165
166
2/2
✓ Branch 0 taken 244080 times.
✓ Branch 1 taken 16020 times.
260100 if(s->mb_y){
167 int qp_dt, qp_tt, qp_tc;
168
169
2/2
✓ Branch 0 taken 65553 times.
✓ Branch 1 taken 178527 times.
244080 if (IS_SKIP(s->current_picture.mb_type[xy - s->mb_stride]))
170 65553 qp_tt=0;
171 else
172 178527 qp_tt = s->current_picture.qscale_table[xy - s->mb_stride];
173
174
2/2
✓ Branch 0 taken 178645 times.
✓ Branch 1 taken 65435 times.
244080 if(qp_c)
175 178645 qp_tc= qp_c;
176 else
177 65435 qp_tc= qp_tt;
178
179
2/2
✓ Branch 0 taken 196681 times.
✓ Branch 1 taken 47399 times.
244080 if(qp_tc){
180 196681 const int chroma_qp= s->chroma_qscale_table[qp_tc];
181 196681 s->h263dsp.h263_v_loop_filter(dest_y, linesize, qp_tc);
182 196681 s->h263dsp.h263_v_loop_filter(dest_y + 8, linesize, qp_tc);
183
184 196681 s->h263dsp.h263_v_loop_filter(dest_cb, uvlinesize, chroma_qp);
185 196681 s->h263dsp.h263_v_loop_filter(dest_cr, uvlinesize, chroma_qp);
186 }
187
188
2/2
✓ Branch 0 taken 178527 times.
✓ Branch 1 taken 65553 times.
244080 if(qp_tt)
189 178527 s->h263dsp.h263_h_loop_filter(dest_y - 8 * linesize + 8, linesize, qp_tt);
190
191
2/2
✓ Branch 0 taken 232386 times.
✓ Branch 1 taken 11694 times.
244080 if(s->mb_x){
192
4/4
✓ Branch 0 taken 61555 times.
✓ Branch 1 taken 170831 times.
✓ Branch 2 taken 46604 times.
✓ Branch 3 taken 14951 times.
232386 if (qp_tt || IS_SKIP(s->current_picture.mb_type[xy - 1 - s->mb_stride]))
193 217435 qp_dt= qp_tt;
194 else
195 14951 qp_dt = s->current_picture.qscale_table[xy - 1 - s->mb_stride];
196
197
2/2
✓ Branch 0 taken 185782 times.
✓ Branch 1 taken 46604 times.
232386 if(qp_dt){
198 185782 const int chroma_qp= s->chroma_qscale_table[qp_dt];
199 185782 s->h263dsp.h263_h_loop_filter(dest_y - 8 * linesize, linesize, qp_dt);
200 185782 s->h263dsp.h263_h_loop_filter(dest_cb - 8 * uvlinesize, uvlinesize, chroma_qp);
201 185782 s->h263dsp.h263_h_loop_filter(dest_cr - 8 * uvlinesize, uvlinesize, chroma_qp);
202 }
203 }
204 }
205
206
2/2
✓ Branch 0 taken 186447 times.
✓ Branch 1 taken 73653 times.
260100 if(qp_c){
207 186447 s->h263dsp.h263_h_loop_filter(dest_y + 8, linesize, qp_c);
208
2/2
✓ Branch 0 taken 7920 times.
✓ Branch 1 taken 178527 times.
186447 if(s->mb_y + 1 == s->mb_height)
209 7920 s->h263dsp.h263_h_loop_filter(dest_y + 8 * linesize + 8, linesize, qp_c);
210 }
211
212
2/2
✓ Branch 0 taken 247635 times.
✓ Branch 1 taken 12465 times.
260100 if(s->mb_x){
213 int qp_lc;
214
4/4
✓ Branch 0 taken 69245 times.
✓ Branch 1 taken 178390 times.
✓ Branch 2 taken 54051 times.
✓ Branch 3 taken 15194 times.
247635 if (qp_c || IS_SKIP(s->current_picture.mb_type[xy - 1]))
215 232441 qp_lc= qp_c;
216 else
217 15194 qp_lc = s->current_picture.qscale_table[xy - 1];
218
219
2/2
✓ Branch 0 taken 193584 times.
✓ Branch 1 taken 54051 times.
247635 if(qp_lc){
220 193584 s->h263dsp.h263_h_loop_filter(dest_y, linesize, qp_lc);
221
2/2
✓ Branch 0 taken 7802 times.
✓ Branch 1 taken 185782 times.
193584 if(s->mb_y + 1 == s->mb_height){
222 7802 const int chroma_qp= s->chroma_qscale_table[qp_lc];
223 7802 s->h263dsp.h263_h_loop_filter(dest_y + 8 * linesize, linesize, qp_lc);
224 7802 s->h263dsp.h263_h_loop_filter(dest_cb, uvlinesize, chroma_qp);
225 7802 s->h263dsp.h263_h_loop_filter(dest_cr, uvlinesize, chroma_qp);
226 }
227 }
228 }
229 260100 }
230
231 91098 void ff_h263_pred_acdc(MpegEncContext * s, int16_t *block, int n)
232 {
233 int x, y, wrap, a, c, pred_dc, scale, i;
234 int16_t *dc_val, *ac_val, *ac_val1;
235
236 /* find prediction */
237
2/2
✓ Branch 0 taken 60732 times.
✓ Branch 1 taken 30366 times.
91098 if (n < 4) {
238 60732 x = 2 * s->mb_x + (n & 1);
239 60732 y = 2 * s->mb_y + (n>> 1);
240 60732 wrap = s->b8_stride;
241 60732 dc_val = s->dc_val[0];
242 60732 ac_val = s->ac_val[0][0];
243 60732 scale = s->y_dc_scale;
244 } else {
245 30366 x = s->mb_x;
246 30366 y = s->mb_y;
247 30366 wrap = s->mb_stride;
248 30366 dc_val = s->dc_val[n - 4 + 1];
249 30366 ac_val = s->ac_val[n - 4 + 1][0];
250 30366 scale = s->c_dc_scale;
251 }
252
253 91098 ac_val += ((y) * wrap + (x)) * 16;
254 91098 ac_val1 = ac_val;
255
256 /* B C
257 * A X
258 */
259 91098 a = dc_val[(x - 1) + (y) * wrap];
260 91098 c = dc_val[(x) + (y - 1) * wrap];
261
262 /* No prediction outside GOB boundary */
263
4/4
✓ Branch 0 taken 54174 times.
✓ Branch 1 taken 36924 times.
✓ Branch 2 taken 45145 times.
✓ Branch 3 taken 9029 times.
91098 if(s->first_slice_line && n!=3){
264
2/2
✓ Branch 0 taken 36116 times.
✓ Branch 1 taken 9029 times.
45145 if(n!=2) c= 1024;
265
4/4
✓ Branch 0 taken 36116 times.
✓ Branch 1 taken 9029 times.
✓ Branch 2 taken 1348 times.
✓ Branch 3 taken 34768 times.
45145 if(n!=1 && s->mb_x == s->resync_mb_x) a= 1024;
266 }
267
268
2/2
✓ Branch 0 taken 4434 times.
✓ Branch 1 taken 86664 times.
91098 if (s->ac_pred) {
269 4434 pred_dc = 1024;
270
2/2
✓ Branch 0 taken 1596 times.
✓ Branch 1 taken 2838 times.
4434 if (s->h263_aic_dir) {
271 /* left prediction */
272
2/2
✓ Branch 0 taken 1576 times.
✓ Branch 1 taken 20 times.
1596 if (a != 1024) {
273 1576 ac_val -= 16;
274
2/2
✓ Branch 0 taken 11032 times.
✓ Branch 1 taken 1576 times.
12608 for(i=1;i<8;i++) {
275 11032 block[s->idsp.idct_permutation[i << 3]] += ac_val[i];
276 }
277 1576 pred_dc = a;
278 }
279 } else {
280 /* top prediction */
281
2/2
✓ Branch 0 taken 1910 times.
✓ Branch 1 taken 928 times.
2838 if (c != 1024) {
282 1910 ac_val -= 16 * wrap;
283
2/2
✓ Branch 0 taken 13370 times.
✓ Branch 1 taken 1910 times.
15280 for(i=1;i<8;i++) {
284 13370 block[s->idsp.idct_permutation[i]] += ac_val[i + 8];
285 }
286 1910 pred_dc = c;
287 }
288 }
289 } else {
290 /* just DC prediction */
291
4/4
✓ Branch 0 taken 81140 times.
✓ Branch 1 taken 5524 times.
✓ Branch 2 taken 50003 times.
✓ Branch 3 taken 31137 times.
86664 if (a != 1024 && c != 1024)
292 50003 pred_dc = (a + c) >> 1;
293
2/2
✓ Branch 0 taken 31137 times.
✓ Branch 1 taken 5524 times.
36661 else if (a != 1024)
294 31137 pred_dc = a;
295 else
296 5524 pred_dc = c;
297 }
298
299 /* we assume pred is positive */
300 91098 block[0]=block[0]*scale + pred_dc;
301
302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 91098 times.
91098 if (block[0] < 0)
303 block[0] = 0;
304 else
305 91098 block[0] |= 1;
306
307 /* Update AC/DC tables */
308 91098 dc_val[(x) + (y) * wrap] = block[0];
309
310 /* left copy */
311
2/2
✓ Branch 0 taken 637686 times.
✓ Branch 1 taken 91098 times.
728784 for(i=1;i<8;i++)
312 637686 ac_val1[i] = block[s->idsp.idct_permutation[i << 3]];
313 /* top copy */
314
2/2
✓ Branch 0 taken 637686 times.
✓ Branch 1 taken 91098 times.
728784 for(i=1;i<8;i++)
315 637686 ac_val1[8 + i] = block[s->idsp.idct_permutation[i]];
316 91098 }
317
318 3879579 int16_t *ff_h263_pred_motion(MpegEncContext * s, int block, int dir,
319 int *px, int *py)
320 {
321 int wrap;
322 int16_t *A, *B, *C, (*mot_val)[2];
323 static const int off[4]= {2, 1, 1, -1};
324
325 3879579 wrap = s->b8_stride;
326 3879579 mot_val = s->current_picture.motion_val[dir] + s->block_index[block];
327
328 3879579 A = mot_val[ - 1];
329 /* special case for first (slice) line */
330
4/4
✓ Branch 0 taken 425240 times.
✓ Branch 1 taken 3454339 times.
✓ Branch 2 taken 387337 times.
✓ Branch 3 taken 37903 times.
3879579 if (s->first_slice_line && block<3) {
331 // we can't just change some MVs to simulate that as we need them for the B-frames (and ME)
332 // and if we ever support non rectangular objects than we need to do a few ifs here anyway :(
333
2/2
✓ Branch 0 taken 311531 times.
✓ Branch 1 taken 75806 times.
387337 if(block==0){ //most common case
334
2/2
✓ Branch 0 taken 15084 times.
✓ Branch 1 taken 296447 times.
311531 if(s->mb_x == s->resync_mb_x){ //rare
335 15084 *px= *py = 0;
336
4/4
✓ Branch 0 taken 2120 times.
✓ Branch 1 taken 294327 times.
✓ Branch 2 taken 1859 times.
✓ Branch 3 taken 261 times.
296447 }else if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
337 1859 C = mot_val[off[block] - wrap];
338
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 1741 times.
1859 if(s->mb_x==0){
339 118 *px = C[0];
340 118 *py = C[1];
341 }else{
342 1741 *px = mid_pred(A[0], 0, C[0]);
343 1741 *py = mid_pred(A[1], 0, C[1]);
344 }
345 }else{
346 294588 *px = A[0];
347 294588 *py = A[1];
348 }
349
2/2
✓ Branch 0 taken 37903 times.
✓ Branch 1 taken 37903 times.
75806 }else if(block==1){
350
4/4
✓ Branch 0 taken 751 times.
✓ Branch 1 taken 37152 times.
✓ Branch 2 taken 730 times.
✓ Branch 3 taken 21 times.
37903 if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
351 730 C = mot_val[off[block] - wrap];
352 730 *px = mid_pred(A[0], 0, C[0]);
353 730 *py = mid_pred(A[1], 0, C[1]);
354 }else{
355 37173 *px = A[0];
356 37173 *py = A[1];
357 }
358 }else{ /* block==2*/
359 37903 B = mot_val[ - wrap];
360 37903 C = mot_val[off[block] - wrap];
361
2/2
✓ Branch 0 taken 2170 times.
✓ Branch 1 taken 35733 times.
37903 if(s->mb_x == s->resync_mb_x) //rare
362 2170 A[0]=A[1]=0;
363
364 37903 *px = mid_pred(A[0], B[0], C[0]);
365 37903 *py = mid_pred(A[1], B[1], C[1]);
366 }
367 } else {
368 3492242 B = mot_val[ - wrap];
369 3492242 C = mot_val[off[block] - wrap];
370 3492242 *px = mid_pred(A[0], B[0], C[0]);
371 3492242 *py = mid_pred(A[1], B[1], C[1]);
372 }
373 3879579 return *mot_val;
374 }
375