ffmpeg編碼后的.264數(shù)據(jù)長(zhǎng)啥樣

ffmpeg中利用avcodec_encode_video2函數(shù)將YUV數(shù)據(jù)編碼成.264數(shù)據(jù):

/**
 * Encode a frame of video.
 *
 * Takes input raw video data from frame and writes the next output packet, if
 * available, to avpkt. The output packet does not necessarily contain data for
 * the most recent frame, as encoders can delay and reorder input frames
 * internally as needed.
 *
 * @param avctx     codec context
 * @param avpkt     output AVPacket.
 *                  The user can supply an output buffer by setting
 *                  avpkt->data and avpkt->size prior to calling the
 *                  function, but if the size of the user-provided data is not
 *                  large enough, encoding will fail. All other AVPacket fields
 *                  will be reset by the encoder using av_init_packet(). If
 *                  avpkt->data is NULL, the encoder will allocate it.
 *                  The encoder will set avpkt->size to the size of the
 *                  output packet. The returned data (if any) belongs to the
 *                  caller, he is responsible for freeing it.
 *
 *                  If this function fails or produces no output, avpkt will be
 *                  freed using av_free_packet() (i.e. avpkt->destruct will be
 *                  called to free the user supplied buffer).
 * @param[in] frame AVFrame containing the raw video data to be encoded.
 *                  May be NULL when flushing an encoder that has the
 *                  CODEC_CAP_DELAY capability set.
 * @param[out] got_packet_ptr This field is set to 1 by libavcodec if the
 *                            output packet is non-empty, and to 0 if it is
 *                            empty. If the function returns an error, the
 *                            packet can be assumed to be invalid, and the
 *                            value of got_packet_ptr is undefined and should
 *                            not be used.
 * @return          0 on success, negative error code on failure
 */
int avcodec_encode_video2(AVCodecContext *avctx, 
                          AVPacket *avpkt,
                          const AVFrame *frame, 
                          int *got_packet_ptr);
  • *AVCodecContext avctx
    用來(lái)保存編碼參數(shù)等改览。
    參數(shù)初始化舉例:
    pCodecCtx->bit_rate       = 1024*1024;
    pCodecCtx->width         = m_nScreenWidth;
    pCodecCtx->height        = m_nScreenHeight;
    pCodecCtx->time_base.num = 1;
    pCodecCtx->time_base.den = 25;
    pCodecCtx->gop_size      = 25;
    pCodecCtx->max_b_frames  = 1;
    pCodecCtx->pix_fmt       = AV_PIX_FMT_YUV420P;
  • *AVPacket avpkt
    輸出的編碼數(shù)據(jù)缤言。

  • *const AVFrame frame
    該參數(shù)中包括輸入的YUV數(shù)據(jù)等。

  • *int got_packet_ptr
    這個(gè)參數(shù)用于表明是否生成了編碼數(shù)據(jù)庆揩。
    需要說(shuō)明的是: 不是輸入一個(gè)YUV數(shù)據(jù)跌穗,就會(huì)輸出一個(gè)編碼數(shù)據(jù)。
    編碼器內(nèi)部需要進(jìn)行幀內(nèi)預(yù)測(cè)锈拨、幀間預(yù)測(cè)等各種算法之后,才會(huì)輸出一個(gè)編碼數(shù)據(jù)奕枢。

調(diào)用實(shí)例:

    AVCodec        *pCodec    = NULL;
    AVCodecContext *pCodecCtx = NULL;
    AVFrame *pFrame = NULL;
    AVPacket pkt;

    AVCodecID codec_id = AV_CODEC_ID_H264;

    avcodec_register_all();

    pCodec = avcodec_find_encoder(codec_id);
    if (!pCodec) 
    {
        printf("Codec not found\n");
        return -1;
    }

    pCodecCtx = avcodec_alloc_context3(pCodec);
    if (!pCodecCtx) 
    {
        printf("Could not allocate video codec context\n");
        return -1;
    }

...
        pFrame->data[0] = m_pYUVBuffer;               // Y
        pFrame->data[1] = m_pYUVBuffer + m_nUOffset;  // U 
        pFrame->data[2] = m_pYUVBuffer + m_nVOffset;  // V

        av_init_packet(&pkt);
        pkt.data = NULL;    // packet data will be allocated by the encoder

        pFrame->pts= ...;

        /* encode the image */
        nRet = avcodec_encode_video2(pCodecCtx, &pkt, pFrame, &got_packet);
        if (nRet < 0) 
        {
            printf("Error encoding frame\n");
            return -1;
        }

        if (got_packet)  // 獲取到編碼數(shù)據(jù)
        {
            {
                printf("Succeed to encode frame: %5d\tsize:%5d\n", nVideoFrameCnt, pkt.size);

                av_free_packet(&pkt);
            }  
        }

        Sleep(40);
    }

調(diào)用avcodec_encode_video2輸出的編碼序列到底長(zhǎng)啥樣呢?

H.264編碼序列 (ES碼流)

(以上碼流分析工具為:H264VideoESViewer)
可以看出序列的順序:
SPS -> PPS -> SEI -> I幀 -> ...個(gè)P幀->SPS -> PPS -> SEI -> I幀 -> ...
另外需要注意的是:
SEI和I幀的start code是 0x00 0x00 0x01, 而其他的都是0x00 0x00 0x00 0x01缝彬。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末跌造,一起剝皮案震驚了整個(gè)濱河市族购,隨后出現(xiàn)的幾起案子寝杖,更是在濱河造成了極大的恐慌互纯,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,123評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件只盹,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡站削,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,031評(píng)論 2 384
  • 文/潘曉璐 我一進(jìn)店門(mén)许起,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)园细,“玉大人接校,你說(shuō)我怎么就攤上這事≈朊悖” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,723評(píng)論 0 345
  • 文/不壞的土叔 我叫張陵烈和,是天一觀的道長(zhǎng)皿淋。 經(jīng)常有香客問(wèn)我,道長(zhǎng)疯暑,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,357評(píng)論 1 283
  • 正文 為了忘掉前任妇拯,我火速辦了婚禮越锈,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘甘凭。我一直安慰自己火邓,他們只是感情好德撬,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,412評(píng)論 5 384
  • 文/花漫 我一把揭開(kāi)白布蜓洪。 她就那樣靜靜地躺著,像睡著了一般坯苹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上刚操,一...
    開(kāi)封第一講書(shū)人閱讀 49,760評(píng)論 1 289
  • 那天再芋,我揣著相機(jī)與錄音,去河邊找鬼鉴逞。 笑死,一個(gè)胖子當(dāng)著我的面吹牛构捡,可吹牛的內(nèi)容都是我干的壳猜。 我是一名探鬼主播,決...
    沈念sama閱讀 38,904評(píng)論 3 405
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼喘帚,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼咒钟!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起朱嘴,我...
    開(kāi)封第一講書(shū)人閱讀 37,672評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎乌昔,沒(méi)想到半個(gè)月后帚湘,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,118評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡捅厂,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,456評(píng)論 2 325
  • 正文 我和宋清朗相戀三年资柔,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片辙芍。...
    茶點(diǎn)故事閱讀 38,599評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡羹与,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出纵搁,到底是詐尸還是另有隱情,我是刑警寧澤徘层,帶...
    沈念sama閱讀 34,264評(píng)論 4 328
  • 正文 年R本政府宣布利职,位于F島的核電站,受9級(jí)特大地震影響猪贪,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜热押,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,857評(píng)論 3 312
  • 文/蒙蒙 一楞黄、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鬼廓,春花似錦、人聲如沸碎税。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,731評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)匪煌。三九已至党巾,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間齿拂,已是汗流浹背肴敛。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,956評(píng)論 1 264
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留砸狞,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,286評(píng)論 2 360
  • 正文 我出身青樓刀森,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親撒强。 傳聞我的和親對(duì)象是個(gè)殘疾皇子笙什,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,465評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容