summaryrefslogtreecommitdiffstats
path: root/source/l/QtAV/ffmpeg5.patch
blob: 53e60b8ffe1f5177562480b11838565628c76471 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
diff --git a/src/AVCompat.cpp b/src/AVCompat.cpp
index befb3cd9..cf113f03 100644
--- a/src/AVCompat.cpp
+++ b/src/AVCompat.cpp
@@ -391,7 +391,7 @@ const char *get_codec_long_name(enum AVCodecID id)
     if (cd)
         return cd->long_name;
     av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
-    AVCodec *codec = avcodec_find_decoder(id);
+    const AVCodec *codec = avcodec_find_decoder(id);
     if (codec)
         return codec->long_name;
     codec = avcodec_find_encoder(id);
diff --git a/src/AVDemuxer.cpp b/src/AVDemuxer.cpp
index 432c4f04..5b46afd3 100644
--- a/src/AVDemuxer.cpp
+++ b/src/AVDemuxer.cpp
@@ -290,7 +290,7 @@ public:
     //copy the info, not parse the file when constructed, then need member vars
     QString file;
     QString file_orig;
-    AVInputFormat *input_format;
+    const AVInputFormat *input_format;
     QString format_forced;
     MediaIO *input;
 
@@ -310,7 +310,11 @@ public:
         // wanted_stream is REQUIRED. e.g. always set -1 to indicate the default stream, -2 to disable
         int stream, wanted_stream; // -1 default, selected by ff
         int index, wanted_index; // index in a kind of streams
+#if LIBAVCODEC_VERSION_MAJOR < 59
         AVCodecContext *avctx;
+#else
+	AVCodecParameters *avctx;
+#endif
     } StreamInfo;
     StreamInfo astream, vstream, sstream;
 
@@ -614,12 +618,14 @@ bool AVDemuxer::seek(qint64 pos)
     if (upos <= startTime()) {
         qDebug("************seek to beginning. started = false");
         d->started = false; //???
+#if LIBAVCODEC_VERSION_MAJOR < 59
         if (d->astream.avctx)
             d->astream.avctx->frame_number = 0;
         if (d->vstream.avctx)
             d->vstream.avctx->frame_number = 0; //TODO: why frame_number not changed after seek?
         if (d->sstream.avctx)
             d->sstream.avctx->frame_number = 0;
+#endif
     }
     return true;
 }
@@ -1062,37 +1068,61 @@ QList<int> AVDemuxer::subtitleStreams() const
     return d->subtitle_streams;
 }
 
+#if LIBAVCODEC_VERSION_MAJOR < 59
 AVCodecContext* AVDemuxer::audioCodecContext(int stream) const
+#else
+AVCodecParameters* AVDemuxer::audioCodecContext(int stream) const
+#endif
 {
     if (stream < 0)
         return d->astream.avctx;
     if (stream > (int)d->format_ctx->nb_streams)
         return 0;
+#if LIBAVCODEC_VERSION_MAJOR < 59
     AVCodecContext *avctx = d->format_ctx->streams[stream]->codec;
+#else
+    AVCodecParameters *avctx = d->format_ctx->streams[stream]->codecpar;
+#endif
     if (avctx->codec_type == AVMEDIA_TYPE_AUDIO)
         return avctx;
     return 0;
 }
 
+#if LIBAVCODEC_VERSION_MAJOR < 59
 AVCodecContext* AVDemuxer::videoCodecContext(int stream) const
+#else
+AVCodecParameters* AVDemuxer::videoCodecContext(int stream) const
+#endif
 {
     if (stream < 0)
         return d->vstream.avctx;
     if (stream > (int)d->format_ctx->nb_streams)
         return 0;
+#if LIBAVCODEC_VERSION_MAJOR < 59
     AVCodecContext *avctx = d->format_ctx->streams[stream]->codec;
+#else
+    AVCodecParameters *avctx = d->format_ctx->streams[stream]->codecpar;
+#endif
     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO)
         return avctx;
     return 0;
 }
 
+#if LIBAVCODEC_VERSION_MAJOR < 59
 AVCodecContext* AVDemuxer::subtitleCodecContext(int stream) const
+#else
+AVCodecParameters* AVDemuxer::subtitleCodecContext(int stream) const
+#endif
 {
     if (stream < 0)
         return d->sstream.avctx;
     if (stream > (int)d->format_ctx->nb_streams)
         return 0;
+#if LIBAVCODEC_VERSION_MAJOR < 59
     AVCodecContext *avctx = d->format_ctx->streams[stream]->codec;
+#else
+    AVCodecParameters *avctx = d->format_ctx->streams[stream]->codecpar;
+#endif
     if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE)
         return avctx;
     return 0;
@@ -1289,7 +1319,11 @@ bool AVDemuxer::Private::setStream(AVDemuxer::StreamType st, int streamValue)
     // don't touch wanted index
     si->stream = s;
     si->wanted_stream = streamValue;
+#if LIBAVCODEC_VERSION_MAJOR < 59
     si->avctx = format_ctx->streams[s]->codec;
+#else
+    si->avctx = format_ctx->streams[s]->codecpar;
+#endif
     has_attached_pic = !!(format_ctx->streams[s]->disposition & AV_DISPOSITION_ATTACHED_PIC);
     return true;
 }
@@ -1302,7 +1336,11 @@ bool AVDemuxer::Private::prepareStreams()
         return false;
     AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
     for (unsigned int i = 0; i < format_ctx->nb_streams; ++i) {
+#if LIBAVCODEC_VERSION_MAJOR < 59
         type = format_ctx->streams[i]->codec->codec_type;
+#else
+        type = format_ctx->streams[i]->codecpar->codec_type;
+#endif
         if (type == AVMEDIA_TYPE_VIDEO) {
             video_streams.push_back(i);
         } else if (type == AVMEDIA_TYPE_AUDIO) {
diff --git a/src/AVMuxer.cpp b/src/AVMuxer.cpp
index b601afd8..4ff1463e 100644
--- a/src/AVMuxer.cpp
+++ b/src/AVMuxer.cpp
@@ -81,7 +81,7 @@ public:
     //copy the info, not parse the file when constructed, then need member vars
     QString file;
     QString file_orig;
-    AVOutputFormat *format;
+    const AVOutputFormat *format;
     QString format_forced;
     MediaIO *io;
 
@@ -94,7 +94,7 @@ public:
 
 AVStream *AVMuxer::Private::addStream(AVFormatContext* ctx, const QString &codecName, AVCodecID codecId)
 {
-    AVCodec *codec = NULL;
+    const AVCodec *codec = NULL;
     if (!codecName.isEmpty()) {
         codec = avcodec_find_encoder_by_name(codecName.toUtf8().constData());
         if (!codec) {
@@ -120,7 +120,8 @@ AVStream *AVMuxer::Private::addStream(AVFormatContext* ctx, const QString &codec
     // set by avformat if unset
     s->id = ctx->nb_streams - 1;
     s->time_base = kTB;
-    AVCodecContext *c = s->codec;
+#if LIBAVCODEC_VERSION_MAJOR < 59
+    AVCodec *c = s->codec;
     c->codec_id = codec->id;
     // Using codec->time_base is deprecated, but needed for older lavf.
     c->time_base = s->time_base;
@@ -129,6 +130,7 @@ AVStream *AVMuxer::Private::addStream(AVFormatContext* ctx, const QString &codec
         c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
     // expose avctx to encoder and set properties in encoder?
     // list codecs for a given format in ui
+#endif
     return s;
 }
 
@@ -137,16 +139,24 @@ bool AVMuxer::Private::prepareStreams()
     audio_streams.clear();
     video_streams.clear();
     subtitle_streams.clear();
-    AVOutputFormat* fmt = format_ctx->oformat;
+    const AVOutputFormat* fmt = format_ctx->oformat;
     if (venc) {
         AVStream *s = addStream(format_ctx, venc->codecName(), fmt->video_codec);
         if (s) {
+#if LIBAVCODEC_VERSION_MAJOR < 59
             AVCodecContext *c = s->codec;
+#else
+            AVCodecParameters *c = s->codecpar;
+#endif
             c->bit_rate = venc->bitRate();
             c->width = venc->width();
             c->height = venc->height();
             /// MUST set after encoder is open to ensure format is valid and the same
+#if LIBAVCODEC_VERSION_MAJOR < 59
             c->pix_fmt = (AVPixelFormat)VideoFormat::pixelFormatToFFmpeg(venc->pixelFormat());
+#else
+            c->format = (AVPixelFormat)VideoFormat::pixelFormatToFFmpeg(venc->pixelFormat());
+#endif
 
             // Set avg_frame_rate based on encoder frame_rate
             s->avg_frame_rate = av_d2q(venc->frameRate(), venc->frameRate()*1001.0+2);
@@ -157,11 +167,19 @@ bool AVMuxer::Private::prepareStreams()
     if (aenc) {
         AVStream *s = addStream(format_ctx, aenc->codecName(), fmt->audio_codec);
         if (s) {
+#if LIBAVCODEC_VERSION_MAJOR < 59
             AVCodecContext *c = s->codec;
+#else
+            AVCodecParameters *c = s->codecpar;
+#endif
             c->bit_rate = aenc->bitRate();
             /// MUST set after encoder is open to ensure format is valid and the same
             c->sample_rate = aenc->audioFormat().sampleRate();
+#if LIBAVCODEC_VERSION_MAJOR < 59
             c->sample_fmt = (AVSampleFormat)aenc->audioFormat().sampleFormatFFmpeg();
+#else
+            c->format = (AVSampleFormat)aenc->audioFormat().sampleFormatFFmpeg();
+#endif
             c->channel_layout = aenc->audioFormat().channelLayoutFFmpeg();
             c->channels = aenc->audioFormat().channels();
             c->bits_per_raw_sample = aenc->audioFormat().bytesPerSample()*8; // need??
diff --git a/src/AVPlayerPrivate.cpp b/src/AVPlayerPrivate.cpp
index 84f079ef..d3bf09f1 100644
--- a/src/AVPlayerPrivate.cpp
+++ b/src/AVPlayerPrivate.cpp
@@ -55,7 +55,11 @@ int computeNotifyPrecision(qint64 duration, qreal fps)
 }
 } // namespace Internal
 
+#if LIBAVCODEC_VERSION_MAJOR < 59
 static bool correct_audio_channels(AVCodecContext *ctx) {
+#else
+static bool correct_audio_channels(AVCodecParameters *ctx) {
+#endif
     if (ctx->channels <= 0) {
         if (ctx->channel_layout) {
             ctx->channels = av_get_channel_layout_nb_channels(ctx->channel_layout);
@@ -251,7 +255,11 @@ void AVPlayer::Private::initBaseStatistics()
     updateNotifyInterval();
 }
 
+#if LIBAVCODEC_VERSION_MAJOR < 59
 void AVPlayer::Private::initCommonStatistics(int s, Statistics::Common *st, AVCodecContext *avctx)
+#else
+void AVPlayer::Private::initCommonStatistics(int s, Statistics::Common *st, AVCodecParameters *avctx)
+#endif
 {
     AVFormatContext *fmt_ctx = demuxer.formatContext();
     if (!fmt_ctx) {
@@ -288,7 +296,11 @@ void AVPlayer::Private::initCommonStatistics(int s, Statistics::Common *st, AVCo
 
 void AVPlayer::Private::initAudioStatistics(int s)
 {
+#if LIBAVCODEC_VERSION_MAJOR < 59
     AVCodecContext *avctx = demuxer.audioCodecContext();
+#else
+    AVCodecParameters *avctx = demuxer.audioCodecContext();
+#endif
     statistics.audio = Statistics::Common();
     statistics.audio_only = Statistics::AudioOnly();
     if (!avctx)
@@ -306,14 +318,22 @@ void AVPlayer::Private::initAudioStatistics(int s)
     // nb_channels -1: will use av_get_channel_layout_nb_channels
     av_get_channel_layout_string(cl, sizeof(cl), avctx->channels, avctx->channel_layout);
     statistics.audio_only.channel_layout = QLatin1String(cl);
+#if LIBAVCODEC_VERSION_MAJOR < 59
     statistics.audio_only.sample_fmt = QLatin1String(av_get_sample_fmt_name(avctx->sample_fmt));
+#else
+    statistics.audio_only.sample_fmt = QLatin1String(av_get_sample_fmt_name(static_cast<AVSampleFormat>(avctx->format)));
+#endif
     statistics.audio_only.frame_size = avctx->frame_size;
     statistics.audio_only.sample_rate = avctx->sample_rate;
 }
 
 void AVPlayer::Private::initVideoStatistics(int s)
 {
+#if LIBAVCODEC_VERSION_MAJOR < 59
     AVCodecContext *avctx = demuxer.videoCodecContext();
+#else
+    AVCodecParameters *avctx = demuxer.videoCodecContext();
+#endif
     statistics.video = Statistics::Common();
     statistics.video_only = Statistics::VideoOnly();
     if (!avctx)
@@ -324,10 +344,20 @@ void AVPlayer::Private::initVideoStatistics(int s)
         statistics.video.decoder = vdec->name();
         statistics.video.decoder_detail = vdec->description();
     }
+#if LIBAVCODEC_VERSION_MAJOR < 59
     statistics.video_only.coded_height = avctx->coded_height;
     statistics.video_only.coded_width = avctx->coded_width;
     statistics.video_only.gop_size = avctx->gop_size;
     statistics.video_only.pix_fmt = QLatin1String(av_get_pix_fmt_name(avctx->pix_fmt));
+#else
+    // FIXME we can't really get coded_height, coded_width and gop_size from Parameters
+    // At some point we should make an effort to get the real codec context; in the mean
+    // time, this should be close enough...
+    statistics.video_only.coded_height = avctx->height;
+    statistics.video_only.coded_width = avctx->width;
+    statistics.video_only.gop_size = 0;
+    statistics.video_only.pix_fmt = QLatin1String(av_get_pix_fmt_name(static_cast<AVPixelFormat>(avctx->format)));
+#endif
     statistics.video_only.height = avctx->height;
     statistics.video_only.width = avctx->width;
     statistics.video_only.rotate = 0;
@@ -354,7 +384,11 @@ bool AVPlayer::Private::setupAudioThread(AVPlayer *player)
         athread->setDecoder(0);
         athread->setOutput(0);
     }
+#if LIBAVCODEC_VERSION_MAJOR < 59
     AVCodecContext *avctx = ademuxer->audioCodecContext();
+#else
+    AVCodecParameters *avctx = ademuxer->audioCodecContext();
+#endif
     if (!avctx) {
         // TODO: close ao? //TODO: check pulseaudio perapp control if closed
         return false;
@@ -384,7 +418,11 @@ bool AVPlayer::Private::setupAudioThread(AVPlayer *player)
     correct_audio_channels(avctx);
     AudioFormat af;
     af.setSampleRate(avctx->sample_rate);
+#if LIBAVCODEC_VERSION_MAJOR < 59
     af.setSampleFormatFFmpeg(avctx->sample_fmt);
+#else
+    af.setSampleFormatFFmpeg(avctx->format);
+#endif
     af.setChannelLayoutFFmpeg(avctx->channel_layout);
     if (!af.isValid()) {
         qWarning("invalid audio format. audio stream will be disabled");
@@ -466,7 +504,11 @@ QVariantList AVPlayer::Private::getTracksInfo(AVDemuxer *demuxer, AVDemuxer::Str
         t[QStringLiteral("stream_index")] = QVariant(s);
 
         AVStream *stream = demuxer->formatContext()->streams[s];
+#if LIBAVCODEC_VERSION_MAJOR < 59
         AVCodecContext *ctx = stream->codec;
+#else
+        AVCodecParameters *ctx = stream->codecpar;
+#endif
         if (ctx) {
             t[QStringLiteral("codec")] = QByteArray(avcodec_descriptor_get(ctx->codec_id)->name);
             if (ctx->extradata)
@@ -494,7 +536,11 @@ bool AVPlayer::Private::applySubtitleStream(int n, AVPlayer *player)
 {
     if (!demuxer.setStreamIndex(AVDemuxer::SubtitleStream, n))
         return false;
+#if LIBAVCODEC_VERSION_MAJOR < 59
     AVCodecContext *ctx = demuxer.subtitleCodecContext();
+#else
+    AVCodecParameters *ctx = demuxer.subtitleCodecContext();
+#endif
     if (!ctx)
         return false;
     // FIXME: AVCodecDescriptor.name and AVCodec.name are different!
@@ -512,7 +558,11 @@ bool AVPlayer::Private::tryApplyDecoderPriority(AVPlayer *player)
     // TODO: add an option to apply the new decoder even if not available
     qint64 pos = player->position();
     VideoDecoder *vd = NULL;
+#if LIBAVCODEC_VERSION_MAJOR < 59
     AVCodecContext *avctx = demuxer.videoCodecContext();
+#else
+    AVCodecParameters *avctx = demuxer.videoCodecContext();
+#endif
     foreach(VideoDecoderId vid, vc_ids) {
         qDebug("**********trying video decoder: %s...", VideoDecoder::name(vid));
         vd = VideoDecoder::create(vid);
@@ -560,7 +610,11 @@ bool AVPlayer::Private::setupVideoThread(AVPlayer *player)
         vthread->packetQueue()->clear();
         vthread->setDecoder(0);
     }
+#if LIBAVCODEC_VERSION_MAJOR < 59
     AVCodecContext *avctx = demuxer.videoCodecContext();
+#else
+    AVCodecParameters *avctx = demuxer.videoCodecContext();
+#endif
     if (!avctx) {
         return false;
     }
diff --git a/src/AVPlayerPrivate.h b/src/AVPlayerPrivate.h
index e404b9bf..f0f90fd1 100644
--- a/src/AVPlayerPrivate.h
+++ b/src/AVPlayerPrivate.h
@@ -29,6 +29,10 @@
 #include "AVDemuxThread.h"
 #include "utils/Logger.h"
 
+extern "C" {
+#include <libavcodec/avcodec.h>
+}
+
 namespace QtAV {
 
 static const qint64 kInvalidPosition = std::numeric_limits<qint64>::max();
@@ -43,7 +47,11 @@ public:
     void applyFrameRate();
     void initStatistics();
     void initBaseStatistics();
+#if LIBAVCODEC_VERSION_MAJOR < 59
     void initCommonStatistics(int s, Statistics::Common* st, AVCodecContext* avctx);
+#else
+    void initCommonStatistics(int s, Statistics::Common* st, AVCodecParameters* avctx);
+#endif
     void initAudioStatistics(int s);
     void initVideoStatistics(int s);
     void initSubtitleStatistics(int s);
diff --git a/src/QtAV/AVDemuxer.h b/src/QtAV/AVDemuxer.h
index 3b720f5d..688c82f9 100644
--- a/src/QtAV/AVDemuxer.h
+++ b/src/QtAV/AVDemuxer.h
@@ -28,6 +28,10 @@
 #include <QtCore/QObject>
 #include <QtCore/QScopedPointer>
 
+extern "C" {
+#include <libavcodec/avcodec.h>
+}
+
 struct AVFormatContext;
 struct AVCodecContext;
 QT_BEGIN_NAMESPACE
@@ -151,9 +155,15 @@ public:
     int subtitleStream() const;
     QList<int> subtitleStreams() const;
     //codec. stream < 0: the stream going to play (or the stream set by setStreamIndex())
+#if LIBAVCODEC_VERSION_MAJOR < 59
     AVCodecContext* audioCodecContext(int stream = -1) const;
     AVCodecContext* videoCodecContext(int stream = -1) const;
     AVCodecContext* subtitleCodecContext(int stream = -1) const;
+#else
+    AVCodecParameters* audioCodecContext(int stream = -1) const;
+    AVCodecParameters* videoCodecContext(int stream = -1) const;
+    AVCodecParameters* subtitleCodecContext(int stream = -1) const;
+#endif
     /**
      * @brief getInterruptTimeout return the interrupt timeout
      */
diff --git a/src/QtAV/private/AVDecoder_p.h b/src/QtAV/private/AVDecoder_p.h
index 2382974e..5e952ea8 100644
--- a/src/QtAV/private/AVDecoder_p.h
+++ b/src/QtAV/private/AVDecoder_p.h
@@ -78,7 +78,7 @@ class Q_AV_PRIVATE_EXPORT AVDecoderPrivate : public DPtrPrivate<AVDecoder>
 {
 public:
     static const char* getProfileName(AVCodecID id, int profile) {
-        AVCodec *c = avcodec_find_decoder(id);
+        const AVCodec *c = avcodec_find_decoder(id);
         if (!c)
             return "Unknow";
         return av_get_profile_name(c, profile);
diff --git a/src/VideoFormat.cpp b/src/VideoFormat.cpp
index b9c7b4f0..5e7901f1 100644
--- a/src/VideoFormat.cpp
+++ b/src/VideoFormat.cpp
@@ -702,7 +702,11 @@ bool VideoFormat::hasPalette() const
 
 bool VideoFormat::isPseudoPaletted() const
 {
+#if LIBAVCODEC_VERSION_MAJOR < 59
     return (d->flags() & AV_PIX_FMT_FLAG_PSEUDOPAL) == AV_PIX_FMT_FLAG_PSEUDOPAL;
+#else
+    return hasPalette();
+#endif
 }
 
 bool VideoFormat::isBitStream() const
diff --git a/src/VideoFrameExtractor.cpp b/src/VideoFrameExtractor.cpp
index 8e4a843e..ecaa079e 100644
--- a/src/VideoFrameExtractor.cpp
+++ b/src/VideoFrameExtractor.cpp
@@ -192,7 +192,11 @@ public:
             if (!vd)
                 continue;
             decoder.reset(vd);
+#if LIBAVCODEC_VERSION_MAJOR < 59
             AVCodecContext *cctx = demuxer.videoCodecContext();
+#else
+            AVCodecParameters *cctx = demuxer.videoCodecContext();
+#endif
             if (cctx) decoder->setCodecContext(demuxer.videoCodecContext());
             if (!cctx || !decoder->open()) {
                 decoder.reset(0);
diff --git a/src/codec/AVDecoder.cpp b/src/codec/AVDecoder.cpp
index 440504d0..bc9cc278 100644
--- a/src/codec/AVDecoder.cpp
+++ b/src/codec/AVDecoder.cpp
@@ -27,7 +27,7 @@
 
 namespace QtAV {
 
-static AVCodec* get_codec(const QString &name, const QString& hwa, AVCodecID cid)
+static const AVCodec* get_codec(const QString &name, const QString& hwa, AVCodecID cid)
 {
     QString fullname(name);
     if (name.isEmpty()) {
@@ -35,7 +35,7 @@ static AVCodec* get_codec(const QString &name, const QString& hwa, AVCodecID cid
             return avcodec_find_decoder(cid);
         fullname = QString("%1_%2").arg(avcodec_get_name(cid)).arg(hwa);
     }
-    AVCodec *codec = avcodec_find_decoder_by_name(fullname.toUtf8().constData());
+    const AVCodec *codec = avcodec_find_decoder_by_name(fullname.toUtf8().constData());
     if (codec)
         return codec;
     const AVCodecDescriptor* cd = avcodec_descriptor_get_by_name(fullname.toUtf8().constData());
@@ -76,7 +76,7 @@ bool AVDecoder::open()
         return false;
     }
     const QString hwa = property("hwaccel").toString();
-    AVCodec* codec = get_codec(codecName(), hwa, d.codec_ctx->codec_id);
+    const AVCodec* codec = get_codec(codecName(), hwa, d.codec_ctx->codec_id);
     if (!codec) { // TODO: can be null for none-ffmpeg based decoders
         QString es(tr("No codec could be found for '%1'"));
         if (d.codec_name.isEmpty()) {
@@ -153,6 +153,8 @@ void AVDecoder::flush()
     avcodec_flush_buffers(d_func().codec_ctx);
 }
 
+static QMap<AVCodecParameters*,AVCodecContext*> ccs;
+
 /*
  * do nothing if equal
  * close the old one. the codec context can not be shared in more than 1 decoder.
@@ -160,9 +162,17 @@ void AVDecoder::flush()
 void AVDecoder::setCodecContext(void *codecCtx)
 {
     DPTR_D(AVDecoder);
+#if LIBAVCODEC_VERSION_MAJOR < 59
     AVCodecContext *ctx = (AVCodecContext*)codecCtx;
-    if (d.codec_ctx == ctx)
+    if (d.codec_ctx == codecCtx)
         return;
+#else
+    AVCodecParameters *ctx = (AVCodecParameters*)codecCtx;
+    if(ccs.contains(ctx)) {
+        d.codec_ctx = ccs.value(ctx);
+	return;
+    }
+#endif
     if (isOpen()) {
         qWarning("Can not copy codec properties when it's open");
         close(); //
@@ -180,7 +190,12 @@ void AVDecoder::setCodecContext(void *codecCtx)
         qWarning("avcodec_alloc_context3 failed");
         return;
     }
+    ccs.insert(ctx, d.codec_ctx);
+#if LIBAVCODEC_VERSION_MAJOR < 59
     AV_ENSURE_OK(avcodec_copy_context(d.codec_ctx, ctx));
+#else
+    AV_ENSURE_OK(avcodec_parameters_to_context(d.codec_ctx, ctx));
+#endif
 }
 
 //TODO: reset other parameters?
diff --git a/src/codec/AVEncoder.cpp b/src/codec/AVEncoder.cpp
index 455539c7..5be64db2 100644
--- a/src/codec/AVEncoder.cpp
+++ b/src/codec/AVEncoder.cpp
@@ -146,7 +146,13 @@ void AVEncoder::copyAVCodecContext(void* ctx)
     AVCodecContext* c = static_cast<AVCodecContext*>(ctx);
     if (d.avctx) {
         // dest should be avcodec_alloc_context3(NULL)
+#if LIBAVCODEC_VERSION_MAJOR < 59
         AV_ENSURE_OK(avcodec_copy_context(d.avctx, c));
+#else
+        AVCodecParameters *par;
+        avcodec_parameters_from_context(par, c);
+        AV_ENSURE_OK(avcodec_parameters_to_context(d.avctx, par));
+#endif
         d.is_open = false;
         return;
     }
diff --git a/src/codec/audio/AudioDecoderFFmpeg.cpp b/src/codec/audio/AudioDecoderFFmpeg.cpp
index d783588a..7c5188fc 100644
--- a/src/codec/audio/AudioDecoderFFmpeg.cpp
+++ b/src/codec/audio/AudioDecoderFFmpeg.cpp
@@ -100,10 +100,34 @@ bool AudioDecoderFFmpeg::decode(const Packet &packet)
         av_init_packet(&eofpkt);
         eofpkt.data = NULL;
         eofpkt.size = 0;
+#if LIBAVCODEC_VERSION_MAJOR < 59
         ret = avcodec_decode_audio4(d.codec_ctx, d.frame, &got_frame_ptr, &eofpkt);
+#else
+	ret = avcodec_receive_frame(d.codec_ctx, d.frame);
+	if (ret == AVERROR(EAGAIN))
+            return false;
+	else if (ret < 0) {
+            qWarning("[AudioDecoder] %s", av_err2str(ret));
+            return false;
+	}
+	got_frame_ptr = (ret == 0);
+	ret = avcodec_send_packet(d.codec_ctx, &eofpkt);
+#endif
     } else {
     // const AVPacket*: ffmpeg >= 1.0. no libav
+#if LIBAVCODEC_VERSION_MAJOR < 59
         ret = avcodec_decode_audio4(d.codec_ctx, d.frame, &got_frame_ptr, (AVPacket*)packet.asAVPacket());
+#else
+	ret = avcodec_receive_frame(d.codec_ctx, d.frame);
+	if (ret == AVERROR(EAGAIN))
+            return false;
+	else if (ret < 0) {
+            qWarning("[AudioDecoder] %s", av_err2str(ret));
+            return false;
+	}
+	got_frame_ptr = (ret == 0);
+	ret = avcodec_send_packet(d.codec_ctx, (AVPacket*)packet.asAVPacket());
+#endif
     }
     d.undecoded_size = qMin(packet.data.size() - ret, packet.data.size());
     if (ret == AVERROR(EAGAIN)) {
@@ -145,7 +169,11 @@ AudioFrame AudioDecoderFFmpeg::frame()
     f.setBytesPerLine(d.frame->linesize[0], 0); // for correct alignment
     f.setSamplesPerChannel(d.frame->nb_samples);
     // TODO: ffplay check AVFrame.pts, pkt_pts, last_pts+nb_samples. move to AudioFrame::from(AVFrame*)
+#if LIBAVCODEC_VERSION_MAJOR < 59
     f.setTimestamp((double)d.frame->pkt_pts/1000.0);
+#else
+    f.setTimestamp((double)d.frame->pts/1000.0);
+#endif
     f.setAudioResampler(d.resampler); // TODO: remove. it's not safe if frame is shared. use a pool or detach if ref >1
     return f;
 }
diff --git a/src/codec/audio/AudioEncoderFFmpeg.cpp b/src/codec/audio/AudioEncoderFFmpeg.cpp
index 3811e11a..c338aae3 100644
--- a/src/codec/audio/AudioEncoderFFmpeg.cpp
+++ b/src/codec/audio/AudioEncoderFFmpeg.cpp
@@ -54,7 +54,9 @@ public:
     AudioEncoderFFmpegPrivate()
         : AudioEncoderPrivate()
     {
+#if LIBAVCODEC_VERSION_MAJOR < 59
         avcodec_register_all();
+#endif
         // NULL: codec-specific defaults won't be initialized, which may result in suboptimal default settings (this is important mainly for encoders, e.g. libx264).
         avctx = avcodec_alloc_context3(NULL);
     }
@@ -68,11 +70,11 @@ bool AudioEncoderFFmpegPrivate::open()
 {
     if (codec_name.isEmpty()) {
         // copy ctx from muxer by copyAVCodecContext
-        AVCodec *codec = avcodec_find_encoder(avctx->codec_id);
+        const AVCodec *codec = avcodec_find_encoder(avctx->codec_id);
         AV_ENSURE_OK(avcodec_open2(avctx, codec, &dict), false);
         return true;
     }
-    AVCodec *codec = avcodec_find_encoder_by_name(codec_name.toUtf8().constData());
+    const AVCodec *codec = avcodec_find_encoder_by_name(codec_name.toUtf8().constData());
     if (!codec) {
         const AVCodecDescriptor* cd = avcodec_descriptor_get_by_name(codec_name.toUtf8().constData());
         if (cd) {
@@ -204,7 +206,13 @@ bool AudioEncoderFFmpeg::encode(const AudioFrame &frame)
     pkt.data = (uint8_t*)d.buffer.constData(); //NULL
     pkt.size = d.buffer.size(); //0
     int got_packet = 0;
+#if LIBAVCODEC_VERSION_MAJOR < 59
     int ret = avcodec_encode_audio2(d.avctx, &pkt, f, &got_packet);
+#else
+    int ret = avcodec_send_frame(d.avctx, f);
+    got_packet = (ret == 0);
+    ret = avcodec_receive_packet(d.avctx, &pkt);
+#endif
     av_frame_free(&f);
     if (ret < 0) {
         //qWarning("error avcodec_encode_audio2: %s" ,av_err2str(ret));
diff --git a/src/codec/video/VideoDecoderCUDA.cpp b/src/codec/video/VideoDecoderCUDA.cpp
index 844a3ae6..8015b4f9 100644
--- a/src/codec/video/VideoDecoderCUDA.cpp
+++ b/src/codec/video/VideoDecoderCUDA.cpp
@@ -46,6 +46,10 @@
 #include "utils/Logger.h"
 #include "SurfaceInteropCUDA.h"
 
+extern "C" {
+#include <libavcodec/bsf.h>
+}
+
 //decode error if not floating context
 
 namespace QtAV {
@@ -184,7 +188,7 @@ public:
     }
     ~VideoDecoderCUDAPrivate() {
         if (bsf)
-            av_bitstream_filter_close(bsf);
+            av_bsf_free(&bsf);
         if (!can_load)
             return;
         if (!isLoaded()) //cuda_api
@@ -320,7 +324,7 @@ public:
     int nb_dec_surface;
     QString description;
 
-    AVBitStreamFilterContext *bsf; //TODO: rename bsf_ctx
+    AVBSFContext *bsf; //TODO: rename bsf_ctx
 
     VideoDecoderCUDA::CopyMode copy_mode;
     cuda::InteropResourcePtr interop_res; //may be still used in video frames when decoder is destroyed
@@ -391,9 +395,7 @@ bool VideoDecoderCUDA::decode(const Packet &packet)
     if (d.bsf) {
         // h264_mp4toannexb_filter does not use last parameter 'keyFrame', so just set 0
         //return: 0: not changed, no outBuf allocated. >0: ok. <0: fail
-        filtered = av_bitstream_filter_filter(d.bsf, d.codec_ctx, NULL, &outBuf, &outBufSize
-                                                  , (const uint8_t*)packet.data.constData(), packet.data.size()
-                                                  , 0);//d.is_keyframe);
+        filtered = av_bsf_receive_packet(d.bsf, (AVPacket*)packet.asAVPacket());//d.is_keyframe);
         //qDebug("%s @%d filtered=%d outBuf=%p, outBufSize=%d", __FUNCTION__, __LINE__, filtered, outBuf, outBufSize);
         if (filtered < 0) {
             qDebug("failed to filter: %s", av_err2str(filtered));
@@ -780,15 +782,17 @@ void VideoDecoderCUDAPrivate::setBSF(AVCodecID codec)
 {
     if (codec == QTAV_CODEC_ID(H264)) {
         if (!bsf)
-            bsf = av_bitstream_filter_init("h264_mp4toannexb");
+            av_bsf_alloc(av_bsf_get_by_name("h264_mp4toannexb"), &bsf);
+            av_bsf_init(bsf);
         Q_ASSERT(bsf && "h264_mp4toannexb bsf not found");
     } else if (codec == QTAV_CODEC_ID(HEVC)) {
         if (!bsf)
-            bsf = av_bitstream_filter_init("hevc_mp4toannexb");
+            av_bsf_alloc(av_bsf_get_by_name("hevc_mp4toannexb"), &bsf);
+            av_bsf_init(bsf);
         Q_ASSERT(bsf && "hevc_mp4toannexb bsf not found");
     } else {
         if (bsf) {
-            av_bitstream_filter_close(bsf);
+            av_bsf_free(&bsf);
             bsf = 0;
         }
     }
diff --git a/src/codec/video/VideoDecoderFFmpegBase.cpp b/src/codec/video/VideoDecoderFFmpegBase.cpp
index e344c5cb..49cebb94 100644
--- a/src/codec/video/VideoDecoderFFmpegBase.cpp
+++ b/src/codec/video/VideoDecoderFFmpegBase.cpp
@@ -30,12 +30,21 @@ extern ColorRange colorRangeFromFFmpeg(AVColorRange cr);
 
 static void SetColorDetailsByFFmpeg(VideoFrame *f, AVFrame* frame, AVCodecContext* codec_ctx)
 {
+#if LIBAVCODEC_VERSION_MAJOR < 59
     ColorSpace cs = colorSpaceFromFFmpeg(av_frame_get_colorspace(frame));
     if (cs == ColorSpace_Unknown)
+#else
+    ColorSpace
+#endif
         cs = colorSpaceFromFFmpeg(codec_ctx->colorspace);
     f->setColorSpace(cs);
+#if LIBAVCODEC_VERSION_MAJOR < 59
     ColorRange cr = colorRangeFromFFmpeg(av_frame_get_color_range(frame));
     if (cr == ColorRange_Unknown) {
+#else
+    ColorRange cr;
+    if (1) {
+#endif
         // check yuvj format. TODO: deprecated, check only for old ffmpeg?
         const AVPixelFormat pixfmt = (AVPixelFormat)frame->format;
         switch (pixfmt) {
@@ -125,9 +134,21 @@ bool VideoDecoderFFmpegBase::decode(const Packet &packet)
         av_init_packet(&eofpkt);
         eofpkt.data = NULL;
         eofpkt.size = 0;
+#if LIBAVCODEC_VERSION_MAJOR < 59
         ret = avcodec_decode_video2(d.codec_ctx, d.frame, &got_frame_ptr, &eofpkt);
+#else
+	ret = avcodec_receive_frame(d.codec_ctx, d.frame);
+	got_frame_ptr = (ret == 0);
+	ret = avcodec_send_packet(d.codec_ctx, &eofpkt);
+#endif
     } else {
+#if LIBAVCODEC_VERSION_MAJOR < 59
         ret = avcodec_decode_video2(d.codec_ctx, d.frame, &got_frame_ptr, (AVPacket*)packet.asAVPacket());
+#else
+	ret = avcodec_receive_frame(d.codec_ctx, d.frame);
+	got_frame_ptr = (ret == 0);
+	ret = avcodec_send_packet(d.codec_ctx, (AVPacket*)packet.asAVPacket());
+#endif
     }
     //qDebug("pic_type=%c", av_get_picture_type_char(d.frame->pict_type));
     d.undecoded_size = qMin(packet.data.size() - ret, packet.data.size());
@@ -159,7 +180,11 @@ VideoFrame VideoDecoderFFmpegBase::frame()
     frame.setBits(d.frame->data);
     frame.setBytesPerLine(d.frame->linesize);
     // in s. TODO: what about AVFrame.pts? av_frame_get_best_effort_timestamp? move to VideoFrame::from(AVFrame*)
+#if LIBAVCODEC_VERSION_MAJOR < 59
     frame.setTimestamp((double)d.frame->pkt_pts/1000.0);
+#else
+    frame.setTimestamp((double)d.frame->pts/1000.0);
+#endif
     frame.setMetaData(QStringLiteral("avbuf"), QVariant::fromValue(AVFrameBuffersRef(new AVFrameBuffers(d.frame))));
     d.updateColorDetails(&frame);
     if (frame.format().hasPalette()) {
diff --git a/src/codec/video/VideoDecoderFFmpegHW.cpp b/src/codec/video/VideoDecoderFFmpegHW.cpp
index c17c8b28..17d663e6 100644
--- a/src/codec/video/VideoDecoderFFmpegHW.cpp
+++ b/src/codec/video/VideoDecoderFFmpegHW.cpp
@@ -328,7 +328,11 @@ VideoFrame VideoDecoderFFmpegHW::copyToFrame(const VideoFormat& fmt, int surface
         // TODO: buffer pool and create VideoFrame when needed to avoid copy? also for other va
         frame = frame.clone();
     }
+#if LIBAVCODEC_VERSION_MAJOR < 59
     frame.setTimestamp(double(d.frame->pkt_pts)/1000.0);
+#else
+    frame.setTimestamp(double(d.frame->pts)/1000.0);
+#endif
     frame.setDisplayAspectRatio(d.getDAR(d.frame));
     d.updateColorDetails(&frame);
     return frame;
diff --git a/src/codec/video/VideoDecoderVAAPI.cpp b/src/codec/video/VideoDecoderVAAPI.cpp
index a91caf92..0c8ce016 100644
--- a/src/codec/video/VideoDecoderVAAPI.cpp
+++ b/src/codec/video/VideoDecoderVAAPI.cpp
@@ -27,9 +27,11 @@
 #include <QtCore/QMetaEnum>
 #include <QtCore/QStringList>
 #include <QtCore/QThread>
+#if LIBAVCODEC_VERSION_MAJOR < 59
 extern "C" {
 #include <libavcodec/vaapi.h>
 }
+#endif
 #include "QtAV/private/AVCompat.h"
 #include "QtAV/private/factory.h"
 #include "vaapi/SurfaceInteropVAAPI.h"
@@ -84,7 +86,7 @@ FACTORY_REGISTER(VideoDecoder, VAAPI, "VAAPI")
 
 const char* getProfileName(AVCodecID id, int profile)
 {
-    AVCodec *c = avcodec_find_decoder(id);
+    const AVCodec *c = avcodec_find_decoder(id);
     if (!c)
         return "Unknow";
     return av_get_profile_name(c, profile);
diff --git a/src/codec/video/VideoEncoderFFmpeg.cpp b/src/codec/video/VideoEncoderFFmpeg.cpp
index c0c902cb..a352c533 100644
--- a/src/codec/video/VideoEncoderFFmpeg.cpp
+++ b/src/codec/video/VideoEncoderFFmpeg.cpp
@@ -116,11 +116,11 @@ bool VideoEncoderFFmpegPrivate::open()
     nb_encoded = 0LL;
     if (codec_name.isEmpty()) {
         // copy ctx from muxer by copyAVCodecContext
-        AVCodec *codec = avcodec_find_encoder(avctx->codec_id);
+        const AVCodec *codec = avcodec_find_encoder(avctx->codec_id);
         AV_ENSURE_OK(avcodec_open2(avctx, codec, &dict), false);
         return true;
     }
-    AVCodec *codec = avcodec_find_encoder_by_name(codec_name.toUtf8().constData());
+    const AVCodec *codec = avcodec_find_encoder_by_name(codec_name.toUtf8().constData());
     if (!codec) {
         const AVCodecDescriptor* cd = avcodec_descriptor_get_by_name(codec_name.toUtf8().constData());
         if (cd) {
@@ -247,7 +247,7 @@ bool VideoEncoderFFmpegPrivate::open()
     applyOptionsForContext();
     AV_ENSURE_OK(avcodec_open2(avctx, codec, &dict), false);
     // from mpv ao_lavc
-    const int buffer_size = qMax<int>(qMax<int>(width*height*6+200, AV_INPUT_BUFFER_MIN_SIZE), sizeof(AVPicture));//??
+    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));//??
     buffer.resize(buffer_size);
     return true;
 }
@@ -373,7 +373,13 @@ bool VideoEncoderFFmpeg::encode(const VideoFrame &frame)
     pkt.data = (uint8_t*)d.buffer.constData();
     pkt.size = d.buffer.size();
     int got_packet = 0;
+#if LIBAVCODEC_VERSION_MAJOR < 59
     int ret = avcodec_encode_video2(d.avctx, &pkt, f.data(), &got_packet);
+#else
+    int ret = avcodec_send_frame(d.avctx, f.data());
+    got_packet = (ret == 0);
+    ret = avcodec_receive_packet(d.avctx, &pkt);
+#endif
     if (ret < 0) {
         qWarning("error avcodec_encode_video2: %s" ,av_err2str(ret));
         return false; //false
diff --git a/src/filter/LibAVFilter.cpp b/src/filter/LibAVFilter.cpp
index 8993a91f..d770ddc3 100644
--- a/src/filter/LibAVFilter.cpp
+++ b/src/filter/LibAVFilter.cpp
@@ -84,7 +84,9 @@ public:
         filter_graph = 0;
         in_filter_ctx = 0;
         out_filter_ctx = 0;
+#if LIBAVCODEC_VERSION_MAJOR < 59
         avfilter_register_all();
+#endif
 #endif //QTAV_HAVE(AVFILTER)
     }
     ~Private() {
@@ -204,7 +206,9 @@ QString LibAVFilter::filterDescription(const QString &filterName)
 {
     QString s;
 #if QTAV_HAVE(AVFILTER)
+#if LIBAVCODEC_VERSION_MAJOR < 59
     avfilter_register_all();
+#endif
     const AVFilter *f = avfilter_get_by_name(filterName.toUtf8().constData());
     if (!f)
         return s;
@@ -283,11 +287,18 @@ QStringList LibAVFilter::registeredFilters(int type)
 {
     QStringList filters;
 #if QTAV_HAVE(AVFILTER)
+#if LIBAVCODEC_VERSION_MAJOR < 59
     avfilter_register_all();
+#endif
     const AVFilter* f = NULL;
     AVFilterPad* fp = NULL; // no const in avfilter_pad_get_name() for ffmpeg<=1.2 libav<=9
 #if AV_MODULE_CHECK(LIBAVFILTER, 3, 8, 0, 53, 100)
+#if LIBAVCODEC_VERSION_MAJOR < 59
     while ((f = avfilter_next(f))) {
+#else
+    void** ff = NULL;
+    while (f = av_filter_iterate(ff)) {
+#endif
 #else
     AVFilter** ff = NULL;
     while ((ff = av_filter_next(ff)) && *ff) {
diff --git a/src/subtitle/SubtitleProcessorFFmpeg.cpp b/src/subtitle/SubtitleProcessorFFmpeg.cpp
index 83e53e7c..50ccc31e 100644
--- a/src/subtitle/SubtitleProcessorFFmpeg.cpp
+++ b/src/subtitle/SubtitleProcessorFFmpeg.cpp
@@ -249,7 +249,7 @@ bool SubtitleProcessorFFmpeg::processHeader(const QByteArray &codec, const QByte
     if (codec_ctx) {
         avcodec_free_context(&codec_ctx);
     }
-    AVCodec *c = avcodec_find_decoder_by_name(codec.constData());
+    const AVCodec *c = avcodec_find_decoder_by_name(codec.constData());
     if (!c) {
         qDebug("subtitle avcodec_descriptor_get_by_name %s", codec.constData());
         const AVCodecDescriptor *desc = avcodec_descriptor_get_by_name(codec.constData());
@@ -370,8 +370,15 @@ bool SubtitleProcessorFFmpeg::processSubtitle()
         qWarning("no subtitle stream found");
         return false;
     }
+#if LIBAVCODEC_VERSION_MAJOR < 59
     codec_ctx = m_reader.subtitleCodecContext();
-    AVCodec *dec = avcodec_find_decoder(codec_ctx->codec_id);
+    const AVCodec *dec = avcodec_find_decoder(codec_ctx->codec_id);
+#else
+    AVCodecParameters *par = m_reader.subtitleCodecContext();
+    const AVCodec *dec = avcodec_find_decoder(par->codec_id);
+    codec_ctx = avcodec_alloc_context3(dec);
+    avcodec_parameters_to_context(codec_ctx, par);
+#endif
     const AVCodecDescriptor *dec_desc = avcodec_descriptor_get(codec_ctx->codec_id);
     if (!dec) {
         if (dec_desc)