1 /*****************************************************************************
2 * lavf.c: libavformat input
3 *****************************************************************************
4 * Copyright (C) 2009-2014 x264 project
5 *
6 * Authors: Mike Gurlitz <mike.gurlitz@gmail.com>
7 * Steven Walters <kemuri9@gmail.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22 *
23 * This program is also available under a commercial proprietary license.
24 * For more information, contact us at licensing@x264.com.
25 *****************************************************************************/
26
27 #include "input.h"
28 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "lavf", __VA_ARGS__ )
29 #undef DECLARE_ALIGNED
30 #include <libavformat/avformat.h>
31 #include <libavutil/mem.h>
32 #include <libavutil/pixdesc.h>
33 #include <libavutil/dict.h>
34
35 typedef struct
36 {
37 AVFormatContext *lavf;
38 AVFrame *frame;
39 int stream_id;
40 int next_frame;
41 int vfr_input;
42 cli_pic_t *first_pic;
43 } lavf_hnd_t;
44
45 /* handle the deprecated jpeg pixel formats */
handle_jpeg(int csp,int * fullrange)46 static int handle_jpeg( int csp, int *fullrange )
47 {
48 switch( csp )
49 {
50 case AV_PIX_FMT_YUVJ420P: *fullrange = 1; return AV_PIX_FMT_YUV420P;
51 case AV_PIX_FMT_YUVJ422P: *fullrange = 1; return AV_PIX_FMT_YUV422P;
52 case AV_PIX_FMT_YUVJ444P: *fullrange = 1; return AV_PIX_FMT_YUV444P;
53 default: return csp;
54 }
55 }
56
read_frame_internal(cli_pic_t * p_pic,lavf_hnd_t * h,int i_frame,video_info_t * info)57 static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, video_info_t *info )
58 {
59 if( h->first_pic && !info )
60 {
61 /* see if the frame we are requesting is the frame we have already read and stored.
62 * if so, retrieve the pts and image data before freeing it. */
63 if( !i_frame )
64 {
65 XCHG( cli_image_t, p_pic->img, h->first_pic->img );
66 p_pic->pts = h->first_pic->pts;
67 }
68 lavf_input.picture_clean( h->first_pic );
69 free( h->first_pic );
70 h->first_pic = NULL;
71 if( !i_frame )
72 return 0;
73 }
74
75 AVCodecContext *c = h->lavf->streams[h->stream_id]->codec;
76
77 AVPacket pkt;
78 av_init_packet( &pkt );
79 pkt.data = NULL;
80 pkt.size = 0;
81
82 while( i_frame >= h->next_frame )
83 {
84 int finished = 0;
85 int ret = 0;
86 do
87 {
88 ret = av_read_frame( h->lavf, &pkt );
89
90 if( ret < 0 )
91 {
92 av_init_packet( &pkt );
93 pkt.data = NULL;
94 pkt.size = 0;
95 }
96
97 if( ret < 0 || pkt.stream_index == h->stream_id )
98 {
99 if( avcodec_decode_video2( c, h->frame, &finished, &pkt ) < 0 )
100 x264_cli_log( "lavf", X264_LOG_WARNING, "video decoding failed on frame %d\n", h->next_frame );
101 }
102
103 if( ret >= 0 )
104 av_free_packet( &pkt );
105 } while( !finished && ret >= 0 );
106
107 if( !finished )
108 return -1;
109
110 h->next_frame++;
111 }
112
113 memcpy( p_pic->img.stride, h->frame->linesize, sizeof(p_pic->img.stride) );
114 memcpy( p_pic->img.plane, h->frame->data, sizeof(p_pic->img.plane) );
115 int is_fullrange = 0;
116 p_pic->img.width = c->width;
117 p_pic->img.height = c->height;
118 p_pic->img.csp = handle_jpeg( c->pix_fmt, &is_fullrange ) | X264_CSP_OTHER;
119
120 if( info )
121 {
122 info->fullrange = is_fullrange;
123 info->interlaced = h->frame->interlaced_frame;
124 info->tff = h->frame->top_field_first;
125 }
126
127 if( h->vfr_input )
128 {
129 p_pic->pts = p_pic->duration = 0;
130 if( h->frame->pkt_pts != AV_NOPTS_VALUE )
131 p_pic->pts = h->frame->pkt_pts;
132 else if( h->frame->pkt_dts != AV_NOPTS_VALUE )
133 p_pic->pts = h->frame->pkt_dts; // for AVI files
134 else if( info )
135 {
136 h->vfr_input = info->vfr = 0;
137 return 0;
138 }
139 }
140
141 return 0;
142 }
143
open_file(char * psz_filename,hnd_t * p_handle,video_info_t * info,cli_input_opt_t * opt)144 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
145 {
146 lavf_hnd_t *h = calloc( 1, sizeof(lavf_hnd_t) );
147 if( !h )
148 return -1;
149 av_register_all();
150 if( !strcmp( psz_filename, "-" ) )
151 psz_filename = "pipe:";
152
153 h->frame = av_frame_alloc();
154 if( !h->frame )
155 return -1;
156
157 /* if resolution was passed in, place it and colorspace into options. this allows raw video support */
158 AVDictionary *options = NULL;
159 if( opt->resolution )
160 {
161 av_dict_set( &options, "video_size", opt->resolution, 0 );
162 const char *csp = opt->colorspace ? opt->colorspace : av_get_pix_fmt_name( AV_PIX_FMT_YUV420P );
163 av_dict_set( &options, "pixel_format", csp, 0 );
164 }
165
166 /* specify the input format. this is helpful when lavf fails to guess */
167 AVInputFormat *format = NULL;
168 if( opt->format )
169 FAIL_IF_ERROR( !(format = av_find_input_format( opt->format )), "unknown file format: %s\n", opt->format );
170
171 FAIL_IF_ERROR( avformat_open_input( &h->lavf, psz_filename, format, &options ), "could not open input file\n" )
172 if( options )
173 av_dict_free( &options );
174 FAIL_IF_ERROR( avformat_find_stream_info( h->lavf, NULL ) < 0, "could not find input stream info\n" )
175
176 int i = 0;
177 while( i < h->lavf->nb_streams && h->lavf->streams[i]->codec->codec_type != AVMEDIA_TYPE_VIDEO )
178 i++;
179 FAIL_IF_ERROR( i == h->lavf->nb_streams, "could not find video stream\n" )
180 h->stream_id = i;
181 h->next_frame = 0;
182 AVCodecContext *c = h->lavf->streams[i]->codec;
183 info->fps_num = h->lavf->streams[i]->avg_frame_rate.num;
184 info->fps_den = h->lavf->streams[i]->avg_frame_rate.den;
185 info->timebase_num = h->lavf->streams[i]->time_base.num;
186 info->timebase_den = h->lavf->streams[i]->time_base.den;
187 /* lavf is thread unsafe as calling av_read_frame invalidates previously read AVPackets */
188 info->thread_safe = 0;
189 h->vfr_input = info->vfr;
190 FAIL_IF_ERROR( avcodec_open2( c, avcodec_find_decoder( c->codec_id ), NULL ),
191 "could not find decoder for video stream\n" )
192
193 /* prefetch the first frame and set/confirm flags */
194 h->first_pic = malloc( sizeof(cli_pic_t) );
195 FAIL_IF_ERROR( !h->first_pic || lavf_input.picture_alloc( h->first_pic, X264_CSP_OTHER, info->width, info->height ),
196 "malloc failed\n" )
197 else if( read_frame_internal( h->first_pic, h, 0, info ) )
198 return -1;
199
200 info->width = c->width;
201 info->height = c->height;
202 info->csp = h->first_pic->img.csp;
203 info->num_frames = h->lavf->streams[i]->nb_frames;
204 info->sar_height = c->sample_aspect_ratio.den;
205 info->sar_width = c->sample_aspect_ratio.num;
206 info->fullrange |= c->color_range == AVCOL_RANGE_JPEG;
207
208 /* avisynth stores rgb data vertically flipped. */
209 if( !strcasecmp( get_filename_extension( psz_filename ), "avs" ) &&
210 (c->pix_fmt == AV_PIX_FMT_BGRA || c->pix_fmt == AV_PIX_FMT_BGR24) )
211 info->csp |= X264_CSP_VFLIP;
212
213 *p_handle = h;
214
215 return 0;
216 }
217
picture_alloc(cli_pic_t * pic,int csp,int width,int height)218 static int picture_alloc( cli_pic_t *pic, int csp, int width, int height )
219 {
220 if( x264_cli_pic_alloc( pic, X264_CSP_NONE, width, height ) )
221 return -1;
222 pic->img.csp = csp;
223 pic->img.planes = 4;
224 return 0;
225 }
226
read_frame(cli_pic_t * pic,hnd_t handle,int i_frame)227 static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
228 {
229 return read_frame_internal( pic, handle, i_frame, NULL );
230 }
231
picture_clean(cli_pic_t * pic)232 static void picture_clean( cli_pic_t *pic )
233 {
234 memset( pic, 0, sizeof(cli_pic_t) );
235 }
236
close_file(hnd_t handle)237 static int close_file( hnd_t handle )
238 {
239 lavf_hnd_t *h = handle;
240 avcodec_close( h->lavf->streams[h->stream_id]->codec );
241 avformat_close_input( &h->lavf );
242 av_frame_free( &h->frame );
243 free( h );
244 return 0;
245 }
246
247 const cli_input_t lavf_input = { open_file, picture_alloc, read_frame, NULL, picture_clean, close_file };
248