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.

331 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. char *command = av_mallocz( 10240 );
  50. int i;
  51. strcpy( command, "" );
  52. for ( i = 0; i < argc; i ++ )
  53. {
  54. strcat( command, argv[ i ] );
  55. strcat( command, " " );
  56. }
  57. dup2( output[ 0 ], STDIN_FILENO );
  58. dup2( input[ 1 ], STDOUT_FILENO );
  59. close( input[ 0 ] );
  60. close( input[ 1 ] );
  61. close( output[ 0 ] );
  62. close( output[ 1 ] );
  63. execl("/bin/sh", "sh", "-c", command, NULL );
  64. exit( 255 );
  65. }
  66. else
  67. {
  68. close( input[ 1 ] );
  69. close( output[ 0 ] );
  70. this->reader = fdopen( input[ 0 ], "r" );
  71. this->writer = fdopen( output[ 1 ], "w" );
  72. }
  73. }
  74. return this;
  75. }
  76. /** Read data from the pipe.
  77. */
  78. FILE *rwpipe_reader( rwpipe *this )
  79. {
  80. if ( this != NULL )
  81. return this->reader;
  82. else
  83. return NULL;
  84. }
  85. /** Write data to the pipe.
  86. */
  87. FILE *rwpipe_writer( rwpipe *this )
  88. {
  89. if ( this != NULL )
  90. return this->writer;
  91. else
  92. return NULL;
  93. }
  94. /* Read a number from the pipe - assumes PNM style headers.
  95. */
  96. int rwpipe_read_number( rwpipe *rw )
  97. {
  98. int value = 0;
  99. int c = 0;
  100. FILE *in = rwpipe_reader( rw );
  101. do
  102. {
  103. c = fgetc( in );
  104. while( c != EOF && !isdigit( c ) && c != '#' )
  105. c = fgetc( in );
  106. if ( c == '#' )
  107. while( c != EOF && c != '\n' )
  108. c = fgetc( in );
  109. }
  110. while ( c != EOF && !isdigit( c ) );
  111. while( c != EOF && isdigit( c ) )
  112. {
  113. value = value * 10 + ( c - '0' );
  114. c = fgetc( in );
  115. }
  116. return value;
  117. }
  118. /** Read a PPM P6 header.
  119. */
  120. int rwpipe_read_ppm_header( rwpipe *rw, int *width, int *height )
  121. {
  122. char line[ 3 ];
  123. FILE *in = rwpipe_reader( rw );
  124. int max;
  125. fgets( line, 3, in );
  126. if ( !strncmp( line, "P6", 2 ) )
  127. {
  128. *width = rwpipe_read_number( rw );
  129. *height = rwpipe_read_number( rw );
  130. max = rwpipe_read_number( rw );
  131. return max != 255 || *width <= 0 || *height <= 0;
  132. }
  133. return 1;
  134. }
  135. /** Close the pipe and process.
  136. */
  137. void rwpipe_close( rwpipe *this )
  138. {
  139. if ( this != NULL )
  140. {
  141. fclose( this->reader );
  142. fclose( this->writer );
  143. waitpid( this->pid, NULL, 0 );
  144. av_free( this );
  145. }
  146. }
  147. /** Context info for this vhook - stores the pipe and image buffers.
  148. */
  149. typedef struct
  150. {
  151. rwpipe *rw;
  152. int size1;
  153. char *buf1;
  154. int size2;
  155. char *buf2;
  156. }
  157. ContextInfo;
  158. /** Initialise the context info for this vhook.
  159. */
  160. int Configure(void **ctxp, int argc, char *argv[])
  161. {
  162. if ( argc > 1 )
  163. {
  164. *ctxp = av_mallocz(sizeof(ContextInfo));
  165. if ( ctxp != NULL && argc > 1 )
  166. {
  167. ContextInfo *info = (ContextInfo *)*ctxp;
  168. info->rw = rwpipe_open( argc - 1, &argv[ 1 ] );
  169. return 0;
  170. }
  171. }
  172. return 1;
  173. }
  174. /** Process a frame.
  175. */
  176. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
  177. {
  178. int err = 0;
  179. ContextInfo *ci = (ContextInfo *) ctx;
  180. AVPicture picture1;
  181. AVPicture picture2;
  182. AVPicture *pict = picture;
  183. int out_width;
  184. int out_height;
  185. int i;
  186. uint8_t *ptr = NULL;
  187. FILE *in = rwpipe_reader( ci->rw );
  188. FILE *out = rwpipe_writer( ci->rw );
  189. /* Check that we have a pipe to talk to. */
  190. if ( in == NULL || out == NULL )
  191. err = 1;
  192. /* Convert to RGB24 if necessary */
  193. if ( !err && pix_fmt != PIX_FMT_RGB24 )
  194. {
  195. int size = avpicture_get_size(PIX_FMT_RGB24, width, height);
  196. if ( size != ci->size1 )
  197. {
  198. av_free( ci->buf1 );
  199. ci->buf1 = av_malloc(size);
  200. ci->size1 = size;
  201. err = ci->buf1 == NULL;
  202. }
  203. if ( !err )
  204. {
  205. avpicture_fill(&picture1, ci->buf1, PIX_FMT_RGB24, width, height);
  206. if (img_convert(&picture1, PIX_FMT_RGB24, picture, pix_fmt, width, height) < 0)
  207. err = 1;
  208. pict = &picture1;
  209. }
  210. }
  211. /* Write out the PPM */
  212. if ( !err )
  213. {
  214. ptr = pict->data[ 0 ];
  215. fprintf( out, "P6\n%d %d\n255\n", width, height );
  216. for ( i = 0; !err && i < height; i ++ )
  217. {
  218. err = !fwrite( ptr, width * 3, 1, out );
  219. ptr += pict->linesize[ 0 ];
  220. }
  221. if ( !err )
  222. err = fflush( out );
  223. }
  224. /* Read the PPM returned. */
  225. if ( !err && !rwpipe_read_ppm_header( ci->rw, &out_width, &out_height ) )
  226. {
  227. int size = avpicture_get_size(PIX_FMT_RGB24, out_width, out_height);
  228. if ( size != ci->size2 )
  229. {
  230. av_free( ci->buf2 );
  231. ci->buf2 = av_malloc(size);
  232. ci->size2 = size;
  233. err = ci->buf2 == NULL;
  234. }
  235. if ( !err )
  236. {
  237. avpicture_fill(&picture2, ci->buf2, PIX_FMT_RGB24, out_width, out_height);
  238. ptr = picture2.data[ 0 ];
  239. for ( i = 0; !err && i < out_height; i ++ )
  240. {
  241. err = !fread( ptr, out_width * 3, 1, in );
  242. ptr += picture2.linesize[ 0 ];
  243. }
  244. }
  245. }
  246. /* Convert the returned PPM back to the input format */
  247. if ( !err )
  248. {
  249. /* Actually, this is wrong, since the out_width/out_height returned from the
  250. * filter won't necessarily be the same as width and height - img_resample
  251. * won't scale rgb24, so the only way out of this is to convert to something
  252. * that img_resample does like [which may or may not be pix_fmt], rescale
  253. * and finally convert to pix_fmt... slow, but would provide the most flexibility.
  254. *
  255. * Currently, we take the upper left width/height pixels from the filtered image,
  256. * smaller images are going to be corrupted or cause a crash.
  257. *
  258. * Finally, what should we do in case of this call failing? Up to now, failures
  259. * are gracefully ignored and the original image is returned - in this case, a
  260. * failure may corrupt the input.
  261. */
  262. if (img_convert(picture, pix_fmt, &picture2, PIX_FMT_RGB24, width, height) < 0)
  263. {
  264. }
  265. }
  266. }
  267. /** Clean up the effect.
  268. */
  269. void Release(void *ctx)
  270. {
  271. ContextInfo *ci;
  272. ci = (ContextInfo *) ctx;
  273. if (ctx)
  274. {
  275. rwpipe_close( ci->rw );
  276. av_free( ci->buf1 );
  277. av_free( ci->buf2 );
  278. av_free(ctx);
  279. }
  280. }