Commit b5accf82 authored by michaelni's avatar michaelni

mpeg2 encoding


git-svn-id: file:///var/local/repositories/ffmpeg/trunk@2164 9553f0bf-9b14-0410-a0b8-cfaf0461ba5b
parent 178ab5c3
......@@ -52,6 +52,7 @@ void avcodec_register_all(void)
register_avcodec(&mpeg1video_encoder);
// register_avcodec(&h264_encoder);
#ifdef CONFIG_RISKY
register_avcodec(&mpeg2video_encoder);
register_avcodec(&h263_encoder);
register_avcodec(&h263p_encoder);
register_avcodec(&flv_encoder);
......
......@@ -15,14 +15,15 @@ extern "C" {
#define LIBAVCODEC_VERSION_INT 0x000406
#define LIBAVCODEC_VERSION "0.4.6"
#define LIBAVCODEC_BUILD 4675
#define LIBAVCODEC_BUILD_STR "4675"
#define LIBAVCODEC_BUILD 4676
#define LIBAVCODEC_BUILD_STR "4676"
#define LIBAVCODEC_IDENT "FFmpeg" LIBAVCODEC_VERSION "b" LIBAVCODEC_BUILD_STR
enum CodecID {
CODEC_ID_NONE,
CODEC_ID_MPEG1VIDEO,
CODEC_ID_MPEG2VIDEO,
CODEC_ID_MPEG2VIDEO_XVMC,
CODEC_ID_H263,
CODEC_ID_RV10,
......@@ -219,6 +220,7 @@ static const int Motion_Est_QTab[] = { ME_ZERO, ME_PHODS, ME_LOG,
#define CODEC_CAP_PARSE_ONLY 0x0004
#define CODEC_CAP_TRUNCATED 0x0008
#define FF_COMMON_FRAME \
/**\
* pointer to the picture planes.\
......@@ -462,7 +464,7 @@ typedef struct AVCodecContext {
* do something for a generic case as well.
* - decoding: set by lavc.
*/
enum PixelFormat pix_fmt;
enum PixelFormat pix_fmt; //FIXME move to AVFrme
/**
* Frame rate emulation. If not zero lower layer (i.e. format handler)
......@@ -710,7 +712,7 @@ typedef struct AVCodecContext {
data is returned. Only MPEG codecs support this now. */
/**
* 0-> h263 quant 1-> mpeg quant.
* 0-> default, 1-> mpeg quant.
* - encoding: set by user.
* - decoding: unused
*/
......@@ -1281,6 +1283,7 @@ extern AVCodec mp2_encoder;
extern AVCodec mp3lame_encoder;
extern AVCodec oggvorbis_encoder;
extern AVCodec mpeg1video_encoder;
extern AVCodec mpeg2video_encoder;
extern AVCodec h263_encoder;
extern AVCodec h263p_encoder;
extern AVCodec flv_encoder;
......@@ -1587,7 +1590,7 @@ typedef enum {
int avcodec(void* handle, avc_cmd_t cmd, void* pin, void* pout);
/* memory */
void *av_malloc(unsigned int size);
void *av_malloc(unsigned int size); //FIXME unsigned could be bad, dunno, need thinking
void *av_mallocz(unsigned int size);
void *av_realloc(void *ptr, unsigned int size);
void av_free(void *ptr);
......
......@@ -32,8 +32,8 @@
#include "dsputil.h"
#include "mpegvideo.h"
//#undef NDEBUG
//#include <assert.h>
#undef NDEBUG
#include <assert.h>
#define SQ(a) ((a)*(a))
......@@ -1507,7 +1507,7 @@ void ff_estimate_b_frame_motion(MpegEncContext * s,
type = MB_TYPE_FORWARD;
// RAL: No MB_TYPE_DIRECT in MPEG-1 video (only MPEG-4)
if (s->codec_id != CODEC_ID_MPEG1VIDEO && dmin <= score){
if (s->codec_id == CODEC_ID_MPEG4 && dmin <= score){
score = dmin;
type = MB_TYPE_DIRECT;
}
......@@ -1586,8 +1586,9 @@ void ff_fix_long_p_mvs(MpegEncContext * s)
{
const int f_code= s->f_code;
int y, range;
assert(s->pict_type==P_TYPE);
range = (((s->codec_id == CODEC_ID_MPEG1VIDEO) ? 8 : 16) << f_code);
range = (((s->out_format == FMT_MPEG1) ? 8 : 16) << f_code);
if(s->msmpeg4_version) range= 16;
......@@ -1647,7 +1648,7 @@ void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, i
int y;
// RAL: 8 in MPEG-1, 16 in MPEG-4
int range = (((s->codec_id == CODEC_ID_MPEG1VIDEO) ? 8 : 16) << f_code);
int range = (((s->out_format == FMT_MPEG1) ? 8 : 16) << f_code);
if(s->avctx->me_range && range > s->avctx->me_range) range= s->avctx->me_range;
......@@ -1673,3 +1674,53 @@ void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, i
}
}
}
#if 0
/**
* estimates global motion and inits sprite_ref
*/
void ff_estimate_global_motion(MpegEncContext *s, int sprite_ref[3][2]){
int y;
int num= 16<<s->f_code;
int score[2][num];
int best_i[2]={0,0};
int best_score[2]={0,0};
memset(score, 0, 2*num*sizeof(int));
for(y=0; y<s->mb_height; y++){
int x;
int xy= (y+1)* (s->mb_width+2)+1;
int i= y*s->mb_width;
for(x=0; x<s->mb_width; x++){
int mv[2];
if(!(s->mb_type[i]&MB_TYPE_INTER))
continue;
mv[0]= s->p_mv_table[xy][0];
mv[1]= s->p_mv_table[xy][1];
if(mv[0]==0 && mv[1]==0) continue;
score[0][mv[0] + num/2]++;
score[1][mv[1] + num/2]++;
}
}
for(n=0; n<2; n++){
for(i=1; i<num-1; i++){
int s= score[n][i-1] + score[n][i]*2 + score[n][i+1];
if(s > best_score[n]){
best_score[n]= s;
best_i[n]= i;
}
}
}
sprite_ref[0][0]= best_i[0] - num/2;
sprite_ref[0][1]= best_i[1] - num/2;
// decide block type
}
#endif
......@@ -19,7 +19,7 @@
/**
* @file mpeg12.c
* MPEG1 codec / MPEG2 decoder.
* MPEG1/2 codec
*/
//#define DEBUG
......@@ -228,9 +228,9 @@ static void mpeg1_encode_sequence_header(MpegEncContext *s)
put_bits(&s->pb, 4, s->aspect_ratio_info);
put_bits(&s->pb, 4, s->frame_rate_index);
v = (s->bit_rate + 399) / 400;
if (v > 0x3ffff)
if (v > 0x3ffff && s->codec_id == CODEC_ID_MPEG1VIDEO)
v = 0x3ffff;
put_bits(&s->pb, 18, v);
put_bits(&s->pb, 18, v & 0x3FFFF);
put_bits(&s->pb, 1, 1); /* marker */
if(s->avctx->rc_buffer_size)
......@@ -238,12 +238,30 @@ static void mpeg1_encode_sequence_header(MpegEncContext *s)
else
/* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
put_bits(&s->pb, 10, (vbv_buffer_size + 16383) / 16384);
put_bits(&s->pb, 10, ((vbv_buffer_size + 16383) / 16384) & 0x3FF);
put_bits(&s->pb, 1, 1); /* constrained parameter flag */
ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
if(s->codec_id == CODEC_ID_MPEG2VIDEO){
put_header(s, EXT_START_CODE);
put_bits(&s->pb, 4, 1); //seq ext
put_bits(&s->pb, 1, 0); //esc
put_bits(&s->pb, 3, 4); //profile
put_bits(&s->pb, 4, 8); //level
put_bits(&s->pb, 1, s->progressive_sequence=1);
put_bits(&s->pb, 2, 1); //chroma format 4:2:0
put_bits(&s->pb, 2, 0); //horizontal size ext
put_bits(&s->pb, 2, 0); //vertical size ext
put_bits(&s->pb, 12, v>>18); //bitrate ext
put_bits(&s->pb, 1, 1); //marker
put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext
put_bits(&s->pb, 1, s->low_delay);
put_bits(&s->pb, 2, 0); // frame_rate_ext_n
put_bits(&s->pb, 5, 0); // frame_rate_ext_d
}
put_header(s, GOP_START_CODE);
put_bits(&s->pb, 1, 0); /* do drop frame */
/* time code : we must convert from the real frame rate to a
......@@ -286,6 +304,8 @@ static inline void encode_mb_skip_run(MpegEncContext *s, int run){
/* insert a fake P picture */
static void mpeg1_skip_picture(MpegEncContext *s, int pict_num)
{
assert(s->codec_id == CODEC_ID_MPEG1VIDEO); // mpeg2 can do these repeat things
/* mpeg1 picture header */
put_header(s, PICTURE_START_CODE);
/* temporal reference */
......@@ -376,6 +396,27 @@ void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
put_bits(&s->pb, 1, 0); /* extra bit picture */
if(s->codec_id == CODEC_ID_MPEG2VIDEO){
put_header(s, EXT_START_CODE);
put_bits(&s->pb, 4, 8); //pic ext
put_bits(&s->pb, 4, s->f_code);
put_bits(&s->pb, 4, s->f_code);
put_bits(&s->pb, 4, s->b_code);
put_bits(&s->pb, 4, s->b_code);
put_bits(&s->pb, 2, s->intra_dc_precision);
put_bits(&s->pb, 2, s->picture_structure= PICT_FRAME);
put_bits(&s->pb, 1, s->top_field_first);
put_bits(&s->pb, 1, s->frame_pred_frame_dct= 1);
put_bits(&s->pb, 1, s->concealment_motion_vectors);
put_bits(&s->pb, 1, s->q_scale_type);
put_bits(&s->pb, 1, s->intra_vlc_format);
put_bits(&s->pb, 1, s->alternate_scan);
put_bits(&s->pb, 1, s->repeat_first_field);
put_bits(&s->pb, 1, s->chroma_420_type=1);
put_bits(&s->pb, 1, s->progressive_frame=1);
put_bits(&s->pb, 1, 0); //composite_display_flag
}
s->mb_y=0;
ff_mpeg1_encode_slice_header(s);
}
......@@ -396,7 +437,7 @@ void mpeg1_encode_mb(MpegEncContext *s,
cbp |= 1 << (5 - i);
}
if (cbp == 0 && !first_mb && (mb_x != s->mb_width - 1 || mb_y != s->mb_height - 1) &&
if (cbp == 0 && !first_mb && (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) &&
((s->pict_type == P_TYPE && (motion_x | motion_y) == 0) ||
(s->pict_type == B_TYPE && s->mv_dir == s->last_mv_dir && (((s->mv_dir & MV_DIR_FORWARD) ? ((s->mv[0][0][0] - s->last_mv[0][0][0])|(s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
((s->mv_dir & MV_DIR_BACKWARD) ? ((s->mv[1][0][0] - s->last_mv[1][0][0])|(s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
......@@ -708,8 +749,13 @@ void ff_mpeg1_encode_init(MpegEncContext *s)
}
s->me.mv_penalty= mv_penalty;
s->fcode_tab= fcode_tab;
if(s->codec_id == CODEC_ID_MPEG1VIDEO){
s->min_qcoeff=-255;
s->max_qcoeff= 255;
}else{
s->min_qcoeff=-2047;
s->max_qcoeff= 2047;
}
s->intra_ac_vlc_length=
s->inter_ac_vlc_length= uni_mpeg1_ac_vlc_len;
}
......@@ -747,6 +793,12 @@ static void mpeg1_encode_block(MpegEncContext *s,
encode_dc(s, diff, component);
s->last_dc[component] = dc;
i = 1;
/*
if (s->intra_vlc_format)
rl = &rl_mpeg2;
else
rl = &rl_mpeg1;
*/
} else {
/* encode the first coefficient : needs to be done here because
it is handled slightly differently */
......@@ -791,6 +843,7 @@ static void mpeg1_encode_block(MpegEncContext *s,
put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]);
/* escape: only clip in this case */
put_bits(&s->pb, 6, run);
if(s->codec_id == CODEC_ID_MPEG1VIDEO){
if (alevel < 128) {
put_bits(&s->pb, 8, level & 0xff);
} else {
......@@ -800,6 +853,9 @@ static void mpeg1_encode_block(MpegEncContext *s,
put_bits(&s->pb, 16, level & 0xffff);
}
}
}else{
put_bits(&s->pb, 12, level & 0xfff);
}
}
last_non_zero = i;
}
......@@ -868,7 +924,7 @@ static inline int get_dmv(MpegEncContext *s)
static inline int get_qscale(MpegEncContext *s)
{
int qscale = get_bits(&s->gb, 5);
if (s->mpeg2) {
if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
if (s->q_scale_type) {
return non_linear_qscale[qscale];
} else {
......@@ -987,7 +1043,7 @@ static int mpeg_decode_mb(MpegEncContext *s,
memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
s->mb_intra = 1;
if (s->mpeg2) {
if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
for(i=0;i<6;i++) {
if (mpeg2_decode_block_intra(s, block[i], i) < 0)
return -1;
......@@ -1165,7 +1221,7 @@ static int mpeg_decode_mb(MpegEncContext *s,
}
cbp++;
if (s->mpeg2) {
if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
for(i=0;i<6;i++) {
if (cbp & 32) {
if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
......@@ -1648,9 +1704,12 @@ static void mpeg_decode_sequence_extension(MpegEncContext *s)
int horiz_size_ext, vert_size_ext;
int bit_rate_ext, vbv_buf_ext;
int frame_rate_ext_n, frame_rate_ext_d;
int level, profile;
float aspect;
skip_bits(&s->gb, 8); /* profil and level */
skip_bits(&s->gb, 1); /* profil and level esc*/
profile= get_bits(&s->gb, 3);
level= get_bits(&s->gb, 4);
s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
skip_bits(&s->gb, 2); /* chroma_format */
horiz_size_ext = get_bits(&s->gb, 2);
......@@ -1675,12 +1734,15 @@ static void mpeg_decode_sequence_extension(MpegEncContext *s)
1<<30);
dprintf("sequence extension\n");
s->mpeg2 = 1;
s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
s->avctx->sub_id = 2; /* indicates mpeg2 found */
aspect= mpeg2_aspect[s->aspect_ratio_info];
if(aspect>0.0) s->avctx->aspect_ratio= s->width/(aspect*s->height);
else if(aspect<0.0) s->avctx->aspect_ratio= -1.0/aspect;
if(s->avctx->debug & FF_DEBUG_PICT_INFO)
printf("profile: %d, level: %d \n", profile, level);
}
static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
......@@ -2046,7 +2108,7 @@ static int slice_end(AVCodecContext *avctx, AVFrame *pict)
if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
/* end of image */
if(s->mpeg2){
if(s->codec_id == CODEC_ID_MPEG2VIDEO){
s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
}else
s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG1;
......@@ -2089,7 +2151,7 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx,
width = get_bits(&s->gb, 12);
height = get_bits(&s->gb, 12);
s->aspect_ratio_info= get_bits(&s->gb, 4);
if(!s->mpeg2){
if(s->codec_id == CODEC_ID_MPEG1VIDEO){
aspect= mpeg1_aspect[s->aspect_ratio_info];
if(aspect!=0.0) avctx->aspect_ratio= width/(aspect*height);
}
......@@ -2187,7 +2249,7 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx,
s->progressive_frame = 1;
s->picture_structure = PICT_FRAME;
s->frame_pred_frame_dct = 1;
s->mpeg2 = 0;
s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
avctx->sub_id = 1; /* indicates mpeg1 */
return 0;
}
......@@ -2233,7 +2295,7 @@ static int vcr2_init_sequence(AVCodecContext *avctx)
s->progressive_frame = 1;
s->picture_structure = PICT_FRAME;
s->frame_pred_frame_dct = 1;
s->mpeg2 = 1;
s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
avctx->sub_id = 2; /* indicates mpeg2 */
return 0;
}
......
This diff is collapsed.
......@@ -48,8 +48,13 @@ enum OutputFormat {
#define MAX_PICTURE_COUNT 15
#if 1
#define ME_MAP_SIZE 4096
#define ME_MAP_SHIFT 6
#else
#define ME_MAP_SIZE 64
#define ME_MAP_SHIFT 3
#endif
#define ME_MAP_MV_BITS 11
/* run length table */
......@@ -385,8 +390,10 @@ typedef struct MpegEncContext {
int me_method; ///< ME algorithm
int scene_change_score;
int mv_dir;
#define MV_DIR_BACKWARD 1
#define MV_DIR_FORWARD 2
#define MV_DIR_L1 1
#define MV_DIR_L0 2
#define MV_DIR_BACKWARD MV_DIR_L1
#define MV_DIR_FORWARD MV_DIR_L0
#define MV_DIRECT 4 ///< bidirectional mode where the difference equals the MV of the last P/S/I-Frame (mpeg4)
int mv_type;
#define MV_TYPE_16X16 0 ///< 1 vector for the whole mb
......@@ -568,6 +575,11 @@ typedef struct MpegEncContext {
uint8_t *tex_pb_buffer;
uint8_t *pb2_buffer;
int mpeg_quant;
#define FF_QUANT_DEFAULT 0
#define FF_QUANT_MPEG2 1
#define FF_QUANT_MPEG1 2
#define FF_QUANT_H263 3
int16_t (*field_mv_table)[2][2]; ///< used for interlaced b frame decoding
int8_t (*field_select_table)[2]; ///< wtf, no really another table for interlaced b frames
int t_frame; ///< time distance of first I -> B, used for interlaced b frames
......@@ -644,7 +656,6 @@ typedef struct MpegEncContext {
int repeat_first_field;
int chroma_420_type;
int progressive_frame;
int mpeg2;
int full_pel[2];
int interlaced_dct;
int first_slice;
......@@ -652,7 +663,7 @@ typedef struct MpegEncContext {
/* RTP specific */
/* These are explained on avcodec.h */
int rtp_mode;
int rtp_mode; //FIXME simplify this mess (remove callback, merge mode + payload)
int rtp_payload_size;
void (*rtp_callback)(void *data, int size, int packet_number);
uint8_t *ptr_lastgob;
......@@ -870,6 +881,7 @@ int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size);
int ff_msmpeg4_decode_init(MpegEncContext *s);
void ff_msmpeg4_encode_init(MpegEncContext *s);
int ff_wmv2_decode_picture_header(MpegEncContext * s);
int ff_wmv2_decode_secondary_picture_header(MpegEncContext * s);
void ff_wmv2_add_mb(MpegEncContext *s, DCTELEM block[6][64], uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr);
void ff_mspel_motion(MpegEncContext *s,
uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment