example.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*****************************************************************************
  2. * example.c: libx264 API usage example
  3. *****************************************************************************
  4. * Copyright (C) 2014-2018 x264 project
  5. *
  6. * Authors: Anton Mitrofanov <BugMaster@narod.ru>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  21. *
  22. * This program is also available under a commercial proprietary license.
  23. * For more information, contact us at licensing@x264.com.
  24. *****************************************************************************/
  25. #ifdef _WIN32
  26. #include <io.h> /* _setmode() */
  27. #include <fcntl.h> /* _O_BINARY */
  28. #endif
  29. #include <stdint.h>
  30. #include <stdio.h>
  31. #include <x264.h>
  32. #define FAIL_IF_ERROR( cond, ... )\
  33. do\
  34. {\
  35. if( cond )\
  36. {\
  37. fprintf( stderr, __VA_ARGS__ );\
  38. goto fail;\
  39. }\
  40. } while( 0 )
  41. int main( int argc, char **argv )
  42. {
  43. int width, height;
  44. x264_param_t param;
  45. x264_picture_t pic;
  46. x264_picture_t pic_out;
  47. x264_t *h;
  48. int i_frame = 0;
  49. int i_frame_size;
  50. x264_nal_t *nal;
  51. int i_nal;
  52. #ifdef _WIN32
  53. _setmode( _fileno( stdin ), _O_BINARY );
  54. _setmode( _fileno( stdout ), _O_BINARY );
  55. _setmode( _fileno( stderr ), _O_BINARY );
  56. #endif
  57. FAIL_IF_ERROR( !(argc > 1), "Example usage: example 352x288 <input.yuv >output.h264\n" );
  58. FAIL_IF_ERROR( 2 != sscanf( argv[1], "%dx%d", &width, &height ), "resolution not specified or incorrect\n" );
  59. /* Get default params for preset/tuning */
  60. if( x264_param_default_preset( &param, "medium", NULL ) < 0 )
  61. goto fail;
  62. /* Configure non-default params */
  63. param.i_bitdepth = 8;
  64. param.i_csp = X264_CSP_I420;
  65. param.i_width = width;
  66. param.i_height = height;
  67. param.b_vfr_input = 0;
  68. param.b_repeat_headers = 1;
  69. param.b_annexb = 1;
  70. /* Apply profile restrictions. */
  71. if( x264_param_apply_profile( &param, "high" ) < 0 )
  72. goto fail;
  73. if( x264_picture_alloc( &pic, param.i_csp, param.i_width, param.i_height ) < 0 )
  74. goto fail;
  75. #undef fail
  76. #define fail fail2
  77. h = x264_encoder_open( &param );
  78. if( !h )
  79. goto fail;
  80. #undef fail
  81. #define fail fail3
  82. int luma_size = width * height;
  83. int chroma_size = luma_size / 4;
  84. /* Encode frames */
  85. for( ;; i_frame++ )
  86. {
  87. /* Read input frame */
  88. if( fread( pic.img.plane[0], 1, luma_size, stdin ) != luma_size )
  89. break;
  90. if( fread( pic.img.plane[1], 1, chroma_size, stdin ) != chroma_size )
  91. break;
  92. if( fread( pic.img.plane[2], 1, chroma_size, stdin ) != chroma_size )
  93. break;
  94. pic.i_pts = i_frame;
  95. i_frame_size = x264_encoder_encode( h, &nal, &i_nal, &pic, &pic_out );
  96. if( i_frame_size < 0 )
  97. goto fail;
  98. else if( i_frame_size )
  99. {
  100. if( !fwrite( nal->p_payload, i_frame_size, 1, stdout ) )
  101. goto fail;
  102. }
  103. }
  104. /* Flush delayed frames */
  105. while( x264_encoder_delayed_frames( h ) )
  106. {
  107. i_frame_size = x264_encoder_encode( h, &nal, &i_nal, NULL, &pic_out );
  108. if( i_frame_size < 0 )
  109. goto fail;
  110. else if( i_frame_size )
  111. {
  112. if( !fwrite( nal->p_payload, i_frame_size, 1, stdout ) )
  113. goto fail;
  114. }
  115. }
  116. x264_encoder_close( h );
  117. x264_picture_clean( &pic );
  118. return 0;
  119. #undef fail
  120. fail3:
  121. x264_encoder_close( h );
  122. fail2:
  123. x264_picture_clean( &pic );
  124. fail:
  125. return -1;
  126. }