null.c 3.69 KB
Newer Older
1
/*
2
 * Null Video Hook
3 4
 * Copyright (c) 2002 Philip Gladstone
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * 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 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 22 23
 */
#include <stdio.h>

#include "framehook.h"
24 25 26
#include "swscale.h"

static int sws_flags = SWS_BICUBIC;
27 28 29

typedef struct {
    int dummy;
30 31 32 33 34 35 36

    // This vhook first converts frame to RGB ...
    struct SwsContext *toRGB_convert_ctx;

    // ... and later converts back frame from RGB to initial format
    struct SwsContext *fromRGB_convert_ctx;

37 38
} ContextInfo;

39 40 41 42 43
void Release(void *ctx)
{
    ContextInfo *ci;
    ci = (ContextInfo *) ctx;

44 45 46
    if (ctx) {
        sws_freeContext(ci->toRGB_convert_ctx);
        sws_freeContext(ci->fromRGB_convert_ctx);
47
        av_free(ctx);
48
    }
49
}
50 51 52 53 54 55 56 57 58

int Configure(void **ctxp, int argc, char *argv[])
{
    fprintf(stderr, "Called with argc=%d\n", argc);

    *ctxp = av_mallocz(sizeof(ContextInfo));
    return 0;
}

59
void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
{
    ContextInfo *ci = (ContextInfo *) ctx;
    char *buf = 0;
    AVPicture picture1;
    AVPicture *pict = picture;

    (void) ci;

    if (pix_fmt != PIX_FMT_RGB24) {
        int size;

        size = avpicture_get_size(PIX_FMT_RGB24, width, height);
        buf = av_malloc(size);

        avpicture_fill(&picture1, buf, PIX_FMT_RGB24, width, height);
75 76

        // if we already got a SWS context, let's realloc if is not re-useable
77 78 79 80
        ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
                                    width, height, pix_fmt,
                                    width, height, PIX_FMT_RGB24,
                                    sws_flags, NULL, NULL, NULL);
81
        if (ci->toRGB_convert_ctx == NULL) {
82 83 84
            av_log(NULL, AV_LOG_ERROR,
                   "Cannot initialize the toRGB conversion context\n");
            exit(1);
85
        }
86 87 88
// img_convert parameters are          2 first destination, then 4 source
// sws_scale   parameters are context, 4 first source,      then 2 destination
        sws_scale(ci->toRGB_convert_ctx,
89
            picture->data, picture->linesize, 0, height,
90 91
            picture1.data, picture1.linesize);

92 93 94 95 96 97
        pict = &picture1;
    }

    /* Insert filter code here */

    if (pix_fmt != PIX_FMT_RGB24) {
98 99 100 101 102 103 104 105 106 107 108
        ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
                                        width, height, PIX_FMT_RGB24,
                                        width, height, pix_fmt,
                                        sws_flags, NULL, NULL, NULL);
        if (ci->fromRGB_convert_ctx == NULL) {
            av_log(NULL, AV_LOG_ERROR,
                   "Cannot initialize the fromRGB conversion context\n");
            exit(1);
        }
// img_convert parameters are          2 first destination, then 4 source
// sws_scale   parameters are context, 4 first source,      then 2 destination
109
        sws_scale(ci->fromRGB_convert_ctx,
110
            picture1.data, picture1.linesize, 0, height,
111
            picture->data, picture->linesize);
112 113 114 115 116
    }

    av_free(buf);
}