Libav
h261enc.c
Go to the documentation of this file.
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 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 "mpegvideo.h"
31 #include "h263.h"
32 #include "h261.h"
33 
35 {
36  // QCIF
37  if (width == 176 && height == 144)
38  return 0;
39  // CIF
40  else if (width == 352 && height == 288)
41  return 1;
42  // ERROR
43  else
44  return -1;
45 }
46 
47 void ff_h261_encode_picture_header(MpegEncContext *s, int picture_number)
48 {
49  H261Context *h = (H261Context *)s;
50  int format, temp_ref;
51 
53 
54  /* Update the pointer to last GOB */
55  s->ptr_lastgob = put_bits_ptr(&s->pb);
56 
57  put_bits(&s->pb, 20, 0x10); /* PSC */
58 
59  temp_ref = s->picture_number * (int64_t)30000 * s->avctx->time_base.num /
60  (1001 * (int64_t)s->avctx->time_base.den); // FIXME maybe this should use a timestamp
61  put_sbits(&s->pb, 5, temp_ref); /* TemporalReference */
62 
63  put_bits(&s->pb, 1, 0); /* split screen off */
64  put_bits(&s->pb, 1, 0); /* camera off */
65  put_bits(&s->pb, 1, 0); /* freeze picture release off */
66 
67  format = ff_h261_get_picture_format(s->width, s->height);
68 
69  put_bits(&s->pb, 1, format); /* 0 == QCIF, 1 == CIF */
70 
71  put_bits(&s->pb, 1, 0); /* still image mode */
72  put_bits(&s->pb, 1, 0); /* reserved */
73 
74  put_bits(&s->pb, 1, 0); /* no PEI */
75  if (format == 0)
76  h->gob_number = -1;
77  else
78  h->gob_number = 0;
79  h->current_mba = 0;
80 }
81 
85 static void h261_encode_gob_header(MpegEncContext *s, int mb_line)
86 {
87  H261Context *h = (H261Context *)s;
88  if (ff_h261_get_picture_format(s->width, s->height) == 0) {
89  h->gob_number += 2; // QCIF
90  } else {
91  h->gob_number++; // CIF
92  }
93  put_bits(&s->pb, 16, 1); /* GBSC */
94  put_bits(&s->pb, 4, h->gob_number); /* GN */
95  put_bits(&s->pb, 5, s->qscale); /* GQUANT */
96  put_bits(&s->pb, 1, 0); /* no GEI */
97  h->current_mba = 0;
98  h->previous_mba = 0;
99  h->current_mv_x = 0;
100  h->current_mv_y = 0;
101 }
102 
104 {
105  int index = s->mb_x + s->mb_y * s->mb_width;
106 
107  if (index % 33 == 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;
114  index /= 11;
115  s->mb_y = index % 3;
116  index /= 3;
117  s->mb_x += 11 * (index % 2);
118  index /= 2;
119  s->mb_y += 3 * index;
120 
123  }
124 }
125 
126 static void h261_encode_motion(H261Context *h, int val)
127 {
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  } else {
134  if (val > 15)
135  val -= 32;
136  if (val < -16)
137  val += 32;
138  sign = val < 0;
139  code = sign ? -val : val;
140  put_bits(&s->pb, ff_h261_mv_tab[code][1], ff_h261_mv_tab[code][0]);
141  put_bits(&s->pb, 1, sign);
142  }
143 }
144 
145 static inline int get_cbp(MpegEncContext *s, int16_t block[6][64])
146 {
147  int i, cbp;
148  cbp = 0;
149  for (i = 0; i < 6; i++)
150  if (s->block_last_index[i] >= 0)
151  cbp |= 1 << (5 - i);
152  return cbp;
153 }
154 
160 static void h261_encode_block(H261Context *h, int16_t *block, int n)
161 {
162  MpegEncContext *const s = &h->s;
163  int level, run, i, j, last_index, last_non_zero, sign, slevel, code;
164  RLTable *rl;
165 
166  rl = &ff_h261_rl_tcoeff;
167  if (s->mb_intra) {
168  /* DC coef */
169  level = block[0];
170  /* 255 cannot be represented, so we clamp */
171  if (level > 254) {
172  level = 254;
173  block[0] = 254;
174  }
175  /* 0 cannot be represented also */
176  else if (level < 1) {
177  level = 1;
178  block[0] = 1;
179  }
180  if (level == 128)
181  put_bits(&s->pb, 8, 0xff);
182  else
183  put_bits(&s->pb, 8, level);
184  i = 1;
185  } else if ((block[0] == 1 || block[0] == -1) &&
186  (s->block_last_index[n] > -1)) {
187  // special case
188  put_bits(&s->pb, 2, block[0] > 0 ? 2 : 3);
189  i = 1;
190  } else {
191  i = 0;
192  }
193 
194  /* AC coefs */
195  last_index = s->block_last_index[n];
196  last_non_zero = i - 1;
197  for (; i <= last_index; i++) {
198  j = s->intra_scantable.permutated[i];
199  level = block[j];
200  if (level) {
201  run = i - last_non_zero - 1;
202  sign = 0;
203  slevel = level;
204  if (level < 0) {
205  sign = 1;
206  level = -level;
207  }
208  code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/,
209  run, level);
210  if (run == 0 && level < 16)
211  code += 1;
212  put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
213  if (code == rl->n) {
214  put_bits(&s->pb, 6, run);
215  assert(slevel != 0);
216  assert(level <= 127);
217  put_sbits(&s->pb, 8, slevel);
218  } else {
219  put_bits(&s->pb, 1, sign);
220  }
221  last_non_zero = i;
222  }
223  }
224  if (last_index > -1)
225  put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]); // EOB
226 }
227 
228 void ff_h261_encode_mb(MpegEncContext *s, int16_t block[6][64],
229  int motion_x, int motion_y)
230 {
231  H261Context *h = (H261Context *)s;
232  int mvd, mv_diff_x, mv_diff_y, i, cbp;
233  cbp = 63; // avoid warning
234  mvd = 0;
235 
236  h->current_mba++;
237  h->mtype = 0;
238 
239  if (!s->mb_intra) {
240  /* compute cbp */
241  cbp = get_cbp(s, block);
242 
243  /* mvd indicates if this block is motion compensated */
244  mvd = motion_x | motion_y;
245 
246  if ((cbp | mvd | s->dquant) == 0) {
247  /* skip macroblock */
248  s->skip_count++;
249  h->current_mv_x = 0;
250  h->current_mv_y = 0;
251  return;
252  }
253  }
254 
255  /* MB is not skipped, encode MBA */
256  put_bits(&s->pb,
258  ff_h261_mba_code[(h->current_mba - h->previous_mba) - 1]);
259 
260  /* calculate MTYPE */
261  if (!s->mb_intra) {
262  h->mtype++;
263 
264  if (mvd || s->loop_filter)
265  h->mtype += 3;
266  if (s->loop_filter)
267  h->mtype += 3;
268  if (cbp || s->dquant)
269  h->mtype++;
270  assert(h->mtype > 1);
271  }
272 
273  if (s->dquant)
274  h->mtype++;
275 
276  put_bits(&s->pb,
279 
280  h->mtype = ff_h261_mtype_map[h->mtype];
281 
282  if (IS_QUANT(h->mtype)) {
283  ff_set_qscale(s, s->qscale + s->dquant);
284  put_bits(&s->pb, 5, s->qscale);
285  }
286 
287  if (IS_16X16(h->mtype)) {
288  mv_diff_x = (motion_x >> 1) - h->current_mv_x;
289  mv_diff_y = (motion_y >> 1) - h->current_mv_y;
290  h->current_mv_x = (motion_x >> 1);
291  h->current_mv_y = (motion_y >> 1);
292  h261_encode_motion(h, mv_diff_x);
293  h261_encode_motion(h, mv_diff_y);
294  }
295 
296  h->previous_mba = h->current_mba;
297 
298  if (HAS_CBP(h->mtype)) {
299  assert(cbp > 0);
300  put_bits(&s->pb,
301  ff_h261_cbp_tab[cbp - 1][1],
302  ff_h261_cbp_tab[cbp - 1][0]);
303  }
304  for (i = 0; i < 6; i++)
305  /* encode each block */
306  h261_encode_block(h, block[i], i);
307 
308  if ((h->current_mba == 11) || (h->current_mba == 22) ||
309  (h->current_mba == 33) || (!IS_16X16(h->mtype))) {
310  h->current_mv_x = 0;
311  h->current_mv_y = 0;
312  }
313 }
314 
316 {
318 
319  s->min_qcoeff = -127;
320  s->max_qcoeff = 127;
321  s->y_dc_scale_table =
323 }
324 
326 
328  .name = "h261",
329  .long_name = NULL_IF_CONFIG_SMALL("H.261"),
330  .type = AVMEDIA_TYPE_VIDEO,
331  .id = AV_CODEC_ID_H261,
332  .priv_data_size = sizeof(H261Context),
334  .encode2 = ff_MPV_encode_picture,
336  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
337  AV_PIX_FMT_NONE },
338  .priv_class = &h261_class,
339 };