]> git.sesse.net Git - x264/blob - input/lavf.c
Add TFF/BFF detection to all demuxers
[x264] / input / lavf.c
1 /*****************************************************************************
2  * lavf.c: x264 libavformat input module
3  *****************************************************************************
4  * Copyright (C) 2009 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
24 #include "muxers.h"
25 #undef DECLARE_ALIGNED
26 #include <libavformat/avformat.h>
27 #include <libswscale/swscale.h>
28
29 typedef struct
30 {
31     AVFormatContext *lavf;
32     int stream_id;
33     int next_frame;
34     int vfr_input;
35     int vertical_flip;
36     struct SwsContext *scaler;
37     int pts_offset_flag;
38     int64_t pts_offset;
39     x264_picture_t *first_pic;
40
41     int init_width;
42     int init_height;
43
44     int cur_width;
45     int cur_height;
46     enum PixelFormat cur_pix_fmt;
47 } lavf_hnd_t;
48
49 typedef struct
50 {
51     AVFrame frame;
52     AVPacket packet;
53 } lavf_pic_t;
54
55 static int check_swscale( lavf_hnd_t *h, AVCodecContext *c, int i_frame )
56 {
57     if( h->scaler && (h->cur_width == c->width) && (h->cur_height == c->height) && (h->cur_pix_fmt == c->pix_fmt) )
58         return 0;
59     if( h->scaler )
60     {
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;
67     }
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 );
70     if( !h->scaler )
71     {
72         fprintf( stderr, "lavf [error]: could not open swscale context\n" );
73         return -1;
74     }
75     return 0;
76 }
77
78 static int read_frame_internal( x264_picture_t *p_pic, lavf_hnd_t *h, int i_frame, video_info_t *info )
79 {
80     if( h->first_pic && !info )
81     {
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. */
84         if( !i_frame )
85         {
86             XCHG( x264_image_t, p_pic->img, h->first_pic->img );
87             p_pic->i_pts = h->first_pic->i_pts;
88         }
89         lavf_input.picture_clean( h->first_pic );
90         free( h->first_pic );
91         h->first_pic = NULL;
92         if( !i_frame )
93             return 0;
94     }
95
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;
100
101     while( i_frame >= h->next_frame )
102     {
103         int finished = 0;
104         while( !finished && av_read_frame( h->lavf, pkt ) >= 0 )
105             if( pkt->stream_index == h->stream_id )
106             {
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 );
110             }
111         if( !finished )
112         {
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 );
115             if( !finished )
116                 return -1;
117         }
118         h->next_frame++;
119     }
120
121     if( check_swscale( h, c, i_frame ) )
122         return -1;
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 );
125
126     if( info )
127     {
128         info->interlaced = frame->interlaced_frame;
129         info->tff = frame->top_field_first;
130     }
131
132     if( h->vfr_input )
133     {
134         p_pic->i_pts = 0;
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
139         else if( info )
140         {
141             h->vfr_input = info->vfr = 0;
142             goto exit;
143         }
144         if( !h->pts_offset_flag )
145         {
146             h->pts_offset = p_pic->i_pts;
147             h->pts_offset_flag = 1;
148         }
149         p_pic->i_pts -= h->pts_offset;
150     }
151
152 exit:
153     if( pkt->destruct )
154         pkt->destruct( pkt );
155     avcodec_get_frame_defaults( frame );
156     return 0;
157 }
158
159 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
160 {
161     lavf_hnd_t *h = malloc( sizeof(lavf_hnd_t) );
162     if( !h )
163         return -1;
164     av_register_all();
165     h->scaler = NULL;
166     if( !strcmp( psz_filename, "-" ) )
167         psz_filename = "pipe:";
168
169     if( av_open_input_file( &h->lavf, psz_filename, NULL, 0, NULL ) )
170     {
171         fprintf( stderr, "lavf [error]: could not open input file\n" );
172         return -1;
173     }
174
175     if( av_find_stream_info( h->lavf ) < 0 )
176     {
177         fprintf( stderr, "lavf [error]: could not find input stream info\n" );
178         return -1;
179     }
180
181     int i = 0;
182     while( i < h->lavf->nb_streams && h->lavf->streams[i]->codec->codec_type != CODEC_TYPE_VIDEO )
183         i++;
184     if( i == h->lavf->nb_streams )
185     {
186         fprintf( stderr, "lavf [error]: could not find video stream\n" );
187         return -1;
188     }
189     h->stream_id       = i;
190     h->next_frame      = 0;
191     h->pts_offset_flag = 0;
192     h->pts_offset      = 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;
203
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;
208
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 ) );
212
213     if( avcodec_open( c, avcodec_find_decoder( c->codec_id ) ) )
214     {
215         fprintf( stderr, "lavf [error]: could not find decoder for video stream\n" );
216         return -1;
217     }
218
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 ) )
222     {
223         fprintf( stderr, "lavf [error]: malloc failed\n" );
224         return -1;
225     }
226     else if( read_frame_internal( h->first_pic, h, 0, info ) )
227         return -1;
228
229     info->sar_height = c->sample_aspect_ratio.den;
230     info->sar_width  = c->sample_aspect_ratio.num;
231     *p_handle = h;
232
233     return 0;
234 }
235
236 static int picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
237 {
238     if( x264_picture_alloc( pic, i_csp, i_width, i_height ) )
239         return -1;
240     lavf_pic_t *pic_h = pic->opaque = malloc( sizeof(lavf_pic_t) );
241     if( !pic_h )
242         return -1;
243     avcodec_get_frame_defaults( &pic_h->frame );
244     av_init_packet( &pic_h->packet );
245     return 0;
246 }
247
248 /* FIXME */
249 static int get_frame_total( hnd_t handle )
250 {
251     return 0;
252 }
253
254 static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
255 {
256     return read_frame_internal( p_pic, handle, i_frame, NULL );
257 }
258
259 static void picture_clean( x264_picture_t *pic )
260 {
261     free( pic->opaque );
262     x264_picture_clean( pic );
263 }
264
265 static int close_file( hnd_t handle )
266 {
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 );
271     free( h );
272     return 0;
273 }
274
275 const cli_input_t lavf_input = { open_file, get_frame_total, picture_alloc, read_frame, NULL, picture_clean, close_file };