FFmpeg
Loading...
Searching...
No Matches
ituh263enc.c
Go to the documentation of this file.
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 "config_components.h"
31
32#include <limits.h>
33#include <string.h>
34
36#include "libavutil/thread.h"
37#include "avcodec.h"
38#include "codec_internal.h"
39#include "mpegvideo.h"
40#include "flvenc.h"
41#include "mpegvideodata.h"
42#include "mpegvideoenc.h"
43#include "h263.h"
44#include "h263enc.h"
45#include "h263data.h"
46#include "h263dsp.h"
47#include "mathops.h"
48#include "mpegutils.h"
49#include "internal.h"
50#include "put_bits.h"
51
52/**
53 * Table of number of bits a motion vector component needs.
54 */
55static uint8_t mv_penalty[MAX_FCODE+1][MAX_DMV*2+1];
56
57static av_cold void init_mv_penalty(void)
58{
59 for (int f_code = 1; f_code <= MAX_FCODE; f_code++) {
60 for (int mv = -MAX_DMV; mv <= MAX_DMV; mv++) {
61 int len;
62
63 if (mv == 0) len = 1; // ff_mvtab[0][1]
64 else {
65 int val, bit_size, code;
66
67 bit_size = f_code - 1;
68
69 val = mv;
70 if (val < 0)
71 val = -val;
72 val--;
73 code = (val >> bit_size) + 1;
74 if (code < 33) {
75 len = ff_mvtab[code][1] + 1 + bit_size;
76 } else {
77 len = 12 /* ff_mvtab[32][1] */ + av_log2(code>>5) + 2 + bit_size;
78 }
79 }
80
81 mv_penalty[f_code][mv + MAX_DMV] = len;
82 }
83 }
84}
85
86#if CONFIG_H263_ENCODER
87/**
88 * Minimal fcode that a motion vector component would need in umv.
89 * All entries in this table are 1.
90 */
91static uint8_t umv_fcode_tab[MAX_MV*2+1];
92
93//unified encoding tables for run length encoding of coefficients
94//unified in the sense that the specification specifies the encoding in several steps.
95static uint8_t uni_h263_intra_aic_rl_len [64*64*2*2];
96static uint8_t uni_h263_inter_rl_len [64*64*2*2];
97//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
98//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
99#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
100
101static av_cold void init_uni_h263_rl_tab(const RLTable *rl, uint8_t *len_tab)
102{
103 const uint16_t (*table_vlc)[2] = rl->table_vlc;
104 const uint8_t *table_run = rl->table_run;
105 const uint8_t *table_level = rl->table_level;
106
107 av_assert0(MAX_LEVEL >= 64);
108 av_assert0(MAX_RUN >= 63);
109
110 // Note: The LUT only covers level values for which the escape value
111 // is eight bits (not 8 + 5 + 6)
112 memset(len_tab, H263_ESCAPE_CODE_LENGTH + 1 + 6 + 8,
113 sizeof(uni_h263_intra_aic_rl_len));
114
115 len_tab += 64; // simplifies addressing
116 for (int i = 0; i < H263_RL_NB_ELEMS; ++i) {
117 int run = table_run[i];
118 int level = table_level[i];
119 int last = i >= H263_RL_NON_LAST_CODES;
120 int len = table_vlc[i][1];
121
122 len_tab[UNI_MPEG4_ENC_INDEX(last, run, level)] =
123 len_tab[UNI_MPEG4_ENC_INDEX(last, run, -level)] = len + 1 /* sign */;
124 }
125 for (int run = 0; run < MAX_RUN; ++run) {
126 len_tab[UNI_MPEG4_ENC_INDEX(0, run, 0)] =
127 len_tab[UNI_MPEG4_ENC_INDEX(1, run, 0)] = 0; // is this necessary?
128 }
129}
130#endif
131
133{
134#if CONFIG_H263_ENCODER
135 static uint8_t rl_intra_table[2][2 * MAX_RUN + MAX_LEVEL + 3];
136 ff_rl_init(&ff_rl_intra_aic, rl_intra_table);
138
139 init_uni_h263_rl_tab(&ff_rl_intra_aic, uni_h263_intra_aic_rl_len);
140 init_uni_h263_rl_tab(&ff_h263_rl_inter, uni_h263_inter_rl_len);
141
142 memset(umv_fcode_tab, 1, sizeof(umv_fcode_tab));
143#endif
144
146}
147
148av_cold const uint8_t (*ff_h263_get_mv_penalty(void))[MAX_DMV*2+1]
149{
150 static AVOnce init_static_once = AV_ONCE_INIT;
151
152 ff_thread_once(&init_static_once, h263_encode_init_static);
153
154 return mv_penalty;
155}
156
157void ff_h263_encode_motion(PutBitContext *pb, int val, int f_code)
158{
159 if (val == 0) {
160 /* zero vector -- corresponds to ff_mvtab[0] */
161 put_bits(pb, 1, 1);
162 } else {
163 int sign, code, bits;
164 int bit_size = f_code - 1;
165 int range = 1 << bit_size;
166 /* modulo encoding */
167 val = sign_extend(val, 6 + bit_size);
168 sign = val>>31;
169 val= (val^sign)-sign;
170 sign&=1;
171
172 val--;
173 code = (val >> bit_size) + 1;
174 bits = val & (range - 1);
175
176 put_bits(pb, ff_mvtab[code][1] + 1, (ff_mvtab[code][0] << 1) | sign);
177 if (bit_size > 0) {
178 put_bits(pb, bit_size, bits);
179 }
180 }
181}
182
183#if CONFIG_H263_ENCODER // Snow and SVQ1 need the above
184static const uint8_t wrong_run[102] = {
185 1, 2, 3, 5, 4, 10, 9, 8,
18611, 15, 17, 16, 23, 22, 21, 20,
18719, 18, 25, 24, 27, 26, 11, 7,
188 6, 1, 2, 13, 2, 2, 2, 2,
189 6, 12, 3, 9, 1, 3, 4, 3,
190 7, 4, 1, 1, 5, 5, 14, 6,
191 1, 7, 1, 8, 1, 1, 1, 1,
19210, 1, 1, 5, 9, 17, 25, 24,
19329, 33, 32, 41, 2, 23, 28, 31,
194 3, 22, 30, 4, 27, 40, 8, 26,
195 6, 39, 7, 38, 16, 37, 15, 10,
19611, 12, 13, 14, 1, 21, 20, 18,
19719, 2, 1, 34, 35, 36
198};
199
200/**
201 * Return the 4 bit value that specifies the given aspect ratio.
202 * This may be one of the standard aspect ratios or it specifies
203 * that the aspect will be stored explicitly later.
204 */
206 int i;
207
208 if(aspect.num==0 || aspect.den==0) aspect= (AVRational){1,1};
209
210 for(i=1; i<6; i++){
211 if(av_cmp_q(ff_h263_pixel_aspect[i], aspect) == 0){
212 return i;
213 }
214 }
215
216 return FF_ASPECT_EXTENDED;
217}
218
219static int h263_encode_picture_header(MPVMainEncContext *const m)
220{
221 MPVEncContext *const s = &m->s;
222 int format, coded_frame_rate, coded_frame_rate_base, i, temp_ref;
223 int best_clock_code=1;
224 int best_divisor=60;
225 int best_error= INT_MAX;
226 int custom_pcf;
227
229
230 if (s->c.codec_id == AV_CODEC_ID_H263P) {
231 for(i=0; i<2; i++){
232 int div, error;
233 div= (s->c.avctx->time_base.num*1800000LL + 500LL*s->c.avctx->time_base.den) / ((1000LL+i)*s->c.avctx->time_base.den);
234 div= av_clip(div, 1, 127);
235 error= FFABS(s->c.avctx->time_base.num*1800000LL - (1000LL+i)*s->c.avctx->time_base.den*div);
236 if(error < best_error){
237 best_error= error;
238 best_divisor= div;
239 best_clock_code= i;
240 }
241 }
242 }
243 custom_pcf = best_clock_code != 1 || best_divisor != 60;
244 coded_frame_rate= 1800000;
245 coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
246
247 put_bits(&s->pb, 22, 0x20); /* PSC */
248 temp_ref = s->picture_number * (int64_t)coded_frame_rate * s->c.avctx->time_base.num / //FIXME use timestamp
249 (coded_frame_rate_base * (int64_t)s->c.avctx->time_base.den);
250 put_sbits(&s->pb, 8, temp_ref); /* TemporalReference */
251
252 put_bits(&s->pb, 1, 1); /* marker */
253 put_bits(&s->pb, 1, 0); /* H.263 id */
254 put_bits(&s->pb, 1, 0); /* split screen off */
255 put_bits(&s->pb, 1, 0); /* camera off */
256 put_bits(&s->pb, 1, 0); /* freeze picture release off */
257
259 if (s->c.codec_id != AV_CODEC_ID_H263P) {
260 /* H.263v1 */
261 put_bits(&s->pb, 3, format);
262 put_bits(&s->pb, 1, (s->c.pict_type == AV_PICTURE_TYPE_P));
263 /* By now UMV IS DISABLED ON H.263v1, since the restrictions
264 of H.263v1 UMV implies to check the predicted MV after
265 calculation of the current MB to see if we're on the limits */
266 put_bits(&s->pb, 1, 0); /* Unrestricted Motion Vector: off */
267 put_bits(&s->pb, 1, 0); /* SAC: off */
268 put_bits(&s->pb, 1, s->c.obmc); /* Advanced Prediction */
269 put_bits(&s->pb, 1, 0); /* only I/P-frames, no PB-frame */
270 put_bits(&s->pb, 5, s->c.qscale);
271 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
272 } else {
273 int ufep=1;
274 /* H.263v2 */
275 /* H.263 Plus PTYPE */
276
277 put_bits(&s->pb, 3, 7);
278 put_bits(&s->pb,3,ufep); /* Update Full Extended PTYPE */
279 if (format == 8)
280 put_bits(&s->pb,3,6); /* Custom Source Format */
281 else
282 put_bits(&s->pb, 3, format);
283
284 put_bits(&s->pb,1, custom_pcf);
285 put_bits(&s->pb,1, s->umvplus); /* Unrestricted Motion Vector */
286 put_bits(&s->pb,1,0); /* SAC: off */
287 put_bits(&s->pb,1,s->c.obmc); /* Advanced Prediction Mode */
288 put_bits(&s->pb,1,s->c.h263_aic); /* Advanced Intra Coding */
289 put_bits(&s->pb,1,s->loop_filter); /* Deblocking Filter */
290 put_bits(&s->pb,1,s->h263_slice_structured); /* Slice Structured */
291 put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
292 put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
293 put_bits(&s->pb,1,s->alt_inter_vlc); /* Alternative Inter VLC */
294 put_bits(&s->pb,1,s->modified_quant); /* Modified Quantization: */
295 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
296 put_bits(&s->pb,3,0); /* Reserved */
297
298 put_bits(&s->pb, 3, s->c.pict_type == AV_PICTURE_TYPE_P);
299
300 put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
301 put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
302 put_bits(&s->pb,1,s->c.no_rounding); /* Rounding Type */
303 put_bits(&s->pb,2,0); /* Reserved */
304 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
305
306 /* This should be here if PLUSPTYPE */
307 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
308
309 if (format == 8) {
310 /* Custom Picture Format (CPFMT) */
311 unsigned aspect_ratio_info = ff_h263_aspect_to_info(s->c.avctx->sample_aspect_ratio);
312
313 put_bits(&s->pb,4, aspect_ratio_info);
314 put_bits(&s->pb,9,(s->c.width >> 2) - 1);
315 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
316 put_bits(&s->pb,9,(s->c.height >> 2));
317 if (aspect_ratio_info == FF_ASPECT_EXTENDED){
318 put_bits(&s->pb, 8, s->c.avctx->sample_aspect_ratio.num);
319 put_bits(&s->pb, 8, s->c.avctx->sample_aspect_ratio.den);
320 }
321 }
322 if (custom_pcf) {
323 if(ufep){
324 put_bits(&s->pb, 1, best_clock_code);
325 put_bits(&s->pb, 7, best_divisor);
326 }
327 put_sbits(&s->pb, 2, temp_ref>>8);
328 }
329
330 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
331 if (s->umvplus)
332// put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
333//FIXME check actual requested range
334 put_bits(&s->pb,2,1); /* unlimited */
335 if (s->h263_slice_structured)
336 put_bits(&s->pb,2,0); /* no weird submodes */
337
338 put_bits(&s->pb, 5, s->c.qscale);
339 }
340
341 put_bits(&s->pb, 1, 0); /* no PEI */
342
343 if (s->h263_slice_structured) {
344 put_bits(&s->pb, 1, 1);
345
346 av_assert1(s->c.mb_x == 0 && s->c.mb_y == 0);
348
349 put_bits(&s->pb, 1, 1);
350 }
351
352 return 0;
353}
354
356{
357 int16_t *dc = s->c.dc_val;
358
359 // The "- 1" is for the top-left entry
360 const int l_xy = s->c.block_index[2];
361 for (int i = l_xy - 2 * s->c.b8_stride - 1; i < l_xy; i += 2)
362 AV_WN32A(dc + i, 1024 << 16 | 1024);
363
364 const int u_xy = s->c.block_index[4];
365 const int v_xy = s->c.block_index[5];
366 int16_t *dc2 = dc + v_xy - u_xy;
367 for (int i = u_xy - s->c.mb_stride - 1; i < u_xy; ++i)
368 dc[i] = dc2[i] = 1024;
369}
370
371/**
372 * Encode a group of blocks header.
373 */
374void ff_h263_encode_gob_header(MPVEncContext *const s, int mb_line)
375{
376 put_bits(&s->pb, 17, 1); /* GBSC */
377
378 if (s->h263_slice_structured) {
379 put_bits(&s->pb, 1, 1);
380
382
383 if(s->c.mb_num > 1583)
384 put_bits(&s->pb, 1, 1);
385 put_bits(&s->pb, 5, s->c.qscale); /* GQUANT */
386 put_bits(&s->pb, 1, 1);
387 put_bits(&s->pb, 2, s->c.pict_type == AV_PICTURE_TYPE_I); /* GFID */
388 }else{
389 int gob_number = mb_line / s->gob_index;
390
391 put_bits(&s->pb, 5, gob_number); /* GN */
392 put_bits(&s->pb, 2, s->c.pict_type == AV_PICTURE_TYPE_I); /* GFID */
393 put_bits(&s->pb, 5, s->c.qscale); /* GQUANT */
394 }
395}
396
397/**
398 * modify qscale so that encoding is actually possible in H.263 (limit difference to -2..2)
399 */
401{
402 int8_t * const qscale_table = s->c.cur_pic.qscale_table;
403
404 for (int i = 1; i < s->c.mb_num; i++) {
405 if (qscale_table[ s->c.mb_index2xy[i] ] - qscale_table[ s->c.mb_index2xy[i-1] ] > 2)
406 qscale_table[ s->c.mb_index2xy[i] ] = qscale_table[ s->c.mb_index2xy[i-1] ] + 2;
407 }
408 for(int i = s->c.mb_num - 2; i >= 0; i--) {
409 if (qscale_table[ s->c.mb_index2xy[i] ] - qscale_table[ s->c.mb_index2xy[i+1] ] > 2)
410 qscale_table[ s->c.mb_index2xy[i] ] = qscale_table[ s->c.mb_index2xy[i+1] ] + 2;
411 }
412
413 if (s->c.codec_id != AV_CODEC_ID_H263P) {
414 for (int i = 1; i < s->c.mb_num; i++) {
415 int mb_xy = s->c.mb_index2xy[i];
416
417 if (qscale_table[mb_xy] != qscale_table[s->c.mb_index2xy[i - 1]] &&
418 (s->mb_type[mb_xy] & CANDIDATE_MB_TYPE_INTER4V)) {
419 s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER;
420 }
421 }
422 }
423}
424
425static const int dquant_code[5]= {1,0,9,2,3};
426
427static void flv2_encode_ac_esc(PutBitContext *pb, int slevel, int level,
428 int run, int last)
429{
430 unsigned code;
431 int bits;
432 if (level < 64) { // 7-bit level
433 bits = 1 + 1 + 6 + 7;
434 code = (0 << (1 + 6 + 7)) |
435 (last << (6 + 7)) |
436 (run << 7) |
437 (slevel & 0x7f);
438 } else {
439 /* 11-bit level */
440 bits = 1 + 1 + 6 + 11;
441 code = (1 << (1 + 6 + 11)) |
442 (last << (6 + 11)) |
443 (run << 11) |
444 (slevel & 0x7ff);
445 }
446 put_bits(pb, bits, code);
447}
448
449/**
450 * Encode an 8x8 block.
451 * @param block the 8x8 block
452 * @param n block index (0-3 are luma, 4-5 are chroma)
453 */
454static void h263_encode_block(MPVEncContext *const s, int16_t block[], int n)
455{
456 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
457 const RLTable *rl;
458
459 rl = &ff_h263_rl_inter;
460 if (s->c.mb_intra && !s->c.h263_aic) {
461 /* DC coef */
462 level = block[0];
463 /* 255 cannot be represented, so we clamp */
464 if (level > 254) {
465 level = 254;
466 block[0] = 254;
467 }
468 /* 0 cannot be represented also */
469 else if (level < 1) {
470 level = 1;
471 block[0] = 1;
472 }
473 if (level == 128) //FIXME check rv10
474 put_bits(&s->pb, 8, 0xff);
475 else
476 put_bits(&s->pb, 8, level);
477 i = 1;
478 } else {
479 i = 0;
480 if (s->c.h263_aic && s->c.mb_intra)
481 rl = &ff_rl_intra_aic;
482
483 if (s->alt_inter_vlc && !s->c.mb_intra) {
484 int aic_vlc_bits=0;
485 int inter_vlc_bits=0;
486 int wrong_pos=-1;
487 int aic_code;
488
489 last_index = s->c.block_last_index[n];
490 last_non_zero = i - 1;
491 for (; i <= last_index; i++) {
492 j = s->c.intra_scantable.permutated[i];
493 level = block[j];
494 if (level) {
495 run = i - last_non_zero - 1;
496 last = (i == last_index);
497
498 if(level<0) level= -level;
499
500 code = get_rl_index(rl, last, run, level);
501 aic_code = get_rl_index(&ff_rl_intra_aic, last, run, level);
502 inter_vlc_bits += rl->table_vlc[code][1]+1;
503 aic_vlc_bits += ff_rl_intra_aic.table_vlc[aic_code][1]+1;
504
505 if (code == rl->n) {
506 inter_vlc_bits += 1+6+8-1;
507 }
508 if (aic_code == ff_rl_intra_aic.n) {
509 aic_vlc_bits += 1+6+8-1;
510 wrong_pos += run + 1;
511 }else
512 wrong_pos += wrong_run[aic_code];
513 last_non_zero = i;
514 }
515 }
516 i = 0;
517 if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
518 rl = &ff_rl_intra_aic;
519 }
520 }
521
522 /* AC coefs */
523 last_index = s->c.block_last_index[n];
524 last_non_zero = i - 1;
525 for (; i <= last_index; i++) {
526 j = s->c.intra_scantable.permutated[i];
527 level = block[j];
528 if (level) {
529 run = i - last_non_zero - 1;
530 last = (i == last_index);
531 sign = 0;
532 slevel = level;
533 if (level < 0) {
534 sign = 1;
535 level = -level;
536 }
537 code = get_rl_index(rl, last, run, level);
538 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
539 if (code == rl->n) {
540 if (!CONFIG_FLV_ENCODER || s->c.codec_id != AV_CODEC_ID_FLV1) {
541 put_bits(&s->pb, 1, last);
542 put_bits(&s->pb, 6, run);
543
544 av_assert2(slevel != 0);
545
546 if (level < 128) {
547 put_sbits(&s->pb, 8, slevel);
548 } else {
549 put_bits(&s->pb, 8, 128);
550 put_sbits(&s->pb, 5, slevel);
551 put_sbits(&s->pb, 6, slevel>>5);
552 }
553 } else {
554 flv2_encode_ac_esc(&s->pb, slevel, level, run, last);
555 }
556 } else {
557 put_bits(&s->pb, 1, sign);
558 }
559 last_non_zero = i;
560 }
561 }
562}
563
564/* Encode MV differences on H.263+ with Unrestricted MV mode */
565static void h263p_encode_umotion(PutBitContext *pb, int val)
566{
567 if ( val == 0)
568 put_bits(pb, 1, 1);
569 else {
570 unsigned code = (val < 0) << 1;
571 unsigned aval = val < 0 ? -val : val;
572 unsigned n_bits = 2;
573
574 while (aval != 1) { // The leading digit is implicitly coded via length
575 unsigned tmp = (aval & 1) << 1 | 1;
576 aval >>= 1;
577 code |= tmp << n_bits;
578 n_bits += 2;
579 }
580 put_bits(pb, n_bits + 1, code);
581 }
582}
583
584static int h263_pred_dc(MPVEncContext *const s, int n, int16_t **dc_val_ptr)
585{
586 const int wrap = s->c.block_wrap[n];
587 const int xy = s->c.block_index[n];
588 int16_t *const dc_val = s->c.dc_val + xy;
589 int pred_dc;
590
591 /* find prediction */
592 /* B C
593 * A X
594 */
595 int a = dc_val[-1];
596 int c = dc_val[-wrap];
597
598 /* just DC prediction */
599 if (a != 1024 && c != 1024)
600 pred_dc = (a + c) >> 1;
601 else if (a != 1024)
602 pred_dc = a;
603 else
604 pred_dc = c;
605
606 /* we assume pred is positive */
607 *dc_val_ptr = dc_val;
608 return pred_dc;
609}
610
611static void h263_encode_mb(MPVEncContext *const s,
612 int16_t block[][64],
613 int motion_x, int motion_y)
614{
615 int cbpc, cbpy, i, cbp, pred_x, pred_y;
616 int16_t pred_dc;
617 int16_t rec_intradc[6];
618 const int interleaved_stats = s->c.avctx->flags & AV_CODEC_FLAG_PASS1;
619
620 if (!s->c.mb_intra) {
621 /* compute cbp */
622 cbp= get_p_cbp(s, block, motion_x, motion_y);
623
624 if ((cbp | motion_x | motion_y | s->dquant | (s->c.mv_type - MV_TYPE_16X16)) == 0) {
625 /* skip macroblock */
626 put_bits(&s->pb, 1, 1);
627 if(interleaved_stats){
628 s->misc_bits++;
629 s->last_bits++;
630 }
631
632 return;
633 }
634 put_bits(&s->pb, 1, 0); /* mb coded */
635
636 cbpc = cbp & 3;
637 cbpy = cbp >> 2;
638 if (!s->alt_inter_vlc || cbpc!=3)
639 cbpy ^= 0xF;
640 if(s->dquant) cbpc+= 8;
641 if(s->c.mv_type==MV_TYPE_16X16){
642 put_bits(&s->pb,
645
646 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
647 if(s->dquant)
648 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
649
650 if(interleaved_stats){
651 s->misc_bits+= get_bits_diff(s);
652 }
653
654 /* motion vectors: 16x16 mode */
655 ff_h263_pred_motion(&s->c, 0, 0, &pred_x, &pred_y);
656
657 if (!s->umvplus) {
658 ff_h263_encode_motion_vector(s, motion_x - pred_x,
659 motion_y - pred_y, 1);
660 }
661 else {
662 h263p_encode_umotion(&s->pb, motion_x - pred_x);
663 h263p_encode_umotion(&s->pb, motion_y - pred_y);
664 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
665 /* To prevent Start Code emulation */
666 put_bits(&s->pb,1,1);
667 }
668 }else{
669 put_bits(&s->pb,
671 ff_h263_inter_MCBPC_code[cbpc+16]);
672 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
673 if(s->dquant)
674 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
675
676 if(interleaved_stats){
677 s->misc_bits+= get_bits_diff(s);
678 }
679
680 for(i=0; i<4; i++){
681 /* motion vectors: 8x8 mode*/
682 ff_h263_pred_motion(&s->c, i, 0, &pred_x, &pred_y);
683
684 motion_x = s->c.cur_pic.motion_val[0][s->c.block_index[i]][0];
685 motion_y = s->c.cur_pic.motion_val[0][s->c.block_index[i]][1];
686 if (!s->umvplus) {
687 ff_h263_encode_motion_vector(s, motion_x - pred_x,
688 motion_y - pred_y, 1);
689 }
690 else {
691 h263p_encode_umotion(&s->pb, motion_x - pred_x);
692 h263p_encode_umotion(&s->pb, motion_y - pred_y);
693 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
694 /* To prevent Start Code emulation */
695 put_bits(&s->pb,1,1);
696 }
697 }
698 }
699
700 if(interleaved_stats){
701 s->mv_bits+= get_bits_diff(s);
702 }
703 } else {
704 av_assert2(s->c.mb_intra);
705
706 cbp = 0;
707 if (s->c.h263_aic) {
708 /* Predict DC */
709 for(i=0; i<6; i++) {
710 int16_t level = block[i][0];
711 int16_t *dc_ptr;
712 int scale = i < 4 ? s->c.y_dc_scale : s->c.c_dc_scale;
713
714 pred_dc = h263_pred_dc(s, i, &dc_ptr);
715 level -= pred_dc;
716 /* Quant */
717 if (level >= 0)
718 level = (level + (scale>>1))/scale;
719 else
720 level = (level - (scale>>1))/scale;
721
722 if (!s->modified_quant) {
723 if (level < -127)
724 level = -127;
725 else if (level > 127)
726 level = 127;
727 }
728
729 block[i][0] = level;
730 /* Reconstruction */
731 rec_intradc[i] = scale*level + pred_dc;
732 /* Oddify */
733 rec_intradc[i] |= 1;
734 //if ((rec_intradc[i] % 2) == 0)
735 // rec_intradc[i]++;
736 /* Clipping */
737 if (rec_intradc[i] < 0)
738 rec_intradc[i] = 0;
739 else if (rec_intradc[i] > 2047)
740 rec_intradc[i] = 2047;
741
742 /* Update AC/DC tables */
743 *dc_ptr = rec_intradc[i];
744 /* AIC can change CBP */
745 if (s->c.block_last_index[i] > 0 ||
746 (s->c.block_last_index[i] == 0 && level !=0))
747 cbp |= 1 << (5 - i);
748 }
749 }else{
750 for(i=0; i<6; i++) {
751 /* compute cbp */
752 if (s->c.block_last_index[i] >= 1)
753 cbp |= 1 << (5 - i);
754 }
755 }
756
757 cbpc = cbp & 3;
758 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
759 if(s->dquant) cbpc+=4;
760 put_bits(&s->pb,
763 } else {
764 if(s->dquant) cbpc+=8;
765 put_bits(&s->pb, 1, 0); /* mb coded */
766 put_bits(&s->pb,
767 ff_h263_inter_MCBPC_bits[cbpc + 4],
768 ff_h263_inter_MCBPC_code[cbpc + 4]);
769 }
770 if (s->c.h263_aic) {
771 /* XXX: currently, we do not try to use ac prediction */
772 put_bits(&s->pb, 1, 0); /* no AC prediction */
773 }
774 cbpy = cbp >> 2;
775 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
776 if(s->dquant)
777 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
778
779 if(interleaved_stats){
780 s->misc_bits+= get_bits_diff(s);
781 }
782 }
783
784 for(i=0; i<6; i++) {
785 /* encode each block */
786 h263_encode_block(s, block[i], i);
787
788 /* Update INTRADC for decoding */
789 if (s->c.h263_aic && s->c.mb_intra)
790 block[i][0] = rec_intradc[i];
791 }
792
793 if(interleaved_stats){
794 if (!s->c.mb_intra) {
795 s->p_tex_bits+= get_bits_diff(s);
796 }else{
797 s->i_tex_bits+= get_bits_diff(s);
798 s->i_count++;
799 }
800 }
801}
802
804{
805 const int mb_xy = s->c.mb_y * s->c.mb_stride + s->c.mb_x;
806
807 if (s->c.cur_pic.mbskip_table)
808 s->c.cur_pic.mbskip_table[mb_xy] = s->c.mb_skipped;
809
810 if (s->c.mv_type == MV_TYPE_8X8)
811 s->c.cur_pic.mb_type[mb_xy] = MB_TYPE_FORWARD_MV | MB_TYPE_8x8;
812 else if(s->c.mb_intra)
813 s->c.cur_pic.mb_type[mb_xy] = MB_TYPE_INTRA;
814 else
815 s->c.cur_pic.mb_type[mb_xy] = MB_TYPE_FORWARD_MV | MB_TYPE_16x16;
816
818}
819
821{
822 MPVEncContext *const s = &m->s;
823
824 s->me.mv_penalty = ff_h263_get_mv_penalty(); // FIXME exact table for MSMPEG4 & H.263+
825
826 ff_h263dsp_init(&s->c.h263dsp);
827
828 ff_permute_scantable(s->permutated_intra_h_scantable, ff_alternate_horizontal_scan,
829 s->c.idsp.idct_permutation);
830 ff_permute_scantable(s->permutated_intra_v_scantable, ff_alternate_vertical_scan,
831 s->c.idsp.idct_permutation);
832
833 if (s->c.codec_id == AV_CODEC_ID_MPEG4)
834 return;
835
836 s->intra_ac_vlc_length =s->inter_ac_vlc_length = uni_h263_inter_rl_len;
837 s->intra_ac_vlc_last_length=s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64;
838 if (s->c.h263_aic) {
839 s->intra_ac_vlc_length = uni_h263_intra_aic_rl_len;
840 s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64;
841
842 s->c.y_dc_scale_table =
843 s->c.c_dc_scale_table = ff_aic_dc_scale_table;
844 }
845 s->ac_esc_length= 7+1+6+8;
846
847 if (s->modified_quant)
848 s->c.chroma_qscale_table = ff_h263_chroma_qscale_table;
849
850 // Only used for H.263 and H.263+
851 s->gob_index = H263_GOB_HEIGHT(s->c.height);
852
853 // use fcodes >1 only for MPEG-4 & H.263 & H.263+ FIXME
854 switch(s->c.codec_id){
856 if (s->umvplus)
857 m->fcode_tab = umv_fcode_tab + MAX_MV;
858 if (s->modified_quant) {
859 s->min_qcoeff= -2047;
860 s->max_qcoeff= 2047;
861 }else{
862 s->min_qcoeff= -127;
863 s->max_qcoeff= 127;
864 }
865 break;
866 // Note for MPEG-4 & H.263 the dc-scale table will be set per frame as needed later
867#if CONFIG_FLV_ENCODER
868 case AV_CODEC_ID_FLV1:
870 /* format = 1; 11-bit codes */
871 s->min_qcoeff = -1023;
872 s->max_qcoeff = 1023;
873 break;
874#endif
875 default: //nothing needed - default table already set in mpegvideo.c
876 s->min_qcoeff= -127;
877 s->max_qcoeff= 127;
878 }
879 // H.263, H.263+; will be overwritten for MSMPEG-4 later
880 if (!m->encode_picture_header)
881 m->encode_picture_header = h263_encode_picture_header;
882 if (!s->encode_mb)
883 s->encode_mb = h263_encode_mb;
884}
885
887{
888 int i, mb_pos;
889
890 for(i=0; i<6; i++){
891 if(s->c.mb_num-1 <= ff_mba_max[i]) break;
892 }
893 mb_pos= s->c.mb_x + s->c.mb_width*s->c.mb_y;
894 put_bits(&s->pb, ff_mba_length[i], mb_pos);
895}
896
897#define OFFSET(x) offsetof(MpegEncContext, x)
898#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
899static const AVOption h263_options[] = {
900 { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
901 { "mb_info", "emit macroblock info for RFC 2190 packetization, the parameter value is the maximum payload size", FF_MPV_OFFSET(mb_info), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
904 { NULL },
905};
906
907static const AVClass h263_class = {
908 .class_name = "H.263 encoder",
909 .item_name = av_default_item_name,
910 .option = h263_options,
911 .version = LIBAVUTIL_VERSION_INT,
912};
913
914const FFCodec ff_h263_encoder = {
915 .p.name = "h263",
916 CODEC_LONG_NAME("H.263 / H.263-1996"),
917 .p.type = AVMEDIA_TYPE_VIDEO,
918 .p.id = AV_CODEC_ID_H263,
920 .color_ranges = AVCOL_RANGE_MPEG,
921 .p.priv_class = &h263_class,
923 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
924 .priv_data_size = sizeof(MPVMainEncContext),
927 .close = ff_mpv_encode_end,
928};
929
930static const AVOption h263p_options[] = {
931 { "umv", "Use unlimited motion vectors.", FF_MPV_OFFSET(umvplus), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
932 { "aiv", "Use alternative inter VLC.", FF_MPV_OFFSET(alt_inter_vlc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
933 { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
934 { "structured_slices", "Write slice start position at every GOB header instead of just GOB number.", FF_MPV_OFFSET(h263_slice_structured), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE},
937 { NULL },
938};
939static const AVClass h263p_class = {
940 .class_name = "H.263p encoder",
941 .item_name = av_default_item_name,
942 .option = h263p_options,
943 .version = LIBAVUTIL_VERSION_INT,
944};
945
947 .p.name = "h263p",
948 CODEC_LONG_NAME("H.263+ / H.263-1998 / H.263 version 2"),
949 .p.type = AVMEDIA_TYPE_VIDEO,
950 .p.id = AV_CODEC_ID_H263P,
952 .color_ranges = AVCOL_RANGE_MPEG,
953 .p.priv_class = &h263p_class,
954 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS |
956 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
957 .priv_data_size = sizeof(MPVMainEncContext),
960 .close = ff_mpv_encode_end,
961};
962#endif
#define wrap(func)
Definition neontest.h:65
static double val(void *priv, double ch)
Definition aeval.c:77
static const char *const format[]
Definition af_aiir.c:444
const FFCodec ff_h263p_encoder
const FFCodec ff_h263_encoder
#define VE
Definition amfenc_av1.c:30
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define av_clip
Definition common.h:100
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static int16_t block[64]
Definition dct.c:125
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static const uint8_t bits[8]
Definition fastaudio.c:100
#define OFFSET(x)
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition codec.h:102
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition avcodec.h:290
@ AV_CODEC_ID_FLV1
Definition codec_id.h:71
@ AV_CODEC_ID_H263
Definition codec_id.h:54
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
@ AV_CODEC_ID_H263P
Definition codec_id.h:69
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition rational.h:89
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
static uint8_t mv_penalty[MAX_FCODE+1][MAX_DMV *2+1]
Definition h261enc.c:53
void ff_h263_update_motion_val(MpegEncContext *s)
Definition h263.c:56
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
Definition h263.c:182
#define FF_ASPECT_EXTENDED
Definition h263.h:26
void ff_h263_init_rl_inter(void)
av_const int ff_h263_aspect_to_info(AVRational aspect)
#define H263_GOB_HEIGHT(h)
Definition h263.h:28
const uint8_t ff_mba_length[7]
Definition h263data.c:269
RLTable ff_rl_intra_aic
Definition h263data.c:228
RLTable ff_h263_rl_inter
Definition h263data.c:159
const uint8_t ff_aic_dc_scale_table[32]
Definition h263data.c:245
const AVRational ff_h263_pixel_aspect[16]
Definition h263data.c:273
const uint8_t ff_h263_intra_MCBPC_bits[9]
Definition h263data.c:33
const uint8_t ff_h263_chroma_qscale_table[32]
Definition h263data.c:260
const uint8_t ff_h263_inter_MCBPC_code[28]
Definition h263data.c:38
const uint8_t ff_mvtab[33][2]
Definition h263data.c:88
const uint8_t ff_h263_inter_MCBPC_bits[28]
Definition h263data.c:47
const uint16_t ff_mba_max[6]
Definition h263data.c:265
const uint8_t ff_h263_intra_MCBPC_code[9]
Definition h263data.c:32
const uint8_t ff_h263_cbpy_tab[16][2]
Definition h263data.c:82
const uint16_t ff_h263_format[8][2]
Definition h263data.c:236
H.263 tables.
#define H263_RL_NB_ELEMS
Definition h263data.h:63
#define H263_ESCAPE_CODE_LENGTH
Definition h263data.h:65
#define H263_RL_NON_LAST_CODES
Definition h263data.h:64
void ff_h263_encode_mba(MPVEncContext *s)
void ff_h263_mpeg4_reset_dc(MPVEncContext *s)
static void ff_h263_encode_motion_vector(MPVEncContext *s, int x, int y, int f_code)
Definition h263enc.h:39
static int get_p_cbp(MPVEncContext *const s, int16_t block[6][64], int motion_x, int motion_y)
Definition h263enc.h:46
void ff_h263_encode_gob_header(MPVEncContext *s, int mb_line)
void ff_clean_h263_qscales(MPVEncContext *s)
void ff_h263_encode_init(MPVMainEncContext *m)
void ff_h263_update_mb(MPVEncContext *s)
int a
#define av_log2
Definition intmath.h:84
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
#define AV_WN32A(p, v)
void ff_h263_encode_motion(PutBitContext *pb, int val, int f_code)
Definition ituh263enc.c:157
static av_cold void init_mv_penalty(void)
Definition ituh263enc.c:57
av_cold const uint8_t(* ff_h263_get_mv_penalty(void))[MAX_DMV *2+1]
Definition ituh263enc.c:148
static av_cold void h263_encode_init_static(void)
Definition ituh263enc.c:132
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
static const int8_t mv[256][2]
Definition 4xm.c:81
int ff_flv_encode_picture_header(MPVMainEncContext *const m)
Definition flvenc.c:27
av_cold void ff_h263dsp_init(H263DSPContext *ctx)
Definition h263dsp.c:117
av_cold void ff_permute_scantable(uint8_t dst[64], const uint8_t src[64], const uint8_t permutation[64])
Definition idctdsp.c:30
common internal api header.
int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
Return the index into tab at which {a,b} match elements {[0],[1]} of tab.
Definition utils.c:850
Macro definitions for various function/variable attributes.
#define av_const
Definition attributes.h:111
#define av_cold
Definition attributes.h:117
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
static av_const int sign_extend(int val, unsigned bits)
Definition mathops.h:135
enum AVColorRange range
#define MAX_MV
Definition motion_est.h:37
#define MAX_DMV
Definition motion_est.h:39
#define UNI_MPEG4_ENC_INDEX(last, run, level)
static const int dquant_code[5]
#define MB_TYPE_8x8
Definition mpegutils.h:44
#define MB_TYPE_INTRA
Definition mpegutils.h:64
#define MB_TYPE_16x16
Definition mpegutils.h:41
#define MB_TYPE_FORWARD_MV
Definition mpegutils.h:49
mpegvideo header.
#define MV_TYPE_8X8
4 vectors (H.263, MPEG-4 4MV)
Definition mpegvideo.h:173
#define MV_TYPE_16X16
1 vector for the whole mb
Definition mpegvideo.h:172
av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pic_arg, int *got_packet)
av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
const uint8_t ff_alternate_horizontal_scan[64]
const uint8_t ff_alternate_vertical_scan[64]
mpegvideo header.
static int get_bits_diff(MPVEncContext *s)
#define CANDIDATE_MB_TYPE_INTER
#define MAX_FCODE
#define CANDIDATE_MB_TYPE_INTER4V
#define FF_MPV_OFFSET(x)
#define FF_MPV_COMMON_OPTS
#define FF_MPV_COMMON_MOTION_EST_OPTS
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
static void FUNC pred_dc(uint8_t *_src, const uint8_t *_top, const uint8_t *_left, ptrdiff_t stride, int log2_size, int c_idx)
bitstream writer API
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition put_bits.h:291
static void put_bits_assume_flushed(const PutBitContext *s)
Inform the compiler that a PutBitContext is flushed (i.e.
Definition put_bits.h:82
av_cold void ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Initialize index_run, max_level and max_run from n, last, table_vlc, table_run and table_level.
Definition rl.c:43
#define MAX_LEVEL
Definition rl.h:36
#define MAX_RUN
Definition rl.h:35
static int get_rl_index(const RLTable *rl, int last, int run, int level)
Definition rl.h:101
#define FF_ARRAY_ELEMS(a)
const uint8_t * code
Definition spdifenc.c:433
Describe the class of an AVClass context structure.
Definition log.h:76
AVOption.
Definition opt.h:428
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
MPVEncContext s
The main slicecontext.
const uint8_t * fcode_tab
smallest fcode needed for each MV
int(* encode_picture_header)(struct MPVMainEncContext *m)
RLTable.
Definition rl.h:39
int n
number of entries of table_vlc minus 1
Definition rl.h:40
const uint16_t(* table_vlc)[2]
Definition rl.h:42
const int8_t * table_level
Definition rl.h:44
const int8_t * table_run
Definition rl.h:43
uint8_t run
Definition svq3.c:207
uint8_t level
Definition svq3.c:208
static void error(const char *err)
static uint8_t tmp[40]
Definition aes_ctr.c:52
int len
static double c[64]