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
162
163
164
165
166
167
168
169
170
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <memory.h>
#include <ctype.h>
#include <jni.h>
#include <android/log.h>

extern "C" {
#include "typedef.h"
#include "basic_op.h"
#include "ld8a.h"

#include "g729a.h"
}

/*encoder variable for open*/
Word32 encodersize;
void *hEncoder;
/*decoder variable for open*/
Word32 decodersize;
void *hDecoder;

/*Default bit streem size*/
#define  BITSTREAM_SIZE  10
/*Fixed rtp header size*/
#define  RTP_HDR_SIZE  12

#define BLOCK_LEN       160

static int codec_open = 0;

static JavaVM *gJavaVM;
const char *kInterfacePath = "org/sipdroid/pjlib/g729";


extern "C"
JNIEXPORT jint JNICALL Java_org_sipdroid_codecs_G729_open
  (JNIEnv *env, jobject obj) {
  //int ret;
  Flag en;
  Flag de;

  if (codec_open++ != 0)
    return (jint)0;

  /*--------------------------------------------------------------------------*
   * Initialization of the coder.                                             *
   *--------------------------------------------------------------------------*/

  encodersize = g729a_enc_mem_size();
  hEncoder = calloc(1, encodersize * sizeof(UWord8));

  if (hEncoder == NULL)
    {
      printf("Cannot create encoder\n");
      exit(2);
    }
  en=g729a_enc_init(hEncoder);
  if(en == 0)
      {
        printf("Cannot create encoder\n");
        exit(2);
      }
  /*-----------------------------------------------------------------*
   *           Initialization of decoder                             *
   *-----------------------------------------------------------------*/

  decodersize = g729a_dec_mem_size();
  hDecoder = calloc(1, decodersize * sizeof(UWord8));
    if (hDecoder == NULL)
    {
      printf("Cannot create decoder\n");
      exit(2);
    }
    de=g729a_dec_init(hDecoder);
    if (de == 0)
          {
            printf("Cannot create decoder\n");
            exit(2);
          }

  return (jint)0;
}

extern "C"
JNIEXPORT jint JNICALL Java_org_sipdroid_codecs_G729_encode
    (JNIEnv *env, jobject obj, jshortArray lin, jint offset, jbyteArray encoded, jint size) {

Word16  speech[L_FRAME];
UWord8  serial[BITSTREAM_SIZE];

int i;

int frsz=L_FRAME;

unsigned int lin_pos = 0;

    if (!codec_open)
      return 0;
      
    if(hEncoder == NULL)
      return 0;

for (i = 0; (i + L_FRAME) <= size; i+=frsz)
   {
    env->GetShortArrayRegion(lin, offset + i,frsz, speech);
    g729a_enc_process(hEncoder, speech, (unsigned char*)serial);
    env->SetByteArrayRegion(encoded, RTP_HDR_SIZE+ lin_pos, BITSTREAM_SIZE, (jbyte *)serial);
    lin_pos += BITSTREAM_SIZE;
   }

    return (jint)lin_pos;
}

extern "C"
JNIEXPORT jint JNICALL Java_org_sipdroid_codecs_G729_decode
    (JNIEnv *env, jobject obj, jbyteArray encoded, jshortArray lin, jint size) {

    Word16  synth[BLOCK_LEN];

    jbyte  serial[BLOCK_LEN];          /* Serial stream               */


      unsigned int lin_pos = 0;

      jbyte i;

      int len=80;

      if (!codec_open)
      return 0;
      
    if(hDecoder == NULL)
      return 0;

      for (i=0; i + BITSTREAM_SIZE <= size; i=i+BITSTREAM_SIZE)
        {
          //env->GetByteArrayRegion(encoded, RTP_HDR_SIZE, size, serial);
            env->GetByteArrayRegion(encoded, i+RTP_HDR_SIZE, BITSTREAM_SIZE,serial);

          g729a_dec_process(hDecoder, (unsigned char*)serial, synth, 0);

          //env->SetShortArrayRegion(lin, 0, size,synth);
          //env->SetShortArrayRegion(lin, lin_pos, size,synth);
          env->SetShortArrayRegion(lin, lin_pos, len,synth);
          lin_pos = lin_pos + len;
        }
    return (jint)lin_pos;
}


extern "C"
JNIEXPORT void JNICALL Java_org_sipdroid_codecs_G729_close
    (JNIEnv *env, jobject obj) {

  if (--codec_open != 0)
    return;

  /*encoder closed*/
  g729a_enc_deinit(hEncoder);
  free(hEncoder);
  hEncoder = NULL;
  /*decoder closed*/
  g729a_dec_deinit(hDecoder);
  free(hDecoder);
  hDecoder = NULL;

}