2014-09-02 19:10:44 -07:00
|
|
|
/******************************************************************************
|
|
|
|
Copyright (C) 2014 by Ruwen Hahn <palana@stunned.de>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "media-remux.h"
|
|
|
|
|
|
|
|
#include "../util/base.h"
|
|
|
|
#include "../util/bmem.h"
|
|
|
|
#include "../util/platform.h"
|
|
|
|
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2017-10-29 15:50:01 -07:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR >= 58
|
|
|
|
#define CODEC_FLAG_GLOBAL_H AV_CODEC_FLAG_GLOBAL_HEADER
|
|
|
|
#else
|
|
|
|
#define CODEC_FLAG_GLOBAL_H CODEC_FLAG_GLOBAL_HEADER
|
|
|
|
#endif
|
|
|
|
|
2014-09-02 19:10:44 -07:00
|
|
|
struct media_remux_job {
|
|
|
|
int64_t in_size;
|
|
|
|
AVFormatContext *ifmt_ctx, *ofmt_ctx;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void init_size(media_remux_job_t job, const char *in_filename)
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
struct _stat64 st = {0};
|
|
|
|
_stat64(in_filename, &st);
|
|
|
|
#else
|
|
|
|
struct stat st = {0};
|
|
|
|
stat(in_filename, &st);
|
|
|
|
#endif
|
|
|
|
job->in_size = st.st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool init_input(media_remux_job_t job, const char *in_filename)
|
|
|
|
{
|
|
|
|
int ret = avformat_open_input(&job->ifmt_ctx, in_filename, NULL, NULL);
|
|
|
|
if (ret < 0) {
|
|
|
|
blog(LOG_ERROR, "media_remux: Could not open input file '%s'",
|
2019-06-22 22:13:45 -07:00
|
|
|
in_filename);
|
2014-09-02 19:10:44 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = avformat_find_stream_info(job->ifmt_ctx, NULL);
|
|
|
|
if (ret < 0) {
|
|
|
|
blog(LOG_ERROR, "media_remux: Failed to retrieve input stream"
|
|
|
|
" information");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _NDEBUG
|
|
|
|
av_dump_format(job->ifmt_ctx, 0, in_filename, false);
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool init_output(media_remux_job_t job, const char *out_filename)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
avformat_alloc_output_context2(&job->ofmt_ctx, NULL, NULL,
|
2019-06-22 22:13:45 -07:00
|
|
|
out_filename);
|
2014-09-02 19:10:44 -07:00
|
|
|
if (!job->ofmt_ctx) {
|
|
|
|
blog(LOG_ERROR, "media_remux: Could not create output context");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < job->ifmt_ctx->nb_streams; i++) {
|
2019-06-22 22:13:45 -07:00
|
|
|
AVStream *in_stream = job->ifmt_ctx->streams[i];
|
|
|
|
AVStream *out_stream = avformat_new_stream(
|
|
|
|
job->ofmt_ctx, in_stream->codec->codec);
|
2014-09-02 19:10:44 -07:00
|
|
|
if (!out_stream) {
|
|
|
|
blog(LOG_ERROR, "media_remux: Failed to allocate output"
|
|
|
|
" stream");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-06 07:46:49 -08:00
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
|
2017-12-05 13:53:18 -08:00
|
|
|
AVCodecParameters *par = avcodec_parameters_alloc();
|
|
|
|
ret = avcodec_parameters_from_context(par, in_stream->codec);
|
|
|
|
if (ret == 0)
|
|
|
|
ret = avcodec_parameters_to_context(out_stream->codec,
|
2019-06-22 22:13:45 -07:00
|
|
|
par);
|
2017-12-05 13:53:18 -08:00
|
|
|
avcodec_parameters_free(&par);
|
2017-12-06 07:46:49 -08:00
|
|
|
#else
|
|
|
|
ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
|
|
|
|
#endif
|
2017-12-05 13:53:18 -08:00
|
|
|
|
2014-09-02 19:10:44 -07:00
|
|
|
if (ret < 0) {
|
|
|
|
blog(LOG_ERROR, "media_remux: Failed to copy context");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
out_stream->time_base = out_stream->codec->time_base;
|
|
|
|
|
2018-06-02 07:15:13 -07:00
|
|
|
av_dict_copy(&out_stream->metadata, in_stream->metadata, 0);
|
|
|
|
|
2014-09-02 19:10:44 -07:00
|
|
|
out_stream->codec->codec_tag = 0;
|
|
|
|
if (job->ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
|
2017-10-29 15:50:01 -07:00
|
|
|
out_stream->codec->flags |= CODEC_FLAG_GLOBAL_H;
|
2014-09-02 19:10:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _NDEBUG
|
|
|
|
av_dump_format(job->ofmt_ctx, 0, out_filename, true);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!(job->ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
|
|
|
|
ret = avio_open(&job->ofmt_ctx->pb, out_filename,
|
|
|
|
AVIO_FLAG_WRITE);
|
|
|
|
if (ret < 0) {
|
2019-06-22 22:13:45 -07:00
|
|
|
blog(LOG_ERROR,
|
|
|
|
"media_remux: Failed to open output"
|
|
|
|
" file '%s'",
|
|
|
|
out_filename);
|
2014-09-02 19:10:44 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool media_remux_job_create(media_remux_job_t *job, const char *in_filename,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *out_filename)
|
2014-09-02 19:10:44 -07:00
|
|
|
{
|
|
|
|
if (!job)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*job = NULL;
|
|
|
|
if (!os_file_exists(in_filename))
|
|
|
|
return false;
|
|
|
|
|
2018-08-12 08:04:49 -07:00
|
|
|
if (strcmp(in_filename, out_filename) == 0)
|
|
|
|
return false;
|
|
|
|
|
2014-09-02 19:10:44 -07:00
|
|
|
*job = (media_remux_job_t)bzalloc(sizeof(struct media_remux_job));
|
|
|
|
if (!*job)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
init_size(*job, in_filename);
|
|
|
|
|
2019-07-28 18:31:43 -07:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
|
2014-09-02 19:10:44 -07:00
|
|
|
av_register_all();
|
2019-07-28 18:31:43 -07:00
|
|
|
#endif
|
2014-09-02 19:10:44 -07:00
|
|
|
|
|
|
|
if (!init_input(*job, in_filename))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!init_output(*job, out_filename))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
media_remux_job_destroy(*job);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
static inline void process_packet(AVPacket *pkt, AVStream *in_stream,
|
|
|
|
AVStream *out_stream)
|
2014-09-02 19:10:44 -07:00
|
|
|
{
|
|
|
|
pkt->pts = av_rescale_q_rnd(pkt->pts, in_stream->time_base,
|
2019-06-22 22:13:45 -07:00
|
|
|
out_stream->time_base,
|
|
|
|
AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
|
2014-09-02 19:10:44 -07:00
|
|
|
pkt->dts = av_rescale_q_rnd(pkt->dts, in_stream->time_base,
|
2019-06-22 22:13:45 -07:00
|
|
|
out_stream->time_base,
|
|
|
|
AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
|
|
|
|
pkt->duration = (int)av_rescale_q(pkt->duration, in_stream->time_base,
|
|
|
|
out_stream->time_base);
|
2014-09-02 19:10:44 -07:00
|
|
|
pkt->pos = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int process_packets(media_remux_job_t job,
|
2019-06-22 22:13:45 -07:00
|
|
|
media_remux_progress_callback callback,
|
|
|
|
void *data)
|
2014-09-02 19:10:44 -07:00
|
|
|
{
|
|
|
|
AVPacket pkt;
|
|
|
|
|
|
|
|
int ret, throttle = 0;
|
|
|
|
for (;;) {
|
|
|
|
ret = av_read_frame(job->ifmt_ctx, &pkt);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (ret != AVERROR_EOF)
|
2019-06-22 22:13:45 -07:00
|
|
|
blog(LOG_ERROR,
|
|
|
|
"media_remux: Error reading"
|
|
|
|
" packet: %s",
|
|
|
|
av_err2str(ret));
|
2014-09-02 19:10:44 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callback != NULL && throttle++ > 10) {
|
|
|
|
float progress = pkt.pos / (float)job->in_size * 100.f;
|
|
|
|
if (!callback(data, progress))
|
|
|
|
break;
|
|
|
|
throttle = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
process_packet(&pkt, job->ifmt_ctx->streams[pkt.stream_index],
|
2019-06-22 22:13:45 -07:00
|
|
|
job->ofmt_ctx->streams[pkt.stream_index]);
|
2014-09-02 19:10:44 -07:00
|
|
|
|
|
|
|
ret = av_interleaved_write_frame(job->ofmt_ctx, &pkt);
|
2017-12-05 13:53:18 -08:00
|
|
|
av_packet_unref(&pkt);
|
2014-09-02 19:10:44 -07:00
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
blog(LOG_ERROR, "media_remux: Error muxing packet: %s",
|
2019-06-22 22:13:45 -07:00
|
|
|
av_err2str(ret));
|
2014-09-02 19:10:44 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool media_remux_job_process(media_remux_job_t job,
|
2019-06-22 22:13:45 -07:00
|
|
|
media_remux_progress_callback callback, void *data)
|
2014-09-02 19:10:44 -07:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
if (!job)
|
|
|
|
return success;
|
|
|
|
|
|
|
|
ret = avformat_write_header(job->ofmt_ctx, NULL);
|
|
|
|
if (ret < 0) {
|
|
|
|
blog(LOG_ERROR, "media_remux: Error opening output file: %s",
|
2019-06-22 22:13:45 -07:00
|
|
|
av_err2str(ret));
|
2014-09-02 19:10:44 -07:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callback != NULL)
|
|
|
|
callback(data, 0.f);
|
|
|
|
|
|
|
|
ret = process_packets(job, callback, data);
|
|
|
|
success = ret >= 0 || ret == AVERROR_EOF;
|
|
|
|
|
|
|
|
ret = av_write_trailer(job->ofmt_ctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
blog(LOG_ERROR, "media_remux: av_write_trailer: %s",
|
2019-06-22 22:13:45 -07:00
|
|
|
av_err2str(ret));
|
2014-09-02 19:10:44 -07:00
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callback != NULL)
|
|
|
|
callback(data, 100.f);
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void media_remux_job_destroy(media_remux_job_t job)
|
|
|
|
{
|
|
|
|
if (!job)
|
|
|
|
return;
|
|
|
|
|
|
|
|
avformat_close_input(&job->ifmt_ctx);
|
|
|
|
|
|
|
|
if (job->ofmt_ctx && !(job->ofmt_ctx->oformat->flags & AVFMT_NOFILE))
|
|
|
|
avio_close(job->ofmt_ctx->pb);
|
|
|
|
|
|
|
|
avformat_free_context(job->ofmt_ctx);
|
|
|
|
|
|
|
|
bfree(job);
|
|
|
|
}
|