| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #pragma once
|
|
|
| #include "NvCodecUtils.h"
|
| #include "FFmpegDemuxer.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
| |
|
|
|
|
| typedef enum {
|
| MEDIA_FORMAT_ELEMENTARY = 0,
|
| MEDIA_FORMAT_MOV = 1,
|
| MEDIA_FORMAT_MP4 = 2,
|
| } MEDIA_FORMAT;
|
|
|
| MEDIA_FORMAT GetMediaFormat (char* szOutputFileName)
|
| {
|
| char* extension = strrchr(szOutputFileName, '.');
|
| if (_stricmp(extension, ".mov") == 0)
|
| {
|
| return MEDIA_FORMAT_MOV;
|
| }
|
| else if (_stricmp(extension, ".mp4") == 0)
|
| {
|
| return MEDIA_FORMAT_MP4;
|
| }
|
| else
|
| return MEDIA_FORMAT_ELEMENTARY;
|
| }
|
|
|
| class FFmpegMuxer {
|
| private:
|
| AVFormatContext* inFmtc = NULL;
|
| AVFormatContext* fmtc = NULL;
|
| AVIOContext* avioc = NULL;
|
| AVPacket* packet = NULL;
|
| AVStream* videoStream = NULL;
|
| AVStream* stream = NULL;
|
|
|
| int iVideoStream;
|
| int nWidth, nHeight;
|
| double timeBase = 0.0;
|
| int64_t userTimeScale = 0;
|
|
|
| public:
|
| FFmpegMuxer(const char* szFilePath, MEDIA_FORMAT mediaFormat, AVFormatContext *inFmtc, AVCodecID codecID, int width, int height) {
|
|
|
| if (mediaFormat == MEDIA_FORMAT_MOV)
|
| {
|
| ck(avformat_alloc_output_context2(&fmtc, NULL, "mov", szFilePath));
|
| }
|
| else if (mediaFormat == MEDIA_FORMAT_MP4)
|
| {
|
| ck(avformat_alloc_output_context2(&fmtc, NULL, "mp4", szFilePath));
|
| }
|
|
|
| this->inFmtc = inFmtc;
|
| for (int i = 0; i < (int)inFmtc->nb_streams; i++)
|
| {
|
| if (inFmtc->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
|
| {
|
| stream = avformat_new_stream(fmtc, NULL);
|
| if (!stream) {
|
| LOG(ERROR) << "Stream creation failed";
|
| return;
|
| }
|
|
|
| stream->codecpar->codec_id = codecID;
|
| stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
| stream->codecpar->width = width;
|
| stream->codecpar->height = height;
|
| }
|
| else
|
| {
|
| stream = avformat_new_stream(fmtc, NULL);
|
| if (!stream) {
|
| LOG(ERROR) << "Stream creation failed";
|
| return;
|
| }
|
|
|
| if (avcodec_parameters_copy(stream->codecpar, inFmtc->streams[i]->codecpar) < 0) {
|
| LOG(ERROR) << "Error copying codec parameters";
|
| return;
|
| }
|
| }
|
|
|
| stream->time_base = inFmtc->streams[i]->time_base;
|
|
|
| if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
|
| {
|
| iVideoStream = i;
|
| videoStream = stream;
|
| }
|
| }
|
|
|
|
|
| if (!(fmtc->oformat->flags & AVFMT_NOFILE)) {
|
| if (avio_open(&fmtc->pb, szFilePath, AVIO_FLAG_WRITE) < 0) {
|
| LOG(ERROR) << "Error opening output file" << szFilePath;
|
| return;
|
| }
|
| }
|
|
|
| if (avformat_write_header(fmtc, NULL) < 0) {
|
| LOG(ERROR) << "Error writing header";
|
| return;
|
| }
|
|
|
| packet = av_packet_alloc();
|
| if (!packet) {
|
| LOG(ERROR) << "Error allocating packet";
|
| return;
|
| }
|
| }
|
|
|
| ~FFmpegMuxer() {
|
|
|
| if (!fmtc) {
|
| return;
|
| }
|
|
|
| av_write_trailer(fmtc);
|
|
|
| if (!(fmtc->oformat->flags & AVFMT_NOFILE)) {
|
| avio_closep(&fmtc->pb);
|
| }
|
|
|
| av_packet_unref(packet);
|
| av_packet_free(&packet);
|
|
|
| avformat_free_context(fmtc);
|
|
|
| if (avioc) {
|
| av_freep(&avioc->buffer);
|
| av_freep(&avioc);
|
| }
|
| }
|
|
|
| bool Mux(uint8_t* data, unsigned int size, int64_t pts, int64_t dts, int stream_index, int is_key_frame = 0, int numb = 0) {
|
| if (!fmtc) {
|
| return false;
|
| }
|
|
|
| packet->data = data;
|
| packet->size = size;
|
| packet->stream_index = stream_index;
|
| if (is_key_frame)
|
| packet->flags |= AV_PKT_FLAG_KEY;
|
|
|
| int offset = 0;
|
| if (inFmtc->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
|
| {
|
| AVRational r = { 1, inFmtc->streams[stream_index]->avg_frame_rate.num };
|
| if (inFmtc->streams[stream_index]->avg_frame_rate.num > 0) {
|
| packet->duration = av_rescale_q(1, r, fmtc->streams[stream_index]->time_base);
|
| offset = (int)(numb * inFmtc->streams[stream_index]->time_base.den * inFmtc->streams[stream_index]->avg_frame_rate.den / inFmtc->streams[stream_index]->avg_frame_rate.num);
|
| }
|
| else {
|
| packet->duration = 0;
|
| }
|
| }
|
|
|
| packet->pts = av_rescale_q(pts, inFmtc->streams[stream_index]->time_base, fmtc->streams[stream_index]->time_base) + offset;
|
| packet->dts = av_rescale_q(dts, inFmtc->streams[stream_index]->time_base, fmtc->streams[stream_index]->time_base);
|
|
|
| if (packet->pts < packet->dts) {
|
| packet->pts = packet->dts;
|
| }
|
|
|
| if (av_interleaved_write_frame(fmtc, packet) < 0) {
|
| LOG(ERROR) << "Error writing frame\n";
|
| return false;
|
| }
|
|
|
| return true;
|
| }
|
| };
|
|
|