FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h263.c
Date: 2022-01-16 20:33:26
Exec Total Coverage
Lines: 120 130 92.3%
Branches: 67 70 95.7%

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 287 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 287 ff_rl_init(&ff_h263_rl_inter, h263_rl_inter_table);
45 287 }
46
47 287 av_cold void ff_h263_init_rl_inter(void)
48 {
49 static AVOnce init_static_once = AV_ONCE_INIT;
50 287 ff_thread_once(&init_static_once, h263_init_rl_inter);
51 287 }
52
53 3352765 void ff_h263_update_motion_val(MpegEncContext * s){
54 3352765 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 3352765 const int wrap = s->b8_stride;
57 3352765 const int xy = s->block_index[0];
58
59 3352765 s->current_picture.mbskip_table[mb_xy] = s->mb_skipped;
60
61
2/2
✓ Branch 0 taken 3148561 times.
✓ Branch 1 taken 204204 times.
3352765 if(s->mv_type != MV_TYPE_8X8){
62 int motion_x, motion_y;
63
2/2
✓ Branch 0 taken 494691 times.
✓ Branch 1 taken 2653870 times.
3148561 if (s->mb_intra) {
64 494691 motion_x = 0;
65 494691 motion_y = 0;
66
1/2
✓ Branch 0 taken 2653870 times.
✗ Branch 1 not taken.
2653870 } else if (s->mv_type == MV_TYPE_16X16) {
67 2653870 motion_x = s->mv[0][0][0];
68 2653870 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 3148561 s->current_picture.motion_val[0][xy][0] = motion_x;
86 3148561 s->current_picture.motion_val[0][xy][1] = motion_y;
87 3148561 s->current_picture.motion_val[0][xy + 1][0] = motion_x;
88 3148561 s->current_picture.motion_val[0][xy + 1][1] = motion_y;
89 3148561 s->current_picture.motion_val[0][xy + wrap][0] = motion_x;
90 3148561 s->current_picture.motion_val[0][xy + wrap][1] = motion_y;
91 3148561 s->current_picture.motion_val[0][xy + 1 + wrap][0] = motion_x;
92 3148561 s->current_picture.motion_val[0][xy + 1 + wrap][1] = motion_y;
93 }
94
95
2/2
✓ Branch 0 taken 1339395 times.
✓ Branch 1 taken 2013370 times.
3352765 if(s->encoding){ //FIXME encoding MUST be cleaned up
96
2/2
✓ Branch 0 taken 79330 times.
✓ Branch 1 taken 1260065 times.
1339395 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 212819 times.
✓ Branch 1 taken 1047246 times.
1260065 else if(s->mb_intra)
99 212819 s->current_picture.mb_type[mb_xy] = MB_TYPE_INTRA;
100 else
101 1047246 s->current_picture.mb_type[mb_xy] = MB_TYPE_L0 | MB_TYPE_16x16;
102 }
103 3352765 }
104
105 260100 void ff_h263_loop_filter(MpegEncContext * s){
106 int qp_c;
107 260100 const int linesize = s->linesize;
108 260100 const int uvlinesize= s->uvlinesize;
109 260100 const int xy = s->mb_y * s->mb_stride + s->mb_x;
110 260100 uint8_t *dest_y = s->dest[0];
111 260100 uint8_t *dest_cb= s->dest[1];
112 260100 uint8_t *dest_cr= s->dest[2];
113
114 /*
115 Diag Top
116 Left Center
117 */
118
2/2
✓ Branch 0 taken 186447 times.
✓ Branch 1 taken 73653 times.
260100 if (!IS_SKIP(s->current_picture.mb_type[xy])) {
119 186447 qp_c= s->qscale;
120 186447 s->h263dsp.h263_v_loop_filter(dest_y + 8 * linesize, linesize, qp_c);
121 186447 s->h263dsp.h263_v_loop_filter(dest_y + 8 * linesize + 8, linesize, qp_c);
122 }else
123 73653 qp_c= 0;
124
125
2/2
✓ Branch 0 taken 244080 times.
✓ Branch 1 taken 16020 times.
260100 if(s->mb_y){
126 int qp_dt, qp_tt, qp_tc;
127
128
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]))
129 65553 qp_tt=0;
130 else
131 178527 qp_tt = s->current_picture.qscale_table[xy - s->mb_stride];
132
133
2/2
✓ Branch 0 taken 178645 times.
✓ Branch 1 taken 65435 times.
244080 if(qp_c)
134 178645 qp_tc= qp_c;
135 else
136 65435 qp_tc= qp_tt;
137
138
2/2
✓ Branch 0 taken 196681 times.
✓ Branch 1 taken 47399 times.
244080 if(qp_tc){
139 196681 const int chroma_qp= s->chroma_qscale_table[qp_tc];
140 196681 s->h263dsp.h263_v_loop_filter(dest_y, linesize, qp_tc);
141 196681 s->h263dsp.h263_v_loop_filter(dest_y + 8, linesize, qp_tc);
142
143 196681 s->h263dsp.h263_v_loop_filter(dest_cb, uvlinesize, chroma_qp);
144 196681 s->h263dsp.h263_v_loop_filter(dest_cr, uvlinesize, chroma_qp);
145 }
146
147
2/2
✓ Branch 0 taken 178527 times.
✓ Branch 1 taken 65553 times.
244080 if(qp_tt)
148 178527 s->h263dsp.h263_h_loop_filter(dest_y - 8 * linesize + 8, linesize, qp_tt);
149
150
2/2
✓ Branch 0 taken 232386 times.
✓ Branch 1 taken 11694 times.
244080 if(s->mb_x){
151
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]))
152 217435 qp_dt= qp_tt;
153 else
154 14951 qp_dt = s->current_picture.qscale_table[xy - 1 - s->mb_stride];
155
156
2/2
✓ Branch 0 taken 185782 times.
✓ Branch 1 taken 46604 times.
232386 if(qp_dt){
157 185782 const int chroma_qp= s->chroma_qscale_table[qp_dt];
158 185782 s->h263dsp.h263_h_loop_filter(dest_y - 8 * linesize, linesize, qp_dt);
159 185782 s->h263dsp.h263_h_loop_filter(dest_cb - 8 * uvlinesize, uvlinesize, chroma_qp);
160 185782 s->h263dsp.h263_h_loop_filter(dest_cr - 8 * uvlinesize, uvlinesize, chroma_qp);
161 }
162 }
163 }
164
165
2/2
✓ Branch 0 taken 186447 times.
✓ Branch 1 taken 73653 times.
260100 if(qp_c){
166 186447 s->h263dsp.h263_h_loop_filter(dest_y + 8, linesize, qp_c);
167
2/2
✓ Branch 0 taken 7920 times.
✓ Branch 1 taken 178527 times.
186447 if(s->mb_y + 1 == s->mb_height)
168 7920 s->h263dsp.h263_h_loop_filter(dest_y + 8 * linesize + 8, linesize, qp_c);
169 }
170
171
2/2
✓ Branch 0 taken 247635 times.
✓ Branch 1 taken 12465 times.
260100 if(s->mb_x){
172 int qp_lc;
173
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]))
174 232441 qp_lc= qp_c;
175 else
176 15194 qp_lc = s->current_picture.qscale_table[xy - 1];
177
178
2/2
✓ Branch 0 taken 193584 times.
✓ Branch 1 taken 54051 times.
247635 if(qp_lc){
179 193584 s->h263dsp.h263_h_loop_filter(dest_y, linesize, qp_lc);
180
2/2
✓ Branch 0 taken 7802 times.
✓ Branch 1 taken 185782 times.
193584 if(s->mb_y + 1 == s->mb_height){
181 7802 const int chroma_qp= s->chroma_qscale_table[qp_lc];
182 7802 s->h263dsp.h263_h_loop_filter(dest_y + 8 * linesize, linesize, qp_lc);
183 7802 s->h263dsp.h263_h_loop_filter(dest_cb, uvlinesize, chroma_qp);
184 7802 s->h263dsp.h263_h_loop_filter(dest_cr, uvlinesize, chroma_qp);
185 }
186 }
187 }
188 260100 }
189
190 3914351 int16_t *ff_h263_pred_motion(MpegEncContext * s, int block, int dir,
191 int *px, int *py)
192 {
193 int wrap;
194 int16_t *A, *B, *C, (*mot_val)[2];
195 static const int off[4]= {2, 1, 1, -1};
196
197 3914351 wrap = s->b8_stride;
198 3914351 mot_val = s->current_picture.motion_val[dir] + s->block_index[block];
199
200 3914351 A = mot_val[ - 1];
201 /* special case for first (slice) line */
202
4/4
✓ Branch 0 taken 427174 times.
✓ Branch 1 taken 3487177 times.
✓ Branch 2 taken 389271 times.
✓ Branch 3 taken 37903 times.
3914351 if (s->first_slice_line && block<3) {
203 // we can't just change some MVs to simulate that as we need them for the B-frames (and ME)
204 // and if we ever support non rectangular objects than we need to do a few ifs here anyway :(
205
2/2
✓ Branch 0 taken 313465 times.
✓ Branch 1 taken 75806 times.
389271 if(block==0){ //most common case
206
2/2
✓ Branch 0 taken 15172 times.
✓ Branch 1 taken 298293 times.
313465 if(s->mb_x == s->resync_mb_x){ //rare
207 15172 *px= *py = 0;
208
4/4
✓ Branch 0 taken 2120 times.
✓ Branch 1 taken 296173 times.
✓ Branch 2 taken 1859 times.
✓ Branch 3 taken 261 times.
298293 }else if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
209 1859 C = mot_val[off[block] - wrap];
210
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 1741 times.
1859 if(s->mb_x==0){
211 118 *px = C[0];
212 118 *py = C[1];
213 }else{
214 1741 *px = mid_pred(A[0], 0, C[0]);
215 1741 *py = mid_pred(A[1], 0, C[1]);
216 }
217 }else{
218 296434 *px = A[0];
219 296434 *py = A[1];
220 }
221
2/2
✓ Branch 0 taken 37903 times.
✓ Branch 1 taken 37903 times.
75806 }else if(block==1){
222
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
223 730 C = mot_val[off[block] - wrap];
224 730 *px = mid_pred(A[0], 0, C[0]);
225 730 *py = mid_pred(A[1], 0, C[1]);
226 }else{
227 37173 *px = A[0];
228 37173 *py = A[1];
229 }
230 }else{ /* block==2*/
231 37903 B = mot_val[ - wrap];
232 37903 C = mot_val[off[block] - wrap];
233
2/2
✓ Branch 0 taken 2170 times.
✓ Branch 1 taken 35733 times.
37903 if(s->mb_x == s->resync_mb_x) //rare
234 2170 A[0]=A[1]=0;
235
236 37903 *px = mid_pred(A[0], B[0], C[0]);
237 37903 *py = mid_pred(A[1], B[1], C[1]);
238 }
239 } else {
240 3525080 B = mot_val[ - wrap];
241 3525080 C = mot_val[off[block] - wrap];
242 3525080 *px = mid_pred(A[0], B[0], C[0]);
243 3525080 *py = mid_pred(A[1], B[1], C[1]);
244 }
245 3914351 return *mot_val;
246 }
247