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.

272 lines
5.5KB

  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. /** Close the pipe and process.
  95. */
  96. void rwpipe_close( rwpipe *this )
  97. {
  98. if ( this != NULL )
  99. {
  100. fclose( this->reader );
  101. fclose( this->writer );
  102. waitpid( this->pid, NULL, 0 );
  103. av_free( this );
  104. }
  105. }
  106. typedef struct
  107. {
  108. rwpipe *rw;
  109. }
  110. ContextInfo;
  111. int Configure(void **ctxp, int argc, char *argv[])
  112. {
  113. *ctxp = av_mallocz(sizeof(ContextInfo));
  114. if ( ctxp != NULL && argc > 1 )
  115. {
  116. ContextInfo *info = (ContextInfo *)*ctxp;
  117. info->rw = rwpipe_open( argc - 1, &argv[ 1 ] );
  118. }
  119. return 0;
  120. }
  121. int rwpipe_read_number( rwpipe *rw )
  122. {
  123. int value = 0;
  124. int c = 0;
  125. FILE *in = rwpipe_reader( rw );
  126. do
  127. {
  128. c = fgetc( in );
  129. while( c != EOF && !isdigit( c ) && c != '#' )
  130. c = fgetc( in );
  131. if ( c == '#' )
  132. while( c != EOF && c != '\n' )
  133. c = fgetc( in );
  134. }
  135. while ( c != EOF && !isdigit( c ) );
  136. while( c != EOF && isdigit( c ) )
  137. {
  138. value = value * 10 + ( c - '0' );
  139. c = fgetc( in );
  140. }
  141. return value;
  142. }
  143. int rwpipe_read_ppm_header( rwpipe *rw, int *width, int *height )
  144. {
  145. char line[ 3 ];
  146. FILE *in = rwpipe_reader( rw );
  147. int max;
  148. fgets( line, 3, in );
  149. if ( !strncmp( line, "P6", 2 ) )
  150. {
  151. *width = rwpipe_read_number( rw );
  152. *height = rwpipe_read_number( rw );
  153. max = rwpipe_read_number( rw );
  154. return max != 255 || *width <= 0 || *height <= 0;
  155. }
  156. return 1;
  157. }
  158. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
  159. {
  160. int err = 0;
  161. ContextInfo *ci = (ContextInfo *) ctx;
  162. char *buf1 = 0;
  163. char *buf2 = 0;
  164. AVPicture picture1;
  165. AVPicture picture2;
  166. AVPicture *pict = picture;
  167. int out_width;
  168. int out_height;
  169. int i;
  170. uint8_t *ptr = NULL;
  171. FILE *in = rwpipe_reader( ci->rw );
  172. FILE *out = rwpipe_writer( ci->rw );
  173. /* Convert to RGB24 if necessary */
  174. if (pix_fmt != PIX_FMT_RGB24) {
  175. int size;
  176. size = avpicture_get_size(PIX_FMT_RGB24, width, height);
  177. buf1 = av_malloc(size);
  178. avpicture_fill(&picture1, buf1, PIX_FMT_RGB24, width, height);
  179. if (img_convert(&picture1, PIX_FMT_RGB24,
  180. picture, pix_fmt, width, height) < 0) {
  181. err = 1;
  182. }
  183. pict = &picture1;
  184. }
  185. /* Write out the PPM */
  186. if ( !err )
  187. {
  188. ptr = pict->data[ 0 ];
  189. fprintf( out, "P6\n%d %d\n255\n", width, height );
  190. for ( i = 0; !err && i < height; i ++ )
  191. {
  192. err = !fwrite( ptr, width * 3, 1, out );
  193. ptr += pict->linesize[ 0 ];
  194. }
  195. if ( !err )
  196. err = fflush( out );
  197. }
  198. /* Read the PPM returned. */
  199. if ( !err && !rwpipe_read_ppm_header( ci->rw, &out_width, &out_height ) )
  200. {
  201. int size = avpicture_get_size(PIX_FMT_RGB24, out_width, out_height);
  202. buf2 = av_malloc(size);
  203. avpicture_fill(&picture2, buf2, PIX_FMT_RGB24, out_width, out_height);
  204. ptr = picture2.data[ 0 ];
  205. for ( i = 0; !err && i < out_height; i ++ )
  206. {
  207. err = !fread( ptr, out_width * 3, 1, in );
  208. ptr += picture2.linesize[ 0 ];
  209. }
  210. }
  211. /* Convert the returned PPM back to the input format */
  212. if ( !err )
  213. {
  214. if (img_convert(picture, pix_fmt, &picture2, PIX_FMT_RGB24, width, height) < 0) {
  215. }
  216. }
  217. av_free(buf1);
  218. av_free(buf2);
  219. }
  220. void Release(void *ctx)
  221. {
  222. ContextInfo *ci;
  223. ci = (ContextInfo *) ctx;
  224. if (ctx)
  225. {
  226. rwpipe_close( ci->rw );
  227. av_free(ctx);
  228. }
  229. }