houxiao
2016-12-28 4ef430e946e717d72e923c4708a9120f94d55dbd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
//  FFmpegH264Encoder.h
//  FFmpegRTSPServer
//
//  Created by Mina Saad on 9/22/15.
//  Copyright (c) 2015 Mina Saad. All rights reserved.
//
 
#ifndef MESAI_FFMPEGH264_ENCODER_H
#define MESAI_FFMPEGH264_ENCODER_H
 
#include <string>
#include <queue>
#include <pthread.h>
#include <functional>
 
#include "IEncoder.h"
 
extern "C" {
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <libavutil/opt.h>
    #include <libavutil/mathematics.h>
    #include <libavformat/avformat.h>
    #include <libswscale/swscale.h>
    #include <libswresample/swresample.h>
    #include <libavutil/imgutils.h>
    #include <libavcodec/avcodec.h>
 
}
 
namespace MESAI
{
    class FrameStructure {
        public:
            uint8_t * dataPointer;
            int dataSize;
            int frameID;
            ~FrameStructure()
            {
                delete dataPointer;
            }
    };
 
    class FFmpegH264Encoder : public IEncoder
    {
    public:
        FFmpegH264Encoder();
        virtual ~FFmpegH264Encoder();
        
        virtual void setCallbackFunctionFrameIsReady(std::function<void()> func);
        
        bool SetupVideo(std::string filename, int Width, int Height, int FPS, int GOB, int BitPerSecond);
        void CloseVideo();
        bool SetupCodec(const char *filename, int codec_id);
        void CloseCodec();
        
 
        void SendNewFrame(uint8_t * RGBFrame);        
        bool WriteFrameRGB(uint8_t * RGBFrame);
        bool WriteFrameYUV420(AVFrame * YUVFrame);
        virtual char ReleaseFrame();
 
        void run();    
        virtual char GetFrame(u_int8_t** FrameBuffer, unsigned int *FrameSize);
 
    private:
 
 
        std::queue<uint8_t*> inqueue;
        pthread_mutex_t inqueue_mutex;
        std::queue<FrameStructure *> outqueue;
        pthread_mutex_t outqueue_mutex;
 
 
        int m_sws_flags;
        int    m_AVIMOV_FPS;
        int    m_AVIMOV_GOB;
        int    m_AVIMOV_BPS;
        int m_frame_count;
        int    m_AVIMOV_WIDTH;
        int    m_AVIMOV_HEIGHT;
        std::string m_filename;
 
        double m_video_time;
 
        AVCodecContext *m_c;
        AVStream *m_video_st;
        AVOutputFormat *m_fmt;
        AVFormatContext *m_oc;
        AVCodec *m_video_codec;
        AVFrame * m_src_picture, * m_dst_picture;
        SwsContext *sws_ctx;
        int bufferSize;
 
        std::function<void()> onFrame;
 
    };
}
#endif