vp8_parser.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. // This file contains an implementation of a VP8 raw stream parser,
  6. // as defined in RFC 6386.
  7. #ifndef MEDIA_PARSERS_VP8_PARSER_H_
  8. #define MEDIA_PARSERS_VP8_PARSER_H_
  9. #include <stddef.h>
  10. #include <stdint.h>
  11. #include "media/parsers/media_parsers_export.h"
  12. #include "media/parsers/vp8_bool_decoder.h"
  13. namespace media {
  14. // See spec for definitions of values/fields.
  15. const size_t kMaxMBSegments = 4;
  16. const size_t kNumMBFeatureTreeProbs = 3;
  17. // Member of Vp8FrameHeader and will be 0-initialized
  18. // in Vp8FrameHeader's constructor.
  19. struct Vp8SegmentationHeader {
  20. enum SegmentFeatureMode { FEATURE_MODE_DELTA = 0, FEATURE_MODE_ABSOLUTE = 1 };
  21. bool segmentation_enabled;
  22. bool update_mb_segmentation_map;
  23. bool update_segment_feature_data;
  24. SegmentFeatureMode segment_feature_mode;
  25. int8_t quantizer_update_value[kMaxMBSegments];
  26. int8_t lf_update_value[kMaxMBSegments];
  27. static const int kDefaultSegmentProb = 255;
  28. uint8_t segment_prob[kNumMBFeatureTreeProbs];
  29. };
  30. const size_t kNumBlockContexts = 4;
  31. // Member of Vp8FrameHeader and will be 0-initialized
  32. // in Vp8FrameHeader's constructor.
  33. struct Vp8LoopFilterHeader {
  34. enum Type { LOOP_FILTER_TYPE_NORMAL = 0, LOOP_FILTER_TYPE_SIMPLE = 1 };
  35. Type type;
  36. uint8_t level;
  37. uint8_t sharpness_level;
  38. bool loop_filter_adj_enable;
  39. bool mode_ref_lf_delta_update;
  40. int8_t ref_frame_delta[kNumBlockContexts];
  41. int8_t mb_mode_delta[kNumBlockContexts];
  42. };
  43. // Member of Vp8FrameHeader and will be 0-initialized
  44. // in Vp8FrameHeader's constructor.
  45. struct Vp8QuantizationHeader {
  46. uint8_t y_ac_qi;
  47. int8_t y_dc_delta;
  48. int8_t y2_dc_delta;
  49. int8_t y2_ac_delta;
  50. int8_t uv_dc_delta;
  51. int8_t uv_ac_delta;
  52. };
  53. const size_t kNumBlockTypes = 4;
  54. const size_t kNumCoeffBands = 8;
  55. const size_t kNumPrevCoeffContexts = 3;
  56. const size_t kNumEntropyNodes = 11;
  57. const size_t kNumMVContexts = 2;
  58. const size_t kNumMVProbs = 19;
  59. const size_t kNumYModeProbs = 4;
  60. const size_t kNumUVModeProbs = 3;
  61. // Member of Vp8FrameHeader and will be 0-initialized
  62. // in Vp8FrameHeader's constructor.
  63. struct Vp8EntropyHeader {
  64. uint8_t coeff_probs[kNumBlockTypes][kNumCoeffBands][kNumPrevCoeffContexts]
  65. [kNumEntropyNodes];
  66. uint8_t y_mode_probs[kNumYModeProbs];
  67. uint8_t uv_mode_probs[kNumUVModeProbs];
  68. uint8_t mv_probs[kNumMVContexts][kNumMVProbs];
  69. };
  70. const size_t kMaxDCTPartitions = 8;
  71. const size_t kNumVp8ReferenceBuffers = 3;
  72. enum Vp8RefType : size_t {
  73. VP8_FRAME_LAST = 0,
  74. VP8_FRAME_GOLDEN = 1,
  75. VP8_FRAME_ALTREF = 2,
  76. };
  77. struct MEDIA_PARSERS_EXPORT Vp8FrameHeader {
  78. Vp8FrameHeader();
  79. ~Vp8FrameHeader();
  80. Vp8FrameHeader& operator=(const Vp8FrameHeader&);
  81. Vp8FrameHeader(const Vp8FrameHeader&);
  82. enum FrameType { KEYFRAME = 0, INTERFRAME = 1 };
  83. bool IsKeyframe() const { return frame_type == KEYFRAME; }
  84. enum GoldenRefreshMode {
  85. NO_GOLDEN_REFRESH = 0,
  86. COPY_LAST_TO_GOLDEN = 1,
  87. COPY_ALT_TO_GOLDEN = 2,
  88. };
  89. enum AltRefreshMode {
  90. NO_ALT_REFRESH = 0,
  91. COPY_LAST_TO_ALT = 1,
  92. COPY_GOLDEN_TO_ALT = 2,
  93. };
  94. FrameType frame_type;
  95. uint8_t version;
  96. bool is_experimental;
  97. bool show_frame;
  98. size_t first_part_size;
  99. uint16_t width;
  100. uint8_t horizontal_scale;
  101. uint16_t height;
  102. uint8_t vertical_scale;
  103. Vp8SegmentationHeader segmentation_hdr;
  104. Vp8LoopFilterHeader loopfilter_hdr;
  105. Vp8QuantizationHeader quantization_hdr;
  106. size_t num_of_dct_partitions;
  107. Vp8EntropyHeader entropy_hdr;
  108. bool refresh_entropy_probs;
  109. bool refresh_golden_frame;
  110. bool refresh_alternate_frame;
  111. GoldenRefreshMode copy_buffer_to_golden;
  112. AltRefreshMode copy_buffer_to_alternate;
  113. uint8_t sign_bias_golden;
  114. uint8_t sign_bias_alternate;
  115. bool refresh_last;
  116. bool mb_no_skip_coeff;
  117. uint8_t prob_skip_false;
  118. uint8_t prob_intra;
  119. uint8_t prob_last;
  120. uint8_t prob_gf;
  121. const uint8_t* data;
  122. size_t frame_size;
  123. size_t dct_partition_sizes[kMaxDCTPartitions];
  124. // Offset in bytes from data.
  125. off_t first_part_offset;
  126. // Offset in bits from first_part_offset.
  127. off_t macroblock_bit_offset;
  128. // Bool decoder state
  129. uint8_t bool_dec_range;
  130. uint8_t bool_dec_value;
  131. uint8_t bool_dec_count;
  132. // Color range information.
  133. bool is_full_range;
  134. };
  135. // A parser for raw VP8 streams as specified in RFC 6386.
  136. class MEDIA_PARSERS_EXPORT Vp8Parser {
  137. public:
  138. Vp8Parser();
  139. Vp8Parser(const Vp8Parser&) = delete;
  140. Vp8Parser& operator=(const Vp8Parser&) = delete;
  141. ~Vp8Parser();
  142. // Try to parse exactly one VP8 frame starting at |ptr| and of size |size|,
  143. // filling the parsed data in |fhdr|. Return true on success.
  144. // Size has to be exactly the size of the frame and coming from the caller,
  145. // who needs to acquire it from elsewhere (normally from a container).
  146. bool ParseFrame(const uint8_t* ptr, size_t size, Vp8FrameHeader* fhdr);
  147. private:
  148. bool ParseFrameTag(Vp8FrameHeader* fhdr);
  149. bool ParseFrameHeader(Vp8FrameHeader* fhdr);
  150. bool ParseSegmentationHeader(bool keyframe);
  151. bool ParseLoopFilterHeader(bool keyframe);
  152. bool ParseQuantizationHeader(Vp8QuantizationHeader* qhdr);
  153. bool ParseTokenProbs(Vp8EntropyHeader* ehdr, bool update_curr_probs);
  154. bool ParseIntraProbs(Vp8EntropyHeader* ehdr,
  155. bool update_curr_probs,
  156. bool keyframe);
  157. bool ParseMVProbs(Vp8EntropyHeader* ehdr, bool update_curr_probs);
  158. bool ParsePartitions(Vp8FrameHeader* fhdr);
  159. void ResetProbs();
  160. // These persist across calls to ParseFrame() and may be used and/or updated
  161. // for subsequent frames if the stream instructs us to do so.
  162. Vp8SegmentationHeader curr_segmentation_hdr_;
  163. Vp8LoopFilterHeader curr_loopfilter_hdr_;
  164. Vp8EntropyHeader curr_entropy_hdr_;
  165. const uint8_t* stream_;
  166. size_t bytes_left_;
  167. Vp8BoolDecoder bd_;
  168. };
  169. } // namespace media
  170. #endif // MEDIA_PARSERS_VP8_PARSER_H_