OSDN Git Service

[WIP][Qt][MOVIE] Still crash when using libav.
[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
16 #if defined(USE_LIBAV)
17 extern "C" {
18         #include "libavutil/channel_layout.h"
19         #include "libavutil/opt.h"
20         #include "libavutil/mathematics.h"
21         #include "libavutil/timestamp.h"
22         #include "libavformat/avformat.h"
23         #include "libswscale/swscale.h"
24         #include "libswresample/swresample.h"
25 }
26 #endif
27
28 class OSD;
29
30 QT_BEGIN_NAMESPACE
31 class MOVIE_SAVER: public QThread
32 {
33         Q_OBJECT
34 protected:
35         OSD *p_osd;
36
37         bool have_video;
38         bool have_audio;
39         bool encode_video;
40         bool encode_audio;
41
42 #if defined(USE_LIBAV)
43         AVFormatContext *output_context;
44         AVOutputFormat *format;
45         AVCodec *audio_codec;
46         AVCodecContext *audio_codec_context;
47         AVCodec *video_codec;
48         AVCodecContext *video_codec_context;
49         
50         AVStream *audio_stream;
51         AVStream *video_stream;
52
53         AVFrame *audio_frame_data;
54         AVFrame *audio_tmp_frame;
55         struct AVCodec codec_real;
56         struct SwrContext *audio_swr_context;
57         int audio_nb_samples;
58         int64_t audio_samples_count;
59         
60         int64_t audio_next_pts;
61         AVDictionary *audio_option;
62         AVDictionary *video_option;
63 #endif   
64         QString _filename;
65         bool bRunThread;
66         
67         uint min_rate;
68         uint max_rate;
69         uint buffer_size;
70         uint bitrate;
71         int _width;
72         int _height;
73         bool recording;
74         int rec_fps;
75
76         AVRational time_base;
77         
78         uint64_t audio_size;
79         uint64_t video_size;
80         uint32_t ptsFrame;
81
82         uint64_t totalSrcFrame;
83         uint64_t totalDstFrame;
84         uint64_t totalAudioFrame;
85
86         int16_t audio_frame[2 * 48000 * sizeof(int16_t)]; // 1Sec
87         uint32_t video_frame[1280 * 512 * sizeof(uint32_t)]; // 1 frame : right?
88         uint32_t video_dst[1280 * 1024 * sizeof(uint32_t)]; // 1 frame : right?
89
90         QQueue<int> video_width_queue;
91         QQueue<int> video_height_queue;
92         QQueue<QByteArray *> video_data_queue;
93         
94         QQueue<QByteArray *> audio_data_queue;
95         
96         bool dequeue_audio(int16_t *);
97         bool dequeue_video(uint32_t *);
98         
99         QString create_date_file_name(void);
100
101
102         bool setup_context(QString filename, int fps);
103         bool setup_audio_codec(void *_opt);
104         bool setup_video_codec();
105
106         void *alloc_audio_frame(int sample_fmt,
107                                                            uint64_t channel_layout,
108                                                            int sample_rate, int nb_samples);
109         int write_audio_frame(const void *_time_base, void *_pkt);
110         bool audio_resample(void *_frame);
111         bool setup_audio_resampler(void);
112         void add_stream_audio(void **_codec, int _codec_id);
113         
114 public:
115         MOVIE_SAVER(int width, int height, int fps, OSD *osd);
116         ~MOVIE_SAVER();
117         bool is_recording(void);
118
119 public slots:
120         void run();
121         void enqueue_video(QByteArray *p, int width, int height);
122         void enqueue_audio(QByteArray *p);
123         void do_close();
124         void do_open(QString filename, int);
125         void do_set_width(int width);
126         void do_set_height(int height);
127         void do_set_record_fps(int fps);
128         void do_exit();
129 };
130 QT_END_NAMESPACE
131
132 #endif