Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- jint Java_com_encoder_util_EncADPCM_InitEncoder(JNIEnv* env, jobject thiz, jint samplerate, jint bitrate, jint channels, jint codeccode) {
- avcodec_register_all();
- av_init_packet(&packet);
- av_log_set_level(AV_LOG_VERBOSE);
- av_log_set_callback(log_callback);
- switch(codeccode) {
- case 0: codec = avcodec_find_encoder(AV_CODEC_ID_ADPCM_IMA_QT); break;
- case 1: codec = avcodec_find_encoder(AV_CODEC_ID_ADPCM_IMA_WAV); break;
- case 2: codec = avcodec_find_encoder(AV_CODEC_ID_ADPCM_G726); break;
- case 3: codec = avcodec_find_encoder(AV_CODEC_ID_ADPCM_G722); break;
- case 4: codec = avcodec_find_encoder(AV_CODEC_ID_PCM_ALAW); break;
- case 5: codec = avcodec_find_encoder(AV_CODEC_ID_PCM_MULAW); break;
- default: codec = NULL;
- }
- if (codec == NULL) {
- __android_log_print(ANDROID_LOG_VERBOSE, "INITENC", "something is wrong with codec", 1);
- return -1;
- } else {
- __android_log_print(ANDROID_LOG_VERBOSE, "INITENC", "codec inited: %s", codec->name);
- }
- c = avcodec_alloc_context3(codec);
- if (c==NULL) {
- __android_log_print(ANDROID_LOG_VERBOSE, "INITENC", "something is wrong with context", 1);
- return -2;
- } else {
- c->sample_rate=samplerate;
- //c->bit_rate=bitrate;
- c->sample_fmt = AV_SAMPLE_FMT_S16;
- c->channel_layout = av_get_default_channel_layout(1);
- c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
- }
- avcodec_open2(c, codec, NULL);
- frame = av_frame_alloc();
- if (frame == NULL) {
- __android_log_print(ANDROID_LOG_VERBOSE, "INITENC", "something is wrong with frame alloc");
- return -4;
- }
- frame->nb_samples = 640;
- frame->format = c->sample_fmt;
- frame->channel_layout = c->channel_layout;
- int error = av_frame_get_buffer(frame, 0);
- __android_log_print(ANDROID_LOG_VERBOSE, "INITENC", "framegetbuffer %d", error);
- firstTime = true;
- return 0;
- }
- jint Java_com_encoder_util_EncADPCM_EncodeFrame(JNIEnv* env, jobject thiz, jbyteArray in, jint frameLen, jbyteArray out) {
- jbyte* buf = env->GetByteArrayElements(in, 0);
- jbyte* result = env->GetByteArrayElements(out, 0);
- __android_log_print(ANDROID_LOG_VERBOSE, "encoder", "framelen = %d", frameLen);
- int gotOutput;
- __android_log_print(ANDROID_LOG_VERBOSE, "encoder", "channels = %d, sample fmt = %d, buffer[0] = %d, buffersize = %d", c->channels, c->sample_fmt, (uint8_t)(*buf), frameLen);
- int ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt, (uint8_t*)buf, frameLen, 0);
- if (ret<0) {
- __android_log_print(ANDROID_LOG_VERBOSE, "audenc", "something is wrong with filling audio frame %d", ret);
- return ret;
- }
- ret = avcodec_encode_audio2(c, &packet, frame, &gotOutput);
- __android_log_print(ANDROID_LOG_VERBOSE, "audenc", "gotOutput = %d", gotOutput);
- if (ret >= 0 && gotOutput > 0) {
- for (int i=0; i<frameLen/2; i++) {
- result[i] = packet.data[i];
- }
- }
- (env)->ReleaseByteArrayElements(in, buf, 0);
- (env)->ReleaseByteArrayElements(out, result, 0);
- return frameLen/2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement