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 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 "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 
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 
82 /**
83  * Encode a group of blocks header.
84  */
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 
155 /**
156  * Encode an 8x8 block.
157  * @param block the 8x8 block
158  * @param n block index (0-3 are luma, 4-5 are chroma)
159  */
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  av_assert1(slevel != 0);
216  av_assert1(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  av_assert1(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  av_assert1(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  .type = AVMEDIA_TYPE_VIDEO,
330  .id = AV_CODEC_ID_H261,
331  .priv_data_size = sizeof(H261Context),
333  .encode2 = ff_MPV_encode_picture,
335  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
336  AV_PIX_FMT_NONE },
337  .long_name = NULL_IF_CONFIG_SMALL("H.261"),
338  .priv_class = &h261_class,
339 };
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:3005
struct H261Context H261Context
H261Context.
int picture_number
Definition: mpegvideo.h:275
const char * s
Definition: avisynth_c.h:668
const uint8_t * y_dc_scale_table
qscale -> y_dc_scale table
Definition: mpegvideo.h:351
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:172
#define FF_MPV_GENERIC_CLASS(name)
Definition: mpegvideo.h:772
void ff_h261_encode_mb(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
Definition: h261enc.c:228
MpegEncContext s
Definition: h261.h:38
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int ff_MPV_encode_end(AVCodecContext *avctx)
const uint8_t ff_h261_mba_bits[35]
Definition: h261data.c:47
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:479
int gob_number
Definition: h261.h:46
mpegvideo header.
uint8_t permutated[64]
Definition: dsputil.h:116
void ff_h261_encode_picture_header(MpegEncContext *s, int picture_number)
Definition: h261enc.c:47
uint8_t run
Definition: svq3.c:136
const uint8_t ff_h261_mba_code[35]
Definition: h261data.c:33
RLTable.
Definition: rl.h:38
int qscale
QP.
Definition: mpegvideo.h:369
const uint8_t ff_h261_cbp_tab[63][2]
Definition: h261data.c:94
#define HAS_CBP(a)
Definition: mpegvideo.h:157
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
AVCodec ff_h261_encoder
Definition: h261enc.c:327
uint8_t * ptr_lastgob
Definition: mpegvideo.h:696
int current_mv_y
Definition: h261.h:45
#define IS_QUANT(a)
Definition: mpegvideo.h:154
static void h261_encode_gob_header(MpegEncContext *s, int mb_line)
Encode a group of blocks header.
Definition: h261enc.c:85
int max_qcoeff
maximum encodable coefficient
Definition: mpegvideo.h:480
void ff_h261_encode_init(MpegEncContext *s)
Definition: h261enc.c:315
int dquant
qscale difference to prev qscale
Definition: mpegvideo.h:375
static void ff_update_block_index(MpegEncContext *s)
Definition: mpegvideo.h:861
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
Definition: mpegvideo.c:3300
int ff_MPV_encode_init(AVCodecContext *avctx)
static void h261_encode_block(H261Context *h, int16_t *block, int n)
Encode an 8x8 block.
Definition: h261enc.c:160
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
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
simple assert() macros that are a bit more flexible than ISO C assert().
int mtype
Definition: h261.h:43
const char * name
Name of the codec implementation.
static void put_bits(J2kEncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
external API header
int n
number of entries of table_vlc minus 1
Definition: rl.h:39
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
const uint16_t(* table_vlc)[2]
Definition: rl.h:41
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate.The lists are not just lists
int current_mba
Definition: h261.h:40
int block_last_index[12]
last non zero coefficient in block
Definition: mpegvideo.h:291
const uint8_t ff_h261_mtype_code[10]
Definition: h261data.c:62
FIXME Range Coding of cr are level
Definition: snow.txt:367
const uint8_t ff_h261_mtype_bits[10]
Definition: h261data.c:68
const uint8_t ff_h261_mv_tab[17][2]
Definition: h261data.c:88
or the Software in violation of any applicable export control laws in any jurisdiction Except as provided by mandatorily applicable UPF has no obligation to provide you with source code to the Software In the event Software contains any source code
const int ff_h261_mtype_map[10]
Definition: h261data.c:74
static int width
Definition: tests/utils.c:158
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
void ff_h261_reorder_mb_index(MpegEncContext *s)
Definition: h261enc.c:103
ScanTable intra_scantable
Definition: mpegvideo.h:296
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:245
const uint8_t ff_mpeg1_dc_scale_table[128]
Definition: mpegvideo.c:72
int ff_h261_get_picture_format(int width, int height)
Definition: h261enc.c:34
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
h261codec.
static int get_cbp(MpegEncContext *s, int16_t block[6][64])
Definition: h261enc.c:145
int index
Definition: gxfenc.c:89
synthesis window for stochastic i
static void h261_encode_motion(H261Context *h, int val)
Definition: h261enc.c:126
RLTable ff_h261_rl_tcoeff
Definition: h261data.c:149
const uint8_t * c_dc_scale_table
qscale -> c_dc_scale table
Definition: mpegvideo.h:352
MpegEncContext.
Definition: mpegvideo.h:241
struct AVCodecContext * avctx
Definition: mpegvideo.h:243
PutBitContext pb
bit output
Definition: mpegvideo.h:314
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
int den
denominator
Definition: rational.h:45
static int get_rl_index(const RLTable *rl, int last, int run, int level)
Definition: rl.h:75
#define IS_16X16(a)
Definition: mpegvideo.h:145
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
int ff_MPV_encode_picture(AVCodecContext *avctx, AVPacket *pkt, AVFrame *frame, int *got_packet)