1 /*
2  * H.264 Plugin codec for OpenH323/OPAL
3  *
4  * Copyright (C) 2007 Matthias Schneider, All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  */
21 
22 #ifndef __ENC_CTX_H__
23 #define __ENC_CTX_H__ 1
24 
25 #include <stdarg.h>
26 
27 #if defined(_WIN32)
28 #include "../../common/critsect.h"
29 #if _MSC_VER < 1600
30 #include "../../common/vs-stdint.h"
31 #endif
32 #else
33 #include <stdint.h>
34 #include "../common/critsect.h"
35 #endif
36 
37 #include "../shared/h264frame.h"
38 
39 extern "C" {
40   #include <x264.h>
41 };
42 
43 #if X264_BUILD < 80
44 #if _WIN32
45 #pragma message("X264 Build > 79 required for single NAL support")
46 #else
47 #warning("X264 Build > 79 required for single NAL support")
48 #endif
49 #endif
50 
51 #if defined(_WIN32) || defined(_STATIC_LINK)
52 /* to keep compatibility with old build */
53 #define X264_LINK_STATIC 1
54 #define X264_DELAYLOAD   1
55 #endif
56 
57 #define CIF_WIDTH 352
58 #define CIF_HEIGHT 288
59 #define QCIF_WIDTH 176
60 #define QCIF_HEIGHT 144
61 #define IT_QCIF 0
62 #define IT_CIF 1
63 
64 #define H264_BITRATE         768000
65 #define H264_PAYLOAD_SIZE      1400
66 #define H264_SINGLE_NAL_SIZE   1400
67 #define H264_FRAME_RATE          30
68 #define H264_KEY_FRAME_INTERVAL  (30*60) // Send a key frame no more than once every minute (unless requested through fast update)
69 #define H264_PROFILE_LEVEL       ((66 << 16) + (0xC0 << 8) +  30)
70 #define H264_TSTO                31
71 #define H264_MIN_QUANT           10
72 
73 #if X264_LINK_STATIC
74   #define X264_ENCODER_OPEN x264_encoder_open
75   #define X264_PARAM_DEFAULT x264_param_default
76   #define X264_ENCODER_ENCODE x264_encoder_encode
77   #define X264_NAL_ENCODE x264_nal_encode
78   #define X264_ENCODER_RECONFIG x264_encoder_reconfig
79   #define X264_ENCODER_HEADERS x264_encoder_headers
80   #define X264_ENCODER_CLOSE x264_encoder_close
81   #if X264_BUILD >= 98
82   #define X264_PICTURE_INIT x264_picture_init
83   #endif
84 #else
85 #if defined(_WIN32)
86   #include "x264loader_win32.h"
87 #else
88   #include "x264loader_unix.h"
89 #endif
90   #define X264_ENCODER_OPEN  X264Lib.Xx264_encoder_open
91   #define X264_PARAM_DEFAULT X264Lib.Xx264_param_default
92   #define X264_ENCODER_ENCODE X264Lib.Xx264_encoder_encode
93   #define X264_NAL_ENCODE X264Lib.Xx264_nal_encode
94   #define X264_ENCODER_RECONFIG X264Lib.Xx264_encoder_reconfig
95   #define X264_ENCODER_HEADERS X264Lib.Xx264_encoder_headers
96   #define X264_ENCODER_CLOSE X264Lib.Xx264_encoder_close
97   #if X264_BUILD >= 98
98   #define X264_PICTURE_INIT X264Lib.Xx264_picture_init
99   #endif
100 #endif
101 
102 class X264EncoderContext
103 {
104   public:
105     X264EncoderContext ();
106     ~X264EncoderContext ();
107 
108     bool Initialise();
109     void Uninitialise();
110 
111     int EncodeFrames (const unsigned char * src, unsigned & srcLen, unsigned char * dst, unsigned & dstLen, unsigned int & flags);
112 
113     void fastUpdateRequested(void);
114 
115     void SetMaxRTPFrameSize (unsigned size);
116     void SetMaxKeyFramePeriod (unsigned period);
117     void SetTargetBitrate (unsigned rate);
118     void SetFrameWidth (unsigned width);
119     void SetFrameHeight (unsigned height);
120     void SetFrameRate (unsigned rate);
121     void SetTSTO (unsigned tsto);
122     void SetProfileLevel (unsigned profileLevel);
123     void SetMaxNALSize (unsigned size);
124     void ApplyOptions ();
125 
126 
127   protected:
128 
129     x264_t* _codec;
130     x264_param_t _context;
131     x264_picture_t _inputFrame;
132     H264Frame* _txH264Frame;
133 
134     uint32_t _PFramesSinceLastIFrame; // counts frames since last keyframe
135     uint32_t _IFrameInterval; // confd frames between keyframes
136     int _frameCounter;
137     bool _fastUpdateRequested;
138 
139 } ;
140 
141 
142 #endif /* __ENC_CTX_H__ */
143