| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_FORMATS_MP2T_ES_PARSER_H264_H_ | 5 #ifndef MEDIA_FORMATS_MP2T_ES_PARSER_H264_H_ |
| 6 #define MEDIA_FORMATS_MP2T_ES_PARSER_H264_H_ | 6 #define MEDIA_FORMATS_MP2T_ES_PARSER_H264_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "media/base/media_export.h" | 16 #include "media/base/media_export.h" |
| 17 #include "media/base/video_decoder_config.h" | 17 #include "media/base/video_decoder_config.h" |
| 18 #include "media/formats/mp2t/es_adapter_video.h" | 18 #include "media/formats/mp2t/es_adapter_video.h" |
| 19 #include "media/formats/mp2t/es_parser.h" | 19 #include "media/formats/mp2t/es_parser.h" |
| 20 | 20 |
| 21 namespace media { | 21 namespace media { |
| 22 class H264Parser; | 22 class H264Parser; |
| 23 struct H264SPS; | 23 struct H264SPS; |
| 24 class OffsetByteQueue; | 24 class OffsetByteQueue; |
| 25 } | 25 } |
| 26 | 26 |
| 27 namespace media { | 27 namespace media { |
| 28 namespace mp2t { | 28 namespace mp2t { |
| 29 | 29 |
| 30 // Remark: | 30 // A few remarks: |
| 31 // In this h264 parser, frame splitting is based on AUD nals. | 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" | 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;" | 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. |
| 34 // | 38 // |
| 35 class MEDIA_EXPORT EsParserH264 : public EsParser { | 39 class MEDIA_EXPORT EsParserH264 : public EsParser { |
| 36 public: | 40 public: |
| 37 typedef base::Callback<void(const VideoDecoderConfig&)> NewVideoConfigCB; | 41 typedef base::Callback<void(const VideoDecoderConfig&)> NewVideoConfigCB; |
| 38 | 42 |
| 39 EsParserH264(const NewVideoConfigCB& new_video_config_cb, | 43 EsParserH264(const NewVideoConfigCB& new_video_config_cb, |
| 40 const EmitBufferCB& emit_buffer_cb); | 44 const EmitBufferCB& emit_buffer_cb); |
| 41 virtual ~EsParserH264(); | 45 virtual ~EsParserH264(); |
| 42 | 46 |
| 43 // EsParser implementation. | 47 // EsParser implementation. |
| 44 virtual bool Parse(const uint8* buf, int size, | |
| 45 base::TimeDelta pts, | |
| 46 DecodeTimestamp dts) OVERRIDE; | |
| 47 virtual void Flush() OVERRIDE; | 48 virtual void Flush() OVERRIDE; |
| 48 virtual void Reset() OVERRIDE; | |
| 49 | 49 |
| 50 private: | 50 private: |
| 51 struct TimingDesc { | 51 // EsParser implementation. |
| 52 DecodeTimestamp dts; | 52 virtual bool ParseFromEsQueue() OVERRIDE; |
| 53 base::TimeDelta pts; | 53 virtual void ResetInternal() OVERRIDE; |
| 54 }; | |
| 55 | 54 |
| 56 // Find the AUD located at or after |*stream_pos|. | 55 // Find the AUD located at or after |*stream_pos|. |
| 57 // Return true if an AUD is found. | 56 // Return true if an AUD is found. |
| 58 // If found, |*stream_pos| corresponds to the position of the AUD start code | 57 // If found, |*stream_pos| corresponds to the position of the AUD start code |
| 59 // in the stream. Otherwise, |*stream_pos| corresponds to the last position | 58 // in the stream. Otherwise, |*stream_pos| corresponds to the last position |
| 60 // of the start code parser. | 59 // of the start code parser. |
| 61 bool FindAUD(int64* stream_pos); | 60 bool FindAUD(int64* stream_pos); |
| 62 | 61 |
| 63 // Resumes the H264 ES parsing. | |
| 64 // Return true if successful. | |
| 65 bool ParseInternal(); | |
| 66 | |
| 67 // Emit a frame whose position in the ES queue starts at |access_unit_pos|. | 62 // Emit a frame whose position in the ES queue starts at |access_unit_pos|. |
| 68 // Returns true if successful, false if no PTS is available for the frame. | 63 // Returns true if successful, false if no PTS is available for the frame. |
| 69 bool EmitFrame(int64 access_unit_pos, int access_unit_size, | 64 bool EmitFrame(int64 access_unit_pos, int access_unit_size, |
| 70 bool is_key_frame, int pps_id); | 65 bool is_key_frame, int pps_id); |
| 71 | 66 |
| 72 // Update the video decoder config based on an H264 SPS. | 67 // Update the video decoder config based on an H264 SPS. |
| 73 // Return true if successful. | 68 // Return true if successful. |
| 74 bool UpdateVideoDecoderConfig(const H264SPS* sps); | 69 bool UpdateVideoDecoderConfig(const H264SPS* sps); |
| 75 | 70 |
| 76 EsAdapterVideo es_adapter_; | 71 EsAdapterVideo es_adapter_; |
| 77 | 72 |
| 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. | 73 // H264 parser state. |
| 83 // - |current_access_unit_pos_| is pointing to an annexB syncword | 74 // - |current_access_unit_pos_| is pointing to an annexB syncword |
| 84 // representing the first NALU of an H264 access unit. | 75 // representing the first NALU of an H264 access unit. |
| 85 scoped_ptr<H264Parser> h264_parser_; | 76 scoped_ptr<H264Parser> h264_parser_; |
| 86 int64 current_access_unit_pos_; | 77 int64 current_access_unit_pos_; |
| 87 int64 next_access_unit_pos_; | 78 int64 next_access_unit_pos_; |
| 88 | 79 |
| 89 // Last video decoder config. | 80 // Last video decoder config. |
| 90 VideoDecoderConfig last_video_decoder_config_; | 81 VideoDecoderConfig last_video_decoder_config_; |
| 91 | 82 |
| 92 DISALLOW_COPY_AND_ASSIGN(EsParserH264); | 83 DISALLOW_COPY_AND_ASSIGN(EsParserH264); |
| 93 }; | 84 }; |
| 94 | 85 |
| 95 } // namespace mp2t | 86 } // namespace mp2t |
| 96 } // namespace media | 87 } // namespace media |
| 97 | 88 |
| 98 #endif | 89 #endif |
| OLD | NEW |