PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/input/lavf.c

https://bitbucket.org/astrataro/x264_tmod
C | 339 lines | 262 code | 41 blank | 36 comment | 52 complexity | e32af8e6b8bcf9b1df1f5a234a5c6cd8 MD5 | raw file
  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. #include "input.h"
  27. #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "lavf", __VA_ARGS__ )
  28. #undef DECLARE_ALIGNED
  29. #include <libavformat/avformat.h>
  30. #include <libavutil/mem.h>
  31. #include <libavutil/pixdesc.h>
  32. #include <libavutil/dict.h>
  33. #if HAVE_AUDIO
  34. #include "audio/audio.h"
  35. #endif
  36. typedef struct
  37. {
  38. AVFormatContext *lavf;
  39. AVFrame *frame;
  40. int stream_id;
  41. int next_frame;
  42. int vfr_input;
  43. cli_pic_t *first_pic;
  44. #if HAVE_AUDIO
  45. char *filename;
  46. int has_audio;
  47. #endif
  48. } lavf_hnd_t;
  49. #define x264_free_packet( pkt )\
  50. {\
  51. av_free_packet( pkt );\
  52. av_init_packet( pkt );\
  53. }
  54. /* handle the deprecated jpeg pixel formats */
  55. static int handle_jpeg( int csp, int *fullrange )
  56. {
  57. switch( csp )
  58. {
  59. case AV_PIX_FMT_YUVJ420P: *fullrange = 1; return AV_PIX_FMT_YUV420P;
  60. case AV_PIX_FMT_YUVJ422P: *fullrange = 1; return AV_PIX_FMT_YUV422P;
  61. case AV_PIX_FMT_YUVJ444P: *fullrange = 1; return AV_PIX_FMT_YUV444P;
  62. default: return csp;
  63. }
  64. }
  65. static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, video_info_t *info )
  66. {
  67. if( h->first_pic && !info )
  68. {
  69. /* see if the frame we are requesting is the frame we have already read and stored.
  70. * if so, retrieve the pts and image data before freeing it. */
  71. if( !i_frame )
  72. {
  73. XCHG( cli_image_t, p_pic->img, h->first_pic->img );
  74. p_pic->pts = h->first_pic->pts;
  75. XCHG( void*, p_pic->opaque, h->first_pic->opaque );
  76. }
  77. lavf_input.release_frame( h->first_pic, NULL );
  78. lavf_input.picture_clean( h->first_pic );
  79. free( h->first_pic );
  80. h->first_pic = NULL;
  81. if( !i_frame )
  82. return 0;
  83. }
  84. AVCodecContext *c = h->lavf->streams[h->stream_id]->codec;
  85. AVPacket *pkt = p_pic->opaque;
  86. avcodec_get_frame_defaults( h->frame );
  87. while( i_frame >= h->next_frame )
  88. {
  89. int finished = 0;
  90. int ret = 0;
  91. do
  92. {
  93. ret = av_read_frame( h->lavf, pkt );
  94. if( pkt->stream_index == h->stream_id )
  95. {
  96. if( ret < 0 )
  97. pkt->size = 0;
  98. c->reordered_opaque = pkt->pts;
  99. if( avcodec_decode_video2( c, h->frame, &finished, pkt ) < 0 )
  100. x264_cli_log( "lavf", X264_LOG_WARNING, "video decoding failed on frame %d\n", h->next_frame );
  101. }
  102. /* if the packet successfully decoded but the data from it is not desired, free it */
  103. else if( ret >= 0 )
  104. x264_free_packet( pkt );
  105. } while( !finished && ret >= 0 );
  106. if( !finished )
  107. return -1;
  108. h->next_frame++;
  109. }
  110. memcpy( p_pic->img.stride, h->frame->linesize, sizeof(p_pic->img.stride) );
  111. memcpy( p_pic->img.plane, h->frame->data, sizeof(p_pic->img.plane) );
  112. int is_fullrange = 0;
  113. p_pic->img.width = c->width;
  114. p_pic->img.height = c->height;
  115. p_pic->img.csp = handle_jpeg( c->pix_fmt, &is_fullrange ) | X264_CSP_OTHER;
  116. if( info )
  117. {
  118. info->fullrange = is_fullrange;
  119. info->interlaced = h->frame->interlaced_frame;
  120. info->tff = h->frame->top_field_first;
  121. }
  122. if( h->vfr_input )
  123. {
  124. p_pic->pts = p_pic->duration = 0;
  125. if( c->has_b_frames && h->frame->reordered_opaque != AV_NOPTS_VALUE )
  126. p_pic->pts = h->frame->reordered_opaque;
  127. else if( pkt->dts != AV_NOPTS_VALUE )
  128. p_pic->pts = pkt->dts; // for AVI files
  129. else if( info )
  130. {
  131. h->vfr_input = info->vfr = 0;
  132. return 0;
  133. }
  134. }
  135. return 0;
  136. }
  137. static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
  138. {
  139. lavf_hnd_t *h = calloc( 1, sizeof(lavf_hnd_t) );
  140. if( !h )
  141. return -1;
  142. av_register_all();
  143. if( !strcmp( psz_filename, "-" ) )
  144. psz_filename = "pipe:";
  145. h->frame = avcodec_alloc_frame();
  146. if( !h->frame )
  147. return -1;
  148. /* if resolution was passed in, place it and colorspace into options. this allows raw video support */
  149. AVDictionary *options = NULL;
  150. if( opt->resolution )
  151. {
  152. av_dict_set( &options, "video_size", opt->resolution, 0 );
  153. const char *csp = opt->colorspace ? opt->colorspace : av_get_pix_fmt_name( AV_PIX_FMT_YUV420P );
  154. av_dict_set( &options, "pixel_format", csp, 0 );
  155. }
  156. /* specify the input format. this is helpful when lavf fails to guess */
  157. AVInputFormat *format = NULL;
  158. if( opt->format )
  159. FAIL_IF_ERROR( !(format = av_find_input_format( opt->format )), "unknown file format: %s\n", opt->format );
  160. FAIL_IF_ERROR( avformat_open_input( &h->lavf, psz_filename, format, &options ), "could not open input file\n" )
  161. if( options )
  162. av_dict_free( &options );
  163. FAIL_IF_ERROR( avformat_find_stream_info( h->lavf, NULL ) < 0, "could not find input stream info\n" )
  164. #if HAVE_AUDIO
  165. h->filename = strdup( psz_filename );
  166. int j = 0;
  167. while( j < h->lavf->nb_streams )
  168. h->has_audio |= !!(h->lavf->streams[j++]->codec->codec_type == AVMEDIA_TYPE_AUDIO );
  169. #endif
  170. int i = 0;
  171. while( i < h->lavf->nb_streams && h->lavf->streams[i]->codec->codec_type != AVMEDIA_TYPE_VIDEO )
  172. i++;
  173. FAIL_IF_ERROR( i == h->lavf->nb_streams, "could not find video stream\n" )
  174. h->stream_id = i;
  175. h->next_frame = 0;
  176. AVStream *s = h->lavf->streams[i];
  177. AVCodecContext *c = s->codec;
  178. info->fps_num = s->avg_frame_rate.num;
  179. info->fps_den = s->avg_frame_rate.den;
  180. info->timebase_num = s->time_base.num;
  181. info->timebase_den = s->time_base.den;
  182. /* lavf is thread unsafe as calling av_read_frame invalidates previously read AVPackets */
  183. info->thread_safe = 0;
  184. h->vfr_input = info->vfr;
  185. if( !opt->b_accurate_fps )
  186. x264_ntsc_fps( &info->fps_num, &info->fps_den );
  187. if( opt->demuxer_threads > 1 )
  188. c->thread_count = opt->demuxer_threads;
  189. AVCodec *p;
  190. if( opt->lavf_decoder )
  191. p = avcodec_find_decoder_by_name(opt->lavf_decoder);
  192. else
  193. p = avcodec_find_decoder(c->codec_id);
  194. AVDictionary *avcodec_opts = NULL;
  195. av_dict_set( &avcodec_opts, "strict", "-2", 0 );
  196. FAIL_IF_ERROR( avcodec_open2( c, p, &avcodec_opts ),
  197. "could not find decoder for video stream\n" )
  198. if( avcodec_opts )
  199. av_dict_free( &avcodec_opts );
  200. /* prefetch the first frame and set/confirm flags */
  201. h->first_pic = malloc( sizeof(cli_pic_t) );
  202. FAIL_IF_ERROR( !h->first_pic || lavf_input.picture_alloc( h->first_pic, X264_CSP_OTHER, info->width, info->height ),
  203. "malloc failed\n" )
  204. else if( read_frame_internal( h->first_pic, h, 0, info ) )
  205. return -1;
  206. info->width = c->width;
  207. info->height = c->height;
  208. info->csp = h->first_pic->img.csp;
  209. info->num_frames = s->nb_frames;
  210. info->sar_height = c->sample_aspect_ratio.den;
  211. info->sar_width = c->sample_aspect_ratio.num;
  212. info->fullrange |= c->color_range == AVCOL_RANGE_JPEG;
  213. /* -1 = 'unset' (internal) , 2 from lavf|ffms = 'unset' */
  214. if( c->colorspace >= 0 && c->colorspace <= 8 && c->colorspace != 2 )
  215. info->colormatrix = c->colorspace;
  216. else
  217. info->colormatrix = -1;
  218. /* avisynth stores rgb data vertically flipped. */
  219. if( !strcasecmp( get_filename_extension( psz_filename ), "avs" ) &&
  220. (c->pix_fmt == AV_PIX_FMT_BGRA || c->pix_fmt == AV_PIX_FMT_BGR24) )
  221. info->csp |= X264_CSP_VFLIP;
  222. /* show video info */
  223. double duration = s->duration * av_q2d(s->time_base);
  224. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(c->pix_fmt);
  225. x264_cli_log( "lavf", X264_LOG_INFO,
  226. "\n Format : %s"
  227. "\n Codec : %s ( %s )"
  228. "\n PixFmt : %s"
  229. "\n Framerate : %d/%d"
  230. "\n Timebase : %d/%d"
  231. "\n Duration : %d:%02d:%02d\n",
  232. format ? format->name : h->lavf->iformat->name,
  233. p->name, p->long_name,
  234. pix_desc->name,
  235. s->avg_frame_rate.num, s->avg_frame_rate.den,
  236. s->time_base.num, s->time_base.den,
  237. (int)duration / 60 / 60, (int)duration / 60 % 60, (int)duration - (int)duration / 60 * 60 );
  238. *p_handle = h;
  239. return 0;
  240. }
  241. static int picture_alloc( cli_pic_t *pic, int csp, int width, int height )
  242. {
  243. if( x264_cli_pic_alloc( pic, csp, width, height ) )
  244. return -1;
  245. pic->img.planes = 4;
  246. pic->opaque = malloc( sizeof(AVPacket) );
  247. if( !pic->opaque )
  248. return -1;
  249. av_init_packet( pic->opaque );
  250. return 0;
  251. }
  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. static int release_frame( cli_pic_t *pic, hnd_t handle )
  257. {
  258. x264_free_packet( pic->opaque );
  259. return 0;
  260. }
  261. static void picture_clean( cli_pic_t *pic )
  262. {
  263. free( pic->opaque );
  264. memset( pic, 0, sizeof(cli_pic_t) );
  265. }
  266. static int close_file( hnd_t handle )
  267. {
  268. lavf_hnd_t *h = handle;
  269. avcodec_close( h->lavf->streams[h->stream_id]->codec );
  270. avformat_close_input( &h->lavf );
  271. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 28, 0)
  272. avcodec_free_frame( &h->frame );
  273. #else
  274. av_freep( &h->frame );
  275. #endif
  276. #if HAVE_AUDIO
  277. free( h->filename );
  278. #endif
  279. free( h );
  280. return 0;
  281. }
  282. #if HAVE_AUDIO
  283. static hnd_t open_audio( hnd_t handle, int track )
  284. {
  285. lavf_hnd_t *h = handle;
  286. if( !x264_is_regular_file_path( h->filename ) )
  287. {
  288. x264_cli_log( "lavf", X264_LOG_WARNING, "reading audio from non-regular files is not implemented yet.\n" );
  289. return 0;
  290. }
  291. if( !h->has_audio )
  292. return 0;
  293. return x264_audio_open_from_file( NULL, h->filename, track );
  294. }
  295. const cli_input_t lavf_input = { open_file, picture_alloc, read_frame, release_frame, picture_clean, close_file, open_audio };
  296. #else
  297. const cli_input_t lavf_input = { open_file, picture_alloc, read_frame, release_frame, picture_clean, close_file };
  298. #endif