Home | History | Annotate | Download | only in vda
      1 // Copyright 2015 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef VP8_DECODER_H_
      6 #define VP8_DECODER_H_
      7 
      8 #include <stddef.h>
      9 #include <stdint.h>
     10 
     11 #include <memory>
     12 
     13 #include "base/macros.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "accelerated_video_decoder.h"
     16 #include "size.h"
     17 #include "vp8_parser.h"
     18 #include "vp8_picture.h"
     19 
     20 namespace media {
     21 
     22 // Clients of this class are expected to pass raw VP8 stream and are expected
     23 // to provide an implementation of VP8Accelerator for offloading final steps
     24 // of the decoding process.
     25 //
     26 // This class must be created, called and destroyed on a single thread, and
     27 // does nothing internally on any other thread.
     28 class VP8Decoder : public AcceleratedVideoDecoder {
     29  public:
     30   class VP8Accelerator {
     31    public:
     32     VP8Accelerator();
     33     virtual ~VP8Accelerator();
     34 
     35     // Create a new VP8Picture that the decoder client can use for decoding
     36     // and pass back to this accelerator for decoding or reference.
     37     // When the picture is no longer needed by decoder, it will just drop
     38     // its reference to it, and it may do so at any time.
     39     // Note that this may return nullptr if accelerator is not able to provide
     40     // any new pictures at given time. The decoder is expected to handle
     41     // this situation as normal and return from Decode() with kRanOutOfSurfaces.
     42     virtual scoped_refptr<VP8Picture> CreateVP8Picture() = 0;
     43 
     44     // Submit decode for |pic|, taking as arguments |frame_hdr| with parsed
     45     // VP8 frame header information for current frame, and using |last_frame|,
     46     // |golden_frame| and |alt_frame| as references, as per VP8 specification.
     47     // Note that this runs the decode in hardware.
     48     // Return true if successful.
     49     virtual bool SubmitDecode(const scoped_refptr<VP8Picture>& pic,
     50                               const Vp8FrameHeader* frame_hdr,
     51                               const scoped_refptr<VP8Picture>& last_frame,
     52                               const scoped_refptr<VP8Picture>& golden_frame,
     53                               const scoped_refptr<VP8Picture>& alt_frame) = 0;
     54 
     55     // Schedule output (display) of |pic|. Note that returning from this
     56     // method does not mean that |pic| has already been outputted (displayed),
     57     // but guarantees that all pictures will be outputted in the same order
     58     // as this method was called for them. Decoder may drop its reference
     59     // to |pic| after calling this method.
     60     // Return true if successful.
     61     virtual bool OutputPicture(const scoped_refptr<VP8Picture>& pic) = 0;
     62 
     63    private:
     64     DISALLOW_COPY_AND_ASSIGN(VP8Accelerator);
     65   };
     66 
     67   VP8Decoder(VP8Accelerator* accelerator);
     68   ~VP8Decoder() override;
     69 
     70   // AcceleratedVideoDecoder implementation.
     71   bool Flush() override WARN_UNUSED_RESULT;
     72   void Reset() override;
     73   void SetStream(const uint8_t* ptr, size_t size) override;
     74   DecodeResult Decode() override WARN_UNUSED_RESULT;
     75   Size GetPicSize() const override;
     76   size_t GetRequiredNumOfPictures() const override;
     77 
     78  private:
     79   bool DecodeAndOutputCurrentFrame();
     80   void RefreshReferenceFrames();
     81 
     82   enum State {
     83     kNeedStreamMetadata,  // After initialization, need a keyframe.
     84     kDecoding,            // Ready to decode from any point.
     85     kAfterReset,          // After Reset(), need a resume point.
     86     kError,               // Error in decode, can't continue.
     87   };
     88 
     89   State state_;
     90 
     91   Vp8Parser parser_;
     92 
     93   std::unique_ptr<Vp8FrameHeader> curr_frame_hdr_;
     94   scoped_refptr<VP8Picture> curr_pic_;
     95   scoped_refptr<VP8Picture> last_frame_;
     96   scoped_refptr<VP8Picture> golden_frame_;
     97   scoped_refptr<VP8Picture> alt_frame_;
     98 
     99   const uint8_t* curr_frame_start_;
    100   size_t frame_size_;
    101 
    102   Size pic_size_;
    103   int horizontal_scale_;
    104   int vertical_scale_;
    105 
    106   VP8Accelerator* accelerator_;
    107 
    108   DISALLOW_COPY_AND_ASSIGN(VP8Decoder);
    109 };
    110 
    111 }  // namespace media
    112 
    113 #endif  // VP8_DECODER_H_
    114