Note that there are some explanatory texts on larger screens.

plurals
  1. POav_read_frame function in ffmpeg in android always returning packet.stream_index as 0
    primarykey
    data
    text
    <p>I am using the following standard code pasted below (ref: <a href="http://dranger.com/ffmpeg/" rel="nofollow">http://dranger.com/ffmpeg/</a>) to use ffmpeg in android using ndk. My code is working fine in ubuntu 10.04 using gcc compiler. But I am facing an issue in android.The issue is <code>av_read_frame(pFormatCtx, &amp;packet)</code> is always returning <code>packet.stream_index=0</code>. I have tested my code with various rtsp urls and I have the same behaviour in all cases. I do not have any linking or compiling issues as everything seems to be working fine except this issue. I am trying to solve this from last 2 days but I am stuck badly.Please point me in right direction.</p> <pre><code>#include &lt;jni.h&gt; #include &lt;android/log.h&gt; #include &lt;libavcodec/avcodec.h&gt; #include &lt;libavformat/avformat.h&gt; #include &lt;libswscale/swscale.h&gt; #include &lt;stdio.h&gt; #define DEBUG_TAG "mydebug_ndk" jint Java_com_example_tut2_MainActivity_myfunc(JNIEnv * env, jobject this,jstring myjurl) { AVFormatContext *pFormatCtx = NULL; int i, videoStream; AVCodecContext *pCodecCtx = NULL; AVCodec *pCodec = NULL; AVFrame *pFrame = NULL; AVPacket packet; int frameFinished; AVDictionary *optionsDict = NULL; struct SwsContext *sws_ctx = NULL; jboolean isCopy; const char * mycurl = (*env)-&gt;GetStringUTFChars(env, myjurl, &amp;isCopy); __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:p2: [%s]", mycurl); // Register all formats and codecs av_register_all(); avformat_network_init(); // Open video file __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:before_open"); if(avformat_open_input(&amp;pFormatCtx, mycurl, NULL, NULL)!=0) return -1; __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "start: %d\t%d\n",pFormatCtx-&gt;raw_packet_buffer_remaining_size,pFormatCtx-&gt;max_index_size); (*env)-&gt;ReleaseStringUTFChars(env, myjurl, mycurl); __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:before_stream"); // Retrieve stream information if(avformat_find_stream_info(pFormatCtx, NULL)&lt;0) return -1; __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:after_stream"); // Find the first video stream videoStream=-1; for(i=0; i&lt;pFormatCtx-&gt;nb_streams; i++) if(pFormatCtx-&gt;streams[i]-&gt;codec-&gt;codec_type==AVMEDIA_TYPE_VIDEO) { videoStream=i; break; } if(videoStream==-1) return -1; // Didn't find a video stream __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:after_videostream"); // Get a pointer to the codec context for the video stream pCodecCtx=pFormatCtx-&gt;streams[videoStream]-&gt;codec; __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:after_codec_context"); // Find the decoder for the video stream pCodec=avcodec_find_decoder(pCodecCtx-&gt;codec_id); __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:after_decoder"); if(pCodec==NULL) return -1; __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:found_decoder"); // Open codec if(avcodec_open2(pCodecCtx, pCodec, &amp;optionsDict)&lt;0) return -1; // Allocate video frame pFrame=avcodec_alloc_frame(); sws_ctx = sws_getContext(pCodecCtx-&gt;width,pCodecCtx-&gt;height,pCodecCtx-&gt;pix_fmt,pCodecCtx-&gt;width, pCodecCtx-&gt;height,PIX_FMT_YUV420P,SWS_BILINEAR,NULL,NULL,NULL); __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:before_while"); int count=0; while(av_read_frame(pFormatCtx, &amp;packet)&gt;=0) { __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "entered while: %d %d %d\n", packet.duration,packet.stream_index,packet.size); if(packet.stream_index==videoStream) { // Decode video frame //break; avcodec_decode_video2(pCodecCtx, pFrame, &amp;frameFinished,&amp;packet); __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:in_while"); if(frameFinished) { __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:gng_out_of_while"); break; } } // Free the packet that was allocated by av_read_frame av_free_packet(&amp;packet); if(++count&gt;1000) return -2; //infinite while loop } __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:after_while"); // Free the YUV frame av_free(pFrame); // Close the codec avcodec_close(pCodecCtx); // Close the video file avformat_close_input(&amp;pFormatCtx); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload