OSDN Git Service

[Qt][MOVIE_SAVER] (MAYBE) Fix not running with Ubuntu 16.04 LTS.
[csp-qt/common_source_project-fm7.git] / source / src / qt / avio / movie_saver.h
1 /*
2  * Common Source Code Project for Qt : movie saver.
3  * (C) 2016 K.Ohta <whatisthis.sowhat _at_ gmail.com>
4  *  License: GPLv2
5  *  History: May 27, 2016 : Initial. This refer from avidemux 2.5.6 .
6  */
7
8 #ifndef _QT_OSD_MOVIE_SAVER_H
9 #define _QT_OSD_MOVIE_SAVER_H
10
11 #include <QByteArray>
12 #include <QQueue>
13 #include <QString>
14 #include <QThread>
15 #include <QSize>
16
17 #if defined(USE_LIBAV)
18 extern "C" {
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"
26 }
27 #endif
28 // Copy from FFMPEG-3.0.2; doc/example/muxing.c .
29
30 #define STREAM_PIX_FMT    AV_PIX_FMT_YUV420P /* default pix_fmt */
31
32 //#define SCALE_FLAGS SWS_BICUBLIN
33 #define SCALE_FLAGS SWS_POINT
34
35 // a wrapper around a single output AVStream
36 typedef struct OutputStream {
37     AVStream *st;
38
39     /* pts of the next frame that will be generated */
40     int64_t next_pts;
41     int samples_count;
42
43     AVFrame *frame;
44     AVFrame *tmp_frame;
45
46     float t, tincr, tincr2;
47
48     struct SwsContext *sws_ctx;
49     struct SwrContext *swr_ctx;
50 } OutputStream;
51
52 class OSD;
53
54 QT_BEGIN_NAMESPACE
55 class MOVIE_SAVER: public QThread
56 {
57         Q_OBJECT
58 protected:
59         OSD *p_osd;
60
61         bool have_video;
62         bool have_audio;
63         bool encode_video;
64         bool encode_audio;
65
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;
73
74         OutputStream video_st;
75         OutputStream audio_st;
76 #endif   
77         QString _filename;
78         bool bRunThread;
79         bool debug_timestamp;
80         
81         uint min_rate;
82         uint max_rate;
83         uint buffer_size;
84         int audio_sample_rate;
85         int _width;
86         int _height;
87         int old_width;
88         int old_height;
89         
90         bool recording;
91         int rec_fps;
92
93         
94         uint64_t audio_size;
95         uint64_t video_size;
96         uint32_t ptsFrame;
97
98         uint64_t totalSrcFrame;
99         uint64_t totalDstFrame;
100         uint64_t totalAudioFrame;
101
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?
104
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;
116
117         int audio_bit_rate;
118         int video_bit_rate;
119         QSize video_geometry;
120         int video_encode_threads;
121         
122         bool dequeue_audio(int16_t *);
123         bool dequeue_video(uint32_t *);
124         
125         QString create_date_file_name(void);
126
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)
146         bool open_video();
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);
153
154
155         QString ts2str(int64_t ts);
156         QString ts2timestr(int64_t ts, void *timebase);
157         QString err2str(int errnum);
158
159 public:
160         MOVIE_SAVER(int width, int height, int fps, OSD *osd);
161         ~MOVIE_SAVER();
162         bool is_recording(void);
163
164 public slots:
165         void run();
166         void enqueue_video(QImage *p);
167         void enqueue_audio(int16_t *p, int size);
168         void do_close();
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);
173
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);
178         
179         void do_clear_options_list(void);
180         void do_add_option(QString key, QString value);
181         void do_reset_encoder_options(void);
182         
183         void do_exit();
184 };
185 QT_END_NAMESPACE
186
187 #endif