Commit a0df454b authored by Steven Walters's avatar Steven Walters Committed by Loren Merritt

split muxers.c into one file per format

simplify internal muxer API
parent d73d798e
......@@ -12,7 +12,23 @@ SRCS = common/mc.c common/predict.c common/pixel.c common/macroblock.c \
encoder/set.c encoder/macroblock.c encoder/cabac.c \
encoder/cavlc.c encoder/encoder.c encoder/lookahead.c
SRCCLI = x264.c matroska.c muxers.c
SRCCLI = x264.c input/yuv.c input/y4m.c output/raw.c \
output/matroska.c output/matroska_ebml.c
MUXERS := $(shell grep -E "(IN|OUT)PUT" config.h)
# Optional muxer module sources
ifneq ($(findstring AVIS_INPUT, $(MUXERS)),)
SRCCLI += input/avis.c
endif
ifneq ($(findstring HAVE_PTHREAD, $(CFLAGS)),)
SRCCLI += input/thread.c
endif
ifneq ($(findstring MP4_OUTPUT, $(MUXERS)),)
SRCCLI += output/mp4.c
endif
# Visualization sources
ifeq ($(VIS),yes)
......
/*****************************************************************************
* avis.c: x264 avi/avs input module
*****************************************************************************
* Copyright (C) 2003-2009 x264 project
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
* 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 Street, Fifth Floor, Boston, MA 02111, USA.
*****************************************************************************/
#include "muxers.h"
#include <windows.h>
#include <vfw.h>
typedef struct
{
PAVISTREAM p_avi;
int width, height;
} avis_hnd_t;
static int open_file( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
{
avis_hnd_t *h = malloc( sizeof(avis_hnd_t) );
if( !h )
return -1;
AVISTREAMINFO info;
int i;
*p_handle = h;
AVIFileInit();
if( AVIStreamOpenFromFile( &h->p_avi, psz_filename, streamtypeVIDEO, 0, OF_READ, NULL ) )
{
AVIFileExit();
return -1;
}
if( AVIStreamInfo( h->p_avi, &info, sizeof(AVISTREAMINFO) ) )
{
AVIStreamRelease( h->p_avi );
AVIFileExit();
return -1;
}
// check input format
if( info.fccHandler != MAKEFOURCC('Y', 'V', '1', '2') )
{
fprintf( stderr, "avis [error]: unsupported input format (%c%c%c%c)\n",
(char)(info.fccHandler & 0xff), (char)((info.fccHandler >> 8) & 0xff),
(char)((info.fccHandler >> 16) & 0xff), (char)((info.fccHandler >> 24)) );
AVIStreamRelease( h->p_avi );
AVIFileExit();
return -1;
}
h->width =
p_param->i_width = info.rcFrame.right - info.rcFrame.left;
h->height =
p_param->i_height = info.rcFrame.bottom - info.rcFrame.top;
i = gcd( info.dwRate, info.dwScale );
p_param->i_fps_den = info.dwScale / i;
p_param->i_fps_num = info.dwRate / i;
fprintf( stderr, "avis [info]: %dx%d @ %.2f fps (%d frames)\n",
p_param->i_width, p_param->i_height,
(double)p_param->i_fps_num / (double)p_param->i_fps_den,
(int)info.dwLength );
return 0;
}
static int get_frame_total( hnd_t handle )
{
avis_hnd_t *h = handle;
AVISTREAMINFO info;
if( AVIStreamInfo( h->p_avi, &info, sizeof(AVISTREAMINFO) ) )
return -1;
return info.dwLength;
}
static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
{
avis_hnd_t *h = handle;
p_pic->img.i_csp = X264_CSP_YV12;
if( AVIStreamRead( h->p_avi, i_frame, 1, p_pic->img.plane[0], h->width * h->height * 3 / 2, NULL, NULL ) )
return -1;
return 0;
}
static int close_file( hnd_t handle )
{
avis_hnd_t *h = handle;
AVIStreamRelease( h->p_avi );
AVIFileExit();
free( h );
return 0;
}
cli_input_t avis_input = { open_file, get_frame_total, read_frame, close_file };
/*****************************************************************************
* input.h: x264 file input modules
*****************************************************************************
* Copyright (C) 2003-2009 x264 project
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
* 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 Street, Fifth Floor, Boston, MA 02111, USA.
*****************************************************************************/
#ifndef X264_INPUT_H
#define X264_INPUT_H
typedef struct
{
int (*open_file)( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param );
int (*get_frame_total)( hnd_t handle );
int (*read_frame)( x264_picture_t *p_pic, hnd_t handle, int i_frame );
int (*close_file)( hnd_t handle );
} cli_input_t;
extern cli_input_t yuv_input;
extern cli_input_t y4m_input;
extern cli_input_t avis_input;
extern cli_input_t thread_input;
#endif
/*****************************************************************************
* thread.c: x264 threaded input module
*****************************************************************************
* Copyright (C) 2003-2009 x264 project
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
* 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 Street, Fifth Floor, Boston, MA 02111, USA.
*****************************************************************************/
#include "muxers.h"
extern cli_input_t input;
typedef struct
{
cli_input_t input;
hnd_t p_handle;
x264_picture_t pic;
x264_pthread_t tid;
int next_frame;
int frame_total;
int in_progress;
struct thread_input_arg_t *next_args;
} thread_hnd_t;
typedef struct thread_input_arg_t
{
thread_hnd_t *h;
x264_picture_t *pic;
int i_frame;
int status;
} thread_input_arg_t;
static int open_file( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
{
thread_hnd_t *h = malloc( sizeof(thread_hnd_t) );
if( !h || x264_picture_alloc( &h->pic, X264_CSP_I420, p_param->i_width, p_param->i_height ) < 0 )
{
fprintf( stderr, "x264 [error]: malloc failed\n" );
return -1;
}
h->input = input;
h->p_handle = *p_handle;
h->in_progress = 0;
h->next_frame = -1;
h->next_args = malloc( sizeof(thread_input_arg_t) );
if( !h->next_args )
return -1;
h->next_args->h = h;
h->next_args->status = 0;
h->frame_total = input.get_frame_total( h->p_handle );
*p_handle = h;
return 0;
}
static int get_frame_total( hnd_t handle )
{
thread_hnd_t *h = handle;
return h->frame_total;
}
static void read_frame_thread_int( thread_input_arg_t *i )
{
i->status = i->h->input.read_frame( i->pic, i->h->p_handle, i->i_frame );
}
static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
{
thread_hnd_t *h = handle;
int ret = 0;
if( h->next_frame >= 0 )
{
x264_pthread_join( h->tid, NULL );
ret |= h->next_args->status;
h->in_progress = 0;
}
if( h->next_frame == i_frame )
XCHG( x264_picture_t, *p_pic, h->pic );
else
ret |= h->input.read_frame( p_pic, h->p_handle, i_frame );
if( !h->frame_total || i_frame+1 < h->frame_total )
{
h->next_frame =
h->next_args->i_frame = i_frame+1;
h->next_args->pic = &h->pic;
if( x264_pthread_create( &h->tid, NULL, (void*)read_frame_thread_int, h->next_args ) )
return -1;
h->in_progress = 1;
}
else
h->next_frame = -1;
return ret;
}
static int close_file( hnd_t handle )
{
thread_hnd_t *h = handle;
if( h->in_progress )
x264_pthread_join( h->tid, NULL );
h->input.close_file( h->p_handle );
x264_picture_clean( &h->pic );
free( h->next_args );
free( h );
return 0;
}
cli_input_t thread_input = { open_file, get_frame_total, read_frame, close_file };
/*****************************************************************************
* y4m.c: x264 y4m input module
*****************************************************************************
* Copyright (C) 2003-2009 x264 project
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
* 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 Street, Fifth Floor, Boston, MA 02111, USA.
*****************************************************************************/
#include "muxers.h"
typedef struct
{
FILE *fh;
int width, height;
int next_frame;
int seq_header_len, frame_header_len;
int frame_size;
} y4m_hnd_t;
#define Y4M_MAGIC "YUV4MPEG2"
#define MAX_YUV4_HEADER 80
#define Y4M_FRAME_MAGIC "FRAME"
#define MAX_FRAME_HEADER 80
static int open_file( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
{
y4m_hnd_t *h = malloc( sizeof(y4m_hnd_t) );
int i, n, d;
char header[MAX_YUV4_HEADER+10];
char *tokstart, *tokend, *header_end;
if( !h )
return -1;
h->next_frame = 0;
if( !strcmp( psz_filename, "-" ) )
h->fh = stdin;
else
h->fh = fopen(psz_filename, "rb");
if( h->fh == NULL )
return -1;
h->frame_header_len = strlen( Y4M_FRAME_MAGIC )+1;
/* Read header */
for( i = 0; i < MAX_YUV4_HEADER; i++ )
{
header[i] = fgetc( h->fh );
if( header[i] == '\n' )
{
/* Add a space after last option. Makes parsing "444" vs
"444alpha" easier. */
header[i+1] = 0x20;
header[i+2] = 0;
break;
}
}
if( i == MAX_YUV4_HEADER || strncmp( header, Y4M_MAGIC, strlen( Y4M_MAGIC ) ) )
return -1;
/* Scan properties */
header_end = &header[i+1]; /* Include space */
h->seq_header_len = i+1;
for( tokstart = &header[strlen( Y4M_MAGIC )+1]; tokstart < header_end; tokstart++ )
{
if( *tokstart == 0x20 )
continue;
switch( *tokstart++ )
{
case 'W': /* Width. Required. */
h->width = p_param->i_width = strtol( tokstart, &tokend, 10 );
tokstart=tokend;
break;
case 'H': /* Height. Required. */
h->height = p_param->i_height = strtol( tokstart, &tokend, 10 );
tokstart=tokend;
break;
case 'C': /* Color space */
if( strncmp( "420", tokstart, 3 ) )
{
fprintf( stderr, "Colorspace unhandled\n" );
return -1;
}
tokstart = strchr( tokstart, 0x20 );
break;
case 'I': /* Interlace type */
switch( *tokstart++ )
{
case 'p': break;
case '?':
case 't':
case 'b':
case 'm':
default:
fprintf( stderr, "Warning, this sequence might be interlaced\n" );
}
break;
case 'F': /* Frame rate - 0:0 if unknown */
if( sscanf( tokstart, "%d:%d", &n, &d ) == 2 && n && d )
{
x264_reduce_fraction( &n, &d );
p_param->i_fps_num = n;
p_param->i_fps_den = d;
}
tokstart = strchr( tokstart, 0x20 );
break;
case 'A': /* Pixel aspect - 0:0 if unknown */
/* Don't override the aspect ratio if sar has been explicitly set on the commandline. */
if( sscanf( tokstart, "%d:%d", &n, &d ) == 2 && n && d && !p_param->vui.i_sar_width && !p_param->vui.i_sar_height )
{
x264_reduce_fraction( &n, &d );
p_param->vui.i_sar_width = n;
p_param->vui.i_sar_height = d;
}
tokstart = strchr( tokstart, 0x20 );
break;
case 'X': /* Vendor extensions */
if( !strncmp( "YSCSS=", tokstart, 6 ) )
{
/* Older nonstandard pixel format representation */
tokstart += 6;
if( strncmp( "420JPEG",tokstart, 7 ) &&
strncmp( "420MPEG2",tokstart, 8 ) &&
strncmp( "420PALDV",tokstart, 8 ) )
{
fprintf( stderr, "Unsupported extended colorspace\n" );
return -1;
}
}
tokstart = strchr( tokstart, 0x20 );
break;
}
}
fprintf( stderr, "yuv4mpeg: %ix%i@%i/%ifps, %i:%i\n",
h->width, h->height, p_param->i_fps_num, p_param->i_fps_den,
p_param->vui.i_sar_width, p_param->vui.i_sar_height );
*p_handle = h;
return 0;
}
/* Most common case: frame_header = "FRAME" */
static int get_frame_total( hnd_t handle )
{
y4m_hnd_t *h = handle;
int i_frame_total = 0;
uint64_t init_pos = ftell( h->fh );
if( !fseek( h->fh, 0, SEEK_END ) )
{
uint64_t i_size = ftell( h->fh );
fseek( h->fh, init_pos, SEEK_SET );
i_frame_total = (int)((i_size - h->seq_header_len) /
(3*(h->width*h->height)/2+h->frame_header_len));
}
return i_frame_total;
}
static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
{
y4m_hnd_t *h = handle;
int slen = strlen( Y4M_FRAME_MAGIC );
int i = 0;
char header[16];
if( i_frame != h->next_frame )
{
if( fseek( h->fh, (uint64_t)i_frame*(3*(h->width*h->height)/2+h->frame_header_len)
+ h->seq_header_len, SEEK_SET ) )
return -1;
}
/* Read frame header - without terminating '\n' */
if( fread( header, 1, slen, h->fh ) != slen )
return -1;
header[slen] = 0;
if( strncmp( header, Y4M_FRAME_MAGIC, slen ) )
{
fprintf( stderr, "Bad header magic (%"PRIx32" <=> %s)\n",
*((uint32_t*)header), header );
return -1;
}
/* Skip most of it */
while( i < MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
i++;
if( i == MAX_FRAME_HEADER )
{
fprintf( stderr, "Bad frame header!\n" );
return -1;
}
h->frame_header_len = i+slen+1;
if( fread( p_pic->img.plane[0], 1, h->width*h->height, h->fh ) <= 0
|| fread( p_pic->img.plane[1], 1, h->width * h->height / 4, h->fh ) <= 0
|| fread( p_pic->img.plane[2], 1, h->width * h->height / 4, h->fh ) <= 0 )
return -1;
h->next_frame = i_frame+1;
return 0;
}
static int close_file( hnd_t handle )
{
y4m_hnd_t *h = handle;
if( !h || !h->fh )
return 0;
fclose( h->fh );
free( h );
return 0;
}
cli_input_t y4m_input = { open_file, get_frame_total, read_frame, close_file };
/*****************************************************************************
* yuv.c: x264 yuv input module
*****************************************************************************
* Copyright (C) 2003-2009 x264 project
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
* 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 Street, Fifth Floor, Boston, MA 02111, USA.
*****************************************************************************/
#include "muxers.h"
typedef struct
{
FILE *fh;
int width, height;
int next_frame;
} yuv_hnd_t;
static int open_file( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
{
yuv_hnd_t *h = malloc( sizeof(yuv_hnd_t) );
if( !h )
return -1;
h->width = p_param->i_width;
h->height = p_param->i_height;
h->next_frame = 0;
if( !strcmp( psz_filename, "-" ) )
h->fh = stdin;
else
h->fh = fopen( psz_filename, "rb" );
if( h->fh == NULL )
return -1;
*p_handle = h;
return 0;
}
static int get_frame_total( hnd_t handle )
{
yuv_hnd_t *h = handle;
int i_frame_total = 0;
if( !fseek( h->fh, 0, SEEK_END ) )
{
uint64_t i_size = ftell( h->fh );
fseek( h->fh, 0, SEEK_SET );
i_frame_total = (int)(i_size / ( h->width * h->height * 3 / 2 ));
}
return i_frame_total;
}
static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
{
yuv_hnd_t *h = handle;
if( i_frame != h->next_frame )
if( fseek( h->fh, (uint64_t)i_frame * h->width * h->height * 3 / 2, SEEK_SET ) )
return -1;
if( fread( p_pic->img.plane[0], 1, h->width * h->height, h->fh ) <= 0
|| fread( p_pic->img.plane[1], 1, h->width * h->height / 4, h->fh ) <= 0
|| fread( p_pic->img.plane[2], 1, h->width * h->height / 4, h->fh ) <= 0 )
return -1;
h->next_frame = i_frame+1;
return 0;
}
static int close_file( hnd_t handle )
{
yuv_hnd_t *h = handle;
if( !h || !h->fh )
return 0;
fclose( h->fh );
free( h );
return 0;
}
cli_input_t yuv_input = { open_file, get_frame_total, read_frame, close_file };
/*****************************************************************************
* muxers.c: h264 file i/o plugins
*****************************************************************************
* Copyright (C) 2003-2008 x264 project
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
* 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 Street, Fifth Floor, Boston, MA 02111, USA.
*****************************************************************************/
#include "common/common.h"
#include "x264.h"
#include "matroska.h"
#include "muxers.h"
#include "config.h"
#include <sys/types.h>
#ifdef AVIS_INPUT
#include <windows.h>
#include <vfw.h>
#endif
#ifdef MP4_OUTPUT
#include <gpac/isomedia.h>
#endif
static int64_t gcd( int64_t a, int64_t b )
{
while( 1 )
{
int64_t c = a % b;
if( !c )
return b;
a = b;
b = c;
}
}
typedef struct
{
FILE *fh;
int width, height;
int next_frame;
} yuv_input_t;
/* raw 420 yuv file operation */
int open_file_yuv( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
{
yuv_input_t *h = malloc( sizeof(yuv_input_t) );
if( !h )
return -1;
h->width = p_param->i_width;
h->height = p_param->i_height;
h->next_frame = 0;
if( !strcmp( psz_filename, "-" ) )
h->fh = stdin;
else
h->fh = fopen( psz_filename, "rb" );
if( h->fh == NULL )
return -1;
*p_handle = (hnd_t)h;
return 0;
}
int get_frame_total_yuv( hnd_t handle )
{
yuv_input_t *h = handle;
int i_frame_total = 0;
if( !fseek( h->fh, 0, SEEK_END ) )
{
uint64_t i_size = ftell( h->fh );