xref: /aosp_15_r20/external/webrtc/modules/video_coding/codecs/h264/h264_decoder_impl.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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/video_frame_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   bool Configure(const Settings& settings) override;
64   int32_t Release() override;
65 
66   int32_t RegisterDecodeCompleteCallback(
67       DecodedImageCallback* callback) override;
68 
69   // `missing_frames`, `fragmentation` and `render_time_ms` are ignored.
70   int32_t Decode(const EncodedImage& input_image,
71                  bool /*missing_frames*/,
72                  int64_t render_time_ms = -1) override;
73 
74   const char* ImplementationName() const override;
75 
76  private:
77   // Called by FFmpeg when it needs a frame buffer to store decoded frames in.
78   // The `VideoFrame` returned by FFmpeg at `Decode` originate from here. Their
79   // buffers are reference counted and freed by FFmpeg using `AVFreeBuffer2`.
80   static int AVGetBuffer2(AVCodecContext* context,
81                           AVFrame* av_frame,
82                           int flags);
83   // Called by FFmpeg when it is done with a video frame, see `AVGetBuffer2`.
84   static void AVFreeBuffer2(void* opaque, uint8_t* data);
85 
86   bool IsInitialized() const;
87 
88   // Reports statistics with histograms.
89   void ReportInit();
90   void ReportError();
91 
92   // Used by ffmpeg via `AVGetBuffer2()` to allocate I420 images.
93   VideoFrameBufferPool ffmpeg_buffer_pool_;
94   std::unique_ptr<AVCodecContext, AVCodecContextDeleter> av_context_;
95   std::unique_ptr<AVFrame, AVFrameDeleter> av_frame_;
96 
97   DecodedImageCallback* decoded_image_callback_;
98 
99   bool has_reported_init_;
100   bool has_reported_error_;
101 
102   webrtc::H264BitstreamParser h264_bitstream_parser_;
103 };
104 
105 }  // namespace webrtc
106 
107 #endif  // WEBRTC_USE_H264
108 
109 #endif  // MODULES_VIDEO_CODING_CODECS_H264_H264_DECODER_IMPL_H_
110