Home | History | Annotate | Download | only in mp2t
      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 #include "media/formats/mp2t/es_parser_mpeg1audio.h"
      6 
      7 #include <list>
      8 
      9 #include "base/basictypes.h"
     10 #include "base/bind.h"
     11 #include "base/logging.h"
     12 #include "base/strings/string_number_conversions.h"
     13 #include "media/base/audio_timestamp_helper.h"
     14 #include "media/base/bit_reader.h"
     15 #include "media/base/buffers.h"
     16 #include "media/base/channel_layout.h"
     17 #include "media/base/stream_parser_buffer.h"
     18 #include "media/formats/common/offset_byte_queue.h"
     19 #include "media/formats/mp2t/mp2t_common.h"
     20 #include "media/formats/mpeg/mpeg1_audio_stream_parser.h"
     21 
     22 namespace media {
     23 namespace mp2t {
     24 
     25 struct EsParserMpeg1Audio::Mpeg1AudioFrame {
     26   // Pointer to the ES data.
     27   const uint8* data;
     28 
     29   // Frame size.
     30   int size;
     31 
     32   // Number of samples in the frame.
     33   int sample_count;
     34 
     35   // Frame offset in the ES queue.
     36   int64 queue_offset;
     37 };
     38 
     39 EsParserMpeg1Audio::EsParserMpeg1Audio(
     40     const NewAudioConfigCB& new_audio_config_cb,
     41     const EmitBufferCB& emit_buffer_cb,
     42     const LogCB& log_cb)
     43   : log_cb_(log_cb),
     44     new_audio_config_cb_(new_audio_config_cb),
     45     emit_buffer_cb_(emit_buffer_cb) {
     46 }
     47 
     48 EsParserMpeg1Audio::~EsParserMpeg1Audio() {
     49 }
     50 
     51 bool EsParserMpeg1Audio::ParseFromEsQueue() {
     52   // Look for every MPEG1 audio frame in the ES buffer.
     53   Mpeg1AudioFrame mpeg1audio_frame;
     54   while (LookForMpeg1AudioFrame(&mpeg1audio_frame)) {
     55     // Update the audio configuration if needed.
     56     DCHECK_GE(mpeg1audio_frame.size, MPEG1AudioStreamParser::kHeaderSize);
     57     if (!UpdateAudioConfiguration(mpeg1audio_frame.data))
     58       return false;
     59 
     60     // Get the PTS & the duration of this access unit.
     61     TimingDesc current_timing_desc =
     62         GetTimingDescriptor(mpeg1audio_frame.queue_offset);
     63     if (current_timing_desc.pts != kNoTimestamp())
     64       audio_timestamp_helper_->SetBaseTimestamp(current_timing_desc.pts);
     65 
     66     if (audio_timestamp_helper_->base_timestamp() == kNoTimestamp()) {
     67       DVLOG(1) << "Audio frame with unknown timestamp";
     68       return false;
     69     }
     70     base::TimeDelta current_pts = audio_timestamp_helper_->GetTimestamp();
     71     base::TimeDelta frame_duration =
     72         audio_timestamp_helper_->GetFrameDuration(
     73             mpeg1audio_frame.sample_count);
     74 
     75     // Emit an audio frame.
     76     bool is_key_frame = true;
     77 
     78     // TODO(wolenetz/acolwell): Validate and use a common cross-parser TrackId
     79     // type and allow multiple audio tracks. See https://crbug.com/341581.
     80     scoped_refptr<StreamParserBuffer> stream_parser_buffer =
     81         StreamParserBuffer::CopyFrom(
     82             mpeg1audio_frame.data,
     83             mpeg1audio_frame.size,
     84             is_key_frame,
     85             DemuxerStream::AUDIO, 0);
     86     stream_parser_buffer->set_timestamp(current_pts);
     87     stream_parser_buffer->set_duration(frame_duration);
     88     emit_buffer_cb_.Run(stream_parser_buffer);
     89 
     90     // Update the PTS of the next frame.
     91     audio_timestamp_helper_->AddFrames(mpeg1audio_frame.sample_count);
     92 
     93     // Skip the current frame.
     94     SkipMpeg1AudioFrame(mpeg1audio_frame);
     95   }
     96 
     97   return true;
     98 }
     99 
    100 void EsParserMpeg1Audio::Flush() {
    101 }
    102 
    103 void EsParserMpeg1Audio::ResetInternal() {
    104   last_audio_decoder_config_ = AudioDecoderConfig();
    105 }
    106 
    107 bool EsParserMpeg1Audio::LookForMpeg1AudioFrame(
    108     Mpeg1AudioFrame* mpeg1audio_frame) {
    109   int es_size;
    110   const uint8* es;
    111   es_queue_->Peek(&es, &es_size);
    112 
    113   int max_offset = es_size - MPEG1AudioStreamParser::kHeaderSize;
    114   if (max_offset <= 0)
    115     return false;
    116 
    117   for (int offset = 0; offset < max_offset; offset++) {
    118     const uint8* cur_buf = &es[offset];
    119     if (cur_buf[0] != 0xff)
    120       continue;
    121 
    122     int remaining_size = es_size - offset;
    123     DCHECK_GE(remaining_size, MPEG1AudioStreamParser::kHeaderSize);
    124     MPEG1AudioStreamParser::Header header;
    125     if (!MPEG1AudioStreamParser::ParseHeader(log_cb_, cur_buf, &header))
    126       continue;
    127 
    128     if (remaining_size < header.frame_size) {
    129       // Not a full frame: will resume when we have more data.
    130       // Remove all the bytes located before the frame header,
    131       // these bytes will not be used anymore.
    132       es_queue_->Pop(offset);
    133       return false;
    134     }
    135 
    136     // Check whether there is another frame
    137     // |frame_size| apart from the current one.
    138     if (remaining_size >= header.frame_size + 1 &&
    139         cur_buf[header.frame_size] != 0xff) {
    140       continue;
    141     }
    142 
    143     es_queue_->Pop(offset);
    144     es_queue_->Peek(&mpeg1audio_frame->data, &es_size);
    145     mpeg1audio_frame->queue_offset = es_queue_->head();
    146     mpeg1audio_frame->size = header.frame_size;
    147     mpeg1audio_frame->sample_count = header.sample_count;
    148     DVLOG(LOG_LEVEL_ES)
    149         << "MPEG1 audio syncword @ pos=" << mpeg1audio_frame->queue_offset
    150         << " frame_size=" << mpeg1audio_frame->size;
    151     DVLOG(LOG_LEVEL_ES)
    152         << "MPEG1 audio header: "
    153         << base::HexEncode(mpeg1audio_frame->data,
    154                            MPEG1AudioStreamParser::kHeaderSize);
    155     return true;
    156   }
    157 
    158   es_queue_->Pop(max_offset);
    159   return false;
    160 }
    161 
    162 bool EsParserMpeg1Audio::UpdateAudioConfiguration(
    163     const uint8* mpeg1audio_header) {
    164   MPEG1AudioStreamParser::Header header;
    165   if (!MPEG1AudioStreamParser::ParseHeader(log_cb_,
    166                                            mpeg1audio_header,
    167                                            &header)) {
    168     return false;
    169   }
    170 
    171   // TODO(damienv): Verify whether Android playback requires the extra data
    172   // field for Mpeg1 audio. If yes, we should generate this field.
    173   AudioDecoderConfig audio_decoder_config(
    174       kCodecMP3,
    175       kSampleFormatS16,
    176       header.channel_layout,
    177       header.sample_rate,
    178       NULL, 0,
    179       false);
    180 
    181   if (!audio_decoder_config.Matches(last_audio_decoder_config_)) {
    182     DVLOG(1) << "Sampling frequency: " << header.sample_rate;
    183     DVLOG(1) << "Channel layout: " << header.channel_layout;
    184     // Reset the timestamp helper to use a new time scale.
    185     if (audio_timestamp_helper_ &&
    186         audio_timestamp_helper_->base_timestamp() != kNoTimestamp()) {
    187       base::TimeDelta base_timestamp = audio_timestamp_helper_->GetTimestamp();
    188       audio_timestamp_helper_.reset(
    189         new AudioTimestampHelper(header.sample_rate));
    190       audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
    191     } else {
    192       audio_timestamp_helper_.reset(
    193           new AudioTimestampHelper(header.sample_rate));
    194     }
    195     // Audio config notification.
    196     last_audio_decoder_config_ = audio_decoder_config;
    197     new_audio_config_cb_.Run(audio_decoder_config);
    198   }
    199 
    200   return true;
    201 }
    202 
    203 void EsParserMpeg1Audio::SkipMpeg1AudioFrame(
    204     const Mpeg1AudioFrame& mpeg1audio_frame) {
    205   DCHECK_EQ(mpeg1audio_frame.queue_offset, es_queue_->head());
    206   es_queue_->Pop(mpeg1audio_frame.size);
    207 }
    208 
    209 }  // namespace mp2t
    210 }  // namespace media
    211