Commit | Line | Data |
---|---|---|
d632239c PI |
1 | /* |
2 | * H261 encoder | |
3 | * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> | |
4 | * Copyright (c) 2004 Maarten Daniels | |
5 | * | |
2912e87a | 6 | * This file is part of Libav. |
d632239c | 7 | * |
2912e87a | 8 | * Libav is free software; you can redistribute it and/or |
d632239c PI |
9 | * modify it under the terms of the GNU Lesser General Public |
10 | * License as published by the Free Software Foundation; either | |
11 | * version 2.1 of the License, or (at your option) any later version. | |
12 | * | |
2912e87a | 13 | * Libav is distributed in the hope that it will be useful, |
d632239c PI |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
2912e87a | 19 | * License along with Libav; if not, write to the Free Software |
d632239c PI |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | */ | |
22 | ||
23 | /** | |
ba87f080 | 24 | * @file |
d632239c PI |
25 | * H.261 encoder. |
26 | */ | |
27 | ||
d632239c PI |
28 | #include "avcodec.h" |
29 | #include "mpegvideo.h" | |
06e03fa0 | 30 | #include "h263.h" |
d632239c | 31 | #include "h261.h" |
b3e83c96 | 32 | #include "h261data.h" |
d632239c | 33 | |
88bd7fdc | 34 | static void h261_encode_block(H261Context * h, int16_t * block, |
d632239c PI |
35 | int n); |
36 | ||
37 | int ff_h261_get_picture_format(int width, int height){ | |
38 | // QCIF | |
39 | if (width == 176 && height == 144) | |
40 | return 0; | |
41 | // CIF | |
42 | else if (width == 352 && height == 288) | |
43 | return 1; | |
44 | // ERROR | |
45 | else | |
46 | return -1; | |
47 | } | |
48 | ||
49 | void ff_h261_encode_picture_header(MpegEncContext * s, int picture_number){ | |
50 | H261Context * h = (H261Context *) s; | |
51 | int format, temp_ref; | |
52 | ||
9f51c682 | 53 | avpriv_align_put_bits(&s->pb); |
d632239c PI |
54 | |
55 | /* Update the pointer to last GOB */ | |
fb53b4a0 | 56 | s->ptr_lastgob = put_bits_ptr(&s->pb); |
d632239c PI |
57 | |
58 | put_bits(&s->pb, 20, 0x10); /* PSC */ | |
59 | ||
60 | temp_ref= s->picture_number * (int64_t)30000 * s->avctx->time_base.num / | |
61 | (1001 * (int64_t)s->avctx->time_base.den); //FIXME maybe this should use a timestamp | |
6647ab80 | 62 | put_sbits(&s->pb, 5, temp_ref); /* TemporalReference */ |
d632239c PI |
63 | |
64 | put_bits(&s->pb, 1, 0); /* split screen off */ | |
65 | put_bits(&s->pb, 1, 0); /* camera off */ | |
66 | put_bits(&s->pb, 1, 0); /* freeze picture release off */ | |
67 | ||
68 | format = ff_h261_get_picture_format(s->width, s->height); | |
69 | ||
70 | put_bits(&s->pb, 1, format); /* 0 == QCIF, 1 == CIF */ | |
71 | ||
72 | put_bits(&s->pb, 1, 0); /* still image mode */ | |
73 | put_bits(&s->pb, 1, 0); /* reserved */ | |
74 | ||
75 | put_bits(&s->pb, 1, 0); /* no PEI */ | |
76 | if(format == 0) | |
77 | h->gob_number = -1; | |
78 | else | |
79 | h->gob_number = 0; | |
80 | h->current_mba = 0; | |
81 | } | |
82 | ||
83 | /** | |
49bd8e4b | 84 | * Encode a group of blocks header. |
d632239c PI |
85 | */ |
86 | static void h261_encode_gob_header(MpegEncContext * s, int mb_line){ | |
87 | H261Context * h = (H261Context *)s; | |
88 | if(ff_h261_get_picture_format(s->width, s->height) == 0){ | |
89 | h->gob_number+=2; // QCIF | |
90 | } | |
91 | else{ | |
92 | h->gob_number++; // CIF | |
93 | } | |
94 | put_bits(&s->pb, 16, 1); /* GBSC */ | |
95 | put_bits(&s->pb, 4, h->gob_number); /* GN */ | |
96 | put_bits(&s->pb, 5, s->qscale); /* GQUANT */ | |
97 | put_bits(&s->pb, 1, 0); /* no GEI */ | |
98 | h->current_mba = 0; | |
99 | h->previous_mba = 0; | |
100 | h->current_mv_x=0; | |
101 | h->current_mv_y=0; | |
102 | } | |
103 | ||
104 | void ff_h261_reorder_mb_index(MpegEncContext* s){ | |
105 | int index= s->mb_x + s->mb_y*s->mb_width; | |
106 | ||
107 | if(index % 33 == 0) | |
108 | h261_encode_gob_header(s,0); | |
109 | ||
110 | /* for CIF the GOB's are fragmented in the middle of a scanline | |
111 | that's why we need to adjust the x and y index of the macroblocks */ | |
112 | if(ff_h261_get_picture_format(s->width,s->height) == 1){ // CIF | |
113 | s->mb_x = index % 11 ; index /= 11; | |
114 | s->mb_y = index % 3 ; index /= 3; | |
115 | s->mb_x+= 11*(index % 2); index /= 2; | |
116 | s->mb_y+= 3*index; | |
117 | ||
118 | ff_init_block_index(s); | |
119 | ff_update_block_index(s); | |
120 | } | |
121 | } | |
122 | ||
123 | static void h261_encode_motion(H261Context * h, int val){ | |
124 | MpegEncContext * const s = &h->s; | |
125 | int sign, code; | |
126 | if(val==0){ | |
127 | code = 0; | |
128 | put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]); | |
129 | } | |
130 | else{ | |
131 | if(val > 15) | |
132 | val -=32; | |
133 | if(val < -16) | |
134 | val+=32; | |
135 | sign = val < 0; | |
136 | code = sign ? -val : val; | |
137 | put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]); | |
138 | put_bits(&s->pb,1,sign); | |
139 | } | |
140 | } | |
141 | ||
142 | static inline int get_cbp(MpegEncContext * s, | |
88bd7fdc | 143 | int16_t block[6][64]) |
d632239c PI |
144 | { |
145 | int i, cbp; | |
146 | cbp= 0; | |
147 | for (i = 0; i < 6; i++) { | |
148 | if (s->block_last_index[i] >= 0) | |
149 | cbp |= 1 << (5 - i); | |
150 | } | |
151 | return cbp; | |
152 | } | |
153 | void ff_h261_encode_mb(MpegEncContext * s, | |
88bd7fdc | 154 | int16_t block[6][64], |
d632239c PI |
155 | int motion_x, int motion_y) |
156 | { | |
157 | H261Context * h = (H261Context *)s; | |
158 | int mvd, mv_diff_x, mv_diff_y, i, cbp; | |
159 | cbp = 63; // avoid warning | |
160 | mvd = 0; | |
161 | ||
162 | h->current_mba++; | |
163 | h->mtype = 0; | |
164 | ||
165 | if (!s->mb_intra){ | |
166 | /* compute cbp */ | |
167 | cbp= get_cbp(s, block); | |
168 | ||
169 | /* mvd indicates if this block is motion compensated */ | |
170 | mvd = motion_x | motion_y; | |
171 | ||
172 | if((cbp | mvd | s->dquant ) == 0) { | |
173 | /* skip macroblock */ | |
174 | s->skip_count++; | |
175 | h->current_mv_x=0; | |
176 | h->current_mv_y=0; | |
177 | return; | |
178 | } | |
179 | } | |
180 | ||
181 | /* MB is not skipped, encode MBA */ | |
182 | put_bits(&s->pb, h261_mba_bits[(h->current_mba-h->previous_mba)-1], h261_mba_code[(h->current_mba-h->previous_mba)-1]); | |
183 | ||
184 | /* calculate MTYPE */ | |
185 | if(!s->mb_intra){ | |
186 | h->mtype++; | |
187 | ||
188 | if(mvd || s->loop_filter) | |
189 | h->mtype+=3; | |
190 | if(s->loop_filter) | |
191 | h->mtype+=3; | |
192 | if(cbp || s->dquant) | |
193 | h->mtype++; | |
194 | assert(h->mtype > 1); | |
195 | } | |
196 | ||
197 | if(s->dquant) | |
198 | h->mtype++; | |
199 | ||
200 | put_bits(&s->pb, h261_mtype_bits[h->mtype], h261_mtype_code[h->mtype]); | |
201 | ||
202 | h->mtype = h261_mtype_map[h->mtype]; | |
203 | ||
204 | if(IS_QUANT(h->mtype)){ | |
205 | ff_set_qscale(s,s->qscale+s->dquant); | |
206 | put_bits(&s->pb, 5, s->qscale); | |
207 | } | |
208 | ||
209 | if(IS_16X16(h->mtype)){ | |
210 | mv_diff_x = (motion_x >> 1) - h->current_mv_x; | |
211 | mv_diff_y = (motion_y >> 1) - h->current_mv_y; | |
212 | h->current_mv_x = (motion_x >> 1); | |
213 | h->current_mv_y = (motion_y >> 1); | |
214 | h261_encode_motion(h,mv_diff_x); | |
215 | h261_encode_motion(h,mv_diff_y); | |
216 | } | |
217 | ||
218 | h->previous_mba = h->current_mba; | |
219 | ||
220 | if(HAS_CBP(h->mtype)){ | |
221 | assert(cbp>0); | |
222 | put_bits(&s->pb,h261_cbp_tab[cbp-1][1],h261_cbp_tab[cbp-1][0]); | |
223 | } | |
224 | for(i=0; i<6; i++) { | |
225 | /* encode each block */ | |
226 | h261_encode_block(h, block[i], i); | |
227 | } | |
228 | ||
229 | if ( ( h->current_mba == 11 ) || ( h->current_mba == 22 ) || ( h->current_mba == 33 ) || ( !IS_16X16 ( h->mtype ) )){ | |
230 | h->current_mv_x=0; | |
231 | h->current_mv_y=0; | |
232 | } | |
233 | } | |
234 | ||
235 | void ff_h261_encode_init(MpegEncContext *s){ | |
236 | static int done = 0; | |
237 | ||
238 | if (!done) { | |
239 | done = 1; | |
e96b4a53 | 240 | ff_init_rl(&h261_rl_tcoeff, ff_h261_rl_table_store); |
d632239c PI |
241 | } |
242 | ||
243 | s->min_qcoeff= -127; | |
244 | s->max_qcoeff= 127; | |
245 | s->y_dc_scale_table= | |
246 | s->c_dc_scale_table= ff_mpeg1_dc_scale_table; | |
247 | } | |
248 | ||
249 | ||
250 | /** | |
58c42af7 | 251 | * Encode an 8x8 block. |
d632239c PI |
252 | * @param block the 8x8 block |
253 | * @param n block index (0-3 are luma, 4-5 are chroma) | |
254 | */ | |
88bd7fdc | 255 | static void h261_encode_block(H261Context * h, int16_t * block, int n){ |
d632239c | 256 | MpegEncContext * const s = &h->s; |
38a7695f | 257 | int level, run, i, j, last_index, last_non_zero, sign, slevel, code; |
d632239c PI |
258 | RLTable *rl; |
259 | ||
260 | rl = &h261_rl_tcoeff; | |
261 | if (s->mb_intra) { | |
262 | /* DC coef */ | |
263 | level = block[0]; | |
264 | /* 255 cannot be represented, so we clamp */ | |
265 | if (level > 254) { | |
266 | level = 254; | |
267 | block[0] = 254; | |
268 | } | |
269 | /* 0 cannot be represented also */ | |
270 | else if (level < 1) { | |
271 | level = 1; | |
272 | block[0] = 1; | |
273 | } | |
274 | if (level == 128) | |
275 | put_bits(&s->pb, 8, 0xff); | |
276 | else | |
277 | put_bits(&s->pb, 8, level); | |
278 | i = 1; | |
279 | } else if((block[0]==1 || block[0] == -1) && (s->block_last_index[n] > -1)){ | |
280 | //special case | |
281 | put_bits(&s->pb,2,block[0]>0 ? 2 : 3 ); | |
282 | i = 1; | |
283 | } else { | |
284 | i = 0; | |
285 | } | |
286 | ||
287 | /* AC coefs */ | |
288 | last_index = s->block_last_index[n]; | |
289 | last_non_zero = i - 1; | |
290 | for (; i <= last_index; i++) { | |
291 | j = s->intra_scantable.permutated[i]; | |
292 | level = block[j]; | |
293 | if (level) { | |
294 | run = i - last_non_zero - 1; | |
d632239c PI |
295 | sign = 0; |
296 | slevel = level; | |
297 | if (level < 0) { | |
298 | sign = 1; | |
299 | level = -level; | |
300 | } | |
301 | code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/, run, level); | |
302 | if(run==0 && level < 16) | |
303 | code+=1; | |
304 | put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
305 | if (code == rl->n) { | |
306 | put_bits(&s->pb, 6, run); | |
307 | assert(slevel != 0); | |
308 | assert(level <= 127); | |
6647ab80 | 309 | put_sbits(&s->pb, 8, slevel); |
d632239c PI |
310 | } else { |
311 | put_bits(&s->pb, 1, sign); | |
312 | } | |
313 | last_non_zero = i; | |
314 | } | |
315 | } | |
316 | if(last_index > -1){ | |
317 | put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]);// END OF BLOCK | |
318 | } | |
319 | } | |
320 | ||
ed019b8e AK |
321 | FF_MPV_GENERIC_CLASS(h261) |
322 | ||
d36beb3f | 323 | AVCodec ff_h261_encoder = { |
ec6402b7 AK |
324 | .name = "h261", |
325 | .type = AVMEDIA_TYPE_VIDEO, | |
36ef5369 | 326 | .id = AV_CODEC_ID_H261, |
ec6402b7 | 327 | .priv_data_size = sizeof(H261Context), |
efd29844 | 328 | .init = ff_MPV_encode_init, |
445a7d48 | 329 | .encode2 = ff_MPV_encode_picture, |
efd29844 | 330 | .close = ff_MPV_encode_end, |
716d413c | 331 | .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE }, |
00c3b67b | 332 | .long_name = NULL_IF_CONFIG_SMALL("H.261"), |
ed019b8e | 333 | .priv_class = &h261_class, |
d632239c | 334 | }; |