aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/easy_codec/codec-g7231.c
blob: d2c65ee31fe0c1dc2df55d70221418b5f96730bd (plain)
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
/* codec-g7231.c
* Easy codecs stub for EasyG7231
* 2007 Ales Kocourek
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "config.h"

#include <string.h>

#include <glib.h>

#include "codec-g7231.h"

#include "EasyG7231/EasyG7231.h"

struct g7231_context {
  CODER_HANDLE handle;
  short speach_buffer[L_G7231_FRAME];
  int l_g7231_frame_compressed;
};

void *codec_g7231_init(void) {
  struct g7231_context *ctx = 0;

  ctx = (struct g7231_context*)g_malloc0(sizeof(struct g7231_context));
  ctx->handle = -1;
  return ctx;
}

void codec_g7231_release(void *context) {
  struct g7231_context *ctx = (struct g7231_context*)context;

  if (!ctx) return;
  EasyG7231_release_decoder(ctx->handle);
  g_free(ctx);
}

int codec_g7231_decode(void *context, const void *input, int inputSizeBytes, void *output, int *outputSizeBytes) {
  struct g7231_context *ctx = (struct g7231_context*)context;
  const unsigned char *bitstream = (const unsigned char*)input;
  short *speech = (short*)output;
  int decodedBytes = 0;

  if (!ctx) return 0;

  if ( ctx->handle == -1) {
    if ( bitstream[0] & 0x03 ) {
      ctx->handle=EasyG7231_init_decoder(FALSE);
      ctx->l_g7231_frame_compressed = L_G7231_FRAME_COMPRESSED_53;
    } else {
      ctx->handle=EasyG7231_init_decoder(TRUE);
      ctx->l_g7231_frame_compressed = L_G7231_FRAME_COMPRESSED_63;
    }
  }

  if ((inputSizeBytes % ctx->l_g7231_frame_compressed) != 0)
    return 0;

  if (!output)
    return (inputSizeBytes / ctx->l_g7231_frame_compressed) * L_G7231_FRAME * sizeof(short);


  while ((inputSizeBytes >= ctx->l_g7231_frame_compressed) &&
         ((*outputSizeBytes - decodedBytes) >= L_G7231_FRAME * sizeof(short))) {
    if (EasyG7231_decoder(ctx->handle, (unsigned char*)bitstream, ctx->speach_buffer)) {

      memcpy(speech, ctx->speach_buffer, L_G7231_FRAME * sizeof(short));
      speech += L_G7231_FRAME;
      decodedBytes += L_G7231_FRAME * sizeof(short);

    }
    bitstream += ctx->l_g7231_frame_compressed;
    inputSizeBytes -= ctx->l_g7231_frame_compressed;
  }

  return decodedBytes;
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local Variables:
 * c-basic-offset: 2
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=2 tabstop=8 expandtab:
 * :indentSize=2:tabSize=8:noTabs=true:
 */