FFmpeg
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 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/attributes.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/thread.h"
31 #include "avcodec.h"
32 #include "codec_internal.h"
33 #include "mpegutils.h"
34 #include "mpegvideo.h"
35 #include "h261.h"
36 #include "h261enc.h"
37 #include "mpegvideoenc.h"
38 
39 #define H261_MAX_RUN 26
40 #define H261_MAX_LEVEL 15
41 #define H261_ESC_LEN (6 + 6 + 8)
42 
43 static struct VLCLUT {
44  uint8_t len;
45  uint16_t code;
46 } vlc_lut[H261_MAX_RUN + 1][32 /* 0..2 * H261_MAX_LEN are used */];
47 
48 static uint8_t uni_h261_rl_len [64 * 128];
49 static uint8_t uni_h261_rl_len_last[64 * 128];
50 
51 typedef struct H261EncContext {
53 
55 
57  enum {
58  H261_QCIF = 0,
59  H261_CIF = 1,
60  } format;
62 
64 {
65  H261EncContext *const h = (H261EncContext *)s;
66  int temp_ref;
67 
68  align_put_bits(&s->pb);
69 
70  /* Update the pointer to last GOB */
71  s->ptr_lastgob = put_bits_ptr(&s->pb);
72 
73  put_bits(&s->pb, 20, 0x10); /* PSC */
74 
75  temp_ref = s->picture_number * 30000LL * s->avctx->time_base.num /
76  (1001LL * s->avctx->time_base.den); // FIXME maybe this should use a timestamp
77  put_sbits(&s->pb, 5, temp_ref); /* TemporalReference */
78 
79  put_bits(&s->pb, 1, 0); /* split screen off */
80  put_bits(&s->pb, 1, 0); /* camera off */
81  put_bits(&s->pb, 1, s->pict_type == AV_PICTURE_TYPE_I); /* freeze picture release on/off */
82 
83  put_bits(&s->pb, 1, h->format); /* 0 == QCIF, 1 == CIF */
84 
85  put_bits(&s->pb, 1, 1); /* still image mode */
86  put_bits(&s->pb, 1, 1); /* reserved */
87 
88  put_bits(&s->pb, 1, 0); /* no PEI */
89  h->gob_number = h->format - 1;
90  s->mb_skip_run = 0;
91 }
92 
93 /**
94  * Encode a group of blocks header.
95  */
96 static void h261_encode_gob_header(MpegEncContext *s, int mb_line)
97 {
98  H261EncContext *const h = (H261EncContext *)s;
99  if (h->format == H261_QCIF) {
100  h->gob_number += 2; // QCIF
101  } else {
102  h->gob_number++; // CIF
103  }
104  put_bits(&s->pb, 16, 1); /* GBSC */
105  put_bits(&s->pb, 4, h->gob_number); /* GN */
106  put_bits(&s->pb, 5, s->qscale); /* GQUANT */
107  put_bits(&s->pb, 1, 0); /* no GEI */
108  s->mb_skip_run = 0;
109  s->last_mv[0][0][0] = 0;
110  s->last_mv[0][0][1] = 0;
111 }
112 
114 {
115  const H261EncContext *const h = (H261EncContext*)s;
116  int index = s->mb_x + s->mb_y * s->mb_width;
117 
118  if (index % 11 == 0) {
119  if (index % 33 == 0)
121  s->last_mv[0][0][0] = 0;
122  s->last_mv[0][0][1] = 0;
123  }
124 
125  /* for CIF the GOB's are fragmented in the middle of a scanline
126  * that's why we need to adjust the x and y index of the macroblocks */
127  if (h->format == H261_CIF) {
128  s->mb_x = index % 11;
129  index /= 11;
130  s->mb_y = index % 3;
131  index /= 3;
132  s->mb_x += 11 * (index % 2);
133  index /= 2;
134  s->mb_y += 3 * index;
135 
137  ff_update_block_index(s, 8, 0, 1);
138  }
139 }
140 
141 static void h261_encode_motion(PutBitContext *pb, int val)
142 {
143  int sign, code;
144  if (val == 0) {
145  // Corresponds to ff_h261_mv_tab[0]
146  put_bits(pb, 1, 1);
147  } else {
148  if (val > 15)
149  val -= 32;
150  if (val < -16)
151  val += 32;
152  sign = val < 0;
153  code = sign ? -val : val;
155  put_bits(pb, 1, sign);
156  }
157 }
158 
159 static inline int get_cbp(MpegEncContext *s, int16_t block[6][64])
160 {
161  int i, cbp;
162  cbp = 0;
163  for (i = 0; i < 6; i++)
164  if (s->block_last_index[i] >= 0)
165  cbp |= 1 << (5 - i);
166  return cbp;
167 }
168 
169 /**
170  * Encode an 8x8 block.
171  * @param block the 8x8 block
172  * @param n block index (0-3 are luma, 4-5 are chroma)
173  */
174 static void h261_encode_block(H261EncContext *h, int16_t *block, int n)
175 {
176  MpegEncContext *const s = &h->s;
177  int level, run, i, j, last_index, last_non_zero;
178 
179  if (s->mb_intra) {
180  /* DC coef */
181  level = block[0];
182  /* 255 cannot be represented, so we clamp */
183  if (level > 254) {
184  level = 254;
185  block[0] = 254;
186  }
187  /* 0 cannot be represented also */
188  else if (level < 1) {
189  level = 1;
190  block[0] = 1;
191  }
192  if (level == 128)
193  put_bits(&s->pb, 8, 0xff);
194  else
195  put_bits(&s->pb, 8, level);
196  i = 1;
197  } else if ((block[0] == 1 || block[0] == -1) &&
198  (s->block_last_index[n] > -1)) {
199  // special case
200  put_bits(&s->pb, 2, block[0] > 0 ? 2 : 3);
201  i = 1;
202  } else {
203  i = 0;
204  }
205 
206  /* AC coefs */
207  last_index = s->block_last_index[n];
208  last_non_zero = i - 1;
209  for (; i <= last_index; i++) {
210  j = s->intra_scantable.permutated[i];
211  level = block[j];
212  if (level) {
213  run = i - last_non_zero - 1;
214 
215  if (run <= H261_MAX_RUN &&
216  (unsigned)(level + H261_MAX_LEVEL) <= 2 * H261_MAX_LEVEL &&
220  } else {
221  /* Escape */
222  put_bits(&s->pb, 6 + 6, (1 << 6) | run);
223  av_assert1(level != 0);
224  av_assert1(FFABS(level) <= 127);
225  put_sbits(&s->pb, 8, level);
226  }
227  last_non_zero = i;
228  }
229  }
230  if (last_index > -1)
231  put_bits(&s->pb, 2, 0x2); // EOB
232 }
233 
234 void ff_h261_encode_mb(MpegEncContext *s, int16_t block[6][64],
235  int motion_x, int motion_y)
236 {
237  /* The following is only allowed because this encoder
238  * does not use slice threading. */
239  H261EncContext *const h = (H261EncContext *)s;
240  H261Context *const com = &h->common;
241  int mvd, mv_diff_x, mv_diff_y, i, cbp;
242  cbp = 63; // avoid warning
243  mvd = 0;
244 
245  com->mtype = 0;
246 
247  if (!s->mb_intra) {
248  /* compute cbp */
249  cbp = get_cbp(s, block);
250 
251  /* mvd indicates if this block is motion compensated */
252  mvd = motion_x | motion_y;
253 
254  if ((cbp | mvd) == 0) {
255  /* skip macroblock */
256  s->mb_skip_run++;
257  s->last_mv[0][0][0] = 0;
258  s->last_mv[0][0][1] = 0;
259  s->qscale -= s->dquant;
260  return;
261  }
262  }
263 
264  /* MB is not skipped, encode MBA */
265  put_bits(&s->pb,
266  ff_h261_mba_bits[s->mb_skip_run],
267  ff_h261_mba_code[s->mb_skip_run]);
268  s->mb_skip_run = 0;
269 
270  /* calculate MTYPE */
271  if (!s->mb_intra) {
272  com->mtype++;
273 
274  if (mvd || s->loop_filter)
275  com->mtype += 3;
276  if (s->loop_filter)
277  com->mtype += 3;
278  if (cbp)
279  com->mtype++;
280  av_assert1(com->mtype > 1);
281  }
282 
283  if (s->dquant && cbp) {
284  com->mtype++;
285  } else
286  s->qscale -= s->dquant;
287 
288  put_bits(&s->pb,
290  ff_h261_mtype_code[com->mtype]);
291 
292  com->mtype = ff_h261_mtype_map[com->mtype];
293 
294  if (IS_QUANT(com->mtype)) {
295  ff_set_qscale(s, s->qscale + s->dquant);
296  put_bits(&s->pb, 5, s->qscale);
297  }
298 
299  if (IS_16X16(com->mtype)) {
300  mv_diff_x = (motion_x >> 1) - s->last_mv[0][0][0];
301  mv_diff_y = (motion_y >> 1) - s->last_mv[0][0][1];
302  s->last_mv[0][0][0] = (motion_x >> 1);
303  s->last_mv[0][0][1] = (motion_y >> 1);
304  h261_encode_motion(&s->pb, mv_diff_x);
305  h261_encode_motion(&s->pb, mv_diff_y);
306  }
307 
308  if (HAS_CBP(com->mtype)) {
309  av_assert1(cbp > 0);
310  put_bits(&s->pb,
311  ff_h261_cbp_tab[cbp - 1][1],
312  ff_h261_cbp_tab[cbp - 1][0]);
313  }
314  for (i = 0; i < 6; i++)
315  /* encode each block */
317 
318  if (!IS_16X16(com->mtype)) {
319  s->last_mv[0][0][0] = 0;
320  s->last_mv[0][0][1] = 0;
321  }
322 }
323 
325 {
326  memset(uni_h261_rl_len, H261_ESC_LEN, sizeof(uni_h261_rl_len));
327  memset(uni_h261_rl_len_last, H261_ESC_LEN + 2 /* EOB */, sizeof(uni_h261_rl_len_last));
328 
329  // The following loop is over the ordinary elements, not EOB or escape.
330  for (size_t i = 1; i < FF_ARRAY_ELEMS(ff_h261_tcoeff_vlc) - 1; i++) {
331  unsigned run = ff_h261_tcoeff_run[i];
332  unsigned level = ff_h261_tcoeff_level[i];
333  unsigned len = ff_h261_tcoeff_vlc[i][1] + 1 /* sign */;
334  unsigned code = ff_h261_tcoeff_vlc[i][0];
335 
336  vlc_lut[run][H261_MAX_LEVEL + level] = (struct VLCLUT){ len, code << 1 };
337  vlc_lut[run][H261_MAX_LEVEL - level] = (struct VLCLUT){ len, (code << 1) | 1 };
338 
343  }
344 }
345 
347 {
348  H261EncContext *const h = (H261EncContext*)s;
349  static AVOnce init_static_once = AV_ONCE_INIT;
350 
351  if (s->width == 176 && s->height == 144) {
352  h->format = H261_QCIF;
353  } else if (s->width == 352 && s->height == 288) {
354  h->format = H261_CIF;
355  } else {
356  av_log(s->avctx, AV_LOG_ERROR,
357  "The specified picture size of %dx%d is not valid for the "
358  "H.261 codec.\nValid sizes are 176x144, 352x288\n",
359  s->width, s->height);
360  return AVERROR(EINVAL);
361  }
362  s->private_ctx = &h->common;
363 
364  s->min_qcoeff = -127;
365  s->max_qcoeff = 127;
366  s->ac_esc_length = H261_ESC_LEN;
367 
368  s->intra_ac_vlc_length = s->inter_ac_vlc_length = uni_h261_rl_len;
369  s->intra_ac_vlc_last_length = s->inter_ac_vlc_last_length = uni_h261_rl_len_last;
370  ff_thread_once(&init_static_once, h261_encode_init_static);
371 
372  return 0;
373 }
374 
376  .p.name = "h261",
377  CODEC_LONG_NAME("H.261"),
378  .p.type = AVMEDIA_TYPE_VIDEO,
379  .p.id = AV_CODEC_ID_H261,
380  .p.priv_class = &ff_mpv_enc_class,
381  .priv_data_size = sizeof(H261EncContext),
384  .close = ff_mpv_encode_end,
385  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
386  .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
387  AV_PIX_FMT_NONE },
388  .color_ranges = AVCOL_RANGE_MPEG,
389  .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
390 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
ff_mpv_enc_class
const AVClass ff_mpv_enc_class
Definition: mpegvideo_enc.c:105
level
uint8_t level
Definition: svq3.c:205
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:43
H261EncContext
Definition: h261enc.c:51
ff_h261_encode_init
av_cold int ff_h261_encode_init(MpegEncContext *s)
Definition: h261enc.c:346
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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 they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
H261Context::mtype
int mtype
Definition: h261.h:39
H261EncContext::H261_QCIF
@ H261_QCIF
Definition: h261enc.c:58
align_put_bits
static void align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
Definition: put_bits.h:420
thread.h
ff_h261_encode_picture_header
void ff_h261_encode_picture_header(MpegEncContext *s)
Definition: h261enc.c:63
H261EncContext::s
MpegEncContext s
Definition: h261enc.c:52
ff_h261_tcoeff_run
const int8_t ff_h261_tcoeff_run[64]
Definition: h261data.c:138
mpegvideoenc.h
put_sbits
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:281
H261EncContext::common
H261Context common
Definition: h261enc.c:54
VLCLUT::code
uint16_t code
Definition: h261enc.c:45
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:223
ff_h261_encode_mb
void ff_h261_encode_mb(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
Definition: h261enc.c:234
FFCodec
Definition: codec_internal.h:127
ff_init_block_index
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:820
mpegvideo.h
mpegutils.h
AV_CODEC_ID_H261
@ AV_CODEC_ID_H261
Definition: codec_id.h:55
ff_h261_encoder
const FFCodec ff_h261_encoder
Definition: h261enc.c:375
ff_mpv_encode_picture
int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pic_arg, int *got_packet)
Definition: mpegvideo_enc.c:1793
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
H261_MAX_LEVEL
#define H261_MAX_LEVEL
Definition: h261enc.c:40
h261.h
val
static double val(void *priv, double ch)
Definition: aeval.c:77
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:320
HAS_CBP
#define HAS_CBP(a)
Definition: mpegutils.h:88
avassert.h
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:198
VLCLUT
Definition: h261enc.c:43
h261_encode_gob_header
static void h261_encode_gob_header(MpegEncContext *s, int mb_line)
Encode a group of blocks header.
Definition: h261enc.c:96
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:159
ff_h261_mba_code
const uint8_t ff_h261_mba_code[35]
Definition: h261data.c:34
vlc_lut
static struct VLCLUT vlc_lut[H261_MAX_RUN+1][32/*0..2 *H261_MAX_LEN are used */]
ff_h261_mv_tab
const uint8_t ff_h261_mv_tab[17][2]
Definition: h261data.c:89
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
PutBitContext
Definition: put_bits.h:50
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:74
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
h261_encode_block
static void h261_encode_block(H261EncContext *h, int16_t *block, int n)
Encode an 8x8 block.
Definition: h261enc.c:174
run
uint8_t run
Definition: svq3.c:204
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
ff_set_qscale
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
Definition: mpegvideo.c:853
ff_mpv_encode_end
av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
Definition: mpegvideo_enc.c:1028
ff_h261_tcoeff_level
const int8_t ff_h261_tcoeff_level[64]
Definition: h261data.c:127
AVOnce
#define AVOnce
Definition: thread.h:202
H261_MAX_RUN
#define H261_MAX_RUN
Definition: h261enc.c:39
index
int index
Definition: gxfenc.c:90
uni_h261_rl_len_last
static uint8_t uni_h261_rl_len_last[64 *128]
Definition: h261enc.c:49
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
codec_internal.h
ff_update_block_index
static void ff_update_block_index(MpegEncContext *s, int bits_per_raw_sample, int lowres, int chroma_x_shift)
Definition: mpegvideo.h:609
H261EncContext::format
enum H261EncContext::@119 format
attributes.h
H261EncContext::gob_number
int gob_number
Definition: h261enc.c:56
ff_h261_tcoeff_vlc
const uint16_t ff_h261_tcoeff_vlc[65][2]
Definition: h261data.c:107
UNI_AC_ENC_INDEX
#define UNI_AC_ENC_INDEX(run, level)
Definition: mpegvideoenc.h:36
IS_16X16
#define IS_16X16(a)
Definition: mpegutils.h:81
H261Context
H261Context.
Definition: h261.h:38
h261_encode_init_static
static av_cold void h261_encode_init_static(void)
Definition: h261enc.c:324
ff_h261_cbp_tab
const uint8_t ff_h261_cbp_tab[63][2]
Definition: h261data.c:95
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
IS_QUANT
#define IS_QUANT(a)
Definition: mpegutils.h:86
ff_h261_reorder_mb_index
void ff_h261_reorder_mb_index(MpegEncContext *s)
Definition: h261enc.c:113
ff_h261_mtype_code
const uint8_t ff_h261_mtype_code[10]
Definition: h261data.c:63
uni_h261_rl_len
static uint8_t uni_h261_rl_len[64 *128]
Definition: h261enc.c:48
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
len
int len
Definition: vorbis_enc_data.h:426
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:700
get_cbp
static int get_cbp(MpegEncContext *s, int16_t block[6][64])
Definition: h261enc.c:159
avcodec.h
h261enc.h
put_bits_ptr
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:377
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
ff_h261_mtype_map
const uint16_t ff_h261_mtype_map[10]
Definition: h261data.c:75
ff_h261_mtype_bits
const uint8_t ff_h261_mtype_bits[10]
Definition: h261data.c:69
VLCLUT::len
uint8_t len
Definition: h261enc.c:44
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ff_mpv_encode_init
av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
Definition: mpegvideo_enc.c:351
h261_encode_motion
static void h261_encode_motion(PutBitContext *pb, int val)
Definition: h261enc.c:141
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
h
h
Definition: vp9dsp_template.c:2070
H261_ESC_LEN
#define H261_ESC_LEN
Definition: h261enc.c:41
ff_h261_mba_bits
const uint8_t ff_h261_mba_bits[35]
Definition: h261data.c:48
H261EncContext::H261_CIF
@ H261_CIF
Definition: h261enc.c:59
MpegEncContext
MpegEncContext.
Definition: mpegvideo.h:73