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.

375 lines
9.5KB

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