FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ituh263enc.c
Date: 2021-09-26 18:22:30
Exec Total Coverage
Lines: 379 444 85.4%
Branches: 197 254 77.6%

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