ffms.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*****************************************************************************
  2. * ffms.c: ffmpegsource 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. * Henrik Gramner <henrik@gramner.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  23. *
  24. * This program is also available under a commercial proprietary license.
  25. * For more information, contact us at licensing@x264.com.
  26. *****************************************************************************/
  27. #include "input.h"
  28. #include <ffms.h>
  29. #undef DECLARE_ALIGNED
  30. #include <libavcodec/avcodec.h>
  31. #include <libswscale/swscale.h>
  32. #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "ffms", __VA_ARGS__ )
  33. #define PROGRESS_LENGTH 36
  34. typedef struct
  35. {
  36. FFMS_VideoSource *video_source;
  37. FFMS_Track *track;
  38. int reduce_pts;
  39. int vfr_input;
  40. int num_frames;
  41. int64_t time;
  42. } ffms_hnd_t;
  43. static int FFMS_CC update_progress( int64_t current, int64_t total, void *private )
  44. {
  45. int64_t *update_time = private;
  46. int64_t oldtime = *update_time;
  47. int64_t newtime = x264_mdate();
  48. if( oldtime && newtime - oldtime < UPDATE_INTERVAL )
  49. return 0;
  50. *update_time = newtime;
  51. char buf[PROGRESS_LENGTH+5+1];
  52. snprintf( buf, sizeof(buf), "ffms [info]: indexing input file [%.1f%%]", 100.0 * current / total );
  53. fprintf( stderr, "%-*s\r", PROGRESS_LENGTH, buf+5 );
  54. x264_cli_set_console_title( buf );
  55. fflush( stderr );
  56. return 0;
  57. }
  58. /* handle the deprecated jpeg pixel formats */
  59. static int handle_jpeg( int csp, int *fullrange )
  60. {
  61. switch( csp )
  62. {
  63. case AV_PIX_FMT_YUVJ420P: *fullrange = 1; return AV_PIX_FMT_YUV420P;
  64. case AV_PIX_FMT_YUVJ422P: *fullrange = 1; return AV_PIX_FMT_YUV422P;
  65. case AV_PIX_FMT_YUVJ444P: *fullrange = 1; return AV_PIX_FMT_YUV444P;
  66. default: return csp;
  67. }
  68. }
  69. static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
  70. {
  71. ffms_hnd_t *h = calloc( 1, sizeof(ffms_hnd_t) );
  72. if( !h )
  73. return -1;
  74. FFMS_Init( 0, 1 );
  75. FFMS_ErrorInfo e;
  76. e.BufferSize = 0;
  77. int seekmode = opt->seek ? FFMS_SEEK_NORMAL : FFMS_SEEK_LINEAR_NO_RW;
  78. FFMS_Index *idx = NULL;
  79. if( opt->index_file )
  80. {
  81. x264_struct_stat index_s, input_s;
  82. if( !x264_stat( opt->index_file, &index_s ) && !x264_stat( psz_filename, &input_s ) && input_s.st_mtime < index_s.st_mtime )
  83. {
  84. idx = FFMS_ReadIndex( opt->index_file, &e );
  85. if( idx && FFMS_IndexBelongsToFile( idx, psz_filename, &e ) )
  86. {
  87. FFMS_DestroyIndex( idx );
  88. idx = NULL;
  89. }
  90. }
  91. }
  92. if( !idx )
  93. {
  94. FFMS_Indexer *indexer = FFMS_CreateIndexer( psz_filename, &e );
  95. FAIL_IF_ERROR( !indexer, "could not create indexer\n" );
  96. if( opt->progress )
  97. FFMS_SetProgressCallback( indexer, update_progress, &h->time );
  98. idx = FFMS_DoIndexing2( indexer, FFMS_IEH_ABORT, &e );
  99. fprintf( stderr, "%*c", PROGRESS_LENGTH+1, '\r' );
  100. FAIL_IF_ERROR( !idx, "could not create index\n" );
  101. if( opt->index_file && FFMS_WriteIndex( opt->index_file, idx, &e ) )
  102. x264_cli_log( "ffms", X264_LOG_WARNING, "could not write index file\n" );
  103. }
  104. int trackno = FFMS_GetFirstTrackOfType( idx, FFMS_TYPE_VIDEO, &e );
  105. if( trackno >= 0 )
  106. h->video_source = FFMS_CreateVideoSource( psz_filename, trackno, idx, 1, seekmode, &e );
  107. FFMS_DestroyIndex( idx );
  108. FAIL_IF_ERROR( trackno < 0, "could not find video track\n" );
  109. FAIL_IF_ERROR( !h->video_source, "could not create video source\n" );
  110. const FFMS_VideoProperties *videop = FFMS_GetVideoProperties( h->video_source );
  111. info->num_frames = h->num_frames = videop->NumFrames;
  112. info->sar_height = videop->SARDen;
  113. info->sar_width = videop->SARNum;
  114. info->fps_den = videop->FPSDenominator;
  115. info->fps_num = videop->FPSNumerator;
  116. h->vfr_input = info->vfr;
  117. /* ffms is thread unsafe as it uses a single frame buffer for all frame requests */
  118. info->thread_safe = 0;
  119. const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, 0, &e );
  120. FAIL_IF_ERROR( !frame, "could not read frame 0\n" );
  121. info->fullrange = 0;
  122. info->width = frame->EncodedWidth;
  123. info->height = frame->EncodedHeight;
  124. info->csp = handle_jpeg( frame->EncodedPixelFormat, &info->fullrange ) | X264_CSP_OTHER;
  125. info->interlaced = frame->InterlacedFrame;
  126. info->tff = frame->TopFieldFirst;
  127. info->fullrange |= frame->ColorRange == FFMS_CR_JPEG;
  128. /* ffms timestamps are in milliseconds. ffms also uses int64_ts for timebase,
  129. * so we need to reduce large timebases to prevent overflow */
  130. if( h->vfr_input )
  131. {
  132. h->track = FFMS_GetTrackFromVideo( h->video_source );
  133. const FFMS_TrackTimeBase *timebase = FFMS_GetTimeBase( h->track );
  134. int64_t timebase_num = timebase->Num;
  135. int64_t timebase_den = timebase->Den * 1000;
  136. h->reduce_pts = 0;
  137. while( timebase_num > UINT32_MAX || timebase_den > INT32_MAX )
  138. {
  139. timebase_num >>= 1;
  140. timebase_den >>= 1;
  141. h->reduce_pts++;
  142. }
  143. info->timebase_num = timebase_num;
  144. info->timebase_den = timebase_den;
  145. }
  146. *p_handle = h;
  147. return 0;
  148. }
  149. static int picture_alloc( cli_pic_t *pic, hnd_t handle, int csp, int width, int height )
  150. {
  151. if( x264_cli_pic_alloc( pic, X264_CSP_NONE, width, height ) )
  152. return -1;
  153. pic->img.csp = csp;
  154. pic->img.planes = 4;
  155. return 0;
  156. }
  157. static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
  158. {
  159. ffms_hnd_t *h = handle;
  160. if( i_frame >= h->num_frames )
  161. return -1;
  162. FFMS_ErrorInfo e;
  163. e.BufferSize = 0;
  164. const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, i_frame, &e );
  165. FAIL_IF_ERROR( !frame, "could not read frame %d \n", i_frame );
  166. memcpy( pic->img.stride, frame->Linesize, sizeof(pic->img.stride) );
  167. memcpy( pic->img.plane, frame->Data, sizeof(pic->img.plane) );
  168. if( h->vfr_input )
  169. {
  170. const FFMS_FrameInfo *info = FFMS_GetFrameInfo( h->track, i_frame );
  171. FAIL_IF_ERROR( info->PTS == AV_NOPTS_VALUE, "invalid timestamp. "
  172. "Use --force-cfr and specify a framerate with --fps\n" );
  173. pic->pts = info->PTS >> h->reduce_pts;
  174. pic->duration = 0;
  175. }
  176. return 0;
  177. }
  178. static void picture_clean( cli_pic_t *pic, hnd_t handle )
  179. {
  180. memset( pic, 0, sizeof(cli_pic_t) );
  181. }
  182. static int close_file( hnd_t handle )
  183. {
  184. ffms_hnd_t *h = handle;
  185. FFMS_DestroyVideoSource( h->video_source );
  186. free( h );
  187. return 0;
  188. }
  189. const cli_input_t ffms_input = { open_file, picture_alloc, read_frame, NULL, picture_clean, close_file };