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
17 #if defined(USE_LIBAV)
19 #include "libavutil/channel_layout.h"
20 #include "libavutil/opt.h"
21 #include "libavutil/mathematics.h"
22 #include "libavutil/timestamp.h"
23 #include "libavformat/avformat.h"
24 #include "libswscale/swscale.h"
25 #include "libswresample/swresample.h"
28 // Copy from FFMPEG-3.0.2; doc/example/muxing.c .
30 #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
32 //#define SCALE_FLAGS SWS_BICUBLIN
33 #define SCALE_FLAGS SWS_POINT
35 // a wrapper around a single output AVStream
36 typedef struct OutputStream {
39 /* pts of the next frame that will be generated */
46 float t, tincr, tincr2;
48 struct SwsContext *sws_ctx;
49 struct SwrContext *swr_ctx;
55 class MOVIE_SAVER: public QThread
66 QStringList encode_opt_keys;
67 QStringList encode_options;
68 #if defined(USE_LIBAV)
69 AVOutputFormat *stream_format;
70 AVFormatContext *output_context;
71 AVCodec *audio_codec, *video_codec;
72 AVDictionary *raw_options_list;
74 OutputStream video_st;
75 OutputStream audio_st;
84 int audio_sample_rate;
98 uint64_t totalSrcFrame;
99 uint64_t totalDstFrame;
100 uint64_t totalAudioFrame;
102 int16_t audio_frame_buf[2 * 48000 * sizeof(int16_t)]; // 1Sec
103 uint32_t video_frame_buf[1280 * 1024 * sizeof(uint32_t)]; // 1 frame : right?
105 QQueue<QImage *> video_data_queue;
106 QQueue<QByteArray *> audio_data_queue;
107 int64_t audio_remain;
108 int64_t video_remain;
109 uint32_t audio_offset;
110 uint32_t audio_frame_offset;
111 uint32_t video_offset;
112 uint64_t audio_frame_number;
113 uint64_t audio_frame_max;
114 uint64_t video_frame_number;
115 uint64_t video_frame_max;
119 QSize video_geometry;
120 int video_encode_threads;
122 bool dequeue_audio(int16_t *);
123 bool dequeue_video(uint32_t *);
125 QString create_date_file_name(void);
127 // Got from FFMPEG 3.0.2, doc/examples/muxer.c
128 //void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt)
129 void log_packet(const void *_fmt_ctx, const void *_pkt);
130 //int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt)
131 int write_frame(void *_fmt_ctx, const void *_time_base, void *_st, void *_pkt);
132 //void add_stream(OutputStream *ost, AVFormatContext *oc, AVCodec **codec, enum AVCodecID codec_id)
133 bool add_stream(void *_ost, void *_oc, void **_codec, uint64_t codec_id);
134 //AVFrame *alloc_audio_frame(enum AVSampleFormat sample_fmt, uint64_t channel_layout, int sample_rate, int nb_samples)
135 void *alloc_audio_frame(uint64_t _sample_fmt, uint64_t channel_layout,
136 int sample_rate, int nb_samples);
137 //static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
138 bool open_audio(void);
139 //static AVFrame *get_audio_frame(OutputStream *ost)
140 void *get_audio_frame();
141 //static int write_audio_frame(AVFormatContext *oc, OutputStream *ost)
142 int write_audio_frame();
143 //static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
144 void *alloc_picture(uint64_t _pix_fmt, int width, int height);
145 //void open_video(OutputStream *_ost, AVDictionary *_opt_arg)
147 //AVFrame *get_video_frame(OutputStream *ost)
148 void *get_video_frame(void);
149 //static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
150 int write_video_frame();
151 //void MOVIE_SAVER::close_stream(AVFormatContext *oc, OutputStream *ost)
152 void close_stream(void *_oc, void *_ost);
155 QString ts2str(int64_t ts);
156 QString ts2timestr(int64_t ts, void *timebase);
157 QString err2str(int errnum);
160 MOVIE_SAVER(int width, int height, int fps, OSD *osd);
162 bool is_recording(void);
166 void enqueue_video(QImage *p);
167 void enqueue_audio(int16_t *p, int size);
169 bool do_open(QString filename, int _fps, int sample_rate);
170 void do_set_width(int width);
171 void do_set_height(int height);
172 void do_set_record_fps(int fps);
174 void do_set_video_bitrate(int kbps);
175 void do_set_audio_bitrate(int kbps);
176 void do_set_video_geometry(QSize geometry);
177 void do_set_video_threads(int threads);
179 void do_clear_options_list(void);
180 void do_add_option(QString key, QString value);
181 void do_reset_encoder_options(void);