1 /* 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 * 10 */ 11 12 #ifndef MODULES_VIDEO_CODING_CODECS_H264_H264_DECODER_IMPL_H_ 13 #define MODULES_VIDEO_CODING_CODECS_H264_H264_DECODER_IMPL_H_ 14 15 // Everything declared in this header is only required when WebRTC is 16 // build with H264 support, please do not move anything out of the 17 // #ifdef unless needed and tested. 18 #ifdef WEBRTC_USE_H264 19 20 #if defined(WEBRTC_WIN) && !defined(__clang__) 21 #error "See: bugs.webrtc.org/9213#c13." 22 #endif 23 24 #include <memory> 25 26 #include "modules/video_coding/codecs/h264/include/h264.h" 27 28 // CAVEAT: According to ffmpeg docs for avcodec_send_packet, ffmpeg requires a 29 // few extra padding bytes after the end of input. And in addition, docs for 30 // AV_INPUT_BUFFER_PADDING_SIZE says "If the first 23 bits of the additional 31 // bytes are not 0, then damaged MPEG bitstreams could cause overread and 32 // segfault." 33 // 34 // WebRTC doesn't ensure any such padding, and REQUIRES ffmpeg to be compiled 35 // with CONFIG_SAFE_BITSTREAM_READER, which is intended to eliminate 36 // out-of-bounds reads. ffmpeg docs doesn't say explicitly what effects this 37 // flag has on the h.264 decoder or avcodec_send_packet, though, so this is in 38 // some way depending on undocumented behavior. If any problems turn up, we may 39 // have to add an extra copy operation, to enforce padding before buffers are 40 // passed to ffmpeg. 41 42 extern "C" { 43 #include "third_party/ffmpeg/libavcodec/avcodec.h" 44 } // extern "C" 45 46 #include "common_video/h264/h264_bitstream_parser.h" 47 #include "common_video/include/i420_buffer_pool.h" 48 49 namespace webrtc { 50 51 struct AVCodecContextDeleter { operatorAVCodecContextDeleter52 void operator()(AVCodecContext* ptr) const { avcodec_free_context(&ptr); } 53 }; 54 struct AVFrameDeleter { operatorAVFrameDeleter55 void operator()(AVFrame* ptr) const { av_frame_free(&ptr); } 56 }; 57 58 class H264DecoderImpl : public H264Decoder { 59 public: 60 H264DecoderImpl(); 61 ~H264DecoderImpl() override; 62 63 // If |codec_settings| is NULL it is ignored. If it is not NULL, 64 // |codec_settings->codecType| must be |kVideoCodecH264|. 65 int32_t InitDecode(const VideoCodec* codec_settings, 66 int32_t number_of_cores) override; 67 int32_t Release() override; 68 69 int32_t RegisterDecodeCompleteCallback( 70 DecodedImageCallback* callback) override; 71 72 // |missing_frames|, |fragmentation| and |render_time_ms| are ignored. 73 int32_t Decode(const EncodedImage& input_image, 74 bool /*missing_frames*/, 75 int64_t render_time_ms = -1) override; 76 77 const char* ImplementationName() const override; 78 79 private: 80 // Called by FFmpeg when it needs a frame buffer to store decoded frames in. 81 // The |VideoFrame| returned by FFmpeg at |Decode| originate from here. Their 82 // buffers are reference counted and freed by FFmpeg using |AVFreeBuffer2|. 83 static int AVGetBuffer2(AVCodecContext* context, 84 AVFrame* av_frame, 85 int flags); 86 // Called by FFmpeg when it is done with a video frame, see |AVGetBuffer2|. 87 static void AVFreeBuffer2(void* opaque, uint8_t* data); 88 89 bool IsInitialized() const; 90 91 // Reports statistics with histograms. 92 void ReportInit(); 93 void ReportError(); 94 95 I420BufferPool pool_; 96 std::unique_ptr<AVCodecContext, AVCodecContextDeleter> av_context_; 97 std::unique_ptr<AVFrame, AVFrameDeleter> av_frame_; 98 99 DecodedImageCallback* decoded_image_callback_; 100 101 bool has_reported_init_; 102 bool has_reported_error_; 103 104 webrtc::H264BitstreamParser h264_bitstream_parser_; 105 }; 106 107 } // namespace webrtc 108 109 #endif // WEBRTC_USE_H264 110 111 #endif // MODULES_VIDEO_CODING_CODECS_H264_H264_DECODER_IMPL_H_ 112