]> git.sesse.net Git - x264/blob - output/matroska.c
Fix aspect ratio writing in the MKV muxer
[x264] / output / matroska.c
1 /*****************************************************************************
2  * matroska.c: x264 matroska output module
3  *****************************************************************************
4  * Copyright (C) 2005 Mike Matsnev
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20
21 #include "output.h"
22 #include "matroska_ebml.h"
23
24 typedef struct
25 {
26     mk_writer *w;
27
28     int width, height, d_width, d_height;
29
30     int display_size_units;
31
32     int64_t frame_duration;
33
34     char b_writing_frame;
35     uint32_t i_timebase_num;
36     uint32_t i_timebase_den;
37
38 } mkv_hnd_t;
39
40 static int open_file( char *psz_filename, hnd_t *p_handle )
41 {
42     mkv_hnd_t *p_mkv;
43
44     *p_handle = NULL;
45
46     p_mkv  = malloc( sizeof(*p_mkv) );
47     if( !p_mkv )
48         return -1;
49
50     memset( p_mkv, 0, sizeof(*p_mkv) );
51
52     p_mkv->w = mk_create_writer( psz_filename );
53     if( !p_mkv->w )
54     {
55         free( p_mkv );
56         return -1;
57     }
58
59     *p_handle = p_mkv;
60
61     return 0;
62 }
63
64 static int set_param( hnd_t handle, x264_param_t *p_param )
65 {
66     mkv_hnd_t   *p_mkv = handle;
67     int64_t dw, dh;
68
69     if( p_param->i_fps_num > 0 && !p_param->b_vfr_input )
70     {
71         p_mkv->frame_duration = (int64_t)p_param->i_fps_den *
72                                 (int64_t)1000000000 / p_param->i_fps_num;
73     }
74     else
75     {
76         p_mkv->frame_duration = 0;
77     }
78
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;
82
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 )
85     {
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;
89         } else {
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;
92         }
93
94         p_mkv->d_width = (int)dw;
95         p_mkv->d_height = (int)dh;
96     }
97
98     p_mkv->i_timebase_num = p_param->i_timebase_num;
99     p_mkv->i_timebase_den = p_param->i_timebase_den;
100
101     return 0;
102 }
103
104 static int write_headers( hnd_t handle, x264_nal_t *p_nal )
105 {
106     mkv_hnd_t *p_mkv = handle;
107
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;
111
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;
115
116     int ret;
117     uint8_t *avcC;
118     int avcC_len;
119
120     if( !p_mkv->width || !p_mkv->height ||
121         !p_mkv->d_width || !p_mkv->d_height )
122         return -1;
123
124     avcC_len = 5 + 1 + 2 + sps_size + 1 + 2 + pps_size;
125     avcC = malloc( avcC_len );
126     if( !avcC )
127         return -1;
128
129     avcC[0] = 1;
130     avcC[1] = sps[1];
131     avcC[2] = sps[2];
132     avcC[3] = sps[3];
133     avcC[4] = 0xff; // nalu size length is four bytes
134     avcC[5] = 0xe1; // one sps
135
136     avcC[6] = sps_size >> 8;
137     avcC[7] = sps_size;
138
139     memcpy( avcC+8, sps, sps_size );
140
141     avcC[8+sps_size] = 1; // one pps
142     avcC[9+sps_size] = pps_size >> 8;
143     avcC[10+sps_size] = pps_size;
144
145     memcpy( avcC+11+sps_size, pps, pps_size );
146
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 );
151     if( ret < 0 )
152         return ret;
153
154     free( avcC );
155
156     // SEI
157
158     if( !p_mkv->b_writing_frame )
159     {
160         if( mk_start_frame( p_mkv->w ) < 0 )
161             return -1;
162         p_mkv->b_writing_frame = 1;
163     }
164     if( mk_add_frame_data( p_mkv->w, sei, sei_size ) < 0 )
165         return -1;
166
167     return sei_size + sps_size + pps_size;
168 }
169
170 static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_t *p_picture )
171 {
172     mkv_hnd_t *p_mkv = handle;
173
174     if( !p_mkv->b_writing_frame )
175     {
176         if( mk_start_frame( p_mkv->w ) < 0 )
177             return -1;
178         p_mkv->b_writing_frame = 1;
179     }
180
181     if( mk_add_frame_data( p_mkv->w, p_nalu, i_size ) < 0 )
182         return -1;
183
184     int64_t i_stamp = (int64_t)((p_picture->i_pts * 1e9 * p_mkv->i_timebase_num / p_mkv->i_timebase_den) + 0.5);
185
186     p_mkv->b_writing_frame = 0;
187
188     if( mk_set_frame_flags( p_mkv->w, i_stamp, p_picture->b_keyframe, p_picture->i_type == X264_TYPE_B ) < 0 )
189         return -1;
190
191     return i_size;
192 }
193
194 static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest_pts )
195 {
196     mkv_hnd_t *p_mkv = handle;
197     int ret;
198     int64_t i_last_delta;
199
200     i_last_delta = (int64_t)(((largest_pts - second_largest_pts) * p_mkv->i_timebase_num / p_mkv->i_timebase_den) + 0.5);
201
202     ret = mk_close( p_mkv->w, i_last_delta );
203
204     free( p_mkv );
205
206     return ret;
207 }
208
209 const cli_output_t mkv_output = { open_file, set_param, write_headers, write_frame, close_file };