Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / formats / mp2t / es_parser_h264.h
1 // Copyright 2014 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 MEDIA_FORMATS_MP2T_ES_PARSER_H264_H_
6 #define MEDIA_FORMATS_MP2T_ES_PARSER_H264_H_
7
8 #include <list>
9 #include <utility>
10
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/time/time.h"
16 #include "media/base/video_decoder_config.h"
17 #include "media/formats/mp2t/es_parser.h"
18
19 namespace media {
20 class H264Parser;
21 struct H264SPS;
22 class OffsetByteQueue;
23 }
24
25 namespace media {
26 namespace mp2t {
27
28 // Remark:
29 // In this h264 parser, frame splitting is based on AUD nals.
30 // Mpeg2 TS spec: "2.14 Carriage of Rec. ITU-T H.264 | ISO/IEC 14496-10 video"
31 // "Each AVC access unit shall contain an access unit delimiter NAL Unit;"
32 //
33 class EsParserH264 : public EsParser {
34  public:
35   typedef base::Callback<void(const VideoDecoderConfig&)> NewVideoConfigCB;
36
37   EsParserH264(const NewVideoConfigCB& new_video_config_cb,
38                const EmitBufferCB& emit_buffer_cb);
39   virtual ~EsParserH264();
40
41   // EsParser implementation.
42   virtual bool Parse(const uint8* buf, int size,
43                      base::TimeDelta pts,
44                      base::TimeDelta dts) OVERRIDE;
45   virtual void Flush() OVERRIDE;
46   virtual void Reset() OVERRIDE;
47
48  private:
49   struct TimingDesc {
50     base::TimeDelta dts;
51     base::TimeDelta pts;
52   };
53
54   // Find the AUD located at or after |*stream_pos|.
55   // Return true if an AUD is found.
56   // If found, |*stream_pos| corresponds to the position of the AUD start code
57   // in the stream. Otherwise, |*stream_pos| corresponds to the last position
58   // of the start code parser.
59   bool FindAUD(int64* stream_pos);
60
61   // Resumes the H264 ES parsing.
62   // Return true if successful.
63   bool ParseInternal();
64
65   // Emit a frame whose position in the ES queue starts at |access_unit_pos|.
66   // Returns true if successful, false if no PTS is available for the frame.
67   bool EmitFrame(int64 access_unit_pos, int access_unit_size,
68                  bool is_key_frame, int pps_id);
69
70   // Update the video decoder config based on an H264 SPS.
71   // Return true if successful.
72   bool UpdateVideoDecoderConfig(const H264SPS* sps);
73
74   // Callbacks to pass the stream configuration and the frames.
75   NewVideoConfigCB new_video_config_cb_;
76   EmitBufferCB emit_buffer_cb_;
77
78   // Bytes of the ES stream that have not been emitted yet.
79   scoped_ptr<media::OffsetByteQueue> es_queue_;
80   std::list<std::pair<int64, TimingDesc> > timing_desc_list_;
81
82   // H264 parser state.
83   // - |current_access_unit_pos_| is pointing to an annexB syncword
84   // representing the first NALU of an H264 access unit.
85   scoped_ptr<H264Parser> h264_parser_;
86   int64 current_access_unit_pos_;
87   int64 next_access_unit_pos_;
88
89   // Last video decoder config.
90   VideoDecoderConfig last_video_decoder_config_;
91 };
92
93 }  // namespace mp2t
94 }  // namespace media
95
96 #endif
97