]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/rv34.c
aacdec: reset max_sfb on invalid data.
[ffmpeg] / libavcodec / rv34.c
index e09d5dcf14ab0efbcac1bb440d0fe774480e7e8c..687bbfe21d38444c83dba357bd6b169fe14cb14e 100644 (file)
@@ -131,10 +131,10 @@ static void rv34_gen_vlc(const uint8_t *bits, int size, VLC *vlc, const uint8_t
 
     vlc->table = &table_data[table_offs[num]];
     vlc->table_allocated = table_offs[num + 1] - table_offs[num];
-    init_vlc_sparse(vlc, FFMIN(maxbits, 9), realsize,
-                    bits2, 1, 1,
-                    cw,    2, 2,
-                    syms,  2, 2, INIT_VLC_USE_NEW_STATIC);
+    ff_init_vlc_sparse(vlc, FFMIN(maxbits, 9), realsize,
+                       bits2, 1, 1,
+                       cw,    2, 2,
+                       syms,  2, 2, INIT_VLC_USE_NEW_STATIC);
 }
 
 /**
@@ -472,26 +472,26 @@ static void rv34_pred_mv(RV34DecContext *r, int block_type, int subblock_no, int
     int A[2] = {0}, B[2], C[2];
     int i, j;
     int mx, my;
-    int avail_index = avail_indexes[subblock_no];
+    int* avail = r->avail_cache + avail_indexes[subblock_no];
     int c_off = part_sizes_w[block_type];
 
     mv_pos += (subblock_no & 1) + (subblock_no >> 1)*s->b8_stride;
     if(subblock_no == 3)
         c_off = -1;
 
-    if(r->avail_cache[avail_index - 1]){
+    if(avail[-1]){
         A[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-1][0];
         A[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-1][1];
     }
-    if(r->avail_cache[avail_index - 4]){
+    if(avail[-4]){
         B[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride][0];
         B[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride][1];
     }else{
         B[0] = A[0];
         B[1] = A[1];
     }
-    if(!r->avail_cache[avail_index - 4 + c_off]){
-        if(r->avail_cache[avail_index - 4] && (r->avail_cache[avail_index - 1] || r->rv30)){
+    if(!avail[c_off-4]){
+        if(avail[-4] && (avail[-1] || r->rv30)){
             C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride-1][0];
             C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride-1][1];
         }else{
@@ -611,21 +611,21 @@ static void rv34_pred_mv_rv3(RV34DecContext *r, int block_type, int dir)
     int A[2] = {0}, B[2], C[2];
     int i, j, k;
     int mx, my;
-    int avail_index = avail_indexes[0];
+    int* avail = r->avail_cache + avail_indexes[0];
 
-    if(r->avail_cache[avail_index - 1]){
+    if(avail[-1]){
         A[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - 1][0];
         A[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - 1][1];
     }
-    if(r->avail_cache[avail_index - 4]){
+    if(avail[-4]){
         B[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride][0];
         B[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride][1];
     }else{
         B[0] = A[0];
         B[1] = A[1];
     }
-    if(!r->avail_cache[avail_index - 4 + 2]){
-        if(r->avail_cache[avail_index - 4] && (r->avail_cache[avail_index - 1])){
+    if(!avail[-4 + 2]){
+        if(avail[-4] && (avail[-1])){
             C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride - 1][0];
             C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride - 1][1];
         }else{
@@ -711,8 +711,7 @@ static inline void rv34_mc(RV34DecContext *r, const int block_type,
 
     if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
         /* wait for the referenced mb row to be finished */
-        int mb_row = FFMIN(s->mb_height - 1,
-                           s->mb_y + ((yoff + my + 5 + 8 * height) >> 4));
+        int mb_row = s->mb_y + ((yoff + my + 5 + 8 * height) >> 4);
         AVFrame *f = dir ? &s->next_picture_ptr->f : &s->last_picture_ptr->f;
         ff_thread_await_progress(f, mb_row, 0);
     }
@@ -1023,25 +1022,10 @@ static void rv34_output_i16x16(RV34DecContext *r, int8_t *intra_types, int cbp)
                     q_ac = rv34_qscale_tab[s->qscale];
     uint8_t        *dst  = s->dest[0];
     DCTELEM        *ptr  = s->block[0];
-    int       avail[6*8] = {0};
     int i, j, itype, has_ac;
 
     memset(block16, 0, 16 * sizeof(*block16));
 
-    // Set neighbour information.
-    if(r->avail_cache[1])
-        avail[0] = 1;
-    if(r->avail_cache[2])
-        avail[1] = avail[2] = 1;
-    if(r->avail_cache[3])
-        avail[3] = avail[4] = 1;
-    if(r->avail_cache[4])
-        avail[5] = 1;
-    if(r->avail_cache[5])
-        avail[8] = avail[16] = 1;
-    if(r->avail_cache[9])
-        avail[24] = avail[32] = 1;
-
     has_ac = rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0, q_dc, q_dc, q_ac);
     if(has_ac)
         r->rdsp.rv34_inv_transform(block16);
@@ -1361,6 +1345,53 @@ static int check_slice_end(RV34DecContext *r, MpegEncContext *s)
     return 0;
 }
 
+
+static void rv34_decoder_free(RV34DecContext *r)
+{
+    av_freep(&r->intra_types_hist);
+    r->intra_types = NULL;
+    av_freep(&r->tmp_b_block_base);
+    av_freep(&r->mb_type);
+    av_freep(&r->cbp_luma);
+    av_freep(&r->cbp_chroma);
+    av_freep(&r->deblock_coefs);
+}
+
+
+static int rv34_decoder_alloc(RV34DecContext *r)
+{
+    r->intra_types_stride = r->s.mb_width * 4 + 4;
+
+    r->cbp_chroma       = av_malloc(r->s.mb_stride * r->s.mb_height *
+                                    sizeof(*r->cbp_chroma));
+    r->cbp_luma         = av_malloc(r->s.mb_stride * r->s.mb_height *
+                                    sizeof(*r->cbp_luma));
+    r->deblock_coefs    = av_malloc(r->s.mb_stride * r->s.mb_height *
+                                    sizeof(*r->deblock_coefs));
+    r->intra_types_hist = av_malloc(r->intra_types_stride * 4 * 2 *
+                                    sizeof(*r->intra_types_hist));
+    r->mb_type          = av_mallocz(r->s.mb_stride * r->s.mb_height *
+                                     sizeof(*r->mb_type));
+
+    if (!(r->cbp_chroma       && r->cbp_luma && r->deblock_coefs &&
+          r->intra_types_hist && r->mb_type)) {
+        rv34_decoder_free(r);
+        return AVERROR(ENOMEM);
+    }
+
+    r->intra_types = r->intra_types_hist + r->intra_types_stride * 4;
+
+    return 0;
+}
+
+
+static int rv34_decoder_realloc(RV34DecContext *r)
+{
+    rv34_decoder_free(r);
+    return rv34_decoder_alloc(r);
+}
+
+
 static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int buf_size)
 {
     MpegEncContext *s = &r->s;
@@ -1376,25 +1407,22 @@ static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int
     }
 
     if ((s->mb_x == 0 && s->mb_y == 0) || s->current_picture_ptr==NULL) {
-        if(s->width != r->si.width || s->height != r->si.height){
-            av_log(s->avctx, AV_LOG_DEBUG, "Changing dimensions to %dx%d\n", r->si.width,r->si.height);
-            MPV_common_end(s);
+        if (s->width != r->si.width || s->height != r->si.height) {
+            int err;
+
+            av_log(s->avctx, AV_LOG_WARNING, "Changing dimensions to %dx%d\n",
+                   r->si.width, r->si.height);
+            ff_MPV_common_end(s);
             s->width  = r->si.width;
             s->height = r->si.height;
             avcodec_set_dimensions(s->avctx, s->width, s->height);
-            if(MPV_common_init(s) < 0)
-                return -1;
-            r->intra_types_stride = s->mb_width*4 + 4;
-            r->intra_types_hist = av_realloc(r->intra_types_hist, r->intra_types_stride * 4 * 2 * sizeof(*r->intra_types_hist));
-            r->intra_types = r->intra_types_hist + r->intra_types_stride * 4;
-            r->mb_type = av_realloc(r->mb_type, r->s.mb_stride * r->s.mb_height * sizeof(*r->mb_type));
-            r->cbp_luma   = av_realloc(r->cbp_luma,   r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_luma));
-            r->cbp_chroma = av_realloc(r->cbp_chroma, r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_chroma));
-            r->deblock_coefs = av_realloc(r->deblock_coefs, r->s.mb_stride * r->s.mb_height * sizeof(*r->deblock_coefs));
-            av_freep(&r->tmp_b_block_base);
+            if ((err = ff_MPV_common_init(s)) < 0)
+                return err;
+            if ((err = rv34_decoder_realloc(r)) < 0)
+                return err;
         }
         s->pict_type = r->si.type ? r->si.type : AV_PICTURE_TYPE_I;
-        if(MPV_frame_start(s, s->avctx) < 0)
+        if(ff_MPV_frame_start(s, s->avctx) < 0)
             return -1;
         ff_er_frame_start(s);
         if (!r->tmp_b_block_base) {
@@ -1500,8 +1528,9 @@ av_cold int ff_rv34_decode_init(AVCodecContext *avctx)
 {
     RV34DecContext *r = avctx->priv_data;
     MpegEncContext *s = &r->s;
+    int ret;
 
-    MPV_decode_defaults(s);
+    ff_MPV_decode_defaults(s);
     s->avctx      = avctx;
     s->out_format = FMT_H263;
     s->codec_id   = avctx->codec_id;
@@ -1516,8 +1545,8 @@ av_cold int ff_rv34_decode_init(AVCodecContext *avctx)
     avctx->has_b_frames = 1;
     s->low_delay = 0;
 
-    if (MPV_common_init(s) < 0)
-        return -1;
+    if ((ret = ff_MPV_common_init(s)) < 0)
+        return ret;
 
     ff_h264_pred_init(&r->h, CODEC_ID_RV40, 8, 1);
 
@@ -1530,15 +1559,8 @@ av_cold int ff_rv34_decode_init(AVCodecContext *avctx)
         ff_rv40dsp_init(&r->rdsp, &r->s.dsp);
 #endif
 
-    r->intra_types_stride = 4*s->mb_stride + 4;
-    r->intra_types_hist = av_malloc(r->intra_types_stride * 4 * 2 * sizeof(*r->intra_types_hist));
-    r->intra_types = r->intra_types_hist + r->intra_types_stride * 4;
-
-    r->mb_type = av_mallocz(r->s.mb_stride * r->s.mb_height * sizeof(*r->mb_type));
-
-    r->cbp_luma   = av_malloc(r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_luma));
-    r->cbp_chroma = av_malloc(r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_chroma));
-    r->deblock_coefs = av_malloc(r->s.mb_stride * r->s.mb_height * sizeof(*r->deblock_coefs));
+    if ((ret = rv34_decoder_alloc(r)) < 0)
+        return ret;
 
     if(!intra_vlcs[0].cbppattern[0].bits)
         rv34_init_tables();
@@ -1548,40 +1570,17 @@ av_cold int ff_rv34_decode_init(AVCodecContext *avctx)
 
 int ff_rv34_decode_init_thread_copy(AVCodecContext *avctx)
 {
+    int err;
     RV34DecContext *r = avctx->priv_data;
 
     r->s.avctx = avctx;
 
     if (avctx->internal->is_copy) {
-        r->cbp_chroma       = av_malloc(r->s.mb_stride * r->s.mb_height *
-                                        sizeof(*r->cbp_chroma));
-        r->cbp_luma         = av_malloc(r->s.mb_stride * r->s.mb_height *
-                                        sizeof(*r->cbp_luma));
-        r->deblock_coefs    = av_malloc(r->s.mb_stride * r->s.mb_height *
-                                        sizeof(*r->deblock_coefs));
-        r->intra_types_hist = av_malloc(r->intra_types_stride * 4 * 2 *
-                                        sizeof(*r->intra_types_hist));
-        r->mb_type          = av_malloc(r->s.mb_stride * r->s.mb_height *
-                                        sizeof(*r->mb_type));
-
-        if (!(r->cbp_chroma       && r->cbp_luma && r->deblock_coefs &&
-              r->intra_types_hist && r->mb_type)) {
-            av_freep(&r->cbp_chroma);
-            av_freep(&r->cbp_luma);
-            av_freep(&r->deblock_coefs);
-            av_freep(&r->intra_types_hist);
-            av_freep(&r->mb_type);
-            r->intra_types = NULL;
-            return AVERROR(ENOMEM);
-        }
-
-        r->intra_types      = r->intra_types_hist + r->intra_types_stride * 4;
         r->tmp_b_block_base = NULL;
-
-        memset(r->mb_type, 0,  r->s.mb_stride * r->s.mb_height *
-               sizeof(*r->mb_type));
-
-        MPV_common_init(&r->s);
+        if ((err = ff_MPV_common_init(&r->s)) < 0)
+            return err;
+        if ((err = rv34_decoder_alloc(r)) < 0)
+            return err;
     }
     return 0;
 }
@@ -1595,6 +1594,16 @@ int ff_rv34_decode_update_thread_context(AVCodecContext *dst, const AVCodecConte
     if (dst == src || !s1->context_initialized)
         return 0;
 
+    if (s->height != s1->height || s->width != s1->width) {
+        ff_MPV_common_end(s);
+        s->height = s1->height;
+        s->width  = s1->width;
+        if ((err = ff_MPV_common_init(s)) < 0)
+            return err;
+        if ((err = rv34_decoder_realloc(r)) < 0)
+            return err;
+    }
+
     if ((err = ff_mpeg_update_thread_context(dst, src)))
         return err;
 
@@ -1605,7 +1614,7 @@ int ff_rv34_decode_update_thread_context(AVCodecContext *dst, const AVCodecConte
     memset(&r->si, 0, sizeof(r->si));
 
     /* necessary since it is it the condition checked for in decode_slice
-     * to call MPV_frame_start. cmp. comment at the end of decode_frame */
+     * to call ff_MPV_frame_start. cmp. comment at the end of decode_frame */
     s->current_picture_ptr = NULL;
 
     return 0;
@@ -1636,7 +1645,7 @@ int ff_rv34_decode_frame(AVCodecContext *avctx,
     if (buf_size == 0) {
         /* special case for last picture */
         if (s->low_delay==0 && s->next_picture_ptr) {
-            *pict = *(AVFrame*)s->next_picture_ptr;
+            *pict = s->next_picture_ptr->f;
             s->next_picture_ptr = NULL;
 
             *data_size = sizeof(AVFrame);
@@ -1656,15 +1665,19 @@ int ff_rv34_decode_frame(AVCodecContext *avctx,
     if(get_slice_offset(avctx, slices_hdr, 0) < 0 ||
        get_slice_offset(avctx, slices_hdr, 0) > buf_size){
         av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, 0), (buf_size-get_slice_offset(avctx, slices_hdr, 0))*8);
     if(r->parse_slice_header(r, &r->s.gb, &si) < 0 || si.start){
         av_log(avctx, AV_LOG_ERROR, "First slice header is incorrect\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
+    }
+    if ((!s->last_picture_ptr || !s->last_picture_ptr->f.data[0]) &&
+        si.type == AV_PICTURE_TYPE_B) {
+        av_log(avctx, AV_LOG_ERROR, "Invalid decoder state: B-frame without "
+               "reference data.\n");
+        return AVERROR_INVALIDDATA;
     }
-    if ((!s->last_picture_ptr || !s->last_picture_ptr->f.data[0]) && si.type == AV_PICTURE_TYPE_B)
-        return -1;
     if(   (avctx->skip_frame >= AVDISCARD_NONREF && si.type==AV_PICTURE_TYPE_B)
        || (avctx->skip_frame >= AVDISCARD_NONKEY && si.type!=AV_PICTURE_TYPE_I)
        ||  avctx->skip_frame >= AVDISCARD_ALL)
@@ -1712,15 +1725,16 @@ int ff_rv34_decode_frame(AVCodecContext *avctx,
     if(last && s->current_picture_ptr){
         if(r->loop_filter)
             r->loop_filter(r, s->mb_height - 1);
-        if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
-            ff_thread_report_progress(&s->current_picture_ptr->f,
-                                      s->mb_height - 1, 0);
         ff_er_frame_end(s);
-        MPV_frame_end(s);
+        ff_MPV_frame_end(s);
+
+        if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
+            ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);
+
         if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
-            *pict = *(AVFrame*)s->current_picture_ptr;
+            *pict = s->current_picture_ptr->f;
         } else if (s->last_picture_ptr != NULL) {
-            *pict = *(AVFrame*)s->last_picture_ptr;
+            *pict = s->last_picture_ptr->f;
         }
 
         if(s->last_picture_ptr || s->low_delay){
@@ -1736,15 +1750,8 @@ av_cold int ff_rv34_decode_end(AVCodecContext *avctx)
 {
     RV34DecContext *r = avctx->priv_data;
 
-    MPV_common_end(&r->s);
-
-    av_freep(&r->intra_types_hist);
-    r->intra_types = NULL;
-    av_freep(&r->tmp_b_block_base);
-    av_freep(&r->mb_type);
-    av_freep(&r->cbp_luma);
-    av_freep(&r->cbp_chroma);
-    av_freep(&r->deblock_coefs);
+    ff_MPV_common_end(&r->s);
+    rv34_decoder_free(r);
 
     return 0;
 }