Fix a typo in an error message.
[obnox/wireshark/wip.git] / plugins / easy_codec / codec-g7231.c
1 /* codec-g7231.c
2 * Easy codecs stub for EasyG7231
3 * 2007 Ales Kocourek
4 *
5 * $Id$
6 *
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU General Public License for more details.
20
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <memory.h>
32
33 #include "codec-g7231.h"
34
35 #include "EasyG7231/EasyG7231.h"
36
37 struct g7231_context {
38   CODER_HANDLE handle;
39   short speach_buffer[L_G7231_FRAME];
40   int l_g7231_frame_compressed;
41 };
42
43 void *codec_g7231_init(void) {
44   struct g7231_context *ctx = 0;
45
46   ctx = (struct g7231_context*)g_malloc0(sizeof(struct g7231_context));
47   ctx->handle = -1;
48   return ctx;
49 }
50
51 void codec_g7231_release(void *context) {
52   struct g7231_context *ctx = (struct g7231_context*)context;
53
54   if (!ctx) return;
55   EasyG7231_release_decoder(ctx->handle);
56   g_free(ctx);
57 }
58
59 int codec_g7231_decode(void *context, const void *input, int inputSizeBytes, void *output, int *outputSizeBytes) {
60   struct g7231_context *ctx = (struct g7231_context*)context;
61   const unsigned char *bitstream = (const unsigned char*)input;
62   short *speech = (short*)output;
63   int decodedBytes = 0;
64   
65   if (!ctx) return 0;
66   
67   if ( ctx->handle == -1) {
68         if ( bitstream[0] & 0x03 ) {
69            ctx->handle=EasyG7231_init_decoder(FALSE);
70            ctx->l_g7231_frame_compressed = L_G7231_FRAME_COMPRESSED_53;
71         } else {
72            ctx->handle=EasyG7231_init_decoder(TRUE);
73            ctx->l_g7231_frame_compressed = L_G7231_FRAME_COMPRESSED_63;
74         }  
75   }
76   
77   if ((inputSizeBytes % ctx->l_g7231_frame_compressed) != 0) 
78     return 0;
79
80   if (!output)
81     return (inputSizeBytes / ctx->l_g7231_frame_compressed) * L_G7231_FRAME * sizeof(short);
82
83
84   while ((inputSizeBytes >= ctx->l_g7231_frame_compressed) &&
85          ((*outputSizeBytes - decodedBytes) >= L_G7231_FRAME * sizeof(short))) {
86     if (EasyG7231_decoder(ctx->handle, (unsigned char*)bitstream, ctx->speach_buffer)) {
87
88       memcpy(speech, ctx->speach_buffer, L_G7231_FRAME * sizeof(short));
89       speech += L_G7231_FRAME;
90       decodedBytes += L_G7231_FRAME * sizeof(short);
91     
92     }
93     bitstream += ctx->l_g7231_frame_compressed;
94     inputSizeBytes -= ctx->l_g7231_frame_compressed;
95   }
96
97   return decodedBytes;
98 }
99