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.

333 lines
7.9KB

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