]> git.sesse.net Git - x264/blob - output/matroska.c
windows: Flag debug builds in the resource file
[x264] / output / matroska.c
1 /*****************************************************************************
2  * matroska.c: matroska muxer
3  *****************************************************************************
4  * Copyright (C) 2005-2016 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
26 #include "output.h"
27 #include "matroska_ebml.h"
28
29 typedef struct
30 {
31     mk_writer *w;
32
33     int width, height, d_width, d_height;
34
35     int display_size_units;
36     int stereo_mode;
37
38     int64_t frame_duration;
39
40     char b_writing_frame;
41     uint32_t i_timebase_num;
42     uint32_t i_timebase_den;
43
44 } mkv_hnd_t;
45
46 static int open_file( char *psz_filename, hnd_t *p_handle, cli_output_opt_t *opt )
47 {
48     *p_handle = NULL;
49     mkv_hnd_t *p_mkv = calloc( 1, sizeof(mkv_hnd_t) );
50     if( !p_mkv )
51         return -1;
52
53     p_mkv->w = mk_create_writer( psz_filename );
54     if( !p_mkv->w )
55     {
56         free( p_mkv );
57         return -1;
58     }
59
60     *p_handle = p_mkv;
61
62     return 0;
63 }
64
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};
69
70 static int set_param( hnd_t handle, x264_param_t *p_param )
71 {
72     mkv_hnd_t *p_mkv = handle;
73     int64_t dw, dh;
74
75     if( p_param->i_fps_num > 0 && !p_param->b_vfr_input )
76     {
77         p_mkv->frame_duration = (int64_t)p_param->i_fps_den *
78                                 (int64_t)1000000000 / p_param->i_fps_num;
79     }
80     else
81     {
82         p_mkv->frame_duration = 0;
83     }
84
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 )
90     {
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];
94     }
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 )
97     {
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;
100         } else {
101             dh = dh * p_param->vui.i_sar_height / p_param->vui.i_sar_width;
102         }
103     }
104     p_mkv->d_width = (int)dw;
105     p_mkv->d_height = (int)dh;
106
107     p_mkv->i_timebase_num = p_param->i_timebase_num;
108     p_mkv->i_timebase_den = p_param->i_timebase_den;
109
110     return 0;
111 }
112
113 static int write_headers( hnd_t handle, x264_nal_t *p_nal )
114 {
115     mkv_hnd_t *p_mkv = handle;
116
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;
120
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;
124
125     int ret;
126     uint8_t *avcC;
127     int avcC_len;
128
129     if( !p_mkv->width || !p_mkv->height ||
130         !p_mkv->d_width || !p_mkv->d_height )
131         return -1;
132
133     avcC_len = 5 + 1 + 2 + sps_size + 1 + 2 + pps_size;
134     avcC = malloc( avcC_len );
135     if( !avcC )
136         return -1;
137
138     avcC[0] = 1;
139     avcC[1] = sps[1];
140     avcC[2] = sps[2];
141     avcC[3] = sps[3];
142     avcC[4] = 0xff; // nalu size length is four bytes
143     avcC[5] = 0xe1; // one sps
144
145     avcC[6] = sps_size >> 8;
146     avcC[7] = sps_size;
147
148     memcpy( avcC+8, sps, sps_size );
149
150     avcC[8+sps_size] = 1; // one pps
151     avcC[9+sps_size] = pps_size >> 8;
152     avcC[10+sps_size] = pps_size;
153
154     memcpy( avcC+11+sps_size, pps, pps_size );
155
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 );
160     free( avcC );
161
162     if( ret < 0 )
163         return ret;
164
165     // SEI
166
167     if( !p_mkv->b_writing_frame )
168     {
169         if( mk_start_frame( p_mkv->w ) < 0 )
170             return -1;
171         p_mkv->b_writing_frame = 1;
172     }
173     if( mk_add_frame_data( p_mkv->w, sei, sei_size ) < 0 )
174         return -1;
175
176     return sei_size + sps_size + pps_size;
177 }
178
179 static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_t *p_picture )
180 {
181     mkv_hnd_t *p_mkv = handle;
182
183     if( !p_mkv->b_writing_frame )
184     {
185         if( mk_start_frame( p_mkv->w ) < 0 )
186             return -1;
187         p_mkv->b_writing_frame = 1;
188     }
189
190     if( mk_add_frame_data( p_mkv->w, p_nalu, i_size ) < 0 )
191         return -1;
192
193     int64_t i_stamp = (int64_t)((p_picture->i_pts * 1e9 * p_mkv->i_timebase_num / p_mkv->i_timebase_den) + 0.5);
194
195     p_mkv->b_writing_frame = 0;
196
197     if( mk_set_frame_flags( p_mkv->w, i_stamp, p_picture->b_keyframe, p_picture->i_type == X264_TYPE_B ) < 0 )
198         return -1;
199
200     return i_size;
201 }
202
203 static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest_pts )
204 {
205     mkv_hnd_t *p_mkv = handle;
206     int ret;
207     int64_t i_last_delta;
208
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;
210
211     ret = mk_close( p_mkv->w, i_last_delta );
212
213     free( p_mkv );
214
215     return ret;
216 }
217
218 const cli_output_t mkv_output = { open_file, set_param, write_headers, write_frame, close_file };