lavf.c 8.9 KB

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