]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/fraps.c
snow altivec is broken
[ffmpeg] / libavcodec / fraps.c
index fd2b8505f284c41032b49ea19621237d5f953e53..a15f602a57caa3815072532eb2fb06d938c206e2 100644 (file)
@@ -18,7 +18,6 @@
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
  */
 
 /**
@@ -73,7 +72,6 @@ static int decode_init(AVCodecContext *avctx)
     FrapsContext * const s = avctx->priv_data;
 
     avctx->coded_frame = (AVFrame*)&s->frame;
-    avctx->has_b_frames = 0;
     avctx->pix_fmt= PIX_FMT_NONE; /* set in decode_frame */
 
     s->avctx = avctx;
@@ -89,34 +87,38 @@ static int decode_init(AVCodecContext *avctx)
  * Comparator - our nodes should ascend by count
  * but with preserved symbol order
  */
-static int huff_cmp(const Node *a, const Node *b){
+static int huff_cmp(const void *va, const void *vb){
+    const Node *a = va, *b = vb;
     return (a->count - b->count)*256 + a->sym - b->sym;
 }
 
-static void get_tree_codes(uint32_t *bits, int16_t *lens, Node *nodes, int node, uint32_t pfx, int pl)
+static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat, Node *nodes, int node, uint32_t pfx, int pl, int *pos)
 {
     int s;
 
     s = nodes[node].sym;
-    if(s != HNODE){
-        bits[s] = pfx;
-        lens[s] = pl;
+    if(s != HNODE || !nodes[node].count){
+        bits[*pos] = pfx;
+        lens[*pos] = pl;
+        xlat[*pos] = s;
+        (*pos)++;
     }else{
         pfx <<= 1;
         pl++;
-        get_tree_codes(bits, lens, nodes, nodes[node].n0, pfx, pl);
+        get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0, pfx, pl, pos);
         pfx |= 1;
-        get_tree_codes(bits, lens, nodes, nodes[node].n0+1, pfx, pl);
+        get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0+1, pfx, pl, pos);
     }
 }
 
-static int build_huff_tree(VLC *vlc, Node *nodes)
+static int build_huff_tree(VLC *vlc, Node *nodes, uint8_t *xlat)
 {
     uint32_t bits[256];
     int16_t lens[256];
+    int pos = 0;
 
-    get_tree_codes(bits, lens, nodes, 510, 0, 0);
-    return init_vlc(vlc, 9, 256, lens, 2, 2, bits, 4, 4, 0);
+    get_tree_codes(bits, lens, xlat, nodes, 510, 0, 0, &pos);
+    return init_vlc(vlc, 9, pos, lens, 2, 2, bits, 4, 4, 0);
 }
 
 
@@ -131,11 +133,16 @@ static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
     GetBitContext gb;
     VLC vlc;
     int64_t sum = 0;
+    uint8_t recode[256];
 
     for(i = 0; i < 256; i++){
         s->nodes[i].sym = i;
-        s->nodes[i].count = LE_32(src);
+        s->nodes[i].count = AV_RL32(src);
         s->nodes[i].n0 = -2;
+        if(s->nodes[i].count < 0) {
+            av_log(s->avctx, AV_LOG_ERROR, "Symbol count < 0\n");
+            return -1;
+        }
         src += 4;
         sum += s->nodes[i].count;
     }
@@ -147,7 +154,6 @@ static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
     }
     qsort(s->nodes, 256, sizeof(Node), huff_cmp);
     cur_node = 256;
-    // FIXME how it will handle nodes with zero count?
     for(i = 0; i < 511; i += 2){
         s->nodes[cur_node].sym = HNODE;
         s->nodes[cur_node].count = s->nodes[i].count + s->nodes[i+1].count;
@@ -158,7 +164,7 @@ static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
         }
         cur_node++;
     }
-    if(build_huff_tree(&vlc, s->nodes) < 0){
+    if(build_huff_tree(&vlc, s->nodes, recode) < 0){
         av_log(s->avctx, AV_LOG_ERROR, "Error building tree\n");
         return -1;
     }
@@ -170,7 +176,7 @@ static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
     init_get_bits(&gb, s->tmpbuf, size * 8);
     for(j = 0; j < h; j++){
         for(i = 0; i < w; i++){
-            dst[i] = get_vlc2(&gb, vlc.table, 9, 3);
+            dst[i] = recode[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
              */
@@ -208,7 +214,7 @@ static int decode_frame(AVCodecContext *avctx,
     int i, is_chroma, planes;
 
 
-    header = LE_32(buf);
+    header = AV_RL32(buf);
     version = header & 0xff;
     header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
 
@@ -310,10 +316,10 @@ static int decode_frame(AVCodecContext *avctx,
     case 4:
         /**
          * Fraps v2 is Huffman-coded YUV420 planes
-         * Fraps v4 is the same except it works in grayscale
+         * Fraps v4 is virtually the same
          */
-        avctx->pix_fmt = (version == 2) ? PIX_FMT_YUV420P : PIX_FMT_GRAY8;
-        planes = (version == 2) ? 3 : 1;
+        avctx->pix_fmt = PIX_FMT_YUV420P;
+        planes = 3;
         f->reference = 1;
         f->buffer_hints = FF_BUFFER_HINTS_VALID |
                           FF_BUFFER_HINTS_PRESERVE |
@@ -330,12 +336,12 @@ static int decode_frame(AVCodecContext *avctx,
         }
         f->pict_type = FF_I_TYPE;
         f->key_frame = 1;
-        if ((LE_32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
+        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] = LE_32(buf + 4 + i * 4);
+            offs[i] = AV_RL32(buf + 4 + i * 4);
             if(offs[i] >= buf_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;
@@ -344,7 +350,7 @@ static int decode_frame(AVCodecContext *avctx,
         offs[planes] = buf_size;
         for(i = 0; i < planes; i++){
             is_chroma = !!i;
-            s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024);
+            s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
             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) < 0) {
                 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);