FFMPEG 視頻解碼流程

來源:互聯網
上載者:User

FFMPEG解碼流程注釋

static void video_decode_example(const char *outfilename, const char *filename){                                                                //1.定義資料結構    AVCodec *codec;    AVCodecContext *c= NULL;    int frame_count;    FILE *f;    AVFrame *frame;    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];    //用於存放待解碼資料    AVPacket avpkt;    av_init_packet(&avpkt);                                      //2.初始化包結構體    /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */    memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);    printf("Decode video file %s to %s\n", filename, outfilename);    /* find the mpeg1 video decoder */    codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);       //3.尋找解碼器    if (!codec) {        fprintf(stderr, "Codec not found\n");        exit(1);    }    c = avcodec_alloc_context3(codec);                          //4.分配解碼器結構體    if (!c) {        fprintf(stderr, "Could not allocate video codec context\n");        exit(1);    }    if(codec->capabilities&CODEC_CAP_TRUNCATED)                 //在有的codec中,必須設定width和 height,因為流資料中不包含這些資訊        c->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */    /* For some codecs, such as msmpeg4 and mpeg4, width and height       MUST be initialized there because this information is not       available in the bitstream. */    /* open it */    if (avcodec_open2(c, codec, NULL) < 0) {                    //5.開啟解碼器        fprintf(stderr, "Could not open codec\n");        exit(1);    }    f = fopen(filename, "rb");                                  //6.開啟檔案    if (!f) {        fprintf(stderr, "Could not open %s\n", filename);        exit(1);    }    frame = avcodec_alloc_frame();                              //7.分配幀結構體    if (!frame) {        fprintf(stderr, "Could not allocate video frame\n");        exit(1);    }    frame_count = 0;    for(;;) {        avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);            //8.讀取待解碼資料到inbuf        if (avpkt.size == 0)            break;                                                                //對於某些基於流的codec,由於事先不知道檔案大小,只能使用上面的方法擷取資料        /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)           and this is the only method to use them because you cannot           know the compressed data size before analysing it.                                                                //對於某些基於固定(大小?)幀的codec,則必須完整讀完一幀後才能使用它,切必須出示話width 和 height           BUT some other codecs (msmpeg4, mpeg4) are inherently frame           based, so you must call them with all the data for one           frame exactly. You must also initialize 'width' and           'height' before initializing them. */                                                                //對於有些codec,可以在任何時候改變參數包括幀大小 和採樣頻率額        /* NOTE2: some codecs allow the raw parameters (frame size,           sample rate) to be changed at any frame. We handle this, so           you should also take care of it */        /* here, we use a stream based decoder (mpeg1video), so we           feed decoder and see if it could decode a frame */   //本例使用的是基於流的codec        avpkt.data = inbuf;        while (avpkt.size > 0)                                  //9.按幀解碼並儲存資料到outfile                     if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 0) < 0)                     exit(1);    }    /* 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;                                             //10.在有的編碼器中,需要如下操作以擷取最後一幀    decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 1);    fclose(f);                                                  //11.close檔案 轉碼器,釋放結構體和資料空間    avcodec_close(c);    av_free(c);    avcodec_free_frame(&frame);    printf("\n");}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.