]> git.sesse.net Git - x264/blob - output/matroska.c
Bump dates to 2015
[x264] / output / matroska.c
1 /*****************************************************************************
2  * matroska.c: matroska muxer
3  *****************************************************************************
4  * Copyright (C) 2005-2015 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 static int set_param( hnd_t handle, x264_param_t *p_param )
66 {
67     mkv_hnd_t   *p_mkv = handle;
68     int64_t dw, dh;
69
70     if( p_param->i_fps_num > 0 && !p_param->b_vfr_input )
71     {
72         p_mkv->frame_duration = (int64_t)p_param->i_fps_den *
73                                 (int64_t)1000000000 / p_param->i_fps_num;
74     }
75     else
76     {
77         p_mkv->frame_duration = 0;
78     }
79
80     p_mkv->width = p_mkv->d_width = p_param->i_width;
81     p_mkv->height = p_mkv->d_height = p_param->i_height;
82     p_mkv->display_size_units = DS_PIXELS;
83     p_mkv->stereo_mode = p_param->i_frame_packing;
84
85     if( p_param->vui.i_sar_width && p_param->vui.i_sar_height
86         && p_param->vui.i_sar_width != p_param->vui.i_sar_height )
87     {
88         if ( p_param->vui.i_sar_width > p_param->vui.i_sar_height ) {
89             dw = (int64_t)p_param->i_width * p_param->vui.i_sar_width / p_param->vui.i_sar_height;
90             dh = p_param->i_height;
91         } else {
92             dw = p_param->i_width;
93             dh = (int64_t)p_param->i_height * p_param->vui.i_sar_height / p_param->vui.i_sar_width;
94         }
95
96         p_mkv->d_width = (int)dw;
97         p_mkv->d_height = (int)dh;
98     }
99
100     p_mkv->i_timebase_num = p_param->i_timebase_num;
101     p_mkv->i_timebase_den = p_param->i_timebase_den;
102
103     return 0;
104 }
105
106 static int write_headers( hnd_t handle, x264_nal_t *p_nal )
107 {
108     mkv_hnd_t *p_mkv = handle;
109
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;
113
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;
117
118     int ret;
119     uint8_t *avcC;
120     int avcC_len;
121
122     if( !p_mkv->width || !p_mkv->height ||
123         !p_mkv->d_width || !p_mkv->d_height )
124         return -1;
125
126     avcC_len = 5 + 1 + 2 + sps_size + 1 + 2 + pps_size;
127     avcC = malloc( avcC_len );
128     if( !avcC )
129         return -1;
130
131     avcC[0] = 1;
132     avcC[1] = sps[1];
133     avcC[2] = sps[2];
134     avcC[3] = sps[3];
135     avcC[4] = 0xff; // nalu size length is four bytes
136     avcC[5] = 0xe1; // one sps
137
138     avcC[6] = sps_size >> 8;
139     avcC[7] = sps_size;
140
141     memcpy( avcC+8, sps, sps_size );
142
143     avcC[8+sps_size] = 1; // one pps
144     avcC[9+sps_size] = pps_size >> 8;
145     avcC[10+sps_size] = pps_size;
146
147     memcpy( avcC+11+sps_size, pps, pps_size );
148
149     ret = mk_write_header( 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, p_mkv->display_size_units, p_mkv->stereo_mode );
153     if( ret < 0 )
154         return ret;
155
156     free( avcC );
157
158     // SEI
159
160     if( !p_mkv->b_writing_frame )
161     {
162         if( mk_start_frame( p_mkv->w ) < 0 )
163             return -1;
164         p_mkv->b_writing_frame = 1;
165     }
166     if( mk_add_frame_data( p_mkv->w, sei, sei_size ) < 0 )
167         return -1;
168
169     return sei_size + sps_size + pps_size;
170 }
171
172 static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_t *p_picture )
173 {
174     mkv_hnd_t *p_mkv = handle;
175
176     if( !p_mkv->b_writing_frame )
177     {
178         if( mk_start_frame( p_mkv->w ) < 0 )
179             return -1;
180         p_mkv->b_writing_frame = 1;
181     }
182
183     if( mk_add_frame_data( p_mkv->w, p_nalu, i_size ) < 0 )
184         return -1;
185
186     int64_t i_stamp = (int64_t)((p_picture->i_pts * 1e9 * p_mkv->i_timebase_num / p_mkv->i_timebase_den) + 0.5);
187
188     p_mkv->b_writing_frame = 0;
189
190     if( mk_set_frame_flags( p_mkv->w, i_stamp, p_picture->b_keyframe, p_picture->i_type == X264_TYPE_B ) < 0 )
191         return -1;
192
193     return i_size;
194 }
195
196 static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest_pts )
197 {
198     mkv_hnd_t *p_mkv = handle;
199     int ret;
200     int64_t i_last_delta;
201
202     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;
203
204     ret = mk_close( p_mkv->w, i_last_delta );
205
206     free( p_mkv );
207
208     return ret;
209 }
210
211 const cli_output_t mkv_output = { open_file, set_param, write_headers, write_frame, close_file };