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