| 1 | #ifndef CONVERTER_H
|
|---|
| 2 | #define CONVERTER_H
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 | #include <QObject>
|
|---|
| 6 | #include <QDebug>
|
|---|
| 7 | #include <QThread>
|
|---|
| 8 | #include <QDir>
|
|---|
| 9 | #include <QFile>
|
|---|
| 10 | #include <QFileInfo>
|
|---|
| 11 | #include <stdio.h>
|
|---|
| 12 | #include <string.h>
|
|---|
| 13 | #include <iostream>
|
|---|
| 14 | #include <fstream>
|
|---|
| 15 |
|
|---|
| 16 | #include "torrent/torrenthandle.h"
|
|---|
| 17 |
|
|---|
| 18 | extern "C" {
|
|---|
| 19 | #include <libavformat/avformat.h>
|
|---|
| 20 | #include <libavutil/imgutils.h>
|
|---|
| 21 | #include <libswscale/swscale.h>
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 | #include <libavcodec/avcodec.h>
|
|---|
| 25 | #include <libavformat/avformat.h>
|
|---|
| 26 | #include <libavutil/timestamp.h>
|
|---|
| 27 | #include <libavformat/avformat.h>
|
|---|
| 28 | #include <libavfilter/buffersink.h>
|
|---|
| 29 | #include <libavfilter/buffersrc.h>
|
|---|
| 30 | #include <libavutil/opt.h>
|
|---|
| 31 | #include <libavutil/pixdesc.h>
|
|---|
| 32 |
|
|---|
| 33 | #include <libavutil/opt.h>
|
|---|
| 34 | #include <libavutil/file.h>
|
|---|
| 35 | #include <libavutil/channel_layout.h>
|
|---|
| 36 | #include <libavutil/samplefmt.h>
|
|---|
| 37 | #include <libswresample/swresample.h>
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | #include <libavutil\audio_fifo.h>
|
|---|
| 41 | #include <libavcodec\avcodec.h>
|
|---|
| 42 | #include <libavformat\avformat.h>
|
|---|
| 43 | #include <libavutil\imgutils.h>
|
|---|
| 44 | #include <libswresample\swresample.h>
|
|---|
| 45 | #include <libswscale\swscale.h>
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | struct BufferData {
|
|---|
| 49 | QByteArray data;
|
|---|
| 50 | qint64 seek;
|
|---|
| 51 | qint64 fullsize;
|
|---|
| 52 | qint64 writeSize;
|
|---|
| 53 | std::ofstream stream;
|
|---|
| 54 |
|
|---|
| 55 | BufferData() {
|
|---|
| 56 | seek = -1;
|
|---|
| 57 | fullsize =0;
|
|---|
| 58 | writeSize = 0;
|
|---|
| 59 | stream.open("log.txt");
|
|---|
| 60 | }
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | class Converter : public QThread
|
|---|
| 64 | {
|
|---|
| 65 | Q_OBJECT
|
|---|
| 66 |
|
|---|
| 67 | qint64 m_bytesReceived;
|
|---|
| 68 | qint64 m_bytesTotal;
|
|---|
| 69 | float m_downloadProgress;
|
|---|
| 70 | QString m_inputName;
|
|---|
| 71 | static QString m_outputName;
|
|---|
| 72 | static QFile fileOutput;
|
|---|
| 73 | TorrentHandle *m_handle;
|
|---|
| 74 |
|
|---|
| 75 | public slots:
|
|---|
| 76 | void setDownloadProgress(float value) { m_downloadProgress= value; }
|
|---|
| 77 | void setBytesReceived(qint64 value) { m_bytesReceived= value; }
|
|---|
| 78 | void setBytesTotal(qint64 value) { m_bytesTotal= value; }
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | public:
|
|---|
| 82 | Converter(QObject *parent = Q_NULLPTR);
|
|---|
| 83 | void run() override;
|
|---|
| 84 |
|
|---|
| 85 | void setInputName(const QString &name) { qDebug()<<"inputName="<<name; m_inputName=name; }
|
|---|
| 86 | void setOutputName(const QString &name) { qDebug()<<"outputName="<<name; m_outputName=name; }
|
|---|
| 87 | void setTorrentHandle(TorrentHandle *value);
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 | static int read_packet_to_buffer(void *opaque, uint8_t *buf, int buf_size);
|
|---|
| 91 | static int write_packet_to_buffer(void *opaque, uint8_t *buf, int buf_size) ;
|
|---|
| 92 | static bool writeBuffer(BufferData *bufferData);
|
|---|
| 93 |
|
|---|
| 94 | void createBufferInput(const char* input_filename);
|
|---|
| 95 | void createBuffer();
|
|---|
| 96 | void createSWR();
|
|---|
| 97 | int open (const char* source);
|
|---|
| 98 | bool createAudioEncoder();
|
|---|
| 99 | bool createVideoEncoder();
|
|---|
| 100 | int open_destination (const char* destination);
|
|---|
| 101 | void createStreamList();
|
|---|
| 102 | int encode(AVFrame *enc_frame, AVMediaType type);
|
|---|
| 103 | void flushVideo();
|
|---|
| 104 | void flushAudio(long audio_pts);
|
|---|
| 105 | void transcode();
|
|---|
| 106 |
|
|---|
| 107 | bool hasAudio;
|
|---|
| 108 | bool hasVideo;
|
|---|
| 109 | std::ofstream logFile;
|
|---|
| 110 | BufferData *bufferData;
|
|---|
| 111 | BufferData *bufferData_input;
|
|---|
| 112 | AVIOContext *avio_ctx;
|
|---|
| 113 | AVIOContext *avio_ctx_input;
|
|---|
| 114 | AVFormatContext *fmt_ctx;
|
|---|
| 115 | AVFormatContext *ofmt_ctx;
|
|---|
| 116 | //audio
|
|---|
| 117 | AVStream *streamAudio;
|
|---|
| 118 | AVStream *out_stream_audio;
|
|---|
| 119 | AVCodecContext *decoderAudio_ctx;
|
|---|
| 120 | AVCodecContext *encoderAudio_ctx;
|
|---|
| 121 | AVCodec *decoderAudio;
|
|---|
| 122 | AVCodec *encoderAudio;
|
|---|
| 123 | AVPacket packet;
|
|---|
| 124 | AVFrame *frame;
|
|---|
| 125 | SwrContext *swrContext;
|
|---|
| 126 | AVFrame *output_audio_frame;
|
|---|
| 127 | AVPacket *output_audio_packet;
|
|---|
| 128 | AVAudioFifo *audioSampleBuffer;
|
|---|
| 129 | //video
|
|---|
| 130 | AVStream *streamVideo;
|
|---|
| 131 | AVStream *out_stream_video;
|
|---|
| 132 | AVCodecContext *decoderVideo_ctx;
|
|---|
| 133 | AVCodecContext *encoderVideo_ctx;
|
|---|
| 134 | AVCodec *decoderVideo;
|
|---|
| 135 | AVCodec *encoderVideo;
|
|---|
| 136 |
|
|---|
| 137 | int audio_stream_index;
|
|---|
| 138 | int video_stream_index;
|
|---|
| 139 | int output_audio_stream_index;
|
|---|
| 140 | int output_video_stream_index;
|
|---|
| 141 | };
|
|---|
| 142 |
|
|---|
| 143 | #endif // CONVERTER_H
|
|---|