簡體   English   中英

調用 av_frame_get_buffer 時如何指定幀的線寬

[英]How do I specify the linesize of a frame when calling av_frame_get_buffer

    int stream_index = find_stream(AVMEDIA_TYPE_VIDEO, input);

    AVStream *s = input->container->streams[stream_index];

    AVFrame *new_frame = av_frame_alloc();

    new_frame->width = s->codecpar->width;
    new_frame->height = s->codecpar->height;
    new_frame->format = s->codecpar->format;

    av_frame_get_buffer(new_frame, 0);

    printf("%i %i %i %i %i %i\n", new_frame->linesize[0], new_frame->linesize[1], new_frame->linesize[2], new_frame->width, new_frame->height, new_frame->format);
    apply_path(input->paths[stream_index], new_frame);

使用av_frame_get_buffer(new_frame, 0); 這是:

linesize 0  1080  
linesize 1  540  
linesize 2  540  
width  1080  
height  1080  
format  0 (AV_PIX_FMT_YUV420P)  

我想要的值:

linesize 0  1152  
linesize 1  576  
linesize 2  576  
width  1080  
height  1080  
format  0 (AV_PIX_FMT_YUV420P)   

我想使用線寬而不是寬度和高度,因為當我解碼來自不同視頻的幀時,它們可能具有不同的線寬但寬度和高度相同

我們可以使用av_malloc手動分配:

AVFrame* new_frame = av_frame_alloc();

int linesize0 = 1152;
int linesize1 = 576;
int linesize2 = 576;
int width = 1080;
int height = 1080;

new_frame->linesize[0] = linesize0;
new_frame->linesize[1] = linesize1;
new_frame->linesize[2] = linesize2;

new_frame->data[0] = (uint8_t*)av_malloc(linesize0 * height);
new_frame->data[1] = (uint8_t*)av_malloc(linesize1 * height/2); //For YUV420 we need to allocate height/2 rows.
new_frame->data[2] = (uint8_t*)av_malloc(linesize2 * height/2);

new_frame->width = s->codecpar->width;
new_frame->height = s->codecpar->height;
new_frame->format = s->codecpar->format;

根據av_malloc文檔(在mem.h中),function 處理 alignment:

分配一個 memory 塊和 alignment 適合所有 memory 訪問。

建議使用av_malloc而不是使用newmalloc


最后釋放分配的緩沖區:

av_freep(new_frame->data[0]);
av_freep(new_frame->data[1]);
av_freep(new_frame->data[2]);

筆記:

看起來我們也可以使用av_image_alloc
我測試了它,它並沒有像我想象的那樣工作。

/**
 * Allocate an image with size w and h and pixel format pix_fmt, and
 * fill pointers and linesizes accordingly.
 * The allocated image buffer has to be freed by using
 * av_freep(&pointers[0]).
 *
 * @param align the value to use for buffer size alignment
 * @return the size in bytes required for the image buffer, a negative
 * error code in case of failure
 */
int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
                   int w, int h, enum AVPixelFormat pix_fmt, int align);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM