1 /*****************************************************************************
2 * lavf.c: x264 libavformat input module
3 *****************************************************************************
4 * Copyright (C) 2009 x264 project
6 * Authors: Mike Gurlitz <mike.gurlitz@gmail.com>
7 * Steven Walters <kemuri9@gmail.com>
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.
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.
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 *****************************************************************************/
25 #undef DECLARE_ALIGNED
26 #include <libavformat/avformat.h>
27 #include <libswscale/swscale.h>
31 AVFormatContext *lavf;
36 struct SwsContext *scaler;
39 x264_picture_t *first_pic;
46 enum PixelFormat cur_pix_fmt;
55 static int check_swscale( lavf_hnd_t *h, AVCodecContext *c, int i_frame )
57 if( h->scaler && (h->cur_width == c->width) && (h->cur_height == c->height) && (h->cur_pix_fmt == c->pix_fmt) )
61 sws_freeContext( h->scaler );
62 fprintf( stderr, "lavf [warning]: stream properties changed to %dx%d, %s at frame %d \n",
63 c->width, c->height, avcodec_get_pix_fmt_name( c->pix_fmt ), i_frame );
64 h->cur_width = c->width;
65 h->cur_height = c->height;
66 h->cur_pix_fmt = c->pix_fmt;
68 h->scaler = sws_getContext( h->cur_width, h->cur_height, h->cur_pix_fmt, h->init_width, h->init_height,
69 PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL );
72 fprintf( stderr, "lavf [error]: could not open swscale context\n" );
78 static int read_frame_internal( x264_picture_t *p_pic, lavf_hnd_t *h, int i_frame, video_info_t *info )
80 if( h->first_pic && !info )
82 /* see if the frame we are requesting is the frame we have already read and stored.
83 * if so, retrieve the pts and image data before freeing it. */
86 XCHG( x264_image_t, p_pic->img, h->first_pic->img );
87 p_pic->i_pts = h->first_pic->i_pts;
89 lavf_input.picture_clean( h->first_pic );
96 AVCodecContext *c = h->lavf->streams[h->stream_id]->codec;
97 lavf_pic_t *pic_h = p_pic->opaque;
98 AVPacket *pkt = &pic_h->packet;
99 AVFrame *frame = &pic_h->frame;
101 while( i_frame >= h->next_frame )
104 while( !finished && av_read_frame( h->lavf, pkt ) >= 0 )
105 if( pkt->stream_index == h->stream_id )
107 c->reordered_opaque = pkt->pts;
108 if( avcodec_decode_video2( c, frame, &finished, pkt ) < 0 )
109 fprintf( stderr, "lavf [warning]: video decoding failed on frame %d\n", h->next_frame );
113 if( avcodec_decode_video2( c, frame, &finished, pkt ) < 0 )
114 fprintf( stderr, "lavf [warning]: video decoding failed on frame %d\n", h->next_frame );
121 if( check_swscale( h, c, i_frame ) )
123 /* FIXME: avoid sws_scale where possible (no colorspace conversion). */
124 sws_scale( h->scaler, frame->data, frame->linesize, 0, c->height, p_pic->img.plane, p_pic->img.i_stride );
128 info->interlaced = frame->interlaced_frame;
129 info->tff = frame->top_field_first;
135 if( frame->reordered_opaque != AV_NOPTS_VALUE )
136 p_pic->i_pts = frame->reordered_opaque;
137 else if( pkt->dts != AV_NOPTS_VALUE )
138 p_pic->i_pts = pkt->dts; // for AVI files
141 h->vfr_input = info->vfr = 0;
144 if( !h->pts_offset_flag )
146 h->pts_offset = p_pic->i_pts;
147 h->pts_offset_flag = 1;
149 p_pic->i_pts -= h->pts_offset;
154 pkt->destruct( pkt );
155 avcodec_get_frame_defaults( frame );
159 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
161 lavf_hnd_t *h = malloc( sizeof(lavf_hnd_t) );
166 if( !strcmp( psz_filename, "-" ) )
167 psz_filename = "pipe:";
169 if( av_open_input_file( &h->lavf, psz_filename, NULL, 0, NULL ) )
171 fprintf( stderr, "lavf [error]: could not open input file\n" );
175 if( av_find_stream_info( h->lavf ) < 0 )
177 fprintf( stderr, "lavf [error]: could not find input stream info\n" );
182 while( i < h->lavf->nb_streams && h->lavf->streams[i]->codec->codec_type != CODEC_TYPE_VIDEO )
184 if( i == h->lavf->nb_streams )
186 fprintf( stderr, "lavf [error]: could not find video stream\n" );
191 h->pts_offset_flag = 0;
193 AVCodecContext *c = h->lavf->streams[i]->codec;
194 h->init_width = h->cur_width = info->width = c->width;
195 h->init_height = h->cur_height = info->height = c->height;
196 h->cur_pix_fmt = c->pix_fmt;
197 info->fps_num = h->lavf->streams[i]->r_frame_rate.num;
198 info->fps_den = h->lavf->streams[i]->r_frame_rate.den;
199 info->timebase_num = h->lavf->streams[i]->time_base.num;
200 info->timebase_den = h->lavf->streams[i]->time_base.den;
201 h->vfr_input = info->vfr;
202 h->vertical_flip = 0;
204 /* avisynth stores rgb data vertically flipped. */
205 if( !strcasecmp( get_filename_extension( psz_filename ), "avs" ) &&
206 (h->cur_pix_fmt == PIX_FMT_BGRA || h->cur_pix_fmt == PIX_FMT_BGR24) )
207 info->csp |= X264_CSP_VFLIP;
209 if( h->cur_pix_fmt != PIX_FMT_YUV420P )
210 fprintf( stderr, "lavf [warning]: converting from %s to YV12\n",
211 avcodec_get_pix_fmt_name( h->cur_pix_fmt ) );
213 if( avcodec_open( c, avcodec_find_decoder( c->codec_id ) ) )
215 fprintf( stderr, "lavf [error]: could not find decoder for video stream\n" );
219 /* prefetch the first frame and set/confirm flags */
220 h->first_pic = malloc( sizeof(x264_picture_t) );
221 if( !h->first_pic || lavf_input.picture_alloc( h->first_pic, info->csp, info->width, info->height ) )
223 fprintf( stderr, "lavf [error]: malloc failed\n" );
226 else if( read_frame_internal( h->first_pic, h, 0, info ) )
229 info->sar_height = c->sample_aspect_ratio.den;
230 info->sar_width = c->sample_aspect_ratio.num;
236 static int picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
238 if( x264_picture_alloc( pic, i_csp, i_width, i_height ) )
240 lavf_pic_t *pic_h = pic->opaque = malloc( sizeof(lavf_pic_t) );
243 avcodec_get_frame_defaults( &pic_h->frame );
244 av_init_packet( &pic_h->packet );
249 static int get_frame_total( hnd_t handle )
254 static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
256 return read_frame_internal( p_pic, handle, i_frame, NULL );
259 static void picture_clean( x264_picture_t *pic )
262 x264_picture_clean( pic );
265 static int close_file( hnd_t handle )
267 lavf_hnd_t *h = handle;
268 sws_freeContext( h->scaler );
269 avcodec_close( h->lavf->streams[h->stream_id]->codec );
270 av_close_input_file( h->lavf );
275 const cli_input_t lavf_input = { open_file, get_frame_total, picture_alloc, read_frame, NULL, picture_clean, close_file };