Fix integer overflow in ft_rendered_size_line
[ilaris-y4m-tools.git] / 0001-Support-VFR-mode-for-y4m.patch
blob3086c99df6faf0713709363ba724b817b0ba2cc3
1 From 399827128a0060cd09c17e22db62e6c9b0db2e35 Mon Sep 17 00:00:00 2001
2 From: Ilari Liusvaara <ilari.liusvaara@elisanet.fi>
3 Date: Fri, 26 Apr 2013 22:32:25 +0300
4 Subject: [PATCH] Support VFR mode for y4m
6 ---
7 input/y4m.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
8 1 files changed, 54 insertions(+), 3 deletions(-)
10 diff --git a/input/y4m.c b/input/y4m.c
11 index 014450c..1f46ac8 100644
12 --- a/input/y4m.c
13 +++ b/input/y4m.c
14 @@ -24,6 +24,13 @@
15 * For more information, contact us at licensing@x264.com.
16 *****************************************************************************/
18 +//
19 +// Modified 2013-04-26 by H. Ilari Liusvaara:
20 +// - Support VFR mode.
21 +//
22 +// Modified 2013-06-27 by H. Ilari Liusvaara:
23 +// - Support RGB input.
25 #include "input.h"
26 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "y4m", __VA_ARGS__ )
28 @@ -35,6 +42,8 @@ typedef struct
29 int frame_header_len;
30 uint64_t frame_size;
31 uint64_t plane_size[3];
32 + uint64_t pts;
33 + int is_vfr;
34 int bit_depth;
35 } y4m_hnd_t;
37 @@ -54,6 +63,8 @@ static int parse_csp_and_depth( char *csp_name, int *bit_depth )
38 csp = X264_CSP_I422;
39 else if( !strncmp( "444", csp_name, 3 ) && strncmp( "444alpha", csp_name, 8 ) ) // only accept alphaless 4:4:4
40 csp = X264_CSP_I444;
41 + else if( !strncmp( "rgb", csp_name, 3 ) )
42 + csp = X264_CSP_RGB;
44 /* Set high bit depth from known extensions */
45 if( sscanf( csp_name, "%*d%*[pP]%d", bit_depth ) != 1 )
46 @@ -86,6 +97,7 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
47 return -1;
49 h->frame_header_len = strlen( Y4M_FRAME_MAGIC )+1;
50 + h->is_vfr = 0;
52 /* Read header */
53 for( i = 0; i < MAX_YUV4_HEADER; i++ )
54 @@ -169,6 +181,11 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
55 /* Older nonstandard pixel format representation */
56 tokstart += 6;
57 alt_colorspace = parse_csp_and_depth( tokstart, &alt_bit_depth );
58 + } else if( !strncmp( "VFR", tokstart, 3 ) )
59 + {
60 + /* VFR mode. */
61 + tokstart += 3;
62 + h->is_vfr = 1;
64 tokstart = strchr( tokstart, 0x20 );
65 break;
66 @@ -194,7 +211,13 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
67 info->thread_safe = 1;
68 info->num_frames = 0;
69 info->csp = colorspace;
70 + info->vfr = h->is_vfr;
71 h->frame_size = h->frame_header_len;
72 + h->pts = 0;
73 + if(info->vfr) {
74 + info->timebase_num = info->fps_den;
75 + info->timebase_den = info->fps_num;
76 + }
78 if( h->bit_depth > 8 )
79 info->csp |= X264_CSP_HIGH_DEPTH;
80 @@ -229,6 +252,9 @@ static int read_frame_internal( cli_pic_t *pic, y4m_hnd_t *h )
81 int pixel_depth = x264_cli_csp_depth_factor( pic->img.csp );
82 int i = 0;
83 char header[16];
84 + char fheader[MAX_FRAME_HEADER + 1];
85 + int tmp;
86 + unsigned duration = 1;
88 /* Read frame header - without terminating '\n' */
89 if( fread( header, 1, slen, h->fh ) != slen )
90 @@ -238,13 +264,38 @@ static int read_frame_internal( cli_pic_t *pic, y4m_hnd_t *h )
91 FAIL_IF_ERROR( strncmp( header, Y4M_FRAME_MAGIC, slen ), "bad header magic (%"PRIx32" <=> %s)\n",
92 M32(header), header )
94 - /* Skip most of it */
95 - while( i < MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
96 - i++;
97 + /* Read the header in oder to get the duration. */
98 + while( i < MAX_FRAME_HEADER && (tmp = fgetc( h->fh )) != '\n' )
99 + fheader[i++] = tmp;
100 FAIL_IF_ERROR( i == MAX_FRAME_HEADER, "bad frame header!\n" )
101 + fheader[i] = 0;
102 h->frame_size = h->frame_size - h->frame_header_len + i+slen+1;
103 h->frame_header_len = i+slen+1;
105 + /* VFR. */
106 + if(h->is_vfr) {
107 + const char* hend = fheader + i;
108 + for(const char* tokstart = fheader; tokstart < hend; tokstart++) {
109 + if( *tokstart == 0x20 )
110 + continue;
111 + if( *tokstart == 'X' ) {
112 + if( !strncmp( "Xduration=", tokstart, 10 ) )
114 + /* Frame duration. */
115 + duration = strtol(tokstart + 10, NULL, 10);
116 + if( !duration )
117 + duration = 1;
119 + tokstart = strchr( tokstart, 0x20 );
120 + if( !tokstart )
121 + break;
124 + pic->pts = h->pts;
125 + pic->duration = duration;
126 + h->pts += duration;
129 int error = 0;
130 for( i = 0; i < pic->img.planes && !error; i++ )
133 1.7.9.48.g85da4d