File: jpg_filter.c

package info (click to toggle)
camserv 0.42-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,884 kB
  • ctags: 1,123
  • sloc: ansic: 19,694; sh: 7,454; makefile: 206
file content (161 lines) | stat: -rw-r--r-- 4,828 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*  camserv - An internet streaming picture application
 *
 *  Copyright (C) 1999  Jon Travis (jon@dsndata.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/time.h>

#include "log.h"
#include "camconfig.h"
#include "video.h"
#include "filter.h"
#include "jpgstuff.h"

#define MODNAME "jpg_filter"

typedef struct jpg_info_st {
  JPEG_Wrapper jwrap;
  JPEG_Params jparams;
  char *jpeg_data;
  int is_last_filter;
} JPG_Info;

/*
 * filter_init:  Standard filter initialization routine.
 *
 * Filter variables:  jpgmod_quality = Number between 0 and 100 of the jpeg
 *                                     quality to use when compressing
 */                 

void *filter_init( CamConfig *ccfg, char *section_name ){
  JPG_Info *jinfo;
  int err, nbytes;

  if( (jinfo = malloc( sizeof( *jinfo ))) == NULL ){
    camserv_log( MODNAME,
		 "FATAL!  Could not allocate %d bytes!", sizeof( *jinfo ));
    return NULL;
  }
  
  /* Reconfigure based upon camcfg information */
  jinfo->jparams.quality = camconfig_query_def_int( ccfg, section_name, 
						    "quality",10);
  jinfo->jparams.width = camconfig_query_int( ccfg, SEC_VIDEO, 
					      VIDCONFIG_WIDTH, &err );
  if( err ) camserv_log( MODNAME, "Config inconsistancy! (width)");

  jinfo->jparams.height = camconfig_query_int( ccfg, SEC_VIDEO,
					       VIDCONFIG_HEIGHT, &err );
  if( err ) camserv_log( MODNAME, "Config inconsistancy! (height)");

  jinfo->jparams.is_black_white = camconfig_query_int( ccfg, SEC_VIDEO,
						       VIDCONFIG_ISB_N_W,
						       &err );
  if( err ) camserv_log( MODNAME, "Config inconsistancy! (isb_n_w)");
  
  nbytes = jinfo->jparams.width * jinfo->jparams.height *
    (jinfo->jparams.is_black_white ? 1 : 3 );
  if( (jinfo->jpeg_data = malloc( nbytes )) == NULL ){
    camserv_log( MODNAME, 
		"FATAL!  Unable to allocate %d bytes for jpeg picture",nbytes);
    free( jinfo );
    return NULL;
  }

  if( JPEG_Wrapper_initialize( &jinfo->jwrap, &jinfo->jparams,
			       jinfo->jpeg_data, nbytes ) == -1 ){
    camserv_log( MODNAME, "FATAL!  Unable to initialize jpg wrapper!");
    free( jinfo->jpeg_data );
    free( jinfo );
    return NULL;
  }
  return jinfo;
}

void filter_deinit( void *filter_dat ){
  JPG_Info *jinfo = filter_dat;

  JPEG_Wrapper_deinitialize( &jinfo->jwrap );
  free( jinfo->jpeg_data );
  free( jinfo );
}

void filter_func( char *in_data, char **out_data, void *cldat, 
		  const Video_Info *vinfo_in, Video_Info *vinfo_out )
{
  JPG_Info *jinfo = cldat;
  int is_last_filter;
  JOCTET *last_buf_value;

  is_last_filter = (*out_data != NULL ) && (in_data != *out_data);

  *vinfo_out = *vinfo_in;

  last_buf_value = jinfo->jwrap.jpeg_output_buffer;
  if( is_last_filter ) {
    /* Small optimization:  we can output directly to the final 
       picture memory */
    jinfo->jwrap.jpeg_output_buffer = (JOCTET *)*out_data;
  }  else {
    jinfo->jwrap.jpeg_output_buffer = (JOCTET *)jinfo->jpeg_data;
    *out_data = jinfo->jpeg_data;
  }

  JPEG_Wrapper_do_compress( &jinfo->jwrap, vinfo_in->width, vinfo_in->height,
			    (JSAMPLE *)in_data );

  vinfo_out->nbytes = jinfo->jwrap.actual_jpeg_size;

  if( !is_last_filter ) {
    memcpy( jinfo->jpeg_data, jinfo->jwrap.jpeg_output_buffer, 
	    jinfo->jwrap.actual_jpeg_size );
  }

  jinfo->jwrap.jpeg_output_buffer = last_buf_value;
}

void filter_validation(){
  Filter_Init_Func init = filter_init;
  Filter_Deinit_Func deinit = filter_deinit;
  Filter_Func_Func func = filter_func;

  if( init != NULL && deinit != NULL && func != NULL ) return;
}

/*
 * modinfo_query:  Routine to return information about the variables
 *                 accessed by this particular module.
 *
 * Return values:  Returns a malloced ModInfo structure, for which
 *                 the caller must free, or NULL on failure.
 */

ModInfo *modinfo_query(){
  ModInfo *res;

  if( (res = modinfo_create( 1 )) == NULL )
    return NULL;

  modinfo_varname_set( res, 0, "quality" );
  modinfo_desc_set( res, 0, "JPEG Quality (0->100)" );
  res->vars[ 0 ].type = MODINFO_TYPE_INT;
  
  return res;
}