Change license headers to say 'FFmpeg' instead of 'this program/this library'
[ffmpeg.git] / vhook / null.c
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 "framehook.h"
24 #include "swscale.h"
25
26 static int sws_flags = SWS_BICUBIC;
27
28 typedef 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     enum PixelFormat sws_pix_fmt;   // Sws_Context is opaque, we need to save
38     int sws_width, sws_height;      // this to check if we can re-use contexts
39     // Contexts will be re-used 99% of time,
40     // I know of width/height changes only in DVB broadcasts
41
42 } ContextInfo;
43
44 void Release(void *ctx)
45 {
46     ContextInfo *ci;
47     ci = (ContextInfo *) ctx;
48
49     if (ctx) {
50         sws_freeContext(ci->toRGB_convert_ctx);
51         sws_freeContext(ci->fromRGB_convert_ctx);
52         av_free(ctx);
53     }
54 }
55
56 int Configure(void **ctxp, int argc, char *argv[])
57 {
58     fprintf(stderr, "Called with argc=%d\n", argc);
59
60     *ctxp = av_mallocz(sizeof(ContextInfo));
61     return 0;
62 }
63
64 void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
65 {
66     ContextInfo *ci = (ContextInfo *) ctx;
67     char *buf = 0;
68     AVPicture picture1;
69     AVPicture *pict = picture;
70
71     (void) ci;
72
73     if (pix_fmt != PIX_FMT_RGB24) {
74         int size;
75
76         size = avpicture_get_size(PIX_FMT_RGB24, width, height);
77         buf = av_malloc(size);
78
79         avpicture_fill(&picture1, buf, PIX_FMT_RGB24, width, height);
80
81         // if we already got a SWS context, let's realloc if is not re-useable
82         if (ci->toRGB_convert_ctx != NULL) {
83             if ((ci->sws_pix_fmt != pix_fmt) ||
84                  (ci->sws_width != width) || (ci->sws_height != height)) {
85                 sws_freeContext(ci->toRGB_convert_ctx);
86                 ci->toRGB_convert_ctx = NULL;
87                 sws_freeContext(ci->fromRGB_convert_ctx);
88                 ci->fromRGB_convert_ctx = NULL;
89             }
90         }
91         if (ci->toRGB_convert_ctx == NULL) {
92             ci->sws_pix_fmt = pix_fmt;
93             ci->sws_width = width;
94             ci->sws_height = height;
95             ci->toRGB_convert_ctx = sws_getContext(
96                                              ci->sws_width, ci->sws_height,
97                                              ci->sws_pix_fmt,
98                                              ci->sws_width, ci->sws_height,
99                                              PIX_FMT_RGB24,
100                                              sws_flags, NULL, NULL, NULL);
101             if (ci->toRGB_convert_ctx == NULL) {
102                 av_log(NULL, AV_LOG_ERROR,
103                        "Cannot initialize the toRGB conversion context\n");
104                 exit(1);
105             }
106             ci->fromRGB_convert_ctx = sws_getContext(
107                                              ci->sws_width, ci->sws_height,
108                                              PIX_FMT_RGB24,
109                                              ci->sws_width, ci->sws_height,
110                                              ci->sws_pix_fmt,
111                                              sws_flags, NULL, NULL, NULL);
112             if (ci->fromRGB_convert_ctx == NULL) {
113                 av_log(NULL, AV_LOG_ERROR,
114                        "Cannot initialize the fromRGB conversion context\n");
115                 exit(1);
116             }
117         }
118 // img_convert parameters are          2 first destination, then 4 source
119 // sws_scale   parameters are context, 4 first source,      then 2 destination
120         sws_scale(ci->toRGB_convert_ctx,
121             picture->data, picture->linesize, 0, ci->sws_height,
122             picture1.data, picture1.linesize);
123
124         pict = &picture1;
125     }
126
127     /* Insert filter code here */
128
129     if (pix_fmt != PIX_FMT_RGB24) {
130         sws_scale(ci->fromRGB_convert_ctx,
131             picture1.data, picture1.linesize, 0, ci->sws_height,
132             picture->data, picture->linesize);
133     }
134
135     av_free(buf);
136 }
137