]> git.sesse.net Git - x264/blob - output/matroska.c
Periodic intra refresh
[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 "muxers.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     int64_t frame_duration;
31
32     char b_writing_frame;
33     int i_timebase_num;
34     int i_timebase_den;
35
36 } mkv_hnd_t;
37
38 static int open_file( char *psz_filename, hnd_t *p_handle )
39 {
40     mkv_hnd_t *p_mkv;
41
42     *p_handle = NULL;
43
44     p_mkv  = malloc( sizeof(*p_mkv) );
45     if( !p_mkv )
46         return -1;
47
48     memset( p_mkv, 0, sizeof(*p_mkv) );
49
50     p_mkv->w = mk_create_writer( psz_filename );
51     if( !p_mkv->w )
52     {
53         free( p_mkv );
54         return -1;
55     }
56
57     *p_handle = p_mkv;
58
59     return 0;
60 }
61
62 static int set_param( hnd_t handle, x264_param_t *p_param )
63 {
64     mkv_hnd_t   *p_mkv = handle;
65     int64_t dw, dh;
66
67     if( p_param->i_fps_num > 0 && !p_param->b_vfr_input )
68     {
69         p_mkv->frame_duration = (int64_t)p_param->i_fps_den *
70                                 (int64_t)1000000000 / p_param->i_fps_num;
71     }
72     else
73     {
74         p_mkv->frame_duration = 0;
75     }
76
77     p_mkv->width = p_param->i_width;
78     p_mkv->height = p_param->i_height;
79
80     if( p_param->vui.i_sar_width && p_param->vui.i_sar_height )
81     {
82         dw = (int64_t)p_param->i_width  * p_param->vui.i_sar_width;
83         dh = (int64_t)p_param->i_height * p_param->vui.i_sar_height;
84     }
85     else
86     {
87         dw = p_param->i_width;
88         dh = p_param->i_height;
89     }
90
91     if( dw > 0 && dh > 0 )
92     {
93         int64_t x = gcd( dw, dh );
94         dw /= x;
95         dh /= x;
96     }
97
98     p_mkv->d_width = (int)dw;
99     p_mkv->d_height = (int)dh;
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 sei_size = p_nal[0].i_payload;
111     int sps_size = p_nal[1].i_payload - 4;
112     int pps_size = p_nal[2].i_payload - 4;
113
114     uint8_t *sei = p_nal[0].p_payload;
115     uint8_t *sps = p_nal[1].p_payload + 4;
116     uint8_t *pps = p_nal[2].p_payload + 4;
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_writeHeader( p_mkv->w, "x264", "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 );
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 ) < 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 cli_output_t mkv_output = { open_file, set_param, write_headers, write_frame, close_file };