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.

332 lines
7.8KB

  1. /*
  2. * PPM Video Hook
  3. * Copyright (c) 2003 Charles Yates
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <sys/types.h>
  23. #include <sys/wait.h>
  24. #include <ctype.h>
  25. #include "framehook.h"
  26. /** Bi-directional pipe structure.
  27. */
  28. typedef struct rwpipe
  29. {
  30. int pid;
  31. FILE *reader;
  32. FILE *writer;
  33. }
  34. rwpipe;
  35. /** Create a bidirectional pipe for the given command.
  36. */
  37. rwpipe *rwpipe_open( int argc, char *argv[] )
  38. {
  39. rwpipe *this = av_mallocz( sizeof( rwpipe ) );
  40. if ( this != NULL )
  41. {
  42. int input[ 2 ];
  43. int output[ 2 ];
  44. pipe( input );
  45. pipe( output );
  46. this->pid = fork();
  47. if ( this->pid == 0 )
  48. {
  49. #define COMMAND_SIZE 10240
  50. char *command = av_mallocz( COMMAND_SIZE );
  51. int i;
  52. strcpy( command, "" );
  53. for ( i = 0; i < argc; i ++ )
  54. {
  55. pstrcat( command, COMMAND_SIZE, argv[ i ] );
  56. pstrcat( command, COMMAND_SIZE, " " );
  57. }
  58. dup2( output[ 0 ], STDIN_FILENO );
  59. dup2( input[ 1 ], STDOUT_FILENO );
  60. close( input[ 0 ] );
  61. close( input[ 1 ] );
  62. close( output[ 0 ] );
  63. close( output[ 1 ] );
  64. execl("/bin/sh", "sh", "-c", command, NULL );
  65. exit( 255 );
  66. }
  67. else
  68. {
  69. close( input[ 1 ] );
  70. close( output[ 0 ] );
  71. this->reader = fdopen( input[ 0 ], "r" );
  72. this->writer = fdopen( output[ 1 ], "w" );
  73. }
  74. }
  75. return this;
  76. }
  77. /** Read data from the pipe.
  78. */
  79. FILE *rwpipe_reader( rwpipe *this )
  80. {
  81. if ( this != NULL )
  82. return this->reader;
  83. else
  84. return NULL;
  85. }
  86. /** Write data to the pipe.
  87. */
  88. FILE *rwpipe_writer( rwpipe *this )
  89. {
  90. if ( this != NULL )
  91. return this->writer;
  92. else
  93. return NULL;
  94. }
  95. /* Read a number from the pipe - assumes PNM style headers.
  96. */
  97. int rwpipe_read_number( rwpipe *rw )
  98. {
  99. int value = 0;
  100. int c = 0;
  101. FILE *in = rwpipe_reader( rw );
  102. do
  103. {
  104. c = fgetc( in );
  105. while( c != EOF && !isdigit( c ) && c != '#' )
  106. c = fgetc( in );
  107. if ( c == '#' )
  108. while( c != EOF && c != '\n' )
  109. c = fgetc( in );
  110. }
  111. while ( c != EOF && !isdigit( c ) );
  112. while( c != EOF && isdigit( c ) )
  113. {
  114. value = value * 10 + ( c - '0' );
  115. c = fgetc( in );
  116. }
  117. return value;
  118. }
  119. /** Read a PPM P6 header.
  120. */
  121. int rwpipe_read_ppm_header( rwpipe *rw, int *width, int *height )
  122. {
  123. char line[ 3 ];
  124. FILE *in = rwpipe_reader( rw );
  125. int max;
  126. fgets( line, 3, in );
  127. if ( !strncmp( line, "P6", 2 ) )
  128. {
  129. *width = rwpipe_read_number( rw );
  130. *height = rwpipe_read_number( rw );
  131. max = rwpipe_read_number( rw );
  132. return max != 255 || *width <= 0 || *height <= 0;
  133. }
  134. return 1;
  135. }
  136. /** Close the pipe and process.
  137. */
  138. void rwpipe_close( rwpipe *this )
  139. {
  140. if ( this != NULL )
  141. {
  142. fclose( this->reader );
  143. fclose( this->writer );
  144. waitpid( this->pid, NULL, 0 );
  145. av_free( this );
  146. }
  147. }
  148. /** Context info for this vhook - stores the pipe and image buffers.
  149. */
  150. typedef struct
  151. {
  152. rwpipe *rw;
  153. int size1;
  154. char *buf1;
  155. int size2;
  156. char *buf2;
  157. }
  158. ContextInfo;
  159. /** Initialise the context info for this vhook.
  160. */
  161. int Configure(void **ctxp, int argc, char *argv[])
  162. {
  163. if ( argc > 1 )
  164. {
  165. *ctxp = av_mallocz(sizeof(ContextInfo));
  166. if ( ctxp != NULL && argc > 1 )
  167. {
  168. ContextInfo *info = (ContextInfo *)*ctxp;
  169. info->rw = rwpipe_open( argc - 1, &argv[ 1 ] );
  170. return 0;
  171. }
  172. }
  173. return 1;
  174. }
  175. /** Process a frame.
  176. */
  177. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
  178. {
  179. int err = 0;
  180. ContextInfo *ci = (ContextInfo *) ctx;
  181. AVPicture picture1;
  182. AVPicture picture2;
  183. AVPicture *pict = picture;
  184. int out_width;
  185. int out_height;
  186. int i;
  187. uint8_t *ptr = NULL;
  188. FILE *in = rwpipe_reader( ci->rw );
  189. FILE *out = rwpipe_writer( ci->rw );
  190. /* Check that we have a pipe to talk to. */
  191. if ( in == NULL || out == NULL )
  192. err = 1;
  193. /* Convert to RGB24 if necessary */
  194. if ( !err && pix_fmt != PIX_FMT_RGB24 )
  195. {
  196. int size = avpicture_get_size(PIX_FMT_RGB24, width, height);
  197. if ( size != ci->size1 )
  198. {
  199. av_free( ci->buf1 );
  200. ci->buf1 = av_malloc(size);
  201. ci->size1 = size;
  202. err = ci->buf1 == NULL;
  203. }
  204. if ( !err )
  205. {
  206. avpicture_fill(&picture1, ci->buf1, PIX_FMT_RGB24, width, height);
  207. if (img_convert(&picture1, PIX_FMT_RGB24, picture, pix_fmt, width, height) < 0)
  208. err = 1;
  209. pict = &picture1;
  210. }
  211. }
  212. /* Write out the PPM */
  213. if ( !err )
  214. {
  215. ptr = pict->data[ 0 ];
  216. fprintf( out, "P6\n%d %d\n255\n", width, height );
  217. for ( i = 0; !err && i < height; i ++ )
  218. {
  219. err = !fwrite( ptr, width * 3, 1, out );
  220. ptr += pict->linesize[ 0 ];
  221. }
  222. if ( !err )
  223. err = fflush( out );
  224. }
  225. /* Read the PPM returned. */
  226. if ( !err && !rwpipe_read_ppm_header( ci->rw, &out_width, &out_height ) )
  227. {
  228. int size = avpicture_get_size(PIX_FMT_RGB24, out_width, out_height);
  229. if ( size != ci->size2 )
  230. {
  231. av_free( ci->buf2 );
  232. ci->buf2 = av_malloc(size);
  233. ci->size2 = size;
  234. err = ci->buf2 == NULL;
  235. }
  236. if ( !err )
  237. {
  238. avpicture_fill(&picture2, ci->buf2, PIX_FMT_RGB24, out_width, out_height);
  239. ptr = picture2.data[ 0 ];
  240. for ( i = 0; !err && i < out_height; i ++ )
  241. {
  242. err = !fread( ptr, out_width * 3, 1, in );
  243. ptr += picture2.linesize[ 0 ];
  244. }
  245. }
  246. }
  247. /* Convert the returned PPM back to the input format */
  248. if ( !err )
  249. {
  250. /* Actually, this is wrong, since the out_width/out_height returned from the
  251. * filter won't necessarily be the same as width and height - img_resample
  252. * won't scale rgb24, so the only way out of this is to convert to something
  253. * that img_resample does like [which may or may not be pix_fmt], rescale
  254. * and finally convert to pix_fmt... slow, but would provide the most flexibility.
  255. *
  256. * Currently, we take the upper left width/height pixels from the filtered image,
  257. * smaller images are going to be corrupted or cause a crash.
  258. *
  259. * Finally, what should we do in case of this call failing? Up to now, failures
  260. * are gracefully ignored and the original image is returned - in this case, a
  261. * failure may corrupt the input.
  262. */
  263. if (img_convert(picture, pix_fmt, &picture2, PIX_FMT_RGB24, width, height) < 0)
  264. {
  265. }
  266. }
  267. }
  268. /** Clean up the effect.
  269. */
  270. void Release(void *ctx)
  271. {
  272. ContextInfo *ci;
  273. ci = (ContextInfo *) ctx;
  274. if (ctx)
  275. {
  276. rwpipe_close( ci->rw );
  277. av_free( ci->buf1 );
  278. av_free( ci->buf2 );
  279. av_free(ctx);
  280. }
  281. }