X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=input%2Flavf.c;h=104230eb9dffc9b34c2c9543ba454450abf459f4;hb=59683a97b50b34c6282457a959bb6b3e9e7f8c0d;hp=fc1172c312442ce2e36126dc0fff482ab07bf14b;hpb=ee9bc136e9e6f0875308c9505a08360294e7cd4a;p=x264 diff --git a/input/lavf.c b/input/lavf.c index fc1172c3..104230eb 100644 --- a/input/lavf.c +++ b/input/lavf.c @@ -1,7 +1,7 @@ /***************************************************************************** * lavf.c: libavformat input ***************************************************************************** - * Copyright (C) 2009-2011 x264 project + * Copyright (C) 2009-2015 x264 project * * Authors: Mike Gurlitz * Steven Walters @@ -28,21 +28,30 @@ #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "lavf", __VA_ARGS__ ) #undef DECLARE_ALIGNED #include +#include #include +#include typedef struct { AVFormatContext *lavf; + AVFrame *frame; int stream_id; int next_frame; int vfr_input; cli_pic_t *first_pic; } lavf_hnd_t; -#define x264_free_packet( pkt )\ -{\ - av_free_packet( pkt );\ - av_init_packet( pkt );\ +/* handle the deprecated jpeg pixel formats */ +static int handle_jpeg( int csp, int *fullrange ) +{ + switch( csp ) + { + case AV_PIX_FMT_YUVJ420P: *fullrange = 1; return AV_PIX_FMT_YUV420P; + case AV_PIX_FMT_YUVJ422P: *fullrange = 1; return AV_PIX_FMT_YUV422P; + case AV_PIX_FMT_YUVJ444P: *fullrange = 1; return AV_PIX_FMT_YUV444P; + default: return csp; + } } static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, video_info_t *info ) @@ -55,9 +64,7 @@ static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, vi { XCHG( cli_image_t, p_pic->img, h->first_pic->img ); p_pic->pts = h->first_pic->pts; - XCHG( void*, p_pic->opaque, h->first_pic->opaque ); } - lavf_input.release_frame( h->first_pic, NULL ); lavf_input.picture_clean( h->first_pic ); free( h->first_pic ); h->first_pic = NULL; @@ -66,9 +73,11 @@ static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, vi } AVCodecContext *c = h->lavf->streams[h->stream_id]->codec; - AVPacket *pkt = p_pic->opaque; - AVFrame frame; - avcodec_get_frame_defaults( &frame ); + + AVPacket pkt; + av_init_packet( &pkt ); + pkt.data = NULL; + pkt.size = 0; while( i_frame >= h->next_frame ) { @@ -76,20 +85,23 @@ static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, vi int ret = 0; do { - ret = av_read_frame( h->lavf, pkt ); + ret = av_read_frame( h->lavf, &pkt ); - if( pkt->stream_index == h->stream_id ) + if( ret < 0 ) { - if( ret < 0 ) - pkt->size = 0; + av_init_packet( &pkt ); + pkt.data = NULL; + pkt.size = 0; + } - c->reordered_opaque = pkt->pts; - if( avcodec_decode_video2( c, &frame, &finished, pkt ) < 0 ) + if( ret < 0 || pkt.stream_index == h->stream_id ) + { + if( avcodec_decode_video2( c, h->frame, &finished, &pkt ) < 0 ) x264_cli_log( "lavf", X264_LOG_WARNING, "video decoding failed on frame %d\n", h->next_frame ); } - /* if the packet successfully decoded but the data from it is not desired, free it */ - else if( ret >= 0 ) - x264_free_packet( pkt ); + + if( ret >= 0 ) + av_free_packet( &pkt ); } while( !finished && ret >= 0 ); if( !finished ) @@ -98,25 +110,27 @@ static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, vi h->next_frame++; } - memcpy( p_pic->img.stride, frame.linesize, sizeof(p_pic->img.stride) ); - memcpy( p_pic->img.plane, frame.data, sizeof(p_pic->img.plane) ); - p_pic->img.height = c->height; - p_pic->img.csp = c->pix_fmt | X264_CSP_OTHER; + memcpy( p_pic->img.stride, h->frame->linesize, sizeof(p_pic->img.stride) ); + memcpy( p_pic->img.plane, h->frame->data, sizeof(p_pic->img.plane) ); + int is_fullrange = 0; p_pic->img.width = c->width; + p_pic->img.height = c->height; + p_pic->img.csp = handle_jpeg( c->pix_fmt, &is_fullrange ) | X264_CSP_OTHER; if( info ) { - info->interlaced = frame.interlaced_frame; - info->tff = frame.top_field_first; + info->fullrange = is_fullrange; + info->interlaced = h->frame->interlaced_frame; + info->tff = h->frame->top_field_first; } if( h->vfr_input ) { p_pic->pts = p_pic->duration = 0; - if( c->has_b_frames && frame.reordered_opaque != AV_NOPTS_VALUE ) - p_pic->pts = frame.reordered_opaque; - else if( pkt->dts != AV_NOPTS_VALUE ) - p_pic->pts = pkt->dts; // for AVI files + if( h->frame->pkt_pts != AV_NOPTS_VALUE ) + p_pic->pts = h->frame->pkt_pts; + else if( h->frame->pkt_dts != AV_NOPTS_VALUE ) + p_pic->pts = h->frame->pkt_dts; // for AVI files else if( info ) { h->vfr_input = info->vfr = 0; @@ -136,37 +150,44 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c if( !strcmp( psz_filename, "-" ) ) psz_filename = "pipe:"; - /* if resolution was passed in, parse it and colorspace into parameters. this allows raw video support */ - AVFormatParameters *param = NULL; + h->frame = av_frame_alloc(); + if( !h->frame ) + return -1; + + /* if resolution was passed in, place it and colorspace into options. this allows raw video support */ + AVDictionary *options = NULL; if( opt->resolution ) { - param = calloc( 1, sizeof(AVFormatParameters) ); - if( !param ) - return -1; - sscanf( opt->resolution, "%dx%d", ¶m->width, ¶m->height ); - param->pix_fmt = opt->colorspace ? av_get_pix_fmt( opt->colorspace ) : PIX_FMT_YUV420P; + av_dict_set( &options, "video_size", opt->resolution, 0 ); + const char *csp = opt->colorspace ? opt->colorspace : av_get_pix_fmt_name( AV_PIX_FMT_YUV420P ); + av_dict_set( &options, "pixel_format", csp, 0 ); } - FAIL_IF_ERROR( av_open_input_file( &h->lavf, psz_filename, NULL, 0, param ), "could not open input file\n" ) - if( param ) - free( param ); - FAIL_IF_ERROR( av_find_stream_info( h->lavf ) < 0, "could not find input stream info\n" ) + /* specify the input format. this is helpful when lavf fails to guess */ + AVInputFormat *format = NULL; + if( opt->format ) + FAIL_IF_ERROR( !(format = av_find_input_format( opt->format )), "unknown file format: %s\n", opt->format ); + + FAIL_IF_ERROR( avformat_open_input( &h->lavf, psz_filename, format, &options ), "could not open input file\n" ) + if( options ) + av_dict_free( &options ); + FAIL_IF_ERROR( avformat_find_stream_info( h->lavf, NULL ) < 0, "could not find input stream info\n" ) int i = 0; - while( i < h->lavf->nb_streams && h->lavf->streams[i]->codec->codec_type != CODEC_TYPE_VIDEO ) + while( i < h->lavf->nb_streams && h->lavf->streams[i]->codec->codec_type != AVMEDIA_TYPE_VIDEO ) i++; FAIL_IF_ERROR( i == h->lavf->nb_streams, "could not find video stream\n" ) h->stream_id = i; h->next_frame = 0; AVCodecContext *c = h->lavf->streams[i]->codec; - info->fps_num = h->lavf->streams[i]->r_frame_rate.num; - info->fps_den = h->lavf->streams[i]->r_frame_rate.den; + info->fps_num = h->lavf->streams[i]->avg_frame_rate.num; + info->fps_den = h->lavf->streams[i]->avg_frame_rate.den; info->timebase_num = h->lavf->streams[i]->time_base.num; info->timebase_den = h->lavf->streams[i]->time_base.den; /* lavf is thread unsafe as calling av_read_frame invalidates previously read AVPackets */ info->thread_safe = 0; h->vfr_input = info->vfr; - FAIL_IF_ERROR( avcodec_open( c, avcodec_find_decoder( c->codec_id ) ), + FAIL_IF_ERROR( avcodec_open2( c, avcodec_find_decoder( c->codec_id ), NULL ), "could not find decoder for video stream\n" ) /* prefetch the first frame and set/confirm flags */ @@ -182,10 +203,11 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c info->num_frames = h->lavf->streams[i]->nb_frames; info->sar_height = c->sample_aspect_ratio.den; info->sar_width = c->sample_aspect_ratio.num; + info->fullrange |= c->color_range == AVCOL_RANGE_JPEG; /* avisynth stores rgb data vertically flipped. */ if( !strcasecmp( get_filename_extension( psz_filename ), "avs" ) && - (c->pix_fmt == PIX_FMT_BGRA || c->pix_fmt == PIX_FMT_BGR24) ) + (c->pix_fmt == AV_PIX_FMT_BGRA || c->pix_fmt == AV_PIX_FMT_BGR24) ) info->csp |= X264_CSP_VFLIP; *p_handle = h; @@ -195,13 +217,10 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c static int picture_alloc( cli_pic_t *pic, int csp, int width, int height ) { - if( x264_cli_pic_alloc( pic, csp, width, height ) ) + if( x264_cli_pic_alloc( pic, X264_CSP_NONE, width, height ) ) return -1; + pic->img.csp = csp; pic->img.planes = 4; - pic->opaque = malloc( sizeof(AVPacket) ); - if( !pic->opaque ) - return -1; - av_init_packet( pic->opaque ); return 0; } @@ -210,15 +229,8 @@ static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame ) return read_frame_internal( pic, handle, i_frame, NULL ); } -static int release_frame( cli_pic_t *pic, hnd_t handle ) -{ - x264_free_packet( pic->opaque ); - return 0; -} - static void picture_clean( cli_pic_t *pic ) { - free( pic->opaque ); memset( pic, 0, sizeof(cli_pic_t) ); } @@ -226,9 +238,10 @@ static int close_file( hnd_t handle ) { lavf_hnd_t *h = handle; avcodec_close( h->lavf->streams[h->stream_id]->codec ); - av_close_input_file( h->lavf ); + avformat_close_input( &h->lavf ); + av_frame_free( &h->frame ); free( h ); return 0; } -const cli_input_t lavf_input = { open_file, picture_alloc, read_frame, release_frame, picture_clean, close_file }; +const cli_input_t lavf_input = { open_file, picture_alloc, read_frame, NULL, picture_clean, close_file };