PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/demux/rawvid.c

https://gitlab.com/evilbinary/vlc
C | 439 lines | 314 code | 57 blank | 68 comment | 51 complexity | 6b1f7a374fc87a6e453507d0ec53bec2 MD5 | raw file
  1. /*****************************************************************************
  2. * rawvid.c : raw video input module for vlc
  3. *****************************************************************************
  4. * Copyright (C) 2007 VLC authors and VideoLAN
  5. * $Id: 11d27a145bb1dc1134d78a354482816b79cf89de $
  6. *
  7. * Authors: Gildas Bazin <gbazin@videolan.org>
  8. * Antoine Cellerier <dionoea at videolan d.t org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this program; if not, write to the Free Software Foundation,
  22. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23. *****************************************************************************/
  24. /*****************************************************************************
  25. * Preamble
  26. *****************************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. # include "config.h"
  29. #endif
  30. #include <vlc_common.h>
  31. #include <vlc_plugin.h>
  32. #include <vlc_demux.h>
  33. #include <assert.h>
  34. /*****************************************************************************
  35. * Module descriptor
  36. *****************************************************************************/
  37. static int Open ( vlc_object_t * );
  38. static void Close( vlc_object_t * );
  39. #define FPS_TEXT N_("Frames per Second")
  40. #define FPS_LONGTEXT N_("This is the desired frame rate when " \
  41. "playing raw video streams. In the form 30000/1001 or 29.97")
  42. #define WIDTH_TEXT N_("Width")
  43. #define WIDTH_LONGTEXT N_("This specifies the width in pixels of the raw " \
  44. "video stream.")
  45. #define HEIGHT_TEXT N_("Height")
  46. #define HEIGHT_LONGTEXT N_("This specifies the height in pixels of the raw " \
  47. "video stream.")
  48. #define CHROMA_TEXT N_("Force chroma (Use carefully)")
  49. #define CHROMA_LONGTEXT N_("Force chroma. This is a four character string.")
  50. #define ASPECT_RATIO_TEXT N_("Aspect ratio")
  51. #define ASPECT_RATIO_LONGTEXT N_( \
  52. "Aspect ratio (4:3, 16:9). Default assumes square pixels." )
  53. vlc_module_begin ()
  54. set_shortname( "Raw Video" )
  55. set_description( N_("Raw video demuxer") )
  56. set_capability( "demux", 10 )
  57. set_category( CAT_INPUT )
  58. set_subcategory( SUBCAT_INPUT_DEMUX )
  59. set_callbacks( Open, Close )
  60. add_shortcut( "rawvideo" )
  61. add_string( "rawvid-fps", NULL, FPS_TEXT, FPS_LONGTEXT, false )
  62. add_integer( "rawvid-width", 0, WIDTH_TEXT, WIDTH_LONGTEXT, 0 )
  63. add_integer( "rawvid-height", 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, 0 )
  64. add_string( "rawvid-chroma", NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
  65. true )
  66. add_string( "rawvid-aspect-ratio", NULL,
  67. ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true )
  68. vlc_module_end ()
  69. /*****************************************************************************
  70. * Definitions of structures used by this plugin
  71. *****************************************************************************/
  72. struct demux_sys_t
  73. {
  74. int frame_size;
  75. es_out_id_t *p_es_video;
  76. es_format_t fmt_video;
  77. date_t pcr;
  78. bool b_y4m;
  79. };
  80. /*****************************************************************************
  81. * Local prototypes
  82. *****************************************************************************/
  83. static int Demux( demux_t * );
  84. static int Control( demux_t *, int i_query, va_list args );
  85. struct preset_t
  86. {
  87. const char *psz_ext;
  88. int i_width;
  89. int i_height;
  90. unsigned u_fps_num;
  91. unsigned u_fps_den;
  92. unsigned u_ar_num;
  93. unsigned u_ar_den;
  94. vlc_fourcc_t i_chroma;
  95. };
  96. static const struct preset_t p_presets[] =
  97. {
  98. { "sqcif", 128, 96, 30000, 1001, 4,3, VLC_CODEC_YV12 },
  99. { "qcif", 176, 144, 30000, 1001, 4,3, VLC_CODEC_YV12 },
  100. { "cif", 352, 288, 30000, 1001, 4,3, VLC_CODEC_YV12 },
  101. { "4cif", 704, 576, 30000, 1001, 4,3, VLC_CODEC_YV12 },
  102. { "16cif", 1408, 1152, 30000, 1001, 4,3, VLC_CODEC_YV12 },
  103. { "yuv", 176, 144, 25, 1, 4,3, VLC_CODEC_YV12 },
  104. { NULL, 0, 0, 0, 0, 0,0, 0 }
  105. };
  106. /*****************************************************************************
  107. * Open: initializes raw DV demux structures
  108. *****************************************************************************/
  109. static int Open( vlc_object_t * p_this )
  110. {
  111. demux_t *p_demux = (demux_t*)p_this;
  112. demux_sys_t *p_sys;
  113. int i_width=-1, i_height=-1;
  114. unsigned u_fps_num, u_fps_den;
  115. vlc_fourcc_t i_chroma = 0;
  116. unsigned int i_sar_num;
  117. unsigned int i_sar_den;
  118. const struct preset_t *p_preset = NULL;
  119. const uint8_t *p_peek;
  120. bool b_y4m = false;
  121. if( vlc_stream_Peek( p_demux->s, &p_peek, 9 ) == 9 )
  122. {
  123. /* http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 */
  124. if( !strncmp( (char *)p_peek, "YUV4MPEG2", 9 ) )
  125. {
  126. b_y4m = true;
  127. goto valid;
  128. }
  129. }
  130. if( !p_demux->obj.force )
  131. {
  132. /* guess preset based on file extension */
  133. if( !p_demux->psz_file )
  134. return VLC_EGENERIC;
  135. const char *psz_ext = strrchr( p_demux->psz_file, '.' );
  136. if( !psz_ext )
  137. return VLC_EGENERIC;
  138. psz_ext++;
  139. for( unsigned i = 0; p_presets[i].psz_ext ; i++ )
  140. {
  141. if( !strcasecmp( psz_ext, p_presets[i].psz_ext ) )
  142. {
  143. p_preset = &p_presets[i];
  144. goto valid;
  145. }
  146. }
  147. return VLC_EGENERIC;
  148. }
  149. valid:
  150. p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
  151. if( !p_sys )
  152. return VLC_ENOMEM;
  153. p_sys->b_y4m = b_y4m;
  154. /* guess the parameters based on the preset */
  155. if( p_preset )
  156. {
  157. i_width = p_preset->i_width;
  158. i_height = p_preset->i_height;
  159. u_fps_num = p_preset->u_fps_num;
  160. u_fps_den = p_preset->u_fps_den;
  161. i_sar_num = p_preset->u_ar_num * p_preset->i_height;
  162. i_sar_den = p_preset->u_ar_den * p_preset->i_width;
  163. i_chroma = p_preset->i_chroma;
  164. }
  165. /* override presets if yuv4mpeg2 */
  166. if( b_y4m )
  167. {
  168. /* The string should start with "YUV4MPEG2" */
  169. char *psz = vlc_stream_ReadLine( p_demux->s );
  170. char *psz_buf;
  171. int a = 1;
  172. int b = 1;
  173. if( unlikely(psz == NULL) )
  174. goto error;
  175. /* NB, it is not possible to handle interlaced here, since the
  176. * interlaced picture flags are in picture_t not block_t */
  177. #define READ_FRAC( key, num, den ) do { \
  178. psz_buf = strstr( psz+9, key );\
  179. if( psz_buf )\
  180. {\
  181. char *end = strchr( psz_buf+1, ' ' );\
  182. char *sep;\
  183. if( end ) *end = '\0';\
  184. sep = strchr( psz_buf+1, ':' );\
  185. if( sep )\
  186. {\
  187. *sep = '\0';\
  188. den = atoi( sep+1 );\
  189. }\
  190. else\
  191. {\
  192. den = 1;\
  193. }\
  194. num = atoi( psz_buf+2 );\
  195. if( sep ) *sep = ':';\
  196. if( end ) *end = ' ';\
  197. } } while(0)
  198. READ_FRAC( " W", i_width, a );
  199. READ_FRAC( " H", i_height, a );
  200. READ_FRAC( " F", u_fps_num, u_fps_den );
  201. READ_FRAC( " A", a, b );
  202. #undef READ_FRAC
  203. if( b != 0 )
  204. {
  205. i_sar_num = a;
  206. i_sar_den = b;
  207. }
  208. psz_buf = strstr( psz+9, " C" );
  209. if( psz_buf )
  210. {
  211. static const struct { const char *psz_name; vlc_fourcc_t i_fcc; } formats[] =
  212. {
  213. { "420jpeg", VLC_CODEC_I420 },
  214. { "420paldv", VLC_CODEC_I420 },
  215. { "420", VLC_CODEC_I420 },
  216. { "422", VLC_CODEC_I422 },
  217. { "444", VLC_CODEC_I444 },
  218. { "mono", VLC_CODEC_GREY },
  219. { NULL, 0 }
  220. };
  221. bool b_found = false;
  222. char *psz_end = strchr( psz_buf+1, ' ' );
  223. if( psz_end )
  224. *psz_end = '\0';
  225. psz_buf += 2;
  226. for( int i = 0; formats[i].psz_name != NULL; i++ )
  227. {
  228. if( !strncmp( psz_buf, formats[i].psz_name, strlen(formats[i].psz_name) ) )
  229. {
  230. i_chroma = formats[i].i_fcc;
  231. b_found = true;
  232. break;
  233. }
  234. }
  235. if( !b_found )
  236. msg_Warn( p_demux, "Unknown YUV4MPEG2 chroma type \"%s\"", psz_buf );
  237. if( psz_end )
  238. *psz_end = ' ';
  239. }
  240. free( psz );
  241. }
  242. /* allow the user to override anything guessed from the input */
  243. int i_tmp;
  244. i_tmp = var_CreateGetInteger( p_demux, "rawvid-width" );
  245. if( i_tmp ) i_width = i_tmp;
  246. i_tmp = var_CreateGetInteger( p_demux, "rawvid-height" );
  247. if( i_tmp ) i_height = i_tmp;
  248. char *psz_tmp;
  249. psz_tmp = var_CreateGetNonEmptyString( p_demux, "rawvid-chroma" );
  250. if( psz_tmp )
  251. {
  252. if( strlen( psz_tmp ) != 4 )
  253. {
  254. msg_Err( p_demux, "Invalid fourcc format/chroma specification %s"
  255. " expecting four characters eg, UYVY", psz_tmp );
  256. free( psz_tmp );
  257. goto error;
  258. }
  259. memcpy( &i_chroma, psz_tmp, 4 );
  260. msg_Dbg( p_demux, "Forcing chroma to 0x%.8x (%4.4s)", i_chroma, (char*)&i_chroma );
  261. free( psz_tmp );
  262. }
  263. if( var_InheritURational( p_demux, &u_fps_num, &u_fps_den, "rawvid-fps" ) )
  264. {
  265. u_fps_num = 0;
  266. u_fps_den = 1;
  267. }
  268. if( var_InheritURational( p_demux, &i_sar_num, &i_sar_den,
  269. "rawvid-aspect-ratio" ) )
  270. i_sar_num = i_sar_den = 1;
  271. /* moan about anything wrong */
  272. if( i_width <= 0 || i_height <= 0 )
  273. {
  274. msg_Err( p_demux, "width and height must be strictly positive." );
  275. goto error;
  276. }
  277. if( !u_fps_num || !u_fps_den )
  278. {
  279. msg_Err( p_demux, "invalid or no framerate specified." );
  280. goto error;
  281. }
  282. if( i_chroma == 0 )
  283. {
  284. msg_Err( p_demux, "invalid or no chroma specified." );
  285. goto error;
  286. }
  287. /* fixup anything missing with sensible assumptions */
  288. if( i_sar_num <= 0 || i_sar_den <= 0 )
  289. {
  290. /* assume 1:1 sar */
  291. i_sar_num = 1;
  292. i_sar_den = 1;
  293. }
  294. es_format_Init( &p_sys->fmt_video, VIDEO_ES, i_chroma );
  295. video_format_Setup( &p_sys->fmt_video.video, i_chroma,
  296. i_width, i_height, i_width, i_height,
  297. i_sar_num, i_sar_den );
  298. vlc_ureduce( &p_sys->fmt_video.video.i_frame_rate,
  299. &p_sys->fmt_video.video.i_frame_rate_base,
  300. u_fps_num, u_fps_den, 0);
  301. date_Init( &p_sys->pcr, p_sys->fmt_video.video.i_frame_rate,
  302. p_sys->fmt_video.video.i_frame_rate_base );
  303. date_Set( &p_sys->pcr, 0 );
  304. if( !p_sys->fmt_video.video.i_bits_per_pixel )
  305. {
  306. msg_Err( p_demux, "Unsupported chroma 0x%.8x (%4.4s)", i_chroma,
  307. (char*)&i_chroma );
  308. goto error;
  309. }
  310. p_sys->frame_size = i_width * i_height
  311. * p_sys->fmt_video.video.i_bits_per_pixel / 8;
  312. p_sys->p_es_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
  313. p_demux->pf_demux = Demux;
  314. p_demux->pf_control = Control;
  315. return VLC_SUCCESS;
  316. error:
  317. /* workaround, but y4m uses vlc_stream_ReadLine */
  318. vlc_stream_Seek( p_demux->s, 0 );
  319. free( p_sys );
  320. return VLC_EGENERIC;
  321. }
  322. /*****************************************************************************
  323. * Close: frees unused data
  324. *****************************************************************************/
  325. static void Close( vlc_object_t *p_this )
  326. {
  327. demux_t *p_demux = (demux_t*)p_this;
  328. demux_sys_t *p_sys = p_demux->p_sys;
  329. free( p_sys );
  330. }
  331. /*****************************************************************************
  332. * Demux: reads and demuxes data packets
  333. *****************************************************************************
  334. * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
  335. *****************************************************************************/
  336. static int Demux( demux_t *p_demux )
  337. {
  338. demux_sys_t *p_sys = p_demux->p_sys;
  339. block_t *p_block;
  340. mtime_t i_pcr = date_Get( &p_sys->pcr );
  341. /* Call the pace control */
  342. es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + i_pcr );
  343. if( p_sys->b_y4m )
  344. {
  345. /* Skip the frame header */
  346. /* Skip "FRAME" */
  347. if( vlc_stream_Read( p_demux->s, NULL, 5 ) < 5 )
  348. return 0;
  349. /* Find \n */
  350. for( ;; )
  351. {
  352. uint8_t b;
  353. if( vlc_stream_Read( p_demux->s, &b, 1 ) < 1 )
  354. return 0;
  355. if( b == 0x0a )
  356. break;
  357. }
  358. }
  359. p_block = vlc_stream_Block( p_demux->s, p_sys->frame_size );
  360. if( p_block == NULL )
  361. {
  362. /* EOF */
  363. return 0;
  364. }
  365. p_block->i_dts = p_block->i_pts = VLC_TS_0 + i_pcr;
  366. es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
  367. date_Increment( &p_sys->pcr, 1 );
  368. return 1;
  369. }
  370. /*****************************************************************************
  371. * Control:
  372. *****************************************************************************/
  373. static int Control( demux_t *p_demux, int i_query, va_list args )
  374. {
  375. demux_sys_t *p_sys = p_demux->p_sys;
  376. /* (2**31)-1 is insufficient to store 1080p50 4:4:4. */
  377. const int64_t i_bps = 8LL * p_sys->frame_size * p_sys->pcr.i_divider_num /
  378. p_sys->pcr.i_divider_den;
  379. /* XXX: DEMUX_SET_TIME is precise here */
  380. return demux_vaControlHelper( p_demux->s, 0, -1, i_bps,
  381. p_sys->frame_size, i_query, args );
  382. }