]> git.sesse.net Git - nageru/blobdiff - x264_encoder.h
Support switching Y'CbCr coefficients midway, which will allow doing the Right Thing...
[nageru] / x264_encoder.h
index bb9f1dc066dd43b0b544b744452a1c93251a56da..2e64e66118328cd99e7240d5fcef22a257f7a0fe 100644 (file)
 #ifndef _X264ENCODE_H
 #define _X264ENCODE_H 1
 
+#include <sched.h>
 #include <stdint.h>
-
+#include <x264.h>
 #include <atomic>
+#include <chrono>
 #include <condition_variable>
 #include <memory>
 #include <mutex>
-#include <thread>
 #include <queue>
+#include <string>
+#include <thread>
+#include <unordered_map>
 
 extern "C" {
-#include "x264.h"
 #include <libavformat/avformat.h>
 }
 
+#include <movit/image_format.h>
+
+#include "print_latency.h"
+
 class Mux;
 class X264SpeedControl;
 
@@ -46,9 +53,14 @@ public:
 
        // <data> is taken to be raw NV12 data of WIDTHxHEIGHT resolution.
        // Does not block.
-       void add_frame(int64_t pts, int64_t duration, const uint8_t *data);
+       void add_frame(int64_t pts, int64_t duration, movit::YCbCrLumaCoefficients ycbcr_coefficients, const uint8_t *data, const ReceivedTimestamps &received_ts);
 
-       std::string get_global_headers() const { return global_headers; }
+       std::string get_global_headers() const {
+               while (!x264_init_done) {
+                       sched_yield();
+               }
+               return global_headers;
+       }
 
        void change_bitrate(unsigned rate_kbit) {
                new_bitrate_kbit = rate_kbit;
@@ -57,7 +69,9 @@ public:
 private:
        struct QueuedFrame {
                int64_t pts, duration;
+               movit::YCbCrLumaCoefficients ycbcr_coefficients;
                uint8_t *data;
+               ReceivedTimestamps received_ts;
        };
        void encoder_thread_func();
        void init_x264();
@@ -75,10 +89,13 @@ private:
        std::string buffered_sei;  // Will be output before first frame, if any.
 
        std::thread encoder_thread;
+       std::atomic<bool> x264_init_done{false};
        std::atomic<bool> should_quit{false};
        x264_t *x264;
        std::unique_ptr<X264SpeedControl> speed_control;
 
+       std::function<void(x264_param_t *)> bitrate_override_func;
+
        std::atomic<unsigned> new_bitrate_kbit{0};  // 0 for no change.
 
        // Protects everything below it.
@@ -94,6 +111,9 @@ private:
 
        // Whenever the state of <queued_frames> changes.
        std::condition_variable queued_frames_nonempty;
+
+       // Key is the pts of the frame.
+       std::unordered_map<int64_t, ReceivedTimestamps> frames_being_encoded;
 };
 
 #endif  // !defined(_X264ENCODE_H)