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_adapter_video.h"
19#include "media/formats/mp2t/es_parser.h"
20
21namespace media {
22class H264Parser;
23struct H264SPS;
24class OffsetByteQueue;
25}
26
27namespace media {
28namespace mp2t {
29
30// A few remarks:
31// - In this h264 parser, frame splitting is based on AUD nals.
32// Mpeg2 TS spec: "2.14 Carriage of Rec. ITU-T H.264 | ISO/IEC 14496-10 video"
33// "Each AVC access unit shall contain an access unit delimiter NAL Unit;"
34// - PES packets do not necessarily map to an H264 access unit although the HLS
35// recommendation is to use one PES for each access unit. In this parser,
36// we handle the general case and do not make any assumption about the access
37// unit organization within PES packets.
38//
39class MEDIA_EXPORT EsParserH264 : public EsParser {
40 public:
41  typedef base::Callback<void(const VideoDecoderConfig&)> NewVideoConfigCB;
42
43  EsParserH264(const NewVideoConfigCB& new_video_config_cb,
44               const EmitBufferCB& emit_buffer_cb);
45  virtual ~EsParserH264();
46
47  // EsParser implementation.
48  virtual void Flush() OVERRIDE;
49
50 private:
51  // EsParser implementation.
52  virtual bool ParseFromEsQueue() OVERRIDE;
53  virtual void ResetInternal() OVERRIDE;
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  // Emit a frame whose position in the ES queue starts at |access_unit_pos|.
63  // Returns true if successful, false if no PTS is available for the frame.
64  bool EmitFrame(int64 access_unit_pos, int access_unit_size,
65                 bool is_key_frame, int pps_id);
66
67  // Update the video decoder config based on an H264 SPS.
68  // Return true if successful.
69  bool UpdateVideoDecoderConfig(const H264SPS* sps);
70
71  EsAdapterVideo es_adapter_;
72
73  // H264 parser state.
74  // - |current_access_unit_pos_| is pointing to an annexB syncword
75  // representing the first NALU of an H264 access unit.
76  scoped_ptr<H264Parser> h264_parser_;
77  int64 current_access_unit_pos_;
78  int64 next_access_unit_pos_;
79
80  // Last video decoder config.
81  VideoDecoderConfig last_video_decoder_config_;
82
83  DISALLOW_COPY_AND_ASSIGN(EsParserH264);
84};
85
86}  // namespace mp2t
87}  // namespace media
88
89#endif
90