-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now successfully remuxes input into a file
- Loading branch information
Showing
9 changed files
with
275 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#include "chiton_ffmpeg.hpp" | ||
|
||
void load_ffmpeg(void){ | ||
av_log_set_level(AV_LOG_DEBUG); | ||
//probably should call av_log_set_callback ( void(*)(void *, int, const char *, va_list) callback ) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
#include "stream_writer.hpp" | ||
#include "util.hpp" | ||
#include "chiton_ffmpeg.hpp" | ||
|
||
bool StreamWriter::open(void){ | ||
//need to free input_ctx | ||
/* | ||
AVCodecContext *input_ctx = unwrap.alloc_decode_context(0); | ||
if (!input_ctx){ | ||
return false; | ||
} | ||
*/ | ||
|
||
int error; | ||
|
||
avformat_alloc_output_context2(&output_format_context, NULL, NULL, path.c_str()); | ||
if (!output_format_context) { | ||
LERROR("Could not create output context"); | ||
error = AVERROR_UNKNOWN; | ||
LERROR("Error occurred: " + std::string(av_err2str(error))); | ||
return false; | ||
} | ||
|
||
stream_mapping_size = unwrap.get_stream_count(); | ||
stream_mapping = NULL; | ||
stream_mapping = (int*)av_mallocz_array(stream_mapping_size, sizeof(*stream_mapping)); | ||
if (!stream_mapping) { | ||
error = AVERROR(ENOMEM); | ||
LERROR("Error occurred: " + std::string(av_err2str(error))); | ||
return false; | ||
} | ||
|
||
|
||
AVOutputFormat *ofmt = output_format_context->oformat; | ||
int stream_index = 0; | ||
for (unsigned int i = 0; i < unwrap.get_stream_count(); i++) { | ||
AVStream *out_stream; | ||
AVStream *in_stream = unwrap.get_format_context()->streams[i]; | ||
AVCodecParameters *in_codecpar = in_stream->codecpar; | ||
|
||
if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO && | ||
in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO && | ||
in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) { | ||
stream_mapping[i] = -1; | ||
continue; | ||
} | ||
|
||
stream_mapping[i] = stream_index++; | ||
|
||
out_stream = avformat_new_stream(output_format_context, NULL); | ||
if (!out_stream) { | ||
LERROR("Failed allocating output stream"); | ||
error = AVERROR_UNKNOWN; | ||
LERROR("Error occurred: " + std::string(av_err2str(error))); | ||
return false; | ||
} | ||
|
||
error = avcodec_parameters_copy(out_stream->codecpar, in_codecpar); | ||
if (error < 0) { | ||
LERROR("Failed to copy codec parameters\n"); | ||
LERROR("Error occurred: " + std::string(av_err2str(error))); | ||
return false; | ||
} | ||
out_stream->codecpar->codec_tag = 0; | ||
} | ||
av_dump_format(output_format_context, 0, path.c_str(), 1); | ||
|
||
if (!(ofmt->flags & AVFMT_NOFILE)) { | ||
error = avio_open(&output_format_context->pb, path.c_str(), AVIO_FLAG_WRITE); | ||
if (error < 0) { | ||
LERROR("Could not open output file '" + path + "'"); | ||
LERROR("Error occurred: " + std::string(av_err2str(error))); | ||
return false; | ||
} | ||
} | ||
|
||
error = avformat_write_header(output_format_context, NULL); | ||
if (error < 0) { | ||
LERROR("Error occurred when opening output file"); | ||
LERROR("Error occurred: " + std::string(av_err2str(error))); | ||
return false; | ||
} | ||
return true; | ||
|
||
} | ||
|
||
|
||
StreamWriter::~StreamWriter(){ | ||
/* close output */ | ||
if (output_format_context && !(output_format_context->flags & AVFMT_NOFILE)) | ||
avio_closep(&output_format_context->pb); | ||
avformat_free_context(output_format_context); | ||
|
||
av_freep(&stream_mapping); | ||
} | ||
|
||
void StreamWriter::close(void){ | ||
//flush it... | ||
if (0 > av_interleaved_write_frame(output_format_context, NULL)){ | ||
LERROR("Error flushing muxing output"); | ||
} | ||
|
||
av_write_trailer(output_format_context); | ||
} | ||
|
||
bool StreamWriter::write(const AVPacket &packet){ | ||
AVStream *in_stream, *out_stream; | ||
AVPacket out_pkt; | ||
if (av_packet_ref(&out_pkt, &packet)){ | ||
LERROR("Could not allocate new output packet for writing"); | ||
return false; | ||
} | ||
|
||
in_stream = unwrap.get_format_context()->streams[out_pkt.stream_index]; | ||
if (out_pkt.stream_index >= stream_mapping_size || | ||
stream_mapping[out_pkt.stream_index] < 0) { | ||
av_packet_unref(&out_pkt); | ||
return true;//we processed the stream we don't care about | ||
} | ||
|
||
out_pkt.stream_index = stream_mapping[out_pkt.stream_index]; | ||
out_stream = output_format_context->streams[out_pkt.stream_index]; | ||
|
||
|
||
/* copy packet */ | ||
out_pkt.pts = av_rescale_q_rnd(out_pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX); | ||
out_pkt.dts = av_rescale_q_rnd(out_pkt.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX); | ||
out_pkt.duration = av_rescale_q(out_pkt.duration, in_stream->time_base, out_stream->time_base); | ||
out_pkt.pos = -1; | ||
|
||
|
||
int ret = av_interleaved_write_frame(output_format_context, &out_pkt); | ||
if (ret < 0) { | ||
LERROR("Error muxing packet"); | ||
return false; | ||
} | ||
|
||
av_packet_unref(&out_pkt); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef __STREAM_WRITER_HPP__ | ||
#define __STREAM_WRITER_HPP__ | ||
#include "chiton_config.hpp" | ||
#include "stream_unwrap.hpp" | ||
|
||
class StreamWriter { | ||
public: | ||
StreamWriter(Config cfg, std::string path, StreamUnwrap &unwrap) : cfg(cfg), path(path), unwrap(unwrap) {}; | ||
~StreamWriter(); | ||
|
||
bool open();//open the file for writing, returns true on success | ||
void close(void); | ||
bool write(const AVPacket &pkt);//write the packet to the file | ||
private: | ||
Config &cfg; | ||
std::string path; | ||
StreamUnwrap &unwrap; | ||
|
||
AVFormatContext *output_format_context = NULL; | ||
int stream_mapping_size = 0; | ||
int *stream_mapping = NULL; | ||
|
||
}; | ||
#endif |