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