From a4065ca4146fb0dfe555ffa7ed553f687af200a6 Mon Sep 17 00:00:00 2001
From: Javier Cabezas <jcabgz@gmail.com>
Date: Mon, 30 Jan 2012 20:16:55 +0100
Subject: [PATCH] Fraps: restore old behavior regarding P frames.

P frames are repeated frames in Fraps, not skip frames. Restore the old behavior where those P frames are outputted as normal frames with their own timestamp. This matches the Fraps reference decoder. In order to make frame multithreading work, a 2 frame buffer is used.
---
 libavcodec/fraps.c |  228 +++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 154 insertions(+), 74 deletions(-)

diff --git a/libavcodec/fraps.c b/libavcodec/fraps.c
index a7d5a73..8e3b816 100644
--- a/libavcodec/fraps.c
+++ b/libavcodec/fraps.c
@@ -32,11 +32,13 @@
  */
 
 #include "avcodec.h"
+#include "internal.h"
 #include "get_bits.h"
 #include "huffman.h"
 #include "bytestream.h"
 #include "dsputil.h"
 #include "thread.h"
+#include "libavutil/imgutils.h"
 
 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
 
@@ -45,7 +47,9 @@
  */
 typedef struct FrapsContext{
     AVCodecContext *avctx;
-    AVFrame frame;
+    int cur_index, prev_index;
+    int next_cur_index, next_prev_index;
+    AVFrame buf_ptrs[2];
     uint8_t *tmpbuf;
     int tmpbuf_size;
     DSPContext dsp;
@@ -61,8 +65,8 @@ static av_cold int decode_init(AVCodecContext *avctx)
 {
     FrapsContext * const s = avctx->priv_data;
 
-    avcodec_get_frame_defaults(&s->frame);
-    avctx->coded_frame = (AVFrame*)&s->frame;
+    s->prev_index = 0;
+    s->cur_index = 1;
 
     s->avctx = avctx;
     s->tmpbuf = NULL;
@@ -72,6 +76,22 @@ static av_cold int decode_init(AVCodecContext *avctx)
     return 0;
 }
 
+static int fraps_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
+{
+    FrapsContext *dst = avctx->priv_data, *src = avctx_from->priv_data;
+
+    if (avctx == avctx_from) return 0;
+
+    dst->cur_index  = src->next_cur_index;
+    dst->prev_index = src->next_prev_index;
+
+    memcpy(dst->buf_ptrs, src->buf_ptrs, sizeof(src->buf_ptrs));
+
+    memset(&dst->buf_ptrs[dst->cur_index], 0, sizeof(AVFrame));
+
+    return 0;
+}
+
 /**
  * Comparator - our nodes should ascend by count
  * but with preserved symbol order
@@ -93,7 +113,7 @@ static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
     VLC vlc;
     Node nodes[512];
 
-    for(i = 0; i < 256; i++)
+    for (i = 0; i < 256; i++)
         nodes[i].count = bytestream_get_le32(&src);
     size -= 1024;
     if (ff_huff_build_tree(s->avctx, &vlc, 256, nodes, huff_cmp,
@@ -105,14 +125,16 @@ static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
     s->dsp.bswap_buf((uint32_t *)s->tmpbuf, (const uint32_t *)src, size >> 2);
 
     init_get_bits(&gb, s->tmpbuf, size * 8);
-    for(j = 0; j < h; j++){
-        for(i = 0; i < w*step; i += step){
+    for (j = 0; j < h; j++){
+        for (i = 0; i < w*step; i += step){
             dst[i] = get_vlc2(&gb, vlc.table, 9, 3);
             /* lines are stored as deltas between previous lines
              * and we need to add 0x80 to the first lines of chroma planes
              */
-            if(j) dst[i] += dst[i - stride];
-            else if(Uoff) dst[i] += 0x80;
+            if (j)
+                dst[i] += dst[i - stride];
+            else if (Uoff)
+                dst[i] += 0x80;
             if (get_bits_left(&gb) < 0) {
                 free_vlc(&vlc);
                 return AVERROR_INVALIDDATA;
@@ -124,6 +146,27 @@ static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
     return 0;
 }
 
+static void fraps_frame_copy(FrapsContext *s, uint8_t *dst_data[3], int dst_linesizes[3],
+                             uint8_t *src_data[3], const int src_linesizes[3],
+                             enum PixelFormat pix_fmt, int width, int height)
+{
+    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
+    int i;
+
+    for (i = 0; i < 3; i++) {
+        int h = height;
+        int bwidth = av_image_get_linesize(pix_fmt, width, i);
+        if (i) {
+            h = -((-height)>>desc->log2_chroma_h);
+        }
+        ff_thread_await_progress(&s->buf_ptrs[s->prev_index], i, 0);
+        av_image_copy_plane(dst_data[i], dst_linesizes[i],
+                            src_data[i], src_linesizes[i],
+                            bwidth, h);
+        ff_thread_report_progress(&s->buf_ptrs[s->cur_index], i, 0);
+    }
+}
+
 static int decode_frame(AVCodecContext *avctx,
                         void *data, int *data_size,
                         AVPacket *avpkt)
@@ -131,15 +174,13 @@ static int decode_frame(AVCodecContext *avctx,
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     FrapsContext * const s = avctx->priv_data;
-    AVFrame *frame = data;
-    AVFrame * const f = (AVFrame*)&s->frame;
     uint32_t header;
-    unsigned int version,header_size;
+    unsigned int version, header_size;
     unsigned int x, y;
     const uint32_t *buf32;
-    uint32_t *luma1,*luma2,*cb,*cr;
+    uint32_t *luma1, *luma2, *cb, *cr;
     uint32_t offs[4];
-    int i, j, is_chroma;
+    int i, j, is_chroma, is_Pframe, ret;
     const int planes = 3;
     uint8_t *out;
 
@@ -148,6 +189,8 @@ static int decode_frame(AVCodecContext *avctx,
     version = header & 0xff;
     header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
 
+    buf += header_size;
+
     if (version > 5) {
         av_log(avctx, AV_LOG_ERROR,
                "This file is encoded with Fraps version %d. " \
@@ -155,13 +198,18 @@ static int decode_frame(AVCodecContext *avctx,
         return -1;
     }
 
-    buf += header_size;
-
     avctx->pix_fmt = version & 1 ? PIX_FMT_BGR24 : PIX_FMT_YUVJ420P;
 
     if (version < 2) {
         unsigned needed_size = avctx->width*avctx->height*3;
-        if (version == 0) needed_size /= 2;
+        if (version == 0) {
+            if ( (avctx->width % 8) != 0 || (avctx->height % 2) != 0 ) {
+                av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
+                       avctx->width, avctx->height);
+                return -1;
+            }
+            needed_size /= 2;
+        }
         needed_size += header_size;
         if (buf_size != needed_size && buf_size != header_size) {
             av_log(avctx, AV_LOG_ERROR,
@@ -169,64 +217,75 @@ static int decode_frame(AVCodecContext *avctx,
                    buf_size, needed_size);
             return -1;
         }
-        /* bit 31 means same as previous pic */
-        if (header & (1U<<31)) {
-            *data_size = 0;
-            return buf_size;
-        }
+        is_Pframe = (header & (1U<<31)) ? 1 : 0;
     } else {
-        /* skip frame */
-        if (buf_size == 8) {
-            *data_size = 0;
-            return buf_size;
-        }
-        if (AV_RL32(buf) != FPS_TAG || buf_size < planes*1024 + 24) {
-            av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
-            return -1;
-        }
-        for(i = 0; i < planes; i++) {
-            offs[i] = AV_RL32(buf + 4 + i * 4);
-            if(offs[i] >= buf_size - header_size || (i && offs[i] <= offs[i - 1] + 1024)) {
-                av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
+        is_Pframe = buf_size == 8 ? 1 : 0;
+        if (!is_Pframe) {
+            if (AV_RL32(buf) != FPS_TAG || buf_size < (planes*1024 + 24)) {
+                av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
                 return -1;
             }
-        }
-        offs[planes] = buf_size - header_size;
-        for(i = 0; i < planes; i++) {
-            av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024);
-            if (!s->tmpbuf)
-                return AVERROR(ENOMEM);
+            for(i = 0; i < planes; i++) {
+                offs[i] = AV_RL32(buf + 4 + i * 4);
+                if(offs[i] >= buf_size - header_size || (i && offs[i] <= offs[i - 1] + 1024)) {
+                    av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
+                    return -1;
+                }
+            }
+            offs[planes] = buf_size - header_size;
+            for(i = 0; i < planes; i++) {
+                av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024);
+                if (!s->tmpbuf)
+                    return AVERROR(ENOMEM);
+            }
         }
     }
 
-    if (f->data[0])
-        ff_thread_release_buffer(avctx, f);
-    f->pict_type = AV_PICTURE_TYPE_I;
-    f->key_frame = 1;
-    f->reference = 0;
-    f->buffer_hints = FF_BUFFER_HINTS_VALID;
-    if (ff_thread_get_buffer(avctx, f)) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if (is_Pframe && !s->buf_ptrs[s->prev_index].data[0]) {
+        av_log(avctx, AV_LOG_ERROR, "decoding must start with keyframe\n");
         return -1;
     }
 
-    switch(version) {
+    s->buf_ptrs[s->cur_index].reference = 3;
+
+    if (is_Pframe) {
+        s->buf_ptrs[s->cur_index].pict_type = AV_PICTURE_TYPE_P;
+        s->buf_ptrs[s->cur_index].key_frame = 0;
+    } else {
+        s->buf_ptrs[s->cur_index].pict_type = AV_PICTURE_TYPE_I;
+        s->buf_ptrs[s->cur_index].key_frame = 1;
+    }
+
+    if ((ret = ff_thread_get_buffer(avctx, &s->buf_ptrs[s->cur_index])) < 0) {
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        return ret;
+    }
+
+    s->next_prev_index = s->cur_index;
+    s->next_cur_index  = (s->cur_index - 1) & 1;
+
+    ff_thread_finish_setup(avctx);
+
+    /* Copy previous frame */
+    if (is_Pframe) {
+        fraps_frame_copy(s, s->buf_ptrs[s->cur_index].data, s->buf_ptrs[s->cur_index].linesize,
+                         s->buf_ptrs[s->prev_index].data, s->buf_ptrs[s->prev_index].linesize,
+                         avctx->pix_fmt, avctx->width, avctx->height);
+        goto end;
+    }
+
+    /* Decode I-frame */
+    switch (version) {
     case 0:
     default:
         /* Fraps v0 is a reordered YUV420 */
-        if ( (avctx->width % 8) != 0 || (avctx->height % 2) != 0 ) {
-            av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
-                   avctx->width, avctx->height);
-            return -1;
-        }
-
-        buf32=(const uint32_t*)buf;
-        for(y=0; y<avctx->height/2; y++){
-            luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
-            luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
-            cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
-            cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
-            for(x=0; x<avctx->width; x+=8){
+        buf32 = (const uint32_t*)buf;
+        for(y = 0; y < avctx->height/2; y++){
+            luma1 = (uint32_t*)&s->buf_ptrs[s->cur_index].data[0][y*2*s->buf_ptrs[s->cur_index].linesize[0]];
+            luma2 = (uint32_t*)&s->buf_ptrs[s->cur_index].data[0][(y*2+1)*s->buf_ptrs[s->cur_index].linesize[0]];
+            cr    = (uint32_t*)&s->buf_ptrs[s->cur_index].data[1][y*s->buf_ptrs[s->cur_index].linesize[1]];
+            cb    = (uint32_t*)&s->buf_ptrs[s->cur_index].data[2][y*s->buf_ptrs[s->cur_index].linesize[2]];
+            for(x = 0; x < avctx->width; x += 8){
                 *luma1++ = *buf32++;
                 *luma1++ = *buf32++;
                 *luma2++ = *buf32++;
@@ -235,14 +294,16 @@ static int decode_frame(AVCodecContext *avctx,
                 *cb++    = *buf32++;
             }
         }
+        ff_thread_report_progress(&s->buf_ptrs[s->cur_index], INT_MAX, 0);
         break;
 
     case 1:
         /* Fraps v1 is an upside-down BGR24 */
-        for(y=0; y<avctx->height; y++)
-            memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
+        for(y = 0; y < avctx->height; y++)
+            memcpy(&s->buf_ptrs[s->cur_index].data[0][(avctx->height-y)*s->buf_ptrs[s->cur_index].linesize[0]],
                    &buf[y*avctx->width*3],
                    3*avctx->width);
+        ff_thread_report_progress(&s->buf_ptrs[s->cur_index], INT_MAX, 0);
         break;
 
     case 2:
@@ -253,25 +314,28 @@ static int decode_frame(AVCodecContext *avctx,
          */
         for(i = 0; i < planes; i++){
             is_chroma = !!i;
-            if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
-                    avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma, 1) < 0) {
+            if(fraps2_decode_plane(s, s->buf_ptrs[s->cur_index].data[i], s->buf_ptrs[s->cur_index].linesize[i], avctx->width >> is_chroma,
+                                   avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma, 1) < 0) {
                 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
                 return -1;
             }
+            else
+                ff_thread_report_progress(&s->buf_ptrs[s->cur_index], i, 0);
         }
         break;
+
     case 3:
     case 5:
         /* Virtually the same as version 4, but is for RGB24 */
         for(i = 0; i < planes; i++){
-            if(fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)), -f->linesize[0],
-                    avctx->width, avctx->height, buf + offs[i], offs[i + 1] - offs[i], 0, 3) < 0) {
+            if(fraps2_decode_plane(s, s->buf_ptrs[s->cur_index].data[0] + i + (s->buf_ptrs[s->cur_index].linesize[0] * (avctx->height - 1)), -s->buf_ptrs[s->cur_index].linesize[0],
+                                   avctx->width, avctx->height, buf + offs[i], offs[i + 1] - offs[i], 0, 3) < 0) {
                 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
                 return -1;
             }
         }
-        out = f->data[0];
         // convert pseudo-YUV into real RGB
+        out = s->buf_ptrs[s->cur_index].data[0];
         for(j = 0; j < avctx->height; j++){
             uint8_t *line_end = out + 3*avctx->width;
             while (out < line_end) {
@@ -279,14 +343,23 @@ static int decode_frame(AVCodecContext *avctx,
                 out[2]  += out[1];
                 out += 3;
             }
-            out += f->linesize[0] - 3*avctx->width;
+            out += s->buf_ptrs[s->cur_index].linesize[0] - 3*avctx->width;
         }
+        ff_thread_report_progress(&s->buf_ptrs[s->cur_index], INT_MAX, 0);
         break;
     }
 
-    *frame = *f;
+end:
+    *(AVFrame*)data = s->buf_ptrs[s->cur_index];
     *data_size = sizeof(AVFrame);
 
+    s->prev_index = s->next_prev_index;
+    s->cur_index  = s->next_cur_index;
+
+    /* Only release frames that aren't used anymore */
+    if(s->buf_ptrs[s->cur_index].data[0])
+        ff_thread_release_buffer(avctx, &s->buf_ptrs[s->cur_index]);
+
     return buf_size;
 }
 
@@ -299,11 +372,17 @@ static int decode_frame(AVCodecContext *avctx,
 static av_cold int decode_end(AVCodecContext *avctx)
 {
     FrapsContext *s = (FrapsContext*)avctx->priv_data;
-
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    int i;
 
     av_freep(&s->tmpbuf);
+
+    if (avctx->internal->is_copy)
+        return 0;
+
+    for(i = 0; i < 2; i++)
+        if(s->buf_ptrs[i].data[0])
+            ff_thread_release_buffer(avctx, &s->buf_ptrs[i]);
+
     return 0;
 }
 
@@ -318,4 +397,5 @@ AVCodec ff_fraps_decoder = {
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
     .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
+    .update_thread_context = ONLY_IF_THREADS_ENABLED(fraps_decode_update_thread_context)
 };
-- 
1.7.8

