• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * H.263 encoder header
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 #ifndef AVCODEC_H263ENC_H
21 #define AVCODEC_H263ENC_H
22 
23 #include <stdint.h>
24 #include "h263data.h"
25 #include "mpegvideoenc.h"
26 
27 void ff_h263_encode_init(MpegEncContext *s);
28 void ff_h263_encode_picture_header(MpegEncContext *s, int picture_number);
29 void ff_h263_encode_gob_header(MpegEncContext * s, int mb_line);
30 void ff_h263_encode_mb(MpegEncContext *s,
31                        int16_t block[6][64],
32                        int motion_x, int motion_y);
33 void ff_h263_encode_mba(MpegEncContext *s);
34 
35 void ff_init_qscale_tab(MpegEncContext *s);
36 void ff_clean_h263_qscales(MpegEncContext *s);
37 
38 void ff_h263_encode_motion(PutBitContext *pb, int val, int f_code);
39 
40 
h263_get_motion_length(int val,int f_code)41 static inline int h263_get_motion_length(int val, int f_code)
42 {
43     int bit_size, code, sign;
44 
45     if (val == 0) {
46         return 1; /* ff_mvtab[0][1] */
47     } else {
48         bit_size = f_code - 1;
49         /* modulo encoding */
50         val  = sign_extend(val, 6 + bit_size);
51         sign = val >> 31;
52         val  = (val ^ sign) - sign; /* val = FFABS(val) */
53         val--;
54         code = (val >> bit_size) + 1;
55 
56         return ff_mvtab[code][1] + 1 + bit_size;
57     }
58 }
59 
ff_h263_encode_motion_vector(MpegEncContext * s,int x,int y,int f_code)60 static inline void ff_h263_encode_motion_vector(MpegEncContext * s,
61                                                 int x, int y, int f_code)
62 {
63     if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT) {
64         skip_put_bits(&s->pb,
65                       h263_get_motion_length(x, f_code) +
66                       h263_get_motion_length(y, f_code));
67     } else {
68         ff_h263_encode_motion(&s->pb, x, f_code);
69         ff_h263_encode_motion(&s->pb, y, f_code);
70     }
71 }
72 
get_p_cbp(MpegEncContext * s,int16_t block[6][64],int motion_x,int motion_y)73 static inline int get_p_cbp(MpegEncContext * s,
74                       int16_t block[6][64],
75                       int motion_x, int motion_y){
76     int cbp;
77 
78     if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
79         int best_cbpy_score = INT_MAX;
80         int best_cbpc_score = INT_MAX;
81         int cbpc = (-1), cbpy = (-1);
82         const int offset = (s->mv_type == MV_TYPE_16X16 ? 0 : 16) + (s->dquant ? 8 : 0);
83         const int lambda = s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
84 
85         for (int i = 0; i < 4; i++) {
86             int score = ff_h263_inter_MCBPC_bits[i + offset] * lambda;
87             if (i & 1) score += s->coded_score[5];
88             if (i & 2) score += s->coded_score[4];
89 
90             if (score < best_cbpc_score) {
91                 best_cbpc_score = score;
92                 cbpc = i;
93             }
94         }
95 
96         for (int i = 0; i < 16; i++) {
97             int score= ff_h263_cbpy_tab[i ^ 0xF][1] * lambda;
98             if (i & 1) score += s->coded_score[3];
99             if (i & 2) score += s->coded_score[2];
100             if (i & 4) score += s->coded_score[1];
101             if (i & 8) score += s->coded_score[0];
102 
103             if (score < best_cbpy_score) {
104                 best_cbpy_score = score;
105                 cbpy = i;
106             }
107         }
108         cbp = cbpc + 4 * cbpy;
109         if (!(motion_x | motion_y | s->dquant) && s->mv_type == MV_TYPE_16X16) {
110             if (best_cbpy_score + best_cbpc_score + 2 * lambda >= 0)
111                 cbp= 0;
112         }
113 
114         for (int i = 0; i < 6; i++) {
115             if (s->block_last_index[i] >= 0 && !((cbp >> (5 - i)) & 1)) {
116                 s->block_last_index[i] = -1;
117                 s->bdsp.clear_block(s->block[i]);
118             }
119         }
120     } else {
121         cbp = 0;
122         for (int i = 0; i < 6; i++) {
123             if (s->block_last_index[i] >= 0)
124                 cbp |= 1 << (5 - i);
125         }
126     }
127     return cbp;
128 }
129 
130 #endif
131