tags:

views:

33

answers:

1

This is a code snippet to add a logo to decoded YUV picture (by ffmpeg). http://pastebin.com/md6c16c5

It almost works. Only one problem : sometimes the logo's board is heavily messed up with the surrounding part. The most strange thing is that sometimes it works OK but sometimes it doesn't.

The code would modify the AVFrame allocated by ffmpeg. Would that cause the problem?

    void    OsdLogo(AVFrame* picture, unsigned char *pBufLogo, int xPos, int yPos)
{
    unsigned char * pYBuf   = picture->data[0];
    unsigned char * pUBuf       = picture->data[1];
    unsigned char * pVBuf       = picture->data[2];
    unsigned char * pBufLogoY = pBufLogo;
    unsigned char * pBufLogoU = pBufLogo+180*52;
    unsigned char * pBufLogoV = pBufLogoU+180*52/4;
    int i,j;
    if ((xPos&0x01)!=0)
    {
        xPos&=(~0x01);
    }
    if ((yPos&0x01)!=0)
    {
        yPos&=(~0x01);
    }
    pYBuf+=(yPos*picture->linesize[0]+xPos);
    pUBuf+=(yPos*picture->linesize[0]/4+xPos/2);
    pVBuf+=(yPos*picture->linesize[0]/4+xPos/2);
    for (i=0; i < 52 ; i+=2)
    {
        for(j=0; j < 180 ; j+=2)
        {
            if ((*pBufLogo)<205)
            {
                //Y
                *(pYBuf+picture->linesize[0]) = *(pBufLogo);
                *pYBuf++ = *pBufLogo;
                *(pYBuf+picture->linesize[0]) = *(pBufLogo);
                *pYBuf++ = *pBufLogo;
                //U
                (*pUBuf++) =(*pBufLogoU++);
                //V
                (*pVBuf++) =(*pBufLogoV++);
            }
            else
            {
                pYBuf+=2;
                pUBuf++;
                pVBuf++;
                pBufLogoU++;
                pBufLogoV++;
            }
            pBufLogo+=2;
        }
        pYBuf+=(picture->linesize[0]-180+picture->linesize[0]);
        pUBuf+=((picture->linesize[0]-180)/2);
        pVBuf+=((picture->linesize[0]-180)/2);
        pBufLogo+=180;
    }
}
+1  A: 

Here is an implementation of alpha blended watermarks for the vhook interface of ffmpeg, which sounds similar to what you are doing, so may be helpful.

http://refractalize.blogspot.com/2009/09/alpha-blended-watermarks-with-ffmpeg.html

Eric Strom