OSDN Git Service

[MOVIE_SAVER] Use SIMD to transfer a picture OSD(VM)->MOVIE_SAVER .
[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 <QStringList>
15 #include <QThread>
16 #include <QSize>
17 #include <QImage>
18 #include "config.h"
19
20 #if defined(USE_LIBAV)
21 extern "C" {
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"
29 }
30 #endif
31 // Copy from FFMPEG-3.0.2; doc/example/muxing.c .
32
33 #define STREAM_PIX_FMT  AV_PIX_FMT_YUV420P /* default pix_fmt */
34
35 //#define SCALE_FLAGS SWS_BICUBLIN
36 #define SCALE_FLAGS SWS_POINT
37
38 enum {
39         VIDEO_CODEC_MPEG4 = 0,
40         VIDEO_CODEC_H264,
41         VIDEO_CODEC_END,
42 };
43
44 enum {
45         AUDIO_CODEC_MP3 = 0,
46         AUDIO_CODEC_AAC,
47         AUDIO_CODEC_VORBIS,
48         AUDIO_CODEC_END,
49 };
50
51 enum {
52         VIDEO_CONTAINER_TYPE_MP4 = 0,
53         VIDEO_CONTAINER_TYPE_MKV,
54         VIDEO_CONTAINER_TYPE_END,
55
56 };
57         
58 // a wrapper around a single output AVStream
59 typedef struct OutputStream {
60         AVStream *st;
61
62         /* pts of the next frame that will be generated */
63         int64_t next_pts;
64         int samples_count;
65
66         AVFrame *frame;
67         AVFrame *tmp_frame;
68
69         float t, tincr, tincr2;
70
71         struct SwsContext *sws_ctx;
72         struct SwrContext *swr_ctx;
73 } OutputStream;
74
75 class OSD;
76 class QMutex;
77 QT_BEGIN_NAMESPACE
78 class VIDEO_DATA {
79
80 public:
81         int frames;
82         int _width;
83         int _height;
84         QImage frame_data;
85         VIDEO_DATA(int n_frames, int width, int height, QImage *pp) {
86                 frame_data = QImage(*pp);
87                 frames = n_frames;
88                 _width = width;
89                 _height = height;
90         };
91         ~VIDEO_DATA() {};
92 };
93
94 class MOVIE_SAVER: public QThread
95 {
96         Q_OBJECT
97 private:
98         int n_encode_audio;
99         int n_encode_video;
100         //QMutex *video_queue_mutex;
101 protected:
102         OSD *p_osd;
103         config_t *p_config;
104         
105         bool req_close;
106         bool req_stop;
107
108         bool have_video;
109         bool have_audio;
110         bool encode_video;
111         bool encode_audio;
112         int64_t audio_count;
113         int64_t video_count;
114         QStringList encode_opt_keys;
115         QStringList encode_options;
116 #if defined(USE_LIBAV)
117         AVOutputFormat *stream_format;
118         AVFormatContext *output_context;
119         AVCodec *audio_codec, *video_codec;
120         AVDictionary *raw_options_list;
121
122         OutputStream video_st;
123         OutputStream audio_st;
124 #endif   
125         QString _filename;
126         bool bRunThread;
127         bool debug_timestamp;
128         
129         uint min_rate;
130         uint max_rate;
131         uint buffer_size;
132         int audio_sample_rate;
133         int _width;
134         int _height;
135         int old_width;
136         int old_height;
137         
138         bool recording;
139         int rec_fps;
140
141         
142         uint64_t audio_size;
143         uint64_t video_size;
144         uint32_t ptsFrame;
145
146         uint64_t totalSrcFrame;
147         uint64_t totalDstFrame;
148         uint64_t totalAudioFrame;
149         int left_frames;
150         
151         int16_t audio_frame_buf[2 * 48000 * sizeof(int16_t)]; // 1Sec
152         uint32_t video_frame_buf[1280 * 1024 * sizeof(uint32_t)]; // 1 frame : right?
153
154         QQueue<VIDEO_DATA *> video_data_queue;
155         QQueue<QByteArray *> audio_data_queue;
156         int64_t audio_remain;
157         int64_t video_remain;
158         uint32_t audio_offset;
159         uint32_t audio_frame_offset;
160         uint32_t video_offset;
161         uint64_t audio_frame_number;
162         uint64_t audio_frame_max;
163         uint64_t video_frame_number;
164         uint64_t video_frame_max;
165
166         int audio_bit_rate;
167         int video_bit_rate;
168         QSize video_geometry;
169         int video_encode_threads;
170         
171         bool dequeue_audio(int16_t *);
172         bool dequeue_video(uint32_t *);
173         
174         QString create_date_file_name(void);
175
176         // Got from FFMPEG 3.0.2, doc/examples/muxer.c 
177         //void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt)
178         void log_packet(const void *_fmt_ctx, const void *_pkt);
179         //int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt)
180         int write_frame(void *_fmt_ctx, const void *_time_base, void *_st, void *_pkt);
181         //void add_stream(OutputStream *ost, AVFormatContext *oc, AVCodec **codec, enum AVCodecID codec_id)
182         bool add_stream(void *_ost, void *_oc, void **_codec, uint64_t codec_id);
183         //AVFrame *alloc_audio_frame(enum AVSampleFormat sample_fmt, uint64_t channel_layout, int sample_rate, int nb_samples)
184         void *alloc_audio_frame(uint64_t _sample_fmt, uint64_t channel_layout,
185                                                         int sample_rate, int nb_samples);
186         //static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
187         bool open_audio(void);
188         //static AVFrame *get_audio_frame(OutputStream *ost)
189         void *get_audio_frame();
190         //static int write_audio_frame(AVFormatContext *oc, OutputStream *ost)
191         int write_audio_frame();
192         //static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
193         void *alloc_picture(uint64_t _pix_fmt, int width, int height);
194         //void open_video(OutputStream *_ost, AVDictionary *_opt_arg)
195         bool open_video();
196         //AVFrame *get_video_frame(OutputStream *ost)
197         void *get_video_frame(void);
198         //static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
199         int write_video_frame();
200         //void MOVIE_SAVER::close_stream(AVFormatContext *oc, OutputStream *ost)
201         void close_stream(void *_oc, void *_ost);
202         //void MOVIE_SAVER::setup_h264(AVCodecContext *_codec)
203         void setup_h264(void *_codec);
204         //void MOVIE_SAVER::setup_mpeg4(AVCodecContext *_codec)
205         void setup_mpeg4(void *_codec);
206         //void MOVIE_SAVER::setup_audio(AVCodecContext *_codec_context, AVCodec **_codec)
207         void setup_audio(void *_codec_context, void **_codec);
208
209         QString ts2str(int64_t ts);
210         QString ts2timestr(int64_t ts, void *timebase);
211         QString err2str(int errnum);
212         void do_close_main();
213         bool do_open_main();
214
215 public:
216         MOVIE_SAVER(int width, int height, int fps, OSD *osd, config_t *cfg);
217         ~MOVIE_SAVER();
218         bool is_recording(void);
219
220 public slots:
221         void run();
222         void enqueue_video(int frames, int width, int height, QImage *p);
223         void enqueue_audio(int16_t *p, int size);
224         void do_close();
225         void do_open(QString filename, int _fps, int sample_rate);
226         void do_set_width(int width);
227         void do_set_height(int height);
228         void do_set_record_fps(int fps);
229
230         void do_set_video_bitrate(int kbps);
231         void do_set_audio_bitrate(int kbps);
232         void do_set_video_geometry(QSize geometry);
233         void do_set_video_threads(int threads);
234         
235         void do_clear_options_list(void);
236         void do_add_option(QString key, QString value);
237         void do_reset_encoder_options(void);
238         
239         void do_exit();
240 signals:
241         int sig_ready_to_record();
242         int sig_set_state_saving_movie(bool);
243 };
244 QT_END_NAMESPACE
245
246 #endif