FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ituh263enc.c
Date: 2022-01-28 07:56:06
Exec Total Coverage
Lines: 401 466 86.1%
Branches: 215 272 79.0%

Line Branch Exec Source
1 /*
2 * ITU H.263 bitstream encoder
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 bitstream encoder.
28 */
29
30 #include <limits.h>
31
32 #include "libavutil/attributes.h"
33 #include "libavutil/thread.h"
34 #include "avcodec.h"
35 #include "mpegvideo.h"
36 #include "mpegvideodata.h"
37 #include "h263.h"
38 #include "h263data.h"
39 #include "mathops.h"
40 #include "mpegutils.h"
41 #include "flv.h"
42 #include "internal.h"
43
44 /**
45 * Table of number of bits a motion vector component needs.
46 */
47 static uint8_t mv_penalty[MAX_FCODE+1][MAX_DMV*2+1];
48
49 /**
50 * Minimal fcode that a motion vector component would need.
51 */
52 static uint8_t fcode_tab[MAX_MV*2+1];
53
54 /**
55 * Minimal fcode that a motion vector component would need in umv.
56 * All entries in this table are 1.
57 */
58 static uint8_t umv_fcode_tab[MAX_MV*2+1];
59
60 //unified encoding tables for run length encoding of coefficients
61 //unified in the sense that the specification specifies the encoding in several steps.
62 static uint8_t uni_h263_intra_aic_rl_len [64*64*2*2];
63 static uint8_t uni_h263_inter_rl_len [64*64*2*2];
64 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
65 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
66 #define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
67
68 static const uint8_t wrong_run[102] = {
69 1, 2, 3, 5, 4, 10, 9, 8,
70 11, 15, 17, 16, 23, 22, 21, 20,
71 19, 18, 25, 24, 27, 26, 11, 7,
72 6, 1, 2, 13, 2, 2, 2, 2,
73 6, 12, 3, 9, 1, 3, 4, 3,
74 7, 4, 1, 1, 5, 5, 14, 6,
75 1, 7, 1, 8, 1, 1, 1, 1,
76 10, 1, 1, 5, 9, 17, 25, 24,
77 29, 33, 32, 41, 2, 23, 28, 31,
78 3, 22, 30, 4, 27, 40, 8, 26,
79 6, 39, 7, 38, 16, 37, 15, 10,
80 11, 12, 13, 14, 1, 21, 20, 18,
81 19, 2, 1, 34, 35, 36
82 };
83
84 /**
85 * Return the 4 bit value that specifies the given aspect ratio.
86 * This may be one of the standard aspect ratios or it specifies
87 * that the aspect will be stored explicitly later.
88 */
89 219 av_const int ff_h263_aspect_to_info(AVRational aspect){
90 int i;
91
92
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 210 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
219 if(aspect.num==0 || aspect.den==0) aspect= (AVRational){1,1};
93
94
1/2
✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
219 for(i=1; i<6; i++){
95
1/2
✓ Branch 1 taken 219 times.
✗ Branch 2 not taken.
219 if(av_cmp_q(ff_h263_pixel_aspect[i], aspect) == 0){
96 219 return i;
97 }
98 }
99
100 return FF_ASPECT_EXTENDED;
101 }
102
103 450 void ff_h263_encode_picture_header(MpegEncContext * s, int picture_number)
104 {
105 int format, coded_frame_rate, coded_frame_rate_base, i, temp_ref;
106 450 int best_clock_code=1;
107 450 int best_divisor=60;
108 450 int best_error= INT_MAX;
109
110
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 300 times.
450 if(s->h263_plus){
111
2/2
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 150 times.
450 for(i=0; i<2; i++){
112 int div, error;
113 300 div= (s->avctx->time_base.num*1800000LL + 500LL*s->avctx->time_base.den) / ((1000LL+i)*s->avctx->time_base.den);
114 300 div= av_clip(div, 1, 127);
115
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 150 times.
300 error= FFABS(s->avctx->time_base.num*1800000LL - (1000LL+i)*s->avctx->time_base.den*div);
116
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 150 times.
300 if(error < best_error){
117 150 best_error= error;
118 150 best_divisor= div;
119 150 best_clock_code= i;
120 }
121 }
122 }
123
3/4
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 150 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 300 times.
450 s->custom_pcf= best_clock_code!=1 || best_divisor!=60;
124 450 coded_frame_rate= 1800000;
125 450 coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
126
127 450 align_put_bits(&s->pb);
128
129 /* Update the pointer to last GOB */
130 450 s->ptr_lastgob = put_bits_ptr(&s->pb);
131 450 put_bits(&s->pb, 22, 0x20); /* PSC */
132 450 temp_ref= s->picture_number * (int64_t)coded_frame_rate * s->avctx->time_base.num / //FIXME use timestamp
133 450 (coded_frame_rate_base * (int64_t)s->avctx->time_base.den);
134 450 put_sbits(&s->pb, 8, temp_ref); /* TemporalReference */
135
136 450 put_bits(&s->pb, 1, 1); /* marker */
137 450 put_bits(&s->pb, 1, 0); /* H.263 id */
138 450 put_bits(&s->pb, 1, 0); /* split screen off */
139 450 put_bits(&s->pb, 1, 0); /* camera off */
140 450 put_bits(&s->pb, 1, 0); /* freeze picture release off */
141
142 450 format = ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format), s->width, s->height);
143
2/2
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 150 times.
450 if (!s->h263_plus) {
144 /* H.263v1 */
145 300 put_bits(&s->pb, 3, format);
146 300 put_bits(&s->pb, 1, (s->pict_type == AV_PICTURE_TYPE_P));
147 /* By now UMV IS DISABLED ON H.263v1, since the restrictions
148 of H.263v1 UMV implies to check the predicted MV after
149 calculation of the current MB to see if we're on the limits */
150 300 put_bits(&s->pb, 1, 0); /* Unrestricted Motion Vector: off */
151 300 put_bits(&s->pb, 1, 0); /* SAC: off */
152 300 put_bits(&s->pb, 1, s->obmc); /* Advanced Prediction */
153 300 put_bits(&s->pb, 1, 0); /* only I/P-frames, no PB-frame */
154 300 put_bits(&s->pb, 5, s->qscale);
155 300 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
156 } else {
157 150 int ufep=1;
158 /* H.263v2 */
159 /* H.263 Plus PTYPE */
160
161 150 put_bits(&s->pb, 3, 7);
162 150 put_bits(&s->pb,3,ufep); /* Update Full Extended PTYPE */
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (format == 8)
164 put_bits(&s->pb,3,6); /* Custom Source Format */
165 else
166 150 put_bits(&s->pb, 3, format);
167
168 150 put_bits(&s->pb,1, s->custom_pcf);
169 150 put_bits(&s->pb,1, s->umvplus); /* Unrestricted Motion Vector */
170 150 put_bits(&s->pb,1,0); /* SAC: off */
171 150 put_bits(&s->pb,1,s->obmc); /* Advanced Prediction Mode */
172 150 put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */
173 150 put_bits(&s->pb,1,s->loop_filter); /* Deblocking Filter */
174 150 put_bits(&s->pb,1,s->h263_slice_structured); /* Slice Structured */
175 150 put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
176 150 put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
177 150 put_bits(&s->pb,1,s->alt_inter_vlc); /* Alternative Inter VLC */
178 150 put_bits(&s->pb,1,s->modified_quant); /* Modified Quantization: */
179 150 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
180 150 put_bits(&s->pb,3,0); /* Reserved */
181
182 150 put_bits(&s->pb, 3, s->pict_type == AV_PICTURE_TYPE_P);
183
184 150 put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
185 150 put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
186 150 put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */
187 150 put_bits(&s->pb,2,0); /* Reserved */
188 150 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
189
190 /* This should be here if PLUSPTYPE */
191 150 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
192
193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (format == 8) {
194 /* Custom Picture Format (CPFMT) */
195 s->aspect_ratio_info= ff_h263_aspect_to_info(s->avctx->sample_aspect_ratio);
196
197 put_bits(&s->pb,4,s->aspect_ratio_info);
198 put_bits(&s->pb,9,(s->width >> 2) - 1);
199 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
200 put_bits(&s->pb,9,(s->height >> 2));
201 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
202 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
203 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
204 }
205 }
206
1/2
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
150 if(s->custom_pcf){
207
1/2
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
150 if(ufep){
208 150 put_bits(&s->pb, 1, best_clock_code);
209 150 put_bits(&s->pb, 7, best_divisor);
210 }
211 150 put_sbits(&s->pb, 2, temp_ref>>8);
212 }
213
214 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
215
1/2
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
150 if (s->umvplus)
216 // put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
217 //FIXME check actual requested range
218 150 put_bits(&s->pb,2,1); /* unlimited */
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if(s->h263_slice_structured)
220 put_bits(&s->pb,2,0); /* no weird submodes */
221
222 150 put_bits(&s->pb, 5, s->qscale);
223 }
224
225 450 put_bits(&s->pb, 1, 0); /* no PEI */
226
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
450 if(s->h263_slice_structured){
228 put_bits(&s->pb, 1, 1);
229
230 av_assert1(s->mb_x == 0 && s->mb_y == 0);
231 ff_h263_encode_mba(s);
232
233 put_bits(&s->pb, 1, 1);
234 }
235 450 }
236
237 /**
238 * Encode a group of blocks header.
239 */
240 2544 void ff_h263_encode_gob_header(MpegEncContext * s, int mb_line)
241 {
242 2544 put_bits(&s->pb, 17, 1); /* GBSC */
243
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2544 times.
2544 if(s->h263_slice_structured){
245 put_bits(&s->pb, 1, 1);
246
247 ff_h263_encode_mba(s);
248
249 if(s->mb_num > 1583)
250 put_bits(&s->pb, 1, 1);
251 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
252 put_bits(&s->pb, 1, 1);
253 put_bits(&s->pb, 2, s->pict_type == AV_PICTURE_TYPE_I); /* GFID */
254 }else{
255 2544 int gob_number= mb_line / s->gob_index;
256
257 2544 put_bits(&s->pb, 5, gob_number); /* GN */
258 2544 put_bits(&s->pb, 2, s->pict_type == AV_PICTURE_TYPE_I); /* GFID */
259 2544 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
260 }
261 2544 }
262
263 /**
264 * modify qscale so that encoding is actually possible in H.263 (limit difference to -2..2)
265 */
266 400 void ff_clean_h263_qscales(MpegEncContext *s){
267 int i;
268 400 int8_t * const qscale_table = s->current_picture.qscale_table;
269
270 400 ff_init_qscale_tab(s);
271
272
2/2
✓ Branch 0 taken 119300 times.
✓ Branch 1 taken 400 times.
119700 for(i=1; i<s->mb_num; i++){
273
2/2
✓ Branch 0 taken 4341 times.
✓ Branch 1 taken 114959 times.
119300 if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i-1] ] >2)
274 4341 qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i-1] ]+2;
275 }
276
2/2
✓ Branch 0 taken 119300 times.
✓ Branch 1 taken 400 times.
119700 for(i=s->mb_num-2; i>=0; i--){
277
2/2
✓ Branch 0 taken 3504 times.
✓ Branch 1 taken 115796 times.
119300 if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i+1] ] >2)
278 3504 qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i+1] ]+2;
279 }
280
281
1/2
✓ Branch 0 taken 400 times.
✗ Branch 1 not taken.
400 if(s->codec_id != AV_CODEC_ID_H263P){
282
2/2
✓ Branch 0 taken 119300 times.
✓ Branch 1 taken 400 times.
119700 for(i=1; i<s->mb_num; i++){
283 119300 int mb_xy= s->mb_index2xy[i];
284
285
4/4
✓ Branch 0 taken 29685 times.
✓ Branch 1 taken 89615 times.
✓ Branch 2 taken 7334 times.
✓ Branch 3 taken 22351 times.
119300 if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTER4V)){
286 7334 s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER;
287 }
288 }
289 }
290 400 }
291
292 static const int dquant_code[5]= {1,0,9,2,3};
293
294 /**
295 * Encode an 8x8 block.
296 * @param block the 8x8 block
297 * @param n block index (0-3 are luma, 4-5 are chroma)
298 */
299 2388636 static void h263_encode_block(MpegEncContext * s, int16_t * block, int n)
300 {
301 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
302 RLTable *rl;
303
304 2388636 rl = &ff_h263_rl_inter;
305
4/4
✓ Branch 0 taken 348792 times.
✓ Branch 1 taken 2039844 times.
✓ Branch 2 taken 266694 times.
✓ Branch 3 taken 82098 times.
2388636 if (s->mb_intra && !s->h263_aic) {
306 /* DC coef */
307 266694 level = block[0];
308 /* 255 cannot be represented, so we clamp */
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 266694 times.
266694 if (level > 254) {
310 level = 254;
311 block[0] = 254;
312 }
313 /* 0 cannot be represented also */
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 266694 times.
266694 else if (level < 1) {
315 level = 1;
316 block[0] = 1;
317 }
318
2/2
✓ Branch 0 taken 5826 times.
✓ Branch 1 taken 260868 times.
266694 if (level == 128) //FIXME check rv10
319 5826 put_bits(&s->pb, 8, 0xff);
320 else
321 260868 put_bits(&s->pb, 8, level);
322 266694 i = 1;
323 } else {
324 2121942 i = 0;
325
4/4
✓ Branch 0 taken 391992 times.
✓ Branch 1 taken 1729950 times.
✓ Branch 2 taken 82098 times.
✓ Branch 3 taken 309894 times.
2121942 if (s->h263_aic && s->mb_intra)
326 82098 rl = &ff_rl_intra_aic;
327
328
4/4
✓ Branch 0 taken 356352 times.
✓ Branch 1 taken 1765590 times.
✓ Branch 2 taken 309894 times.
✓ Branch 3 taken 46458 times.
2121942 if(s->alt_inter_vlc && !s->mb_intra){
329 309894 int aic_vlc_bits=0;
330 309894 int inter_vlc_bits=0;
331 309894 int wrong_pos=-1;
332 int aic_code;
333
334 309894 last_index = s->block_last_index[n];
335 309894 last_non_zero = i - 1;
336
2/2
✓ Branch 0 taken 9817100 times.
✓ Branch 1 taken 309894 times.
10126994 for (; i <= last_index; i++) {
337 9817100 j = s->intra_scantable.permutated[i];
338 9817100 level = block[j];
339
2/2
✓ Branch 0 taken 3646040 times.
✓ Branch 1 taken 6171060 times.
9817100 if (level) {
340 3646040 run = i - last_non_zero - 1;
341 3646040 last = (i == last_index);
342
343
2/2
✓ Branch 0 taken 1800387 times.
✓ Branch 1 taken 1845653 times.
3646040 if(level<0) level= -level;
344
345 3646040 code = get_rl_index(rl, last, run, level);
346 3646040 aic_code = get_rl_index(&ff_rl_intra_aic, last, run, level);
347 3646040 inter_vlc_bits += rl->table_vlc[code][1]+1;
348 3646040 aic_vlc_bits += ff_rl_intra_aic.table_vlc[aic_code][1]+1;
349
350
2/2
✓ Branch 0 taken 238217 times.
✓ Branch 1 taken 3407823 times.
3646040 if (code == rl->n) {
351 238217 inter_vlc_bits += 1+6+8-1;
352 }
353
2/2
✓ Branch 0 taken 160580 times.
✓ Branch 1 taken 3485460 times.
3646040 if (aic_code == ff_rl_intra_aic.n) {
354 160580 aic_vlc_bits += 1+6+8-1;
355 160580 wrong_pos += run + 1;
356 }else
357 3485460 wrong_pos += wrong_run[aic_code];
358 3646040 last_non_zero = i;
359 }
360 }
361 309894 i = 0;
362
4/4
✓ Branch 0 taken 102063 times.
✓ Branch 1 taken 207831 times.
✓ Branch 2 taken 66459 times.
✓ Branch 3 taken 35604 times.
309894 if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
363 66459 rl = &ff_rl_intra_aic;
364 }
365 }
366
367 /* AC coefs */
368 2388636 last_index = s->block_last_index[n];
369 2388636 last_non_zero = i - 1;
370
2/2
✓ Branch 0 taken 37329308 times.
✓ Branch 1 taken 2388636 times.
39717944 for (; i <= last_index; i++) {
371 37329308 j = s->intra_scantable.permutated[i];
372 37329308 level = block[j];
373
2/2
✓ Branch 0 taken 11712914 times.
✓ Branch 1 taken 25616394 times.
37329308 if (level) {
374 11712914 run = i - last_non_zero - 1;
375 11712914 last = (i == last_index);
376 11712914 sign = 0;
377 11712914 slevel = level;
378
2/2
✓ Branch 0 taken 5838423 times.
✓ Branch 1 taken 5874491 times.
11712914 if (level < 0) {
379 5838423 sign = 1;
380 5838423 level = -level;
381 }
382 11712914 code = get_rl_index(rl, last, run, level);
383 11712914 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
384
2/2
✓ Branch 0 taken 590306 times.
✓ Branch 1 taken 11122608 times.
11712914 if (code == rl->n) {
385
2/2
✓ Branch 0 taken 402806 times.
✓ Branch 1 taken 187500 times.
590306 if(!CONFIG_FLV_ENCODER || s->h263_flv <= 1){
386 402806 put_bits(&s->pb, 1, last);
387 402806 put_bits(&s->pb, 6, run);
388
389 av_assert2(slevel != 0);
390
391
2/2
✓ Branch 0 taken 400394 times.
✓ Branch 1 taken 2412 times.
402806 if(level < 128)
392 400394 put_sbits(&s->pb, 8, slevel);
393 else{
394 2412 put_bits(&s->pb, 8, 128);
395 2412 put_sbits(&s->pb, 5, slevel);
396 2412 put_sbits(&s->pb, 6, slevel>>5);
397 }
398 }else{
399 187500 ff_flv2_encode_ac_esc(&s->pb, slevel, level, run, last);
400 }
401 } else {
402 11122608 put_bits(&s->pb, 1, sign);
403 }
404 11712914 last_non_zero = i;
405 }
406 }
407 2388636 }
408
409 /* Encode MV differences on H.263+ with Unrestricted MV mode */
410 103298 static void h263p_encode_umotion(PutBitContext *pb, int val)
411 {
412 103298 short sval = 0;
413 103298 short i = 0;
414 103298 short n_bits = 0;
415 short temp_val;
416 103298 int code = 0;
417 int tcode;
418
419
2/2
✓ Branch 0 taken 51255 times.
✓ Branch 1 taken 52043 times.
103298 if ( val == 0)
420 51255 put_bits(pb, 1, 1);
421
2/2
✓ Branch 0 taken 12904 times.
✓ Branch 1 taken 39139 times.
52043 else if (val == 1)
422 12904 put_bits(pb, 3, 0);
423
2/2
✓ Branch 0 taken 20988 times.
✓ Branch 1 taken 18151 times.
39139 else if (val == -1)
424 20988 put_bits(pb, 3, 2);
425 else {
426
427
2/2
✓ Branch 0 taken 9429 times.
✓ Branch 1 taken 8722 times.
18151 sval = ((val < 0) ? (short)(-val):(short)val);
428 18151 temp_val = sval;
429
430
2/2
✓ Branch 0 taken 46910 times.
✓ Branch 1 taken 18151 times.
65061 while (temp_val != 0) {
431 46910 temp_val = temp_val >> 1;
432 46910 n_bits++;
433 }
434
435 18151 i = n_bits - 1;
436
2/2
✓ Branch 0 taken 28759 times.
✓ Branch 1 taken 18151 times.
46910 while (i > 0) {
437 28759 tcode = (sval & (1 << (i-1))) >> (i-1);
438 28759 tcode = (tcode << 1) | 1;
439 28759 code = (code << 2) | tcode;
440 28759 i--;
441 }
442 18151 code = ((code << 1) | (val < 0)) << 1;
443 18151 put_bits(pb, (2*n_bits)+1, code);
444 }
445 103298 }
446
447 82098 static int h263_pred_dc(MpegEncContext * s, int n, int16_t **dc_val_ptr)
448 {
449 int x, y, wrap, a, c, pred_dc;
450 int16_t *dc_val;
451
452 /* find prediction */
453
2/2
✓ Branch 0 taken 54732 times.
✓ Branch 1 taken 27366 times.
82098 if (n < 4) {
454 54732 x = 2 * s->mb_x + (n & 1);
455 54732 y = 2 * s->mb_y + ((n & 2) >> 1);
456 54732 wrap = s->b8_stride;
457 54732 dc_val = s->dc_val[0];
458 } else {
459 27366 x = s->mb_x;
460 27366 y = s->mb_y;
461 27366 wrap = s->mb_stride;
462 27366 dc_val = s->dc_val[n - 4 + 1];
463 }
464 /* B C
465 * A X
466 */
467 82098 a = dc_val[(x - 1) + (y) * wrap];
468 82098 c = dc_val[(x) + (y - 1) * wrap];
469
470 /* No prediction outside GOB boundary */
471
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) {
472
2/2
✓ Branch 0 taken 32292 times.
✓ Branch 1 taken 8073 times.
40365 if (n != 2) c = 1024;
473
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;
474 }
475 /* just DC prediction */
476
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)
477 47737 pred_dc = (a + c) >> 1;
478
2/2
✓ Branch 0 taken 29145 times.
✓ Branch 1 taken 5216 times.
34361 else if (a != 1024)
479 29145 pred_dc = a;
480 else
481 5216 pred_dc = c;
482
483 /* we assume pred is positive */
484 82098 *dc_val_ptr = &dc_val[x + y * wrap];
485 82098 return pred_dc;
486 }
487
488 428550 void ff_h263_encode_mb(MpegEncContext * s,
489 int16_t block[6][64],
490 int motion_x, int motion_y)
491 {
492 int cbpc, cbpy, i, cbp, pred_x, pred_y;
493 int16_t pred_dc;
494 int16_t rec_intradc[6];
495 int16_t *dc_ptr[6];
496 428550 const int interleaved_stats = s->avctx->flags & AV_CODEC_FLAG_PASS1;
497
498
2/2
✓ Branch 0 taken 370418 times.
✓ Branch 1 taken 58132 times.
428550 if (!s->mb_intra) {
499 /* compute cbp */
500 370418 cbp= get_p_cbp(s, block, motion_x, motion_y);
501
502
2/2
✓ Branch 0 taken 30444 times.
✓ Branch 1 taken 339974 times.
370418 if ((cbp | motion_x | motion_y | s->dquant | (s->mv_type - MV_TYPE_16X16)) == 0) {
503 /* skip macroblock */
504 30444 put_bits(&s->pb, 1, 1);
505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30444 times.
30444 if(interleaved_stats){
506 s->misc_bits++;
507 s->last_bits++;
508 }
509 30444 s->skip_count++;
510
511 30444 return;
512 }
513 339974 put_bits(&s->pb, 1, 0); /* mb coded */
514
515 339974 cbpc = cbp & 3;
516 339974 cbpy = cbp >> 2;
517
4/4
✓ Branch 0 taken 51649 times.
✓ Branch 1 taken 288325 times.
✓ Branch 2 taken 13805 times.
✓ Branch 3 taken 37844 times.
339974 if(s->alt_inter_vlc==0 || cbpc!=3)
518 302130 cbpy ^= 0xF;
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 339974 times.
339974 if(s->dquant) cbpc+= 8;
520
1/2
✓ Branch 0 taken 339974 times.
✗ Branch 1 not taken.
339974 if(s->mv_type==MV_TYPE_16X16){
521 339974 put_bits(&s->pb,
522 339974 ff_h263_inter_MCBPC_bits[cbpc],
523 339974 ff_h263_inter_MCBPC_code[cbpc]);
524
525 339974 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
526
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 339974 times.
339974 if(s->dquant)
527 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
528
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 339974 times.
339974 if(interleaved_stats){
530 s->misc_bits+= get_bits_diff(s);
531 }
532
533 /* motion vectors: 16x16 mode */
534 339974 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
535
536
2/2
✓ Branch 0 taken 288325 times.
✓ Branch 1 taken 51649 times.
339974 if (!s->umvplus) {
537 288325 ff_h263_encode_motion_vector(s, motion_x - pred_x,
538 motion_y - pred_y, 1);
539 }
540 else {
541 51649 h263p_encode_umotion(&s->pb, motion_x - pred_x);
542 51649 h263p_encode_umotion(&s->pb, motion_y - pred_y);
543
4/4
✓ Branch 0 taken 10131 times.
✓ Branch 1 taken 41518 times.
✓ Branch 2 taken 340 times.
✓ Branch 3 taken 9791 times.
51649 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
544 /* To prevent Start Code emulation */
545 340 put_bits(&s->pb,1,1);
546 }
547 }else{
548 put_bits(&s->pb,
549 ff_h263_inter_MCBPC_bits[cbpc+16],
550 ff_h263_inter_MCBPC_code[cbpc+16]);
551 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
552 if(s->dquant)
553 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
554
555 if(interleaved_stats){
556 s->misc_bits+= get_bits_diff(s);
557 }
558
559 for(i=0; i<4; i++){
560 /* motion vectors: 8x8 mode*/
561 ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
562
563 motion_x = s->current_picture.motion_val[0][s->block_index[i]][0];
564 motion_y = s->current_picture.motion_val[0][s->block_index[i]][1];
565 if (!s->umvplus) {
566 ff_h263_encode_motion_vector(s, motion_x - pred_x,
567 motion_y - pred_y, 1);
568 }
569 else {
570 h263p_encode_umotion(&s->pb, motion_x - pred_x);
571 h263p_encode_umotion(&s->pb, motion_y - pred_y);
572 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
573 /* To prevent Start Code emulation */
574 put_bits(&s->pb,1,1);
575 }
576 }
577 }
578
579
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 339974 times.
339974 if(interleaved_stats){
580 s->mv_bits+= get_bits_diff(s);
581 }
582 } else {
583 av_assert2(s->mb_intra);
584
585 58132 cbp = 0;
586
2/2
✓ Branch 0 taken 13683 times.
✓ Branch 1 taken 44449 times.
58132 if (s->h263_aic) {
587 /* Predict DC */
588
2/2
✓ Branch 0 taken 82098 times.
✓ Branch 1 taken 13683 times.
95781 for(i=0; i<6; i++) {
589 82098 int16_t level = block[i][0];
590 int scale;
591
592
2/2
✓ Branch 0 taken 54732 times.
✓ Branch 1 taken 27366 times.
82098 if(i<4) scale= s->y_dc_scale;
593 27366 else scale= s->c_dc_scale;
594
595 82098 pred_dc = h263_pred_dc(s, i, &dc_ptr[i]);
596 82098 level -= pred_dc;
597 /* Quant */
598
2/2
✓ Branch 0 taken 43407 times.
✓ Branch 1 taken 38691 times.
82098 if (level >= 0)
599 43407 level = (level + (scale>>1))/scale;
600 else
601 38691 level = (level - (scale>>1))/scale;
602
603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82098 times.
82098 if(!s->modified_quant){
604 if (level < -127)
605 level = -127;
606 else if (level > 127)
607 level = 127;
608 }
609
610 82098 block[i][0] = level;
611 /* Reconstruction */
612 82098 rec_intradc[i] = scale*level + pred_dc;
613 /* Oddify */
614 82098 rec_intradc[i] |= 1;
615 //if ((rec_intradc[i] % 2) == 0)
616 // rec_intradc[i]++;
617 /* Clipping */
618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82098 times.
82098 if (rec_intradc[i] < 0)
619 rec_intradc[i] = 0;
620
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82098 times.
82098 else if (rec_intradc[i] > 2047)
621 rec_intradc[i] = 2047;
622
623 /* Update AC/DC tables */
624 82098 *dc_ptr[i] = rec_intradc[i];
625 /* AIC can change CBP */
626
2/2
✓ Branch 0 taken 6224 times.
✓ Branch 1 taken 75874 times.
82098 if (s->block_last_index[i] > 0 ||
627
3/4
✓ Branch 0 taken 6224 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4973 times.
✓ Branch 3 taken 1251 times.
6224 (s->block_last_index[i] == 0 && level !=0))
628 80847 cbp |= 1 << (5 - i);
629 }
630 }else{
631
2/2
✓ Branch 0 taken 266694 times.
✓ Branch 1 taken 44449 times.
311143 for(i=0; i<6; i++) {
632 /* compute cbp */
633
2/2
✓ Branch 0 taken 206994 times.
✓ Branch 1 taken 59700 times.
266694 if (s->block_last_index[i] >= 1)
634 206994 cbp |= 1 << (5 - i);
635 }
636 }
637
638 58132 cbpc = cbp & 3;
639
2/2
✓ Branch 0 taken 45249 times.
✓ Branch 1 taken 12883 times.
58132 if (s->pict_type == AV_PICTURE_TYPE_I) {
640
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45249 times.
45249 if(s->dquant) cbpc+=4;
641 45249 put_bits(&s->pb,
642 45249 ff_h263_intra_MCBPC_bits[cbpc],
643 45249 ff_h263_intra_MCBPC_code[cbpc]);
644 } else {
645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12883 times.
12883 if(s->dquant) cbpc+=8;
646 12883 put_bits(&s->pb, 1, 0); /* mb coded */
647 12883 put_bits(&s->pb,
648 12883 ff_h263_inter_MCBPC_bits[cbpc + 4],
649 12883 ff_h263_inter_MCBPC_code[cbpc + 4]);
650 }
651
2/2
✓ Branch 0 taken 13683 times.
✓ Branch 1 taken 44449 times.
58132 if (s->h263_aic) {
652 /* XXX: currently, we do not try to use ac prediction */
653 13683 put_bits(&s->pb, 1, 0); /* no AC prediction */
654 }
655 58132 cbpy = cbp >> 2;
656 58132 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
657
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58132 times.
58132 if(s->dquant)
658 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
659
660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58132 times.
58132 if(interleaved_stats){
661 s->misc_bits+= get_bits_diff(s);
662 }
663 }
664
665
2/2
✓ Branch 0 taken 2388636 times.
✓ Branch 1 taken 398106 times.
2786742 for(i=0; i<6; i++) {
666 /* encode each block */
667 2388636 h263_encode_block(s, block[i], i);
668
669 /* Update INTRADC for decoding */
670
4/4
✓ Branch 0 taken 391992 times.
✓ Branch 1 taken 1996644 times.
✓ Branch 2 taken 82098 times.
✓ Branch 3 taken 309894 times.
2388636 if (s->h263_aic && s->mb_intra) {
671 82098 block[i][0] = rec_intradc[i];
672
673 }
674 }
675
676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 398106 times.
398106 if(interleaved_stats){
677 if (!s->mb_intra) {
678 s->p_tex_bits+= get_bits_diff(s);
679 s->f_count++;
680 }else{
681 s->i_tex_bits+= get_bits_diff(s);
682 s->i_count++;
683 }
684 }
685 }
686
687 5384436 void ff_h263_encode_motion(PutBitContext *pb, int val, int f_code)
688 {
689 int range, bit_size, sign, code, bits;
690
691
2/2
✓ Branch 0 taken 2254783 times.
✓ Branch 1 taken 3129653 times.
5384436 if (val == 0) {
692 /* zero vector */
693 2254783 code = 0;
694 2254783 put_bits(pb, ff_mvtab[code][1], ff_mvtab[code][0]);
695 } else {
696 3129653 bit_size = f_code - 1;
697 3129653 range = 1 << bit_size;
698 /* modulo encoding */
699 3129653 val = sign_extend(val, 6 + bit_size);
700 3129653 sign = val>>31;
701 3129653 val= (val^sign)-sign;
702 3129653 sign&=1;
703
704 3129653 val--;
705 3129653 code = (val >> bit_size) + 1;
706 3129653 bits = val & (range - 1);
707
708 3129653 put_bits(pb, ff_mvtab[code][1] + 1, (ff_mvtab[code][0] << 1) | sign);
709
2/2
✓ Branch 0 taken 434215 times.
✓ Branch 1 taken 2695438 times.
3129653 if (bit_size > 0) {
710 434215 put_bits(pb, bit_size, bits);
711 }
712 }
713 5384436 }
714
715 113 static av_cold void init_mv_penalty_and_fcode(void)
716 {
717 int f_code;
718 int mv;
719
720
2/2
✓ Branch 0 taken 791 times.
✓ Branch 1 taken 113 times.
904 for(f_code=1; f_code<=MAX_FCODE; f_code++){
721
2/2
✓ Branch 0 taken 12960535 times.
✓ Branch 1 taken 791 times.
12961326 for(mv=-MAX_DMV; mv<=MAX_DMV; mv++){
722 int len;
723
724
2/2
✓ Branch 0 taken 791 times.
✓ Branch 1 taken 12959744 times.
12960535 if(mv==0) len= ff_mvtab[0][1];
725 else{
726 int val, bit_size, code;
727
728 12959744 bit_size = f_code - 1;
729
730 12959744 val=mv;
731
2/2
✓ Branch 0 taken 6479872 times.
✓ Branch 1 taken 6479872 times.
12959744 if (val < 0)
732 6479872 val = -val;
733 12959744 val--;
734 12959744 code = (val >> bit_size) + 1;
735
2/2
✓ Branch 0 taken 918464 times.
✓ Branch 1 taken 12041280 times.
12959744 if(code<33){
736 918464 len= ff_mvtab[code][1] + 1 + bit_size;
737 }else{
738 12041280 len= ff_mvtab[32][1] + av_log2(code>>5) + 2 + bit_size;
739 }
740 }
741
742 12960535 mv_penalty[f_code][mv+MAX_DMV]= len;
743 }
744 }
745
746
2/2
✓ Branch 0 taken 791 times.
✓ Branch 1 taken 113 times.
904 for(f_code=MAX_FCODE; f_code>0; f_code--){
747
2/2
✓ Branch 0 taken 918464 times.
✓ Branch 1 taken 791 times.
919255 for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
748 918464 fcode_tab[mv+MAX_MV]= f_code;
749 }
750 }
751
752
2/2
✓ Branch 0 taken 925809 times.
✓ Branch 1 taken 113 times.
925922 for(mv=0; mv<MAX_MV*2+1; mv++){
753 925809 umv_fcode_tab[mv]= 1;
754 }
755 113 }
756
757 226 static av_cold void init_uni_h263_rl_tab(const RLTable *rl, uint8_t *len_tab)
758 {
759 int slevel, run, last;
760
761 av_assert0(MAX_LEVEL >= 64);
762 av_assert0(MAX_RUN >= 63);
763
764
2/2
✓ Branch 0 taken 28928 times.
✓ Branch 1 taken 226 times.
29154 for(slevel=-64; slevel<64; slevel++){
765
2/2
✓ Branch 0 taken 226 times.
✓ Branch 1 taken 28702 times.
28928 if(slevel==0) continue;
766
2/2
✓ Branch 0 taken 1836928 times.
✓ Branch 1 taken 28702 times.
1865630 for(run=0; run<64; run++){
767
2/2
✓ Branch 0 taken 3673856 times.
✓ Branch 1 taken 1836928 times.
5510784 for(last=0; last<=1; last++){
768 3673856 const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
769 3673856 int level= slevel < 0 ? -slevel : slevel;
770 3673856 int sign= slevel < 0 ? 1 : 0;
771 int bits, len, code;
772
773 3673856 len_tab[index]= 100;
774
775 /* ESC0 */
776 3673856 code= get_rl_index(rl, last, run, level);
777 3673856 bits= rl->table_vlc[code][0];
778 3673856 len= rl->table_vlc[code][1];
779 3673856 bits=bits*2+sign; len++;
780
781
3/4
✓ Branch 0 taken 46104 times.
✓ Branch 1 taken 3627752 times.
✓ Branch 2 taken 46104 times.
✗ Branch 3 not taken.
3673856 if (code != rl->n && len < len_tab[index])
782 46104 len_tab [index]= len;
783
784 /* ESC */
785 3673856 bits= rl->table_vlc[rl->n][0];
786 3673856 len = rl->table_vlc[rl->n][1];
787 3673856 bits=bits*2+last; len++;
788 3673856 bits=bits*64+run; len+=6;
789 3673856 bits=bits*256+(level&0xff); len+=8;
790
791
2/2
✓ Branch 0 taken 3627752 times.
✓ Branch 1 taken 46104 times.
3673856 if (len < len_tab[index])
792 3627752 len_tab [index]= len;
793 }
794 }
795 }
796 226 }
797
798 113 static av_cold void h263_encode_init_static(void)
799 {
800 static uint8_t rl_intra_table[2][2 * MAX_RUN + MAX_LEVEL + 3];
801
802 113 ff_rl_init(&ff_rl_intra_aic, rl_intra_table);
803 113 ff_h263_init_rl_inter();
804
805 113 init_uni_h263_rl_tab(&ff_rl_intra_aic, uni_h263_intra_aic_rl_len);
806 113 init_uni_h263_rl_tab(&ff_h263_rl_inter, uni_h263_inter_rl_len);
807
808 113 init_mv_penalty_and_fcode();
809 113 }
810
811 113 av_cold void ff_h263_encode_init(MpegEncContext *s)
812 {
813 static AVOnce init_static_once = AV_ONCE_INIT;
814
815 113 s->me.mv_penalty= mv_penalty; // FIXME exact table for MSMPEG4 & H.263+
816
817 113 s->intra_ac_vlc_length =s->inter_ac_vlc_length = uni_h263_inter_rl_len;
818 113 s->intra_ac_vlc_last_length=s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64;
819
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 107 times.
113 if(s->h263_aic){
820 6 s->intra_ac_vlc_length = uni_h263_intra_aic_rl_len;
821 6 s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64;
822 }
823 113 s->ac_esc_length= 7+1+6+8;
824
825 // use fcodes >1 only for MPEG-4 & H.263 & H.263+ FIXME
826
4/4
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 43 times.
113 switch(s->codec_id){
827 60 case AV_CODEC_ID_MPEG4:
828 60 s->fcode_tab= fcode_tab;
829 60 break;
830 3 case AV_CODEC_ID_H263P:
831
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if(s->umvplus)
832 3 s->fcode_tab= umv_fcode_tab;
833
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if(s->modified_quant){
834 3 s->min_qcoeff= -2047;
835 3 s->max_qcoeff= 2047;
836 }else{
837 s->min_qcoeff= -127;
838 s->max_qcoeff= 127;
839 }
840 3 break;
841 // Note for MPEG-4 & H.263 the dc-scale table will be set per frame as needed later
842 7 case AV_CODEC_ID_FLV1:
843
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (s->h263_flv > 1) {
844 7 s->min_qcoeff= -1023;
845 7 s->max_qcoeff= 1023;
846 } else {
847 s->min_qcoeff= -127;
848 s->max_qcoeff= 127;
849 }
850 7 break;
851 43 default: //nothing needed - default table already set in mpegvideo.c
852 43 s->min_qcoeff= -127;
853 43 s->max_qcoeff= 127;
854 }
855
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 107 times.
113 if(s->h263_aic){
856 6 s->y_dc_scale_table=
857 6 s->c_dc_scale_table= ff_aic_dc_scale_table;
858 }else{
859 107 s->y_dc_scale_table=
860 107 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
861 }
862
863 113 ff_thread_once(&init_static_once, h263_encode_init_static);
864 113 }
865
866 150 void ff_h263_encode_mba(MpegEncContext *s)
867 {
868 int i, mb_pos;
869
870
1/2
✓ Branch 0 taken 450 times.
✗ Branch 1 not taken.
450 for(i=0; i<6; i++){
871
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 300 times.
450 if(s->mb_num-1 <= ff_mba_max[i]) break;
872 }
873 150 mb_pos= s->mb_x + s->mb_width*s->mb_y;
874 150 put_bits(&s->pb, ff_mba_length[i], mb_pos);
875 150 }
876