av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Failed to open input file [%s]\n", ci->filename);
av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open input file [%s]\n", ci->filename);
int get_watermark_picture(ContextInfo *ci, int cleanup){    if (1 == ci->is_done && 0 == cleanup) return 0;    // Yes, *pFrameRGB arguments must be null the first time otherwise it's not good..    // This block is only executed the first time we enter this function.    if (0 == ci->pFrameRGB &&         0 == cleanup)     {              /*          * The last three parameters specify the file format, buffer size and format         * parameters; by simply specifying NULL or 0 we ask libavformat to auto-detect         * the format and use a default buffer size. (Didn't work!)         */        if (av_open_input_file(&ci->pFormatCtx, ci->filename, NULL, 0, NULL) != 0) {            // Martin says this should not be necessary but it failed for me sending in            // NULL instead of file_iformat to av_open_input_file()            ci->i = strlen(ci->filename);            if (0 == ci->i) {                av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() No filename to watermark vhook\n");                return -1;            }            while (ci->i > 0) {                if (ci->filename[ci->i] == '.') {                    ci->i++;                    break;                }                ci->i--;            }               ci->p_ext = &(ci->filename[ci->i]);            ci->file_iformat = av_find_input_format (ci->p_ext);            if (0 == ci->file_iformat) {                av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Really failed to find iformat [%s]\n", ci->p_ext);                return -1;            }            // now continues the Martin template.                            if (av_open_input_file(&ci->pFormatCtx, ci->filename, ci->file_iformat, 0, NULL)!=0) {                av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Failed to open input file [%s]\n", ci->filename);                return -1;            }                    }                /*          * This fills the streams field of the AVFormatContext with valid information.         */        if(av_find_stream_info(ci->pFormatCtx)<0) {            av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Failed to find stream info\n");            return -1;        }            /*         * As mentioned in the introduction, we'll handle only video streams, not audio          * streams. To make things nice and easy, we simply use the first video stream we         * find.         */        ci->videoStream=-1;        for(ci->i = 0; ci->i < ci->pFormatCtx->nb_streams; ci->i++)            if(ci->pFormatCtx->streams[ci->i]->codec->codec_type==CODEC_TYPE_VIDEO)            {                ci->videoStream = ci->i;                break;            }        if(ci->videoStream == -1) {            av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Failed to find any video stream\n");            return -1;        }                ci->st = ci->pFormatCtx->streams[ci->videoStream];        ci->x_size = ci->st->codec->width;        ci->y_size = ci->st->codec->height;                // Get a pointer to the codec context for the video stream        ci->pCodecCtx = ci->pFormatCtx->streams[ci->videoStream]->codec;                            /*         * OK, so now we've got a pointer to the so-called codec context for our video         * stream, but we still have to find the actual codec and open it.         */            // Find the decoder for the video stream        ci->pCodec = avcodec_find_decoder(ci->pCodecCtx->codec_id);        if(ci->pCodec == NULL) {            av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Failed to find any codec\n");            return -1;        }                // Inform the codec that we can handle truncated bitstreams -- i.e.,        // bitstreams where frame boundaries can fall in the middle of packets        if (ci->pCodec->capabilities & CODEC_CAP_TRUNCATED)            ci->pCodecCtx->flags|=CODEC_FLAG_TRUNCATED;                // Open codec        if(avcodec_open(ci->pCodecCtx, ci->pCodec)<0) {            av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Failed to open codec\n");            return -1;        }                // Hack to correct wrong frame rates that seem to be generated by some         // codecs        if (ci->pCodecCtx->time_base.den>1000 && ci->pCodecCtx->time_base.num==1)            ci->pCodecCtx->time_base.num=1000;                /*         * Allocate a video frame to store the decoded images in.         */        ci->pFrame = avcodec_alloc_frame();                    /*         * The RGB image pFrameRGB (of type AVFrame *) is allocated like this:         */        // Allocate an AVFrame structure        ci->pFrameRGB=avcodec_alloc_frame();        if(ci->pFrameRGB==NULL) {            av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Failed to alloc pFrameRGB\n");            return -1;        }                // Determine required buffer size and allocate buffer        ci->numBytes = avpicture_get_size(PIX_FMT_RGBA32, ci->pCodecCtx->width,            ci->pCodecCtx->height);        ci->buffer = av_malloc(ci->numBytes);                // Assign appropriate parts of buffer to image planes in pFrameRGB        avpicture_fill((AVPicture *)ci->pFrameRGB, ci->buffer, PIX_FMT_RGBA32,            ci->pCodecCtx->width, ci->pCodecCtx->height);        }    // TODO loop, pingpong etc?    if (0 == cleanup)     {    //        av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Get a frame\n");        while(av_read_frame(ci->pFormatCtx, &ci->packet)>=0)        {            // Is this a packet from the video stream?            if(ci->packet.stream_index == ci->videoStream)            {                // Decode video frame                avcodec_decode_video(ci->pCodecCtx, ci->pFrame, &ci->frameFinished,                     ci->packet.data, ci->packet.size);                        // Did we get a video frame?                if(ci->frameFinished)                {                    // Convert the image from its native format to RGBA32                    img_convert((AVPicture *)ci->pFrameRGB, PIX_FMT_RGBA32,                         (AVPicture*)(ci->pFrame), ci->pCodecCtx->pix_fmt, ci->pCodecCtx->width,                         ci->pCodecCtx->height);                            // Process the video frame (save to disk etc.)                    //fprintf(stderr,"banan() New frame!\n");                    //DoSomethingWithTheImage(ci->pFrameRGB);                    return 0;                }            }                    // Free the packet that was allocated by av_read_frame            av_free_packet(&ci->packet);        }        ci->is_done = 1;            return 0;    } // if 0 != cleanup        if (0 != cleanup)     {        // Free the RGB image        if (0 != ci->buffer) {            av_free(ci->buffer);            ci->buffer = 0;        }        if (0 != ci->pFrameRGB) {            av_free(ci->pFrameRGB);            ci->pFrameRGB = 0;        }                 // Close the codec        if (0 != ci->pCodecCtx) {            avcodec_close(ci->pCodecCtx);            ci->pCodecCtx = 0;        }                // Close the video file        if (0 != ci->pFormatCtx) {            av_close_input_file(ci->pFormatCtx);                ci->pFormatCtx = 0;        }                ci->is_done = 0;    }    return 0;}