Cleanup media BitReader ReadBits() calls

Initialize temporary values, check return values.
Small tweaks to solution proposed by adtolbar@microsoft.com.

Bug: 929962
Change-Id: Iaa7da7534174882d040ec7e4c353ba5cd0da5735
Reviewed-on: https://chromium-review.googlesource.com/c/1481085
Commit-Queue: Chrome Cunningham <chcunningham@chromium.org>
Reviewed-by: Dan Sanders <sandersd@chromium.org>
Cr-Commit-Position: refs/heads/master@{#634889}
static bool CheckMpeg2ProgramStream(const uint8_t* buffer, int buffer_size) {
  RCHECK(buffer_size > 14);

  int offset = 0;
  while (offset + 14 < buffer_size) {
    BitReader reader(buffer + offset, 14);

    RCHECK(ReadBits(&reader, 24) == 1);
    RCHECK(ReadBits(&reader, 8) == PACK_START_CODE);

    int mpeg_version = ReadBits(&reader, 2);
    if (mpeg_version == 0) {
      RCHECK(ReadBits(&reader, 2) == 2);
    } else {
      RCHECK(mpeg_version == 1);
    }

    reader.SkipBits(3);

    RCHECK(ReadBits(&reader, 1) == 1);

    reader.SkipBits(15);

    RCHECK(ReadBits(&reader, 1) == 1);

    reader.SkipBits(15);

    RCHECK(ReadBits(&reader, 1) == 1);

    if (mpeg_version == 0) {
      RCHECK(ReadBits(&reader, 1) == 1);

      reader.SkipBits(22);

      RCHECK(ReadBits(&reader, 1) == 1);

      offset += 12;
    } else {
      reader.SkipBits(22);

      RCHECK(ReadBits(&reader, 2) == 3);

      reader.SkipBits(5);

      int pack_stuffing_length = ReadBits(&reader, 3);
      offset += 14 + pack_stuffing_length;
    }

    while (offset + 6 < buffer_size && Read24(buffer + offset) == 1) {
      int stream_id = buffer[offset + 3];

      if (mpeg_version == 0)
        RCHECK(stream_id != 0xbc && stream_id < 0xf0);
      else
        RCHECK(stream_id != 0xfc && stream_id != 0xfd && stream_id != 0xfe);

      if (stream_id == PACK_START_CODE)  // back to outer loop.
        break;
      if (stream_id == PROGRAM_END_CODE)  // end of stream.
        return true;

      int pes_length = Read16(buffer + offset + 4);
      RCHECK(pes_length > 0);
      offset = offset + 6 + pes_length;
    }
  }
  return true;
}

static bool CheckMpeg2ProgramStream(const uint8_t* buffer, int buffer_size) {
  RCHECK(buffer_size > 14);

  int offset = 0;
  while (offset + 14 < buffer_size) {
    BitReader reader(buffer + offset, 14);

    RCHECK(ReadBits(&reader, 24) == 1);
    RCHECK(ReadBits(&reader, 8) == PACK_START_CODE);

    int mpeg_version = ReadBits(&reader, 2);
    if (mpeg_version == 0) {
      RCHECK(ReadBits(&reader, 2) == 2);
    } else {
      RCHECK(mpeg_version == 1);
    }

    reader.SkipBits(3);

    RCHECK(ReadBits(&reader, 1) == 1);

    reader.SkipBits(15);

    RCHECK(ReadBits(&reader, 1) == 1);

    reader.SkipBits(15);

    RCHECK(ReadBits(&reader, 1) == 1);

    if (mpeg_version == 0) {
      RCHECK(ReadBits(&reader, 1) == 1);

      reader.SkipBits(22);

      RCHECK(ReadBits(&reader, 1) == 1);

      offset += 12;
    } else {
      reader.SkipBits(22);

      RCHECK(ReadBits(&reader, 2) == 3);

      reader.SkipBits(5);

      int pack_stuffing_length = ReadBits(&reader, 3);
      offset += 14 + pack_stuffing_length;
    }

    while (offset + 6 < buffer_size && Read24(buffer + offset) == 1) {
      int stream_id = buffer[offset + 3];

      if (mpeg_version == 0)
        RCHECK(stream_id != 0xbc && stream_id < 0xf0);
      else
        RCHECK(stream_id != 0xfc && stream_id != 0xfd && stream_id != 0xfe);

      if (stream_id == PACK_START_CODE)  // back to outer loop.
        break;
      if (stream_id == PROGRAM_END_CODE)  // end of stream.
        return true;

      int pes_length = Read16(buffer + offset + 4);
      RCHECK(pes_length > 0);
      offset = offset + 6 + pes_length;
    }
  }
  return true;
}
