Part of the code H264 through the rtmp release V2.0 (code is not full!) )

Source: Internet
Author: User
Tags win32 htons

said in front.

See a lot of people reprint this article, but certainly compile does not pass, oneself need to add some header file.

In fact, for me, I just need to compile from 264 to FLV (vacant audio part does not need), so this code I did not directly copy, borrowed from the 264 to the FLV part of the function,

If who use or from the Internet to see this code, it is recommended not to copy ...

Demo.cpp: Defines the entry point for a console application. #include "stdafx.h" #include extern "C" {#include. /.. /common/common.h "#include". /.. /common/cpu.h "#include". /.. /x264.h "#include". /..
/encoder/set.h "} #include" Librtmp/rtmp_sys.h "#include" librtmp/log.h "#include" librtmp/amf.h "#include" CameraDS.h "
void convertycbcr2bgr (unsigned char *pyuv,unsigned char *pbgr,int iwidth,int iheight);
void Convertrgb2yuv (int w,int h,unsigned char *bmp,unsigned char *YUV);
    int Initsockets () {#ifdef WIN32 WORD version;
    Wsadata Wsadata;
    Version = Makeword (1, 1);
Return (WSAStartup (version, &wsadata) = = 0);
#else return TRUE; #endif} inline void Cleanupsockets () {#ifdef WIN32 wsacleanup () #endif} #define Hex2bin (A) (((a) &0x40)? (
    (a) &0xf) +9: ((a) &0xf) int Hex2bin (char *str, char **hex) {char *ptr;
    int I, L = strlen (str);
    if (L & 1) return 0;
    *hex = (char *) malloc (L/2);
    ptr = *hex;
    if (!ptr) return 0;
  for (i=0 I      *ptr++ = (Hex2bin (str[i]) << 4) |
    Hex2bin (str[i+1]);
return L/2;
    } char * Put_byte (char *output, uint8_t nval) {output[0] = Nval;
return output+1;
    } char * PUT_BE16 (char *output, uint16_t nval) {output[1] = nval & 0xFF;
    Output[0] = nval >> 8;
return output+2;
    } char * PUT_BE24 (char *output,uint32_t nval) {output[2] = nval & 0xFF;
    OUTPUT[1] = nval >> 8;
    Output[0] = nval >> 16;
return output+3;
    } char * PUT_BE32 (char *output, uint32_t nval) {output[3] = nval & 0xFF;
    OUTPUT[2] = nval >> 8;
    OUTPUT[1] = nval >> 16;
    Output[0] = Nval >> 24;
return output+4;
    } char * PUT_BE64 (char *output, uint64_t nval) {output=put_be32 (output, nval >> 32);
    Output=put_be32 (output, nval);
return output;
    } char * put_amf_string (char *c, const char *str) {uint16_t len = strlen (str);
    C=put_be16 (c, Len);
    memcpy (C,str,len);
return C+len;
}char * put_amf_double (char *c, double d) {*c++ = Amf_number;
        {unsigned char *ci, *co;
        CI = (unsigned char *) &d;
        CO = (unsigned char *) C;
        Co[0] = ci[7];
        CO[1] = ci[6];
        CO[2] = ci[5];
        CO[3] = ci[4];
        CO[4] = ci[3];
        CO[5] = ci[2];
        CO[6] = ci[1];
    CO[7] = ci[0];
return c+8; int main (int argc, char * argv[]) {if (argc<2) {rtmp_logprintf ("Rtmp_url is NULL!!!
        /n ");
    return-1; } if (!
        Initsockets ()) {rtmp_logprintf ("initsockets error!/n");
    return-1;
    } rtmp_logprintf ("initsockets!/n");
    CoInitialize (NULL);
    Ccamerads camera; if (!camera.
        Opencamera (0,320,240,false)) {rtmp_logprintf ("Open Camera error/n");
    return-1; int Nheight=camera.
    GetHeight (); int Nwidth=camera.
    GetWidth ();
    unsigned char * szrgbbuffer=new unsigned char[nheight*nwidth * 3]; rtmp_logprintf ("CamERA Open scuess.
    Picture size[-x%d]/n ", nwidth,nheight);
    Rtmp_debuglevel = Rtmp_loginfo;
    RTMP *r;
    Char uri[]= "Rtmp://127.0.0.1/live/test";
    Char uri[]= "Rtmp://192.199.15.223/live/test";
    Char uri[]= "RTMP://221.9.244.4/LIVE/JLTV";
    Char uri[]= "Rtmp://192.199.15.223/oflademo/red5streamdemo";
    Char uri[]= "Rtmp://192.199.15.151/live/test";
    Char uri[]= "RTMP://127.0.0.1/LIVE/ZZJ";
    R= Rtmp_alloc ();
    Rtmp_init (R);
    Rtmp_setupurl (R, (char*) URI);
    Rtmp_enablewrite (R);
    Rtmp_connect (R, NULL);
    Rtmp_connectstream (r,0);
    unsigned char sznalbuffer[1024*32];
    unsigned char szbodybuffer[1024*32];
    x264_nal_t *p264nal;
    int i264nal;
    x264_param_t * P264PARAM;
    x264_picture_t * P264PIC;    
    x264_t *p264handle;
    P264param = new x264_param_t ();
    P264pic = new x264_picture_t ();
    memset (p264pic,0,sizeof (x264_picture_t));    X264_param_default (P264param); Set default Param P264param->i_threads=2;    P264param->i_width = nwidth;    Set frame width p264param->i_height = nheight;
    Set frame Height P264param->b_cabac = 0;
    P264param->i_bframe = 0;
    p264param->b_interlaced=0;
    P264PARAM-&GT;RC.I_RC_METHOD=X264_RC_ABR;//X264_RC_CQP p264param->i_level_idc=21;
    p264param->rc.i_bitrate=200;
    p264param->i_fps_num=30;
    p264param->i_keyint_max=p264param->i_fps_num*3;
        if ((P264handle = X264_encoder_open (p264param)) = = NULL) {fprintf (stderr, X264_encoder_open failed/n);
    Return-2;
    } bs_t bs={0};
    X264_picture_alloc (P264pic, X264_CSP_YV12, P264param->i_width, p264param->i_height);
    P264pic->i_type = X264_type_auto;
    x264_picture_t pic_out;
    Rtmppacket packet={0};
    memset (&packet,0,sizeof (Rtmppacket));
    Packet.m_nchannel = 0x04;
    Packet.m_headertype = Rtmp_packet_size_large;
    Packet.m_ntimestamp = 0; Packet.m_ninfofield2 = R->m_stream_id;
    Packet.m_hasabstimestamp = 0;
    Packet.m_body = (char *) Szbodybuffer;
    char * sztmp= (char *) Szbodybuffer;
    Packet.m_packettype = Rtmp_packet_type_info;
    Sztmp=put_byte (sztmp, amf_string);
    Sztmp=put_amf_string (sztmp, "@setDataFrame");
    Sztmp=put_byte (sztmp, amf_string);
    Sztmp=put_amf_string (sztmp, "onmetadata");
    Sztmp=put_byte (sztmp, amf_object);
    Sztmp=put_amf_string (sztmp, "author");
    Sztmp=put_byte (sztmp, amf_string);
    Sztmp=put_amf_string (Sztmp, "");
    Sztmp=put_amf_string (sztmp, "copyright");
    Sztmp=put_byte (sztmp, amf_string);
    Sztmp=put_amf_string (Sztmp, "");
    Sztmp=put_amf_string (sztmp, "description");
    Sztmp=put_byte (sztmp, amf_string);
    Sztmp=put_amf_string (Sztmp, "");
    Sztmp=put_amf_string (sztmp, "keywords");
    Sztmp=put_byte (sztmp, amf_string);
    Sztmp=put_amf_string (Sztmp, "");
    Sztmp=put_amf_string (sztmp, "rating");
    Sztmp=put_byte (sztmp, amf_string); Sztmp=put_amf_strING (sztmp, "");
    Sztmp=put_amf_string (sztmp, "presetname");
    Sztmp=put_byte (sztmp, amf_string);
    Sztmp=put_amf_string (sztmp, "Custom");
    Sztmp=put_amf_string (sztmp, "width");
    Sztmp=put_amf_double (sztmp, p264param->i_width);
    Sztmp=put_amf_string (sztmp, "width");
    Sztmp=put_amf_double (sztmp, p264param->i_width);
    Sztmp=put_amf_string (sztmp, "height");
    Sztmp=put_amf_double (sztmp, p264param->i_height);
    Sztmp=put_amf_string (sztmp, "framerate");
    Sztmp=put_amf_double (Sztmp, (double) p264param->i_fps_num/p264param->i_fps_den);
    Sztmp=put_amf_string (sztmp, "videocodecid");
    Sztmp=put_byte (sztmp, amf_string);
    Sztmp=put_amf_string (sztmp, "avc1");
    Sztmp=put_amf_string (sztmp, "videodatarate");
    Sztmp=put_amf_double (sztmp, p264param->rc.i_bitrate);
    Sztmp=put_amf_string (sztmp, "avclevel");
    Sztmp=put_amf_double (sztmp, P264PARAM-&GT;I_LEVEL_IDC); Sztmp=put_amf_string (sztmp, "AvcproFile ");
    Sztmp=put_amf_double (sztmp, 0x42);
    Sztmp=put_amf_string (sztmp, "videokeyframe_frequency");
    Sztmp=put_amf_double (Sztmp, 3);
    Sztmp=put_amf_string (Sztmp, "");
    Sztmp=put_byte (sztmp, amf_object_end);
    packet.m_nbodysize=sztmp-(char *) Szbodybuffer;
    Rtmp_sendpacket (r,&packet,1);    
    Packet.m_packettype = Rtmp_packet_type_video;
    szbodybuffer[0]=0x17;
    szbodybuffer[1]=0x00;
    szbodybuffer[2]=0x00;
    szbodybuffer[3]=0x00;
    szbodybuffer[4]=0x00;
    szbodybuffer[5]=0x01;
    szbodybuffer[6]=0x42;
    szbodybuffer[7]=0xc0;
    szbodybuffer[8]=0x15;
    szbodybuffer[9]=0x03;
    szbodybuffer[10]=0x01;
    sztmp= (char *) szbodybuffer+11;
    Short slen=0; Bs_init (&bs,sznalbuffer,16);//Initial session BS X264_sps_write (&bs, P264handle->sps);//Read the encoder's SPS Slen=bs.p-bs.p_st
    Art+1;//spslen (short) slen=htons (Slen);
    memcpy (sztmp,&slen,sizeof (short));
    Sztmp+=sizeof (short);
    *sztmp=0x67;
    Sztmp+=1;memcpy (Sztmp,bs.p_start,bs.p-bs.p_start);
    Sztmp+=bs.p-bs.p_start;
    *sztmp=0x01;
    Sztmp+=1; Bs_init (&bs,sznalbuffer,16);//Initial session BS X264_pps_write (&bs, P264handle->pps);//Read encoder PPS Slen=bs.p-bs.p_st
    Art+1;//spslen (short) slen=htons (Slen);
    memcpy (sztmp,&slen,sizeof (short));
    Sztmp+=sizeof (short);
    *sztmp=0x68;
    Sztmp+=1;
    memcpy (Sztmp,bs.p_start,bs.p-bs.p_start);
    Sztmp+=bs.p-bs.p_start;
    packet.m_nbodysize=sztmp-(char *) Szbodybuffer;
    Rtmp_sendpacket (r,&packet,0);
    unsigned int ntimes=0;
    unsigned int oldtick=gettickcount ();
    unsigned int newtick=0;
    
    Packet.m_ntimestamp=0;
        while (true) {szbodybuffer[0]=0x17;
        szbodybuffer[1]=0x01;
        szbodybuffer[2]=0x00;
        szbodybuffer[3]=0x00;
        szbodybuffer[4]=0x42;
        unsigned char * SZTMP=SZBODYBUFFER+5;
        unsigned char * pnal=sznalbuffer;
        ntimes++;
        int nframsinpack=0;
       while (true) {nframsinpack++; unsigned char * pcamerabuf = camera.
            Queryframe ();
            if (!pcamerabuf) {return-1; for (int ii=0;ii {memcpy szrgbbuffer+ (nwidth*3) * (nheight-ii-1), pcamerabuf+ (Nwidt
                H*3) *ii,nwidth*3);
                memcpy (pcamerabuf+nwidth* (nheight-ii-1), pcamerabuf+nwidth*ii,nwidth*3);
            memcpy (szlinebuffer,pcamerabuf+nwidth* (nheight-ii-1), nwidth*3);
            } CONVERTRGB2YUV (Nwidth,nheight,szrgbbuffer,p264pic->img.plane[0]);
            memcpy (P264pic->img.plane[0],sznalbuffer,nwidth*nheight);
            memcpy (P264PIC-&GT;IMG.PLANE[1],SZNALBUFFER+NWIDTH*NHEIGHT,NWIDTH*NHEIGHT/4);
            
            memcpy (P264PIC-&GT;IMG.PLANE[2],SZNALBUFFER+NWIDTH*NHEIGHT*5/4,NWIDTH*NHEIGHT/4);
                if (X264_encoder_encode (P264handle, &p264nal, &i264nal, P264pic, &pic_out) < 0) { Fprintf (stderr, "X264_encoder_encode failed/n");
                for (int i = 0; i < i264nal i++) {int i_size;
                int i_data;
                I_data = 1024*32; if ((I_size = X264_nal_encode (pnal, &i_data, 1, &p264nal[i])) > 0) {i
                    F ((pnal[4]&0x60) ==0) {continue;
                    } if (pnal[4]==0x67) {continue;
                    } if (pnal[4]==0x68) {continue;
                    } memmove (pnal,pnal+4,i_size-4);
                pnal+=i_size-4; else if (I_size < 0) {fprintf (stderr, "need to increase buffer
                Size (size=%d)/n ",-i_size); }} unsigned int nsize=pnal-sznalbuffer;
            packet.m_nbodysize=nsize+9;
            if (i264nal>1) {szbodybuffer[0]=0x17;
            else {szbodybuffer[0]=0x27;
            } put_be32 ((char *) szbodybuffer+5,nsize);
            memcpy (Szbodybuffer+9,sznalbuffer,pnal-sznalbuffer);
            Rtmp_sendpacket (r,&packet,0);
            Sleep (20);
            Newtick=gettickcount (); Rtmp_logstatus ("/rinfo naul type:0xx Size:] tick:d D", Sznalbuffer[0], Nsize,33-nsleep,gettickcount ()-oldTick+
            Nsleep);
            Packet.m_headertype = Rtmp_packet_size_medium;
            Packet.m_ntimestamp+=newtick-oldtick;
            Oldtick=newtick;
        Break
} return 0; }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.