FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h263.c
Date: 2024-04-26 14:42:52
Exec Total Coverage
Lines: 120 130 92.3%
Functions: 5 5 100.0%
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 275 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 275 ff_rl_init(&ff_h263_rl_inter, h263_rl_inter_table);
45 275 }
46
47 275 av_cold void ff_h263_init_rl_inter(void)
48 {
49 static AVOnce init_static_once = AV_ONCE_INIT;
50 275 ff_thread_once(&init_static_once, h263_init_rl_inter);
51 275 }
52
53 3424879 void ff_h263_update_motion_val(MpegEncContext * s){
54 3424879 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 3424879 const int wrap = s->b8_stride;
57 3424879 const int xy = s->block_index[0];
58
59 3424879 s->current_picture.mbskip_table[mb_xy] = s->mb_skipped;
60
61
2/2
✓ Branch 0 taken 3219637 times.
✓ Branch 1 taken 205242 times.
3424879 if(s->mv_type != MV_TYPE_8X8){
62 int motion_x, motion_y;
63
2/2
✓ Branch 0 taken 507616 times.
✓ Branch 1 taken 2712021 times.
3219637 if (s->mb_intra) {
64 507616 motion_x = 0;
65 507616 motion_y = 0;
66
1/2
✓ Branch 0 taken 2712021 times.
✗ Branch 1 not taken.
2712021 } else if (s->mv_type == MV_TYPE_16X16) {
67 2712021 motion_x = s->mv[0][0][0];
68 2712021 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 3219637 s->current_picture.motion_val[0][xy][0] = motion_x;
86 3219637 s->current_picture.motion_val[0][xy][1] = motion_y;
87 3219637 s->current_picture.motion_val[0][xy + 1][0] = motion_x;
88 3219637 s->current_picture.motion_val[0][xy + 1][1] = motion_y;
89 3219637 s->current_picture.motion_val[0][xy + wrap][0] = motion_x;
90 3219637 s->current_picture.motion_val[0][xy + wrap][1] = motion_y;
91 3219637 s->current_picture.motion_val[0][xy + 1 + wrap][0] = motion_x;
92 3219637 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 2085484 times.
3424879 if(s->encoding){ //FIXME encoding MUST be cleaned up
96
2/2
✓ Branch 0 taken 79491 times.
✓ Branch 1 taken 1259904 times.
1339395 if (s->mv_type == MV_TYPE_8X8)
97 79491 s->current_picture.mb_type[mb_xy] = MB_TYPE_L0 | MB_TYPE_8x8;
98
2/2
✓ Branch 0 taken 215522 times.
✓ Branch 1 taken 1044382 times.
1259904 else if(s->mb_intra)
99 215522 s->current_picture.mb_type[mb_xy] = MB_TYPE_INTRA;
100 else
101 1044382 s->current_picture.mb_type[mb_xy] = MB_TYPE_L0 | MB_TYPE_16x16;
102 }
103 3424879 }
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 187629 times.
✓ Branch 1 taken 72471 times.
260100 if (!IS_SKIP(s->current_picture.mb_type[xy])) {
119 187629 qp_c= s->qscale;
120 187629 s->h263dsp.h263_v_loop_filter(dest_y + 8 * linesize, linesize, qp_c);
121 187629 s->h263dsp.h263_v_loop_filter(dest_y + 8 * linesize + 8, linesize, qp_c);
122 }else
123 72471 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 64470 times.
✓ Branch 1 taken 179610 times.
244080 if (IS_SKIP(s->current_picture.mb_type[xy - s->mb_stride]))
129 64470 qp_tt=0;
130 else
131 179610 qp_tt = s->current_picture.qscale_table[xy - s->mb_stride];
132
133
2/2
✓ Branch 0 taken 179730 times.
✓ Branch 1 taken 64350 times.
244080 if(qp_c)
134 179730 qp_tc= qp_c;
135 else
136 64350 qp_tc= qp_tt;
137
138
2/2
✓ Branch 0 taken 197619 times.
✓ Branch 1 taken 46461 times.
244080 if(qp_tc){
139 197619 const int chroma_qp= s->chroma_qscale_table[qp_tc];
140 197619 s->h263dsp.h263_v_loop_filter(dest_y, linesize, qp_tc);
141 197619 s->h263dsp.h263_v_loop_filter(dest_y + 8, linesize, qp_tc);
142
143 197619 s->h263dsp.h263_v_loop_filter(dest_cb, uvlinesize, chroma_qp);
144 197619 s->h263dsp.h263_v_loop_filter(dest_cr, uvlinesize, chroma_qp);
145 }
146
147
2/2
✓ Branch 0 taken 179610 times.
✓ Branch 1 taken 64470 times.
244080 if(qp_tt)
148 179610 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 60530 times.
✓ Branch 1 taken 171856 times.
✓ Branch 2 taken 45687 times.
✓ Branch 3 taken 14843 times.
232386 if (qp_tt || IS_SKIP(s->current_picture.mb_type[xy - 1 - s->mb_stride]))
152 217543 qp_dt= qp_tt;
153 else
154 14843 qp_dt = s->current_picture.qscale_table[xy - 1 - s->mb_stride];
155
156
2/2
✓ Branch 0 taken 186699 times.
✓ Branch 1 taken 45687 times.
232386 if(qp_dt){
157 186699 const int chroma_qp= s->chroma_qscale_table[qp_dt];
158 186699 s->h263dsp.h263_h_loop_filter(dest_y - 8 * linesize, linesize, qp_dt);
159 186699 s->h263dsp.h263_h_loop_filter(dest_cb - 8 * uvlinesize, uvlinesize, chroma_qp);
160 186699 s->h263dsp.h263_h_loop_filter(dest_cr - 8 * uvlinesize, uvlinesize, chroma_qp);
161 }
162 }
163 }
164
165
2/2
✓ Branch 0 taken 187629 times.
✓ Branch 1 taken 72471 times.
260100 if(qp_c){
166 187629 s->h263dsp.h263_h_loop_filter(dest_y + 8, linesize, qp_c);
167
2/2
✓ Branch 0 taken 8019 times.
✓ Branch 1 taken 179610 times.
187629 if(s->mb_y + 1 == s->mb_height)
168 8019 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 68126 times.
✓ Branch 1 taken 179509 times.
✓ Branch 2 taken 53041 times.
✓ Branch 3 taken 15085 times.
247635 if (qp_c || IS_SKIP(s->current_picture.mb_type[xy - 1]))
174 232550 qp_lc= qp_c;
175 else
176 15085 qp_lc = s->current_picture.qscale_table[xy - 1];
177
178
2/2
✓ Branch 0 taken 194594 times.
✓ Branch 1 taken 53041 times.
247635 if(qp_lc){
179 194594 s->h263dsp.h263_h_loop_filter(dest_y, linesize, qp_lc);
180
2/2
✓ Branch 0 taken 7895 times.
✓ Branch 1 taken 186699 times.
194594 if(s->mb_y + 1 == s->mb_height){
181 7895 const int chroma_qp= s->chroma_qscale_table[qp_lc];
182 7895 s->h263dsp.h263_h_loop_filter(dest_y + 8 * linesize, linesize, qp_lc);
183 7895 s->h263dsp.h263_h_loop_filter(dest_cb, uvlinesize, chroma_qp);
184 7895 s->h263dsp.h263_h_loop_filter(dest_cr, uvlinesize, chroma_qp);
185 }
186 }
187 }
188 260100 }
189
190 3919577 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 3919577 wrap = s->b8_stride;
198 3919577 mot_val = s->current_picture.motion_val[dir] + s->block_index[block];
199
200 3919577 A = mot_val[ - 1];
201 /* special case for first (slice) line */
202
4/4
✓ Branch 0 taken 427574 times.
✓ Branch 1 taken 3492003 times.
✓ Branch 2 taken 389648 times.
✓ Branch 3 taken 37926 times.
3919577 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 313796 times.
✓ Branch 1 taken 75852 times.
389648 if(block==0){ //most common case
206
2/2
✓ Branch 0 taken 15182 times.
✓ Branch 1 taken 298614 times.
313796 if(s->mb_x == s->resync_mb_x){ //rare
207 15182 *px= *py = 0;
208
4/4
✓ Branch 0 taken 2120 times.
✓ Branch 1 taken 296494 times.
✓ Branch 2 taken 1859 times.
✓ Branch 3 taken 261 times.
298614 }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 296755 *px = A[0];
219 296755 *py = A[1];
220 }
221
2/2
✓ Branch 0 taken 37926 times.
✓ Branch 1 taken 37926 times.
75852 }else if(block==1){
222
4/4
✓ Branch 0 taken 751 times.
✓ Branch 1 taken 37175 times.
✓ Branch 2 taken 730 times.
✓ Branch 3 taken 21 times.
37926 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 37196 *px = A[0];
228 37196 *py = A[1];
229 }
230 }else{ /* block==2*/
231 37926 B = mot_val[ - wrap];
232 37926 C = mot_val[off[block] - wrap];
233
2/2
✓ Branch 0 taken 2171 times.
✓ Branch 1 taken 35755 times.
37926 if(s->mb_x == s->resync_mb_x) //rare
234 2171 A[0]=A[1]=0;
235
236 37926 *px = mid_pred(A[0], B[0], C[0]);
237 37926 *py = mid_pred(A[1], B[1], C[1]);
238 }
239 } else {
240 3529929 B = mot_val[ - wrap];
241 3529929 C = mot_val[off[block] - wrap];
242 3529929 *px = mid_pred(A[0], B[0], C[0]);
243 3529929 *py = mid_pred(A[1], B[1], C[1]);
244 }
245 3919577 return *mot_val;
246 }
247