1 /*****************************************************************************
2  * matroska.c: matroska muxer
3  *****************************************************************************
4  * Copyright (C) 2005-2021 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 
open_file(char * psz_filename,hnd_t * p_handle,cli_output_opt_t * opt)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 
set_param(hnd_t handle,x264_param_t * p_param)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         {
100             dw = dw * p_param->vui.i_sar_width / p_param->vui.i_sar_height;
101         }
102         else
103         {
104             dh = dh * p_param->vui.i_sar_height / p_param->vui.i_sar_width;
105         }
106     }
107     p_mkv->d_width = (int)dw;
108     p_mkv->d_height = (int)dh;
109 
110     p_mkv->i_timebase_num = p_param->i_timebase_num;
111     p_mkv->i_timebase_den = p_param->i_timebase_den;
112 
113     return 0;
114 }
115 
write_headers(hnd_t handle,x264_nal_t * p_nal)116 static int write_headers( hnd_t handle, x264_nal_t *p_nal )
117 {
118     mkv_hnd_t *p_mkv = handle;
119 
120     int sps_size = p_nal[0].i_payload - 4;
121     int pps_size = p_nal[1].i_payload - 4;
122     int sei_size = p_nal[2].i_payload;
123 
124     uint8_t *sps = p_nal[0].p_payload + 4;
125     uint8_t *pps = p_nal[1].p_payload + 4;
126     uint8_t *sei = p_nal[2].p_payload;
127 
128     int ret;
129     uint8_t *avcC;
130     int avcC_len;
131 
132     if( !p_mkv->width || !p_mkv->height ||
133         !p_mkv->d_width || !p_mkv->d_height )
134         return -1;
135 
136     avcC_len = 5 + 1 + 2 + sps_size + 1 + 2 + pps_size;
137     avcC = malloc( avcC_len );
138     if( !avcC )
139         return -1;
140 
141     avcC[0] = 1;
142     avcC[1] = sps[1];
143     avcC[2] = sps[2];
144     avcC[3] = sps[3];
145     avcC[4] = 0xff; // nalu size length is four bytes
146     avcC[5] = 0xe1; // one sps
147 
148     avcC[6] = sps_size >> 8;
149     avcC[7] = sps_size;
150 
151     memcpy( avcC+8, sps, sps_size );
152 
153     avcC[8+sps_size] = 1; // one pps
154     avcC[9+sps_size] = pps_size >> 8;
155     avcC[10+sps_size] = pps_size;
156 
157     memcpy( avcC+11+sps_size, pps, pps_size );
158 
159     ret = mk_write_header( p_mkv->w, "x264" X264_VERSION, "V_MPEG4/ISO/AVC",
160                            avcC, avcC_len, p_mkv->frame_duration, 50000,
161                            p_mkv->width, p_mkv->height,
162                            p_mkv->d_width, p_mkv->d_height, p_mkv->display_size_units, p_mkv->stereo_mode );
163     free( avcC );
164 
165     if( ret < 0 )
166         return ret;
167 
168     // SEI
169 
170     if( !p_mkv->b_writing_frame )
171     {
172         if( mk_start_frame( p_mkv->w ) < 0 )
173             return -1;
174         p_mkv->b_writing_frame = 1;
175     }
176     if( mk_add_frame_data( p_mkv->w, sei, sei_size ) < 0 )
177         return -1;
178 
179     return sei_size + sps_size + pps_size;
180 }
181 
write_frame(hnd_t handle,uint8_t * p_nalu,int i_size,x264_picture_t * p_picture)182 static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_t *p_picture )
183 {
184     mkv_hnd_t *p_mkv = handle;
185 
186     if( !p_mkv->b_writing_frame )
187     {
188         if( mk_start_frame( p_mkv->w ) < 0 )
189             return -1;
190         p_mkv->b_writing_frame = 1;
191     }
192 
193     if( mk_add_frame_data( p_mkv->w, p_nalu, i_size ) < 0 )
194         return -1;
195 
196     int64_t i_stamp = (int64_t)((p_picture->i_pts * 1e9 * p_mkv->i_timebase_num / p_mkv->i_timebase_den) + 0.5);
197 
198     p_mkv->b_writing_frame = 0;
199 
200     if( mk_set_frame_flags( p_mkv->w, i_stamp, p_picture->b_keyframe, p_picture->i_type == X264_TYPE_B ) < 0 )
201         return -1;
202 
203     return i_size;
204 }
205 
close_file(hnd_t handle,int64_t largest_pts,int64_t second_largest_pts)206 static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest_pts )
207 {
208     mkv_hnd_t *p_mkv = handle;
209     int ret;
210     int64_t i_last_delta;
211 
212     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;
213 
214     ret = mk_close( p_mkv->w, i_last_delta );
215 
216     free( p_mkv );
217 
218     return ret;
219 }
220 
221 const cli_output_t mkv_output = { open_file, set_param, write_headers, write_frame, close_file };
222