1 /*****************************************************************************
2 * matroska.c: matroska muxer
3 *****************************************************************************
4 * Copyright (C) 2005-2015 x264 project
6 * Authors: Mike Matsnev <mike@haali.su>
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.
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.
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.
22 * This program is also available under a commercial proprietary license.
23 * For more information, contact us at licensing@x264.com.
24 *****************************************************************************/
27 #include "matroska_ebml.h"
33 int width, height, d_width, d_height;
35 int display_size_units;
38 int64_t frame_duration;
41 uint32_t i_timebase_num;
42 uint32_t i_timebase_den;
46 static int open_file( char *psz_filename, hnd_t *p_handle, cli_output_opt_t *opt )
49 mkv_hnd_t *p_mkv = calloc( 1, sizeof(mkv_hnd_t) );
53 p_mkv->w = mk_create_writer( psz_filename );
65 #define STEREO_COUNT 7
66 static const uint8_t stereo_modes[STEREO_COUNT] = {5,9,7,1,3,13,0};
67 static const uint8_t stereo_w_div[STEREO_COUNT] = {1,2,1,2,1,1,1};
68 static const uint8_t stereo_h_div[STEREO_COUNT] = {1,1,2,1,2,1,1};
70 static int set_param( hnd_t handle, x264_param_t *p_param )
72 mkv_hnd_t *p_mkv = handle;
75 if( p_param->i_fps_num > 0 && !p_param->b_vfr_input )
77 p_mkv->frame_duration = (int64_t)p_param->i_fps_den *
78 (int64_t)1000000000 / p_param->i_fps_num;
82 p_mkv->frame_duration = 0;
85 dw = p_mkv->width = p_param->i_width;
86 dh = p_mkv->height = p_param->i_height;
87 p_mkv->display_size_units = DS_PIXELS;
88 p_mkv->stereo_mode = -1;
89 if( p_param->i_frame_packing >= 0 && p_param->i_frame_packing < STEREO_COUNT )
91 p_mkv->stereo_mode = stereo_modes[p_param->i_frame_packing];
92 dw /= stereo_w_div[p_param->i_frame_packing];
93 dh /= stereo_h_div[p_param->i_frame_packing];
95 if( p_param->vui.i_sar_width && p_param->vui.i_sar_height
96 && p_param->vui.i_sar_width != p_param->vui.i_sar_height )
98 if ( p_param->vui.i_sar_width > p_param->vui.i_sar_height ) {
99 dw = dw * p_param->vui.i_sar_width / p_param->vui.i_sar_height;
101 dh = dh * p_param->vui.i_sar_height / p_param->vui.i_sar_width;
104 p_mkv->d_width = (int)dw;
105 p_mkv->d_height = (int)dh;
107 p_mkv->i_timebase_num = p_param->i_timebase_num;
108 p_mkv->i_timebase_den = p_param->i_timebase_den;
113 static int write_headers( hnd_t handle, x264_nal_t *p_nal )
115 mkv_hnd_t *p_mkv = handle;
117 int sps_size = p_nal[0].i_payload - 4;
118 int pps_size = p_nal[1].i_payload - 4;
119 int sei_size = p_nal[2].i_payload;
121 uint8_t *sps = p_nal[0].p_payload + 4;
122 uint8_t *pps = p_nal[1].p_payload + 4;
123 uint8_t *sei = p_nal[2].p_payload;
129 if( !p_mkv->width || !p_mkv->height ||
130 !p_mkv->d_width || !p_mkv->d_height )
133 avcC_len = 5 + 1 + 2 + sps_size + 1 + 2 + pps_size;
134 avcC = malloc( avcC_len );
142 avcC[4] = 0xff; // nalu size length is four bytes
143 avcC[5] = 0xe1; // one sps
145 avcC[6] = sps_size >> 8;
148 memcpy( avcC+8, sps, sps_size );
150 avcC[8+sps_size] = 1; // one pps
151 avcC[9+sps_size] = pps_size >> 8;
152 avcC[10+sps_size] = pps_size;
154 memcpy( avcC+11+sps_size, pps, pps_size );
156 ret = mk_write_header( p_mkv->w, "x264" X264_VERSION, "V_MPEG4/ISO/AVC",
157 avcC, avcC_len, p_mkv->frame_duration, 50000,
158 p_mkv->width, p_mkv->height,
159 p_mkv->d_width, p_mkv->d_height, p_mkv->display_size_units, p_mkv->stereo_mode );
167 if( !p_mkv->b_writing_frame )
169 if( mk_start_frame( p_mkv->w ) < 0 )
171 p_mkv->b_writing_frame = 1;
173 if( mk_add_frame_data( p_mkv->w, sei, sei_size ) < 0 )
176 return sei_size + sps_size + pps_size;
179 static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_t *p_picture )
181 mkv_hnd_t *p_mkv = handle;
183 if( !p_mkv->b_writing_frame )
185 if( mk_start_frame( p_mkv->w ) < 0 )
187 p_mkv->b_writing_frame = 1;
190 if( mk_add_frame_data( p_mkv->w, p_nalu, i_size ) < 0 )
193 int64_t i_stamp = (int64_t)((p_picture->i_pts * 1e9 * p_mkv->i_timebase_num / p_mkv->i_timebase_den) + 0.5);
195 p_mkv->b_writing_frame = 0;
197 if( mk_set_frame_flags( p_mkv->w, i_stamp, p_picture->b_keyframe, p_picture->i_type == X264_TYPE_B ) < 0 )
203 static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest_pts )
205 mkv_hnd_t *p_mkv = handle;
207 int64_t i_last_delta;
209 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;
211 ret = mk_close( p_mkv->w, i_last_delta );
218 const cli_output_t mkv_output = { open_file, set_param, write_headers, write_frame, close_file };