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 int display_size_units;
32 int64_t frame_duration;
35 uint32_t i_timebase_num;
36 uint32_t i_timebase_den;
40 static int open_file( char *psz_filename, hnd_t *p_handle )
46 p_mkv = malloc( sizeof(*p_mkv) );
50 memset( p_mkv, 0, sizeof(*p_mkv) );
52 p_mkv->w = mk_create_writer( psz_filename );
64 static int set_param( hnd_t handle, x264_param_t *p_param )
66 mkv_hnd_t *p_mkv = handle;
69 if( p_param->i_fps_num > 0 && !p_param->b_vfr_input )
71 p_mkv->frame_duration = (int64_t)p_param->i_fps_den *
72 (int64_t)1000000000 / p_param->i_fps_num;
76 p_mkv->frame_duration = 0;
79 p_mkv->width = p_mkv->d_width = p_param->i_width;
80 p_mkv->height = p_mkv->d_height = p_param->i_height;
81 p_mkv->display_size_units = DS_PIXELS;
83 if( p_param->vui.i_sar_width && p_param->vui.i_sar_height
84 && p_param->vui.i_sar_width != p_param->vui.i_sar_height )
86 if ( p_param->vui.i_sar_width > p_param->vui.i_sar_height ) {
87 dw = (int64_t)p_param->i_width * p_param->vui.i_sar_width / p_param->vui.i_sar_height;
88 dh = p_param->i_height;
90 dw = p_param->i_width;
91 dh = (int64_t)p_param->i_height * p_param->vui.i_sar_height / p_param->vui.i_sar_width;
94 p_mkv->d_width = (int)dw;
95 p_mkv->d_height = (int)dh;
98 p_mkv->i_timebase_num = p_param->i_timebase_num;
99 p_mkv->i_timebase_den = p_param->i_timebase_den;
104 static int write_headers( hnd_t handle, x264_nal_t *p_nal )
106 mkv_hnd_t *p_mkv = handle;
108 int sps_size = p_nal[0].i_payload - 4;
109 int pps_size = p_nal[1].i_payload - 4;
110 int sei_size = p_nal[2].i_payload;
112 uint8_t *sps = p_nal[0].p_payload + 4;
113 uint8_t *pps = p_nal[1].p_payload + 4;
114 uint8_t *sei = p_nal[2].p_payload;
120 if( !p_mkv->width || !p_mkv->height ||
121 !p_mkv->d_width || !p_mkv->d_height )
124 avcC_len = 5 + 1 + 2 + sps_size + 1 + 2 + pps_size;
125 avcC = malloc( avcC_len );
133 avcC[4] = 0xff; // nalu size length is four bytes
134 avcC[5] = 0xe1; // one sps
136 avcC[6] = sps_size >> 8;
139 memcpy( avcC+8, sps, sps_size );
141 avcC[8+sps_size] = 1; // one pps
142 avcC[9+sps_size] = pps_size >> 8;
143 avcC[10+sps_size] = pps_size;
145 memcpy( avcC+11+sps_size, pps, pps_size );
147 ret = mk_writeHeader( p_mkv->w, "x264" X264_VERSION, "V_MPEG4/ISO/AVC",
148 avcC, avcC_len, p_mkv->frame_duration, 50000,
149 p_mkv->width, p_mkv->height,
150 p_mkv->d_width, p_mkv->d_height, p_mkv->display_size_units );
158 if( !p_mkv->b_writing_frame )
160 if( mk_start_frame( p_mkv->w ) < 0 )
162 p_mkv->b_writing_frame = 1;
164 if( mk_add_frame_data( p_mkv->w, sei, sei_size ) < 0 )
167 return sei_size + sps_size + pps_size;
170 static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_t *p_picture )
172 mkv_hnd_t *p_mkv = handle;
174 if( !p_mkv->b_writing_frame )
176 if( mk_start_frame( p_mkv->w ) < 0 )
178 p_mkv->b_writing_frame = 1;
181 if( mk_add_frame_data( p_mkv->w, p_nalu, i_size ) < 0 )
184 int64_t i_stamp = (int64_t)((p_picture->i_pts * 1e9 * p_mkv->i_timebase_num / p_mkv->i_timebase_den) + 0.5);
186 p_mkv->b_writing_frame = 0;
188 if( mk_set_frame_flags( p_mkv->w, i_stamp, p_picture->b_keyframe, p_picture->i_type == X264_TYPE_B ) < 0 )
194 static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest_pts )
196 mkv_hnd_t *p_mkv = handle;
198 int64_t i_last_delta;
200 i_last_delta = (int64_t)(((largest_pts - second_largest_pts) * p_mkv->i_timebase_num / p_mkv->i_timebase_den) + 0.5);
202 ret = mk_close( p_mkv->w, i_last_delta );
209 const cli_output_t mkv_output = { open_file, set_param, write_headers, write_frame, close_file };