Skip to content

Commit 69a819c

Browse files
committed
ffmpeg mode with not align resolution workaround
1 parent 1529ece commit 69a819c

File tree

1 file changed

+47
-22
lines changed

1 file changed

+47
-22
lines changed

avc2ts.cpp

+47-22
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ extern "C" {
4848
#include "webcam.h"
4949
#include "grabdisplay.h"
5050
#include "vncclient.h"
51+
#include "ffmpegsrc.h"
5152

5253
//#include <linux/videodev2.h>
5354

@@ -2114,7 +2115,7 @@ class TSEncaspulator
21142115
int64_t *pcr_list = NULL;
21152116
uint8_t *out = NULL;
21162117
size_t fn = 0;
2117-
#define MAX_SIZE_PICTURE 256000
2118+
#define MAX_SIZE_PICTURE 512000
21182119
uint8_t *InternalBuffer;//[MAX_SIZE_PICTURE];
21192120
uint8_t PictureHeader[5000];
21202121
int PictureHeaderSize=0;
@@ -2317,7 +2318,7 @@ class TSEncaspulator
23172318
//#define FILLER_OVERHEAD (NALU_OVERHEAD+1)
23182319
// ----------------------- END FROM X264 ----------------------
23192320

2320-
2321+
if(TimeToTransmitFrameUs<5) TimeToTransmitFrameUs=5; // Mini 1ms for 1 frame
23212322
if(previous_cpb_final_arrival_time==0) previous_cpb_final_arrival_time=((key_frame-1)*FrameDuration)*90LL*300LL;
23222323

23232324
tsframe.cpb_initial_arrival_time=previous_cpb_final_arrival_time;
@@ -2681,8 +2682,8 @@ class CameraTots
26812682
OMX_VIDEO_AVCProfileConstrainedBaseline
26822683
*/
26832684

2684-
encoder.setProfileLevel(OMX_VIDEO_AVCProfileBaseline);//OMX_VIDEO_AVCProfileHigh);
2685-
//encoder.setLevelExtension(1500);
2685+
encoder.setProfileLevel(OMX_VIDEO_AVCProfileHigh);//OMX_VIDEO_AVCProfileHigh);
2686+
//encoder.setLevelExtension(VideoBitrate/1000);
26862687
// With Main Profile : have more skipped frame
26872688
tsencoder.SetOutput(FileName,Udp);
26882689
tsencoder.ConstructTsTree(VideoBitrate,TsBitrate,PMTPid,sdt,fps,1);
@@ -2691,7 +2692,7 @@ class CameraTots
26912692
// encoder.setPeakRate(VideoBitrate*1.1);
26922693
// encoder.setDQP(10); // Normally to 2
26932694
encoder.setQPLimits(1,51); // To have high bitrate even at low fps and size : for Now a MUST
2694-
encoder.setAdvanceddAVC();
2695+
//encoder.setAdvanceddAVC();
26952696
//encoder.setMaxFrameLimits(TsBitrate*1.5/fps);
26962697

26972698
}
@@ -3014,6 +3015,7 @@ class PictureTots
30143015
Webcam *pwebcam;
30153016
GrabDisplay *pgrabdisplay;
30163017
VncClient *pvncclient;
3018+
ffmpegsrc *pffmpeg;
30173019
struct timespec InitTime;
30183020
FILE *AudioIn=NULL;
30193021

@@ -3023,6 +3025,7 @@ static const int Mode_PATTERN=0;
30233025
static const int Mode_V4L2=1;
30243026
static const int Mode_GRABDISPLAY=2;
30253027
static const int Mode_VNCCLIENT=3;
3028+
static const int Mode_FFMPEG=4;
30263029
public:
30273030
void Init(VideoFromat &VideoFormat,char *FileName,char *Udp,int VideoBitrate,int TsBitrate,int SetDelayPts,int PMTPid,char* sdt,int fps=25,int IDRPeriod=100,int RowBySlice=0,int EnableMotionVectors=0,int ModeInput=Mode_PATTERN,char *Extra=NULL)
30283031
{
@@ -3067,25 +3070,26 @@ static const int Mode_VNCCLIENT=3;
30673070
printf("Resizer input = %d x %d\n",DisplayWidth,DisplayHeight);
30683071
resizer.setupOutputPort(DisplayWidth,DisplayHeight,VideoFormat, OMX_COLOR_Format32bitABGR8888);
30693072
}
3073+
if(Mode==Mode_FFMPEG)
3074+
{
3075+
3076+
pffmpeg=new ffmpegsrc(Extra);
3077+
int CamWidth,CamHeight;
3078+
pffmpeg->GetVideoSize(CamWidth,CamHeight);
3079+
resizer.setupOutputPort(((CamWidth+31)>>5)<<5,((CamHeight+15)>>4)<<4,VideoFormat,OMX_COLOR_FormatYUV420PackedPlanar);
3080+
}
3081+
30703082
//resizer.setupOutputPort(VideoFormat,OMX_COLOR_Format32bitABGR8888);//OK
30713083

30723084
// configuring encoders
30733085

30743086
//VideoFromat vfResized = VideoFormat;
30753087

3076-
if(VideoBitrate<150000)
3077-
{
3078-
printf("Using QP constant\n");
3079-
encoder.setupOutputPort(VideoFormat,VideoBitrate*2,fps);
3080-
encoder.setQFromBitrate(VideoBitrate,fps,CurrentVideoFormat.width,CurrentVideoFormat.height);
3081-
3082-
}
3083-
else
3084-
{
3088+
30853089
encoder.setupOutputPort(VideoFormat,VideoBitrate,fps);
30863090
encoder.setBitrate(VideoBitrate,OMX_Video_ControlRateVariable/*OMX_Video_ControlRateConstant*/);
30873091

3088-
}
3092+
30893093

30903094
//OMX_Video_ControlRateDisable seems not supported !!!
30913095
encoder.setCodec(OMX_VIDEO_CodingAVC);
@@ -3098,10 +3102,11 @@ static const int Mode_VNCCLIENT=3;
30983102
//encoder.setQP(20,20);
30993103
encoder.setLowLatency();
31003104
//encoder.setSeparateNAL();
3105+
encoder.setMinizeFragmentation(); // Minimize frag seems to block at high resolution : to inspect*/
3106+
31013107
if(RowBySlice)
3102-
encoder.setMultiSlice(RowBySlice);
3103-
else
3104-
encoder.setMinizeFragmentation();
3108+
encoder.setMultiSlice(RowBySlice,OMX_VIDEO_IntraRefreshCyclic/*OMX_VIDEO_IntraRefreshBoth*/);
3109+
31053110
//encoder.setEED();
31063111

31073112
/*OMX_VIDEO_AVCProfileBaseline = 0x01, //< Baseline profile
@@ -3110,7 +3115,7 @@ static const int Mode_VNCCLIENT=3;
31103115
OMX_VIDEO_AVCProfileHigh = 0x08, //< High profile
31113116
OMX_VIDEO_AVCProfileConstrainedBaseline
31123117
*/
3113-
encoder.setProfileLevel(OMX_VIDEO_AVCProfileMain);
3118+
encoder.setProfileLevel(OMX_VIDEO_AVCProfileHigh);
31143119
encoder.setQPLimits(1,51); // To have high bitrate even at low fps and size : for Now a MUST
31153120
// With Main Profile : have more skipped frame
31163121
tsencoder.SetOutput(FileName,Udp);
@@ -3154,7 +3159,10 @@ static const int Mode_VNCCLIENT=3;
31543159
{
31553160
pvncclient->SetOmxBuffer((unsigned char*)resizer.inBuffer().data());
31563161
}
3157-
3162+
if(Mode==Mode_FFMPEG)
3163+
{
3164+
pffmpeg->SetOmxBuffer((unsigned char*)resizer.inBuffer().data());
3165+
}
31583166
printf("Allocsize= %d\n",resizer.inBuffer().allocSize());
31593167
encoder.allocBuffers(false);//Only Bufout
31603168

@@ -3489,6 +3497,16 @@ else
34893497
//printf("DiffFrame %d\n",FrameDiff);
34903498
}
34913499
}
3500+
if(Mode==Mode_FFMPEG)
3501+
{
3502+
3503+
if(!pffmpeg->read_frame(2)){want_quit=1;return;};
3504+
3505+
filledLen=PictureBuffer.allocSize();
3506+
usleep_exactly(1e6/Videofps);
3507+
//V4L2_read_frame(PictureBuffer.data(),&filledLen);
3508+
//usleep_exactly(1e6/(Videofps*2));
3509+
}
34923510
PictureBuffer.setDatasize(filledLen);
34933511
PictureBuffer.setFilled(false);
34943512
resizer.callEmptyThisBuffer();
@@ -3522,6 +3540,10 @@ else
35223540
if(Mode==Mode_VNCCLIENT)
35233541
{
35243542
free(pvncclient);
3543+
}
3544+
if(Mode==Mode_FFMPEG)
3545+
{
3546+
free(pffmpeg);
35253547
}
35263548
// return the last full buffer back to the encoder component
35273549
{
@@ -3625,7 +3647,8 @@ int main(int argc, char **argv)
36253647
#define PATTERN 1
36263648
#define USB_CAMERA 2
36273649
#define DISPLAY 3
3628-
#define VNC 4
3650+
#define VNC 4
3651+
#define FFMPEG 5
36293652
int TypeInput=CAMERA;
36303653

36313654
while(1)
@@ -3769,7 +3792,9 @@ else
37693792
if(ExtraArg==NULL) ExtraArg="/dev/video0";break;
37703793
case DISPLAY:PictureMode=PictureTots::Mode_GRABDISPLAY;break;
37713794
case VNC:PictureMode=PictureTots::Mode_VNCCLIENT;
3772-
if(ExtraArg==NULL) {printf("IP of VNCServer should be set with -e option\n");exit(0);}
3795+
if(ExtraArg==NULL) {printf("IP of VNCServer should be set with -e option\n");exit(0);}break;
3796+
case FFMPEG:PictureMode=PictureTots::Mode_FFMPEG;
3797+
37733798

37743799
}
37753800
picturetots = new PictureTots;

0 commit comments

Comments
 (0)