1/*
2 * Null Video Hook
3 * Copyright (c) 2002 Philip Gladstone
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
23#include "libavformat/framehook.h"
24#include "libswscale/swscale.h"
25
26static int sws_flags = SWS_BICUBIC;
27
28typedef struct {
29    int dummy;
30
31    // This vhook first converts frame to RGB ...
32    struct SwsContext *toRGB_convert_ctx;
33
34    // ... and later converts back frame from RGB to initial format
35    struct SwsContext *fromRGB_convert_ctx;
36
37} ContextInfo;
38
39void Release(void *ctx)
40{
41    ContextInfo *ci;
42    ci = (ContextInfo *) ctx;
43
44    if (ctx) {
45        sws_freeContext(ci->toRGB_convert_ctx);
46        sws_freeContext(ci->fromRGB_convert_ctx);
47        av_free(ctx);
48    }
49}
50
51int Configure(void **ctxp, int argc, char *argv[])
52{
53    av_log(NULL, AV_LOG_DEBUG, "Called with argc=%d\n", argc);
54
55    *ctxp = av_mallocz(sizeof(ContextInfo));
56    return 0;
57}
58
59void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
60{
61    ContextInfo *ci = (ContextInfo *) ctx;
62    char *buf = 0;
63    AVPicture picture1;
64    AVPicture *pict = picture;
65
66    (void) ci;
67
68    if (pix_fmt != PIX_FMT_RGB24) {
69        int size;
70
71        size = avpicture_get_size(PIX_FMT_RGB24, width, height);
72        buf = av_malloc(size);
73
74        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        ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
78                                    width, height, pix_fmt,
79                                    width, height, PIX_FMT_RGB24,
80                                    sws_flags, NULL, NULL, NULL);
81        if (ci->toRGB_convert_ctx == NULL) {
82            av_log(NULL, AV_LOG_ERROR,
83                   "Cannot initialize the toRGB conversion context\n");
84            return;
85        }
86// img_convert parameters are          2 first destination, then 4 source
87// sws_scale   parameters are context, 4 first source,      then 2 destination
88        sws_scale(ci->toRGB_convert_ctx,
89            picture->data, picture->linesize, 0, height,
90            picture1.data, picture1.linesize);
91
92        pict = &picture1;
93    }
94
95    /* Insert filter code here */
96
97    if (pix_fmt != PIX_FMT_RGB24) {
98        ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
99                                        width, height, PIX_FMT_RGB24,
100                                        width, height, pix_fmt,
101                                        sws_flags, NULL, NULL, NULL);
102        if (ci->fromRGB_convert_ctx == NULL) {
103            av_log(NULL, AV_LOG_ERROR,
104                   "Cannot initialize the fromRGB conversion context\n");
105            return;
106        }
107// img_convert parameters are          2 first destination, then 4 source
108// 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    av_free(buf);
115}
116
117