api.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*****************************************************************************
  2. * api.c: bit depth independent interface
  3. *****************************************************************************
  4. * Copyright (C) 2003-2018 x264 project
  5. *
  6. * Authors: Vittorio Giovara <vittorio.giovara@gmail.com>
  7. * Luca Barbato <lu_zero@gentoo.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  22. *
  23. * This program is also available under a commercial proprietary license.
  24. * For more information, contact us at licensing@x264.com.
  25. *****************************************************************************/
  26. #include "common/base.h"
  27. /****************************************************************************
  28. * global symbols
  29. ****************************************************************************/
  30. const int x264_chroma_format = X264_CHROMA_FORMAT;
  31. x264_t *x264_8_encoder_open( x264_param_t * );
  32. void x264_8_nal_encode( x264_t *h, uint8_t *dst, x264_nal_t *nal );
  33. int x264_8_encoder_reconfig( x264_t *, x264_param_t * );
  34. void x264_8_encoder_parameters( x264_t *, x264_param_t * );
  35. int x264_8_encoder_headers( x264_t *, x264_nal_t **pp_nal, int *pi_nal );
  36. int x264_8_encoder_encode( x264_t *, x264_nal_t **pp_nal, int *pi_nal, x264_picture_t *pic_in, x264_picture_t *pic_out );
  37. void x264_8_encoder_close( x264_t * );
  38. int x264_8_encoder_delayed_frames( x264_t * );
  39. int x264_8_encoder_maximum_delayed_frames( x264_t * );
  40. void x264_8_encoder_intra_refresh( x264_t * );
  41. int x264_8_encoder_invalidate_reference( x264_t *, int64_t pts );
  42. x264_t *x264_10_encoder_open( x264_param_t * );
  43. void x264_10_nal_encode( x264_t *h, uint8_t *dst, x264_nal_t *nal );
  44. int x264_10_encoder_reconfig( x264_t *, x264_param_t * );
  45. void x264_10_encoder_parameters( x264_t *, x264_param_t * );
  46. int x264_10_encoder_headers( x264_t *, x264_nal_t **pp_nal, int *pi_nal );
  47. int x264_10_encoder_encode( x264_t *, x264_nal_t **pp_nal, int *pi_nal, x264_picture_t *pic_in, x264_picture_t *pic_out );
  48. void x264_10_encoder_close( x264_t * );
  49. int x264_10_encoder_delayed_frames( x264_t * );
  50. int x264_10_encoder_maximum_delayed_frames( x264_t * );
  51. void x264_10_encoder_intra_refresh( x264_t * );
  52. int x264_10_encoder_invalidate_reference( x264_t *, int64_t pts );
  53. typedef struct x264_api_t
  54. {
  55. /* Internal reference to x264_t data */
  56. x264_t *x264;
  57. /* API entry points */
  58. void (*nal_encode)( x264_t *h, uint8_t *dst, x264_nal_t *nal );
  59. int (*encoder_reconfig)( x264_t *, x264_param_t * );
  60. void (*encoder_parameters)( x264_t *, x264_param_t * );
  61. int (*encoder_headers)( x264_t *, x264_nal_t **pp_nal, int *pi_nal );
  62. int (*encoder_encode)( x264_t *, x264_nal_t **pp_nal, int *pi_nal, x264_picture_t *pic_in, x264_picture_t *pic_out );
  63. void (*encoder_close)( x264_t * );
  64. int (*encoder_delayed_frames)( x264_t * );
  65. int (*encoder_maximum_delayed_frames)( x264_t * );
  66. void (*encoder_intra_refresh)( x264_t * );
  67. int (*encoder_invalidate_reference)( x264_t *, int64_t pts );
  68. } x264_api_t;
  69. static x264_api_t *encoder_open( x264_param_t *param )
  70. {
  71. x264_api_t *api = calloc( 1, sizeof( x264_api_t ) );
  72. if( !api )
  73. return NULL;
  74. if( HAVE_BITDEPTH8 && param->i_bitdepth == 8 )
  75. {
  76. api->nal_encode = x264_8_nal_encode;
  77. api->encoder_reconfig = x264_8_encoder_reconfig;
  78. api->encoder_parameters = x264_8_encoder_parameters;
  79. api->encoder_headers = x264_8_encoder_headers;
  80. api->encoder_encode = x264_8_encoder_encode;
  81. api->encoder_close = x264_8_encoder_close;
  82. api->encoder_delayed_frames = x264_8_encoder_delayed_frames;
  83. api->encoder_maximum_delayed_frames = x264_8_encoder_maximum_delayed_frames;
  84. api->encoder_intra_refresh = x264_8_encoder_intra_refresh;
  85. api->encoder_invalidate_reference = x264_8_encoder_invalidate_reference;
  86. api->x264 = x264_8_encoder_open( param );
  87. }
  88. else if( HAVE_BITDEPTH10 && param->i_bitdepth == 10 )
  89. {
  90. api->nal_encode = x264_10_nal_encode;
  91. api->encoder_reconfig = x264_10_encoder_reconfig;
  92. api->encoder_parameters = x264_10_encoder_parameters;
  93. api->encoder_headers = x264_10_encoder_headers;
  94. api->encoder_encode = x264_10_encoder_encode;
  95. api->encoder_close = x264_10_encoder_close;
  96. api->encoder_delayed_frames = x264_10_encoder_delayed_frames;
  97. api->encoder_maximum_delayed_frames = x264_10_encoder_maximum_delayed_frames;
  98. api->encoder_intra_refresh = x264_10_encoder_intra_refresh;
  99. api->encoder_invalidate_reference = x264_10_encoder_invalidate_reference;
  100. api->x264 = x264_10_encoder_open( param );
  101. }
  102. else
  103. x264_log_internal( X264_LOG_ERROR, "not compiled with %d bit depth support\n", param->i_bitdepth );
  104. if( !api->x264 )
  105. {
  106. free( api );
  107. return NULL;
  108. }
  109. return api;
  110. }
  111. x264_t *x264_encoder_open( x264_param_t *param )
  112. {
  113. /* x264_t is opaque */
  114. return (x264_t *)x264_stack_align( encoder_open, param );
  115. }
  116. void x264_encoder_close( x264_t *h )
  117. {
  118. x264_api_t *api = (x264_api_t *)h;
  119. x264_stack_align( api->encoder_close, api->x264 );
  120. free( api );
  121. }
  122. void x264_nal_encode( x264_t *h, uint8_t *dst, x264_nal_t *nal )
  123. {
  124. x264_api_t *api = (x264_api_t *)h;
  125. x264_stack_align( api->nal_encode, api->x264, dst, nal );
  126. }
  127. int x264_encoder_reconfig( x264_t *h, x264_param_t *param)
  128. {
  129. x264_api_t *api = (x264_api_t *)h;
  130. return x264_stack_align( api->encoder_reconfig, api->x264, param );
  131. }
  132. void x264_encoder_parameters( x264_t *h, x264_param_t *param )
  133. {
  134. x264_api_t *api = (x264_api_t *)h;
  135. x264_stack_align( api->encoder_parameters, api->x264, param );
  136. }
  137. int x264_encoder_headers( x264_t *h, x264_nal_t **pp_nal, int *pi_nal )
  138. {
  139. x264_api_t *api = (x264_api_t *)h;
  140. return x264_stack_align( api->encoder_headers, api->x264, pp_nal, pi_nal );
  141. }
  142. int x264_encoder_encode( x264_t *h, x264_nal_t **pp_nal, int *pi_nal, x264_picture_t *pic_in, x264_picture_t *pic_out )
  143. {
  144. x264_api_t *api = (x264_api_t *)h;
  145. return x264_stack_align( api->encoder_encode, api->x264, pp_nal, pi_nal, pic_in, pic_out );
  146. }
  147. int x264_encoder_delayed_frames( x264_t *h )
  148. {
  149. x264_api_t *api = (x264_api_t *)h;
  150. return x264_stack_align( api->encoder_delayed_frames, api->x264 );
  151. }
  152. int x264_encoder_maximum_delayed_frames( x264_t *h )
  153. {
  154. x264_api_t *api = (x264_api_t *)h;
  155. return x264_stack_align( api->encoder_maximum_delayed_frames, api->x264 );
  156. }
  157. void x264_encoder_intra_refresh( x264_t *h )
  158. {
  159. x264_api_t *api = (x264_api_t *)h;
  160. x264_stack_align( api->encoder_intra_refresh, api->x264 );
  161. }
  162. int x264_encoder_invalidate_reference( x264_t *h, int64_t pts )
  163. {
  164. x264_api_t *api = (x264_api_t *)h;
  165. return x264_stack_align( api->encoder_invalidate_reference, api->x264, pts );
  166. }