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