28 #include "libavutil/attributes.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/log.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/timecode.h"
42 0, 2, 4, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16,
46 0x10, 0x0E, 0x00, 0x80, 0x81, 0x00, 0x80,
47 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
68 for (i = 0; i < 128; i++) {
73 for (run = 0; run < 64; run++) {
75 int alevel =
FFABS(level);
106 for (i = 1; i < 14; i++) {
111 for (ext.
num=1; ext.
num <= 4; ext.
num++) {
112 for (ext.
den=1; ext.
den <= 32; ext.
den++) {
155 "MPEG1/2 does not support %d/%d fps, there may be AV sync issues\n",
178 "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
183 else if (avctx->
width <= 1440)
190 if ((avctx->
width & 0xFFF) == 0 && (avctx->
height & 0xFFF) == 1) {
196 if ((avctx->
width & 0xFFF) == 0 || (avctx->
height & 0xFFF) == 0) {
197 av_log(avctx,
AV_LOG_ERROR,
"Width or Height are not allowed to be multiplies of 4096\n"
208 "Drop frame time code only allowed with 1001/30000 fps\n");
235 unsigned int vbv_buffer_size, fps,
v;
236 int i, constraint_parameter_flag;
238 float best_aspect_error = 1E10;
241 if (aspect_ratio == 0.0)
253 for (i = 1; i < 15; i++) {
254 float error = aspect_ratio;
260 error =
FFABS(error);
262 if (error < best_aspect_error) {
263 best_aspect_error = error;
284 vbv_buffer_size = ((20 * s->
bit_rate) / (1151929 / 2)) * 8 * 1024;
285 vbv_buffer_size = (vbv_buffer_size + 16383) / 16384;
291 constraint_parameter_flag =
296 framerate.
num <= framerate.
den * 30 &&
299 vbv_buffer_size <= 20 &&
300 v <= 1856000 / 400 &&
303 put_bits(&s->
pb, 1, constraint_parameter_flag);
333 fps = (framerate.
num + framerate.
den / 2) / framerate.
den;
343 put_bits(&s->
pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
344 put_bits(&s->
pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
346 put_bits(&s->
pb, 6, (uint32_t)((time_code / fps) % 60));
347 put_bits(&s->
pb, 6, (uint32_t)((time_code % fps)));
478 int has_mv,
int field_motion)
498 int code, sign,
bits;
499 int bit_size = f_or_b_code - 1;
500 int range = 1 << bit_size;
506 code = (val >> bit_size) + 1;
507 bits = val & (range - 1);
512 code = (val >> bit_size) + 1;
513 bits = val & (range - 1);
531 if (((
unsigned) (diff + 255)) >= 511) {
544 (diff & ((1 << index) - 1)));
549 (diff & ((1 << index) - 1)));
564 int alevel,
level, last_non_zero,
dc, diff, i, j,
run, last_index, sign;
572 component = (n <= 3 ? 0 : (n & 1) + 1);
574 diff = dc - s->
last_dc[component];
584 if (abs(level) == 1) {
585 code = ((uint32_t)level >> 31);
596 last_non_zero = i - 1;
598 for (; i <= last_index; i++) {
605 run = i - last_non_zero - 1;
615 (table_vlc[code][0] << 1) + sign);
618 put_bits(&s->
pb, table_vlc[111][1], table_vlc[111][0]);
638 put_bits(&s->
pb, table_vlc[112][1], table_vlc[112][0]);
642 int16_t
block[6][64],
643 int motion_x,
int motion_y,
647 const int mb_x = s->
mb_x;
648 const int mb_y = s->
mb_y;
653 for (i = 0; i < mb_block_count; i++)
655 cbp |= 1 << (mb_block_count - 1 - i);
663 ? ((s->
mv[0][0][0] - s->
last_mv[0][0][0]) |
664 (s->
mv[0][0][1] - s->
last_mv[0][0][1])) : 0) |
666 ? ((s->
mv[1][0][0] - s->
last_mv[1][0][0]) |
667 (s->
mv[1][0][1] - s->
last_mv[1][0][1])) : 0)) == 0))) {
713 if ((motion_x | motion_y) == 0) {
733 motion_x - s->
last_mv[0][0][0],
737 motion_y - s->
last_mv[0][0][1],
748 motion_x - s->
last_mv[0][0][0],
752 motion_y - s->
last_mv[0][0][1],
775 for (i = 0; i < 2; i++) {
781 s->
mv[0][i][1] - (s->
last_mv[0][i][1] >> 1),
784 s->
last_mv[0][i][1] = 2 * s->
mv[0][i][1];
866 for (i = 0; i < 2; i++) {
872 s->
mv[0][i][1] - (s->
last_mv[0][i][1] >> 1),
875 s->
last_mv[0][i][1] = s->
mv[0][i][1] * 2;
880 for (i = 0; i < 2; i++) {
886 s->
mv[1][i][1] - (s->
last_mv[1][i][1] >> 1),
889 s->
last_mv[1][i][1] = s->
mv[1][i][1] * 2;
908 for (i = 0; i < mb_block_count; i++)
909 if (cbp & (1 << (mb_block_count - 1 - i)))
920 int motion_x,
int motion_y)
943 for (i = 0; i < 64; i++) {
953 for (i = -255; i < 256; i++) {
965 (diff & ((1 << index) - 1));
970 (diff & ((1 << index) - 1));
974 for (f_code = 1; f_code <=
MAX_FCODE; f_code++)
981 int val, bit_size, code;
983 bit_size = f_code - 1;
989 code = (val >> bit_size) + 1;
1002 for (f_code =
MAX_FCODE; f_code > 0; f_code--)
1003 for (mv = -(8 << f_code); mv < (8 << f_code); mv++)
1026 #define OFFSET(x) offsetof(MpegEncContext, x)
1027 #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
1028 #define COMMON_OPTS \
1029 { "gop_timecode", "MPEG GOP Timecode in hh:mm:ss[:;.]ff format", \
1030 OFFSET(tc_opt_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, VE },\
1031 { "intra_vlc", "Use MPEG-2 intra VLC table.", \
1032 OFFSET(intra_vlc_format), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \
1033 { "drop_frame_timecode", "Timecode is in drop frame format.", \
1034 OFFSET(drop_frame_timecode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \
1035 { "scan_offset", "Reserve space for SVCD scan offset user data.", \
1036 OFFSET(scan_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1046 {
"non_linear_quant",
"Use nonlinear quantizer.",
OFFSET(q_scale_type),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
VE },
1047 {
"alternate_scan",
"Enable alternate scantable.",
OFFSET(alternate_scan),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
VE },
1052 #define mpeg12_class(x) \
1053 static const AVClass mpeg ## x ## _class = { \
1054 .class_name = "mpeg" # x "video encoder", \
1055 .item_name = av_default_item_name, \
1056 .option = mpeg ## x ## _options, \
1057 .version = LIBAVUTIL_VERSION_INT, \
1064 .
name =
"mpeg1video",
1076 .priv_class = &mpeg1_class,
1080 .
name =
"mpeg2video",
1093 .priv_class = &mpeg2_class,
av_cold void ff_mpeg1_encode_init(MpegEncContext *s)
static uint8_t uni_mpeg1_ac_vlc_len[64 *64 *2]
#define MASK_ABS(mask, level)
AVCodec ff_mpeg2video_encoder
const char const char void * val
const AVRational ff_mpeg2_frame_rate_tab[]
uint8_t * fcode_tab
smallest fcode needed for each MV
#define MV_TYPE_FIELD
2 vectors, one per field
int last_mv[2][2][2]
last MV, used for MV prediction in MPEG1 & B-frame MPEG4
static int find_frame_rate_index(MpegEncContext *s)
static void put_sbits(PutBitContext *pb, int n, int32_t value)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
int end_mb_y
end mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y) ...
static av_cold int init(AVCodecContext *avctx)
int ff_MPV_encode_end(AVCodecContext *avctx)
#define FF_MPV_COMMON_OPTS
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
const unsigned char ff_mpeg12_vlc_dc_lum_bits[12]
void avpriv_align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
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 scan_offset
reserve space for SVCD scan offset user data.
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
int min_qcoeff
minimum encodable coefficient
#define CODEC_FLAG2_DROP_FRAME_TIMECODE
timecode is in drop frame format. DEPRECATED!!!!
const uint16_t ff_mpeg12_vlc_dc_lum_code[12]
uint8_t * intra_ac_vlc_length
#define UNI_AC_ENC_INDEX(run, level)
#define SLICE_MIN_START_CODE
AVCodec ff_mpeg1video_encoder
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
AVRational mpeg2_frame_rate_ext
static uint32_t mpeg1_lum_dc_uni[512]
static void encode_mb_skip_run(MpegEncContext *s, int run)
int me_range
maximum motion estimation search range in subpel units If 0 then no limit.
const float ff_mpeg1_aspect[16]
int misc_bits
cbp, mb_type
static const AVOption mpeg1_options[]
const char * name
Name of the codec implementation.
static void mpeg1_encode_block(MpegEncContext *s, int16_t *block, int n)
Picture current_picture
copy of the current picture structure.
static double av_q2d(AVRational a)
Convert rational to double.
uint8_t(* mv_penalty)[MAX_MV *2+1]
amount of bits needed to encode a MV
static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV *2+1]
AVRational av_mul_q(AVRational b, AVRational c) av_const
Multiply two rationals.
static int8_t mpeg1_max_level[2][64]
int start
timecode frame start (first base frame number)
int8_t * max_level[2]
encoding & decoding
int mb_height
number of MBs horizontally & vertically
int max_qcoeff
maximum encodable coefficient
int dquant
qscale difference to prev qscale
int gop_picture_number
index of the first picture of a GOP based on fake_pic_num & mpeg1 specific
int ff_MPV_encode_init(AVCodecContext *avctx)
static int get_bits_diff(MpegEncContext *s)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int last_dc[3]
last DC values for MPEG1
static void put_mb_modes(MpegEncContext *s, int n, int bits, int has_mv, int field_motion)
uint8_t * inter_ac_vlc_last_length
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);ff_audio_convert_init_arm(ac);ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> dc
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
int strict_std_compliance
strictly follow the std (MPEG4, ...)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static const uint8_t inv_non_linear_qscale[]
int av_nearer_q(AVRational q, AVRational q1, AVRational q2)
const uint16_t ff_mpeg12_vlc_dc_chroma_code[12]
static void encode_dc(MpegEncContext *s, int diff, int component)
int rc_max_rate
maximum bitrate
int64_t av_const av_gcd(int64_t a, int64_t b)
Return the greatest common divisor of a and b.
int low_delay
no reordering needed / has no b-frames
Libavcodec external API header.
AVPixelFormat
Pixel format.
static void mpeg1_encode_sequence_header(MpegEncContext *s)
static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
static int put_bits_count(PutBitContext *s)
int resync_mb_x
x position of last resync marker
int rc_buffer_size
decoder bitstream buffer size
int av_timecode_adjust_ntsc_framenum2(int framenum, int fps)
Adjust frame number for NTSC drop frame time code.
uint8_t * intra_ac_vlc_last_length
int64_t timecode_frame_start
GOP timecode frame start number.
uint8_t * vbv_delay_ptr
pointer to vbv_delay in the bitstream
const uint16_t(* table_vlc)[2]
static uint8_t fcode_tab[MAX_MV *2+1]
uint16_t * intra_matrix
custom intra quantization matrix
int width
picture width / height.
const unsigned char ff_mpeg12_vlc_dc_chroma_bits[12]
Picture * current_picture_ptr
pointer to the current picture
static uint8_t uni_mpeg2_ac_vlc_len[64 *64 *2]
int block_last_index[12]
last non zero coefficient in block
#define FF_PROFILE_UNKNOWN
static av_cold void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len)
static void put_header(MpegEncContext *s, int header)
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
preferred ID for MPEG-1/2 video decoding
void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
static const uint8_t svcd_scan_offset_placeholder[]
static const int8_t mv[256][2]
#define MV_TYPE_16X16
1 vector for the whole mb
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
int coded_picture_number
picture number in bitstream order
#define AV_LOG_INFO
Standard information.
int concealment_motion_vectors
char * tc_opt_str
timecode option string
main external API structure.
static void close(AVCodecParserContext *s)
ScanTable intra_scantable
int height
picture size. must be a multiple of 16
uint8_t * inter_ac_vlc_length
static uint8_t mpeg1_index_run[2][64]
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
rational number numerator/denominator
uint8_t * index_run[2]
encoding only
static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y, int mb_block_count)
void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
int f_code
forward MV resolution
#define CODEC_FLAG_CLOSED_GOP
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
int bit_rate
wanted bit rate
av_cold void ff_mpeg12_common_init(MpegEncContext *s)
static av_const int sign_extend(int val, unsigned bits)
int last_mv_dir
last mv_dir, used for b frame encoding
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
int mv[2][4][2]
motion vectors for a macroblock first coordinate : 0 = forward 1 = backward second " : depend...
int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
Parse timecode representation (hh:mm:ss[:;.
static av_cold int encode_init(AVCodecContext *avctx)
struct AVCodecContext * avctx
PutBitContext pb
bit output
const AVRational ff_mpeg2_aspect[16]
void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
static av_always_inline void put_qscale(MpegEncContext *s)
#define CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
static uint32_t mpeg1_chr_dc_uni[512]
static const AVOption mpeg2_options[]
uint8_t ff_mpeg12_static_rl_table_store[2][2][2 *MAX_RUN+MAX_LEVEL+3]
const uint8_t ff_mpeg12_mbPatTable[64][2]
void ff_mpeg1_encode_slice_header(MpegEncContext *s)
int last_bits
temp var used for calculating the above vars
int top_field_first
If the content is interlaced, is top field displayed first.
int drop_frame_timecode
timecode is in drop frame format.
int resync_mb_y
y position of last resync marker
av_cold void ff_init_rl(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
const uint8_t ff_mpeg12_mbAddrIncrTable[36][2]
int key_frame
1 -> keyframe, 0-> not
uint16_t * inter_matrix
custom inter quantization matrix
int flags
AVCodecContext.flags (HQ, MV4, ...)
const uint8_t ff_mpeg12_mbMotionVectorTable[17][2]
#define PICTURE_START_CODE
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int b_code
backward MV resolution for B Frames (mpeg4)
AVTimecode tc
timecode context
uint32_t flags
flags such as drop frame, +24 hours support, ...
const AVRational ff_mpeg12_frame_rate_tab[16]
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
int ff_MPV_encode_picture(AVCodecContext *avctx, AVPacket *pkt, AVFrame *frame, int *got_packet)
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.