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.

370 lines
9.4KB

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