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