Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中ffmpeg中的av_read_帧函数始终将packet.stream_索引返回为0_Android_Android Ndk_Ffmpeg_Libavformat - Fatal编程技术网

android中ffmpeg中的av_read_帧函数始终将packet.stream_索引返回为0

android中ffmpeg中的av_read_帧函数始终将packet.stream_索引返回为0,android,android-ndk,ffmpeg,libavformat,Android,Android Ndk,Ffmpeg,Libavformat,我使用下面粘贴的标准代码(参考:)在android中使用ndk使用ffmpeg。我的代码在使用gcc编译器的Ubuntu10.04中运行良好。但是我在android中面临一个问题。问题是av读取帧(pFormatCtx和packet)总是返回packet.stream\u index=0。我已经用各种rtsp URL测试了我的代码,并且在所有情况下都有相同的行为。我没有任何链接或编译的问题,因为除了这个问题,一切似乎都很好。在过去的两天里,我一直在努力解决这个问题,但我陷入了困境。请给我指出正确

我使用下面粘贴的标准代码(参考:)在android中使用ndk使用ffmpeg。我的代码在使用gcc编译器的Ubuntu10.04中运行良好。但是我在android中面临一个问题。问题是av读取帧(pFormatCtx和packet)总是返回
packet.stream\u index=0
。我已经用各种rtsp URL测试了我的代码,并且在所有情况下都有相同的行为。我没有任何链接或编译的问题,因为除了这个问题,一切似乎都很好。在过去的两天里,我一直在努力解决这个问题,但我陷入了困境。请给我指出正确的方向

#include <jni.h>
#include <android/log.h>

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>

#include <stdio.h>
#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)->GetStringUTFChars(env, myjurl, &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(&pFormatCtx, mycurl, NULL, NULL)!=0)
      return -1;
  __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "start: %d\t%d\n",pFormatCtx->raw_packet_buffer_remaining_size,pFormatCtx->max_index_size);

  (*env)->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)<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<pFormatCtx->nb_streams; i++)
      if(pFormatCtx->streams[i]->codec->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->streams[videoStream]->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->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, &optionsDict)<0)
      return -1;
  // Allocate video frame
  pFrame=avcodec_alloc_frame();
  sws_ctx = sws_getContext(pCodecCtx->width,pCodecCtx->height,pCodecCtx->pix_fmt,pCodecCtx->width,
        pCodecCtx->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, &packet)>=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, &frameFinished,&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(&packet);
      if(++count>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(&pFormatCtx);

  return 0;

}
#包括
#包括
#包括
#包括
#包括
#包括
#定义调试标签“mydebug\u ndk”
jint Java_com_example_tut2_main activity_myfunc(JNIEnv*env,jobject this,jstring myjurl){
AVFormatContext*pFormatCtx=NULL;
inti,视频流;
AVCodecContext*pCodecCtx=NULL;
AVCodec*pCodec=NULL;
AVFrame*pFrame=NULL;
数据包;
int框架完成;
AVDictionary*选项DICT=NULL;
结构SwsContext*sws_ctx=NULL;
jboolean-isCopy;
const char*mycurl=(*env)->GetStringUTFChars(env、myjurl和isCopy);
__安卓日志打印(安卓日志调试,调试标签,“NDK:p2:[%s]”,mycurl);
//注册所有格式和编解码器
av_寄存器_all();
avformat_network_init();
//打开视频文件
__android_日志_打印(android_日志_调试,调试标签,“NDK:before_open”);
if(avformat\u open\u输入(&pFormatCtx,mycurl,NULL,NULL)!=0)
返回-1;
__android日志打印(android日志调试,调试标签,“开始:%d\t%d\n”,pFormatCtx->raw\u数据包缓冲区剩余大小,pFormatCtx->max\u索引大小);
(*env)->释放StringUTFChars(env、myjurl、mycurl);
__android_日志_打印(android_日志_调试,调试标签,“NDK:before_stream”);
//检索流信息
如果(avformat\u find\u stream\u info(pFormatCtx,NULL)streams[i]->codec->codec\u type==AVMEDIA\u type\u VIDEO){
视频流=i;
打破
}
如果(视频流==-1)
return-1;//未找到视频流
__android日志打印(android日志调试,调试标签,“NDK:after\u videostream”);
//获取指向视频流的编解码器上下文的指针
pCodecCtx=pFormatCtx->streams[videoStream]->编解码器;
__android_log_print(android_log_DEBUG,DEBUG_标签,“NDK:after_codec_context”);
//查找视频流的解码器
pCodec=avcodec\u find\u解码器(pCodecCtx->codec\u id);
__安卓日志打印(安卓日志调试,调试标签,“NDK:after_decoder”);
if(pCodec==NULL)
返回-1;
__android_日志_打印(android_日志_调试,调试标签,“NDK:found_解码器”);
//开放式编解码器
如果(avcodec_open2(pCodecCtx、pCodec和optionsDict)宽度,pCodecCtx->height,pCodecCtx->pix_fmt,pCodecCtx->width,
PCODECTX->高度,PIX_FMT_YUV420P,SWS_双线性,空,空,空);
__android_log_print(android_log_DEBUG,DEBUG_标签,“NDK:before_while”);
整数计数=0;
而(av_读取_帧(pFormatCtx和数据包)>=0){
__android_日志_打印(android_日志_调试,调试标签,“输入时:%d%d%d\n”,数据包。持续时间,数据包。流索引,数据包。大小);
if(数据包流\索引==视频流){
//解码视频帧
//中断;
avcodec_decode_video2(pCodecCtx、pFrame、frameFinished和packet);
__android_log_print(android_log_DEBUG,DEBUG_标签,“NDK:in_while”);
如果(框架完成){
__android_log_print(android_log_DEBUG,DEBUG_标签,“NDK:gng_out of_while”);
打破
}
}
//释放av_read_帧分配的数据包
av_免费_数据包(&数据包);
如果(++计数>1000)
返回-2;//无限while循环
}
__android_log_print(android_log_DEBUG,DEBUG_标签,“NDK:after_while”);
//释放YUV框架
无AVU(pFrame);
//关闭编解码器
avcodec_关闭(PCODECTX);
//关闭视频文件
avformat_close_输入(&P格式发送);
返回0;
}
我也有同样的问题。 经过我的测试,我发现了我的错误,希望能对你有所帮助

my av_read_frame()也总是返回流索引0的av_pkg

问题是:我混合了两个版本的ffmpeg-libs。

在之前,我将安装0.9.x的ffmpeg 现在,我安装1.2版的ffmpeg

lib的版本不同,我使用了0.9.x中的一些lib和1.2中的一些lib。 所以发生错误

一、解决方案:

卸载所有版本的ffmpeg,然后重新安装一个版本的ffmpeg(me->1.2)。
av_read_frame()向右移动。:)

我也有同样的问题。希望有人能帮助我们(我遇到了类似的恐龙问题和解决方案。我的项目使用的是ffmpeg v2.0库,但包含v1.2.1中的头文件。这些版本非常相似,可以避免编译错误,但不同程度足以导致AVPacket的数据对齐问题。