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/ts_section_pmt.h"
      6 
      7 #include <map>
      8 
      9 #include "base/logging.h"
     10 #include "media/base/bit_reader.h"
     11 #include "media/formats/mp2t/mp2t_common.h"
     12 
     13 namespace media {
     14 namespace mp2t {
     15 
     16 TsSectionPmt::TsSectionPmt(const RegisterPesCb& register_pes_cb)
     17     : register_pes_cb_(register_pes_cb) {
     18 }
     19 
     20 TsSectionPmt::~TsSectionPmt() {
     21 }
     22 
     23 bool TsSectionPmt::ParsePsiSection(BitReader* bit_reader) {
     24   // Read up to |last_section_number|.
     25   int table_id;
     26   int section_syntax_indicator;
     27   int dummy_zero;
     28   int reserved;
     29   int section_length;
     30   int program_number;
     31   int version_number;
     32   int current_next_indicator;
     33   int section_number;
     34   int last_section_number;
     35   RCHECK(bit_reader->ReadBits(8, &table_id));
     36   RCHECK(bit_reader->ReadBits(1, &section_syntax_indicator));
     37   RCHECK(bit_reader->ReadBits(1, &dummy_zero));
     38   RCHECK(bit_reader->ReadBits(2, &reserved));
     39   RCHECK(bit_reader->ReadBits(12, &section_length));
     40   int section_start_marker = bit_reader->bits_available() / 8;
     41 
     42   RCHECK(bit_reader->ReadBits(16, &program_number));
     43   RCHECK(bit_reader->ReadBits(2, &reserved));
     44   RCHECK(bit_reader->ReadBits(5, &version_number));
     45   RCHECK(bit_reader->ReadBits(1, &current_next_indicator));
     46   RCHECK(bit_reader->ReadBits(8, &section_number));
     47   RCHECK(bit_reader->ReadBits(8, &last_section_number));
     48 
     49   // Perform a few verifications:
     50   // - table ID should be 2 for a PMT.
     51   // - section_syntax_indicator should be one.
     52   // - section length should not exceed 1021.
     53   RCHECK(table_id == 0x2);
     54   RCHECK(section_syntax_indicator);
     55   RCHECK(!dummy_zero);
     56   RCHECK(section_length <= 1021);
     57   RCHECK(section_number == 0);
     58   RCHECK(last_section_number == 0);
     59 
     60   // TODO(damienv):
     61   // Verify that there is no mismatch between the program number
     62   // and the program number that was provided in a PAT for the current PMT.
     63 
     64   // Read the end of the fixed length section.
     65   int pcr_pid;
     66   int program_info_length;
     67   RCHECK(bit_reader->ReadBits(3, &reserved));
     68   RCHECK(bit_reader->ReadBits(13, &pcr_pid));
     69   RCHECK(bit_reader->ReadBits(4, &reserved));
     70   RCHECK(bit_reader->ReadBits(12, &program_info_length));
     71   RCHECK(program_info_length < 1024);
     72 
     73   // Read the program info descriptor.
     74   // TODO(damienv): check wether any of the descriptors could be useful.
     75   // Defined in section 2.6 of ISO-13818.
     76   RCHECK(bit_reader->SkipBits(8 * program_info_length));
     77 
     78   // Read the ES description table.
     79   // The end of the PID map if 4 bytes away from the end of the section
     80   // (4 bytes = size of the CRC).
     81   int pid_map_end_marker = section_start_marker - section_length + 4;
     82   std::map<int, int> pid_map;
     83   while (bit_reader->bits_available() > 8 * pid_map_end_marker) {
     84     int stream_type;
     85     int reserved;
     86     int pid_es;
     87     int es_info_length;
     88     RCHECK(bit_reader->ReadBits(8, &stream_type));
     89     RCHECK(bit_reader->ReadBits(3, &reserved));
     90     RCHECK(bit_reader->ReadBits(13, &pid_es));
     91     RCHECK(bit_reader->ReadBits(4, &reserved));
     92     RCHECK(bit_reader->ReadBits(12, &es_info_length));
     93 
     94     // Do not register the PID right away.
     95     // Wait for the end of the section to be fully parsed
     96     // to make sure there is no error.
     97     pid_map.insert(std::pair<int, int>(pid_es, stream_type));
     98 
     99     // Read the ES info descriptors.
    100     // TODO(damienv): check wether any of the descriptors could be useful.
    101     // Defined in section 2.6 of ISO-13818.
    102     RCHECK(bit_reader->SkipBits(8 * es_info_length));
    103   }
    104 
    105   // Read the CRC.
    106   int crc32;
    107   RCHECK(bit_reader->ReadBits(32, &crc32));
    108 
    109   // Once the PMT has been proved to be correct, register the PIDs.
    110   for (std::map<int, int>::iterator it = pid_map.begin();
    111        it != pid_map.end(); ++it)
    112     register_pes_cb_.Run(it->first, it->second);
    113 
    114   return true;
    115 }
    116 
    117 void TsSectionPmt::ResetPsiSection() {
    118 }
    119 
    120 }  // namespace mp2t
    121 }  // namespace media
    122 
    123