]> git.sesse.net Git - x264/blobdiff - input/y4m.c
y4m: Support extended frame headers when seeking
[x264] / input / y4m.c
index 1619f746c197166b0d58d6d614009bd820653195..060da0d39b2b19071c8911400a888706240fe694 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
- * y4m.c: x264 y4m input module
+ * y4m.c: y4m input
  *****************************************************************************
- * Copyright (C) 2003-2009 x264 project
+ * Copyright (C) 2003-2015 x264 project
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Loren Merritt <lorenm@u.washington.edu>
  * 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 "input.h"
+#define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "y4m", __VA_ARGS__ )
 
 typedef struct
 {
     FILE *fh;
-    int width, height;
     int next_frame;
-    int seq_header_len, frame_header_len;
-    int frame_size;
+    int seq_header_len;
+    int frame_header_len;
+    uint64_t frame_size;
+    uint64_t plane_size[3];
+    int bit_depth;
 } y4m_hnd_t;
 
 #define Y4M_MAGIC "YUV4MPEG2"
@@ -37,27 +43,47 @@ typedef struct
 #define Y4M_FRAME_MAGIC "FRAME"
 #define MAX_FRAME_HEADER 80
 
+static int parse_csp_and_depth( char *csp_name, int *bit_depth )
+{
+    int csp    = X264_CSP_MAX;
+
+    /* Set colorspace from known variants */
+    if( !strncmp( "420", csp_name, 3 ) )
+        csp = X264_CSP_I420;
+    else if( !strncmp( "422", csp_name, 3 ) )
+        csp = X264_CSP_I422;
+    else if( !strncmp( "444", csp_name, 3 ) && strncmp( "444alpha", csp_name, 8 ) ) // only accept alphaless 4:4:4
+        csp = X264_CSP_I444;
+
+    /* Set high bit depth from known extensions */
+    if( sscanf( csp_name, "%*d%*[pP]%d", bit_depth ) != 1 )
+        *bit_depth = 8;
+
+    return csp;
+}
+
 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
 {
-    y4m_hnd_t *h = malloc( sizeof(y4m_hnd_t) );
-    int  i, n, d;
+    y4m_hnd_t *h = calloc( 1, sizeof(y4m_hnd_t) );
+    int i;
+    uint32_t n, d;
     char header[MAX_YUV4_HEADER+10];
-    char *tokstart, *tokend, *header_end;
+    char *tokend, *header_end;
+    int colorspace = X264_CSP_NONE;
+    int alt_colorspace = X264_CSP_NONE;
+    int alt_bit_depth  = 8;
     if( !h )
         return -1;
 
-    h->next_frame = 0;
     info->vfr = 0;
 
     if( !strcmp( psz_filename, "-" ) )
         h->fh = stdin;
     else
-        h->fh = fopen(psz_filename, "rb");
+        h->fh = x264_fopen(psz_filename, "rb");
     if( h->fh == NULL )
         return -1;
 
-    h->frame_header_len = strlen( Y4M_FRAME_MAGIC )+1;
-
     /* Read header */
     for( i = 0; i < MAX_YUV4_HEADER; i++ )
     {
@@ -71,48 +97,52 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
             break;
         }
     }
-    if( i == MAX_YUV4_HEADER || strncmp( header, Y4M_MAGIC, strlen( Y4M_MAGIC ) ) )
+    if( i == MAX_YUV4_HEADER || strncmp( header, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1 ) )
         return -1;
 
     /* Scan properties */
     header_end = &header[i+1]; /* Include space */
     h->seq_header_len = i+1;
-    for( tokstart = &header[strlen( Y4M_MAGIC )+1]; tokstart < header_end; tokstart++ )
+    for( char *tokstart = header + sizeof(Y4M_MAGIC); tokstart < header_end; tokstart++ )
     {
         if( *tokstart == 0x20 )
             continue;
         switch( *tokstart++ )
         {
             case 'W': /* Width. Required. */
-                h->width = info->width = strtol( tokstart, &tokend, 10 );
+                info->width = strtol( tokstart, &tokend, 10 );
                 tokstart=tokend;
                 break;
             case 'H': /* Height. Required. */
-                h->height = info->height = strtol( tokstart, &tokend, 10 );
+                info->height = strtol( tokstart, &tokend, 10 );
                 tokstart=tokend;
                 break;
             case 'C': /* Color space */
-                if( strncmp( "420", tokstart, 3 ) )
-                {
-                    fprintf( stderr, "y4m [error]: colorspace unhandled\n" );
-                    return -1;
-                }
+                colorspace = parse_csp_and_depth( tokstart, &h->bit_depth );
                 tokstart = strchr( tokstart, 0x20 );
                 break;
             case 'I': /* Interlace type */
                 switch( *tokstart++ )
                 {
-                    case 'p': break;
-                    case '?':
                     case 't':
+                        info->interlaced = 1;
+                        info->tff = 1;
+                        break;
                     case 'b':
+                        info->interlaced = 1;
+                        info->tff = 0;
+                        break;
                     case 'm':
-                    default:
                         info->interlaced = 1;
+                        break;
+                    //case '?':
+                    //case 'p':
+                    default:
+                        break;
                 }
                 break;
             case 'F': /* Frame rate - 0:0 if unknown */
-                if( sscanf( tokstart, "%d:%d", &n, &d ) == 2 && n && d )
+                if( sscanf( tokstart, "%u:%u", &n, &d ) == 2 && n && d )
                 {
                     x264_reduce_fraction( &n, &d );
                     info->fps_num = n;
@@ -122,7 +152,7 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
                 break;
             case 'A': /* Pixel aspect - 0:0 if unknown */
                 /* Don't override the aspect ratio if sar has been explicitly set on the commandline. */
-                if( sscanf( tokstart, "%d:%d", &n, &d ) == 2 && n && d )
+                if( sscanf( tokstart, "%u:%u", &n, &d ) == 2 && n && d )
                 {
                     x264_reduce_fraction( &n, &d );
                     info->sar_width  = n;
@@ -135,45 +165,72 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
                 {
                     /* Older nonstandard pixel format representation */
                     tokstart += 6;
-                    if( strncmp( "420JPEG",tokstart, 7 ) &&
-                        strncmp( "420MPEG2",tokstart, 8 ) &&
-                        strncmp( "420PALDV",tokstart, 8 ) )
-                    {
-                        fprintf( stderr, "y4m [error]: unsupported extended colorspace\n" );
-                        return -1;
-                    }
+                    alt_colorspace = parse_csp_and_depth( tokstart, &alt_bit_depth );
                 }
                 tokstart = strchr( tokstart, 0x20 );
                 break;
         }
     }
 
-    *p_handle = h;
-    return 0;
-}
+    if( colorspace == X264_CSP_NONE )
+    {
+        colorspace   = alt_colorspace;
+        h->bit_depth = alt_bit_depth;
+    }
 
-/* Most common case: frame_header = "FRAME" */
-static int get_frame_total( hnd_t handle )
-{
-    y4m_hnd_t *h = handle;
-    int i_frame_total = 0;
+    // default to 8bit 4:2:0 if nothing is specified
+    if( colorspace == X264_CSP_NONE )
+    {
+        colorspace    = X264_CSP_I420;
+        h->bit_depth  = 8;
+    }
+
+    FAIL_IF_ERROR( colorspace <= X264_CSP_NONE || colorspace >= X264_CSP_MAX, "colorspace unhandled\n" )
+    FAIL_IF_ERROR( h->bit_depth < 8 || h->bit_depth > 16, "unsupported bit depth `%d'\n", h->bit_depth );
+
+    info->thread_safe = 1;
+    info->num_frames  = 0;
+    info->csp         = colorspace;
+
+    if( h->bit_depth > 8 )
+        info->csp |= X264_CSP_HIGH_DEPTH;
+
+    const x264_cli_csp_t *csp = x264_cli_get_csp( info->csp );
+
+    for( i = 0; i < csp->planes; i++ )
+    {
+        h->plane_size[i] = x264_cli_pic_plane_size( info->csp, info->width, info->height, i );
+        h->frame_size += h->plane_size[i];
+        /* x264_cli_pic_plane_size returns the size in bytes, we need the value in pixels from here on */
+        h->plane_size[i] /= x264_cli_csp_depth_factor( info->csp );
+    }
 
     if( x264_is_regular_file( h->fh ) )
     {
         uint64_t init_pos = ftell( h->fh );
+
+        /* Find out the length of the frame header */
+        int len = 1;
+        while( len <= MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
+            len++;
+        FAIL_IF_ERROR( len > MAX_FRAME_HEADER || len < sizeof(Y4M_FRAME_MAGIC), "bad frame header length\n" )
+        h->frame_header_len = len;
+        h->frame_size += len;
+
         fseek( h->fh, 0, SEEK_END );
         uint64_t i_size = ftell( h->fh );
         fseek( h->fh, init_pos, SEEK_SET );
-        i_frame_total = (int)((i_size - h->seq_header_len) /
-                              (3*(h->width*h->height)/2+h->frame_header_len));
+        info->num_frames = (i_size - h->seq_header_len) / h->frame_size;
     }
 
-    return i_frame_total;
+    *p_handle = h;
+    return 0;
 }
 
-static int read_frame_internal( x264_picture_t *p_pic, y4m_hnd_t *h )
+static int read_frame_internal( cli_pic_t *pic, y4m_hnd_t *h, int bit_depth_uc )
 {
-    int slen = strlen( Y4M_FRAME_MAGIC );
+    static const size_t slen = sizeof(Y4M_FRAME_MAGIC)-1;
+    int pixel_depth = x264_cli_csp_depth_factor( pic->img.csp );
     int i = 0;
     char header[16];
 
@@ -182,50 +239,50 @@ static int read_frame_internal( x264_picture_t *p_pic, y4m_hnd_t *h )
         return -1;
 
     header[slen] = 0;
-    if( strncmp( header, Y4M_FRAME_MAGIC, slen ) )
-    {
-        fprintf( stderr, "y4m [error]: bad header magic (%"PRIx32" <=> %s)\n",
-                 M32(header), header );
-        return -1;
-    }
+    FAIL_IF_ERROR( strncmp( header, Y4M_FRAME_MAGIC, slen ), "bad header magic (%"PRIx32" <=> %s)\n",
+                   M32(header), header )
 
     /* Skip most of it */
     while( i < MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
         i++;
-    if( i == MAX_FRAME_HEADER )
+    FAIL_IF_ERROR( i == MAX_FRAME_HEADER, "bad frame header!\n" )
+
+    int error = 0;
+    for( i = 0; i < pic->img.planes && !error; i++ )
     {
-        fprintf( stderr, "y4m [error]: bad frame header!\n" );
-        return -1;
+        error |= fread( pic->img.plane[i], pixel_depth, h->plane_size[i], h->fh ) != h->plane_size[i];
+        if( bit_depth_uc )
+        {
+            /* upconvert non 16bit high depth planes to 16bit using the same
+             * algorithm as used in the depth filter. */
+            uint16_t *plane = (uint16_t*)pic->img.plane[i];
+            uint64_t pixel_count = h->plane_size[i];
+            int lshift = 16 - h->bit_depth;
+            for( uint64_t j = 0; j < pixel_count; j++ )
+                plane[j] = plane[j] << lshift;
+        }
     }
-    h->frame_header_len = i+slen+1;
-
-    if( fread( p_pic->img.plane[0], h->width * h->height, 1, h->fh ) <= 0
-     || fread( p_pic->img.plane[1], h->width * h->height / 4, 1, h->fh ) <= 0
-     || fread( p_pic->img.plane[2], h->width * h->height / 4, 1, h->fh ) <= 0 )
-        return -1;
-
-    return 0;
+    return error;
 }
 
-static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
+static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
 {
     y4m_hnd_t *h = handle;
 
     if( i_frame > h->next_frame )
     {
         if( x264_is_regular_file( h->fh ) )
-            fseek( h->fh, (uint64_t)i_frame*(3*(h->width*h->height)/2+h->frame_header_len)
-                 + h->seq_header_len, SEEK_SET );
+            fseek( h->fh, h->frame_size * i_frame + h->seq_header_len, SEEK_SET );
         else
             while( i_frame > h->next_frame )
             {
-                if( read_frame_internal( p_pic, h ) )
+                if( read_frame_internal( pic, h, 0 ) )
                     return -1;
                 h->next_frame++;
             }
     }
 
-    if( read_frame_internal( p_pic, h ) )
+    if( read_frame_internal( pic, h, h->bit_depth & 7 ) )
         return -1;
 
     h->next_frame = i_frame+1;
@@ -242,4 +299,4 @@ static int close_file( hnd_t handle )
     return 0;
 }
 
-cli_input_t y4m_input = { open_file, get_frame_total, x264_picture_alloc, read_frame, NULL, x264_picture_clean, close_file };
+const cli_input_t y4m_input = { open_file, x264_cli_pic_alloc, read_frame, NULL, x264_cli_pic_clean, close_file };