2014-10-12 18:45:40 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* example.c: libx264 API usage example
|
|
|
|
*****************************************************************************
|
2024-01-13 12:45:39 +01:00
|
|
|
* Copyright (C) 2014-2024 x264 project
|
2014-10-12 18:45:40 +02:00
|
|
|
*
|
|
|
|
* Authors: Anton Mitrofanov <BugMaster@narod.ru>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This program is also available under a commercial proprietary license.
|
|
|
|
* For more information, contact us at licensing@x264.com.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <io.h> /* _setmode() */
|
|
|
|
#include <fcntl.h> /* _O_BINARY */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <x264.h>
|
|
|
|
|
|
|
|
#define FAIL_IF_ERROR( cond, ... )\
|
|
|
|
do\
|
|
|
|
{\
|
|
|
|
if( cond )\
|
|
|
|
{\
|
|
|
|
fprintf( stderr, __VA_ARGS__ );\
|
|
|
|
goto fail;\
|
|
|
|
}\
|
|
|
|
} while( 0 )
|
|
|
|
|
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
int width, height;
|
|
|
|
x264_param_t param;
|
|
|
|
x264_picture_t pic;
|
|
|
|
x264_picture_t pic_out;
|
|
|
|
x264_t *h;
|
|
|
|
int i_frame = 0;
|
|
|
|
int i_frame_size;
|
|
|
|
x264_nal_t *nal;
|
|
|
|
int i_nal;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
_setmode( _fileno( stdin ), _O_BINARY );
|
|
|
|
_setmode( _fileno( stdout ), _O_BINARY );
|
|
|
|
_setmode( _fileno( stderr ), _O_BINARY );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
FAIL_IF_ERROR( !(argc > 1), "Example usage: example 352x288 <input.yuv >output.h264\n" );
|
|
|
|
FAIL_IF_ERROR( 2 != sscanf( argv[1], "%dx%d", &width, &height ), "resolution not specified or incorrect\n" );
|
|
|
|
|
|
|
|
/* Get default params for preset/tuning */
|
|
|
|
if( x264_param_default_preset( ¶m, "medium", NULL ) < 0 )
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Configure non-default params */
|
Unify 8-bit and 10-bit CLI and libraries
Add 'i_bitdepth' to x264_param_t with the corresponding '--output-depth' CLI
option to set the bit depth at runtime.
Drop the 'x264_bit_depth' global variable. Rather than hardcoding it to an
incorrect value, it's preferable to induce a linking failure. If applications
relies on this symbol this will make it more obvious where the problem is.
Add Makefile rules that compiles modules with different bit depths. Assembly
on x86 is prefixed with the 'private_prefix' define, while all other archs
modify their function prefix internally.
Templatize the main C library, x86/x86_64 assembly, ARM assembly, AARCH64
assembly, PowerPC assembly, and MIPS assembly.
The depth and cache CLI filters heavily depend on bit depth size, so they
need to be duplicated for each value. This means having to rename these
filters, and adjust the callers to use the right version.
Unfortunately the threaded input CLI module inherits a common.h dependency
(input/frame -> common/threadpool -> common/frame -> common/common) which
is extremely complicated to address in a sensible way. Instead duplicate
the module and select the appropriate one at run time.
Each bitdepth needs different checkasm compilation rules, so split the main
checkasm target into two executables.
2017-01-06 15:23:38 +01:00
|
|
|
param.i_bitdepth = 8;
|
2014-10-12 18:45:40 +02:00
|
|
|
param.i_csp = X264_CSP_I420;
|
|
|
|
param.i_width = width;
|
|
|
|
param.i_height = height;
|
|
|
|
param.b_vfr_input = 0;
|
|
|
|
param.b_repeat_headers = 1;
|
|
|
|
param.b_annexb = 1;
|
|
|
|
|
|
|
|
/* Apply profile restrictions. */
|
|
|
|
if( x264_param_apply_profile( ¶m, "high" ) < 0 )
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if( x264_picture_alloc( &pic, param.i_csp, param.i_width, param.i_height ) < 0 )
|
|
|
|
goto fail;
|
|
|
|
#undef fail
|
|
|
|
#define fail fail2
|
|
|
|
|
|
|
|
h = x264_encoder_open( ¶m );
|
|
|
|
if( !h )
|
|
|
|
goto fail;
|
|
|
|
#undef fail
|
|
|
|
#define fail fail3
|
|
|
|
|
2014-11-13 20:52:00 +01:00
|
|
|
int luma_size = width * height;
|
|
|
|
int chroma_size = luma_size / 4;
|
2014-10-12 18:45:40 +02:00
|
|
|
/* Encode frames */
|
2014-11-13 20:52:00 +01:00
|
|
|
for( ;; i_frame++ )
|
2014-10-12 18:45:40 +02:00
|
|
|
{
|
|
|
|
/* Read input frame */
|
2020-04-05 21:29:32 +02:00
|
|
|
if( fread( pic.img.plane[0], 1, luma_size, stdin ) != (unsigned)luma_size )
|
2014-10-12 18:45:40 +02:00
|
|
|
break;
|
2020-04-05 21:29:32 +02:00
|
|
|
if( fread( pic.img.plane[1], 1, chroma_size, stdin ) != (unsigned)chroma_size )
|
2014-10-12 18:45:40 +02:00
|
|
|
break;
|
2020-04-05 21:29:32 +02:00
|
|
|
if( fread( pic.img.plane[2], 1, chroma_size, stdin ) != (unsigned)chroma_size )
|
2014-10-12 18:45:40 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
pic.i_pts = i_frame;
|
|
|
|
i_frame_size = x264_encoder_encode( h, &nal, &i_nal, &pic, &pic_out );
|
|
|
|
if( i_frame_size < 0 )
|
|
|
|
goto fail;
|
|
|
|
else if( i_frame_size )
|
|
|
|
{
|
|
|
|
if( !fwrite( nal->p_payload, i_frame_size, 1, stdout ) )
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Flush delayed frames */
|
2014-11-13 20:52:00 +01:00
|
|
|
while( x264_encoder_delayed_frames( h ) )
|
2014-10-12 18:45:40 +02:00
|
|
|
{
|
|
|
|
i_frame_size = x264_encoder_encode( h, &nal, &i_nal, NULL, &pic_out );
|
|
|
|
if( i_frame_size < 0 )
|
|
|
|
goto fail;
|
|
|
|
else if( i_frame_size )
|
|
|
|
{
|
|
|
|
if( !fwrite( nal->p_payload, i_frame_size, 1, stdout ) )
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
x264_encoder_close( h );
|
|
|
|
x264_picture_clean( &pic );
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#undef fail
|
|
|
|
fail3:
|
|
|
|
x264_encoder_close( h );
|
|
|
|
fail2:
|
|
|
|
x264_picture_clean( &pic );
|
|
|
|
fail:
|
|
|
|
return -1;
|
|
|
|
}
|