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 .
8 #ifndef _QT_OSD_MOVIE_SAVER_H
9 #define _QT_OSD_MOVIE_SAVER_H
14 #include <QStringList>
20 #if defined(USE_LIBAV)
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/mathematics.h"
25 #include "libavutil/timestamp.h"
26 #include "libavformat/avformat.h"
27 #include "libswscale/swscale.h"
28 #include "libswresample/swresample.h"
31 // Copy from FFMPEG-3.0.2; doc/example/muxing.c .
33 #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
35 //#define SCALE_FLAGS SWS_BICUBLIN
36 #define SCALE_FLAGS SWS_POINT
39 VIDEO_CODEC_MPEG4 = 0,
52 VIDEO_CONTAINER_TYPE_MP4 = 0,
53 VIDEO_CONTAINER_TYPE_MKV,
54 VIDEO_CONTAINER_TYPE_END,
58 // a wrapper around a single output AVStream
59 typedef struct OutputStream {
62 /* pts of the next frame that will be generated */
69 float t, tincr, tincr2;
71 struct SwsContext *sws_ctx;
72 struct SwrContext *swr_ctx;
79 class DLL_PREFIX VIDEO_DATA {
86 VIDEO_DATA(int n_frames, int width, int height, QImage *pp) {
87 frame_data = QImage(*pp);
95 class DLL_PREFIX MOVIE_SAVER: public QThread
101 //QMutex *video_queue_mutex;
105 CSP_Logger *p_logger;
116 QStringList encode_opt_keys;
117 QStringList encode_options;
118 #if defined(USE_LIBAV)
119 AVOutputFormat *stream_format;
120 AVFormatContext *output_context;
121 AVCodec *audio_codec, *video_codec;
122 AVDictionary *raw_options_list;
124 OutputStream video_st;
125 OutputStream audio_st;
129 bool debug_timestamp;
134 int audio_sample_rate;
148 uint64_t totalSrcFrame;
149 uint64_t totalDstFrame;
150 uint64_t totalAudioFrame;
153 int16_t audio_frame_buf[2 * 48000 * sizeof(int16_t)]; // 1Sec
154 uint32_t video_frame_buf[1280 * 1024 * sizeof(uint32_t)]; // 1 frame : right?
156 QQueue<VIDEO_DATA *> video_data_queue;
157 QQueue<QByteArray *> audio_data_queue;
158 int64_t audio_remain;
159 int64_t video_remain;
160 uint32_t audio_offset;
161 uint32_t audio_frame_offset;
162 uint32_t video_offset;
163 uint64_t audio_frame_number;
164 uint64_t audio_frame_max;
165 uint64_t video_frame_number;
166 uint64_t video_frame_max;
170 QSize video_geometry;
171 int video_encode_threads;
173 bool dequeue_audio(int16_t *);
174 bool dequeue_video(uint32_t *);
176 QString create_date_file_name(void);
178 // Got from FFMPEG 3.0.2, doc/examples/muxer.c
179 //void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt)
180 void log_packet(const void *_fmt_ctx, const void *_pkt);
181 //int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt)
182 int write_frame(void *_fmt_ctx, const void *_time_base, void *_st, void *_pkt);
183 //void add_stream(OutputStream *ost, AVFormatContext *oc, AVCodec **codec, enum AVCodecID codec_id)
184 bool add_stream(void *_ost, void *_oc, void **_codec, uint64_t codec_id);
185 //AVFrame *alloc_audio_frame(enum AVSampleFormat sample_fmt, uint64_t channel_layout, int sample_rate, int nb_samples)
186 void *alloc_audio_frame(uint64_t _sample_fmt, uint64_t channel_layout,
187 int sample_rate, int nb_samples);
188 //static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
189 bool open_audio(void);
190 //static AVFrame *get_audio_frame(OutputStream *ost)
191 void *get_audio_frame();
192 //static int write_audio_frame(AVFormatContext *oc, OutputStream *ost)
193 int write_audio_frame();
194 //static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
195 void *alloc_picture(uint64_t _pix_fmt, int width, int height);
196 //void open_video(OutputStream *_ost, AVDictionary *_opt_arg)
198 //AVFrame *get_video_frame(OutputStream *ost)
199 void *get_video_frame(void);
200 //static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
201 int write_video_frame();
202 //void MOVIE_SAVER::close_stream(AVFormatContext *oc, OutputStream *ost)
203 void close_stream(void *_oc, void *_ost);
204 //void MOVIE_SAVER::setup_h264(AVCodecContext *_codec)
205 void setup_h264(void *_codec);
206 //void MOVIE_SAVER::setup_mpeg4(AVCodecContext *_codec)
207 void setup_mpeg4(void *_codec);
208 //void MOVIE_SAVER::setup_audio(AVCodecContext *_codec_context, AVCodec **_codec)
209 void setup_audio(void *_codec_context, void **_codec);
211 QString ts2str(int64_t ts);
212 QString ts2timestr(int64_t ts, void *timebase);
213 QString err2str(int errnum);
214 void do_close_main();
218 MOVIE_SAVER(int width, int height, int fps, OSD *osd, config_t *cfg);
220 bool is_recording(void);
221 QString get_avio_version();
225 void enqueue_video(int frames, int width, int height, QImage *p);
226 void enqueue_audio(int16_t *p, int size);
228 void do_open(QString filename, int _fps, int sample_rate);
229 void do_set_width(int width);
230 void do_set_height(int height);
231 void do_set_record_fps(int fps);
233 void do_set_video_bitrate(int kbps);
234 void do_set_audio_bitrate(int kbps);
235 void do_set_video_geometry(QSize geometry);
236 void do_set_video_threads(int threads);
238 void do_clear_options_list(void);
239 void do_add_option(QString key, QString value);
240 void do_reset_encoder_options(void);
244 int sig_ready_to_record();
245 int sig_set_state_saving_movie(bool);