1 /*****************************************************************************
2 * matroska.c: x264 matroska output module
3 *****************************************************************************
4 * Copyright (C) 2005 Mike Matsnev
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
19 *****************************************************************************/
22 #include "matroska_ebml.h"
28 int width, height, d_width, d_height;
30 int64_t frame_duration;
33 uint32_t i_timebase_num;
34 uint32_t i_timebase_den;
38 static int open_file( char *psz_filename, hnd_t *p_handle )
44 p_mkv = malloc( sizeof(*p_mkv) );
48 memset( p_mkv, 0, sizeof(*p_mkv) );
50 p_mkv->w = mk_create_writer( psz_filename );
62 static int set_param( hnd_t handle, x264_param_t *p_param )
64 mkv_hnd_t *p_mkv = handle;
67 if( p_param->i_fps_num > 0 && !p_param->b_vfr_input )
69 p_mkv->frame_duration = (int64_t)p_param->i_fps_den *
70 (int64_t)1000000000 / p_param->i_fps_num;
74 p_mkv->frame_duration = 0;
77 p_mkv->width = p_param->i_width;
78 p_mkv->height = p_param->i_height;
80 if( p_param->vui.i_sar_width && p_param->vui.i_sar_height )
82 dw = (int64_t)p_param->i_width * p_param->vui.i_sar_width;
83 dh = (int64_t)p_param->i_height * p_param->vui.i_sar_height;
87 dw = p_param->i_width;
88 dh = p_param->i_height;
91 if( dw > 0 && dh > 0 )
93 int64_t x = gcd( dw, dh );
98 p_mkv->d_width = (int)dw;
99 p_mkv->d_height = (int)dh;
100 p_mkv->i_timebase_num = p_param->i_timebase_num;
101 p_mkv->i_timebase_den = p_param->i_timebase_den;
106 static int write_headers( hnd_t handle, x264_nal_t *p_nal )
108 mkv_hnd_t *p_mkv = handle;
110 int sps_size = p_nal[0].i_payload - 4;
111 int pps_size = p_nal[1].i_payload - 4;
112 int sei_size = p_nal[2].i_payload;
114 uint8_t *sps = p_nal[0].p_payload + 4;
115 uint8_t *pps = p_nal[1].p_payload + 4;
116 uint8_t *sei = p_nal[2].p_payload;
122 if( !p_mkv->width || !p_mkv->height ||
123 !p_mkv->d_width || !p_mkv->d_height )
126 avcC_len = 5 + 1 + 2 + sps_size + 1 + 2 + pps_size;
127 avcC = malloc( avcC_len );
135 avcC[4] = 0xff; // nalu size length is four bytes
136 avcC[5] = 0xe1; // one sps
138 avcC[6] = sps_size >> 8;
141 memcpy( avcC+8, sps, sps_size );
143 avcC[8+sps_size] = 1; // one pps
144 avcC[9+sps_size] = pps_size >> 8;
145 avcC[10+sps_size] = pps_size;
147 memcpy( avcC+11+sps_size, pps, pps_size );
149 ret = mk_writeHeader( p_mkv->w, "x264" X264_VERSION, "V_MPEG4/ISO/AVC",
150 avcC, avcC_len, p_mkv->frame_duration, 50000,
151 p_mkv->width, p_mkv->height,
152 p_mkv->d_width, p_mkv->d_height );
160 if( !p_mkv->b_writing_frame )
162 if( mk_start_frame( p_mkv->w ) < 0 )
164 p_mkv->b_writing_frame = 1;
166 if( mk_add_frame_data( p_mkv->w, sei, sei_size ) < 0 )
169 return sei_size + sps_size + pps_size;
172 static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_t *p_picture )
174 mkv_hnd_t *p_mkv = handle;
176 if( !p_mkv->b_writing_frame )
178 if( mk_start_frame( p_mkv->w ) < 0 )
180 p_mkv->b_writing_frame = 1;
183 if( mk_add_frame_data( p_mkv->w, p_nalu, i_size ) < 0 )
186 int64_t i_stamp = (int64_t)((p_picture->i_pts * 1e9 * p_mkv->i_timebase_num / p_mkv->i_timebase_den) + 0.5);
188 p_mkv->b_writing_frame = 0;
190 if( mk_set_frame_flags( p_mkv->w, i_stamp, p_picture->b_keyframe, p_picture->i_type == X264_TYPE_B ) < 0 )
196 static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest_pts )
198 mkv_hnd_t *p_mkv = handle;
200 int64_t i_last_delta;
202 i_last_delta = (int64_t)(((largest_pts - second_largest_pts) * p_mkv->i_timebase_num / p_mkv->i_timebase_den) + 0.5);
204 ret = mk_close( p_mkv->w, i_last_delta );
211 const cli_output_t mkv_output = { open_file, set_param, write_headers, write_frame, close_file };