]> git.sesse.net Git - x264/blob - input/lavf.c
dc9fe55ffbc2b541a621d6e57b8e641ebb5bbce9
[x264] / input / lavf.c
1 /*****************************************************************************
2  * lavf.c: libavformat input
3  *****************************************************************************
4  * Copyright (C) 2009-2011 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/pixdesc.h>
32
33 typedef struct
34 {
35     AVFormatContext *lavf;
36     int stream_id;
37     int next_frame;
38     int vfr_input;
39     cli_pic_t *first_pic;
40 } lavf_hnd_t;
41
42 #define x264_free_packet( pkt )\
43 {\
44     av_free_packet( pkt );\
45     av_init_packet( pkt );\
46 }
47
48 static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, video_info_t *info )
49 {
50     if( h->first_pic && !info )
51     {
52         /* see if the frame we are requesting is the frame we have already read and stored.
53          * if so, retrieve the pts and image data before freeing it. */
54         if( !i_frame )
55         {
56             XCHG( cli_image_t, p_pic->img, h->first_pic->img );
57             p_pic->pts = h->first_pic->pts;
58             XCHG( void*, p_pic->opaque, h->first_pic->opaque );
59         }
60         lavf_input.release_frame( h->first_pic, NULL );
61         lavf_input.picture_clean( h->first_pic );
62         free( h->first_pic );
63         h->first_pic = NULL;
64         if( !i_frame )
65             return 0;
66     }
67
68     AVCodecContext *c = h->lavf->streams[h->stream_id]->codec;
69     AVPacket *pkt = p_pic->opaque;
70     AVFrame frame;
71     avcodec_get_frame_defaults( &frame );
72
73     while( i_frame >= h->next_frame )
74     {
75         int finished = 0;
76         int ret = 0;
77         do
78         {
79             ret = av_read_frame( h->lavf, pkt );
80
81             if( pkt->stream_index == h->stream_id )
82             {
83                 if( ret < 0 )
84                     pkt->size = 0;
85
86                 c->reordered_opaque = pkt->pts;
87                 if( avcodec_decode_video2( c, &frame, &finished, pkt ) < 0 )
88                     x264_cli_log( "lavf", X264_LOG_WARNING, "video decoding failed on frame %d\n", h->next_frame );
89             }
90             /* if the packet successfully decoded but the data from it is not desired, free it */
91             else if( ret >= 0 )
92                 x264_free_packet( pkt );
93         } while( !finished && ret >= 0 );
94
95         if( !finished )
96             return -1;
97
98         h->next_frame++;
99     }
100
101     memcpy( p_pic->img.stride, frame.linesize, sizeof(p_pic->img.stride) );
102     memcpy( p_pic->img.plane, frame.data, sizeof(p_pic->img.plane) );
103     p_pic->img.height  = c->height;
104     p_pic->img.csp     = c->pix_fmt | X264_CSP_OTHER;
105     p_pic->img.width   = c->width;
106
107     if( info )
108     {
109         info->interlaced = frame.interlaced_frame;
110         info->tff = frame.top_field_first;
111     }
112
113     if( h->vfr_input )
114     {
115         p_pic->pts = p_pic->duration = 0;
116         if( c->has_b_frames && frame.reordered_opaque != AV_NOPTS_VALUE )
117             p_pic->pts = frame.reordered_opaque;
118         else if( pkt->dts != AV_NOPTS_VALUE )
119             p_pic->pts = pkt->dts; // for AVI files
120         else if( info )
121         {
122             h->vfr_input = info->vfr = 0;
123             return 0;
124         }
125     }
126
127     return 0;
128 }
129
130 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
131 {
132     lavf_hnd_t *h = calloc( 1, sizeof(lavf_hnd_t) );
133     if( !h )
134         return -1;
135     av_register_all();
136     if( !strcmp( psz_filename, "-" ) )
137         psz_filename = "pipe:";
138
139     /* if resolution was passed in, parse it and colorspace into parameters. this allows raw video support */
140     AVFormatParameters *param = NULL;
141     if( opt->resolution )
142     {
143         param = calloc( 1, sizeof(AVFormatParameters) );
144         if( !param )
145             return -1;
146         sscanf( opt->resolution, "%dx%d", &param->width, &param->height );
147         param->pix_fmt = opt->colorspace ? av_get_pix_fmt( opt->colorspace ) : PIX_FMT_YUV420P;
148         FAIL_IF_ERROR( param->pix_fmt == PIX_FMT_NONE, "unsupported colorspace: %s\n", opt->colorspace );
149     }
150
151     /* specify the input format. this is helpful when lavf fails to guess */
152     AVInputFormat *format = NULL;
153     if( opt->format )
154         FAIL_IF_ERROR( !(format = av_find_input_format( opt->format )), "unknown file format: %s\n", opt->format );
155
156     FAIL_IF_ERROR( av_open_input_file( &h->lavf, psz_filename, format, 0, param ), "could not open input file\n" )
157     if( param )
158         free( param );
159     FAIL_IF_ERROR( av_find_stream_info( h->lavf ) < 0, "could not find input stream info\n" )
160
161     int i = 0;
162     while( i < h->lavf->nb_streams && h->lavf->streams[i]->codec->codec_type != AVMEDIA_TYPE_VIDEO )
163         i++;
164     FAIL_IF_ERROR( i == h->lavf->nb_streams, "could not find video stream\n" )
165     h->stream_id       = i;
166     h->next_frame      = 0;
167     AVCodecContext *c  = h->lavf->streams[i]->codec;
168     info->fps_num      = h->lavf->streams[i]->r_frame_rate.num;
169     info->fps_den      = h->lavf->streams[i]->r_frame_rate.den;
170     info->timebase_num = h->lavf->streams[i]->time_base.num;
171     info->timebase_den = h->lavf->streams[i]->time_base.den;
172     /* lavf is thread unsafe as calling av_read_frame invalidates previously read AVPackets */
173     info->thread_safe  = 0;
174     h->vfr_input       = info->vfr;
175     FAIL_IF_ERROR( avcodec_open( c, avcodec_find_decoder( c->codec_id ) ),
176                    "could not find decoder for video stream\n" )
177
178     /* prefetch the first frame and set/confirm flags */
179     h->first_pic = malloc( sizeof(cli_pic_t) );
180     FAIL_IF_ERROR( !h->first_pic || lavf_input.picture_alloc( h->first_pic, X264_CSP_OTHER, info->width, info->height ),
181                    "malloc failed\n" )
182     else if( read_frame_internal( h->first_pic, h, 0, info ) )
183         return -1;
184
185     info->width      = c->width;
186     info->height     = c->height;
187     info->csp        = h->first_pic->img.csp;
188     info->num_frames = h->lavf->streams[i]->nb_frames;
189     info->sar_height = c->sample_aspect_ratio.den;
190     info->sar_width  = c->sample_aspect_ratio.num;
191
192     /* avisynth stores rgb data vertically flipped. */
193     if( !strcasecmp( get_filename_extension( psz_filename ), "avs" ) &&
194         (c->pix_fmt == PIX_FMT_BGRA || c->pix_fmt == PIX_FMT_BGR24) )
195         info->csp |= X264_CSP_VFLIP;
196
197     *p_handle = h;
198
199     return 0;
200 }
201
202 static int picture_alloc( cli_pic_t *pic, int csp, int width, int height )
203 {
204     if( x264_cli_pic_alloc( pic, csp, width, height ) )
205         return -1;
206     pic->img.planes = 4;
207     pic->opaque = malloc( sizeof(AVPacket) );
208     if( !pic->opaque )
209         return -1;
210     av_init_packet( pic->opaque );
211     return 0;
212 }
213
214 static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
215 {
216     return read_frame_internal( pic, handle, i_frame, NULL );
217 }
218
219 static int release_frame( cli_pic_t *pic, hnd_t handle )
220 {
221     x264_free_packet( pic->opaque );
222     return 0;
223 }
224
225 static void picture_clean( cli_pic_t *pic )
226 {
227     free( pic->opaque );
228     memset( pic, 0, sizeof(cli_pic_t) );
229 }
230
231 static int close_file( hnd_t handle )
232 {
233     lavf_hnd_t *h = handle;
234     avcodec_close( h->lavf->streams[h->stream_id]->codec );
235     av_close_input_file( h->lavf );
236     free( h );
237     return 0;
238 }
239
240 const cli_input_t lavf_input = { open_file, picture_alloc, read_frame, release_frame, picture_clean, close_file };