]> git.sesse.net Git - ffmpeg/blob - vhook/null.c
ff822037e6ececa3a4fe8546b63e141fc2372814
[ffmpeg] / vhook / null.c
1 /*
2  * Null Video Hook
3  * Copyright (c) 2002 Philip Gladstone
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 #include <stdio.h>
20
21 #include "framehook.h"
22 #include "swscale.h"
23
24 static int sws_flags = SWS_BICUBIC;
25
26 typedef struct {
27     int dummy;
28
29     // This vhook first converts frame to RGB ...
30     struct SwsContext *toRGB_convert_ctx;
31
32     // ... and later converts back frame from RGB to initial format
33     struct SwsContext *fromRGB_convert_ctx;
34
35     enum PixelFormat sws_pix_fmt;   // Sws_Context is opaque, we need to save
36     int sws_width, sws_height;      // this to check if we can re-use contexts
37     // Contexts will be re-used 99% of time,
38     // I know of width/height changes only in DVB broadcasts
39
40 } ContextInfo;
41
42 void Release(void *ctx)
43 {
44     ContextInfo *ci;
45     ci = (ContextInfo *) ctx;
46
47     if (ctx) {
48         sws_freeContext(ci->toRGB_convert_ctx);
49         sws_freeContext(ci->fromRGB_convert_ctx);
50         av_free(ctx);
51     }
52 }
53
54 int Configure(void **ctxp, int argc, char *argv[])
55 {
56     fprintf(stderr, "Called with argc=%d\n", argc);
57
58     *ctxp = av_mallocz(sizeof(ContextInfo));
59     return 0;
60 }
61
62 void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
63 {
64     ContextInfo *ci = (ContextInfo *) ctx;
65     char *buf = 0;
66     AVPicture picture1;
67     AVPicture *pict = picture;
68
69     (void) ci;
70
71     if (pix_fmt != PIX_FMT_RGB24) {
72         int size;
73
74         size = avpicture_get_size(PIX_FMT_RGB24, width, height);
75         buf = av_malloc(size);
76
77         avpicture_fill(&picture1, buf, PIX_FMT_RGB24, width, height);
78
79         // if we already got a SWS context, let's realloc if is not re-useable
80         if (ci->toRGB_convert_ctx != NULL) {
81             if ((ci->sws_pix_fmt != pix_fmt) ||
82                  (ci->sws_width != width) || (ci->sws_height != height)) {
83                 sws_freeContext(ci->toRGB_convert_ctx);
84                 ci->toRGB_convert_ctx = NULL;
85                 sws_freeContext(ci->fromRGB_convert_ctx);
86                 ci->fromRGB_convert_ctx = NULL;
87             }
88         }
89         if (ci->toRGB_convert_ctx == NULL) {
90             ci->sws_pix_fmt = pix_fmt;
91             ci->sws_width = width;
92             ci->sws_height = height;
93             ci->toRGB_convert_ctx = sws_getContext(
94                                              ci->sws_width, ci->sws_height,
95                                              ci->sws_pix_fmt,
96                                              ci->sws_width, ci->sws_height,
97                                              PIX_FMT_RGB24,
98                                              sws_flags, NULL, NULL, NULL);
99             if (ci->toRGB_convert_ctx == NULL) {
100                 av_log(NULL, AV_LOG_ERROR,
101                        "Cannot initialize the toRGB conversion context\n");
102                 exit(1);
103             }
104             ci->fromRGB_convert_ctx = sws_getContext(
105                                              ci->sws_width, ci->sws_height,
106                                              PIX_FMT_RGB24,
107                                              ci->sws_width, ci->sws_height,
108                                              ci->sws_pix_fmt,
109                                              sws_flags, NULL, NULL, NULL);
110             if (ci->fromRGB_convert_ctx == NULL) {
111                 av_log(NULL, AV_LOG_ERROR,
112                        "Cannot initialize the fromRGB conversion context\n");
113                 exit(1);
114             }
115         }
116 // img_convert parameters are          2 first destination, then 4 source
117 // sws_scale   parameters are context, 4 first source,      then 2 destination
118         sws_scale(ci->toRGB_convert_ctx,
119             picture->data, picture->linesize, 0, ci->sws_height,
120             picture1.data, picture1.linesize);
121
122         pict = &picture1;
123     }
124
125     /* Insert filter code here */
126
127     if (pix_fmt != PIX_FMT_RGB24) {
128         sws_scale(ci->fromRGB_convert_ctx,
129             picture1.data, picture1.linesize, 0, ci->sws_height,
130             picture->data, picture->linesize);
131     }
132
133     av_free(buf);
134 }
135