FFmpeg H264解析 [英] FFmpeg H264 parsing

查看:573
本文介绍了FFmpeg H264解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FFmpeg解码H.264视频,用于以下程序:

  #include< stdlib.h> ; 
#include< stdio.h>
#include< string.h>
#include< signal.h>

#include< sys / time.h>
#include< time.h>

#include< libavcodec / avcodec.h>
#include< libavutil / mathematics.h>

#include< SDL / SDL.h>

void sigint_handler(int signal){
printf(\\\
);
exit(0);
}

const char * window_title;
SDL_Surface * screen;
SDL_Overlay * yuv_overlay;

#define INBUF_SIZE 80000

/ *
*视频解码示例
* /

static long get_time_diff time_now){
struct timeval time_now2;
gettimeofday(& time_now2,0);
return time_now2.tv_sec * 1.e6 - time_now.tv_sec * 1.e6 + time_now2.tv_usec - time_now.tv_usec;
}

int video_open(AVCodecContext * avctx,const char * filename){
int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
int w,h;

flags | = SDL_RESIZABLE;

if(avctx-> width){
w = avctx-> width;
h = avctx-> height;
} else {
w = 640;
h = 480;
}

if(SDL_Init(SDL_INIT_VIDEO)< 0){
fprintf(stderr,SDL_INIT_VIDEO failed!\\\
);
exit(1);
}

screen = SDL_SetVideoMode(w,h,0,flags);

if(!screen){
fprintf(stderr,SDL:could not set video mode - exiting\\\
);
return -1;
}
if(!window_title)
window_title = filename;
SDL_WM_SetCaption(window_title,window_title);

yuv_overlay = SDL_CreateYUVOverlay(w,h,SDL_YV12_OVERLAY,screen);

if(yuv_overlay-> hw_overlay){
fprintf(stderr,Using hardware overlay!\\\
);
}

return 0;
}

int main(int argc,char ** argv){
AVCodec * codec;
AVCodecContext * c = NULL;
AVCodecParserContext * parser = NULL;
int frame,got_picture,len2,len;
const char * filename;
FILE * f;
AVFrame * picture;
char * arghwtf = malloc(INBUF_SIZE);
char * luma = NULL;
char * chroma = NULL;
int i = 0;
uint64_t in_len;
int pts,dts;
struct timeval t;
float inv_fps = 1e6 / 23.98;
AVPacket avpkt;
SDL_Rect rect;


/ *注册所有编解码器* /
avcodec_register_all();

filename = argv [1];

av_init_packet(& avpkt);

printf(解码文件%s ... \\\
,filename);

/ *查找H.264视频解码器* /
codec = avcodec_find_decoder(CODEC_ID_H264);
if(!codec){
fprintf(stderr,codec not found\\\
);
exit(1);
}

c = avcodec_alloc_context3(codec);
picture = avcodec_alloc_frame();

c-> skip_loop_filter = 48; // skiploopfilter = all

if(avcodec_open(c,codec)< 0){
fprintf(stderr,could not open codec\);
exit(1);
}

/ *编解码器给出了帧大小,以样本为单位* /
parser = av_parser_init(c-> codec_id);
parser-> flags | = PARSER_FLAG_ONCE;

f = fopen(filename,rb);
if(!f){
fprintf(stderr,can not open%s\\\
,filename);
exit(1);
}

frame = 0;
gettimeofday(& t,0);
if(fread(arghwtf,1,INBUF_SIZE,f)== 0){
exit(1);
}
in_len = 80000;
while(in_len> 0&&!feof(f)){
len = av_parser_parse2(parser,c,& avpkt.data,& avpkt.size,arghwtf,in_len,
pts,dts,AV_NOPTS_VALUE);

len2 = avcodec_decode_video2(c,picture,& got_picture,& avpkt);
if(len2< 0){
fprintf(stderr,Error during decode frame%d \\\
,frame);
exit(1);
}
if(got_picture){
if(!screen){
video_open(c,filename);

rect.x = 0;
rect.y = 0;
rect.w = c-> width;
rect.h = c-> height;
inv_fps = av_q2d(c-> time_base);
fprintf(stderr,w:%i h:%i \\\
,rect.w,rect.h);

luma = malloc(c-> width * c-> height);
chroma = malloc(c-> width * c-> height / 4);

SDL_DisplayYUVOverlay(yuv_overlay,& rect);

signal(SIGINT,sigint_handler);
}
fprintf(stderr,\rDisplaying%c:frame%3d(%02d:%02d)...,av_get_pict_type_char(picture-> pict_type),frame / 1440, (帧/ 24)%60);
fflush(stderr); $ b¥b
SDL_LockYUVOverlay(yuv_overlay);

for(i = 0; i c- height; i ++){
memcpy(luma + i * c-> width,picture-> data [0] i * picture-> linesize [0],c-> width);
}
memcpy(yuv_overlay-> pixels [0],luma,c-> width * c-> height)
for(i = 0; i c- height / 2; i ++){
memcpy(chroma + i * c-> width / 2,picture-> data [2] i * picture-> linesize [2],c-> width / 2);
}
memcpy(yuv_overlay-> pixels [1],chroma,c-> width * c-> height / 4);
for(i = 0; i c- height / 2; i ++){
memcpy(chroma + i * c-> width / 2,picture-> data [1] i * picture-> linesize [1],c-> width / 2);
}
memcpy(yuv_overlay-> pixels [2],chroma,c-> width * c-> height / 4);

SDL_UnlockYUVOverlay(yuv_overlay);
SDL_DisplayYUVOverlay(yuv_overlay,& rect);

while(get_time_diff(t)< inv_fps){
sleep(1000);
}
frame ++;
gettimeofday(& t,0);
}
memcpy(arghwtf,arghwtf + len,80000-len);
fread(arghwtf + 80000 - len,1,len,f);
}

/ *一些编解码器,例如MPEG,以一帧的
延迟传输I和P帧。您必须执行以下操作才能获得
机会获取视频的最后一帧* /
avpkt.data = NULL;
avpkt.size = 0;
len = avcodec_decode_video2(c,picture,& got_picture,& avpkt);
if(got_picture){
printf(saving last frame%3d \\\
,frame);
fflush(stdout);

/ *在这里显示最后一帧,与上面的解码循环相同的代码。 * /

frame ++;
}

fclose(f);

avcodec_close(c);
av_free(c);
av_free(picture);
printf(\\\
);
}

这会产生错误:

  || === H264_decoding,Debug === | 
/ home / yoohoo / codeblocks /实时视频流/ H264_decoding / main.cpp ||在函数'int main(int,char **)':
/ home / yoohoo / codeblocks /实时视频流/ H264_decoding / main.cpp | 107 |错误:从'void *'到'char *'的无效转换[-fpermissive]
/ home / yoohoo / codeblocks /实时视频流/ H264_decoding / main.cpp | 136 |警告:'AVFrame * avcodec_alloc_frame()'已弃用(在/ home / yoohoo / ffmpeg_build / include / libavcodec / avcodec.h:3629)[-Wdeprecated-declarations] |
/ home / yoohoo / codeblocks /实时视频流/ H264_decoding / main.cpp | 136 |警告:'AVFrame * avcodec_alloc_frame()'已弃用(在/ home / yoohoo / ffmpeg_build / include / libavcodec / avcodec.h:3629)[-Wdeprecated-declarations] |
/ home / yoohoo / codeblocks /实时视频流/ H264_decoding / main.cpp | 138 |错误:从'int'到'AVDiscard'的无效转换[-fpermissive] |
/ home / yoohoo / codeblocks /实时视频流/ H264_decoding / main.cpp | 140 |错误:'avcodec_open'没有在此范围内声明|
/ home / yoohoo / codeblocks /实时视频流/ H264_decoding / main.cpp | 163 |错误:无效从'char *'转换为'const uint8_t * {aka const unsigned char *}' ] |
/home/yoohoo/ffmpeg_build/include/libavcodec/avcodec.h|4465|error:初始化'int av_parser_parse2(AVCodecParserContext *,AVCodecContext *,uint8_t **,int *,const uint8_t *,int, int64_t,int64_t,int64_t)'[-fpermissive] |
/ home / yoohoo / codeblocks /实时视频流/ H264_decoding / main.cpp | 181 |错误:从'void *'到'char *'的无效转换[-fpermissive]
/ home / yoohoo / codeblocks /实时视频流/ H264_decoding / main.cpp | 182 |错误:从'void *'到'char *'的无效转换[-fpermissive]
/ home / yoohoo / codeblocks /实时视频流/ H264_decoding / main.cpp | 188 |错误:'av_get_pict_type_char'没有在此范围内声明|
/ home / yoohoo / codeblocks /实时视频流/ H264_decoding / main.cpp | 210 |错误:'sleep'未在此范围内声明|
|| ===构建完成:9错误,2警告=== |

我想知道这是一个不兼容的问题,因为我把这个C程序移动到C + +,需要在C ++中使用它。如何解决这些无效的转换问题?提前感谢!

解决方案

在您的代码的main()函数中

  char * arghwtf = malloc(INBUF_SIZE); 

您已通过使用返回void *的malloc函数分配内存,并且将此void * char *这就是为什么你会得到错误。

 <$ c $ 

c> char * arghwtf =(char *)malloc(INBUF_SIZE);


I am using FFmpeg to decoding H.264 video, for the following program:

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <signal.h>

    #include <sys/time.h>
    #include <time.h>

#include <libavcodec/avcodec.h>
#include <libavutil/mathematics.h>

#include <SDL/SDL.h>

void sigint_handler(int signal) {
    printf("\n");
    exit(0);
}

const char *window_title;
SDL_Surface *screen;
SDL_Overlay *yuv_overlay;

#define INBUF_SIZE 80000

/*
 * Video decoding example
 */

static long get_time_diff(struct timeval time_now) {
   struct timeval time_now2;
   gettimeofday(&time_now2,0);
   return time_now2.tv_sec*1.e6 - time_now.tv_sec*1.e6 + time_now2.tv_usec - time_now.tv_usec;
}

int video_open(AVCodecContext *avctx, const char *filename){
    int flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
    int w,h;

    flags |= SDL_RESIZABLE;

    if (avctx->width){
        w = avctx->width;
        h = avctx->height;
    } else {
        w = 640;
        h = 480;
    }

    if(SDL_Init(SDL_INIT_VIDEO) < 0) {
    fprintf(stderr, "SDL_INIT_VIDEO failed!\n");
    exit(1);
    }

    screen = SDL_SetVideoMode(w, h, 0, flags);

    if (!screen) {
        fprintf(stderr, "SDL: could not set video mode - exiting\n");
        return -1;
    }
    if (!window_title)
        window_title = filename;
    SDL_WM_SetCaption(window_title, window_title);

    yuv_overlay = SDL_CreateYUVOverlay(w, h, SDL_YV12_OVERLAY, screen);

    if (yuv_overlay->hw_overlay) {
    fprintf(stderr, "Using hardware overlay!\n");
    }

    return 0;
}

int main(int argc, char **argv) {
    AVCodec *codec;
    AVCodecContext *c= NULL;
    AVCodecParserContext *parser = NULL;
    int frame, got_picture, len2, len;
    const char *filename;
    FILE *f;
    AVFrame *picture;
    char *arghwtf = malloc(INBUF_SIZE);
    char *luma = NULL;
    char *chroma = NULL;
    int i=0;
    uint64_t in_len;
    int pts, dts;
    struct timeval t;
    float inv_fps = 1e6/23.98;
    AVPacket avpkt;
    SDL_Rect rect;


    /* register all the codecs */
    avcodec_register_all();

    filename = argv[1];

    av_init_packet(&avpkt);

    printf("Decoding file %s...\n", filename);

    /* find the H.264 video decoder */
    codec = avcodec_find_decoder(CODEC_ID_H264);
    if (!codec) {
        fprintf(stderr, "codec not found\n");
        exit(1);
    }

    c = avcodec_alloc_context3(codec);
    picture = avcodec_alloc_frame();

    c->skip_loop_filter = 48; // skiploopfilter=all

    if (avcodec_open(c, codec) < 0) {
        fprintf(stderr, "could not open codec\n");
        exit(1);
    }

    /* the codec gives us the frame size, in samples */
    parser = av_parser_init(c->codec_id);
    parser->flags |= PARSER_FLAG_ONCE;

    f = fopen(filename, "rb");
    if (!f) {
        fprintf(stderr, "could not open %s\n", filename);
        exit(1);
    }

    frame = 0;
    gettimeofday(&t, 0);
    if(fread(arghwtf, 1, INBUF_SIZE, f) == 0) {
    exit(1);
    }
    in_len = 80000;
        while (in_len > 0 && !feof(f)) {
        len = av_parser_parse2(parser, c, &avpkt.data, &avpkt.size, arghwtf, in_len,
                                   pts, dts, AV_NOPTS_VALUE);

            len2 = avcodec_decode_video2(c, picture, &got_picture, &avpkt);
            if (len2 < 0) {
                fprintf(stderr, "Error while decoding frame %d\n", frame);
                exit(1);
            }
            if (got_picture) {
        if(!screen) {
            video_open(c, filename);

            rect.x = 0;
            rect.y = 0;
            rect.w = c->width;
            rect.h = c->height;
            inv_fps = av_q2d(c->time_base);
            fprintf(stderr, "w:%i h:%i\n", rect.w, rect.h);

            luma = malloc(c->width*c->height);
            chroma = malloc(c->width*c->height/4);

            SDL_DisplayYUVOverlay(yuv_overlay, &rect);

            signal(SIGINT, sigint_handler);
        }
                fprintf(stderr, "\rDisplaying %c:frame %3d (%02d:%02d)...", av_get_pict_type_char(picture->pict_type), frame, frame/1440, (frame/24)%60);
                fflush(stderr);

        SDL_LockYUVOverlay(yuv_overlay);

                for(i=0;i<c->height;i++) {
                  memcpy(luma + i * c->width, picture->data[0] + i * picture->linesize[0], c->width);
                }
        memcpy(yuv_overlay->pixels[0], luma, c->width * c->height);
                for(i=0;i<c->height/2;i++) {
                  memcpy(chroma + i * c->width/2, picture->data[2] + i * picture->linesize[2], c->width/2);
                }
        memcpy(yuv_overlay->pixels[1], chroma, c->width * c->height / 4);
                for(i=0;i<c->height/2;i++) {
                  memcpy(chroma + i * c->width/2, picture->data[1] + i * picture->linesize[1], c->width/2);
                }
        memcpy(yuv_overlay->pixels[2], chroma, c->width * c->height / 4);

        SDL_UnlockYUVOverlay(yuv_overlay);
        SDL_DisplayYUVOverlay(yuv_overlay, &rect);

        while(get_time_diff(t) < inv_fps) {
            sleep(1000);
        }
                frame++;
        gettimeofday(&t, 0);
            }
        memcpy(arghwtf, arghwtf + len, 80000-len);
        fread(arghwtf + 80000 - len, 1, len, f);
        }

    /* some codecs, such as MPEG, transmit the I and P frame with a
       latency of one frame. You must do the following to have a
       chance to get the last frame of the video */
    avpkt.data = NULL;
    avpkt.size = 0;
    len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);
    if (got_picture) {
        printf("saving last frame %3d\n", frame);
        fflush(stdout);

    /* Display last frame here, same code as in the decoding loop above. */

        frame++;
    }

    fclose(f);

    avcodec_close(c);
    av_free(c);
    av_free(picture);
    printf("\n");
}

It gives error:

||=== H264_decoding, Debug ===|
/home/yoohoo/codeblocks/Real-time video streaming/H264_decoding/main.cpp||In function ‘int main(int, char**)’:|
/home/yoohoo/codeblocks/Real-time video streaming/H264_decoding/main.cpp|107|error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive]|
/home/yoohoo/codeblocks/Real-time video streaming/H264_decoding/main.cpp|136|warning: ‘AVFrame* avcodec_alloc_frame()’ is deprecated (declared at /home/yoohoo/ffmpeg_build/include/libavcodec/avcodec.h:3629) [-Wdeprecated-declarations]|
/home/yoohoo/codeblocks/Real-time video streaming/H264_decoding/main.cpp|136|warning: ‘AVFrame* avcodec_alloc_frame()’ is deprecated (declared at /home/yoohoo/ffmpeg_build/include/libavcodec/avcodec.h:3629) [-Wdeprecated-declarations]|
/home/yoohoo/codeblocks/Real-time video streaming/H264_decoding/main.cpp|138|error: invalid conversion from ‘int’ to ‘AVDiscard’ [-fpermissive]|
/home/yoohoo/codeblocks/Real-time video streaming/H264_decoding/main.cpp|140|error: ‘avcodec_open’ was not declared in this scope|
/home/yoohoo/codeblocks/Real-time video streaming/H264_decoding/main.cpp|163|error: invalid conversion from ‘char*’ to ‘const uint8_t* {aka const unsigned char*}’ [-fpermissive]|
/home/yoohoo/ffmpeg_build/include/libavcodec/avcodec.h|4465|error:   initializing argument 5 of ‘int av_parser_parse2(AVCodecParserContext*, AVCodecContext*, uint8_t**, int*, const uint8_t*, int, int64_t, int64_t, int64_t)’ [-fpermissive]|
/home/yoohoo/codeblocks/Real-time video streaming/H264_decoding/main.cpp|181|error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive]|
/home/yoohoo/codeblocks/Real-time video streaming/H264_decoding/main.cpp|182|error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive]|
/home/yoohoo/codeblocks/Real-time video streaming/H264_decoding/main.cpp|188|error: ‘av_get_pict_type_char’ was not declared in this scope|
/home/yoohoo/codeblocks/Real-time video streaming/H264_decoding/main.cpp|210|error: ‘sleep’ was not declared in this scope|
||=== Build finished: 9 errors, 2 warnings ===|

I am wondering it is an incompatible problem due to I move this C program to C++, but I finally need to use it in C++. How to solve these invalid conversion problem? Thanks in advance!

解决方案

In main() function of your code

  char *arghwtf = malloc(INBUF_SIZE);

You have allocated memory by using malloc function which returns void * , and you are assigning this void * to char * that's why you are getting errors. You have to typecast it into char *.

Instead try this.

 char *arghwtf =(char *) malloc(INBUF_SIZE);

这篇关于FFmpeg H264解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆