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