]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/rv10.c
PPC fixes & clean-up patch by (Romain Dolbeau <dolbeau at irisa dot fr>)
[ffmpeg] / libavcodec / rv10.c
index 7562cbb2fe1fff7ca8001776d24d1a2778c3e622..4b351ea1eabf87e5f9bd557445e48381478869ac 100644 (file)
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
+
+/**
+ * @file rv10.c
+ * RV10 codec.
+ */
 #include "avcodec.h"
 #include "dsputil.h"
 #include "mpegvideo.h"
 
 //#define DEBUG
 
-#define DC_VLC_BITS 9
+#define DC_VLC_BITS 14 //FIXME find a better solution
 
-static const UINT16 rv_lum_code[256] =
+static const uint16_t rv_lum_code[256] =
 {
  0x3e7f, 0x0f00, 0x0f01, 0x0f02, 0x0f03, 0x0f04, 0x0f05, 0x0f06,
  0x0f07, 0x0f08, 0x0f09, 0x0f0a, 0x0f0b, 0x0f0c, 0x0f0d, 0x0f0e,
@@ -60,7 +66,7 @@ static const UINT16 rv_lum_code[256] =
  0x0f78, 0x0f79, 0x0f7a, 0x0f7b, 0x0f7c, 0x0f7d, 0x0f7e, 0x0f7f,
 };
 
-static const UINT8 rv_lum_bits[256] = 
+static const uint8_t rv_lum_bits[256] = 
 {
  14, 12, 12, 12, 12, 12, 12, 12,
  12, 12, 12, 12, 12, 12, 12, 12,
@@ -96,7 +102,7 @@ static const UINT8 rv_lum_bits[256] =
  12, 12, 12, 12, 12, 12, 12, 12,
 };
 
-static const UINT16 rv_chrom_code[256] =
+static const uint16_t rv_chrom_code[256] =
 {
  0xfe7f, 0x3f00, 0x3f01, 0x3f02, 0x3f03, 0x3f04, 0x3f05, 0x3f06,
  0x3f07, 0x3f08, 0x3f09, 0x3f0a, 0x3f0b, 0x3f0c, 0x3f0d, 0x3f0e,
@@ -132,7 +138,7 @@ static const UINT16 rv_chrom_code[256] =
  0x3f78, 0x3f79, 0x3f7a, 0x3f7b, 0x3f7c, 0x3f7d, 0x3f7e, 0x3f7f,
 };
 
-static const UINT8 rv_chrom_bits[256] =
+static const uint8_t rv_chrom_bits[256] =
 {
  16, 14, 14, 14, 14, 14, 14, 14,
  14, 14, 14, 14, 14, 14, 14, 14,
@@ -182,14 +188,14 @@ int rv_decode_dc(MpegEncContext *s, int n)
                if they had thought about it !!! */
             code = get_bits(&s->gb, 7);
             if (code == 0x7c) {
-                code = (INT8)(get_bits(&s->gb, 7) + 1);
+                code = (int8_t)(get_bits(&s->gb, 7) + 1);
             } else if (code == 0x7d) {
                 code = -128 + get_bits(&s->gb, 7);
             } else if (code == 0x7e) {
                 if (get_bits(&s->gb, 1) == 0)
-                    code = (INT8)(get_bits(&s->gb, 8) + 1);
+                    code = (int8_t)(get_bits(&s->gb, 8) + 1);
                 else
-                    code = (INT8)(get_bits(&s->gb, 8));
+                    code = (int8_t)(get_bits(&s->gb, 8));
             } else if (code == 0x7f) {
                 get_bits(&s->gb, 11);
                 code = 1;
@@ -203,13 +209,14 @@ int rv_decode_dc(MpegEncContext *s, int n)
         if (code < 0) {
             code = get_bits(&s->gb, 9);
             if (code == 0x1fc) {
-                code = (INT8)(get_bits(&s->gb, 7) + 1);
+                code = (int8_t)(get_bits(&s->gb, 7) + 1);
             } else if (code == 0x1fd) {
                 code = -128 + get_bits(&s->gb, 7);
             } else if (code == 0x1fe) {
                 get_bits(&s->gb, 9);
                 code = 1;
             } else {
+                fprintf(stderr, "chroma dc error\n");
                 return 0xffff;
             }
         } else {
@@ -219,11 +226,15 @@ int rv_decode_dc(MpegEncContext *s, int n)
     return -code;
 }
 
+#ifdef CONFIG_ENCODERS
+
 /* write RV 1.0 compatible frame header */
 void rv10_encode_picture_header(MpegEncContext *s, int picture_number)
 {
-    align_put_bits(&s->pb);
+    int full_frame= 0;
 
+    align_put_bits(&s->pb);
+    
     put_bits(&s->pb, 1, 1);    /* marker */
 
     put_bits(&s->pb, 1, (s->pict_type == P_TYPE));
@@ -237,9 +248,11 @@ void rv10_encode_picture_header(MpegEncContext *s, int picture_number)
     }
     /* if multiple packets per frame are sent, the position at which
        to display the macro blocks is coded here */
-    put_bits(&s->pb, 6, 0);    /* mb_x */
-    put_bits(&s->pb, 6, 0);    /* mb_y */
-    put_bits(&s->pb, 12, s->mb_width * s->mb_height);
+    if(!full_frame){
+        put_bits(&s->pb, 6, 0);        /* mb_x */
+        put_bits(&s->pb, 6, 0);        /* mb_y */
+        put_bits(&s->pb, 12, s->mb_width * s->mb_height);
+    }
 
     put_bits(&s->pb, 3, 0);    /* ignored */
 }
@@ -257,43 +270,33 @@ static int get_num(GetBitContext *gb)
     }
 }
 
+#endif //CONFIG_ENCODERS
+
 /* read RV 1.0 compatible frame header */
 static int rv10_decode_picture_header(MpegEncContext *s)
 {
-    int mb_count, pb_frame, marker, h, full_frame;
+    int mb_count, pb_frame, marker, full_frame, unk;
     
-    /* skip packet header */
-    h = get_bits(&s->gb, 8);
-    if ((h & 0xc0) == 0xc0) {
-        int len, pos;
-        full_frame = 1;
-        len = get_num(&s->gb);
-        pos = get_num(&s->gb);
-    } else {
-        int seq, frame_size, pos;
-        full_frame = 0;
-        seq = get_bits(&s->gb, 8);
-        frame_size = get_num(&s->gb);
-        pos = get_num(&s->gb);
-    }
-    /* picture number */
-    get_bits(&s->gb, 8);
-
+    full_frame= s->avctx->slice_count==1;
+//printf("ff:%d\n", full_frame);
     marker = get_bits(&s->gb, 1);
 
     if (get_bits(&s->gb, 1))
         s->pict_type = P_TYPE;
     else
         s->pict_type = I_TYPE;
-
+//printf("h:%X ver:%d\n",h,s->rv10_version);
+    if(!marker) printf("marker missing\n");
     pb_frame = get_bits(&s->gb, 1);
 
 #ifdef DEBUG
     printf("pict_type=%d pb_frame=%d\n", s->pict_type, pb_frame);
 #endif
     
-    if (pb_frame)
+    if (pb_frame){
+        fprintf(stderr, "pb frame not supported\n");
         return -1;
+    }
 
     s->qscale = get_bits(&s->gb, 5);
     if(s->qscale==0){
@@ -317,7 +320,7 @@ 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) {
+    if ((!full_frame) || show_bits(&s->gb, 12)==0) {
         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);
@@ -326,30 +329,43 @@ static int rv10_decode_picture_header(MpegEncContext *s)
         s->mb_y = 0;
         mb_count = s->mb_width * s->mb_height;
     }
-//printf("%d\n", get_bits(&s->gb, 3));
-    get_bits(&s->gb, 3);       /* ignored */
+    unk= get_bits(&s->gb, 3);  /* ignored */
+//printf("%d\n", unk);
     s->f_code = 1;
     s->unrestricted_mv = 1;
-#if 0
-    s->h263_long_vectors = 1;
-#endif
+
     return mb_count;
 }
 
 static int rv10_decode_init(AVCodecContext *avctx)
 {
     MpegEncContext *s = avctx->priv_data;
-    static int done;
+    static int done=0;
 
-//    s->avctx= avctx;
+    s->avctx= avctx;
     s->out_format = FMT_H263;
 
     s->width = avctx->width;
     s->height = avctx->height;
 
     s->h263_rv10 = 1;
-    s->rv10_version = avctx->sub_id;
-    
+    switch(avctx->sub_id){
+    case 0x10000000:
+        s->rv10_version= 0;
+        s->h263_long_vectors=0;
+        break;
+    case 0x10003000:
+        s->rv10_version= 3;
+        s->h263_long_vectors=1;
+        break;
+    case 0x10003001:
+        s->rv10_version= 3;
+        s->h263_long_vectors=0;
+        break;
+    default:
+        fprintf(stderr, "unknown header %X\n", avctx->sub_id);
+    }
+//printf("ver:%X\n", avctx->sub_id);
     s->flags= avctx->flags;
 
     if (MPV_common_init(s) < 0)
@@ -359,6 +375,7 @@ static int rv10_decode_init(AVCodecContext *avctx)
 
     s->y_dc_scale_table=
     s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
+    s->progressive_sequence=1;
 
     /* init rv vlc */
     if (!done) {
@@ -370,6 +387,8 @@ static int rv10_decode_init(AVCodecContext *avctx)
                  rv_chrom_code, 2, 2);
         done = 1;
     }
+    
+    avctx->pix_fmt = PIX_FMT_YUV420P;
 
     return 0;
 }
@@ -382,53 +401,35 @@ static int rv10_decode_end(AVCodecContext *avctx)
     return 0;
 }
 
-static int rv10_decode_frame(AVCodecContext *avctx, 
-                             void *data, int *data_size,
-                             UINT8 *buf, int buf_size)
+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;
-    DCTELEM block[6][64];
-    AVPicture *pict = data; 
-
-#ifdef DEBUG
-    printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
-#endif
-
-    /* no supplementary picture */
-    if (buf_size == 0) {
-        *data_size = 0;
-        return 0;
-    }
-
-    init_get_bits(&s->gb, buf, buf_size);
 
+    init_get_bits(&s->gb, buf, buf_size*8);
+    
     mb_count = rv10_decode_picture_header(s);
     if (mb_count < 0) {
-#ifdef DEBUG
-        printf("HEADER ERROR\n");
-#endif
+        fprintf(stderr, "HEADER ERROR\n");
         return -1;
     }
     
     if (s->mb_x >= s->mb_width ||
         s->mb_y >= s->mb_height) {
-#ifdef DEBUG
-        printf("POS ERROR %d %d\n", s->mb_x, s->mb_y);
-#endif
+        fprintf(stderr, "POS ERROR %d %d\n", s->mb_x, s->mb_y);
         return -1;
     }
     mb_pos = s->mb_y * s->mb_width + s->mb_x;
     left = s->mb_width * s->mb_height - mb_pos;
     if (mb_count > left) {
-#ifdef DEBUG
-        printf("COUNT ERROR\n");
-#endif
+        fprintf(stderr, "COUNT ERROR\n");
         return -1;
     }
 
     if (s->mb_x == 0 && s->mb_y == 0) {
-        MPV_frame_start(s, avctx);
+        if(MPV_frame_start(s, avctx) < 0)
+            return -1;
     }
 
 #ifdef DEBUG
@@ -442,68 +443,87 @@ static int rv10_decode_frame(AVCodecContext *avctx,
     s->rv10_first_dc_coded[1] = 0;
     s->rv10_first_dc_coded[2] = 0;
 
+    if(s->mb_y==0) s->first_slice_line=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[4]=
     s->block_wrap[5]= s->mb_width + 2;
-    s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
-    s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1)     + s->mb_x*2;
-    s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1 + s->mb_x*2;
-    s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2)     + s->mb_x*2;
-    s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
-    s->block_index[5]= s->block_wrap[4]*(s->mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
+    ff_init_block_index(s);
     /* decode each macroblock */
     for(i=0;i<mb_count;i++) {
-        s->block_index[0]+=2;
-        s->block_index[1]+=2;
-        s->block_index[2]+=2;
-        s->block_index[3]+=2;
-        s->block_index[4]++;
-        s->block_index[5]++;
+        ff_update_block_index(s);
 #ifdef DEBUG
         printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
 #endif
         
-        memset(block, 0, sizeof(block));
+       s->dsp.clear_blocks(s->block[0]);
         s->mv_dir = MV_DIR_FORWARD;
         s->mv_type = MV_TYPE_16X16; 
-        if (h263_decode_mb(s, block) < 0) {
-#ifdef DEBUG
-            printf("ERROR\n");
-#endif
+        if (ff_h263_decode_mb(s, s->block) == SLICE_ERROR) {
+            fprintf(stderr, "ERROR at MB %d %d\n", s->mb_x, s->mb_y);
             return -1;
         }
-        MPV_decode_mb(s, block);
+        MPV_decode_mb(s, s->block);
         if (++s->mb_x == s->mb_width) {
             s->mb_x = 0;
             s->mb_y++;
-            s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
-            s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
-            s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
-            s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2);
-            s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2);
-            s->block_index[5]= s->block_wrap[4]*(s->mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
+            ff_init_block_index(s);
+            s->first_slice_line=0;
+        }
+    }
+
+    return buf_size;
+}
+
+static int rv10_decode_frame(AVCodecContext *avctx, 
+                             void *data, int *data_size,
+                             uint8_t *buf, int buf_size)
+{
+    MpegEncContext *s = avctx->priv_data;
+    int i;
+    AVFrame *pict = data; 
+
+#ifdef DEBUG
+    printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
+#endif
+
+    /* 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];
+            int size;
+            
+            if(i+1 == avctx->slice_count)
+                size= buf_size - offset;
+            else
+                size= avctx->slice_offset[i+1] - offset;
+
+            if( rv10_decode_packet(avctx, buf+offset, size) < 0 )
+                return -1;
         }
+    }else{
+        if( rv10_decode_packet(avctx, buf, buf_size) < 0 )
+            return -1;
     }
 
-    if (s->mb_x == 0 &&
-        s->mb_y == s->mb_height) {
+    if(s->mb_y>=s->mb_height){
         MPV_frame_end(s);
         
-        pict->data[0] = s->current_picture[0];
-        pict->data[1] = s->current_picture[1];
-        pict->data[2] = s->current_picture[2];
-        pict->linesize[0] = s->linesize;
-        pict->linesize[1] = s->uvlinesize;
-        pict->linesize[2] = s->uvlinesize;
-        
-        avctx->quality = s->qscale;
-        *data_size = sizeof(AVPicture);
-    } else {
+        *pict= *(AVFrame*)&s->current_picture;
+    
+        *data_size = sizeof(AVFrame);
+    }else{
         *data_size = 0;
     }
+
     return buf_size;
 }