1 /*****************************************************************************
2 * matroska.c: matroska muxer
3 *****************************************************************************
4 * Copyright (C) 2005-2011 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;
37 int64_t frame_duration;
40 uint32_t i_timebase_num;
41 uint32_t i_timebase_den;
45 static int open_file( char *psz_filename, hnd_t *p_handle, cli_output_opt_t *opt )
51 p_mkv = malloc( sizeof(*p_mkv) );
55 memset( p_mkv, 0, sizeof(*p_mkv) );
57 p_mkv->w = mk_create_writer( psz_filename );
69 static int set_param( hnd_t handle, x264_param_t *p_param )
71 mkv_hnd_t *p_mkv = handle;
74 if( p_param->i_fps_num > 0 && !p_param->b_vfr_input )
76 p_mkv->frame_duration = (int64_t)p_param->i_fps_den *
77 (int64_t)1000000000 / p_param->i_fps_num;
81 p_mkv->frame_duration = 0;
84 p_mkv->width = p_mkv->d_width = p_param->i_width;
85 p_mkv->height = p_mkv->d_height = p_param->i_height;
86 p_mkv->display_size_units = DS_PIXELS;
88 if( p_param->vui.i_sar_width && p_param->vui.i_sar_height
89 && p_param->vui.i_sar_width != p_param->vui.i_sar_height )
91 if ( p_param->vui.i_sar_width > p_param->vui.i_sar_height ) {
92 dw = (int64_t)p_param->i_width * p_param->vui.i_sar_width / p_param->vui.i_sar_height;
93 dh = p_param->i_height;
95 dw = p_param->i_width;
96 dh = (int64_t)p_param->i_height * p_param->vui.i_sar_height / p_param->vui.i_sar_width;
99 p_mkv->d_width = (int)dw;
100 p_mkv->d_height = (int)dh;
103 p_mkv->i_timebase_num = p_param->i_timebase_num;
104 p_mkv->i_timebase_den = p_param->i_timebase_den;
109 static int write_headers( hnd_t handle, x264_nal_t *p_nal )
111 mkv_hnd_t *p_mkv = handle;
113 int sps_size = p_nal[0].i_payload - 4;
114 int pps_size = p_nal[1].i_payload - 4;
115 int sei_size = p_nal[2].i_payload;
117 uint8_t *sps = p_nal[0].p_payload + 4;
118 uint8_t *pps = p_nal[1].p_payload + 4;
119 uint8_t *sei = p_nal[2].p_payload;
125 if( !p_mkv->width || !p_mkv->height ||
126 !p_mkv->d_width || !p_mkv->d_height )
129 avcC_len = 5 + 1 + 2 + sps_size + 1 + 2 + pps_size;
130 avcC = malloc( avcC_len );
138 avcC[4] = 0xff; // nalu size length is four bytes
139 avcC[5] = 0xe1; // one sps
141 avcC[6] = sps_size >> 8;
144 memcpy( avcC+8, sps, sps_size );
146 avcC[8+sps_size] = 1; // one pps
147 avcC[9+sps_size] = pps_size >> 8;
148 avcC[10+sps_size] = pps_size;
150 memcpy( avcC+11+sps_size, pps, pps_size );
152 ret = mk_write_header( p_mkv->w, "x264" X264_VERSION, "V_MPEG4/ISO/AVC",
153 avcC, avcC_len, p_mkv->frame_duration, 50000,
154 p_mkv->width, p_mkv->height,
155 p_mkv->d_width, p_mkv->d_height, p_mkv->display_size_units );
163 if( !p_mkv->b_writing_frame )
165 if( mk_start_frame( p_mkv->w ) < 0 )
167 p_mkv->b_writing_frame = 1;
169 if( mk_add_frame_data( p_mkv->w, sei, sei_size ) < 0 )
172 return sei_size + sps_size + pps_size;
175 static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_t *p_picture )
177 mkv_hnd_t *p_mkv = handle;
179 if( !p_mkv->b_writing_frame )
181 if( mk_start_frame( p_mkv->w ) < 0 )
183 p_mkv->b_writing_frame = 1;
186 if( mk_add_frame_data( p_mkv->w, p_nalu, i_size ) < 0 )
189 int64_t i_stamp = (int64_t)((p_picture->i_pts * 1e9 * p_mkv->i_timebase_num / p_mkv->i_timebase_den) + 0.5);
191 p_mkv->b_writing_frame = 0;
193 if( mk_set_frame_flags( p_mkv->w, i_stamp, p_picture->b_keyframe, p_picture->i_type == X264_TYPE_B ) < 0 )
199 static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest_pts )
201 mkv_hnd_t *p_mkv = handle;
203 int64_t i_last_delta;
205 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;
207 ret = mk_close( p_mkv->w, i_last_delta );
214 const cli_output_t mkv_output = { open_file, set_param, write_headers, write_frame, close_file };