Revision a4dd5fb7

View differences:

vhook/ppm.c
25 25
#include <ctype.h>
26 26
#include "framehook.h"
27 27
#include "avformat.h"
28
#include "swscale.h"
29

  
30
static int sws_flags = SWS_BICUBIC;
28 31

  
29 32
/** Bi-directional pipe structure.
30 33
*/
......
188 191
    char *buf1;
189 192
    int size2;
190 193
    char *buf2;
194

  
195
    // This vhook first converts frame to RGB ...
196
    struct SwsContext *toRGB_convert_ctx;
197
    // ... then processes it via a PPM command pipe ...
198
    // ... and finally converts back frame from RGB to initial format
199
    struct SwsContext *fromRGB_convert_ctx;
191 200
}
192 201
ContextInfo;
193 202

  
......
246 255
        if ( !err )
247 256
        {
248 257
            avpicture_fill(&picture1, ci->buf1, PIX_FMT_RGB24, width, height);
249
            if (img_convert(&picture1, PIX_FMT_RGB24, picture, pix_fmt, width, height) < 0)
250
                err = 1;
258

  
259
            // if we already got a SWS context, let's realloc if is not re-useable
260
            ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
261
                                        width, height, pix_fmt,
262
                                        width, height, PIX_FMT_RGB24,
263
                                        sws_flags, NULL, NULL, NULL);
264
            if (ci->toRGB_convert_ctx == NULL) {
265
                av_log(NULL, AV_LOG_ERROR,
266
                       "Cannot initialize the toRGB conversion context\n");
267
                exit(1);
268
            }
269

  
270
// img_convert parameters are          2 first destination, then 4 source
271
// sws_scale   parameters are context, 4 first source,      then 2 destination
272
            sws_scale(ci->toRGB_convert_ctx,
273
                     picture->data, picture->linesize, 0, height,
274
                     picture1.data, picture1.linesize);
275

  
251 276
            pict = &picture1;
252 277
        }
253 278
    }
......
294 319
    /* Convert the returned PPM back to the input format */
295 320
    if ( !err )
296 321
    {
297
        /* Actually, this is wrong, since the out_width/out_height returned from the
298
         * filter won't necessarily be the same as width and height - img_resample
299
         * won't scale rgb24, so the only way out of this is to convert to something
300
         * that img_resample does like [which may or may not be pix_fmt], rescale
301
         * and finally convert to pix_fmt... slow, but would provide the most flexibility.
302
         *
303
         * Currently, we take the upper left width/height pixels from the filtered image,
304
         * smaller images are going to be corrupted or cause a crash.
305
         *
306
         * Finally, what should we do in case of this call failing? Up to now, failures
307
         * are gracefully ignored and the original image is returned - in this case, a
308
         * failure may corrupt the input.
322
        /* The out_width/out_height returned from the PPM
323
         * filter won't necessarily be the same as width and height
324
         * but it will be scaled anyway to width/height.
309 325
         */
310
        if (img_convert(picture, pix_fmt, &picture2, PIX_FMT_RGB24, width, height) < 0)
311
        {
326
        av_log(NULL, AV_LOG_DEBUG,
327
                  "PPM vhook: Input dimensions: %d x %d Output dimensions: %d x %d\n",
328
                  width, height, out_width, out_height);
329
        ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
330
                                        out_width, out_height, PIX_FMT_RGB24,
331
                                        width,     height,     pix_fmt,
332
                                        sws_flags, NULL, NULL, NULL);
333
        if (ci->fromRGB_convert_ctx == NULL) {
334
            av_log(NULL, AV_LOG_ERROR,
335
                   "Cannot initialize the fromRGB conversion context\n");
336
            exit(1);
312 337
        }
338

  
339
// img_convert parameters are          2 first destination, then 4 source
340
// sws_scale   parameters are context, 4 first source,      then 2 destination
341
        sws_scale(ci->fromRGB_convert_ctx,
342
                 picture2.data, picture2.linesize, 0, out_height,
343
                 picture->data, picture->linesize);
313 344
    }
314 345
}
315 346

  
......
326 357
        rwpipe_close( ci->rw );
327 358
        av_free( ci->buf1 );
328 359
        av_free( ci->buf2 );
360
        sws_freeContext(ci->toRGB_convert_ctx);
361
        sws_freeContext(ci->fromRGB_convert_ctx);
329 362
        av_free(ctx);
330 363
    }
331 364
}

Also available in: Unified diff