Mercurial > libavcodec.hg
comparison ituh263enc.c @ 10828:d0657e337f91 libavcodec
Split H263 encoder and decoder from common code.
author | michael |
---|---|
date | Sat, 09 Jan 2010 14:59:06 +0000 |
parents | |
children | f20726a6d538 |
comparison
equal
deleted
inserted
replaced
10827:3d011a01a6a0 | 10828:d0657e337f91 |
---|---|
1 /* | |
2 * ITU H263 bitstream encoder | |
3 * Copyright (c) 2000,2001 Fabrice Bellard | |
4 * H263+ 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 libavcodec/ituh263enc.c | |
27 * h263 encoder. | |
28 */ | |
29 | |
30 //#define DEBUG | |
31 #include <limits.h> | |
32 | |
33 #include "dsputil.h" | |
34 #include "avcodec.h" | |
35 #include "mpegvideo.h" | |
36 #include "h263.h" | |
37 #include "mathops.h" | |
38 #include "unary.h" | |
39 #include "flv.h" | |
40 #include "mpeg4video.h" | |
41 | |
42 //#undef NDEBUG | |
43 //#include <assert.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_MV*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 int h263_get_picture_format(int width, int height) | |
86 { | |
87 if (width == 128 && height == 96) | |
88 return 1; | |
89 else if (width == 176 && height == 144) | |
90 return 2; | |
91 else if (width == 352 && height == 288) | |
92 return 3; | |
93 else if (width == 704 && height == 576) | |
94 return 4; | |
95 else if (width == 1408 && height == 1152) | |
96 return 5; | |
97 else | |
98 return 7; | |
99 } | |
100 | |
101 /** | |
102 * Returns the 4 bit value that specifies the given aspect ratio. | |
103 * This may be one of the standard aspect ratios or it specifies | |
104 * that the aspect will be stored explicitly later. | |
105 */ | |
106 av_const int ff_h263_aspect_to_info(AVRational aspect){ | |
107 int i; | |
108 | |
109 if(aspect.num==0) aspect= (AVRational){1,1}; | |
110 | |
111 for(i=1; i<6; i++){ | |
112 if(av_cmp_q(ff_h263_pixel_aspect[i], aspect) == 0){ | |
113 return i; | |
114 } | |
115 } | |
116 | |
117 return FF_ASPECT_EXTENDED; | |
118 } | |
119 | |
120 void h263_encode_picture_header(MpegEncContext * s, int picture_number) | |
121 { | |
122 int format, coded_frame_rate, coded_frame_rate_base, i, temp_ref; | |
123 int best_clock_code=1; | |
124 int best_divisor=60; | |
125 int best_error= INT_MAX; | |
126 | |
127 if(s->h263_plus){ | |
128 for(i=0; i<2; i++){ | |
129 int div, error; | |
130 div= (s->avctx->time_base.num*1800000LL + 500LL*s->avctx->time_base.den) / ((1000LL+i)*s->avctx->time_base.den); | |
131 div= av_clip(div, 1, 127); | |
132 error= FFABS(s->avctx->time_base.num*1800000LL - (1000LL+i)*s->avctx->time_base.den*div); | |
133 if(error < best_error){ | |
134 best_error= error; | |
135 best_divisor= div; | |
136 best_clock_code= i; | |
137 } | |
138 } | |
139 } | |
140 s->custom_pcf= best_clock_code!=1 || best_divisor!=60; | |
141 coded_frame_rate= 1800000; | |
142 coded_frame_rate_base= (1000+best_clock_code)*best_divisor; | |
143 | |
144 align_put_bits(&s->pb); | |
145 | |
146 /* Update the pointer to last GOB */ | |
147 s->ptr_lastgob = put_bits_ptr(&s->pb); | |
148 put_bits(&s->pb, 22, 0x20); /* PSC */ | |
149 temp_ref= s->picture_number * (int64_t)coded_frame_rate * s->avctx->time_base.num / //FIXME use timestamp | |
150 (coded_frame_rate_base * (int64_t)s->avctx->time_base.den); | |
151 put_sbits(&s->pb, 8, temp_ref); /* TemporalReference */ | |
152 | |
153 put_bits(&s->pb, 1, 1); /* marker */ | |
154 put_bits(&s->pb, 1, 0); /* h263 id */ | |
155 put_bits(&s->pb, 1, 0); /* split screen off */ | |
156 put_bits(&s->pb, 1, 0); /* camera off */ | |
157 put_bits(&s->pb, 1, 0); /* freeze picture release off */ | |
158 | |
159 format = h263_get_picture_format(s->width, s->height); | |
160 if (!s->h263_plus) { | |
161 /* H.263v1 */ | |
162 put_bits(&s->pb, 3, format); | |
163 put_bits(&s->pb, 1, (s->pict_type == FF_P_TYPE)); | |
164 /* By now UMV IS DISABLED ON H.263v1, since the restrictions | |
165 of H.263v1 UMV implies to check the predicted MV after | |
166 calculation of the current MB to see if we're on the limits */ | |
167 put_bits(&s->pb, 1, 0); /* Unrestricted Motion Vector: off */ | |
168 put_bits(&s->pb, 1, 0); /* SAC: off */ | |
169 put_bits(&s->pb, 1, s->obmc); /* Advanced Prediction */ | |
170 put_bits(&s->pb, 1, 0); /* only I/P frames, no PB frame */ | |
171 put_bits(&s->pb, 5, s->qscale); | |
172 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */ | |
173 } else { | |
174 int ufep=1; | |
175 /* H.263v2 */ | |
176 /* H.263 Plus PTYPE */ | |
177 | |
178 put_bits(&s->pb, 3, 7); | |
179 put_bits(&s->pb,3,ufep); /* Update Full Extended PTYPE */ | |
180 if (format == 7) | |
181 put_bits(&s->pb,3,6); /* Custom Source Format */ | |
182 else | |
183 put_bits(&s->pb, 3, format); | |
184 | |
185 put_bits(&s->pb,1, s->custom_pcf); | |
186 put_bits(&s->pb,1, s->umvplus); /* Unrestricted Motion Vector */ | |
187 put_bits(&s->pb,1,0); /* SAC: off */ | |
188 put_bits(&s->pb,1,s->obmc); /* Advanced Prediction Mode */ | |
189 put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */ | |
190 put_bits(&s->pb,1,s->loop_filter); /* Deblocking Filter */ | |
191 put_bits(&s->pb,1,s->h263_slice_structured); /* Slice Structured */ | |
192 put_bits(&s->pb,1,0); /* Reference Picture Selection: off */ | |
193 put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */ | |
194 put_bits(&s->pb,1,s->alt_inter_vlc); /* Alternative Inter VLC */ | |
195 put_bits(&s->pb,1,s->modified_quant); /* Modified Quantization: */ | |
196 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */ | |
197 put_bits(&s->pb,3,0); /* Reserved */ | |
198 | |
199 put_bits(&s->pb, 3, s->pict_type == FF_P_TYPE); | |
200 | |
201 put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */ | |
202 put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */ | |
203 put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */ | |
204 put_bits(&s->pb,2,0); /* Reserved */ | |
205 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */ | |
206 | |
207 /* This should be here if PLUSPTYPE */ | |
208 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */ | |
209 | |
210 if (format == 7) { | |
211 /* Custom Picture Format (CPFMT) */ | |
212 s->aspect_ratio_info= ff_h263_aspect_to_info(s->avctx->sample_aspect_ratio); | |
213 | |
214 put_bits(&s->pb,4,s->aspect_ratio_info); | |
215 put_bits(&s->pb,9,(s->width >> 2) - 1); | |
216 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */ | |
217 put_bits(&s->pb,9,(s->height >> 2)); | |
218 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){ | |
219 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num); | |
220 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den); | |
221 } | |
222 } | |
223 if(s->custom_pcf){ | |
224 if(ufep){ | |
225 put_bits(&s->pb, 1, best_clock_code); | |
226 put_bits(&s->pb, 7, best_divisor); | |
227 } | |
228 put_sbits(&s->pb, 2, temp_ref>>8); | |
229 } | |
230 | |
231 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */ | |
232 if (s->umvplus) | |
233 // put_bits(&s->pb,1,1); /* Limited according tables of Annex D */ | |
234 //FIXME check actual requested range | |
235 put_bits(&s->pb,2,1); /* unlimited */ | |
236 if(s->h263_slice_structured) | |
237 put_bits(&s->pb,2,0); /* no weird submodes */ | |
238 | |
239 put_bits(&s->pb, 5, s->qscale); | |
240 } | |
241 | |
242 put_bits(&s->pb, 1, 0); /* no PEI */ | |
243 | |
244 if(s->h263_slice_structured){ | |
245 put_bits(&s->pb, 1, 1); | |
246 | |
247 assert(s->mb_x == 0 && s->mb_y == 0); | |
248 ff_h263_encode_mba(s); | |
249 | |
250 put_bits(&s->pb, 1, 1); | |
251 } | |
252 | |
253 if(s->h263_aic){ | |
254 s->y_dc_scale_table= | |
255 s->c_dc_scale_table= ff_aic_dc_scale_table; | |
256 }else{ | |
257 s->y_dc_scale_table= | |
258 s->c_dc_scale_table= ff_mpeg1_dc_scale_table; | |
259 } | |
260 } | |
261 | |
262 /** | |
263 * Encodes a group of blocks header. | |
264 */ | |
265 void h263_encode_gob_header(MpegEncContext * s, int mb_line) | |
266 { | |
267 put_bits(&s->pb, 17, 1); /* GBSC */ | |
268 | |
269 if(s->h263_slice_structured){ | |
270 put_bits(&s->pb, 1, 1); | |
271 | |
272 ff_h263_encode_mba(s); | |
273 | |
274 if(s->mb_num > 1583) | |
275 put_bits(&s->pb, 1, 1); | |
276 put_bits(&s->pb, 5, s->qscale); /* GQUANT */ | |
277 put_bits(&s->pb, 1, 1); | |
278 put_bits(&s->pb, 2, s->pict_type == FF_I_TYPE); /* GFID */ | |
279 }else{ | |
280 int gob_number= mb_line / s->gob_index; | |
281 | |
282 put_bits(&s->pb, 5, gob_number); /* GN */ | |
283 put_bits(&s->pb, 2, s->pict_type == FF_I_TYPE); /* GFID */ | |
284 put_bits(&s->pb, 5, s->qscale); /* GQUANT */ | |
285 } | |
286 } | |
287 | |
288 /** | |
289 * modify qscale so that encoding is acually possible in h263 (limit difference to -2..2) | |
290 */ | |
291 void ff_clean_h263_qscales(MpegEncContext *s){ | |
292 int i; | |
293 int8_t * const qscale_table= s->current_picture.qscale_table; | |
294 | |
295 ff_init_qscale_tab(s); | |
296 | |
297 for(i=1; i<s->mb_num; i++){ | |
298 if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i-1] ] >2) | |
299 qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i-1] ]+2; | |
300 } | |
301 for(i=s->mb_num-2; i>=0; i--){ | |
302 if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i+1] ] >2) | |
303 qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i+1] ]+2; | |
304 } | |
305 | |
306 if(s->codec_id != CODEC_ID_H263P){ | |
307 for(i=1; i<s->mb_num; i++){ | |
308 int mb_xy= s->mb_index2xy[i]; | |
309 | |
310 if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTER4V)){ | |
311 s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER; | |
312 } | |
313 } | |
314 } | |
315 } | |
316 | |
317 static const int dquant_code[5]= {1,0,9,2,3}; | |
318 | |
319 /** | |
320 * encodes a 8x8 block. | |
321 * @param block the 8x8 block | |
322 * @param n block index (0-3 are luma, 4-5 are chroma) | |
323 */ | |
324 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n) | |
325 { | |
326 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code; | |
327 RLTable *rl; | |
328 | |
329 rl = &ff_h263_rl_inter; | |
330 if (s->mb_intra && !s->h263_aic) { | |
331 /* DC coef */ | |
332 level = block[0]; | |
333 /* 255 cannot be represented, so we clamp */ | |
334 if (level > 254) { | |
335 level = 254; | |
336 block[0] = 254; | |
337 } | |
338 /* 0 cannot be represented also */ | |
339 else if (level < 1) { | |
340 level = 1; | |
341 block[0] = 1; | |
342 } | |
343 if (level == 128) //FIXME check rv10 | |
344 put_bits(&s->pb, 8, 0xff); | |
345 else | |
346 put_bits(&s->pb, 8, level); | |
347 i = 1; | |
348 } else { | |
349 i = 0; | |
350 if (s->h263_aic && s->mb_intra) | |
351 rl = &rl_intra_aic; | |
352 | |
353 if(s->alt_inter_vlc && !s->mb_intra){ | |
354 int aic_vlc_bits=0; | |
355 int inter_vlc_bits=0; | |
356 int wrong_pos=-1; | |
357 int aic_code; | |
358 | |
359 last_index = s->block_last_index[n]; | |
360 last_non_zero = i - 1; | |
361 for (; i <= last_index; i++) { | |
362 j = s->intra_scantable.permutated[i]; | |
363 level = block[j]; | |
364 if (level) { | |
365 run = i - last_non_zero - 1; | |
366 last = (i == last_index); | |
367 | |
368 if(level<0) level= -level; | |
369 | |
370 code = get_rl_index(rl, last, run, level); | |
371 aic_code = get_rl_index(&rl_intra_aic, last, run, level); | |
372 inter_vlc_bits += rl->table_vlc[code][1]+1; | |
373 aic_vlc_bits += rl_intra_aic.table_vlc[aic_code][1]+1; | |
374 | |
375 if (code == rl->n) { | |
376 inter_vlc_bits += 1+6+8-1; | |
377 } | |
378 if (aic_code == rl_intra_aic.n) { | |
379 aic_vlc_bits += 1+6+8-1; | |
380 wrong_pos += run + 1; | |
381 }else | |
382 wrong_pos += wrong_run[aic_code]; | |
383 last_non_zero = i; | |
384 } | |
385 } | |
386 i = 0; | |
387 if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63) | |
388 rl = &rl_intra_aic; | |
389 } | |
390 } | |
391 | |
392 /* AC coefs */ | |
393 last_index = s->block_last_index[n]; | |
394 last_non_zero = i - 1; | |
395 for (; i <= last_index; i++) { | |
396 j = s->intra_scantable.permutated[i]; | |
397 level = block[j]; | |
398 if (level) { | |
399 run = i - last_non_zero - 1; | |
400 last = (i == last_index); | |
401 sign = 0; | |
402 slevel = level; | |
403 if (level < 0) { | |
404 sign = 1; | |
405 level = -level; | |
406 } | |
407 code = get_rl_index(rl, last, run, level); | |
408 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
409 if (code == rl->n) { | |
410 if(!CONFIG_FLV_ENCODER || s->h263_flv <= 1){ | |
411 put_bits(&s->pb, 1, last); | |
412 put_bits(&s->pb, 6, run); | |
413 | |
414 assert(slevel != 0); | |
415 | |
416 if(level < 128) | |
417 put_sbits(&s->pb, 8, slevel); | |
418 else{ | |
419 put_bits(&s->pb, 8, 128); | |
420 put_sbits(&s->pb, 5, slevel); | |
421 put_sbits(&s->pb, 6, slevel>>5); | |
422 } | |
423 }else{ | |
424 ff_flv2_encode_ac_esc(&s->pb, slevel, level, run, last); | |
425 } | |
426 } else { | |
427 put_bits(&s->pb, 1, sign); | |
428 } | |
429 last_non_zero = i; | |
430 } | |
431 } | |
432 } | |
433 | |
434 /* Encode MV differences on H.263+ with Unrestricted MV mode */ | |
435 static void h263p_encode_umotion(MpegEncContext * s, int val) | |
436 { | |
437 short sval = 0; | |
438 short i = 0; | |
439 short n_bits = 0; | |
440 short temp_val; | |
441 int code = 0; | |
442 int tcode; | |
443 | |
444 if ( val == 0) | |
445 put_bits(&s->pb, 1, 1); | |
446 else if (val == 1) | |
447 put_bits(&s->pb, 3, 0); | |
448 else if (val == -1) | |
449 put_bits(&s->pb, 3, 2); | |
450 else { | |
451 | |
452 sval = ((val < 0) ? (short)(-val):(short)val); | |
453 temp_val = sval; | |
454 | |
455 while (temp_val != 0) { | |
456 temp_val = temp_val >> 1; | |
457 n_bits++; | |
458 } | |
459 | |
460 i = n_bits - 1; | |
461 while (i > 0) { | |
462 tcode = (sval & (1 << (i-1))) >> (i-1); | |
463 tcode = (tcode << 1) | 1; | |
464 code = (code << 2) | tcode; | |
465 i--; | |
466 } | |
467 code = ((code << 1) | (val < 0)) << 1; | |
468 put_bits(&s->pb, (2*n_bits)+1, code); | |
469 } | |
470 } | |
471 | |
472 void h263_encode_mb(MpegEncContext * s, | |
473 DCTELEM block[6][64], | |
474 int motion_x, int motion_y) | |
475 { | |
476 int cbpc, cbpy, i, cbp, pred_x, pred_y; | |
477 int16_t pred_dc; | |
478 int16_t rec_intradc[6]; | |
479 int16_t *dc_ptr[6]; | |
480 const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1); | |
481 | |
482 if (!s->mb_intra) { | |
483 /* compute cbp */ | |
484 cbp= get_p_cbp(s, block, motion_x, motion_y); | |
485 | |
486 if ((cbp | motion_x | motion_y | s->dquant | (s->mv_type - MV_TYPE_16X16)) == 0) { | |
487 /* skip macroblock */ | |
488 put_bits(&s->pb, 1, 1); | |
489 if(interleaved_stats){ | |
490 s->misc_bits++; | |
491 s->last_bits++; | |
492 } | |
493 s->skip_count++; | |
494 | |
495 return; | |
496 } | |
497 put_bits(&s->pb, 1, 0); /* mb coded */ | |
498 | |
499 cbpc = cbp & 3; | |
500 cbpy = cbp >> 2; | |
501 if(s->alt_inter_vlc==0 || cbpc!=3) | |
502 cbpy ^= 0xF; | |
503 if(s->dquant) cbpc+= 8; | |
504 if(s->mv_type==MV_TYPE_16X16){ | |
505 put_bits(&s->pb, | |
506 ff_h263_inter_MCBPC_bits[cbpc], | |
507 ff_h263_inter_MCBPC_code[cbpc]); | |
508 | |
509 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]); | |
510 if(s->dquant) | |
511 put_bits(&s->pb, 2, dquant_code[s->dquant+2]); | |
512 | |
513 if(interleaved_stats){ | |
514 s->misc_bits+= get_bits_diff(s); | |
515 } | |
516 | |
517 /* motion vectors: 16x16 mode */ | |
518 h263_pred_motion(s, 0, 0, &pred_x, &pred_y); | |
519 | |
520 if (!s->umvplus) { | |
521 ff_h263_encode_motion_vector(s, motion_x - pred_x, | |
522 motion_y - pred_y, 1); | |
523 } | |
524 else { | |
525 h263p_encode_umotion(s, motion_x - pred_x); | |
526 h263p_encode_umotion(s, motion_y - pred_y); | |
527 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1)) | |
528 /* To prevent Start Code emulation */ | |
529 put_bits(&s->pb,1,1); | |
530 } | |
531 }else{ | |
532 put_bits(&s->pb, | |
533 ff_h263_inter_MCBPC_bits[cbpc+16], | |
534 ff_h263_inter_MCBPC_code[cbpc+16]); | |
535 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]); | |
536 if(s->dquant) | |
537 put_bits(&s->pb, 2, dquant_code[s->dquant+2]); | |
538 | |
539 if(interleaved_stats){ | |
540 s->misc_bits+= get_bits_diff(s); | |
541 } | |
542 | |
543 for(i=0; i<4; i++){ | |
544 /* motion vectors: 8x8 mode*/ | |
545 h263_pred_motion(s, i, 0, &pred_x, &pred_y); | |
546 | |
547 motion_x= s->current_picture.motion_val[0][ s->block_index[i] ][0]; | |
548 motion_y= s->current_picture.motion_val[0][ s->block_index[i] ][1]; | |
549 if (!s->umvplus) { | |
550 ff_h263_encode_motion_vector(s, motion_x - pred_x, | |
551 motion_y - pred_y, 1); | |
552 } | |
553 else { | |
554 h263p_encode_umotion(s, motion_x - pred_x); | |
555 h263p_encode_umotion(s, motion_y - pred_y); | |
556 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1)) | |
557 /* To prevent Start Code emulation */ | |
558 put_bits(&s->pb,1,1); | |
559 } | |
560 } | |
561 } | |
562 | |
563 if(interleaved_stats){ | |
564 s->mv_bits+= get_bits_diff(s); | |
565 } | |
566 } else { | |
567 assert(s->mb_intra); | |
568 | |
569 cbp = 0; | |
570 if (s->h263_aic) { | |
571 /* Predict DC */ | |
572 for(i=0; i<6; i++) { | |
573 int16_t level = block[i][0]; | |
574 int scale; | |
575 | |
576 if(i<4) scale= s->y_dc_scale; | |
577 else scale= s->c_dc_scale; | |
578 | |
579 pred_dc = h263_pred_dc(s, i, &dc_ptr[i]); | |
580 level -= pred_dc; | |
581 /* Quant */ | |
582 if (level >= 0) | |
583 level = (level + (scale>>1))/scale; | |
584 else | |
585 level = (level - (scale>>1))/scale; | |
586 | |
587 /* AIC can change CBP */ | |
588 if (level == 0 && s->block_last_index[i] == 0) | |
589 s->block_last_index[i] = -1; | |
590 | |
591 if(!s->modified_quant){ | |
592 if (level < -127) | |
593 level = -127; | |
594 else if (level > 127) | |
595 level = 127; | |
596 } | |
597 | |
598 block[i][0] = level; | |
599 /* Reconstruction */ | |
600 rec_intradc[i] = scale*level + pred_dc; | |
601 /* Oddify */ | |
602 rec_intradc[i] |= 1; | |
603 //if ((rec_intradc[i] % 2) == 0) | |
604 // rec_intradc[i]++; | |
605 /* Clipping */ | |
606 if (rec_intradc[i] < 0) | |
607 rec_intradc[i] = 0; | |
608 else if (rec_intradc[i] > 2047) | |
609 rec_intradc[i] = 2047; | |
610 | |
611 /* Update AC/DC tables */ | |
612 *dc_ptr[i] = rec_intradc[i]; | |
613 if (s->block_last_index[i] >= 0) | |
614 cbp |= 1 << (5 - i); | |
615 } | |
616 }else{ | |
617 for(i=0; i<6; i++) { | |
618 /* compute cbp */ | |
619 if (s->block_last_index[i] >= 1) | |
620 cbp |= 1 << (5 - i); | |
621 } | |
622 } | |
623 | |
624 cbpc = cbp & 3; | |
625 if (s->pict_type == FF_I_TYPE) { | |
626 if(s->dquant) cbpc+=4; | |
627 put_bits(&s->pb, | |
628 ff_h263_intra_MCBPC_bits[cbpc], | |
629 ff_h263_intra_MCBPC_code[cbpc]); | |
630 } else { | |
631 if(s->dquant) cbpc+=8; | |
632 put_bits(&s->pb, 1, 0); /* mb coded */ | |
633 put_bits(&s->pb, | |
634 ff_h263_inter_MCBPC_bits[cbpc + 4], | |
635 ff_h263_inter_MCBPC_code[cbpc + 4]); | |
636 } | |
637 if (s->h263_aic) { | |
638 /* XXX: currently, we do not try to use ac prediction */ | |
639 put_bits(&s->pb, 1, 0); /* no AC prediction */ | |
640 } | |
641 cbpy = cbp >> 2; | |
642 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]); | |
643 if(s->dquant) | |
644 put_bits(&s->pb, 2, dquant_code[s->dquant+2]); | |
645 | |
646 if(interleaved_stats){ | |
647 s->misc_bits+= get_bits_diff(s); | |
648 } | |
649 } | |
650 | |
651 for(i=0; i<6; i++) { | |
652 /* encode each block */ | |
653 h263_encode_block(s, block[i], i); | |
654 | |
655 /* Update INTRADC for decoding */ | |
656 if (s->h263_aic && s->mb_intra) { | |
657 block[i][0] = rec_intradc[i]; | |
658 | |
659 } | |
660 } | |
661 | |
662 if(interleaved_stats){ | |
663 if (!s->mb_intra) { | |
664 s->p_tex_bits+= get_bits_diff(s); | |
665 s->f_count++; | |
666 }else{ | |
667 s->i_tex_bits+= get_bits_diff(s); | |
668 s->i_count++; | |
669 } | |
670 } | |
671 } | |
672 | |
673 void ff_h263_encode_motion(MpegEncContext * s, int val, int f_code) | |
674 { | |
675 int range, l, bit_size, sign, code, bits; | |
676 | |
677 if (val == 0) { | |
678 /* zero vector */ | |
679 code = 0; | |
680 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]); | |
681 } else { | |
682 bit_size = f_code - 1; | |
683 range = 1 << bit_size; | |
684 /* modulo encoding */ | |
685 l= INT_BIT - 6 - bit_size; | |
686 val = (val<<l)>>l; | |
687 sign = val>>31; | |
688 val= (val^sign)-sign; | |
689 sign&=1; | |
690 | |
691 val--; | |
692 code = (val >> bit_size) + 1; | |
693 bits = val & (range - 1); | |
694 | |
695 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); | |
696 if (bit_size > 0) { | |
697 put_bits(&s->pb, bit_size, bits); | |
698 } | |
699 } | |
700 } | |
701 | |
702 static void init_mv_penalty_and_fcode(MpegEncContext *s) | |
703 { | |
704 int f_code; | |
705 int mv; | |
706 | |
707 for(f_code=1; f_code<=MAX_FCODE; f_code++){ | |
708 for(mv=-MAX_MV; mv<=MAX_MV; mv++){ | |
709 int len; | |
710 | |
711 if(mv==0) len= mvtab[0][1]; | |
712 else{ | |
713 int val, bit_size, code; | |
714 | |
715 bit_size = f_code - 1; | |
716 | |
717 val=mv; | |
718 if (val < 0) | |
719 val = -val; | |
720 val--; | |
721 code = (val >> bit_size) + 1; | |
722 if(code<33){ | |
723 len= mvtab[code][1] + 1 + bit_size; | |
724 }else{ | |
725 len= mvtab[32][1] + av_log2(code>>5) + 2 + bit_size; | |
726 } | |
727 } | |
728 | |
729 mv_penalty[f_code][mv+MAX_MV]= len; | |
730 } | |
731 } | |
732 | |
733 for(f_code=MAX_FCODE; f_code>0; f_code--){ | |
734 for(mv=-(16<<f_code); mv<(16<<f_code); mv++){ | |
735 fcode_tab[mv+MAX_MV]= f_code; | |
736 } | |
737 } | |
738 | |
739 for(mv=0; mv<MAX_MV*2+1; mv++){ | |
740 umv_fcode_tab[mv]= 1; | |
741 } | |
742 } | |
743 | |
744 static void init_uni_h263_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){ | |
745 int slevel, run, last; | |
746 | |
747 assert(MAX_LEVEL >= 64); | |
748 assert(MAX_RUN >= 63); | |
749 | |
750 for(slevel=-64; slevel<64; slevel++){ | |
751 if(slevel==0) continue; | |
752 for(run=0; run<64; run++){ | |
753 for(last=0; last<=1; last++){ | |
754 const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64); | |
755 int level= slevel < 0 ? -slevel : slevel; | |
756 int sign= slevel < 0 ? 1 : 0; | |
757 int bits, len, code; | |
758 | |
759 len_tab[index]= 100; | |
760 | |
761 /* ESC0 */ | |
762 code= get_rl_index(rl, last, run, level); | |
763 bits= rl->table_vlc[code][0]; | |
764 len= rl->table_vlc[code][1]; | |
765 bits=bits*2+sign; len++; | |
766 | |
767 if(code!=rl->n && len < len_tab[index]){ | |
768 if(bits_tab) bits_tab[index]= bits; | |
769 len_tab [index]= len; | |
770 } | |
771 /* ESC */ | |
772 bits= rl->table_vlc[rl->n][0]; | |
773 len = rl->table_vlc[rl->n][1]; | |
774 bits=bits*2+last; len++; | |
775 bits=bits*64+run; len+=6; | |
776 bits=bits*256+(level&0xff); len+=8; | |
777 | |
778 if(len < len_tab[index]){ | |
779 if(bits_tab) bits_tab[index]= bits; | |
780 len_tab [index]= len; | |
781 } | |
782 } | |
783 } | |
784 } | |
785 } | |
786 | |
787 void h263_encode_init(MpegEncContext *s) | |
788 { | |
789 static int done = 0; | |
790 | |
791 if (!done) { | |
792 done = 1; | |
793 | |
794 init_rl(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]); | |
795 init_rl(&rl_intra_aic, ff_h263_static_rl_table_store[1]); | |
796 | |
797 init_uni_h263_rl_tab(&rl_intra_aic, NULL, uni_h263_intra_aic_rl_len); | |
798 init_uni_h263_rl_tab(&ff_h263_rl_inter , NULL, uni_h263_inter_rl_len); | |
799 | |
800 init_mv_penalty_and_fcode(s); | |
801 } | |
802 s->me.mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p | |
803 | |
804 s->intra_ac_vlc_length =s->inter_ac_vlc_length = uni_h263_inter_rl_len; | |
805 s->intra_ac_vlc_last_length=s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64; | |
806 if(s->h263_aic){ | |
807 s->intra_ac_vlc_length = uni_h263_intra_aic_rl_len; | |
808 s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64; | |
809 } | |
810 s->ac_esc_length= 7+1+6+8; | |
811 | |
812 // use fcodes >1 only for mpeg4 & h263 & h263p FIXME | |
813 switch(s->codec_id){ | |
814 case CODEC_ID_MPEG4: | |
815 s->fcode_tab= fcode_tab; | |
816 break; | |
817 case CODEC_ID_H263P: | |
818 if(s->umvplus) | |
819 s->fcode_tab= umv_fcode_tab; | |
820 if(s->modified_quant){ | |
821 s->min_qcoeff= -2047; | |
822 s->max_qcoeff= 2047; | |
823 }else{ | |
824 s->min_qcoeff= -127; | |
825 s->max_qcoeff= 127; | |
826 } | |
827 break; | |
828 //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later | |
829 case CODEC_ID_FLV1: | |
830 if (s->h263_flv > 1) { | |
831 s->min_qcoeff= -1023; | |
832 s->max_qcoeff= 1023; | |
833 } else { | |
834 s->min_qcoeff= -127; | |
835 s->max_qcoeff= 127; | |
836 } | |
837 s->y_dc_scale_table= | |
838 s->c_dc_scale_table= ff_mpeg1_dc_scale_table; | |
839 break; | |
840 default: //nothing needed - default table already set in mpegvideo.c | |
841 s->min_qcoeff= -127; | |
842 s->max_qcoeff= 127; | |
843 s->y_dc_scale_table= | |
844 s->c_dc_scale_table= ff_mpeg1_dc_scale_table; | |
845 } | |
846 } | |
847 | |
848 void ff_h263_encode_mba(MpegEncContext *s) | |
849 { | |
850 int i, mb_pos; | |
851 | |
852 for(i=0; i<6; i++){ | |
853 if(s->mb_num-1 <= ff_mba_max[i]) break; | |
854 } | |
855 mb_pos= s->mb_x + s->mb_width*s->mb_y; | |
856 put_bits(&s->pb, ff_mba_length[i], mb_pos); | |
857 } |