]> git.sesse.net Git - nageru/blob - kaeru.cpp
Initial check-in of Kaeru, a simple transcoder based on Nageru code.
[nageru] / kaeru.cpp
1 // Kaeru (変える), a simple transcoder intended for use with Nageru.
2 // This is experimental code, not yet supported.
3
4 #include "audio_encoder.h"
5 #include "defs.h"
6 #include "flags.h"
7 #include "ffmpeg_capture.h"
8 #include "mixer.h"
9 #include "mux.h"
10 #include "timebase.h"
11 #include "x264_encoder.h"
12
13 #include <assert.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <string>
17
18 using namespace bmusb;
19 using namespace movit;
20 using namespace std;
21 using namespace std::placeholders;
22
23 Mixer *global_mixer = nullptr;
24
25 int write_packet(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
26 {
27         static bool seen_sync_markers = false;
28         static string stream_mux_header;
29         HTTPD *httpd = (HTTPD *)opaque;
30
31         if (type == AVIO_DATA_MARKER_SYNC_POINT || type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
32                 seen_sync_markers = true;
33         } else if (type == AVIO_DATA_MARKER_UNKNOWN && !seen_sync_markers) {
34                 // We don't know if this is a keyframe or not (the muxer could
35                 // avoid marking it), so we just have to make the best of it.
36                 type = AVIO_DATA_MARKER_SYNC_POINT;
37         }
38
39         if (type == AVIO_DATA_MARKER_HEADER) {
40                 stream_mux_header.append((char *)buf, buf_size);
41                 httpd->set_header(stream_mux_header);
42         } else {
43                 httpd->add_data((char *)buf, buf_size, type == AVIO_DATA_MARKER_SYNC_POINT);
44         }
45         return buf_size;
46 }
47
48 unique_ptr<Mux> create_mux(HTTPD *httpd, AVOutputFormat *oformat, X264Encoder *x264_encoder, AudioEncoder *audio_encoder)
49 {
50         AVFormatContext *avctx = avformat_alloc_context();
51         avctx->oformat = oformat;
52
53         uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
54         avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, httpd, nullptr, nullptr, nullptr);
55         avctx->pb->write_data_type = &write_packet;
56         avctx->pb->ignore_boundary_point = 1;
57
58         string video_extradata = x264_encoder->get_global_headers();
59
60         unique_ptr<Mux> mux;
61         int time_base = global_flags.stream_coarse_timebase ? COARSE_TIMEBASE : TIMEBASE;
62         mux.reset(new Mux(avctx, global_flags.width, global_flags.height, Mux::CODEC_H264, video_extradata, audio_encoder->get_codec_parameters().get(), time_base,
63                 /*write_callback=*/nullptr, Mux::WRITE_FOREGROUND, {}));
64         return mux;
65 }
66
67 void video_frame_callback(FFmpegCapture *video, X264Encoder *x264_encoder, int64_t pts, AVRational timebase, uint16_t timecode,
68                           FrameAllocator::Frame video_frame, size_t video_offset, VideoFormat video_format,
69                           FrameAllocator::Frame audio_frame, size_t audio_offset, AudioFormat audio_format)
70 {
71         if (pts >= 0 && video_frame.len > 0) {
72                 pts = av_rescale_q(pts, timebase, AVRational{ 1, TIMEBASE });
73                 int64_t frame_duration = TIMEBASE * video_format.frame_rate_nom / video_format.frame_rate_den;
74                 x264_encoder->add_frame(pts, frame_duration, video->get_current_frame_ycbcr_format().luma_coefficients, video_frame.data + video_offset, ReceivedTimestamps());
75         }
76
77         if (video_frame.owner) {
78                 video_frame.owner->release_frame(video_frame);
79         }
80         if (audio_frame.owner) {
81                 audio_frame.owner->release_frame(audio_frame);
82         }
83 }
84
85 void audio_frame_callback(Mux *mux, const AVPacket *pkt, AVRational timebase)
86 {
87         mux->add_packet(*pkt, pkt->pts, pkt->dts == AV_NOPTS_VALUE ? pkt->pts : pkt->dts, timebase);
88 }
89
90 int main(int argc, char *argv[])
91 {
92         parse_flags(PROGRAM_KAERU, argc, argv);
93         if (optind + 1 != argc) {
94                 usage(PROGRAM_KAERU);
95                 exit(1);
96         }
97
98         av_register_all();
99         avformat_network_init();
100
101         HTTPD httpd;
102
103         AVOutputFormat *oformat = av_guess_format(global_flags.stream_mux_name.c_str(), nullptr, nullptr);
104         assert(oformat != nullptr);
105
106         unique_ptr<AudioEncoder> audio_encoder;
107         if (global_flags.stream_audio_codec_name.empty()) {
108                 audio_encoder.reset(new AudioEncoder(AUDIO_OUTPUT_CODEC_NAME, DEFAULT_AUDIO_OUTPUT_BIT_RATE, oformat));
109         } else {
110                 audio_encoder.reset(new AudioEncoder(global_flags.stream_audio_codec_name, global_flags.stream_audio_codec_bitrate, oformat));
111         }
112
113         X264Encoder x264_encoder(oformat);
114         unique_ptr<Mux> http_mux = create_mux(&httpd, oformat, &x264_encoder, audio_encoder.get());
115         x264_encoder.add_mux(http_mux.get());
116
117         FFmpegCapture video(argv[optind], global_flags.width, global_flags.height);
118         video.set_pixel_format(FFmpegCapture::PixelFormat_NV12);
119         video.set_frame_callback(bind(video_frame_callback, &video, &x264_encoder, _1, _2, _3, _4, _5, _6, _7, _8, _9));
120         video.set_audio_callback(bind(audio_frame_callback, http_mux.get(), _1, _2));
121         video.configure_card();
122         video.start_bm_capture();
123         video.change_rate(2.0);  // Be sure never to really fall behind, but also don't dump huge amounts of stuff onto x264.
124
125         httpd.start(9095);
126
127         for ( ;; ) {
128                 sleep(3600);
129         }
130 }