#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;
// <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;
private:
struct QueuedFrame {
int64_t pts, duration;
+ movit::YCbCrLumaCoefficients ycbcr_coefficients;
uint8_t *data;
+ ReceivedTimestamps received_ts;
};
void encoder_thread_func();
void init_x264();
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.
// 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)