Commit | Line | Data |
---|---|---|
ca334dd1 MN |
1 | /* |
2 | * H263 internal 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_H263_H | |
21 | #define AVCODEC_H263_H | |
22 | ||
23 | // The defines below define the number of bits that are read at once for | |
24 | // reading vlc values. Changing these may improve speed and data cache needs | |
25 | // be aware though that decreasing them may need the number of stages that is | |
26 | // passed to get_vlc* to be increased. | |
27 | #define INTRA_MCBPC_VLC_BITS 6 | |
28 | #define INTER_MCBPC_VLC_BITS 7 | |
29 | #define CBPY_VLC_BITS 6 | |
30 | #define TEX_VLC_BITS 9 | |
31 | ||
32 | extern const AVRational ff_h263_pixel_aspect[16]; | |
33 | extern const uint8_t cbpy_tab[16][2]; | |
34 | ||
35 | extern const uint8_t mvtab[33][2]; | |
36 | ||
37 | extern const uint8_t intra_MCBPC_code[9]; | |
38 | extern const uint8_t intra_MCBPC_bits[9]; | |
39 | ||
40 | extern const uint8_t inter_MCBPC_code[28]; | |
41 | extern const uint8_t inter_MCBPC_bits[28]; | |
42 | ||
43 | extern VLC intra_MCBPC_vlc; | |
44 | extern VLC inter_MCBPC_vlc; | |
45 | extern VLC cbpy_vlc; | |
46 | ||
47 | extern RLTable rl_inter; | |
48 | ||
49 | int h263_decode_motion(MpegEncContext * s, int pred, int f_code); | |
50 | av_const int ff_h263_aspect_to_info(AVRational aspect); | |
51 | ||
52 | static inline int h263_get_motion_length(MpegEncContext * s, int val, int f_code){ | |
53 | int l, bit_size, code; | |
54 | ||
55 | if (val == 0) { | |
56 | return mvtab[0][1]; | |
57 | } else { | |
58 | bit_size = f_code - 1; | |
59 | /* modulo encoding */ | |
60 | l= INT_BIT - 6 - bit_size; | |
61 | val = (val<<l)>>l; | |
62 | val--; | |
63 | code = (val >> bit_size) + 1; | |
64 | ||
65 | return mvtab[code][1] + 1 + bit_size; | |
66 | } | |
67 | } | |
68 | ||
69 | static inline void ff_h263_encode_motion_vector(MpegEncContext * s, int x, int y, int f_code){ | |
70 | if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){ | |
71 | skip_put_bits(&s->pb, | |
72 | h263_get_motion_length(s, x, f_code) | |
73 | +h263_get_motion_length(s, y, f_code)); | |
74 | }else{ | |
75 | ff_h263_encode_motion(s, x, f_code); | |
76 | ff_h263_encode_motion(s, y, f_code); | |
77 | } | |
78 | } | |
79 | ||
80 | static inline int get_p_cbp(MpegEncContext * s, | |
81 | DCTELEM block[6][64], | |
82 | int motion_x, int motion_y){ | |
83 | int cbp, i; | |
84 | ||
85 | if(s->flags & CODEC_FLAG_CBP_RD){ | |
86 | int best_cbpy_score= INT_MAX; | |
87 | int best_cbpc_score= INT_MAX; | |
88 | int cbpc = (-1), cbpy= (-1); | |
89 | const int offset= (s->mv_type==MV_TYPE_16X16 ? 0 : 16) + (s->dquant ? 8 : 0); | |
90 | const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6); | |
91 | ||
92 | for(i=0; i<4; i++){ | |
93 | int score= inter_MCBPC_bits[i + offset] * lambda; | |
94 | if(i&1) score += s->coded_score[5]; | |
95 | if(i&2) score += s->coded_score[4]; | |
96 | ||
97 | if(score < best_cbpc_score){ | |
98 | best_cbpc_score= score; | |
99 | cbpc= i; | |
100 | } | |
101 | } | |
102 | ||
103 | for(i=0; i<16; i++){ | |
104 | int score= cbpy_tab[i ^ 0xF][1] * lambda; | |
105 | if(i&1) score += s->coded_score[3]; | |
106 | if(i&2) score += s->coded_score[2]; | |
107 | if(i&4) score += s->coded_score[1]; | |
108 | if(i&8) score += s->coded_score[0]; | |
109 | ||
110 | if(score < best_cbpy_score){ | |
111 | best_cbpy_score= score; | |
112 | cbpy= i; | |
113 | } | |
114 | } | |
115 | cbp= cbpc + 4*cbpy; | |
116 | if ((motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16){ | |
117 | if(best_cbpy_score + best_cbpc_score + 2*lambda >= 0) | |
118 | cbp= 0; | |
119 | } | |
120 | ||
121 | for (i = 0; i < 6; i++) { | |
122 | if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){ | |
123 | s->block_last_index[i]= -1; | |
124 | s->dsp.clear_block(s->block[i]); | |
125 | } | |
126 | } | |
127 | }else{ | |
128 | cbp= 0; | |
129 | for (i = 0; i < 6; i++) { | |
130 | if (s->block_last_index[i] >= 0) | |
131 | cbp |= 1 << (5 - i); | |
132 | } | |
133 | } | |
134 | return cbp; | |
135 | } | |
136 | ||
137 | static inline int get_b_cbp(MpegEncContext * s, DCTELEM block[6][64], | |
138 | int motion_x, int motion_y, int mb_type){ | |
139 | int cbp=0, i; | |
140 | ||
141 | if(s->flags & CODEC_FLAG_CBP_RD){ | |
142 | int score=0; | |
143 | const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6); | |
144 | ||
145 | for(i=0; i<6; i++){ | |
146 | if(s->coded_score[i] < 0){ | |
147 | score += s->coded_score[i]; | |
148 | cbp |= 1 << (5 - i); | |
149 | } | |
150 | } | |
151 | ||
152 | if(cbp){ | |
153 | int zero_score= -6; | |
154 | if ((motion_x | motion_y | s->dquant | mb_type) == 0){ | |
155 | zero_score-= 4; //2*MV + mb_type + cbp bit | |
156 | } | |
157 | ||
158 | zero_score*= lambda; | |
159 | if(zero_score <= score){ | |
160 | cbp=0; | |
161 | } | |
162 | } | |
163 | ||
164 | for (i = 0; i < 6; i++) { | |
165 | if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){ | |
166 | s->block_last_index[i]= -1; | |
167 | s->dsp.clear_block(s->block[i]); | |
168 | } | |
169 | } | |
170 | }else{ | |
171 | for (i = 0; i < 6; i++) { | |
172 | if (s->block_last_index[i] >= 0) | |
173 | cbp |= 1 << (5 - i); | |
174 | } | |
175 | } | |
176 | return cbp; | |
177 | } | |
178 | #endif |