]> git.sesse.net Git - x264/blobdiff - output/matroska.c
Bump dates to 2016
[x264] / output / matroska.c
index 8e84f52a115bac7c872ff4c3e031ec4a2a404746..91f42e5bd79956a26ba69e0bd05702103b6bf882 100644 (file)
@@ -1,7 +1,9 @@
 /*****************************************************************************
- * matroska.c: x264 matroska output module
+ * matroska.c: matroska muxer
  *****************************************************************************
- * Copyright (C) 2005 Mike Matsnev
+ * Copyright (C) 2005-2016 x264 project
+ *
+ * Authors: Mike Matsnev <mike@haali.su>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
+ *
+ * This program is also available under a commercial proprietary license.
+ * For more information, contact us at licensing@x264.com.
  *****************************************************************************/
 
-#include "muxers.h"
+#include "output.h"
 #include "matroska_ebml.h"
 
 typedef struct
@@ -27,26 +32,24 @@ typedef struct
 
     int width, height, d_width, d_height;
 
+    int display_size_units;
+    int stereo_mode;
+
     int64_t frame_duration;
 
     char b_writing_frame;
-    int i_timebase_num;
-    int i_timebase_den;
+    uint32_t i_timebase_num;
+    uint32_t i_timebase_den;
 
 } mkv_hnd_t;
 
-static int open_file( char *psz_filename, hnd_t *p_handle )
+static int open_file( char *psz_filename, hnd_t *p_handle, cli_output_opt_t *opt )
 {
-    mkv_hnd_t *p_mkv;
-
     *p_handle = NULL;
-
-    p_mkv  = malloc( sizeof(*p_mkv) );
+    mkv_hnd_t *p_mkv = calloc( 1, sizeof(mkv_hnd_t) );
     if( !p_mkv )
         return -1;
 
-    memset( p_mkv, 0, sizeof(*p_mkv) );
-
     p_mkv->w = mk_create_writer( psz_filename );
     if( !p_mkv->w )
     {
@@ -59,9 +62,14 @@ static int open_file( char *psz_filename, hnd_t *p_handle )
     return 0;
 }
 
+#define STEREO_COUNT 7
+static const uint8_t stereo_modes[STEREO_COUNT] = {5,9,7,1,3,13,0};
+static const uint8_t stereo_w_div[STEREO_COUNT] = {1,2,1,2,1,1,1};
+static const uint8_t stereo_h_div[STEREO_COUNT] = {1,1,2,1,2,1,1};
+
 static int set_param( hnd_t handle, x264_param_t *p_param )
 {
-    mkv_hnd_t   *p_mkv = handle;
+    mkv_hnd_t *p_mkv = handle;
     int64_t dw, dh;
 
     if( p_param->i_fps_num > 0 && !p_param->b_vfr_input )
@@ -74,29 +82,28 @@ static int set_param( hnd_t handle, x264_param_t *p_param )
         p_mkv->frame_duration = 0;
     }
 
-    p_mkv->width = p_param->i_width;
-    p_mkv->height = p_param->i_height;
-
-    if( p_param->vui.i_sar_width && p_param->vui.i_sar_height )
+    dw = p_mkv->width = p_param->i_width;
+    dh = p_mkv->height = p_param->i_height;
+    p_mkv->display_size_units = DS_PIXELS;
+    p_mkv->stereo_mode = -1;
+    if( p_param->i_frame_packing >= 0 && p_param->i_frame_packing < STEREO_COUNT )
     {
-        dw = (int64_t)p_param->i_width  * p_param->vui.i_sar_width;
-        dh = (int64_t)p_param->i_height * p_param->vui.i_sar_height;
+        p_mkv->stereo_mode = stereo_modes[p_param->i_frame_packing];
+        dw /= stereo_w_div[p_param->i_frame_packing];
+        dh /= stereo_h_div[p_param->i_frame_packing];
     }
-    else
-    {
-        dw = p_param->i_width;
-        dh = p_param->i_height;
-    }
-
-    if( dw > 0 && dh > 0 )
+    if( p_param->vui.i_sar_width && p_param->vui.i_sar_height
+        && p_param->vui.i_sar_width != p_param->vui.i_sar_height )
     {
-        int64_t x = gcd( dw, dh );
-        dw /= x;
-        dh /= x;
+        if ( p_param->vui.i_sar_width > p_param->vui.i_sar_height ) {
+            dw = dw * p_param->vui.i_sar_width / p_param->vui.i_sar_height;
+        } else {
+            dh = dh * p_param->vui.i_sar_height / p_param->vui.i_sar_width;
+        }
     }
-
     p_mkv->d_width = (int)dw;
     p_mkv->d_height = (int)dh;
+
     p_mkv->i_timebase_num = p_param->i_timebase_num;
     p_mkv->i_timebase_den = p_param->i_timebase_den;
 
@@ -107,13 +114,13 @@ static int write_headers( hnd_t handle, x264_nal_t *p_nal )
 {
     mkv_hnd_t *p_mkv = handle;
 
-    int sei_size = p_nal[0].i_payload;
-    int sps_size = p_nal[1].i_payload - 4;
-    int pps_size = p_nal[2].i_payload - 4;
+    int sps_size = p_nal[0].i_payload - 4;
+    int pps_size = p_nal[1].i_payload - 4;
+    int sei_size = p_nal[2].i_payload;
 
-    uint8_t *sei = p_nal[0].p_payload;
-    uint8_t *sps = p_nal[1].p_payload + 4;
-    uint8_t *pps = p_nal[2].p_payload + 4;
+    uint8_t *sps = p_nal[0].p_payload + 4;
+    uint8_t *pps = p_nal[1].p_payload + 4;
+    uint8_t *sei = p_nal[2].p_payload;
 
     int ret;
     uint8_t *avcC;
@@ -146,13 +153,15 @@ static int write_headers( hnd_t handle, x264_nal_t *p_nal )
 
     memcpy( avcC+11+sps_size, pps, pps_size );
 
-    ret = mk_writeHeader( p_mkv->w, "x264", "V_MPEG4/ISO/AVC",
-                          avcC, avcC_len, p_mkv->frame_duration, 50000,
-                          p_mkv->width, p_mkv->height,
-                          p_mkv->d_width, p_mkv->d_height );
-
+    ret = mk_write_header( p_mkv->w, "x264" X264_VERSION, "V_MPEG4/ISO/AVC",
+                           avcC, avcC_len, p_mkv->frame_duration, 50000,
+                           p_mkv->width, p_mkv->height,
+                           p_mkv->d_width, p_mkv->d_height, p_mkv->display_size_units, p_mkv->stereo_mode );
     free( avcC );
 
+    if( ret < 0 )
+        return ret;
+
     // SEI
 
     if( !p_mkv->b_writing_frame )
@@ -185,7 +194,7 @@ static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_
 
     p_mkv->b_writing_frame = 0;
 
-    if( mk_set_frame_flags( p_mkv->w, i_stamp, p_picture->b_keyframe ) < 0 )
+    if( mk_set_frame_flags( p_mkv->w, i_stamp, p_picture->b_keyframe, p_picture->i_type == X264_TYPE_B ) < 0 )
         return -1;
 
     return i_size;
@@ -197,7 +206,7 @@ static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest
     int ret;
     int64_t i_last_delta;
 
-    i_last_delta = (int64_t)(((largest_pts - second_largest_pts) * p_mkv->i_timebase_num / p_mkv->i_timebase_den) + 0.5);
+    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;
 
     ret = mk_close( p_mkv->w, i_last_delta );
 
@@ -206,4 +215,4 @@ static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest
     return ret;
 }
 
-cli_output_t mkv_output = { open_file, set_param, write_headers, write_frame, close_file };
+const cli_output_t mkv_output = { open_file, set_param, write_headers, write_frame, close_file };