]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mpeg12.c
10l
[ffmpeg] / libavcodec / mpeg12.c
index d16eaa1da6cca98227b043ccfa2b4fc5e5d18b9c..c68f8d8a05708f587a04f69a6474220ed2ad5029 100644 (file)
@@ -852,16 +852,13 @@ static inline int get_dmv(MpegEncContext *s)
 
 static inline int get_qscale(MpegEncContext *s)
 {
-    int qscale;
+    int qscale = get_bits(&s->gb, 5);
     if (s->mpeg2) {
         if (s->q_scale_type) {
-            qscale = non_linear_qscale[get_bits(&s->gb, 5)];
+            return non_linear_qscale[qscale];
         } else {
-            qscale = get_bits(&s->gb, 5) << 1;
+            return qscale << 1;
         }
-    } else {
-        /* for mpeg1, we use the generic unquant code */
-        qscale = get_bits(&s->gb, 5);
     }
     return qscale;
 }
@@ -1179,7 +1176,7 @@ static int mpeg_decode_mb(MpegEncContext *s,
 /* as h263, but only 17 codes */
 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
 {
-    int code, sign, val, m, l, shift;
+    int code, sign, val, l, shift;
 
     code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
     if (code == 0) {
@@ -1191,22 +1188,19 @@ static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
 
     sign = get_bits1(&s->gb);
     shift = fcode - 1;
-    val = (code - 1) << shift;
-    if (shift > 0)
+    val = code;
+    if (shift) {
+        val = (val - 1) << shift;
         val |= get_bits(&s->gb, shift);
-    val++;
+        val++;
+    }
     if (sign)
         val = -val;
     val += pred;
     
     /* modulo decoding */
     l = 1 << (shift+4);
-    m = 2 * l;
-    if (val < -l) {
-        val += m;
-    } else if (val >= l) {
-        val -= m;
-    }
+    val = ((val + l)&(l*2-1)) - l;
     return val;
 }
 
@@ -1226,9 +1220,7 @@ static inline int decode_dc(MpegEncContext *s, int component)
     if (code == 0) {
         diff = 0;
     } else {
-        diff = get_bits(&s->gb, code);
-        if ((diff & (1 << (code - 1))) == 0) 
-            diff = (-1 << code) | (diff + 1);
+        diff = get_xbits(&s->gb, code);
     }
     return diff;
 }
@@ -1729,15 +1721,15 @@ static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
     }
     
     if(s->alternate_scan){
-        ff_init_scantable(s, &s->inter_scantable  , ff_alternate_vertical_scan);
-        ff_init_scantable(s, &s->intra_scantable  , ff_alternate_vertical_scan);
-        ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_vertical_scan);
-        ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
+        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_alternate_vertical_scan);
+        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_alternate_vertical_scan);
+        ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
+        ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
     }else{
-        ff_init_scantable(s, &s->inter_scantable  , ff_zigzag_direct);
-        ff_init_scantable(s, &s->intra_scantable  , ff_zigzag_direct);
-        ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
-        ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
+        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_zigzag_direct);
+        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_zigzag_direct);
+        ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
+        ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
     }
     
     /* composite display not parsed */
@@ -1778,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,
@@ -1801,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);
@@ -1864,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 */
@@ -1878,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) {
@@ -1909,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)*/ {
@@ -1940,6 +1934,17 @@ static int mpeg_decode_slice(AVCodecContext *avctx,
 
             s->mb_x = 0;
             s->mb_y++;
+
+            if(s->mb_y<<field_pic >= s->mb_height){
+                int left= s->gb.size_in_bits - get_bits_count(&s->gb);
+
+                if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)))
+                   || (avctx->error_resilience >= FF_ER_AGGRESSIVE && left>8)){
+                    fprintf(stderr, "end missmatch left=%d\n", left);
+                    return -1;
+                }else
+                    goto eos;
+            }
         }
 
         /* skip mb handling */
@@ -1969,22 +1974,27 @@ 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;
-        }
     }
-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;
+}
+
+/**
+ * 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;
+       
+    if (!s1->mpeg_enc_ctx_allocated)
+        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);
     /* 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){
@@ -2008,9 +2018,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;
     }
 }
 
@@ -2190,7 +2200,7 @@ static int mpeg1_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
         }
     }        
     pc->state= state;
-    return -1;
+    return END_NOT_FOUND;
 }
 
 /* handle buffering and image synchronisation */
@@ -2218,9 +2228,7 @@ static int mpeg_decode_frame(AVCodecContext *avctx,
     }
 
     if(s2->flags&CODEC_FLAG_TRUNCATED){
-        int next;
-        
-        next= mpeg1_find_frame_end(s2, buf, buf_size);
+        int next= mpeg1_find_frame_end(s2, buf, buf_size);
         
         if( ff_combine_frame(s2, next, &buf, &buf_size) < 0 )
             return buf_size;
@@ -2243,13 +2251,21 @@ static int mpeg_decode_frame(AVCodecContext *avctx,
     for(;;) {
         /* find start next code */
         start_code = find_start_code(&buf_ptr, buf_end);
-        if (start_code < 0){ 
-            printf("missing end of picture\n");
-            return FFMAX(1, buf_ptr - buf - s2->parse_context.last_index);
+        if (start_code < 0){
+            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);
+        }
+        
+        input_size = buf_end - buf_ptr;
+
+        if(avctx->debug & FF_DEBUG_STARTCODE){
+            printf("%3X at %d left %d\n", start_code, buf_ptr-buf, input_size);
         }
 
                 /* prepare data for next start code */
-                input_size = buf_end - buf_ptr;
                 switch(start_code) {
                 case SEQ_START_CODE:
                     mpeg1_decode_sequence(avctx, buf_ptr, 
@@ -2284,17 +2300,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(1, 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;