You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
746 lines
24 KiB
746 lines
24 KiB
#include "videoAVLIBDestination.h"
|
|
|
|
#include <libavutil/avassert.h>
|
|
#include <libavutil/channel_layout.h>
|
|
#include <libavutil/opt.h>
|
|
#include <libavutil/mathematics.h>
|
|
//#include <libavutil/timestamp.h>
|
|
#include <libswscale/swscale.h>
|
|
#include <libswresample/swresample.h>
|
|
#define LOG( X ) m_pLog->Log( "LibAV_DST", this, X )
|
|
#define LOG_ERROR( X ) m_pLog->Log( "LibAV_DST", this, _LOG_ERROR, X )
|
|
//https://gist.github.com/yohhoy/52b31522dbb751e5296e
|
|
//https://gist.github.com/sdumetz/961585ea70f82e4fb27aadf66b2c9cb2
|
|
//https://gist.github.com/victusfate/9420226
|
|
|
|
videoAVLIBDestination::videoAVLIBDestination(){
|
|
m_nPTS = 0;
|
|
m_nDTS = 0;
|
|
img_convert_ctx = NULL;
|
|
m_nWidth = VIDEO_OUTPUT_WIDTH;
|
|
m_nHeight = VIDEO_OUTPUT_HEIGHT;
|
|
LOG("Constructor Done!");
|
|
m_bRecordIsStarted = false;
|
|
};
|
|
|
|
videoAVLIBDestination::~videoAVLIBDestination(){
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
ctx->bit_rate = 500*1000;
|
|
ctx->bit_rate_tolerance = 0;
|
|
ctx->rc_max_rate = 0;
|
|
ctx->rc_buffer_size = 0;
|
|
ctx->gop_size = 40;
|
|
ctx->max_b_frames = 3;
|
|
ctx->b_frame_strategy = 1;
|
|
ctx->coder_type = 1;
|
|
ctx->me_cmp = 1;
|
|
ctx->me_range = 16;
|
|
ctx->qmin = 10;
|
|
ctx->qmax = 51;
|
|
ctx->scenechange_threshold = 40;
|
|
ctx->flags |= CODEC_FLAG_LOOP_FILTER;
|
|
ctx->me_method = ME_HEX;
|
|
ctx->me_subpel_quality = 5;
|
|
ctx->i_quant_factor = 0.71;
|
|
ctx->qcompress = 0.6;
|
|
ctx->max_qdiff = 4;
|
|
ctx->directpred = 1;
|
|
ctx->flags2 |= CODEC_FLAG2_FASTPSKIP;
|
|
c->profile = FF_PROFILE_H264_BASELINE;
|
|
|
|
|
|
|
|
Don't forget to use x264 private options. You can always set a profile:
|
|
|
|
av_dict_set(&This->opts, "vprofile", "baseline", 0)
|
|
|
|
Or set the lowest encoding latency:
|
|
|
|
av_dict_set(&This->opts, "tune", "zerolatency", 0);
|
|
|
|
Or select a preset:
|
|
|
|
av_dict_set(&This->opts, "preset","ultrafast",0);
|
|
|
|
Before opening a codec
|
|
|
|
avcodec_open2(This->context, This->codec, &This->opts)
|
|
|
|
shareedit
|
|
|
|
|
|
|
|
|
|
// libx264-medium.ffpreset preset
|
|
ctx->coder_type = 1; // coder = 1
|
|
ctx->flags|=CODEC_FLAG_LOOP_FILTER; // flags=+loop
|
|
ctx->me_cmp|= 1; // cmp=+chroma, where CHROMA = 1
|
|
ctx->partitions|=X264_PART_I8X8+X264_PART_I4X4+X264_PART_P8X8+X264_PART_B8X8; // partitions=+parti8x8+parti4x4+partp8x8+partb8x8
|
|
ctx->me_method=ME_HEX; // me_method=hex
|
|
ctx->me_subpel_quality = 7; // subq=7
|
|
ctx->me_range = 16; // me_range=16
|
|
ctx->gop_size = 250; // g=250
|
|
ctx->keyint_min = 25; // keyint_min=25
|
|
ctx->scenechange_threshold = 40; // sc_threshold=40
|
|
ctx->i_quant_factor = 0.71; // i_qfactor=0.71
|
|
ctx->b_frame_strategy = 1; // b_strategy=1
|
|
ctx->qcompress = 0.6; // qcomp=0.6
|
|
ctx->qmin = 10; // qmin=10
|
|
ctx->qmax = 51; // qmax=51
|
|
ctx->max_qdiff = 4; // qdiff=4
|
|
ctx->max_b_frames = 3; // bf=3
|
|
ctx->refs = 3; // refs=3
|
|
ctx->directpred = 1; // directpred=1
|
|
ctx->trellis = 1; // trellis=1
|
|
ctx->flags2|=CODEC_FLAG2_BPYRAMID+CODEC_FLAG2_MIXED_REFS+CODEC_FLAG2_WPRED+CODEC_FLAG2_8X8DCT+CODEC_FLAG2_FASTPSKIP; // flags2=+bpyramid+mixed_refs+wpred+dct8x8+fastpskip
|
|
ctx->weighted_p_pred = 2; // wpredp=2
|
|
|
|
// libx264-main.ffpreset preset
|
|
ctx->flags2|=CODEC_FLAG2_8X8DCT;c->flags2^=CODEC_FLAG2_8X8DCT; // flags2=-dct8x8
|
|
|
|
|
|
*/
|
|
|
|
|
|
void videoAVLIBDestination::startRecord( CSTR fileName ){
|
|
if( init() ){
|
|
m_bRecordIsStarted = open( fileName );
|
|
};
|
|
};
|
|
|
|
int videoAVLIBDestination::stopRecord(void){
|
|
if (m_bRecordIsStarted) {
|
|
close();
|
|
m_bRecordIsStarted = false;
|
|
};
|
|
return 0;
|
|
};
|
|
|
|
|
|
/*
|
|
|
|
formatParams.device = "/dev/video0";
|
|
formatParams.channel = 0;
|
|
formatParams.standard = "pal";
|
|
formatParams.width = 324;
|
|
formatParams.height = 248;
|
|
|
|
formatParams.time_base= (AVRational){1,10};
|
|
pInputFormat = av_find_input_format("video4linux");
|
|
|
|
av_dict_set(&options, "b", "2.5M", 0);
|
|
*/
|
|
/*
|
|
typedef struct EncodeCallback {
|
|
int (*pull_frame)(void *priv, AVFrame *frame);
|
|
int (*push_packet)(void *priv, AVPacket *packet);
|
|
void *priv_data_push, *priv_data_pull;
|
|
} EncodeCallback;
|
|
|
|
int encode_loop( AVCodecContext *avctx, EncodeCallback *cb ){
|
|
AVPacket *pkt = av_packet_alloc();
|
|
AVFrame *frame = av_frame_alloc();
|
|
int ret;
|
|
while ((ret = avcodec_encode_need_data(avctx)) > 0) {
|
|
ret = cb->pull_frame(cb->priv_data_pull, frame);
|
|
if (ret < 0) goto end;
|
|
ret = avcodec_encode_push(avctx, frame);
|
|
if (ret < 0) goto end;
|
|
};
|
|
while ((ret = avcodec_encode_have_data(avctx)) > 0) {
|
|
ret = avcodec_encode_pull(avctx, pkt);
|
|
if (ret < 0) goto end;
|
|
ret = cb->push_packet(cb->priv_data_push, pkt);
|
|
if (ret < 0) goto end;
|
|
};
|
|
|
|
end:
|
|
av_frame_free(&frame);
|
|
av_packet_free(&pkt);
|
|
return ret;
|
|
};
|
|
*/
|
|
/*
|
|
|
|
// Decode
|
|
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt);
|
|
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame);
|
|
|
|
// Encode
|
|
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame);
|
|
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt);
|
|
|
|
|
|
|
|
int encode(AVCodecContext *avctx, AVPacket *pkt, int *got_packet, AVFrame *frame){
|
|
int ret;
|
|
|
|
*got_packet = 0;
|
|
|
|
ret = avcodec_send_frame(avctx, frame);
|
|
if (ret < 0) return ret;
|
|
|
|
ret = avcodec_receive_packet(avctx, pkt);
|
|
if (!ret) *got_packet = 1;
|
|
if (ret == AVERROR(EAGAIN)) return 0;
|
|
return ret;
|
|
};
|
|
|
|
// return 0 on success, negative on error
|
|
typedef int (*process_packet_cb)(void *ctx, AVPacket *pkt);
|
|
|
|
int encode(AVCodecContext *avctx, AVFrame *frame, process_packet_cb cb, void *priv){
|
|
AVPacket *pkt = av_packet_alloc();
|
|
int ret;
|
|
|
|
ret = avcodec_send_frame(avctx, frame);
|
|
if (ret < 0) goto out;
|
|
|
|
while (!ret) {
|
|
ret = avcodec_receive_packet(avctx, pkt);
|
|
if (!ret) ret = cb(priv, pkt);
|
|
};
|
|
|
|
out: av_packet_free(&pkt);
|
|
if (ret == AVERROR(EAGAIN)) return 0;
|
|
return ret;
|
|
};
|
|
*/
|
|
|
|
|
|
|
|
|
|
bool videoAVLIBDestination::init(void){
|
|
if (!_FFD_REGISTER_ALL_FLAG){
|
|
_FFD_REGISTER_ALL_FLAG = true;
|
|
LOG("Init FFMPEG");
|
|
av_register_all();
|
|
avdevice_register_all();
|
|
avcodec_register_all();
|
|
avformat_network_init();
|
|
};
|
|
m_pVideoState = (VideoState*)av_mallocz(sizeof(VideoState));
|
|
LOG("INIT!!!");
|
|
if (m_pVideoState == NULL){
|
|
LOG_ERROR("VideoState Memory Allocation Error");
|
|
return false;
|
|
};
|
|
m_pVideoState->m_bStatus = MS_UNDEFINED;
|
|
m_pVideoState->m_pVideoCodec = avcodec_find_encoder( VIDEO_OUTPUT_CODEC );
|
|
|
|
if( m_pVideoState->m_pVideoCodec == NULL ){
|
|
LOG_ERROR("VideoCodec Not Found!");
|
|
return false;
|
|
} else {
|
|
LOG("Video Codec Found");
|
|
};
|
|
|
|
m_pVideoState->m_pCodecCtx = avcodec_alloc_context3(m_pVideoState->m_pVideoCodec);
|
|
if (m_pVideoState->m_pCodecCtx == NULL){
|
|
LOG_ERROR("VideoCodec Context Memory Allocation Error");
|
|
return false;
|
|
} else {
|
|
LOG("Codec Context3 Allocated");
|
|
};
|
|
|
|
/*
|
|
m_pVideoState->m_pFormatCtx = avformat_alloc_context();
|
|
if (m_pVideoState->m_pFormatCtx == NULL){
|
|
LOG_ERROR("VideoFormat Context Memory Allocation Error");
|
|
return false;
|
|
} else {
|
|
LOG("Format Context Allocated");
|
|
};
|
|
|
|
//avformat_alloc_output_context2(&flv_format_context, NULL, NULL, output_filename.c_str());
|
|
m_pVideoState->m_pFormatCtx->oformat = av_guess_format( VIDEO_OUTPUT_CONTAINER, NULL, NULL);
|
|
if (m_pVideoState->m_pFormatCtx->oformat == NULL){
|
|
LOG_ERROR("VideoFormat Format Not Guessed!");
|
|
return false;
|
|
} else {
|
|
LOG("Video Container Format Guessed");
|
|
};
|
|
|
|
//av_dict_set(&m_pVideoState->m_pFormatCtx->metadata, "Author", "Gagua", 0);
|
|
//m_pVideoState->m_pFormatCtx->flags |= AVFMT_NOTIMESTAMPS | AVFMT_TS_NONSTRICT;
|
|
*/
|
|
m_pVideoState->m_pFormatCtx = NULL;
|
|
m_pVideoState->m_pFormatOpts = NULL;
|
|
LOG("NULL !!");
|
|
int ret = avformat_alloc_output_context2(&m_pVideoState->m_pFormatCtx, NULL, NULL, "test.flv");
|
|
if (ret < 0) {
|
|
LOG_ERROR("VideoFormat Context Memory Allocation Error");
|
|
return false;
|
|
};
|
|
m_pVideoState->m_pFormatCtx->oformat = av_guess_format(VIDEO_OUTPUT_CONTAINER, NULL, NULL);
|
|
if (m_pVideoState->m_pFormatCtx->oformat == NULL){
|
|
LOG_ERROR("VideoFormat Format Not Guessed!");
|
|
return false;
|
|
} else {
|
|
LOG("Video Container Format Guessed");
|
|
};
|
|
/* */
|
|
AVDictionary *fmt_opts = NULL;
|
|
|
|
m_pVideoState->m_pVideoStr = avformat_new_stream(m_pVideoState->m_pFormatCtx, m_pVideoState->m_pVideoCodec);
|
|
if (m_pVideoState->m_pVideoStr == NULL){
|
|
LOG_ERROR("VideoStream Not Created!");
|
|
}else{
|
|
LOG("VideoStream is Created");
|
|
};
|
|
|
|
// m_pVideoState->m_pVideoStr->codec = m_pVideoState->m_pCodecCtx; //---->see next page
|
|
|
|
|
|
/*
|
|
|
|
// on versions 55+ (win and mac) we use pEncodedFormat = AV_PIX_FMT_YUV420P and need
|
|
// to set color_range and FF_COMPLIANCE_EXPERIMENTAL
|
|
// TODO!!! CHANGE
|
|
#if (LIBAVCODEC_VERSION_INT >= ((55<<16)+(0<<8)+0))
|
|
m_pVideoState->m_pCodecCtx->color_range = AVCOL_RANGE_JPEG;
|
|
// ffmpeg way: to define PIX_FMT_YUVJ420P as deprecated, but
|
|
// allow using of PIX_FMT_YUV420P + AVCOL_RANGE_JPEG only with
|
|
// experimental flag. Well, ok.
|
|
m_pVideoState->m_pCodecCtx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
|
|
#endif
|
|
|
|
m_pVideoState->m_pCodecCtx->bit_rate = 400000;
|
|
|
|
m_pVideoState->m_pCodecCtx->width = m_nWidth;// 352;// resolution must be a multiple of two
|
|
m_pVideoState->m_pCodecCtx->height = m_nHeight;//288;
|
|
|
|
m_pVideoState->m_pCodecCtx->time_base.num = 1; // frames per second
|
|
m_pVideoState->m_pCodecCtx->time_base.den = 25;
|
|
m_pVideoState->m_pCodecCtx->gop_size = 10; // emit one intra frame every ten frames
|
|
m_pVideoState->m_pCodecCtx->max_b_frames = 1;
|
|
m_pVideoState->m_pCodecCtx->pix_fmt = MJPEG_PIX_FMT;// PIX_FMT_YUV420P;
|
|
|
|
m_pVideoState->m_pCodecCtx->qmin = 1;
|
|
m_pVideoState->m_pCodecCtx->qmax = 1;
|
|
m_pVideoState->m_pCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
|
|
// 14.04+ ,win, mac
|
|
#if (LIBAVCODEC_VERSION_MAJOR > 54)
|
|
//m_pVideoState->m_pCodecCtx->time_base = (AVRational){ 1, 1000 };
|
|
m_pVideoState->m_pCodecCtx->codec_id = AV_CODEC_ID_MJPEG;
|
|
m_pVideoState->m_pCodecCtx->codec_tag = av_codec_get_tag( m_pVideoState->m_pFormatCtx->oformat->codec_tag, AV_CODEC_ID_MJPEG );
|
|
#endif
|
|
*/
|
|
m_pVideoState->m_pCodecCtx->codec_id = VIDEO_OUTPUT_CODEC;
|
|
m_pVideoState->m_pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
m_pVideoState->m_pCodecCtx->pix_fmt = VIDEO_OUTPUT_PIX_FMT;
|
|
m_pVideoState->m_pCodecCtx->bit_rate = VIDEO_OUTPUT_BITRATE;
|
|
//m_pVideoState->m_pCodecCtx->bit_rate_tolerance = 1;
|
|
//m_pVideoState->m_pCodecCtx->global_quality = 100;
|
|
//m_pVideoState->m_pCodecCtx->compression_level = 2;
|
|
m_pVideoState->m_pCodecCtx->width = m_nWidth;
|
|
m_pVideoState->m_pCodecCtx->height = m_nHeight;
|
|
m_pVideoState->m_pCodecCtx->time_base.num = 1;
|
|
m_pVideoState->m_pCodecCtx->time_base.den = VIDEO_OUTPUT_FPS;
|
|
m_pVideoState->m_pCodecCtx->gop_size = VIDEO_OUTPUT_GOP; // emit one intra frame every ten frames
|
|
//m_pVideoState->m_pCodecCtx->qmin = 15;
|
|
//m_pVideoState->m_pCodecCtx->qmax = 30;
|
|
m_pVideoState->m_pCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
|
|
#if (LIBAVCODEC_VERSION_MAJOR > 54)
|
|
m_pVideoState->m_pCodecCtx->codec_tag = av_codec_get_tag( m_pVideoState->m_pFormatCtx->oformat->codec_tag, VIDEO_OUTPUT_CODEC );
|
|
#endif
|
|
if( m_pVideoState->m_pCodecCtx->codec_id == AV_CODEC_ID_H264 ){
|
|
//Reference for AvFormatContext options : https://ffmpeg.org/doxygen/2.8/movenc_8c_source.html
|
|
//Set format's privater options, to be passed to avformat_write_header()
|
|
LOG("Begin Options!");
|
|
int RetVal = 0;
|
|
//RetVal = av_dict_set(&m_pVideoState->m_pFormatOpts, "movflags", "faststart", 0);
|
|
//if( RetVal < 0 ){
|
|
// LOG_ERROR( "av_dict_set movflags!");
|
|
//};
|
|
|
|
//default brand is "isom", which fails on some devices
|
|
RetVal = av_dict_set(&m_pVideoState->m_pFormatOpts, "brand", "mp42", 0);
|
|
if( RetVal < 0 ){
|
|
LOG_ERROR( "av_dict_set brand!" );
|
|
};
|
|
//m_pVideoState->m_pCodecCtx->max_b_frames = 1;
|
|
m_pVideoState->m_pCodecCtx->level = 31;
|
|
RetVal = av_opt_set(m_pVideoState->m_pCodecCtx->priv_data, "crf", "12", 0);
|
|
if( RetVal < 0 ){
|
|
LOG_ERROR("av_opt_set crf");
|
|
};
|
|
RetVal = av_opt_set(m_pVideoState->m_pCodecCtx->priv_data, "profile", "main", 0);
|
|
if( RetVal < 0 ){
|
|
LOG_ERROR("av_opt_set profile");
|
|
};
|
|
RetVal = av_opt_set(m_pVideoState->m_pCodecCtx->priv_data, "preset", "slow", 0);
|
|
if( RetVal < 0 ){
|
|
LOG_ERROR("av_opt_set preset");
|
|
};
|
|
// Disable b-pyramid. CLI options for this is "-b-pyramid 0"
|
|
// Because Quicktime (ie. iOS) doesn't support this option
|
|
RetVal = av_opt_set(m_pVideoState->m_pCodecCtx->priv_data, "b-pyramid", "0", 0);
|
|
if( RetVal < 0 ){
|
|
LOG_ERROR("av_opt_set b-pyramid");
|
|
};
|
|
LOG("End Options!");
|
|
};
|
|
|
|
//See up one page ->
|
|
avcodec_get_context_defaults3(m_pVideoState->m_pVideoStr->codec, m_pVideoState->m_pVideoCodec);
|
|
const AVRational dst_fps = { VIDEO_OUTPUT_FPS, 1 };
|
|
m_pVideoState->m_pVideoStr->codec->width = m_nWidth;
|
|
m_pVideoState->m_pVideoStr->codec->height = m_nHeight;
|
|
m_pVideoState->m_pVideoStr->codec->pix_fmt = m_pVideoState->m_pVideoCodec->pix_fmts[0];
|
|
m_pVideoState->m_pVideoStr->codec->time_base = m_pVideoState->m_pVideoStr->time_base = av_inv_q(dst_fps);
|
|
m_pVideoState->m_pVideoStr->r_frame_rate = m_pVideoState->m_pVideoStr->avg_frame_rate = dst_fps;
|
|
|
|
if (m_pVideoState->m_pFormatCtx->oformat->flags & AVFMT_GLOBALHEADER) m_pVideoState->m_pVideoStr->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
|
|
//---->>
|
|
if (avcodec_is_open(m_pVideoState->m_pCodecCtx) == 0) {
|
|
LOG("output codec context is closed, opening");
|
|
int ret = avcodec_open2(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL);
|
|
if (ret < 0) {
|
|
LOG_ERROR("Error opening2 encoder");
|
|
char buffer[200];
|
|
sprintf(buffer, "Error code: %i", ret );
|
|
LOG_ERROR(buffer);
|
|
return false;
|
|
}
|
|
}else{
|
|
LOG("output codec context is opened");
|
|
};
|
|
|
|
m_pVideoState->m_iVideoStreamID = -1;
|
|
#ifndef _NOAUDIO_
|
|
m_pVideoState->m_iAudioStreamID = -1;
|
|
#endif
|
|
m_nOut_bufferSize = 1048576; // 1Mb
|
|
m_pOut_buffer = (uint8_t *)av_malloc(m_nOut_bufferSize);
|
|
if (m_pOut_buffer == NULL){
|
|
LOG_ERROR("Error Memory Allocation Output Buffer.");
|
|
return false;
|
|
};
|
|
m_pVideoState->m_bStatus = MS_ALOCATED; // All seems to be good -> allocating mem
|
|
m_pVideoState->m_bStatus = MS_INIT;
|
|
LOG("Init Done.");
|
|
return true;
|
|
};
|
|
//https://www.ffmpeg.org/doxygen/0.6/output-example_8c-source.html
|
|
//https://github.com/ugcs/vstreamer/blob/master/src/ffmpeg_save_mjpeg.cpp
|
|
|
|
bool videoAVLIBDestination::open(CSTR URI){
|
|
if (m_pVideoState != NULL){
|
|
if (m_pVideoState->m_bStatus < MS_OPENING){
|
|
if (m_pVideoState->m_bStatus == MS_INIT){
|
|
m_pVideoState->m_bStatus = MS_OPENING;
|
|
strncpy(m_pVideoState->m_pFileName, URI, MAX_PATH_LEN);
|
|
m_pVideoState->m_pFileName[MAX_PATH_LEN - 1] = (char)0;
|
|
if (avio_open(&m_pVideoState->m_pFormatCtx->pb, m_pVideoState->m_pFileName, AVIO_FLAG_WRITE) < 0){
|
|
LOG_ERROR("Could not open output file.");
|
|
return false;
|
|
};
|
|
if (avformat_write_header(m_pVideoState->m_pFormatCtx, &m_pVideoState->m_pFormatOpts ) < 0){
|
|
LOG_ERROR("Error occurred when writing header to output file.");
|
|
return false;
|
|
};
|
|
|
|
m_pVideoState->m_bStatus = MS_OPENED;
|
|
m_nDTS = 0;
|
|
m_nPTS = 0;
|
|
LOG("File Is Opened.");
|
|
return true;
|
|
};
|
|
} else {
|
|
LOG_ERROR("Alredy Opened.");
|
|
};
|
|
};
|
|
return false;
|
|
};
|
|
|
|
void videoAVLIBDestination::close(void){
|
|
if( m_pVideoState->m_bStatus > MS_OPENING ){
|
|
av_write_trailer(m_pVideoState->m_pFormatCtx);
|
|
avio_close( m_pVideoState->m_pFormatCtx->pb );
|
|
m_pVideoState->m_bStatus = MS_ALOCATED;
|
|
};
|
|
};
|
|
|
|
static void log_packet( CSTR tBuff, const AVFormatContext *fmt_ctx, const AVPacket *pkt){
|
|
|
|
AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;
|
|
/* sprintf( tBuff, "pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d",
|
|
av_ts_make_string( " ", pkt->pts),
|
|
//av_ts2str(pkt->pts),
|
|
av_ts_make_time_string( " ", pkt->pts, time_base),
|
|
//av_ts2timestr(pkt->pts, time_base),
|
|
av_ts_make_string( " ", pkt->dts),
|
|
//av_ts2str(pkt->dts),
|
|
av_ts_make_time_string( " ", pkt->dts, time_base),
|
|
//av_ts2timestr(pkt->dts, time_base),
|
|
av_ts_make_string( " ", pkt->duration),
|
|
//av_ts2str(pkt->duration),
|
|
av_ts_make_time_string( " ", pkt->duration, time_base),
|
|
//av_ts2timestr(pkt->duration, time_base),
|
|
pkt->stream_index
|
|
);
|
|
*/
|
|
};
|
|
|
|
void videoAVLIBDestination::pushFrame(cv::Mat *m_mFrame, void *src ){
|
|
if (!m_bRecordIsStarted) return;
|
|
if (m_mFrame != NULL) {
|
|
if (img_convert_ctx == NULL) {
|
|
// Allocating Context and sizes at first frame pushed thru the Bus
|
|
img_convert_ctx = sws_getContext(
|
|
m_mFrame->cols,
|
|
m_mFrame->rows,
|
|
AV_PIX_FMT_BGR24,
|
|
m_nWidth,
|
|
m_nHeight,
|
|
VIDEO_OUTPUT_PIX_FMT,
|
|
SWS_BICUBIC,
|
|
NULL,
|
|
NULL,
|
|
NULL
|
|
);
|
|
|
|
if (img_convert_ctx == NULL) {
|
|
m_pVideoState->m_bStatus = MS_STOPED;
|
|
return;
|
|
};
|
|
// Buffer to scale and convert Pictures
|
|
m_pFrame = av_frame_alloc();
|
|
if (m_pFrame == NULL) {
|
|
m_pVideoState->m_bStatus = MS_STOPED;
|
|
return;
|
|
};
|
|
m_pIn_buffer = (uint8_t *)av_malloc(av_image_get_buffer_size(VIDEO_OUTPUT_PIX_FMT, m_nWidth, m_nHeight, BUFFER_ALIGN));
|
|
if (m_pIn_buffer == NULL) {
|
|
m_pVideoState->m_bStatus = MS_STOPED;
|
|
return;
|
|
};
|
|
avpicture_fill((AVPicture *)m_pFrame, m_pIn_buffer, VIDEO_OUTPUT_PIX_FMT, m_nWidth, m_nHeight);
|
|
};
|
|
int linesize[2];
|
|
linesize[0] = m_mFrame->cols*3;
|
|
linesize[1] = 0;
|
|
const uint8_t* dataPTR[2];
|
|
dataPTR[0] = (uint8_t *) m_mFrame->data;
|
|
dataPTR[1] = NULL;
|
|
sws_scale( img_convert_ctx,
|
|
dataPTR,
|
|
linesize,
|
|
0,
|
|
m_mFrame->rows,
|
|
m_pFrame->data,
|
|
m_pFrame->linesize
|
|
);
|
|
int bGotPacket;
|
|
m_nPTS++;
|
|
m_nDTS++;
|
|
m_pFrame->pts = m_nPTS;
|
|
AVPacket mPacket;
|
|
mPacket.data = NULL;
|
|
mPacket.size = 0;
|
|
av_init_packet( &mPacket );
|
|
|
|
m_nOut_Size = avcodec_encode_video2(m_pVideoState->m_pCodecCtx, &mPacket, m_pFrame, &bGotPacket);
|
|
if( bGotPacket ){
|
|
// rescale packet timestamp
|
|
mPacket.duration = 1;
|
|
av_packet_rescale_ts(&mPacket, m_pVideoState->m_pVideoStr->codec->time_base, m_pVideoState->m_pVideoStr->time_base);
|
|
// write packet
|
|
//int ret = av_interleaved_write_frame(m_pVideoState->m_pFormatCtx, &mPacket);
|
|
int ret = av_write_frame(m_pVideoState->m_pFormatCtx, &mPacket);
|
|
if( ret < 0 ) LOG_ERROR( "Trouble with Video Output!" );
|
|
};
|
|
av_free_packet(&mPacket);
|
|
};
|
|
};
|
|
|
|
|
|
/*
|
|
void videoAVLIBDestination::m_fThreadProc(void){
|
|
uint32_t tBufferSize;
|
|
int counter = 500;
|
|
m_bThreadStatus = TH_WORKING;
|
|
LOG("Thread Started.");
|
|
cv::setBreakOnError(true);
|
|
while ( m_pMediaSource == NULL && m_bThreadStatus == TH_WORKING){
|
|
m_pVideoState->m_bStatus = MS_STOPED;
|
|
#ifdef __linux__
|
|
usleep(2000);
|
|
#else
|
|
Sleep(2);
|
|
#endif
|
|
};
|
|
m_pVideoState->m_bStatus = MS_PRESENT;
|
|
m_nOut_Size = 0;
|
|
while (m_bThreadStatus == TH_WORKING){
|
|
if ((counter--) < 0) {
|
|
LOG("Test Done!");
|
|
m_bThreadStatus = TH_ENDED;
|
|
};
|
|
if (m_pMediaSource->getConnectionStatus() == AS_BOTH){
|
|
m_mFrame = m_pMediaInput->getPackage();
|
|
if(m_mFrame != NULL) {
|
|
if( img_convert_ctx == NULL ) {
|
|
// Allocating Context and sizes at first frame pushed thru the Bus
|
|
img_convert_ctx = sws_getContext(m_mFrame->cols, m_mFrame->rows,
|
|
AV_PIX_FMT_BGR24,
|
|
m_nWidth, m_nHeight,
|
|
MJPEG_PIX_FMT,
|
|
SWS_BICUBIC, NULL, NULL, NULL);
|
|
|
|
if (img_convert_ctx == NULL) {
|
|
m_pVideoState->m_bStatus = MS_STOPED;
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
|
|
// Buffer to scale and convert Pictures
|
|
m_pFrame = av_frame_alloc();
|
|
if (m_pFrame == NULL) {
|
|
m_pVideoState->m_bStatus = MS_STOPED;
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
m_pIn_buffer = (uint8_t *)av_malloc(av_image_get_buffer_size(MJPEG_PIX_FMT, m_nWidth, m_nHeight, BUFFER_ALIGN));
|
|
if (m_pIn_buffer == NULL) {
|
|
m_pVideoState->m_bStatus = MS_STOPED;
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
avpicture_fill((AVPicture *)m_pFrame, m_pIn_buffer, MJPEG_PIX_FMT, m_nWidth, m_nHeight);
|
|
|
|
// Temporary Input Buffer
|
|
m_tFrame = av_frame_alloc();
|
|
if (m_tFrame == NULL) {
|
|
m_pVideoState->m_bStatus = MS_STOPED;
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
//tBufferSize = m_mFrame->cols * m_mFrame->rows * m_mFrame->channels();
|
|
//tBufferSize = av_image_get_buffer_size(AV_PIX_FMT_BGR24, m_mFrame->cols, m_mFrame->rows, BUFFER_ALIGN );
|
|
//m_pTemp_buffer = NULL; // (uint8_t *)av_malloc(tBufferSize);
|
|
//memset(m_pTemp_buffer, 0, tBufferSize);
|
|
//if (m_pTemp_buffer == NULL) {
|
|
// m_pVideoState->m_bStatus = MS_STOPED;
|
|
// m_bThreadStatus = TH_ENDED;
|
|
// continue;
|
|
//};
|
|
avpicture_fill((AVPicture *)m_tFrame, NULL, AV_PIX_FMT_BGR24, m_mFrame->cols, m_mFrame->rows);
|
|
};
|
|
//memcpy(m_tFrame->data[0], m_mFrame->data, m_mFrame->step[0] * m_mFrame->rows);
|
|
//sws_scale(img_convert_ctx, (const uint8_t* const*)m_pFrame->data, m_pFrame->linesize, 0, m_mFrame->col*m_mFrame->row, m_pFrame->data, m_pFrame->linesize);
|
|
//int linesize[2];
|
|
//linesize[0] = m_mFrame->cols*3;
|
|
//linesize[1] = 0; (const uint8_t* const*)
|
|
m_tFrame->data[0] = (uint8_t *) m_mFrame->data;
|
|
sws_scale( img_convert_ctx,
|
|
(const uint8_t* const*)m_tFrame->data,
|
|
m_tFrame->linesize,
|
|
0, m_mFrame->rows,
|
|
m_pFrame->data, m_pFrame->linesize );
|
|
|
|
m_tFrame->data[0] = NULL;
|
|
delete(m_mFrame);
|
|
|
|
//Todo: reslove--> m_nOut_Size = avcodec_encode_video2(m_pVideoState->m_pCodecCtx, m_pOut_buffer, m_nOut_bufferSize, m_pFrame);
|
|
|
|
//AVPacket OPacket;
|
|
//pOPacket->data = NULL;
|
|
//av_init_packet(&OPacket);
|
|
//av_packet_from_data( pOPacket, m_pOut_buffer, m_nOut_Size );
|
|
//fwrite(m_pOut_buffer, 1, m_nOut_Size, m_pOutputFile);
|
|
//av_write_frame(m_pVideoState->m_pFormatCtx, pOPacket);
|
|
//av_free_packet( pOPacket );
|
|
if (m_pVideoState->m_pCodecCtx->coded_frame->pts != AV_NOPTS_VALUE)
|
|
m_pPacket->pts = av_rescale_q(m_pVideoState->m_pCodecCtx->coded_frame->pts,
|
|
m_pVideoState->m_pCodecCtx->time_base,
|
|
m_pVideoState->m_pVideoStr->time_base);
|
|
if (m_pVideoState->m_pCodecCtx->coded_frame->key_frame)
|
|
m_pPacket->flags |= AV_PKT_FLAG_KEY;
|
|
m_pPacket->stream_index = m_pVideoState->m_pVideoStr->index;
|
|
m_pPacket->data = m_pOut_buffer;
|
|
m_pPacket->size = m_nOut_Size;
|
|
// write the compressed frame in the media file
|
|
int ret = av_interleaved_write_frame(m_pVideoState->m_pFormatCtx, m_pPacket);
|
|
|
|
} else {
|
|
// get the delayed frames
|
|
//while (m_nOut_Size) {
|
|
// //fflush(stdout);
|
|
// m_nOut_Size = avcodec_encode_video(m_pVideoState->m_pCodecCtx, m_pOut_buffer, m_nOut_bufferSize, NULL);
|
|
// fwrite(m_pOut_buffer, 1, m_nOut_Size, m_pOutputFile);
|
|
//};
|
|
m_pVideoState->m_bStatus = MS_STOPED;
|
|
#ifdef __linux__
|
|
usleep(2000);
|
|
#else
|
|
Sleep(2);
|
|
#endif
|
|
};
|
|
}else{
|
|
#ifdef __linux__
|
|
usleep(20000);
|
|
#else
|
|
Sleep(20);
|
|
#endif
|
|
};
|
|
};
|
|
this->close();
|
|
LOG("Thread Ended.");
|
|
m_bThreadStatus = TH_ENDED;
|
|
};
|
|
*/
|
|
|
|
|
|
/*
|
|
while(av_read_frame(input_container, &decoded_packet)>=0) {
|
|
|
|
if (decoded_packet.stream_index == video_stream_index) {
|
|
len = avcodec_decode_video2(input_stream->codec, decoded_frame, &got_frame, &decoded_packet);
|
|
if(len < 0) {
|
|
exit(1);
|
|
}
|
|
|
|
if(got_frame) {
|
|
av_init_packet(&encoded_packet);
|
|
encoded_packet.data = NULL;
|
|
encoded_packet.size = 0;
|
|
|
|
if(avcodec_encode_video2(output_stream->codec, &encoded_packet, decoded_frame, &got_frame) < 0) {
|
|
exit(1);
|
|
}
|
|
if(got_frame) {
|
|
if (output_stream->codec->coded_frame->key_frame) {
|
|
encoded_packet.flags |= AV_PKT_FLAG_KEY;
|
|
}
|
|
|
|
encoded_packet.stream_index = output_stream->index;
|
|
encoded_packet.pts = av_rescale_q(current_frame_num, output_stream->codec->time_base, output_stream->time_base);
|
|
encoded_packet.dts = av_rescale_q(current_frame_num, output_stream->codec->time_base, output_stream->time_base);
|
|
|
|
if(av_interleaved_write_frame(output_container, &encoded_packet) < 0) {
|
|
exit(1);
|
|
}
|
|
else {
|
|
current_frame_num +=1;
|
|
}
|
|
}
|
|
frame_count+=1;
|
|
av_free_packet(&encoded_packet);
|
|
}
|
|
}
|
|
}
|
|
*/
|
|
|
|
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|
|
|