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 #include <memory>
16 
17 #include "modules/video_coding/codecs/h264/include/h264.h"
18 
19 extern "C" {
20 #include "third_party/ffmpeg/libavcodec/avcodec.h"
21 }  // extern "C"
22 
23 #include "common_video/h264/h264_bitstream_parser.h"
24 #include "common_video/include/i420_buffer_pool.h"
25 
26 namespace webrtc {
27 
28 struct AVCodecContextDeleter {
operatorAVCodecContextDeleter29   void operator()(AVCodecContext* ptr) const { avcodec_free_context(&ptr); }
30 };
31 struct AVFrameDeleter {
operatorAVFrameDeleter32   void operator()(AVFrame* ptr) const { av_frame_free(&ptr); }
33 };
34 
35 class H264DecoderImpl : public H264Decoder {
36  public:
37   H264DecoderImpl();
38   ~H264DecoderImpl() override;
39 
40   // If |codec_settings| is NULL it is ignored. If it is not NULL,
41   // |codec_settings->codecType| must be |kVideoCodecH264|.
42   int32_t InitDecode(const VideoCodec* codec_settings,
43                      int32_t number_of_cores) override;
44   int32_t Release() override;
45 
46   int32_t RegisterDecodeCompleteCallback(
47       DecodedImageCallback* callback) override;
48 
49   // |missing_frames|, |fragmentation| and |render_time_ms| are ignored.
50   int32_t Decode(const EncodedImage& input_image,
51                  bool /*missing_frames*/,
52                  const RTPFragmentationHeader* /*fragmentation*/,
53                  const CodecSpecificInfo* codec_specific_info = nullptr,
54                  int64_t render_time_ms = -1) override;
55 
56   const char* ImplementationName() const override;
57 
58  private:
59   // Called by FFmpeg when it needs a frame buffer to store decoded frames in.
60   // The |VideoFrame| returned by FFmpeg at |Decode| originate from here. Their
61   // buffers are reference counted and freed by FFmpeg using |AVFreeBuffer2|.
62   static int AVGetBuffer2(
63       AVCodecContext* context, AVFrame* av_frame, int flags);
64   // Called by FFmpeg when it is done with a video frame, see |AVGetBuffer2|.
65   static void AVFreeBuffer2(void* opaque, uint8_t* data);
66 
67   bool IsInitialized() const;
68 
69   // Reports statistics with histograms.
70   void ReportInit();
71   void ReportError();
72 
73   I420BufferPool pool_;
74   std::unique_ptr<AVCodecContext, AVCodecContextDeleter> av_context_;
75   std::unique_ptr<AVFrame, AVFrameDeleter> av_frame_;
76 
77   DecodedImageCallback* decoded_image_callback_;
78 
79   bool has_reported_init_;
80   bool has_reported_error_;
81 
82   webrtc::H264BitstreamParser h264_bitstream_parser_;
83 };
84 
85 }  // namespace webrtc
86 
87 #endif  // MODULES_VIDEO_CODING_CODECS_H264_H264_DECODER_IMPL_H_
88