[x264-devel] libx264 API usage example

Anton Mitrofanov git at videolan.org
Thu Nov 13 13:52:05 CET 2014


x264 | branch: master | Anton Mitrofanov <BugMaster at narod.ru> | Sun Oct 12 20:45:40 2014 +0400| [9bec6fed6d1b95f9921f22ba21e7398eff50b75e] | committer: Fiona

libx264 API usage example

> http://git.videolan.org/gitweb.cgi/x264.git/?a=commit;h=9bec6fed6d1b95f9921f22ba21e7398eff50b75e
---

 Makefile  |   11 ++++-
 example.c |  155 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 164 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index 171b46d..fd72fcd 100644
--- a/Makefile
+++ b/Makefile
@@ -36,6 +36,8 @@ OBJCLI =
 
 OBJCHK = tools/checkasm.o
 
+OBJEXAMPLE = example.o
+
 CONFIG := $(shell cat config.h)
 
 # GPL-only files
@@ -176,9 +178,10 @@ $(SONAME): $(GENERATED) .depend $(OBJS) $(OBJASM) $(OBJSO)
 	$(LD)$@ $(OBJS) $(OBJASM) $(OBJSO) $(SOFLAGS) $(LDFLAGS)
 
 ifneq ($(EXE),)
-.PHONY: x264 checkasm
+.PHONY: x264 checkasm example
 x264: x264$(EXE)
 checkasm: checkasm$(EXE)
+example: example$(EXE)
 endif
 
 x264$(EXE): $(GENERATED) .depend $(OBJCLI) $(CLI_LIBX264)
@@ -187,7 +190,10 @@ x264$(EXE): $(GENERATED) .depend $(OBJCLI) $(CLI_LIBX264)
 checkasm$(EXE): $(GENERATED) .depend $(OBJCHK) $(LIBX264)
 	$(LD)$@ $(OBJCHK) $(LIBX264) $(LDFLAGS)
 
-$(OBJS) $(OBJASM) $(OBJSO) $(OBJCLI) $(OBJCHK): .depend
+example$(EXE): $(GENERATED) .depend $(OBJEXAMPLE) $(LIBX264)
+	$(LD)$@ $(OBJEXAMPLE) $(LIBX264) $(LDFLAGS)
+
+$(OBJS) $(OBJASM) $(OBJSO) $(OBJCLI) $(OBJCHK) $(OBJEXAMPLE): .depend
 
 %.o: %.asm common/x86/x86inc.asm common/x86/x86util.asm
 	$(AS) $(ASFLAGS) -o $@ $<
@@ -254,6 +260,7 @@ endif
 clean:
 	rm -f $(OBJS) $(OBJASM) $(OBJCLI) $(OBJSO) $(SONAME) *.a *.lib *.exp *.pdb x264 x264.exe .depend TAGS
 	rm -f checkasm checkasm.exe $(OBJCHK) $(GENERATED) x264_lookahead.clbin
+	rm -f example example.exe $(OBJEXAMPLE)
 	rm -f $(SRC2:%.c=%.gcda) $(SRC2:%.c=%.gcno) *.dyn pgopti.dpi pgopti.dpi.lock *.pgd *.pgc
 
 distclean: clean
diff --git a/example.c b/example.c
new file mode 100644
index 0000000..385e3c6
--- /dev/null
+++ b/example.c
@@ -0,0 +1,155 @@
+/*****************************************************************************
+ * example.c: libx264 API usage example
+ *****************************************************************************
+ * Copyright (C) 2014 x264 project
+ *
+ * Authors: Anton Mitrofanov <BugMaster at 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 at x264.com.
+ *****************************************************************************/
+
+#ifdef _WIN32
+/* The following two defines must be located before the inclusion of any system header files. */
+#define WINVER       0x0500
+#define _WIN32_WINNT 0x0500
+#include <windows.h>
+#include <io.h>       /* _setmode() */
+#include <fcntl.h>    /* _O_BINARY */
+#endif
+
+#include <stdint.h>
+#include <stdio.h>
+#include <signal.h>
+#include <x264.h>
+
+/* Ctrl-C handler */
+static volatile int b_ctrl_c = 0;
+static void sigint_handler( int a )
+{
+    b_ctrl_c = 1;
+}
+
+#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
+
+    /* Control-C handler */
+    signal( SIGINT, sigint_handler );
+
+    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( &param, "medium", NULL ) < 0 )
+        goto fail;
+
+    /* Configure non-default params */
+    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( &param, "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( &param );
+    if( !h )
+        goto fail;
+#undef fail
+#define fail fail3
+
+    /* Encode frames */
+    for( ; !b_ctrl_c; i_frame++ )
+    {
+        /* Read input frame */
+        int plane_size = width * height;
+        if( fread( pic.img.plane[0], 1, plane_size, stdin ) != plane_size )
+            break;
+        plane_size = ((width + 1) >> 1) * ((height + 1) >> 1);
+        if( fread( pic.img.plane[1], 1, plane_size, stdin ) != plane_size )
+            break;
+        if( fread( pic.img.plane[2], 1, plane_size, stdin ) != plane_size )
+            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 */
+    while( !b_ctrl_c && x264_encoder_delayed_frames( h ) )
+    {
+        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;
+}



More information about the x264-devel mailing list