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.

366 lines
9.3KB

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