matroska.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*****************************************************************************
  2. * matroska.c: matroska muxer
  3. *****************************************************************************
  4. * Copyright (C) 2005-2018 x264 project
  5. *
  6. * Authors: Mike Matsnev <mike@haali.su>
  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. #include "output.h"
  26. #include "matroska_ebml.h"
  27. typedef struct
  28. {
  29. mk_writer *w;
  30. int width, height, d_width, d_height;
  31. int display_size_units;
  32. int stereo_mode;
  33. int64_t frame_duration;
  34. char b_writing_frame;
  35. uint32_t i_timebase_num;
  36. uint32_t i_timebase_den;
  37. } mkv_hnd_t;
  38. static int open_file( char *psz_filename, hnd_t *p_handle, cli_output_opt_t *opt )
  39. {
  40. *p_handle = NULL;
  41. mkv_hnd_t *p_mkv = calloc( 1, sizeof(mkv_hnd_t) );
  42. if( !p_mkv )
  43. return -1;
  44. p_mkv->w = mk_create_writer( psz_filename );
  45. if( !p_mkv->w )
  46. {
  47. free( p_mkv );
  48. return -1;
  49. }
  50. *p_handle = p_mkv;
  51. return 0;
  52. }
  53. #define STEREO_COUNT 7
  54. static const uint8_t stereo_modes[STEREO_COUNT] = {5,9,7,1,3,13,0};
  55. static const uint8_t stereo_w_div[STEREO_COUNT] = {1,2,1,2,1,1,1};
  56. static const uint8_t stereo_h_div[STEREO_COUNT] = {1,1,2,1,2,1,1};
  57. static int set_param( hnd_t handle, x264_param_t *p_param )
  58. {
  59. mkv_hnd_t *p_mkv = handle;
  60. int64_t dw, dh;
  61. if( p_param->i_fps_num > 0 && !p_param->b_vfr_input )
  62. {
  63. p_mkv->frame_duration = (int64_t)p_param->i_fps_den *
  64. (int64_t)1000000000 / p_param->i_fps_num;
  65. }
  66. else
  67. {
  68. p_mkv->frame_duration = 0;
  69. }
  70. dw = p_mkv->width = p_param->i_width;
  71. dh = p_mkv->height = p_param->i_height;
  72. p_mkv->display_size_units = DS_PIXELS;
  73. p_mkv->stereo_mode = -1;
  74. if( p_param->i_frame_packing >= 0 && p_param->i_frame_packing < STEREO_COUNT )
  75. {
  76. p_mkv->stereo_mode = stereo_modes[p_param->i_frame_packing];
  77. dw /= stereo_w_div[p_param->i_frame_packing];
  78. dh /= stereo_h_div[p_param->i_frame_packing];
  79. }
  80. if( p_param->vui.i_sar_width && p_param->vui.i_sar_height
  81. && p_param->vui.i_sar_width != p_param->vui.i_sar_height )
  82. {
  83. if( p_param->vui.i_sar_width > p_param->vui.i_sar_height )
  84. {
  85. dw = dw * p_param->vui.i_sar_width / p_param->vui.i_sar_height;
  86. }
  87. else
  88. {
  89. dh = dh * p_param->vui.i_sar_height / p_param->vui.i_sar_width;
  90. }
  91. }
  92. p_mkv->d_width = (int)dw;
  93. p_mkv->d_height = (int)dh;
  94. p_mkv->i_timebase_num = p_param->i_timebase_num;
  95. p_mkv->i_timebase_den = p_param->i_timebase_den;
  96. return 0;
  97. }
  98. static int write_headers( hnd_t handle, x264_nal_t *p_nal )
  99. {
  100. mkv_hnd_t *p_mkv = handle;
  101. int sps_size = p_nal[0].i_payload - 4;
  102. int pps_size = p_nal[1].i_payload - 4;
  103. int sei_size = p_nal[2].i_payload;
  104. uint8_t *sps = p_nal[0].p_payload + 4;
  105. uint8_t *pps = p_nal[1].p_payload + 4;
  106. uint8_t *sei = p_nal[2].p_payload;
  107. int ret;
  108. uint8_t *avcC;
  109. int avcC_len;
  110. if( !p_mkv->width || !p_mkv->height ||
  111. !p_mkv->d_width || !p_mkv->d_height )
  112. return -1;
  113. avcC_len = 5 + 1 + 2 + sps_size + 1 + 2 + pps_size;
  114. avcC = malloc( avcC_len );
  115. if( !avcC )
  116. return -1;
  117. avcC[0] = 1;
  118. avcC[1] = sps[1];
  119. avcC[2] = sps[2];
  120. avcC[3] = sps[3];
  121. avcC[4] = 0xff; // nalu size length is four bytes
  122. avcC[5] = 0xe1; // one sps
  123. avcC[6] = sps_size >> 8;
  124. avcC[7] = sps_size;
  125. memcpy( avcC+8, sps, sps_size );
  126. avcC[8+sps_size] = 1; // one pps
  127. avcC[9+sps_size] = pps_size >> 8;
  128. avcC[10+sps_size] = pps_size;
  129. memcpy( avcC+11+sps_size, pps, pps_size );
  130. ret = mk_write_header( p_mkv->w, "x264" X264_VERSION, "V_MPEG4/ISO/AVC",
  131. avcC, avcC_len, p_mkv->frame_duration, 50000,
  132. p_mkv->width, p_mkv->height,
  133. p_mkv->d_width, p_mkv->d_height, p_mkv->display_size_units, p_mkv->stereo_mode );
  134. free( avcC );
  135. if( ret < 0 )
  136. return ret;
  137. // SEI
  138. if( !p_mkv->b_writing_frame )
  139. {
  140. if( mk_start_frame( p_mkv->w ) < 0 )
  141. return -1;
  142. p_mkv->b_writing_frame = 1;
  143. }
  144. if( mk_add_frame_data( p_mkv->w, sei, sei_size ) < 0 )
  145. return -1;
  146. return sei_size + sps_size + pps_size;
  147. }
  148. static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_t *p_picture )
  149. {
  150. mkv_hnd_t *p_mkv = handle;
  151. if( !p_mkv->b_writing_frame )
  152. {
  153. if( mk_start_frame( p_mkv->w ) < 0 )
  154. return -1;
  155. p_mkv->b_writing_frame = 1;
  156. }
  157. if( mk_add_frame_data( p_mkv->w, p_nalu, i_size ) < 0 )
  158. return -1;
  159. int64_t i_stamp = (int64_t)((p_picture->i_pts * 1e9 * p_mkv->i_timebase_num / p_mkv->i_timebase_den) + 0.5);
  160. p_mkv->b_writing_frame = 0;
  161. if( mk_set_frame_flags( p_mkv->w, i_stamp, p_picture->b_keyframe, p_picture->i_type == X264_TYPE_B ) < 0 )
  162. return -1;
  163. return i_size;
  164. }
  165. static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest_pts )
  166. {
  167. mkv_hnd_t *p_mkv = handle;
  168. int ret;
  169. int64_t i_last_delta;
  170. i_last_delta = p_mkv->i_timebase_den ? (int64_t)(((largest_pts - second_largest_pts) * p_mkv->i_timebase_num / p_mkv->i_timebase_den) + 0.5) : 0;
  171. ret = mk_close( p_mkv->w, i_last_delta );
  172. free( p_mkv );
  173. return ret;
  174. }
  175. const cli_output_t mkv_output = { open_file, set_param, write_headers, write_frame, close_file };