/*
 * Copyright (C) 2007 Loren Merritt <lorenm@u.washington.edu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "common.h"

xlsc_frame_t *xlsc_frame_new( xlsc_t *h )
{
    xlsc_frame_t *frame = malloc(sizeof(xlsc_frame_t));
    memset( frame, 0, sizeof(xlsc_frame_t) );
    frame->buffer = malloc( (16*h->mb_height + 2*FRAME_PAD) * h->stride );
    frame->plane = frame->buffer + (h->stride + 1) * FRAME_PAD;
    frame->macroblocks = malloc( h->mb_count * sizeof(*frame->macroblocks) );
    if( h->param.lookahead )
        frame->dct = malloc( h->mb_count * sizeof(*frame->dct) );
    frame->frame_num = -1;
    return frame;
}

void xlsc_frame_free( xlsc_frame_t *frame )
{
    free( frame->buffer );
    free( frame->macroblocks );
    free( frame->dct );
    free( frame );
}

void xlsc_frame_from_pic( xlsc_t *h, xlsc_frame_t *frame, xlsc_picture_t *pic )
{
    int y;
    int width  = h->param.width;
    int height = h->param.height;
    int pad_width  = 16*h->mb_width  - width;
    int pad_height = 16*h->mb_height - height;
    int stride = h->stride;
    uint8_t *pix = frame->plane;
    for( y=0; y<height; y++ )
    {
        memcpy( pix + y*stride,
                pic->plane + y*pic->stride,
                width );
        if( pad_width )
        {
            memset( pix+y*stride + width,
                    pix[y*stride + width - 1],
                    pad_width );
        }
    }
    for( ; y < height + pad_height; y++ )
        memcpy( pix +  y   *stride,
                pix + (y-1)*stride,
                width + pad_width );
}

void xlsc_frame_to_pic( xlsc_t *h, xlsc_frame_t *frame, xlsc_picture_t *pic )
{
    pic->stride = h->stride;
    pic->plane = frame->plane;
    pic->type = frame->intra ? XLSC_FRAME_I : XLSC_FRAME_P;
    pic->qp = frame->qp;
}

void xlsc_frame_expand_border( xlsc_t *h, xlsc_frame_t *frame )
{
    int y;
    int width  = 16*h->mb_width;
    int height = 16*h->mb_height;
    int pad = FRAME_PAD;
    int stride = h->stride;
    uint8_t *pix = frame->plane;
    for( y=0; y<height; y++, pix+=stride )
    {
        memset( pix - pad, pix[0], pad );
        memset( pix + width, pix[width-1], pad );
    }
    pix = frame->plane - pad;
    for( y=1; y<=pad; y++ )
        memcpy( pix - y*stride, pix, width + 2*pad );
    pix = frame->plane - pad + (height-1)*stride;
    for( y=1; y<=pad; y++ )
        memcpy( pix + y*stride, pix, width + 2*pad );
}

void xlsc_frame_push( xlsc_frame_t **list, xlsc_frame_t *frame )
{
    int i = 0;
    while( list[i] ) i++;
    list[i] = frame;
}

xlsc_frame_t *xlsc_frame_pop( xlsc_frame_t **list )
{
    xlsc_frame_t *frame;
    int i = 0;
    assert( list[0] );
    while( list[i+1] ) i++;
    frame = list[i];
    list[i] = NULL;
    return frame;
}

void xlsc_frame_unshift( xlsc_frame_t **list, xlsc_frame_t *frame )
{
    int i = 0;
    while( list[i] ) i++;
    while( i-- )
        list[i+1] = list[i];
    list[0] = frame;
}

xlsc_frame_t *xlsc_frame_shift( xlsc_frame_t **list )
{
    xlsc_frame_t *frame = list[0];
    int i;
    for( i = 0; list[i]; i++ )
        list[i] = list[i+1];
    assert(frame);
    return frame;
}

