Ministry of English Composition, reporting for duty (and the word is "skipped", not...
[ffmpeg.git] / libavcodec / rv10.c
index 42eacbd..23106bc 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * RV10 codec
  * Copyright (c) 2000,2001 Fabrice Bellard.
+ * Copyright (c) 2002-2004 Michael Niedermayer
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -257,6 +258,36 @@ void rv10_encode_picture_header(MpegEncContext *s, int picture_number)
     put_bits(&s->pb, 3, 0);    /* ignored */
 }
 
+void rv20_encode_picture_header(MpegEncContext *s, int picture_number){
+    put_bits(&s->pb, 2, s->pict_type); //I 0 vs. 1 ?
+    put_bits(&s->pb, 1, 0);    /* unknown bit */
+    put_bits(&s->pb, 5, s->qscale);
+        
+    put_bits(&s->pb, 8, picture_number&0xFF); //FIXME wrong, but correct is not known
+    s->mb_x= s->mb_y= 0;
+    ff_h263_encode_mba(s);
+    
+    put_bits(&s->pb, 1, s->no_rounding);
+    
+    assert(s->f_code == 1);
+    assert(s->unrestricted_mv == 1);
+//    assert(s->h263_aic== (s->pict_type == I_TYPE));
+    assert(s->alt_inter_vlc == 0);
+    assert(s->umvplus == 0);
+    assert(s->modified_quant==1);
+    assert(s->loop_filter==1);
+
+    s->h263_aic= s->pict_type == I_TYPE;
+    if(s->h263_aic){
+        s->y_dc_scale_table= 
+        s->c_dc_scale_table= ff_aic_dc_scale_table;
+    }else{
+        s->y_dc_scale_table=
+        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
+    }
+}
+
+#if 0 /* unused, remove? */
 static int get_num(GetBitContext *gb)
 {
     int n, n1;
@@ -269,15 +300,15 @@ static int get_num(GetBitContext *gb)
         return (n << 16) | n1;
     }
 }
+#endif
 
 #endif //CONFIG_ENCODERS
 
 /* read RV 1.0 compatible frame header */
 static int rv10_decode_picture_header(MpegEncContext *s)
 {
-    int mb_count, pb_frame, marker, full_frame, unk;
+    int mb_count, pb_frame, marker, unk, mb_xy;
     
-    full_frame= s->avctx->slice_count==1;
 //printf("ff:%d\n", full_frame);
     marker = get_bits(&s->gb, 1);
 
@@ -320,7 +351,9 @@ static int rv10_decode_picture_header(MpegEncContext *s)
     }
     /* if multiple packets per frame are sent, the position at which
        to display the macro blocks is coded here */
-    if ((!full_frame) || show_bits(&s->gb, 12)==0) {
+
+    mb_xy= s->mb_x + s->mb_y*s->mb_width;
+    if(show_bits(&s->gb, 12)==0 || (mb_xy && mb_xy < s->mb_num)){
         s->mb_x = get_bits(&s->gb, 6); /* mb_x */
         s->mb_y = get_bits(&s->gb, 6); /* mb_y */
         mb_count = get_bits(&s->gb, 12);
@@ -341,6 +374,22 @@ static int rv20_decode_picture_header(MpegEncContext *s)
 {
     int seq, mb_pos, i;
     
+#if 0
+    GetBitContext gb= s->gb;
+    for(i=0; i<64; i++){
+        av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&gb));
+        if(i%4==3) av_log(s->avctx, AV_LOG_DEBUG, " ");
+    }
+    av_log(s->avctx, AV_LOG_DEBUG, "\n");
+#endif
+#if 0
+    for(i=0; i<s->avctx->extradata_size; i++){
+        av_log(s->avctx, AV_LOG_DEBUG, "%2X ", ((uint8_t*)s->avctx->extradata)[i]);
+        if(i%4==3) av_log(s->avctx, AV_LOG_DEBUG, " ");
+    }
+    av_log(s->avctx, AV_LOG_DEBUG, "\n");
+#endif
+    
     if(s->avctx->sub_id == 0x30202002 || s->avctx->sub_id == 0x30203002){
         if (get_bits(&s->gb, 3)){
             av_log(s->avctx, AV_LOG_ERROR, "unknown triplet set\n");
@@ -359,6 +408,11 @@ static int rv20_decode_picture_header(MpegEncContext *s)
         return -1;
     }
     
+    if(s->last_picture_ptr==NULL && s->pict_type==B_TYPE){
+        av_log(s->avctx, AV_LOG_ERROR, "early B pix\n");
+        return -1;
+    }
+    
     if (get_bits(&s->gb, 1)){
         av_log(s->avctx, AV_LOG_ERROR, "unknown bit set\n");
         return -1;
@@ -377,14 +431,31 @@ static int rv20_decode_picture_header(MpegEncContext *s)
     }
         
     if(s->avctx->has_b_frames){
+        int f=9;
+        int v= s->avctx->extradata_size >= 4 ? ((uint8_t*)s->avctx->extradata)[1] : 0;
+
         if (get_bits(&s->gb, 1)){
             av_log(s->avctx, AV_LOG_ERROR, "unknown bit3 set\n");
-            return -1;
+//            return -1;
+        }
+        seq= get_bits(&s->gb, 14)<<1;
+
+        if(v>1 || (s->avctx->sub_id < 0x20201002 && v>0)){
+            f= get_bits(&s->gb, av_log2(v-1)+1);
         }
-        seq= get_bits(&s->gb, 15);
-    }else
+        
+        if(s->avctx->debug & FF_DEBUG_PICT_INFO){
+            av_log(s->avctx, AV_LOG_DEBUG, "F %d/%d\n", f, v);
+        }
+
+        mb_pos= get_bits(&s->gb, av_log2(s->mb_num-1)+1);
+        s->mb_x= mb_pos % s->mb_width;
+        s->mb_y= mb_pos / s->mb_width;
+    }else{
         seq= get_bits(&s->gb, 8)*128;
-//printf("%d\n", seq);
+        mb_pos= ff_h263_decode_mba(s);
+    }
+//av_log(s->avctx, AV_LOG_DEBUG, "%d\n", seq);
     seq |= s->time &~0x7FFF;
     if(seq - s->time >  0x4000) seq -= 0x8000;
     if(seq - s->time < -0x4000) seq += 0x8000;
@@ -397,14 +468,16 @@ static int rv20_decode_picture_header(MpegEncContext *s)
             s->time= seq;
             s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
             if(s->pp_time <=s->pb_time || s->pp_time <= s->pp_time - s->pb_time || s->pp_time<=0){
-                printf("messed up order, seeking?, skiping current b frame\n");
-                return FRAME_SKIPED;
+                av_log(s->avctx, AV_LOG_DEBUG, "messed up order, possible from seeking? skipping current b frame\n");
+                return FRAME_SKIPPED;
             }
         }
     }
 //    printf("%d %d %d %d %d\n", seq, (int)s->time, (int)s->last_non_b_time, s->pp_time, s->pb_time);
-
-    ff_h263_decode_mba(s);    
+/*for(i=0; i<32; i++){
+    av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
+}
+av_log(s->avctx, AV_LOG_DEBUG, "\n");*/
     s->no_rounding= get_bits1(&s->gb);
     
     s->f_code = 1;
@@ -431,6 +504,8 @@ static int rv10_decode_init(AVCodecContext *avctx)
     MpegEncContext *s = avctx->priv_data;
     static int done=0;
 
+    MPV_decode_defaults(s);
+    
     s->avctx= avctx;
     s->out_format = FMT_H263;
     s->codec_id= avctx->codec_id;
@@ -444,6 +519,12 @@ static int rv10_decode_init(AVCodecContext *avctx)
         s->h263_long_vectors=0;
         s->low_delay=1;
         break;
+    case 0x10002000:
+        s->rv10_version= 3;
+        s->h263_long_vectors=1;
+        s->low_delay=1;
+        s->obmc=1;
+        break;
     case 0x10003000:
         s->rv10_version= 3;
         s->h263_long_vectors=1;
@@ -457,9 +538,12 @@ static int rv10_decode_init(AVCodecContext *avctx)
     case 0x20001000:
     case 0x20100001:
     case 0x20101001:
+    case 0x20103001:
         s->low_delay=1;
         break;
     case 0x20200002:
+    case 0x20201002:
+    case 0x20203002:
     case 0x30202002:
     case 0x30203002:
         s->low_delay=0;
@@ -468,24 +552,24 @@ static int rv10_decode_init(AVCodecContext *avctx)
     default:
         av_log(s->avctx, AV_LOG_ERROR, "unknown header %X\n", avctx->sub_id);
     }
-//printf("ver:%X\n", avctx->sub_id);
-    s->flags= avctx->flags;
-
+    
+    if(avctx->debug & FF_DEBUG_PICT_INFO){
+        av_log(avctx, AV_LOG_DEBUG, "ver:%X ver0:%X\n", avctx->sub_id, avctx->extradata_size >= 4 ? ((uint32_t*)avctx->extradata)[0] : -1);
+    }
+    
     if (MPV_common_init(s) < 0)
         return -1;
 
     h263_decode_init_vlc(s);
 
-    s->progressive_sequence=1;
-
     /* init rv vlc */
     if (!done) {
         init_vlc(&rv_dc_lum, DC_VLC_BITS, 256, 
                  rv_lum_bits, 1, 1,
-                 rv_lum_code, 2, 2);
+                 rv_lum_code, 2, 2, 1);
         init_vlc(&rv_dc_chrom, DC_VLC_BITS, 256, 
                  rv_chrom_bits, 1, 1,
-                 rv_chrom_code, 2, 2);
+                 rv_chrom_code, 2, 2, 1);
         done = 1;
     }
     
@@ -506,15 +590,9 @@ static int rv10_decode_packet(AVCodecContext *avctx,
                              uint8_t *buf, int buf_size)
 {
     MpegEncContext *s = avctx->priv_data;
-    int i, mb_count, mb_pos, left;
+    int mb_count, mb_pos, left;
 
     init_get_bits(&s->gb, buf, buf_size*8);
-#if 0
-    for(i=0; i<buf_size*8 && i<200; i++)
-        printf("%d", get_bits1(&s->gb));
-    printf("\n");
-    return 0;
-#endif
     if(s->codec_id ==CODEC_ID_RV10)
         mb_count = rv10_decode_picture_header(s);
     else
@@ -537,7 +615,7 @@ static int rv10_decode_packet(AVCodecContext *avctx,
     }
 //if(s->pict_type == P_TYPE) return 0;
 
-    if (s->mb_x == 0 && s->mb_y == 0) {
+    if ((s->mb_x == 0 && s->mb_y == 0) || s->current_picture_ptr==NULL) {
         if(MPV_frame_start(s, avctx) < 0)
             return -1;
     }
@@ -570,22 +648,23 @@ static int rv10_decode_packet(AVCodecContext *avctx,
     s->rv10_first_dc_coded[0] = 0;
     s->rv10_first_dc_coded[1] = 0;
     s->rv10_first_dc_coded[2] = 0;
-
+//printf("%d %X %X\n", s->pict_type, s->current_picture.motion_val[0], s->current_picture.motion_val[1]);
     s->block_wrap[0]=
     s->block_wrap[1]=
     s->block_wrap[2]=
-    s->block_wrap[3]= s->mb_width*2 + 2;
+    s->block_wrap[3]= s->b8_stride;
     s->block_wrap[4]=
-    s->block_wrap[5]= s->mb_width + 2;
+    s->block_wrap[5]= s->mb_stride;
     ff_init_block_index(s);
     /* decode each macroblock */
-    for(i=0;i<mb_count;i++) {
+
+    for(s->mb_num_left= mb_count; s->mb_num_left>0; s->mb_num_left--) {
         int ret;
         ff_update_block_index(s);
 #ifdef DEBUG
         printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
 #endif
-        
+
        s->dsp.clear_blocks(s->block[0]);
         s->mv_dir = MV_DIR_FORWARD;
         s->mv_type = MV_TYPE_16X16; 
@@ -595,7 +674,8 @@ static int rv10_decode_packet(AVCodecContext *avctx,
             av_log(s->avctx, AV_LOG_ERROR, "ERROR at MB %d %d\n", s->mb_x, s->mb_y);
             return -1;
         }
-        ff_h263_update_motion_val(s);
+        if(s->pict_type != B_TYPE)
+            ff_h263_update_motion_val(s);
         MPV_decode_mb(s, s->block);
         if(s->loop_filter)
             ff_h263_loop_filter(s);
@@ -627,10 +707,9 @@ static int rv10_decode_frame(AVCodecContext *avctx,
 
     /* no supplementary picture */
     if (buf_size == 0) {
-        *data_size = 0;
         return 0;
     }
-    
+
     if(avctx->slice_count){
         for(i=0; i<avctx->slice_count; i++){
             int offset= avctx->slice_offset[i];
@@ -648,21 +727,19 @@ static int rv10_decode_frame(AVCodecContext *avctx,
         if( rv10_decode_packet(avctx, buf, buf_size) < 0 )
             return -1;
     }
-
+    
     if(s->mb_y>=s->mb_height){
         MPV_frame_end(s);
     
         if(s->pict_type==B_TYPE || s->low_delay){
             *pict= *(AVFrame*)&s->current_picture;
-            ff_print_debug_info(s, s->current_picture_ptr);
+            ff_print_debug_info(s, pict);
         } else {
             *pict= *(AVFrame*)&s->last_picture;
-            ff_print_debug_info(s, s->last_picture_ptr);
+            ff_print_debug_info(s, pict);
         }
-        
-        *data_size = sizeof(AVFrame);
-    }else{
-        *data_size = 0;
+        if(s->last_picture_ptr || s->low_delay)
+            *data_size = sizeof(AVFrame);
     }
 
     return buf_size;
@@ -689,6 +766,7 @@ AVCodec rv20_decoder = {
     NULL,
     rv10_decode_end,
     rv10_decode_frame,
-    CODEC_CAP_DR1
+    CODEC_CAP_DR1 | CODEC_CAP_DELAY,
+    .flush= ff_mpeg_flush,
 };