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