3 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
4 * Copyright (c) 2004 Maarten Daniels
6 * This file is part of Libav.
8 * Libav 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.
13 * Libav 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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "mpegvideo.h"
35 extern uint8_t ff_h261_rl_table_store
[2][2*MAX_RUN
+ MAX_LEVEL
+ 3];
37 static void h261_encode_block(H261Context
* h
, int16_t * block
,
40 int ff_h261_get_picture_format(int width
, int height
){
42 if (width
== 176 && height
== 144)
45 else if (width
== 352 && height
== 288)
52 void ff_h261_encode_picture_header(MpegEncContext
* s
, int picture_number
){
53 H261Context
* h
= (H261Context
*) s
;
56 avpriv_align_put_bits(&s
->pb
);
58 /* Update the pointer to last GOB */
59 s
->ptr_lastgob
= put_bits_ptr(&s
->pb
);
61 put_bits(&s
->pb
, 20, 0x10); /* PSC */
63 temp_ref
= s
->picture_number
* (int64_t)30000 * s
->avctx
->time_base
.num
/
64 (1001 * (int64_t)s
->avctx
->time_base
.den
); //FIXME maybe this should use a timestamp
65 put_sbits(&s
->pb
, 5, temp_ref
); /* TemporalReference */
67 put_bits(&s
->pb
, 1, 0); /* split screen off */
68 put_bits(&s
->pb
, 1, 0); /* camera off */
69 put_bits(&s
->pb
, 1, 0); /* freeze picture release off */
71 format
= ff_h261_get_picture_format(s
->width
, s
->height
);
73 put_bits(&s
->pb
, 1, format
); /* 0 == QCIF, 1 == CIF */
75 put_bits(&s
->pb
, 1, 0); /* still image mode */
76 put_bits(&s
->pb
, 1, 0); /* reserved */
78 put_bits(&s
->pb
, 1, 0); /* no PEI */
87 * Encode a group of blocks header.
89 static void h261_encode_gob_header(MpegEncContext
* s
, int mb_line
){
90 H261Context
* h
= (H261Context
*)s
;
91 if(ff_h261_get_picture_format(s
->width
, s
->height
) == 0){
92 h
->gob_number
+=2; // QCIF
95 h
->gob_number
++; // CIF
97 put_bits(&s
->pb
, 16, 1); /* GBSC */
98 put_bits(&s
->pb
, 4, h
->gob_number
); /* GN */
99 put_bits(&s
->pb
, 5, s
->qscale
); /* GQUANT */
100 put_bits(&s
->pb
, 1, 0); /* no GEI */
107 void ff_h261_reorder_mb_index(MpegEncContext
* s
){
108 int index
= s
->mb_x
+ s
->mb_y
*s
->mb_width
;
111 h261_encode_gob_header(s
,0);
113 /* for CIF the GOB's are fragmented in the middle of a scanline
114 that's why we need to adjust the x and y index of the macroblocks */
115 if(ff_h261_get_picture_format(s
->width
,s
->height
) == 1){ // CIF
116 s
->mb_x
= index
% 11 ; index
/= 11;
117 s
->mb_y
= index
% 3 ; index
/= 3;
118 s
->mb_x
+= 11*(index
% 2); index
/= 2;
121 ff_init_block_index(s
);
122 ff_update_block_index(s
);
126 static void h261_encode_motion(H261Context
* h
, int val
){
127 MpegEncContext
* const s
= &h
->s
;
131 put_bits(&s
->pb
,h261_mv_tab
[code
][1],h261_mv_tab
[code
][0]);
139 code
= sign
? -val
: val
;
140 put_bits(&s
->pb
,h261_mv_tab
[code
][1],h261_mv_tab
[code
][0]);
141 put_bits(&s
->pb
,1,sign
);
145 static inline int get_cbp(MpegEncContext
* s
,
146 int16_t block
[6][64])
150 for (i
= 0; i
< 6; i
++) {
151 if (s
->block_last_index
[i
] >= 0)
156 void ff_h261_encode_mb(MpegEncContext
* s
,
157 int16_t block
[6][64],
158 int motion_x
, int motion_y
)
160 H261Context
* h
= (H261Context
*)s
;
161 int mvd
, mv_diff_x
, mv_diff_y
, i
, cbp
;
162 cbp
= 63; // avoid warning
170 cbp
= get_cbp(s
, block
);
172 /* mvd indicates if this block is motion compensated */
173 mvd
= motion_x
| motion_y
;
175 if((cbp
| mvd
| s
->dquant
) == 0) {
176 /* skip macroblock */
184 /* MB is not skipped, encode MBA */
185 put_bits(&s
->pb
, h261_mba_bits
[(h
->current_mba
-h
->previous_mba
)-1], h261_mba_code
[(h
->current_mba
-h
->previous_mba
)-1]);
187 /* calculate MTYPE */
191 if(mvd
|| s
->loop_filter
)
197 assert(h
->mtype
> 1);
203 put_bits(&s
->pb
, h261_mtype_bits
[h
->mtype
], h261_mtype_code
[h
->mtype
]);
205 h
->mtype
= h261_mtype_map
[h
->mtype
];
207 if(IS_QUANT(h
->mtype
)){
208 ff_set_qscale(s
,s
->qscale
+s
->dquant
);
209 put_bits(&s
->pb
, 5, s
->qscale
);
212 if(IS_16X16(h
->mtype
)){
213 mv_diff_x
= (motion_x
>> 1) - h
->current_mv_x
;
214 mv_diff_y
= (motion_y
>> 1) - h
->current_mv_y
;
215 h
->current_mv_x
= (motion_x
>> 1);
216 h
->current_mv_y
= (motion_y
>> 1);
217 h261_encode_motion(h
,mv_diff_x
);
218 h261_encode_motion(h
,mv_diff_y
);
221 h
->previous_mba
= h
->current_mba
;
223 if(HAS_CBP(h
->mtype
)){
225 put_bits(&s
->pb
,h261_cbp_tab
[cbp
-1][1],h261_cbp_tab
[cbp
-1][0]);
228 /* encode each block */
229 h261_encode_block(h
, block
[i
], i
);
232 if ( ( h
->current_mba
== 11 ) || ( h
->current_mba
== 22 ) || ( h
->current_mba
== 33 ) || ( !IS_16X16 ( h
->mtype
) )){
238 void ff_h261_encode_init(MpegEncContext
*s
){
243 ff_init_rl(&h261_rl_tcoeff
, ff_h261_rl_table_store
);
249 s
->c_dc_scale_table
= ff_mpeg1_dc_scale_table
;
254 * Encode an 8x8 block.
255 * @param block the 8x8 block
256 * @param n block index (0-3 are luma, 4-5 are chroma)
258 static void h261_encode_block(H261Context
* h
, int16_t * block
, int n
){
259 MpegEncContext
* const s
= &h
->s
;
260 int level
, run
, i
, j
, last_index
, last_non_zero
, sign
, slevel
, code
;
263 rl
= &h261_rl_tcoeff
;
267 /* 255 cannot be represented, so we clamp */
272 /* 0 cannot be represented also */
273 else if (level
< 1) {
278 put_bits(&s
->pb
, 8, 0xff);
280 put_bits(&s
->pb
, 8, level
);
282 } else if((block
[0]==1 || block
[0] == -1) && (s
->block_last_index
[n
] > -1)){
284 put_bits(&s
->pb
,2,block
[0]>0 ? 2 : 3 );
291 last_index
= s
->block_last_index
[n
];
292 last_non_zero
= i
- 1;
293 for (; i
<= last_index
; i
++) {
294 j
= s
->intra_scantable
.permutated
[i
];
297 run
= i
- last_non_zero
- 1;
304 code
= get_rl_index(rl
, 0 /*no last in H.261, EOB is used*/, run
, level
);
305 if(run
==0 && level
< 16)
307 put_bits(&s
->pb
, rl
->table_vlc
[code
][1], rl
->table_vlc
[code
][0]);
309 put_bits(&s
->pb
, 6, run
);
311 assert(level
<= 127);
312 put_sbits(&s
->pb
, 8, slevel
);
314 put_bits(&s
->pb
, 1, sign
);
320 put_bits(&s
->pb
, rl
->table_vlc
[0][1], rl
->table_vlc
[0][0]);// END OF BLOCK
324 FF_MPV_GENERIC_CLASS(h261
)
326 AVCodec ff_h261_encoder
= {
328 .type
= AVMEDIA_TYPE_VIDEO
,
329 .id
= AV_CODEC_ID_H261
,
330 .priv_data_size
= sizeof(H261Context
),
331 .init
= ff_MPV_encode_init
,
332 .encode2
= ff_MPV_encode_picture
,
333 .close
= ff_MPV_encode_end
,
334 .pix_fmts
= (const enum AVPixelFormat
[]){ AV_PIX_FMT_YUV420P
, AV_PIX_FMT_NONE
},
335 .long_name
= NULL_IF_CONFIG_SMALL("H.261"),
336 .priv_class
= &h261_class
,