简体   繁体   中英

FFMPEG API and cropping

I learned how to use FFMPEG API with dranger's tutorial , and I implemented a video reader using the library SDL to display the video.

I have a HD video 1280*720 (i only worked with mp4) and i want to select a VGA-screen anywhere in the HD video (I mean cropping a VGA screen in a HD video), recuperate the data and display it on screen.

In the FFMPEG API, we can use the function av_picture_crop ( here ). I get a "yellow" overlay on the cropped video and my application crashes after fews seconds. Before posting here, i read here that the function wasn't finish yet. But when i reading the code, i don't find a way to finish it. This is a part of my code :

AVFrame *pFrame = NULL;
AVFrame *pFrameCropped = NULL;
bmp = SDL_CreateYUVOverlay(CODEC_WIDTH, // width
                          CODEC_HEIGHT, // height
                          SDL_YV12_OVERLAY, // format
                          screen); // SDL_Surface to display


sws_ctx = sws_getContext(CODEC_WIDTH, // src width
                        CODEC_HEIGHT, // src height
                        pCodecCtx->pix_fmt, // src img format
                        STREAM_WIDTH, // dest width, 
                        STREAM_HEIGHT, // dest height
                        AV_PIX_FMT_YUV420P, // dest img format
                        SWS_BILINEAR, // option to rescalling
                        NULL, //
                        NULL, //
                        NULL //
                        );

while(
av_read_frame(pFormatCtx, 
                &packet)>=0) 
{
if(packet.stream_index==videoStream) 
{
  avcodec_decode_video2(pCodecCtx,
                         pFrame,
                         &frameFinished,
                         &packet);

  if(frameFinished) 
  {
    SDL_LockYUVOverlay(bmp);

    av_picture_crop((AVPicture*)pFrameCropped,
                     (AVPicture*)pFrame,
                     (AVPixelFormat)pFrame->format,
                     150,
                     300);
    pict.data[0] = pFrameCropped->data[0];// "X"
    pict.data[1] = pFrameCropped->data[1];
    pict.data[2] = pFrameCropped->data[2];

    // pict.linesize == number of bytes per line 
    pict.linesize[0] = pFrameCropped->linesize[0];
    pict.linesize[1] = pFrameCropped->linesize[2];
    pict.linesize[2] = pFrameCropped->linesize[1];

    sws_scale(sws_ctx, // the scaling context previously created with sws_getContext()
                (uint8_t const * const *)pFrameCropped->data, // Pointers to the planes of the source slice 
                pFrame->linesize, // the array containing the strides for each plane of the source image 
                0, // position in src img processed slice.  
                   // It's number (counted starting from zero) 
                   // in the image of the first row of the slice  
                CODEC_HEIGHT, // source slice height. Number of rows in the slice
                pict.data, // pointers to the planes of the destination image 
                pict.linesize); // strides for each plane of the destination image 

    // Unlock SDL_Overlay
    SDL_UnlockYUVOverlay(bmp);
}

Get the compilation error :

*** glibc detected *** ./HDtoVGA: corrupted double-linked list: 0x08d74e30 ***

In the FFMPEG command line tool, we can crop a video, using vf_crop ( here ) but I don't find how to implement the same function in my code.

Do you have any hint to help me?

Maybe the API you offered( vf_crop ) is deprecated. For the example of the latest API, you could reference filtering video example.

For cropping a video, pass value like ' crop=ow:oh:x:y ' to the variable filter_descr :

const char *filter_descr = "crop=1/3*in_w:1/3*in_h:1/3*in_w:0";

And also, you could reference the sample project: simplest ffmpeg video filter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM