summaryrefslogtreecommitdiff
path: root/recipes-support/opencv/opencv/0005-cap_ffmpeg-use-avcodec_encode_video2-where-available.patch
blob: 71c5001a767aef514c81e3945bfe7c67aa136944 (plain)
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
From aecaa02aba9f1b4e356127b458e464d1feacf599 Mon Sep 17 00:00:00 2001
From: Anton Khirnov <anton@khirnov.net>
Date: Thu, 6 Feb 2014 10:08:17 +0100
Subject: [PATCH 5/5] cap_ffmpeg: use avcodec_encode_video2() where available

avcodec_encode_video() has been deprecated and removed in newer
libavcodec versions.

(cherry picked from commit 8dbc0ac766f2342bd3004a1459012f65004042a6)
---
 modules/highgui/src/cap_ffmpeg_impl.hpp | 44 +++++++++++++++++++++++----------
 1 file changed, 31 insertions(+), 13 deletions(-)

diff --git a/modules/highgui/src/cap_ffmpeg_impl.hpp b/modules/highgui/src/cap_ffmpeg_impl.hpp
index 44ef553..4ac2f84 100644
--- a/modules/highgui/src/cap_ffmpeg_impl.hpp
+++ b/modules/highgui/src/cap_ffmpeg_impl.hpp
@@ -1136,7 +1136,6 @@ static const int OPENCV_NO_FRAMES_WRITTEN_CODE = 1000;
 static int icv_av_write_frame_FFMPEG( AVFormatContext * oc, AVStream * video_st, uint8_t * outbuf, uint32_t outbuf_size, AVFrame * picture )
 {
     AVCodecContext * c = video_st->codec;
-    int out_size;
     int ret = 0;
 
     if (oc->oformat->flags & AVFMT_RAWPICTURE) {
@@ -1156,20 +1155,39 @@ static int icv_av_write_frame_FFMPEG( AVFormatContext * oc, AVStream * video_st,
 
         ret = av_write_frame(oc, &pkt);
     } else {
+        AVPacket pkt;
+        int got_output;
+
+        av_init_packet(&pkt);
+#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 1, 0)
         /* encode the image */
-        out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
-        /* if zero size, it means the image was buffered */
-        if (out_size > 0) {
-            AVPacket pkt;
-            av_init_packet(&pkt);
-
-            if(c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
-                pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
-            if(c->coded_frame->key_frame)
-                pkt.flags |= PKT_FLAG_KEY;
+        int out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
+        got_output = out_size > 0;
+        pkt.data   = outbuf;
+        pkt.size   = out_size;
+        if(c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
+            pkt.pts = c->coded_frame->pts;
+        pkt.dts = AV_NOPTS_VALUE;
+        if(c->coded_frame->key_frame)
+            pkt.flags |= PKT_FLAG_KEY;
+#else
+        pkt.data = NULL;
+        pkt.size = 0;
+
+        ret = avcodec_encode_video2(c, &pkt, picture, &got_output);
+        if (ret < 0)
+            got_output = 0;
+#endif
+
+        if (got_output) {
+            if (pkt.pts != (int64_t)AV_NOPTS_VALUE)
+                pkt.pts = av_rescale_q(pkt.pts, c->time_base, video_st->time_base);
+            if (pkt.dts != (int64_t)AV_NOPTS_VALUE)
+                pkt.dts = av_rescale_q(pkt.dts, c->time_base, video_st->time_base);
+            if (pkt.duration)
+                pkt.duration = av_rescale_q(pkt.duration, c->time_base, video_st->time_base);
+
             pkt.stream_index= video_st->index;
-            pkt.data= outbuf;
-            pkt.size= out_size;
 
             /* write the compressed frame in the media file */
             ret = av_write_frame(oc, &pkt);
-- 
1.9.3