[FFmpeg-devel] Need Help

Abhishek Srivastava aaabhishek.srivastava at gmail.com
Fri Nov 22 15:01:34 CET 2013


Hi Sir

          From last two week very  frustrated .i am able play to mp3,wav
,flac,.but when I am playing .ogg/ape format its through segmentation fault
in avcodec_decode_audio4(dec_ctx, frame, &got_frame, &packet);.

 my humble request  please see the below code .



{
 jboolean            isCopy;
 AVCodec * dec;
    int                 i;
    int                  err;
    FILE *f, *outfile;
    int                 audioStream=-1;
    int                 res;
    int                 decoded = 0;
    int                 out_size;
    int ret;
    AVFrame *frame = av_frame_alloc();
    int got_frame;
    AVCodec *codec;
    uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
    jclass              cls = (*env)->GetObjectClass(env, obj);
    jmethodID           play = (*env)->GetMethodID(env, cls, "playMusic",
"([BI)V");//At the begining of your main function
    const char *        szfile = (*env)->GetStringUTFChars(env, file,
&isCopy);
    LOGI("in load file");

        if ((ret = avformat_open_input(&fmt_ctx, szfile, NULL, NULL)) < 0)
        {
            LOGE("Cannot open input file\n");
        }

        LOGE("file load %s\n",szfile);

        if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0)
        {
            LOGE("Cannot find stream information\n");
        }

        /* select the audio stream */
        ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1,
&dec, 0);
        if (ret < 0)
        {
            LOGE("Cannot find a audio stream in the input file\n");
        }




        audio_stream_index = ret;
        dec_ctx = fmt_ctx->streams[audio_stream_index]->codec;
        LOGD("%d......%d\n",dec_ctx->codec_id, AV_CODEC_ID_AAC);



     //    filter_codec_opts(codec_opts, dec_ctx->codec_id,
fmt_ctx,fmt_ctx->streams[audio_stream_index] , codec);





        if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0)
        {
            LOGE("Cannot open audio decoder\n");
        }


        dec_ctx->sample_fmt =AV_SAMPLE_FMT_S16;

        f = fopen(szfile, "rb");

       if (!f)
       {
           LOGE("could not open");
           exit(1);
       }



        packet.data = inbuf;
        packet.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
        LOGE("Stage 5");
        LOGD("........%d", packet.size);
        while (1)
        {
         LOGD(".....22.....%d", packet.size);

               if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
               {
                LOGE("Stage........... %d",ret);
                   break;
               }
               if (packet.stream_index == audio_stream_index)
               {

                       avcodec_get_frame_defaults(frame);
                       got_frame = 0;
                       LOGE(".file duration=%d
packet_index=%d",packet.duration,packet.stream_index);
                       LOGE(".........%d",errno);


                       ret = avcodec_decode_audio4(dec_ctx, frame,
&got_frame, &packet);
                       LOGE(".........%d",errno);
                       LOGE("len=%d",ret);
                       if (ret < 0)
                       {
                           LOGE("Error decoding audio\n");
                           continue;
                       }

                       if (got_frame)
                       {
                            LOGE("begin frame decode\n");
                            int data_size =
av_samples_get_buffer_size(NULL,
dec_ctx->channels,frame->nb_samples,dec_ctx->sample_fmt, 1);
                            LOGE("after frame decode\n");

                            jbyte *bytes =
(*env)->GetByteArrayElements(env, array, NULL);
                            memcpy(bytes, frame->data[0], data_size); //
                            (*env)->ReleaseByteArrayElements(env, array,
bytes, 0);
                            (*env)->CallVoidMethod(env, obj,play, array,
data_size);

                       }

                       packet.size -= ret;
                       packet.data += ret;
                       packet.pts = AV_NOPTS_VALUE;


                      if (packet.size < AUDIO_REFILL_THRESH)
                      {
                          memmove(inbuf, packet.data, packet.size);
                          packet.data = inbuf;
                          ret = fread(packet.data + packet.size, 1,
AUDIO_INBUF_SIZE - packet.size, f);
                          if (ret > 0)
                              packet.size += ret;
                      }

                 }
          }
               av_free_packet(&packet);


More information about the ffmpeg-devel mailing list