Commit db6e7795 authored by Michael Niedermayer's avatar Michael Niedermayer

motion vector vissualization for mpeg1/2

mpeg1/2 cleanup/bugfix
error resilience bugfix

Originally committed as revision 1911 to svn://svn.ffmpeg.org/ffmpeg/trunk
parent e82c5a8c
......@@ -733,7 +733,7 @@ void ff_er_frame_end(MpegEncContext *s){
for(i=s->mb_num-2; i>=s->mb_width+100; i--){ //FIXME +100 hack
const int mb_xy= s->mb_index2xy[i];
int error1= s->error_status_table[mb_xy ];
int error2= s->error_status_table[mb_xy+1];
int error2= s->error_status_table[s->mb_index2xy[i+1]];
if(error1&VP_START)
end_ok=1;
......
......@@ -377,75 +377,6 @@ static int h263_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
return END_NOT_FOUND;
}
/**
* draws an line from (ex, ey) -> (sx, sy).
* @param w width of the image
* @param h height of the image
* @param stride stride/linesize of the image
* @param color color of the arrow
*/
static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
int t, x, y, f;
ex= clip(ex, 0, w-1);
ey= clip(ey, 0, h-1);
buf[sy*stride + sx]+= color;
if(ABS(ex - sx) > ABS(ey - sy)){
if(sx > ex){
t=sx; sx=ex; ex=t;
t=sy; sy=ey; ey=t;
}
buf+= sx + sy*stride;
ex-= sx;
f= ((ey-sy)<<16)/ex;
for(x= 0; x <= ex; x++){
y= ((x*f) + (1<<15))>>16;
buf[y*stride + x]+= color;
}
}else{
if(sy > ey){
t=sx; sx=ex; ex=t;
t=sy; sy=ey; ey=t;
}
buf+= sx + sy*stride;
ey-= sy;
if(ey) f= ((ex-sx)<<16)/ey;
else f= 0;
for(y= 0; y <= ey; y++){
x= ((y*f) + (1<<15))>>16;
buf[y*stride + x]+= color;
}
}
}
/**
* draws an arrow from (ex, ey) -> (sx, sy).
* @param w width of the image
* @param h height of the image
* @param stride stride/linesize of the image
* @param color color of the arrow
*/
static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
int dx= ex - sx;
int dy= ey - sy;
if(dx*dx + dy*dy > 3*3){
int rx= dx + dy;
int ry= -dx + dy;
int length= ff_sqrt((rx*rx + ry*ry)<<8);
//FIXME subpixel accuracy
rx= ROUNDED_DIV(rx*3<<4, length);
ry= ROUNDED_DIV(ry*3<<4, length);
draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
}
draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
}
int ff_h263_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
uint8_t *buf, int buf_size)
......@@ -740,39 +671,8 @@ retry:
MPV_frame_end(s);
if((avctx->debug&FF_DEBUG_VIS_MV) && s->last_picture_ptr){
const int shift= 1 + s->quarter_sample;
int mb_y;
uint8_t *ptr= s->last_picture.data[0];
s->low_delay=0; //needed to see the vectors without trashing the buffers
for(mb_y=0; mb_y<s->mb_height; mb_y++){
int mb_x;
for(mb_x=0; mb_x<s->mb_width; mb_x++){
const int mb_index= mb_x + mb_y*s->mb_stride;
if(IS_8X8(s->current_picture.mb_type[mb_index])){
int i;
for(i=0; i<4; i++){
int sx= mb_x*16 + 4 + 8*(i&1);
int sy= mb_y*16 + 4 + 8*(i>>1);
int xy= 1 + mb_x*2 + (i&1) + (mb_y*2 + 1 + (i>>1))*(s->mb_width*2 + 2);
int mx= (s->motion_val[xy][0]>>shift) + sx;
int my= (s->motion_val[xy][1]>>shift) + sy;
draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
}
}else{
int sx= mb_x*16 + 8;
int sy= mb_y*16 + 8;
int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
int mx= (s->motion_val[xy][0]>>shift) + sx;
int my= (s->motion_val[xy][1]>>shift) + sy;
draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
}
s->mbskip_table[mb_index]=0;
}
}
}
assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
assert(s->current_picture.pict_type == s->pict_type);
if(s->pict_type==B_TYPE || s->low_delay){
*pict= *(AVFrame*)&s->current_picture;
ff_print_debug_info(s, s->current_picture_ptr);
......
......@@ -1770,18 +1770,15 @@ static void mpeg_decode_extension(AVCodecContext *avctx,
}
}
#define DECODE_SLICE_MB_ADDR_ERROR -3 //we faild decoding the mb_x/y info
#define DECODE_SLICE_FATAL_ERROR -2
#define DECODE_SLICE_ERROR -1
#define DECODE_SLICE_OK 0
#define DECODE_SLICE_EOP 1
/**
* decodes a slice.
* @return DECODE_SLICE_FATAL_ERROR if a non recoverable error occured<br>
* DECODE_SLICE_ERROR if the slice is damaged<br>
* DECODE_SLICE_OK if this slice is ok<br>
* DECODE_SLICE_EOP if the end of the picture is reached
*/
static int mpeg_decode_slice(AVCodecContext *avctx,
AVFrame *pict,
......@@ -1793,10 +1790,13 @@ static int mpeg_decode_slice(AVCodecContext *avctx,
int ret;
const int field_pic= s->picture_structure != PICT_FRAME;
s->resync_mb_x= s->mb_x =
s->resync_mb_y= s->mb_y = -1;
start_code = (start_code - 1) & 0xff;
if (start_code >= s->mb_height){
fprintf(stderr, "slice below image (%d >= %d)\n", start_code, s->mb_height);
return DECODE_SLICE_MB_ADDR_ERROR;
return -1;
}
ff_mpeg1_clean_buffers(s);
......@@ -1856,7 +1856,7 @@ static int mpeg_decode_slice(AVCodecContext *avctx,
s->qscale = get_qscale(s);
if(s->qscale == 0){
fprintf(stderr, "qscale == 0\n");
return DECODE_SLICE_MB_ADDR_ERROR;
return -1;
}
/* extra slice info */
......@@ -1870,7 +1870,7 @@ static int mpeg_decode_slice(AVCodecContext *avctx,
int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
if (code < 0){
fprintf(stderr, "first mb_incr damaged\n");
return DECODE_SLICE_MB_ADDR_ERROR;
return -1;
}
if (code >= 33) {
if (code == 33) {
......@@ -1901,7 +1901,9 @@ static int mpeg_decode_slice(AVCodecContext *avctx,
const int xy = s->mb_x*2 + 1 + (s->mb_y*2 +1)*wrap;
int motion_x, motion_y;
if (s->mb_intra || s->mv_type == MV_TYPE_16X16) {
if (s->mb_intra) {
motion_x = motion_y = 0;
}else if (s->mv_type == MV_TYPE_16X16) {
motion_x = s->mv[0][0][0];
motion_y = s->mv[0][0][1];
} else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
......@@ -1963,20 +1965,26 @@ static int mpeg_decode_slice(AVCodecContext *avctx,
}
if(s->mb_y<<field_pic >= s->mb_height){
fprintf(stderr, "slice too long\n");
return DECODE_SLICE_ERROR;
return -1;
}
}
eos: //end of slice
eos: // end of slice
*buf += get_bits_count(&s->gb)/8 - 1;
//printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
return 0;
}
emms_c();
*buf += get_bits_count(&s->gb)/8 - 1;
//intf("%d %d %d %d\n", s->mb_y, s->mb_height, s->pict_type, s->picture_number);
/**
* handles slice ends.
* @return 1 if it seems to be the last slice of
*/
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
{
Mpeg1Context *s1 = avctx->priv_data;
MpegEncContext *s = &s1->mpeg_enc_ctx;
/* end of slice reached */
if (s->mb_y<<field_pic == s->mb_height && !s->first_field) {
if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
/* end of image */
if(s->mpeg2){
......@@ -2000,9 +2008,9 @@ eos: //end of slice
ff_print_debug_info(s, s->last_picture_ptr);
}
}
return DECODE_SLICE_EOP;
return 1;
} else {
return DECODE_SLICE_OK;
return 0;
}
}
......@@ -2234,7 +2242,10 @@ static int mpeg_decode_frame(AVCodecContext *avctx,
/* find start next code */
start_code = find_start_code(&buf_ptr, buf_end);
if (start_code < 0){
// printf("missing end of picture\n");
if (slice_end(avctx, picture)) {
if(s2->last_picture_ptr) //FIXME merge with the stuff in mpeg_decode_slice
*data_size = sizeof(AVPicture);
}
return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
}
......@@ -2274,17 +2285,14 @@ static int mpeg_decode_frame(AVCodecContext *avctx,
ret = mpeg_decode_slice(avctx, picture,
start_code, &buf_ptr, input_size);
emms_c();
if (ret == DECODE_SLICE_EOP) {
if(s2->last_picture_ptr) //FIXME merge with the stuff in mpeg_decode_slice
*data_size = sizeof(AVPicture);
return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
}else if(ret < 0){
if(ret == DECODE_SLICE_ERROR)
if(ret < 0){
if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
fprintf(stderr,"Error while decoding slice\n");
if(ret==DECODE_SLICE_FATAL_ERROR) return -1;
if(ret==DECODE_SLICE_FATAL_ERROR) return -1;
}else{
ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, AC_END|DC_END|MV_END);
}
}
break;
......
......@@ -1063,6 +1063,84 @@ void MPV_frame_end(MpegEncContext *s)
#endif
}
/**
* draws an line from (ex, ey) -> (sx, sy).
* @param w width of the image
* @param h height of the image
* @param stride stride/linesize of the image
* @param color color of the arrow
*/
static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
int t, x, y, f;
sx= clip(sx, 0, w-1);
sy= clip(sy, 0, h-1);
ex= clip(ex, 0, w-1);
ey= clip(ey, 0, h-1);
buf[sy*stride + sx]+= color;
if(ABS(ex - sx) > ABS(ey - sy)){
if(sx > ex){
t=sx; sx=ex; ex=t;
t=sy; sy=ey; ey=t;
}
buf+= sx + sy*stride;
ex-= sx;
f= ((ey-sy)<<16)/ex;
for(x= 0; x <= ex; x++){
y= ((x*f) + (1<<15))>>16;
buf[y*stride + x]+= color;
}
}else{
if(sy > ey){
t=sx; sx=ex; ex=t;
t=sy; sy=ey; ey=t;
}
buf+= sx + sy*stride;
ey-= sy;
if(ey) f= ((ex-sx)<<16)/ey;
else f= 0;
for(y= 0; y <= ey; y++){
x= ((y*f) + (1<<15))>>16;
buf[y*stride + x]+= color;
}
}
}
/**
* draws an arrow from (ex, ey) -> (sx, sy).
* @param w width of the image
* @param h height of the image
* @param stride stride/linesize of the image
* @param color color of the arrow
*/
static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
int dx,dy;
sx= clip(sx, -100, w+100);
sy= clip(sy, -100, h+100);
ex= clip(ex, -100, w+100);
ey= clip(ey, -100, h+100);
dx= ex - sx;
dy= ey - sy;
if(dx*dx + dy*dy > 3*3){
int rx= dx + dy;
int ry= -dx + dy;
int length= ff_sqrt((rx*rx + ry*ry)<<8);
//FIXME subpixel accuracy
rx= ROUNDED_DIV(rx*3<<4, length);
ry= ROUNDED_DIV(ry*3<<4, length);
draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
}
draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
}
/**
* prints debuging info for the given picture.
*/
......@@ -1137,6 +1215,39 @@ void ff_print_debug_info(MpegEncContext *s, Picture *pict){
printf("\n");
}
}
if((s->avctx->debug&FF_DEBUG_VIS_MV) && s->motion_val){
const int shift= 1 + s->quarter_sample;
int mb_y;
uint8_t *ptr= pict->data[0];
s->low_delay=0; //needed to see the vectors without trashing the buffers
for(mb_y=0; mb_y<s->mb_height; mb_y++){
int mb_x;
for(mb_x=0; mb_x<s->mb_width; mb_x++){
const int mb_index= mb_x + mb_y*s->mb_stride;
if(IS_8X8(s->current_picture.mb_type[mb_index])){
int i;
for(i=0; i<4; i++){
int sx= mb_x*16 + 4 + 8*(i&1);
int sy= mb_y*16 + 4 + 8*(i>>1);
int xy= 1 + mb_x*2 + (i&1) + (mb_y*2 + 1 + (i>>1))*(s->mb_width*2 + 2);
int mx= (s->motion_val[xy][0]>>shift) + sx;
int my= (s->motion_val[xy][1]>>shift) + sy;
draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
}
}else{
int sx= mb_x*16 + 8;
int sy= mb_y*16 + 8;
int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
int mx= (s->motion_val[xy][0]>>shift) + sx;
int my= (s->motion_val[xy][1]>>shift) + sy;
draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
}
s->mbskip_table[mb_index]=0;
}
}
}
}
#ifdef CONFIG_ENCODERS
......
......@@ -33,8 +33,8 @@ stddev: 10.53 bytes:7145472
b54262af56f6681186fa2c44e4ef6ec7 *./data/out.yuv
stddev: 7.32 bytes:7602176
a38cb11e3035a280f3dad3ccdff5997b *./data/a-mpeg1b.mpg
da8e21c7b78b7a25558dc319524b91d8 *./data/out.yuv
stddev: 6.32 bytes:6842368
bd047a4567c366566da96874d176199d *./data/out.yuv
stddev: 6.35 bytes:6993920
07042f42b3119c39eb26537a84a451df *./data/a-mjpeg.avi
f23a9e50a559e174766ee808c48fea22 *./data/out.yuv
stddev: 8.87 bytes:7602176
......
......@@ -33,8 +33,8 @@ aeb300900461bdf9e0321934aa6dcc93 *./data/a-mpeg4-adv.avi
e465df12f2c8a6492a6ec0b1bddc1d0d *./data/out.yuv
stddev: 4.97 bytes:7602176
2ec827a941ee8e82911280405987f8ef *./data/a-mpeg1b.mpg
145c3741d70bc40d5c93de709aca70ee *./data/out.yuv
stddev: 4.12 bytes:6842368
2935a090a88c74ccb220c18c1399cea7 *./data/out.yuv
stddev: 4.69 bytes:6993920
e9218a1db885fe0262e88f9df630307d *./data/a-mjpeg.avi
a365b4da246ad68caf96b702b7f961a1 *./data/out.yuv
stddev: 4.76 bytes:7602176
......
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