FFmpeg
Macros | Functions | Variables
mpegvideo_enc.c File Reference
#include "config_components.h"
#include <stdint.h>
#include "libavutil/emms.h"
#include "libavutil/internal.h"
#include "libavutil/intmath.h"
#include "libavutil/mathematics.h"
#include "libavutil/mem.h"
#include "libavutil/mem_internal.h"
#include "libavutil/opt.h"
#include "libavutil/thread.h"
#include "avcodec.h"
#include "encode.h"
#include "idctdsp.h"
#include "mpeg12codecs.h"
#include "mpeg12data.h"
#include "mpeg12enc.h"
#include "mpegvideo.h"
#include "mpegvideodata.h"
#include "mpegvideoenc.h"
#include "h261enc.h"
#include "h263.h"
#include "h263data.h"
#include "h263enc.h"
#include "mjpegenc_common.h"
#include "mathops.h"
#include "mpegutils.h"
#include "mjpegenc.h"
#include "speedhqenc.h"
#include "msmpeg4enc.h"
#include "pixblockdsp.h"
#include "qpeldsp.h"
#include "faandct.h"
#include "aandcttab.h"
#include "flvenc.h"
#include "mpeg4video.h"
#include "mpeg4videodata.h"
#include "mpeg4videoenc.h"
#include "internal.h"
#include "bytestream.h"
#include "wmv2enc.h"
#include "rv10enc.h"
#include "packet_internal.h"
#include "libavutil/refstruct.h"
#include <limits.h>
#include "sp5x.h"
#include "mpv_reconstruct_mb_template.c"

Go to the source code of this file.

Macros

#define QUANT_BIAS_SHIFT   8
 
#define QMAT_SHIFT_MMX   16
 
#define QMAT_SHIFT   21
 
#define COPY(a)   dst->a= src->a
 
#define ALLOCZ_ARRAYS(p, mult, numb)   ((p) = av_calloc(numb, mult * sizeof(*(p))))
 
#define IS_ENCODER   1
 
#define INTERLACED_DCT(s)
 
#define MERGE(field)   dst->field += src->field; src->field=0
 

Functions

static int encode_picture (MpegEncContext *s, const AVPacket *pkt)
 
static int dct_quantize_refine (MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale)
 
static int sse_mb (MpegEncContext *s)
 
static void denoise_dct_c (MpegEncContext *s, int16_t *block)
 
static int dct_quantize_c (MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow)
 
static int dct_quantize_trellis_c (MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow)
 
void ff_convert_matrix (MpegEncContext *s, int(*qmat)[64], uint16_t(*qmat16)[2][64], const uint16_t *quant_matrix, int bias, int qmin, int qmax, int intra)
 
static void update_qscale (MpegEncContext *s)
 
void ff_write_quant_matrix (PutBitContext *pb, uint16_t *matrix)
 
static void init_qscale_tab (MpegEncContext *s)
 init s->cur_pic.qscale_table from s->lambda_table More...
 
static void update_duplicate_context_after_me (MpegEncContext *dst, const MpegEncContext *src)
 
static void mpv_encode_init_static (void)
 
static void mpv_encode_defaults (MpegEncContext *s)
 Set the given MpegEncContext to defaults for encoding. More...
 
av_cold void ff_dct_encode_init (MpegEncContext *s)
 
static av_cold int me_cmp_init (MpegEncContext *s, AVCodecContext *avctx)
 
av_cold int ff_mpv_encode_init (AVCodecContext *avctx)
 
av_cold int ff_mpv_encode_end (AVCodecContext *avctx)
 
static void mpv_reconstruct_mb (MpegEncContext *s, int16_t block[12][64])
 
static int get_sae (const uint8_t *src, int ref, int stride)
 
static int get_intra_count (MpegEncContext *s, const uint8_t *src, const uint8_t *ref, int stride)
 
static int prepare_picture (MpegEncContext *s, AVFrame *f, const AVFrame *props_frame)
 Allocates new buffers for an AVFrame and copies the properties from another AVFrame. More...
 
static int load_input_picture (MpegEncContext *s, const AVFrame *pic_arg)
 
static int skip_check (MpegEncContext *s, const MPVPicture *p, const MPVPicture *ref)
 
static int encode_frame (AVCodecContext *c, const AVFrame *frame, AVPacket *pkt)
 
static int estimate_best_b_count (MpegEncContext *s)
 
static int set_bframe_chain_length (MpegEncContext *s)
 Determines whether an input picture is discarded or not and if not determines the length of the next chain of B frames and moves these pictures (including the P frame) into reordered_input_picture. More...
 
static int select_input_picture (MpegEncContext *s)
 
static void frame_end (MpegEncContext *s)
 
static void update_noise_reduction (MpegEncContext *s)
 
static void frame_start (MpegEncContext *s)
 
int ff_mpv_encode_picture (AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pic_arg, int *got_packet)
 
static void dct_single_coeff_elimination (MpegEncContext *s, int n, int threshold)
 
static void clip_coeffs (MpegEncContext *s, int16_t *block, int last_index)
 
static void get_visual_weight (int16_t *weight, const uint8_t *ptr, int stride)
 
static av_always_inline void encode_mb_internal (MpegEncContext *s, int motion_x, int motion_y, int mb_block_height, int mb_block_width, int mb_block_count, int chroma_x_shift, int chroma_y_shift, int chroma_format)
 
static void encode_mb (MpegEncContext *s, int motion_x, int motion_y)
 
static void copy_context_before_encode (MpegEncContext *d, const MpegEncContext *s)
 
static void copy_context_after_encode (MpegEncContext *d, const MpegEncContext *s)
 
static void encode_mb_hq (MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2], int *dmin, int *next_block, int motion_x, int motion_y)
 
static int sse (MpegEncContext *s, const uint8_t *src1, const uint8_t *src2, int w, int h, int stride)
 
static int pre_estimate_motion_thread (AVCodecContext *c, void *arg)
 
static int estimate_motion_thread (AVCodecContext *c, void *arg)
 
static int mb_var_thread (AVCodecContext *c, void *arg)
 
static void write_slice_end (MpegEncContext *s)
 
static void write_mb_info (MpegEncContext *s)
 
static void update_mb_info (MpegEncContext *s, int startcode)
 
int ff_mpv_reallocate_putbitbuffer (MpegEncContext *s, size_t threshold, size_t size_increase)
 
static int encode_thread (AVCodecContext *c, void *arg)
 
static void merge_context_after_me (MpegEncContext *dst, MpegEncContext *src)
 
static void merge_context_after_encode (MpegEncContext *dst, MpegEncContext *src)
 
static int estimate_qp (MpegEncContext *s, int dry_run)
 
static void set_frame_distances (MpegEncContext *s)
 
static void build_basis (uint8_t *perm)
 
void ff_block_permute (int16_t *block, const uint8_t *permutation, const uint8_t *scantable, int last)
 Permute an 8x8 block according to permutation. More...
 

Variables

static uint8_t default_mv_penalty [MAX_FCODE+1][MAX_DMV *2+1]
 
static uint8_t default_fcode_tab [MAX_MV *2+1]
 
static const AVOption mpv_generic_options []
 
const AVClass ff_mpv_enc_class
 
static int16_t basis [64][64]
 

Detailed Description

The simplest mpeg encoder (well, it was the simplest!).

Definition in file mpegvideo_enc.c.

Macro Definition Documentation

◆ QUANT_BIAS_SHIFT

#define QUANT_BIAS_SHIFT   8

Definition at line 82 of file mpegvideo_enc.c.

◆ QMAT_SHIFT_MMX

#define QMAT_SHIFT_MMX   16

Definition at line 84 of file mpegvideo_enc.c.

◆ QMAT_SHIFT

#define QMAT_SHIFT   21

Definition at line 85 of file mpegvideo_enc.c.

◆ COPY

#define COPY (   a)    dst->a= src->a

◆ ALLOCZ_ARRAYS

#define ALLOCZ_ARRAYS (   p,
  mult,
  numb 
)    ((p) = av_calloc(numb, mult * sizeof(*(p))))

◆ IS_ENCODER

#define IS_ENCODER   1

Definition at line 1085 of file mpegvideo_enc.c.

◆ INTERLACED_DCT

#define INTERLACED_DCT (   s)
Value:
((chroma_format == CHROMA_420 || chroma_format == CHROMA_422) && \
(s)->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT)

◆ MERGE

#define MERGE (   field)    dst->field += src->field; src->field=0

Definition at line 3501 of file mpegvideo_enc.c.

Function Documentation

◆ encode_picture()

static int encode_picture ( MpegEncContext s,
const AVPacket pkt 
)
static

Definition at line 3585 of file mpegvideo_enc.c.

Referenced by ff_mpv_encode_picture().

◆ dct_quantize_refine()

static int dct_quantize_refine ( MpegEncContext s,
int16_t *  block,
int16_t *  weight,
int16_t *  orig,
int  n,
int  qscale 
)
static

Definition at line 4231 of file mpegvideo_enc.c.

Referenced by encode_mb_internal().

◆ sse_mb()

static int sse_mb ( MpegEncContext s)
static

Definition at line 2674 of file mpegvideo_enc.c.

Referenced by encode_mb_hq().

◆ denoise_dct_c()

static void denoise_dct_c ( MpegEncContext s,
int16_t *  block 
)
static

Definition at line 3874 of file mpegvideo_enc.c.

Referenced by ff_dct_encode_init().

◆ dct_quantize_c()

static int dct_quantize_c ( MpegEncContext s,
int16_t *  block,
int  n,
int  qscale,
int *  overflow 
)
static

Definition at line 4583 of file mpegvideo_enc.c.

Referenced by encode_mb_internal(), and ff_dct_encode_init().

◆ dct_quantize_trellis_c()

static int dct_quantize_trellis_c ( MpegEncContext s,
int16_t *  block,
int  n,
int  qscale,
int *  overflow 
)
static

Definition at line 3898 of file mpegvideo_enc.c.

Referenced by ff_dct_encode_init().

◆ ff_convert_matrix()

void ff_convert_matrix ( MpegEncContext s,
int(*)  qmat[64],
uint16_t(*)  qmat16[2][64],
const uint16_t *  quant_matrix,
int  bias,
int  qmin,
int  qmax,
int  intra 
)

Definition at line 112 of file mpegvideo_enc.c.

Referenced by dnxhd_init_qmat(), encode_picture(), and ff_mpv_encode_init().

◆ update_qscale()

static void update_qscale ( MpegEncContext s)
inlinestatic

Definition at line 196 of file mpegvideo_enc.c.

Referenced by encode_picture(), and estimate_qp().

◆ ff_write_quant_matrix()

void ff_write_quant_matrix ( PutBitContext pb,
uint16_t *  matrix 
)

Definition at line 224 of file mpegvideo_enc.c.

Referenced by mpeg4_encode_vol_header().

◆ init_qscale_tab()

static void init_qscale_tab ( MpegEncContext s)
static

init s->cur_pic.qscale_table from s->lambda_table

Definition at line 240 of file mpegvideo_enc.c.

Referenced by estimate_qp().

◆ update_duplicate_context_after_me()

static void update_duplicate_context_after_me ( MpegEncContext dst,
const MpegEncContext src 
)
static

Definition at line 253 of file mpegvideo_enc.c.

Referenced by encode_picture().

◆ mpv_encode_init_static()

static void mpv_encode_init_static ( void  )
static

Definition at line 269 of file mpegvideo_enc.c.

Referenced by mpv_encode_defaults().

◆ mpv_encode_defaults()

static void mpv_encode_defaults ( MpegEncContext s)
static

Set the given MpegEncContext to defaults for encoding.

the changed fields will not depend upon the prior state of the MpegEncContext.

Definition at line 279 of file mpegvideo_enc.c.

Referenced by ff_mpv_encode_init().

◆ ff_dct_encode_init()

av_cold void ff_dct_encode_init ( MpegEncContext s)

Definition at line 294 of file mpegvideo_enc.c.

Referenced by dnxhd_encode_init(), and ff_mpv_encode_init().

◆ me_cmp_init()

static av_cold int me_cmp_init ( MpegEncContext s,
AVCodecContext avctx 
)
static

Definition at line 309 of file mpegvideo_enc.c.

Referenced by ff_mpv_encode_init().

◆ ff_mpv_encode_init()

av_cold int ff_mpv_encode_init ( AVCodecContext avctx)

Definition at line 351 of file mpegvideo_enc.c.

Referenced by encode_init(), and wmv2_encode_init().

◆ ff_mpv_encode_end()

av_cold int ff_mpv_encode_end ( AVCodecContext avctx)

Definition at line 1028 of file mpegvideo_enc.c.

Referenced by mjpeg_encode_close().

◆ mpv_reconstruct_mb()

static void mpv_reconstruct_mb ( MpegEncContext s,
int16_t  block[12][64] 
)
static

Definition at line 1088 of file mpegvideo_enc.c.

Referenced by encode_mb_hq(), and encode_thread().

◆ get_sae()

static int get_sae ( const uint8_t *  src,
int  ref,
int  stride 
)
static

Definition at line 1105 of file mpegvideo_enc.c.

Referenced by get_intra_count().

◆ get_intra_count()

static int get_intra_count ( MpegEncContext s,
const uint8_t *  src,
const uint8_t *  ref,
int  stride 
)
static

Definition at line 1119 of file mpegvideo_enc.c.

Referenced by set_bframe_chain_length().

◆ prepare_picture()

static int prepare_picture ( MpegEncContext s,
AVFrame f,
const AVFrame props_frame 
)
static

Allocates new buffers for an AVFrame and copies the properties from another AVFrame.

Definition at line 1146 of file mpegvideo_enc.c.

Referenced by load_input_picture(), and select_input_picture().

◆ load_input_picture()

static int load_input_picture ( MpegEncContext s,
const AVFrame pic_arg 
)
static

Definition at line 1178 of file mpegvideo_enc.c.

Referenced by ff_mpv_encode_picture().

◆ skip_check()

static int skip_check ( MpegEncContext s,
const MPVPicture p,
const MPVPicture ref 
)
static

Definition at line 1318 of file mpegvideo_enc.c.

Referenced by set_bframe_chain_length().

◆ encode_frame()

static int encode_frame ( AVCodecContext c,
const AVFrame frame,
AVPacket pkt 
)
static

Definition at line 1359 of file mpegvideo_enc.c.

Referenced by estimate_best_b_count().

◆ estimate_best_b_count()

static int estimate_best_b_count ( MpegEncContext s)
static

Definition at line 1380 of file mpegvideo_enc.c.

Referenced by set_bframe_chain_length().

◆ set_bframe_chain_length()

static int set_bframe_chain_length ( MpegEncContext s)
static

Determines whether an input picture is discarded or not and if not determines the length of the next chain of B frames and moves these pictures (including the P frame) into reordered_input_picture.

input_picture[0] is always NULL when exiting this function, even on error; reordered_input_picture[0] is always NULL when exiting this function on error.

Definition at line 1532 of file mpegvideo_enc.c.

Referenced by select_input_picture().

◆ select_input_picture()

static int select_input_picture ( MpegEncContext s)
static

Definition at line 1662 of file mpegvideo_enc.c.

Referenced by ff_mpv_encode_picture().

◆ frame_end()

static void frame_end ( MpegEncContext s)
static

Definition at line 1721 of file mpegvideo_enc.c.

Referenced by ff_mpv_encode_picture().

◆ update_noise_reduction()

static void update_noise_reduction ( MpegEncContext s)
static

Definition at line 1757 of file mpegvideo_enc.c.

Referenced by frame_start().

◆ frame_start()

static void frame_start ( MpegEncContext s)
static

◆ ff_mpv_encode_picture()

int ff_mpv_encode_picture ( AVCodecContext avctx,
AVPacket pkt,
const AVFrame pic_arg,
int *  got_packet 
)

Definition at line 1793 of file mpegvideo_enc.c.

◆ dct_single_coeff_elimination()

static void dct_single_coeff_elimination ( MpegEncContext s,
int  n,
int  threshold 
)
inlinestatic

Definition at line 2022 of file mpegvideo_enc.c.

Referenced by encode_mb_internal().

◆ clip_coeffs()

static void clip_coeffs ( MpegEncContext s,
int16_t *  block,
int  last_index 
)
inlinestatic

Definition at line 2078 of file mpegvideo_enc.c.

Referenced by encode_mb_internal().

◆ get_visual_weight()

static void get_visual_weight ( int16_t *  weight,
const uint8_t *  ptr,
int  stride 
)
static

Definition at line 2112 of file mpegvideo_enc.c.

Referenced by encode_mb_internal().

◆ encode_mb_internal()

static av_always_inline void encode_mb_internal ( MpegEncContext s,
int  motion_x,
int  motion_y,
int  mb_block_height,
int  mb_block_width,
int  mb_block_count,
int  chroma_x_shift,
int  chroma_y_shift,
int  chroma_format 
)
static

Definition at line 2136 of file mpegvideo_enc.c.

Referenced by encode_mb().

◆ encode_mb()

static void encode_mb ( MpegEncContext s,
int  motion_x,
int  motion_y 
)
static

Definition at line 2527 of file mpegvideo_enc.c.

Referenced by encode_mb_hq(), and encode_thread().

◆ copy_context_before_encode()

static void copy_context_before_encode ( MpegEncContext d,
const MpegEncContext s 
)
inlinestatic

Definition at line 2537 of file mpegvideo_enc.c.

Referenced by encode_mb_hq(), and encode_thread().

◆ copy_context_after_encode()

static void copy_context_after_encode ( MpegEncContext d,
const MpegEncContext s 
)
inlinestatic

Definition at line 2564 of file mpegvideo_enc.c.

Referenced by encode_mb_hq(), and encode_thread().

◆ encode_mb_hq()

static void encode_mb_hq ( MpegEncContext s,
MpegEncContext backup,
MpegEncContext best,
PutBitContext  pb[2],
PutBitContext  pb2[2],
PutBitContext  tex_pb[2],
int *  dmin,
int *  next_block,
int  motion_x,
int  motion_y 
)
static

Definition at line 2602 of file mpegvideo_enc.c.

Referenced by encode_thread().

◆ sse()

static int sse ( MpegEncContext s,
const uint8_t *  src1,
const uint8_t *  src2,
int  w,
int  h,
int  stride 
)
static

◆ pre_estimate_motion_thread()

static int pre_estimate_motion_thread ( AVCodecContext c,
void *  arg 
)
static

Definition at line 2699 of file mpegvideo_enc.c.

Referenced by encode_picture().

◆ estimate_motion_thread()

static int estimate_motion_thread ( AVCodecContext c,
void *  arg 
)
static

Definition at line 2718 of file mpegvideo_enc.c.

Referenced by encode_picture().

◆ mb_var_thread()

static int mb_var_thread ( AVCodecContext c,
void *  arg 
)
static

Definition at line 2743 of file mpegvideo_enc.c.

Referenced by encode_picture().

◆ write_slice_end()

static void write_slice_end ( MpegEncContext s)
static

Definition at line 2766 of file mpegvideo_enc.c.

Referenced by encode_thread().

◆ write_mb_info()

static void write_mb_info ( MpegEncContext s)
static

Definition at line 2786 of file mpegvideo_enc.c.

Referenced by update_mb_info().

◆ update_mb_info()

static void update_mb_info ( MpegEncContext s,
int  startcode 
)
static

Definition at line 2806 of file mpegvideo_enc.c.

Referenced by encode_thread().

◆ ff_mpv_reallocate_putbitbuffer()

int ff_mpv_reallocate_putbitbuffer ( MpegEncContext s,
size_t  threshold,
size_t  size_increase 
)

Definition at line 2829 of file mpegvideo_enc.c.

Referenced by encode_thread(), and ff_mjpeg_encode_stuffing().

◆ encode_thread()

static int encode_thread ( AVCodecContext c,
void *  arg 
)
static

Definition at line 2863 of file mpegvideo_enc.c.

Referenced by encode_picture().

◆ merge_context_after_me()

static void merge_context_after_me ( MpegEncContext dst,
MpegEncContext src 
)
static

Definition at line 3502 of file mpegvideo_enc.c.

Referenced by encode_picture().

◆ merge_context_after_encode()

static void merge_context_after_encode ( MpegEncContext dst,
MpegEncContext src 
)
static

Definition at line 3508 of file mpegvideo_enc.c.

Referenced by encode_picture().

◆ estimate_qp()

static int estimate_qp ( MpegEncContext s,
int  dry_run 
)
static

Definition at line 3535 of file mpegvideo_enc.c.

Referenced by encode_picture().

◆ set_frame_distances()

static void set_frame_distances ( MpegEncContext s)
static

Definition at line 3571 of file mpegvideo_enc.c.

Referenced by encode_picture().

◆ build_basis()

static void build_basis ( uint8_t *  perm)
static

Definition at line 4212 of file mpegvideo_enc.c.

Referenced by dct_quantize_refine().

◆ ff_block_permute()

void ff_block_permute ( int16_t *  block,
const uint8_t *  permutation,
const uint8_t *  scantable,
int  last 
)

Permute an 8x8 block according to permutation.

Parameters
blockthe block which will be permuted according to the given permutation vector
permutationthe permutation vector
lastthe last non zero coefficient in scantable order, used to speed the permutation up
scantablethe used scantable, this is only used to speed the permutation up, the block is not (inverse) permutated to scantable order!

Definition at line 4558 of file mpegvideo_enc.c.

Referenced by dct_quantize_c(), dnxhd_10bit_dct_quantize(), and dnxhd_10bit_dct_quantize_444().

Variable Documentation

◆ default_mv_penalty

uint8_t default_mv_penalty[MAX_FCODE+1][MAX_DMV *2+1]
static

Definition at line 96 of file mpegvideo_enc.c.

Referenced by mpv_encode_defaults().

◆ default_fcode_tab

uint8_t default_fcode_tab[MAX_MV *2+1]
static

Definition at line 97 of file mpegvideo_enc.c.

Referenced by mpv_encode_defaults(), and mpv_encode_init_static().

◆ mpv_generic_options

const AVOption mpv_generic_options[]
static
Initial value:

Definition at line 99 of file mpegvideo_enc.c.

◆ ff_mpv_enc_class

const AVClass ff_mpv_enc_class
Initial value:
= {
.class_name = "generic mpegvideo encoder",
.item_name = av_default_item_name,
}

Definition at line 105 of file mpegvideo_enc.c.

◆ basis

int16_t basis[64][64]
static
FF_MPV_COMMON_MOTION_EST_OPTS
#define FF_MPV_COMMON_MOTION_EST_OPTS
Definition: mpegvideoenc.h:127
FF_MPV_COMMON_OPTS
#define FF_MPV_COMMON_OPTS
Definition: mpegvideoenc.h:84
AV_CODEC_FLAG_INTERLACED_DCT
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:330
s
#define s(width, name)
Definition: cbs_vp9.c:198
CHROMA_422
#define CHROMA_422
Definition: mpegvideo.h:460
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
NULL
#define NULL
Definition: coverity.c:32
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
mpv_generic_options
static const AVOption mpv_generic_options[]
Definition: mpegvideo_enc.c:99
CHROMA_420
#define CHROMA_420
Definition: mpegvideo.h:459