From 0807aa141fdf700434816bd37368d92b57a58feb Mon Sep 17 00:00:00 2001
From: Stanislav Dolganov <dolganov@qst.hk>
Date: Mon, 28 Mar 2016 20:17:07 +0300
Subject: [PATCH 4/4] ffv1 threads bug fix

---
 libavcodec/ffv1dec.c | 23 ++++++++++++-----------
 libavcodec/ffv1enc.c |  1 -
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/libavcodec/ffv1dec.c b/libavcodec/ffv1dec.c
index b1aaeea..08c087a 100644
--- a/libavcodec/ffv1dec.c
+++ b/libavcodec/ffv1dec.c
@@ -39,7 +39,7 @@
 #include "mathops.h"
 #include "ffv1.h"
 
-static int ff_predict_frame(AVCodecContext *c, FFV1Context *f)
+static int ff_predict_frame(AVCodecContext *avctx, FFV1Context *f)
 {
     int ret, i, x, y;
     AVFrame *curr     = f->picture.f;
@@ -53,11 +53,11 @@ static int ff_predict_frame(AVCodecContext *c, FFV1Context *f)
     const int ch = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
 
     if (f->residual.f)
-        av_frame_unref(f->residual.f);
-    if ((ret = av_frame_ref(f->residual.f, f->picture.f)) < 0)
+        ff_thread_release_buffer(avctx, &f->residual);
+    if ((ret = ff_thread_ref_frame(&f->residual, &f->picture)) < 0)
         return ret;
     if ((ret = av_frame_make_writable(f->residual.f)) < 0) {
-        av_frame_unref(f->residual.f);
+        ff_thread_release_buffer(avctx, &f->residual);
         return ret;
     }
 
@@ -88,7 +88,6 @@ static int ff_predict_frame(AVCodecContext *c, FFV1Context *f)
                               desc,
                               0, y, i, w1, 0);
             for (x = 0; x < w1; ++x)
-                //f->c_image_line_buf[x] ^= f->p_image_line_buf[x];
                 f->c_image_line_buf[x] = (f->c_image_line_buf[x] + f->p_image_line_buf[x] - (max_val >> 2)) & (max_val - 1);
             av_write_image_line(f->c_image_line_buf,
                                 residual->data,
@@ -105,8 +104,10 @@ static int ff_predict_frame(AVCodecContext *c, FFV1Context *f)
         }
     }
 
-    if ((ret = av_frame_ref(f->picture.f, f->residual.f)) < 0)
-        return ret;
+    /*if ((ret = av_frame_ref(f->picture.f, f->residual.f)) < 0)
+        return ret;*/
+
+    av_image_copy(curr->data, curr->linesize, residual->data, residual->linesize, curr->format, f->width, f->height);
 
     return 0;
 }
@@ -1092,15 +1093,15 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPac
                           fs->slice_height);
         }
     }
-    /*ff_thread_report_progress(&f->picture, INT_MAX, 0);
 
-    f->picture_number++;*/
+    ff_thread_await_progress(&f->last_picture, INT_MAX, 0);
 
     if (!p->key_frame) {
-        if ((ret = ff_predict_frame(avctx, f)) < 0)
+        if ((ret = ff_predict_frame(avctx, f)) < 0) {
+            ff_thread_report_progress(&f->picture, INT_MAX, 0);
             return ret;
+        }
     }
-
     ff_thread_report_progress(&f->picture, INT_MAX, 0);
 
     f->picture_number++;
diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index 1859c6e..59ee30c 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -186,7 +186,6 @@ static int ff_frame_diff(FFV1Context *f, const AVFrame *pict)
                               desc,
                               0, y, i, w1, 0);
             for (x = 0; x < w1; ++x)
-                //f->c_image_line_buf[x] ^= f->p_image_line_buf[x];
                 f->c_image_line_buf[x] = (f->c_image_line_buf[x] - f->p_image_line_buf[x] + (max_val >> 2)) & (max_val - 1);
             av_write_image_line(f->c_image_line_buf,
                                 residual->data,
-- 
2.6.4 (Apple Git-63)

