[Libav-user] av_open_input_file with URLProtocol problem

francesco at bltitalia.com francesco at bltitalia.com
Thu Feb 2 17:02:46 CET 2012


Hi to all
i have the subsequent problem.  I wrote my own URLProtocol routines as in
the subsequent:

unsigned char InputCompressed[0x400000];  // 4M buffer
unsigned long bufPnt, fileSize;

int myReadFunction (URLContext *h, uint8_t *buf, int buf_size) 
{
   if(((bufPnt + buf_size) > 0x400000)||(buf_size<0)) return -1;
   memcpy(buf,&InputCompressed[bufPnt],buf_size);
   bufPnt +=buf_size;
   return buf_size;
}

int64_t mySeekFunction (URLContext *h, const int64_t offset, int whence) 
{
 int64_t retval=0;

  switch(whence)
  {
     case SEEK_SET :
       if( offset > 0x400000) retval= -1; else  bufPnt = offset;
     break;
     case SEEK_CUR :
       if((bufPnt + offset) > 0x400000) retval= -1; else  bufPnt += offset;
     break;
     case SEEK_END :
       if( offset > 0x400000) retval= -1; else  bufPnt = 0x400000 - offset;
     break;
     case AVSEEK_SIZE :
     retval=(fileSize<0x400000) ? fileSize : 0x400000;
     break;
  }
  return retval;
}

int myWriteFunction (URLContext *h, const uint8_t *buf, int buf_size)
{
 int realSize;
   if((bufPnt==0x400000)||(buf_size<0)) return -1;
   realSize = ((bufPnt + buf_size) > 0x400000) ? 0x400000 - bufPnt: buf_size;
   memcpy(&InputCompressed[bufPnt],buf, realSize);
   bufPnt +=realSize;
   return realSize;
}

int myOpenFunction(URLContext *h, const char *pseudofilename, int flags)
{
    bufPnt = 0;
}

int myCloseFunction(URLContext* h)
{
    return 0;
}


URLProtocol my_protocol = 
{
    "myProto",
    myOpenFunction,
    myReadFunction,
    myWriteFunction,
    mySeekFunction,
    myCloseFunction,
};

used in this way (after loading the raw image into the InputCompressed buffer) :

  pCodec = NULL;   pFrame = NULL;  pFormatCtx = NULL; pCodecCtx = NULL;

  av_init_packet(&packet);

  av_register_protocol2(&my_protocol,sizeof (URLProtocol));

  str1.printf("myProto:0x%08lx",InputCompressed);

  if(av_open_input_file(&pFormatCtx,str1.c_str(),NULL,0,NULL)!=0)
// >>> if(av_open_input_file(&pFormatCtx,filename.c_str(),NULL,0,NULL)!=0)
<<<<<
     {Memo1->Lines->Add("Error opening raw file"); goto end_conv;}

  if(av_find_stream_info(pFormatCtx)>0)
     {Memo1->Lines->Add("Error reading stream info"); goto end_conv;}

  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) {Memo1->Lines->Add("Unable to detect stream type");
goto end_conv;}
   // Get a pointer to the codec context for the video stream

The strange thing is that if the image is a MPEG2 one it works and detect 
the codec_type, while if the image is a mjpeg compressed, the result is
"Unable to detect stream type".
If I use the av_open_input_file with the filename, then is possible to
detect the
codec_type.
I think the problem is inthe myProtocol functions.  Any idea ?




More information about the Libav-user mailing list