]> git.sesse.net Git - nageru/blob - h264encode.h
Unify muxing between the local file and networking.
[nageru] / h264encode.h
1 // Hardware H.264 encoding via VAAPI. Heavily modified based on example
2 // code by Intel. Intel's original copyright and license is reproduced below:
3 //
4 // Copyright (c) 2007-2013 Intel Corporation. All Rights Reserved.
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sub license, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
13 // 
14 // The above copyright notice and this permission notice (including the
15 // next paragraph) shall be included in all copies or substantial portions
16 // of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 // IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
22 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 #ifndef _H264ENCODE_H
27 #define _H264ENCODE_H
28
29 extern "C" {
30 #include <libavformat/avformat.h>
31 }
32 #include <epoxy/gl.h>
33 #include <atomic>
34 #include <condition_variable>
35 #include <map>
36 #include <memory>
37 #include <mutex>
38 #include <queue>
39 #include <thread>
40 #include <vector>
41
42 #include "bmusb/bmusb.h"
43 #include "context.h"
44 #include "pbo_frame_allocator.h"
45 #include "ref_counted_frame.h"
46 #include "ref_counted_gl_sync.h"
47
48 class HTTPD;
49 class QSurface;
50
51 #define SURFACE_NUM 16 /* 16 surfaces for source YUV */
52
53 class H264Encoder {
54 public:
55         H264Encoder(QSurface *surface, int width, int height, HTTPD *httpd);
56         ~H264Encoder();
57         //void add_frame(FrameAllocator::Frame frame, GLsync fence);
58
59 #if 0
60         struct Frame {
61         public:
62                 GLuint fbo;
63                 GLuint y_tex, cbcr_tex; 
64
65         private:
66                 //int surface_subnum;
67         };
68         void 
69 #endif
70         void add_audio(int64_t pts, std::vector<float> audio);  // Needs to come before end_frame() of same pts.
71         bool begin_frame(GLuint *y_tex, GLuint *cbcr_tex);
72         void end_frame(RefCountedGLsync fence, int64_t pts, const std::vector<RefCountedFrame> &input_frames);
73
74 private:
75         struct storage_task {
76                 unsigned long long display_order;
77                 int frame_type;
78                 std::vector<float> audio;
79                 int64_t pts, dts;
80         };
81
82         void copy_thread_func();
83         void storage_task_thread();
84         void storage_task_enqueue(storage_task task);
85         int save_codeddata(storage_task task);
86
87         std::thread copy_thread, storage_thread;
88
89         std::mutex storage_task_queue_mutex;
90         std::condition_variable storage_task_queue_changed;
91         int srcsurface_status[SURFACE_NUM];  // protected by storage_task_queue_mutex
92         std::queue<storage_task> storage_task_queue;  // protected by storage_task_queue_mutex
93         bool storage_thread_should_quit = false;  // protected by storage_task_queue_mutex
94
95         std::mutex frame_queue_mutex;
96         std::condition_variable frame_queue_nonempty;
97         bool copy_thread_should_quit = false;  // under frame_queue_mutex
98
99         //int frame_width, frame_height;
100         //int ;
101         int current_storage_frame;
102
103         struct PendingFrame {
104                 RefCountedGLsync fence;
105                 std::vector<RefCountedFrame> input_frames;
106                 int64_t pts;
107         };
108         std::map<int, PendingFrame> pending_video_frames;  // under frame_queue_mutex
109         std::map<int64_t, std::vector<float>> pending_audio_frames;  // under frame_queue_mutex
110         QSurface *surface;
111
112         AVCodecContext *context_audio;
113         HTTPD *httpd;
114 };
115
116 #endif