Merge pull request #3185 from jpark37/codec-warnings

FFmpeg deprecation warnings
This commit is contained in:
Jim 2020-07-18 19:34:23 -07:00 committed by GitHub
commit 6a79657a19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 126 additions and 60 deletions

View File

@ -11,9 +11,6 @@ struct ffmpeg_image {
const char *file; const char *file;
AVFormatContext *fmt_ctx; AVFormatContext *fmt_ctx;
AVCodecContext *decoder_ctx; AVCodecContext *decoder_ctx;
AVCodec *decoder;
AVStream *stream;
int stream_idx;
int cx, cy; int cx, cy;
enum AVPixelFormat format; enum AVPixelFormat format;
@ -21,26 +18,46 @@ struct ffmpeg_image {
static bool ffmpeg_image_open_decoder_context(struct ffmpeg_image *info) static bool ffmpeg_image_open_decoder_context(struct ffmpeg_image *info)
{ {
int ret = av_find_best_stream(info->fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, 1, AVFormatContext *const fmt_ctx = info->fmt_ctx;
NULL, 0); int ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, 1, NULL,
0);
if (ret < 0) { if (ret < 0) {
blog(LOG_WARNING, "Couldn't find video stream in file '%s': %s", blog(LOG_WARNING, "Couldn't find video stream in file '%s': %s",
info->file, av_err2str(ret)); info->file, av_err2str(ret));
return false; return false;
} }
info->stream_idx = ret; AVStream *const stream = fmt_ctx->streams[ret];
info->stream = info->fmt_ctx->streams[ret]; #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
info->decoder_ctx = info->stream->codec; AVCodecParameters *const codecpar = stream->codecpar;
info->decoder = avcodec_find_decoder(info->decoder_ctx->codec_id); AVCodec *const decoder = avcodec_find_decoder(codecpar->codec_id);
#else
if (!info->decoder) { AVCodecContext *const decoder_ctx = stream->codec;
AVCodec *const decoder = avcodec_find_decoder(decoder_ctx->codec_id);
#endif
if (!decoder) {
blog(LOG_WARNING, "Failed to find decoder for file '%s'", blog(LOG_WARNING, "Failed to find decoder for file '%s'",
info->file); info->file);
return false; return false;
} }
ret = avcodec_open2(info->decoder_ctx, info->decoder, NULL); #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
AVCodecContext *const decoder_ctx = avcodec_alloc_context3(decoder);
avcodec_parameters_to_context(decoder_ctx, codecpar);
#endif
info->decoder_ctx = decoder_ctx;
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
info->cx = codecpar->width;
info->cy = codecpar->height;
info->format = codecpar->format;
#else
info->cx = decoder_ctx->width;
info->cy = decoder_ctx->height;
info->format = decoder_ctx->pix_fmt;
#endif
ret = avcodec_open2(decoder_ctx, decoder, NULL);
if (ret < 0) { if (ret < 0) {
blog(LOG_WARNING, blog(LOG_WARNING,
"Failed to open video codec for file '%s': " "Failed to open video codec for file '%s': "
@ -54,7 +71,11 @@ static bool ffmpeg_image_open_decoder_context(struct ffmpeg_image *info)
static void ffmpeg_image_free(struct ffmpeg_image *info) static void ffmpeg_image_free(struct ffmpeg_image *info)
{ {
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
avcodec_free_context(&info->decoder_ctx);
#else
avcodec_close(info->decoder_ctx); avcodec_close(info->decoder_ctx);
#endif
avformat_close_input(&info->fmt_ctx); avformat_close_input(&info->fmt_ctx);
} }
@ -67,7 +88,6 @@ static bool ffmpeg_image_init(struct ffmpeg_image *info, const char *file)
memset(info, 0, sizeof(struct ffmpeg_image)); memset(info, 0, sizeof(struct ffmpeg_image));
info->file = file; info->file = file;
info->stream_idx = -1;
ret = avformat_open_input(&info->fmt_ctx, file, NULL, NULL); ret = avformat_open_input(&info->fmt_ctx, file, NULL, NULL);
if (ret < 0) { if (ret < 0) {
@ -88,9 +108,6 @@ static bool ffmpeg_image_init(struct ffmpeg_image *info, const char *file)
if (!ffmpeg_image_open_decoder_context(info)) if (!ffmpeg_image_open_decoder_context(info))
goto fail; goto fail;
info->cx = info->decoder_ctx->width;
info->cy = info->decoder_ctx->height;
info->format = info->decoder_ctx->pix_fmt;
return true; return true;
fail: fail:

View File

@ -65,7 +65,7 @@ static inline bool init_input(media_remux_job_t job, const char *in_filename)
return false; return false;
} }
#ifndef _NDEBUG #ifndef NDEBUG
av_dump_format(job->ifmt_ctx, 0, in_filename, false); av_dump_format(job->ifmt_ctx, 0, in_filename, false);
#endif #endif
return true; return true;
@ -84,8 +84,12 @@ static inline bool init_output(media_remux_job_t job, const char *out_filename)
for (unsigned i = 0; i < job->ifmt_ctx->nb_streams; i++) { for (unsigned i = 0; i < job->ifmt_ctx->nb_streams; i++) {
AVStream *in_stream = job->ifmt_ctx->streams[i]; AVStream *in_stream = job->ifmt_ctx->streams[i];
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
AVStream *out_stream = avformat_new_stream(job->ofmt_ctx, NULL);
#else
AVStream *out_stream = avformat_new_stream( AVStream *out_stream = avformat_new_stream(
job->ofmt_ctx, in_stream->codec->codec); job->ofmt_ctx, in_stream->codec->codec);
#endif
if (!out_stream) { if (!out_stream) {
blog(LOG_ERROR, "media_remux: Failed to allocate output" blog(LOG_ERROR, "media_remux: Failed to allocate output"
" stream"); " stream");
@ -93,30 +97,29 @@ static inline bool init_output(media_remux_job_t job, const char *out_filename)
} }
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101) #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
AVCodecParameters *par = avcodec_parameters_alloc(); ret = avcodec_parameters_copy(out_stream->codecpar,
ret = avcodec_parameters_from_context(par, in_stream->codec); in_stream->codecpar);
if (ret == 0)
ret = avcodec_parameters_to_context(out_stream->codec,
par);
avcodec_parameters_free(&par);
#else #else
ret = avcodec_copy_context(out_stream->codec, in_stream->codec); ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
#endif #endif
if (ret < 0) { if (ret < 0) {
blog(LOG_ERROR, "media_remux: Failed to copy context"); blog(LOG_ERROR,
"media_remux: Failed to copy parameters");
return false; return false;
} }
out_stream->time_base = out_stream->codec->time_base;
av_dict_copy(&out_stream->metadata, in_stream->metadata, 0); av_dict_copy(&out_stream->metadata, in_stream->metadata, 0);
out_stream->codec->codec_tag = 0; out_stream->codec->codec_tag = 0;
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 101)
out_stream->time_base = out_stream->codec->time_base;
if (job->ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) if (job->ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
out_stream->codec->flags |= CODEC_FLAG_GLOBAL_H; out_stream->codec->flags |= CODEC_FLAG_GLOBAL_H;
#endif
} }
#ifndef _NDEBUG #ifndef NDEBUG
av_dump_format(job->ofmt_ctx, 0, out_filename, true); av_dump_format(job->ofmt_ctx, 0, out_filename, true);
#endif #endif

View File

@ -95,10 +95,16 @@ struct header {
int size; int size;
}; };
struct audio_info {
AVStream *stream;
AVCodecContext *ctx;
};
struct ffmpeg_mux { struct ffmpeg_mux {
AVFormatContext *output; AVFormatContext *output;
AVStream *video_stream; AVStream *video_stream;
AVStream **audio_streams; AVCodecContext *video_ctx;
struct audio_info *audio_infos;
struct main_params params; struct main_params params;
struct audio_params *audio; struct audio_params *audio;
struct header video_header; struct header video_header;
@ -116,6 +122,10 @@ static void header_free(struct header *header)
static void free_avformat(struct ffmpeg_mux *ffm) static void free_avformat(struct ffmpeg_mux *ffm)
{ {
if (ffm->output) { if (ffm->output) {
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
avcodec_free_context(&ffm->video_ctx);
#endif
if ((ffm->output->oformat->flags & AVFMT_NOFILE) == 0) if ((ffm->output->oformat->flags & AVFMT_NOFILE) == 0)
avio_close(ffm->output->pb); avio_close(ffm->output->pb);
@ -123,12 +133,16 @@ static void free_avformat(struct ffmpeg_mux *ffm)
ffm->output = NULL; ffm->output = NULL;
} }
if (ffm->audio_streams) { if (ffm->audio_infos) {
free(ffm->audio_streams); #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
for (int i = 0; i < ffm->num_audio_streams; ++i)
avcodec_free_context(&ffm->audio_infos[i].ctx);
#endif
free(ffm->audio_infos);
} }
ffm->video_stream = NULL; ffm->video_stream = NULL;
ffm->audio_streams = NULL; ffm->audio_infos = NULL;
ffm->num_audio_streams = 0; ffm->num_audio_streams = 0;
} }
@ -245,7 +259,7 @@ static bool init_params(int *argc, char ***argv, struct main_params *params,
if (!get_opt_str(argc, argv, &params->acodec, "audio codec")) if (!get_opt_str(argc, argv, &params->acodec, "audio codec"))
return false; return false;
audio = calloc(1, sizeof(*audio) * params->tracks); audio = calloc(params->tracks, sizeof(*audio));
for (int i = 0; i < params->tracks; i++) { for (int i = 0; i < params->tracks; i++) {
if (!get_audio_params(&audio[i], argc, argv)) { if (!get_audio_params(&audio[i], argc, argv)) {
@ -263,25 +277,22 @@ static bool init_params(int *argc, char ***argv, struct main_params *params,
} }
static bool new_stream(struct ffmpeg_mux *ffm, AVStream **stream, static bool new_stream(struct ffmpeg_mux *ffm, AVStream **stream,
const char *name, enum AVCodecID *id) const char *name, AVCodec **codec)
{ {
const AVCodecDescriptor *desc = avcodec_descriptor_get_by_name(name); const AVCodecDescriptor *desc = avcodec_descriptor_get_by_name(name);
AVCodec *codec;
if (!desc) { if (!desc) {
fprintf(stderr, "Couldn't find encoder '%s'\n", name); fprintf(stderr, "Couldn't find encoder '%s'\n", name);
return false; return false;
} }
*id = desc->id; *codec = avcodec_find_encoder(desc->id);
if (!*codec) {
codec = avcodec_find_encoder(desc->id);
if (!codec) {
fprintf(stderr, "Couldn't create encoder"); fprintf(stderr, "Couldn't create encoder");
return false; return false;
} }
*stream = avformat_new_stream(ffm->output, codec); *stream = avformat_new_stream(ffm->output, *codec);
if (!*stream) { if (!*stream) {
fprintf(stderr, "Couldn't create stream for encoder '%s'\n", fprintf(stderr, "Couldn't create stream for encoder '%s'\n",
name); name);
@ -294,11 +305,11 @@ static bool new_stream(struct ffmpeg_mux *ffm, AVStream **stream,
static void create_video_stream(struct ffmpeg_mux *ffm) static void create_video_stream(struct ffmpeg_mux *ffm)
{ {
AVCodec *codec;
AVCodecContext *context; AVCodecContext *context;
void *extradata = NULL; void *extradata = NULL;
if (!new_stream(ffm, &ffm->video_stream, ffm->params.vcodec, if (!new_stream(ffm, &ffm->video_stream, ffm->params.vcodec, &codec))
&ffm->output->oformat->video_codec))
return; return;
if (ffm->video_header.size) { if (ffm->video_header.size) {
@ -306,8 +317,12 @@ static void create_video_stream(struct ffmpeg_mux *ffm)
ffm->video_header.size); ffm->video_header.size);
} }
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
context = avcodec_alloc_context3(codec);
#else
context = ffm->video_stream->codec; context = ffm->video_stream->codec;
context->bit_rate = ffm->params.vbitrate * 1000; #endif
context->bit_rate = (int64_t)ffm->params.vbitrate * 1000;
context->width = ffm->params.width; context->width = ffm->params.width;
context->height = ffm->params.height; context->height = ffm->params.height;
context->coded_width = ffm->params.width; context->coded_width = ffm->params.width;
@ -322,20 +337,24 @@ static void create_video_stream(struct ffmpeg_mux *ffm)
if (ffm->output->oformat->flags & AVFMT_GLOBALHEADER) if (ffm->output->oformat->flags & AVFMT_GLOBALHEADER)
context->flags |= CODEC_FLAG_GLOBAL_H; context->flags |= CODEC_FLAG_GLOBAL_H;
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
avcodec_parameters_from_context(ffm->video_stream->codecpar, context);
#endif
ffm->video_ctx = context;
} }
static void create_audio_stream(struct ffmpeg_mux *ffm, int idx) static void create_audio_stream(struct ffmpeg_mux *ffm, int idx)
{ {
AVCodec *codec;
AVCodecContext *context; AVCodecContext *context;
AVStream *stream; AVStream *stream;
void *extradata = NULL; void *extradata = NULL;
if (!new_stream(ffm, &stream, ffm->params.acodec, if (!new_stream(ffm, &stream, ffm->params.acodec, &codec))
&ffm->output->oformat->audio_codec))
return; return;
ffm->audio_streams[idx] = stream;
av_dict_set(&stream->metadata, "title", ffm->audio[idx].name, 0); av_dict_set(&stream->metadata, "title", ffm->audio[idx].name, 0);
stream->time_base = (AVRational){1, ffm->audio[idx].sample_rate}; stream->time_base = (AVRational){1, ffm->audio[idx].sample_rate};
@ -345,8 +364,12 @@ static void create_audio_stream(struct ffmpeg_mux *ffm, int idx)
ffm->audio_header[idx].size); ffm->audio_header[idx].size);
} }
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
context = avcodec_alloc_context3(codec);
#else
context = stream->codec; context = stream->codec;
context->bit_rate = ffm->audio[idx].abitrate * 1000; #endif
context->bit_rate = (int64_t)ffm->audio[idx].abitrate * 1000;
context->channels = ffm->audio[idx].channels; context->channels = ffm->audio[idx].channels;
context->sample_rate = ffm->audio[idx].sample_rate; context->sample_rate = ffm->audio[idx].sample_rate;
context->sample_fmt = AV_SAMPLE_FMT_S16; context->sample_fmt = AV_SAMPLE_FMT_S16;
@ -364,6 +387,12 @@ static void create_audio_stream(struct ffmpeg_mux *ffm, int idx)
if (ffm->output->oformat->flags & AVFMT_GLOBALHEADER) if (ffm->output->oformat->flags & AVFMT_GLOBALHEADER)
context->flags |= CODEC_FLAG_GLOBAL_H; context->flags |= CODEC_FLAG_GLOBAL_H;
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
avcodec_parameters_from_context(stream->codecpar, context);
#endif
ffm->audio_infos[ffm->num_audio_streams].stream = stream;
ffm->audio_infos[ffm->num_audio_streams].ctx = context;
ffm->num_audio_streams++; ffm->num_audio_streams++;
} }
@ -373,8 +402,8 @@ static bool init_streams(struct ffmpeg_mux *ffm)
create_video_stream(ffm); create_video_stream(ffm);
if (ffm->params.tracks) { if (ffm->params.tracks) {
ffm->audio_streams = ffm->audio_infos =
calloc(1, ffm->params.tracks * sizeof(void *)); calloc(ffm->params.tracks, sizeof(*ffm->audio_infos));
for (int i = 0; i < ffm->params.tracks; i++) for (int i = 0; i < ffm->params.tracks; i++)
create_audio_stream(ffm, i); create_audio_stream(ffm, i);
@ -477,10 +506,6 @@ static inline int open_output_file(struct ffmpeg_mux *ffm)
} }
} }
strncpy(ffm->output->filename, ffm->params.file,
sizeof(ffm->output->filename));
ffm->output->filename[sizeof(ffm->output->filename) - 1] = 0;
AVDictionary *dict = NULL; AVDictionary *dict = NULL;
if ((ret = av_dict_parse_string(&dict, ffm->params.muxer_settings, "=", if ((ret = av_dict_parse_string(&dict, ffm->params.muxer_settings, "=",
" ", 0))) { " ", 0))) {
@ -544,7 +569,7 @@ static int ffmpeg_mux_init_context(struct ffmpeg_mux *ffm)
} }
ret = avformat_alloc_output_context2(&ffm->output, output_format, NULL, ret = avformat_alloc_output_context2(&ffm->output, output_format, NULL,
NULL); ffm->params.file);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "Couldn't initialize output context: %s\n", fprintf(stderr, "Couldn't initialize output context: %s\n",
av_err2str(ret)); av_err2str(ret));
@ -578,7 +603,7 @@ static int ffmpeg_mux_init_internal(struct ffmpeg_mux *ffm, int argc,
if (ffm->params.tracks) { if (ffm->params.tracks) {
ffm->audio_header = ffm->audio_header =
calloc(1, sizeof(struct header) * ffm->params.tracks); calloc(ffm->params.tracks, sizeof(*ffm->audio_header));
} }
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100) #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
@ -614,24 +639,42 @@ static inline int get_index(struct ffmpeg_mux *ffm,
} }
} else { } else {
if ((int)info->index < ffm->num_audio_streams) { if ((int)info->index < ffm->num_audio_streams) {
return ffm->audio_streams[info->index]->id; return ffm->audio_infos[info->index].stream->id;
} }
} }
return -1; return -1;
} }
static AVCodecContext *get_codec_context(struct ffmpeg_mux *ffm,
struct ffm_packet_info *info)
{
if (info->type == FFM_PACKET_VIDEO) {
if (ffm->video_stream) {
return ffm->video_ctx;
}
} else {
if ((int)info->index < ffm->num_audio_streams) {
return ffm->audio_infos[info->index].ctx;
}
}
return NULL;
}
static inline AVStream *get_stream(struct ffmpeg_mux *ffm, int idx) static inline AVStream *get_stream(struct ffmpeg_mux *ffm, int idx)
{ {
return ffm->output->streams[idx]; return ffm->output->streams[idx];
} }
static inline int64_t rescale_ts(struct ffmpeg_mux *ffm, int64_t val, int idx) static inline int64_t rescale_ts(struct ffmpeg_mux *ffm,
AVRational codec_time_base, int64_t val,
int idx)
{ {
AVStream *stream = get_stream(ffm, idx); AVStream *stream = get_stream(ffm, idx);
return av_rescale_q_rnd(val / stream->codec->time_base.num, return av_rescale_q_rnd(val / codec_time_base.num, codec_time_base,
stream->codec->time_base, stream->time_base, stream->time_base,
AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX); AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
} }
@ -646,13 +689,16 @@ static inline bool ffmpeg_mux_packet(struct ffmpeg_mux *ffm, uint8_t *buf,
return true; return true;
} }
const AVRational codec_time_base =
get_codec_context(ffm, info)->time_base;
av_init_packet(&packet); av_init_packet(&packet);
packet.data = buf; packet.data = buf;
packet.size = (int)info->size; packet.size = (int)info->size;
packet.stream_index = idx; packet.stream_index = idx;
packet.pts = rescale_ts(ffm, info->pts, idx); packet.pts = rescale_ts(ffm, codec_time_base, info->pts, idx);
packet.dts = rescale_ts(ffm, info->dts, idx); packet.dts = rescale_ts(ffm, codec_time_base, info->dts, idx);
if (info->keyframe) if (info->keyframe)
packet.flags = AV_PKT_FLAG_KEY; packet.flags = AV_PKT_FLAG_KEY;