raw.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*****************************************************************************
  2. * raw.c: raw input
  3. *****************************************************************************
  4. * Copyright (C) 2003-2018 x264 project
  5. *
  6. * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  7. * Loren Merritt <lorenm@u.washington.edu>
  8. * Steven Walters <kemuri9@gmail.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. #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "raw", __VA_ARGS__ )
  29. typedef struct
  30. {
  31. FILE *fh;
  32. int next_frame;
  33. uint64_t plane_size[4];
  34. uint64_t frame_size;
  35. int bit_depth;
  36. cli_mmap_t mmap;
  37. int use_mmap;
  38. } raw_hnd_t;
  39. static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
  40. {
  41. raw_hnd_t *h = calloc( 1, sizeof(raw_hnd_t) );
  42. if( !h )
  43. return -1;
  44. if( !opt->resolution )
  45. {
  46. /* try to parse the file name */
  47. for( char *p = psz_filename; *p; p++ )
  48. if( *p >= '0' && *p <= '9' && sscanf( p, "%dx%d", &info->width, &info->height ) == 2 )
  49. break;
  50. }
  51. else
  52. sscanf( opt->resolution, "%dx%d", &info->width, &info->height );
  53. FAIL_IF_ERROR( !info->width || !info->height, "raw input requires a resolution.\n" );
  54. if( opt->colorspace )
  55. {
  56. for( info->csp = X264_CSP_CLI_MAX-1; info->csp > X264_CSP_NONE; info->csp-- )
  57. {
  58. if( x264_cli_csps[info->csp].name && !strcasecmp( x264_cli_csps[info->csp].name, opt->colorspace ) )
  59. break;
  60. }
  61. FAIL_IF_ERROR( info->csp == X264_CSP_NONE, "unsupported colorspace `%s'\n", opt->colorspace );
  62. }
  63. else /* default */
  64. info->csp = X264_CSP_I420;
  65. h->bit_depth = opt->bit_depth;
  66. FAIL_IF_ERROR( h->bit_depth < 8 || h->bit_depth > 16, "unsupported bit depth `%d'\n", h->bit_depth );
  67. if( h->bit_depth > 8 )
  68. info->csp |= X264_CSP_HIGH_DEPTH;
  69. if( !strcmp( psz_filename, "-" ) )
  70. h->fh = stdin;
  71. else
  72. h->fh = x264_fopen( psz_filename, "rb" );
  73. if( h->fh == NULL )
  74. return -1;
  75. info->thread_safe = 1;
  76. info->num_frames = 0;
  77. info->vfr = 0;
  78. const x264_cli_csp_t *csp = x264_cli_get_csp( info->csp );
  79. for( int i = 0; i < csp->planes; i++ )
  80. {
  81. h->plane_size[i] = x264_cli_pic_plane_size( info->csp, info->width, info->height, i );
  82. h->frame_size += h->plane_size[i];
  83. /* x264_cli_pic_plane_size returns the size in bytes, we need the value in pixels from here on */
  84. h->plane_size[i] /= x264_cli_csp_depth_factor( info->csp );
  85. }
  86. if( x264_is_regular_file( h->fh ) )
  87. {
  88. fseek( h->fh, 0, SEEK_END );
  89. uint64_t size = ftell( h->fh );
  90. fseek( h->fh, 0, SEEK_SET );
  91. info->num_frames = size / h->frame_size;
  92. FAIL_IF_ERROR( !info->num_frames, "empty input file\n" );
  93. /* Attempt to use memory-mapped input frames if possible */
  94. if( !(h->bit_depth & 7) )
  95. h->use_mmap = !x264_cli_mmap_init( &h->mmap, h->fh );
  96. }
  97. *p_handle = h;
  98. return 0;
  99. }
  100. static int read_frame_internal( cli_pic_t *pic, raw_hnd_t *h, int bit_depth_uc )
  101. {
  102. int pixel_depth = x264_cli_csp_depth_factor( pic->img.csp );
  103. for( int i = 0; i < pic->img.planes; i++ )
  104. {
  105. if( h->use_mmap )
  106. {
  107. if( i )
  108. pic->img.plane[i] = pic->img.plane[i-1] + pixel_depth * h->plane_size[i-1];
  109. }
  110. else if( fread( pic->img.plane[i], pixel_depth, h->plane_size[i], h->fh ) != h->plane_size[i] )
  111. return -1;
  112. if( bit_depth_uc )
  113. {
  114. /* upconvert non 16bit high depth planes to 16bit using the same
  115. * algorithm as used in the depth filter. */
  116. uint16_t *plane = (uint16_t*)pic->img.plane[i];
  117. uint64_t pixel_count = h->plane_size[i];
  118. int lshift = 16 - h->bit_depth;
  119. for( uint64_t j = 0; j < pixel_count; j++ )
  120. plane[j] = plane[j] << lshift;
  121. }
  122. }
  123. return 0;
  124. }
  125. static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
  126. {
  127. raw_hnd_t *h = handle;
  128. if( h->use_mmap )
  129. {
  130. pic->img.plane[0] = x264_cli_mmap( &h->mmap, i_frame * h->frame_size, h->frame_size );
  131. if( !pic->img.plane[0] )
  132. return -1;
  133. }
  134. else if( i_frame > h->next_frame )
  135. {
  136. if( x264_is_regular_file( h->fh ) )
  137. fseek( h->fh, i_frame * h->frame_size, SEEK_SET );
  138. else
  139. while( i_frame > h->next_frame )
  140. {
  141. if( read_frame_internal( pic, h, 0 ) )
  142. return -1;
  143. h->next_frame++;
  144. }
  145. }
  146. if( read_frame_internal( pic, h, h->bit_depth & 7 ) )
  147. return -1;
  148. h->next_frame = i_frame+1;
  149. return 0;
  150. }
  151. static int release_frame( cli_pic_t *pic, hnd_t handle )
  152. {
  153. raw_hnd_t *h = handle;
  154. if( h->use_mmap )
  155. return x264_cli_munmap( &h->mmap, pic->img.plane[0], h->frame_size );
  156. return 0;
  157. }
  158. static int picture_alloc( cli_pic_t *pic, hnd_t handle, int csp, int width, int height )
  159. {
  160. raw_hnd_t *h = handle;
  161. return (h->use_mmap ? x264_cli_pic_init_noalloc : x264_cli_pic_alloc)( pic, csp, width, height );
  162. }
  163. static void picture_clean( cli_pic_t *pic, hnd_t handle )
  164. {
  165. raw_hnd_t *h = handle;
  166. if( h->use_mmap )
  167. memset( pic, 0, sizeof(cli_pic_t) );
  168. else
  169. x264_cli_pic_clean( pic );
  170. }
  171. static int close_file( hnd_t handle )
  172. {
  173. raw_hnd_t *h = handle;
  174. if( !h || !h->fh )
  175. return 0;
  176. if( h->use_mmap )
  177. x264_cli_mmap_close( &h->mmap );
  178. fclose( h->fh );
  179. free( h );
  180. return 0;
  181. }
  182. const cli_input_t raw_input = { open_file, picture_alloc, read_frame, release_frame, picture_clean, close_file };