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 "csp_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 csp_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "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;
129 VIDEO_DATA *px = new VIDEO_DATA(frames, width, height, p);
130 //csp_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "Movie: Enqueue video data %dx%d %d bytes %dx%d %d frames", width, height, p->byteCount(), p->width(), p->height(), frames);
131 //video_queue_mutex->lock();
132 video_data_queue.enqueue(px);
133 //video_queue_mutex->unlock();
137 bool MOVIE_SAVER::dequeue_video(uint32_t *p)
139 //if(!recording) return false;
140 if(p == NULL) return false;
142 VIDEO_DATA *pp = video_data_queue.dequeue();
143 #if defined(USE_MOVIE_SAVER)
144 if(pp == NULL) return false;
147 QImage *pp_i = &(pp->frame_data);
148 left_frames = pp->frames;
150 _height = pp->_height;
151 if((left_frames > 0) && (pp_i != NULL)){
152 for(y = 0; y < _height; y++) {
153 if(y >= pp_i->height()) break;
154 pq = (uint32_t *)(pp_i->constScanLine(y));
155 memcpy(&(p[_width * y]), pq, (((uint)_width * sizeof(uint32_t)) > pp_i->bytesPerLine()) ? pp_i->bytesPerLine() : _width * sizeof(uint32_t));
157 video_size = _width * y;
158 //csp_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "Movie: Dequeue video data %d bytes", pp->byteCount());
163 if(pp != NULL) delete pp;
167 void MOVIE_SAVER::enqueue_audio(int16_t *p, int size)
169 #if defined(USE_MOVIE_SAVER)
170 if(!recording) return;
171 if(p == NULL) return;
173 QByteArray *pp = new QByteArray((const char *)p, size);
174 //csp_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "Movie: Enqueue audio data %d bytes", size);
175 audio_data_queue.enqueue(pp);
176 // audio_enqueue_count++;
180 bool MOVIE_SAVER::dequeue_audio(int16_t *p)
182 //if(!recording) return false;
183 if(audio_data_queue.isEmpty()) return false;
184 if(p == NULL) return false;
186 QByteArray *pp = audio_data_queue.dequeue();
187 int16_t *q = (int16_t *)pp->constData();
189 #if defined(USE_MOVIE_SAVER)
190 if(pp == NULL) return false;
191 audio_size = pp->size();
192 if(audio_size <= 0) return false;
193 // I expect to use SIMD.
194 for(int i = 0; i < (int)(audio_size / sizeof(int16_t)); i++) {
201 //memcpy(p, pp->constData(), audio_size);
206 if(pp != NULL) delete pp;
211 void MOVIE_SAVER::run()
214 //csp_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "MOVIE THREAD: Start");
216 int fps_wait = (int)((1000.0 / (double)rec_fps) / 4.0);
217 int tmp_wait = fps_wait;
218 bool need_audio_transcode = false;
219 bool need_video_transcode = false;
221 int64_t total_packets_written = 0;
224 volatile bool old_recording = false;
228 audio_frame_offset = 0;
237 if(!bRunThread) break;
239 //n_encode_video = n_encode_audio = 1;
243 audio_frame_offset = 0;
250 if(!do_open_main()) {
254 csp_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "MOVIE/Saver: Start to recording.");
255 old_recording = true;
258 if(audio_remain <= 0) {
259 a_f = audio_data_queue.isEmpty();
261 dequeue_audio(audio_frame_buf);
262 audio_remain = audio_size;
264 need_audio_transcode = true;
268 v_f = video_data_queue.isEmpty();
271 if(left_frames <= 0) dequeue_video(video_frame_buf);
273 video_remain = video_size;
275 need_video_transcode = true;
278 if ((n_encode_video == 0) &&
279 ((n_encode_audio != 0) || av_compare_ts(video_st.next_pts, video_st.st->codec->time_base,
280 audio_st.next_pts, audio_st.st->codec->time_base) <= 0)) {
281 n_encode_video = write_video_frame();
282 if(n_encode_video < 0) {
283 csp_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "MOVIE/Saver: Something wrong with encoding video.");
287 n_encode_audio = write_audio_frame();
288 if(n_encode_audio < 0) {
289 csp_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "MOVIE/Saver: Something wrong with encoding audio.");
293 if (ret < 0 && ret != AVERROR_EOF) {
295 av_strerror(ret, errbuf, sizeof(errbuf));
296 csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_MOVIE_SAVER, "Error while filtering: %s\n", (const char *)errbuf);
300 /* dump report by using the output first video and audio streams */
301 //print_report(0, timer_start, cur_time);
304 need_video_transcode = need_audio_transcode = false;
305 old_recording = false;
309 //if(req_stop && a_f && v_f) req_close = true;
310 if(!bRunThread) break;
311 if(need_video_transcode || need_audio_transcode) {
312 need_video_transcode = need_audio_transcode = false;
315 if(fps_wait >= tmp_wait) {
316 this->msleep(tmp_wait);
319 this->msleep(fps_wait);
320 tmp_wait -= fps_wait;
323 fps_wait = (int)((1000.0 / (double)rec_fps) / 4.0);
329 old_recording = false;
335 old_recording = false;
337 csp_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "MOVIE: Exit thread.");
344 bool MOVIE_SAVER::is_recording(void)
350 void MOVIE_SAVER::do_exit()
357 void MOVIE_SAVER::do_set_record_fps(int fps)
359 if((fps > 0) && (fps <= 75)) rec_fps = fps;
362 void MOVIE_SAVER::do_set_width(int width)
367 void MOVIE_SAVER::do_set_height(int height)
372 void MOVIE_SAVER::do_clear_options_list(void)
374 encode_opt_keys.clear();
375 encode_options.clear();
378 void MOVIE_SAVER::do_add_option(QString key, QString value)
380 if(key.isEmpty()) return;
381 encode_opt_keys.append(key);
382 if(value.isEmpty()) {
383 encode_options.append(QString::fromUtf8(""));
385 encode_options.append(value);
389 void MOVIE_SAVER::do_set_video_bitrate(int kbps)
391 if(kbps < 10) return;
392 video_bit_rate = kbps * 1000;
395 void MOVIE_SAVER::do_set_audio_bitrate(int kbps)
398 audio_bit_rate = kbps * 1000;
401 void MOVIE_SAVER::do_set_video_geometry(QSize geometry)
403 if(geometry.width() < 100) return;
404 if(geometry.height() < 80) return;
405 video_geometry = geometry;
408 void MOVIE_SAVER::do_set_video_threads(int threads)
410 video_encode_threads = threads;
413 void MOVIE_SAVER::do_reset_encoder_options(void)
415 encode_opt_keys.clear();
416 encode_options.clear();
418 do_add_option(QString::fromUtf8("c:v"), QString::fromUtf8("libx264"));
419 do_add_option(QString::fromUtf8("c:a"), QString::fromUtf8("aac"));
420 do_add_option(QString::fromUtf8("preset"), QString::fromUtf8("slow"));
421 do_add_option(QString::fromUtf8("tune"), QString::fromUtf8("grain"));
422 //do_add_option(QString::fromUtf8("crf"), QString::fromUtf8("20"));
423 do_add_option(QString::fromUtf8("qmin"), QString::fromUtf8("16"));
424 do_add_option(QString::fromUtf8("qmax"), QString::fromUtf8("24"));
426 do_add_option(QString::fromUtf8("qmax"), QString::fromUtf8("24"));