1 diff --git a/src/AVCompat.cpp b/src/AVCompat.cpp
2 index befb3cd9..cf113f03 100644
5 @@ -391,7 +391,7 @@ const char *get_codec_long_name(enum AVCodecID id)
8 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
9 - AVCodec *codec = avcodec_find_decoder(id);
10 + const AVCodec *codec = avcodec_find_decoder(id);
12 return codec->long_name;
13 codec = avcodec_find_encoder(id);
14 diff --git a/src/AVDemuxer.cpp b/src/AVDemuxer.cpp
15 index 432c4f04..5b46afd3 100644
16 --- a/src/AVDemuxer.cpp
17 +++ b/src/AVDemuxer.cpp
18 @@ -290,7 +290,7 @@ public:
19 //copy the info, not parse the file when constructed, then need member vars
22 - AVInputFormat *input_format;
23 + const AVInputFormat *input_format;
24 QString format_forced;
27 @@ -310,7 +310,11 @@ public:
28 // wanted_stream is REQUIRED. e.g. always set -1 to indicate the default stream, -2 to disable
29 int stream, wanted_stream; // -1 default, selected by ff
30 int index, wanted_index; // index in a kind of streams
31 +#if LIBAVCODEC_VERSION_MAJOR < 59
32 AVCodecContext *avctx;
34 + AVCodecParameters *avctx;
37 StreamInfo astream, vstream, sstream;
39 @@ -614,12 +618,14 @@ bool AVDemuxer::seek(qint64 pos)
40 if (upos <= startTime()) {
41 qDebug("************seek to beginning. started = false");
42 d->started = false; //???
43 +#if LIBAVCODEC_VERSION_MAJOR < 59
45 d->astream.avctx->frame_number = 0;
47 d->vstream.avctx->frame_number = 0; //TODO: why frame_number not changed after seek?
49 d->sstream.avctx->frame_number = 0;
54 @@ -1062,37 +1068,61 @@ QList<int> AVDemuxer::subtitleStreams() const
55 return d->subtitle_streams;
58 +#if LIBAVCODEC_VERSION_MAJOR < 59
59 AVCodecContext* AVDemuxer::audioCodecContext(int stream) const
61 +AVCodecParameters* AVDemuxer::audioCodecContext(int stream) const
65 return d->astream.avctx;
66 if (stream > (int)d->format_ctx->nb_streams)
68 +#if LIBAVCODEC_VERSION_MAJOR < 59
69 AVCodecContext *avctx = d->format_ctx->streams[stream]->codec;
71 + AVCodecParameters *avctx = d->format_ctx->streams[stream]->codecpar;
73 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO)
78 +#if LIBAVCODEC_VERSION_MAJOR < 59
79 AVCodecContext* AVDemuxer::videoCodecContext(int stream) const
81 +AVCodecParameters* AVDemuxer::videoCodecContext(int stream) const
85 return d->vstream.avctx;
86 if (stream > (int)d->format_ctx->nb_streams)
88 +#if LIBAVCODEC_VERSION_MAJOR < 59
89 AVCodecContext *avctx = d->format_ctx->streams[stream]->codec;
91 + AVCodecParameters *avctx = d->format_ctx->streams[stream]->codecpar;
93 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO)
98 +#if LIBAVCODEC_VERSION_MAJOR < 59
99 AVCodecContext* AVDemuxer::subtitleCodecContext(int stream) const
101 +AVCodecParameters* AVDemuxer::subtitleCodecContext(int stream) const
105 return d->sstream.avctx;
106 if (stream > (int)d->format_ctx->nb_streams)
108 +#if LIBAVCODEC_VERSION_MAJOR < 59
109 AVCodecContext *avctx = d->format_ctx->streams[stream]->codec;
111 + AVCodecParameters *avctx = d->format_ctx->streams[stream]->codecpar;
113 if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE)
116 @@ -1289,7 +1319,11 @@ bool AVDemuxer::Private::setStream(AVDemuxer::StreamType st, int streamValue)
117 // don't touch wanted index
119 si->wanted_stream = streamValue;
120 +#if LIBAVCODEC_VERSION_MAJOR < 59
121 si->avctx = format_ctx->streams[s]->codec;
123 + si->avctx = format_ctx->streams[s]->codecpar;
125 has_attached_pic = !!(format_ctx->streams[s]->disposition & AV_DISPOSITION_ATTACHED_PIC);
128 @@ -1302,7 +1336,11 @@ bool AVDemuxer::Private::prepareStreams()
130 AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
131 for (unsigned int i = 0; i < format_ctx->nb_streams; ++i) {
132 +#if LIBAVCODEC_VERSION_MAJOR < 59
133 type = format_ctx->streams[i]->codec->codec_type;
135 + type = format_ctx->streams[i]->codecpar->codec_type;
137 if (type == AVMEDIA_TYPE_VIDEO) {
138 video_streams.push_back(i);
139 } else if (type == AVMEDIA_TYPE_AUDIO) {
140 diff --git a/src/AVMuxer.cpp b/src/AVMuxer.cpp
141 index b601afd8..4ff1463e 100644
142 --- a/src/AVMuxer.cpp
143 +++ b/src/AVMuxer.cpp
144 @@ -81,7 +81,7 @@ public:
145 //copy the info, not parse the file when constructed, then need member vars
148 - AVOutputFormat *format;
149 + const AVOutputFormat *format;
150 QString format_forced;
153 @@ -94,7 +94,7 @@ public:
155 AVStream *AVMuxer::Private::addStream(AVFormatContext* ctx, const QString &codecName, AVCodecID codecId)
157 - AVCodec *codec = NULL;
158 + const AVCodec *codec = NULL;
159 if (!codecName.isEmpty()) {
160 codec = avcodec_find_encoder_by_name(codecName.toUtf8().constData());
162 @@ -120,7 +120,8 @@ AVStream *AVMuxer::Private::addStream(AVFormatContext* ctx, const QString &codec
163 // set by avformat if unset
164 s->id = ctx->nb_streams - 1;
166 - AVCodecContext *c = s->codec;
167 +#if LIBAVCODEC_VERSION_MAJOR < 59
168 + AVCodec *c = s->codec;
169 c->codec_id = codec->id;
170 // Using codec->time_base is deprecated, but needed for older lavf.
171 c->time_base = s->time_base;
172 @@ -129,6 +130,7 @@ AVStream *AVMuxer::Private::addStream(AVFormatContext* ctx, const QString &codec
173 c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
174 // expose avctx to encoder and set properties in encoder?
175 // list codecs for a given format in ui
180 @@ -137,16 +139,24 @@ bool AVMuxer::Private::prepareStreams()
181 audio_streams.clear();
182 video_streams.clear();
183 subtitle_streams.clear();
184 - AVOutputFormat* fmt = format_ctx->oformat;
185 + const AVOutputFormat* fmt = format_ctx->oformat;
187 AVStream *s = addStream(format_ctx, venc->codecName(), fmt->video_codec);
189 +#if LIBAVCODEC_VERSION_MAJOR < 59
190 AVCodecContext *c = s->codec;
192 + AVCodecParameters *c = s->codecpar;
194 c->bit_rate = venc->bitRate();
195 c->width = venc->width();
196 c->height = venc->height();
197 /// MUST set after encoder is open to ensure format is valid and the same
198 +#if LIBAVCODEC_VERSION_MAJOR < 59
199 c->pix_fmt = (AVPixelFormat)VideoFormat::pixelFormatToFFmpeg(venc->pixelFormat());
201 + c->format = (AVPixelFormat)VideoFormat::pixelFormatToFFmpeg(venc->pixelFormat());
204 // Set avg_frame_rate based on encoder frame_rate
205 s->avg_frame_rate = av_d2q(venc->frameRate(), venc->frameRate()*1001.0+2);
206 @@ -157,11 +167,19 @@ bool AVMuxer::Private::prepareStreams()
208 AVStream *s = addStream(format_ctx, aenc->codecName(), fmt->audio_codec);
210 +#if LIBAVCODEC_VERSION_MAJOR < 59
211 AVCodecContext *c = s->codec;
213 + AVCodecParameters *c = s->codecpar;
215 c->bit_rate = aenc->bitRate();
216 /// MUST set after encoder is open to ensure format is valid and the same
217 c->sample_rate = aenc->audioFormat().sampleRate();
218 +#if LIBAVCODEC_VERSION_MAJOR < 59
219 c->sample_fmt = (AVSampleFormat)aenc->audioFormat().sampleFormatFFmpeg();
221 + c->format = (AVSampleFormat)aenc->audioFormat().sampleFormatFFmpeg();
223 c->channel_layout = aenc->audioFormat().channelLayoutFFmpeg();
224 c->channels = aenc->audioFormat().channels();
225 c->bits_per_raw_sample = aenc->audioFormat().bytesPerSample()*8; // need??
226 diff --git a/src/AVPlayerPrivate.cpp b/src/AVPlayerPrivate.cpp
227 index 84f079ef..d3bf09f1 100644
228 --- a/src/AVPlayerPrivate.cpp
229 +++ b/src/AVPlayerPrivate.cpp
230 @@ -55,7 +55,11 @@ int computeNotifyPrecision(qint64 duration, qreal fps)
232 } // namespace Internal
234 +#if LIBAVCODEC_VERSION_MAJOR < 59
235 static bool correct_audio_channels(AVCodecContext *ctx) {
237 +static bool correct_audio_channels(AVCodecParameters *ctx) {
239 if (ctx->channels <= 0) {
240 if (ctx->channel_layout) {
241 ctx->channels = av_get_channel_layout_nb_channels(ctx->channel_layout);
242 @@ -251,7 +255,11 @@ void AVPlayer::Private::initBaseStatistics()
243 updateNotifyInterval();
246 +#if LIBAVCODEC_VERSION_MAJOR < 59
247 void AVPlayer::Private::initCommonStatistics(int s, Statistics::Common *st, AVCodecContext *avctx)
249 +void AVPlayer::Private::initCommonStatistics(int s, Statistics::Common *st, AVCodecParameters *avctx)
252 AVFormatContext *fmt_ctx = demuxer.formatContext();
254 @@ -288,7 +296,11 @@ void AVPlayer::Private::initCommonStatistics(int s, Statistics::Common *st, AVCo
256 void AVPlayer::Private::initAudioStatistics(int s)
258 +#if LIBAVCODEC_VERSION_MAJOR < 59
259 AVCodecContext *avctx = demuxer.audioCodecContext();
261 + AVCodecParameters *avctx = demuxer.audioCodecContext();
263 statistics.audio = Statistics::Common();
264 statistics.audio_only = Statistics::AudioOnly();
266 @@ -306,14 +318,22 @@ void AVPlayer::Private::initAudioStatistics(int s)
267 // nb_channels -1: will use av_get_channel_layout_nb_channels
268 av_get_channel_layout_string(cl, sizeof(cl), avctx->channels, avctx->channel_layout);
269 statistics.audio_only.channel_layout = QLatin1String(cl);
270 +#if LIBAVCODEC_VERSION_MAJOR < 59
271 statistics.audio_only.sample_fmt = QLatin1String(av_get_sample_fmt_name(avctx->sample_fmt));
273 + statistics.audio_only.sample_fmt = QLatin1String(av_get_sample_fmt_name(static_cast<AVSampleFormat>(avctx->format)));
275 statistics.audio_only.frame_size = avctx->frame_size;
276 statistics.audio_only.sample_rate = avctx->sample_rate;
279 void AVPlayer::Private::initVideoStatistics(int s)
281 +#if LIBAVCODEC_VERSION_MAJOR < 59
282 AVCodecContext *avctx = demuxer.videoCodecContext();
284 + AVCodecParameters *avctx = demuxer.videoCodecContext();
286 statistics.video = Statistics::Common();
287 statistics.video_only = Statistics::VideoOnly();
289 @@ -324,10 +344,20 @@ void AVPlayer::Private::initVideoStatistics(int s)
290 statistics.video.decoder = vdec->name();
291 statistics.video.decoder_detail = vdec->description();
293 +#if LIBAVCODEC_VERSION_MAJOR < 59
294 statistics.video_only.coded_height = avctx->coded_height;
295 statistics.video_only.coded_width = avctx->coded_width;
296 statistics.video_only.gop_size = avctx->gop_size;
297 statistics.video_only.pix_fmt = QLatin1String(av_get_pix_fmt_name(avctx->pix_fmt));
299 + // FIXME we can't really get coded_height, coded_width and gop_size from Parameters
300 + // At some point we should make an effort to get the real codec context; in the mean
301 + // time, this should be close enough...
302 + statistics.video_only.coded_height = avctx->height;
303 + statistics.video_only.coded_width = avctx->width;
304 + statistics.video_only.gop_size = 0;
305 + statistics.video_only.pix_fmt = QLatin1String(av_get_pix_fmt_name(static_cast<AVPixelFormat>(avctx->format)));
307 statistics.video_only.height = avctx->height;
308 statistics.video_only.width = avctx->width;
309 statistics.video_only.rotate = 0;
310 @@ -354,7 +384,11 @@ bool AVPlayer::Private::setupAudioThread(AVPlayer *player)
311 athread->setDecoder(0);
312 athread->setOutput(0);
314 +#if LIBAVCODEC_VERSION_MAJOR < 59
315 AVCodecContext *avctx = ademuxer->audioCodecContext();
317 + AVCodecParameters *avctx = ademuxer->audioCodecContext();
320 // TODO: close ao? //TODO: check pulseaudio perapp control if closed
322 @@ -384,7 +418,11 @@ bool AVPlayer::Private::setupAudioThread(AVPlayer *player)
323 correct_audio_channels(avctx);
325 af.setSampleRate(avctx->sample_rate);
326 +#if LIBAVCODEC_VERSION_MAJOR < 59
327 af.setSampleFormatFFmpeg(avctx->sample_fmt);
329 + af.setSampleFormatFFmpeg(avctx->format);
331 af.setChannelLayoutFFmpeg(avctx->channel_layout);
333 qWarning("invalid audio format. audio stream will be disabled");
334 @@ -466,7 +504,11 @@ QVariantList AVPlayer::Private::getTracksInfo(AVDemuxer *demuxer, AVDemuxer::Str
335 t[QStringLiteral("stream_index")] = QVariant(s);
337 AVStream *stream = demuxer->formatContext()->streams[s];
338 +#if LIBAVCODEC_VERSION_MAJOR < 59
339 AVCodecContext *ctx = stream->codec;
341 + AVCodecParameters *ctx = stream->codecpar;
344 t[QStringLiteral("codec")] = QByteArray(avcodec_descriptor_get(ctx->codec_id)->name);
346 @@ -494,7 +536,11 @@ bool AVPlayer::Private::applySubtitleStream(int n, AVPlayer *player)
348 if (!demuxer.setStreamIndex(AVDemuxer::SubtitleStream, n))
350 +#if LIBAVCODEC_VERSION_MAJOR < 59
351 AVCodecContext *ctx = demuxer.subtitleCodecContext();
353 + AVCodecParameters *ctx = demuxer.subtitleCodecContext();
357 // FIXME: AVCodecDescriptor.name and AVCodec.name are different!
358 @@ -512,7 +558,11 @@ bool AVPlayer::Private::tryApplyDecoderPriority(AVPlayer *player)
359 // TODO: add an option to apply the new decoder even if not available
360 qint64 pos = player->position();
361 VideoDecoder *vd = NULL;
362 +#if LIBAVCODEC_VERSION_MAJOR < 59
363 AVCodecContext *avctx = demuxer.videoCodecContext();
365 + AVCodecParameters *avctx = demuxer.videoCodecContext();
367 foreach(VideoDecoderId vid, vc_ids) {
368 qDebug("**********trying video decoder: %s...", VideoDecoder::name(vid));
369 vd = VideoDecoder::create(vid);
370 @@ -560,7 +610,11 @@ bool AVPlayer::Private::setupVideoThread(AVPlayer *player)
371 vthread->packetQueue()->clear();
372 vthread->setDecoder(0);
374 +#if LIBAVCODEC_VERSION_MAJOR < 59
375 AVCodecContext *avctx = demuxer.videoCodecContext();
377 + AVCodecParameters *avctx = demuxer.videoCodecContext();
382 diff --git a/src/AVPlayerPrivate.h b/src/AVPlayerPrivate.h
383 index e404b9bf..f0f90fd1 100644
384 --- a/src/AVPlayerPrivate.h
385 +++ b/src/AVPlayerPrivate.h
387 #include "AVDemuxThread.h"
388 #include "utils/Logger.h"
391 +#include <libavcodec/avcodec.h>
396 static const qint64 kInvalidPosition = std::numeric_limits<qint64>::max();
397 @@ -43,7 +47,11 @@ public:
398 void applyFrameRate();
399 void initStatistics();
400 void initBaseStatistics();
401 +#if LIBAVCODEC_VERSION_MAJOR < 59
402 void initCommonStatistics(int s, Statistics::Common* st, AVCodecContext* avctx);
404 + void initCommonStatistics(int s, Statistics::Common* st, AVCodecParameters* avctx);
406 void initAudioStatistics(int s);
407 void initVideoStatistics(int s);
408 void initSubtitleStatistics(int s);
409 diff --git a/src/QtAV/AVDemuxer.h b/src/QtAV/AVDemuxer.h
410 index 3b720f5d..688c82f9 100644
411 --- a/src/QtAV/AVDemuxer.h
412 +++ b/src/QtAV/AVDemuxer.h
414 #include <QtCore/QObject>
415 #include <QtCore/QScopedPointer>
418 +#include <libavcodec/avcodec.h>
421 struct AVFormatContext;
422 struct AVCodecContext;
424 @@ -151,9 +155,15 @@ public:
425 int subtitleStream() const;
426 QList<int> subtitleStreams() const;
427 //codec. stream < 0: the stream going to play (or the stream set by setStreamIndex())
428 +#if LIBAVCODEC_VERSION_MAJOR < 59
429 AVCodecContext* audioCodecContext(int stream = -1) const;
430 AVCodecContext* videoCodecContext(int stream = -1) const;
431 AVCodecContext* subtitleCodecContext(int stream = -1) const;
433 + AVCodecParameters* audioCodecContext(int stream = -1) const;
434 + AVCodecParameters* videoCodecContext(int stream = -1) const;
435 + AVCodecParameters* subtitleCodecContext(int stream = -1) const;
438 * @brief getInterruptTimeout return the interrupt timeout
440 diff --git a/src/QtAV/private/AVDecoder_p.h b/src/QtAV/private/AVDecoder_p.h
441 index 2382974e..5e952ea8 100644
442 --- a/src/QtAV/private/AVDecoder_p.h
443 +++ b/src/QtAV/private/AVDecoder_p.h
444 @@ -78,7 +78,7 @@ class Q_AV_PRIVATE_EXPORT AVDecoderPrivate : public DPtrPrivate<AVDecoder>
447 static const char* getProfileName(AVCodecID id, int profile) {
448 - AVCodec *c = avcodec_find_decoder(id);
449 + const AVCodec *c = avcodec_find_decoder(id);
452 return av_get_profile_name(c, profile);
453 diff --git a/src/VideoFormat.cpp b/src/VideoFormat.cpp
454 index b9c7b4f0..5e7901f1 100644
455 --- a/src/VideoFormat.cpp
456 +++ b/src/VideoFormat.cpp
457 @@ -702,7 +702,11 @@ bool VideoFormat::hasPalette() const
459 bool VideoFormat::isPseudoPaletted() const
461 +#if LIBAVCODEC_VERSION_MAJOR < 59
462 return (d->flags() & AV_PIX_FMT_FLAG_PSEUDOPAL) == AV_PIX_FMT_FLAG_PSEUDOPAL;
464 + return hasPalette();
468 bool VideoFormat::isBitStream() const
469 diff --git a/src/VideoFrameExtractor.cpp b/src/VideoFrameExtractor.cpp
470 index 8e4a843e..ecaa079e 100644
471 --- a/src/VideoFrameExtractor.cpp
472 +++ b/src/VideoFrameExtractor.cpp
473 @@ -192,7 +192,11 @@ public:
477 +#if LIBAVCODEC_VERSION_MAJOR < 59
478 AVCodecContext *cctx = demuxer.videoCodecContext();
480 + AVCodecParameters *cctx = demuxer.videoCodecContext();
482 if (cctx) decoder->setCodecContext(demuxer.videoCodecContext());
483 if (!cctx || !decoder->open()) {
485 diff --git a/src/codec/AVDecoder.cpp b/src/codec/AVDecoder.cpp
486 index 440504d0..bc9cc278 100644
487 --- a/src/codec/AVDecoder.cpp
488 +++ b/src/codec/AVDecoder.cpp
493 -static AVCodec* get_codec(const QString &name, const QString& hwa, AVCodecID cid)
494 +static const AVCodec* get_codec(const QString &name, const QString& hwa, AVCodecID cid)
496 QString fullname(name);
497 if (name.isEmpty()) {
498 @@ -35,7 +35,7 @@ static AVCodec* get_codec(const QString &name, const QString& hwa, AVCodecID cid
499 return avcodec_find_decoder(cid);
500 fullname = QString("%1_%2").arg(avcodec_get_name(cid)).arg(hwa);
502 - AVCodec *codec = avcodec_find_decoder_by_name(fullname.toUtf8().constData());
503 + const AVCodec *codec = avcodec_find_decoder_by_name(fullname.toUtf8().constData());
506 const AVCodecDescriptor* cd = avcodec_descriptor_get_by_name(fullname.toUtf8().constData());
507 @@ -76,7 +76,7 @@ bool AVDecoder::open()
510 const QString hwa = property("hwaccel").toString();
511 - AVCodec* codec = get_codec(codecName(), hwa, d.codec_ctx->codec_id);
512 + const AVCodec* codec = get_codec(codecName(), hwa, d.codec_ctx->codec_id);
513 if (!codec) { // TODO: can be null for none-ffmpeg based decoders
514 QString es(tr("No codec could be found for '%1'"));
515 if (d.codec_name.isEmpty()) {
516 @@ -153,6 +153,8 @@ void AVDecoder::flush()
517 avcodec_flush_buffers(d_func().codec_ctx);
520 +static QMap<AVCodecParameters*,AVCodecContext*> ccs;
523 * do nothing if equal
524 * close the old one. the codec context can not be shared in more than 1 decoder.
525 @@ -160,9 +162,17 @@ void AVDecoder::flush()
526 void AVDecoder::setCodecContext(void *codecCtx)
529 +#if LIBAVCODEC_VERSION_MAJOR < 59
530 AVCodecContext *ctx = (AVCodecContext*)codecCtx;
531 - if (d.codec_ctx == ctx)
532 + if (d.codec_ctx == codecCtx)
535 + AVCodecParameters *ctx = (AVCodecParameters*)codecCtx;
536 + if(ccs.contains(ctx)) {
537 + d.codec_ctx = ccs.value(ctx);
542 qWarning("Can not copy codec properties when it's open");
544 @@ -180,7 +190,12 @@ void AVDecoder::setCodecContext(void *codecCtx)
545 qWarning("avcodec_alloc_context3 failed");
548 + ccs.insert(ctx, d.codec_ctx);
549 +#if LIBAVCODEC_VERSION_MAJOR < 59
550 AV_ENSURE_OK(avcodec_copy_context(d.codec_ctx, ctx));
552 + AV_ENSURE_OK(avcodec_parameters_to_context(d.codec_ctx, ctx));
556 //TODO: reset other parameters?
557 diff --git a/src/codec/AVEncoder.cpp b/src/codec/AVEncoder.cpp
558 index 455539c7..5be64db2 100644
559 --- a/src/codec/AVEncoder.cpp
560 +++ b/src/codec/AVEncoder.cpp
561 @@ -146,7 +146,13 @@ void AVEncoder::copyAVCodecContext(void* ctx)
562 AVCodecContext* c = static_cast<AVCodecContext*>(ctx);
564 // dest should be avcodec_alloc_context3(NULL)
565 +#if LIBAVCODEC_VERSION_MAJOR < 59
566 AV_ENSURE_OK(avcodec_copy_context(d.avctx, c));
568 + AVCodecParameters *par;
569 + avcodec_parameters_from_context(par, c);
570 + AV_ENSURE_OK(avcodec_parameters_to_context(d.avctx, par));
575 diff --git a/src/codec/audio/AudioDecoderFFmpeg.cpp b/src/codec/audio/AudioDecoderFFmpeg.cpp
576 index d783588a..7c5188fc 100644
577 --- a/src/codec/audio/AudioDecoderFFmpeg.cpp
578 +++ b/src/codec/audio/AudioDecoderFFmpeg.cpp
579 @@ -100,10 +100,34 @@ bool AudioDecoderFFmpeg::decode(const Packet &packet)
580 av_init_packet(&eofpkt);
583 +#if LIBAVCODEC_VERSION_MAJOR < 59
584 ret = avcodec_decode_audio4(d.codec_ctx, d.frame, &got_frame_ptr, &eofpkt);
586 + ret = avcodec_receive_frame(d.codec_ctx, d.frame);
587 + if (ret == AVERROR(EAGAIN))
589 + else if (ret < 0) {
590 + qWarning("[AudioDecoder] %s", av_err2str(ret));
593 + got_frame_ptr = (ret == 0);
594 + ret = avcodec_send_packet(d.codec_ctx, &eofpkt);
597 // const AVPacket*: ffmpeg >= 1.0. no libav
598 +#if LIBAVCODEC_VERSION_MAJOR < 59
599 ret = avcodec_decode_audio4(d.codec_ctx, d.frame, &got_frame_ptr, (AVPacket*)packet.asAVPacket());
601 + ret = avcodec_receive_frame(d.codec_ctx, d.frame);
602 + if (ret == AVERROR(EAGAIN))
604 + else if (ret < 0) {
605 + qWarning("[AudioDecoder] %s", av_err2str(ret));
608 + got_frame_ptr = (ret == 0);
609 + ret = avcodec_send_packet(d.codec_ctx, (AVPacket*)packet.asAVPacket());
612 d.undecoded_size = qMin(packet.data.size() - ret, packet.data.size());
613 if (ret == AVERROR(EAGAIN)) {
614 @@ -145,7 +169,11 @@ AudioFrame AudioDecoderFFmpeg::frame()
615 f.setBytesPerLine(d.frame->linesize[0], 0); // for correct alignment
616 f.setSamplesPerChannel(d.frame->nb_samples);
617 // TODO: ffplay check AVFrame.pts, pkt_pts, last_pts+nb_samples. move to AudioFrame::from(AVFrame*)
618 +#if LIBAVCODEC_VERSION_MAJOR < 59
619 f.setTimestamp((double)d.frame->pkt_pts/1000.0);
621 + f.setTimestamp((double)d.frame->pts/1000.0);
623 f.setAudioResampler(d.resampler); // TODO: remove. it's not safe if frame is shared. use a pool or detach if ref >1
626 diff --git a/src/codec/audio/AudioEncoderFFmpeg.cpp b/src/codec/audio/AudioEncoderFFmpeg.cpp
627 index 3811e11a..c338aae3 100644
628 --- a/src/codec/audio/AudioEncoderFFmpeg.cpp
629 +++ b/src/codec/audio/AudioEncoderFFmpeg.cpp
630 @@ -54,7 +54,9 @@ public:
631 AudioEncoderFFmpegPrivate()
632 : AudioEncoderPrivate()
634 +#if LIBAVCODEC_VERSION_MAJOR < 59
635 avcodec_register_all();
637 // NULL: codec-specific defaults won't be initialized, which may result in suboptimal default settings (this is important mainly for encoders, e.g. libx264).
638 avctx = avcodec_alloc_context3(NULL);
640 @@ -68,11 +70,11 @@ bool AudioEncoderFFmpegPrivate::open()
642 if (codec_name.isEmpty()) {
643 // copy ctx from muxer by copyAVCodecContext
644 - AVCodec *codec = avcodec_find_encoder(avctx->codec_id);
645 + const AVCodec *codec = avcodec_find_encoder(avctx->codec_id);
646 AV_ENSURE_OK(avcodec_open2(avctx, codec, &dict), false);
649 - AVCodec *codec = avcodec_find_encoder_by_name(codec_name.toUtf8().constData());
650 + const AVCodec *codec = avcodec_find_encoder_by_name(codec_name.toUtf8().constData());
652 const AVCodecDescriptor* cd = avcodec_descriptor_get_by_name(codec_name.toUtf8().constData());
654 @@ -204,7 +206,13 @@ bool AudioEncoderFFmpeg::encode(const AudioFrame &frame)
655 pkt.data = (uint8_t*)d.buffer.constData(); //NULL
656 pkt.size = d.buffer.size(); //0
658 +#if LIBAVCODEC_VERSION_MAJOR < 59
659 int ret = avcodec_encode_audio2(d.avctx, &pkt, f, &got_packet);
661 + int ret = avcodec_send_frame(d.avctx, f);
662 + got_packet = (ret == 0);
663 + ret = avcodec_receive_packet(d.avctx, &pkt);
667 //qWarning("error avcodec_encode_audio2: %s" ,av_err2str(ret));
668 diff --git a/src/codec/video/VideoDecoderCUDA.cpp b/src/codec/video/VideoDecoderCUDA.cpp
669 index 844a3ae6..8015b4f9 100644
670 --- a/src/codec/video/VideoDecoderCUDA.cpp
671 +++ b/src/codec/video/VideoDecoderCUDA.cpp
673 #include "utils/Logger.h"
674 #include "SurfaceInteropCUDA.h"
677 +#include <libavcodec/bsf.h>
680 //decode error if not floating context
683 @@ -184,7 +188,7 @@ public:
685 ~VideoDecoderCUDAPrivate() {
687 - av_bitstream_filter_close(bsf);
691 if (!isLoaded()) //cuda_api
692 @@ -320,7 +324,7 @@ public:
696 - AVBitStreamFilterContext *bsf; //TODO: rename bsf_ctx
697 + AVBSFContext *bsf; //TODO: rename bsf_ctx
699 VideoDecoderCUDA::CopyMode copy_mode;
700 cuda::InteropResourcePtr interop_res; //may be still used in video frames when decoder is destroyed
701 @@ -391,9 +395,7 @@ bool VideoDecoderCUDA::decode(const Packet &packet)
703 // h264_mp4toannexb_filter does not use last parameter 'keyFrame', so just set 0
704 //return: 0: not changed, no outBuf allocated. >0: ok. <0: fail
705 - filtered = av_bitstream_filter_filter(d.bsf, d.codec_ctx, NULL, &outBuf, &outBufSize
706 - , (const uint8_t*)packet.data.constData(), packet.data.size()
707 - , 0);//d.is_keyframe);
708 + filtered = av_bsf_receive_packet(d.bsf, (AVPacket*)packet.asAVPacket());//d.is_keyframe);
709 //qDebug("%s @%d filtered=%d outBuf=%p, outBufSize=%d", __FUNCTION__, __LINE__, filtered, outBuf, outBufSize);
711 qDebug("failed to filter: %s", av_err2str(filtered));
712 @@ -780,15 +782,17 @@ void VideoDecoderCUDAPrivate::setBSF(AVCodecID codec)
714 if (codec == QTAV_CODEC_ID(H264)) {
716 - bsf = av_bitstream_filter_init("h264_mp4toannexb");
717 + av_bsf_alloc(av_bsf_get_by_name("h264_mp4toannexb"), &bsf);
719 Q_ASSERT(bsf && "h264_mp4toannexb bsf not found");
720 } else if (codec == QTAV_CODEC_ID(HEVC)) {
722 - bsf = av_bitstream_filter_init("hevc_mp4toannexb");
723 + av_bsf_alloc(av_bsf_get_by_name("hevc_mp4toannexb"), &bsf);
725 Q_ASSERT(bsf && "hevc_mp4toannexb bsf not found");
728 - av_bitstream_filter_close(bsf);
733 diff --git a/src/codec/video/VideoDecoderFFmpegBase.cpp b/src/codec/video/VideoDecoderFFmpegBase.cpp
734 index e344c5cb..49cebb94 100644
735 --- a/src/codec/video/VideoDecoderFFmpegBase.cpp
736 +++ b/src/codec/video/VideoDecoderFFmpegBase.cpp
737 @@ -30,12 +30,21 @@ extern ColorRange colorRangeFromFFmpeg(AVColorRange cr);
739 static void SetColorDetailsByFFmpeg(VideoFrame *f, AVFrame* frame, AVCodecContext* codec_ctx)
741 +#if LIBAVCODEC_VERSION_MAJOR < 59
742 ColorSpace cs = colorSpaceFromFFmpeg(av_frame_get_colorspace(frame));
743 if (cs == ColorSpace_Unknown)
747 cs = colorSpaceFromFFmpeg(codec_ctx->colorspace);
748 f->setColorSpace(cs);
749 +#if LIBAVCODEC_VERSION_MAJOR < 59
750 ColorRange cr = colorRangeFromFFmpeg(av_frame_get_color_range(frame));
751 if (cr == ColorRange_Unknown) {
756 // check yuvj format. TODO: deprecated, check only for old ffmpeg?
757 const AVPixelFormat pixfmt = (AVPixelFormat)frame->format;
759 @@ -125,9 +134,21 @@ bool VideoDecoderFFmpegBase::decode(const Packet &packet)
760 av_init_packet(&eofpkt);
763 +#if LIBAVCODEC_VERSION_MAJOR < 59
764 ret = avcodec_decode_video2(d.codec_ctx, d.frame, &got_frame_ptr, &eofpkt);
766 + ret = avcodec_receive_frame(d.codec_ctx, d.frame);
767 + got_frame_ptr = (ret == 0);
768 + ret = avcodec_send_packet(d.codec_ctx, &eofpkt);
771 +#if LIBAVCODEC_VERSION_MAJOR < 59
772 ret = avcodec_decode_video2(d.codec_ctx, d.frame, &got_frame_ptr, (AVPacket*)packet.asAVPacket());
774 + ret = avcodec_receive_frame(d.codec_ctx, d.frame);
775 + got_frame_ptr = (ret == 0);
776 + ret = avcodec_send_packet(d.codec_ctx, (AVPacket*)packet.asAVPacket());
779 //qDebug("pic_type=%c", av_get_picture_type_char(d.frame->pict_type));
780 d.undecoded_size = qMin(packet.data.size() - ret, packet.data.size());
781 @@ -159,7 +180,11 @@ VideoFrame VideoDecoderFFmpegBase::frame()
782 frame.setBits(d.frame->data);
783 frame.setBytesPerLine(d.frame->linesize);
784 // in s. TODO: what about AVFrame.pts? av_frame_get_best_effort_timestamp? move to VideoFrame::from(AVFrame*)
785 +#if LIBAVCODEC_VERSION_MAJOR < 59
786 frame.setTimestamp((double)d.frame->pkt_pts/1000.0);
788 + frame.setTimestamp((double)d.frame->pts/1000.0);
790 frame.setMetaData(QStringLiteral("avbuf"), QVariant::fromValue(AVFrameBuffersRef(new AVFrameBuffers(d.frame))));
791 d.updateColorDetails(&frame);
792 if (frame.format().hasPalette()) {
793 diff --git a/src/codec/video/VideoDecoderFFmpegHW.cpp b/src/codec/video/VideoDecoderFFmpegHW.cpp
794 index c17c8b28..17d663e6 100644
795 --- a/src/codec/video/VideoDecoderFFmpegHW.cpp
796 +++ b/src/codec/video/VideoDecoderFFmpegHW.cpp
797 @@ -328,7 +328,11 @@ VideoFrame VideoDecoderFFmpegHW::copyToFrame(const VideoFormat& fmt, int surface
798 // TODO: buffer pool and create VideoFrame when needed to avoid copy? also for other va
799 frame = frame.clone();
801 +#if LIBAVCODEC_VERSION_MAJOR < 59
802 frame.setTimestamp(double(d.frame->pkt_pts)/1000.0);
804 + frame.setTimestamp(double(d.frame->pts)/1000.0);
806 frame.setDisplayAspectRatio(d.getDAR(d.frame));
807 d.updateColorDetails(&frame);
809 diff --git a/src/codec/video/VideoDecoderVAAPI.cpp b/src/codec/video/VideoDecoderVAAPI.cpp
810 index a91caf92..0c8ce016 100644
811 --- a/src/codec/video/VideoDecoderVAAPI.cpp
812 +++ b/src/codec/video/VideoDecoderVAAPI.cpp
814 #include <QtCore/QMetaEnum>
815 #include <QtCore/QStringList>
816 #include <QtCore/QThread>
817 +#if LIBAVCODEC_VERSION_MAJOR < 59
819 #include <libavcodec/vaapi.h>
822 #include "QtAV/private/AVCompat.h"
823 #include "QtAV/private/factory.h"
824 #include "vaapi/SurfaceInteropVAAPI.h"
825 @@ -84,7 +86,7 @@ FACTORY_REGISTER(VideoDecoder, VAAPI, "VAAPI")
827 const char* getProfileName(AVCodecID id, int profile)
829 - AVCodec *c = avcodec_find_decoder(id);
830 + const AVCodec *c = avcodec_find_decoder(id);
833 return av_get_profile_name(c, profile);
834 diff --git a/src/codec/video/VideoEncoderFFmpeg.cpp b/src/codec/video/VideoEncoderFFmpeg.cpp
835 index c0c902cb..a352c533 100644
836 --- a/src/codec/video/VideoEncoderFFmpeg.cpp
837 +++ b/src/codec/video/VideoEncoderFFmpeg.cpp
838 @@ -116,11 +116,11 @@ bool VideoEncoderFFmpegPrivate::open()
840 if (codec_name.isEmpty()) {
841 // copy ctx from muxer by copyAVCodecContext
842 - AVCodec *codec = avcodec_find_encoder(avctx->codec_id);
843 + const AVCodec *codec = avcodec_find_encoder(avctx->codec_id);
844 AV_ENSURE_OK(avcodec_open2(avctx, codec, &dict), false);
847 - AVCodec *codec = avcodec_find_encoder_by_name(codec_name.toUtf8().constData());
848 + const AVCodec *codec = avcodec_find_encoder_by_name(codec_name.toUtf8().constData());
850 const AVCodecDescriptor* cd = avcodec_descriptor_get_by_name(codec_name.toUtf8().constData());
852 @@ -247,7 +247,7 @@ bool VideoEncoderFFmpegPrivate::open()
853 applyOptionsForContext();
854 AV_ENSURE_OK(avcodec_open2(avctx, codec, &dict), false);
856 - const int buffer_size = qMax<int>(qMax<int>(width*height*6+200, AV_INPUT_BUFFER_MIN_SIZE), sizeof(AVPicture));//??
857 + const int buffer_size = qMax<int>(qMax<int>(width*height*6+200, AV_INPUT_BUFFER_MIN_SIZE), av_image_get_buffer_size(avctx->pix_fmt, avctx->width, avctx->height, 1));//??
858 buffer.resize(buffer_size);
861 @@ -373,7 +373,13 @@ bool VideoEncoderFFmpeg::encode(const VideoFrame &frame)
862 pkt.data = (uint8_t*)d.buffer.constData();
863 pkt.size = d.buffer.size();
865 +#if LIBAVCODEC_VERSION_MAJOR < 59
866 int ret = avcodec_encode_video2(d.avctx, &pkt, f.data(), &got_packet);
868 + int ret = avcodec_send_frame(d.avctx, f.data());
869 + got_packet = (ret == 0);
870 + ret = avcodec_receive_packet(d.avctx, &pkt);
873 qWarning("error avcodec_encode_video2: %s" ,av_err2str(ret));
874 return false; //false
875 diff --git a/src/filter/LibAVFilter.cpp b/src/filter/LibAVFilter.cpp
876 index 8993a91f..d770ddc3 100644
877 --- a/src/filter/LibAVFilter.cpp
878 +++ b/src/filter/LibAVFilter.cpp
879 @@ -84,7 +84,9 @@ public:
883 +#if LIBAVCODEC_VERSION_MAJOR < 59
884 avfilter_register_all();
886 #endif //QTAV_HAVE(AVFILTER)
889 @@ -204,7 +206,9 @@ QString LibAVFilter::filterDescription(const QString &filterName)
892 #if QTAV_HAVE(AVFILTER)
893 +#if LIBAVCODEC_VERSION_MAJOR < 59
894 avfilter_register_all();
896 const AVFilter *f = avfilter_get_by_name(filterName.toUtf8().constData());
899 @@ -283,11 +287,18 @@ QStringList LibAVFilter::registeredFilters(int type)
902 #if QTAV_HAVE(AVFILTER)
903 +#if LIBAVCODEC_VERSION_MAJOR < 59
904 avfilter_register_all();
906 const AVFilter* f = NULL;
907 AVFilterPad* fp = NULL; // no const in avfilter_pad_get_name() for ffmpeg<=1.2 libav<=9
908 #if AV_MODULE_CHECK(LIBAVFILTER, 3, 8, 0, 53, 100)
909 +#if LIBAVCODEC_VERSION_MAJOR < 59
910 while ((f = avfilter_next(f))) {
913 + while (f = av_filter_iterate(ff)) {
916 AVFilter** ff = NULL;
917 while ((ff = av_filter_next(ff)) && *ff) {
918 diff --git a/src/subtitle/SubtitleProcessorFFmpeg.cpp b/src/subtitle/SubtitleProcessorFFmpeg.cpp
919 index 83e53e7c..50ccc31e 100644
920 --- a/src/subtitle/SubtitleProcessorFFmpeg.cpp
921 +++ b/src/subtitle/SubtitleProcessorFFmpeg.cpp
922 @@ -249,7 +249,7 @@ bool SubtitleProcessorFFmpeg::processHeader(const QByteArray &codec, const QByte
924 avcodec_free_context(&codec_ctx);
926 - AVCodec *c = avcodec_find_decoder_by_name(codec.constData());
927 + const AVCodec *c = avcodec_find_decoder_by_name(codec.constData());
929 qDebug("subtitle avcodec_descriptor_get_by_name %s", codec.constData());
930 const AVCodecDescriptor *desc = avcodec_descriptor_get_by_name(codec.constData());
931 @@ -370,8 +370,15 @@ bool SubtitleProcessorFFmpeg::processSubtitle()
932 qWarning("no subtitle stream found");
935 +#if LIBAVCODEC_VERSION_MAJOR < 59
936 codec_ctx = m_reader.subtitleCodecContext();
937 - AVCodec *dec = avcodec_find_decoder(codec_ctx->codec_id);
938 + const AVCodec *dec = avcodec_find_decoder(codec_ctx->codec_id);
940 + AVCodecParameters *par = m_reader.subtitleCodecContext();
941 + const AVCodec *dec = avcodec_find_decoder(par->codec_id);
942 + codec_ctx = avcodec_alloc_context3(dec);
943 + avcodec_parameters_to_context(codec_ctx, par);
945 const AVCodecDescriptor *dec_desc = avcodec_descriptor_get(codec_ctx->codec_id);