2 * Common Source Code Project for Qt : movie saver.
3 * (C) 2016 K.Ohta <whatisthis.sowhat _at_ gmail.com>
5 * History: May 27, 2016 : Initial. This refer from avidemux 2.5.6 .
9 #include "movie_saver.h"
11 #include "agar_logger.h"
13 MOVIE_SAVER::MOVIE_SAVER(int width, int height, int fps, OSD *osd, config_t *cfg) : QThread(0)
15 buffer_size=8 * 1024 * 224;
25 audio_sample_rate = 48000;
26 #if defined(USE_MOVIE_SAVER)
27 memset(audio_frame_buf, 0x00, sizeof(audio_frame_buf));
28 memset(video_frame_buf, 0x00, sizeof(video_frame_buf));
29 output_context = NULL;
31 audio_codec = video_codec = NULL;
32 raw_options_list = NULL;
34 memset(&video_st, 0x00, sizeof(video_st));
35 memset(&audio_st, 0x00, sizeof(audio_st));
38 output_context = NULL;
40 audio_data_queue.clear();
41 video_data_queue.clear();
43 do_reset_encoder_options();
45 //video_queue_mutex = new QMutex(QMutex::Recursive);
50 video_bit_rate = 1500 * 1000;
51 audio_bit_rate = 160 * 1000;
52 video_geometry = QSize(640, 480);
53 video_encode_threads = 4;
54 // audio_enqueue_count = 0;
62 MOVIE_SAVER::~MOVIE_SAVER()
64 if(recording) do_close_main();
65 //if(video_queue_mutex) delete video_queue_mutex;
69 QString MOVIE_SAVER::ts2str(int64_t ts)
71 #if defined(USE_LIBAV)
72 char buffer[AV_TS_MAX_STRING_SIZE + 16];
73 memset(buffer, 0x00, sizeof(buffer));
74 return QString::fromLocal8Bit(av_ts_make_string(buffer, ts));
76 return QString::fromUtf8("");
80 QString MOVIE_SAVER::ts2timestr(int64_t ts, void *timebase)
82 #if defined(USE_LIBAV)
83 char buffer[AV_TS_MAX_STRING_SIZE + 16];
84 AVRational *tb = (AVRational *)timebase;
85 memset(buffer, 0x00, sizeof(buffer));
86 return QString::fromLocal8Bit(av_ts_make_time_string(buffer, ts, tb));
88 return QString::fromUtf8("");
92 QString MOVIE_SAVER::err2str(int errnum)
94 #if defined(USE_LIBAV)
95 char buffer[AV_TS_MAX_STRING_SIZE + 16];
96 memset(buffer, 0x00, sizeof(buffer));
97 return QString::fromLocal8Bit(av_make_error_string(buffer, sizeof(buffer), errnum));
99 return QString::fromUtf8("");
103 //void MOVIE_SAVER::log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt)
104 void MOVIE_SAVER::log_packet(const void *_fmt_ctx, const void *_pkt)
106 #if defined(USE_LIBAV)
107 const AVFormatContext *fmt_ctx = (const AVFormatContext *)_fmt_ctx;
108 const AVPacket *pkt = (const AVPacket *)_pkt;
109 AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;
111 AGAR_DebugLog(AGAR_LOG_DEBUG, "pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
112 ts2str(pkt->pts).toLocal8Bit().constData(),
113 ts2timestr(pkt->pts, (void *)time_base).toLocal8Bit().constData(),
114 ts2str(pkt->dts).toLocal8Bit().constData(),
115 ts2timestr(pkt->dts, (void *)time_base).toLocal8Bit().constData(),
116 ts2str(pkt->duration).toLocal8Bit().constData(),
117 ts2timestr(pkt->duration, (void *)time_base).toLocal8Bit().constData(),
123 void MOVIE_SAVER::enqueue_video(int frames, int width, int height, QImage *p)
125 #if defined(USE_MOVIE_SAVER)
126 if(!recording) return;
127 if(p == NULL) return;
130 VIDEO_DATA *px = new VIDEO_DATA(frames, width, height, p);
131 //AGAR_DebugLog(AGAR_LOG_DEBUG, "Movie: Enqueue video data %dx%d %d bytes %dx%d %d frames", width, height, p->byteCount(), p->width(), p->height(), frames);
132 //video_queue_mutex->lock();
133 video_data_queue.enqueue(px);
134 //video_queue_mutex->unlock();
138 bool MOVIE_SAVER::dequeue_video(uint32_t *p)
140 //if(!recording) return false;
141 if(p == NULL) return false;
143 VIDEO_DATA *pp = video_data_queue.dequeue();
144 #if defined(USE_MOVIE_SAVER)
145 if(pp == NULL) return false;
149 QImage *pp_i = &(pp->frame_data);
150 left_frames = pp->frames;
152 _height = pp->_height;
153 if((left_frames > 0) && (pp_i != NULL)){
154 for(y = 0; y < _height; y++) {
155 if(y >= pp_i->height()) break;
156 pq = (uint32_t *)(pp_i->constScanLine(y));
157 memcpy(&(p[_width * y]), pq, ((_width * sizeof(uint32_t)) > pp_i->bytesPerLine()) ? pp_i->bytesPerLine() : _width * sizeof(uint32_t));
159 video_size = _width * y;
160 //AGAR_DebugLog(AGAR_LOG_DEBUG, "Movie: Dequeue video data %d bytes", pp->byteCount());
165 if(pp != NULL) delete pp;
169 void MOVIE_SAVER::enqueue_audio(int16_t *p, int size)
171 #if defined(USE_MOVIE_SAVER)
172 if(!recording) return;
173 if(p == NULL) return;
175 QByteArray *pp = new QByteArray((const char *)p, size);
176 //AGAR_DebugLog(AGAR_LOG_DEBUG, "Movie: Enqueue audio data %d bytes", size);
177 audio_data_queue.enqueue(pp);
178 // audio_enqueue_count++;
182 bool MOVIE_SAVER::dequeue_audio(int16_t *p)
184 //if(!recording) return false;
185 if(audio_data_queue.isEmpty()) return false;
186 if(p == NULL) return false;
188 QByteArray *pp = audio_data_queue.dequeue();
189 int16_t *q = (int16_t *)pp->constData();
191 #if defined(USE_MOVIE_SAVER)
192 if(pp == NULL) return false;
193 audio_size = pp->size();
194 if(audio_size <= 0) return false;
195 // I expect to use SIMD.
196 for(int i = 0; i < audio_size / sizeof(int16_t); i++) {
203 //memcpy(p, pp->constData(), audio_size);
205 //AGAR_DebugLog(AGAR_LOG_DEBUG, "Movie: Dequeue audio data %d bytes", pp->size());
209 if(pp != NULL) delete pp;
214 void MOVIE_SAVER::run()
217 //AGAR_DebugLog(AGAR_LOG_DEBUG, "MOVIE THREAD: Start");
219 int fps_wait = (int)((1000.0 / (double)rec_fps) / 4.0);
220 int tmp_wait = fps_wait;
221 bool need_audio_transcode = false;
222 bool need_video_transcode = false;
224 int64_t total_packets_written = 0;
227 volatile bool old_recording = false;
231 audio_frame_offset = 0;
240 if(!bRunThread) break;
242 //n_encode_video = n_encode_audio = 1;
246 audio_frame_offset = 0;
254 if(!do_open_main()) {
258 AGAR_DebugLog(AGAR_LOG_DEBUG, "MOVIE/Saver: Start to recording.");
259 old_recording = true;
262 if(audio_remain <= 0) {
263 a_f = audio_data_queue.isEmpty();
265 dequeue_audio(audio_frame_buf);
266 audio_remain = audio_size;
268 need_audio_transcode = true;
272 v_f = video_data_queue.isEmpty();
275 if(left_frames <= 0) dequeue_video(video_frame_buf);
277 video_remain = video_size;
279 need_video_transcode = true;
282 if ((n_encode_video == 0) &&
283 ((n_encode_audio != 0) || av_compare_ts(video_st.next_pts, video_st.st->codec->time_base,
284 audio_st.next_pts, audio_st.st->codec->time_base) <= 0)) {
285 n_encode_video = write_video_frame();
286 if(n_encode_video < 0) {
287 AGAR_DebugLog(AGAR_LOG_DEBUG, "MOVIE/Saver: Something wrong with encoding video.");
291 n_encode_audio = write_audio_frame();
292 if(n_encode_audio < 0) {
293 AGAR_DebugLog(AGAR_LOG_DEBUG, "MOVIE/Saver: Something wrong with encoding audio.");
297 if (ret < 0 && ret != AVERROR_EOF) {
299 av_strerror(ret, errbuf, sizeof(errbuf));
300 AGAR_DebugLog(AGAR_LOG_INFO, "Error while filtering: %s\n", (const char *)errbuf);
304 /* dump report by using the output first video and audio streams */
305 //print_report(0, timer_start, cur_time);
308 need_video_transcode = need_audio_transcode = false;
309 old_recording = false;
313 //if(req_stop && a_f && v_f) req_close = true;
314 //printf("%d\n", req_close);
315 if(!bRunThread) break;
316 if(need_video_transcode || need_audio_transcode) {
317 need_video_transcode = need_audio_transcode = false;
320 if(fps_wait >= tmp_wait) {
321 this->msleep(tmp_wait);
324 this->msleep(fps_wait);
325 tmp_wait -= fps_wait;
328 fps_wait = (int)((1000.0 / (double)rec_fps) / 4.0);
334 old_recording = false;
340 old_recording = false;
342 AGAR_DebugLog(AGAR_LOG_DEBUG, "MOVIE: Exit thread.");
349 bool MOVIE_SAVER::is_recording(void)
355 void MOVIE_SAVER::do_exit()
362 void MOVIE_SAVER::do_set_record_fps(int fps)
364 if((fps > 0) && (fps <= 75)) rec_fps = fps;
367 void MOVIE_SAVER::do_set_width(int width)
369 //printf("width = %d -> %d\n", _width, width);
373 void MOVIE_SAVER::do_set_height(int height)
375 //printf("height = %d -> %d\n", _height, height);
379 void MOVIE_SAVER::do_clear_options_list(void)
381 encode_opt_keys.clear();
382 encode_options.clear();
385 void MOVIE_SAVER::do_add_option(QString key, QString value)
387 if(key.isEmpty()) return;
388 encode_opt_keys.append(key);
389 if(value.isEmpty()) {
390 encode_options.append(QString::fromUtf8(""));
392 encode_options.append(value);
396 void MOVIE_SAVER::do_set_video_bitrate(int kbps)
398 if(kbps < 10) return;
399 video_bit_rate = kbps * 1000;
402 void MOVIE_SAVER::do_set_audio_bitrate(int kbps)
405 audio_bit_rate = kbps * 1000;
408 void MOVIE_SAVER::do_set_video_geometry(QSize geometry)
410 if(geometry.width() < 100) return;
411 if(geometry.height() < 80) return;
412 video_geometry = geometry;
415 void MOVIE_SAVER::do_set_video_threads(int threads)
417 video_encode_threads = threads;
420 void MOVIE_SAVER::do_reset_encoder_options(void)
422 encode_opt_keys.clear();
423 encode_options.clear();
425 do_add_option(QString::fromUtf8("c:v"), QString::fromUtf8("libx264"));
426 do_add_option(QString::fromUtf8("c:a"), QString::fromUtf8("aac"));
427 do_add_option(QString::fromUtf8("preset"), QString::fromUtf8("slow"));
428 do_add_option(QString::fromUtf8("tune"), QString::fromUtf8("grain"));
429 //do_add_option(QString::fromUtf8("crf"), QString::fromUtf8("20"));
430 do_add_option(QString::fromUtf8("qmin"), QString::fromUtf8("16"));
431 do_add_option(QString::fromUtf8("qmax"), QString::fromUtf8("24"));
433 do_add_option(QString::fromUtf8("qmax"), QString::fromUtf8("24"));