Advertisement
Guest User

ffmpeg pcm_mulaw jni

a guest
Oct 8th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. jint Java_com_encoder_util_EncADPCM_InitEncoder(JNIEnv* env, jobject thiz, jint samplerate, jint bitrate, jint channels, jint codeccode) {
  2.     avcodec_register_all();
  3.     av_init_packet(&packet);
  4.     av_log_set_level(AV_LOG_VERBOSE);
  5.     av_log_set_callback(log_callback);
  6.     switch(codeccode) {
  7.         case 0: codec = avcodec_find_encoder(AV_CODEC_ID_ADPCM_IMA_QT); break;
  8.         case 1: codec = avcodec_find_encoder(AV_CODEC_ID_ADPCM_IMA_WAV); break;
  9.         case 2: codec = avcodec_find_encoder(AV_CODEC_ID_ADPCM_G726); break;
  10.         case 3: codec = avcodec_find_encoder(AV_CODEC_ID_ADPCM_G722); break;
  11.         case 4: codec = avcodec_find_encoder(AV_CODEC_ID_PCM_ALAW); break;
  12.         case 5: codec = avcodec_find_encoder(AV_CODEC_ID_PCM_MULAW); break;
  13.         default: codec = NULL;
  14.     }
  15.     if (codec == NULL) {
  16.         __android_log_print(ANDROID_LOG_VERBOSE, "INITENC", "something is wrong with codec", 1);
  17.         return -1;
  18.     } else {
  19.         __android_log_print(ANDROID_LOG_VERBOSE, "INITENC", "codec inited: %s", codec->name);
  20.     }
  21.     c = avcodec_alloc_context3(codec);
  22.     if (c==NULL) {
  23.         __android_log_print(ANDROID_LOG_VERBOSE, "INITENC", "something is wrong with context", 1);
  24.         return -2;
  25.     } else {
  26.         c->sample_rate=samplerate;
  27.         //c->bit_rate=bitrate;
  28.         c->sample_fmt = AV_SAMPLE_FMT_S16;
  29.         c->channel_layout = av_get_default_channel_layout(1);
  30.         c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
  31.     }
  32.     avcodec_open2(c, codec, NULL);
  33.     frame = av_frame_alloc();
  34.     if (frame == NULL) {
  35.         __android_log_print(ANDROID_LOG_VERBOSE, "INITENC", "something is wrong with frame alloc");
  36.         return -4;
  37.     }
  38.     frame->nb_samples = 640;
  39.     frame->format = c->sample_fmt;
  40.     frame->channel_layout = c->channel_layout;
  41.     int error = av_frame_get_buffer(frame, 0);
  42.     __android_log_print(ANDROID_LOG_VERBOSE, "INITENC", "framegetbuffer %d", error);
  43.     firstTime = true;
  44.     return 0;
  45. }
  46.  
  47. jint Java_com_encoder_util_EncADPCM_EncodeFrame(JNIEnv* env, jobject thiz, jbyteArray in, jint frameLen, jbyteArray out) {
  48.     jbyte* buf = env->GetByteArrayElements(in, 0);
  49.     jbyte* result = env->GetByteArrayElements(out, 0);
  50.     __android_log_print(ANDROID_LOG_VERBOSE, "encoder", "framelen = %d", frameLen);
  51.     int gotOutput;
  52.     __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);
  53.     int ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt, (uint8_t*)buf, frameLen, 0);
  54.     if (ret<0) {
  55.         __android_log_print(ANDROID_LOG_VERBOSE, "audenc", "something is wrong with filling audio frame %d", ret);
  56.         return ret;
  57.     }
  58.     ret = avcodec_encode_audio2(c, &packet, frame, &gotOutput);
  59.     __android_log_print(ANDROID_LOG_VERBOSE, "audenc", "gotOutput = %d", gotOutput);
  60.     if (ret >= 0 && gotOutput > 0) {
  61.         for (int i=0; i<frameLen/2; i++) {
  62.             result[i] = packet.data[i];
  63.         }
  64.     }
  65.     (env)->ReleaseByteArrayElements(in, buf, 0);
  66.     (env)->ReleaseByteArrayElements(out, result, 0);
  67.     return frameLen/2;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement