30#include "config_components.h"
59 for (
int f_code = 1; f_code <=
MAX_FCODE; f_code++) {
67 bit_size = f_code - 1;
86#if CONFIG_H263_ENCODER
91static uint8_t umv_fcode_tab[
MAX_MV*2+1];
95static uint8_t uni_h263_intra_aic_rl_len [64*64*2*2];
96static uint8_t uni_h263_inter_rl_len [64*64*2*2];
99#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
101static av_cold void init_uni_h263_rl_tab(
const RLTable *rl, uint8_t *len_tab)
103 const uint16_t (*table_vlc)[2] = rl->
table_vlc;
104 const uint8_t *table_run = rl->
table_run;
113 sizeof(uni_h263_intra_aic_rl_len));
117 int run = table_run[
i];
118 int level = table_level[
i];
120 int len = table_vlc[
i][1];
134#if CONFIG_H263_ENCODER
142 memset(umv_fcode_tab, 1,
sizeof(umv_fcode_tab));
164 int bit_size = f_code - 1;
165 int range = 1 << bit_size;
183#if CONFIG_H263_ENCODER
184static const uint8_t wrong_run[102] = {
185 1, 2, 3, 5, 4, 10, 9, 8,
18611, 15, 17, 16, 23, 22, 21, 20,
18719, 18, 25, 24, 27, 26, 11, 7,
188 6, 1, 2, 13, 2, 2, 2, 2,
189 6, 12, 3, 9, 1, 3, 4, 3,
190 7, 4, 1, 1, 5, 5, 14, 6,
191 1, 7, 1, 8, 1, 1, 1, 1,
19210, 1, 1, 5, 9, 17, 25, 24,
19329, 33, 32, 41, 2, 23, 28, 31,
194 3, 22, 30, 4, 27, 40, 8, 26,
195 6, 39, 7, 38, 16, 37, 15, 10,
19611, 12, 13, 14, 1, 21, 20, 18,
222 int format, coded_frame_rate, coded_frame_rate_base,
i, temp_ref;
223 int best_clock_code=1;
225 int best_error= INT_MAX;
233 div= (
s->c.avctx->time_base.num*1800000LL + 500LL*
s->c.avctx->time_base.den) / ((1000LL+
i)*
s->c.avctx->time_base.den);
235 error=
FFABS(
s->c.avctx->time_base.num*1800000LL - (1000LL+
i)*
s->c.avctx->time_base.den*div);
236 if(
error < best_error){
243 custom_pcf = best_clock_code != 1 || best_divisor != 60;
244 coded_frame_rate= 1800000;
245 coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
248 temp_ref =
s->picture_number * (
int64_t)coded_frame_rate *
s->c.avctx->time_base.num /
249 (coded_frame_rate_base * (
int64_t)
s->c.avctx->time_base.den);
318 put_bits(&
s->pb, 8,
s->c.avctx->sample_aspect_ratio.num);
319 put_bits(&
s->pb, 8,
s->c.avctx->sample_aspect_ratio.den);
335 if (
s->h263_slice_structured)
343 if (
s->h263_slice_structured) {
357 int16_t *dc =
s->c.dc_val;
360 const int l_xy =
s->c.block_index[2];
361 for (
int i = l_xy - 2 *
s->c.b8_stride - 1;
i < l_xy;
i += 2)
364 const int u_xy =
s->c.block_index[4];
365 const int v_xy =
s->c.block_index[5];
366 int16_t *dc2 = dc + v_xy - u_xy;
367 for (
int i = u_xy -
s->c.mb_stride - 1;
i < u_xy; ++
i)
368 dc[
i] = dc2[
i] = 1024;
378 if (
s->h263_slice_structured) {
383 if(
s->c.mb_num > 1583)
389 int gob_number = mb_line /
s->gob_index;
402 int8_t *
const qscale_table =
s->c.cur_pic.qscale_table;
404 for (
int i = 1;
i <
s->c.mb_num;
i++) {
405 if (qscale_table[
s->c.mb_index2xy[
i] ] - qscale_table[
s->c.mb_index2xy[
i-1] ] > 2)
406 qscale_table[
s->c.mb_index2xy[
i] ] = qscale_table[
s->c.mb_index2xy[
i-1] ] + 2;
408 for(
int i =
s->c.mb_num - 2;
i >= 0;
i--) {
409 if (qscale_table[
s->c.mb_index2xy[
i] ] - qscale_table[
s->c.mb_index2xy[
i+1] ] > 2)
410 qscale_table[
s->c.mb_index2xy[
i] ] = qscale_table[
s->c.mb_index2xy[
i+1] ] + 2;
414 for (
int i = 1;
i <
s->c.mb_num;
i++) {
415 int mb_xy =
s->c.mb_index2xy[
i];
417 if (qscale_table[mb_xy] != qscale_table[
s->c.mb_index2xy[
i - 1]] &&
433 bits = 1 + 1 + 6 + 7;
434 code = (0 << (1 + 6 + 7)) |
440 bits = 1 + 1 + 6 + 11;
441 code = (1 << (1 + 6 + 11)) |
456 int level,
run, last,
i, j, last_index, last_non_zero, sign, slevel,
code;
460 if (
s->c.mb_intra && !
s->c.h263_aic) {
469 else if (
level < 1) {
480 if (
s->c.h263_aic &&
s->c.mb_intra)
483 if (
s->alt_inter_vlc && !
s->c.mb_intra) {
485 int inter_vlc_bits=0;
489 last_index =
s->c.block_last_index[n];
490 last_non_zero =
i - 1;
491 for (;
i <= last_index;
i++) {
492 j =
s->c.intra_scantable.permutated[
i];
495 run =
i - last_non_zero - 1;
496 last = (
i == last_index);
506 inter_vlc_bits += 1+6+8-1;
509 aic_vlc_bits += 1+6+8-1;
510 wrong_pos +=
run + 1;
512 wrong_pos += wrong_run[aic_code];
517 if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
523 last_index =
s->c.block_last_index[n];
524 last_non_zero =
i - 1;
525 for (;
i <= last_index;
i++) {
526 j =
s->c.intra_scantable.permutated[
i];
529 run =
i - last_non_zero - 1;
530 last = (
i == last_index);
554 flv2_encode_ac_esc(&
s->pb, slevel,
level,
run, last);
570 unsigned code = (
val < 0) << 1;
575 unsigned tmp = (aval & 1) << 1 | 1;
584static int h263_pred_dc(
MPVEncContext *
const s,
int n, int16_t **dc_val_ptr)
586 const int wrap =
s->c.block_wrap[n];
587 const int xy =
s->c.block_index[n];
588 int16_t *
const dc_val =
s->c.dc_val + xy;
596 int c = dc_val[-
wrap];
599 if (
a != 1024 &&
c != 1024)
607 *dc_val_ptr = dc_val;
613 int motion_x,
int motion_y)
615 int cbpc, cbpy,
i, cbp, pred_x, pred_y;
617 int16_t rec_intradc[6];
620 if (!
s->c.mb_intra) {
624 if ((cbp | motion_x | motion_y |
s->dquant | (
s->c.mv_type -
MV_TYPE_16X16)) == 0) {
627 if(interleaved_stats){
638 if (!
s->alt_inter_vlc || cbpc!=3)
640 if(
s->dquant) cbpc+= 8;
650 if(interleaved_stats){
659 motion_y - pred_y, 1);
662 h263p_encode_umotion(&
s->pb, motion_x - pred_x);
663 h263p_encode_umotion(&
s->pb, motion_y - pred_y);
664 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
676 if(interleaved_stats){
684 motion_x =
s->c.cur_pic.motion_val[0][
s->c.block_index[
i]][0];
685 motion_y =
s->c.cur_pic.motion_val[0][
s->c.block_index[
i]][1];
688 motion_y - pred_y, 1);
691 h263p_encode_umotion(&
s->pb, motion_x - pred_x);
692 h263p_encode_umotion(&
s->pb, motion_y - pred_y);
693 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
700 if(interleaved_stats){
712 int scale =
i < 4 ?
s->c.y_dc_scale :
s->c.c_dc_scale;
722 if (!
s->modified_quant) {
725 else if (
level > 127)
737 if (rec_intradc[
i] < 0)
739 else if (rec_intradc[
i] > 2047)
740 rec_intradc[
i] = 2047;
743 *dc_ptr = rec_intradc[
i];
745 if (
s->c.block_last_index[
i] > 0 ||
746 (
s->c.block_last_index[
i] == 0 &&
level !=0))
752 if (
s->c.block_last_index[
i] >= 1)
759 if(
s->dquant) cbpc+=4;
764 if(
s->dquant) cbpc+=8;
779 if(interleaved_stats){
789 if (
s->c.h263_aic &&
s->c.mb_intra)
793 if(interleaved_stats){
794 if (!
s->c.mb_intra) {
805 const int mb_xy =
s->c.mb_y *
s->c.mb_stride +
s->c.mb_x;
807 if (
s->c.cur_pic.mbskip_table)
808 s->c.cur_pic.mbskip_table[mb_xy] =
s->c.mb_skipped;
812 else if(
s->c.mb_intra)
829 s->c.idsp.idct_permutation);
831 s->c.idsp.idct_permutation);
836 s->intra_ac_vlc_length =
s->inter_ac_vlc_length = uni_h263_inter_rl_len;
837 s->intra_ac_vlc_last_length=
s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64;
839 s->intra_ac_vlc_length = uni_h263_intra_aic_rl_len;
840 s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64;
842 s->c.y_dc_scale_table =
845 s->ac_esc_length= 7+1+6+8;
847 if (
s->modified_quant)
854 switch(
s->c.codec_id){
858 if (
s->modified_quant) {
859 s->min_qcoeff= -2047;
867#if CONFIG_FLV_ENCODER
871 s->min_qcoeff = -1023;
872 s->max_qcoeff = 1023;
883 s->encode_mb = h263_encode_mb;
893 mb_pos=
s->c.mb_x +
s->c.mb_width*
s->c.mb_y;
897#define OFFSET(x) offsetof(MpegEncContext, x)
898#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
899static const AVOption h263_options[] = {
901 {
"mb_info",
"emit macroblock info for RFC 2190 packetization, the parameter value is the maximum payload size",
FF_MPV_OFFSET(
mb_info),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
VE },
907static const AVClass h263_class = {
908 .class_name =
"H.263 encoder",
910 .option = h263_options,
921 .p.priv_class = &h263_class,
930static const AVOption h263p_options[] = {
934 {
"structured_slices",
"Write slice start position at every GOB header instead of just GOB number.",
FF_MPV_OFFSET(h263_slice_structured),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VE},
939static const AVClass h263p_class = {
940 .class_name =
"H.263p encoder",
942 .option = h263p_options,
953 .p.priv_class = &h263p_class,
static double val(void *priv, double ch)
static const char *const format[]
const FFCodec ff_h263p_encoder
const FFCodec ff_h263_encoder
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
#define i(width, name, range_min, range_max)
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
int(* init)(AVBSFContext *ctx)
static const uint8_t bits[8]
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
const char * av_default_item_name(void *ptr)
Return the context name.
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
@ AV_PICTURE_TYPE_I
Intra.
@ AV_PICTURE_TYPE_P
Predicted.
#define LIBAVUTIL_VERSION_INT
static uint8_t mv_penalty[MAX_FCODE+1][MAX_DMV *2+1]
void ff_h263_update_motion_val(MpegEncContext *s)
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
#define FF_ASPECT_EXTENDED
void ff_h263_init_rl_inter(void)
av_const int ff_h263_aspect_to_info(AVRational aspect)
#define H263_GOB_HEIGHT(h)
const uint8_t ff_mba_length[7]
const uint8_t ff_aic_dc_scale_table[32]
const AVRational ff_h263_pixel_aspect[16]
const uint8_t ff_h263_intra_MCBPC_bits[9]
const uint8_t ff_h263_chroma_qscale_table[32]
const uint8_t ff_h263_inter_MCBPC_code[28]
const uint8_t ff_mvtab[33][2]
const uint8_t ff_h263_inter_MCBPC_bits[28]
const uint16_t ff_mba_max[6]
const uint8_t ff_h263_intra_MCBPC_code[9]
const uint8_t ff_h263_cbpy_tab[16][2]
const uint16_t ff_h263_format[8][2]
#define H263_ESCAPE_CODE_LENGTH
#define H263_RL_NON_LAST_CODES
void ff_h263_encode_mba(MPVEncContext *s)
void ff_h263_mpeg4_reset_dc(MPVEncContext *s)
static void ff_h263_encode_motion_vector(MPVEncContext *s, int x, int y, int f_code)
static int get_p_cbp(MPVEncContext *const s, int16_t block[6][64], int motion_x, int motion_y)
void ff_h263_encode_gob_header(MPVEncContext *s, int mb_line)
void ff_clean_h263_qscales(MPVEncContext *s)
void ff_h263_encode_init(MPVMainEncContext *m)
void ff_h263_update_mb(MPVEncContext *s)
static void scale(int *out, const int *in, const int w, const int h, const int shift)
void ff_h263_encode_motion(PutBitContext *pb, int val, int f_code)
static av_cold void init_mv_penalty(void)
av_cold const uint8_t(* ff_h263_get_mv_penalty(void))[MAX_DMV *2+1]
static av_cold void h263_encode_init_static(void)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
static const int8_t mv[256][2]
int ff_flv_encode_picture_header(MPVMainEncContext *const m)
av_cold void ff_h263dsp_init(H263DSPContext *ctx)
av_cold void ff_permute_scantable(uint8_t dst[64], const uint8_t src[64], const uint8_t permutation[64])
common internal api header.
int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
Return the index into tab at which {a,b} match elements {[0],[1]} of tab.
Macro definitions for various function/variable attributes.
static int ff_thread_once(char *control, void(*routine)(void))
static av_const int sign_extend(int val, unsigned bits)
#define UNI_MPEG4_ENC_INDEX(last, run, level)
static const int dquant_code[5]
#define MB_TYPE_FORWARD_MV
#define MV_TYPE_8X8
4 vectors (H.263, MPEG-4 4MV)
#define MV_TYPE_16X16
1 vector for the whole mb
av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pic_arg, int *got_packet)
av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
const uint8_t ff_alternate_horizontal_scan[64]
const uint8_t ff_alternate_vertical_scan[64]
static int get_bits_diff(MPVEncContext *s)
#define CANDIDATE_MB_TYPE_INTER
#define CANDIDATE_MB_TYPE_INTER4V
#define FF_MPV_COMMON_OPTS
#define FF_MPV_COMMON_MOTION_EST_OPTS
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
static void FUNC pred_dc(uint8_t *_src, const uint8_t *_top, const uint8_t *_left, ptrdiff_t stride, int log2_size, int c_idx)
static void put_sbits(PutBitContext *pb, int n, int32_t value)
static void put_bits_assume_flushed(const PutBitContext *s)
Inform the compiler that a PutBitContext is flushed (i.e.
av_cold void ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Initialize index_run, max_level and max_run from n, last, table_vlc, table_run and table_level.
static int get_rl_index(const RLTable *rl, int last, int run, int level)
#define FF_ARRAY_ELEMS(a)
Describe the class of an AVClass context structure.
Rational number (pair of numerator and denominator).
MPVEncContext s
The main slicecontext.
const uint8_t * fcode_tab
smallest fcode needed for each MV
int(* encode_picture_header)(struct MPVMainEncContext *m)
int n
number of entries of table_vlc minus 1
const uint16_t(* table_vlc)[2]
const int8_t * table_level
static void error(const char *err)