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