/*
 * 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
 */

#define _LARGEFILE_SOURCE
#define _FILE_OFFSET_BITS 64

#include <string.h>
#include <stdlib.h>
#include "muxers.h"

#define fseek fseeko
#define ftell ftello

struct y8_mux_t
{
    FILE *fh;
    int width, height;
    int next_frame;
    uint8_t *tmp;
};

y8_mux_t *y8_in_open( char *filename, int width, int height )
{
    y8_mux_t *h = malloc(sizeof(y8_mux_t));
    h->width = width;
    h->height = height;
    h->next_frame = 0;

    if( !strcmp( filename, "-" ) )
        h->fh = stdin;
    else
        h->fh = fopen( filename, "rb" );
    if( h->fh == NULL )
        return NULL;

    return h;
}

int y8_in_get_frame_total( y8_mux_t *h )
{
    int frame_total = 0;

    if( !fseek( h->fh, 0, SEEK_END ) )
    {
        uint64_t size = ftell( h->fh );
        fseek( h->fh, 0, SEEK_SET );
        frame_total = size / (h->width * h->height);
    }

    return frame_total;
}

int y8_in_read_frame( y8_mux_t *h, xlsc_picture_t *pic, int frame )
{
    if( frame != h->next_frame )
        if( fseek( h->fh, (uint64_t)frame * h->width * h->height * 3 / 2, SEEK_SET ) )
            return -1;

    if( fread( pic->plane, h->width * h->height, 1, h->fh ) != 1 )
        return -1;
    pic->stride = h->width;

    h->next_frame = frame+1;

    return 0;
}

void y8_in_close( y8_mux_t *h )
{
    if( !h || !h->fh )
        return;
    fclose( h->fh );
    free( h );
}

y8_mux_t *y8_out_open( char *filename, int width, int height )
{
    y8_mux_t *h = malloc(sizeof(y8_mux_t));
    h->width = width;
    h->height = height;

    if( !strcmp( filename, "-" ) )
        h->fh = stdout;
    else
        h->fh = fopen( filename, "wb" );
    if( h->fh == NULL )
        return NULL;

    h->tmp = malloc( width*height );

    return h;
}

int y8_out_write_frame( y8_mux_t *h, xlsc_picture_t *pic )
{
    int y;
    // faster than multiple fwrite, I think.
    for( y=0; y<h->height; y++ )
        memcpy( h->tmp + y*h->width, pic->plane + y*pic->stride, h->width );
    if( fwrite( h->tmp, h->width*h->height, 1, h->fh ) != 1 )
        return -1;
    return 0;
}

void y8_out_close( y8_mux_t *h )
{
    if( !h || !h->fh )
        return;
    fclose( h->fh );
    free( h->tmp );
    free( h );
}

int bsf_out_write_frame( FILE *fh, uint8_t *data, int size )
{
    uint8_t prefix[4] = { size>>24, size>>16, size>>8, size };
    if( fwrite( prefix, 4, 1, fh ) != 1 )
        return -1;
    if( fwrite( data, size, 1, fh ) != 1 )
        return -1;
    return size+4;
}

int bsf_in_read_frame( FILE *fh, uint8_t **data, int *size )
{
    uint8_t prefix[4];
    if( fread( prefix, 4, 1, fh ) != 1 )
        return -1;
    *size = prefix[0]<<24 | prefix[1]<<16 | prefix[2]<<8 | prefix[3];
    *data = realloc( *data, *size+16 ); // some extra in case the decoder overreads
    if( !*data )
        return -1;
    if( fread( *data, *size, 1, fh ) != 1 )
        return -1;
    return *size+4;
}
