You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

368 lines
9.3KB

  1. /*
  2. * PPM Video Hook
  3. * Copyright (c) 2003 Charles Yates
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #include <sys/types.h>
  25. #include <sys/wait.h>
  26. #include <ctype.h>
  27. #include "framehook.h"
  28. #include "avformat.h"
  29. #include "swscale.h"
  30. static int sws_flags = SWS_BICUBIC;
  31. /** Bi-directional pipe structure.
  32. */
  33. typedef struct rwpipe
  34. {
  35. int pid;
  36. FILE *reader;
  37. FILE *writer;
  38. }
  39. rwpipe;
  40. /** Create a bidirectional pipe for the given command.
  41. */
  42. static rwpipe *rwpipe_open( int argc, char *argv[] )
  43. {
  44. rwpipe *this = av_mallocz( sizeof( rwpipe ) );
  45. if ( this != NULL )
  46. {
  47. int input[ 2 ];
  48. int output[ 2 ];
  49. pipe( input );
  50. pipe( output );
  51. this->pid = fork();
  52. if ( this->pid == 0 )
  53. {
  54. #define COMMAND_SIZE 10240
  55. char *command = av_mallocz( COMMAND_SIZE );
  56. int i;
  57. strcpy( command, "" );
  58. for ( i = 0; i < argc; i ++ )
  59. {
  60. pstrcat( command, COMMAND_SIZE, argv[ i ] );
  61. pstrcat( command, COMMAND_SIZE, " " );
  62. }
  63. dup2( output[ 0 ], STDIN_FILENO );
  64. dup2( input[ 1 ], STDOUT_FILENO );
  65. close( input[ 0 ] );
  66. close( input[ 1 ] );
  67. close( output[ 0 ] );
  68. close( output[ 1 ] );
  69. execl("/bin/sh", "sh", "-c", command, (char*)NULL );
  70. exit( 255 );
  71. }
  72. else
  73. {
  74. close( input[ 1 ] );
  75. close( output[ 0 ] );
  76. this->reader = fdopen( input[ 0 ], "r" );
  77. this->writer = fdopen( output[ 1 ], "w" );
  78. }
  79. }
  80. return this;
  81. }
  82. /** Read data from the pipe.
  83. */
  84. static FILE *rwpipe_reader( rwpipe *this )
  85. {
  86. if ( this != NULL )
  87. return this->reader;
  88. else
  89. return NULL;
  90. }
  91. /** Write data to the pipe.
  92. */
  93. static FILE *rwpipe_writer( rwpipe *this )
  94. {
  95. if ( this != NULL )
  96. return this->writer;
  97. else
  98. return NULL;
  99. }
  100. /* Read a number from the pipe - assumes PNM style headers.
  101. */
  102. static int rwpipe_read_number( rwpipe *rw )
  103. {
  104. int value = 0;
  105. int c = 0;
  106. FILE *in = rwpipe_reader( rw );
  107. do
  108. {
  109. c = fgetc( in );
  110. while( c != EOF && !isdigit( c ) && c != '#' )
  111. c = fgetc( in );
  112. if ( c == '#' )
  113. while( c != EOF && c != '\n' )
  114. c = fgetc( in );
  115. }
  116. while ( c != EOF && !isdigit( c ) );
  117. while( c != EOF && isdigit( c ) )
  118. {
  119. value = value * 10 + ( c - '0' );
  120. c = fgetc( in );
  121. }
  122. return value;
  123. }
  124. /** Read a PPM P6 header.
  125. */
  126. static int rwpipe_read_ppm_header( rwpipe *rw, int *width, int *height )
  127. {
  128. char line[ 3 ];
  129. FILE *in = rwpipe_reader( rw );
  130. int max;
  131. fgets( line, 3, in );
  132. if ( !strncmp( line, "P6", 2 ) )
  133. {
  134. *width = rwpipe_read_number( rw );
  135. *height = rwpipe_read_number( rw );
  136. max = rwpipe_read_number( rw );
  137. return max != 255 || *width <= 0 || *height <= 0;
  138. }
  139. return 1;
  140. }
  141. /** Close the pipe and process.
  142. */
  143. static void rwpipe_close( rwpipe *this )
  144. {
  145. if ( this != NULL )
  146. {
  147. fclose( this->reader );
  148. fclose( this->writer );
  149. waitpid( this->pid, NULL, 0 );
  150. av_free( this );
  151. }
  152. }
  153. /** Context info for this vhook - stores the pipe and image buffers.
  154. */
  155. typedef struct
  156. {
  157. rwpipe *rw;
  158. int size1;
  159. char *buf1;
  160. int size2;
  161. char *buf2;
  162. // This vhook first converts frame to RGB ...
  163. struct SwsContext *toRGB_convert_ctx;
  164. // ... then processes it via a PPM command pipe ...
  165. // ... and finally converts back frame from RGB to initial format
  166. struct SwsContext *fromRGB_convert_ctx;
  167. }
  168. ContextInfo;
  169. /** Initialise the context info for this vhook.
  170. */
  171. int Configure(void **ctxp, int argc, char *argv[])
  172. {
  173. if ( argc > 1 )
  174. {
  175. *ctxp = av_mallocz(sizeof(ContextInfo));
  176. if ( ctxp != NULL && argc > 1 )
  177. {
  178. ContextInfo *info = (ContextInfo *)*ctxp;
  179. info->rw = rwpipe_open( argc - 1, &argv[ 1 ] );
  180. return 0;
  181. }
  182. }
  183. return 1;
  184. }
  185. /** Process a frame.
  186. */
  187. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
  188. {
  189. int err = 0;
  190. ContextInfo *ci = (ContextInfo *) ctx;
  191. AVPicture picture1;
  192. AVPicture picture2;
  193. AVPicture *pict = picture;
  194. int out_width;
  195. int out_height;
  196. int i;
  197. uint8_t *ptr = NULL;
  198. FILE *in = rwpipe_reader( ci->rw );
  199. FILE *out = rwpipe_writer( ci->rw );
  200. /* Check that we have a pipe to talk to. */
  201. if ( in == NULL || out == NULL )
  202. err = 1;
  203. /* Convert to RGB24 if necessary */
  204. if ( !err && pix_fmt != PIX_FMT_RGB24 )
  205. {
  206. int size = avpicture_get_size(PIX_FMT_RGB24, width, height);
  207. if ( size != ci->size1 )
  208. {
  209. av_free( ci->buf1 );
  210. ci->buf1 = av_malloc(size);
  211. ci->size1 = size;
  212. err = ci->buf1 == NULL;
  213. }
  214. if ( !err )
  215. {
  216. avpicture_fill(&picture1, ci->buf1, PIX_FMT_RGB24, width, height);
  217. // if we already got a SWS context, let's realloc if is not re-useable
  218. ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
  219. width, height, pix_fmt,
  220. width, height, PIX_FMT_RGB24,
  221. sws_flags, NULL, NULL, NULL);
  222. if (ci->toRGB_convert_ctx == NULL) {
  223. av_log(NULL, AV_LOG_ERROR,
  224. "Cannot initialize the toRGB conversion context\n");
  225. exit(1);
  226. }
  227. // img_convert parameters are 2 first destination, then 4 source
  228. // sws_scale parameters are context, 4 first source, then 2 destination
  229. sws_scale(ci->toRGB_convert_ctx,
  230. picture->data, picture->linesize, 0, height,
  231. picture1.data, picture1.linesize);
  232. pict = &picture1;
  233. }
  234. }
  235. /* Write out the PPM */
  236. if ( !err )
  237. {
  238. ptr = pict->data[ 0 ];
  239. fprintf( out, "P6\n%d %d\n255\n", width, height );
  240. for ( i = 0; !err && i < height; i ++ )
  241. {
  242. err = !fwrite( ptr, width * 3, 1, out );
  243. ptr += pict->linesize[ 0 ];
  244. }
  245. if ( !err )
  246. err = fflush( out );
  247. }
  248. /* Read the PPM returned. */
  249. if ( !err && !rwpipe_read_ppm_header( ci->rw, &out_width, &out_height ) )
  250. {
  251. int size = avpicture_get_size(PIX_FMT_RGB24, out_width, out_height);
  252. if ( size != ci->size2 )
  253. {
  254. av_free( ci->buf2 );
  255. ci->buf2 = av_malloc(size);
  256. ci->size2 = size;
  257. err = ci->buf2 == NULL;
  258. }
  259. if ( !err )
  260. {
  261. avpicture_fill(&picture2, ci->buf2, PIX_FMT_RGB24, out_width, out_height);
  262. ptr = picture2.data[ 0 ];
  263. for ( i = 0; !err && i < out_height; i ++ )
  264. {
  265. err = !fread( ptr, out_width * 3, 1, in );
  266. ptr += picture2.linesize[ 0 ];
  267. }
  268. }
  269. }
  270. /* Convert the returned PPM back to the input format */
  271. if ( !err )
  272. {
  273. /* The out_width/out_height returned from the PPM
  274. * filter won't necessarily be the same as width and height
  275. * but it will be scaled anyway to width/height.
  276. */
  277. av_log(NULL, AV_LOG_DEBUG,
  278. "PPM vhook: Input dimensions: %d x %d Output dimensions: %d x %d\n",
  279. width, height, out_width, out_height);
  280. ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
  281. out_width, out_height, PIX_FMT_RGB24,
  282. width, height, pix_fmt,
  283. sws_flags, NULL, NULL, NULL);
  284. if (ci->fromRGB_convert_ctx == NULL) {
  285. av_log(NULL, AV_LOG_ERROR,
  286. "Cannot initialize the fromRGB conversion context\n");
  287. exit(1);
  288. }
  289. // img_convert parameters are 2 first destination, then 4 source
  290. // sws_scale parameters are context, 4 first source, then 2 destination
  291. sws_scale(ci->fromRGB_convert_ctx,
  292. picture2.data, picture2.linesize, 0, out_height,
  293. picture->data, picture->linesize);
  294. }
  295. }
  296. /** Clean up the effect.
  297. */
  298. void Release(void *ctx)
  299. {
  300. ContextInfo *ci;
  301. ci = (ContextInfo *) ctx;
  302. if (ctx)
  303. {
  304. rwpipe_close( ci->rw );
  305. av_free( ci->buf1 );
  306. av_free( ci->buf2 );
  307. sws_freeContext(ci->toRGB_convert_ctx);
  308. sws_freeContext(ci->fromRGB_convert_ctx);
  309. av_free(ctx);
  310. }
  311. }