Merge remote-tracking branch 'newdev/master'
authorMichael Niedermayer <michaelni@gmx.at>
Sun, 27 Mar 2011 00:40:18 +0000 (01:40 +0100)
committerMichael Niedermayer <michaelni@gmx.at>
Sun, 27 Mar 2011 00:40:18 +0000 (01:40 +0100)
* newdev/master:
  dsputil: allow to skip drawing of top/bottom edges.
  Split fate-psx-str-v3 into a video-only and audio-only test.

Conflicts:
libavcodec/dsputil.c
libavcodec/mpegvideo.c
libavcodec/snow.c
libavcodec/x86/dsputil_mmx.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2 
libavcodec/dsputil.c
libavcodec/mpegvideo.c
libavcodec/x86/dsputil_mmx.c

diff --combined libavcodec/dsputil.c
@@@ -5,20 -5,20 +5,20 @@@
   *
   * gmc & q-pel & 32/64 bit based MC by Michael Niedermayer <michaelni@gmx.at>
   *
 - * This file is part of Libav.
 + * This file is part of FFmpeg.
   *
 - * Libav is free software; you can redistribute it and/or
 + * FFmpeg is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
 - * Libav is distributed in the hope that it will be useful,
 + * FFmpeg is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
 - * License along with Libav; if not, write to the Free Software
 + * License along with FFmpeg; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
   */
  
@@@ -303,12 -303,6 +303,6 @@@ static void draw_edges_c(uint8_t *buf, 
      uint8_t *ptr, *last_line;
      int i;
  
-     last_line = buf + (height - 1) * wrap;
-     for(i=0;i<w;i++) {
-         /* top and bottom */
-         if (sides&EDGE_TOP)    memcpy(buf - (i + 1) * wrap, buf, width);
-         if (sides&EDGE_BOTTOM) memcpy(last_line + (i + 1) * wrap, last_line, width);
-     }
      /* left and right */
      ptr = buf;
      for(i=0;i<height;i++) {
          memset(ptr + width, ptr[width-1], w);
          ptr += wrap;
      }
-     /* corners */
-     for(i=0;i<w;i++) {
-         if (sides&EDGE_TOP) {
-             memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
-             memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
-         }
  
-         if (sides&EDGE_BOTTOM) {
-             memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
-             memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
-         }
-     }
+     /* top and bottom + corners */
+     buf -= w;
+     last_line = buf + (height - 1) * wrap;
+     if (sides & EDGE_TOP)
+         for(i = 0; i < w; i++)
+             memcpy(buf - (i + 1) * wrap, buf, width + w + w); // top
+     if (sides & EDGE_BOTTOM)
+         for (i = 0; i < w; i++)
+             memcpy(last_line + (i + 1) * wrap, last_line, width + w + w); // bottom
  }
  
  /**
diff --combined libavcodec/mpegvideo.c
@@@ -5,20 -5,20 +5,20 @@@
   *
   * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
   *
 - * This file is part of Libav.
 + * This file is part of FFmpeg.
   *
 - * Libav is free software; you can redistribute it and/or
 + * FFmpeg is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
 - * Libav is distributed in the hope that it will be useful,
 + * FFmpeg is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
 - * License along with Libav; if not, write to the Free Software
 + * License along with FFmpeg; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
   */
  
@@@ -38,7 -38,6 +38,7 @@@
  #include "msmpeg4.h"
  #include "faandct.h"
  #include "xvmc_internal.h"
 +#include "thread.h"
  #include <limits.h>
  
  //#undef NDEBUG
@@@ -206,7 -205,7 +206,7 @@@ void ff_copy_picture(Picture *dst, Pict
   */
  static void free_frame_buffer(MpegEncContext *s, Picture *pic)
  {
 -    s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
 +    ff_thread_release_buffer(s->avctx, (AVFrame*)pic);
      av_freep(&pic->hwaccel_picture_private);
  }
  
@@@ -228,7 -227,7 +228,7 @@@ static int alloc_frame_buffer(MpegEncCo
          }
      }
  
 -    r = s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
 +    r = ff_thread_get_buffer(s->avctx, (AVFrame*)pic);
  
      if (r<0 || !pic->age || !pic->type || !pic->data[0]) {
          av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
@@@ -459,81 -458,6 +459,81 @@@ void ff_update_duplicate_context(MpegEn
  //STOP_TIMER("update_duplicate_context") //about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  }
  
 +int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
 +{
 +    MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
 +
 +    if(dst == src || !s1->context_initialized) return 0;
 +
 +    //FIXME can parameters change on I-frames? in that case dst may need a reinit
 +    if(!s->context_initialized){
 +        memcpy(s, s1, sizeof(MpegEncContext));
 +
 +        s->avctx                 = dst;
 +        s->picture_range_start  += MAX_PICTURE_COUNT;
 +        s->picture_range_end    += MAX_PICTURE_COUNT;
 +        s->bitstream_buffer      = NULL;
 +        s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
 +
 +        MPV_common_init(s);
 +    }
 +
 +    s->avctx->coded_height  = s1->avctx->coded_height;
 +    s->avctx->coded_width   = s1->avctx->coded_width;
 +    s->avctx->width         = s1->avctx->width;
 +    s->avctx->height        = s1->avctx->height;
 +
 +    s->coded_picture_number = s1->coded_picture_number;
 +    s->picture_number       = s1->picture_number;
 +    s->input_picture_number = s1->input_picture_number;
 +
 +    memcpy(s->picture, s1->picture, s1->picture_count * sizeof(Picture));
 +    memcpy(&s->last_picture, &s1->last_picture, (char*)&s1->last_picture_ptr - (char*)&s1->last_picture);
 +
 +    s->last_picture_ptr     = REBASE_PICTURE(s1->last_picture_ptr,    s, s1);
 +    s->current_picture_ptr  = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
 +    s->next_picture_ptr     = REBASE_PICTURE(s1->next_picture_ptr,    s, s1);
 +
 +    memcpy(s->prev_pict_types, s1->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
 +
 +    //Error/bug resilience
 +    s->next_p_frame_damaged = s1->next_p_frame_damaged;
 +    s->workaround_bugs      = s1->workaround_bugs;
 +
 +    //MPEG4 timing info
 +    memcpy(&s->time_increment_bits, &s1->time_increment_bits, (char*)&s1->shape - (char*)&s1->time_increment_bits);
 +
 +    //B-frame info
 +    s->max_b_frames         = s1->max_b_frames;
 +    s->low_delay            = s1->low_delay;
 +    s->dropable             = s1->dropable;
 +
 +    //DivX handling (doesn't work)
 +    s->divx_packed          = s1->divx_packed;
 +
 +    if(s1->bitstream_buffer){
 +        if (s1->bitstream_buffer_size + FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
 +            av_fast_malloc(&s->bitstream_buffer, &s->allocated_bitstream_buffer_size, s1->allocated_bitstream_buffer_size);
 +        s->bitstream_buffer_size  = s1->bitstream_buffer_size;
 +        memcpy(s->bitstream_buffer, s1->bitstream_buffer, s1->bitstream_buffer_size);
 +        memset(s->bitstream_buffer+s->bitstream_buffer_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
 +    }
 +
 +    //MPEG2/interlacing info
 +    memcpy(&s->progressive_sequence, &s1->progressive_sequence, (char*)&s1->rtp_mode - (char*)&s1->progressive_sequence);
 +
 +    if(!s1->first_field){
 +        s->last_pict_type= s1->pict_type;
 +        if (s1->current_picture_ptr) s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->quality;
 +
 +        if(s1->pict_type!=FF_B_TYPE){
 +            s->last_non_b_pict_type= s1->pict_type;
 +        }
 +    }
 +
 +    return 0;
 +}
 +
  /**
   * sets the given MpegEncContext to common defaults (same for encoding and decoding).
   * the changed fields will not depend upon the prior state of the MpegEncContext.
@@@ -554,9 -478,6 +554,9 @@@ void MPV_common_defaults(MpegEncContex
  
      s->f_code = 1;
      s->b_code = 1;
 +
 +    s->picture_range_start = 0;
 +    s->picture_range_end = MAX_PICTURE_COUNT;
  }
  
  /**
@@@ -585,8 -506,7 +585,8 @@@ av_cold int MPV_common_init(MpegEncCont
          return -1;
      }
  
 -    if(s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height)){
 +    if(s->avctx->active_thread_type&FF_THREAD_SLICE &&
 +       (s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height))){
          av_log(s->avctx, AV_LOG_ERROR, "too many threads\n");
          return -1;
      }
              FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail)
          }
      }
 -    FF_ALLOCZ_OR_GOTO(s->avctx, s->picture, MAX_PICTURE_COUNT * sizeof(Picture), fail)
 -    for(i = 0; i < MAX_PICTURE_COUNT; i++) {
 +    s->picture_count = MAX_PICTURE_COUNT * FFMAX(1, s->avctx->thread_count);
 +    FF_ALLOCZ_OR_GOTO(s->avctx, s->picture, s->picture_count * sizeof(Picture), fail)
 +    for(i = 0; i < s->picture_count; i++) {
          avcodec_get_frame_defaults((AVFrame *)&s->picture[i]);
      }
  
      }
  
      s->context_initialized = 1;
 -
      s->thread_context[0]= s;
 -    threads = s->avctx->thread_count;
  
 -    for(i=1; i<threads; i++){
 -        s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
 -        memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
 -    }
 +    if (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_SLICE) {
 +        threads = s->avctx->thread_count;
  
 -    for(i=0; i<threads; i++){
 -        if(init_duplicate_context(s->thread_context[i], s) < 0)
 -           goto fail;
 -        s->thread_context[i]->start_mb_y= (s->mb_height*(i  ) + s->avctx->thread_count/2) / s->avctx->thread_count;
 -        s->thread_context[i]->end_mb_y  = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
 +        for(i=1; i<threads; i++){
 +            s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
 +            memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
 +        }
 +
 +        for(i=0; i<threads; i++){
 +            if(init_duplicate_context(s->thread_context[i], s) < 0)
 +               goto fail;
 +            s->thread_context[i]->start_mb_y= (s->mb_height*(i  ) + s->avctx->thread_count/2) / s->avctx->thread_count;
 +            s->thread_context[i]->end_mb_y  = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
 +        }
 +    } else {
 +        if(init_duplicate_context(s, s) < 0) goto fail;
 +        s->start_mb_y = 0;
 +        s->end_mb_y   = s->mb_height;
      }
  
      return 0;
@@@ -774,14 -687,12 +774,14 @@@ void MPV_common_end(MpegEncContext *s
  {
      int i, j, k;
  
 -    for(i=0; i<s->avctx->thread_count; i++){
 -        free_duplicate_context(s->thread_context[i]);
 -    }
 -    for(i=1; i<s->avctx->thread_count; i++){
 -        av_freep(&s->thread_context[i]);
 -    }
 +    if (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_SLICE) {
 +        for(i=0; i<s->avctx->thread_count; i++){
 +            free_duplicate_context(s->thread_context[i]);
 +        }
 +        for(i=1; i<s->avctx->thread_count; i++){
 +            av_freep(&s->thread_context[i]);
 +        }
 +    } else free_duplicate_context(s);
  
      av_freep(&s->parse_context.buffer);
      s->parse_context.buffer_size=0;
      av_freep(&s->reordered_input_picture);
      av_freep(&s->dct_offset);
  
 -    if(s->picture){
 -        for(i=0; i<MAX_PICTURE_COUNT; i++){
 +    if(s->picture && !s->avctx->is_copy){
 +        for(i=0; i<s->picture_count; i++){
              free_picture(s, &s->picture[i]);
          }
      }
      for(i=0; i<3; i++)
          av_freep(&s->visualization_buffer[i]);
  
 -    avcodec_default_free_buffers(s->avctx);
 +    if(!(s->avctx->active_thread_type&FF_THREAD_FRAME))
 +        avcodec_default_free_buffers(s->avctx);
  }
  
  void init_rl(RLTable *rl, uint8_t static_store[2][2*MAX_RUN + MAX_LEVEL + 3])
@@@ -950,14 -860,14 +950,14 @@@ int ff_find_unused_picture(MpegEncConte
      int i;
  
      if(shared){
 -        for(i=0; i<MAX_PICTURE_COUNT; i++){
 +        for(i=s->picture_range_start; i<s->picture_range_end; i++){
              if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
          }
      }else{
 -        for(i=0; i<MAX_PICTURE_COUNT; i++){
 +        for(i=s->picture_range_start; i<s->picture_range_end; i++){
              if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
          }
 -        for(i=0; i<MAX_PICTURE_COUNT; i++){
 +        for(i=s->picture_range_start; i<s->picture_range_end; i++){
              if(s->picture[i].data[0]==NULL) return i;
          }
      }
@@@ -1014,7 -924,7 +1014,7 @@@ int MPV_frame_start(MpegEncContext *s, 
          /* release forgotten pictures */
          /* if(mpeg124/h263) */
          if(!s->encoding){
 -            for(i=0; i<MAX_PICTURE_COUNT; i++){
 +            for(i=0; i<s->picture_count; i++){
                  if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
                      av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
                      free_frame_buffer(s, &s->picture[i]);
  
      if(!s->encoding){
          /* release non reference frames */
 -        for(i=0; i<MAX_PICTURE_COUNT; i++){
 +        for(i=0; i<s->picture_count; i++){
              if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
                  free_frame_buffer(s, &s->picture[i]);
              }
                  s->current_picture_ptr->top_field_first= (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
          }
          s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;
 +        s->current_picture_ptr->field_picture= s->picture_structure != PICT_FRAME;
      }
  
      s->current_picture_ptr->pict_type= s->pict_type;
              s->last_picture_ptr= &s->picture[i];
              if(ff_alloc_picture(s, s->last_picture_ptr, 0) < 0)
                  return -1;
 +            ff_thread_report_progress((AVFrame*)s->last_picture_ptr, INT_MAX, 0);
 +            ff_thread_report_progress((AVFrame*)s->last_picture_ptr, INT_MAX, 1);
          }
          if((s->next_picture_ptr==NULL || s->next_picture_ptr->data[0]==NULL) && s->pict_type==FF_B_TYPE){
              /* Allocate a dummy frame */
              s->next_picture_ptr= &s->picture[i];
              if(ff_alloc_picture(s, s->next_picture_ptr, 0) < 0)
                  return -1;
 +            ff_thread_report_progress((AVFrame*)s->next_picture_ptr, INT_MAX, 0);
 +            ff_thread_report_progress((AVFrame*)s->next_picture_ptr, INT_MAX, 1);
          }
      }
  
  void MPV_frame_end(MpegEncContext *s)
  {
      int i;
 -    /* draw edge for correct motion prediction if outside */
 +    /* redraw edges for the frame if decoding didn't complete */
      //just to make sure that all data is rendered.
      if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
          ff_xvmc_field_end(s);
 -    }else if(!s->avctx->hwaccel
 +   }else if((s->error_count || s->encoding)
 +       && !s->avctx->hwaccel
         && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
         && s->unrestricted_mv
         && s->current_picture.reference
         && !s->intra_only
         && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
 -            s->dsp.draw_edges(s->current_picture.data[0], s->linesize  ,
 -                              s->h_edge_pos   , s->v_edge_pos   ,
 -                              EDGE_WIDTH  , EDGE_TOP | EDGE_BOTTOM);
 -            s->dsp.draw_edges(s->current_picture.data[1], s->uvlinesize,
 -                              s->h_edge_pos>>1, s->v_edge_pos>>1,
 -                              EDGE_WIDTH/2, EDGE_TOP | EDGE_BOTTOM);
 -            s->dsp.draw_edges(s->current_picture.data[2], s->uvlinesize,
 -                              s->h_edge_pos>>1, s->v_edge_pos>>1,
 -                              EDGE_WIDTH/2, EDGE_TOP | EDGE_BOTTOM);
 +        int edges = EDGE_BOTTOM | EDGE_TOP, h = s->v_edge_pos;
 +
-             s->dsp.draw_edges(s->current_picture_ptr->data[0], s->linesize  , s->h_edge_pos   , h   , EDGE_WIDTH  , edges);
-             s->dsp.draw_edges(s->current_picture_ptr->data[1], s->uvlinesize, s->h_edge_pos>>1, h>>1, EDGE_WIDTH/2, edges);
-             s->dsp.draw_edges(s->current_picture_ptr->data[2], s->uvlinesize, s->h_edge_pos>>1, h>>1, EDGE_WIDTH/2, edges);
++            s->dsp.draw_edges(s->current_picture.data[0], s->linesize  , s->h_edge_pos   , h   , EDGE_WIDTH  , edges);
++            s->dsp.draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, h>>1, EDGE_WIDTH/2, edges);
++            s->dsp.draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, h>>1, EDGE_WIDTH/2, edges);
 +
      }
 +
      emms_c();
  
      s->last_pict_type    = s->pict_type;
  
      if(s->encoding){
          /* release non-reference frames */
 -        for(i=0; i<MAX_PICTURE_COUNT; i++){
 +        for(i=0; i<s->picture_count; i++){
              if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
                  free_frame_buffer(s, &s->picture[i]);
              }
      memset(&s->current_picture, 0, sizeof(Picture));
  #endif
      s->avctx->coded_frame= (AVFrame*)s->current_picture_ptr;
 +
 +    if (s->codec_id != CODEC_ID_H264 && s->current_picture.reference) {
 +        ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_height-1, 0);
 +    }
  }
  
  /**
@@@ -1872,43 -1774,6 +1872,43 @@@ static inline void MPV_motion_lowres(Mp
      }
  }
  
 +/**
 + * find the lowest MB row referenced in the MVs
 + */
 +int MPV_lowest_referenced_row(MpegEncContext *s, int dir)
 +{
 +    int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
 +    int my, off, i, mvs;
 +
 +    if (s->picture_structure != PICT_FRAME) goto unhandled;
 +
 +    switch (s->mv_type) {
 +        case MV_TYPE_16X16:
 +            mvs = 1;
 +            break;
 +        case MV_TYPE_16X8:
 +            mvs = 2;
 +            break;
 +        case MV_TYPE_8X8:
 +            mvs = 4;
 +            break;
 +        default:
 +            goto unhandled;
 +    }
 +
 +    for (i = 0; i < mvs; i++) {
 +        my = s->mv[dir][i][1]<<qpel_shift;
 +        my_max = FFMAX(my_max, my);
 +        my_min = FFMIN(my_min, my);
 +    }
 +
 +    off = (FFMAX(-my_min, my_max) + 63) >> 6;
 +
 +    return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
 +unhandled:
 +    return s->mb_height-1;
 +}
 +
  /* put block[] to dest[] */
  static inline void put_dct(MpegEncContext *s,
                             DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
@@@ -1993,14 -1858,9 +1993,14 @@@ void MPV_decode_mb_internal(MpegEncCont
         /* save DCT coefficients */
         int i,j;
         DCTELEM *dct = &s->current_picture.dct_coeff[mb_xy*64*6];
 -       for(i=0; i<6; i++)
 -           for(j=0; j<64; j++)
 +       av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
 +       for(i=0; i<6; i++){
 +           for(j=0; j<64; j++){
                 *dct++ = block[i][s->dsp.idct_permutation[j]];
 +               av_log(s->avctx, AV_LOG_DEBUG, "%5d", dct[-1]);
 +           }
 +           av_log(s->avctx, AV_LOG_DEBUG, "\n");
 +       }
      }
  
      s->current_picture.qscale_table[mb_xy]= s->qscale;
              /* motion handling */
              /* decoding or more than one mb_type (MC was already done otherwise) */
              if(!s->encoding){
 +
 +                if(HAVE_PTHREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
 +                    if (s->mv_dir & MV_DIR_FORWARD) {
 +                        ff_thread_await_progress((AVFrame*)s->last_picture_ptr, MPV_lowest_referenced_row(s, 0), 0);
 +                    }
 +                    if (s->mv_dir & MV_DIR_BACKWARD) {
 +                        ff_thread_await_progress((AVFrame*)s->next_picture_ptr, MPV_lowest_referenced_row(s, 1), 0);
 +                    }
 +                }
 +
                  if(lowres_flag){
                      h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
  
@@@ -2247,37 -2097,19 +2247,37 @@@ void MPV_decode_mb(MpegEncContext *s, D
   * @param h is the normal height, this will be reduced automatically if needed for the last row
   */
  void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
 +    const int field_pic= s->picture_structure != PICT_FRAME;
 +    if(field_pic){
 +        h <<= 1;
 +        y <<= 1;
 +    }
 +
 +    if (!s->avctx->hwaccel
 +       && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
 +       && s->unrestricted_mv
 +       && s->current_picture.reference
 +       && !s->intra_only
 +       && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
 +        int sides = 0, edge_h;
 +        if (y==0) sides |= EDGE_TOP;
 +        if (y + h >= s->v_edge_pos) sides |= EDGE_BOTTOM;
 +
 +        edge_h= FFMIN(h, s->v_edge_pos - y);
 +
 +        s->dsp.draw_edges(s->current_picture_ptr->data[0] +  y    *s->linesize  , s->linesize  , s->h_edge_pos   , edge_h   , EDGE_WIDTH  , sides);
 +        s->dsp.draw_edges(s->current_picture_ptr->data[1] + (y>>1)*s->uvlinesize, s->uvlinesize, s->h_edge_pos>>1, edge_h>>1, EDGE_WIDTH/2, sides);
 +        s->dsp.draw_edges(s->current_picture_ptr->data[2] + (y>>1)*s->uvlinesize, s->uvlinesize, s->h_edge_pos>>1, edge_h>>1, EDGE_WIDTH/2, sides);
 +    }
 +
 +    h= FFMIN(h, s->avctx->height - y);
 +
 +    if(field_pic && s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
 +
      if (s->avctx->draw_horiz_band) {
          AVFrame *src;
 -        const int field_pic= s->picture_structure != PICT_FRAME;
          int offset[4];
  
 -        h= FFMIN(h, (s->avctx->height>>field_pic) - y);
 -
 -        if(field_pic && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)){
 -            h <<= 1;
 -            y <<= 1;
 -            if(s->first_field) return;
 -        }
 -
          if(s->pict_type==FF_B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
              src= (AVFrame*)s->current_picture_ptr;
          else if(s->last_picture_ptr)
@@@ -2343,7 -2175,7 +2343,7 @@@ void ff_mpeg_flush(AVCodecContext *avct
      if(s==NULL || s->picture==NULL)
          return;
  
 -    for(i=0; i<MAX_PICTURE_COUNT; i++){
 +    for(i=0; i<s->picture_count; i++){
         if(s->picture[i].data[0] && (   s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
                                      || s->picture[i].type == FF_BUFFER_TYPE_USER))
          free_frame_buffer(s, &s->picture[i]);
@@@ -2597,9 -2429,3 +2597,9 @@@ void ff_set_qscale(MpegEncContext * s, 
      s->y_dc_scale= s->y_dc_scale_table[ qscale ];
      s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  }
 +
 +void MPV_report_decode_progress(MpegEncContext *s)
 +{
 +    if (s->pict_type != FF_B_TYPE && !s->partitioned_frame)
 +        ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_y, 0);
 +}
@@@ -3,20 -3,20 +3,20 @@@
   * Copyright (c) 2000, 2001 Fabrice Bellard
   * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
   *
 - * This file is part of Libav.
 + * This file is part of FFmpeg.
   *
 - * Libav is free software; you can redistribute it and/or
 + * FFmpeg is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
 - * Libav is distributed in the hope that it will be useful,
 + * FFmpeg is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
 - * License along with Libav; if not, write to the Free Software
 + * License along with FFmpeg; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
   *
   * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
@@@ -836,9 -836,9 +836,9 @@@ static void draw_edges_mmx(uint8_t *buf
          );
      }
  
-     for(i=0;i<w;i+=4) {
-         /* top and bottom (and hopefully also the corners) */
-         if (sides&EDGE_TOP) {
+     /* top and bottom (and hopefully also the corners) */
+     if (sides&EDGE_TOP) {
+         for(i = 0; i < w; i += 4) {
              ptr= buf - (i + 1) * wrap - w;
              __asm__ volatile(
                      "1:                             \n\t"
                      : "r" ((x86_reg)buf - (x86_reg)ptr - w), "r" ((x86_reg)-wrap), "r" ((x86_reg)-wrap*3), "r" (ptr+width+2*w)
              );
          }
+     }
  
-         if (sides&EDGE_BOTTOM) {
+     if (sides&EDGE_BOTTOM) {
+         for(i = 0; i < w; i += 4) {
              ptr= last_line + (i + 1) * wrap - w;
              __asm__ volatile(
                      "1:                             \n\t"