]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/video/video_decoder.cpp
ffmpeg dshow: Fix packet life preserver in video_decoder and revert move of packet...
[casparcg] / modules / ffmpeg / producer / video / video_decoder.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../../stdafx.h"\r
23 \r
24 #include "video_decoder.h"\r
25 \r
26 #include "../util/util.h"\r
27 \r
28 #include "../../ffmpeg_error.h"\r
29 \r
30 #include <core/producer/frame/frame_transform.h>\r
31 #include <core/producer/frame/frame_factory.h>\r
32 \r
33 #include <boost/range/algorithm_ext/push_back.hpp>\r
34 #include <boost/filesystem.hpp>\r
35 \r
36 #include <queue>\r
37 \r
38 #if defined(_MSC_VER)\r
39 #pragma warning (push)\r
40 #pragma warning (disable : 4244)\r
41 #endif\r
42 extern "C" \r
43 {\r
44         #include <libavcodec/avcodec.h>\r
45         #include <libavformat/avformat.h>\r
46 }\r
47 #if defined(_MSC_VER)\r
48 #pragma warning (pop)\r
49 #endif\r
50 \r
51 namespace caspar { namespace ffmpeg {\r
52         \r
53 struct video_decoder::implementation : boost::noncopyable\r
54 {\r
55         int                                                                             index_;\r
56         const safe_ptr<AVCodecContext>                  codec_context_;\r
57 \r
58         std::queue<safe_ptr<AVPacket>>                  packets_;\r
59         \r
60         const uint32_t                                                  nb_frames_;\r
61 \r
62         const size_t                                                    width_;\r
63         const size_t                                                    height_;\r
64         bool                                                                    is_progressive_;\r
65 \r
66         tbb::atomic<size_t>                                             file_frame_number_;\r
67 \r
68 public:\r
69         explicit implementation(const safe_ptr<AVFormatContext>& context) \r
70                 : codec_context_(open_codec(*context, AVMEDIA_TYPE_VIDEO, index_))\r
71                 , nb_frames_(static_cast<uint32_t>(context->streams[index_]->nb_frames))\r
72                 , width_(codec_context_->width)\r
73                 , height_(codec_context_->height)\r
74         {\r
75                 file_frame_number_ = 0;\r
76         }\r
77 \r
78         void push(const std::shared_ptr<AVPacket>& packet)\r
79         {\r
80                 if(!packet)\r
81                         return;\r
82 \r
83                 if(packet->stream_index == index_ || packet->data == nullptr)\r
84                         packets_.push(make_safe_ptr(packet));\r
85         }\r
86 \r
87         std::shared_ptr<AVFrame> poll()\r
88         {               \r
89                 if(packets_.empty())\r
90                         return nullptr;\r
91                 \r
92                 auto packet = packets_.front();\r
93                                         \r
94                 if(packet->data == nullptr)\r
95                 {                       \r
96                         if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)\r
97                         {\r
98                                 auto video = decode(packet);\r
99                                 if(video)\r
100                                         return video;\r
101                         }\r
102                                         \r
103                         packets_.pop();\r
104                         file_frame_number_ = static_cast<size_t>(packet->pos);\r
105                         avcodec_flush_buffers(codec_context_.get());\r
106                         return flush_video();   \r
107                 }\r
108                         \r
109                 packets_.pop();\r
110                 return decode(packet);\r
111         }\r
112 \r
113         std::shared_ptr<AVFrame> decode(safe_ptr<AVPacket> pkt)\r
114         {\r
115                 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);\r
116 \r
117                 int frame_finished = 0;\r
118                 THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, pkt.get()), "[video_decoder]");\r
119                 \r
120                 // If a decoder consumes less then the whole packet then something is wrong\r
121                 // that might be just harmless padding at the end, or a problem with the\r
122                 // AVParser or demuxer which puted more then one frame in a AVPacket.\r
123 \r
124                 if(frame_finished == 0) \r
125                         return nullptr;\r
126 \r
127                 is_progressive_ = !decoded_frame->interlaced_frame;\r
128 \r
129                 if(decoded_frame->repeat_pict > 0)\r
130                         CASPAR_LOG(warning) << "[video_decoder] Field repeat_pict not implemented.";\r
131                 \r
132                 ++file_frame_number_;\r
133 \r
134                 // This ties the life of the decoded_frame to the packet that it came from. For the\r
135                 // current version of ffmpeg (0.8 or c17808c) the RAW_VIDEO codec returns frame data\r
136                 // owned by the packet.\r
137                 return std::shared_ptr<AVFrame>(decoded_frame.get(), [decoded_frame, pkt](AVFrame*){});\r
138         }\r
139         \r
140         bool ready() const\r
141         {\r
142                 return packets_.size() >= 8;\r
143         }\r
144 \r
145         uint32_t nb_frames() const\r
146         {\r
147                 return std::max<uint32_t>(nb_frames_, file_frame_number_);\r
148         }\r
149 \r
150         std::wstring print() const\r
151         {               \r
152                 return L"[video-decoder] " + widen(codec_context_->codec->long_name);\r
153         }\r
154 };\r
155 \r
156 video_decoder::video_decoder(const safe_ptr<AVFormatContext>& context) : impl_(new implementation(context)){}\r
157 void video_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}\r
158 std::shared_ptr<AVFrame> video_decoder::poll(){return impl_->poll();}\r
159 bool video_decoder::ready() const{return impl_->ready();}\r
160 size_t video_decoder::width() const{return impl_->width_;}\r
161 size_t video_decoder::height() const{return impl_->height_;}\r
162 uint32_t video_decoder::nb_frames() const{return impl_->nb_frames();}\r
163 uint32_t video_decoder::file_frame_number() const{return impl_->file_frame_number_;}\r
164 bool    video_decoder::is_progressive() const{return impl_->is_progressive_;}\r
165 std::wstring video_decoder::print() const{return impl_->print();}\r
166 \r
167 }}