Libav
h261enc.c
Go to the documentation of this file.
1 /*
2  * H.261 encoder
3  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
4  * Copyright (c) 2004 Maarten Daniels
5  *
6  * This file is part of Libav.
7  *
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.
12  *
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.
17  *
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
21  */
22 
28 #include "libavutil/attributes.h"
29 #include "avcodec.h"
30 #include "mpegutils.h"
31 #include "mpegvideo.h"
32 #include "h263.h"
33 #include "h261.h"
34 #include "mpegvideodata.h"
35 
37 {
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 {
51  H261Context *h = (H261Context *)s;
52  int format, temp_ref;
53 
55 
56  /* Update the pointer to last GOB */
57  s->ptr_lastgob = put_bits_ptr(&s->pb);
58 
59  put_bits(&s->pb, 20, 0x10); /* PSC */
60 
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 */
64 
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, s->pict_type == AV_PICTURE_TYPE_I); /* freeze picture release on/off */
68 
69  format = ff_h261_get_picture_format(s->width, s->height);
70 
71  put_bits(&s->pb, 1, format); /* 0 == QCIF, 1 == CIF */
72 
73  put_bits(&s->pb, 1, 1); /* still image mode */
74  put_bits(&s->pb, 1, 1); /* reserved */
75 
76  put_bits(&s->pb, 1, 0); /* no PEI */
77  if (format == 0)
78  h->gob_number = -1;
79  else
80  h->gob_number = 0;
81  h->current_mba = 0;
82 }
83 
87 static void h261_encode_gob_header(MpegEncContext *s, int mb_line)
88 {
89  H261Context *h = (H261Context *)s;
90  if (ff_h261_get_picture_format(s->width, s->height) == 0) {
91  h->gob_number += 2; // QCIF
92  } else {
93  h->gob_number++; // CIF
94  }
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 */
99  h->current_mba = 0;
100  h->previous_mba = 0;
101  h->current_mv_x = 0;
102  h->current_mv_y = 0;
103 }
104 
106 {
107  int index = s->mb_x + s->mb_y * s->mb_width;
108 
109  if (index % 33 == 0)
111 
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;
116  index /= 11;
117  s->mb_y = index % 3;
118  index /= 3;
119  s->mb_x += 11 * (index % 2);
120  index /= 2;
121  s->mb_y += 3 * index;
122 
125  }
126 }
127 
128 static void h261_encode_motion(H261Context *h, int val)
129 {
130  MpegEncContext *const s = &h->s;
131  int sign, code;
132  if (val == 0) {
133  code = 0;
134  put_bits(&s->pb, ff_h261_mv_tab[code][1], ff_h261_mv_tab[code][0]);
135  } else {
136  if (val > 15)
137  val -= 32;
138  if (val < -16)
139  val += 32;
140  sign = val < 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);
144  }
145 }
146 
147 static inline int get_cbp(MpegEncContext *s, int16_t 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  return cbp;
155 }
156 
162 static void h261_encode_block(H261Context *h, int16_t *block, int n)
163 {
164  MpegEncContext *const s = &h->s;
165  int level, run, i, j, last_index, last_non_zero, sign, slevel, code;
166  RLTable *rl;
167 
168  rl = &ff_h261_rl_tcoeff;
169  if (s->mb_intra) {
170  /* DC coef */
171  level = block[0];
172  /* 255 cannot be represented, so we clamp */
173  if (level > 254) {
174  level = 254;
175  block[0] = 254;
176  }
177  /* 0 cannot be represented also */
178  else if (level < 1) {
179  level = 1;
180  block[0] = 1;
181  }
182  if (level == 128)
183  put_bits(&s->pb, 8, 0xff);
184  else
185  put_bits(&s->pb, 8, level);
186  i = 1;
187  } else if ((block[0] == 1 || block[0] == -1) &&
188  (s->block_last_index[n] > -1)) {
189  // special case
190  put_bits(&s->pb, 2, block[0] > 0 ? 2 : 3);
191  i = 1;
192  } else {
193  i = 0;
194  }
195 
196  /* AC coefs */
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];
201  level = block[j];
202  if (level) {
203  run = i - last_non_zero - 1;
204  sign = 0;
205  slevel = level;
206  if (level < 0) {
207  sign = 1;
208  level = -level;
209  }
210  code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/,
211  run, level);
212  if (run == 0 && level < 16)
213  code += 1;
214  put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
215  if (code == rl->n) {
216  put_bits(&s->pb, 6, run);
217  assert(slevel != 0);
218  assert(level <= 127);
219  put_sbits(&s->pb, 8, slevel);
220  } else {
221  put_bits(&s->pb, 1, sign);
222  }
223  last_non_zero = i;
224  }
225  }
226  if (last_index > -1)
227  put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]); // EOB
228 }
229 
230 void ff_h261_encode_mb(MpegEncContext *s, int16_t block[6][64],
231  int motion_x, int motion_y)
232 {
233  H261Context *h = (H261Context *)s;
234  int mvd, mv_diff_x, mv_diff_y, i, cbp;
235  cbp = 63; // avoid warning
236  mvd = 0;
237 
238  h->current_mba++;
239  h->mtype = 0;
240 
241  if (!s->mb_intra) {
242  /* compute cbp */
243  cbp = get_cbp(s, block);
244 
245  /* mvd indicates if this block is motion compensated */
246  mvd = motion_x | motion_y;
247 
248  if ((cbp | mvd | s->dquant) == 0) {
249  /* skip macroblock */
250  s->skip_count++;
251  h->current_mv_x = 0;
252  h->current_mv_y = 0;
253  return;
254  }
255  }
256 
257  /* MB is not skipped, encode MBA */
258  put_bits(&s->pb,
260  ff_h261_mba_code[(h->current_mba - h->previous_mba) - 1]);
261 
262  /* calculate MTYPE */
263  if (!s->mb_intra) {
264  h->mtype++;
265 
266  if (mvd || s->loop_filter)
267  h->mtype += 3;
268  if (s->loop_filter)
269  h->mtype += 3;
270  if (cbp || s->dquant)
271  h->mtype++;
272  assert(h->mtype > 1);
273  }
274 
275  if (s->dquant)
276  h->mtype++;
277 
278  put_bits(&s->pb,
281 
282  h->mtype = ff_h261_mtype_map[h->mtype];
283 
284  if (IS_QUANT(h->mtype)) {
285  ff_set_qscale(s, s->qscale + s->dquant);
286  put_bits(&s->pb, 5, s->qscale);
287  }
288 
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);
296  }
297 
298  h->previous_mba = h->current_mba;
299 
300  if (HAS_CBP(h->mtype)) {
301  assert(cbp > 0);
302  put_bits(&s->pb,
303  ff_h261_cbp_tab[cbp - 1][1],
304  ff_h261_cbp_tab[cbp - 1][0]);
305  }
306  for (i = 0; i < 6; i++)
307  /* encode each block */
308  h261_encode_block(h, block[i], i);
309 
310  if ((h->current_mba == 11) || (h->current_mba == 22) ||
311  (h->current_mba == 33) || (!IS_16X16(h->mtype))) {
312  h->current_mv_x = 0;
313  h->current_mv_y = 0;
314  }
315 }
316 
318 {
320 
321  s->min_qcoeff = -127;
322  s->max_qcoeff = 127;
323  s->y_dc_scale_table =
325 }
326 
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,
332 };
333 
335  .name = "h261",
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),
341  .encode2 = ff_mpv_encode_picture,
342  .close = ff_mpv_encode_end,
343  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
344  AV_PIX_FMT_NONE },
345  .priv_class = &h261_class,
346 };
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:1744
int picture_number
Definition: mpegvideo.h:122
const uint8_t * y_dc_scale_table
qscale -> y_dc_scale table
Definition: mpegvideo.h:183
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:172
void ff_h261_encode_mb(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
Definition: h261enc.c:230
MpegEncContext s
Definition: h261.h:38
const uint8_t ff_h261_mba_bits[35]
Definition: h261data.c:48
int num
numerator
Definition: rational.h:44
int previous_mba
Definition: h261.h:41
void avpriv_align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
Definition: bitstream.c:46
int current_mv_x
Definition: h261.h:44
H261Context.
Definition: h261.h:37
int min_qcoeff
minimum encodable coefficient
Definition: mpegvideo.h:301
int gob_number
Definition: h261.h:46
mpegvideo header.
#define HAS_CBP(a)
Definition: mpegutils.h:103
uint8_t permutated[64]
Definition: idctdsp.h:31
void ff_h261_encode_picture_header(MpegEncContext *s, int picture_number)
Definition: h261enc.c:49
uint8_t run
Definition: svq3.c:203
AVCodec.
Definition: avcodec.h:3120
const uint8_t ff_h261_mba_code[35]
Definition: h261data.c:34
RLTable.
Definition: rl.h:39
int qscale
QP.
Definition: mpegvideo.h:199
Macro definitions for various function/variable attributes.
const uint8_t ff_h261_cbp_tab[63][2]
Definition: h261data.c:95
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1535
static int16_t block[64]
Definition: dct.c:97
AVCodec ff_h261_encoder
Definition: h261enc.c:334
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:39
uint8_t * ptr_lastgob
Definition: mpegvideo.h:473
const AVOption ff_mpv_generic_options[]
Definition: mpegvideo_enc.c:80
const uint8_t ff_mpeg1_dc_scale_table[128]
Definition: mpegvideodata.c:27
#define av_cold
Definition: attributes.h:66
int current_mv_y
Definition: h261.h:45
static void h261_encode_gob_header(MpegEncContext *s, int mb_line)
Encode a group of blocks header.
Definition: h261enc.c:87
int max_qcoeff
maximum encodable coefficient
Definition: mpegvideo.h:302
int dquant
qscale difference to prev qscale
Definition: mpegvideo.h:205
static void ff_update_block_index(MpegEncContext *s)
Definition: mpegvideo.h:680
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
Definition: mpegvideo.c:1806
static void h261_encode_block(H261Context *h, int16_t *block, int n)
Encode an 8x8 block.
Definition: h261enc.c:162
av_cold void ff_h261_common_init(void)
Definition: h261.c:83
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
Definition: put_bits.h:199
static const AVClass h261_class
Definition: h261enc.c:327
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
int mtype
Definition: h261.h:43
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:134
int n
number of entries of table_vlc minus 1
Definition: rl.h:40
const uint16_t(* table_vlc)[2]
Definition: rl.h:42
int current_mba
Definition: h261.h:40
int block_last_index[12]
last non zero coefficient in block
Definition: mpegvideo.h:81
const uint8_t ff_h261_mtype_code[10]
Definition: h261data.c:63
const uint8_t ff_h261_mtype_bits[10]
Definition: h261data.c:69
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
const uint8_t ff_h261_mv_tab[17][2]
Definition: h261data.c:89
const int ff_h261_mtype_map[10]
Definition: h261data.c:75
static int width
Definition: utils.c:156
Libavcodec external API header.
av_default_item_name
Definition: dnxhdenc.c:55
void ff_h261_reorder_mb_index(MpegEncContext *s)
Definition: h261enc.c:105
ScanTable intra_scantable
Definition: mpegvideo.h:86
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:95
#define IS_QUANT(a)
Definition: mpegutils.h:97
int ff_h261_get_picture_format(int width, int height)
Definition: h261enc.c:36
int ff_mpv_encode_init(AVCodecContext *avctx)
H.261 codec.
const char * format
Definition: movenc.c:47
Describe the class of an AVClass context structure.
Definition: log.h:34
#define IS_16X16(a)
Definition: mpegutils.h:88
static int get_cbp(MpegEncContext *s, int16_t block[6][64])
Definition: h261enc.c:147
int index
Definition: gxfenc.c:72
static void h261_encode_motion(H261Context *h, int val)
Definition: h261enc.c:128
RLTable ff_h261_rl_tcoeff
Definition: h261data.c:150
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:206
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:257
const uint8_t * c_dc_scale_table
qscale -> c_dc_scale table
Definition: mpegvideo.h:184
uint8_t level
Definition: svq3.c:204
int height
Definition: gxfenc.c:72
MpegEncContext.
Definition: mpegvideo.h:76
struct AVCodecContext * avctx
Definition: mpegvideo.h:93
PutBitContext pb
bit output
Definition: mpegvideo.h:146
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:59
int den
denominator
Definition: rational.h:45
int ff_mpv_encode_end(AVCodecContext *avctx)
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
static int get_rl_index(const RLTable *rl, int last, int run, int level)
Definition: rl.h:80
int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
av_cold void ff_h261_encode_init(MpegEncContext *s)
Definition: h261enc.c:317
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57