48 #define UNI_MPEG4_ENC_INDEX(last, run, level) ((last) * 128 * 64 + (run) * 128 + (level))
65 int block_last_index,
uint8_t scantable[64])
71 for (j = 1; j <= block_last_index; j++) {
72 const int index = scantable[j];
76 if ((level & (~127)) == 0) {
77 if (j < block_last_index)
100 const int dir[6],
uint8_t *st[6],
101 const int zigzag_last_index[6])
106 for (n = 0; n < 6; n++) {
112 for (i = 1; i < 8; i++)
116 for (i = 1; i < 8; i++)
131 const int dir[6],
uint8_t *st[6],
132 int zigzag_last_index[6])
140 for (n = 0; n < 6; n++) {
141 int16_t *ac_val, *ac_val1;
152 if (s->
mb_y == 0 || s->
qscale == qscale_table[xy] || n == 2 || n == 3) {
154 for (i = 1; i < 8; i++) {
158 ac_val1[i + 8] =
level;
162 for (i = 1; i < 8; i++) {
166 ac_val1[i + 8] =
level;
174 if (s->
mb_x == 0 || s->
qscale == qscale_table[xy] || n == 1 || n == 3) {
176 for (i = 1; i < 8; i++) {
184 for (i = 1; i < 8; i++) {
194 for (i = 63; i > 0; i--)
195 if (block[n][st[n][i]])
225 for (i = 0; i < s->
mb_num; i++) {
227 odd += qscale_table[mb_xy] & 1;
235 for (i = 0; i < s->
mb_num; i++) {
237 if ((qscale_table[mb_xy] & 1) != odd)
238 qscale_table[mb_xy]++;
239 if (qscale_table[mb_xy] > 31)
240 qscale_table[mb_xy] = 31;
243 for (i = 1; i < s->
mb_num; i++) {
245 if (qscale_table[mb_xy] != qscale_table[s->
mb_index2xy[i - 1]] &&
283 int16_t *
block,
int n,
int intra_dc,
287 int i, last_non_zero;
309 last_non_zero = i - 1;
310 for (; i < last_index; i++) {
311 int level = block[scan_table[i]];
313 int run = i - last_non_zero - 1;
315 if ((level & (~127)) == 0) {
317 put_bits(ac_pb, len_tab[index], bits_tab[index]);
320 7 + 2 + 1 + 6 + 1 + 12 + 1,
321 (3 << 23) + (3 << 21) + (0 << 20) + (run << 14) +
322 (1 << 13) + (((level - 64) & 0xfff) << 1) + 1);
328 int level = block[scan_table[i]];
329 int run = i - last_non_zero - 1;
331 if ((level & (~127)) == 0) {
333 put_bits(ac_pb, len_tab[index], bits_tab[index]);
336 7 + 2 + 1 + 6 + 1 + 12 + 1,
337 (3 << 23) + (3 << 21) + (1 << 20) + (run << 14) +
338 (1 << 13) + (((level - 64) & 0xfff) << 1) + 1);
345 int intra_dc,
uint8_t *scan_table)
347 int i, last_non_zero;
367 last_non_zero = i - 1;
368 for (; i < last_index; i++) {
369 int level = block[scan_table[i]];
371 int run = i - last_non_zero - 1;
373 if ((level & (~127)) == 0) {
375 len += len_tab[
index];
377 len += 7 + 2 + 1 + 6 + 1 + 12 + 1;
383 int level = block[scan_table[i]];
384 int run = i - last_non_zero - 1;
386 if ((level & (~127)) == 0) {
388 len += len_tab[
index];
390 len += 7 + 2 + 1 + 6 + 1 + 12 + 1;
398 int intra_dc[6],
uint8_t **scan_table,
406 for (i = 0; i < 6; i++)
409 intra_dc[i], scan_table[i]));
412 for (i = 0; i < 6; i++)
414 intra_dc[i], scan_table[i], dc_pb, ac_pb);
418 for (i = 0; i < 6; i++)
424 for (i = 0; i < 6; i++)
432 int motion_x,
int motion_y,
int mb_type)
440 for (i = 0; i < 6; i++) {
449 if ((motion_x | motion_y | s->
dquant | mb_type) == 0)
452 zero_score *= lambda;
453 if (zero_score <= score)
457 for (i = 0; i < 6; i++) {
464 for (i = 0; i < 6; i++) {
476 int motion_x,
int motion_y)
478 int cbpc, cbpy, pred_x, pred_y;
489 static const int mb_type_table[8] = { -1, 3, 2, 1, -1, -1, -1, 0 };
490 int mb_type = mb_type_table[s->
mv_dir];
493 for (i = 0; i < 2; i++)
518 cbp =
get_b_cbp(s, block, motion_x, motion_y, mb_type);
520 if ((cbp | motion_x | motion_y | mb_type) == 0) {
526 if (interleaved_stats) {
540 if (cbp && mb_type) {
555 if (interleaved_stats)
598 for (i = 0; i < 2; i++) {
601 s->
mv[0][i][1] - s->
last_mv[0][i][1] / 2,
604 s->
last_mv[0][i][1] = s->
mv[0][i][1] * 2;
609 for (i = 0; i < 2; i++) {
612 s->
mv[1][i][1] - s->
last_mv[1][i][1] / 2,
615 s->
last_mv[1][i][1] = s->
mv[1][i][1] * 2;
622 if (interleaved_stats)
627 if (interleaved_stats)
630 cbp =
get_p_cbp(s, block, motion_x, motion_y);
632 if ((cbp | motion_x | motion_y | s->
dquant) == 0 &&
666 for (y1 = 0; y1 < ye; y1++) {
667 for (x1 = 0; x1 < xe; x1++) {
671 diff = diff * 256 / (xe * ye);
675 if (diff > s->
qscale * 70) {
687 if (interleaved_stats) {
718 if (interleaved_stats)
744 if (interleaved_stats)
755 s->
mv[0][0][0] - pred_x,
756 s->
mv[0][0][1] - pred_y,
759 s->
mv[0][1][0] - pred_x,
760 s->
mv[0][1][1] - pred_y,
772 if (interleaved_stats)
775 for (i = 0; i < 4; i++) {
786 if (interleaved_stats)
791 if (interleaved_stats)
800 int zigzag_last_index[6];
804 for (i = 0; i < 6; i++)
810 for (i = 0; i < 6; i++)
816 for (i = 0; i < 6; i++)
844 if (interleaved_stats)
849 if (interleaved_stats)
869 put_bits(pbc, length, (1 << length) - 1);
885 int64_t hours, minutes, seconds;
898 minutes =
FFUDIV(seconds, 60); seconds =
FFUMOD(seconds, 60);
899 hours =
FFUDIV(minutes, 60); minutes =
FFUMOD(minutes, 60);
900 hours =
FFUMOD(hours , 24);
915 int profile_and_level_indication;
921 profile_and_level_indication = 0xF0;
923 profile_and_level_indication = 0x00;
927 profile_and_level_indication |= s->
avctx->
level;
929 profile_and_level_indication |= 1;
931 if (profile_and_level_indication >> 4 == 0xF)
941 put_bits(&s->
pb, 8, profile_and_level_indication);
963 if (!CONFIG_MPEG4_ENCODER)
1044 if (vo_ver_id != 1) {
1064 int64_t time_div, time_mod;
1088 if (time_incr > 3600) {
1123 int level, uni_code, uni_len;
1125 for (level = -256; level < 256; level++) {
1136 l = (-
level) ^ ((1 << size) - 1);
1179 int slevel,
run, last;
1184 for (slevel = -64; slevel < 64; slevel++) {
1187 for (run = 0; run < 64; run++) {
1188 for (last = 0; last <= 1; last++) {
1190 int level = slevel < 0 ? -slevel : slevel;
1191 int sign = slevel < 0 ? 1 : 0;
1192 int bits,
len, code;
1195 len_tab[
index] = 100;
1201 bits = bits * 2 + sign;
1204 if (code != rl->
n && len < len_tab[index]) {
1205 bits_tab[
index] = bits;
1219 bits = bits * 2 + sign;
1222 if (code != rl->
n && len < len_tab[index]) {
1223 bits_tab[
index] = bits;
1230 bits = bits * 4 + 2;
1238 bits = bits * 2 + sign;
1241 if (code != rl->
n && len < len_tab[index]) {
1242 bits_tab[
index] = bits;
1249 bits = bits * 4 + 3;
1251 bits = bits * 2 + last;
1253 bits = bits * 64 +
run;
1255 bits = bits * 2 + 1;
1257 bits = bits * 4096 + (slevel & 0xfff);
1259 bits = bits * 2 + 1;
1262 if (len < len_tab[index]) {
1263 bits_tab[
index] = bits;
1275 static int done = 0;
1277 if (avctx->
width >= (1<<13) || avctx->
height >= (1<<13)) {
1327 int pb_size = (((intptr_t)start + size / 3) & (~3)) - (intptr_t)start;
1328 int tex_size = (size - 2 * pb_size) & (~3);
1373 #define OFFSET(x) offsetof(MpegEncContext, x)
1374 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1376 {
"data_partitioning",
"Use data partitioning.",
OFFSET(data_partitioning),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VE },
1377 {
"alternate_scan",
"Enable alternate scantable.",
OFFSET(alternate_scan),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VE },
static void mpeg4_encode_visual_object_header(MpegEncContext *s)
ScanTable intra_v_scantable
RLTable ff_mpeg4_rl_intra
const uint8_t ff_mpeg4_c_dc_scale_table[32]
#define MV_TYPE_FIELD
2 vectors, one per field
static uint8_t uni_mpeg4_intra_rl_len[64 *64 *2 *2]
const uint8_t * y_dc_scale_table
qscale -> y_dc_scale table
static void mpeg4_encode_block(MpegEncContext *s, int16_t *block, int n, int intra_dc, uint8_t *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
Encode an 8x8 block.
void(* clear_block)(int16_t *block)
int last_mv[2][2][2]
last MV, used for MV prediction in MPEG-1 & B-frame MPEG-4
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
#define LIBAVUTIL_VERSION_INT
void ff_clean_mpeg4_qscales(MpegEncContext *s)
modify mb_type & qscale so that encoding is actually possible in MPEG-4
static av_cold int init(AVCodecContext *avctx)
int16_t(*[3] ac_val)[16]
used for MPEG-4 AC prediction, all 3 arrays must be continuous
static int get_block_rate(MpegEncContext *s, int16_t block[64], int block_last_index, uint8_t scantable[64])
Return the number of bits that encoding the 8x8 block in block would need.
#define FF_COMPLIANCE_VERY_STRICT
Strictly conform to an older more strict version of the spec or reference software.
#define FF_MPV_COMMON_OPTS
#define CANDIDATE_MB_TYPE_BIDIR
static void skip_put_bits(PutBitContext *s, int n)
Skip the given number of bits.
const char * av_default_item_name(void *ptr)
Return the context name.
void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
int min_qcoeff
minimum encodable coefficient
static uint32_t uni_mpeg4_intra_rl_bits[64 *64 *2 *2]
int mpv_flags
flags set by private options
uint8_t * intra_ac_vlc_length
#define UNI_AC_ENC_INDEX(run, level)
int mb_num
number of MBs of a picture
int time_base
time in seconds of last I,P,S Frame
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
static av_cold void init_uni_dc_tab(void)
Macro definitions for various function/variable attributes.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
#define FF_BUG_MS
Work around various bugs in Microsoft's broken decoders.
#define FF_MPV_FLAG_CBP_RD
void ff_mpeg4_stuffing(PutBitContext *pbc)
add MPEG-4 stuffing bits (01...1)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
static uint8_t uni_DCtab_chrom_len[512]
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int8_t * max_run[2]
encoding & decoding
int64_t time
time of current frame
#define MV_DIRECT
bidirectional mode where the difference equals the MV of the last P/S/I-Frame (MPEG-4) ...
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
PutBitContext pb2
used for data partitioned VOPs
static av_cold int end(AVCodecContext *avctx)
static int decide_ac_pred(MpegEncContext *s, int16_t block[6][64], const int dir[6], uint8_t *st[6], int zigzag_last_index[6])
Return the optimal value (0 or 1) for the ac_pred element for the given MB in MPEG-4.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
int misc_bits
cbp, mb_type
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int no_rounding
apply no rounding to motion compensation (MPEG-4, msmpeg4, ...) for B-frames rounding mode is always ...
Picture current_picture
copy of the current picture structure.
const uint8_t ff_mpeg4_DCtab_chrom[13][2]
int8_t * max_level[2]
encoding & decoding
const uint8_t ff_h263_intra_MCBPC_bits[9]
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
int max_qcoeff
maximum encodable coefficient
static const int dquant_code[5]
static uint32_t uni_mpeg4_inter_rl_bits[64 *64 *2 *2]
static int ff_mpeg4_pred_dc(MpegEncContext *s, int n, int level, int *dir_ptr, int encoding)
Predict the dc.
int dquant
qscale difference to prev qscale
#define UNI_MPEG4_ENC_INDEX(last, run, level)
#define ROUNDED_DIV(a, b)
static void mpeg4_encode_gop_header(MpegEncContext *s)
static int get_bits_diff(MpegEncContext *s)
void ff_mpeg4_init_partitions(MpegEncContext *s)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
const uint8_t ff_h263_inter_MCBPC_code[28]
static void restore_ac_coeffs(MpegEncContext *s, int16_t block[6][64], const int dir[6], uint8_t *st[6], const int zigzag_last_index[6])
Restore the ac coefficients in block that have been changed by decide_ac_pred().
uint8_t * inter_ac_vlc_last_length
void ff_mpeg4_merge_partitions(MpegEncContext *s)
int mb_skipped
MUST BE SET only during DECODING.
int strict_std_compliance
strictly follow the std (MPEG-4, ...)
int partitioned_frame
is current frame partitioned
int ff_mpeg4_encode_picture_header(MpegEncContext *s, int picture_number)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void ff_mpeg4_encode_mb(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
int flags
AV_CODEC_FLAG_*.
void ff_mpeg4_init_direct_mv(MpegEncContext *s)
const char * name
Name of the codec implementation.
int quarter_sample
1->qpel, 0->half pel ME/MC
uint16_t * mb_type
Table for candidate MB types for encoding (defines in mpegutils.h)
av_const int ff_h263_aspect_to_info(AVRational aspect)
Return the 4 bit value that specifies the given aspect ratio.
int low_delay
no reordering needed / has no B-frames
static av_cold void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab)
static const uint8_t offset[127][2]
static int put_bits_count(PutBitContext *s)
void ff_clean_h263_qscales(MpegEncContext *s)
modify qscale so that encoding is actually possible in H.263 (limit difference to -2...
uint8_t * intra_ac_vlc_last_length
#define ADV_SIMPLE_VO_TYPE
const uint8_t ff_mpeg4_DCtab_lum[13][2]
uint8_t ff_mpeg4_static_rl_table_store[3][2][2 *MAX_RUN+MAX_LEVEL+3]
int n
number of entries of table_vlc minus 1
const uint8_t ff_h263_inter_MCBPC_bits[28]
enum AVPictureType pict_type
Picture type of the frame.
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
const uint8_t ff_mpeg4_y_dc_scale_table[32]
const uint16_t(* table_vlc)[2]
#define AV_CODEC_FLAG_AC_PRED
H.263 advanced intra coding / MPEG-4 AC prediction.
Picture new_picture
copy of the source picture structure for encoding.
static uint8_t uni_mpeg4_inter_rl_len[64 *64 *2 *2]
int width
picture width / height.
int16_t(*[2] motion_val)[2]
Picture * current_picture_ptr
pointer to the current picture
#define FF_PROFILE_UNKNOWN
static const AVClass mpeg4enc_class
static uint16_t uni_DCtab_lum_bits[512]
static uint16_t uni_DCtab_chrom_bits[512]
static int get_b_cbp(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y, int mb_type)
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
static uint8_t uni_DCtab_lum_len[512]
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
int block_last_index[12]
last non zero coefficient in block
uint8_t idct_permutation[64]
IDCT input permutation.
int ac_esc_length
num of bits needed to encode the longest esc
static void set_put_bits_buffer_size(PutBitContext *s, int size)
Change the end of the buffer.
void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
int block_index[6]
index to current MB in block based arrays with edges
int * mb_index2xy
mb_index -> mb_x + mb_y*mb_stride
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
#define MV_TYPE_16X16
1 vector for the whole mb
uint8_t * luma_dc_vlc_length
unsigned int lambda2
(lambda*lambda) >> FF_LAMBDA_SHIFT
#define FF_ASPECT_EXTENDED
ptrdiff_t linesize
line size, in bytes, may be different from width
const uint8_t ff_h263_intra_MCBPC_code[9]
main external API structure.
ScanTable intra_scantable
int height
picture size. must be a multiple of 16
av_cold int ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
int ff_mpv_encode_init(AVCodecContext *avctx)
int data_partitioning
data partitioning flag from header
uint8_t * inter_ac_vlc_length
uint16_t * intra_matrix
custom intra quantization matrix
Describe the class of an AVClass context structure.
ScanTable intra_h_scantable
#define CANDIDATE_MB_TYPE_DIRECT
static av_cold int encode_init(AVCodecContext *avctx)
static int mpeg4_get_block_length(MpegEncContext *s, int16_t *block, int n, int intra_dc, uint8_t *scan_table)
static void ff_h263_encode_motion_vector(MpegEncContext *s, int x, int y, int f_code)
int f_code
forward MV resolution
const uint8_t ff_h263_cbpy_tab[16][2]
uint16_t * inter_matrix
custom inter quantization matrix
int max_b_frames
max number of B-frames for encoding
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
static enum AVPixelFormat pix_fmts[]
int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
const uint8_t * c_dc_scale_table
qscale -> c_dc_scale table
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
int mv[2][4][2]
motion vectors for a macroblock first coordinate : 0 = forward 1 = backward second " : depend...
struct AVCodecContext * avctx
PutBitContext pb
bit output
#define VISUAL_OBJ_STARTCODE
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 ...
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
#define AV_CODEC_FLAG2_NO_OUTPUT
Skip bitstream encoding.
static const AVOption options[]
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
int ff_mpv_encode_end(AVCodecContext *avctx)
static int get_rl_index(const RLTable *rl, int last, int run, int level)
void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
int last_bits
temp var used for calculating the above vars
static av_always_inline int diff(const uint32_t a, const uint32_t b)
int top_field_first
If the content is interlaced, is top field displayed first.
int16_t(* block)[64]
points to one of the following blocks
PutBitContext tex_pb
used for data partitioned VOPs
Picture next_picture
copy of the next picture structure.
static int get_p_cbp(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
Picture ** reordered_input_picture
pointer to the next pictures in coded order for encoding
int flags2
AV_CODEC_FLAG2_*.
void avpriv_put_string(PutBitContext *pb, const char *string, int terminate_string)
Put the string string in the bitstream.
int workaround_bugs
workaround bugs in encoders which cannot be detected automatically
static int mpeg4_get_dc_length(int level, int n)
void ff_set_mpeg4_time(MpegEncContext *s)
#define AV_CODEC_FLAG_CLOSED_GOP
static void mpeg4_encode_blocks(MpegEncContext *s, int16_t block[6][64], int intra_dc[6], uint8_t **scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
#define MV_TYPE_8X8
4 vectors (H.263, MPEG-4 4MV)
int b_code
backward MV resolution for B-frames (MPEG-4)
int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
static void mpeg4_encode_dc(PutBitContext *s, int level, int n)
Encode the dc value.
AVPixelFormat
Pixel format.
static void mpeg4_encode_vol_header(MpegEncContext *s, int vo_number, int vol_number)