30ba1379186475a09f74400f0f4689629129fdee
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
28 #include "libavutil/attributes.h"
30 #include "mpegutils.h"
31 #include "mpegvideo.h"
34 #include "mpegvideodata.h"
36 int ff_h261_get_picture_format(int width
, int height
)
39 if (width
== 176 && height
== 144)
42 else if (width
== 352 && height
== 288)
49 void ff_h261_encode_picture_header(MpegEncContext
*s
, int picture_number
)
51 H261Context
*h
= (H261Context
*)s
;
54 avpriv_align_put_bits(&s
->pb
);
56 /* Update the pointer to last GOB */
57 s
->ptr_lastgob
= put_bits_ptr(&s
->pb
);
59 put_bits(&s
->pb
, 20, 0x10); /* PSC */
61 temp_ref
= s
->picture_number
* (int64_t)30000 * s
->avctx
->time_base
.num
/
62 (1001 * (int64_t)s
->avctx
->time_base
.den
); // FIXME maybe this should use a timestamp
63 put_sbits(&s
->pb
, 5, temp_ref
); /* TemporalReference */
65 put_bits(&s
->pb
, 1, 0); /* split screen off */
66 put_bits(&s
->pb
, 1, 0); /* camera off */
67 put_bits(&s
->pb
, 1, 0); /* freeze picture release off */
69 format
= ff_h261_get_picture_format(s
->width
, s
->height
);
71 put_bits(&s
->pb
, 1, format
); /* 0 == QCIF, 1 == CIF */
73 put_bits(&s
->pb
, 1, 1); /* still image mode */
74 put_bits(&s
->pb
, 1, 1); /* reserved */
76 put_bits(&s
->pb
, 1, 0); /* no PEI */
85 * Encode a group of blocks header.
87 static void h261_encode_gob_header(MpegEncContext
*s
, int mb_line
)
89 H261Context
*h
= (H261Context
*)s
;
90 if (ff_h261_get_picture_format(s
->width
, s
->height
) == 0) {
91 h
->gob_number
+= 2; // QCIF
93 h
->gob_number
++; // CIF
95 put_bits(&s
->pb
, 16, 1); /* GBSC */
96 put_bits(&s
->pb
, 4, h
->gob_number
); /* GN */
97 put_bits(&s
->pb
, 5, s
->qscale
); /* GQUANT */
98 put_bits(&s
->pb
, 1, 0); /* no GEI */
105 void ff_h261_reorder_mb_index(MpegEncContext
*s
)
107 int index
= s
->mb_x
+ s
->mb_y
* s
->mb_width
;
110 h261_encode_gob_header(s
, 0);
112 /* for CIF the GOB's are fragmented in the middle of a scanline
113 * that's why we need to adjust the x and y index of the macroblocks */
114 if (ff_h261_get_picture_format(s
->width
, s
->height
) == 1) { // CIF
115 s
->mb_x
= index
% 11;
119 s
->mb_x
+= 11 * (index
% 2);
121 s
->mb_y
+= 3 * index
;
123 ff_init_block_index(s
);
124 ff_update_block_index(s
);
128 static void h261_encode_motion(H261Context
*h
, int val
)
130 MpegEncContext
*const s
= &h
->s
;
134 put_bits(&s
->pb
, ff_h261_mv_tab
[code
][1], ff_h261_mv_tab
[code
][0]);
141 code
= sign ?
-val
: val
;
142 put_bits(&s
->pb
, ff_h261_mv_tab
[code
][1], ff_h261_mv_tab
[code
][0]);
143 put_bits(&s
->pb
, 1, sign
);
147 static inline int get_cbp(MpegEncContext
*s
, int16_t block
[6][64])
151 for (i
= 0; i
< 6; i
++)
152 if (s
->block_last_index
[i
] >= 0)
158 * Encode an 8x8 block.
159 * @param block the 8x8 block
160 * @param n block index (0-3 are luma, 4-5 are chroma)
162 static void h261_encode_block(H261Context
*h
, int16_t *block
, int n
)
164 MpegEncContext
*const s
= &h
->s
;
165 int level
, run
, i
, j
, last_index
, last_non_zero
, sign
, slevel
, code
;
168 rl
= &ff_h261_rl_tcoeff
;
172 /* 255 cannot be represented, so we clamp */
177 /* 0 cannot be represented also */
178 else if (level
< 1) {
183 put_bits(&s
->pb
, 8, 0xff);
185 put_bits(&s
->pb
, 8, level
);
187 } else if ((block
[0] == 1 || block
[0] == -1) &&
188 (s
->block_last_index
[n
] > -1)) {
190 put_bits(&s
->pb
, 2, block
[0] > 0 ?
2 : 3);
197 last_index
= s
->block_last_index
[n
];
198 last_non_zero
= i
- 1;
199 for (; i
<= last_index
; i
++) {
200 j
= s
->intra_scantable
.permutated
[i
];
203 run
= i
- last_non_zero
- 1;
210 code
= get_rl_index(rl
, 0 /*no last in H.261, EOB is used*/,
212 if (run
== 0 && level
< 16)
214 put_bits(&s
->pb
, rl
->table_vlc
[code
][1], rl
->table_vlc
[code
][0]);
216 put_bits(&s
->pb
, 6, run
);
218 assert(level
<= 127);
219 put_sbits(&s
->pb
, 8, slevel
);
221 put_bits(&s
->pb
, 1, sign
);
227 put_bits(&s
->pb
, rl
->table_vlc
[0][1], rl
->table_vlc
[0][0]); // EOB
230 void ff_h261_encode_mb(MpegEncContext
*s
, int16_t block
[6][64],
231 int motion_x
, int motion_y
)
233 H261Context
*h
= (H261Context
*)s
;
234 int mvd
, mv_diff_x
, mv_diff_y
, i
, cbp
;
235 cbp
= 63; // avoid warning
243 cbp
= get_cbp(s
, block
);
245 /* mvd indicates if this block is motion compensated */
246 mvd
= motion_x
| motion_y
;
248 if ((cbp
| mvd
| s
->dquant
) == 0) {
249 /* skip macroblock */
257 /* MB is not skipped, encode MBA */
259 ff_h261_mba_bits
[(h
->current_mba
- h
->previous_mba
) - 1],
260 ff_h261_mba_code
[(h
->current_mba
- h
->previous_mba
) - 1]);
262 /* calculate MTYPE */
266 if (mvd
|| s
->loop_filter
)
270 if (cbp
|| s
->dquant
)
272 assert(h
->mtype
> 1);
279 ff_h261_mtype_bits
[h
->mtype
],
280 ff_h261_mtype_code
[h
->mtype
]);
282 h
->mtype
= ff_h261_mtype_map
[h
->mtype
];
284 if (IS_QUANT(h
->mtype
)) {
285 ff_set_qscale(s
, s
->qscale
+ s
->dquant
);
286 put_bits(&s
->pb
, 5, s
->qscale
);
289 if (IS_16X16(h
->mtype
)) {
290 mv_diff_x
= (motion_x
>> 1) - h
->current_mv_x
;
291 mv_diff_y
= (motion_y
>> 1) - h
->current_mv_y
;
292 h
->current_mv_x
= (motion_x
>> 1);
293 h
->current_mv_y
= (motion_y
>> 1);
294 h261_encode_motion(h
, mv_diff_x
);
295 h261_encode_motion(h
, mv_diff_y
);
298 h
->previous_mba
= h
->current_mba
;
300 if (HAS_CBP(h
->mtype
)) {
303 ff_h261_cbp_tab
[cbp
- 1][1],
304 ff_h261_cbp_tab
[cbp
- 1][0]);
306 for (i
= 0; i
< 6; i
++)
307 /* encode each block */
308 h261_encode_block(h
, block
[i
], i
);
310 if ((h
->current_mba
== 11) || (h
->current_mba
== 22) ||
311 (h
->current_mba
== 33) || (!IS_16X16(h
->mtype
))) {
317 av_cold
void ff_h261_encode_init(MpegEncContext
*s
)
319 ff_h261_common_init();
321 s
->min_qcoeff
= -127;
323 s
->y_dc_scale_table
=
324 s
->c_dc_scale_table
= ff_mpeg1_dc_scale_table
;
327 static const AVClass h261_class
= {
328 .class_name
= "h261 encoder",
329 .item_name
= av_default_item_name
,
330 .option
= ff_mpv_generic_options
,
331 .version
= LIBAVUTIL_VERSION_INT
,
334 AVCodec ff_h261_encoder
= {
336 .long_name
= NULL_IF_CONFIG_SMALL("H.261"),
337 .type
= AVMEDIA_TYPE_VIDEO
,
338 .id
= AV_CODEC_ID_H261
,
339 .priv_data_size
= sizeof(H261Context
),
340 .init
= ff_mpv_encode_init
,
341 .encode2
= ff_mpv_encode_picture
,
342 .close
= ff_mpv_encode_end
,
343 .pix_fmts
= (const enum AVPixelFormat
[]) { AV_PIX_FMT_YUV420P
,
345 .priv_class
= &h261_class
,