[libav-api] please support about Custom AVIOContext

HungNT hungnt at hiworld.com.vn
Sat Dec 14 07:08:53 CET 2013


why i'm can't  call avformat_open_input(&pFormatCtx,"", NULL, NULL).my program alway crash when call this function for Open Custom buff.my Buffer is Raw Data video input here 


-(id)initWithBuffer:(void*)buff size:(int)buff_size{
    
    AVCodec *pCodec;
    avcodec_register_all();
    // Register all formats and codecs
    av_register_all();
  //  av_init_packet(&packet);
    if(avformat_network_init()!=0){
        goto initError; // Couldn't init new work
    }
    uint8_t* pBuffer=av_malloc(buff_size*sizeof(uint8_t));
    
    pFormatCtx = avformat_alloc_context();

    AVIOContext* pIOCtx = avio_alloc_context(pBuffer,buff_size + FF_INPUT_BUFFER_PADDING_SIZE,0,buff,&read_packet, NULL, NULL);
    pIOCtx->seekable = 0;
    
    pFormatCtx->iformat=av_find_input_format("h264");
    pFormatCtx->flags = AVFMT_FLAG_CUSTOM_IO;
    pFormatCtx->iformat->flags=AVFMT_NOFILE;
    pFormatCtx->pb=pIOCtx;
    /**
     * Return the LIBAVFORMAT_VERSION_INT constant.
     */
     printf("======>FFmpeg Version:%d \n ", avformat_version());
    
    /**
     * Return the libavformat build-time configuration.
     */
    printf("========>FFmpeg libavformat Configuration:%s \n ",avformat_configuration());
    
    /**
     * Return the libavformat license.
     */
    printf("=========>FFmpeg libavformat license:%s \n",avformat_license());
    
    // Retrieve stream information
    if(avformat_open_input(&pFormatCtx,"", NULL, NULL)<0)
       goto initError;

     if(avformat_find_stream_info(pFormatCtx,NULL)<0){
            goto initError; // Couldn't find stream information
    }
     streamNo=-1;
    // Find the first video stream
    if ((streamNo =  av_find_best_stream(pFormatCtx,AVMEDIA_TYPE_VIDEO, -1, -1, &pCodec, 0)) < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "Cannot find a video stream in the input file %d\n",streamNo);
        goto initError;
    }
    // Get a pointer to the codec context for the video stream
    pCodecCtx=pFormatCtx->streams[streamNo]->codec;
    
    // Find the decoder for the video stream
    pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
    // pCodec=avcodec_find_decoder(AV_CODEC_ID_FLV1);
    if(pCodec==NULL)
        goto initError; // Codec not found
    
    // Open codec
    if(avcodec_open2(pCodecCtx, pCodec, NULL)<0)
        goto initError; // Could not open codec
    
    // Allocate video frame
    pFrame = avcodec_alloc_frame();
    if(pFrame==NULL)
        goto initError; // Not Found Frame
    
    uint8_t *buffer;
    int numBytes;
    // Determine required buffer size and allocate buffer
    //numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
    //                            pCodecCtx->height);
    numBytes=avpicture_get_size(PIX_FMT_YUYV422, pCodecCtx->width,
                                pCodecCtx->height);
    buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
    
	outputWidth = pCodecCtx->width;
	outputHeight = pCodecCtx->height;
    
    avformat_free_context(pFormatCtx);
    return self;
initError:
    av_log(NULL, AV_LOG_ERROR,"====>Error %d \n", pFormatCtx->pb->error);
    NSLog(@"initError in VideoFrameExtractor");
    avformat_free_context(pFormatCtx);
    return nil;
}


More information about the libav-api mailing list